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.
32 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
36 int64_t address
; /* relative to _start_ of section */
37 int64_t symbol
; /* symbol index */
38 int64_t offset
; /* symbol addend */
39 int type
; /* type of relocation */
43 struct rbtree symv
; /* symbol value and rbtree of globals */
44 int32_t strpos
; /* string table position of name */
45 int32_t section
; /* section ID of the symbol */
46 int type
; /* symbol type */
47 int other
; /* symbol visibility */
48 int32_t size
; /* size of symbol */
49 int32_t globnum
; /* symbol table offset if global */
50 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
51 char *name
; /* used temporarily if in above list */
58 int32_t index
; /* index into sects array */
59 uint32_t type
; /* SHT_PROGBITS or SHT_NOBITS */
60 uint64_t align
; /* alignment: power of two */
61 uint64_t flags
; /* section flags */
65 struct Reloc
*head
, **tail
;
66 struct rbtree
*gsyms
; /* global symbols in section */
70 static struct Section
**sects
;
71 static int nsects
, sectlen
;
73 #define SHSTR_DELTA 256
74 static char *shstrtab
;
75 static int shstrtablen
, shstrtabsize
;
77 static struct SAA
*syms
;
78 static uint32_t nlocals
, nglobs
;
80 static int32_t def_seg
;
82 static struct RAA
*bsym
;
84 static struct SAA
*strs
;
85 static uint32_t strslen
;
89 static evalfunc evaluate
;
91 static struct Symbol
*fwds
;
93 static char elf_module
[FILENAME_MAX
];
95 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
96 static uint8_t elf_abiver
= 0; /* Current ABI version */
98 extern struct ofmt of_elf64
;
100 static struct ELF_SECTDATA
{
105 static int elf_nsect
, nsections
;
106 static int64_t elf_foffs
;
108 static void elf_write(void);
109 static void elf_sect_write(struct Section
*, const void *, size_t);
110 static void elf_sect_writeaddr(struct Section
*, int64_t, size_t);
111 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
113 static void elf_write_sections(void);
114 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
115 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
116 static void add_sectname(char *, char *);
118 /* type values for stabs debugging sections */
119 #define N_SO 0x64 /* ID for main source file */
120 #define N_SOL 0x84 /* ID for sub-source file */
121 #define N_BINCL 0x82 /* not currently used */
122 #define N_EINCL 0xA2 /* not currently used */
139 int section
; /* index into sects[] */
140 int segto
; /* internal section number */
141 char *name
; /* shallow-copied pointer of section name */
145 struct symlininfo info
;
148 struct linelist
*next
;
149 struct linelist
*last
;
158 struct sectlist
*next
;
159 struct sectlist
*last
;
162 /* common debug variables */
163 static int currentline
= 1;
164 static int debug_immcall
= 0;
166 /* stabs debug variables */
167 static struct linelist
*stabslines
= 0;
168 static int numlinestabs
= 0;
169 static char *stabs_filename
= 0;
170 static int symtabsection
;
171 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
172 static int stablen
, stabstrlen
, stabrellen
;
174 /* dwarf debug variables */
175 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
176 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
177 static int dwarf_numfiles
= 0, dwarf_nsections
;
178 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
179 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
180 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
181 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
182 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
183 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
186 static struct dfmt df_dwarf
;
187 static struct dfmt df_stabs
;
188 static struct Symbol
*lastsym
;
190 /* common debugging routines */
191 void debug64_typevalue(int32_t);
192 void debug64_init(struct ofmt
*, void *, FILE *, efunc
);
193 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
194 void debug64_directive(const char *, const char *);
196 /* stabs debugging routines */
197 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
198 void stabs64_output(int, void *);
199 void stabs64_generate(void);
200 void stabs64_cleanup(void);
202 /* dwarf debugging routines */
203 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
204 void dwarf64_output(int, void *);
205 void dwarf64_generate(void);
206 void dwarf64_cleanup(void);
207 void dwarf64_findfile(const char *);
208 void dwarf64_findsect(const int);
211 * Special section numbers which are used to define ELF special
212 * symbols, which can be used with WRT to provide PIC relocation
215 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
216 static int32_t elf_got_sect
, elf_plt_sect
;
217 static int32_t elf_sym_sect
;
218 static int32_t elf_gottpoff_sect
;
220 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
226 (void)ldef
; /* placate optimisers */
228 nsects
= sectlen
= 0;
229 syms
= saa_init((int32_t)sizeof(struct Symbol
));
230 nlocals
= nglobs
= 0;
233 saa_wbytes(strs
, "\0", 1L);
234 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
235 strslen
= 2 + strlen(elf_module
);
237 shstrtablen
= shstrtabsize
= 0;;
238 add_sectname("", "");
242 elf_gotpc_sect
= seg_alloc();
243 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
245 elf_gotoff_sect
= seg_alloc();
246 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
248 elf_got_sect
= seg_alloc();
249 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
251 elf_plt_sect
= seg_alloc();
252 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
254 elf_sym_sect
= seg_alloc();
255 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
257 elf_gottpoff_sect
= seg_alloc();
258 ldef("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
261 def_seg
= seg_alloc();
265 static void elf_cleanup(int debuginfo
)
274 for (i
= 0; i
< nsects
; i
++) {
275 if (sects
[i
]->type
!= SHT_NOBITS
)
276 saa_free(sects
[i
]->data
);
278 saa_free(sects
[i
]->rel
);
279 while (sects
[i
]->head
) {
281 sects
[i
]->head
= sects
[i
]->head
->next
;
289 if (of_elf64
.current_dfmt
) {
290 of_elf64
.current_dfmt
->cleanup();
293 /* add entry to the elf .shstrtab section */
294 static void add_sectname(char *firsthalf
, char *secondhalf
)
296 int len
= strlen(firsthalf
) + strlen(secondhalf
);
297 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
298 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
299 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
300 strcat(shstrtab
+ shstrtablen
, secondhalf
);
301 shstrtablen
+= len
+ 1;
304 static int elf_make_section(char *name
, int type
, int flags
, int align
)
308 s
= nasm_malloc(sizeof(*s
));
310 if (type
!= SHT_NOBITS
)
311 s
->data
= saa_init(1L);
314 s
->len
= s
->size
= 0;
316 if (!strcmp(name
, ".text"))
319 s
->index
= seg_alloc();
320 add_sectname("", name
);
321 s
->name
= nasm_malloc(1 + strlen(name
));
322 strcpy(s
->name
, name
);
328 if (nsects
>= sectlen
)
330 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
336 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
339 uint32_t flags
, flags_and
, flags_or
;
344 * Default is 64 bits.
352 while (*p
&& !nasm_isspace(*p
))
356 flags_and
= flags_or
= type
= align
= 0;
358 while (*p
&& nasm_isspace(*p
))
362 while (*p
&& !nasm_isspace(*p
))
366 while (*p
&& nasm_isspace(*p
))
369 if (!nasm_strnicmp(q
, "align=", 6)) {
373 if ((align
- 1) & align
) { /* means it's not a power of two */
374 error(ERR_NONFATAL
, "section alignment %d is not"
375 " a power of two", align
);
378 } else if (!nasm_stricmp(q
, "alloc")) {
379 flags_and
|= SHF_ALLOC
;
380 flags_or
|= SHF_ALLOC
;
381 } else if (!nasm_stricmp(q
, "noalloc")) {
382 flags_and
|= SHF_ALLOC
;
383 flags_or
&= ~SHF_ALLOC
;
384 } else if (!nasm_stricmp(q
, "exec")) {
385 flags_and
|= SHF_EXECINSTR
;
386 flags_or
|= SHF_EXECINSTR
;
387 } else if (!nasm_stricmp(q
, "noexec")) {
388 flags_and
|= SHF_EXECINSTR
;
389 flags_or
&= ~SHF_EXECINSTR
;
390 } else if (!nasm_stricmp(q
, "write")) {
391 flags_and
|= SHF_WRITE
;
392 flags_or
|= SHF_WRITE
;
393 } else if (!nasm_stricmp(q
, "tls")) {
394 flags_and
|= SHF_TLS
;
396 } else if (!nasm_stricmp(q
, "nowrite")) {
397 flags_and
|= SHF_WRITE
;
398 flags_or
&= ~SHF_WRITE
;
399 } else if (!nasm_stricmp(q
, "progbits")) {
401 } else if (!nasm_stricmp(q
, "nobits")) {
403 } else if (pass
== 1) error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
404 " declaration of section `%s'", q
, name
);
407 if (!strcmp(name
, ".shstrtab") ||
408 !strcmp(name
, ".symtab") ||
409 !strcmp(name
, ".strtab")) {
410 error(ERR_NONFATAL
, "attempt to redefine reserved section"
415 for (i
= 0; i
< nsects
; i
++)
416 if (!strcmp(name
, sects
[i
]->name
))
419 const struct elf_known_section
*ks
= elf_known_sections
;
422 if (!strcmp(name
, ks
->name
))
427 type
= type
? type
: ks
->type
;
428 align
= align
? align
: ks
->align
;
429 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
431 i
= elf_make_section(name
, type
, flags
, align
);
432 } else if (pass
== 1) {
433 if ((type
&& sects
[i
]->type
!= type
)
434 || (align
&& sects
[i
]->align
!= align
)
435 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
436 error(ERR_WARNING
, "incompatible section attributes ignored on"
437 " redeclaration of section `%s'", name
);
440 return sects
[i
]->index
;
443 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
444 int is_global
, char *special
)
448 bool special_used
= false;
450 #if defined(DEBUG) && DEBUG>2
452 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
453 name
, segment
, offset
, is_global
, special
);
455 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
457 * This is a NASM special symbol. We never allow it into
458 * the ELF symbol table, even if it's a valid one. If it
459 * _isn't_ a valid one, we should barf immediately.
461 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
462 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
463 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
464 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
468 if (is_global
== 3) {
471 * Fix up a forward-reference symbol size from the first
474 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
475 if (!strcmp((*s
)->name
, name
)) {
476 struct tokenval tokval
;
480 while (*p
&& !nasm_isspace(*p
))
482 while (*p
&& nasm_isspace(*p
))
486 tokval
.t_type
= TOKEN_INVALID
;
487 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
490 error(ERR_NONFATAL
, "cannot use relocatable"
491 " expression as symbol size");
493 (*s
)->size
= reloc_value(e
);
497 * Remove it from the list of unresolved sizes.
499 nasm_free((*s
)->name
);
503 return; /* it wasn't an important one */
506 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
507 strslen
+= 1 + strlen(name
);
509 lastsym
= sym
= saa_wstruct(syms
);
511 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
514 sym
->type
= is_global
? SYM_GLOBAL
: 0;
515 sym
->other
= STV_DEFAULT
;
517 if (segment
== NO_SEG
)
518 sym
->section
= SHN_ABS
;
521 sym
->section
= SHN_UNDEF
;
522 if (nsects
== 0 && segment
== def_seg
) {
524 if (segment
!= elf_section_names(".text", 2, &tempint
))
526 "strange segment conditions in ELF driver");
527 sym
->section
= nsects
;
529 for (i
= 0; i
< nsects
; i
++)
530 if (segment
== sects
[i
]->index
) {
531 sym
->section
= i
+ 1;
537 if (is_global
== 2) {
540 sym
->section
= SHN_COMMON
;
542 * We have a common variable. Check the special text to see
543 * if it's a valid number and power of two; if so, store it
544 * as the alignment for the common variable.
548 sym
->symv
.key
= readnum(special
, &err
);
550 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
551 " valid number", special
);
552 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
553 != 2 * sym
->symv
.key
- 1)
554 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
555 " power of two", special
);
559 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
561 if (sym
->type
== SYM_GLOBAL
) {
563 * If sym->section == SHN_ABS, then the first line of the
564 * else section would cause a core dump, because its a reference
565 * beyond the end of the section array.
566 * This behaviour is exhibited by this code:
569 * To avoid such a crash, such requests are silently discarded.
570 * This may not be the best solution.
572 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
573 bsym
= raa_write(bsym
, segment
, nglobs
);
574 } else if (sym
->section
!= SHN_ABS
) {
576 * This is a global symbol; so we must add it to the rbtree
577 * of global symbols in its section.
579 * In addition, we check the special text for symbol
580 * type and size information.
582 sects
[sym
->section
-1]->gsyms
=
583 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
586 int n
= strcspn(special
, " \t");
588 if (!nasm_strnicmp(special
, "function", n
))
589 sym
->type
|= STT_FUNC
;
590 else if (!nasm_strnicmp(special
, "data", n
) ||
591 !nasm_strnicmp(special
, "object", n
))
592 sym
->type
|= STT_OBJECT
;
593 else if (!nasm_strnicmp(special
, "notype", n
))
594 sym
->type
|= STT_NOTYPE
;
596 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
600 while (nasm_isspace(*special
))
603 n
= strcspn(special
, " \t");
604 if (!nasm_strnicmp(special
, "default", n
))
605 sym
->other
= STV_DEFAULT
;
606 else if (!nasm_strnicmp(special
, "internal", n
))
607 sym
->other
= STV_INTERNAL
;
608 else if (!nasm_strnicmp(special
, "hidden", n
))
609 sym
->other
= STV_HIDDEN
;
610 else if (!nasm_strnicmp(special
, "protected", n
))
611 sym
->other
= STV_PROTECTED
;
618 struct tokenval tokval
;
621 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
623 while (special
[n
] && nasm_isspace(special
[n
]))
626 * We have a size expression; attempt to
630 stdscan_bufptr
= special
+ n
;
631 tokval
.t_type
= TOKEN_INVALID
;
632 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
637 sym
->name
= nasm_strdup(name
);
640 error(ERR_NONFATAL
, "cannot use relocatable"
641 " expression as symbol size");
643 sym
->size
= reloc_value(e
);
645 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
650 * If TLS segment, mark symbol accordingly.
652 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
654 sym
->type
|= STT_TLS
;
657 sym
->globnum
= nglobs
;
662 if (special
&& !special_used
)
663 error(ERR_NONFATAL
, "no special symbol features supported here");
666 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
667 int64_t offset
, int type
)
670 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
671 sect
->tail
= &r
->next
;
674 r
->address
= sect
->len
;
676 if (segment
== NO_SEG
)
681 for (i
= 0; i
< nsects
; i
++)
682 if (segment
== sects
[i
]->index
)
685 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
693 * This routine deals with ..got and ..sym relocations: the more
694 * complicated kinds. In shared-library writing, some relocations
695 * with respect to global symbols must refer to the precise symbol
696 * rather than referring to an offset from the base of the section
697 * _containing_ the symbol. Such relocations call to this routine,
698 * which searches the symbol list for the symbol in question.
700 * R_386_GOT32 references require the _exact_ symbol address to be
701 * used; R_386_32 references can be at an offset from the symbol.
702 * The boolean argument `exact' tells us this.
704 * Return value is the adjusted value of `addr', having become an
705 * offset from the symbol rather than the section. Should always be
706 * zero when returning from an exact call.
708 * Limitation: if you define two symbols at the same place,
709 * confusion will occur.
711 * Inefficiency: we search, currently, using a linked list which
712 * isn't even necessarily sorted.
714 static void elf_add_gsym_reloc(struct Section
*sect
,
715 int32_t segment
, uint64_t offset
, int64_t pcrel
,
716 int type
, bool exact
)
725 * First look up the segment/offset pair and find a global
726 * symbol corresponding to it. If it's not one of our segments,
727 * then it must be an external symbol, in which case we're fine
728 * doing a normal elf_add_reloc after first sanity-checking
729 * that the offset from the symbol is zero.
732 for (i
= 0; i
< nsects
; i
++)
733 if (segment
== sects
[i
]->index
) {
740 error(ERR_NONFATAL
, "invalid access to an external symbol");
742 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
746 srb
= rb_search(s
->gsyms
, offset
);
747 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
748 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
749 " for this reference");
752 sym
= container_of(srb
, struct Symbol
, symv
);
754 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
755 sect
->tail
= &r
->next
;
758 r
->address
= sect
->len
;
759 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
760 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
766 static void elf_out(int32_t segto
, const void *data
,
767 enum out_type type
, uint64_t size
,
768 int32_t segment
, int32_t wrt
)
773 static struct symlininfo sinfo
;
777 #if defined(DEBUG) && DEBUG>2
778 if (data
) fprintf(stderr
,
779 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
780 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
782 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
783 currentline
, type
, segment
, segto
, size
);
787 * handle absolute-assembly (structure definitions)
789 if (segto
== NO_SEG
) {
790 if (type
!= OUT_RESERVE
)
791 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
797 for (i
= 0; i
< nsects
; i
++)
798 if (segto
== sects
[i
]->index
) {
803 int tempint
; /* ignored */
804 if (segto
!= elf_section_names(".text", 2, &tempint
))
805 error(ERR_PANIC
, "strange segment conditions in ELF driver");
807 s
= sects
[nsects
- 1];
811 /* invoke current debug_output routine */
812 if (of_elf64
.current_dfmt
) {
813 sinfo
.offset
= s
->len
;
816 sinfo
.name
= s
->name
;
817 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
819 /* end of debugging stuff */
821 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
822 error(ERR_WARNING
, "attempt to initialize memory in"
823 " BSS section `%s': ignored", s
->name
);
824 s
->len
+= realsize(type
, size
);
828 if (type
== OUT_RESERVE
) {
829 if (s
->type
== SHT_PROGBITS
) {
830 error(ERR_WARNING
, "uninitialized space declared in"
831 " non-BSS section `%s': zeroing", s
->name
);
832 elf_sect_write(s
, NULL
, size
);
835 } else if (type
== OUT_RAWDATA
) {
836 if (segment
!= NO_SEG
)
837 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
838 elf_sect_write(s
, data
, size
);
839 } else if (type
== OUT_ADDRESS
) {
840 addr
= *(int64_t *)data
;
841 if (segment
== NO_SEG
) {
843 } else if (segment
% 2) {
844 error(ERR_NONFATAL
, "ELF format does not support"
845 " segment base references");
850 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
853 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
856 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
859 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
862 error(ERR_PANIC
, "internal error elf64-hpa-871");
866 } else if (wrt
== elf_gotpc_sect
+ 1) {
868 * The user will supply GOT relative to $$. ELF
869 * will let us have GOT relative to $. So we
870 * need to fix up the data item by $-$$.
873 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
875 } else if (wrt
== elf_gotoff_sect
+ 1) {
877 error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
878 "references to be qword");
880 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
883 } else if (wrt
== elf_got_sect
+ 1) {
886 elf_add_gsym_reloc(s
, segment
, addr
, 0,
887 R_X86_64_GOT32
, true);
891 elf_add_gsym_reloc(s
, segment
, addr
, 0,
892 R_X86_64_GOT64
, true);
896 error(ERR_NONFATAL
, "invalid ..got reference");
899 } else if (wrt
== elf_sym_sect
+ 1) {
902 elf_add_gsym_reloc(s
, segment
, addr
, 0,
907 elf_add_gsym_reloc(s
, segment
, addr
, 0,
912 elf_add_gsym_reloc(s
, segment
, addr
, 0,
917 elf_add_gsym_reloc(s
, segment
, addr
, 0,
922 error(ERR_PANIC
, "internal error elf64-hpa-903");
925 } else if (wrt
== elf_plt_sect
+ 1) {
926 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
927 "relative PLT references");
929 error(ERR_NONFATAL
, "ELF format does not support this"
933 elf_sect_writeaddr(s
, addr
, size
);
934 } else if (type
== OUT_REL2ADR
) {
935 addr
= *(int64_t *)data
- size
;
936 if (segment
== segto
)
937 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
938 if (segment
== NO_SEG
) {
940 } else if (segment
% 2) {
941 error(ERR_NONFATAL
, "ELF format does not support"
942 " segment base references");
945 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
949 "Unsupported non-32-bit ELF relocation [2]");
952 elf_sect_writeaddr(s
, addr
, 2);
953 } else if (type
== OUT_REL4ADR
) {
954 addr
= *(int64_t *)data
- size
;
955 if (segment
== segto
)
956 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
957 if (segment
== NO_SEG
) {
959 } else if (segment
% 2) {
960 error(ERR_NONFATAL
, "ELF64 format does not support"
961 " segment base references");
964 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
966 } else if (wrt
== elf_plt_sect
+ 1) {
967 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
968 R_X86_64_PLT32
, true);
970 } else if (wrt
== elf_gotpc_sect
+ 1 ||
971 wrt
== elf_got_sect
+ 1) {
972 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
973 R_X86_64_GOTPCREL
, true);
975 } else if (wrt
== elf_gotoff_sect
+ 1 ||
976 wrt
== elf_got_sect
+ 1) {
977 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
979 } else if (wrt
== elf_gottpoff_sect
+ 1) {
980 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
981 R_X86_64_GOTTPOFF
, true);
984 error(ERR_NONFATAL
, "ELF64 format does not support this"
988 elf_sect_writeaddr(s
, addr
, 4);
989 } else if (type
== OUT_REL8ADR
) {
990 addr
= *(int64_t *)data
- size
;
991 if (segment
== segto
)
992 error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
993 if (segment
== NO_SEG
) {
995 } else if (segment
% 2) {
996 error(ERR_NONFATAL
, "ELF64 format does not support"
997 " segment base references");
1000 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1002 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1003 wrt
== elf_got_sect
+ 1) {
1004 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1005 R_X86_64_GOTPCREL64
, true);
1007 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1008 wrt
== elf_got_sect
+ 1) {
1009 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1011 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1012 error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1015 error(ERR_NONFATAL
, "ELF64 format does not support this"
1019 elf_sect_writeaddr(s
, addr
, 8);
1023 static void elf_write(void)
1031 int32_t symtablen
, symtablocal
;
1034 * Work out how many sections we will have. We have SHN_UNDEF,
1035 * then the flexible user sections, then the fixed sections
1036 * `.shstrtab', `.symtab' and `.strtab', then optionally
1037 * relocation sections for the user sections.
1040 if (of_elf64
.current_dfmt
== &df_stabs
)
1042 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1045 add_sectname("", ".shstrtab");
1046 add_sectname("", ".symtab");
1047 add_sectname("", ".strtab");
1048 for (i
= 0; i
< nsects
; i
++) {
1049 nsections
++; /* for the section itself */
1050 if (sects
[i
]->head
) {
1051 nsections
++; /* for its relocations */
1052 add_sectname(".rela", sects
[i
]->name
);
1056 if (of_elf64
.current_dfmt
== &df_stabs
) {
1057 /* in case the debug information is wanted, just add these three sections... */
1058 add_sectname("", ".stab");
1059 add_sectname("", ".stabstr");
1060 add_sectname(".rel", ".stab");
1063 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1064 /* the dwarf debug standard specifies the following ten sections,
1065 not all of which are currently implemented,
1066 although all of them are defined. */
1067 #define debug_aranges (int64_t) (nsections-10)
1068 #define debug_info (int64_t) (nsections-7)
1069 #define debug_abbrev (int64_t) (nsections-5)
1070 #define debug_line (int64_t) (nsections-4)
1071 add_sectname("", ".debug_aranges");
1072 add_sectname(".rela", ".debug_aranges");
1073 add_sectname("", ".debug_pubnames");
1074 add_sectname("", ".debug_info");
1075 add_sectname(".rela", ".debug_info");
1076 add_sectname("", ".debug_abbrev");
1077 add_sectname("", ".debug_line");
1078 add_sectname(".rela", ".debug_line");
1079 add_sectname("", ".debug_frame");
1080 add_sectname("", ".debug_loc");
1084 * Output the ELF header.
1086 fwrite("\177ELF\2\1\1", 7, 1, elffp
);
1087 fputc(elf_osabi
, elffp
);
1088 fputc(elf_abiver
, elffp
);
1089 fwritezero(7, elffp
);
1090 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1091 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1092 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1093 fwriteint64_t(0L, elffp
); /* no entry point */
1094 fwriteint64_t(0L, elffp
); /* no program header table */
1095 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1096 * ELF header plus alignment */
1097 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1098 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1099 fwriteint16_t(0, elffp
); /* no program header table, again */
1100 fwriteint16_t(0, elffp
); /* still no program header table */
1101 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1102 fwriteint16_t(nsections
, elffp
); /* number of sections */
1103 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1104 * section header table */
1107 * Build the symbol table and relocation tables.
1109 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1110 for (i
= 0; i
< nsects
; i
++)
1112 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1116 * Now output the section header table.
1119 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1120 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1123 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1124 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1125 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1127 for (i
= 0; i
< nsects
; i
++) {
1128 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1129 (sects
[i
]->type
== SHT_PROGBITS
?
1130 sects
[i
]->data
: NULL
), true,
1131 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1133 scount
++; /* ditto */
1135 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1136 scount
++; /* ditto */
1138 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 24); /* .symtab */
1139 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1141 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1142 for (i
= 0; i
< nsects
; i
++)
1143 if (sects
[i
]->head
) {
1145 elf_section_header(p
- shstrtab
,SHT_RELA
, 0, sects
[i
]->rel
, true,
1146 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 24);
1148 if (of_elf64
.current_dfmt
== &df_stabs
) {
1149 /* for debugging information, create the last three sections
1150 which are the .stab , .stabstr and .rel.stab sections respectively */
1152 /* this function call creates the stab sections in memory */
1155 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1157 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1158 nsections
- 2, 0, 4, 12);
1161 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1162 stabstrlen
, 0, 0, 4, 0);
1165 /* link -> symtable info -> section to refer to */
1166 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1167 stabrellen
, symtabsection
, nsections
- 3, 4,
1171 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1172 /* for dwarf debugging information, create the ten dwarf sections */
1174 /* this function call creates the dwarf sections in memory */
1175 if (dwarf_fsect
) dwarf64_generate();
1178 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1179 arangeslen
, 0, 0, 1, 0);
1181 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1182 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1184 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1185 pubnameslen
, 0, 0, 1, 0);
1187 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1188 infolen
, 0, 0, 1, 0);
1190 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1191 inforellen
, symtabsection
, debug_info
, 1, 24);
1193 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1194 abbrevlen
, 0, 0, 1, 0);
1196 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1197 linelen
, 0, 0, 1, 0);
1199 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1200 linerellen
, symtabsection
, debug_line
, 1, 24);
1202 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1203 framelen
, 0, 0, 8, 0);
1205 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1206 loclen
, 0, 0, 1, 0);
1209 fwritezero(align
, elffp
);
1212 * Now output the sections.
1214 elf_write_sections();
1216 nasm_free(elf_sects
);
1220 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1222 struct SAA
*s
= saa_init(1L);
1224 uint8_t entry
[24], *p
;
1230 * First, an all-zeros entry, required by the ELF spec.
1232 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1237 * Next, an entry for the file name.
1240 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1241 WRITESHORT(p
, STT_FILE
); /* type FILE */
1242 WRITESHORT(p
, SHN_ABS
);
1243 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1244 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1245 saa_wbytes(s
, entry
, 24L);
1250 * Now some standard symbols defining the segments, for relocation
1253 for (i
= 1; i
<= nsects
; i
++) {
1255 WRITELONG(p
, 0); /* no symbol name */
1256 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1257 WRITESHORT(p
, i
); /* section id */
1258 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1259 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1260 saa_wbytes(s
, entry
, 24L);
1267 * Now the other local symbols.
1270 while ((sym
= saa_rstruct(syms
))) {
1271 if (sym
->type
& SYM_GLOBAL
)
1274 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1275 WRITECHAR(p
, sym
->type
); /* type and binding */
1276 WRITECHAR(p
, sym
->other
); /* visibility */
1277 WRITESHORT(p
, sym
->section
); /* index into section header table */
1278 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1279 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1280 saa_wbytes(s
, entry
, 24L);
1285 * dwarf needs symbols for debug sections
1286 * which are relocation targets.
1288 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1289 dwarf_infosym
= *local
;
1291 WRITELONG(p
, 0); /* no symbol name */
1292 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1293 WRITESHORT(p
, debug_info
); /* section id */
1294 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1295 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1296 saa_wbytes(s
, entry
, 24L);
1299 dwarf_abbrevsym
= *local
;
1301 WRITELONG(p
, 0); /* no symbol name */
1302 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1303 WRITESHORT(p
, debug_abbrev
); /* section id */
1304 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1305 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1306 saa_wbytes(s
, entry
, 24L);
1309 dwarf_linesym
= *local
;
1311 WRITELONG(p
, 0); /* no symbol name */
1312 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1313 WRITESHORT(p
, debug_line
); /* section id */
1314 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1315 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1316 saa_wbytes(s
, entry
, 24L);
1322 * Now the global symbols.
1325 while ((sym
= saa_rstruct(syms
))) {
1326 if (!(sym
->type
& SYM_GLOBAL
))
1329 WRITELONG(p
, sym
->strpos
);
1330 WRITECHAR(p
, sym
->type
); /* type and binding */
1331 WRITECHAR(p
, sym
->other
); /* visibility */
1332 WRITESHORT(p
, sym
->section
);
1333 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1334 WRITEDLONG(p
, (int64_t)sym
->size
);
1335 saa_wbytes(s
, entry
, 24L);
1342 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1345 uint8_t *p
, entry
[24];
1354 int64_t sym
= r
->symbol
;
1356 if (sym
>= GLOBAL_TEMP_BASE
)
1358 if (of_elf64
.current_dfmt
== &df_dwarf
)
1359 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 5) + nlocals
;
1360 else sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1363 WRITEDLONG(p
, r
->address
);
1364 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1365 WRITEDLONG(p
, r
->offset
);
1366 saa_wbytes(s
, entry
, 24L);
1375 static void elf_section_header(int name
, int type
, uint64_t flags
,
1376 void *data
, bool is_saa
, uint64_t datalen
,
1377 int link
, int info
, int align
, int eltsize
)
1379 elf_sects
[elf_nsect
].data
= data
;
1380 elf_sects
[elf_nsect
].len
= datalen
;
1381 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1384 fwriteint32_t((int32_t)name
, elffp
);
1385 fwriteint32_t((int32_t)type
, elffp
);
1386 fwriteint64_t((int64_t)flags
, elffp
);
1387 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1388 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1389 fwriteint64_t(datalen
, elffp
);
1391 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1392 fwriteint32_t((int32_t)link
, elffp
);
1393 fwriteint32_t((int32_t)info
, elffp
);
1394 fwriteint64_t((int64_t)align
, elffp
);
1395 fwriteint64_t((int64_t)eltsize
, elffp
);
1398 static void elf_write_sections(void)
1401 for (i
= 0; i
< elf_nsect
; i
++)
1402 if (elf_sects
[i
].data
) {
1403 int32_t len
= elf_sects
[i
].len
;
1404 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1405 int32_t align
= reallen
- len
;
1406 if (elf_sects
[i
].is_saa
)
1407 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1409 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1410 fwritezero(align
, elffp
);
1414 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1416 saa_wbytes(sect
->data
, data
, len
);
1419 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1421 saa_writeaddr(sect
->data
, data
, len
);
1425 static int32_t elf_segbase(int32_t segment
)
1430 static int elf_directive(char *directive
, char *value
, int pass
)
1436 if (!strcmp(directive
, "osabi")) {
1438 return 1; /* ignore in pass 2 */
1440 n
= readnum(value
, &err
);
1442 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1445 if (n
< 0 || n
> 255) {
1446 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1452 if ((p
= strchr(value
,',')) == NULL
)
1455 n
= readnum(p
+1, &err
);
1456 if (err
|| n
< 0 || n
> 255) {
1457 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1468 static void elf_filename(char *inname
, char *outname
, efunc error
)
1470 strcpy(elf_module
, inname
);
1471 standard_extension(inname
, outname
, ".o", error
);
1474 extern macros_t elf_stdmac
[];
1476 static int elf_set_info(enum geninfo type
, char **val
)
1482 static struct dfmt df_dwarf
= {
1483 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1493 static struct dfmt df_stabs
= {
1494 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1505 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1507 struct ofmt of_elf64
= {
1508 "ELF64 (x86_64) object files (e.g. Linux)",
1525 /* common debugging routines */
1526 void debug64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1533 void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1543 void debug64_directive(const char *directive
, const char *params
)
1549 void debug64_typevalue(int32_t type
)
1551 int32_t stype
, ssize
;
1552 switch (TYM_TYPE(type
)) {
1591 stype
= STT_SECTION
;
1606 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1607 lastsym
->size
= ssize
;
1608 lastsym
->type
= stype
;
1612 /* stabs debugging routines */
1615 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1618 if (!stabs_filename
) {
1619 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1620 strcpy(stabs_filename
, filename
);
1622 if (strcmp(stabs_filename
, filename
)) {
1623 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1624 in fact, this leak comes in quite handy to maintain a list of files
1625 encountered so far in the symbol lines... */
1627 /* why not nasm_free(stabs_filename); we're done with the old one */
1629 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1630 strcpy(stabs_filename
, filename
);
1634 currentline
= linenumber
;
1638 void stabs64_output(int type
, void *param
)
1640 struct symlininfo
*s
;
1641 struct linelist
*el
;
1642 if (type
== TY_DEBUGSYMLIN
) {
1643 if (debug_immcall
) {
1644 s
= (struct symlininfo
*)param
;
1645 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1646 return; /* line info is only collected for executable sections */
1648 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1649 el
->info
.offset
= s
->offset
;
1650 el
->info
.section
= s
->section
;
1651 el
->info
.name
= s
->name
;
1652 el
->line
= currentline
;
1653 el
->filename
= stabs_filename
;
1656 stabslines
->last
->next
= el
;
1657 stabslines
->last
= el
;
1660 stabslines
->last
= el
;
1667 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1669 WRITELONG(p,n_strx); \
1670 WRITECHAR(p,n_type); \
1671 WRITECHAR(p,n_other); \
1672 WRITESHORT(p,n_desc); \
1673 WRITELONG(p,n_value); \
1676 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1678 void stabs64_generate(void)
1680 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1681 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1685 struct linelist
*ptr
;
1689 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1690 for (i
= 0; i
< numlinestabs
; i
++)
1694 if (numfiles
== 0) {
1695 allfiles
[0] = ptr
->filename
;
1698 for (i
= 0; i
< numfiles
; i
++) {
1699 if (!strcmp(allfiles
[i
], ptr
->filename
))
1702 if (i
>= numfiles
) {
1703 allfiles
[i
] = ptr
->filename
;
1710 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1711 for (i
= 0; i
< numfiles
; i
++) {
1712 fileidx
[i
] = strsize
;
1713 strsize
+= strlen(allfiles
[i
]) + 1;
1716 for (i
= 0; i
< numfiles
; i
++) {
1717 if (!strcmp(allfiles
[i
], elf_module
)) {
1723 /* worst case size of the stab buffer would be:
1724 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1727 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1728 sizeof(struct stabentry
));
1730 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1732 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1735 for (i
= 0; i
< numfiles
; i
++) {
1736 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1740 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1747 /* this is the first stab, its strx points to the filename of the
1748 the source-file, the n_desc field should be set to the number
1751 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1753 /* this is the stab for the main source file */
1754 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1756 /* relocation table entry */
1758 /* Since the symbol table has two entries before */
1759 /* the section symbols, the index in the info.section */
1760 /* member must be adjusted by adding 2 */
1762 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1763 WRITELONG(rptr
, R_X86_64_32
);
1764 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1767 currfile
= mainfileindex
;
1771 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1772 /* oops file has changed... */
1773 for (i
= 0; i
< numfiles
; i
++)
1774 if (!strcmp(allfiles
[i
], ptr
->filename
))
1777 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1781 /* relocation table entry */
1783 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1784 WRITELONG(rptr
, R_X86_64_32
);
1785 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1788 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1791 /* relocation table entry */
1793 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1794 WRITELONG(rptr
, R_X86_64_32
);
1795 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1801 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1803 nasm_free(allfiles
);
1806 stablen
= (sptr
- sbuf
);
1807 stabrellen
= (rptr
- rbuf
);
1813 void stabs64_cleanup(void)
1815 struct linelist
*ptr
, *del
;
1827 nasm_free(stabrelbuf
);
1829 nasm_free(stabstrbuf
);
1831 /* dwarf routines */
1834 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1837 dwarf64_findfile(filename
);
1839 currentline
= linenumber
;
1842 /* called from elf_out with type == TY_DEBUGSYMLIN */
1843 void dwarf64_output(int type
, void *param
)
1845 int ln
, aa
, inx
, maxln
, soc
;
1846 struct symlininfo
*s
;
1851 s
= (struct symlininfo
*)param
;
1852 /* line number info is only gathered for executable sections */
1853 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1855 /* Check if section index has changed */
1856 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1858 dwarf64_findsect(s
->section
);
1860 /* do nothing unless line or file has changed */
1863 ln
= currentline
- dwarf_csect
->line
;
1864 aa
= s
->offset
- dwarf_csect
->offset
;
1865 inx
= dwarf_clist
->line
;
1866 plinep
= dwarf_csect
->psaa
;
1867 /* check for file change */
1868 if (!(inx
== dwarf_csect
->file
))
1870 saa_write8(plinep
,DW_LNS_set_file
);
1871 saa_write8(plinep
,inx
);
1872 dwarf_csect
->file
= inx
;
1874 /* check for line change */
1877 /* test if in range of special op code */
1878 maxln
= line_base
+ line_range
;
1879 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1880 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1882 saa_write8(plinep
,soc
);
1888 saa_write8(plinep
,DW_LNS_advance_line
);
1889 saa_wleb128s(plinep
,ln
);
1893 saa_write8(plinep
,DW_LNS_advance_pc
);
1894 saa_wleb128u(plinep
,aa
);
1897 dwarf_csect
->line
= currentline
;
1898 dwarf_csect
->offset
= s
->offset
;
1900 /* show change handled */
1906 void dwarf64_generate(void)
1910 struct linelist
*ftentry
;
1911 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1912 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1913 struct sectlist
*psect
;
1914 size_t saalen
, linepoff
, totlen
, highaddr
;
1916 /* write epilogues for each line program range */
1917 /* and build aranges section */
1918 paranges
= saa_init(1L);
1919 parangesrel
= saa_init(1L);
1920 saa_write16(paranges
,3); /* dwarf version */
1921 saa_write64(parangesrel
, paranges
->datalen
+4);
1922 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1923 saa_write64(parangesrel
, 0);
1924 saa_write32(paranges
,0); /* offset into info */
1925 saa_write8(paranges
,8); /* pointer size */
1926 saa_write8(paranges
,0); /* not segmented */
1927 saa_write32(paranges
,0); /* padding */
1928 /* iterate though sectlist entries */
1929 psect
= dwarf_fsect
;
1932 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1934 plinep
= psect
->psaa
;
1935 /* Line Number Program Epilogue */
1936 saa_write8(plinep
,2); /* std op 2 */
1937 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1938 saa_write8(plinep
,DW_LNS_extended_op
);
1939 saa_write8(plinep
,1); /* operand length */
1940 saa_write8(plinep
,DW_LNE_end_sequence
);
1941 totlen
+= plinep
->datalen
;
1942 /* range table relocation entry */
1943 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1944 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1945 saa_write64(parangesrel
, (uint64_t) 0);
1946 /* range table entry */
1947 saa_write64(paranges
,0x0000); /* range start */
1948 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1949 highaddr
+= sects
[psect
->section
]->len
;
1950 /* done with this entry */
1951 psect
= psect
->next
;
1953 saa_write64(paranges
,0); /* null address */
1954 saa_write64(paranges
,0); /* null length */
1955 saalen
= paranges
->datalen
;
1956 arangeslen
= saalen
+ 4;
1957 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1958 WRITELONG(pbuf
,saalen
); /* initial length */
1959 saa_rnbytes(paranges
, pbuf
, saalen
);
1962 /* build rela.aranges section */
1963 arangesrellen
= saalen
= parangesrel
->datalen
;
1964 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1965 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1966 saa_free(parangesrel
);
1968 /* build pubnames section */
1969 ppubnames
= saa_init(1L);
1970 saa_write16(ppubnames
,3); /* dwarf version */
1971 saa_write32(ppubnames
,0); /* offset into info */
1972 saa_write32(ppubnames
,0); /* space used in info */
1973 saa_write32(ppubnames
,0); /* end of list */
1974 saalen
= ppubnames
->datalen
;
1975 pubnameslen
= saalen
+ 4;
1976 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1977 WRITELONG(pbuf
,saalen
); /* initial length */
1978 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1979 saa_free(ppubnames
);
1981 /* build info section */
1982 pinfo
= saa_init(1L);
1983 pinforel
= saa_init(1L);
1984 saa_write16(pinfo
,3); /* dwarf version */
1985 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1986 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
1987 saa_write64(pinforel
, 0);
1988 saa_write32(pinfo
,0); /* offset into abbrev */
1989 saa_write8(pinfo
,8); /* pointer size */
1990 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1991 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1992 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1993 saa_write64(pinforel
, 0);
1994 saa_write64(pinfo
,0); /* DW_AT_low_pc */
1995 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1996 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1997 saa_write64(pinforel
, 0);
1998 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
1999 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2000 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2001 saa_write64(pinforel
, 0);
2002 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2003 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2004 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2005 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2006 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2007 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2008 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2009 saa_write64(pinforel
, 0);
2010 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2011 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2012 saa_write8(pinfo
,0); /* end of entries */
2013 saalen
= pinfo
->datalen
;
2014 infolen
= saalen
+ 4;
2015 infobuf
= pbuf
= nasm_malloc(infolen
);
2016 WRITELONG(pbuf
,saalen
); /* initial length */
2017 saa_rnbytes(pinfo
, pbuf
, saalen
);
2020 /* build rela.info section */
2021 inforellen
= saalen
= pinforel
->datalen
;
2022 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2023 saa_rnbytes(pinforel
, pbuf
, saalen
);
2026 /* build abbrev section */
2027 pabbrev
= saa_init(1L);
2028 saa_write8(pabbrev
,1); /* entry number LEB128u */
2029 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2030 saa_write8(pabbrev
,1); /* has children */
2031 /* the following attributes and forms are all LEB128u values */
2032 saa_write8(pabbrev
,DW_AT_low_pc
);
2033 saa_write8(pabbrev
,DW_FORM_addr
);
2034 saa_write8(pabbrev
,DW_AT_high_pc
);
2035 saa_write8(pabbrev
,DW_FORM_addr
);
2036 saa_write8(pabbrev
,DW_AT_stmt_list
);
2037 saa_write8(pabbrev
,DW_FORM_data4
);
2038 saa_write8(pabbrev
,DW_AT_name
);
2039 saa_write8(pabbrev
,DW_FORM_string
);
2040 saa_write8(pabbrev
,DW_AT_producer
);
2041 saa_write8(pabbrev
,DW_FORM_string
);
2042 saa_write8(pabbrev
,DW_AT_language
);
2043 saa_write8(pabbrev
,DW_FORM_data2
);
2044 saa_write16(pabbrev
,0); /* end of entry */
2045 /* LEB128u usage same as above */
2046 saa_write8(pabbrev
,2); /* entry number */
2047 saa_write8(pabbrev
,DW_TAG_subprogram
);
2048 saa_write8(pabbrev
,0); /* no children */
2049 saa_write8(pabbrev
,DW_AT_low_pc
);
2050 saa_write8(pabbrev
,DW_FORM_addr
);
2051 saa_write8(pabbrev
,DW_AT_frame_base
);
2052 saa_write8(pabbrev
,DW_FORM_data4
);
2053 saa_write16(pabbrev
,0); /* end of entry */
2054 abbrevlen
= saalen
= pabbrev
->datalen
;
2055 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2056 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2059 /* build line section */
2061 plines
= saa_init(1L);
2062 saa_write8(plines
,1); /* Minimum Instruction Length */
2063 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2064 saa_write8(plines
,line_base
); /* Line Base */
2065 saa_write8(plines
,line_range
); /* Line Range */
2066 saa_write8(plines
,opcode_base
); /* Opcode Base */
2067 /* standard opcode lengths (# of LEB128u operands) */
2068 saa_write8(plines
,0); /* Std opcode 1 length */
2069 saa_write8(plines
,1); /* Std opcode 2 length */
2070 saa_write8(plines
,1); /* Std opcode 3 length */
2071 saa_write8(plines
,1); /* Std opcode 4 length */
2072 saa_write8(plines
,1); /* Std opcode 5 length */
2073 saa_write8(plines
,0); /* Std opcode 6 length */
2074 saa_write8(plines
,0); /* Std opcode 7 length */
2075 saa_write8(plines
,0); /* Std opcode 8 length */
2076 saa_write8(plines
,1); /* Std opcode 9 length */
2077 saa_write8(plines
,0); /* Std opcode 10 length */
2078 saa_write8(plines
,0); /* Std opcode 11 length */
2079 saa_write8(plines
,1); /* Std opcode 12 length */
2080 /* Directory Table */
2081 saa_write8(plines
,0); /* End of table */
2082 /* File Name Table */
2083 ftentry
= dwarf_flist
;
2084 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2086 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2087 saa_write8(plines
,0); /* directory LEB128u */
2088 saa_write8(plines
,0); /* time LEB128u */
2089 saa_write8(plines
,0); /* size LEB128u */
2090 ftentry
= ftentry
->next
;
2092 saa_write8(plines
,0); /* End of table */
2093 linepoff
= plines
->datalen
;
2094 linelen
= linepoff
+ totlen
+ 10;
2095 linebuf
= pbuf
= nasm_malloc(linelen
);
2096 WRITELONG(pbuf
,linelen
-4); /* initial length */
2097 WRITESHORT(pbuf
,3); /* dwarf version */
2098 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2099 /* write line header */
2101 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2104 /* concatonate line program ranges */
2106 plinesrel
= saa_init(1L);
2107 psect
= dwarf_fsect
;
2108 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2110 saa_write64(plinesrel
, linepoff
);
2111 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2112 saa_write64(plinesrel
, (uint64_t) 0);
2113 plinep
= psect
->psaa
;
2114 saalen
= plinep
->datalen
;
2115 saa_rnbytes(plinep
, pbuf
, saalen
);
2119 /* done with this entry */
2120 psect
= psect
->next
;
2124 /* build rela.lines section */
2125 linerellen
=saalen
= plinesrel
->datalen
;
2126 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2127 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2128 saa_free(plinesrel
);
2130 /* build frame section */
2132 framebuf
= pbuf
= nasm_malloc(framelen
);
2133 WRITELONG(pbuf
,framelen
-4); /* initial length */
2135 /* build loc section */
2137 locbuf
= pbuf
= nasm_malloc(loclen
);
2138 WRITEDLONG(pbuf
,0); /* null beginning offset */
2139 WRITEDLONG(pbuf
,0); /* null ending offset */
2142 void dwarf64_cleanup(void)
2145 nasm_free(arangesbuf
);
2147 nasm_free(arangesrelbuf
);
2149 nasm_free(pubnamesbuf
);
2153 nasm_free(inforelbuf
);
2155 nasm_free(abbrevbuf
);
2159 nasm_free(linerelbuf
);
2161 nasm_free(framebuf
);
2165 void dwarf64_findfile(const char * fname
)
2168 struct linelist
*match
;
2170 /* return if fname is current file name */
2171 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2172 /* search for match */
2178 match
= dwarf_flist
;
2179 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2181 if (!(strcmp(fname
, match
->filename
)))
2183 dwarf_clist
= match
;
2188 /* add file name to end of list */
2189 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2191 dwarf_clist
->line
= dwarf_numfiles
;
2192 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2193 strcpy(dwarf_clist
->filename
,fname
);
2194 dwarf_clist
->next
= 0;
2195 /* if first entry */
2198 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2199 dwarf_clist
->last
= 0;
2201 /* chain to previous entry */
2204 dwarf_elist
->next
= dwarf_clist
;
2205 dwarf_elist
= dwarf_clist
;
2210 void dwarf64_findsect(const int index
)
2213 struct sectlist
*match
;
2215 /* return if index is current section index */
2216 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2220 /* search for match */
2226 match
= dwarf_fsect
;
2227 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2229 if ((match
->section
== index
))
2231 dwarf_csect
= match
;
2234 match
= match
->next
;
2237 /* add entry to end of list */
2238 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2240 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2241 dwarf_csect
->line
= 1;
2242 dwarf_csect
->offset
= 0;
2243 dwarf_csect
->file
= 1;
2244 dwarf_csect
->section
= index
;
2245 dwarf_csect
->next
= 0;
2246 /* set relocatable address at start of line program */
2247 saa_write8(plinep
,DW_LNS_extended_op
);
2248 saa_write8(plinep
,9); /* operand length */
2249 saa_write8(plinep
,DW_LNE_set_address
);
2250 saa_write64(plinep
,0); /* Start Address */
2251 /* if first entry */
2254 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2255 dwarf_csect
->last
= 0;
2257 /* chain to previous entry */
2260 dwarf_esect
->next
= dwarf_csect
;
2261 dwarf_esect
= dwarf_csect
;