1 /* outelf64.c output routines for the Netwide Assembler to produce
2 * ELF64 (x86_64 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.
23 /* Definitions in lieu of elf.h */
24 #define SHT_NULL 0 /* Inactive section header */
25 #define SHT_PROGBITS 1 /* Program defined content */
26 #define SHT_RELA 4 /* Relocation entries with addends */
27 #define SHT_NOBITS 8 /* Section requires no space in file */
28 #define SHF_WRITE (1 << 0) /* Writable */
29 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
30 #define SHF_EXECINSTR (1 << 2) /* Executable */
31 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
32 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
33 #define R_X86_64_NONE 0 /* No reloc */
34 #define R_X86_64_64 1 /* Direct 64 bit address */
35 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
36 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
37 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
38 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
39 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
40 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
41 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
42 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
43 #define ET_REL 1 /* Relocatable file */
44 #define EM_X86_64 62 /* AMD x86-64 architecture */
45 #define STT_NOTYPE 0 /* Symbol type is unspecified */
46 #define STT_OBJECT 1 /* Symbol is a data object */
47 #define STT_FUNC 2 /* Symbol is a code object */
48 #define STT_SECTION 3 /* Symbol associated with a section */
49 #define STT_FILE 4 /* Symbol's name is file name */
50 #define STT_COMMON 5 /* Symbol is a common data object */
51 #define STT_TLS 6 /* Symbol is thread-local data object*/
52 #define STT_NUM 7 /* Number of defined types. */
54 /* Definitions in lieu of dwarf.h */
55 #define DW_TAG_compile_unit 0x11
56 #define DW_TAG_subprogram 0x2e
57 #define DW_AT_name 0x03
58 #define DW_AT_stmt_list 0x10
59 #define DW_AT_low_pc 0x11
60 #define DW_AT_high_pc 0x12
61 #define DW_AT_language 0x13
62 #define DW_AT_producer 0x25
63 #define DW_AT_frame_base 0x40
64 #define DW_FORM_addr 0x01
65 #define DW_FORM_data2 0x05
66 #define DW_FORM_data4 0x06
67 #define DW_FORM_string 0x08
68 #define DW_LNS_extended_op 0
69 #define DW_LNS_advance_pc 2
70 #define DW_LNS_advance_line 3
71 #define DW_LNS_set_file 4
72 #define DW_LNE_end_sequence 1
73 #define DW_LNE_set_address 2
74 #define DW_LNE_define_file 3
75 #define DW_LANG_Mips_Assembler 0x8001
77 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
79 typedef uint32_t Elf64_Word;
80 typedef uint64_t Elf64_Xword;
81 typedef uint64_t Elf64_Addr;
82 typedef uint64_t Elf64_Off;
85 Elf64_Word sh_name; /* Section name (string tbl index) */
86 Elf64_Word sh_type; /* Section type */
87 Elf64_Xword sh_flags; /* Section flags */
88 Elf64_Addr sh_addr; /* Section virtual addr at execution */
89 Elf64_Off sh_offset; /* Section file offset */
90 Elf64_Xword sh_size; /* Section size in bytes */
91 Elf64_Word sh_link; /* Link to another section */
92 Elf64_Word sh_info; /* Additional section information */
93 Elf64_Xword sh_addralign; /* Section alignment */
94 Elf64_Xword sh_entsize; /* Entry size if section holds table */
103 int64_t address; /* relative to _start_ of section */
104 int64_t symbol; /* symbol index */
105 int type; /* type of relocation */
109 int32_t strpos; /* string table position of name */
110 int32_t section; /* section ID of the symbol */
111 int type; /* symbol type */
112 int other; /* symbol visibility */
113 int64_t value; /* address, or COMMON variable align */
114 int32_t size; /* size of symbol */
115 int32_t globnum; /* symbol table offset if global */
116 struct Symbol *next; /* list of globals in each section */
117 struct Symbol *nextfwd; /* list of unresolved-size symbols */
118 char *name; /* used temporarily if in above list */
126 int32_t index; /* index into sects array */
127 uint32_t type; /* SHT_PROGBITS or SHT_NOBITS */
128 uint64_t align; /* alignment: power of two */
129 uint64_t flags; /* section flags */
133 struct Reloc *head, **tail;
134 struct Symbol *gsyms; /* global symbols in section */
137 #define SECT_DELTA 32
138 static struct Section **sects;
139 static int nsects, sectlen;
141 #define SHSTR_DELTA 256
142 static char *shstrtab;
143 static int shstrtablen, shstrtabsize;
145 static struct SAA *syms;
146 static uint32_t nlocals, nglobs;
148 static int32_t def_seg;
150 static struct RAA *bsym;
152 static struct SAA *strs;
153 static uint32_t strslen;
157 static evalfunc evaluate;
159 static struct Symbol *fwds;
161 static char elf_module[FILENAME_MAX];
163 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
164 static uint8_t elf_abiver = 0; /* Current ABI version */
166 extern struct ofmt of_elf64;
170 #define SYM_GLOBAL 0x10
172 #define STV_DEFAULT 0
173 #define STV_INTERNAL 1
175 #define STV_PROTECTED 3
177 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
179 #define SEG_ALIGN 16 /* alignment of sections in file */
180 #define SEG_ALIGN_1 (SEG_ALIGN-1)
182 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
184 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
186 static struct ELF_SECTDATA {
191 static int elf_nsect, nsections;
192 static int64_t elf_foffs;
194 static void elf_write(void);
195 static void elf_sect_write(struct Section *, const uint8_t *,
197 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
199 static void elf_write_sections(void);
200 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
201 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
202 static void add_sectname(char *, char *);
204 /* type values for stabs debugging sections */
205 #define N_SO 0x64 /* ID for main source file */
206 #define N_SOL 0x84 /* ID for sub-source file */
207 #define N_BINCL 0x82 /* not currently used */
208 #define N_EINCL 0xA2 /* not currently used */
225 int section; /* index into sects[] */
226 int segto; /* internal section number */
227 char *name; /* shallow-copied pointer of section name */
231 struct symlininfo info;
234 struct linelist *next;
235 struct linelist *last;
244 struct sectlist *next;
245 struct sectlist *last;
248 /* common debug variables */
249 static int currentline = 1;
250 static int debug_immcall = 0;
252 /* stabs debug variables */
253 static struct linelist *stabslines = 0;
254 static int numlinestabs = 0;
255 static char *stabs_filename = 0;
256 static int symtabsection;
257 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
258 static int stablen, stabstrlen, stabrellen;
260 /* dwarf debug variables */
261 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
262 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
263 static int dwarf_numfiles = 0, dwarf_nsections;
264 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
265 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
266 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
267 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
268 abbrevlen, linelen, linerellen, framelen, loclen;
269 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
272 static struct dfmt df_dwarf;
273 static struct dfmt df_stabs;
274 static struct Symbol *lastsym;
276 /* common debugging routines */
277 void debug64_typevalue(int32_t);
278 void debug64_init(struct ofmt *, void *, FILE *, efunc);
279 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
280 void debug64_directive(const char *, const char *);
282 /* stabs debugging routines */
283 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
284 void stabs64_output(int, void *);
285 void stabs64_generate(void);
286 void stabs64_cleanup(void);
288 /* dwarf debugging routines */
289 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
290 void dwarf64_output(int, void *);
291 void dwarf64_generate(void);
292 void dwarf64_cleanup(void);
293 void dwarf64_findfile(const char *);
294 void dwarf64_findsect(const int);
297 * Special section numbers which are used to define ELF special
298 * symbols, which can be used with WRT to provide PIC relocation
301 static int32_t elf_gotpc_sect, elf_gotoff_sect;
302 static int32_t elf_got_sect, elf_plt_sect;
303 static int32_t elf_sym_sect;
305 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
311 (void)ldef; /* placate optimisers */
313 nsects = sectlen = 0;
314 syms = saa_init((int32_t)sizeof(struct Symbol));
315 nlocals = nglobs = 0;
318 saa_wbytes(strs, "\0", 1L);
319 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
320 strslen = 2 + strlen(elf_module);
322 shstrtablen = shstrtabsize = 0;;
323 add_sectname("", "");
327 elf_gotpc_sect = seg_alloc();
328 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
330 elf_gotoff_sect = seg_alloc();
331 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
333 elf_got_sect = seg_alloc();
334 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
336 elf_plt_sect = seg_alloc();
337 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
339 elf_sym_sect = seg_alloc();
340 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
343 def_seg = seg_alloc();
347 static void elf_cleanup(int debuginfo)
356 for (i = 0; i < nsects; i++) {
357 if (sects[i]->type != SHT_NOBITS)
358 saa_free(sects[i]->data);
360 saa_free(sects[i]->rel);
361 while (sects[i]->head) {
363 sects[i]->head = sects[i]->head->next;
371 if (of_elf64.current_dfmt) {
372 of_elf64.current_dfmt->cleanup();
375 /* add entry to the elf .shstrtab section */
376 static void add_sectname(char *firsthalf, char *secondhalf)
378 int len = strlen(firsthalf) + strlen(secondhalf);
379 while (shstrtablen + len + 1 > shstrtabsize)
380 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
381 strcpy(shstrtab + shstrtablen, firsthalf);
382 strcat(shstrtab + shstrtablen, secondhalf);
383 shstrtablen += len + 1;
386 static int elf_make_section(char *name, int type, int flags, int align)
390 s = nasm_malloc(sizeof(*s));
392 if (type != SHT_NOBITS)
393 s->data = saa_init(1L);
396 s->len = s->size = 0;
398 if (!strcmp(name, ".text"))
401 s->index = seg_alloc();
402 add_sectname("", name);
403 s->name = nasm_malloc(1 + strlen(name));
404 strcpy(s->name, name);
410 if (nsects >= sectlen)
412 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
418 static int32_t elf_section_names(char *name, int pass, int *bits)
421 unsigned flags_and, flags_or;
422 uint64_t type, align;
426 * Default is 64 bits.
434 while (*p && !isspace(*p))
438 flags_and = flags_or = type = align = 0;
440 while (*p && isspace(*p))
444 while (*p && !isspace(*p))
448 while (*p && isspace(*p))
451 if (!nasm_strnicmp(q, "align=", 6)) {
455 if ((align - 1) & align) { /* means it's not a power of two */
456 error(ERR_NONFATAL, "section alignment %d is not"
457 " a power of two", align);
460 } else if (!nasm_stricmp(q, "alloc")) {
461 flags_and |= SHF_ALLOC;
462 flags_or |= SHF_ALLOC;
463 } else if (!nasm_stricmp(q, "noalloc")) {
464 flags_and |= SHF_ALLOC;
465 flags_or &= ~SHF_ALLOC;
466 } else if (!nasm_stricmp(q, "exec")) {
467 flags_and |= SHF_EXECINSTR;
468 flags_or |= SHF_EXECINSTR;
469 } else if (!nasm_stricmp(q, "noexec")) {
470 flags_and |= SHF_EXECINSTR;
471 flags_or &= ~SHF_EXECINSTR;
472 } else if (!nasm_stricmp(q, "write")) {
473 flags_and |= SHF_WRITE;
474 flags_or |= SHF_WRITE;
475 } else if (!nasm_stricmp(q, "nowrite")) {
476 flags_and |= SHF_WRITE;
477 flags_or &= ~SHF_WRITE;
478 } else if (!nasm_stricmp(q, "progbits")) {
480 } else if (!nasm_stricmp(q, "nobits")) {
485 if (!strcmp(name, ".comment") ||
486 !strcmp(name, ".shstrtab") ||
487 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
488 error(ERR_NONFATAL, "attempt to redefine reserved section"
493 for (i = 0; i < nsects; i++)
494 if (!strcmp(name, sects[i]->name))
497 if (!strcmp(name, ".text"))
498 i = elf_make_section(name, SHT_PROGBITS,
499 SHF_ALLOC | SHF_EXECINSTR, 16);
500 else if (!strcmp(name, ".rodata"))
501 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
502 else if (!strcmp(name, ".data"))
503 i = elf_make_section(name, SHT_PROGBITS,
504 SHF_ALLOC | SHF_WRITE, 4);
505 else if (!strcmp(name, ".bss"))
506 i = elf_make_section(name, SHT_NOBITS,
507 SHF_ALLOC | SHF_WRITE, 4);
509 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
511 sects[i]->type = type;
513 sects[i]->align = align;
514 sects[i]->flags &= ~flags_and;
515 sects[i]->flags |= flags_or;
516 } else if (pass == 1) {
517 if ((type && sects[i]->type != type)
518 || (align && sects[i]->align != align)
519 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
520 error(ERR_WARNING, "incompatible section attributes ignored on"
521 " redeclaration of section `%s'", name);
524 return sects[i]->index;
527 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
528 int is_global, char *special)
532 bool special_used = false;
534 #if defined(DEBUG) && DEBUG>2
536 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
537 name, segment, offset, is_global, special);
539 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
541 * This is a NASM special symbol. We never allow it into
542 * the ELF symbol table, even if it's a valid one. If it
543 * _isn't_ a valid one, we should barf immediately.
545 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
546 strcmp(name, "..got") && strcmp(name, "..plt") &&
547 strcmp(name, "..sym"))
548 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
552 if (is_global == 3) {
555 * Fix up a forward-reference symbol size from the first
558 for (s = &fwds; *s; s = &(*s)->nextfwd)
559 if (!strcmp((*s)->name, name)) {
560 struct tokenval tokval;
564 while (*p && !isspace(*p))
566 while (*p && isspace(*p))
570 tokval.t_type = TOKEN_INVALID;
571 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
574 error(ERR_NONFATAL, "cannot use relocatable"
575 " expression as symbol size");
577 (*s)->size = reloc_value(e);
581 * Remove it from the list of unresolved sizes.
583 nasm_free((*s)->name);
587 return; /* it wasn't an important one */
590 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
591 strslen += 1 + strlen(name);
593 lastsym = sym = saa_wstruct(syms);
596 sym->type = is_global ? SYM_GLOBAL : 0;
597 sym->other = STV_DEFAULT;
599 if (segment == NO_SEG)
600 sym->section = SHN_ABS;
603 sym->section = SHN_UNDEF;
604 if (nsects == 0 && segment == def_seg) {
606 if (segment != elf_section_names(".text", 2, &tempint))
608 "strange segment conditions in ELF driver");
609 sym->section = nsects;
611 for (i = 0; i < nsects; i++)
612 if (segment == sects[i]->index) {
613 sym->section = i + 1;
619 if (is_global == 2) {
622 sym->section = SHN_COMMON;
624 * We have a common variable. Check the special text to see
625 * if it's a valid number and power of two; if so, store it
626 * as the alignment for the common variable.
630 sym->value = readnum(special, &err);
632 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
633 " valid number", special);
634 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
635 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
636 " power of two", special);
640 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
642 if (sym->type == SYM_GLOBAL) {
644 * If sym->section == SHN_ABS, then the first line of the
645 * else section would cause a core dump, because its a reference
646 * beyond the end of the section array.
647 * This behaviour is exhibited by this code:
650 * To avoid such a crash, such requests are silently discarded.
651 * This may not be the best solution.
653 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
654 bsym = raa_write(bsym, segment, nglobs);
655 } else if (sym->section != SHN_ABS) {
657 * This is a global symbol; so we must add it to the linked
658 * list of global symbols in its section. We'll push it on
659 * the beginning of the list, because it doesn't matter
660 * much which end we put it on and it's easier like this.
662 * In addition, we check the special text for symbol
663 * type and size information.
665 sym->next = sects[sym->section - 1]->gsyms;
666 sects[sym->section - 1]->gsyms = sym;
669 int n = strcspn(special, " \t");
671 if (!nasm_strnicmp(special, "function", n))
672 sym->type |= STT_FUNC;
673 else if (!nasm_strnicmp(special, "data", n) ||
674 !nasm_strnicmp(special, "object", n))
675 sym->type |= STT_OBJECT;
676 else if (!nasm_strnicmp(special, "notype", n))
677 sym->type |= STT_NOTYPE;
679 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
683 while (isspace(*special))
686 n = strcspn(special, " \t");
687 if (!nasm_strnicmp(special, "default", n))
688 sym->other = STV_DEFAULT;
689 else if (!nasm_strnicmp(special, "internal", n))
690 sym->other = STV_INTERNAL;
691 else if (!nasm_strnicmp(special, "hidden", n))
692 sym->other = STV_HIDDEN;
693 else if (!nasm_strnicmp(special, "protected", n))
694 sym->other = STV_PROTECTED;
701 struct tokenval tokval;
704 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
706 while (special[n] && isspace(special[n]))
709 * We have a size expression; attempt to
713 stdscan_bufptr = special + n;
714 tokval.t_type = TOKEN_INVALID;
715 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
720 sym->name = nasm_strdup(name);
723 error(ERR_NONFATAL, "cannot use relocatable"
724 " expression as symbol size");
726 sym->size = reloc_value(e);
728 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
733 sym->globnum = nglobs;
738 if (special && !special_used)
739 error(ERR_NONFATAL, "no special symbol features supported here");
742 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
745 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
746 sect->tail = &r->next;
749 r->address = sect->len;
750 if (segment == NO_SEG)
755 for (i = 0; i < nsects; i++)
756 if (segment == sects[i]->index)
759 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
767 * This routine deals with ..got and ..sym relocations: the more
768 * complicated kinds. In shared-library writing, some relocations
769 * with respect to global symbols must refer to the precise symbol
770 * rather than referring to an offset from the base of the section
771 * _containing_ the symbol. Such relocations call to this routine,
772 * which searches the symbol list for the symbol in question.
774 * R_386_GOT32 references require the _exact_ symbol address to be
775 * used; R_386_32 references can be at an offset from the symbol.
776 * The boolean argument `exact' tells us this.
778 * Return value is the adjusted value of `addr', having become an
779 * offset from the symbol rather than the section. Should always be
780 * zero when returning from an exact call.
782 * Limitation: if you define two symbols at the same place,
783 * confusion will occur.
785 * Inefficiency: we search, currently, using a linked list which
786 * isn't even necessarily sorted.
788 static int32_t elf_add_gsym_reloc(struct Section *sect,
789 int32_t segment, int64_t offset,
790 int type, bool exact)
794 struct Symbol *sym, *sm;
798 * First look up the segment/offset pair and find a global
799 * symbol corresponding to it. If it's not one of our segments,
800 * then it must be an external symbol, in which case we're fine
801 * doing a normal elf_add_reloc after first sanity-checking
802 * that the offset from the symbol is zero.
805 for (i = 0; i < nsects; i++)
806 if (segment == sects[i]->index) {
811 if (exact && offset != 0)
812 error(ERR_NONFATAL, "unable to find a suitable global symbol"
813 " for this reference");
815 elf_add_reloc(sect, segment, type);
821 * Find a symbol pointing _exactly_ at this one.
823 for (sym = s->gsyms; sym; sym = sym->next)
824 if (sym->value == offset)
828 * Find the nearest symbol below this one.
831 for (sm = s->gsyms; sm; sm = sm->next)
832 if (sm->value <= offset && (!sym || sm->value > sym->value))
836 error(ERR_NONFATAL, "unable to find a suitable global symbol"
837 " for this reference");
841 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
842 sect->tail = &r->next;
845 r->address = sect->len;
846 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
851 return offset - sym->value;
854 static void elf_out(int32_t segto, const void *data,
855 enum out_type type, uint64_t size,
856 int32_t segment, int32_t wrt)
860 uint8_t mydata[16], *p;
862 static struct symlininfo sinfo;
864 #if defined(DEBUG) && DEBUG>2
865 if (data) fprintf(stderr,
866 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
867 currentline, type, segment, segto, size, *(int64_t *)data);
869 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
870 currentline, type, segment, segto, size);
874 * handle absolute-assembly (structure definitions)
876 if (segto == NO_SEG) {
877 if (type != OUT_RESERVE)
878 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
884 for (i = 0; i < nsects; i++)
885 if (segto == sects[i]->index) {
890 int tempint; /* ignored */
891 if (segto != elf_section_names(".text", 2, &tempint))
892 error(ERR_PANIC, "strange segment conditions in ELF driver");
894 s = sects[nsects - 1];
898 /* invoke current debug_output routine */
899 if (of_elf64.current_dfmt) {
900 sinfo.offset = s->len;
903 sinfo.name = s->name;
904 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
906 /* end of debugging stuff */
908 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
909 error(ERR_WARNING, "attempt to initialize memory in"
910 " BSS section `%s': ignored", s->name);
911 if (type == OUT_REL2ADR)
913 else if (type == OUT_REL4ADR)
919 if (type == OUT_RESERVE) {
920 if (s->type == SHT_PROGBITS) {
921 error(ERR_WARNING, "uninitialized space declared in"
922 " non-BSS section `%s': zeroing", s->name);
923 elf_sect_write(s, NULL, size);
926 } else if (type == OUT_RAWDATA) {
927 if (segment != NO_SEG)
928 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
929 elf_sect_write(s, data, size);
930 } else if (type == OUT_ADDRESS) {
932 addr = *(int64_t *)data;
933 if (segment != NO_SEG) {
935 error(ERR_NONFATAL, "ELF format does not support"
936 " segment base references");
941 elf_add_reloc(s, segment, R_X86_64_16);
944 elf_add_reloc(s, segment, R_X86_64_32);
947 elf_add_reloc(s, segment, R_X86_64_64);
950 error(ERR_PANIC, "internal error elf64-hpa-871");
953 } else if (wrt == elf_gotpc_sect + 1) {
955 * The user will supply GOT relative to $$. ELF
956 * will let us have GOT relative to $. So we
957 * need to fix up the data item by $-$$.
960 elf_add_reloc(s, segment, R_X86_64_GOTPCREL);
961 } else if (wrt == elf_gotoff_sect + 1) {
962 elf_add_reloc(s, segment, R_X86_64_GOTTPOFF);
963 } else if (wrt == elf_got_sect + 1) {
964 addr = elf_add_gsym_reloc(s, segment, addr,
965 R_X86_64_GOT32, true);
966 } else if (wrt == elf_sym_sect + 1) {
970 addr = elf_add_gsym_reloc(s, segment, addr,
974 addr = elf_add_gsym_reloc(s, segment, addr,
978 addr = elf_add_gsym_reloc(s, segment, addr,
982 error(ERR_PANIC, "internal error elf64-hpa-903");
985 } else if (wrt == elf_plt_sect + 1) {
986 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
987 "relative PLT references");
989 error(ERR_NONFATAL, "ELF format does not support this"
991 wrt = NO_SEG; /* we can at least _try_ to continue */
999 if (size != 8 && size != 4 && segment != NO_SEG) {
1001 "Unsupported non-64-bit ELF relocation");
1003 if (size == 4) WRITELONG(p, addr);
1004 else WRITEDLONG(p, (int64_t)addr);
1006 elf_sect_write(s, mydata, size);
1007 } else if (type == OUT_REL2ADR) {
1008 if (segment == segto)
1009 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
1010 if (segment != NO_SEG && segment % 2) {
1011 error(ERR_NONFATAL, "ELF format does not support"
1012 " segment base references");
1014 if (wrt == NO_SEG) {
1015 elf_add_reloc(s, segment, R_X86_64_PC16);
1018 "Unsupported non-32-bit ELF relocation [2]");
1022 WRITESHORT(p, *(int64_t *)data - size);
1023 elf_sect_write(s, mydata, 2L);
1024 } else if (type == OUT_REL4ADR) {
1025 if (segment == segto)
1026 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1027 if (segment != NO_SEG && segment % 2) {
1028 error(ERR_NONFATAL, "ELF format does not support"
1029 " segment base references");
1031 if (wrt == NO_SEG) {
1032 elf_add_reloc(s, segment, R_X86_64_PC32);
1033 } else if (wrt == elf_plt_sect + 1) {
1034 elf_add_reloc(s, segment, R_X86_64_PLT32);
1035 } else if (wrt == elf_gotpc_sect + 1 ||
1036 wrt == elf_gotoff_sect + 1 ||
1037 wrt == elf_got_sect + 1) {
1038 error(ERR_NONFATAL, "ELF format cannot produce PC-"
1039 "relative GOT references");
1041 error(ERR_NONFATAL, "ELF format does not support this"
1043 wrt = NO_SEG; /* we can at least _try_ to continue */
1047 WRITELONG(p, *(int64_t *)data - size);
1048 elf_sect_write(s, mydata, 4L);
1052 static void elf_write(void)
1062 int32_t symtablen, symtablocal;
1065 * Work out how many sections we will have. We have SHN_UNDEF,
1066 * then the flexible user sections, then the four fixed
1067 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1068 * then optionally relocation sections for the user sections.
1070 if (of_elf64.current_dfmt == &df_stabs)
1072 else if (of_elf64.current_dfmt == &df_dwarf)
1075 nsections = 5; /* SHN_UNDEF and the fixed ones */
1077 add_sectname("", ".comment");
1078 add_sectname("", ".shstrtab");
1079 add_sectname("", ".symtab");
1080 add_sectname("", ".strtab");
1081 for (i = 0; i < nsects; i++) {
1082 nsections++; /* for the section itself */
1083 if (sects[i]->head) {
1084 nsections++; /* for its relocations */
1085 add_sectname(".rela", sects[i]->name);
1089 if (of_elf64.current_dfmt == &df_stabs) {
1090 /* in case the debug information is wanted, just add these three sections... */
1091 add_sectname("", ".stab");
1092 add_sectname("", ".stabstr");
1093 add_sectname(".rel", ".stab");
1096 else if (of_elf64.current_dfmt == &df_dwarf) {
1097 /* the dwarf debug standard specifies the following ten sections,
1098 not all of which are currently implemented,
1099 although all of them are defined. */
1100 #define debug_aranges (int64_t) (nsections-10)
1101 #define debug_info (int64_t) (nsections-7)
1102 #define debug_abbrev (int64_t) (nsections-5)
1103 #define debug_line (int64_t) (nsections-4)
1104 add_sectname("", ".debug_aranges");
1105 add_sectname(".rela", ".debug_aranges");
1106 add_sectname("", ".debug_pubnames");
1107 add_sectname("", ".debug_info");
1108 add_sectname(".rela", ".debug_info");
1109 add_sectname("", ".debug_abbrev");
1110 add_sectname("", ".debug_line");
1111 add_sectname(".rela", ".debug_line");
1112 add_sectname("", ".debug_frame");
1113 add_sectname("", ".debug_loc");
1121 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1124 * Output the ELF header.
1126 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1127 fputc(elf_osabi, elffp);
1128 fputc(elf_abiver, elffp);
1129 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1130 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1131 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1132 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1133 fwriteint64_t(0L, elffp); /* no entry point */
1134 fwriteint64_t(0L, elffp); /* no program header table */
1135 fwriteint64_t(0x40L, elffp); /* section headers straight after
1136 * ELF header plus alignment */
1137 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1138 fwriteint16_t(0x40, elffp); /* size of ELF header */
1139 fwriteint16_t(0, elffp); /* no program header table, again */
1140 fwriteint16_t(0, elffp); /* still no program header table */
1141 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1142 fwriteint16_t(nsections, elffp); /* number of sections */
1143 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1144 * section header table */
1147 * Build the symbol table and relocation tables.
1149 symtab = elf_build_symtab(&symtablen, &symtablocal);
1150 for (i = 0; i < nsects; i++)
1152 sects[i]->rel = elf_build_reltab(§s[i]->rellen,
1156 * Now output the section header table.
1159 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1160 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1163 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1164 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1165 scount = 1; /* needed for the stabs debugging to track the symtable section */
1167 for (i = 0; i < nsects; i++) {
1168 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1169 (sects[i]->type == SHT_PROGBITS ?
1170 sects[i]->data : NULL), true,
1171 sects[i]->len, 0, 0, sects[i]->align, 0);
1173 scount++; /* ditto */
1175 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1176 scount++; /* ditto */
1178 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1179 scount++; /* ditto */
1181 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1182 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1184 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1185 for (i = 0; i < nsects; i++)
1186 if (sects[i]->head) {
1188 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1189 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1191 if (of_elf64.current_dfmt == &df_stabs) {
1192 /* for debugging information, create the last three sections
1193 which are the .stab , .stabstr and .rel.stab sections respectively */
1195 /* this function call creates the stab sections in memory */
1198 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1200 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1201 nsections - 2, 0, 4, 12);
1204 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1205 stabstrlen, 0, 0, 4, 0);
1208 /* link -> symtable info -> section to refer to */
1209 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1210 stabrellen, symtabsection, nsections - 3, 4,
1214 else if (of_elf64.current_dfmt == &df_dwarf) {
1215 /* for dwarf debugging information, create the ten dwarf sections */
1217 /* this function call creates the dwarf sections in memory */
1218 if (dwarf_fsect) dwarf64_generate();
1221 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1222 arangeslen, 0, 0, 1, 0);
1224 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1225 arangesrellen, symtabsection, debug_aranges, 1, 24);
1227 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1228 pubnameslen, 0, 0, 1, 0);
1230 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1231 infolen, 0, 0, 1, 0);
1233 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1234 inforellen, symtabsection, debug_info, 1, 24);
1236 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1237 abbrevlen, 0, 0, 1, 0);
1239 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1240 linelen, 0, 0, 1, 0);
1242 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1243 linerellen, symtabsection, debug_line, 1, 24);
1245 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1246 framelen, 0, 0, 8, 0);
1248 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1249 loclen, 0, 0, 1, 0);
1252 fwrite(align_str, align, 1, elffp);
1255 * Now output the sections.
1257 elf_write_sections();
1259 nasm_free(elf_sects);
1263 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1265 struct SAA *s = saa_init(1L);
1267 uint8_t entry[24], *p;
1273 * First, an all-zeros entry, required by the ELF spec.
1275 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1280 * Next, an entry for the file name.
1283 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1284 WRITESHORT(p, STT_FILE); /* type FILE */
1285 WRITESHORT(p, SHN_ABS);
1286 WRITEDLONG(p, (uint64_t) 0); /* no value */
1287 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1288 saa_wbytes(s, entry, 24L);
1293 * Now some standard symbols defining the segments, for relocation
1296 for (i = 1; i <= nsects; i++) {
1298 WRITELONG(p, 0); /* no symbol name */
1299 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1300 WRITESHORT(p, i); /* section id */
1301 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1302 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1303 saa_wbytes(s, entry, 24L);
1310 * Now the other local symbols.
1313 while ((sym = saa_rstruct(syms))) {
1314 if (sym->type & SYM_GLOBAL)
1317 WRITELONG(p, sym->strpos); /* index into symbol string table */
1318 WRITECHAR(p, sym->type); /* type and binding */
1319 WRITECHAR(p, sym->other); /* visibility */
1320 WRITESHORT(p, sym->section); /* index into section header table */
1321 WRITEDLONG(p, (int64_t)sym->value); /* value of symbol */
1322 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1323 saa_wbytes(s, entry, 24L);
1328 * dwarf needs symbols for debug sections
1329 * which are relocation targets.
1331 if (of_elf64.current_dfmt == &df_dwarf) {
1332 dwarf_infosym = *local;
1334 WRITELONG(p, 0); /* no symbol name */
1335 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1336 WRITESHORT(p, debug_info); /* section id */
1337 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1338 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1339 saa_wbytes(s, entry, 24L);
1342 dwarf_abbrevsym = *local;
1344 WRITELONG(p, 0); /* no symbol name */
1345 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1346 WRITESHORT(p, debug_abbrev); /* section id */
1347 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1348 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1349 saa_wbytes(s, entry, 24L);
1352 dwarf_linesym = *local;
1354 WRITELONG(p, 0); /* no symbol name */
1355 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1356 WRITESHORT(p, debug_line); /* section id */
1357 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1358 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1359 saa_wbytes(s, entry, 24L);
1365 * Now the global symbols.
1368 while ((sym = saa_rstruct(syms))) {
1369 if (!(sym->type & SYM_GLOBAL))
1372 WRITELONG(p, sym->strpos);
1373 WRITECHAR(p, sym->type); /* type and binding */
1374 WRITECHAR(p, sym->other); /* visibility */
1375 WRITESHORT(p, sym->section);
1376 WRITEDLONG(p, (int64_t)sym->value);
1377 WRITEDLONG(p, (int64_t)sym->size);
1378 saa_wbytes(s, entry, 24L);
1385 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1388 uint8_t *p, entry[24];
1397 int64_t sym = r->symbol;
1399 if (sym >= GLOBAL_TEMP_BASE)
1401 if (of_elf64.current_dfmt == &df_dwarf)
1402 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1403 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1406 WRITEDLONG(p, r->address);
1407 WRITEDLONG(p, (sym << 32) + r->type);
1408 WRITEDLONG(p, (uint64_t) 0);
1409 saa_wbytes(s, entry, 24L);
1418 static void elf_section_header(int name, int type, uint64_t flags,
1419 void *data, bool is_saa, uint64_t datalen,
1420 int link, int info, int align, int eltsize)
1422 elf_sects[elf_nsect].data = data;
1423 elf_sects[elf_nsect].len = datalen;
1424 elf_sects[elf_nsect].is_saa = is_saa;
1427 fwriteint32_t((int32_t)name, elffp);
1428 fwriteint32_t((int32_t)type, elffp);
1429 fwriteint64_t((int64_t)flags, elffp);
1430 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1431 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1432 fwriteint64_t(datalen, elffp);
1434 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1435 fwriteint32_t((int32_t)link, elffp);
1436 fwriteint32_t((int32_t)info, elffp);
1437 fwriteint64_t((int64_t)align, elffp);
1438 fwriteint64_t((int64_t)eltsize, elffp);
1441 static void elf_write_sections(void)
1444 for (i = 0; i < elf_nsect; i++)
1445 if (elf_sects[i].data) {
1446 int32_t len = elf_sects[i].len;
1447 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1448 int32_t align = reallen - len;
1449 if (elf_sects[i].is_saa)
1450 saa_fpwrite(elf_sects[i].data, elffp);
1452 fwrite(elf_sects[i].data, len, 1, elffp);
1453 fwrite(align_str, align, 1, elffp);
1457 static void elf_sect_write(struct Section *sect,
1458 const uint8_t *data, uint64_t len)
1460 saa_wbytes(sect->data, data, len);
1464 static int32_t elf_segbase(int32_t segment)
1469 static int elf_directive(char *directive, char *value, int pass)
1475 if (!strcmp(directive, "osabi")) {
1477 return 1; /* ignore in pass 2 */
1479 n = readnum(value, &err);
1481 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1484 if (n < 0 || n > 255) {
1485 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1491 if ((p = strchr(value,',')) == NULL)
1494 n = readnum(p+1, &err);
1495 if (err || n < 0 || n > 255) {
1496 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1507 static void elf_filename(char *inname, char *outname, efunc error)
1509 strcpy(elf_module, inname);
1510 standard_extension(inname, outname, ".o", error);
1513 static const char *elf_stdmac[] = {
1514 "%define __SECT__ [section .text]",
1515 "%macro __NASM_CDecl__ 1",
1518 "%macro osabi 1+.nolist",
1523 static int elf_set_info(enum geninfo type, char **val)
1529 static struct dfmt df_dwarf = {
1530 "ELF64 (X86_64) dwarf debug format for Linux",
1540 static struct dfmt df_stabs = {
1541 "ELF64 (X86_64) stabs debug format for Linux",
1552 struct dfmt *elf64_debugs_arr[3] = { &df_stabs, &df_dwarf, NULL };
1554 struct ofmt of_elf64 = {
1555 "ELF64 (x86_64) object files (e.g. Linux)",
1572 /* common debugging routines */
1573 void debug64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1580 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1590 void debug64_directive(const char *directive, const char *params)
1596 void debug64_typevalue(int32_t type)
1598 int32_t stype, ssize;
1599 switch (TYM_TYPE(type)) {
1638 stype = STT_SECTION;
1653 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1654 lastsym->size = ssize;
1655 lastsym->type = stype;
1659 /* stabs debugging routines */
1662 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1665 if (!stabs_filename) {
1666 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1667 strcpy(stabs_filename, filename);
1669 if (strcmp(stabs_filename, filename)) {
1670 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1671 in fact, this leak comes in quite handy to maintain a list of files
1672 encountered so far in the symbol lines... */
1674 /* why not nasm_free(stabs_filename); we're done with the old one */
1676 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1677 strcpy(stabs_filename, filename);
1681 currentline = linenumber;
1685 void stabs64_output(int type, void *param)
1687 struct symlininfo *s;
1688 struct linelist *el;
1689 if (type == TY_DEBUGSYMLIN) {
1690 if (debug_immcall) {
1691 s = (struct symlininfo *)param;
1692 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1693 return; /* line info is only collected for executable sections */
1695 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1696 el->info.offset = s->offset;
1697 el->info.section = s->section;
1698 el->info.name = s->name;
1699 el->line = currentline;
1700 el->filename = stabs_filename;
1703 stabslines->last->next = el;
1704 stabslines->last = el;
1707 stabslines->last = el;
1714 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1716 WRITELONG(p,n_strx); \
1717 WRITECHAR(p,n_type); \
1718 WRITECHAR(p,n_other); \
1719 WRITESHORT(p,n_desc); \
1720 WRITELONG(p,n_value); \
1723 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1725 void stabs64_generate(void)
1727 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1728 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1732 struct linelist *ptr;
1736 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1737 for (i = 0; i < numlinestabs; i++)
1741 if (numfiles == 0) {
1742 allfiles[0] = ptr->filename;
1745 for (i = 0; i < numfiles; i++) {
1746 if (!strcmp(allfiles[i], ptr->filename))
1749 if (i >= numfiles) {
1750 allfiles[i] = ptr->filename;
1757 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1758 for (i = 0; i < numfiles; i++) {
1759 fileidx[i] = strsize;
1760 strsize += strlen(allfiles[i]) + 1;
1763 for (i = 0; i < numfiles; i++) {
1764 if (!strcmp(allfiles[i], elf_module)) {
1770 /* worst case size of the stab buffer would be:
1771 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1774 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1775 sizeof(struct stabentry));
1777 ssbuf = (uint8_t *)nasm_malloc(strsize);
1779 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1782 for (i = 0; i < numfiles; i++) {
1783 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1787 stabstrlen = strsize; /* set global variable for length of stab strings */
1794 /* this is the first stab, its strx points to the filename of the
1795 the source-file, the n_desc field should be set to the number
1798 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1800 /* this is the stab for the main source file */
1801 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1803 /* relocation table entry */
1805 /* Since the symbol table has two entries before */
1806 /* the section symbols, the index in the info.section */
1807 /* member must be adjusted by adding 2 */
1809 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1810 WRITELONG(rptr, R_X86_64_32);
1811 WRITELONG(rptr, ptr->info.section + 2);
1814 currfile = mainfileindex;
1818 if (strcmp(allfiles[currfile], ptr->filename)) {
1819 /* oops file has changed... */
1820 for (i = 0; i < numfiles; i++)
1821 if (!strcmp(allfiles[i], ptr->filename))
1824 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1828 /* relocation table entry */
1830 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1831 WRITELONG(rptr, R_X86_64_32);
1832 WRITELONG(rptr, ptr->info.section + 2);
1835 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1838 /* relocation table entry */
1840 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1841 WRITELONG(rptr, R_X86_64_32);
1842 WRITELONG(rptr, ptr->info.section + 2);
1848 ((struct stabentry *)sbuf)->n_desc = numstabs;
1850 nasm_free(allfiles);
1853 stablen = (sptr - sbuf);
1854 stabrellen = (rptr - rbuf);
1860 void stabs64_cleanup(void)
1862 struct linelist *ptr, *del;
1874 nasm_free(stabrelbuf);
1876 nasm_free(stabstrbuf);
1878 /* dwarf routines */
1881 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1884 dwarf64_findfile(filename);
1886 currentline = linenumber;
1889 /* called from elf_out with type == TY_DEBUGSYMLIN */
1890 void dwarf64_output(int type, void *param)
1892 int ln, aa, inx, maxln, soc;
1893 struct symlininfo *s;
1898 s = (struct symlininfo *)param;
1899 /* line number info is only gathered for executable sections */
1900 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1902 /* Check if section index has changed */
1903 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1905 dwarf64_findsect(s->section);
1907 /* do nothing unless line or file has changed */
1910 ln = currentline - dwarf_csect->line;
1911 aa = s->offset - dwarf_csect->offset;
1912 inx = dwarf_clist->line;
1913 plinep = dwarf_csect->psaa;
1914 /* check for file change */
1915 if (!(inx == dwarf_csect->file))
1917 WSAACHAR(plinep,DW_LNS_set_file);
1918 WSAACHAR(plinep,inx);
1919 dwarf_csect->file = inx;
1921 /* check for line change */
1924 /* test if in range of special op code */
1925 maxln = line_base + line_range;
1926 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1927 if (ln >= line_base && ln < maxln && soc < 256)
1929 WSAACHAR(plinep,soc);
1935 WSAACHAR(plinep,DW_LNS_advance_line);
1936 saa_wleb128s(plinep,ln);
1940 WSAACHAR(plinep,DW_LNS_advance_pc);
1941 saa_wleb128u(plinep,aa);
1944 dwarf_csect->line = currentline;
1945 dwarf_csect->offset = s->offset;
1947 /* show change handled */
1953 void dwarf64_generate(void)
1955 static const char nasm_signature[] = "NASM " NASM_VER;
1958 struct linelist *ftentry;
1959 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1960 struct SAA *parangesrel, *plinesrel, *pinforel;
1961 struct sectlist *psect;
1962 size_t saalen, linepoff, totlen, highaddr;
1964 /* write epilogues for each line program range */
1965 /* and build aranges section */
1966 paranges = saa_init(1L);
1967 parangesrel = saa_init(1L);
1968 WSAASHORT(paranges,3); /* dwarf version */
1969 WSAADLONG(parangesrel, paranges->datalen+4);
1970 WSAADLONG(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1971 WSAADLONG(parangesrel, 0);
1972 WSAALONG(paranges,0); /* offset into info */
1973 WSAACHAR(paranges,8); /* pointer size */
1974 WSAACHAR(paranges,0); /* not segmented */
1975 WSAALONG(paranges,0); /* padding */
1976 /* iterate though sectlist entries */
1977 psect = dwarf_fsect;
1980 for (indx = 0; indx < dwarf_nsections; indx++)
1982 plinep = psect->psaa;
1983 /* Line Number Program Epilogue */
1984 WSAACHAR(plinep,2); /* std op 2 */
1985 WSAACHAR(plinep,(sects[psect->section]->len)-psect->offset);
1986 WSAACHAR(plinep,DW_LNS_extended_op);
1987 WSAACHAR(plinep,1); /* operand length */
1988 WSAACHAR(plinep,DW_LNE_end_sequence);
1989 totlen += plinep->datalen;
1990 /* range table relocation entry */
1991 WSAADLONG(parangesrel, paranges->datalen + 4);
1992 WSAADLONG(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1993 WSAADLONG(parangesrel, (uint64_t) 0);
1994 /* range table entry */
1995 WSAADLONG(paranges,0x0000); /* range start */
1996 WSAADLONG(paranges,sects[psect->section]->len); /* range length */
1997 highaddr += sects[psect->section]->len;
1998 /* done with this entry */
1999 psect = psect->next;
2001 WSAADLONG(paranges,0); /* null address */
2002 WSAADLONG(paranges,0); /* null length */
2003 saalen = paranges->datalen;
2004 arangeslen = saalen + 4;
2005 arangesbuf = pbuf = nasm_malloc(arangeslen);
2006 WRITELONG(pbuf,saalen); /* initial length */
2007 saa_rnbytes(paranges, pbuf, saalen);
2010 /* build rela.aranges section */
2011 arangesrellen = saalen = parangesrel->datalen;
2012 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2013 saa_rnbytes(parangesrel, pbuf, saalen);
2014 saa_free(parangesrel);
2016 /* build pubnames section */
2017 ppubnames = saa_init(1L);
2018 WSAASHORT(ppubnames,3); /* dwarf version */
2019 WSAALONG(ppubnames,0); /* offset into info */
2020 WSAALONG(ppubnames,0); /* space used in info */
2021 WSAALONG(ppubnames,0); /* end of list */
2022 saalen = ppubnames->datalen;
2023 pubnameslen = saalen + 4;
2024 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2025 WRITELONG(pbuf,saalen); /* initial length */
2026 saa_rnbytes(ppubnames, pbuf, saalen);
2027 saa_free(ppubnames);
2029 /* build info section */
2030 pinfo = saa_init(1L);
2031 pinforel = saa_init(1L);
2032 WSAASHORT(pinfo,3); /* dwarf version */
2033 WSAADLONG(pinforel, pinfo->datalen + 4);
2034 WSAADLONG(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2035 WSAADLONG(pinforel, 0);
2036 WSAALONG(pinfo,0); /* offset into abbrev */
2037 WSAACHAR(pinfo,8); /* pointer size */
2038 WSAACHAR(pinfo,1); /* abbrviation number LEB128u */
2039 WSAADLONG(pinforel, pinfo->datalen + 4);
2040 WSAADLONG(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2041 WSAADLONG(pinforel, 0);
2042 WSAADLONG(pinfo,0); /* DW_AT_low_pc */
2043 WSAADLONG(pinforel, pinfo->datalen + 4);
2044 WSAADLONG(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2045 WSAADLONG(pinforel, 0);
2046 WSAADLONG(pinfo,highaddr); /* DW_AT_high_pc */
2047 WSAADLONG(pinforel, pinfo->datalen + 4);
2048 WSAADLONG(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2049 WSAADLONG(pinforel, 0);
2050 WSAALONG(pinfo,0); /* DW_AT_stmt_list */
2051 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2052 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2053 WSAASHORT(pinfo,DW_LANG_Mips_Assembler);
2054 WSAACHAR(pinfo,2); /* abbrviation number LEB128u */
2055 WSAADLONG(pinforel, pinfo->datalen + 4);
2056 WSAADLONG(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2057 WSAADLONG(pinforel, 0);
2058 WSAADLONG(pinfo,0); /* DW_AT_low_pc */
2059 WSAADLONG(pinfo,0); /* DW_AT_frame_base */
2060 WSAACHAR(pinfo,0); /* end of entries */
2061 saalen = pinfo->datalen;
2062 infolen = saalen + 4;
2063 infobuf = pbuf = nasm_malloc(infolen);
2064 WRITELONG(pbuf,saalen); /* initial length */
2065 saa_rnbytes(pinfo, pbuf, saalen);
2068 /* build rela.info section */
2069 inforellen = saalen = pinforel->datalen;
2070 inforelbuf = pbuf = nasm_malloc(inforellen);
2071 saa_rnbytes(pinforel, pbuf, saalen);
2074 /* build abbrev section */
2075 pabbrev = saa_init(1L);
2076 WSAACHAR(pabbrev,1); /* entry number LEB128u */
2077 WSAACHAR(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2078 WSAACHAR(pabbrev,1); /* has children */
2079 /* the following attributes and forms are all LEB128u values */
2080 WSAACHAR(pabbrev,DW_AT_low_pc);
2081 WSAACHAR(pabbrev,DW_FORM_addr);
2082 WSAACHAR(pabbrev,DW_AT_high_pc);
2083 WSAACHAR(pabbrev,DW_FORM_addr);
2084 WSAACHAR(pabbrev,DW_AT_stmt_list);
2085 WSAACHAR(pabbrev,DW_FORM_data4);
2086 WSAACHAR(pabbrev,DW_AT_name);
2087 WSAACHAR(pabbrev,DW_FORM_string);
2088 WSAACHAR(pabbrev,DW_AT_producer);
2089 WSAACHAR(pabbrev,DW_FORM_string);
2090 WSAACHAR(pabbrev,DW_AT_language);
2091 WSAACHAR(pabbrev,DW_FORM_data2);
2092 WSAASHORT(pabbrev,0); /* end of entry */
2093 /* LEB128u usage same as above */
2094 WSAACHAR(pabbrev,2); /* entry number */
2095 WSAACHAR(pabbrev,DW_TAG_subprogram);
2096 WSAACHAR(pabbrev,0); /* no children */
2097 WSAACHAR(pabbrev,DW_AT_low_pc);
2098 WSAACHAR(pabbrev,DW_FORM_addr);
2099 WSAACHAR(pabbrev,DW_AT_frame_base);
2100 WSAACHAR(pabbrev,DW_FORM_data4);
2101 WSAASHORT(pabbrev,0); /* end of entry */
2102 abbrevlen = saalen = pabbrev->datalen;
2103 abbrevbuf = pbuf = nasm_malloc(saalen);
2104 saa_rnbytes(pabbrev, pbuf, saalen);
2107 /* build line section */
2109 plines = saa_init(1L);
2110 WSAACHAR(plines,1); /* Minimum Instruction Length */
2111 WSAACHAR(plines,1); /* Initial value of 'is_stmt' */
2112 WSAACHAR(plines,line_base); /* Line Base */
2113 WSAACHAR(plines,line_range); /* Line Range */
2114 WSAACHAR(plines,opcode_base); /* Opcode Base */
2115 /* standard opcode lengths (# of LEB128u operands) */
2116 WSAACHAR(plines,0); /* Std opcode 1 length */
2117 WSAACHAR(plines,1); /* Std opcode 2 length */
2118 WSAACHAR(plines,1); /* Std opcode 3 length */
2119 WSAACHAR(plines,1); /* Std opcode 4 length */
2120 WSAACHAR(plines,1); /* Std opcode 5 length */
2121 WSAACHAR(plines,0); /* Std opcode 6 length */
2122 WSAACHAR(plines,0); /* Std opcode 7 length */
2123 WSAACHAR(plines,0); /* Std opcode 8 length */
2124 WSAACHAR(plines,1); /* Std opcode 9 length */
2125 WSAACHAR(plines,0); /* Std opcode 10 length */
2126 WSAACHAR(plines,0); /* Std opcode 11 length */
2127 WSAACHAR(plines,1); /* Std opcode 12 length */
2128 /* Directory Table */
2129 WSAACHAR(plines,0); /* End of table */
2130 /* File Name Table */
2131 ftentry = dwarf_flist;
2132 for (indx = 0;indx<dwarf_numfiles;indx++)
2134 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2135 WSAACHAR(plines,0); /* directory LEB128u */
2136 WSAACHAR(plines,0); /* time LEB128u */
2137 WSAACHAR(plines,0); /* size LEB128u */
2138 ftentry = ftentry->next;
2140 WSAACHAR(plines,0); /* End of table */
2141 linepoff = plines->datalen;
2142 linelen = linepoff + totlen + 10;
2143 linebuf = pbuf = nasm_malloc(linelen);
2144 WRITELONG(pbuf,linelen-4); /* initial length */
2145 WRITESHORT(pbuf,3); /* dwarf version */
2146 WRITELONG(pbuf,linepoff); /* offset to line number program */
2147 /* write line header */
2149 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2152 /* concatonate line program ranges */
2154 plinesrel = saa_init(1L);
2155 psect = dwarf_fsect;
2156 for (indx = 0; indx < dwarf_nsections; indx++)
2158 WSAADLONG(plinesrel, linepoff);
2159 WSAADLONG(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2160 WSAADLONG(plinesrel, (uint64_t) 0);
2161 plinep = psect->psaa;
2162 saalen = plinep->datalen;
2163 saa_rnbytes(plinep, pbuf, saalen);
2167 /* done with this entry */
2168 psect = psect->next;
2172 /* build rela.lines section */
2173 linerellen =saalen = plinesrel->datalen;
2174 linerelbuf = pbuf = nasm_malloc(linerellen);
2175 saa_rnbytes(plinesrel, pbuf, saalen);
2176 saa_free(plinesrel);
2178 /* build frame section */
2180 framebuf = pbuf = nasm_malloc(framelen);
2181 WRITELONG(pbuf,framelen-4); /* initial length */
2183 /* build loc section */
2185 locbuf = pbuf = nasm_malloc(loclen);
2186 WRITEDLONG(pbuf,0); /* null beginning offset */
2187 WRITEDLONG(pbuf,0); /* null ending offset */
2190 void dwarf64_cleanup(void)
2193 nasm_free(arangesbuf);
2195 nasm_free(arangesrelbuf);
2197 nasm_free(pubnamesbuf);
2201 nasm_free(inforelbuf);
2203 nasm_free(abbrevbuf);
2207 nasm_free(linerelbuf);
2209 nasm_free(framebuf);
2213 void dwarf64_findfile(const char * fname)
2216 struct linelist *match;
2218 /* return if fname is current file name */
2219 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2220 /* search for match */
2226 match = dwarf_flist;
2227 for (finx = 0; finx < dwarf_numfiles; finx++)
2229 if (!(strcmp(fname, match->filename)))
2231 dwarf_clist = match;
2236 /* add file name to end of list */
2237 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2239 dwarf_clist->line = dwarf_numfiles;
2240 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2241 strcpy(dwarf_clist->filename,fname);
2242 dwarf_clist->next = 0;
2243 /* if first entry */
2246 dwarf_flist = dwarf_elist = dwarf_clist;
2247 dwarf_clist->last = 0;
2249 /* chain to previous entry */
2252 dwarf_elist->next = dwarf_clist;
2253 dwarf_elist = dwarf_clist;
2258 void dwarf64_findsect(const int index)
2261 struct sectlist *match;
2263 /* return if index is current section index */
2264 if (dwarf_csect && (dwarf_csect->section == index))
2268 /* search for match */
2274 match = dwarf_fsect;
2275 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2277 if ((match->section == index))
2279 dwarf_csect = match;
2282 match = match->next;
2285 /* add entry to end of list */
2286 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2288 dwarf_csect->psaa = plinep = saa_init(1L);
2289 dwarf_csect->line = 1;
2290 dwarf_csect->offset = 0;
2291 dwarf_csect->file = 1;
2292 dwarf_csect->section = index;
2293 dwarf_csect->next = 0;
2294 /* set relocatable address at start of line program */
2295 WSAACHAR(plinep,DW_LNS_extended_op);
2296 WSAACHAR(plinep,9); /* operand length */
2297 WSAACHAR(plinep,DW_LNE_set_address);
2298 WSAADLONG(plinep,0); /* Start Address */
2299 /* if first entry */
2302 dwarf_fsect = dwarf_esect = dwarf_csect;
2303 dwarf_csect->last = 0;
2305 /* chain to previous entry */
2308 dwarf_esect->next = dwarf_csect;
2309 dwarf_esect = dwarf_csect;