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 void debug64_typevalue(int32_t);
192 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
193 void debug64_directive(const char *, const char *);
195 /* stabs debugging routines */
196 void stabs64_init(struct ofmt
*, void *, FILE *, efunc
);
197 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
198 void stabs64_output(int, void *);
199 void stabs64_generate(void);
200 void stabs64_cleanup(void);
202 /* dwarf debugging routines */
203 void dwarf64_init(struct ofmt
*, void *, FILE *, efunc
);
204 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
205 void dwarf64_output(int, void *);
206 void dwarf64_generate(void);
207 void dwarf64_cleanup(void);
208 void dwarf64_findfile(const char *);
209 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) error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
404 " declaration of section `%s'", q
, name
);
407 if (!strcmp(name
, ".shstrtab") ||
408 !strcmp(name
, ".symtab") ||
409 !strcmp(name
, ".strtab")) {
410 error(ERR_NONFATAL
, "attempt to redefine reserved section"
415 for (i
= 0; i
< nsects
; i
++)
416 if (!strcmp(name
, sects
[i
]->name
))
419 const struct elf_known_section
*ks
= elf_known_sections
;
422 if (!strcmp(name
, ks
->name
))
427 type
= type
? type
: ks
->type
;
428 align
= align
? align
: ks
->align
;
429 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
431 i
= elf_make_section(name
, type
, flags
, align
);
432 } else if (pass
== 1) {
433 if ((type
&& sects
[i
]->type
!= type
)
434 || (align
&& sects
[i
]->align
!= align
)
435 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
436 error(ERR_WARNING
, "incompatible section attributes ignored on"
437 " redeclaration of section `%s'", name
);
440 return sects
[i
]->index
;
443 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
444 int is_global
, char *special
)
448 bool special_used
= false;
450 #if defined(DEBUG) && DEBUG>2
452 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
453 name
, segment
, offset
, is_global
, special
);
455 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
457 * This is a NASM special symbol. We never allow it into
458 * the ELF symbol table, even if it's a valid one. If it
459 * _isn't_ a valid one, we should barf immediately.
461 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
462 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
463 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
464 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
468 if (is_global
== 3) {
471 * Fix up a forward-reference symbol size from the first
474 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
475 if (!strcmp((*s
)->name
, name
)) {
476 struct tokenval tokval
;
480 while (*p
&& !nasm_isspace(*p
))
482 while (*p
&& nasm_isspace(*p
))
486 tokval
.t_type
= TOKEN_INVALID
;
487 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
490 error(ERR_NONFATAL
, "cannot use relocatable"
491 " expression as symbol size");
493 (*s
)->size
= reloc_value(e
);
497 * Remove it from the list of unresolved sizes.
499 nasm_free((*s
)->name
);
503 return; /* it wasn't an important one */
506 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
507 strslen
+= 1 + strlen(name
);
509 lastsym
= sym
= saa_wstruct(syms
);
511 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
514 sym
->type
= is_global
? SYM_GLOBAL
: 0;
515 sym
->other
= STV_DEFAULT
;
517 if (segment
== NO_SEG
)
518 sym
->section
= SHN_ABS
;
521 sym
->section
= SHN_UNDEF
;
522 if (nsects
== 0 && segment
== def_seg
) {
524 if (segment
!= elf_section_names(".text", 2, &tempint
))
526 "strange segment conditions in ELF driver");
527 sym
->section
= nsects
;
529 for (i
= 0; i
< nsects
; i
++)
530 if (segment
== sects
[i
]->index
) {
531 sym
->section
= i
+ 1;
537 if (is_global
== 2) {
540 sym
->section
= SHN_COMMON
;
542 * We have a common variable. Check the special text to see
543 * if it's a valid number and power of two; if so, store it
544 * as the alignment for the common variable.
548 sym
->symv
.key
= readnum(special
, &err
);
550 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
551 " valid number", special
);
552 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
553 != 2 * sym
->symv
.key
- 1)
554 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
555 " power of two", special
);
559 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
561 if (sym
->type
== SYM_GLOBAL
) {
563 * If sym->section == SHN_ABS, then the first line of the
564 * else section would cause a core dump, because its a reference
565 * beyond the end of the section array.
566 * This behaviour is exhibited by this code:
569 * To avoid such a crash, such requests are silently discarded.
570 * This may not be the best solution.
572 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
573 bsym
= raa_write(bsym
, segment
, nglobs
);
574 } else if (sym
->section
!= SHN_ABS
) {
576 * This is a global symbol; so we must add it to the rbtree
577 * of global symbols in its section.
579 * In addition, we check the special text for symbol
580 * type and size information.
582 sects
[sym
->section
-1]->gsyms
=
583 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
586 int n
= strcspn(special
, " \t");
588 if (!nasm_strnicmp(special
, "function", n
))
589 sym
->type
|= STT_FUNC
;
590 else if (!nasm_strnicmp(special
, "data", n
) ||
591 !nasm_strnicmp(special
, "object", n
))
592 sym
->type
|= STT_OBJECT
;
593 else if (!nasm_strnicmp(special
, "notype", n
))
594 sym
->type
|= STT_NOTYPE
;
596 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
600 while (nasm_isspace(*special
))
603 n
= strcspn(special
, " \t");
604 if (!nasm_strnicmp(special
, "default", n
))
605 sym
->other
= STV_DEFAULT
;
606 else if (!nasm_strnicmp(special
, "internal", n
))
607 sym
->other
= STV_INTERNAL
;
608 else if (!nasm_strnicmp(special
, "hidden", n
))
609 sym
->other
= STV_HIDDEN
;
610 else if (!nasm_strnicmp(special
, "protected", n
))
611 sym
->other
= STV_PROTECTED
;
618 struct tokenval tokval
;
621 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
623 while (special
[n
] && nasm_isspace(special
[n
]))
626 * We have a size expression; attempt to
630 stdscan_bufptr
= special
+ n
;
631 tokval
.t_type
= TOKEN_INVALID
;
632 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
637 sym
->name
= nasm_strdup(name
);
640 error(ERR_NONFATAL
, "cannot use relocatable"
641 " expression as symbol size");
643 sym
->size
= reloc_value(e
);
645 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
650 * If TLS segment, mark symbol accordingly.
652 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
654 sym
->type
|= STT_TLS
;
657 sym
->globnum
= nglobs
;
662 if (special
&& !special_used
)
663 error(ERR_NONFATAL
, "no special symbol features supported here");
666 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
667 int64_t offset
, int type
)
670 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
671 sect
->tail
= &r
->next
;
674 r
->address
= sect
->len
;
676 if (segment
== NO_SEG
)
681 for (i
= 0; i
< nsects
; i
++)
682 if (segment
== sects
[i
]->index
)
685 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
693 * This routine deals with ..got and ..sym relocations: the more
694 * complicated kinds. In shared-library writing, some relocations
695 * with respect to global symbols must refer to the precise symbol
696 * rather than referring to an offset from the base of the section
697 * _containing_ the symbol. Such relocations call to this routine,
698 * which searches the symbol list for the symbol in question.
700 * R_386_GOT32 references require the _exact_ symbol address to be
701 * used; R_386_32 references can be at an offset from the symbol.
702 * The boolean argument `exact' tells us this.
704 * Return value is the adjusted value of `addr', having become an
705 * offset from the symbol rather than the section. Should always be
706 * zero when returning from an exact call.
708 * Limitation: if you define two symbols at the same place,
709 * confusion will occur.
711 * Inefficiency: we search, currently, using a linked list which
712 * isn't even necessarily sorted.
714 static void elf_add_gsym_reloc(struct Section
*sect
,
715 int32_t segment
, uint64_t offset
, int64_t pcrel
,
716 int type
, bool exact
)
725 * First look up the segment/offset pair and find a global
726 * symbol corresponding to it. If it's not one of our segments,
727 * then it must be an external symbol, in which case we're fine
728 * doing a normal elf_add_reloc after first sanity-checking
729 * that the offset from the symbol is zero.
732 for (i
= 0; i
< nsects
; i
++)
733 if (segment
== sects
[i
]->index
) {
740 error(ERR_NONFATAL
, "invalid access to an external symbol");
742 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
746 srb
= rb_search(s
->gsyms
, offset
);
747 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
748 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
749 " for this reference");
752 sym
= container_of(srb
, struct Symbol
, symv
);
754 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
755 sect
->tail
= &r
->next
;
758 r
->address
= sect
->len
;
759 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
760 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
766 static void elf_out(int32_t segto
, const void *data
,
767 enum out_type type
, uint64_t size
,
768 int32_t segment
, int32_t wrt
)
773 static struct symlininfo sinfo
;
777 #if defined(DEBUG) && DEBUG>2
778 if (data
) fprintf(stderr
,
779 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
780 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
782 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
783 currentline
, type
, segment
, segto
, size
);
787 * handle absolute-assembly (structure definitions)
789 if (segto
== NO_SEG
) {
790 if (type
!= OUT_RESERVE
)
791 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
797 for (i
= 0; i
< nsects
; i
++)
798 if (segto
== sects
[i
]->index
) {
803 int tempint
; /* ignored */
804 if (segto
!= elf_section_names(".text", 2, &tempint
))
805 error(ERR_PANIC
, "strange segment conditions in ELF driver");
807 s
= sects
[nsects
- 1];
811 /* invoke current debug_output routine */
812 if (of_elf64
.current_dfmt
) {
813 sinfo
.offset
= s
->len
;
816 sinfo
.name
= s
->name
;
817 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
819 /* end of debugging stuff */
821 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
822 error(ERR_WARNING
, "attempt to initialize memory in"
823 " BSS section `%s': ignored", s
->name
);
824 s
->len
+= realsize(type
, size
);
828 if (type
== OUT_RESERVE
) {
829 if (s
->type
== SHT_PROGBITS
) {
830 error(ERR_WARNING
, "uninitialized space declared in"
831 " non-BSS section `%s': zeroing", s
->name
);
832 elf_sect_write(s
, NULL
, size
);
835 } else if (type
== OUT_RAWDATA
) {
836 if (segment
!= NO_SEG
)
837 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
838 elf_sect_write(s
, data
, size
);
839 } else if (type
== OUT_ADDRESS
) {
840 addr
= *(int64_t *)data
;
841 if (segment
== NO_SEG
) {
843 } else if (segment
% 2) {
844 error(ERR_NONFATAL
, "ELF format does not support"
845 " segment base references");
850 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
853 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
856 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
859 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
862 error(ERR_PANIC
, "internal error elf64-hpa-871");
866 } else if (wrt
== elf_gotpc_sect
+ 1) {
868 * The user will supply GOT relative to $$. ELF
869 * will let us have GOT relative to $. So we
870 * need to fix up the data item by $-$$.
873 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
875 } else if (wrt
== elf_gotoff_sect
+ 1) {
877 error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
878 "references to be qword");
880 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
883 } else if (wrt
== elf_got_sect
+ 1) {
886 elf_add_gsym_reloc(s
, segment
, addr
, 0,
887 R_X86_64_GOT32
, true);
891 elf_add_gsym_reloc(s
, segment
, addr
, 0,
892 R_X86_64_GOT64
, true);
896 error(ERR_NONFATAL
, "invalid ..got reference");
899 } else if (wrt
== elf_sym_sect
+ 1) {
902 elf_add_gsym_reloc(s
, segment
, addr
, 0,
907 elf_add_gsym_reloc(s
, segment
, addr
, 0,
912 elf_add_gsym_reloc(s
, segment
, addr
, 0,
917 elf_add_gsym_reloc(s
, segment
, addr
, 0,
922 error(ERR_PANIC
, "internal error elf64-hpa-903");
925 } else if (wrt
== elf_plt_sect
+ 1) {
926 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
927 "relative PLT references");
929 error(ERR_NONFATAL
, "ELF format does not support this"
933 elf_sect_writeaddr(s
, addr
, size
);
934 } else if (type
== OUT_REL2ADR
) {
935 addr
= *(int64_t *)data
- size
;
936 if (segment
== segto
)
937 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
938 if (segment
== NO_SEG
) {
940 } else if (segment
% 2) {
941 error(ERR_NONFATAL
, "ELF format does not support"
942 " segment base references");
945 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
949 "Unsupported non-32-bit ELF relocation [2]");
952 elf_sect_writeaddr(s
, addr
, 2);
953 } else if (type
== OUT_REL4ADR
) {
954 addr
= *(int64_t *)data
- size
;
955 if (segment
== segto
)
956 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
957 if (segment
== NO_SEG
) {
959 } else if (segment
% 2) {
960 error(ERR_NONFATAL
, "ELF64 format does not support"
961 " segment base references");
964 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
966 } else if (wrt
== elf_plt_sect
+ 1) {
967 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
968 R_X86_64_PLT32
, true);
970 } else if (wrt
== elf_gotpc_sect
+ 1 ||
971 wrt
== elf_got_sect
+ 1) {
972 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
973 R_X86_64_GOTPCREL
, true);
975 } else if (wrt
== elf_gotoff_sect
+ 1 ||
976 wrt
== elf_got_sect
+ 1) {
977 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
979 } else if (wrt
== elf_gottpoff_sect
+ 1) {
980 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
981 R_X86_64_GOTTPOFF
, true);
984 error(ERR_NONFATAL
, "ELF64 format does not support this"
988 elf_sect_writeaddr(s
, addr
, 4);
989 } else if (type
== OUT_REL8ADR
) {
990 addr
= *(int64_t *)data
- size
;
991 if (segment
== segto
)
992 error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
993 if (segment
== NO_SEG
) {
995 } else if (segment
% 2) {
996 error(ERR_NONFATAL
, "ELF64 format does not support"
997 " segment base references");
1000 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1002 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1003 wrt
== elf_got_sect
+ 1) {
1004 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1005 R_X86_64_GOTPCREL64
, true);
1007 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1008 wrt
== elf_got_sect
+ 1) {
1009 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1011 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1012 error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1015 error(ERR_NONFATAL
, "ELF64 format does not support this"
1019 elf_sect_writeaddr(s
, addr
, 8);
1023 static void elf_write(void)
1030 int32_t symtablen
, symtablocal
;
1033 * Work out how many sections we will have. We have SHN_UNDEF,
1034 * then the flexible user sections, then the fixed sections
1035 * `.shstrtab', `.symtab' and `.strtab', then optionally
1036 * relocation sections for the user sections.
1038 nsections
= sec_numspecial
+ 1;
1039 if (of_elf64
.current_dfmt
== &df_stabs
)
1041 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1044 add_sectname("", ".shstrtab");
1045 add_sectname("", ".symtab");
1046 add_sectname("", ".strtab");
1047 for (i
= 0; i
< nsects
; i
++) {
1048 nsections
++; /* for the section itself */
1049 if (sects
[i
]->head
) {
1050 nsections
++; /* for its relocations */
1051 add_sectname(".rela", sects
[i
]->name
);
1055 if (of_elf64
.current_dfmt
== &df_stabs
) {
1056 /* in case the debug information is wanted, just add these three sections... */
1057 add_sectname("", ".stab");
1058 add_sectname("", ".stabstr");
1059 add_sectname(".rel", ".stab");
1062 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1063 /* the dwarf debug standard specifies the following ten sections,
1064 not all of which are currently implemented,
1065 although all of them are defined. */
1066 #define debug_aranges (int64_t) (nsections-10)
1067 #define debug_info (int64_t) (nsections-7)
1068 #define debug_abbrev (int64_t) (nsections-5)
1069 #define debug_line (int64_t) (nsections-4)
1070 add_sectname("", ".debug_aranges");
1071 add_sectname(".rela", ".debug_aranges");
1072 add_sectname("", ".debug_pubnames");
1073 add_sectname("", ".debug_info");
1074 add_sectname(".rela", ".debug_info");
1075 add_sectname("", ".debug_abbrev");
1076 add_sectname("", ".debug_line");
1077 add_sectname(".rela", ".debug_line");
1078 add_sectname("", ".debug_frame");
1079 add_sectname("", ".debug_loc");
1083 * Output the ELF header.
1085 fwrite("\177ELF\2\1\1", 7, 1, elffp
);
1086 fputc(elf_osabi
, elffp
);
1087 fputc(elf_abiver
, elffp
);
1088 fwritezero(7, elffp
);
1089 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1090 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1091 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1092 fwriteint64_t(0L, elffp
); /* no entry point */
1093 fwriteint64_t(0L, elffp
); /* no program header table */
1094 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1095 * ELF header plus alignment */
1096 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1097 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1098 fwriteint16_t(0, elffp
); /* no program header table, again */
1099 fwriteint16_t(0, elffp
); /* still no program header table */
1100 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1101 fwriteint16_t(nsections
, elffp
); /* number of sections */
1102 fwriteint16_t(sec_shstrtab
, elffp
); /* string table section index for
1103 * section header table */
1106 * Build the symbol table and relocation tables.
1108 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1109 for (i
= 0; i
< nsects
; i
++)
1111 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1115 * Now output the section header table.
1118 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1119 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1122 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1125 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1128 /* The normal sections */
1129 for (i
= 0; i
< nsects
; i
++) {
1130 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1131 (sects
[i
]->type
== SHT_PROGBITS
?
1132 sects
[i
]->data
: NULL
), true,
1133 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1138 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1139 shstrtablen
, 0, 0, 1, 0);
1143 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1144 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1148 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, strs
, true,
1149 strslen
, 0, 0, 1, 0);
1152 /* The relocation sections */
1153 for (i
= 0; i
< nsects
; i
++)
1154 if (sects
[i
]->head
) {
1155 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1156 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1160 if (of_elf64
.current_dfmt
== &df_stabs
) {
1161 /* for debugging information, create the last three sections
1162 which are the .stab , .stabstr and .rel.stab sections respectively */
1164 /* this function call creates the stab sections in memory */
1167 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1168 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1169 stablen
, sec_stabstr
, 0, 4, 12);
1172 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1173 stabstrlen
, 0, 0, 4, 0);
1176 /* link -> symtable info -> section to refer to */
1177 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1178 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1182 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1183 /* for dwarf debugging information, create the ten dwarf sections */
1185 /* this function call creates the dwarf sections in memory */
1189 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1190 arangeslen
, 0, 0, 1, 0);
1193 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1194 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1197 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1198 pubnameslen
, 0, 0, 1, 0);
1201 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1202 infolen
, 0, 0, 1, 0);
1205 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1206 inforellen
, symtabsection
, debug_info
, 1, 24);
1209 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1210 abbrevlen
, 0, 0, 1, 0);
1213 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1214 linelen
, 0, 0, 1, 0);
1217 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1218 linerellen
, symtabsection
, debug_line
, 1, 24);
1221 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1222 framelen
, 0, 0, 8, 0);
1225 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1226 loclen
, 0, 0, 1, 0);
1229 fwritezero(align
, elffp
);
1232 * Now output the sections.
1234 elf_write_sections();
1236 nasm_free(elf_sects
);
1240 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1242 struct SAA
*s
= saa_init(1L);
1244 uint8_t entry
[24], *p
;
1250 * First, an all-zeros entry, required by the ELF spec.
1252 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1257 * Next, an entry for the file name.
1260 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1261 WRITESHORT(p
, STT_FILE
); /* type FILE */
1262 WRITESHORT(p
, SHN_ABS
);
1263 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1264 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1265 saa_wbytes(s
, entry
, 24L);
1270 * Now some standard symbols defining the segments, for relocation
1273 for (i
= 1; i
<= nsects
; i
++) {
1275 WRITELONG(p
, 0); /* no symbol name */
1276 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1277 WRITESHORT(p
, i
); /* section id */
1278 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1279 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1280 saa_wbytes(s
, entry
, 24L);
1287 * Now the other local symbols.
1290 while ((sym
= saa_rstruct(syms
))) {
1291 if (sym
->type
& SYM_GLOBAL
)
1294 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1295 WRITECHAR(p
, sym
->type
); /* type and binding */
1296 WRITECHAR(p
, sym
->other
); /* visibility */
1297 WRITESHORT(p
, sym
->section
); /* index into section header table */
1298 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1299 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1300 saa_wbytes(s
, entry
, 24L);
1305 * dwarf needs symbols for debug sections
1306 * which are relocation targets.
1308 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1309 dwarf_infosym
= *local
;
1311 WRITELONG(p
, 0); /* no symbol name */
1312 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1313 WRITESHORT(p
, debug_info
); /* section id */
1314 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1315 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1316 saa_wbytes(s
, entry
, 24L);
1319 dwarf_abbrevsym
= *local
;
1321 WRITELONG(p
, 0); /* no symbol name */
1322 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1323 WRITESHORT(p
, debug_abbrev
); /* section id */
1324 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1325 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1326 saa_wbytes(s
, entry
, 24L);
1329 dwarf_linesym
= *local
;
1331 WRITELONG(p
, 0); /* no symbol name */
1332 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1333 WRITESHORT(p
, debug_line
); /* section id */
1334 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1335 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1336 saa_wbytes(s
, entry
, 24L);
1342 * Now the global symbols.
1345 while ((sym
= saa_rstruct(syms
))) {
1346 if (!(sym
->type
& SYM_GLOBAL
))
1349 WRITELONG(p
, sym
->strpos
);
1350 WRITECHAR(p
, sym
->type
); /* type and binding */
1351 WRITECHAR(p
, sym
->other
); /* visibility */
1352 WRITESHORT(p
, sym
->section
);
1353 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1354 WRITEDLONG(p
, (int64_t)sym
->size
);
1355 saa_wbytes(s
, entry
, 24L);
1362 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1365 uint8_t *p
, entry
[24];
1366 int32_t global_offset
;
1375 * How to onvert from a global placeholder to a real symbol index;
1376 * the +2 refers to the two special entries, the null entry and
1377 * the filename entry.
1379 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1382 int32_t sym
= r
->symbol
;
1384 if (sym
>= GLOBAL_TEMP_BASE
)
1385 sym
+= global_offset
;
1388 WRITEDLONG(p
, r
->address
);
1389 WRITELONG(p
, r
->type
);
1391 WRITEDLONG(p
, r
->offset
);
1392 saa_wbytes(s
, entry
, 24L);
1401 static void elf_section_header(int name
, int type
, uint64_t flags
,
1402 void *data
, bool is_saa
, uint64_t datalen
,
1403 int link
, int info
, int align
, int eltsize
)
1405 elf_sects
[elf_nsect
].data
= data
;
1406 elf_sects
[elf_nsect
].len
= datalen
;
1407 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1410 fwriteint32_t((int32_t)name
, elffp
);
1411 fwriteint32_t((int32_t)type
, elffp
);
1412 fwriteint64_t((int64_t)flags
, elffp
);
1413 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1414 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1415 fwriteint64_t(datalen
, elffp
);
1417 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1418 fwriteint32_t((int32_t)link
, elffp
);
1419 fwriteint32_t((int32_t)info
, elffp
);
1420 fwriteint64_t((int64_t)align
, elffp
);
1421 fwriteint64_t((int64_t)eltsize
, elffp
);
1424 static void elf_write_sections(void)
1427 for (i
= 0; i
< elf_nsect
; i
++)
1428 if (elf_sects
[i
].data
) {
1429 int32_t len
= elf_sects
[i
].len
;
1430 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1431 int32_t align
= reallen
- len
;
1432 if (elf_sects
[i
].is_saa
)
1433 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1435 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1436 fwritezero(align
, elffp
);
1440 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1442 saa_wbytes(sect
->data
, data
, len
);
1445 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1447 saa_writeaddr(sect
->data
, data
, len
);
1451 static int32_t elf_segbase(int32_t segment
)
1456 static int elf_directive(char *directive
, char *value
, int pass
)
1462 if (!strcmp(directive
, "osabi")) {
1464 return 1; /* ignore in pass 2 */
1466 n
= readnum(value
, &err
);
1468 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1471 if (n
< 0 || n
> 255) {
1472 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1478 if ((p
= strchr(value
,',')) == NULL
)
1481 n
= readnum(p
+1, &err
);
1482 if (err
|| n
< 0 || n
> 255) {
1483 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1494 static void elf_filename(char *inname
, char *outname
, efunc error
)
1496 strcpy(elf_module
, inname
);
1497 standard_extension(inname
, outname
, ".o", error
);
1500 extern macros_t elf_stdmac
[];
1502 static int elf_set_info(enum geninfo type
, char **val
)
1508 static struct dfmt df_dwarf
= {
1509 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1519 static struct dfmt df_stabs
= {
1520 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1531 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1533 struct ofmt of_elf64
= {
1534 "ELF64 (x86_64) object files (e.g. Linux)",
1551 /* common debugging routines */
1552 void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1562 void debug64_directive(const char *directive
, const char *params
)
1568 void debug64_typevalue(int32_t type
)
1570 int32_t stype
, ssize
;
1571 switch (TYM_TYPE(type
)) {
1610 stype
= STT_SECTION
;
1625 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1626 lastsym
->size
= ssize
;
1627 lastsym
->type
= stype
;
1631 /* stabs debugging routines */
1632 void stabs64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1641 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1644 if (!stabs_filename
) {
1645 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1646 strcpy(stabs_filename
, filename
);
1648 if (strcmp(stabs_filename
, filename
)) {
1649 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1650 in fact, this leak comes in quite handy to maintain a list of files
1651 encountered so far in the symbol lines... */
1653 /* why not nasm_free(stabs_filename); we're done with the old one */
1655 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1656 strcpy(stabs_filename
, filename
);
1660 currentline
= linenumber
;
1664 void stabs64_output(int type
, void *param
)
1666 struct symlininfo
*s
;
1667 struct linelist
*el
;
1668 if (type
== TY_DEBUGSYMLIN
) {
1669 if (debug_immcall
) {
1670 s
= (struct symlininfo
*)param
;
1671 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1672 return; /* line info is only collected for executable sections */
1674 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1675 el
->info
.offset
= s
->offset
;
1676 el
->info
.section
= s
->section
;
1677 el
->info
.name
= s
->name
;
1678 el
->line
= currentline
;
1679 el
->filename
= stabs_filename
;
1682 stabslines
->last
->next
= el
;
1683 stabslines
->last
= el
;
1686 stabslines
->last
= el
;
1693 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1695 WRITELONG(p,n_strx); \
1696 WRITECHAR(p,n_type); \
1697 WRITECHAR(p,n_other); \
1698 WRITESHORT(p,n_desc); \
1699 WRITELONG(p,n_value); \
1702 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1704 void stabs64_generate(void)
1706 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1707 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1711 struct linelist
*ptr
;
1715 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1716 for (i
= 0; i
< numlinestabs
; i
++)
1720 if (numfiles
== 0) {
1721 allfiles
[0] = ptr
->filename
;
1724 for (i
= 0; i
< numfiles
; i
++) {
1725 if (!strcmp(allfiles
[i
], ptr
->filename
))
1728 if (i
>= numfiles
) {
1729 allfiles
[i
] = ptr
->filename
;
1736 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1737 for (i
= 0; i
< numfiles
; i
++) {
1738 fileidx
[i
] = strsize
;
1739 strsize
+= strlen(allfiles
[i
]) + 1;
1742 for (i
= 0; i
< numfiles
; i
++) {
1743 if (!strcmp(allfiles
[i
], elf_module
)) {
1749 /* worst case size of the stab buffer would be:
1750 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1753 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1754 sizeof(struct stabentry
));
1756 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1758 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1761 for (i
= 0; i
< numfiles
; i
++) {
1762 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1766 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1773 /* this is the first stab, its strx points to the filename of the
1774 the source-file, the n_desc field should be set to the number
1777 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1779 /* this is the stab for the main source file */
1780 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1782 /* relocation table entry */
1784 /* Since the symbol table has two entries before */
1785 /* the section symbols, the index in the info.section */
1786 /* member must be adjusted by adding 2 */
1788 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1789 WRITELONG(rptr
, R_X86_64_32
);
1790 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1793 currfile
= mainfileindex
;
1797 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1798 /* oops file has changed... */
1799 for (i
= 0; i
< numfiles
; i
++)
1800 if (!strcmp(allfiles
[i
], ptr
->filename
))
1803 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1807 /* relocation table entry */
1809 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1810 WRITELONG(rptr
, R_X86_64_32
);
1811 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1814 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1817 /* relocation table entry */
1819 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1820 WRITELONG(rptr
, R_X86_64_32
);
1821 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1827 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1829 nasm_free(allfiles
);
1832 stablen
= (sptr
- sbuf
);
1833 stabrellen
= (rptr
- rbuf
);
1839 void stabs64_cleanup(void)
1841 struct linelist
*ptr
, *del
;
1853 nasm_free(stabrelbuf
);
1855 nasm_free(stabstrbuf
);
1857 /* dwarf routines */
1858 void dwarf64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1865 ndebugs
= 3; /* 3 debug symbols */
1868 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1871 dwarf64_findfile(filename
);
1873 currentline
= linenumber
;
1876 /* called from elf_out with type == TY_DEBUGSYMLIN */
1877 void dwarf64_output(int type
, void *param
)
1879 int ln
, aa
, inx
, maxln
, soc
;
1880 struct symlininfo
*s
;
1885 s
= (struct symlininfo
*)param
;
1886 /* line number info is only gathered for executable sections */
1887 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1889 /* Check if section index has changed */
1890 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1892 dwarf64_findsect(s
->section
);
1894 /* do nothing unless line or file has changed */
1897 ln
= currentline
- dwarf_csect
->line
;
1898 aa
= s
->offset
- dwarf_csect
->offset
;
1899 inx
= dwarf_clist
->line
;
1900 plinep
= dwarf_csect
->psaa
;
1901 /* check for file change */
1902 if (!(inx
== dwarf_csect
->file
))
1904 saa_write8(plinep
,DW_LNS_set_file
);
1905 saa_write8(plinep
,inx
);
1906 dwarf_csect
->file
= inx
;
1908 /* check for line change */
1911 /* test if in range of special op code */
1912 maxln
= line_base
+ line_range
;
1913 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1914 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1916 saa_write8(plinep
,soc
);
1922 saa_write8(plinep
,DW_LNS_advance_line
);
1923 saa_wleb128s(plinep
,ln
);
1927 saa_write8(plinep
,DW_LNS_advance_pc
);
1928 saa_wleb128u(plinep
,aa
);
1931 dwarf_csect
->line
= currentline
;
1932 dwarf_csect
->offset
= s
->offset
;
1934 /* show change handled */
1940 void dwarf64_generate(void)
1944 struct linelist
*ftentry
;
1945 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1946 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1947 struct sectlist
*psect
;
1948 size_t saalen
, linepoff
, totlen
, highaddr
;
1950 /* write epilogues for each line program range */
1951 /* and build aranges section */
1952 paranges
= saa_init(1L);
1953 parangesrel
= saa_init(1L);
1954 saa_write16(paranges
,3); /* dwarf version */
1955 saa_write64(parangesrel
, paranges
->datalen
+4);
1956 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1957 saa_write64(parangesrel
, 0);
1958 saa_write32(paranges
,0); /* offset into info */
1959 saa_write8(paranges
,8); /* pointer size */
1960 saa_write8(paranges
,0); /* not segmented */
1961 saa_write32(paranges
,0); /* padding */
1962 /* iterate though sectlist entries */
1963 psect
= dwarf_fsect
;
1966 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1968 plinep
= psect
->psaa
;
1969 /* Line Number Program Epilogue */
1970 saa_write8(plinep
,2); /* std op 2 */
1971 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1972 saa_write8(plinep
,DW_LNS_extended_op
);
1973 saa_write8(plinep
,1); /* operand length */
1974 saa_write8(plinep
,DW_LNE_end_sequence
);
1975 totlen
+= plinep
->datalen
;
1976 /* range table relocation entry */
1977 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1978 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1979 saa_write64(parangesrel
, (uint64_t) 0);
1980 /* range table entry */
1981 saa_write64(paranges
,0x0000); /* range start */
1982 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1983 highaddr
+= sects
[psect
->section
]->len
;
1984 /* done with this entry */
1985 psect
= psect
->next
;
1987 saa_write64(paranges
,0); /* null address */
1988 saa_write64(paranges
,0); /* null length */
1989 saalen
= paranges
->datalen
;
1990 arangeslen
= saalen
+ 4;
1991 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1992 WRITELONG(pbuf
,saalen
); /* initial length */
1993 saa_rnbytes(paranges
, pbuf
, saalen
);
1996 /* build rela.aranges section */
1997 arangesrellen
= saalen
= parangesrel
->datalen
;
1998 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1999 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2000 saa_free(parangesrel
);
2002 /* build pubnames section */
2003 ppubnames
= saa_init(1L);
2004 saa_write16(ppubnames
,3); /* dwarf version */
2005 saa_write32(ppubnames
,0); /* offset into info */
2006 saa_write32(ppubnames
,0); /* space used in info */
2007 saa_write32(ppubnames
,0); /* end of list */
2008 saalen
= ppubnames
->datalen
;
2009 pubnameslen
= saalen
+ 4;
2010 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2011 WRITELONG(pbuf
,saalen
); /* initial length */
2012 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2013 saa_free(ppubnames
);
2015 /* build info section */
2016 pinfo
= saa_init(1L);
2017 pinforel
= saa_init(1L);
2018 saa_write16(pinfo
,3); /* dwarf version */
2019 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2020 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2021 saa_write64(pinforel
, 0);
2022 saa_write32(pinfo
,0); /* offset into abbrev */
2023 saa_write8(pinfo
,8); /* pointer size */
2024 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2025 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2026 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2027 saa_write64(pinforel
, 0);
2028 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2029 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2030 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2031 saa_write64(pinforel
, 0);
2032 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2033 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2034 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2035 saa_write64(pinforel
, 0);
2036 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2037 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2038 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2039 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2040 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2041 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2042 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2043 saa_write64(pinforel
, 0);
2044 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2045 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2046 saa_write8(pinfo
,0); /* end of entries */
2047 saalen
= pinfo
->datalen
;
2048 infolen
= saalen
+ 4;
2049 infobuf
= pbuf
= nasm_malloc(infolen
);
2050 WRITELONG(pbuf
,saalen
); /* initial length */
2051 saa_rnbytes(pinfo
, pbuf
, saalen
);
2054 /* build rela.info section */
2055 inforellen
= saalen
= pinforel
->datalen
;
2056 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2057 saa_rnbytes(pinforel
, pbuf
, saalen
);
2060 /* build abbrev section */
2061 pabbrev
= saa_init(1L);
2062 saa_write8(pabbrev
,1); /* entry number LEB128u */
2063 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2064 saa_write8(pabbrev
,1); /* has children */
2065 /* the following attributes and forms are all LEB128u values */
2066 saa_write8(pabbrev
,DW_AT_low_pc
);
2067 saa_write8(pabbrev
,DW_FORM_addr
);
2068 saa_write8(pabbrev
,DW_AT_high_pc
);
2069 saa_write8(pabbrev
,DW_FORM_addr
);
2070 saa_write8(pabbrev
,DW_AT_stmt_list
);
2071 saa_write8(pabbrev
,DW_FORM_data4
);
2072 saa_write8(pabbrev
,DW_AT_name
);
2073 saa_write8(pabbrev
,DW_FORM_string
);
2074 saa_write8(pabbrev
,DW_AT_producer
);
2075 saa_write8(pabbrev
,DW_FORM_string
);
2076 saa_write8(pabbrev
,DW_AT_language
);
2077 saa_write8(pabbrev
,DW_FORM_data2
);
2078 saa_write16(pabbrev
,0); /* end of entry */
2079 /* LEB128u usage same as above */
2080 saa_write8(pabbrev
,2); /* entry number */
2081 saa_write8(pabbrev
,DW_TAG_subprogram
);
2082 saa_write8(pabbrev
,0); /* no children */
2083 saa_write8(pabbrev
,DW_AT_low_pc
);
2084 saa_write8(pabbrev
,DW_FORM_addr
);
2085 saa_write8(pabbrev
,DW_AT_frame_base
);
2086 saa_write8(pabbrev
,DW_FORM_data4
);
2087 saa_write16(pabbrev
,0); /* end of entry */
2088 abbrevlen
= saalen
= pabbrev
->datalen
;
2089 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2090 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2093 /* build line section */
2095 plines
= saa_init(1L);
2096 saa_write8(plines
,1); /* Minimum Instruction Length */
2097 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2098 saa_write8(plines
,line_base
); /* Line Base */
2099 saa_write8(plines
,line_range
); /* Line Range */
2100 saa_write8(plines
,opcode_base
); /* Opcode Base */
2101 /* standard opcode lengths (# of LEB128u operands) */
2102 saa_write8(plines
,0); /* Std opcode 1 length */
2103 saa_write8(plines
,1); /* Std opcode 2 length */
2104 saa_write8(plines
,1); /* Std opcode 3 length */
2105 saa_write8(plines
,1); /* Std opcode 4 length */
2106 saa_write8(plines
,1); /* Std opcode 5 length */
2107 saa_write8(plines
,0); /* Std opcode 6 length */
2108 saa_write8(plines
,0); /* Std opcode 7 length */
2109 saa_write8(plines
,0); /* Std opcode 8 length */
2110 saa_write8(plines
,1); /* Std opcode 9 length */
2111 saa_write8(plines
,0); /* Std opcode 10 length */
2112 saa_write8(plines
,0); /* Std opcode 11 length */
2113 saa_write8(plines
,1); /* Std opcode 12 length */
2114 /* Directory Table */
2115 saa_write8(plines
,0); /* End of table */
2116 /* File Name Table */
2117 ftentry
= dwarf_flist
;
2118 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2120 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2121 saa_write8(plines
,0); /* directory LEB128u */
2122 saa_write8(plines
,0); /* time LEB128u */
2123 saa_write8(plines
,0); /* size LEB128u */
2124 ftentry
= ftentry
->next
;
2126 saa_write8(plines
,0); /* End of table */
2127 linepoff
= plines
->datalen
;
2128 linelen
= linepoff
+ totlen
+ 10;
2129 linebuf
= pbuf
= nasm_malloc(linelen
);
2130 WRITELONG(pbuf
,linelen
-4); /* initial length */
2131 WRITESHORT(pbuf
,3); /* dwarf version */
2132 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2133 /* write line header */
2135 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2138 /* concatonate line program ranges */
2140 plinesrel
= saa_init(1L);
2141 psect
= dwarf_fsect
;
2142 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2144 saa_write64(plinesrel
, linepoff
);
2145 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2146 saa_write64(plinesrel
, (uint64_t) 0);
2147 plinep
= psect
->psaa
;
2148 saalen
= plinep
->datalen
;
2149 saa_rnbytes(plinep
, pbuf
, saalen
);
2153 /* done with this entry */
2154 psect
= psect
->next
;
2158 /* build rela.lines section */
2159 linerellen
=saalen
= plinesrel
->datalen
;
2160 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2161 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2162 saa_free(plinesrel
);
2164 /* build frame section */
2166 framebuf
= pbuf
= nasm_malloc(framelen
);
2167 WRITELONG(pbuf
,framelen
-4); /* initial length */
2169 /* build loc section */
2171 locbuf
= pbuf
= nasm_malloc(loclen
);
2172 WRITEDLONG(pbuf
,0); /* null beginning offset */
2173 WRITEDLONG(pbuf
,0); /* null ending offset */
2176 void dwarf64_cleanup(void)
2179 nasm_free(arangesbuf
);
2181 nasm_free(arangesrelbuf
);
2183 nasm_free(pubnamesbuf
);
2187 nasm_free(inforelbuf
);
2189 nasm_free(abbrevbuf
);
2193 nasm_free(linerelbuf
);
2195 nasm_free(framebuf
);
2199 void dwarf64_findfile(const char * fname
)
2202 struct linelist
*match
;
2204 /* return if fname is current file name */
2205 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2206 /* search for match */
2212 match
= dwarf_flist
;
2213 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2215 if (!(strcmp(fname
, match
->filename
)))
2217 dwarf_clist
= match
;
2222 /* add file name to end of list */
2223 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2225 dwarf_clist
->line
= dwarf_numfiles
;
2226 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2227 strcpy(dwarf_clist
->filename
,fname
);
2228 dwarf_clist
->next
= 0;
2229 /* if first entry */
2232 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2233 dwarf_clist
->last
= 0;
2235 /* chain to previous entry */
2238 dwarf_elist
->next
= dwarf_clist
;
2239 dwarf_elist
= dwarf_clist
;
2244 void dwarf64_findsect(const int index
)
2247 struct sectlist
*match
;
2249 /* return if index is current section index */
2250 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2254 /* search for match */
2260 match
= dwarf_fsect
;
2261 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2263 if ((match
->section
== index
))
2265 dwarf_csect
= match
;
2268 match
= match
->next
;
2271 /* add entry to end of list */
2272 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2274 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2275 dwarf_csect
->line
= 1;
2276 dwarf_csect
->offset
= 0;
2277 dwarf_csect
->file
= 1;
2278 dwarf_csect
->section
= index
;
2279 dwarf_csect
->next
= 0;
2280 /* set relocatable address at start of line program */
2281 saa_write8(plinep
,DW_LNS_extended_op
);
2282 saa_write8(plinep
,9); /* operand length */
2283 saa_write8(plinep
,DW_LNE_set_address
);
2284 saa_write64(plinep
,0); /* Start Address */
2285 /* if first entry */
2288 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2289 dwarf_csect
->last
= 0;
2291 /* chain to previous entry */
2294 dwarf_esect
->next
= dwarf_csect
;
2295 dwarf_esect
= dwarf_csect
;