NASM 2.06rc1
[nasm/avx512.git] / output / outelf32.c
blob0f9ba10bf0853edb46bcdb23a75a1331ca14eaf1
1 /* outelf.c output routines for the Netwide Assembler to produce
2 * ELF32 (i386 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "saa.h"
21 #include "raa.h"
22 #include "stdscan.h"
23 #include "outform.h"
24 #include "outlib.h"
25 #include "rbtree.h"
27 #ifdef OF_ELF32
30 * Relocation types.
32 enum reloc_type {
33 R_386_32 = 1, /* ordinary absolute relocation */
34 R_386_PC32 = 2, /* PC-relative relocation */
35 R_386_GOT32 = 3, /* an offset into GOT */
36 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
37 R_386_COPY = 5, /* ??? */
38 R_386_GLOB_DAT = 6, /* ??? */
39 R_386_JUMP_SLOT = 7, /* ??? */
40 R_386_RELATIVE = 8, /* ??? */
41 R_386_GOTOFF = 9, /* an offset from GOT base */
42 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
43 R_386_TLS_TPOFF = 14, /* Offset in static TLS block */
44 R_386_TLS_IE = 15, /* Address of GOT entry for static TLS
45 block offset */
46 /* These are GNU extensions, but useful */
47 R_386_16 = 20, /* A 16-bit absolute relocation */
48 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
49 R_386_8 = 22, /* An 8-bit absolute relocation */
50 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
53 struct Reloc {
54 struct Reloc *next;
55 int32_t address; /* relative to _start_ of section */
56 int32_t symbol; /* symbol index */
57 int type; /* type of relocation */
60 struct Symbol {
61 struct rbtree symv; /* symbol value and symbol rbtree */
62 int32_t strpos; /* string table position of name */
63 int32_t section; /* section ID of the symbol */
64 int type; /* symbol type */
65 int other; /* symbol visibility */
66 int32_t size; /* size of symbol */
67 int32_t globnum; /* symbol table offset if global */
68 struct Symbol *nextfwd; /* list of unresolved-size symbols */
69 char *name; /* used temporarily if in above list */
72 #define SHT_PROGBITS 1
73 #define SHT_NOBITS 8
75 #define SHF_WRITE 1
76 #define SHF_ALLOC 2
77 #define SHF_EXECINSTR 4
78 #define SHF_TLS (1 << 10) /* Section holds thread-local data. */
80 struct Section {
81 struct SAA *data;
82 uint32_t len, size, nrelocs;
83 int32_t index;
84 int type; /* SHT_PROGBITS or SHT_NOBITS */
85 int align; /* alignment: power of two */
86 uint32_t flags; /* section flags */
87 char *name;
88 struct SAA *rel;
89 int32_t rellen;
90 struct Reloc *head, **tail;
91 struct rbtree *gsyms; /* global symbols in section */
94 #define SECT_DELTA 32
95 static struct Section **sects;
96 static int nsects, sectlen;
98 #define SHSTR_DELTA 256
99 static char *shstrtab;
100 static int shstrtablen, shstrtabsize;
102 static struct SAA *syms;
103 static uint32_t nlocals, nglobs;
105 static int32_t def_seg;
107 static struct RAA *bsym;
109 static struct SAA *strs;
110 static uint32_t strslen;
112 static FILE *elffp;
113 static efunc error;
114 static evalfunc evaluate;
116 static struct Symbol *fwds;
118 static char elf_module[FILENAME_MAX];
120 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
121 static uint8_t elf_abiver = 0; /* Current ABI version */
123 extern struct ofmt of_elf32;
124 extern struct ofmt of_elf;
126 #define SHN_ABS 0xFFF1
127 #define SHN_COMMON 0xFFF2
128 #define SHN_UNDEF 0
130 #define SYM_GLOBAL 0x10
132 #define SHT_RELA 4 /* Relocation entries with addends */
134 #define STT_NOTYPE 0 /* Symbol type is unspecified */
135 #define STT_OBJECT 1 /* Symbol is a data object */
136 #define STT_FUNC 2 /* Symbol is a code object */
137 #define STT_SECTION 3 /* Symbol associated with a section */
138 #define STT_FILE 4 /* Symbol's name is file name */
139 #define STT_COMMON 5 /* Symbol is a common data object */
140 #define STT_TLS 6 /* Symbol is thread-local data object*/
141 #define STT_NUM 7 /* Number of defined types. */
143 #define STV_DEFAULT 0
144 #define STV_INTERNAL 1
145 #define STV_HIDDEN 2
146 #define STV_PROTECTED 3
148 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
150 #define SEG_ALIGN 16 /* alignment of sections in file */
151 #define SEG_ALIGN_1 (SEG_ALIGN-1)
153 /* Definitions in lieu of dwarf.h */
154 #define DW_TAG_compile_unit 0x11
155 #define DW_TAG_subprogram 0x2e
156 #define DW_AT_name 0x03
157 #define DW_AT_stmt_list 0x10
158 #define DW_AT_low_pc 0x11
159 #define DW_AT_high_pc 0x12
160 #define DW_AT_language 0x13
161 #define DW_AT_producer 0x25
162 #define DW_AT_frame_base 0x40
163 #define DW_FORM_addr 0x01
164 #define DW_FORM_data2 0x05
165 #define DW_FORM_data4 0x06
166 #define DW_FORM_string 0x08
167 #define DW_LNS_extended_op 0
168 #define DW_LNS_advance_pc 2
169 #define DW_LNS_advance_line 3
170 #define DW_LNS_set_file 4
171 #define DW_LNE_end_sequence 1
172 #define DW_LNE_set_address 2
173 #define DW_LNE_define_file 3
174 #define DW_LANG_Mips_Assembler 0x8001
176 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
178 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
180 static struct ELF_SECTDATA {
181 void *data;
182 int32_t len;
183 bool is_saa;
184 } *elf_sects;
185 static int elf_nsect, nsections;
186 static int32_t elf_foffs;
188 static void elf_write(void);
189 static void elf_sect_write(struct Section *, const uint8_t *,
190 uint32_t);
191 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
192 int, int);
193 static void elf_write_sections(void);
194 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
195 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
196 static void add_sectname(char *, char *);
198 /* this stuff is needed for the stabs debugging format */
199 #define N_SO 0x64 /* ID for main source file */
200 #define N_SOL 0x84 /* ID for sub-source file */
201 #define N_BINCL 0x82
202 #define N_EINCL 0xA2
203 #define N_SLINE 0x44
204 #define TY_STABSSYMLIN 0x40 /* ouch */
206 struct stabentry {
207 uint32_t n_strx;
208 uint8_t n_type;
209 uint8_t n_other;
210 uint16_t n_desc;
211 uint32_t n_value;
214 struct erel {
215 int offset, info;
218 struct symlininfo {
219 int offset;
220 int section; /* section index */
221 char *name; /* shallow-copied pointer of section name */
224 struct linelist {
225 struct symlininfo info;
226 int line;
227 char *filename;
228 struct linelist *next;
229 struct linelist *last;
232 struct sectlist {
233 struct SAA *psaa;
234 int section;
235 int line;
236 int offset;
237 int file;
238 struct sectlist *next;
239 struct sectlist *last;
242 /* common debug variables */
243 static int currentline = 1;
244 static int debug_immcall = 0;
246 /* stabs debug variables */
247 static struct linelist *stabslines = 0;
248 static int numlinestabs = 0;
249 static char *stabs_filename = 0;
250 static int symtabsection;
251 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
252 static int stablen, stabstrlen, stabrellen;
254 /* dwarf debug variables */
255 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
256 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
257 static int dwarf_numfiles = 0, dwarf_nsections;
258 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
259 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
260 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
261 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
262 abbrevlen, linelen, linerellen, framelen, loclen;
263 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
265 static struct dfmt df_dwarf;
266 static struct dfmt df_stabs;
267 static struct Symbol *lastsym;
269 /* common debugging routines */
270 void debug32_typevalue(int32_t);
271 void debug32_init(struct ofmt *, void *, FILE *, efunc);
272 void debug32_deflabel(char *, int32_t, int64_t, int, char *);
273 void debug32_directive(const char *, const char *);
275 /* stabs debugging routines */
276 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
277 void stabs32_output(int, void *);
278 void stabs32_generate(void);
279 void stabs32_cleanup(void);
281 /* dwarf debugging routines */
282 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
283 void dwarf32_output(int, void *);
284 void dwarf32_generate(void);
285 void dwarf32_cleanup(void);
286 void dwarf32_findfile(const char *);
287 void dwarf32_findsect(const int);
288 void saa_wleb128u(struct SAA *, int);
289 void saa_wleb128s(struct SAA *, int);
292 * Special section numbers which are used to define ELF special
293 * symbols, which can be used with WRT to provide PIC and TLS
294 * relocation types.
296 static int32_t elf_gotpc_sect, elf_gotoff_sect;
297 static int32_t elf_got_sect, elf_plt_sect;
298 static int32_t elf_sym_sect, elf_tlsie_sect;
300 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
302 elffp = fp;
303 error = errfunc;
304 evaluate = eval;
305 (void)ldef; /* placate optimisers */
306 sects = NULL;
307 nsects = sectlen = 0;
308 syms = saa_init((int32_t)sizeof(struct Symbol));
309 nlocals = nglobs = 0;
310 bsym = raa_init();
311 strs = saa_init(1L);
312 saa_wbytes(strs, "\0", 1L);
313 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
314 strslen = 2 + strlen(elf_module);
315 shstrtab = NULL;
316 shstrtablen = shstrtabsize = 0;;
317 add_sectname("", "");
319 fwds = NULL;
321 elf_gotpc_sect = seg_alloc();
322 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
323 error);
324 elf_gotoff_sect = seg_alloc();
325 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
326 error);
327 elf_got_sect = seg_alloc();
328 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
329 error);
330 elf_plt_sect = seg_alloc();
331 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
332 error);
333 elf_sym_sect = seg_alloc();
334 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
335 error);
336 elf_tlsie_sect = seg_alloc();
337 ldef("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false, &of_elf32,
338 error);
340 def_seg = seg_alloc();
343 static void elf_init_hack(FILE * fp, efunc errfunc, ldfunc ldef,
344 evalfunc eval)
346 of_elf32.current_dfmt = of_elf.current_dfmt; /* Sync debugging format */
347 elf_init(fp, errfunc, ldef, eval);
350 static void elf_cleanup(int debuginfo)
352 struct Reloc *r;
353 int i;
355 (void)debuginfo;
357 elf_write();
358 fclose(elffp);
359 for (i = 0; i < nsects; i++) {
360 if (sects[i]->type != SHT_NOBITS)
361 saa_free(sects[i]->data);
362 if (sects[i]->head)
363 saa_free(sects[i]->rel);
364 while (sects[i]->head) {
365 r = sects[i]->head;
366 sects[i]->head = sects[i]->head->next;
367 nasm_free(r);
370 nasm_free(sects);
371 saa_free(syms);
372 raa_free(bsym);
373 saa_free(strs);
374 if (of_elf32.current_dfmt) {
375 of_elf32.current_dfmt->cleanup();
379 static void add_sectname(char *firsthalf, char *secondhalf)
381 int len = strlen(firsthalf) + strlen(secondhalf);
382 while (shstrtablen + len + 1 > shstrtabsize)
383 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
384 strcpy(shstrtab + shstrtablen, firsthalf);
385 strcat(shstrtab + shstrtablen, secondhalf);
386 shstrtablen += len + 1;
389 static int elf_make_section(char *name, int type, int flags, int align)
391 struct Section *s;
393 s = nasm_malloc(sizeof(*s));
395 if (type != SHT_NOBITS)
396 s->data = saa_init(1L);
397 s->head = NULL;
398 s->tail = &s->head;
399 s->len = s->size = 0;
400 s->nrelocs = 0;
401 if (!strcmp(name, ".text"))
402 s->index = def_seg;
403 else
404 s->index = seg_alloc();
405 add_sectname("", name);
406 s->name = nasm_malloc(1 + strlen(name));
407 strcpy(s->name, name);
408 s->type = type;
409 s->flags = flags;
410 s->align = align;
411 s->gsyms = NULL;
413 if (nsects >= sectlen)
414 sects =
415 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
416 sects[nsects++] = s;
418 return nsects - 1;
421 static int32_t elf_section_names(char *name, int pass, int *bits)
423 char *p;
424 unsigned flags_and, flags_or;
425 int type, align, i;
428 * Default is 32 bits.
430 if (!name) {
431 *bits = 32;
432 return def_seg;
435 p = name;
436 while (*p && !nasm_isspace(*p))
437 p++;
438 if (*p)
439 *p++ = '\0';
440 flags_and = flags_or = type = align = 0;
442 while (*p && nasm_isspace(*p))
443 p++;
444 while (*p) {
445 char *q = p;
446 while (*p && !nasm_isspace(*p))
447 p++;
448 if (*p)
449 *p++ = '\0';
450 while (*p && nasm_isspace(*p))
451 p++;
453 if (!nasm_strnicmp(q, "align=", 6)) {
454 align = atoi(q + 6);
455 if (align == 0)
456 align = 1;
457 if ((align - 1) & align) { /* means it's not a power of two */
458 error(ERR_NONFATAL, "section alignment %d is not"
459 " a power of two", align);
460 align = 1;
462 } else if (!nasm_stricmp(q, "alloc")) {
463 flags_and |= SHF_ALLOC;
464 flags_or |= SHF_ALLOC;
465 } else if (!nasm_stricmp(q, "noalloc")) {
466 flags_and |= SHF_ALLOC;
467 flags_or &= ~SHF_ALLOC;
468 } else if (!nasm_stricmp(q, "exec")) {
469 flags_and |= SHF_EXECINSTR;
470 flags_or |= SHF_EXECINSTR;
471 } else if (!nasm_stricmp(q, "noexec")) {
472 flags_and |= SHF_EXECINSTR;
473 flags_or &= ~SHF_EXECINSTR;
474 } else if (!nasm_stricmp(q, "write")) {
475 flags_and |= SHF_WRITE;
476 flags_or |= SHF_WRITE;
477 } else if (!nasm_stricmp(q, "tls")) {
478 flags_and |= SHF_TLS;
479 flags_or |= SHF_TLS;
480 } else if (!nasm_stricmp(q, "nowrite")) {
481 flags_and |= SHF_WRITE;
482 flags_or &= ~SHF_WRITE;
483 } else if (!nasm_stricmp(q, "progbits")) {
484 type = SHT_PROGBITS;
485 } else if (!nasm_stricmp(q, "nobits")) {
486 type = SHT_NOBITS;
490 if (!strcmp(name, ".comment") ||
491 !strcmp(name, ".shstrtab") ||
492 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
493 error(ERR_NONFATAL, "attempt to redefine reserved section"
494 "name `%s'", name);
495 return NO_SEG;
498 for (i = 0; i < nsects; i++)
499 if (!strcmp(name, sects[i]->name))
500 break;
501 if (i == nsects) {
502 if (!strcmp(name, ".text"))
503 i = elf_make_section(name, SHT_PROGBITS,
504 SHF_ALLOC | SHF_EXECINSTR, 16);
505 else if (!strcmp(name, ".rodata"))
506 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
507 else if (!strcmp(name, ".data"))
508 i = elf_make_section(name, SHT_PROGBITS,
509 SHF_ALLOC | SHF_WRITE, 4);
510 else if (!strcmp(name, ".bss"))
511 i = elf_make_section(name, SHT_NOBITS,
512 SHF_ALLOC | SHF_WRITE, 4);
513 else if (!strcmp(name, ".tdata"))
514 i = elf_make_section(name, SHT_PROGBITS,
515 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
516 else if (!strcmp(name, ".tbss"))
517 i = elf_make_section(name, SHT_NOBITS,
518 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
519 else
520 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
521 if (type)
522 sects[i]->type = type;
523 if (align)
524 sects[i]->align = align;
525 sects[i]->flags &= ~flags_and;
526 sects[i]->flags |= flags_or;
527 } else if (pass == 1) {
528 if ((type && sects[i]->type != type)
529 || (align && sects[i]->align != align)
530 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
531 error(ERR_WARNING, "section attributes ignored on"
532 " redeclaration of section `%s'", name);
535 return sects[i]->index;
538 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
539 int is_global, char *special)
541 int pos = strslen;
542 struct Symbol *sym;
543 bool special_used = false;
545 #if defined(DEBUG) && DEBUG>2
546 fprintf(stderr,
547 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
548 name, segment, offset, is_global, special);
549 #endif
550 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
552 * This is a NASM special symbol. We never allow it into
553 * the ELF symbol table, even if it's a valid one. If it
554 * _isn't_ a valid one, we should barf immediately.
556 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
557 strcmp(name, "..got") && strcmp(name, "..plt") &&
558 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
559 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
560 return;
563 if (is_global == 3) {
564 struct Symbol **s;
566 * Fix up a forward-reference symbol size from the first
567 * pass.
569 for (s = &fwds; *s; s = &(*s)->nextfwd)
570 if (!strcmp((*s)->name, name)) {
571 struct tokenval tokval;
572 expr *e;
573 char *p = special;
575 while (*p && !nasm_isspace(*p))
576 p++;
577 while (*p && nasm_isspace(*p))
578 p++;
579 stdscan_reset();
580 stdscan_bufptr = p;
581 tokval.t_type = TOKEN_INVALID;
582 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
583 if (e) {
584 if (!is_simple(e))
585 error(ERR_NONFATAL, "cannot use relocatable"
586 " expression as symbol size");
587 else
588 (*s)->size = reloc_value(e);
592 * Remove it from the list of unresolved sizes.
594 nasm_free((*s)->name);
595 *s = (*s)->nextfwd;
596 return;
598 return; /* it wasn't an important one */
601 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
602 strslen += 1 + strlen(name);
604 lastsym = sym = saa_wstruct(syms);
606 sym->strpos = pos;
607 sym->type = is_global ? SYM_GLOBAL : 0;
608 sym->other = STV_DEFAULT;
609 sym->size = 0;
610 if (segment == NO_SEG)
611 sym->section = SHN_ABS;
612 else {
613 int i;
614 sym->section = SHN_UNDEF;
615 if (nsects == 0 && segment == def_seg) {
616 int tempint;
617 if (segment != elf_section_names(".text", 2, &tempint))
618 error(ERR_PANIC,
619 "strange segment conditions in ELF driver");
620 sym->section = nsects;
621 } else {
622 for (i = 0; i < nsects; i++)
623 if (segment == sects[i]->index) {
624 sym->section = i + 1;
625 break;
630 if (is_global == 2) {
631 sym->size = offset;
632 sym->symv.key = 0;
633 sym->section = SHN_COMMON;
635 * We have a common variable. Check the special text to see
636 * if it's a valid number and power of two; if so, store it
637 * as the alignment for the common variable.
639 if (special) {
640 bool err;
641 sym->symv.key = readnum(special, &err);
642 if (err)
643 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
644 " valid number", special);
645 else if ((sym->symv.key | (sym->symv.key - 1))
646 != 2 * sym->symv.key - 1)
647 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
648 " power of two", special);
650 special_used = true;
651 } else
652 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
654 if (sym->type == SYM_GLOBAL) {
656 * If sym->section == SHN_ABS, then the first line of the
657 * else section would cause a core dump, because its a reference
658 * beyond the end of the section array.
659 * This behaviour is exhibited by this code:
660 * GLOBAL crash_nasm
661 * crash_nasm equ 0
662 * To avoid such a crash, such requests are silently discarded.
663 * This may not be the best solution.
665 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
666 bsym = raa_write(bsym, segment, nglobs);
667 } else if (sym->section != SHN_ABS) {
669 * This is a global symbol; so we must add it to the rbtree
670 * of global symbols in its section.
672 * In addition, we check the special text for symbol
673 * type and size information.
675 sects[sym->section-1]->gsyms =
676 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
678 if (special) {
679 int n = strcspn(special, " \t");
681 if (!nasm_strnicmp(special, "function", n))
682 sym->type |= STT_FUNC;
683 else if (!nasm_strnicmp(special, "data", n) ||
684 !nasm_strnicmp(special, "object", n))
685 sym->type |= STT_OBJECT;
686 else if (!nasm_strnicmp(special, "notype", n))
687 sym->type |= STT_NOTYPE;
688 else
689 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
690 n, special);
691 special += n;
693 while (nasm_isspace(*special))
694 ++special;
695 if (*special) {
696 n = strcspn(special, " \t");
697 if (!nasm_strnicmp(special, "default", n))
698 sym->other = STV_DEFAULT;
699 else if (!nasm_strnicmp(special, "internal", n))
700 sym->other = STV_INTERNAL;
701 else if (!nasm_strnicmp(special, "hidden", n))
702 sym->other = STV_HIDDEN;
703 else if (!nasm_strnicmp(special, "protected", n))
704 sym->other = STV_PROTECTED;
705 else
706 n = 0;
707 special += n;
710 if (*special) {
711 struct tokenval tokval;
712 expr *e;
713 int fwd = 0;
714 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
716 while (special[n] && nasm_isspace(special[n]))
717 n++;
719 * We have a size expression; attempt to
720 * evaluate it.
722 stdscan_reset();
723 stdscan_bufptr = special + n;
724 tokval.t_type = TOKEN_INVALID;
725 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
726 NULL);
727 if (fwd) {
728 sym->nextfwd = fwds;
729 fwds = sym;
730 sym->name = nasm_strdup(name);
731 } else if (e) {
732 if (!is_simple(e))
733 error(ERR_NONFATAL, "cannot use relocatable"
734 " expression as symbol size");
735 else
736 sym->size = reloc_value(e);
738 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
740 special_used = true;
743 * If TLS segment, mark symbol accordingly.
745 if (sects[sym->section - 1]->flags & SHF_TLS) {
746 sym->type &= 0xf0;
747 sym->type |= STT_TLS;
750 sym->globnum = nglobs;
751 nglobs++;
752 } else
753 nlocals++;
755 if (special && !special_used)
756 error(ERR_NONFATAL, "no special symbol features supported here");
759 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
761 struct Reloc *r;
763 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
764 sect->tail = &r->next;
765 r->next = NULL;
767 r->address = sect->len;
768 if (segment == NO_SEG)
769 r->symbol = 0;
770 else {
771 int i;
772 r->symbol = 0;
773 for (i = 0; i < nsects; i++)
774 if (segment == sects[i]->index)
775 r->symbol = i + 2;
776 if (!r->symbol)
777 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
779 r->type = type;
781 sect->nrelocs++;
785 * This routine deals with ..got and ..sym relocations: the more
786 * complicated kinds. In shared-library writing, some relocations
787 * with respect to global symbols must refer to the precise symbol
788 * rather than referring to an offset from the base of the section
789 * _containing_ the symbol. Such relocations call to this routine,
790 * which searches the symbol list for the symbol in question.
792 * R_386_GOT32 references require the _exact_ symbol address to be
793 * used; R_386_32 references can be at an offset from the symbol.
794 * The boolean argument `exact' tells us this.
796 * Return value is the adjusted value of `addr', having become an
797 * offset from the symbol rather than the section. Should always be
798 * zero when returning from an exact call.
800 * Limitation: if you define two symbols at the same place,
801 * confusion will occur.
803 * Inefficiency: we search, currently, using a linked list which
804 * isn't even necessarily sorted.
806 static int32_t elf_add_gsym_reloc(struct Section *sect,
807 int32_t segment, uint32_t offset,
808 int type, bool exact)
810 struct Reloc *r;
811 struct Section *s;
812 struct Symbol *sym;
813 struct rbtree *srb;
814 int i;
817 * First look up the segment/offset pair and find a global
818 * symbol corresponding to it. If it's not one of our segments,
819 * then it must be an external symbol, in which case we're fine
820 * doing a normal elf_add_reloc after first sanity-checking
821 * that the offset from the symbol is zero.
823 s = NULL;
824 for (i = 0; i < nsects; i++)
825 if (segment == sects[i]->index) {
826 s = sects[i];
827 break;
829 if (!s) {
830 if (exact && offset != 0)
831 error(ERR_NONFATAL, "unable to find a suitable global symbol"
832 " for this reference");
833 else
834 elf_add_reloc(sect, segment, type);
835 return offset;
838 srb = rb_search(s->gsyms, offset);
839 if (!srb || (exact && srb->key != offset)) {
840 error(ERR_NONFATAL, "unable to find a suitable global symbol"
841 " for this reference");
842 return 0;
844 sym = container_of(srb, struct Symbol, symv);
846 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
847 sect->tail = &r->next;
848 r->next = NULL;
850 r->address = sect->len;
851 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
852 r->type = type;
854 sect->nrelocs++;
856 return offset - sym->symv.key;
859 static void elf_out(int32_t segto, const void *data,
860 enum out_type type, uint64_t size,
861 int32_t segment, int32_t wrt)
863 struct Section *s;
864 int32_t addr;
865 uint8_t mydata[4], *p;
866 int i;
867 static struct symlininfo sinfo;
870 * handle absolute-assembly (structure definitions)
872 if (segto == NO_SEG) {
873 if (type != OUT_RESERVE)
874 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
875 " space");
876 return;
879 s = NULL;
880 for (i = 0; i < nsects; i++)
881 if (segto == sects[i]->index) {
882 s = sects[i];
883 break;
885 if (!s) {
886 int tempint; /* ignored */
887 if (segto != elf_section_names(".text", 2, &tempint))
888 error(ERR_PANIC, "strange segment conditions in ELF driver");
889 else {
890 s = sects[nsects - 1];
891 i = nsects - 1;
895 /* again some stabs debugging stuff */
896 if (of_elf32.current_dfmt) {
897 sinfo.offset = s->len;
898 sinfo.section = i;
899 sinfo.name = s->name;
900 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
902 /* end of debugging stuff */
904 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
905 error(ERR_WARNING, "attempt to initialize memory in"
906 " BSS section `%s': ignored", s->name);
907 s->len += realsize(type, size);
908 return;
911 if (type == OUT_RESERVE) {
912 if (s->type == SHT_PROGBITS) {
913 error(ERR_WARNING, "uninitialized space declared in"
914 " non-BSS section `%s': zeroing", s->name);
915 elf_sect_write(s, NULL, size);
916 } else
917 s->len += size;
918 } else if (type == OUT_RAWDATA) {
919 if (segment != NO_SEG)
920 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
921 elf_sect_write(s, data, size);
922 } else if (type == OUT_ADDRESS) {
923 bool gnu16 = false;
924 addr = *(int64_t *)data;
925 if (segment != NO_SEG) {
926 if (segment % 2) {
927 error(ERR_NONFATAL, "ELF format does not support"
928 " segment base references");
929 } else {
930 if (wrt == NO_SEG) {
931 if (size == 2) {
932 gnu16 = true;
933 elf_add_reloc(s, segment, R_386_16);
934 } else {
935 elf_add_reloc(s, segment, R_386_32);
937 } else if (wrt == elf_gotpc_sect + 1) {
939 * The user will supply GOT relative to $$. ELF
940 * will let us have GOT relative to $. So we
941 * need to fix up the data item by $-$$.
943 addr += s->len;
944 elf_add_reloc(s, segment, R_386_GOTPC);
945 } else if (wrt == elf_gotoff_sect + 1) {
946 elf_add_reloc(s, segment, R_386_GOTOFF);
947 } else if (wrt == elf_tlsie_sect + 1) {
948 addr = elf_add_gsym_reloc(s, segment, addr,
949 R_386_TLS_IE, true);
950 } else if (wrt == elf_got_sect + 1) {
951 addr = elf_add_gsym_reloc(s, segment, addr,
952 R_386_GOT32, true);
953 } else if (wrt == elf_sym_sect + 1) {
954 if (size == 2) {
955 gnu16 = true;
956 addr = elf_add_gsym_reloc(s, segment, addr,
957 R_386_16, false);
958 } else {
959 addr = elf_add_gsym_reloc(s, segment, addr,
960 R_386_32, false);
962 } else if (wrt == elf_plt_sect + 1) {
963 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
964 "relative PLT references");
965 } else {
966 error(ERR_NONFATAL, "ELF format does not support this"
967 " use of WRT");
968 wrt = NO_SEG; /* we can at least _try_ to continue */
972 p = mydata;
973 if (gnu16) {
974 error(ERR_WARNING | ERR_WARN_GNUELF,
975 "16-bit relocations in ELF is a GNU extension");
976 WRITESHORT(p, addr);
977 } else {
978 if (size != 4 && segment != NO_SEG) {
979 error(ERR_NONFATAL,
980 "Unsupported non-32-bit ELF relocation");
982 WRITELONG(p, addr);
984 elf_sect_write(s, mydata, size);
985 } else if (type == OUT_REL2ADR) {
986 if (segment == segto)
987 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
988 if (segment != NO_SEG && segment % 2) {
989 error(ERR_NONFATAL, "ELF format does not support"
990 " segment base references");
991 } else {
992 if (wrt == NO_SEG) {
993 error(ERR_WARNING | ERR_WARN_GNUELF,
994 "16-bit relocations in ELF is a GNU extension");
995 elf_add_reloc(s, segment, R_386_PC16);
996 } else {
997 error(ERR_NONFATAL,
998 "Unsupported non-32-bit ELF relocation");
1001 p = mydata;
1002 WRITESHORT(p, *(int64_t *)data - size);
1003 elf_sect_write(s, mydata, 2L);
1004 } else if (type == OUT_REL4ADR) {
1005 if (segment == segto)
1006 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1007 if (segment != NO_SEG && segment % 2) {
1008 error(ERR_NONFATAL, "ELF format does not support"
1009 " segment base references");
1010 } else {
1011 if (wrt == NO_SEG) {
1012 elf_add_reloc(s, segment, R_386_PC32);
1013 } else if (wrt == elf_plt_sect + 1) {
1014 elf_add_reloc(s, segment, R_386_PLT32);
1015 } else if (wrt == elf_gotpc_sect + 1 ||
1016 wrt == elf_gotoff_sect + 1 ||
1017 wrt == elf_got_sect + 1) {
1018 error(ERR_NONFATAL, "ELF format cannot produce PC-"
1019 "relative GOT references");
1020 } else {
1021 error(ERR_NONFATAL, "ELF format does not support this"
1022 " use of WRT");
1023 wrt = NO_SEG; /* we can at least _try_ to continue */
1026 p = mydata;
1027 WRITELONG(p, *(int64_t *)data - size);
1028 elf_sect_write(s, mydata, 4L);
1032 static void elf_write(void)
1034 int align;
1035 int scount;
1036 char *p;
1037 int commlen;
1038 char comment[64];
1039 int i;
1041 struct SAA *symtab;
1042 int32_t symtablen, symtablocal;
1045 * Work out how many sections we will have. We have SHN_UNDEF,
1046 * then the flexible user sections, then the four fixed
1047 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1048 * then optionally relocation sections for the user sections.
1050 if (of_elf32.current_dfmt == &df_stabs)
1051 nsections = 8;
1052 else if (of_elf32.current_dfmt == &df_dwarf)
1053 nsections = 15;
1054 else
1055 nsections = 5; /* SHN_UNDEF and the fixed ones */
1057 add_sectname("", ".comment");
1058 add_sectname("", ".shstrtab");
1059 add_sectname("", ".symtab");
1060 add_sectname("", ".strtab");
1061 for (i = 0; i < nsects; i++) {
1062 nsections++; /* for the section itself */
1063 if (sects[i]->head) {
1064 nsections++; /* for its relocations */
1065 add_sectname(".rel", sects[i]->name);
1069 if (of_elf32.current_dfmt == &df_stabs) {
1070 /* in case the debug information is wanted, just add these three sections... */
1071 add_sectname("", ".stab");
1072 add_sectname("", ".stabstr");
1073 add_sectname(".rel", ".stab");
1076 else if (of_elf32.current_dfmt == &df_dwarf) {
1077 /* the dwarf debug standard specifies the following ten sections,
1078 not all of which are currently implemented,
1079 although all of them are defined. */
1080 #define debug_aranges (int32_t) (nsections-10)
1081 #define debug_info (int32_t) (nsections-7)
1082 #define debug_abbrev (int32_t) (nsections-5)
1083 #define debug_line (int32_t) (nsections-4)
1084 add_sectname("", ".debug_aranges");
1085 add_sectname(".rela", ".debug_aranges");
1086 add_sectname("", ".debug_pubnames");
1087 add_sectname("", ".debug_info");
1088 add_sectname(".rela", ".debug_info");
1089 add_sectname("", ".debug_abbrev");
1090 add_sectname("", ".debug_line");
1091 add_sectname(".rela", ".debug_line");
1092 add_sectname("", ".debug_frame");
1093 add_sectname("", ".debug_loc");
1097 * Do the comment.
1099 *comment = '\0';
1100 commlen = 2 + snprintf(comment+1, sizeof comment-1, "%s", nasm_comment);
1103 * Output the ELF header.
1105 fwrite("\177ELF\1\1\1", 7, 1, elffp);
1106 fputc(elf_osabi, elffp);
1107 fputc(elf_abiver, elffp);
1108 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1109 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1110 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1111 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1112 fwriteint32_t(0L, elffp); /* no entry point */
1113 fwriteint32_t(0L, elffp); /* no program header table */
1114 fwriteint32_t(0x40L, elffp); /* section headers straight after
1115 * ELF header plus alignment */
1116 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1117 fwriteint16_t(0x34, elffp); /* size of ELF header */
1118 fwriteint16_t(0, elffp); /* no program header table, again */
1119 fwriteint16_t(0, elffp); /* still no program header table */
1120 fwriteint16_t(0x28, elffp); /* size of section header */
1121 fwriteint16_t(nsections, elffp); /* number of sections */
1122 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1123 * section header table */
1124 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1125 fwriteint32_t(0L, elffp);
1126 fwriteint32_t(0L, elffp);
1129 * Build the symbol table and relocation tables.
1131 symtab = elf_build_symtab(&symtablen, &symtablocal);
1132 for (i = 0; i < nsects; i++)
1133 if (sects[i]->head)
1134 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1135 sects[i]->head);
1138 * Now output the section header table.
1141 elf_foffs = 0x40 + 0x28 * nsections;
1142 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1143 elf_foffs += align;
1144 elf_nsect = 0;
1145 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1147 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1148 scount = 1; /* needed for the stabs debugging to track the symtable section */
1149 p = shstrtab + 1;
1150 for (i = 0; i < nsects; i++) {
1151 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1152 (sects[i]->type == SHT_PROGBITS ?
1153 sects[i]->data : NULL), true,
1154 sects[i]->len, 0, 0, sects[i]->align, 0);
1155 p += strlen(p) + 1;
1156 scount++; /* dito */
1158 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1159 scount++; /* dito */
1160 p += strlen(p) + 1;
1161 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1162 scount++; /* dito */
1163 p += strlen(p) + 1;
1164 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1165 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1166 p += strlen(p) + 1;
1167 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1168 for (i = 0; i < nsects; i++)
1169 if (sects[i]->head) {
1170 p += strlen(p) + 1;
1171 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
1172 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1174 if (of_elf32.current_dfmt == &df_stabs) {
1175 /* for debugging information, create the last three sections
1176 which are the .stab , .stabstr and .rel.stab sections respectively */
1178 /* this function call creates the stab sections in memory */
1179 stabs32_generate();
1181 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1182 p += strlen(p) + 1;
1183 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1184 nsections - 2, 0, 4, 12);
1186 p += strlen(p) + 1;
1187 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1188 stabstrlen, 0, 0, 4, 0);
1190 p += strlen(p) + 1;
1191 /* link -> symtable info -> section to refer to */
1192 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1193 stabrellen, symtabsection, nsections - 3, 4,
1197 else if (of_elf32.current_dfmt == &df_dwarf) {
1198 /* for dwarf debugging information, create the ten dwarf sections */
1200 /* this function call creates the dwarf sections in memory */
1201 if (dwarf_fsect) dwarf32_generate();
1203 p += strlen(p) + 1;
1204 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1205 arangeslen, 0, 0, 1, 0);
1206 p += strlen(p) + 1;
1207 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1208 arangesrellen, symtabsection, debug_aranges, 1, 12);
1209 p += strlen(p) + 1;
1210 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1211 pubnameslen, 0, 0, 1, 0);
1212 p += strlen(p) + 1;
1213 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1214 infolen, 0, 0, 1, 0);
1215 p += strlen(p) + 1;
1216 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1217 inforellen, symtabsection, debug_info, 1, 12);
1218 p += strlen(p) + 1;
1219 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1220 abbrevlen, 0, 0, 1, 0);
1221 p += strlen(p) + 1;
1222 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1223 linelen, 0, 0, 1, 0);
1224 p += strlen(p) + 1;
1225 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1226 linerellen, symtabsection, debug_line, 1, 12);
1227 p += strlen(p) + 1;
1228 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1229 framelen, 0, 0, 8, 0);
1230 p += strlen(p) + 1;
1231 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1232 loclen, 0, 0, 1, 0);
1235 fwrite(align_str, align, 1, elffp);
1238 * Now output the sections.
1240 elf_write_sections();
1242 nasm_free(elf_sects);
1243 saa_free(symtab);
1246 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1248 struct SAA *s = saa_init(1L);
1249 struct Symbol *sym;
1250 uint8_t entry[16], *p;
1251 int i;
1253 *len = *local = 0;
1256 * First, an all-zeros entry, required by the ELF spec.
1258 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1259 *len += 16;
1260 (*local)++;
1263 * Next, an entry for the file name.
1265 p = entry;
1266 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1267 WRITELONG(p, 0); /* no value */
1268 WRITELONG(p, 0); /* no size either */
1269 WRITESHORT(p, STT_FILE); /* type FILE */
1270 WRITESHORT(p, SHN_ABS);
1271 saa_wbytes(s, entry, 16L);
1272 *len += 16;
1273 (*local)++;
1276 * Now some standard symbols defining the segments, for relocation
1277 * purposes.
1279 for (i = 1; i <= nsects; i++) {
1280 p = entry;
1281 WRITELONG(p, 0); /* no symbol name */
1282 WRITELONG(p, 0); /* offset zero */
1283 WRITELONG(p, 0); /* size zero */
1284 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1285 WRITESHORT(p, i); /* section id */
1286 saa_wbytes(s, entry, 16L);
1287 *len += 16;
1288 (*local)++;
1292 * Now the other local symbols.
1294 saa_rewind(syms);
1295 while ((sym = saa_rstruct(syms))) {
1296 if (sym->type & SYM_GLOBAL)
1297 continue;
1298 p = entry;
1299 WRITELONG(p, sym->strpos);
1300 WRITELONG(p, sym->symv.key);
1301 WRITELONG(p, sym->size);
1302 WRITECHAR(p, sym->type); /* type and binding */
1303 WRITECHAR(p, sym->other); /* visibility */
1304 WRITESHORT(p, sym->section);
1305 saa_wbytes(s, entry, 16L);
1306 *len += 16;
1307 (*local)++;
1310 * dwarf needs symbols for debug sections
1311 * which are relocation targets.
1313 //*** fix for 32 bit
1314 if (of_elf32.current_dfmt == &df_dwarf) {
1315 dwarf_infosym = *local;
1316 p = entry;
1317 WRITELONG(p, 0); /* no symbol name */
1318 WRITELONG(p, (uint32_t) 0); /* offset zero */
1319 WRITELONG(p, (uint32_t) 0); /* size zero */
1320 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1321 WRITESHORT(p, debug_info); /* section id */
1322 saa_wbytes(s, entry, 16L);
1323 *len += 16;
1324 (*local)++;
1325 dwarf_abbrevsym = *local;
1326 p = entry;
1327 WRITELONG(p, 0); /* no symbol name */
1328 WRITELONG(p, (uint32_t) 0); /* offset zero */
1329 WRITELONG(p, (uint32_t) 0); /* size zero */
1330 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1331 WRITESHORT(p, debug_abbrev); /* section id */
1332 saa_wbytes(s, entry, 16L);
1333 *len += 16;
1334 (*local)++;
1335 dwarf_linesym = *local;
1336 p = entry;
1337 WRITELONG(p, 0); /* no symbol name */
1338 WRITELONG(p, (uint32_t) 0); /* offset zero */
1339 WRITELONG(p, (uint32_t) 0); /* size zero */
1340 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1341 WRITESHORT(p, debug_line); /* section id */
1342 saa_wbytes(s, entry, 16L);
1343 *len += 16;
1344 (*local)++;
1348 * Now the global symbols.
1350 saa_rewind(syms);
1351 while ((sym = saa_rstruct(syms))) {
1352 if (!(sym->type & SYM_GLOBAL))
1353 continue;
1354 p = entry;
1355 WRITELONG(p, sym->strpos);
1356 WRITELONG(p, sym->symv.key);
1357 WRITELONG(p, sym->size);
1358 WRITECHAR(p, sym->type); /* type and binding */
1359 WRITECHAR(p, sym->other); /* visibility */
1360 WRITESHORT(p, sym->section);
1361 saa_wbytes(s, entry, 16L);
1362 *len += 16;
1365 return s;
1368 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1370 struct SAA *s;
1371 uint8_t *p, entry[8];
1373 if (!r)
1374 return NULL;
1376 s = saa_init(1L);
1377 *len = 0;
1379 while (r) {
1380 int32_t sym = r->symbol;
1382 if (sym >= GLOBAL_TEMP_BASE)
1384 if (of_elf32.current_dfmt == &df_dwarf)
1385 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1386 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1389 p = entry;
1390 WRITELONG(p, r->address);
1391 WRITELONG(p, (sym << 8) + r->type);
1392 saa_wbytes(s, entry, 8L);
1393 *len += 8;
1395 r = r->next;
1398 return s;
1401 static void elf_section_header(int name, int type, int flags,
1402 void *data, bool is_saa, int32_t datalen,
1403 int link, int info, int align, int eltsize)
1405 elf_sects[elf_nsect].data = data;
1406 elf_sects[elf_nsect].len = datalen;
1407 elf_sects[elf_nsect].is_saa = is_saa;
1408 elf_nsect++;
1410 fwriteint32_t((int32_t)name, elffp);
1411 fwriteint32_t((int32_t)type, elffp);
1412 fwriteint32_t((int32_t)flags, elffp);
1413 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1414 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1415 fwriteint32_t(datalen, elffp);
1416 if (data)
1417 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1418 fwriteint32_t((int32_t)link, elffp);
1419 fwriteint32_t((int32_t)info, elffp);
1420 fwriteint32_t((int32_t)align, elffp);
1421 fwriteint32_t((int32_t)eltsize, elffp);
1424 static void elf_write_sections(void)
1426 int i;
1427 for (i = 0; i < elf_nsect; i++)
1428 if (elf_sects[i].data) {
1429 int32_t len = elf_sects[i].len;
1430 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1431 int32_t align = reallen - len;
1432 if (elf_sects[i].is_saa)
1433 saa_fpwrite(elf_sects[i].data, elffp);
1434 else
1435 fwrite(elf_sects[i].data, len, 1, elffp);
1436 fwrite(align_str, align, 1, elffp);
1440 static void elf_sect_write(struct Section *sect,
1441 const uint8_t *data, uint32_t len)
1443 saa_wbytes(sect->data, data, len);
1444 sect->len += len;
1447 static int32_t elf_segbase(int32_t segment)
1449 return segment;
1452 static int elf_directive(char *directive, char *value, int pass)
1454 bool err;
1455 int64_t n;
1456 char *p;
1458 if (!strcmp(directive, "osabi")) {
1459 if (pass == 2)
1460 return 1; /* ignore in pass 2 */
1462 n = readnum(value, &err);
1463 if (err) {
1464 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1465 return 1;
1467 if (n < 0 || n > 255) {
1468 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1469 return 1;
1471 elf_osabi = n;
1472 elf_abiver = 0;
1474 if ((p = strchr(value,',')) == NULL)
1475 return 1;
1477 n = readnum(p+1, &err);
1478 if (err || n < 0 || n > 255) {
1479 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1480 return 1;
1483 elf_abiver = n;
1484 return 1;
1487 return 0;
1490 static void elf_filename(char *inname, char *outname, efunc error)
1492 strcpy(elf_module, inname);
1493 standard_extension(inname, outname, ".o", error);
1496 extern macros_t elf_stdmac[];
1498 static int elf_set_info(enum geninfo type, char **val)
1500 (void)type;
1501 (void)val;
1502 return 0;
1504 static struct dfmt df_dwarf = {
1505 "ELF32 (i386) dwarf debug format for Linux",
1506 "dwarf",
1507 debug32_init,
1508 dwarf32_linenum,
1509 debug32_deflabel,
1510 debug32_directive,
1511 debug32_typevalue,
1512 dwarf32_output,
1513 dwarf32_cleanup
1515 static struct dfmt df_stabs = {
1516 "ELF32 (i386) stabs debug format for Linux",
1517 "stabs",
1518 debug32_init,
1519 stabs32_linenum,
1520 debug32_deflabel,
1521 debug32_directive,
1522 debug32_typevalue,
1523 stabs32_output,
1524 stabs32_cleanup
1527 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1529 struct ofmt of_elf32 = {
1530 "ELF32 (i386) object files (e.g. Linux)",
1531 "elf32",
1532 NULL,
1533 elf32_debugs_arr,
1534 &df_stabs,
1535 elf_stdmac,
1536 elf_init,
1537 elf_set_info,
1538 elf_out,
1539 elf_deflabel,
1540 elf_section_names,
1541 elf_segbase,
1542 elf_directive,
1543 elf_filename,
1544 elf_cleanup
1547 struct ofmt of_elf = {
1548 "ELF (short name for ELF32) ",
1549 "elf",
1550 NULL,
1551 elf32_debugs_arr,
1552 &df_stabs,
1553 elf_stdmac,
1554 elf_init_hack,
1555 elf_set_info,
1556 elf_out,
1557 elf_deflabel,
1558 elf_section_names,
1559 elf_segbase,
1560 elf_directive,
1561 elf_filename,
1562 elf_cleanup
1564 /* again, the stabs debugging stuff (code) */
1566 void debug32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1568 (void)of;
1569 (void)id;
1570 (void)fp;
1571 (void)error;
1574 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1576 (void)segto;
1578 if (!stabs_filename) {
1579 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1580 strcpy(stabs_filename, filename);
1581 } else {
1582 if (strcmp(stabs_filename, filename)) {
1583 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1584 in fact, this leak comes in quite handy to maintain a list of files
1585 encountered so far in the symbol lines... */
1587 /* why not nasm_free(stabs_filename); we're done with the old one */
1589 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1590 strcpy(stabs_filename, filename);
1593 debug_immcall = 1;
1594 currentline = linenumber;
1597 void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1598 char *special)
1600 (void)name;
1601 (void)segment;
1602 (void)offset;
1603 (void)is_global;
1604 (void)special;
1607 void debug32_directive(const char *directive, const char *params)
1609 (void)directive;
1610 (void)params;
1613 void debug32_typevalue(int32_t type)
1615 int32_t stype, ssize;
1616 switch (TYM_TYPE(type)) {
1617 case TY_LABEL:
1618 ssize = 0;
1619 stype = STT_NOTYPE;
1620 break;
1621 case TY_BYTE:
1622 ssize = 1;
1623 stype = STT_OBJECT;
1624 break;
1625 case TY_WORD:
1626 ssize = 2;
1627 stype = STT_OBJECT;
1628 break;
1629 case TY_DWORD:
1630 ssize = 4;
1631 stype = STT_OBJECT;
1632 break;
1633 case TY_FLOAT:
1634 ssize = 4;
1635 stype = STT_OBJECT;
1636 break;
1637 case TY_QWORD:
1638 ssize = 8;
1639 stype = STT_OBJECT;
1640 break;
1641 case TY_TBYTE:
1642 ssize = 10;
1643 stype = STT_OBJECT;
1644 break;
1645 case TY_OWORD:
1646 ssize = 8;
1647 stype = STT_OBJECT;
1648 break;
1649 case TY_COMMON:
1650 ssize = 0;
1651 stype = STT_COMMON;
1652 break;
1653 case TY_SEG:
1654 ssize = 0;
1655 stype = STT_SECTION;
1656 break;
1657 case TY_EXTERN:
1658 ssize = 0;
1659 stype = STT_NOTYPE;
1660 break;
1661 case TY_EQU:
1662 ssize = 0;
1663 stype = STT_NOTYPE;
1664 break;
1665 default:
1666 ssize = 0;
1667 stype = STT_NOTYPE;
1668 break;
1670 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1671 lastsym->size = ssize;
1672 lastsym->type = stype;
1676 void stabs32_output(int type, void *param)
1678 struct symlininfo *s;
1679 struct linelist *el;
1680 if (type == TY_STABSSYMLIN) {
1681 if (debug_immcall) {
1682 s = (struct symlininfo *)param;
1683 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1684 return; /* we are only interested in the text stuff */
1685 numlinestabs++;
1686 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1687 el->info.offset = s->offset;
1688 el->info.section = s->section;
1689 el->info.name = s->name;
1690 el->line = currentline;
1691 el->filename = stabs_filename;
1692 el->next = 0;
1693 if (stabslines) {
1694 stabslines->last->next = el;
1695 stabslines->last = el;
1696 } else {
1697 stabslines = el;
1698 stabslines->last = el;
1702 debug_immcall = 0;
1705 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1706 do {\
1707 WRITELONG(p,n_strx); \
1708 WRITECHAR(p,n_type); \
1709 WRITECHAR(p,n_other); \
1710 WRITESHORT(p,n_desc); \
1711 WRITELONG(p,n_value); \
1712 } while (0)
1714 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1716 void stabs32_generate(void)
1718 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1719 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1720 char **allfiles;
1721 int *fileidx;
1723 struct linelist *ptr;
1725 ptr = stabslines;
1727 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1728 for (i = 0; i < numlinestabs; i++)
1729 allfiles[i] = 0;
1730 numfiles = 0;
1731 while (ptr) {
1732 if (numfiles == 0) {
1733 allfiles[0] = ptr->filename;
1734 numfiles++;
1735 } else {
1736 for (i = 0; i < numfiles; i++) {
1737 if (!strcmp(allfiles[i], ptr->filename))
1738 break;
1740 if (i >= numfiles) {
1741 allfiles[i] = ptr->filename;
1742 numfiles++;
1745 ptr = ptr->next;
1747 strsize = 1;
1748 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1749 for (i = 0; i < numfiles; i++) {
1750 fileidx[i] = strsize;
1751 strsize += strlen(allfiles[i]) + 1;
1753 mainfileindex = 0;
1754 for (i = 0; i < numfiles; i++) {
1755 if (!strcmp(allfiles[i], elf_module)) {
1756 mainfileindex = i;
1757 break;
1761 /* worst case size of the stab buffer would be:
1762 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1764 sbuf =
1765 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1766 sizeof(struct stabentry));
1768 ssbuf = (uint8_t *)nasm_malloc(strsize);
1770 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1771 rptr = rbuf;
1773 for (i = 0; i < numfiles; i++) {
1774 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1776 ssbuf[0] = 0;
1778 stabstrlen = strsize; /* set global variable for length of stab strings */
1780 sptr = sbuf;
1781 ptr = stabslines;
1782 numstabs = 0;
1784 if (ptr) {
1785 /* this is the first stab, its strx points to the filename of the
1786 the source-file, the n_desc field should be set to the number
1787 of remaining stabs
1789 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1791 /* this is the stab for the main source file */
1792 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1794 /* relocation table entry */
1796 /* Since the symbol table has two entries before */
1797 /* the section symbols, the index in the info.section */
1798 /* member must be adjusted by adding 2 */
1800 WRITELONG(rptr, (sptr - sbuf) - 4);
1801 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1803 numstabs++;
1804 currfile = mainfileindex;
1807 while (ptr) {
1808 if (strcmp(allfiles[currfile], ptr->filename)) {
1809 /* oops file has changed... */
1810 for (i = 0; i < numfiles; i++)
1811 if (!strcmp(allfiles[i], ptr->filename))
1812 break;
1813 currfile = i;
1814 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1815 ptr->info.offset);
1816 numstabs++;
1818 /* relocation table entry */
1819 WRITELONG(rptr, (sptr - sbuf) - 4);
1820 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1823 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1824 numstabs++;
1826 /* relocation table entry */
1828 WRITELONG(rptr, (sptr - sbuf) - 4);
1829 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1831 ptr = ptr->next;
1835 ((struct stabentry *)sbuf)->n_desc = numstabs;
1837 nasm_free(allfiles);
1838 nasm_free(fileidx);
1840 stablen = (sptr - sbuf);
1841 stabrellen = (rptr - rbuf);
1842 stabrelbuf = rbuf;
1843 stabbuf = sbuf;
1844 stabstrbuf = ssbuf;
1847 void stabs32_cleanup(void)
1849 struct linelist *ptr, *del;
1850 if (!stabslines)
1851 return;
1852 ptr = stabslines;
1853 while (ptr) {
1854 del = ptr;
1855 ptr = ptr->next;
1856 nasm_free(del);
1858 if (stabbuf)
1859 nasm_free(stabbuf);
1860 if (stabrelbuf)
1861 nasm_free(stabrelbuf);
1862 if (stabstrbuf)
1863 nasm_free(stabstrbuf);
1865 /* dwarf routines */
1868 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1870 (void)segto;
1871 dwarf32_findfile(filename);
1872 debug_immcall = 1;
1873 currentline = linenumber;
1876 /* called from elf_out with type == TY_DEBUGSYMLIN */
1877 void dwarf32_output(int type, void *param)
1879 int ln, aa, inx, maxln, soc;
1880 struct symlininfo *s;
1881 struct SAA *plinep;
1883 (void)type;
1885 s = (struct symlininfo *)param;
1886 /* line number info is only gathered for executable sections */
1887 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1888 return;
1889 /* Check if section index has changed */
1890 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1892 dwarf32_findsect(s->section);
1894 /* do nothing unless line or file has changed */
1895 if (debug_immcall)
1897 ln = currentline - dwarf_csect->line;
1898 aa = s->offset - dwarf_csect->offset;
1899 inx = dwarf_clist->line;
1900 plinep = dwarf_csect->psaa;
1901 /* check for file change */
1902 if (!(inx == dwarf_csect->file))
1904 saa_write8(plinep,DW_LNS_set_file);
1905 saa_write8(plinep,inx);
1906 dwarf_csect->file = inx;
1908 /* check for line change */
1909 if (ln)
1911 /* test if in range of special op code */
1912 maxln = line_base + line_range;
1913 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1914 if (ln >= line_base && ln < maxln && soc < 256)
1916 saa_write8(plinep,soc);
1918 else
1920 if (ln)
1922 saa_write8(plinep,DW_LNS_advance_line);
1923 saa_wleb128s(plinep,ln);
1925 if (aa)
1927 saa_write8(plinep,DW_LNS_advance_pc);
1928 saa_wleb128u(plinep,aa);
1931 dwarf_csect->line = currentline;
1932 dwarf_csect->offset = s->offset;
1934 /* show change handled */
1935 debug_immcall = 0;
1940 void dwarf32_generate(void)
1942 uint8_t *pbuf;
1943 int indx;
1944 struct linelist *ftentry;
1945 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1946 struct SAA *parangesrel, *plinesrel, *pinforel;
1947 struct sectlist *psect;
1948 size_t saalen, linepoff, totlen, highaddr;
1950 /* write epilogues for each line program range */
1951 /* and build aranges section */
1952 paranges = saa_init(1L);
1953 parangesrel = saa_init(1L);
1954 saa_write16(paranges,2); /* dwarf version */
1955 saa_write32(parangesrel, paranges->datalen+4);
1956 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1957 saa_write32(parangesrel, 0);
1958 saa_write32(paranges,0); /* offset into info */
1959 saa_write8(paranges,4); /* pointer size */
1960 saa_write8(paranges,0); /* not segmented */
1961 saa_write32(paranges,0); /* padding */
1962 /* iterate though sectlist entries */
1963 psect = dwarf_fsect;
1964 totlen = 0;
1965 highaddr = 0;
1966 for (indx = 0; indx < dwarf_nsections; indx++)
1968 plinep = psect->psaa;
1969 /* Line Number Program Epilogue */
1970 saa_write8(plinep,2); /* std op 2 */
1971 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1972 saa_write8(plinep,DW_LNS_extended_op);
1973 saa_write8(plinep,1); /* operand length */
1974 saa_write8(plinep,DW_LNE_end_sequence);
1975 totlen += plinep->datalen;
1976 /* range table relocation entry */
1977 saa_write32(parangesrel, paranges->datalen + 4);
1978 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1979 saa_write32(parangesrel, (uint32_t) 0);
1980 /* range table entry */
1981 saa_write32(paranges,0x0000); /* range start */
1982 saa_write32(paranges,sects[psect->section]->len); /* range length */
1983 highaddr += sects[psect->section]->len;
1984 /* done with this entry */
1985 psect = psect->next;
1987 saa_write32(paranges,0); /* null address */
1988 saa_write32(paranges,0); /* null length */
1989 saalen = paranges->datalen;
1990 arangeslen = saalen + 4;
1991 arangesbuf = pbuf = nasm_malloc(arangeslen);
1992 WRITELONG(pbuf,saalen); /* initial length */
1993 saa_rnbytes(paranges, pbuf, saalen);
1994 saa_free(paranges);
1996 /* build rela.aranges section */
1997 arangesrellen = saalen = parangesrel->datalen;
1998 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1999 saa_rnbytes(parangesrel, pbuf, saalen);
2000 saa_free(parangesrel);
2002 /* build pubnames section */
2003 ppubnames = saa_init(1L);
2004 saa_write16(ppubnames,3); /* dwarf version */
2005 saa_write32(ppubnames,0); /* offset into info */
2006 saa_write32(ppubnames,0); /* space used in info */
2007 saa_write32(ppubnames,0); /* end of list */
2008 saalen = ppubnames->datalen;
2009 pubnameslen = saalen + 4;
2010 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2011 WRITELONG(pbuf,saalen); /* initial length */
2012 saa_rnbytes(ppubnames, pbuf, saalen);
2013 saa_free(ppubnames);
2015 /* build info section */
2016 pinfo = saa_init(1L);
2017 pinforel = saa_init(1L);
2018 saa_write16(pinfo,2); /* dwarf version */
2019 saa_write32(pinforel, pinfo->datalen + 4);
2020 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
2021 saa_write32(pinforel, 0);
2022 saa_write32(pinfo,0); /* offset into abbrev */
2023 saa_write8(pinfo,4); /* pointer size */
2024 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2025 saa_write32(pinforel, pinfo->datalen + 4);
2026 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2027 saa_write32(pinforel, 0);
2028 saa_write32(pinfo,0); /* DW_AT_low_pc */
2029 saa_write32(pinforel, pinfo->datalen + 4);
2030 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2031 saa_write32(pinforel, 0);
2032 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
2033 saa_write32(pinforel, pinfo->datalen + 4);
2034 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
2035 saa_write32(pinforel, 0);
2036 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2037 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2038 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2039 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2040 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2041 saa_write32(pinforel, pinfo->datalen + 4);
2042 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2043 saa_write32(pinforel, 0);
2044 saa_write32(pinfo,0); /* DW_AT_low_pc */
2045 saa_write32(pinfo,0); /* DW_AT_frame_base */
2046 saa_write8(pinfo,0); /* end of entries */
2047 saalen = pinfo->datalen;
2048 infolen = saalen + 4;
2049 infobuf = pbuf = nasm_malloc(infolen);
2050 WRITELONG(pbuf,saalen); /* initial length */
2051 saa_rnbytes(pinfo, pbuf, saalen);
2052 saa_free(pinfo);
2054 /* build rela.info section */
2055 inforellen = saalen = pinforel->datalen;
2056 inforelbuf = pbuf = nasm_malloc(inforellen);
2057 saa_rnbytes(pinforel, pbuf, saalen);
2058 saa_free(pinforel);
2060 /* build abbrev section */
2061 pabbrev = saa_init(1L);
2062 saa_write8(pabbrev,1); /* entry number LEB128u */
2063 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2064 saa_write8(pabbrev,1); /* has children */
2065 /* the following attributes and forms are all LEB128u values */
2066 saa_write8(pabbrev,DW_AT_low_pc);
2067 saa_write8(pabbrev,DW_FORM_addr);
2068 saa_write8(pabbrev,DW_AT_high_pc);
2069 saa_write8(pabbrev,DW_FORM_addr);
2070 saa_write8(pabbrev,DW_AT_stmt_list);
2071 saa_write8(pabbrev,DW_FORM_data4);
2072 saa_write8(pabbrev,DW_AT_name);
2073 saa_write8(pabbrev,DW_FORM_string);
2074 saa_write8(pabbrev,DW_AT_producer);
2075 saa_write8(pabbrev,DW_FORM_string);
2076 saa_write8(pabbrev,DW_AT_language);
2077 saa_write8(pabbrev,DW_FORM_data2);
2078 saa_write16(pabbrev,0); /* end of entry */
2079 /* LEB128u usage same as above */
2080 saa_write8(pabbrev,2); /* entry number */
2081 saa_write8(pabbrev,DW_TAG_subprogram);
2082 saa_write8(pabbrev,0); /* no children */
2083 saa_write8(pabbrev,DW_AT_low_pc);
2084 saa_write8(pabbrev,DW_FORM_addr);
2085 saa_write8(pabbrev,DW_AT_frame_base);
2086 saa_write8(pabbrev,DW_FORM_data4);
2087 saa_write16(pabbrev,0); /* end of entry */
2088 abbrevlen = saalen = pabbrev->datalen;
2089 abbrevbuf = pbuf = nasm_malloc(saalen);
2090 saa_rnbytes(pabbrev, pbuf, saalen);
2091 saa_free(pabbrev);
2093 /* build line section */
2094 /* prolog */
2095 plines = saa_init(1L);
2096 saa_write8(plines,1); /* Minimum Instruction Length */
2097 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2098 saa_write8(plines,line_base); /* Line Base */
2099 saa_write8(plines,line_range); /* Line Range */
2100 saa_write8(plines,opcode_base); /* Opcode Base */
2101 /* standard opcode lengths (# of LEB128u operands) */
2102 saa_write8(plines,0); /* Std opcode 1 length */
2103 saa_write8(plines,1); /* Std opcode 2 length */
2104 saa_write8(plines,1); /* Std opcode 3 length */
2105 saa_write8(plines,1); /* Std opcode 4 length */
2106 saa_write8(plines,1); /* Std opcode 5 length */
2107 saa_write8(plines,0); /* Std opcode 6 length */
2108 saa_write8(plines,0); /* Std opcode 7 length */
2109 saa_write8(plines,0); /* Std opcode 8 length */
2110 saa_write8(plines,1); /* Std opcode 9 length */
2111 saa_write8(plines,0); /* Std opcode 10 length */
2112 saa_write8(plines,0); /* Std opcode 11 length */
2113 saa_write8(plines,1); /* Std opcode 12 length */
2114 /* Directory Table */
2115 saa_write8(plines,0); /* End of table */
2116 /* File Name Table */
2117 ftentry = dwarf_flist;
2118 for (indx = 0;indx<dwarf_numfiles;indx++)
2120 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2121 saa_write8(plines,0); /* directory LEB128u */
2122 saa_write8(plines,0); /* time LEB128u */
2123 saa_write8(plines,0); /* size LEB128u */
2124 ftentry = ftentry->next;
2126 saa_write8(plines,0); /* End of table */
2127 linepoff = plines->datalen;
2128 linelen = linepoff + totlen + 10;
2129 linebuf = pbuf = nasm_malloc(linelen);
2130 WRITELONG(pbuf,linelen-4); /* initial length */
2131 WRITESHORT(pbuf,3); /* dwarf version */
2132 WRITELONG(pbuf,linepoff); /* offset to line number program */
2133 /* write line header */
2134 saalen = linepoff;
2135 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2136 pbuf += linepoff;
2137 saa_free(plines);
2138 /* concatonate line program ranges */
2139 linepoff += 13;
2140 plinesrel = saa_init(1L);
2141 psect = dwarf_fsect;
2142 for (indx = 0; indx < dwarf_nsections; indx++)
2144 saa_write32(plinesrel, linepoff);
2145 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2146 saa_write32(plinesrel, (uint32_t) 0);
2147 plinep = psect->psaa;
2148 saalen = plinep->datalen;
2149 saa_rnbytes(plinep, pbuf, saalen);
2150 pbuf += saalen;
2151 linepoff += saalen;
2152 saa_free(plinep);
2153 /* done with this entry */
2154 psect = psect->next;
2158 /* build rela.lines section */
2159 linerellen =saalen = plinesrel->datalen;
2160 linerelbuf = pbuf = nasm_malloc(linerellen);
2161 saa_rnbytes(plinesrel, pbuf, saalen);
2162 saa_free(plinesrel);
2164 /* build frame section */
2165 framelen = 4;
2166 framebuf = pbuf = nasm_malloc(framelen);
2167 WRITELONG(pbuf,framelen-4); /* initial length */
2169 /* build loc section */
2170 loclen = 16;
2171 locbuf = pbuf = nasm_malloc(loclen);
2172 WRITELONG(pbuf,0); /* null beginning offset */
2173 WRITELONG(pbuf,0); /* null ending offset */
2176 void dwarf32_cleanup(void)
2178 if (arangesbuf)
2179 nasm_free(arangesbuf);
2180 if (arangesrelbuf)
2181 nasm_free(arangesrelbuf);
2182 if (pubnamesbuf)
2183 nasm_free(pubnamesbuf);
2184 if (infobuf)
2185 nasm_free(infobuf);
2186 if (inforelbuf)
2187 nasm_free(inforelbuf);
2188 if (abbrevbuf)
2189 nasm_free(abbrevbuf);
2190 if (linebuf)
2191 nasm_free(linebuf);
2192 if (linerelbuf)
2193 nasm_free(linerelbuf);
2194 if (framebuf)
2195 nasm_free(framebuf);
2196 if (locbuf)
2197 nasm_free(locbuf);
2199 void dwarf32_findfile(const char * fname)
2201 int finx;
2202 struct linelist *match;
2204 /* return if fname is current file name */
2205 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2206 /* search for match */
2207 else
2209 match = 0;
2210 if (dwarf_flist)
2212 match = dwarf_flist;
2213 for (finx = 0; finx < dwarf_numfiles; finx++)
2215 if (!(strcmp(fname, match->filename)))
2217 dwarf_clist = match;
2218 return;
2222 /* add file name to end of list */
2223 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2224 dwarf_numfiles++;
2225 dwarf_clist->line = dwarf_numfiles;
2226 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2227 strcpy(dwarf_clist->filename,fname);
2228 dwarf_clist->next = 0;
2229 /* if first entry */
2230 if (!dwarf_flist)
2232 dwarf_flist = dwarf_elist = dwarf_clist;
2233 dwarf_clist->last = 0;
2235 /* chain to previous entry */
2236 else
2238 dwarf_elist->next = dwarf_clist;
2239 dwarf_elist = dwarf_clist;
2243 /* */
2244 void dwarf32_findsect(const int index)
2246 int sinx;
2247 struct sectlist *match;
2248 struct SAA *plinep;
2249 /* return if index is current section index */
2250 if (dwarf_csect && (dwarf_csect->section == index))
2252 return;
2254 /* search for match */
2255 else
2257 match = 0;
2258 if (dwarf_fsect)
2260 match = dwarf_fsect;
2261 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2263 if ((match->section == index))
2265 dwarf_csect = match;
2266 return;
2268 match = match->next;
2271 /* add entry to end of list */
2272 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2273 dwarf_nsections++;
2274 dwarf_csect->psaa = plinep = saa_init(1L);
2275 dwarf_csect->line = 1;
2276 dwarf_csect->offset = 0;
2277 dwarf_csect->file = 1;
2278 dwarf_csect->section = index;
2279 dwarf_csect->next = 0;
2280 /* set relocatable address at start of line program */
2281 saa_write8(plinep,DW_LNS_extended_op);
2282 saa_write8(plinep,5); /* operand length */
2283 saa_write8(plinep,DW_LNE_set_address);
2284 saa_write32(plinep,0); /* Start Address */
2285 /* if first entry */
2286 if (!dwarf_fsect)
2288 dwarf_fsect = dwarf_esect = dwarf_csect;
2289 dwarf_csect->last = 0;
2291 /* chain to previous entry */
2292 else
2294 dwarf_esect->next = dwarf_csect;
2295 dwarf_esect = dwarf_csect;
2300 #endif /* OF_ELF */