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.
22 #include "output/outform.h"
23 #include "output/outlib.h"
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
, ndebugs
;
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 static void debug64_typevalue(int32_t);
192 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
193 static void debug64_directive(const char *, const char *);
195 /* stabs debugging routines */
196 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
197 static void stabs64_output(int, void *);
198 static void stabs64_generate(void);
199 static void stabs64_cleanup(void);
201 /* dwarf debugging routines */
202 static void dwarf64_init(struct ofmt
*, void *, FILE *, efunc
);
203 static void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
204 static void dwarf64_output(int, void *);
205 static void dwarf64_generate(void);
206 static void dwarf64_cleanup(void);
207 static void dwarf64_findfile(const char *);
208 static 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
= ndebugs
= 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) {
403 error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
404 " declaration of section `%s'", q
, name
);
408 if (!strcmp(name
, ".shstrtab") ||
409 !strcmp(name
, ".symtab") ||
410 !strcmp(name
, ".strtab")) {
411 error(ERR_NONFATAL
, "attempt to redefine reserved section"
416 for (i
= 0; i
< nsects
; i
++)
417 if (!strcmp(name
, sects
[i
]->name
))
420 const struct elf_known_section
*ks
= elf_known_sections
;
423 if (!strcmp(name
, ks
->name
))
428 type
= type
? type
: ks
->type
;
429 align
= align
? align
: ks
->align
;
430 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
432 i
= elf_make_section(name
, type
, flags
, align
);
433 } else if (pass
== 1) {
434 if ((type
&& sects
[i
]->type
!= type
)
435 || (align
&& sects
[i
]->align
!= align
)
436 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
437 error(ERR_WARNING
, "incompatible section attributes ignored on"
438 " redeclaration of section `%s'", name
);
441 return sects
[i
]->index
;
444 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
445 int is_global
, char *special
)
449 bool special_used
= false;
451 #if defined(DEBUG) && DEBUG>2
453 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
454 name
, segment
, offset
, is_global
, special
);
456 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
458 * This is a NASM special symbol. We never allow it into
459 * the ELF symbol table, even if it's a valid one. If it
460 * _isn't_ a valid one, we should barf immediately.
462 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
463 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
464 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
465 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
469 if (is_global
== 3) {
472 * Fix up a forward-reference symbol size from the first
475 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
476 if (!strcmp((*s
)->name
, name
)) {
477 struct tokenval tokval
;
481 while (*p
&& !nasm_isspace(*p
))
483 while (*p
&& nasm_isspace(*p
))
487 tokval
.t_type
= TOKEN_INVALID
;
488 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
491 error(ERR_NONFATAL
, "cannot use relocatable"
492 " expression as symbol size");
494 (*s
)->size
= reloc_value(e
);
498 * Remove it from the list of unresolved sizes.
500 nasm_free((*s
)->name
);
504 return; /* it wasn't an important one */
507 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
508 strslen
+= 1 + strlen(name
);
510 lastsym
= sym
= saa_wstruct(syms
);
512 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
515 sym
->type
= is_global
? SYM_GLOBAL
: 0;
516 sym
->other
= STV_DEFAULT
;
518 if (segment
== NO_SEG
)
519 sym
->section
= SHN_ABS
;
522 sym
->section
= SHN_UNDEF
;
523 if (nsects
== 0 && segment
== def_seg
) {
525 if (segment
!= elf_section_names(".text", 2, &tempint
))
527 "strange segment conditions in ELF driver");
528 sym
->section
= nsects
;
530 for (i
= 0; i
< nsects
; i
++)
531 if (segment
== sects
[i
]->index
) {
532 sym
->section
= i
+ 1;
538 if (is_global
== 2) {
541 sym
->section
= SHN_COMMON
;
543 * We have a common variable. Check the special text to see
544 * if it's a valid number and power of two; if so, store it
545 * as the alignment for the common variable.
549 sym
->symv
.key
= readnum(special
, &err
);
551 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
552 " valid number", special
);
553 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
554 != 2 * sym
->symv
.key
- 1)
555 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
556 " power of two", special
);
560 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
562 if (sym
->type
== SYM_GLOBAL
) {
564 * If sym->section == SHN_ABS, then the first line of the
565 * else section would cause a core dump, because its a reference
566 * beyond the end of the section array.
567 * This behaviour is exhibited by this code:
570 * To avoid such a crash, such requests are silently discarded.
571 * This may not be the best solution.
573 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
574 bsym
= raa_write(bsym
, segment
, nglobs
);
575 } else if (sym
->section
!= SHN_ABS
) {
577 * This is a global symbol; so we must add it to the rbtree
578 * of global symbols in its section.
580 * In addition, we check the special text for symbol
581 * type and size information.
583 sects
[sym
->section
-1]->gsyms
=
584 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
587 int n
= strcspn(special
, " \t");
589 if (!nasm_strnicmp(special
, "function", n
))
590 sym
->type
|= STT_FUNC
;
591 else if (!nasm_strnicmp(special
, "data", n
) ||
592 !nasm_strnicmp(special
, "object", n
))
593 sym
->type
|= STT_OBJECT
;
594 else if (!nasm_strnicmp(special
, "notype", n
))
595 sym
->type
|= STT_NOTYPE
;
597 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
601 while (nasm_isspace(*special
))
604 n
= strcspn(special
, " \t");
605 if (!nasm_strnicmp(special
, "default", n
))
606 sym
->other
= STV_DEFAULT
;
607 else if (!nasm_strnicmp(special
, "internal", n
))
608 sym
->other
= STV_INTERNAL
;
609 else if (!nasm_strnicmp(special
, "hidden", n
))
610 sym
->other
= STV_HIDDEN
;
611 else if (!nasm_strnicmp(special
, "protected", n
))
612 sym
->other
= STV_PROTECTED
;
619 struct tokenval tokval
;
622 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
624 while (special
[n
] && nasm_isspace(special
[n
]))
627 * We have a size expression; attempt to
631 stdscan_bufptr
= special
+ n
;
632 tokval
.t_type
= TOKEN_INVALID
;
633 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
638 sym
->name
= nasm_strdup(name
);
641 error(ERR_NONFATAL
, "cannot use relocatable"
642 " expression as symbol size");
644 sym
->size
= reloc_value(e
);
646 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
651 * If TLS segment, mark symbol accordingly.
653 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
655 sym
->type
|= STT_TLS
;
658 sym
->globnum
= nglobs
;
663 if (special
&& !special_used
)
664 error(ERR_NONFATAL
, "no special symbol features supported here");
667 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
668 int64_t offset
, int type
)
671 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
672 sect
->tail
= &r
->next
;
675 r
->address
= sect
->len
;
677 if (segment
== NO_SEG
)
682 for (i
= 0; i
< nsects
; i
++)
683 if (segment
== sects
[i
]->index
)
686 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
694 * This routine deals with ..got and ..sym relocations: the more
695 * complicated kinds. In shared-library writing, some relocations
696 * with respect to global symbols must refer to the precise symbol
697 * rather than referring to an offset from the base of the section
698 * _containing_ the symbol. Such relocations call to this routine,
699 * which searches the symbol list for the symbol in question.
701 * R_386_GOT32 references require the _exact_ symbol address to be
702 * used; R_386_32 references can be at an offset from the symbol.
703 * The boolean argument `exact' tells us this.
705 * Return value is the adjusted value of `addr', having become an
706 * offset from the symbol rather than the section. Should always be
707 * zero when returning from an exact call.
709 * Limitation: if you define two symbols at the same place,
710 * confusion will occur.
712 * Inefficiency: we search, currently, using a linked list which
713 * isn't even necessarily sorted.
715 static void elf_add_gsym_reloc(struct Section
*sect
,
716 int32_t segment
, uint64_t offset
, int64_t pcrel
,
717 int type
, bool exact
)
726 * First look up the segment/offset pair and find a global
727 * symbol corresponding to it. If it's not one of our segments,
728 * then it must be an external symbol, in which case we're fine
729 * doing a normal elf_add_reloc after first sanity-checking
730 * that the offset from the symbol is zero.
733 for (i
= 0; i
< nsects
; i
++)
734 if (segment
== sects
[i
]->index
) {
741 error(ERR_NONFATAL
, "invalid access to an external symbol");
743 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
747 srb
= rb_search(s
->gsyms
, offset
);
748 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
749 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
750 " for this reference");
753 sym
= container_of(srb
, struct Symbol
, symv
);
755 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
756 sect
->tail
= &r
->next
;
759 r
->address
= sect
->len
;
760 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
761 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
767 static void elf_out(int32_t segto
, const void *data
,
768 enum out_type type
, uint64_t size
,
769 int32_t segment
, int32_t wrt
)
774 static struct symlininfo sinfo
;
778 #if defined(DEBUG) && DEBUG>2
779 if (data
) fprintf(stderr
,
780 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
781 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
783 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
784 currentline
, type
, segment
, segto
, size
);
788 * handle absolute-assembly (structure definitions)
790 if (segto
== NO_SEG
) {
791 if (type
!= OUT_RESERVE
)
792 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
798 for (i
= 0; i
< nsects
; i
++)
799 if (segto
== sects
[i
]->index
) {
804 int tempint
; /* ignored */
805 if (segto
!= elf_section_names(".text", 2, &tempint
))
806 error(ERR_PANIC
, "strange segment conditions in ELF driver");
808 s
= sects
[nsects
- 1];
812 /* invoke current debug_output routine */
813 if (of_elf64
.current_dfmt
) {
814 sinfo
.offset
= s
->len
;
817 sinfo
.name
= s
->name
;
818 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
820 /* end of debugging stuff */
822 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
823 error(ERR_WARNING
, "attempt to initialize memory in"
824 " BSS section `%s': ignored", s
->name
);
825 s
->len
+= realsize(type
, size
);
829 if (type
== OUT_RESERVE
) {
830 if (s
->type
== SHT_PROGBITS
) {
831 error(ERR_WARNING
, "uninitialized space declared in"
832 " non-BSS section `%s': zeroing", s
->name
);
833 elf_sect_write(s
, NULL
, size
);
836 } else if (type
== OUT_RAWDATA
) {
837 if (segment
!= NO_SEG
)
838 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
839 elf_sect_write(s
, data
, size
);
840 } else if (type
== OUT_ADDRESS
) {
841 addr
= *(int64_t *)data
;
842 if (segment
== NO_SEG
) {
844 } else if (segment
% 2) {
845 error(ERR_NONFATAL
, "ELF format does not support"
846 " segment base references");
851 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
854 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
857 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
860 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
863 error(ERR_PANIC
, "internal error elf64-hpa-871");
867 } else if (wrt
== elf_gotpc_sect
+ 1) {
869 * The user will supply GOT relative to $$. ELF
870 * will let us have GOT relative to $. So we
871 * need to fix up the data item by $-$$.
874 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
876 } else if (wrt
== elf_gotoff_sect
+ 1) {
878 error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
879 "references to be qword");
881 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
884 } else if (wrt
== elf_got_sect
+ 1) {
887 elf_add_gsym_reloc(s
, segment
, addr
, 0,
888 R_X86_64_GOT32
, true);
892 elf_add_gsym_reloc(s
, segment
, addr
, 0,
893 R_X86_64_GOT64
, true);
897 error(ERR_NONFATAL
, "invalid ..got reference");
900 } else if (wrt
== elf_sym_sect
+ 1) {
903 elf_add_gsym_reloc(s
, segment
, addr
, 0,
908 elf_add_gsym_reloc(s
, segment
, addr
, 0,
913 elf_add_gsym_reloc(s
, segment
, addr
, 0,
918 elf_add_gsym_reloc(s
, segment
, addr
, 0,
923 error(ERR_PANIC
, "internal error elf64-hpa-903");
926 } else if (wrt
== elf_plt_sect
+ 1) {
927 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
928 "relative PLT references");
930 error(ERR_NONFATAL
, "ELF format does not support this"
934 elf_sect_writeaddr(s
, addr
, size
);
935 } else if (type
== OUT_REL2ADR
) {
936 addr
= *(int64_t *)data
- size
;
937 if (segment
== segto
)
938 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
939 if (segment
== NO_SEG
) {
941 } else if (segment
% 2) {
942 error(ERR_NONFATAL
, "ELF format does not support"
943 " segment base references");
946 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
950 "Unsupported non-32-bit ELF relocation [2]");
953 elf_sect_writeaddr(s
, addr
, 2);
954 } else if (type
== OUT_REL4ADR
) {
955 addr
= *(int64_t *)data
- size
;
956 if (segment
== segto
)
957 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
958 if (segment
== NO_SEG
) {
960 } else if (segment
% 2) {
961 error(ERR_NONFATAL
, "ELF64 format does not support"
962 " segment base references");
965 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
967 } else if (wrt
== elf_plt_sect
+ 1) {
968 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
969 R_X86_64_PLT32
, true);
971 } else if (wrt
== elf_gotpc_sect
+ 1 ||
972 wrt
== elf_got_sect
+ 1) {
973 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
974 R_X86_64_GOTPCREL
, true);
976 } else if (wrt
== elf_gotoff_sect
+ 1 ||
977 wrt
== elf_got_sect
+ 1) {
978 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
980 } else if (wrt
== elf_gottpoff_sect
+ 1) {
981 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
982 R_X86_64_GOTTPOFF
, true);
985 error(ERR_NONFATAL
, "ELF64 format does not support this"
989 elf_sect_writeaddr(s
, addr
, 4);
990 } else if (type
== OUT_REL8ADR
) {
991 addr
= *(int64_t *)data
- size
;
992 if (segment
== segto
)
993 error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
994 if (segment
== NO_SEG
) {
996 } else if (segment
% 2) {
997 error(ERR_NONFATAL
, "ELF64 format does not support"
998 " segment base references");
1000 if (wrt
== NO_SEG
) {
1001 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1003 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1004 wrt
== elf_got_sect
+ 1) {
1005 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1006 R_X86_64_GOTPCREL64
, true);
1008 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1009 wrt
== elf_got_sect
+ 1) {
1010 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1012 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1013 error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1016 error(ERR_NONFATAL
, "ELF64 format does not support this"
1020 elf_sect_writeaddr(s
, addr
, 8);
1024 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.
1039 nsections
= sec_numspecial
+ 1;
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(sec_shstrtab
, 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
);
1126 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1129 /* The normal sections */
1130 for (i
= 0; i
< nsects
; i
++) {
1131 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1132 (sects
[i
]->type
== SHT_PROGBITS
?
1133 sects
[i
]->data
: NULL
), true,
1134 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1139 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1140 shstrtablen
, 0, 0, 1, 0);
1144 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1145 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1149 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, strs
, true,
1150 strslen
, 0, 0, 1, 0);
1153 /* The relocation sections */
1154 for (i
= 0; i
< nsects
; i
++)
1155 if (sects
[i
]->head
) {
1156 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1157 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1161 if (of_elf64
.current_dfmt
== &df_stabs
) {
1162 /* for debugging information, create the last three sections
1163 which are the .stab , .stabstr and .rel.stab sections respectively */
1165 /* this function call creates the stab sections in memory */
1168 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1169 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1170 stablen
, sec_stabstr
, 0, 4, 12);
1173 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1174 stabstrlen
, 0, 0, 4, 0);
1177 /* link -> symtable info -> section to refer to */
1178 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1179 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1183 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1184 /* for dwarf debugging information, create the ten dwarf sections */
1186 /* this function call creates the dwarf sections in memory */
1190 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1191 arangeslen
, 0, 0, 1, 0);
1194 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1195 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1198 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1199 pubnameslen
, 0, 0, 1, 0);
1202 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1203 infolen
, 0, 0, 1, 0);
1206 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1207 inforellen
, symtabsection
, debug_info
, 1, 24);
1210 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1211 abbrevlen
, 0, 0, 1, 0);
1214 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1215 linelen
, 0, 0, 1, 0);
1218 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1219 linerellen
, symtabsection
, debug_line
, 1, 24);
1222 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1223 framelen
, 0, 0, 8, 0);
1226 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1227 loclen
, 0, 0, 1, 0);
1230 fwritezero(align
, elffp
);
1233 * Now output the sections.
1235 elf_write_sections();
1237 nasm_free(elf_sects
);
1241 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1243 struct SAA
*s
= saa_init(1L);
1245 uint8_t entry
[24], *p
;
1251 * First, an all-zeros entry, required by the ELF spec.
1253 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1258 * Next, an entry for the file name.
1261 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1262 WRITESHORT(p
, STT_FILE
); /* type FILE */
1263 WRITESHORT(p
, SHN_ABS
);
1264 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1265 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1266 saa_wbytes(s
, entry
, 24L);
1271 * Now some standard symbols defining the segments, for relocation
1274 for (i
= 1; i
<= nsects
; i
++) {
1276 WRITELONG(p
, 0); /* no symbol name */
1277 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1278 WRITESHORT(p
, i
); /* section id */
1279 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1280 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1281 saa_wbytes(s
, entry
, 24L);
1288 * Now the other local symbols.
1291 while ((sym
= saa_rstruct(syms
))) {
1292 if (sym
->type
& SYM_GLOBAL
)
1295 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1296 WRITECHAR(p
, sym
->type
); /* type and binding */
1297 WRITECHAR(p
, sym
->other
); /* visibility */
1298 WRITESHORT(p
, sym
->section
); /* index into section header table */
1299 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1300 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1301 saa_wbytes(s
, entry
, 24L);
1306 * dwarf needs symbols for debug sections
1307 * which are relocation targets.
1309 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1310 dwarf_infosym
= *local
;
1312 WRITELONG(p
, 0); /* no symbol name */
1313 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1314 WRITESHORT(p
, debug_info
); /* section id */
1315 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1316 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1317 saa_wbytes(s
, entry
, 24L);
1320 dwarf_abbrevsym
= *local
;
1322 WRITELONG(p
, 0); /* no symbol name */
1323 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1324 WRITESHORT(p
, debug_abbrev
); /* section id */
1325 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1326 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1327 saa_wbytes(s
, entry
, 24L);
1330 dwarf_linesym
= *local
;
1332 WRITELONG(p
, 0); /* no symbol name */
1333 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1334 WRITESHORT(p
, debug_line
); /* section id */
1335 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1336 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1337 saa_wbytes(s
, entry
, 24L);
1343 * Now the global symbols.
1346 while ((sym
= saa_rstruct(syms
))) {
1347 if (!(sym
->type
& SYM_GLOBAL
))
1350 WRITELONG(p
, sym
->strpos
);
1351 WRITECHAR(p
, sym
->type
); /* type and binding */
1352 WRITECHAR(p
, sym
->other
); /* visibility */
1353 WRITESHORT(p
, sym
->section
);
1354 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1355 WRITEDLONG(p
, (int64_t)sym
->size
);
1356 saa_wbytes(s
, entry
, 24L);
1363 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1366 uint8_t *p
, entry
[24];
1367 int32_t global_offset
;
1376 * How to onvert from a global placeholder to a real symbol index;
1377 * the +2 refers to the two special entries, the null entry and
1378 * the filename entry.
1380 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1383 int32_t sym
= r
->symbol
;
1385 if (sym
>= GLOBAL_TEMP_BASE
)
1386 sym
+= global_offset
;
1389 WRITEDLONG(p
, r
->address
);
1390 WRITELONG(p
, r
->type
);
1392 WRITEDLONG(p
, r
->offset
);
1393 saa_wbytes(s
, entry
, 24L);
1402 static void elf_section_header(int name
, int type
, uint64_t flags
,
1403 void *data
, bool is_saa
, uint64_t datalen
,
1404 int link
, int info
, int align
, int eltsize
)
1406 elf_sects
[elf_nsect
].data
= data
;
1407 elf_sects
[elf_nsect
].len
= datalen
;
1408 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1411 fwriteint32_t((int32_t)name
, elffp
);
1412 fwriteint32_t((int32_t)type
, elffp
);
1413 fwriteint64_t((int64_t)flags
, elffp
);
1414 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1415 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1416 fwriteint64_t(datalen
, elffp
);
1418 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1419 fwriteint32_t((int32_t)link
, elffp
);
1420 fwriteint32_t((int32_t)info
, elffp
);
1421 fwriteint64_t((int64_t)align
, elffp
);
1422 fwriteint64_t((int64_t)eltsize
, elffp
);
1425 static void elf_write_sections(void)
1428 for (i
= 0; i
< elf_nsect
; i
++)
1429 if (elf_sects
[i
].data
) {
1430 int32_t len
= elf_sects
[i
].len
;
1431 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1432 int32_t align
= reallen
- len
;
1433 if (elf_sects
[i
].is_saa
)
1434 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1436 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1437 fwritezero(align
, elffp
);
1441 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1443 saa_wbytes(sect
->data
, data
, len
);
1446 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1448 saa_writeaddr(sect
->data
, data
, len
);
1452 static int32_t elf_segbase(int32_t segment
)
1457 static int elf_directive(char *directive
, char *value
, int pass
)
1463 if (!strcmp(directive
, "osabi")) {
1465 return 1; /* ignore in pass 2 */
1467 n
= readnum(value
, &err
);
1469 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1472 if (n
< 0 || n
> 255) {
1473 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1479 if ((p
= strchr(value
,',')) == NULL
)
1482 n
= readnum(p
+1, &err
);
1483 if (err
|| n
< 0 || n
> 255) {
1484 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1495 static void elf_filename(char *inname
, char *outname
, efunc error
)
1497 strcpy(elf_module
, inname
);
1498 standard_extension(inname
, outname
, ".o", error
);
1501 extern macros_t elf_stdmac
[];
1503 static int elf_set_info(enum geninfo type
, char **val
)
1509 static struct dfmt df_dwarf
= {
1510 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1520 static struct dfmt df_stabs
= {
1521 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1532 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1534 struct ofmt of_elf64
= {
1535 "ELF64 (x86_64) object files (e.g. Linux)",
1552 /* common debugging routines */
1553 static void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
,
1554 int is_global
, char *special
)
1563 static void debug64_directive(const char *directive
, const char *params
)
1569 static 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 */
1634 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1637 if (!stabs_filename
) {
1638 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1639 strcpy(stabs_filename
, filename
);
1641 if (strcmp(stabs_filename
, filename
)) {
1642 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1643 in fact, this leak comes in quite handy to maintain a list of files
1644 encountered so far in the symbol lines... */
1646 /* why not nasm_free(stabs_filename); we're done with the old one */
1648 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1649 strcpy(stabs_filename
, filename
);
1653 currentline
= linenumber
;
1657 static void stabs64_output(int type
, void *param
)
1659 struct symlininfo
*s
;
1660 struct linelist
*el
;
1661 if (type
== TY_DEBUGSYMLIN
) {
1662 if (debug_immcall
) {
1663 s
= (struct symlininfo
*)param
;
1664 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1665 return; /* line info is only collected for executable sections */
1667 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1668 el
->info
.offset
= s
->offset
;
1669 el
->info
.section
= s
->section
;
1670 el
->info
.name
= s
->name
;
1671 el
->line
= currentline
;
1672 el
->filename
= stabs_filename
;
1675 stabslines
->last
->next
= el
;
1676 stabslines
->last
= el
;
1679 stabslines
->last
= el
;
1686 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1688 WRITELONG(p,n_strx); \
1689 WRITECHAR(p,n_type); \
1690 WRITECHAR(p,n_other); \
1691 WRITESHORT(p,n_desc); \
1692 WRITELONG(p,n_value); \
1695 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1697 static void stabs64_generate(void)
1699 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1700 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1704 struct linelist
*ptr
;
1708 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1709 for (i
= 0; i
< numlinestabs
; i
++)
1713 if (numfiles
== 0) {
1714 allfiles
[0] = ptr
->filename
;
1717 for (i
= 0; i
< numfiles
; i
++) {
1718 if (!strcmp(allfiles
[i
], ptr
->filename
))
1721 if (i
>= numfiles
) {
1722 allfiles
[i
] = ptr
->filename
;
1729 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1730 for (i
= 0; i
< numfiles
; i
++) {
1731 fileidx
[i
] = strsize
;
1732 strsize
+= strlen(allfiles
[i
]) + 1;
1735 for (i
= 0; i
< numfiles
; i
++) {
1736 if (!strcmp(allfiles
[i
], elf_module
)) {
1742 /* worst case size of the stab buffer would be:
1743 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1746 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1747 sizeof(struct stabentry
));
1749 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1751 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1754 for (i
= 0; i
< numfiles
; i
++) {
1755 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1759 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1766 /* this is the first stab, its strx points to the filename of the
1767 the source-file, the n_desc field should be set to the number
1770 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1772 /* this is the stab for the main source file */
1773 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1775 /* relocation table entry */
1777 /* Since the symbol table has two entries before */
1778 /* the section symbols, the index in the info.section */
1779 /* member must be adjusted by adding 2 */
1781 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1782 WRITELONG(rptr
, R_X86_64_32
);
1783 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1786 currfile
= mainfileindex
;
1790 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1791 /* oops file has changed... */
1792 for (i
= 0; i
< numfiles
; i
++)
1793 if (!strcmp(allfiles
[i
], ptr
->filename
))
1796 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1800 /* relocation table entry */
1802 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1803 WRITELONG(rptr
, R_X86_64_32
);
1804 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1807 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1810 /* relocation table entry */
1812 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1813 WRITELONG(rptr
, R_X86_64_32
);
1814 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1820 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1822 nasm_free(allfiles
);
1825 stablen
= (sptr
- sbuf
);
1826 stabrellen
= (rptr
- rbuf
);
1832 static void stabs64_cleanup(void)
1834 struct linelist
*ptr
, *del
;
1846 nasm_free(stabrelbuf
);
1848 nasm_free(stabstrbuf
);
1850 /* dwarf routines */
1851 static void dwarf64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1858 ndebugs
= 3; /* 3 debug symbols */
1861 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1865 dwarf64_findfile(filename
);
1867 currentline
= linenumber
;
1870 /* called from elf_out with type == TY_DEBUGSYMLIN */
1871 static void dwarf64_output(int type
, void *param
)
1873 int ln
, aa
, inx
, maxln
, soc
;
1874 struct symlininfo
*s
;
1879 s
= (struct symlininfo
*)param
;
1880 /* line number info is only gathered for executable sections */
1881 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1883 /* Check if section index has changed */
1884 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1886 dwarf64_findsect(s
->section
);
1888 /* do nothing unless line or file has changed */
1891 ln
= currentline
- dwarf_csect
->line
;
1892 aa
= s
->offset
- dwarf_csect
->offset
;
1893 inx
= dwarf_clist
->line
;
1894 plinep
= dwarf_csect
->psaa
;
1895 /* check for file change */
1896 if (!(inx
== dwarf_csect
->file
))
1898 saa_write8(plinep
,DW_LNS_set_file
);
1899 saa_write8(plinep
,inx
);
1900 dwarf_csect
->file
= inx
;
1902 /* check for line change */
1905 /* test if in range of special op code */
1906 maxln
= line_base
+ line_range
;
1907 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1908 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1910 saa_write8(plinep
,soc
);
1916 saa_write8(plinep
,DW_LNS_advance_line
);
1917 saa_wleb128s(plinep
,ln
);
1921 saa_write8(plinep
,DW_LNS_advance_pc
);
1922 saa_wleb128u(plinep
,aa
);
1925 dwarf_csect
->line
= currentline
;
1926 dwarf_csect
->offset
= s
->offset
;
1928 /* show change handled */
1934 static void dwarf64_generate(void)
1938 struct linelist
*ftentry
;
1939 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1940 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1941 struct sectlist
*psect
;
1942 size_t saalen
, linepoff
, totlen
, highaddr
;
1944 /* write epilogues for each line program range */
1945 /* and build aranges section */
1946 paranges
= saa_init(1L);
1947 parangesrel
= saa_init(1L);
1948 saa_write16(paranges
,3); /* dwarf version */
1949 saa_write64(parangesrel
, paranges
->datalen
+4);
1950 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1951 saa_write64(parangesrel
, 0);
1952 saa_write32(paranges
,0); /* offset into info */
1953 saa_write8(paranges
,8); /* pointer size */
1954 saa_write8(paranges
,0); /* not segmented */
1955 saa_write32(paranges
,0); /* padding */
1956 /* iterate though sectlist entries */
1957 psect
= dwarf_fsect
;
1960 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1962 plinep
= psect
->psaa
;
1963 /* Line Number Program Epilogue */
1964 saa_write8(plinep
,2); /* std op 2 */
1965 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1966 saa_write8(plinep
,DW_LNS_extended_op
);
1967 saa_write8(plinep
,1); /* operand length */
1968 saa_write8(plinep
,DW_LNE_end_sequence
);
1969 totlen
+= plinep
->datalen
;
1970 /* range table relocation entry */
1971 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1972 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1973 saa_write64(parangesrel
, (uint64_t) 0);
1974 /* range table entry */
1975 saa_write64(paranges
,0x0000); /* range start */
1976 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1977 highaddr
+= sects
[psect
->section
]->len
;
1978 /* done with this entry */
1979 psect
= psect
->next
;
1981 saa_write64(paranges
,0); /* null address */
1982 saa_write64(paranges
,0); /* null length */
1983 saalen
= paranges
->datalen
;
1984 arangeslen
= saalen
+ 4;
1985 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1986 WRITELONG(pbuf
,saalen
); /* initial length */
1987 saa_rnbytes(paranges
, pbuf
, saalen
);
1990 /* build rela.aranges section */
1991 arangesrellen
= saalen
= parangesrel
->datalen
;
1992 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1993 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1994 saa_free(parangesrel
);
1996 /* build pubnames section */
1997 ppubnames
= saa_init(1L);
1998 saa_write16(ppubnames
,3); /* dwarf version */
1999 saa_write32(ppubnames
,0); /* offset into info */
2000 saa_write32(ppubnames
,0); /* space used in info */
2001 saa_write32(ppubnames
,0); /* end of list */
2002 saalen
= ppubnames
->datalen
;
2003 pubnameslen
= saalen
+ 4;
2004 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2005 WRITELONG(pbuf
,saalen
); /* initial length */
2006 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2007 saa_free(ppubnames
);
2009 /* build info section */
2010 pinfo
= saa_init(1L);
2011 pinforel
= saa_init(1L);
2012 saa_write16(pinfo
,3); /* dwarf version */
2013 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2014 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2015 saa_write64(pinforel
, 0);
2016 saa_write32(pinfo
,0); /* offset into abbrev */
2017 saa_write8(pinfo
,8); /* pointer size */
2018 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2019 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2020 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2021 saa_write64(pinforel
, 0);
2022 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2023 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2024 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2025 saa_write64(pinforel
, 0);
2026 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2027 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2028 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2029 saa_write64(pinforel
, 0);
2030 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2031 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2032 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2033 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2034 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2035 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2036 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2037 saa_write64(pinforel
, 0);
2038 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2039 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2040 saa_write8(pinfo
,0); /* end of entries */
2041 saalen
= pinfo
->datalen
;
2042 infolen
= saalen
+ 4;
2043 infobuf
= pbuf
= nasm_malloc(infolen
);
2044 WRITELONG(pbuf
,saalen
); /* initial length */
2045 saa_rnbytes(pinfo
, pbuf
, saalen
);
2048 /* build rela.info section */
2049 inforellen
= saalen
= pinforel
->datalen
;
2050 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2051 saa_rnbytes(pinforel
, pbuf
, saalen
);
2054 /* build abbrev section */
2055 pabbrev
= saa_init(1L);
2056 saa_write8(pabbrev
,1); /* entry number LEB128u */
2057 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2058 saa_write8(pabbrev
,1); /* has children */
2059 /* the following attributes and forms are all LEB128u values */
2060 saa_write8(pabbrev
,DW_AT_low_pc
);
2061 saa_write8(pabbrev
,DW_FORM_addr
);
2062 saa_write8(pabbrev
,DW_AT_high_pc
);
2063 saa_write8(pabbrev
,DW_FORM_addr
);
2064 saa_write8(pabbrev
,DW_AT_stmt_list
);
2065 saa_write8(pabbrev
,DW_FORM_data4
);
2066 saa_write8(pabbrev
,DW_AT_name
);
2067 saa_write8(pabbrev
,DW_FORM_string
);
2068 saa_write8(pabbrev
,DW_AT_producer
);
2069 saa_write8(pabbrev
,DW_FORM_string
);
2070 saa_write8(pabbrev
,DW_AT_language
);
2071 saa_write8(pabbrev
,DW_FORM_data2
);
2072 saa_write16(pabbrev
,0); /* end of entry */
2073 /* LEB128u usage same as above */
2074 saa_write8(pabbrev
,2); /* entry number */
2075 saa_write8(pabbrev
,DW_TAG_subprogram
);
2076 saa_write8(pabbrev
,0); /* no children */
2077 saa_write8(pabbrev
,DW_AT_low_pc
);
2078 saa_write8(pabbrev
,DW_FORM_addr
);
2079 saa_write8(pabbrev
,DW_AT_frame_base
);
2080 saa_write8(pabbrev
,DW_FORM_data4
);
2081 saa_write16(pabbrev
,0); /* end of entry */
2082 abbrevlen
= saalen
= pabbrev
->datalen
;
2083 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2084 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2087 /* build line section */
2089 plines
= saa_init(1L);
2090 saa_write8(plines
,1); /* Minimum Instruction Length */
2091 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2092 saa_write8(plines
,line_base
); /* Line Base */
2093 saa_write8(plines
,line_range
); /* Line Range */
2094 saa_write8(plines
,opcode_base
); /* Opcode Base */
2095 /* standard opcode lengths (# of LEB128u operands) */
2096 saa_write8(plines
,0); /* Std opcode 1 length */
2097 saa_write8(plines
,1); /* Std opcode 2 length */
2098 saa_write8(plines
,1); /* Std opcode 3 length */
2099 saa_write8(plines
,1); /* Std opcode 4 length */
2100 saa_write8(plines
,1); /* Std opcode 5 length */
2101 saa_write8(plines
,0); /* Std opcode 6 length */
2102 saa_write8(plines
,0); /* Std opcode 7 length */
2103 saa_write8(plines
,0); /* Std opcode 8 length */
2104 saa_write8(plines
,1); /* Std opcode 9 length */
2105 saa_write8(plines
,0); /* Std opcode 10 length */
2106 saa_write8(plines
,0); /* Std opcode 11 length */
2107 saa_write8(plines
,1); /* Std opcode 12 length */
2108 /* Directory Table */
2109 saa_write8(plines
,0); /* End of table */
2110 /* File Name Table */
2111 ftentry
= dwarf_flist
;
2112 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2114 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2115 saa_write8(plines
,0); /* directory LEB128u */
2116 saa_write8(plines
,0); /* time LEB128u */
2117 saa_write8(plines
,0); /* size LEB128u */
2118 ftentry
= ftentry
->next
;
2120 saa_write8(plines
,0); /* End of table */
2121 linepoff
= plines
->datalen
;
2122 linelen
= linepoff
+ totlen
+ 10;
2123 linebuf
= pbuf
= nasm_malloc(linelen
);
2124 WRITELONG(pbuf
,linelen
-4); /* initial length */
2125 WRITESHORT(pbuf
,3); /* dwarf version */
2126 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2127 /* write line header */
2129 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2132 /* concatonate line program ranges */
2134 plinesrel
= saa_init(1L);
2135 psect
= dwarf_fsect
;
2136 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2138 saa_write64(plinesrel
, linepoff
);
2139 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2140 saa_write64(plinesrel
, (uint64_t) 0);
2141 plinep
= psect
->psaa
;
2142 saalen
= plinep
->datalen
;
2143 saa_rnbytes(plinep
, pbuf
, saalen
);
2147 /* done with this entry */
2148 psect
= psect
->next
;
2152 /* build rela.lines section */
2153 linerellen
=saalen
= plinesrel
->datalen
;
2154 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2155 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2156 saa_free(plinesrel
);
2158 /* build frame section */
2160 framebuf
= pbuf
= nasm_malloc(framelen
);
2161 WRITELONG(pbuf
,framelen
-4); /* initial length */
2163 /* build loc section */
2165 locbuf
= pbuf
= nasm_malloc(loclen
);
2166 WRITEDLONG(pbuf
,0); /* null beginning offset */
2167 WRITEDLONG(pbuf
,0); /* null ending offset */
2170 static void dwarf64_cleanup(void)
2173 nasm_free(arangesbuf
);
2175 nasm_free(arangesrelbuf
);
2177 nasm_free(pubnamesbuf
);
2181 nasm_free(inforelbuf
);
2183 nasm_free(abbrevbuf
);
2187 nasm_free(linerelbuf
);
2189 nasm_free(framebuf
);
2193 static void dwarf64_findfile(const char * fname
)
2196 struct linelist
*match
;
2198 /* return if fname is current file name */
2199 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2200 /* search for match */
2206 match
= dwarf_flist
;
2207 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2209 if (!(strcmp(fname
, match
->filename
)))
2211 dwarf_clist
= match
;
2216 /* add file name to end of list */
2217 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2219 dwarf_clist
->line
= dwarf_numfiles
;
2220 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2221 strcpy(dwarf_clist
->filename
,fname
);
2222 dwarf_clist
->next
= 0;
2223 /* if first entry */
2226 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2227 dwarf_clist
->last
= 0;
2229 /* chain to previous entry */
2232 dwarf_elist
->next
= dwarf_clist
;
2233 dwarf_elist
= dwarf_clist
;
2238 static void dwarf64_findsect(const int index
)
2241 struct sectlist
*match
;
2243 /* return if index is current section index */
2244 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2248 /* search for match */
2254 match
= dwarf_fsect
;
2255 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2257 if ((match
->section
== index
))
2259 dwarf_csect
= match
;
2262 match
= match
->next
;
2265 /* add entry to end of list */
2266 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2268 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2269 dwarf_csect
->line
= 1;
2270 dwarf_csect
->offset
= 0;
2271 dwarf_csect
->file
= 1;
2272 dwarf_csect
->section
= index
;
2273 dwarf_csect
->next
= 0;
2274 /* set relocatable address at start of line program */
2275 saa_write8(plinep
,DW_LNS_extended_op
);
2276 saa_write8(plinep
,9); /* operand length */
2277 saa_write8(plinep
,DW_LNE_set_address
);
2278 saa_write64(plinep
,0); /* Start Address */
2279 /* if first entry */
2282 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2283 dwarf_csect
->last
= 0;
2285 /* chain to previous entry */
2288 dwarf_esect
->next
= dwarf_csect
;
2289 dwarf_esect
= dwarf_csect
;