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
, 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_init(struct ofmt
*, void *, FILE *, efunc
);
197 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
198 static void stabs64_output(int, void *);
199 static void stabs64_generate(void);
200 static void stabs64_cleanup(void);
202 /* dwarf debugging routines */
203 static void dwarf64_init(struct ofmt
*, void *, FILE *, efunc
);
204 static void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
205 static void dwarf64_output(int, void *);
206 static void dwarf64_generate(void);
207 static void dwarf64_cleanup(void);
208 static void dwarf64_findfile(const char *);
209 static void dwarf64_findsect(const int);
212 * Special section numbers which are used to define ELF special
213 * symbols, which can be used with WRT to provide PIC relocation
216 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
217 static int32_t elf_got_sect
, elf_plt_sect
;
218 static int32_t elf_sym_sect
;
219 static int32_t elf_gottpoff_sect
;
221 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
227 (void)ldef
; /* placate optimisers */
229 nsects
= sectlen
= 0;
230 syms
= saa_init((int32_t)sizeof(struct Symbol
));
231 nlocals
= nglobs
= ndebugs
= 0;
234 saa_wbytes(strs
, "\0", 1L);
235 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
236 strslen
= 2 + strlen(elf_module
);
238 shstrtablen
= shstrtabsize
= 0;;
239 add_sectname("", "");
243 elf_gotpc_sect
= seg_alloc();
244 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
246 elf_gotoff_sect
= seg_alloc();
247 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
249 elf_got_sect
= seg_alloc();
250 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
252 elf_plt_sect
= seg_alloc();
253 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
255 elf_sym_sect
= seg_alloc();
256 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
258 elf_gottpoff_sect
= seg_alloc();
259 ldef("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
262 def_seg
= seg_alloc();
266 static void elf_cleanup(int debuginfo
)
275 for (i
= 0; i
< nsects
; i
++) {
276 if (sects
[i
]->type
!= SHT_NOBITS
)
277 saa_free(sects
[i
]->data
);
279 saa_free(sects
[i
]->rel
);
280 while (sects
[i
]->head
) {
282 sects
[i
]->head
= sects
[i
]->head
->next
;
290 if (of_elf64
.current_dfmt
) {
291 of_elf64
.current_dfmt
->cleanup();
294 /* add entry to the elf .shstrtab section */
295 static void add_sectname(char *firsthalf
, char *secondhalf
)
297 int len
= strlen(firsthalf
) + strlen(secondhalf
);
298 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
299 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
300 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
301 strcat(shstrtab
+ shstrtablen
, secondhalf
);
302 shstrtablen
+= len
+ 1;
305 static int elf_make_section(char *name
, int type
, int flags
, int align
)
309 s
= nasm_malloc(sizeof(*s
));
311 if (type
!= SHT_NOBITS
)
312 s
->data
= saa_init(1L);
315 s
->len
= s
->size
= 0;
317 if (!strcmp(name
, ".text"))
320 s
->index
= seg_alloc();
321 add_sectname("", name
);
322 s
->name
= nasm_malloc(1 + strlen(name
));
323 strcpy(s
->name
, name
);
329 if (nsects
>= sectlen
)
330 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
336 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
339 uint32_t flags
, flags_and
, flags_or
;
344 * Default is 64 bits.
352 while (*p
&& !nasm_isspace(*p
))
356 flags_and
= flags_or
= type
= align
= 0;
358 while (*p
&& nasm_isspace(*p
))
362 while (*p
&& !nasm_isspace(*p
))
366 while (*p
&& nasm_isspace(*p
))
369 if (!nasm_strnicmp(q
, "align=", 6)) {
373 if ((align
- 1) & align
) { /* means it's not a power of two */
374 error(ERR_NONFATAL
, "section alignment %d is not"
375 " a power of two", align
);
378 } else if (!nasm_stricmp(q
, "alloc")) {
379 flags_and
|= SHF_ALLOC
;
380 flags_or
|= SHF_ALLOC
;
381 } else if (!nasm_stricmp(q
, "noalloc")) {
382 flags_and
|= SHF_ALLOC
;
383 flags_or
&= ~SHF_ALLOC
;
384 } else if (!nasm_stricmp(q
, "exec")) {
385 flags_and
|= SHF_EXECINSTR
;
386 flags_or
|= SHF_EXECINSTR
;
387 } else if (!nasm_stricmp(q
, "noexec")) {
388 flags_and
|= SHF_EXECINSTR
;
389 flags_or
&= ~SHF_EXECINSTR
;
390 } else if (!nasm_stricmp(q
, "write")) {
391 flags_and
|= SHF_WRITE
;
392 flags_or
|= SHF_WRITE
;
393 } else if (!nasm_stricmp(q
, "tls")) {
394 flags_and
|= SHF_TLS
;
396 } else if (!nasm_stricmp(q
, "nowrite")) {
397 flags_and
|= SHF_WRITE
;
398 flags_or
&= ~SHF_WRITE
;
399 } else if (!nasm_stricmp(q
, "progbits")) {
401 } else if (!nasm_stricmp(q
, "nobits")) {
403 } else if (pass
== 1) {
404 error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
405 " declaration of section `%s'", q
, name
);
409 if (!strcmp(name
, ".shstrtab") ||
410 !strcmp(name
, ".symtab") ||
411 !strcmp(name
, ".strtab")) {
412 error(ERR_NONFATAL
, "attempt to redefine reserved section"
417 for (i
= 0; i
< nsects
; i
++)
418 if (!strcmp(name
, sects
[i
]->name
))
421 const struct elf_known_section
*ks
= elf_known_sections
;
424 if (!strcmp(name
, ks
->name
))
429 type
= type
? type
: ks
->type
;
430 align
= align
? align
: ks
->align
;
431 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
433 i
= elf_make_section(name
, type
, flags
, align
);
434 } else if (pass
== 1) {
435 if ((type
&& sects
[i
]->type
!= type
)
436 || (align
&& sects
[i
]->align
!= align
)
437 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
438 error(ERR_WARNING
, "incompatible section attributes ignored on"
439 " redeclaration of section `%s'", name
);
442 return sects
[i
]->index
;
445 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
446 int is_global
, char *special
)
450 bool special_used
= false;
452 #if defined(DEBUG) && DEBUG>2
454 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
455 name
, segment
, offset
, is_global
, special
);
457 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
459 * This is a NASM special symbol. We never allow it into
460 * the ELF symbol table, even if it's a valid one. If it
461 * _isn't_ a valid one, we should barf immediately.
463 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
464 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
465 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
466 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
470 if (is_global
== 3) {
473 * Fix up a forward-reference symbol size from the first
476 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
477 if (!strcmp((*s
)->name
, name
)) {
478 struct tokenval tokval
;
482 while (*p
&& !nasm_isspace(*p
))
484 while (*p
&& nasm_isspace(*p
))
488 tokval
.t_type
= TOKEN_INVALID
;
489 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
492 error(ERR_NONFATAL
, "cannot use relocatable"
493 " expression as symbol size");
495 (*s
)->size
= reloc_value(e
);
499 * Remove it from the list of unresolved sizes.
501 nasm_free((*s
)->name
);
505 return; /* it wasn't an important one */
508 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
509 strslen
+= 1 + strlen(name
);
511 lastsym
= sym
= saa_wstruct(syms
);
513 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
516 sym
->type
= is_global
? SYM_GLOBAL
: 0;
517 sym
->other
= STV_DEFAULT
;
519 if (segment
== NO_SEG
)
520 sym
->section
= SHN_ABS
;
523 sym
->section
= SHN_UNDEF
;
524 if (nsects
== 0 && segment
== def_seg
) {
526 if (segment
!= elf_section_names(".text", 2, &tempint
))
528 "strange segment conditions in ELF driver");
529 sym
->section
= nsects
;
531 for (i
= 0; i
< nsects
; i
++)
532 if (segment
== sects
[i
]->index
) {
533 sym
->section
= i
+ 1;
539 if (is_global
== 2) {
542 sym
->section
= SHN_COMMON
;
544 * We have a common variable. Check the special text to see
545 * if it's a valid number and power of two; if so, store it
546 * as the alignment for the common variable.
550 sym
->symv
.key
= readnum(special
, &err
);
552 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
553 " valid number", special
);
554 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
555 != 2 * sym
->symv
.key
- 1)
556 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
557 " power of two", special
);
561 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
563 if (sym
->type
== SYM_GLOBAL
) {
565 * If sym->section == SHN_ABS, then the first line of the
566 * else section would cause a core dump, because its a reference
567 * beyond the end of the section array.
568 * This behaviour is exhibited by this code:
571 * To avoid such a crash, such requests are silently discarded.
572 * This may not be the best solution.
574 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
575 bsym
= raa_write(bsym
, segment
, nglobs
);
576 } else if (sym
->section
!= SHN_ABS
) {
578 * This is a global symbol; so we must add it to the rbtree
579 * of global symbols in its section.
581 * In addition, we check the special text for symbol
582 * type and size information.
584 sects
[sym
->section
-1]->gsyms
=
585 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
588 int n
= strcspn(special
, " \t");
590 if (!nasm_strnicmp(special
, "function", n
))
591 sym
->type
|= STT_FUNC
;
592 else if (!nasm_strnicmp(special
, "data", n
) ||
593 !nasm_strnicmp(special
, "object", n
))
594 sym
->type
|= STT_OBJECT
;
595 else if (!nasm_strnicmp(special
, "notype", n
))
596 sym
->type
|= STT_NOTYPE
;
598 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
602 while (nasm_isspace(*special
))
605 n
= strcspn(special
, " \t");
606 if (!nasm_strnicmp(special
, "default", n
))
607 sym
->other
= STV_DEFAULT
;
608 else if (!nasm_strnicmp(special
, "internal", n
))
609 sym
->other
= STV_INTERNAL
;
610 else if (!nasm_strnicmp(special
, "hidden", n
))
611 sym
->other
= STV_HIDDEN
;
612 else if (!nasm_strnicmp(special
, "protected", n
))
613 sym
->other
= STV_PROTECTED
;
620 struct tokenval tokval
;
623 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
625 while (special
[n
] && nasm_isspace(special
[n
]))
628 * We have a size expression; attempt to
632 stdscan_bufptr
= special
+ n
;
633 tokval
.t_type
= TOKEN_INVALID
;
634 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
639 sym
->name
= nasm_strdup(name
);
642 error(ERR_NONFATAL
, "cannot use relocatable"
643 " expression as symbol size");
645 sym
->size
= reloc_value(e
);
647 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
652 * If TLS segment, mark symbol accordingly.
654 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
656 sym
->type
|= STT_TLS
;
659 sym
->globnum
= nglobs
;
664 if (special
&& !special_used
)
665 error(ERR_NONFATAL
, "no special symbol features supported here");
668 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
669 int64_t offset
, int type
)
672 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
673 sect
->tail
= &r
->next
;
676 r
->address
= sect
->len
;
678 if (segment
== NO_SEG
)
683 for (i
= 0; i
< nsects
; i
++)
684 if (segment
== sects
[i
]->index
)
687 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
695 * This routine deals with ..got and ..sym relocations: the more
696 * complicated kinds. In shared-library writing, some relocations
697 * with respect to global symbols must refer to the precise symbol
698 * rather than referring to an offset from the base of the section
699 * _containing_ the symbol. Such relocations call to this routine,
700 * which searches the symbol list for the symbol in question.
702 * R_386_GOT32 references require the _exact_ symbol address to be
703 * used; R_386_32 references can be at an offset from the symbol.
704 * The boolean argument `exact' tells us this.
706 * Return value is the adjusted value of `addr', having become an
707 * offset from the symbol rather than the section. Should always be
708 * zero when returning from an exact call.
710 * Limitation: if you define two symbols at the same place,
711 * confusion will occur.
713 * Inefficiency: we search, currently, using a linked list which
714 * isn't even necessarily sorted.
716 static void elf_add_gsym_reloc(struct Section
*sect
,
717 int32_t segment
, uint64_t offset
, int64_t pcrel
,
718 int type
, bool exact
)
727 * First look up the segment/offset pair and find a global
728 * symbol corresponding to it. If it's not one of our segments,
729 * then it must be an external symbol, in which case we're fine
730 * doing a normal elf_add_reloc after first sanity-checking
731 * that the offset from the symbol is zero.
734 for (i
= 0; i
< nsects
; i
++)
735 if (segment
== sects
[i
]->index
) {
742 error(ERR_NONFATAL
, "invalid access to an external symbol");
744 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
748 srb
= rb_search(s
->gsyms
, offset
);
749 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
750 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
751 " for this reference");
754 sym
= container_of(srb
, struct Symbol
, symv
);
756 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
757 sect
->tail
= &r
->next
;
760 r
->address
= sect
->len
;
761 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
762 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
768 static void elf_out(int32_t segto
, const void *data
,
769 enum out_type type
, uint64_t size
,
770 int32_t segment
, int32_t wrt
)
775 static struct symlininfo sinfo
;
779 #if defined(DEBUG) && DEBUG>2
780 if (data
) fprintf(stderr
,
781 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
782 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
784 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
785 currentline
, type
, segment
, segto
, size
);
789 * handle absolute-assembly (structure definitions)
791 if (segto
== NO_SEG
) {
792 if (type
!= OUT_RESERVE
)
793 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
799 for (i
= 0; i
< nsects
; i
++)
800 if (segto
== sects
[i
]->index
) {
805 int tempint
; /* ignored */
806 if (segto
!= elf_section_names(".text", 2, &tempint
))
807 error(ERR_PANIC
, "strange segment conditions in ELF driver");
809 s
= sects
[nsects
- 1];
813 /* invoke current debug_output routine */
814 if (of_elf64
.current_dfmt
) {
815 sinfo
.offset
= s
->len
;
818 sinfo
.name
= s
->name
;
819 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
821 /* end of debugging stuff */
823 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
824 error(ERR_WARNING
, "attempt to initialize memory in"
825 " BSS section `%s': ignored", s
->name
);
826 s
->len
+= realsize(type
, size
);
830 if (type
== OUT_RESERVE
) {
831 if (s
->type
== SHT_PROGBITS
) {
832 error(ERR_WARNING
, "uninitialized space declared in"
833 " non-BSS section `%s': zeroing", s
->name
);
834 elf_sect_write(s
, NULL
, size
);
837 } else if (type
== OUT_RAWDATA
) {
838 if (segment
!= NO_SEG
)
839 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
840 elf_sect_write(s
, data
, size
);
841 } else if (type
== OUT_ADDRESS
) {
842 addr
= *(int64_t *)data
;
843 if (segment
== NO_SEG
) {
845 } else if (segment
% 2) {
846 error(ERR_NONFATAL
, "ELF format does not support"
847 " segment base references");
852 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
855 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
858 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
861 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
864 error(ERR_PANIC
, "internal error elf64-hpa-871");
868 } else if (wrt
== elf_gotpc_sect
+ 1) {
870 * The user will supply GOT relative to $$. ELF
871 * will let us have GOT relative to $. So we
872 * need to fix up the data item by $-$$.
875 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
877 } else if (wrt
== elf_gotoff_sect
+ 1) {
879 error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
880 "references to be qword");
882 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
885 } else if (wrt
== elf_got_sect
+ 1) {
888 elf_add_gsym_reloc(s
, segment
, addr
, 0,
889 R_X86_64_GOT32
, true);
893 elf_add_gsym_reloc(s
, segment
, addr
, 0,
894 R_X86_64_GOT64
, true);
898 error(ERR_NONFATAL
, "invalid ..got reference");
901 } else if (wrt
== elf_sym_sect
+ 1) {
904 elf_add_gsym_reloc(s
, segment
, addr
, 0,
909 elf_add_gsym_reloc(s
, segment
, addr
, 0,
914 elf_add_gsym_reloc(s
, segment
, addr
, 0,
919 elf_add_gsym_reloc(s
, segment
, addr
, 0,
924 error(ERR_PANIC
, "internal error elf64-hpa-903");
927 } else if (wrt
== elf_plt_sect
+ 1) {
928 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
929 "relative PLT references");
931 error(ERR_NONFATAL
, "ELF format does not support this"
935 elf_sect_writeaddr(s
, addr
, size
);
936 } else if (type
== OUT_REL2ADR
) {
937 addr
= *(int64_t *)data
- size
;
938 if (segment
== segto
)
939 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
940 if (segment
== NO_SEG
) {
942 } else if (segment
% 2) {
943 error(ERR_NONFATAL
, "ELF format does not support"
944 " segment base references");
947 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
951 "Unsupported non-32-bit ELF relocation [2]");
954 elf_sect_writeaddr(s
, addr
, 2);
955 } else if (type
== OUT_REL4ADR
) {
956 addr
= *(int64_t *)data
- size
;
957 if (segment
== segto
)
958 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
959 if (segment
== NO_SEG
) {
961 } else if (segment
% 2) {
962 error(ERR_NONFATAL
, "ELF64 format does not support"
963 " segment base references");
966 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
968 } else if (wrt
== elf_plt_sect
+ 1) {
969 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
970 R_X86_64_PLT32
, true);
972 } else if (wrt
== elf_gotpc_sect
+ 1 ||
973 wrt
== elf_got_sect
+ 1) {
974 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
975 R_X86_64_GOTPCREL
, true);
977 } else if (wrt
== elf_gotoff_sect
+ 1 ||
978 wrt
== elf_got_sect
+ 1) {
979 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
981 } else if (wrt
== elf_gottpoff_sect
+ 1) {
982 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
983 R_X86_64_GOTTPOFF
, true);
986 error(ERR_NONFATAL
, "ELF64 format does not support this"
990 elf_sect_writeaddr(s
, addr
, 4);
991 } else if (type
== OUT_REL8ADR
) {
992 addr
= *(int64_t *)data
- size
;
993 if (segment
== segto
)
994 error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
995 if (segment
== NO_SEG
) {
997 } else if (segment
% 2) {
998 error(ERR_NONFATAL
, "ELF64 format does not support"
999 " segment base references");
1001 if (wrt
== NO_SEG
) {
1002 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1004 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1005 wrt
== elf_got_sect
+ 1) {
1006 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1007 R_X86_64_GOTPCREL64
, true);
1009 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1010 wrt
== elf_got_sect
+ 1) {
1011 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1013 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1014 error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1017 error(ERR_NONFATAL
, "ELF64 format does not support this"
1021 elf_sect_writeaddr(s
, addr
, 8);
1025 static void elf_write(void)
1032 int32_t symtablen
, symtablocal
;
1035 * Work out how many sections we will have. We have SHN_UNDEF,
1036 * then the flexible user sections, then the fixed sections
1037 * `.shstrtab', `.symtab' and `.strtab', then optionally
1038 * relocation sections for the user sections.
1040 nsections
= sec_numspecial
+ 1;
1041 if (of_elf64
.current_dfmt
== &df_stabs
)
1043 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1046 add_sectname("", ".shstrtab");
1047 add_sectname("", ".symtab");
1048 add_sectname("", ".strtab");
1049 for (i
= 0; i
< nsects
; i
++) {
1050 nsections
++; /* for the section itself */
1051 if (sects
[i
]->head
) {
1052 nsections
++; /* for its relocations */
1053 add_sectname(".rela", sects
[i
]->name
);
1057 if (of_elf64
.current_dfmt
== &df_stabs
) {
1058 /* in case the debug information is wanted, just add these three sections... */
1059 add_sectname("", ".stab");
1060 add_sectname("", ".stabstr");
1061 add_sectname(".rel", ".stab");
1064 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1065 /* the dwarf debug standard specifies the following ten sections,
1066 not all of which are currently implemented,
1067 although all of them are defined. */
1068 #define debug_aranges (int64_t) (nsections-10)
1069 #define debug_info (int64_t) (nsections-7)
1070 #define debug_abbrev (int64_t) (nsections-5)
1071 #define debug_line (int64_t) (nsections-4)
1072 add_sectname("", ".debug_aranges");
1073 add_sectname(".rela", ".debug_aranges");
1074 add_sectname("", ".debug_pubnames");
1075 add_sectname("", ".debug_info");
1076 add_sectname(".rela", ".debug_info");
1077 add_sectname("", ".debug_abbrev");
1078 add_sectname("", ".debug_line");
1079 add_sectname(".rela", ".debug_line");
1080 add_sectname("", ".debug_frame");
1081 add_sectname("", ".debug_loc");
1085 * Output the ELF header.
1087 fwrite("\177ELF\2\1\1", 7, 1, elffp
);
1088 fputc(elf_osabi
, elffp
);
1089 fputc(elf_abiver
, elffp
);
1090 fwritezero(7, elffp
);
1091 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1092 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1093 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1094 fwriteint64_t(0L, elffp
); /* no entry point */
1095 fwriteint64_t(0L, elffp
); /* no program header table */
1096 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1097 * ELF header plus alignment */
1098 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1099 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1100 fwriteint16_t(0, elffp
); /* no program header table, again */
1101 fwriteint16_t(0, elffp
); /* still no program header table */
1102 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1103 fwriteint16_t(nsections
, elffp
); /* number of sections */
1104 fwriteint16_t(sec_shstrtab
, elffp
); /* string table section index for
1105 * section header table */
1108 * Build the symbol table and relocation tables.
1110 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1111 for (i
= 0; i
< nsects
; i
++)
1113 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1117 * Now output the section header table.
1120 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1121 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1124 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1127 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1130 /* The normal sections */
1131 for (i
= 0; i
< nsects
; i
++) {
1132 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1133 (sects
[i
]->type
== SHT_PROGBITS
?
1134 sects
[i
]->data
: NULL
), true,
1135 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1140 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1141 shstrtablen
, 0, 0, 1, 0);
1145 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1146 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1150 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, strs
, true,
1151 strslen
, 0, 0, 1, 0);
1154 /* The relocation sections */
1155 for (i
= 0; i
< nsects
; i
++)
1156 if (sects
[i
]->head
) {
1157 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1158 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1162 if (of_elf64
.current_dfmt
== &df_stabs
) {
1163 /* for debugging information, create the last three sections
1164 which are the .stab , .stabstr and .rel.stab sections respectively */
1166 /* this function call creates the stab sections in memory */
1169 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1170 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1171 stablen
, sec_stabstr
, 0, 4, 12);
1174 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1175 stabstrlen
, 0, 0, 4, 0);
1178 /* link -> symtable info -> section to refer to */
1179 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1180 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1184 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1185 /* for dwarf debugging information, create the ten dwarf sections */
1187 /* this function call creates the dwarf sections in memory */
1191 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1192 arangeslen
, 0, 0, 1, 0);
1195 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1196 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1199 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1200 pubnameslen
, 0, 0, 1, 0);
1203 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1204 infolen
, 0, 0, 1, 0);
1207 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1208 inforellen
, symtabsection
, debug_info
, 1, 24);
1211 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1212 abbrevlen
, 0, 0, 1, 0);
1215 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1216 linelen
, 0, 0, 1, 0);
1219 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1220 linerellen
, symtabsection
, debug_line
, 1, 24);
1223 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1224 framelen
, 0, 0, 8, 0);
1227 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1228 loclen
, 0, 0, 1, 0);
1231 fwritezero(align
, elffp
);
1234 * Now output the sections.
1236 elf_write_sections();
1238 nasm_free(elf_sects
);
1242 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1244 struct SAA
*s
= saa_init(1L);
1246 uint8_t entry
[24], *p
;
1252 * First, an all-zeros entry, required by the ELF spec.
1254 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1259 * Next, an entry for the file name.
1262 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1263 WRITESHORT(p
, STT_FILE
); /* type FILE */
1264 WRITESHORT(p
, SHN_ABS
);
1265 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1266 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1267 saa_wbytes(s
, entry
, 24L);
1272 * Now some standard symbols defining the segments, for relocation
1275 for (i
= 1; i
<= nsects
; i
++) {
1277 WRITELONG(p
, 0); /* no symbol name */
1278 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1279 WRITESHORT(p
, i
); /* section id */
1280 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1281 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1282 saa_wbytes(s
, entry
, 24L);
1289 * Now the other local symbols.
1292 while ((sym
= saa_rstruct(syms
))) {
1293 if (sym
->type
& SYM_GLOBAL
)
1296 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1297 WRITECHAR(p
, sym
->type
); /* type and binding */
1298 WRITECHAR(p
, sym
->other
); /* visibility */
1299 WRITESHORT(p
, sym
->section
); /* index into section header table */
1300 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1301 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1302 saa_wbytes(s
, entry
, 24L);
1307 * dwarf needs symbols for debug sections
1308 * which are relocation targets.
1310 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1311 dwarf_infosym
= *local
;
1313 WRITELONG(p
, 0); /* no symbol name */
1314 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1315 WRITESHORT(p
, debug_info
); /* section id */
1316 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1317 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1318 saa_wbytes(s
, entry
, 24L);
1321 dwarf_abbrevsym
= *local
;
1323 WRITELONG(p
, 0); /* no symbol name */
1324 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1325 WRITESHORT(p
, debug_abbrev
); /* section id */
1326 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1327 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1328 saa_wbytes(s
, entry
, 24L);
1331 dwarf_linesym
= *local
;
1333 WRITELONG(p
, 0); /* no symbol name */
1334 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1335 WRITESHORT(p
, debug_line
); /* section id */
1336 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1337 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1338 saa_wbytes(s
, entry
, 24L);
1344 * Now the global symbols.
1347 while ((sym
= saa_rstruct(syms
))) {
1348 if (!(sym
->type
& SYM_GLOBAL
))
1351 WRITELONG(p
, sym
->strpos
);
1352 WRITECHAR(p
, sym
->type
); /* type and binding */
1353 WRITECHAR(p
, sym
->other
); /* visibility */
1354 WRITESHORT(p
, sym
->section
);
1355 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1356 WRITEDLONG(p
, (int64_t)sym
->size
);
1357 saa_wbytes(s
, entry
, 24L);
1364 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1367 uint8_t *p
, entry
[24];
1368 int32_t global_offset
;
1377 * How to onvert from a global placeholder to a real symbol index;
1378 * the +2 refers to the two special entries, the null entry and
1379 * the filename entry.
1381 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1384 int32_t sym
= r
->symbol
;
1386 if (sym
>= GLOBAL_TEMP_BASE
)
1387 sym
+= global_offset
;
1390 WRITEDLONG(p
, r
->address
);
1391 WRITELONG(p
, r
->type
);
1393 WRITEDLONG(p
, r
->offset
);
1394 saa_wbytes(s
, entry
, 24L);
1403 static void elf_section_header(int name
, int type
, uint64_t flags
,
1404 void *data
, bool is_saa
, uint64_t datalen
,
1405 int link
, int info
, int align
, int eltsize
)
1407 elf_sects
[elf_nsect
].data
= data
;
1408 elf_sects
[elf_nsect
].len
= datalen
;
1409 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1412 fwriteint32_t((int32_t)name
, elffp
);
1413 fwriteint32_t((int32_t)type
, elffp
);
1414 fwriteint64_t((int64_t)flags
, elffp
);
1415 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1416 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1417 fwriteint64_t(datalen
, elffp
);
1419 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1420 fwriteint32_t((int32_t)link
, elffp
);
1421 fwriteint32_t((int32_t)info
, elffp
);
1422 fwriteint64_t((int64_t)align
, elffp
);
1423 fwriteint64_t((int64_t)eltsize
, elffp
);
1426 static void elf_write_sections(void)
1429 for (i
= 0; i
< elf_nsect
; i
++)
1430 if (elf_sects
[i
].data
) {
1431 int32_t len
= elf_sects
[i
].len
;
1432 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1433 int32_t align
= reallen
- len
;
1434 if (elf_sects
[i
].is_saa
)
1435 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1437 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1438 fwritezero(align
, elffp
);
1442 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1444 saa_wbytes(sect
->data
, data
, len
);
1447 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1449 saa_writeaddr(sect
->data
, data
, len
);
1453 static int32_t elf_segbase(int32_t segment
)
1458 static int elf_directive(char *directive
, char *value
, int pass
)
1464 if (!strcmp(directive
, "osabi")) {
1466 return 1; /* ignore in pass 2 */
1468 n
= readnum(value
, &err
);
1470 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1473 if (n
< 0 || n
> 255) {
1474 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1480 if ((p
= strchr(value
,',')) == NULL
)
1483 n
= readnum(p
+1, &err
);
1484 if (err
|| n
< 0 || n
> 255) {
1485 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1496 static void elf_filename(char *inname
, char *outname
, efunc error
)
1498 strcpy(elf_module
, inname
);
1499 standard_extension(inname
, outname
, ".o", error
);
1502 extern macros_t elf_stdmac
[];
1504 static int elf_set_info(enum geninfo type
, char **val
)
1510 static struct dfmt df_dwarf
= {
1511 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1521 static struct dfmt df_stabs
= {
1522 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1533 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1535 struct ofmt of_elf64
= {
1536 "ELF64 (x86_64) object files (e.g. Linux)",
1553 /* common debugging routines */
1554 static void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
,
1555 int is_global
, char *special
)
1564 static void debug64_directive(const char *directive
, const char *params
)
1570 static void debug64_typevalue(int32_t type
)
1572 int32_t stype
, ssize
;
1573 switch (TYM_TYPE(type
)) {
1612 stype
= STT_SECTION
;
1627 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1628 lastsym
->size
= ssize
;
1629 lastsym
->type
= stype
;
1633 /* stabs debugging routines */
1634 static void stabs64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1643 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1646 if (!stabs_filename
) {
1647 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1648 strcpy(stabs_filename
, filename
);
1650 if (strcmp(stabs_filename
, filename
)) {
1651 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1652 in fact, this leak comes in quite handy to maintain a list of files
1653 encountered so far in the symbol lines... */
1655 /* why not nasm_free(stabs_filename); we're done with the old one */
1657 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1658 strcpy(stabs_filename
, filename
);
1662 currentline
= linenumber
;
1666 static void stabs64_output(int type
, void *param
)
1668 struct symlininfo
*s
;
1669 struct linelist
*el
;
1670 if (type
== TY_DEBUGSYMLIN
) {
1671 if (debug_immcall
) {
1672 s
= (struct symlininfo
*)param
;
1673 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1674 return; /* line info is only collected for executable sections */
1676 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1677 el
->info
.offset
= s
->offset
;
1678 el
->info
.section
= s
->section
;
1679 el
->info
.name
= s
->name
;
1680 el
->line
= currentline
;
1681 el
->filename
= stabs_filename
;
1684 stabslines
->last
->next
= el
;
1685 stabslines
->last
= el
;
1688 stabslines
->last
= el
;
1695 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1697 WRITELONG(p,n_strx); \
1698 WRITECHAR(p,n_type); \
1699 WRITECHAR(p,n_other); \
1700 WRITESHORT(p,n_desc); \
1701 WRITELONG(p,n_value); \
1704 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1706 static void stabs64_generate(void)
1708 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1709 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1713 struct linelist
*ptr
;
1717 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1718 for (i
= 0; i
< numlinestabs
; i
++)
1722 if (numfiles
== 0) {
1723 allfiles
[0] = ptr
->filename
;
1726 for (i
= 0; i
< numfiles
; i
++) {
1727 if (!strcmp(allfiles
[i
], ptr
->filename
))
1730 if (i
>= numfiles
) {
1731 allfiles
[i
] = ptr
->filename
;
1738 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1739 for (i
= 0; i
< numfiles
; i
++) {
1740 fileidx
[i
] = strsize
;
1741 strsize
+= strlen(allfiles
[i
]) + 1;
1744 for (i
= 0; i
< numfiles
; i
++) {
1745 if (!strcmp(allfiles
[i
], elf_module
)) {
1751 /* worst case size of the stab buffer would be:
1752 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1755 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1756 sizeof(struct stabentry
));
1758 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1760 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1763 for (i
= 0; i
< numfiles
; i
++) {
1764 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1768 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1775 /* this is the first stab, its strx points to the filename of the
1776 the source-file, the n_desc field should be set to the number
1779 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1781 /* this is the stab for the main source file */
1782 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1784 /* relocation table entry */
1786 /* Since the symbol table has two entries before */
1787 /* the section symbols, the index in the info.section */
1788 /* member must be adjusted by adding 2 */
1790 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1791 WRITELONG(rptr
, R_X86_64_32
);
1792 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1795 currfile
= mainfileindex
;
1799 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1800 /* oops file has changed... */
1801 for (i
= 0; i
< numfiles
; i
++)
1802 if (!strcmp(allfiles
[i
], ptr
->filename
))
1805 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1809 /* relocation table entry */
1811 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1812 WRITELONG(rptr
, R_X86_64_32
);
1813 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1816 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1819 /* relocation table entry */
1821 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1822 WRITELONG(rptr
, R_X86_64_32
);
1823 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1829 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1831 nasm_free(allfiles
);
1834 stablen
= (sptr
- sbuf
);
1835 stabrellen
= (rptr
- rbuf
);
1841 static void stabs64_cleanup(void)
1843 struct linelist
*ptr
, *del
;
1855 nasm_free(stabrelbuf
);
1857 nasm_free(stabstrbuf
);
1859 /* dwarf routines */
1860 static void dwarf64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1867 ndebugs
= 3; /* 3 debug symbols */
1870 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1874 dwarf64_findfile(filename
);
1876 currentline
= linenumber
;
1879 /* called from elf_out with type == TY_DEBUGSYMLIN */
1880 static void dwarf64_output(int type
, void *param
)
1882 int ln
, aa
, inx
, maxln
, soc
;
1883 struct symlininfo
*s
;
1888 s
= (struct symlininfo
*)param
;
1889 /* line number info is only gathered for executable sections */
1890 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1892 /* Check if section index has changed */
1893 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1895 dwarf64_findsect(s
->section
);
1897 /* do nothing unless line or file has changed */
1900 ln
= currentline
- dwarf_csect
->line
;
1901 aa
= s
->offset
- dwarf_csect
->offset
;
1902 inx
= dwarf_clist
->line
;
1903 plinep
= dwarf_csect
->psaa
;
1904 /* check for file change */
1905 if (!(inx
== dwarf_csect
->file
))
1907 saa_write8(plinep
,DW_LNS_set_file
);
1908 saa_write8(plinep
,inx
);
1909 dwarf_csect
->file
= inx
;
1911 /* check for line change */
1914 /* test if in range of special op code */
1915 maxln
= line_base
+ line_range
;
1916 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1917 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1919 saa_write8(plinep
,soc
);
1925 saa_write8(plinep
,DW_LNS_advance_line
);
1926 saa_wleb128s(plinep
,ln
);
1930 saa_write8(plinep
,DW_LNS_advance_pc
);
1931 saa_wleb128u(plinep
,aa
);
1934 dwarf_csect
->line
= currentline
;
1935 dwarf_csect
->offset
= s
->offset
;
1937 /* show change handled */
1943 static void dwarf64_generate(void)
1947 struct linelist
*ftentry
;
1948 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1949 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1950 struct sectlist
*psect
;
1951 size_t saalen
, linepoff
, totlen
, highaddr
;
1953 /* write epilogues for each line program range */
1954 /* and build aranges section */
1955 paranges
= saa_init(1L);
1956 parangesrel
= saa_init(1L);
1957 saa_write16(paranges
,3); /* dwarf version */
1958 saa_write64(parangesrel
, paranges
->datalen
+4);
1959 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1960 saa_write64(parangesrel
, 0);
1961 saa_write32(paranges
,0); /* offset into info */
1962 saa_write8(paranges
,8); /* pointer size */
1963 saa_write8(paranges
,0); /* not segmented */
1964 saa_write32(paranges
,0); /* padding */
1965 /* iterate though sectlist entries */
1966 psect
= dwarf_fsect
;
1969 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1971 plinep
= psect
->psaa
;
1972 /* Line Number Program Epilogue */
1973 saa_write8(plinep
,2); /* std op 2 */
1974 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1975 saa_write8(plinep
,DW_LNS_extended_op
);
1976 saa_write8(plinep
,1); /* operand length */
1977 saa_write8(plinep
,DW_LNE_end_sequence
);
1978 totlen
+= plinep
->datalen
;
1979 /* range table relocation entry */
1980 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1981 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1982 saa_write64(parangesrel
, (uint64_t) 0);
1983 /* range table entry */
1984 saa_write64(paranges
,0x0000); /* range start */
1985 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1986 highaddr
+= sects
[psect
->section
]->len
;
1987 /* done with this entry */
1988 psect
= psect
->next
;
1990 saa_write64(paranges
,0); /* null address */
1991 saa_write64(paranges
,0); /* null length */
1992 saalen
= paranges
->datalen
;
1993 arangeslen
= saalen
+ 4;
1994 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1995 WRITELONG(pbuf
,saalen
); /* initial length */
1996 saa_rnbytes(paranges
, pbuf
, saalen
);
1999 /* build rela.aranges section */
2000 arangesrellen
= saalen
= parangesrel
->datalen
;
2001 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2002 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2003 saa_free(parangesrel
);
2005 /* build pubnames section */
2006 ppubnames
= saa_init(1L);
2007 saa_write16(ppubnames
,3); /* dwarf version */
2008 saa_write32(ppubnames
,0); /* offset into info */
2009 saa_write32(ppubnames
,0); /* space used in info */
2010 saa_write32(ppubnames
,0); /* end of list */
2011 saalen
= ppubnames
->datalen
;
2012 pubnameslen
= saalen
+ 4;
2013 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2014 WRITELONG(pbuf
,saalen
); /* initial length */
2015 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2016 saa_free(ppubnames
);
2018 /* build info section */
2019 pinfo
= saa_init(1L);
2020 pinforel
= saa_init(1L);
2021 saa_write16(pinfo
,3); /* dwarf version */
2022 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2023 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2024 saa_write64(pinforel
, 0);
2025 saa_write32(pinfo
,0); /* offset into abbrev */
2026 saa_write8(pinfo
,8); /* pointer size */
2027 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2028 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2029 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2030 saa_write64(pinforel
, 0);
2031 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2032 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2033 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2034 saa_write64(pinforel
, 0);
2035 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2036 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2037 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2038 saa_write64(pinforel
, 0);
2039 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2040 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2041 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2042 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2043 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2044 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2045 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2046 saa_write64(pinforel
, 0);
2047 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2048 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2049 saa_write8(pinfo
,0); /* end of entries */
2050 saalen
= pinfo
->datalen
;
2051 infolen
= saalen
+ 4;
2052 infobuf
= pbuf
= nasm_malloc(infolen
);
2053 WRITELONG(pbuf
,saalen
); /* initial length */
2054 saa_rnbytes(pinfo
, pbuf
, saalen
);
2057 /* build rela.info section */
2058 inforellen
= saalen
= pinforel
->datalen
;
2059 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2060 saa_rnbytes(pinforel
, pbuf
, saalen
);
2063 /* build abbrev section */
2064 pabbrev
= saa_init(1L);
2065 saa_write8(pabbrev
,1); /* entry number LEB128u */
2066 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2067 saa_write8(pabbrev
,1); /* has children */
2068 /* the following attributes and forms are all LEB128u values */
2069 saa_write8(pabbrev
,DW_AT_low_pc
);
2070 saa_write8(pabbrev
,DW_FORM_addr
);
2071 saa_write8(pabbrev
,DW_AT_high_pc
);
2072 saa_write8(pabbrev
,DW_FORM_addr
);
2073 saa_write8(pabbrev
,DW_AT_stmt_list
);
2074 saa_write8(pabbrev
,DW_FORM_data4
);
2075 saa_write8(pabbrev
,DW_AT_name
);
2076 saa_write8(pabbrev
,DW_FORM_string
);
2077 saa_write8(pabbrev
,DW_AT_producer
);
2078 saa_write8(pabbrev
,DW_FORM_string
);
2079 saa_write8(pabbrev
,DW_AT_language
);
2080 saa_write8(pabbrev
,DW_FORM_data2
);
2081 saa_write16(pabbrev
,0); /* end of entry */
2082 /* LEB128u usage same as above */
2083 saa_write8(pabbrev
,2); /* entry number */
2084 saa_write8(pabbrev
,DW_TAG_subprogram
);
2085 saa_write8(pabbrev
,0); /* no children */
2086 saa_write8(pabbrev
,DW_AT_low_pc
);
2087 saa_write8(pabbrev
,DW_FORM_addr
);
2088 saa_write8(pabbrev
,DW_AT_frame_base
);
2089 saa_write8(pabbrev
,DW_FORM_data4
);
2090 saa_write16(pabbrev
,0); /* end of entry */
2091 abbrevlen
= saalen
= pabbrev
->datalen
;
2092 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2093 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2096 /* build line section */
2098 plines
= saa_init(1L);
2099 saa_write8(plines
,1); /* Minimum Instruction Length */
2100 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2101 saa_write8(plines
,line_base
); /* Line Base */
2102 saa_write8(plines
,line_range
); /* Line Range */
2103 saa_write8(plines
,opcode_base
); /* Opcode Base */
2104 /* standard opcode lengths (# of LEB128u operands) */
2105 saa_write8(plines
,0); /* Std opcode 1 length */
2106 saa_write8(plines
,1); /* Std opcode 2 length */
2107 saa_write8(plines
,1); /* Std opcode 3 length */
2108 saa_write8(plines
,1); /* Std opcode 4 length */
2109 saa_write8(plines
,1); /* Std opcode 5 length */
2110 saa_write8(plines
,0); /* Std opcode 6 length */
2111 saa_write8(plines
,0); /* Std opcode 7 length */
2112 saa_write8(plines
,0); /* Std opcode 8 length */
2113 saa_write8(plines
,1); /* Std opcode 9 length */
2114 saa_write8(plines
,0); /* Std opcode 10 length */
2115 saa_write8(plines
,0); /* Std opcode 11 length */
2116 saa_write8(plines
,1); /* Std opcode 12 length */
2117 /* Directory Table */
2118 saa_write8(plines
,0); /* End of table */
2119 /* File Name Table */
2120 ftentry
= dwarf_flist
;
2121 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2123 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2124 saa_write8(plines
,0); /* directory LEB128u */
2125 saa_write8(plines
,0); /* time LEB128u */
2126 saa_write8(plines
,0); /* size LEB128u */
2127 ftentry
= ftentry
->next
;
2129 saa_write8(plines
,0); /* End of table */
2130 linepoff
= plines
->datalen
;
2131 linelen
= linepoff
+ totlen
+ 10;
2132 linebuf
= pbuf
= nasm_malloc(linelen
);
2133 WRITELONG(pbuf
,linelen
-4); /* initial length */
2134 WRITESHORT(pbuf
,3); /* dwarf version */
2135 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2136 /* write line header */
2138 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2141 /* concatonate line program ranges */
2143 plinesrel
= saa_init(1L);
2144 psect
= dwarf_fsect
;
2145 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2147 saa_write64(plinesrel
, linepoff
);
2148 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2149 saa_write64(plinesrel
, (uint64_t) 0);
2150 plinep
= psect
->psaa
;
2151 saalen
= plinep
->datalen
;
2152 saa_rnbytes(plinep
, pbuf
, saalen
);
2156 /* done with this entry */
2157 psect
= psect
->next
;
2161 /* build rela.lines section */
2162 linerellen
=saalen
= plinesrel
->datalen
;
2163 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2164 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2165 saa_free(plinesrel
);
2167 /* build frame section */
2169 framebuf
= pbuf
= nasm_malloc(framelen
);
2170 WRITELONG(pbuf
,framelen
-4); /* initial length */
2172 /* build loc section */
2174 locbuf
= pbuf
= nasm_malloc(loclen
);
2175 WRITEDLONG(pbuf
,0); /* null beginning offset */
2176 WRITEDLONG(pbuf
,0); /* null ending offset */
2179 static void dwarf64_cleanup(void)
2182 nasm_free(arangesbuf
);
2184 nasm_free(arangesrelbuf
);
2186 nasm_free(pubnamesbuf
);
2190 nasm_free(inforelbuf
);
2192 nasm_free(abbrevbuf
);
2196 nasm_free(linerelbuf
);
2198 nasm_free(framebuf
);
2202 static void dwarf64_findfile(const char * fname
)
2205 struct linelist
*match
;
2207 /* return if fname is current file name */
2208 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2209 /* search for match */
2215 match
= dwarf_flist
;
2216 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2218 if (!(strcmp(fname
, match
->filename
)))
2220 dwarf_clist
= match
;
2225 /* add file name to end of list */
2226 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2228 dwarf_clist
->line
= dwarf_numfiles
;
2229 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2230 strcpy(dwarf_clist
->filename
,fname
);
2231 dwarf_clist
->next
= 0;
2232 /* if first entry */
2235 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2236 dwarf_clist
->last
= 0;
2238 /* chain to previous entry */
2241 dwarf_elist
->next
= dwarf_clist
;
2242 dwarf_elist
= dwarf_clist
;
2247 static void dwarf64_findsect(const int index
)
2250 struct sectlist
*match
;
2252 /* return if index is current section index */
2253 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2257 /* search for match */
2263 match
= dwarf_fsect
;
2264 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2266 if ((match
->section
== index
))
2268 dwarf_csect
= match
;
2271 match
= match
->next
;
2274 /* add entry to end of list */
2275 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2277 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2278 dwarf_csect
->line
= 1;
2279 dwarf_csect
->offset
= 0;
2280 dwarf_csect
->file
= 1;
2281 dwarf_csect
->section
= index
;
2282 dwarf_csect
->next
= 0;
2283 /* set relocatable address at start of line program */
2284 saa_write8(plinep
,DW_LNS_extended_op
);
2285 saa_write8(plinep
,9); /* operand length */
2286 saa_write8(plinep
,DW_LNE_set_address
);
2287 saa_write64(plinep
,0); /* Start Address */
2288 /* if first entry */
2291 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2292 dwarf_csect
->last
= 0;
2294 /* chain to previous entry */
2297 dwarf_esect
->next
= dwarf_csect
;
2298 dwarf_esect
= dwarf_csect
;