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.
26 #include "output/elf64.h"
27 #include "output/dwarf.h"
28 #include "output/outelf.h"
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 int 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
)
329 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
335 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
338 uint32_t flags
, flags_and
, flags_or
;
343 * Default is 64 bits.
351 while (*p
&& !nasm_isspace(*p
))
355 flags_and
= flags_or
= type
= align
= 0;
357 while (*p
&& nasm_isspace(*p
))
361 while (*p
&& !nasm_isspace(*p
))
365 while (*p
&& nasm_isspace(*p
))
368 if (!nasm_strnicmp(q
, "align=", 6)) {
372 if ((align
- 1) & align
) { /* means it's not a power of two */
373 error(ERR_NONFATAL
, "section alignment %d is not"
374 " a power of two", align
);
377 } else if (!nasm_stricmp(q
, "alloc")) {
378 flags_and
|= SHF_ALLOC
;
379 flags_or
|= SHF_ALLOC
;
380 } else if (!nasm_stricmp(q
, "noalloc")) {
381 flags_and
|= SHF_ALLOC
;
382 flags_or
&= ~SHF_ALLOC
;
383 } else if (!nasm_stricmp(q
, "exec")) {
384 flags_and
|= SHF_EXECINSTR
;
385 flags_or
|= SHF_EXECINSTR
;
386 } else if (!nasm_stricmp(q
, "noexec")) {
387 flags_and
|= SHF_EXECINSTR
;
388 flags_or
&= ~SHF_EXECINSTR
;
389 } else if (!nasm_stricmp(q
, "write")) {
390 flags_and
|= SHF_WRITE
;
391 flags_or
|= SHF_WRITE
;
392 } else if (!nasm_stricmp(q
, "tls")) {
393 flags_and
|= SHF_TLS
;
395 } else if (!nasm_stricmp(q
, "nowrite")) {
396 flags_and
|= SHF_WRITE
;
397 flags_or
&= ~SHF_WRITE
;
398 } else if (!nasm_stricmp(q
, "progbits")) {
400 } else if (!nasm_stricmp(q
, "nobits")) {
402 } else if (pass
== 1) error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
403 " declaration of section `%s'", q
, name
);
406 if (!strcmp(name
, ".shstrtab") ||
407 !strcmp(name
, ".symtab") ||
408 !strcmp(name
, ".strtab")) {
409 error(ERR_NONFATAL
, "attempt to redefine reserved section"
414 for (i
= 0; i
< nsects
; i
++)
415 if (!strcmp(name
, sects
[i
]->name
))
418 const struct elf_known_section
*ks
= elf_known_sections
;
421 if (!strcmp(name
, ks
->name
))
426 type
= type
? type
: ks
->type
;
427 align
= align
? align
: ks
->align
;
428 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
430 i
= elf_make_section(name
, type
, flags
, align
);
431 } else if (pass
== 1) {
432 if ((type
&& sects
[i
]->type
!= type
)
433 || (align
&& sects
[i
]->align
!= align
)
434 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
435 error(ERR_WARNING
, "incompatible section attributes ignored on"
436 " redeclaration of section `%s'", name
);
439 return sects
[i
]->index
;
442 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
443 int is_global
, char *special
)
447 bool special_used
= false;
449 #if defined(DEBUG) && DEBUG>2
451 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
452 name
, segment
, offset
, is_global
, special
);
454 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
456 * This is a NASM special symbol. We never allow it into
457 * the ELF symbol table, even if it's a valid one. If it
458 * _isn't_ a valid one, we should barf immediately.
460 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
461 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
462 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
463 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
467 if (is_global
== 3) {
470 * Fix up a forward-reference symbol size from the first
473 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
474 if (!strcmp((*s
)->name
, name
)) {
475 struct tokenval tokval
;
479 while (*p
&& !nasm_isspace(*p
))
481 while (*p
&& nasm_isspace(*p
))
485 tokval
.t_type
= TOKEN_INVALID
;
486 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
489 error(ERR_NONFATAL
, "cannot use relocatable"
490 " expression as symbol size");
492 (*s
)->size
= reloc_value(e
);
496 * Remove it from the list of unresolved sizes.
498 nasm_free((*s
)->name
);
502 return; /* it wasn't an important one */
505 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
506 strslen
+= 1 + strlen(name
);
508 lastsym
= sym
= saa_wstruct(syms
);
510 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
513 sym
->type
= is_global
? SYM_GLOBAL
: 0;
514 sym
->other
= STV_DEFAULT
;
516 if (segment
== NO_SEG
)
517 sym
->section
= SHN_ABS
;
520 sym
->section
= SHN_UNDEF
;
521 if (nsects
== 0 && segment
== def_seg
) {
523 if (segment
!= elf_section_names(".text", 2, &tempint
))
525 "strange segment conditions in ELF driver");
526 sym
->section
= nsects
;
528 for (i
= 0; i
< nsects
; i
++)
529 if (segment
== sects
[i
]->index
) {
530 sym
->section
= i
+ 1;
536 if (is_global
== 2) {
539 sym
->section
= SHN_COMMON
;
541 * We have a common variable. Check the special text to see
542 * if it's a valid number and power of two; if so, store it
543 * as the alignment for the common variable.
547 sym
->symv
.key
= readnum(special
, &err
);
549 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
550 " valid number", special
);
551 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
552 != 2 * sym
->symv
.key
- 1)
553 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
554 " power of two", special
);
558 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
560 if (sym
->type
== SYM_GLOBAL
) {
562 * If sym->section == SHN_ABS, then the first line of the
563 * else section would cause a core dump, because its a reference
564 * beyond the end of the section array.
565 * This behaviour is exhibited by this code:
568 * To avoid such a crash, such requests are silently discarded.
569 * This may not be the best solution.
571 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
572 bsym
= raa_write(bsym
, segment
, nglobs
);
573 } else if (sym
->section
!= SHN_ABS
) {
575 * This is a global symbol; so we must add it to the rbtree
576 * of global symbols in its section.
578 * In addition, we check the special text for symbol
579 * type and size information.
581 sects
[sym
->section
-1]->gsyms
=
582 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
585 int n
= strcspn(special
, " \t");
587 if (!nasm_strnicmp(special
, "function", n
))
588 sym
->type
|= STT_FUNC
;
589 else if (!nasm_strnicmp(special
, "data", n
) ||
590 !nasm_strnicmp(special
, "object", n
))
591 sym
->type
|= STT_OBJECT
;
592 else if (!nasm_strnicmp(special
, "notype", n
))
593 sym
->type
|= STT_NOTYPE
;
595 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
599 while (nasm_isspace(*special
))
602 n
= strcspn(special
, " \t");
603 if (!nasm_strnicmp(special
, "default", n
))
604 sym
->other
= STV_DEFAULT
;
605 else if (!nasm_strnicmp(special
, "internal", n
))
606 sym
->other
= STV_INTERNAL
;
607 else if (!nasm_strnicmp(special
, "hidden", n
))
608 sym
->other
= STV_HIDDEN
;
609 else if (!nasm_strnicmp(special
, "protected", n
))
610 sym
->other
= STV_PROTECTED
;
617 struct tokenval tokval
;
620 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
622 while (special
[n
] && nasm_isspace(special
[n
]))
625 * We have a size expression; attempt to
629 stdscan_bufptr
= special
+ n
;
630 tokval
.t_type
= TOKEN_INVALID
;
631 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
636 sym
->name
= nasm_strdup(name
);
639 error(ERR_NONFATAL
, "cannot use relocatable"
640 " expression as symbol size");
642 sym
->size
= reloc_value(e
);
644 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
649 * If TLS segment, mark symbol accordingly.
651 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
653 sym
->type
|= STT_TLS
;
656 sym
->globnum
= nglobs
;
661 if (special
&& !special_used
)
662 error(ERR_NONFATAL
, "no special symbol features supported here");
665 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
666 int64_t offset
, int type
)
669 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
670 sect
->tail
= &r
->next
;
673 r
->address
= sect
->len
;
675 if (segment
== NO_SEG
)
680 for (i
= 0; i
< nsects
; i
++)
681 if (segment
== sects
[i
]->index
)
684 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
692 * This routine deals with ..got and ..sym relocations: the more
693 * complicated kinds. In shared-library writing, some relocations
694 * with respect to global symbols must refer to the precise symbol
695 * rather than referring to an offset from the base of the section
696 * _containing_ the symbol. Such relocations call to this routine,
697 * which searches the symbol list for the symbol in question.
699 * R_386_GOT32 references require the _exact_ symbol address to be
700 * used; R_386_32 references can be at an offset from the symbol.
701 * The boolean argument `exact' tells us this.
703 * Return value is the adjusted value of `addr', having become an
704 * offset from the symbol rather than the section. Should always be
705 * zero when returning from an exact call.
707 * Limitation: if you define two symbols at the same place,
708 * confusion will occur.
710 * Inefficiency: we search, currently, using a linked list which
711 * isn't even necessarily sorted.
713 static void elf_add_gsym_reloc(struct Section
*sect
,
714 int32_t segment
, uint64_t offset
, int64_t pcrel
,
715 int type
, bool exact
)
724 * First look up the segment/offset pair and find a global
725 * symbol corresponding to it. If it's not one of our segments,
726 * then it must be an external symbol, in which case we're fine
727 * doing a normal elf_add_reloc after first sanity-checking
728 * that the offset from the symbol is zero.
731 for (i
= 0; i
< nsects
; i
++)
732 if (segment
== sects
[i
]->index
) {
739 error(ERR_NONFATAL
, "invalid access to an external symbol");
741 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
745 srb
= rb_search(s
->gsyms
, offset
);
746 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
747 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
748 " for this reference");
751 sym
= container_of(srb
, struct Symbol
, symv
);
753 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
754 sect
->tail
= &r
->next
;
757 r
->address
= sect
->len
;
758 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
759 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
765 static void elf_out(int32_t segto
, const void *data
,
766 enum out_type type
, uint64_t size
,
767 int32_t segment
, int32_t wrt
)
772 static struct symlininfo sinfo
;
776 #if defined(DEBUG) && DEBUG>2
777 if (data
) fprintf(stderr
,
778 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
779 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
781 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
782 currentline
, type
, segment
, segto
, size
);
786 * handle absolute-assembly (structure definitions)
788 if (segto
== NO_SEG
) {
789 if (type
!= OUT_RESERVE
)
790 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
796 for (i
= 0; i
< nsects
; i
++)
797 if (segto
== sects
[i
]->index
) {
802 int tempint
; /* ignored */
803 if (segto
!= elf_section_names(".text", 2, &tempint
))
804 error(ERR_PANIC
, "strange segment conditions in ELF driver");
806 s
= sects
[nsects
- 1];
810 /* invoke current debug_output routine */
811 if (of_elf64
.current_dfmt
) {
812 sinfo
.offset
= s
->len
;
815 sinfo
.name
= s
->name
;
816 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
818 /* end of debugging stuff */
820 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
821 error(ERR_WARNING
, "attempt to initialize memory in"
822 " BSS section `%s': ignored", s
->name
);
823 s
->len
+= realsize(type
, size
);
827 if (type
== OUT_RESERVE
) {
828 if (s
->type
== SHT_PROGBITS
) {
829 error(ERR_WARNING
, "uninitialized space declared in"
830 " non-BSS section `%s': zeroing", s
->name
);
831 elf_sect_write(s
, NULL
, size
);
834 } else if (type
== OUT_RAWDATA
) {
835 if (segment
!= NO_SEG
)
836 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
837 elf_sect_write(s
, data
, size
);
838 } else if (type
== OUT_ADDRESS
) {
839 addr
= *(int64_t *)data
;
840 if (segment
== NO_SEG
) {
842 } else if (segment
% 2) {
843 error(ERR_NONFATAL
, "ELF format does not support"
844 " segment base references");
849 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
852 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
855 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
858 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
861 error(ERR_PANIC
, "internal error elf64-hpa-871");
865 } else if (wrt
== elf_gotpc_sect
+ 1) {
867 * The user will supply GOT relative to $$. ELF
868 * will let us have GOT relative to $. So we
869 * need to fix up the data item by $-$$.
872 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
874 } else if (wrt
== elf_gotoff_sect
+ 1) {
876 error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
877 "references to be qword");
879 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
882 } else if (wrt
== elf_got_sect
+ 1) {
885 elf_add_gsym_reloc(s
, segment
, addr
, 0,
886 R_X86_64_GOT32
, true);
890 elf_add_gsym_reloc(s
, segment
, addr
, 0,
891 R_X86_64_GOT64
, true);
895 error(ERR_NONFATAL
, "invalid ..got reference");
898 } else if (wrt
== elf_sym_sect
+ 1) {
901 elf_add_gsym_reloc(s
, segment
, addr
, 0,
906 elf_add_gsym_reloc(s
, segment
, addr
, 0,
911 elf_add_gsym_reloc(s
, segment
, addr
, 0,
916 elf_add_gsym_reloc(s
, segment
, addr
, 0,
921 error(ERR_PANIC
, "internal error elf64-hpa-903");
924 } else if (wrt
== elf_plt_sect
+ 1) {
925 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
926 "relative PLT references");
928 error(ERR_NONFATAL
, "ELF format does not support this"
932 elf_sect_writeaddr(s
, addr
, size
);
933 } else if (type
== OUT_REL2ADR
) {
934 addr
= *(int64_t *)data
- size
;
935 if (segment
== segto
)
936 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
937 if (segment
== NO_SEG
) {
939 } else if (segment
% 2) {
940 error(ERR_NONFATAL
, "ELF format does not support"
941 " segment base references");
944 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
948 "Unsupported non-32-bit ELF relocation [2]");
951 elf_sect_writeaddr(s
, addr
, 2);
952 } else if (type
== OUT_REL4ADR
) {
953 addr
= *(int64_t *)data
- size
;
954 if (segment
== segto
)
955 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
956 if (segment
== NO_SEG
) {
958 } else if (segment
% 2) {
959 error(ERR_NONFATAL
, "ELF64 format does not support"
960 " segment base references");
963 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
965 } else if (wrt
== elf_plt_sect
+ 1) {
966 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
967 R_X86_64_PLT32
, true);
969 } else if (wrt
== elf_gotpc_sect
+ 1 ||
970 wrt
== elf_got_sect
+ 1) {
971 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
972 R_X86_64_GOTPCREL
, true);
974 } else if (wrt
== elf_gotoff_sect
+ 1 ||
975 wrt
== elf_got_sect
+ 1) {
976 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
978 } else if (wrt
== elf_gottpoff_sect
+ 1) {
979 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
980 R_X86_64_GOTTPOFF
, true);
983 error(ERR_NONFATAL
, "ELF64 format does not support this"
987 elf_sect_writeaddr(s
, addr
, 4);
988 } else if (type
== OUT_REL8ADR
) {
989 addr
= *(int64_t *)data
- size
;
990 if (segment
== segto
)
991 error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
992 if (segment
== NO_SEG
) {
994 } else if (segment
% 2) {
995 error(ERR_NONFATAL
, "ELF64 format does not support"
996 " segment base references");
999 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1001 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1002 wrt
== elf_got_sect
+ 1) {
1003 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1004 R_X86_64_GOTPCREL64
, true);
1006 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1007 wrt
== elf_got_sect
+ 1) {
1008 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1010 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1011 error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1014 error(ERR_NONFATAL
, "ELF64 format does not support this"
1018 elf_sect_writeaddr(s
, addr
, 8);
1022 static void elf_write(void)
1029 int32_t symtablen
, symtablocal
;
1032 * Work out how many sections we will have. We have SHN_UNDEF,
1033 * then the flexible user sections, then the fixed sections
1034 * `.shstrtab', `.symtab' and `.strtab', then optionally
1035 * relocation sections for the user sections.
1037 nsections
= sec_numspecial
+ 1;
1038 if (of_elf64
.current_dfmt
== &df_stabs
)
1040 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1043 add_sectname("", ".shstrtab");
1044 add_sectname("", ".symtab");
1045 add_sectname("", ".strtab");
1046 for (i
= 0; i
< nsects
; i
++) {
1047 nsections
++; /* for the section itself */
1048 if (sects
[i
]->head
) {
1049 nsections
++; /* for its relocations */
1050 add_sectname(".rela", sects
[i
]->name
);
1054 if (of_elf64
.current_dfmt
== &df_stabs
) {
1055 /* in case the debug information is wanted, just add these three sections... */
1056 add_sectname("", ".stab");
1057 add_sectname("", ".stabstr");
1058 add_sectname(".rel", ".stab");
1061 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1062 /* the dwarf debug standard specifies the following ten sections,
1063 not all of which are currently implemented,
1064 although all of them are defined. */
1065 #define debug_aranges (int64_t) (nsections-10)
1066 #define debug_info (int64_t) (nsections-7)
1067 #define debug_abbrev (int64_t) (nsections-5)
1068 #define debug_line (int64_t) (nsections-4)
1069 add_sectname("", ".debug_aranges");
1070 add_sectname(".rela", ".debug_aranges");
1071 add_sectname("", ".debug_pubnames");
1072 add_sectname("", ".debug_info");
1073 add_sectname(".rela", ".debug_info");
1074 add_sectname("", ".debug_abbrev");
1075 add_sectname("", ".debug_line");
1076 add_sectname(".rela", ".debug_line");
1077 add_sectname("", ".debug_frame");
1078 add_sectname("", ".debug_loc");
1082 * Output the ELF header.
1084 fwrite("\177ELF\2\1\1", 7, 1, elffp
);
1085 fputc(elf_osabi
, elffp
);
1086 fputc(elf_abiver
, elffp
);
1087 fwritezero(7, elffp
);
1088 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1089 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1090 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1091 fwriteint64_t(0L, elffp
); /* no entry point */
1092 fwriteint64_t(0L, elffp
); /* no program header table */
1093 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1094 * ELF header plus alignment */
1095 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1096 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1097 fwriteint16_t(0, elffp
); /* no program header table, again */
1098 fwriteint16_t(0, elffp
); /* still no program header table */
1099 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1100 fwriteint16_t(nsections
, elffp
); /* number of sections */
1101 fwriteint16_t(sec_shstrtab
, elffp
); /* string table section index for
1102 * section header table */
1105 * Build the symbol table and relocation tables.
1107 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1108 for (i
= 0; i
< nsects
; i
++)
1110 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1114 * Now output the section header table.
1117 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1118 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1121 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1124 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1127 /* The normal sections */
1128 for (i
= 0; i
< nsects
; i
++) {
1129 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1130 (sects
[i
]->type
== SHT_PROGBITS
?
1131 sects
[i
]->data
: NULL
), true,
1132 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1137 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1138 shstrtablen
, 0, 0, 1, 0);
1142 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1143 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1147 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, strs
, true,
1148 strslen
, 0, 0, 1, 0);
1151 /* The relocation sections */
1152 for (i
= 0; i
< nsects
; i
++)
1153 if (sects
[i
]->head
) {
1154 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1155 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1159 if (of_elf64
.current_dfmt
== &df_stabs
) {
1160 /* for debugging information, create the last three sections
1161 which are the .stab , .stabstr and .rel.stab sections respectively */
1163 /* this function call creates the stab sections in memory */
1166 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1167 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1168 stablen
, sec_stabstr
, 0, 4, 12);
1171 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1172 stabstrlen
, 0, 0, 4, 0);
1175 /* link -> symtable info -> section to refer to */
1176 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1177 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1181 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1182 /* for dwarf debugging information, create the ten dwarf sections */
1184 /* this function call creates the dwarf sections in memory */
1188 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1189 arangeslen
, 0, 0, 1, 0);
1192 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1193 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1196 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1197 pubnameslen
, 0, 0, 1, 0);
1200 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1201 infolen
, 0, 0, 1, 0);
1204 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1205 inforellen
, symtabsection
, debug_info
, 1, 24);
1208 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1209 abbrevlen
, 0, 0, 1, 0);
1212 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1213 linelen
, 0, 0, 1, 0);
1216 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1217 linerellen
, symtabsection
, debug_line
, 1, 24);
1220 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1221 framelen
, 0, 0, 8, 0);
1224 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1225 loclen
, 0, 0, 1, 0);
1228 fwritezero(align
, elffp
);
1231 * Now output the sections.
1233 elf_write_sections();
1235 nasm_free(elf_sects
);
1239 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1241 struct SAA
*s
= saa_init(1L);
1243 uint8_t entry
[24], *p
;
1249 * First, an all-zeros entry, required by the ELF spec.
1251 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1256 * Next, an entry for the file name.
1259 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1260 WRITESHORT(p
, STT_FILE
); /* type FILE */
1261 WRITESHORT(p
, SHN_ABS
);
1262 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1263 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1264 saa_wbytes(s
, entry
, 24L);
1269 * Now some standard symbols defining the segments, for relocation
1272 for (i
= 1; i
<= nsects
; i
++) {
1274 WRITELONG(p
, 0); /* no symbol name */
1275 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1276 WRITESHORT(p
, i
); /* section id */
1277 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1278 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1279 saa_wbytes(s
, entry
, 24L);
1286 * Now the other local symbols.
1289 while ((sym
= saa_rstruct(syms
))) {
1290 if (sym
->type
& SYM_GLOBAL
)
1293 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1294 WRITECHAR(p
, sym
->type
); /* type and binding */
1295 WRITECHAR(p
, sym
->other
); /* visibility */
1296 WRITESHORT(p
, sym
->section
); /* index into section header table */
1297 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1298 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1299 saa_wbytes(s
, entry
, 24L);
1304 * dwarf needs symbols for debug sections
1305 * which are relocation targets.
1307 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1308 dwarf_infosym
= *local
;
1310 WRITELONG(p
, 0); /* no symbol name */
1311 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1312 WRITESHORT(p
, debug_info
); /* section id */
1313 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1314 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1315 saa_wbytes(s
, entry
, 24L);
1318 dwarf_abbrevsym
= *local
;
1320 WRITELONG(p
, 0); /* no symbol name */
1321 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1322 WRITESHORT(p
, debug_abbrev
); /* section id */
1323 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1324 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1325 saa_wbytes(s
, entry
, 24L);
1328 dwarf_linesym
= *local
;
1330 WRITELONG(p
, 0); /* no symbol name */
1331 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1332 WRITESHORT(p
, debug_line
); /* section id */
1333 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1334 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1335 saa_wbytes(s
, entry
, 24L);
1341 * Now the global symbols.
1344 while ((sym
= saa_rstruct(syms
))) {
1345 if (!(sym
->type
& SYM_GLOBAL
))
1348 WRITELONG(p
, sym
->strpos
);
1349 WRITECHAR(p
, sym
->type
); /* type and binding */
1350 WRITECHAR(p
, sym
->other
); /* visibility */
1351 WRITESHORT(p
, sym
->section
);
1352 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1353 WRITEDLONG(p
, (int64_t)sym
->size
);
1354 saa_wbytes(s
, entry
, 24L);
1361 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1364 uint8_t *p
, entry
[24];
1373 int64_t sym
= r
->symbol
;
1375 if (sym
>= GLOBAL_TEMP_BASE
)
1377 if (of_elf64
.current_dfmt
== &df_dwarf
)
1378 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 5) + nlocals
;
1380 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1383 WRITEDLONG(p
, r
->address
);
1384 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1385 WRITEDLONG(p
, r
->offset
);
1386 saa_wbytes(s
, entry
, 24L);
1395 static void elf_section_header(int name
, int type
, uint64_t flags
,
1396 void *data
, bool is_saa
, uint64_t datalen
,
1397 int link
, int info
, int align
, int eltsize
)
1399 elf_sects
[elf_nsect
].data
= data
;
1400 elf_sects
[elf_nsect
].len
= datalen
;
1401 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1404 fwriteint32_t((int32_t)name
, elffp
);
1405 fwriteint32_t((int32_t)type
, elffp
);
1406 fwriteint64_t((int64_t)flags
, elffp
);
1407 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1408 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1409 fwriteint64_t(datalen
, elffp
);
1411 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1412 fwriteint32_t((int32_t)link
, elffp
);
1413 fwriteint32_t((int32_t)info
, elffp
);
1414 fwriteint64_t((int64_t)align
, elffp
);
1415 fwriteint64_t((int64_t)eltsize
, elffp
);
1418 static void elf_write_sections(void)
1421 for (i
= 0; i
< elf_nsect
; i
++)
1422 if (elf_sects
[i
].data
) {
1423 int32_t len
= elf_sects
[i
].len
;
1424 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1425 int32_t align
= reallen
- len
;
1426 if (elf_sects
[i
].is_saa
)
1427 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1429 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1430 fwritezero(align
, elffp
);
1434 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1436 saa_wbytes(sect
->data
, data
, len
);
1439 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1441 saa_writeaddr(sect
->data
, data
, len
);
1445 static int32_t elf_segbase(int32_t segment
)
1450 static int elf_directive(char *directive
, char *value
, int pass
)
1456 if (!strcmp(directive
, "osabi")) {
1458 return 1; /* ignore in pass 2 */
1460 n
= readnum(value
, &err
);
1462 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1465 if (n
< 0 || n
> 255) {
1466 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1472 if ((p
= strchr(value
,',')) == NULL
)
1475 n
= readnum(p
+1, &err
);
1476 if (err
|| n
< 0 || n
> 255) {
1477 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1488 static void elf_filename(char *inname
, char *outname
, efunc error
)
1490 strcpy(elf_module
, inname
);
1491 standard_extension(inname
, outname
, ".o", error
);
1494 extern macros_t elf_stdmac
[];
1496 static int elf_set_info(enum geninfo type
, char **val
)
1502 static struct dfmt df_dwarf
= {
1503 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1513 static struct dfmt df_stabs
= {
1514 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1525 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1527 struct ofmt of_elf64
= {
1528 "ELF64 (x86_64) object files (e.g. Linux)",
1545 /* common debugging routines */
1546 void debug64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1553 void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1563 void debug64_directive(const char *directive
, const char *params
)
1569 void debug64_typevalue(int32_t type
)
1571 int32_t stype
, ssize
;
1572 switch (TYM_TYPE(type
)) {
1611 stype
= STT_SECTION
;
1626 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1627 lastsym
->size
= ssize
;
1628 lastsym
->type
= stype
;
1632 /* stabs debugging routines */
1635 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1638 if (!stabs_filename
) {
1639 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1640 strcpy(stabs_filename
, filename
);
1642 if (strcmp(stabs_filename
, filename
)) {
1643 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1644 in fact, this leak comes in quite handy to maintain a list of files
1645 encountered so far in the symbol lines... */
1647 /* why not nasm_free(stabs_filename); we're done with the old one */
1649 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1650 strcpy(stabs_filename
, filename
);
1654 currentline
= linenumber
;
1658 void stabs64_output(int type
, void *param
)
1660 struct symlininfo
*s
;
1661 struct linelist
*el
;
1662 if (type
== TY_DEBUGSYMLIN
) {
1663 if (debug_immcall
) {
1664 s
= (struct symlininfo
*)param
;
1665 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1666 return; /* line info is only collected for executable sections */
1668 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1669 el
->info
.offset
= s
->offset
;
1670 el
->info
.section
= s
->section
;
1671 el
->info
.name
= s
->name
;
1672 el
->line
= currentline
;
1673 el
->filename
= stabs_filename
;
1676 stabslines
->last
->next
= el
;
1677 stabslines
->last
= el
;
1680 stabslines
->last
= el
;
1687 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1689 WRITELONG(p,n_strx); \
1690 WRITECHAR(p,n_type); \
1691 WRITECHAR(p,n_other); \
1692 WRITESHORT(p,n_desc); \
1693 WRITELONG(p,n_value); \
1696 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1698 void stabs64_generate(void)
1700 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1701 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1705 struct linelist
*ptr
;
1709 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1710 for (i
= 0; i
< numlinestabs
; i
++)
1714 if (numfiles
== 0) {
1715 allfiles
[0] = ptr
->filename
;
1718 for (i
= 0; i
< numfiles
; i
++) {
1719 if (!strcmp(allfiles
[i
], ptr
->filename
))
1722 if (i
>= numfiles
) {
1723 allfiles
[i
] = ptr
->filename
;
1730 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1731 for (i
= 0; i
< numfiles
; i
++) {
1732 fileidx
[i
] = strsize
;
1733 strsize
+= strlen(allfiles
[i
]) + 1;
1736 for (i
= 0; i
< numfiles
; i
++) {
1737 if (!strcmp(allfiles
[i
], elf_module
)) {
1743 /* worst case size of the stab buffer would be:
1744 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1747 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1748 sizeof(struct stabentry
));
1750 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1752 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1755 for (i
= 0; i
< numfiles
; i
++) {
1756 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1760 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1767 /* this is the first stab, its strx points to the filename of the
1768 the source-file, the n_desc field should be set to the number
1771 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1773 /* this is the stab for the main source file */
1774 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1776 /* relocation table entry */
1778 /* Since the symbol table has two entries before */
1779 /* the section symbols, the index in the info.section */
1780 /* member must be adjusted by adding 2 */
1782 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1783 WRITELONG(rptr
, R_X86_64_32
);
1784 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1787 currfile
= mainfileindex
;
1791 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1792 /* oops file has changed... */
1793 for (i
= 0; i
< numfiles
; i
++)
1794 if (!strcmp(allfiles
[i
], ptr
->filename
))
1797 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1801 /* relocation table entry */
1803 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1804 WRITELONG(rptr
, R_X86_64_32
);
1805 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1808 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1811 /* relocation table entry */
1813 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1814 WRITELONG(rptr
, R_X86_64_32
);
1815 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1821 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1823 nasm_free(allfiles
);
1826 stablen
= (sptr
- sbuf
);
1827 stabrellen
= (rptr
- rbuf
);
1833 void stabs64_cleanup(void)
1835 struct linelist
*ptr
, *del
;
1847 nasm_free(stabrelbuf
);
1849 nasm_free(stabstrbuf
);
1851 /* dwarf routines */
1854 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1857 dwarf64_findfile(filename
);
1859 currentline
= linenumber
;
1862 /* called from elf_out with type == TY_DEBUGSYMLIN */
1863 void dwarf64_output(int type
, void *param
)
1865 int ln
, aa
, inx
, maxln
, soc
;
1866 struct symlininfo
*s
;
1871 s
= (struct symlininfo
*)param
;
1872 /* line number info is only gathered for executable sections */
1873 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1875 /* Check if section index has changed */
1876 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1878 dwarf64_findsect(s
->section
);
1880 /* do nothing unless line or file has changed */
1883 ln
= currentline
- dwarf_csect
->line
;
1884 aa
= s
->offset
- dwarf_csect
->offset
;
1885 inx
= dwarf_clist
->line
;
1886 plinep
= dwarf_csect
->psaa
;
1887 /* check for file change */
1888 if (!(inx
== dwarf_csect
->file
))
1890 saa_write8(plinep
,DW_LNS_set_file
);
1891 saa_write8(plinep
,inx
);
1892 dwarf_csect
->file
= inx
;
1894 /* check for line change */
1897 /* test if in range of special op code */
1898 maxln
= line_base
+ line_range
;
1899 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1900 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1902 saa_write8(plinep
,soc
);
1908 saa_write8(plinep
,DW_LNS_advance_line
);
1909 saa_wleb128s(plinep
,ln
);
1913 saa_write8(plinep
,DW_LNS_advance_pc
);
1914 saa_wleb128u(plinep
,aa
);
1917 dwarf_csect
->line
= currentline
;
1918 dwarf_csect
->offset
= s
->offset
;
1920 /* show change handled */
1926 void dwarf64_generate(void)
1930 struct linelist
*ftentry
;
1931 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1932 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1933 struct sectlist
*psect
;
1934 size_t saalen
, linepoff
, totlen
, highaddr
;
1936 /* write epilogues for each line program range */
1937 /* and build aranges section */
1938 paranges
= saa_init(1L);
1939 parangesrel
= saa_init(1L);
1940 saa_write16(paranges
,3); /* dwarf version */
1941 saa_write64(parangesrel
, paranges
->datalen
+4);
1942 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1943 saa_write64(parangesrel
, 0);
1944 saa_write32(paranges
,0); /* offset into info */
1945 saa_write8(paranges
,8); /* pointer size */
1946 saa_write8(paranges
,0); /* not segmented */
1947 saa_write32(paranges
,0); /* padding */
1948 /* iterate though sectlist entries */
1949 psect
= dwarf_fsect
;
1952 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1954 plinep
= psect
->psaa
;
1955 /* Line Number Program Epilogue */
1956 saa_write8(plinep
,2); /* std op 2 */
1957 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1958 saa_write8(plinep
,DW_LNS_extended_op
);
1959 saa_write8(plinep
,1); /* operand length */
1960 saa_write8(plinep
,DW_LNE_end_sequence
);
1961 totlen
+= plinep
->datalen
;
1962 /* range table relocation entry */
1963 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1964 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1965 saa_write64(parangesrel
, (uint64_t) 0);
1966 /* range table entry */
1967 saa_write64(paranges
,0x0000); /* range start */
1968 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1969 highaddr
+= sects
[psect
->section
]->len
;
1970 /* done with this entry */
1971 psect
= psect
->next
;
1973 saa_write64(paranges
,0); /* null address */
1974 saa_write64(paranges
,0); /* null length */
1975 saalen
= paranges
->datalen
;
1976 arangeslen
= saalen
+ 4;
1977 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1978 WRITELONG(pbuf
,saalen
); /* initial length */
1979 saa_rnbytes(paranges
, pbuf
, saalen
);
1982 /* build rela.aranges section */
1983 arangesrellen
= saalen
= parangesrel
->datalen
;
1984 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1985 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1986 saa_free(parangesrel
);
1988 /* build pubnames section */
1989 ppubnames
= saa_init(1L);
1990 saa_write16(ppubnames
,3); /* dwarf version */
1991 saa_write32(ppubnames
,0); /* offset into info */
1992 saa_write32(ppubnames
,0); /* space used in info */
1993 saa_write32(ppubnames
,0); /* end of list */
1994 saalen
= ppubnames
->datalen
;
1995 pubnameslen
= saalen
+ 4;
1996 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1997 WRITELONG(pbuf
,saalen
); /* initial length */
1998 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1999 saa_free(ppubnames
);
2001 /* build info section */
2002 pinfo
= saa_init(1L);
2003 pinforel
= saa_init(1L);
2004 saa_write16(pinfo
,3); /* dwarf version */
2005 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2006 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2007 saa_write64(pinforel
, 0);
2008 saa_write32(pinfo
,0); /* offset into abbrev */
2009 saa_write8(pinfo
,8); /* pointer size */
2010 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2011 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2012 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2013 saa_write64(pinforel
, 0);
2014 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2015 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2016 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2017 saa_write64(pinforel
, 0);
2018 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2019 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2020 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2021 saa_write64(pinforel
, 0);
2022 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2023 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2024 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2025 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2026 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2027 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2028 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2029 saa_write64(pinforel
, 0);
2030 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2031 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2032 saa_write8(pinfo
,0); /* end of entries */
2033 saalen
= pinfo
->datalen
;
2034 infolen
= saalen
+ 4;
2035 infobuf
= pbuf
= nasm_malloc(infolen
);
2036 WRITELONG(pbuf
,saalen
); /* initial length */
2037 saa_rnbytes(pinfo
, pbuf
, saalen
);
2040 /* build rela.info section */
2041 inforellen
= saalen
= pinforel
->datalen
;
2042 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2043 saa_rnbytes(pinforel
, pbuf
, saalen
);
2046 /* build abbrev section */
2047 pabbrev
= saa_init(1L);
2048 saa_write8(pabbrev
,1); /* entry number LEB128u */
2049 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2050 saa_write8(pabbrev
,1); /* has children */
2051 /* the following attributes and forms are all LEB128u values */
2052 saa_write8(pabbrev
,DW_AT_low_pc
);
2053 saa_write8(pabbrev
,DW_FORM_addr
);
2054 saa_write8(pabbrev
,DW_AT_high_pc
);
2055 saa_write8(pabbrev
,DW_FORM_addr
);
2056 saa_write8(pabbrev
,DW_AT_stmt_list
);
2057 saa_write8(pabbrev
,DW_FORM_data4
);
2058 saa_write8(pabbrev
,DW_AT_name
);
2059 saa_write8(pabbrev
,DW_FORM_string
);
2060 saa_write8(pabbrev
,DW_AT_producer
);
2061 saa_write8(pabbrev
,DW_FORM_string
);
2062 saa_write8(pabbrev
,DW_AT_language
);
2063 saa_write8(pabbrev
,DW_FORM_data2
);
2064 saa_write16(pabbrev
,0); /* end of entry */
2065 /* LEB128u usage same as above */
2066 saa_write8(pabbrev
,2); /* entry number */
2067 saa_write8(pabbrev
,DW_TAG_subprogram
);
2068 saa_write8(pabbrev
,0); /* no children */
2069 saa_write8(pabbrev
,DW_AT_low_pc
);
2070 saa_write8(pabbrev
,DW_FORM_addr
);
2071 saa_write8(pabbrev
,DW_AT_frame_base
);
2072 saa_write8(pabbrev
,DW_FORM_data4
);
2073 saa_write16(pabbrev
,0); /* end of entry */
2074 abbrevlen
= saalen
= pabbrev
->datalen
;
2075 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2076 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2079 /* build line section */
2081 plines
= saa_init(1L);
2082 saa_write8(plines
,1); /* Minimum Instruction Length */
2083 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2084 saa_write8(plines
,line_base
); /* Line Base */
2085 saa_write8(plines
,line_range
); /* Line Range */
2086 saa_write8(plines
,opcode_base
); /* Opcode Base */
2087 /* standard opcode lengths (# of LEB128u operands) */
2088 saa_write8(plines
,0); /* Std opcode 1 length */
2089 saa_write8(plines
,1); /* Std opcode 2 length */
2090 saa_write8(plines
,1); /* Std opcode 3 length */
2091 saa_write8(plines
,1); /* Std opcode 4 length */
2092 saa_write8(plines
,1); /* Std opcode 5 length */
2093 saa_write8(plines
,0); /* Std opcode 6 length */
2094 saa_write8(plines
,0); /* Std opcode 7 length */
2095 saa_write8(plines
,0); /* Std opcode 8 length */
2096 saa_write8(plines
,1); /* Std opcode 9 length */
2097 saa_write8(plines
,0); /* Std opcode 10 length */
2098 saa_write8(plines
,0); /* Std opcode 11 length */
2099 saa_write8(plines
,1); /* Std opcode 12 length */
2100 /* Directory Table */
2101 saa_write8(plines
,0); /* End of table */
2102 /* File Name Table */
2103 ftentry
= dwarf_flist
;
2104 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2106 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2107 saa_write8(plines
,0); /* directory LEB128u */
2108 saa_write8(plines
,0); /* time LEB128u */
2109 saa_write8(plines
,0); /* size LEB128u */
2110 ftentry
= ftentry
->next
;
2112 saa_write8(plines
,0); /* End of table */
2113 linepoff
= plines
->datalen
;
2114 linelen
= linepoff
+ totlen
+ 10;
2115 linebuf
= pbuf
= nasm_malloc(linelen
);
2116 WRITELONG(pbuf
,linelen
-4); /* initial length */
2117 WRITESHORT(pbuf
,3); /* dwarf version */
2118 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2119 /* write line header */
2121 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2124 /* concatonate line program ranges */
2126 plinesrel
= saa_init(1L);
2127 psect
= dwarf_fsect
;
2128 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2130 saa_write64(plinesrel
, linepoff
);
2131 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2132 saa_write64(plinesrel
, (uint64_t) 0);
2133 plinep
= psect
->psaa
;
2134 saalen
= plinep
->datalen
;
2135 saa_rnbytes(plinep
, pbuf
, saalen
);
2139 /* done with this entry */
2140 psect
= psect
->next
;
2144 /* build rela.lines section */
2145 linerellen
=saalen
= plinesrel
->datalen
;
2146 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2147 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2148 saa_free(plinesrel
);
2150 /* build frame section */
2152 framebuf
= pbuf
= nasm_malloc(framelen
);
2153 WRITELONG(pbuf
,framelen
-4); /* initial length */
2155 /* build loc section */
2157 locbuf
= pbuf
= nasm_malloc(loclen
);
2158 WRITEDLONG(pbuf
,0); /* null beginning offset */
2159 WRITEDLONG(pbuf
,0); /* null ending offset */
2162 void dwarf64_cleanup(void)
2165 nasm_free(arangesbuf
);
2167 nasm_free(arangesrelbuf
);
2169 nasm_free(pubnamesbuf
);
2173 nasm_free(inforelbuf
);
2175 nasm_free(abbrevbuf
);
2179 nasm_free(linerelbuf
);
2181 nasm_free(framebuf
);
2185 void dwarf64_findfile(const char * fname
)
2188 struct linelist
*match
;
2190 /* return if fname is current file name */
2191 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2192 /* search for match */
2198 match
= dwarf_flist
;
2199 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2201 if (!(strcmp(fname
, match
->filename
)))
2203 dwarf_clist
= match
;
2208 /* add file name to end of list */
2209 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2211 dwarf_clist
->line
= dwarf_numfiles
;
2212 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2213 strcpy(dwarf_clist
->filename
,fname
);
2214 dwarf_clist
->next
= 0;
2215 /* if first entry */
2218 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2219 dwarf_clist
->last
= 0;
2221 /* chain to previous entry */
2224 dwarf_elist
->next
= dwarf_clist
;
2225 dwarf_elist
= dwarf_clist
;
2230 void dwarf64_findsect(const int index
)
2233 struct sectlist
*match
;
2235 /* return if index is current section index */
2236 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2240 /* search for match */
2246 match
= dwarf_fsect
;
2247 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2249 if ((match
->section
== index
))
2251 dwarf_csect
= match
;
2254 match
= match
->next
;
2257 /* add entry to end of list */
2258 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2260 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2261 dwarf_csect
->line
= 1;
2262 dwarf_csect
->offset
= 0;
2263 dwarf_csect
->file
= 1;
2264 dwarf_csect
->section
= index
;
2265 dwarf_csect
->next
= 0;
2266 /* set relocatable address at start of line program */
2267 saa_write8(plinep
,DW_LNS_extended_op
);
2268 saa_write8(plinep
,9); /* operand length */
2269 saa_write8(plinep
,DW_LNE_set_address
);
2270 saa_write64(plinep
,0); /* Start Address */
2271 /* if first entry */
2274 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2275 dwarf_csect
->last
= 0;
2277 /* chain to previous entry */
2280 dwarf_esect
->next
= dwarf_csect
;
2281 dwarf_esect
= dwarf_csect
;