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/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
68 int64_t address
; /* relative to _start_ of section */
69 int64_t symbol
; /* symbol index */
70 int64_t offset
; /* symbol addend */
71 int type
; /* type of relocation */
75 struct rbtree symv
; /* symbol value and rbtree of globals */
76 int32_t strpos
; /* string table position of name */
77 int32_t section
; /* section ID of the symbol */
78 int type
; /* symbol type */
79 int other
; /* symbol visibility */
80 int32_t size
; /* size of symbol */
81 int32_t globnum
; /* symbol table offset if global */
82 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
83 char *name
; /* used temporarily if in above list */
90 int32_t index
; /* index into sects array */
91 int type
; /* SHT_PROGBITS or SHT_NOBITS */
92 uint64_t align
; /* alignment: power of two */
93 uint64_t flags
; /* section flags */
97 struct Reloc
*head
, **tail
;
98 struct rbtree
*gsyms
; /* global symbols in section */
101 #define SECT_DELTA 32
102 static struct Section
**sects
;
103 static int nsects
, sectlen
;
105 #define SHSTR_DELTA 256
106 static char *shstrtab
;
107 static int shstrtablen
, shstrtabsize
;
109 static struct SAA
*syms
;
110 static uint32_t nlocals
, nglobs
, ndebugs
; /* Symbol counts */
112 static int32_t def_seg
;
114 static struct RAA
*bsym
;
116 static struct SAA
*strs
;
117 static uint32_t strslen
;
119 static struct Symbol
*fwds
;
121 static char elf_module
[FILENAME_MAX
];
123 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
124 static uint8_t elf_abiver
= 0; /* Current ABI version */
126 extern struct ofmt of_elf64
;
128 static struct ELF_SECTDATA
{
133 static int elf_nsect
, nsections
;
134 static int64_t elf_foffs
;
136 static void elf_write(void);
137 static void elf_sect_write(struct Section
*, const void *, size_t);
138 static void elf_sect_writeaddr(struct Section
*, int64_t, size_t);
139 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
141 static void elf_write_sections(void);
142 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
143 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
144 static void add_sectname(char *, char *);
160 int section
; /* index into sects[] */
161 int segto
; /* internal section number */
162 char *name
; /* shallow-copied pointer of section name */
166 struct symlininfo info
;
169 struct linelist
*next
;
170 struct linelist
*last
;
179 struct sectlist
*next
;
180 struct sectlist
*last
;
183 /* common debug variables */
184 static int currentline
= 1;
185 static int debug_immcall
= 0;
187 /* stabs debug variables */
188 static struct linelist
*stabslines
= 0;
189 static int numlinestabs
= 0;
190 static char *stabs_filename
= 0;
191 static int symtabsection
;
192 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
193 static int stablen
, stabstrlen
, stabrellen
;
195 /* dwarf debug variables */
196 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
197 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
198 static int dwarf_numfiles
= 0, dwarf_nsections
;
199 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
200 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
201 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
202 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
203 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
204 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
207 static struct dfmt df_dwarf
;
208 static struct dfmt df_stabs
;
209 static struct Symbol
*lastsym
;
211 /* common debugging routines */
212 static void debug64_typevalue(int32_t);
213 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
214 static void debug64_directive(const char *, const char *);
216 /* stabs debugging routines */
217 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
218 static void stabs64_output(int, void *);
219 static void stabs64_generate(void);
220 static void stabs64_cleanup(void);
222 /* dwarf debugging routines */
223 static void dwarf64_init(void);
224 static void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
225 static void dwarf64_output(int, void *);
226 static void dwarf64_generate(void);
227 static void dwarf64_cleanup(void);
228 static void dwarf64_findfile(const char *);
229 static void dwarf64_findsect(const int);
232 * Special section numbers which are used to define ELF special
233 * symbols, which can be used with WRT to provide PIC relocation
236 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
237 static int32_t elf_got_sect
, elf_plt_sect
;
238 static int32_t elf_sym_sect
;
239 static int32_t elf_gottpoff_sect
;
241 static void elf_init(void)
245 nsects
= sectlen
= 0;
246 syms
= saa_init((int32_t)sizeof(struct Symbol
));
247 nlocals
= nglobs
= ndebugs
= 0;
250 saa_wbytes(strs
, "\0", 1L);
251 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
252 strslen
= 2 + strlen(elf_module
);
254 shstrtablen
= shstrtabsize
= 0;;
255 add_sectname("", "");
259 elf_gotpc_sect
= seg_alloc();
260 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
261 elf_gotoff_sect
= seg_alloc();
262 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
263 elf_got_sect
= seg_alloc();
264 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
265 elf_plt_sect
= seg_alloc();
266 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
267 elf_sym_sect
= seg_alloc();
268 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
269 elf_gottpoff_sect
= seg_alloc();
270 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
272 def_seg
= seg_alloc();
276 static void elf_cleanup(int debuginfo
)
284 for (i
= 0; i
< nsects
; i
++) {
285 if (sects
[i
]->type
!= SHT_NOBITS
)
286 saa_free(sects
[i
]->data
);
288 saa_free(sects
[i
]->rel
);
289 while (sects
[i
]->head
) {
291 sects
[i
]->head
= sects
[i
]->head
->next
;
299 if (of_elf64
.current_dfmt
) {
300 of_elf64
.current_dfmt
->cleanup();
304 /* add entry to the elf .shstrtab section */
305 static void add_sectname(char *firsthalf
, char *secondhalf
)
307 int len
= strlen(firsthalf
) + strlen(secondhalf
);
308 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
309 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
310 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
311 strcat(shstrtab
+ shstrtablen
, secondhalf
);
312 shstrtablen
+= len
+ 1;
315 static int elf_make_section(char *name
, int type
, int flags
, int align
)
319 s
= nasm_malloc(sizeof(*s
));
321 if (type
!= SHT_NOBITS
)
322 s
->data
= saa_init(1L);
325 s
->len
= s
->size
= 0;
327 if (!strcmp(name
, ".text"))
330 s
->index
= seg_alloc();
331 add_sectname("", name
);
332 s
->name
= nasm_malloc(1 + strlen(name
));
333 strcpy(s
->name
, name
);
339 if (nsects
>= sectlen
)
340 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
346 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
349 uint32_t flags
, flags_and
, flags_or
;
354 * Default is 64 bits.
361 p
= nasm_skip_word(name
);
364 flags_and
= flags_or
= type
= align
= 0;
366 section_attrib(name
, p
, pass
, &flags_and
,
367 &flags_or
, &align
, &type
);
369 if (!strcmp(name
, ".shstrtab") ||
370 !strcmp(name
, ".symtab") ||
371 !strcmp(name
, ".strtab")) {
372 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
377 for (i
= 0; i
< nsects
; i
++)
378 if (!strcmp(name
, sects
[i
]->name
))
381 const struct elf_known_section
*ks
= elf_known_sections
;
384 if (!strcmp(name
, ks
->name
))
389 type
= type
? type
: ks
->type
;
390 align
= align
? align
: ks
->align
;
391 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
393 i
= elf_make_section(name
, type
, flags
, align
);
394 } else if (pass
== 1) {
395 if ((type
&& sects
[i
]->type
!= type
)
396 || (align
&& sects
[i
]->align
!= align
)
397 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
398 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
399 " redeclaration of section `%s'", name
);
402 return sects
[i
]->index
;
405 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
406 int is_global
, char *special
)
410 bool special_used
= false;
412 #if defined(DEBUG) && DEBUG>2
413 nasm_error(ERR_DEBUG
,
414 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
415 name
, segment
, offset
, is_global
, special
);
417 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
419 * This is a NASM special symbol. We never allow it into
420 * the ELF symbol table, even if it's a valid one. If it
421 * _isn't_ a valid one, we should barf immediately.
423 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
424 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
425 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
426 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
430 if (is_global
== 3) {
433 * Fix up a forward-reference symbol size from the first
436 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
437 if (!strcmp((*s
)->name
, name
)) {
438 struct tokenval tokval
;
440 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
444 tokval
.t_type
= TOKEN_INVALID
;
445 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
448 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
449 " expression as symbol size");
451 (*s
)->size
= reloc_value(e
);
455 * Remove it from the list of unresolved sizes.
457 nasm_free((*s
)->name
);
461 return; /* it wasn't an important one */
464 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
465 strslen
+= 1 + strlen(name
);
467 lastsym
= sym
= saa_wstruct(syms
);
469 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
472 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
473 sym
->other
= STV_DEFAULT
;
475 if (segment
== NO_SEG
)
476 sym
->section
= SHN_ABS
;
479 sym
->section
= SHN_UNDEF
;
480 if (segment
== def_seg
) {
481 /* we have to be sure at least text section is there */
483 if (segment
!= elf_section_names(".text", 2, &tempint
))
484 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
486 for (i
= 0; i
< nsects
; i
++) {
487 if (segment
== sects
[i
]->index
) {
488 sym
->section
= i
+ 1;
494 if (is_global
== 2) {
497 sym
->section
= SHN_COMMON
;
499 * We have a common variable. Check the special text to see
500 * if it's a valid number and power of two; if so, store it
501 * as the alignment for the common variable.
505 sym
->symv
.key
= readnum(special
, &err
);
507 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
508 " valid number", special
);
509 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
510 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
511 " power of two", special
);
515 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
517 if (sym
->type
== SYM_GLOBAL
) {
519 * If sym->section == SHN_ABS, then the first line of the
520 * else section would cause a core dump, because its a reference
521 * beyond the end of the section array.
522 * This behaviour is exhibited by this code:
525 * To avoid such a crash, such requests are silently discarded.
526 * This may not be the best solution.
528 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
529 bsym
= raa_write(bsym
, segment
, nglobs
);
530 } else if (sym
->section
!= SHN_ABS
) {
532 * This is a global symbol; so we must add it to the rbtree
533 * of global symbols in its section.
535 * In addition, we check the special text for symbol
536 * type and size information.
538 sects
[sym
->section
-1]->gsyms
=
539 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
542 int n
= strcspn(special
, " \t");
544 if (!nasm_strnicmp(special
, "function", n
))
545 sym
->type
|= STT_FUNC
;
546 else if (!nasm_strnicmp(special
, "data", n
) ||
547 !nasm_strnicmp(special
, "object", n
))
548 sym
->type
|= STT_OBJECT
;
549 else if (!nasm_strnicmp(special
, "notype", n
))
550 sym
->type
|= STT_NOTYPE
;
552 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
556 special
= nasm_skip_spaces(special
);
558 n
= strcspn(special
, " \t");
559 if (!nasm_strnicmp(special
, "default", n
))
560 sym
->other
= STV_DEFAULT
;
561 else if (!nasm_strnicmp(special
, "internal", n
))
562 sym
->other
= STV_INTERNAL
;
563 else if (!nasm_strnicmp(special
, "hidden", n
))
564 sym
->other
= STV_HIDDEN
;
565 else if (!nasm_strnicmp(special
, "protected", n
))
566 sym
->other
= STV_PROTECTED
;
573 struct tokenval tokval
;
576 char *saveme
= stdscan_get();
578 while (special
[n
] && nasm_isspace(special
[n
]))
581 * We have a size expression; attempt to
585 stdscan_set(special
+ n
);
586 tokval
.t_type
= TOKEN_INVALID
;
587 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
592 sym
->name
= nasm_strdup(name
);
595 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
596 " expression as symbol size");
598 sym
->size
= reloc_value(e
);
605 * If TLS segment, mark symbol accordingly.
607 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
609 sym
->type
|= STT_TLS
;
612 sym
->globnum
= nglobs
;
617 if (special
&& !special_used
)
618 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
621 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
622 int64_t offset
, int type
)
625 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
626 sect
->tail
= &r
->next
;
629 r
->address
= sect
->len
;
631 if (segment
== NO_SEG
)
636 for (i
= 0; i
< nsects
; i
++)
637 if (segment
== sects
[i
]->index
)
640 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
648 * This routine deals with ..got and ..sym relocations: the more
649 * complicated kinds. In shared-library writing, some relocations
650 * with respect to global symbols must refer to the precise symbol
651 * rather than referring to an offset from the base of the section
652 * _containing_ the symbol. Such relocations call to this routine,
653 * which searches the symbol list for the symbol in question.
655 * R_386_GOT32 references require the _exact_ symbol address to be
656 * used; R_386_32 references can be at an offset from the symbol.
657 * The boolean argument `exact' tells us this.
659 * Return value is the adjusted value of `addr', having become an
660 * offset from the symbol rather than the section. Should always be
661 * zero when returning from an exact call.
663 * Limitation: if you define two symbols at the same place,
664 * confusion will occur.
666 * Inefficiency: we search, currently, using a linked list which
667 * isn't even necessarily sorted.
669 static void elf_add_gsym_reloc(struct Section
*sect
,
670 int32_t segment
, uint64_t offset
, int64_t pcrel
,
671 int type
, bool exact
)
680 * First look up the segment/offset pair and find a global
681 * symbol corresponding to it. If it's not one of our segments,
682 * then it must be an external symbol, in which case we're fine
683 * doing a normal elf_add_reloc after first sanity-checking
684 * that the offset from the symbol is zero.
687 for (i
= 0; i
< nsects
; i
++)
688 if (segment
== sects
[i
]->index
) {
695 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
697 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
701 srb
= rb_search(s
->gsyms
, offset
);
702 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
703 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
704 " for this reference");
707 sym
= container_of(srb
, struct Symbol
, symv
);
709 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
710 sect
->tail
= &r
->next
;
713 r
->address
= sect
->len
;
714 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
715 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
721 static void elf_out(int32_t segto
, const void *data
,
722 enum out_type type
, uint64_t size
,
723 int32_t segment
, int32_t wrt
)
728 static struct symlininfo sinfo
;
732 #if defined(DEBUG) && DEBUG>2
734 nasm_error(ERR_DEBUG
,
735 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
736 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
738 nasm_error(ERR_DEBUG
,
739 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
740 currentline
, type
, segment
, segto
, size
);
744 * handle absolute-assembly (structure definitions)
746 if (segto
== NO_SEG
) {
747 if (type
!= OUT_RESERVE
)
748 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
754 for (i
= 0; i
< nsects
; i
++)
755 if (segto
== sects
[i
]->index
) {
760 int tempint
; /* ignored */
761 if (segto
!= elf_section_names(".text", 2, &tempint
))
762 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
764 s
= sects
[nsects
- 1];
769 /* again some stabs debugging stuff */
770 if (of_elf64
.current_dfmt
) {
771 sinfo
.offset
= s
->len
;
774 sinfo
.name
= s
->name
;
775 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
777 /* end of debugging stuff */
779 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
780 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
781 " BSS section `%s': ignored", s
->name
);
782 s
->len
+= realsize(type
, size
);
786 if (type
== OUT_RESERVE
) {
787 if (s
->type
== SHT_PROGBITS
) {
788 nasm_error(ERR_WARNING
, "uninitialized space declared in"
789 " non-BSS section `%s': zeroing", s
->name
);
790 elf_sect_write(s
, NULL
, size
);
793 } else if (type
== OUT_RAWDATA
) {
794 if (segment
!= NO_SEG
)
795 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
796 elf_sect_write(s
, data
, size
);
797 } else if (type
== OUT_ADDRESS
) {
798 addr
= *(int64_t *)data
;
799 if (segment
== NO_SEG
) {
801 } else if (segment
% 2) {
802 nasm_error(ERR_NONFATAL
, "ELF format does not support"
803 " segment base references");
808 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
811 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
814 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
817 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
820 nasm_error(ERR_PANIC
, "internal error elf64-hpa-871");
824 } else if (wrt
== elf_gotpc_sect
+ 1) {
826 * The user will supply GOT relative to $$. ELF
827 * will let us have GOT relative to $. So we
828 * need to fix up the data item by $-$$.
831 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
833 } else if (wrt
== elf_gotoff_sect
+ 1) {
835 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
836 "references to be qword");
838 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
841 } else if (wrt
== elf_got_sect
+ 1) {
844 elf_add_gsym_reloc(s
, segment
, addr
, 0,
845 R_X86_64_GOT32
, true);
849 elf_add_gsym_reloc(s
, segment
, addr
, 0,
850 R_X86_64_GOT64
, true);
854 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
857 } else if (wrt
== elf_sym_sect
+ 1) {
860 elf_add_gsym_reloc(s
, segment
, addr
, 0,
865 elf_add_gsym_reloc(s
, segment
, addr
, 0,
870 elf_add_gsym_reloc(s
, segment
, addr
, 0,
875 elf_add_gsym_reloc(s
, segment
, addr
, 0,
880 nasm_error(ERR_PANIC
, "internal error elf64-hpa-903");
883 } else if (wrt
== elf_plt_sect
+ 1) {
884 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
885 "relative PLT references");
887 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
891 elf_sect_writeaddr(s
, addr
, size
);
892 } else if (type
== OUT_REL2ADR
) {
893 addr
= *(int64_t *)data
- size
;
894 if (segment
== segto
)
895 nasm_error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
896 if (segment
== NO_SEG
) {
898 } else if (segment
% 2) {
899 nasm_error(ERR_NONFATAL
, "ELF format does not support"
900 " segment base references");
903 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
906 nasm_error(ERR_NONFATAL
,
907 "Unsupported non-32-bit ELF relocation [2]");
910 elf_sect_writeaddr(s
, addr
, 2);
911 } else if (type
== OUT_REL4ADR
) {
912 addr
= *(int64_t *)data
- size
;
913 if (segment
== segto
)
914 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
915 if (segment
== NO_SEG
) {
917 } else if (segment
% 2) {
918 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
919 " segment base references");
922 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
924 } else if (wrt
== elf_plt_sect
+ 1) {
925 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
926 R_X86_64_PLT32
, true);
928 } else if (wrt
== elf_gotpc_sect
+ 1 ||
929 wrt
== elf_got_sect
+ 1) {
930 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
931 R_X86_64_GOTPCREL
, true);
933 } else if (wrt
== elf_gotoff_sect
+ 1 ||
934 wrt
== elf_got_sect
+ 1) {
935 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
937 } else if (wrt
== elf_gottpoff_sect
+ 1) {
938 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
939 R_X86_64_GOTTPOFF
, true);
942 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
946 elf_sect_writeaddr(s
, addr
, 4);
947 } else if (type
== OUT_REL8ADR
) {
948 addr
= *(int64_t *)data
- size
;
949 if (segment
== segto
)
950 nasm_error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
951 if (segment
== NO_SEG
) {
953 } else if (segment
% 2) {
954 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
955 " segment base references");
958 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
960 } else if (wrt
== elf_gotpc_sect
+ 1 ||
961 wrt
== elf_got_sect
+ 1) {
962 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
963 R_X86_64_GOTPCREL64
, true);
965 } else if (wrt
== elf_gotoff_sect
+ 1 ||
966 wrt
== elf_got_sect
+ 1) {
967 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
969 } else if (wrt
== elf_gottpoff_sect
+ 1) {
970 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
973 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
977 elf_sect_writeaddr(s
, addr
, 8);
981 static void elf_write(void)
988 int32_t symtablen
, symtablocal
;
991 * Work out how many sections we will have. We have SHN_UNDEF,
992 * then the flexible user sections, then the fixed sections
993 * `.shstrtab', `.symtab' and `.strtab', then optionally
994 * relocation sections for the user sections.
996 nsections
= sec_numspecial
+ 1;
997 if (of_elf64
.current_dfmt
== &df_stabs
)
999 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1002 add_sectname("", ".shstrtab");
1003 add_sectname("", ".symtab");
1004 add_sectname("", ".strtab");
1005 for (i
= 0; i
< nsects
; i
++) {
1006 nsections
++; /* for the section itself */
1007 if (sects
[i
]->head
) {
1008 nsections
++; /* for its relocations */
1009 add_sectname(".rela", sects
[i
]->name
);
1013 if (of_elf64
.current_dfmt
== &df_stabs
) {
1014 /* in case the debug information is wanted, just add these three sections... */
1015 add_sectname("", ".stab");
1016 add_sectname("", ".stabstr");
1017 add_sectname(".rel", ".stab");
1020 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1021 /* the dwarf debug standard specifies the following ten sections,
1022 not all of which are currently implemented,
1023 although all of them are defined. */
1024 #define debug_aranges (int64_t) (nsections-10)
1025 #define debug_info (int64_t) (nsections-7)
1026 #define debug_abbrev (int64_t) (nsections-5)
1027 #define debug_line (int64_t) (nsections-4)
1028 add_sectname("", ".debug_aranges");
1029 add_sectname(".rela", ".debug_aranges");
1030 add_sectname("", ".debug_pubnames");
1031 add_sectname("", ".debug_info");
1032 add_sectname(".rela", ".debug_info");
1033 add_sectname("", ".debug_abbrev");
1034 add_sectname("", ".debug_line");
1035 add_sectname(".rela", ".debug_line");
1036 add_sectname("", ".debug_frame");
1037 add_sectname("", ".debug_loc");
1041 * Output the ELF header.
1043 fwrite("\177ELF\2\1\1", 7, 1, ofile
);
1044 fputc(elf_osabi
, ofile
);
1045 fputc(elf_abiver
, ofile
);
1046 fwritezero(7, ofile
);
1047 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1048 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1049 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1050 fwriteint64_t(0L, ofile
); /* no entry point */
1051 fwriteint64_t(0L, ofile
); /* no program header table */
1052 fwriteint64_t(0x40L
, ofile
); /* section headers straight after
1053 * ELF header plus alignment */
1054 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
1055 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1056 fwriteint16_t(0, ofile
); /* no program header table, again */
1057 fwriteint16_t(0, ofile
); /* still no program header table */
1058 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1059 fwriteint16_t(nsections
, ofile
); /* number of sections */
1060 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1061 * section header table */
1064 * Build the symbol table and relocation tables.
1066 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1067 for (i
= 0; i
< nsects
; i
++)
1069 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1073 * Now output the section header table.
1076 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1077 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1080 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1083 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1086 /* The normal sections */
1087 for (i
= 0; i
< nsects
; i
++) {
1088 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1089 (sects
[i
]->type
== SHT_PROGBITS
?
1090 sects
[i
]->data
: NULL
), true,
1091 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1096 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1097 shstrtablen
, 0, 0, 1, 0);
1101 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1102 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1106 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1107 strslen
, 0, 0, 1, 0);
1110 /* The relocation sections */
1111 for (i
= 0; i
< nsects
; i
++)
1112 if (sects
[i
]->head
) {
1113 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1114 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1118 if (of_elf64
.current_dfmt
== &df_stabs
) {
1119 /* for debugging information, create the last three sections
1120 which are the .stab , .stabstr and .rel.stab sections respectively */
1122 /* this function call creates the stab sections in memory */
1125 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1126 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1127 stablen
, sec_stabstr
, 0, 4, 12);
1130 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1131 stabstrlen
, 0, 0, 4, 0);
1134 /* link -> symtable info -> section to refer to */
1135 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1136 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1139 } else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1140 /* for dwarf debugging information, create the ten dwarf sections */
1142 /* this function call creates the dwarf sections in memory */
1146 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1147 arangeslen
, 0, 0, 1, 0);
1150 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1151 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1154 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1155 pubnameslen
, 0, 0, 1, 0);
1158 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1159 infolen
, 0, 0, 1, 0);
1162 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1163 inforellen
, symtabsection
, debug_info
, 1, 24);
1166 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1167 abbrevlen
, 0, 0, 1, 0);
1170 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1171 linelen
, 0, 0, 1, 0);
1174 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1175 linerellen
, symtabsection
, debug_line
, 1, 24);
1178 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1179 framelen
, 0, 0, 8, 0);
1182 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1183 loclen
, 0, 0, 1, 0);
1186 fwritezero(align
, ofile
);
1189 * Now output the sections.
1191 elf_write_sections();
1193 nasm_free(elf_sects
);
1197 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1199 struct SAA
*s
= saa_init(1L);
1201 uint8_t entry
[24], *p
;
1207 * First, an all-zeros entry, required by the ELF spec.
1209 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1214 * Next, an entry for the file name.
1217 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1218 WRITESHORT(p
, STT_FILE
); /* type FILE */
1219 WRITESHORT(p
, SHN_ABS
);
1220 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1221 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1222 saa_wbytes(s
, entry
, 24L);
1227 * Now some standard symbols defining the segments, for relocation
1230 for (i
= 1; i
<= nsects
; i
++) {
1232 WRITELONG(p
, 0); /* no symbol name */
1233 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1234 WRITESHORT(p
, i
); /* section id */
1235 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1236 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1237 saa_wbytes(s
, entry
, 24L);
1244 * Now the other local symbols.
1247 while ((sym
= saa_rstruct(syms
))) {
1248 if (sym
->type
& SYM_GLOBAL
)
1251 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1252 WRITECHAR(p
, sym
->type
); /* type and binding */
1253 WRITECHAR(p
, sym
->other
); /* visibility */
1254 WRITESHORT(p
, sym
->section
); /* index into section header table */
1255 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1256 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1257 saa_wbytes(s
, entry
, 24L);
1262 * dwarf needs symbols for debug sections
1263 * which are relocation targets.
1265 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1266 dwarf_infosym
= *local
;
1268 WRITELONG(p
, 0); /* no symbol name */
1269 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1270 WRITESHORT(p
, debug_info
); /* section id */
1271 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1272 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1273 saa_wbytes(s
, entry
, 24L);
1276 dwarf_abbrevsym
= *local
;
1278 WRITELONG(p
, 0); /* no symbol name */
1279 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1280 WRITESHORT(p
, debug_abbrev
); /* section id */
1281 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1282 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1283 saa_wbytes(s
, entry
, 24L);
1286 dwarf_linesym
= *local
;
1288 WRITELONG(p
, 0); /* no symbol name */
1289 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1290 WRITESHORT(p
, debug_line
); /* section id */
1291 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1292 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1293 saa_wbytes(s
, entry
, 24L);
1299 * Now the global symbols.
1302 while ((sym
= saa_rstruct(syms
))) {
1303 if (!(sym
->type
& SYM_GLOBAL
))
1306 WRITELONG(p
, sym
->strpos
);
1307 WRITECHAR(p
, sym
->type
); /* type and binding */
1308 WRITECHAR(p
, sym
->other
); /* visibility */
1309 WRITESHORT(p
, sym
->section
);
1310 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1311 WRITEDLONG(p
, (int64_t)sym
->size
);
1312 saa_wbytes(s
, entry
, 24L);
1319 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1322 uint8_t *p
, entry
[24];
1323 int32_t global_offset
;
1332 * How to onvert from a global placeholder to a real symbol index;
1333 * the +2 refers to the two special entries, the null entry and
1334 * the filename entry.
1336 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1339 int32_t sym
= r
->symbol
;
1341 if (sym
>= GLOBAL_TEMP_BASE
)
1342 sym
+= global_offset
;
1345 WRITEDLONG(p
, r
->address
);
1346 WRITELONG(p
, r
->type
);
1348 WRITEDLONG(p
, r
->offset
);
1349 saa_wbytes(s
, entry
, 24L);
1358 static void elf_section_header(int name
, int type
, uint64_t flags
,
1359 void *data
, bool is_saa
, uint64_t datalen
,
1360 int link
, int info
, int align
, int eltsize
)
1362 elf_sects
[elf_nsect
].data
= data
;
1363 elf_sects
[elf_nsect
].len
= datalen
;
1364 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1367 fwriteint32_t((int32_t)name
, ofile
);
1368 fwriteint32_t((int32_t)type
, ofile
);
1369 fwriteint64_t((int64_t)flags
, ofile
);
1370 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
1371 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1372 fwriteint64_t(datalen
, ofile
);
1374 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1375 fwriteint32_t((int32_t)link
, ofile
);
1376 fwriteint32_t((int32_t)info
, ofile
);
1377 fwriteint64_t((int64_t)align
, ofile
);
1378 fwriteint64_t((int64_t)eltsize
, ofile
);
1381 static void elf_write_sections(void)
1384 for (i
= 0; i
< elf_nsect
; i
++)
1385 if (elf_sects
[i
].data
) {
1386 int32_t len
= elf_sects
[i
].len
;
1387 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1388 int32_t align
= reallen
- len
;
1389 if (elf_sects
[i
].is_saa
)
1390 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1392 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1393 fwritezero(align
, ofile
);
1397 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1399 saa_wbytes(sect
->data
, data
, len
);
1402 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1404 saa_writeaddr(sect
->data
, data
, len
);
1408 static int32_t elf_segbase(int32_t segment
)
1413 static int elf_directive(enum directives directive
, char *value
, int pass
)
1419 switch (directive
) {
1422 return 1; /* ignore in pass 2 */
1424 n
= readnum(value
, &err
);
1426 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1429 if (n
< 0 || n
> 255) {
1430 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1436 if ((p
= strchr(value
,',')) == NULL
)
1439 n
= readnum(p
+1, &err
);
1440 if (err
|| n
< 0 || n
> 255) {
1441 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1453 static void elf_filename(char *inname
, char *outname
)
1455 strcpy(elf_module
, inname
);
1456 standard_extension(inname
, outname
, ".o");
1459 extern macros_t elf_stdmac
[];
1461 static int elf_set_info(enum geninfo type
, char **val
)
1467 static struct dfmt df_dwarf
= {
1468 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1478 static struct dfmt df_stabs
= {
1479 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1490 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1492 struct ofmt of_elf64
= {
1493 "ELF64 (x86_64) object files (e.g. Linux)",
1510 /* common debugging routines */
1511 static void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
,
1512 int is_global
, char *special
)
1521 static void debug64_directive(const char *directive
, const char *params
)
1527 static void debug64_typevalue(int32_t type
)
1529 int32_t stype
, ssize
;
1530 switch (TYM_TYPE(type
)) {
1573 stype
= STT_SECTION
;
1588 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1589 lastsym
->size
= ssize
;
1590 lastsym
->type
= stype
;
1594 /* stabs debugging routines */
1596 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1599 if (!stabs_filename
) {
1600 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1601 strcpy(stabs_filename
, filename
);
1603 if (strcmp(stabs_filename
, filename
)) {
1604 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1605 in fact, this leak comes in quite handy to maintain a list of files
1606 encountered so far in the symbol lines... */
1608 /* why not nasm_free(stabs_filename); we're done with the old one */
1610 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1611 strcpy(stabs_filename
, filename
);
1615 currentline
= linenumber
;
1619 static void stabs64_output(int type
, void *param
)
1621 struct symlininfo
*s
;
1622 struct linelist
*el
;
1623 if (type
== TY_DEBUGSYMLIN
) {
1624 if (debug_immcall
) {
1625 s
= (struct symlininfo
*)param
;
1626 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1627 return; /* line info is only collected for executable sections */
1629 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1630 el
->info
.offset
= s
->offset
;
1631 el
->info
.section
= s
->section
;
1632 el
->info
.name
= s
->name
;
1633 el
->line
= currentline
;
1634 el
->filename
= stabs_filename
;
1637 stabslines
->last
->next
= el
;
1638 stabslines
->last
= el
;
1641 stabslines
->last
= el
;
1648 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1650 static void stabs64_generate(void)
1652 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1653 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1657 struct linelist
*ptr
;
1661 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1662 for (i
= 0; i
< numlinestabs
; i
++)
1666 if (numfiles
== 0) {
1667 allfiles
[0] = ptr
->filename
;
1670 for (i
= 0; i
< numfiles
; i
++) {
1671 if (!strcmp(allfiles
[i
], ptr
->filename
))
1674 if (i
>= numfiles
) {
1675 allfiles
[i
] = ptr
->filename
;
1682 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1683 for (i
= 0; i
< numfiles
; i
++) {
1684 fileidx
[i
] = strsize
;
1685 strsize
+= strlen(allfiles
[i
]) + 1;
1688 for (i
= 0; i
< numfiles
; i
++) {
1689 if (!strcmp(allfiles
[i
], elf_module
)) {
1696 * worst case size of the stab buffer would be:
1697 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1698 * plus one "ending" entry
1700 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1701 sizeof(struct stabentry
));
1702 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1703 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1706 for (i
= 0; i
< numfiles
; i
++)
1707 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1710 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1718 * this is the first stab, its strx points to the filename of the
1719 * the source-file, the n_desc field should be set to the number
1720 * of remaining stabs
1722 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1724 /* this is the stab for the main source file */
1725 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1727 /* relocation table entry */
1730 * Since the symbol table has two entries before
1731 * the section symbols, the index in the info.section
1732 * member must be adjusted by adding 2
1735 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1736 WRITELONG(rptr
, R_X86_64_32
);
1737 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1740 currfile
= mainfileindex
;
1744 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1745 /* oops file has changed... */
1746 for (i
= 0; i
< numfiles
; i
++)
1747 if (!strcmp(allfiles
[i
], ptr
->filename
))
1750 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1754 /* relocation table entry */
1756 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1757 WRITELONG(rptr
, R_X86_64_32
);
1758 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1761 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1764 /* relocation table entry */
1766 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1767 WRITELONG(rptr
, R_X86_64_32
);
1768 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1774 /* this is an "ending" token */
1775 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1778 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1780 nasm_free(allfiles
);
1783 stablen
= (sptr
- sbuf
);
1784 stabrellen
= (rptr
- rbuf
);
1790 static void stabs64_cleanup(void)
1792 struct linelist
*ptr
, *del
;
1804 nasm_free(stabrelbuf
);
1805 nasm_free(stabstrbuf
);
1808 /* dwarf routines */
1810 static void dwarf64_init(void)
1812 ndebugs
= 3; /* 3 debug symbols */
1815 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1819 dwarf64_findfile(filename
);
1821 currentline
= linenumber
;
1824 /* called from elf_out with type == TY_DEBUGSYMLIN */
1825 static void dwarf64_output(int type
, void *param
)
1827 int ln
, aa
, inx
, maxln
, soc
;
1828 struct symlininfo
*s
;
1833 s
= (struct symlininfo
*)param
;
1835 /* line number info is only gathered for executable sections */
1836 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1839 /* Check if section index has changed */
1840 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1841 dwarf64_findsect(s
->section
);
1843 /* do nothing unless line or file has changed */
1847 ln
= currentline
- dwarf_csect
->line
;
1848 aa
= s
->offset
- dwarf_csect
->offset
;
1849 inx
= dwarf_clist
->line
;
1850 plinep
= dwarf_csect
->psaa
;
1851 /* check for file change */
1852 if (!(inx
== dwarf_csect
->file
)) {
1853 saa_write8(plinep
,DW_LNS_set_file
);
1854 saa_write8(plinep
,inx
);
1855 dwarf_csect
->file
= inx
;
1857 /* check for line change */
1859 /* test if in range of special op code */
1860 maxln
= line_base
+ line_range
;
1861 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1862 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1863 saa_write8(plinep
,soc
);
1865 saa_write8(plinep
,DW_LNS_advance_line
);
1866 saa_wleb128s(plinep
,ln
);
1868 saa_write8(plinep
,DW_LNS_advance_pc
);
1869 saa_wleb128u(plinep
,aa
);
1872 dwarf_csect
->line
= currentline
;
1873 dwarf_csect
->offset
= s
->offset
;
1876 /* show change handled */
1881 static void dwarf64_generate(void)
1885 struct linelist
*ftentry
;
1886 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1887 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1888 struct sectlist
*psect
;
1889 size_t saalen
, linepoff
, totlen
, highaddr
;
1891 /* write epilogues for each line program range */
1892 /* and build aranges section */
1893 paranges
= saa_init(1L);
1894 parangesrel
= saa_init(1L);
1895 saa_write16(paranges
,3); /* dwarf version */
1896 saa_write64(parangesrel
, paranges
->datalen
+4);
1897 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1898 saa_write64(parangesrel
, 0);
1899 saa_write32(paranges
,0); /* offset into info */
1900 saa_write8(paranges
,8); /* pointer size */
1901 saa_write8(paranges
,0); /* not segmented */
1902 saa_write32(paranges
,0); /* padding */
1903 /* iterate though sectlist entries */
1904 psect
= dwarf_fsect
;
1907 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1909 plinep
= psect
->psaa
;
1910 /* Line Number Program Epilogue */
1911 saa_write8(plinep
,2); /* std op 2 */
1912 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1913 saa_write8(plinep
,DW_LNS_extended_op
);
1914 saa_write8(plinep
,1); /* operand length */
1915 saa_write8(plinep
,DW_LNE_end_sequence
);
1916 totlen
+= plinep
->datalen
;
1917 /* range table relocation entry */
1918 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1919 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1920 saa_write64(parangesrel
, (uint64_t) 0);
1921 /* range table entry */
1922 saa_write64(paranges
,0x0000); /* range start */
1923 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1924 highaddr
+= sects
[psect
->section
]->len
;
1925 /* done with this entry */
1926 psect
= psect
->next
;
1928 saa_write64(paranges
,0); /* null address */
1929 saa_write64(paranges
,0); /* null length */
1930 saalen
= paranges
->datalen
;
1931 arangeslen
= saalen
+ 4;
1932 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1933 WRITELONG(pbuf
,saalen
); /* initial length */
1934 saa_rnbytes(paranges
, pbuf
, saalen
);
1937 /* build rela.aranges section */
1938 arangesrellen
= saalen
= parangesrel
->datalen
;
1939 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1940 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1941 saa_free(parangesrel
);
1943 /* build pubnames section */
1944 ppubnames
= saa_init(1L);
1945 saa_write16(ppubnames
,3); /* dwarf version */
1946 saa_write32(ppubnames
,0); /* offset into info */
1947 saa_write32(ppubnames
,0); /* space used in info */
1948 saa_write32(ppubnames
,0); /* end of list */
1949 saalen
= ppubnames
->datalen
;
1950 pubnameslen
= saalen
+ 4;
1951 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1952 WRITELONG(pbuf
,saalen
); /* initial length */
1953 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1954 saa_free(ppubnames
);
1956 /* build info section */
1957 pinfo
= saa_init(1L);
1958 pinforel
= saa_init(1L);
1959 saa_write16(pinfo
,3); /* dwarf version */
1960 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1961 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
1962 saa_write64(pinforel
, 0);
1963 saa_write32(pinfo
,0); /* offset into abbrev */
1964 saa_write8(pinfo
,8); /* pointer size */
1965 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1966 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1967 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1968 saa_write64(pinforel
, 0);
1969 saa_write64(pinfo
,0); /* DW_AT_low_pc */
1970 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1971 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1972 saa_write64(pinforel
, 0);
1973 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
1974 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1975 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
1976 saa_write64(pinforel
, 0);
1977 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1978 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1979 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1980 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1981 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1982 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1983 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1984 saa_write64(pinforel
, 0);
1985 saa_write64(pinfo
,0); /* DW_AT_low_pc */
1986 saa_write64(pinfo
,0); /* DW_AT_frame_base */
1987 saa_write8(pinfo
,0); /* end of entries */
1988 saalen
= pinfo
->datalen
;
1989 infolen
= saalen
+ 4;
1990 infobuf
= pbuf
= nasm_malloc(infolen
);
1991 WRITELONG(pbuf
,saalen
); /* initial length */
1992 saa_rnbytes(pinfo
, pbuf
, saalen
);
1995 /* build rela.info section */
1996 inforellen
= saalen
= pinforel
->datalen
;
1997 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1998 saa_rnbytes(pinforel
, pbuf
, saalen
);
2001 /* build abbrev section */
2002 pabbrev
= saa_init(1L);
2003 saa_write8(pabbrev
,1); /* entry number LEB128u */
2004 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2005 saa_write8(pabbrev
,1); /* has children */
2006 /* the following attributes and forms are all LEB128u values */
2007 saa_write8(pabbrev
,DW_AT_low_pc
);
2008 saa_write8(pabbrev
,DW_FORM_addr
);
2009 saa_write8(pabbrev
,DW_AT_high_pc
);
2010 saa_write8(pabbrev
,DW_FORM_addr
);
2011 saa_write8(pabbrev
,DW_AT_stmt_list
);
2012 saa_write8(pabbrev
,DW_FORM_data4
);
2013 saa_write8(pabbrev
,DW_AT_name
);
2014 saa_write8(pabbrev
,DW_FORM_string
);
2015 saa_write8(pabbrev
,DW_AT_producer
);
2016 saa_write8(pabbrev
,DW_FORM_string
);
2017 saa_write8(pabbrev
,DW_AT_language
);
2018 saa_write8(pabbrev
,DW_FORM_data2
);
2019 saa_write16(pabbrev
,0); /* end of entry */
2020 /* LEB128u usage same as above */
2021 saa_write8(pabbrev
,2); /* entry number */
2022 saa_write8(pabbrev
,DW_TAG_subprogram
);
2023 saa_write8(pabbrev
,0); /* no children */
2024 saa_write8(pabbrev
,DW_AT_low_pc
);
2025 saa_write8(pabbrev
,DW_FORM_addr
);
2026 saa_write8(pabbrev
,DW_AT_frame_base
);
2027 saa_write8(pabbrev
,DW_FORM_data4
);
2028 saa_write16(pabbrev
,0); /* end of entry */
2029 abbrevlen
= saalen
= pabbrev
->datalen
;
2030 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2031 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2034 /* build line section */
2036 plines
= saa_init(1L);
2037 saa_write8(plines
,1); /* Minimum Instruction Length */
2038 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2039 saa_write8(plines
,line_base
); /* Line Base */
2040 saa_write8(plines
,line_range
); /* Line Range */
2041 saa_write8(plines
,opcode_base
); /* Opcode Base */
2042 /* standard opcode lengths (# of LEB128u operands) */
2043 saa_write8(plines
,0); /* Std opcode 1 length */
2044 saa_write8(plines
,1); /* Std opcode 2 length */
2045 saa_write8(plines
,1); /* Std opcode 3 length */
2046 saa_write8(plines
,1); /* Std opcode 4 length */
2047 saa_write8(plines
,1); /* Std opcode 5 length */
2048 saa_write8(plines
,0); /* Std opcode 6 length */
2049 saa_write8(plines
,0); /* Std opcode 7 length */
2050 saa_write8(plines
,0); /* Std opcode 8 length */
2051 saa_write8(plines
,1); /* Std opcode 9 length */
2052 saa_write8(plines
,0); /* Std opcode 10 length */
2053 saa_write8(plines
,0); /* Std opcode 11 length */
2054 saa_write8(plines
,1); /* Std opcode 12 length */
2055 /* Directory Table */
2056 saa_write8(plines
,0); /* End of table */
2057 /* File Name Table */
2058 ftentry
= dwarf_flist
;
2059 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2061 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2062 saa_write8(plines
,0); /* directory LEB128u */
2063 saa_write8(plines
,0); /* time LEB128u */
2064 saa_write8(plines
,0); /* size LEB128u */
2065 ftentry
= ftentry
->next
;
2067 saa_write8(plines
,0); /* End of table */
2068 linepoff
= plines
->datalen
;
2069 linelen
= linepoff
+ totlen
+ 10;
2070 linebuf
= pbuf
= nasm_malloc(linelen
);
2071 WRITELONG(pbuf
,linelen
-4); /* initial length */
2072 WRITESHORT(pbuf
,3); /* dwarf version */
2073 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2074 /* write line header */
2076 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2079 /* concatonate line program ranges */
2081 plinesrel
= saa_init(1L);
2082 psect
= dwarf_fsect
;
2083 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2084 saa_write64(plinesrel
, linepoff
);
2085 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2086 saa_write64(plinesrel
, (uint64_t) 0);
2087 plinep
= psect
->psaa
;
2088 saalen
= plinep
->datalen
;
2089 saa_rnbytes(plinep
, pbuf
, saalen
);
2093 /* done with this entry */
2094 psect
= psect
->next
;
2098 /* build rela.lines section */
2099 linerellen
=saalen
= plinesrel
->datalen
;
2100 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2101 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2102 saa_free(plinesrel
);
2104 /* build frame section */
2106 framebuf
= pbuf
= nasm_malloc(framelen
);
2107 WRITELONG(pbuf
,framelen
-4); /* initial length */
2109 /* build loc section */
2111 locbuf
= pbuf
= nasm_malloc(loclen
);
2112 WRITEDLONG(pbuf
,0); /* null beginning offset */
2113 WRITEDLONG(pbuf
,0); /* null ending offset */
2116 static void dwarf64_cleanup(void)
2118 nasm_free(arangesbuf
);
2119 nasm_free(arangesrelbuf
);
2120 nasm_free(pubnamesbuf
);
2122 nasm_free(inforelbuf
);
2123 nasm_free(abbrevbuf
);
2125 nasm_free(linerelbuf
);
2126 nasm_free(framebuf
);
2130 static void dwarf64_findfile(const char * fname
)
2133 struct linelist
*match
;
2135 /* return if fname is current file name */
2136 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2139 /* search for match */
2142 match
= dwarf_flist
;
2143 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2144 if (!(strcmp(fname
, match
->filename
))) {
2145 dwarf_clist
= match
;
2151 /* add file name to end of list */
2152 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2154 dwarf_clist
->line
= dwarf_numfiles
;
2155 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2156 strcpy(dwarf_clist
->filename
,fname
);
2157 dwarf_clist
->next
= 0;
2158 if (!dwarf_flist
) { /* if first entry */
2159 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2160 dwarf_clist
->last
= 0;
2161 } else { /* chain to previous entry */
2162 dwarf_elist
->next
= dwarf_clist
;
2163 dwarf_elist
= dwarf_clist
;
2167 static void dwarf64_findsect(const int index
)
2170 struct sectlist
*match
;
2173 /* return if index is current section index */
2174 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2177 /* search for match */
2180 match
= dwarf_fsect
;
2181 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2182 if ((match
->section
== index
)) {
2183 dwarf_csect
= match
;
2186 match
= match
->next
;
2190 /* add entry to end of list */
2191 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2193 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2194 dwarf_csect
->line
= 1;
2195 dwarf_csect
->offset
= 0;
2196 dwarf_csect
->file
= 1;
2197 dwarf_csect
->section
= index
;
2198 dwarf_csect
->next
= 0;
2199 /* set relocatable address at start of line program */
2200 saa_write8(plinep
,DW_LNS_extended_op
);
2201 saa_write8(plinep
,9); /* operand length */
2202 saa_write8(plinep
,DW_LNE_set_address
);
2203 saa_write64(plinep
,0); /* Start Address */
2205 if (!dwarf_fsect
) { /* if first entry */
2206 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2207 dwarf_csect
->last
= 0;
2208 } else { /* chain to previous entry */
2209 dwarf_esect
->next
= dwarf_csect
;
2210 dwarf_esect
= dwarf_csect
;