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
53 #include "output/outform.h"
54 #include "output/outlib.h"
57 #include "output/elf64.h"
58 #include "output/dwarf.h"
59 #include "output/outelf.h"
63 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
67 int64_t address
; /* relative to _start_ of section */
68 int64_t symbol
; /* symbol index */
69 int64_t offset
; /* symbol addend */
70 int type
; /* type of relocation */
74 struct rbtree symv
; /* symbol value and rbtree of globals */
75 int32_t strpos
; /* string table position of name */
76 int32_t section
; /* section ID of the symbol */
77 int type
; /* symbol type */
78 int other
; /* symbol visibility */
79 int32_t size
; /* size of symbol */
80 int32_t globnum
; /* symbol table offset if global */
81 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
82 char *name
; /* used temporarily if in above list */
89 int32_t index
; /* index into sects array */
90 int type
; /* SHT_PROGBITS or SHT_NOBITS */
91 uint64_t align
; /* alignment: power of two */
92 uint64_t flags
; /* section flags */
96 struct Reloc
*head
, **tail
;
97 struct rbtree
*gsyms
; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section
**sects
;
102 static int nsects
, sectlen
;
104 #define SHSTR_DELTA 256
105 static char *shstrtab
;
106 static int shstrtablen
, shstrtabsize
;
108 static struct SAA
*syms
;
109 static uint32_t nlocals
, nglobs
, ndebugs
;
111 static int32_t def_seg
;
113 static struct RAA
*bsym
;
115 static struct SAA
*strs
;
116 static uint32_t strslen
;
118 static struct Symbol
*fwds
;
120 static char elf_module
[FILENAME_MAX
];
122 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
123 static uint8_t elf_abiver
= 0; /* Current ABI version */
125 extern struct ofmt of_elf64
;
127 static struct ELF_SECTDATA
{
132 static int elf_nsect
, nsections
;
133 static int64_t elf_foffs
;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section
*, const void *, size_t);
137 static void elf_sect_writeaddr(struct Section
*, int64_t, size_t);
138 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
140 static void elf_write_sections(void);
141 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
142 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
143 static void add_sectname(char *, char *);
145 /* type values for stabs debugging sections */
146 #define N_SO 0x64 /* ID for main source file */
147 #define N_SOL 0x84 /* ID for sub-source file */
148 #define N_BINCL 0x82 /* not currently used */
149 #define N_EINCL 0xA2 /* not currently used */
166 int section
; /* index into sects[] */
167 int segto
; /* internal section number */
168 char *name
; /* shallow-copied pointer of section name */
172 struct symlininfo info
;
175 struct linelist
*next
;
176 struct linelist
*last
;
185 struct sectlist
*next
;
186 struct sectlist
*last
;
189 /* common debug variables */
190 static int currentline
= 1;
191 static int debug_immcall
= 0;
193 /* stabs debug variables */
194 static struct linelist
*stabslines
= 0;
195 static int numlinestabs
= 0;
196 static char *stabs_filename
= 0;
197 static int symtabsection
;
198 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
199 static int stablen
, stabstrlen
, stabrellen
;
201 /* dwarf debug variables */
202 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
203 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
204 static int dwarf_numfiles
= 0, dwarf_nsections
;
205 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
206 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
207 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
208 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
209 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
210 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
213 static struct dfmt df_dwarf
;
214 static struct dfmt df_stabs
;
215 static struct Symbol
*lastsym
;
217 /* common debugging routines */
218 static void debug64_typevalue(int32_t);
219 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
220 static void debug64_directive(const char *, const char *);
222 /* stabs debugging routines */
223 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
224 static void stabs64_output(int, void *);
225 static void stabs64_generate(void);
226 static void stabs64_cleanup(void);
228 /* dwarf debugging routines */
229 static void dwarf64_init(void);
230 static void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
231 static void dwarf64_output(int, void *);
232 static void dwarf64_generate(void);
233 static void dwarf64_cleanup(void);
234 static void dwarf64_findfile(const char *);
235 static void dwarf64_findsect(const int);
238 * Special section numbers which are used to define ELF special
239 * symbols, which can be used with WRT to provide PIC relocation
242 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
243 static int32_t elf_got_sect
, elf_plt_sect
;
244 static int32_t elf_sym_sect
;
245 static int32_t elf_gottpoff_sect
;
247 static void elf_init(void)
251 nsects
= sectlen
= 0;
252 syms
= saa_init((int32_t)sizeof(struct Symbol
));
253 nlocals
= nglobs
= ndebugs
= 0;
256 saa_wbytes(strs
, "\0", 1L);
257 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
258 strslen
= 2 + strlen(elf_module
);
260 shstrtablen
= shstrtabsize
= 0;;
261 add_sectname("", "");
265 elf_gotpc_sect
= seg_alloc();
266 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
268 elf_gotoff_sect
= seg_alloc();
269 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
271 elf_got_sect
= seg_alloc();
272 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
274 elf_plt_sect
= seg_alloc();
275 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
277 elf_sym_sect
= seg_alloc();
278 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
280 elf_gottpoff_sect
= seg_alloc();
281 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
284 def_seg
= seg_alloc();
288 static void elf_cleanup(int debuginfo
)
296 for (i
= 0; i
< nsects
; i
++) {
297 if (sects
[i
]->type
!= SHT_NOBITS
)
298 saa_free(sects
[i
]->data
);
300 saa_free(sects
[i
]->rel
);
301 while (sects
[i
]->head
) {
303 sects
[i
]->head
= sects
[i
]->head
->next
;
311 if (of_elf64
.current_dfmt
) {
312 of_elf64
.current_dfmt
->cleanup();
315 /* add entry to the elf .shstrtab section */
316 static void add_sectname(char *firsthalf
, char *secondhalf
)
318 int len
= strlen(firsthalf
) + strlen(secondhalf
);
319 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
320 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
321 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
322 strcat(shstrtab
+ shstrtablen
, secondhalf
);
323 shstrtablen
+= len
+ 1;
326 static int elf_make_section(char *name
, int type
, int flags
, int align
)
330 s
= nasm_malloc(sizeof(*s
));
332 if (type
!= SHT_NOBITS
)
333 s
->data
= saa_init(1L);
336 s
->len
= s
->size
= 0;
338 if (!strcmp(name
, ".text"))
341 s
->index
= seg_alloc();
342 add_sectname("", name
);
343 s
->name
= nasm_malloc(1 + strlen(name
));
344 strcpy(s
->name
, name
);
350 if (nsects
>= sectlen
)
351 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
357 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
360 uint32_t flags
, flags_and
, flags_or
;
365 * Default is 64 bits.
373 while (*p
&& !nasm_isspace(*p
))
377 flags_and
= flags_or
= type
= align
= 0;
379 while (*p
&& nasm_isspace(*p
))
383 while (*p
&& !nasm_isspace(*p
))
387 while (*p
&& nasm_isspace(*p
))
390 if (!nasm_strnicmp(q
, "align=", 6)) {
394 if ((align
- 1) & align
) { /* means it's not a power of two */
395 nasm_error(ERR_NONFATAL
, "section alignment %d is not"
396 " a power of two", align
);
399 } else if (!nasm_stricmp(q
, "alloc")) {
400 flags_and
|= SHF_ALLOC
;
401 flags_or
|= SHF_ALLOC
;
402 } else if (!nasm_stricmp(q
, "noalloc")) {
403 flags_and
|= SHF_ALLOC
;
404 flags_or
&= ~SHF_ALLOC
;
405 } else if (!nasm_stricmp(q
, "exec")) {
406 flags_and
|= SHF_EXECINSTR
;
407 flags_or
|= SHF_EXECINSTR
;
408 } else if (!nasm_stricmp(q
, "noexec")) {
409 flags_and
|= SHF_EXECINSTR
;
410 flags_or
&= ~SHF_EXECINSTR
;
411 } else if (!nasm_stricmp(q
, "write")) {
412 flags_and
|= SHF_WRITE
;
413 flags_or
|= SHF_WRITE
;
414 } else if (!nasm_stricmp(q
, "tls")) {
415 flags_and
|= SHF_TLS
;
417 } else if (!nasm_stricmp(q
, "nowrite")) {
418 flags_and
|= SHF_WRITE
;
419 flags_or
&= ~SHF_WRITE
;
420 } else if (!nasm_stricmp(q
, "progbits")) {
422 } else if (!nasm_stricmp(q
, "nobits")) {
424 } else if (pass
== 1) {
425 nasm_error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
426 " declaration of section `%s'", q
, name
);
430 if (!strcmp(name
, ".shstrtab") ||
431 !strcmp(name
, ".symtab") ||
432 !strcmp(name
, ".strtab")) {
433 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
438 for (i
= 0; i
< nsects
; i
++)
439 if (!strcmp(name
, sects
[i
]->name
))
442 const struct elf_known_section
*ks
= elf_known_sections
;
445 if (!strcmp(name
, ks
->name
))
450 type
= type
? type
: ks
->type
;
451 align
= align
? align
: ks
->align
;
452 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
454 i
= elf_make_section(name
, type
, flags
, align
);
455 } else if (pass
== 1) {
456 if ((type
&& sects
[i
]->type
!= type
)
457 || (align
&& sects
[i
]->align
!= align
)
458 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
459 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
460 " redeclaration of section `%s'", name
);
463 return sects
[i
]->index
;
466 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
467 int is_global
, char *special
)
471 bool special_used
= false;
473 #if defined(DEBUG) && DEBUG>2
475 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
476 name
, segment
, offset
, is_global
, special
);
478 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
480 * This is a NASM special symbol. We never allow it into
481 * the ELF symbol table, even if it's a valid one. If it
482 * _isn't_ a valid one, we should barf immediately.
484 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
485 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
486 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
487 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
491 if (is_global
== 3) {
494 * Fix up a forward-reference symbol size from the first
497 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
498 if (!strcmp((*s
)->name
, name
)) {
499 struct tokenval tokval
;
503 while (*p
&& !nasm_isspace(*p
))
505 while (*p
&& nasm_isspace(*p
))
509 tokval
.t_type
= TOKEN_INVALID
;
510 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
513 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
514 " expression as symbol size");
516 (*s
)->size
= reloc_value(e
);
520 * Remove it from the list of unresolved sizes.
522 nasm_free((*s
)->name
);
526 return; /* it wasn't an important one */
529 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
530 strslen
+= 1 + strlen(name
);
532 lastsym
= sym
= saa_wstruct(syms
);
534 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
537 sym
->type
= is_global
? SYM_GLOBAL
: 0;
538 sym
->other
= STV_DEFAULT
;
540 if (segment
== NO_SEG
)
541 sym
->section
= SHN_ABS
;
544 sym
->section
= SHN_UNDEF
;
545 if (nsects
== 0 && segment
== def_seg
) {
547 if (segment
!= elf_section_names(".text", 2, &tempint
))
548 nasm_error(ERR_PANIC
,
549 "strange segment conditions in ELF driver");
550 sym
->section
= nsects
;
552 for (i
= 0; i
< nsects
; i
++)
553 if (segment
== sects
[i
]->index
) {
554 sym
->section
= i
+ 1;
560 if (is_global
== 2) {
563 sym
->section
= SHN_COMMON
;
565 * We have a common variable. Check the special text to see
566 * if it's a valid number and power of two; if so, store it
567 * as the alignment for the common variable.
571 sym
->symv
.key
= readnum(special
, &err
);
573 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
574 " valid number", special
);
575 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
576 != 2 * sym
->symv
.key
- 1)
577 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
578 " power of two", special
);
582 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
584 if (sym
->type
== SYM_GLOBAL
) {
586 * If sym->section == SHN_ABS, then the first line of the
587 * else section would cause a core dump, because its a reference
588 * beyond the end of the section array.
589 * This behaviour is exhibited by this code:
592 * To avoid such a crash, such requests are silently discarded.
593 * This may not be the best solution.
595 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
596 bsym
= raa_write(bsym
, segment
, nglobs
);
597 } else if (sym
->section
!= SHN_ABS
) {
599 * This is a global symbol; so we must add it to the rbtree
600 * of global symbols in its section.
602 * In addition, we check the special text for symbol
603 * type and size information.
605 sects
[sym
->section
-1]->gsyms
=
606 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
609 int n
= strcspn(special
, " \t");
611 if (!nasm_strnicmp(special
, "function", n
))
612 sym
->type
|= STT_FUNC
;
613 else if (!nasm_strnicmp(special
, "data", n
) ||
614 !nasm_strnicmp(special
, "object", n
))
615 sym
->type
|= STT_OBJECT
;
616 else if (!nasm_strnicmp(special
, "notype", n
))
617 sym
->type
|= STT_NOTYPE
;
619 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
623 while (nasm_isspace(*special
))
626 n
= strcspn(special
, " \t");
627 if (!nasm_strnicmp(special
, "default", n
))
628 sym
->other
= STV_DEFAULT
;
629 else if (!nasm_strnicmp(special
, "internal", n
))
630 sym
->other
= STV_INTERNAL
;
631 else if (!nasm_strnicmp(special
, "hidden", n
))
632 sym
->other
= STV_HIDDEN
;
633 else if (!nasm_strnicmp(special
, "protected", n
))
634 sym
->other
= STV_PROTECTED
;
641 struct tokenval tokval
;
644 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
646 while (special
[n
] && nasm_isspace(special
[n
]))
649 * We have a size expression; attempt to
653 stdscan_bufptr
= special
+ n
;
654 tokval
.t_type
= TOKEN_INVALID
;
655 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
660 sym
->name
= nasm_strdup(name
);
663 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
664 " expression as symbol size");
666 sym
->size
= reloc_value(e
);
668 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
673 * If TLS segment, mark symbol accordingly.
675 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
677 sym
->type
|= STT_TLS
;
680 sym
->globnum
= nglobs
;
685 if (special
&& !special_used
)
686 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
689 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
690 int64_t offset
, int type
)
693 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
694 sect
->tail
= &r
->next
;
697 r
->address
= sect
->len
;
699 if (segment
== NO_SEG
)
704 for (i
= 0; i
< nsects
; i
++)
705 if (segment
== sects
[i
]->index
)
708 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
716 * This routine deals with ..got and ..sym relocations: the more
717 * complicated kinds. In shared-library writing, some relocations
718 * with respect to global symbols must refer to the precise symbol
719 * rather than referring to an offset from the base of the section
720 * _containing_ the symbol. Such relocations call to this routine,
721 * which searches the symbol list for the symbol in question.
723 * R_386_GOT32 references require the _exact_ symbol address to be
724 * used; R_386_32 references can be at an offset from the symbol.
725 * The boolean argument `exact' tells us this.
727 * Return value is the adjusted value of `addr', having become an
728 * offset from the symbol rather than the section. Should always be
729 * zero when returning from an exact call.
731 * Limitation: if you define two symbols at the same place,
732 * confusion will occur.
734 * Inefficiency: we search, currently, using a linked list which
735 * isn't even necessarily sorted.
737 static void elf_add_gsym_reloc(struct Section
*sect
,
738 int32_t segment
, uint64_t offset
, int64_t pcrel
,
739 int type
, bool exact
)
748 * First look up the segment/offset pair and find a global
749 * symbol corresponding to it. If it's not one of our segments,
750 * then it must be an external symbol, in which case we're fine
751 * doing a normal elf_add_reloc after first sanity-checking
752 * that the offset from the symbol is zero.
755 for (i
= 0; i
< nsects
; i
++)
756 if (segment
== sects
[i
]->index
) {
763 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
765 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
769 srb
= rb_search(s
->gsyms
, offset
);
770 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
771 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
772 " for this reference");
775 sym
= container_of(srb
, struct Symbol
, symv
);
777 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
778 sect
->tail
= &r
->next
;
781 r
->address
= sect
->len
;
782 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
783 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
789 static void elf_out(int32_t segto
, const void *data
,
790 enum out_type type
, uint64_t size
,
791 int32_t segment
, int32_t wrt
)
796 static struct symlininfo sinfo
;
800 #if defined(DEBUG) && DEBUG>2
801 if (data
) fprintf(stderr
,
802 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
803 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
805 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
806 currentline
, type
, segment
, segto
, size
);
810 * handle absolute-assembly (structure definitions)
812 if (segto
== NO_SEG
) {
813 if (type
!= OUT_RESERVE
)
814 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
820 for (i
= 0; i
< nsects
; i
++)
821 if (segto
== sects
[i
]->index
) {
826 int tempint
; /* ignored */
827 if (segto
!= elf_section_names(".text", 2, &tempint
))
828 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
830 s
= sects
[nsects
- 1];
834 /* invoke current debug_output routine */
835 if (of_elf64
.current_dfmt
) {
836 sinfo
.offset
= s
->len
;
839 sinfo
.name
= s
->name
;
840 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
842 /* end of debugging stuff */
844 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
845 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
846 " BSS section `%s': ignored", s
->name
);
847 s
->len
+= realsize(type
, size
);
851 if (type
== OUT_RESERVE
) {
852 if (s
->type
== SHT_PROGBITS
) {
853 nasm_error(ERR_WARNING
, "uninitialized space declared in"
854 " non-BSS section `%s': zeroing", s
->name
);
855 elf_sect_write(s
, NULL
, size
);
858 } else if (type
== OUT_RAWDATA
) {
859 if (segment
!= NO_SEG
)
860 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
861 elf_sect_write(s
, data
, size
);
862 } else if (type
== OUT_ADDRESS
) {
863 addr
= *(int64_t *)data
;
864 if (segment
== NO_SEG
) {
866 } else if (segment
% 2) {
867 nasm_error(ERR_NONFATAL
, "ELF format does not support"
868 " segment base references");
873 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
876 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
879 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
882 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
885 nasm_error(ERR_PANIC
, "internal error elf64-hpa-871");
889 } else if (wrt
== elf_gotpc_sect
+ 1) {
891 * The user will supply GOT relative to $$. ELF
892 * will let us have GOT relative to $. So we
893 * need to fix up the data item by $-$$.
896 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
898 } else if (wrt
== elf_gotoff_sect
+ 1) {
900 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
901 "references to be qword");
903 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
906 } else if (wrt
== elf_got_sect
+ 1) {
909 elf_add_gsym_reloc(s
, segment
, addr
, 0,
910 R_X86_64_GOT32
, true);
914 elf_add_gsym_reloc(s
, segment
, addr
, 0,
915 R_X86_64_GOT64
, true);
919 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
922 } else if (wrt
== elf_sym_sect
+ 1) {
925 elf_add_gsym_reloc(s
, segment
, addr
, 0,
930 elf_add_gsym_reloc(s
, segment
, addr
, 0,
935 elf_add_gsym_reloc(s
, segment
, addr
, 0,
940 elf_add_gsym_reloc(s
, segment
, addr
, 0,
945 nasm_error(ERR_PANIC
, "internal error elf64-hpa-903");
948 } else if (wrt
== elf_plt_sect
+ 1) {
949 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
950 "relative PLT references");
952 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
956 elf_sect_writeaddr(s
, addr
, size
);
957 } else if (type
== OUT_REL2ADR
) {
958 addr
= *(int64_t *)data
- size
;
959 if (segment
== segto
)
960 nasm_error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
961 if (segment
== NO_SEG
) {
963 } else if (segment
% 2) {
964 nasm_error(ERR_NONFATAL
, "ELF format does not support"
965 " segment base references");
968 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
971 nasm_error(ERR_NONFATAL
,
972 "Unsupported non-32-bit ELF relocation [2]");
975 elf_sect_writeaddr(s
, addr
, 2);
976 } else if (type
== OUT_REL4ADR
) {
977 addr
= *(int64_t *)data
- size
;
978 if (segment
== segto
)
979 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
980 if (segment
== NO_SEG
) {
982 } else if (segment
% 2) {
983 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
984 " segment base references");
987 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
989 } else if (wrt
== elf_plt_sect
+ 1) {
990 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
991 R_X86_64_PLT32
, true);
993 } else if (wrt
== elf_gotpc_sect
+ 1 ||
994 wrt
== elf_got_sect
+ 1) {
995 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
996 R_X86_64_GOTPCREL
, true);
998 } else if (wrt
== elf_gotoff_sect
+ 1 ||
999 wrt
== elf_got_sect
+ 1) {
1000 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1002 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1003 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1004 R_X86_64_GOTTPOFF
, true);
1007 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1011 elf_sect_writeaddr(s
, addr
, 4);
1012 } else if (type
== OUT_REL8ADR
) {
1013 addr
= *(int64_t *)data
- size
;
1014 if (segment
== segto
)
1015 nasm_error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
1016 if (segment
== NO_SEG
) {
1018 } else if (segment
% 2) {
1019 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
1020 " segment base references");
1022 if (wrt
== NO_SEG
) {
1023 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1025 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1026 wrt
== elf_got_sect
+ 1) {
1027 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1028 R_X86_64_GOTPCREL64
, true);
1030 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1031 wrt
== elf_got_sect
+ 1) {
1032 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1034 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1035 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1038 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1042 elf_sect_writeaddr(s
, addr
, 8);
1046 static void elf_write(void)
1053 int32_t symtablen
, symtablocal
;
1056 * Work out how many sections we will have. We have SHN_UNDEF,
1057 * then the flexible user sections, then the fixed sections
1058 * `.shstrtab', `.symtab' and `.strtab', then optionally
1059 * relocation sections for the user sections.
1061 nsections
= sec_numspecial
+ 1;
1062 if (of_elf64
.current_dfmt
== &df_stabs
)
1064 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1067 add_sectname("", ".shstrtab");
1068 add_sectname("", ".symtab");
1069 add_sectname("", ".strtab");
1070 for (i
= 0; i
< nsects
; i
++) {
1071 nsections
++; /* for the section itself */
1072 if (sects
[i
]->head
) {
1073 nsections
++; /* for its relocations */
1074 add_sectname(".rela", sects
[i
]->name
);
1078 if (of_elf64
.current_dfmt
== &df_stabs
) {
1079 /* in case the debug information is wanted, just add these three sections... */
1080 add_sectname("", ".stab");
1081 add_sectname("", ".stabstr");
1082 add_sectname(".rel", ".stab");
1085 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1086 /* the dwarf debug standard specifies the following ten sections,
1087 not all of which are currently implemented,
1088 although all of them are defined. */
1089 #define debug_aranges (int64_t) (nsections-10)
1090 #define debug_info (int64_t) (nsections-7)
1091 #define debug_abbrev (int64_t) (nsections-5)
1092 #define debug_line (int64_t) (nsections-4)
1093 add_sectname("", ".debug_aranges");
1094 add_sectname(".rela", ".debug_aranges");
1095 add_sectname("", ".debug_pubnames");
1096 add_sectname("", ".debug_info");
1097 add_sectname(".rela", ".debug_info");
1098 add_sectname("", ".debug_abbrev");
1099 add_sectname("", ".debug_line");
1100 add_sectname(".rela", ".debug_line");
1101 add_sectname("", ".debug_frame");
1102 add_sectname("", ".debug_loc");
1106 * Output the ELF header.
1108 fwrite("\177ELF\2\1\1", 7, 1, ofile
);
1109 fputc(elf_osabi
, ofile
);
1110 fputc(elf_abiver
, ofile
);
1111 fwritezero(7, ofile
);
1112 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1113 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1114 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1115 fwriteint64_t(0L, ofile
); /* no entry point */
1116 fwriteint64_t(0L, ofile
); /* no program header table */
1117 fwriteint64_t(0x40L
, ofile
); /* section headers straight after
1118 * ELF header plus alignment */
1119 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
1120 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1121 fwriteint16_t(0, ofile
); /* no program header table, again */
1122 fwriteint16_t(0, ofile
); /* still no program header table */
1123 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1124 fwriteint16_t(nsections
, ofile
); /* number of sections */
1125 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1126 * section header table */
1129 * Build the symbol table and relocation tables.
1131 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1132 for (i
= 0; i
< nsects
; i
++)
1134 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1138 * Now output the section header table.
1141 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1142 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1145 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1148 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1151 /* The normal sections */
1152 for (i
= 0; i
< nsects
; i
++) {
1153 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1154 (sects
[i
]->type
== SHT_PROGBITS
?
1155 sects
[i
]->data
: NULL
), true,
1156 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1161 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1162 shstrtablen
, 0, 0, 1, 0);
1166 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1167 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1171 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1172 strslen
, 0, 0, 1, 0);
1175 /* The relocation sections */
1176 for (i
= 0; i
< nsects
; i
++)
1177 if (sects
[i
]->head
) {
1178 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1179 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1183 if (of_elf64
.current_dfmt
== &df_stabs
) {
1184 /* for debugging information, create the last three sections
1185 which are the .stab , .stabstr and .rel.stab sections respectively */
1187 /* this function call creates the stab sections in memory */
1190 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1191 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1192 stablen
, sec_stabstr
, 0, 4, 12);
1195 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1196 stabstrlen
, 0, 0, 4, 0);
1199 /* link -> symtable info -> section to refer to */
1200 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1201 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1205 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1206 /* for dwarf debugging information, create the ten dwarf sections */
1208 /* this function call creates the dwarf sections in memory */
1212 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1213 arangeslen
, 0, 0, 1, 0);
1216 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1217 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1220 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1221 pubnameslen
, 0, 0, 1, 0);
1224 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1225 infolen
, 0, 0, 1, 0);
1228 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1229 inforellen
, symtabsection
, debug_info
, 1, 24);
1232 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1233 abbrevlen
, 0, 0, 1, 0);
1236 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1237 linelen
, 0, 0, 1, 0);
1240 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1241 linerellen
, symtabsection
, debug_line
, 1, 24);
1244 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1245 framelen
, 0, 0, 8, 0);
1248 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1249 loclen
, 0, 0, 1, 0);
1252 fwritezero(align
, ofile
);
1255 * Now output the sections.
1257 elf_write_sections();
1259 nasm_free(elf_sects
);
1263 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1265 struct SAA
*s
= saa_init(1L);
1267 uint8_t entry
[24], *p
;
1273 * First, an all-zeros entry, required by the ELF spec.
1275 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1280 * Next, an entry for the file name.
1283 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1284 WRITESHORT(p
, STT_FILE
); /* type FILE */
1285 WRITESHORT(p
, SHN_ABS
);
1286 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1287 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1288 saa_wbytes(s
, entry
, 24L);
1293 * Now some standard symbols defining the segments, for relocation
1296 for (i
= 1; i
<= nsects
; i
++) {
1298 WRITELONG(p
, 0); /* no symbol name */
1299 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1300 WRITESHORT(p
, i
); /* section id */
1301 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1302 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1303 saa_wbytes(s
, entry
, 24L);
1310 * Now the other local symbols.
1313 while ((sym
= saa_rstruct(syms
))) {
1314 if (sym
->type
& SYM_GLOBAL
)
1317 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1318 WRITECHAR(p
, sym
->type
); /* type and binding */
1319 WRITECHAR(p
, sym
->other
); /* visibility */
1320 WRITESHORT(p
, sym
->section
); /* index into section header table */
1321 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1322 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1323 saa_wbytes(s
, entry
, 24L);
1328 * dwarf needs symbols for debug sections
1329 * which are relocation targets.
1331 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1332 dwarf_infosym
= *local
;
1334 WRITELONG(p
, 0); /* no symbol name */
1335 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1336 WRITESHORT(p
, debug_info
); /* section id */
1337 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1338 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1339 saa_wbytes(s
, entry
, 24L);
1342 dwarf_abbrevsym
= *local
;
1344 WRITELONG(p
, 0); /* no symbol name */
1345 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1346 WRITESHORT(p
, debug_abbrev
); /* section id */
1347 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1348 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1349 saa_wbytes(s
, entry
, 24L);
1352 dwarf_linesym
= *local
;
1354 WRITELONG(p
, 0); /* no symbol name */
1355 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1356 WRITESHORT(p
, debug_line
); /* section id */
1357 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1358 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1359 saa_wbytes(s
, entry
, 24L);
1365 * Now the global symbols.
1368 while ((sym
= saa_rstruct(syms
))) {
1369 if (!(sym
->type
& SYM_GLOBAL
))
1372 WRITELONG(p
, sym
->strpos
);
1373 WRITECHAR(p
, sym
->type
); /* type and binding */
1374 WRITECHAR(p
, sym
->other
); /* visibility */
1375 WRITESHORT(p
, sym
->section
);
1376 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1377 WRITEDLONG(p
, (int64_t)sym
->size
);
1378 saa_wbytes(s
, entry
, 24L);
1385 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1388 uint8_t *p
, entry
[24];
1389 int32_t global_offset
;
1398 * How to onvert from a global placeholder to a real symbol index;
1399 * the +2 refers to the two special entries, the null entry and
1400 * the filename entry.
1402 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1405 int32_t sym
= r
->symbol
;
1407 if (sym
>= GLOBAL_TEMP_BASE
)
1408 sym
+= global_offset
;
1411 WRITEDLONG(p
, r
->address
);
1412 WRITELONG(p
, r
->type
);
1414 WRITEDLONG(p
, r
->offset
);
1415 saa_wbytes(s
, entry
, 24L);
1424 static void elf_section_header(int name
, int type
, uint64_t flags
,
1425 void *data
, bool is_saa
, uint64_t datalen
,
1426 int link
, int info
, int align
, int eltsize
)
1428 elf_sects
[elf_nsect
].data
= data
;
1429 elf_sects
[elf_nsect
].len
= datalen
;
1430 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1433 fwriteint32_t((int32_t)name
, ofile
);
1434 fwriteint32_t((int32_t)type
, ofile
);
1435 fwriteint64_t((int64_t)flags
, ofile
);
1436 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
1437 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1438 fwriteint64_t(datalen
, ofile
);
1440 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1441 fwriteint32_t((int32_t)link
, ofile
);
1442 fwriteint32_t((int32_t)info
, ofile
);
1443 fwriteint64_t((int64_t)align
, ofile
);
1444 fwriteint64_t((int64_t)eltsize
, ofile
);
1447 static void elf_write_sections(void)
1450 for (i
= 0; i
< elf_nsect
; i
++)
1451 if (elf_sects
[i
].data
) {
1452 int32_t len
= elf_sects
[i
].len
;
1453 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1454 int32_t align
= reallen
- len
;
1455 if (elf_sects
[i
].is_saa
)
1456 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1458 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1459 fwritezero(align
, ofile
);
1463 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1465 saa_wbytes(sect
->data
, data
, len
);
1468 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1470 saa_writeaddr(sect
->data
, data
, len
);
1474 static int32_t elf_segbase(int32_t segment
)
1479 static int elf_directive(enum directives directive
, char *value
, int pass
)
1485 switch (directive
) {
1488 return 1; /* ignore in pass 2 */
1490 n
= readnum(value
, &err
);
1492 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1495 if (n
< 0 || n
> 255) {
1496 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1502 if ((p
= strchr(value
,',')) == NULL
)
1505 n
= readnum(p
+1, &err
);
1506 if (err
|| n
< 0 || n
> 255) {
1507 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1519 static void elf_filename(char *inname
, char *outname
)
1521 strcpy(elf_module
, inname
);
1522 standard_extension(inname
, outname
, ".o");
1525 extern macros_t elf_stdmac
[];
1527 static int elf_set_info(enum geninfo type
, char **val
)
1533 static struct dfmt df_dwarf
= {
1534 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1544 static struct dfmt df_stabs
= {
1545 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1556 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1558 struct ofmt of_elf64
= {
1559 "ELF64 (x86_64) object files (e.g. Linux)",
1576 /* common debugging routines */
1577 static void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
,
1578 int is_global
, char *special
)
1587 static void debug64_directive(const char *directive
, const char *params
)
1593 static void debug64_typevalue(int32_t type
)
1595 int32_t stype
, ssize
;
1596 switch (TYM_TYPE(type
)) {
1639 stype
= STT_SECTION
;
1654 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1655 lastsym
->size
= ssize
;
1656 lastsym
->type
= stype
;
1660 /* stabs debugging routines */
1662 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1665 if (!stabs_filename
) {
1666 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1667 strcpy(stabs_filename
, filename
);
1669 if (strcmp(stabs_filename
, filename
)) {
1670 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1671 in fact, this leak comes in quite handy to maintain a list of files
1672 encountered so far in the symbol lines... */
1674 /* why not nasm_free(stabs_filename); we're done with the old one */
1676 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1677 strcpy(stabs_filename
, filename
);
1681 currentline
= linenumber
;
1685 static void stabs64_output(int type
, void *param
)
1687 struct symlininfo
*s
;
1688 struct linelist
*el
;
1689 if (type
== TY_DEBUGSYMLIN
) {
1690 if (debug_immcall
) {
1691 s
= (struct symlininfo
*)param
;
1692 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1693 return; /* line info is only collected for executable sections */
1695 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1696 el
->info
.offset
= s
->offset
;
1697 el
->info
.section
= s
->section
;
1698 el
->info
.name
= s
->name
;
1699 el
->line
= currentline
;
1700 el
->filename
= stabs_filename
;
1703 stabslines
->last
->next
= el
;
1704 stabslines
->last
= el
;
1707 stabslines
->last
= el
;
1714 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1716 WRITELONG(p,n_strx); \
1717 WRITECHAR(p,n_type); \
1718 WRITECHAR(p,n_other); \
1719 WRITESHORT(p,n_desc); \
1720 WRITELONG(p,n_value); \
1723 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1725 static void stabs64_generate(void)
1727 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1728 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1732 struct linelist
*ptr
;
1736 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1737 for (i
= 0; i
< numlinestabs
; i
++)
1741 if (numfiles
== 0) {
1742 allfiles
[0] = ptr
->filename
;
1745 for (i
= 0; i
< numfiles
; i
++) {
1746 if (!strcmp(allfiles
[i
], ptr
->filename
))
1749 if (i
>= numfiles
) {
1750 allfiles
[i
] = ptr
->filename
;
1757 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1758 for (i
= 0; i
< numfiles
; i
++) {
1759 fileidx
[i
] = strsize
;
1760 strsize
+= strlen(allfiles
[i
]) + 1;
1763 for (i
= 0; i
< numfiles
; i
++) {
1764 if (!strcmp(allfiles
[i
], elf_module
)) {
1770 /* worst case size of the stab buffer would be:
1771 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1774 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1775 sizeof(struct stabentry
));
1777 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1779 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1782 for (i
= 0; i
< numfiles
; i
++) {
1783 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1787 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1794 /* this is the first stab, its strx points to the filename of the
1795 the source-file, the n_desc field should be set to the number
1798 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1800 /* this is the stab for the main source file */
1801 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1803 /* relocation table entry */
1805 /* Since the symbol table has two entries before */
1806 /* the section symbols, the index in the info.section */
1807 /* member must be adjusted by adding 2 */
1809 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1810 WRITELONG(rptr
, R_X86_64_32
);
1811 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1814 currfile
= mainfileindex
;
1818 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1819 /* oops file has changed... */
1820 for (i
= 0; i
< numfiles
; i
++)
1821 if (!strcmp(allfiles
[i
], ptr
->filename
))
1824 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1828 /* relocation table entry */
1830 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1831 WRITELONG(rptr
, R_X86_64_32
);
1832 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1835 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1838 /* relocation table entry */
1840 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1841 WRITELONG(rptr
, R_X86_64_32
);
1842 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1848 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1850 nasm_free(allfiles
);
1853 stablen
= (sptr
- sbuf
);
1854 stabrellen
= (rptr
- rbuf
);
1860 static void stabs64_cleanup(void)
1862 struct linelist
*ptr
, *del
;
1874 nasm_free(stabrelbuf
);
1876 nasm_free(stabstrbuf
);
1878 /* dwarf routines */
1879 static void dwarf64_init(void)
1881 ndebugs
= 3; /* 3 debug symbols */
1884 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1888 dwarf64_findfile(filename
);
1890 currentline
= linenumber
;
1893 /* called from elf_out with type == TY_DEBUGSYMLIN */
1894 static void dwarf64_output(int type
, void *param
)
1896 int ln
, aa
, inx
, maxln
, soc
;
1897 struct symlininfo
*s
;
1902 s
= (struct symlininfo
*)param
;
1903 /* line number info is only gathered for executable sections */
1904 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1906 /* Check if section index has changed */
1907 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1909 dwarf64_findsect(s
->section
);
1911 /* do nothing unless line or file has changed */
1914 ln
= currentline
- dwarf_csect
->line
;
1915 aa
= s
->offset
- dwarf_csect
->offset
;
1916 inx
= dwarf_clist
->line
;
1917 plinep
= dwarf_csect
->psaa
;
1918 /* check for file change */
1919 if (!(inx
== dwarf_csect
->file
))
1921 saa_write8(plinep
,DW_LNS_set_file
);
1922 saa_write8(plinep
,inx
);
1923 dwarf_csect
->file
= inx
;
1925 /* check for line change */
1928 /* test if in range of special op code */
1929 maxln
= line_base
+ line_range
;
1930 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1931 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1933 saa_write8(plinep
,soc
);
1939 saa_write8(plinep
,DW_LNS_advance_line
);
1940 saa_wleb128s(plinep
,ln
);
1944 saa_write8(plinep
,DW_LNS_advance_pc
);
1945 saa_wleb128u(plinep
,aa
);
1948 dwarf_csect
->line
= currentline
;
1949 dwarf_csect
->offset
= s
->offset
;
1951 /* show change handled */
1957 static void dwarf64_generate(void)
1961 struct linelist
*ftentry
;
1962 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1963 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1964 struct sectlist
*psect
;
1965 size_t saalen
, linepoff
, totlen
, highaddr
;
1967 /* write epilogues for each line program range */
1968 /* and build aranges section */
1969 paranges
= saa_init(1L);
1970 parangesrel
= saa_init(1L);
1971 saa_write16(paranges
,3); /* dwarf version */
1972 saa_write64(parangesrel
, paranges
->datalen
+4);
1973 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1974 saa_write64(parangesrel
, 0);
1975 saa_write32(paranges
,0); /* offset into info */
1976 saa_write8(paranges
,8); /* pointer size */
1977 saa_write8(paranges
,0); /* not segmented */
1978 saa_write32(paranges
,0); /* padding */
1979 /* iterate though sectlist entries */
1980 psect
= dwarf_fsect
;
1983 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1985 plinep
= psect
->psaa
;
1986 /* Line Number Program Epilogue */
1987 saa_write8(plinep
,2); /* std op 2 */
1988 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1989 saa_write8(plinep
,DW_LNS_extended_op
);
1990 saa_write8(plinep
,1); /* operand length */
1991 saa_write8(plinep
,DW_LNE_end_sequence
);
1992 totlen
+= plinep
->datalen
;
1993 /* range table relocation entry */
1994 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1995 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1996 saa_write64(parangesrel
, (uint64_t) 0);
1997 /* range table entry */
1998 saa_write64(paranges
,0x0000); /* range start */
1999 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
2000 highaddr
+= sects
[psect
->section
]->len
;
2001 /* done with this entry */
2002 psect
= psect
->next
;
2004 saa_write64(paranges
,0); /* null address */
2005 saa_write64(paranges
,0); /* null length */
2006 saalen
= paranges
->datalen
;
2007 arangeslen
= saalen
+ 4;
2008 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2009 WRITELONG(pbuf
,saalen
); /* initial length */
2010 saa_rnbytes(paranges
, pbuf
, saalen
);
2013 /* build rela.aranges section */
2014 arangesrellen
= saalen
= parangesrel
->datalen
;
2015 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2016 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2017 saa_free(parangesrel
);
2019 /* build pubnames section */
2020 ppubnames
= saa_init(1L);
2021 saa_write16(ppubnames
,3); /* dwarf version */
2022 saa_write32(ppubnames
,0); /* offset into info */
2023 saa_write32(ppubnames
,0); /* space used in info */
2024 saa_write32(ppubnames
,0); /* end of list */
2025 saalen
= ppubnames
->datalen
;
2026 pubnameslen
= saalen
+ 4;
2027 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2028 WRITELONG(pbuf
,saalen
); /* initial length */
2029 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2030 saa_free(ppubnames
);
2032 /* build info section */
2033 pinfo
= saa_init(1L);
2034 pinforel
= saa_init(1L);
2035 saa_write16(pinfo
,3); /* dwarf version */
2036 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2037 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2038 saa_write64(pinforel
, 0);
2039 saa_write32(pinfo
,0); /* offset into abbrev */
2040 saa_write8(pinfo
,8); /* pointer size */
2041 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2042 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2043 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2044 saa_write64(pinforel
, 0);
2045 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2046 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2047 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2048 saa_write64(pinforel
, 0);
2049 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2050 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2051 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2052 saa_write64(pinforel
, 0);
2053 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2054 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2055 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2056 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2057 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2058 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2059 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2060 saa_write64(pinforel
, 0);
2061 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2062 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2063 saa_write8(pinfo
,0); /* end of entries */
2064 saalen
= pinfo
->datalen
;
2065 infolen
= saalen
+ 4;
2066 infobuf
= pbuf
= nasm_malloc(infolen
);
2067 WRITELONG(pbuf
,saalen
); /* initial length */
2068 saa_rnbytes(pinfo
, pbuf
, saalen
);
2071 /* build rela.info section */
2072 inforellen
= saalen
= pinforel
->datalen
;
2073 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2074 saa_rnbytes(pinforel
, pbuf
, saalen
);
2077 /* build abbrev section */
2078 pabbrev
= saa_init(1L);
2079 saa_write8(pabbrev
,1); /* entry number LEB128u */
2080 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2081 saa_write8(pabbrev
,1); /* has children */
2082 /* the following attributes and forms are all LEB128u values */
2083 saa_write8(pabbrev
,DW_AT_low_pc
);
2084 saa_write8(pabbrev
,DW_FORM_addr
);
2085 saa_write8(pabbrev
,DW_AT_high_pc
);
2086 saa_write8(pabbrev
,DW_FORM_addr
);
2087 saa_write8(pabbrev
,DW_AT_stmt_list
);
2088 saa_write8(pabbrev
,DW_FORM_data4
);
2089 saa_write8(pabbrev
,DW_AT_name
);
2090 saa_write8(pabbrev
,DW_FORM_string
);
2091 saa_write8(pabbrev
,DW_AT_producer
);
2092 saa_write8(pabbrev
,DW_FORM_string
);
2093 saa_write8(pabbrev
,DW_AT_language
);
2094 saa_write8(pabbrev
,DW_FORM_data2
);
2095 saa_write16(pabbrev
,0); /* end of entry */
2096 /* LEB128u usage same as above */
2097 saa_write8(pabbrev
,2); /* entry number */
2098 saa_write8(pabbrev
,DW_TAG_subprogram
);
2099 saa_write8(pabbrev
,0); /* no children */
2100 saa_write8(pabbrev
,DW_AT_low_pc
);
2101 saa_write8(pabbrev
,DW_FORM_addr
);
2102 saa_write8(pabbrev
,DW_AT_frame_base
);
2103 saa_write8(pabbrev
,DW_FORM_data4
);
2104 saa_write16(pabbrev
,0); /* end of entry */
2105 abbrevlen
= saalen
= pabbrev
->datalen
;
2106 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2107 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2110 /* build line section */
2112 plines
= saa_init(1L);
2113 saa_write8(plines
,1); /* Minimum Instruction Length */
2114 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2115 saa_write8(plines
,line_base
); /* Line Base */
2116 saa_write8(plines
,line_range
); /* Line Range */
2117 saa_write8(plines
,opcode_base
); /* Opcode Base */
2118 /* standard opcode lengths (# of LEB128u operands) */
2119 saa_write8(plines
,0); /* Std opcode 1 length */
2120 saa_write8(plines
,1); /* Std opcode 2 length */
2121 saa_write8(plines
,1); /* Std opcode 3 length */
2122 saa_write8(plines
,1); /* Std opcode 4 length */
2123 saa_write8(plines
,1); /* Std opcode 5 length */
2124 saa_write8(plines
,0); /* Std opcode 6 length */
2125 saa_write8(plines
,0); /* Std opcode 7 length */
2126 saa_write8(plines
,0); /* Std opcode 8 length */
2127 saa_write8(plines
,1); /* Std opcode 9 length */
2128 saa_write8(plines
,0); /* Std opcode 10 length */
2129 saa_write8(plines
,0); /* Std opcode 11 length */
2130 saa_write8(plines
,1); /* Std opcode 12 length */
2131 /* Directory Table */
2132 saa_write8(plines
,0); /* End of table */
2133 /* File Name Table */
2134 ftentry
= dwarf_flist
;
2135 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2137 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2138 saa_write8(plines
,0); /* directory LEB128u */
2139 saa_write8(plines
,0); /* time LEB128u */
2140 saa_write8(plines
,0); /* size LEB128u */
2141 ftentry
= ftentry
->next
;
2143 saa_write8(plines
,0); /* End of table */
2144 linepoff
= plines
->datalen
;
2145 linelen
= linepoff
+ totlen
+ 10;
2146 linebuf
= pbuf
= nasm_malloc(linelen
);
2147 WRITELONG(pbuf
,linelen
-4); /* initial length */
2148 WRITESHORT(pbuf
,3); /* dwarf version */
2149 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2150 /* write line header */
2152 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2155 /* concatonate line program ranges */
2157 plinesrel
= saa_init(1L);
2158 psect
= dwarf_fsect
;
2159 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2161 saa_write64(plinesrel
, linepoff
);
2162 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2163 saa_write64(plinesrel
, (uint64_t) 0);
2164 plinep
= psect
->psaa
;
2165 saalen
= plinep
->datalen
;
2166 saa_rnbytes(plinep
, pbuf
, saalen
);
2170 /* done with this entry */
2171 psect
= psect
->next
;
2175 /* build rela.lines section */
2176 linerellen
=saalen
= plinesrel
->datalen
;
2177 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2178 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2179 saa_free(plinesrel
);
2181 /* build frame section */
2183 framebuf
= pbuf
= nasm_malloc(framelen
);
2184 WRITELONG(pbuf
,framelen
-4); /* initial length */
2186 /* build loc section */
2188 locbuf
= pbuf
= nasm_malloc(loclen
);
2189 WRITEDLONG(pbuf
,0); /* null beginning offset */
2190 WRITEDLONG(pbuf
,0); /* null ending offset */
2193 static void dwarf64_cleanup(void)
2196 nasm_free(arangesbuf
);
2198 nasm_free(arangesrelbuf
);
2200 nasm_free(pubnamesbuf
);
2204 nasm_free(inforelbuf
);
2206 nasm_free(abbrevbuf
);
2210 nasm_free(linerelbuf
);
2212 nasm_free(framebuf
);
2216 static void dwarf64_findfile(const char * fname
)
2219 struct linelist
*match
;
2221 /* return if fname is current file name */
2222 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2223 /* search for match */
2229 match
= dwarf_flist
;
2230 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2232 if (!(strcmp(fname
, match
->filename
)))
2234 dwarf_clist
= match
;
2239 /* add file name to end of list */
2240 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2242 dwarf_clist
->line
= dwarf_numfiles
;
2243 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2244 strcpy(dwarf_clist
->filename
,fname
);
2245 dwarf_clist
->next
= 0;
2246 /* if first entry */
2249 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2250 dwarf_clist
->last
= 0;
2252 /* chain to previous entry */
2255 dwarf_elist
->next
= dwarf_clist
;
2256 dwarf_elist
= dwarf_clist
;
2261 static void dwarf64_findsect(const int index
)
2264 struct sectlist
*match
;
2266 /* return if index is current section index */
2267 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2271 /* search for match */
2277 match
= dwarf_fsect
;
2278 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2280 if ((match
->section
== index
))
2282 dwarf_csect
= match
;
2285 match
= match
->next
;
2288 /* add entry to end of list */
2289 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2291 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2292 dwarf_csect
->line
= 1;
2293 dwarf_csect
->offset
= 0;
2294 dwarf_csect
->file
= 1;
2295 dwarf_csect
->section
= index
;
2296 dwarf_csect
->next
= 0;
2297 /* set relocatable address at start of line program */
2298 saa_write8(plinep
,DW_LNS_extended_op
);
2299 saa_write8(plinep
,9); /* operand length */
2300 saa_write8(plinep
,DW_LNE_set_address
);
2301 saa_write64(plinep
,0); /* Start Address */
2302 /* if first entry */
2305 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2306 dwarf_csect
->last
= 0;
2308 /* chain to previous entry */
2311 dwarf_esect
->next
= dwarf_csect
;
2312 dwarf_esect
= dwarf_csect
;