1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelf64.c output routines for the Netwide Assembler to produce
36 * ELF64 (x86_64 of course) object file format
52 #include "output/outform.h"
53 #include "output/outlib.h"
56 #include "output/elf64.h"
57 #include "output/dwarf.h"
58 #include "output/outelf.h"
62 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
66 int64_t address
; /* relative to _start_ of section */
67 int64_t symbol
; /* symbol index */
68 int64_t offset
; /* symbol addend */
69 int type
; /* type of relocation */
73 struct rbtree symv
; /* symbol value and rbtree of globals */
74 int32_t strpos
; /* string table position of name */
75 int32_t section
; /* section ID of the symbol */
76 int type
; /* symbol type */
77 int other
; /* symbol visibility */
78 int32_t size
; /* size of symbol */
79 int32_t globnum
; /* symbol table offset if global */
80 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
81 char *name
; /* used temporarily if in above list */
88 int32_t index
; /* index into sects array */
89 int type
; /* SHT_PROGBITS or SHT_NOBITS */
90 uint64_t align
; /* alignment: power of two */
91 uint64_t flags
; /* section flags */
95 struct Reloc
*head
, **tail
;
96 struct rbtree
*gsyms
; /* global symbols in section */
100 static struct Section
**sects
;
101 static int nsects
, sectlen
;
103 #define SHSTR_DELTA 256
104 static char *shstrtab
;
105 static int shstrtablen
, shstrtabsize
;
107 static struct SAA
*syms
;
108 static uint32_t nlocals
, nglobs
, ndebugs
;
110 static int32_t def_seg
;
112 static struct RAA
*bsym
;
114 static struct SAA
*strs
;
115 static uint32_t strslen
;
119 static evalfunc evaluate
;
121 static struct Symbol
*fwds
;
123 static char elf_module
[FILENAME_MAX
];
125 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
126 static uint8_t elf_abiver
= 0; /* Current ABI version */
128 extern struct ofmt of_elf64
;
130 static struct ELF_SECTDATA
{
135 static int elf_nsect
, nsections
;
136 static int64_t elf_foffs
;
138 static void elf_write(void);
139 static void elf_sect_write(struct Section
*, const void *, size_t);
140 static void elf_sect_writeaddr(struct Section
*, int64_t, size_t);
141 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
143 static void elf_write_sections(void);
144 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
145 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
146 static void add_sectname(char *, char *);
148 /* type values for stabs debugging sections */
149 #define N_SO 0x64 /* ID for main source file */
150 #define N_SOL 0x84 /* ID for sub-source file */
151 #define N_BINCL 0x82 /* not currently used */
152 #define N_EINCL 0xA2 /* not currently used */
169 int section
; /* index into sects[] */
170 int segto
; /* internal section number */
171 char *name
; /* shallow-copied pointer of section name */
175 struct symlininfo info
;
178 struct linelist
*next
;
179 struct linelist
*last
;
188 struct sectlist
*next
;
189 struct sectlist
*last
;
192 /* common debug variables */
193 static int currentline
= 1;
194 static int debug_immcall
= 0;
196 /* stabs debug variables */
197 static struct linelist
*stabslines
= 0;
198 static int numlinestabs
= 0;
199 static char *stabs_filename
= 0;
200 static int symtabsection
;
201 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
202 static int stablen
, stabstrlen
, stabrellen
;
204 /* dwarf debug variables */
205 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
206 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
207 static int dwarf_numfiles
= 0, dwarf_nsections
;
208 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
209 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
210 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
211 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
212 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
213 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
216 static struct dfmt df_dwarf
;
217 static struct dfmt df_stabs
;
218 static struct Symbol
*lastsym
;
220 /* common debugging routines */
221 static void debug64_typevalue(int32_t);
222 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
223 static void debug64_directive(const char *, const char *);
225 /* stabs debugging routines */
226 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
227 static void stabs64_output(int, void *);
228 static void stabs64_generate(void);
229 static void stabs64_cleanup(void);
231 /* dwarf debugging routines */
232 static void dwarf64_init(struct ofmt
*, void *, FILE *, efunc
);
233 static void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
234 static void dwarf64_output(int, void *);
235 static void dwarf64_generate(void);
236 static void dwarf64_cleanup(void);
237 static void dwarf64_findfile(const char *);
238 static void dwarf64_findsect(const int);
241 * Special section numbers which are used to define ELF special
242 * symbols, which can be used with WRT to provide PIC relocation
245 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
246 static int32_t elf_got_sect
, elf_plt_sect
;
247 static int32_t elf_sym_sect
;
248 static int32_t elf_gottpoff_sect
;
250 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
256 (void)ldef
; /* placate optimisers */
258 nsects
= sectlen
= 0;
259 syms
= saa_init((int32_t)sizeof(struct Symbol
));
260 nlocals
= nglobs
= ndebugs
= 0;
263 saa_wbytes(strs
, "\0", 1L);
264 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
265 strslen
= 2 + strlen(elf_module
);
267 shstrtablen
= shstrtabsize
= 0;;
268 add_sectname("", "");
272 elf_gotpc_sect
= seg_alloc();
273 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
275 elf_gotoff_sect
= seg_alloc();
276 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
278 elf_got_sect
= seg_alloc();
279 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
281 elf_plt_sect
= seg_alloc();
282 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
284 elf_sym_sect
= seg_alloc();
285 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
287 elf_gottpoff_sect
= seg_alloc();
288 ldef("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
291 def_seg
= seg_alloc();
295 static void elf_cleanup(int debuginfo
)
304 for (i
= 0; i
< nsects
; i
++) {
305 if (sects
[i
]->type
!= SHT_NOBITS
)
306 saa_free(sects
[i
]->data
);
308 saa_free(sects
[i
]->rel
);
309 while (sects
[i
]->head
) {
311 sects
[i
]->head
= sects
[i
]->head
->next
;
319 if (of_elf64
.current_dfmt
) {
320 of_elf64
.current_dfmt
->cleanup();
323 /* add entry to the elf .shstrtab section */
324 static void add_sectname(char *firsthalf
, char *secondhalf
)
326 int len
= strlen(firsthalf
) + strlen(secondhalf
);
327 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
328 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
329 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
330 strcat(shstrtab
+ shstrtablen
, secondhalf
);
331 shstrtablen
+= len
+ 1;
334 static int elf_make_section(char *name
, int type
, int flags
, int align
)
338 s
= nasm_malloc(sizeof(*s
));
340 if (type
!= SHT_NOBITS
)
341 s
->data
= saa_init(1L);
344 s
->len
= s
->size
= 0;
346 if (!strcmp(name
, ".text"))
349 s
->index
= seg_alloc();
350 add_sectname("", name
);
351 s
->name
= nasm_malloc(1 + strlen(name
));
352 strcpy(s
->name
, name
);
358 if (nsects
>= sectlen
)
359 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
365 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
368 uint32_t flags
, flags_and
, flags_or
;
373 * Default is 64 bits.
381 while (*p
&& !nasm_isspace(*p
))
385 flags_and
= flags_or
= type
= align
= 0;
387 while (*p
&& nasm_isspace(*p
))
391 while (*p
&& !nasm_isspace(*p
))
395 while (*p
&& nasm_isspace(*p
))
398 if (!nasm_strnicmp(q
, "align=", 6)) {
402 if ((align
- 1) & align
) { /* means it's not a power of two */
403 error(ERR_NONFATAL
, "section alignment %d is not"
404 " a power of two", align
);
407 } else if (!nasm_stricmp(q
, "alloc")) {
408 flags_and
|= SHF_ALLOC
;
409 flags_or
|= SHF_ALLOC
;
410 } else if (!nasm_stricmp(q
, "noalloc")) {
411 flags_and
|= SHF_ALLOC
;
412 flags_or
&= ~SHF_ALLOC
;
413 } else if (!nasm_stricmp(q
, "exec")) {
414 flags_and
|= SHF_EXECINSTR
;
415 flags_or
|= SHF_EXECINSTR
;
416 } else if (!nasm_stricmp(q
, "noexec")) {
417 flags_and
|= SHF_EXECINSTR
;
418 flags_or
&= ~SHF_EXECINSTR
;
419 } else if (!nasm_stricmp(q
, "write")) {
420 flags_and
|= SHF_WRITE
;
421 flags_or
|= SHF_WRITE
;
422 } else if (!nasm_stricmp(q
, "tls")) {
423 flags_and
|= SHF_TLS
;
425 } else if (!nasm_stricmp(q
, "nowrite")) {
426 flags_and
|= SHF_WRITE
;
427 flags_or
&= ~SHF_WRITE
;
428 } else if (!nasm_stricmp(q
, "progbits")) {
430 } else if (!nasm_stricmp(q
, "nobits")) {
432 } else if (pass
== 1) {
433 error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
434 " declaration of section `%s'", q
, name
);
438 if (!strcmp(name
, ".shstrtab") ||
439 !strcmp(name
, ".symtab") ||
440 !strcmp(name
, ".strtab")) {
441 error(ERR_NONFATAL
, "attempt to redefine reserved section"
446 for (i
= 0; i
< nsects
; i
++)
447 if (!strcmp(name
, sects
[i
]->name
))
450 const struct elf_known_section
*ks
= elf_known_sections
;
453 if (!strcmp(name
, ks
->name
))
458 type
= type
? type
: ks
->type
;
459 align
= align
? align
: ks
->align
;
460 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
462 i
= elf_make_section(name
, type
, flags
, align
);
463 } else if (pass
== 1) {
464 if ((type
&& sects
[i
]->type
!= type
)
465 || (align
&& sects
[i
]->align
!= align
)
466 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
467 error(ERR_WARNING
, "incompatible section attributes ignored on"
468 " redeclaration of section `%s'", name
);
471 return sects
[i
]->index
;
474 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
475 int is_global
, char *special
)
479 bool special_used
= false;
481 #if defined(DEBUG) && DEBUG>2
483 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
484 name
, segment
, offset
, is_global
, special
);
486 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
488 * This is a NASM special symbol. We never allow it into
489 * the ELF symbol table, even if it's a valid one. If it
490 * _isn't_ a valid one, we should barf immediately.
492 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
493 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
494 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
495 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
499 if (is_global
== 3) {
502 * Fix up a forward-reference symbol size from the first
505 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
506 if (!strcmp((*s
)->name
, name
)) {
507 struct tokenval tokval
;
511 while (*p
&& !nasm_isspace(*p
))
513 while (*p
&& nasm_isspace(*p
))
517 tokval
.t_type
= TOKEN_INVALID
;
518 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
521 error(ERR_NONFATAL
, "cannot use relocatable"
522 " expression as symbol size");
524 (*s
)->size
= reloc_value(e
);
528 * Remove it from the list of unresolved sizes.
530 nasm_free((*s
)->name
);
534 return; /* it wasn't an important one */
537 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
538 strslen
+= 1 + strlen(name
);
540 lastsym
= sym
= saa_wstruct(syms
);
542 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
545 sym
->type
= is_global
? SYM_GLOBAL
: 0;
546 sym
->other
= STV_DEFAULT
;
548 if (segment
== NO_SEG
)
549 sym
->section
= SHN_ABS
;
552 sym
->section
= SHN_UNDEF
;
553 if (nsects
== 0 && segment
== def_seg
) {
555 if (segment
!= elf_section_names(".text", 2, &tempint
))
557 "strange segment conditions in ELF driver");
558 sym
->section
= nsects
;
560 for (i
= 0; i
< nsects
; i
++)
561 if (segment
== sects
[i
]->index
) {
562 sym
->section
= i
+ 1;
568 if (is_global
== 2) {
571 sym
->section
= SHN_COMMON
;
573 * We have a common variable. Check the special text to see
574 * if it's a valid number and power of two; if so, store it
575 * as the alignment for the common variable.
579 sym
->symv
.key
= readnum(special
, &err
);
581 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
582 " valid number", special
);
583 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
584 != 2 * sym
->symv
.key
- 1)
585 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
586 " power of two", special
);
590 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
592 if (sym
->type
== SYM_GLOBAL
) {
594 * If sym->section == SHN_ABS, then the first line of the
595 * else section would cause a core dump, because its a reference
596 * beyond the end of the section array.
597 * This behaviour is exhibited by this code:
600 * To avoid such a crash, such requests are silently discarded.
601 * This may not be the best solution.
603 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
604 bsym
= raa_write(bsym
, segment
, nglobs
);
605 } else if (sym
->section
!= SHN_ABS
) {
607 * This is a global symbol; so we must add it to the rbtree
608 * of global symbols in its section.
610 * In addition, we check the special text for symbol
611 * type and size information.
613 sects
[sym
->section
-1]->gsyms
=
614 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
617 int n
= strcspn(special
, " \t");
619 if (!nasm_strnicmp(special
, "function", n
))
620 sym
->type
|= STT_FUNC
;
621 else if (!nasm_strnicmp(special
, "data", n
) ||
622 !nasm_strnicmp(special
, "object", n
))
623 sym
->type
|= STT_OBJECT
;
624 else if (!nasm_strnicmp(special
, "notype", n
))
625 sym
->type
|= STT_NOTYPE
;
627 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
631 while (nasm_isspace(*special
))
634 n
= strcspn(special
, " \t");
635 if (!nasm_strnicmp(special
, "default", n
))
636 sym
->other
= STV_DEFAULT
;
637 else if (!nasm_strnicmp(special
, "internal", n
))
638 sym
->other
= STV_INTERNAL
;
639 else if (!nasm_strnicmp(special
, "hidden", n
))
640 sym
->other
= STV_HIDDEN
;
641 else if (!nasm_strnicmp(special
, "protected", n
))
642 sym
->other
= STV_PROTECTED
;
649 struct tokenval tokval
;
652 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
654 while (special
[n
] && nasm_isspace(special
[n
]))
657 * We have a size expression; attempt to
661 stdscan_bufptr
= special
+ n
;
662 tokval
.t_type
= TOKEN_INVALID
;
663 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
668 sym
->name
= nasm_strdup(name
);
671 error(ERR_NONFATAL
, "cannot use relocatable"
672 " expression as symbol size");
674 sym
->size
= reloc_value(e
);
676 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
681 * If TLS segment, mark symbol accordingly.
683 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
685 sym
->type
|= STT_TLS
;
688 sym
->globnum
= nglobs
;
693 if (special
&& !special_used
)
694 error(ERR_NONFATAL
, "no special symbol features supported here");
697 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
698 int64_t offset
, int type
)
701 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
702 sect
->tail
= &r
->next
;
705 r
->address
= sect
->len
;
707 if (segment
== NO_SEG
)
712 for (i
= 0; i
< nsects
; i
++)
713 if (segment
== sects
[i
]->index
)
716 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
724 * This routine deals with ..got and ..sym relocations: the more
725 * complicated kinds. In shared-library writing, some relocations
726 * with respect to global symbols must refer to the precise symbol
727 * rather than referring to an offset from the base of the section
728 * _containing_ the symbol. Such relocations call to this routine,
729 * which searches the symbol list for the symbol in question.
731 * R_386_GOT32 references require the _exact_ symbol address to be
732 * used; R_386_32 references can be at an offset from the symbol.
733 * The boolean argument `exact' tells us this.
735 * Return value is the adjusted value of `addr', having become an
736 * offset from the symbol rather than the section. Should always be
737 * zero when returning from an exact call.
739 * Limitation: if you define two symbols at the same place,
740 * confusion will occur.
742 * Inefficiency: we search, currently, using a linked list which
743 * isn't even necessarily sorted.
745 static void elf_add_gsym_reloc(struct Section
*sect
,
746 int32_t segment
, uint64_t offset
, int64_t pcrel
,
747 int type
, bool exact
)
756 * First look up the segment/offset pair and find a global
757 * symbol corresponding to it. If it's not one of our segments,
758 * then it must be an external symbol, in which case we're fine
759 * doing a normal elf_add_reloc after first sanity-checking
760 * that the offset from the symbol is zero.
763 for (i
= 0; i
< nsects
; i
++)
764 if (segment
== sects
[i
]->index
) {
771 error(ERR_NONFATAL
, "invalid access to an external symbol");
773 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
777 srb
= rb_search(s
->gsyms
, offset
);
778 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
779 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
780 " for this reference");
783 sym
= container_of(srb
, struct Symbol
, symv
);
785 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
786 sect
->tail
= &r
->next
;
789 r
->address
= sect
->len
;
790 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
791 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
797 static void elf_out(int32_t segto
, const void *data
,
798 enum out_type type
, uint64_t size
,
799 int32_t segment
, int32_t wrt
)
804 static struct symlininfo sinfo
;
808 #if defined(DEBUG) && DEBUG>2
809 if (data
) fprintf(stderr
,
810 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
811 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
813 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
814 currentline
, type
, segment
, segto
, size
);
818 * handle absolute-assembly (structure definitions)
820 if (segto
== NO_SEG
) {
821 if (type
!= OUT_RESERVE
)
822 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
828 for (i
= 0; i
< nsects
; i
++)
829 if (segto
== sects
[i
]->index
) {
834 int tempint
; /* ignored */
835 if (segto
!= elf_section_names(".text", 2, &tempint
))
836 error(ERR_PANIC
, "strange segment conditions in ELF driver");
838 s
= sects
[nsects
- 1];
842 /* invoke current debug_output routine */
843 if (of_elf64
.current_dfmt
) {
844 sinfo
.offset
= s
->len
;
847 sinfo
.name
= s
->name
;
848 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
850 /* end of debugging stuff */
852 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
853 error(ERR_WARNING
, "attempt to initialize memory in"
854 " BSS section `%s': ignored", s
->name
);
855 s
->len
+= realsize(type
, size
);
859 if (type
== OUT_RESERVE
) {
860 if (s
->type
== SHT_PROGBITS
) {
861 error(ERR_WARNING
, "uninitialized space declared in"
862 " non-BSS section `%s': zeroing", s
->name
);
863 elf_sect_write(s
, NULL
, size
);
866 } else if (type
== OUT_RAWDATA
) {
867 if (segment
!= NO_SEG
)
868 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
869 elf_sect_write(s
, data
, size
);
870 } else if (type
== OUT_ADDRESS
) {
871 addr
= *(int64_t *)data
;
872 if (segment
== NO_SEG
) {
874 } else if (segment
% 2) {
875 error(ERR_NONFATAL
, "ELF format does not support"
876 " segment base references");
881 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
884 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
887 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
890 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
893 error(ERR_PANIC
, "internal error elf64-hpa-871");
897 } else if (wrt
== elf_gotpc_sect
+ 1) {
899 * The user will supply GOT relative to $$. ELF
900 * will let us have GOT relative to $. So we
901 * need to fix up the data item by $-$$.
904 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
906 } else if (wrt
== elf_gotoff_sect
+ 1) {
908 error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
909 "references to be qword");
911 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
914 } else if (wrt
== elf_got_sect
+ 1) {
917 elf_add_gsym_reloc(s
, segment
, addr
, 0,
918 R_X86_64_GOT32
, true);
922 elf_add_gsym_reloc(s
, segment
, addr
, 0,
923 R_X86_64_GOT64
, true);
927 error(ERR_NONFATAL
, "invalid ..got reference");
930 } else if (wrt
== elf_sym_sect
+ 1) {
933 elf_add_gsym_reloc(s
, segment
, addr
, 0,
938 elf_add_gsym_reloc(s
, segment
, addr
, 0,
943 elf_add_gsym_reloc(s
, segment
, addr
, 0,
948 elf_add_gsym_reloc(s
, segment
, addr
, 0,
953 error(ERR_PANIC
, "internal error elf64-hpa-903");
956 } else if (wrt
== elf_plt_sect
+ 1) {
957 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
958 "relative PLT references");
960 error(ERR_NONFATAL
, "ELF format does not support this"
964 elf_sect_writeaddr(s
, addr
, size
);
965 } else if (type
== OUT_REL2ADR
) {
966 addr
= *(int64_t *)data
- size
;
967 if (segment
== segto
)
968 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
969 if (segment
== NO_SEG
) {
971 } else if (segment
% 2) {
972 error(ERR_NONFATAL
, "ELF format does not support"
973 " segment base references");
976 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
980 "Unsupported non-32-bit ELF relocation [2]");
983 elf_sect_writeaddr(s
, addr
, 2);
984 } else if (type
== OUT_REL4ADR
) {
985 addr
= *(int64_t *)data
- size
;
986 if (segment
== segto
)
987 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
988 if (segment
== NO_SEG
) {
990 } else if (segment
% 2) {
991 error(ERR_NONFATAL
, "ELF64 format does not support"
992 " segment base references");
995 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
997 } else if (wrt
== elf_plt_sect
+ 1) {
998 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
999 R_X86_64_PLT32
, true);
1001 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1002 wrt
== elf_got_sect
+ 1) {
1003 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1004 R_X86_64_GOTPCREL
, true);
1006 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1007 wrt
== elf_got_sect
+ 1) {
1008 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1010 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1011 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1012 R_X86_64_GOTTPOFF
, true);
1015 error(ERR_NONFATAL
, "ELF64 format does not support this"
1019 elf_sect_writeaddr(s
, addr
, 4);
1020 } else if (type
== OUT_REL8ADR
) {
1021 addr
= *(int64_t *)data
- size
;
1022 if (segment
== segto
)
1023 error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
1024 if (segment
== NO_SEG
) {
1026 } else if (segment
% 2) {
1027 error(ERR_NONFATAL
, "ELF64 format does not support"
1028 " segment base references");
1030 if (wrt
== NO_SEG
) {
1031 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1033 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1034 wrt
== elf_got_sect
+ 1) {
1035 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1036 R_X86_64_GOTPCREL64
, true);
1038 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1039 wrt
== elf_got_sect
+ 1) {
1040 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1042 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1043 error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1046 error(ERR_NONFATAL
, "ELF64 format does not support this"
1050 elf_sect_writeaddr(s
, addr
, 8);
1054 static void elf_write(void)
1061 int32_t symtablen
, symtablocal
;
1064 * Work out how many sections we will have. We have SHN_UNDEF,
1065 * then the flexible user sections, then the fixed sections
1066 * `.shstrtab', `.symtab' and `.strtab', then optionally
1067 * relocation sections for the user sections.
1069 nsections
= sec_numspecial
+ 1;
1070 if (of_elf64
.current_dfmt
== &df_stabs
)
1072 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1075 add_sectname("", ".shstrtab");
1076 add_sectname("", ".symtab");
1077 add_sectname("", ".strtab");
1078 for (i
= 0; i
< nsects
; i
++) {
1079 nsections
++; /* for the section itself */
1080 if (sects
[i
]->head
) {
1081 nsections
++; /* for its relocations */
1082 add_sectname(".rela", sects
[i
]->name
);
1086 if (of_elf64
.current_dfmt
== &df_stabs
) {
1087 /* in case the debug information is wanted, just add these three sections... */
1088 add_sectname("", ".stab");
1089 add_sectname("", ".stabstr");
1090 add_sectname(".rel", ".stab");
1093 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1094 /* the dwarf debug standard specifies the following ten sections,
1095 not all of which are currently implemented,
1096 although all of them are defined. */
1097 #define debug_aranges (int64_t) (nsections-10)
1098 #define debug_info (int64_t) (nsections-7)
1099 #define debug_abbrev (int64_t) (nsections-5)
1100 #define debug_line (int64_t) (nsections-4)
1101 add_sectname("", ".debug_aranges");
1102 add_sectname(".rela", ".debug_aranges");
1103 add_sectname("", ".debug_pubnames");
1104 add_sectname("", ".debug_info");
1105 add_sectname(".rela", ".debug_info");
1106 add_sectname("", ".debug_abbrev");
1107 add_sectname("", ".debug_line");
1108 add_sectname(".rela", ".debug_line");
1109 add_sectname("", ".debug_frame");
1110 add_sectname("", ".debug_loc");
1114 * Output the ELF header.
1116 fwrite("\177ELF\2\1\1", 7, 1, elffp
);
1117 fputc(elf_osabi
, elffp
);
1118 fputc(elf_abiver
, elffp
);
1119 fwritezero(7, elffp
);
1120 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1121 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1122 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1123 fwriteint64_t(0L, elffp
); /* no entry point */
1124 fwriteint64_t(0L, elffp
); /* no program header table */
1125 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1126 * ELF header plus alignment */
1127 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1128 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1129 fwriteint16_t(0, elffp
); /* no program header table, again */
1130 fwriteint16_t(0, elffp
); /* still no program header table */
1131 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1132 fwriteint16_t(nsections
, elffp
); /* number of sections */
1133 fwriteint16_t(sec_shstrtab
, elffp
); /* string table section index for
1134 * section header table */
1137 * Build the symbol table and relocation tables.
1139 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1140 for (i
= 0; i
< nsects
; i
++)
1142 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1146 * Now output the section header table.
1149 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1150 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1153 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1156 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1159 /* The normal sections */
1160 for (i
= 0; i
< nsects
; i
++) {
1161 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1162 (sects
[i
]->type
== SHT_PROGBITS
?
1163 sects
[i
]->data
: NULL
), true,
1164 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1169 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1170 shstrtablen
, 0, 0, 1, 0);
1174 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1175 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1179 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1180 strslen
, 0, 0, 1, 0);
1183 /* The relocation sections */
1184 for (i
= 0; i
< nsects
; i
++)
1185 if (sects
[i
]->head
) {
1186 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1187 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1191 if (of_elf64
.current_dfmt
== &df_stabs
) {
1192 /* for debugging information, create the last three sections
1193 which are the .stab , .stabstr and .rel.stab sections respectively */
1195 /* this function call creates the stab sections in memory */
1198 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1199 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1200 stablen
, sec_stabstr
, 0, 4, 12);
1203 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1204 stabstrlen
, 0, 0, 4, 0);
1207 /* link -> symtable info -> section to refer to */
1208 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1209 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1213 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1214 /* for dwarf debugging information, create the ten dwarf sections */
1216 /* this function call creates the dwarf sections in memory */
1220 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1221 arangeslen
, 0, 0, 1, 0);
1224 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1225 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1228 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1229 pubnameslen
, 0, 0, 1, 0);
1232 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1233 infolen
, 0, 0, 1, 0);
1236 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1237 inforellen
, symtabsection
, debug_info
, 1, 24);
1240 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1241 abbrevlen
, 0, 0, 1, 0);
1244 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1245 linelen
, 0, 0, 1, 0);
1248 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1249 linerellen
, symtabsection
, debug_line
, 1, 24);
1252 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1253 framelen
, 0, 0, 8, 0);
1256 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1257 loclen
, 0, 0, 1, 0);
1260 fwritezero(align
, elffp
);
1263 * Now output the sections.
1265 elf_write_sections();
1267 nasm_free(elf_sects
);
1271 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1273 struct SAA
*s
= saa_init(1L);
1275 uint8_t entry
[24], *p
;
1281 * First, an all-zeros entry, required by the ELF spec.
1283 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1288 * Next, an entry for the file name.
1291 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1292 WRITESHORT(p
, STT_FILE
); /* type FILE */
1293 WRITESHORT(p
, SHN_ABS
);
1294 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1295 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1296 saa_wbytes(s
, entry
, 24L);
1301 * Now some standard symbols defining the segments, for relocation
1304 for (i
= 1; i
<= nsects
; i
++) {
1306 WRITELONG(p
, 0); /* no symbol name */
1307 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1308 WRITESHORT(p
, i
); /* section id */
1309 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1310 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1311 saa_wbytes(s
, entry
, 24L);
1318 * Now the other local symbols.
1321 while ((sym
= saa_rstruct(syms
))) {
1322 if (sym
->type
& SYM_GLOBAL
)
1325 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1326 WRITECHAR(p
, sym
->type
); /* type and binding */
1327 WRITECHAR(p
, sym
->other
); /* visibility */
1328 WRITESHORT(p
, sym
->section
); /* index into section header table */
1329 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1330 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1331 saa_wbytes(s
, entry
, 24L);
1336 * dwarf needs symbols for debug sections
1337 * which are relocation targets.
1339 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1340 dwarf_infosym
= *local
;
1342 WRITELONG(p
, 0); /* no symbol name */
1343 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1344 WRITESHORT(p
, debug_info
); /* section id */
1345 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1346 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1347 saa_wbytes(s
, entry
, 24L);
1350 dwarf_abbrevsym
= *local
;
1352 WRITELONG(p
, 0); /* no symbol name */
1353 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1354 WRITESHORT(p
, debug_abbrev
); /* section id */
1355 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1356 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1357 saa_wbytes(s
, entry
, 24L);
1360 dwarf_linesym
= *local
;
1362 WRITELONG(p
, 0); /* no symbol name */
1363 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1364 WRITESHORT(p
, debug_line
); /* section id */
1365 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1366 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1367 saa_wbytes(s
, entry
, 24L);
1373 * Now the global symbols.
1376 while ((sym
= saa_rstruct(syms
))) {
1377 if (!(sym
->type
& SYM_GLOBAL
))
1380 WRITELONG(p
, sym
->strpos
);
1381 WRITECHAR(p
, sym
->type
); /* type and binding */
1382 WRITECHAR(p
, sym
->other
); /* visibility */
1383 WRITESHORT(p
, sym
->section
);
1384 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1385 WRITEDLONG(p
, (int64_t)sym
->size
);
1386 saa_wbytes(s
, entry
, 24L);
1393 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1396 uint8_t *p
, entry
[24];
1397 int32_t global_offset
;
1406 * How to onvert from a global placeholder to a real symbol index;
1407 * the +2 refers to the two special entries, the null entry and
1408 * the filename entry.
1410 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1413 int32_t sym
= r
->symbol
;
1415 if (sym
>= GLOBAL_TEMP_BASE
)
1416 sym
+= global_offset
;
1419 WRITEDLONG(p
, r
->address
);
1420 WRITELONG(p
, r
->type
);
1422 WRITEDLONG(p
, r
->offset
);
1423 saa_wbytes(s
, entry
, 24L);
1432 static void elf_section_header(int name
, int type
, uint64_t flags
,
1433 void *data
, bool is_saa
, uint64_t datalen
,
1434 int link
, int info
, int align
, int eltsize
)
1436 elf_sects
[elf_nsect
].data
= data
;
1437 elf_sects
[elf_nsect
].len
= datalen
;
1438 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1441 fwriteint32_t((int32_t)name
, elffp
);
1442 fwriteint32_t((int32_t)type
, elffp
);
1443 fwriteint64_t((int64_t)flags
, elffp
);
1444 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1445 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1446 fwriteint64_t(datalen
, elffp
);
1448 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1449 fwriteint32_t((int32_t)link
, elffp
);
1450 fwriteint32_t((int32_t)info
, elffp
);
1451 fwriteint64_t((int64_t)align
, elffp
);
1452 fwriteint64_t((int64_t)eltsize
, elffp
);
1455 static void elf_write_sections(void)
1458 for (i
= 0; i
< elf_nsect
; i
++)
1459 if (elf_sects
[i
].data
) {
1460 int32_t len
= elf_sects
[i
].len
;
1461 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1462 int32_t align
= reallen
- len
;
1463 if (elf_sects
[i
].is_saa
)
1464 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1466 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1467 fwritezero(align
, elffp
);
1471 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1473 saa_wbytes(sect
->data
, data
, len
);
1476 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1478 saa_writeaddr(sect
->data
, data
, len
);
1482 static int32_t elf_segbase(int32_t segment
)
1487 static int elf_directive(char *directive
, char *value
, int pass
)
1493 if (!strcmp(directive
, "osabi")) {
1495 return 1; /* ignore in pass 2 */
1497 n
= readnum(value
, &err
);
1499 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1502 if (n
< 0 || n
> 255) {
1503 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1509 if ((p
= strchr(value
,',')) == NULL
)
1512 n
= readnum(p
+1, &err
);
1513 if (err
|| n
< 0 || n
> 255) {
1514 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1525 static void elf_filename(char *inname
, char *outname
, efunc error
)
1527 strcpy(elf_module
, inname
);
1528 standard_extension(inname
, outname
, ".o", error
);
1531 extern macros_t elf_stdmac
[];
1533 static int elf_set_info(enum geninfo type
, char **val
)
1539 static struct dfmt df_dwarf
= {
1540 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1550 static struct dfmt df_stabs
= {
1551 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1562 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1564 struct ofmt of_elf64
= {
1565 "ELF64 (x86_64) object files (e.g. Linux)",
1582 /* common debugging routines */
1583 static void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
,
1584 int is_global
, char *special
)
1593 static void debug64_directive(const char *directive
, const char *params
)
1599 static void debug64_typevalue(int32_t type
)
1601 int32_t stype
, ssize
;
1602 switch (TYM_TYPE(type
)) {
1645 stype
= STT_SECTION
;
1660 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1661 lastsym
->size
= ssize
;
1662 lastsym
->type
= stype
;
1666 /* stabs debugging routines */
1668 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1671 if (!stabs_filename
) {
1672 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1673 strcpy(stabs_filename
, filename
);
1675 if (strcmp(stabs_filename
, filename
)) {
1676 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1677 in fact, this leak comes in quite handy to maintain a list of files
1678 encountered so far in the symbol lines... */
1680 /* why not nasm_free(stabs_filename); we're done with the old one */
1682 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1683 strcpy(stabs_filename
, filename
);
1687 currentline
= linenumber
;
1691 static void stabs64_output(int type
, void *param
)
1693 struct symlininfo
*s
;
1694 struct linelist
*el
;
1695 if (type
== TY_DEBUGSYMLIN
) {
1696 if (debug_immcall
) {
1697 s
= (struct symlininfo
*)param
;
1698 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1699 return; /* line info is only collected for executable sections */
1701 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1702 el
->info
.offset
= s
->offset
;
1703 el
->info
.section
= s
->section
;
1704 el
->info
.name
= s
->name
;
1705 el
->line
= currentline
;
1706 el
->filename
= stabs_filename
;
1709 stabslines
->last
->next
= el
;
1710 stabslines
->last
= el
;
1713 stabslines
->last
= el
;
1720 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1722 WRITELONG(p,n_strx); \
1723 WRITECHAR(p,n_type); \
1724 WRITECHAR(p,n_other); \
1725 WRITESHORT(p,n_desc); \
1726 WRITELONG(p,n_value); \
1729 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1731 static void stabs64_generate(void)
1733 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1734 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1738 struct linelist
*ptr
;
1742 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1743 for (i
= 0; i
< numlinestabs
; i
++)
1747 if (numfiles
== 0) {
1748 allfiles
[0] = ptr
->filename
;
1751 for (i
= 0; i
< numfiles
; i
++) {
1752 if (!strcmp(allfiles
[i
], ptr
->filename
))
1755 if (i
>= numfiles
) {
1756 allfiles
[i
] = ptr
->filename
;
1763 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1764 for (i
= 0; i
< numfiles
; i
++) {
1765 fileidx
[i
] = strsize
;
1766 strsize
+= strlen(allfiles
[i
]) + 1;
1769 for (i
= 0; i
< numfiles
; i
++) {
1770 if (!strcmp(allfiles
[i
], elf_module
)) {
1776 /* worst case size of the stab buffer would be:
1777 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1780 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1781 sizeof(struct stabentry
));
1783 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1785 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1788 for (i
= 0; i
< numfiles
; i
++) {
1789 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1793 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1800 /* this is the first stab, its strx points to the filename of the
1801 the source-file, the n_desc field should be set to the number
1804 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1806 /* this is the stab for the main source file */
1807 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1809 /* relocation table entry */
1811 /* Since the symbol table has two entries before */
1812 /* the section symbols, the index in the info.section */
1813 /* member must be adjusted by adding 2 */
1815 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1816 WRITELONG(rptr
, R_X86_64_32
);
1817 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1820 currfile
= mainfileindex
;
1824 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1825 /* oops file has changed... */
1826 for (i
= 0; i
< numfiles
; i
++)
1827 if (!strcmp(allfiles
[i
], ptr
->filename
))
1830 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1834 /* relocation table entry */
1836 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1837 WRITELONG(rptr
, R_X86_64_32
);
1838 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1841 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1844 /* relocation table entry */
1846 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1847 WRITELONG(rptr
, R_X86_64_32
);
1848 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1854 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1856 nasm_free(allfiles
);
1859 stablen
= (sptr
- sbuf
);
1860 stabrellen
= (rptr
- rbuf
);
1866 static void stabs64_cleanup(void)
1868 struct linelist
*ptr
, *del
;
1880 nasm_free(stabrelbuf
);
1882 nasm_free(stabstrbuf
);
1884 /* dwarf routines */
1885 static void dwarf64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1892 ndebugs
= 3; /* 3 debug symbols */
1895 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1899 dwarf64_findfile(filename
);
1901 currentline
= linenumber
;
1904 /* called from elf_out with type == TY_DEBUGSYMLIN */
1905 static void dwarf64_output(int type
, void *param
)
1907 int ln
, aa
, inx
, maxln
, soc
;
1908 struct symlininfo
*s
;
1913 s
= (struct symlininfo
*)param
;
1914 /* line number info is only gathered for executable sections */
1915 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1917 /* Check if section index has changed */
1918 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1920 dwarf64_findsect(s
->section
);
1922 /* do nothing unless line or file has changed */
1925 ln
= currentline
- dwarf_csect
->line
;
1926 aa
= s
->offset
- dwarf_csect
->offset
;
1927 inx
= dwarf_clist
->line
;
1928 plinep
= dwarf_csect
->psaa
;
1929 /* check for file change */
1930 if (!(inx
== dwarf_csect
->file
))
1932 saa_write8(plinep
,DW_LNS_set_file
);
1933 saa_write8(plinep
,inx
);
1934 dwarf_csect
->file
= inx
;
1936 /* check for line change */
1939 /* test if in range of special op code */
1940 maxln
= line_base
+ line_range
;
1941 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1942 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1944 saa_write8(plinep
,soc
);
1950 saa_write8(plinep
,DW_LNS_advance_line
);
1951 saa_wleb128s(plinep
,ln
);
1955 saa_write8(plinep
,DW_LNS_advance_pc
);
1956 saa_wleb128u(plinep
,aa
);
1959 dwarf_csect
->line
= currentline
;
1960 dwarf_csect
->offset
= s
->offset
;
1962 /* show change handled */
1968 static void dwarf64_generate(void)
1972 struct linelist
*ftentry
;
1973 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1974 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1975 struct sectlist
*psect
;
1976 size_t saalen
, linepoff
, totlen
, highaddr
;
1978 /* write epilogues for each line program range */
1979 /* and build aranges section */
1980 paranges
= saa_init(1L);
1981 parangesrel
= saa_init(1L);
1982 saa_write16(paranges
,3); /* dwarf version */
1983 saa_write64(parangesrel
, paranges
->datalen
+4);
1984 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1985 saa_write64(parangesrel
, 0);
1986 saa_write32(paranges
,0); /* offset into info */
1987 saa_write8(paranges
,8); /* pointer size */
1988 saa_write8(paranges
,0); /* not segmented */
1989 saa_write32(paranges
,0); /* padding */
1990 /* iterate though sectlist entries */
1991 psect
= dwarf_fsect
;
1994 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1996 plinep
= psect
->psaa
;
1997 /* Line Number Program Epilogue */
1998 saa_write8(plinep
,2); /* std op 2 */
1999 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2000 saa_write8(plinep
,DW_LNS_extended_op
);
2001 saa_write8(plinep
,1); /* operand length */
2002 saa_write8(plinep
,DW_LNE_end_sequence
);
2003 totlen
+= plinep
->datalen
;
2004 /* range table relocation entry */
2005 saa_write64(parangesrel
, paranges
->datalen
+ 4);
2006 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2007 saa_write64(parangesrel
, (uint64_t) 0);
2008 /* range table entry */
2009 saa_write64(paranges
,0x0000); /* range start */
2010 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
2011 highaddr
+= sects
[psect
->section
]->len
;
2012 /* done with this entry */
2013 psect
= psect
->next
;
2015 saa_write64(paranges
,0); /* null address */
2016 saa_write64(paranges
,0); /* null length */
2017 saalen
= paranges
->datalen
;
2018 arangeslen
= saalen
+ 4;
2019 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2020 WRITELONG(pbuf
,saalen
); /* initial length */
2021 saa_rnbytes(paranges
, pbuf
, saalen
);
2024 /* build rela.aranges section */
2025 arangesrellen
= saalen
= parangesrel
->datalen
;
2026 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2027 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2028 saa_free(parangesrel
);
2030 /* build pubnames section */
2031 ppubnames
= saa_init(1L);
2032 saa_write16(ppubnames
,3); /* dwarf version */
2033 saa_write32(ppubnames
,0); /* offset into info */
2034 saa_write32(ppubnames
,0); /* space used in info */
2035 saa_write32(ppubnames
,0); /* end of list */
2036 saalen
= ppubnames
->datalen
;
2037 pubnameslen
= saalen
+ 4;
2038 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2039 WRITELONG(pbuf
,saalen
); /* initial length */
2040 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2041 saa_free(ppubnames
);
2043 /* build info section */
2044 pinfo
= saa_init(1L);
2045 pinforel
= saa_init(1L);
2046 saa_write16(pinfo
,3); /* dwarf version */
2047 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2048 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2049 saa_write64(pinforel
, 0);
2050 saa_write32(pinfo
,0); /* offset into abbrev */
2051 saa_write8(pinfo
,8); /* pointer size */
2052 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2053 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2054 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2055 saa_write64(pinforel
, 0);
2056 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2057 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2058 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2059 saa_write64(pinforel
, 0);
2060 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2061 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2062 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2063 saa_write64(pinforel
, 0);
2064 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2065 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2066 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2067 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2068 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2069 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2070 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2071 saa_write64(pinforel
, 0);
2072 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2073 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2074 saa_write8(pinfo
,0); /* end of entries */
2075 saalen
= pinfo
->datalen
;
2076 infolen
= saalen
+ 4;
2077 infobuf
= pbuf
= nasm_malloc(infolen
);
2078 WRITELONG(pbuf
,saalen
); /* initial length */
2079 saa_rnbytes(pinfo
, pbuf
, saalen
);
2082 /* build rela.info section */
2083 inforellen
= saalen
= pinforel
->datalen
;
2084 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2085 saa_rnbytes(pinforel
, pbuf
, saalen
);
2088 /* build abbrev section */
2089 pabbrev
= saa_init(1L);
2090 saa_write8(pabbrev
,1); /* entry number LEB128u */
2091 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2092 saa_write8(pabbrev
,1); /* has children */
2093 /* the following attributes and forms are all LEB128u values */
2094 saa_write8(pabbrev
,DW_AT_low_pc
);
2095 saa_write8(pabbrev
,DW_FORM_addr
);
2096 saa_write8(pabbrev
,DW_AT_high_pc
);
2097 saa_write8(pabbrev
,DW_FORM_addr
);
2098 saa_write8(pabbrev
,DW_AT_stmt_list
);
2099 saa_write8(pabbrev
,DW_FORM_data4
);
2100 saa_write8(pabbrev
,DW_AT_name
);
2101 saa_write8(pabbrev
,DW_FORM_string
);
2102 saa_write8(pabbrev
,DW_AT_producer
);
2103 saa_write8(pabbrev
,DW_FORM_string
);
2104 saa_write8(pabbrev
,DW_AT_language
);
2105 saa_write8(pabbrev
,DW_FORM_data2
);
2106 saa_write16(pabbrev
,0); /* end of entry */
2107 /* LEB128u usage same as above */
2108 saa_write8(pabbrev
,2); /* entry number */
2109 saa_write8(pabbrev
,DW_TAG_subprogram
);
2110 saa_write8(pabbrev
,0); /* no children */
2111 saa_write8(pabbrev
,DW_AT_low_pc
);
2112 saa_write8(pabbrev
,DW_FORM_addr
);
2113 saa_write8(pabbrev
,DW_AT_frame_base
);
2114 saa_write8(pabbrev
,DW_FORM_data4
);
2115 saa_write16(pabbrev
,0); /* end of entry */
2116 abbrevlen
= saalen
= pabbrev
->datalen
;
2117 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2118 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2121 /* build line section */
2123 plines
= saa_init(1L);
2124 saa_write8(plines
,1); /* Minimum Instruction Length */
2125 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2126 saa_write8(plines
,line_base
); /* Line Base */
2127 saa_write8(plines
,line_range
); /* Line Range */
2128 saa_write8(plines
,opcode_base
); /* Opcode Base */
2129 /* standard opcode lengths (# of LEB128u operands) */
2130 saa_write8(plines
,0); /* Std opcode 1 length */
2131 saa_write8(plines
,1); /* Std opcode 2 length */
2132 saa_write8(plines
,1); /* Std opcode 3 length */
2133 saa_write8(plines
,1); /* Std opcode 4 length */
2134 saa_write8(plines
,1); /* Std opcode 5 length */
2135 saa_write8(plines
,0); /* Std opcode 6 length */
2136 saa_write8(plines
,0); /* Std opcode 7 length */
2137 saa_write8(plines
,0); /* Std opcode 8 length */
2138 saa_write8(plines
,1); /* Std opcode 9 length */
2139 saa_write8(plines
,0); /* Std opcode 10 length */
2140 saa_write8(plines
,0); /* Std opcode 11 length */
2141 saa_write8(plines
,1); /* Std opcode 12 length */
2142 /* Directory Table */
2143 saa_write8(plines
,0); /* End of table */
2144 /* File Name Table */
2145 ftentry
= dwarf_flist
;
2146 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2148 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2149 saa_write8(plines
,0); /* directory LEB128u */
2150 saa_write8(plines
,0); /* time LEB128u */
2151 saa_write8(plines
,0); /* size LEB128u */
2152 ftentry
= ftentry
->next
;
2154 saa_write8(plines
,0); /* End of table */
2155 linepoff
= plines
->datalen
;
2156 linelen
= linepoff
+ totlen
+ 10;
2157 linebuf
= pbuf
= nasm_malloc(linelen
);
2158 WRITELONG(pbuf
,linelen
-4); /* initial length */
2159 WRITESHORT(pbuf
,3); /* dwarf version */
2160 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2161 /* write line header */
2163 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2166 /* concatonate line program ranges */
2168 plinesrel
= saa_init(1L);
2169 psect
= dwarf_fsect
;
2170 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2172 saa_write64(plinesrel
, linepoff
);
2173 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2174 saa_write64(plinesrel
, (uint64_t) 0);
2175 plinep
= psect
->psaa
;
2176 saalen
= plinep
->datalen
;
2177 saa_rnbytes(plinep
, pbuf
, saalen
);
2181 /* done with this entry */
2182 psect
= psect
->next
;
2186 /* build rela.lines section */
2187 linerellen
=saalen
= plinesrel
->datalen
;
2188 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2189 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2190 saa_free(plinesrel
);
2192 /* build frame section */
2194 framebuf
= pbuf
= nasm_malloc(framelen
);
2195 WRITELONG(pbuf
,framelen
-4); /* initial length */
2197 /* build loc section */
2199 locbuf
= pbuf
= nasm_malloc(loclen
);
2200 WRITEDLONG(pbuf
,0); /* null beginning offset */
2201 WRITEDLONG(pbuf
,0); /* null ending offset */
2204 static void dwarf64_cleanup(void)
2207 nasm_free(arangesbuf
);
2209 nasm_free(arangesrelbuf
);
2211 nasm_free(pubnamesbuf
);
2215 nasm_free(inforelbuf
);
2217 nasm_free(abbrevbuf
);
2221 nasm_free(linerelbuf
);
2223 nasm_free(framebuf
);
2227 static void dwarf64_findfile(const char * fname
)
2230 struct linelist
*match
;
2232 /* return if fname is current file name */
2233 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2234 /* search for match */
2240 match
= dwarf_flist
;
2241 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2243 if (!(strcmp(fname
, match
->filename
)))
2245 dwarf_clist
= match
;
2250 /* add file name to end of list */
2251 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2253 dwarf_clist
->line
= dwarf_numfiles
;
2254 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2255 strcpy(dwarf_clist
->filename
,fname
);
2256 dwarf_clist
->next
= 0;
2257 /* if first entry */
2260 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2261 dwarf_clist
->last
= 0;
2263 /* chain to previous entry */
2266 dwarf_elist
->next
= dwarf_clist
;
2267 dwarf_elist
= dwarf_clist
;
2272 static void dwarf64_findsect(const int index
)
2275 struct sectlist
*match
;
2277 /* return if index is current section index */
2278 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2282 /* search for match */
2288 match
= dwarf_fsect
;
2289 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2291 if ((match
->section
== index
))
2293 dwarf_csect
= match
;
2296 match
= match
->next
;
2299 /* add entry to end of list */
2300 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2302 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2303 dwarf_csect
->line
= 1;
2304 dwarf_csect
->offset
= 0;
2305 dwarf_csect
->file
= 1;
2306 dwarf_csect
->section
= index
;
2307 dwarf_csect
->next
= 0;
2308 /* set relocatable address at start of line program */
2309 saa_write8(plinep
,DW_LNS_extended_op
);
2310 saa_write8(plinep
,9); /* operand length */
2311 saa_write8(plinep
,DW_LNE_set_address
);
2312 saa_write64(plinep
,0); /* Start Address */
2313 /* if first entry */
2316 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2317 dwarf_csect
->last
= 0;
2319 /* chain to previous entry */
2322 dwarf_esect
->next
= dwarf_csect
;
2323 dwarf_esect
= dwarf_csect
;