1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 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
)
729 static struct symlininfo sinfo
;
733 #if defined(DEBUG) && DEBUG>2
735 nasm_error(ERR_DEBUG
,
736 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
737 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
739 nasm_error(ERR_DEBUG
,
740 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
741 currentline
, type
, segment
, segto
, size
);
745 * handle absolute-assembly (structure definitions)
747 if (segto
== NO_SEG
) {
748 if (type
!= OUT_RESERVE
)
749 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
755 for (i
= 0; i
< nsects
; i
++)
756 if (segto
== sects
[i
]->index
) {
761 int tempint
; /* ignored */
762 if (segto
!= elf_section_names(".text", 2, &tempint
))
763 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
765 s
= sects
[nsects
- 1];
770 /* again some stabs debugging stuff */
771 if (of_elf64
.current_dfmt
) {
772 sinfo
.offset
= s
->len
;
775 sinfo
.name
= s
->name
;
776 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
778 /* end of debugging stuff */
780 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
781 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
782 " BSS section `%s': ignored", s
->name
);
783 s
->len
+= realsize(type
, size
);
789 if (s
->type
== SHT_PROGBITS
) {
790 nasm_error(ERR_WARNING
, "uninitialized space declared in"
791 " non-BSS section `%s': zeroing", s
->name
);
792 elf_sect_write(s
, NULL
, size
);
798 if (segment
!= NO_SEG
)
799 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
800 elf_sect_write(s
, data
, size
);
804 addr
= *(int64_t *)data
;
805 if (segment
== NO_SEG
) {
807 } else if (segment
% 2) {
808 nasm_error(ERR_NONFATAL
, "ELF format does not support"
809 " segment base references");
814 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
817 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
820 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
823 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
826 nasm_error(ERR_PANIC
, "internal error elf64-hpa-871");
830 } else if (wrt
== elf_gotpc_sect
+ 1) {
832 * The user will supply GOT relative to $$. ELF
833 * will let us have GOT relative to $. So we
834 * need to fix up the data item by $-$$.
837 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
839 } else if (wrt
== elf_gotoff_sect
+ 1) {
841 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
842 "references to be qword");
844 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
847 } else if (wrt
== elf_got_sect
+ 1) {
850 elf_add_gsym_reloc(s
, segment
, addr
, 0,
851 R_X86_64_GOT32
, true);
855 elf_add_gsym_reloc(s
, segment
, addr
, 0,
856 R_X86_64_GOT64
, true);
860 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
863 } else if (wrt
== elf_sym_sect
+ 1) {
866 elf_add_gsym_reloc(s
, segment
, addr
, 0,
871 elf_add_gsym_reloc(s
, segment
, addr
, 0,
876 elf_add_gsym_reloc(s
, segment
, addr
, 0,
881 elf_add_gsym_reloc(s
, segment
, addr
, 0,
886 nasm_error(ERR_PANIC
, "internal error elf64-hpa-903");
889 } else if (wrt
== elf_plt_sect
+ 1) {
890 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
891 "relative PLT references");
893 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
897 elf_sect_writeaddr(s
, addr
, size
);
901 reltype
= R_X86_64_PC8
;
906 reltype
= R_X86_64_PC16
;
911 addr
= *(int64_t *)data
- size
;
912 if (segment
== segto
)
913 nasm_error(ERR_PANIC
, "intra-segment OUT_REL1ADR");
914 if (segment
== NO_SEG
) {
916 } else if (segment
% 2) {
917 nasm_error(ERR_NONFATAL
, "ELF format does not support"
918 " segment base references");
921 elf_add_reloc(s
, segment
, addr
, reltype
);
924 nasm_error(ERR_NONFATAL
,
925 "Unsupported non-32-bit ELF relocation");
928 elf_sect_writeaddr(s
, addr
, bytes
);
932 addr
= *(int64_t *)data
- size
;
933 if (segment
== segto
)
934 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
935 if (segment
== NO_SEG
) {
937 } else if (segment
% 2) {
938 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
939 " segment base references");
942 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
944 } else if (wrt
== elf_plt_sect
+ 1) {
945 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
946 R_X86_64_PLT32
, true);
948 } else if (wrt
== elf_gotpc_sect
+ 1 ||
949 wrt
== elf_got_sect
+ 1) {
950 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
951 R_X86_64_GOTPCREL
, true);
953 } else if (wrt
== elf_gotoff_sect
+ 1 ||
954 wrt
== elf_got_sect
+ 1) {
955 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
957 } else if (wrt
== elf_gottpoff_sect
+ 1) {
958 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
959 R_X86_64_GOTTPOFF
, true);
962 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
966 elf_sect_writeaddr(s
, addr
, 4);
970 addr
= *(int64_t *)data
- size
;
971 if (segment
== segto
)
972 nasm_error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
973 if (segment
== NO_SEG
) {
975 } else if (segment
% 2) {
976 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
977 " segment base references");
980 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
982 } else if (wrt
== elf_gotpc_sect
+ 1 ||
983 wrt
== elf_got_sect
+ 1) {
984 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
985 R_X86_64_GOTPCREL64
, true);
987 } else if (wrt
== elf_gotoff_sect
+ 1 ||
988 wrt
== elf_got_sect
+ 1) {
989 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
991 } else if (wrt
== elf_gottpoff_sect
+ 1) {
992 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
995 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
999 elf_sect_writeaddr(s
, addr
, 8);
1004 static void elf_write(void)
1011 int32_t symtablen
, symtablocal
;
1014 * Work out how many sections we will have. We have SHN_UNDEF,
1015 * then the flexible user sections, then the fixed sections
1016 * `.shstrtab', `.symtab' and `.strtab', then optionally
1017 * relocation sections for the user sections.
1019 nsections
= sec_numspecial
+ 1;
1020 if (of_elf64
.current_dfmt
== &df_stabs
)
1022 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1025 add_sectname("", ".shstrtab");
1026 add_sectname("", ".symtab");
1027 add_sectname("", ".strtab");
1028 for (i
= 0; i
< nsects
; i
++) {
1029 nsections
++; /* for the section itself */
1030 if (sects
[i
]->head
) {
1031 nsections
++; /* for its relocations */
1032 add_sectname(".rela", sects
[i
]->name
);
1036 if (of_elf64
.current_dfmt
== &df_stabs
) {
1037 /* in case the debug information is wanted, just add these three sections... */
1038 add_sectname("", ".stab");
1039 add_sectname("", ".stabstr");
1040 add_sectname(".rel", ".stab");
1043 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1044 /* the dwarf debug standard specifies the following ten sections,
1045 not all of which are currently implemented,
1046 although all of them are defined. */
1047 #define debug_aranges (int64_t) (nsections-10)
1048 #define debug_info (int64_t) (nsections-7)
1049 #define debug_abbrev (int64_t) (nsections-5)
1050 #define debug_line (int64_t) (nsections-4)
1051 add_sectname("", ".debug_aranges");
1052 add_sectname(".rela", ".debug_aranges");
1053 add_sectname("", ".debug_pubnames");
1054 add_sectname("", ".debug_info");
1055 add_sectname(".rela", ".debug_info");
1056 add_sectname("", ".debug_abbrev");
1057 add_sectname("", ".debug_line");
1058 add_sectname(".rela", ".debug_line");
1059 add_sectname("", ".debug_frame");
1060 add_sectname("", ".debug_loc");
1064 * Output the ELF header.
1066 fwrite("\177ELF\2\1\1", 7, 1, ofile
);
1067 fputc(elf_osabi
, ofile
);
1068 fputc(elf_abiver
, ofile
);
1069 fwritezero(7, ofile
);
1070 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1071 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1072 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1073 fwriteint64_t(0L, ofile
); /* no entry point */
1074 fwriteint64_t(0L, ofile
); /* no program header table */
1075 fwriteint64_t(0x40L
, ofile
); /* section headers straight after
1076 * ELF header plus alignment */
1077 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
1078 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1079 fwriteint16_t(0, ofile
); /* no program header table, again */
1080 fwriteint16_t(0, ofile
); /* still no program header table */
1081 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1082 fwriteint16_t(nsections
, ofile
); /* number of sections */
1083 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1084 * section header table */
1087 * Build the symbol table and relocation tables.
1089 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1090 for (i
= 0; i
< nsects
; i
++)
1092 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1096 * Now output the section header table.
1099 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1100 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1103 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1106 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1109 /* The normal sections */
1110 for (i
= 0; i
< nsects
; i
++) {
1111 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1112 (sects
[i
]->type
== SHT_PROGBITS
?
1113 sects
[i
]->data
: NULL
), true,
1114 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1119 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1120 shstrtablen
, 0, 0, 1, 0);
1124 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1125 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1129 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1130 strslen
, 0, 0, 1, 0);
1133 /* The relocation sections */
1134 for (i
= 0; i
< nsects
; i
++)
1135 if (sects
[i
]->head
) {
1136 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1137 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1141 if (of_elf64
.current_dfmt
== &df_stabs
) {
1142 /* for debugging information, create the last three sections
1143 which are the .stab , .stabstr and .rel.stab sections respectively */
1145 /* this function call creates the stab sections in memory */
1148 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1149 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1150 stablen
, sec_stabstr
, 0, 4, 12);
1153 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1154 stabstrlen
, 0, 0, 4, 0);
1157 /* link -> symtable info -> section to refer to */
1158 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1159 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1162 } else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1163 /* for dwarf debugging information, create the ten dwarf sections */
1165 /* this function call creates the dwarf sections in memory */
1169 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1170 arangeslen
, 0, 0, 1, 0);
1173 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1174 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1177 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1178 pubnameslen
, 0, 0, 1, 0);
1181 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1182 infolen
, 0, 0, 1, 0);
1185 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1186 inforellen
, symtabsection
, debug_info
, 1, 24);
1189 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1190 abbrevlen
, 0, 0, 1, 0);
1193 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1194 linelen
, 0, 0, 1, 0);
1197 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1198 linerellen
, symtabsection
, debug_line
, 1, 24);
1201 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1202 framelen
, 0, 0, 8, 0);
1205 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1206 loclen
, 0, 0, 1, 0);
1209 fwritezero(align
, ofile
);
1212 * Now output the sections.
1214 elf_write_sections();
1216 nasm_free(elf_sects
);
1220 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1222 struct SAA
*s
= saa_init(1L);
1224 uint8_t entry
[24], *p
;
1230 * First, an all-zeros entry, required by the ELF spec.
1232 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1237 * Next, an entry for the file name.
1240 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1241 WRITESHORT(p
, STT_FILE
); /* type FILE */
1242 WRITESHORT(p
, SHN_ABS
);
1243 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1244 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1245 saa_wbytes(s
, entry
, 24L);
1250 * Now some standard symbols defining the segments, for relocation
1253 for (i
= 1; i
<= nsects
; i
++) {
1255 WRITELONG(p
, 0); /* no symbol name */
1256 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1257 WRITESHORT(p
, i
); /* section id */
1258 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1259 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1260 saa_wbytes(s
, entry
, 24L);
1267 * Now the other local symbols.
1270 while ((sym
= saa_rstruct(syms
))) {
1271 if (sym
->type
& SYM_GLOBAL
)
1274 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1275 WRITECHAR(p
, sym
->type
); /* type and binding */
1276 WRITECHAR(p
, sym
->other
); /* visibility */
1277 WRITESHORT(p
, sym
->section
); /* index into section header table */
1278 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1279 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1280 saa_wbytes(s
, entry
, 24L);
1285 * dwarf needs symbols for debug sections
1286 * which are relocation targets.
1288 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1289 dwarf_infosym
= *local
;
1291 WRITELONG(p
, 0); /* no symbol name */
1292 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1293 WRITESHORT(p
, debug_info
); /* section id */
1294 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1295 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1296 saa_wbytes(s
, entry
, 24L);
1299 dwarf_abbrevsym
= *local
;
1301 WRITELONG(p
, 0); /* no symbol name */
1302 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1303 WRITESHORT(p
, debug_abbrev
); /* section id */
1304 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1305 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1306 saa_wbytes(s
, entry
, 24L);
1309 dwarf_linesym
= *local
;
1311 WRITELONG(p
, 0); /* no symbol name */
1312 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1313 WRITESHORT(p
, debug_line
); /* section id */
1314 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1315 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1316 saa_wbytes(s
, entry
, 24L);
1322 * Now the global symbols.
1325 while ((sym
= saa_rstruct(syms
))) {
1326 if (!(sym
->type
& SYM_GLOBAL
))
1329 WRITELONG(p
, sym
->strpos
);
1330 WRITECHAR(p
, sym
->type
); /* type and binding */
1331 WRITECHAR(p
, sym
->other
); /* visibility */
1332 WRITESHORT(p
, sym
->section
);
1333 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1334 WRITEDLONG(p
, (int64_t)sym
->size
);
1335 saa_wbytes(s
, entry
, 24L);
1342 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1345 uint8_t *p
, entry
[24];
1346 int32_t global_offset
;
1355 * How to onvert from a global placeholder to a real symbol index;
1356 * the +2 refers to the two special entries, the null entry and
1357 * the filename entry.
1359 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1362 int32_t sym
= r
->symbol
;
1364 if (sym
>= GLOBAL_TEMP_BASE
)
1365 sym
+= global_offset
;
1368 WRITEDLONG(p
, r
->address
);
1369 WRITELONG(p
, r
->type
);
1371 WRITEDLONG(p
, r
->offset
);
1372 saa_wbytes(s
, entry
, 24L);
1381 static void elf_section_header(int name
, int type
, uint64_t flags
,
1382 void *data
, bool is_saa
, uint64_t datalen
,
1383 int link
, int info
, int align
, int eltsize
)
1385 elf_sects
[elf_nsect
].data
= data
;
1386 elf_sects
[elf_nsect
].len
= datalen
;
1387 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1390 fwriteint32_t((int32_t)name
, ofile
);
1391 fwriteint32_t((int32_t)type
, ofile
);
1392 fwriteint64_t((int64_t)flags
, ofile
);
1393 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
1394 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1395 fwriteint64_t(datalen
, ofile
);
1397 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1398 fwriteint32_t((int32_t)link
, ofile
);
1399 fwriteint32_t((int32_t)info
, ofile
);
1400 fwriteint64_t((int64_t)align
, ofile
);
1401 fwriteint64_t((int64_t)eltsize
, ofile
);
1404 static void elf_write_sections(void)
1407 for (i
= 0; i
< elf_nsect
; i
++)
1408 if (elf_sects
[i
].data
) {
1409 int32_t len
= elf_sects
[i
].len
;
1410 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1411 int32_t align
= reallen
- len
;
1412 if (elf_sects
[i
].is_saa
)
1413 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1415 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1416 fwritezero(align
, ofile
);
1420 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1422 saa_wbytes(sect
->data
, data
, len
);
1425 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1427 saa_writeaddr(sect
->data
, data
, len
);
1431 static void elf_sectalign(int32_t seg
, unsigned int value
)
1433 struct Section
*s
= NULL
;
1436 for (i
= 0; i
< nsects
; i
++) {
1437 if (sects
[i
]->index
== seg
) {
1442 if (!s
|| !is_power2(value
))
1445 if (value
> s
->align
)
1449 static int32_t elf_segbase(int32_t segment
)
1454 static int elf_directive(enum directives directive
, char *value
, int pass
)
1460 switch (directive
) {
1463 return 1; /* ignore in pass 2 */
1465 n
= readnum(value
, &err
);
1467 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1470 if (n
< 0 || n
> 255) {
1471 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1477 if ((p
= strchr(value
,',')) == NULL
)
1480 n
= readnum(p
+1, &err
);
1481 if (err
|| n
< 0 || n
> 255) {
1482 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1494 static void elf_filename(char *inname
, char *outname
)
1496 strcpy(elf_module
, inname
);
1497 standard_extension(inname
, outname
, ".o");
1500 extern macros_t elf_stdmac
[];
1502 static int elf_set_info(enum geninfo type
, char **val
)
1508 static struct dfmt df_dwarf
= {
1509 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1519 static struct dfmt df_stabs
= {
1520 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1531 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1533 struct ofmt of_elf64
= {
1534 "ELF64 (x86_64) object files (e.g. Linux)",
1552 /* common debugging routines */
1553 static void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
,
1554 int is_global
, char *special
)
1563 static void debug64_directive(const char *directive
, const char *params
)
1569 static void debug64_typevalue(int32_t type
)
1571 int32_t stype
, ssize
;
1572 switch (TYM_TYPE(type
)) {
1615 stype
= STT_SECTION
;
1630 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1631 lastsym
->size
= ssize
;
1632 lastsym
->type
= stype
;
1636 /* stabs debugging routines */
1638 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1641 if (!stabs_filename
) {
1642 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1643 strcpy(stabs_filename
, filename
);
1645 if (strcmp(stabs_filename
, filename
)) {
1646 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1647 in fact, this leak comes in quite handy to maintain a list of files
1648 encountered so far in the symbol lines... */
1650 /* why not nasm_free(stabs_filename); we're done with the old one */
1652 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1653 strcpy(stabs_filename
, filename
);
1657 currentline
= linenumber
;
1661 static void stabs64_output(int type
, void *param
)
1663 struct symlininfo
*s
;
1664 struct linelist
*el
;
1665 if (type
== TY_DEBUGSYMLIN
) {
1666 if (debug_immcall
) {
1667 s
= (struct symlininfo
*)param
;
1668 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1669 return; /* line info is only collected for executable sections */
1671 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1672 el
->info
.offset
= s
->offset
;
1673 el
->info
.section
= s
->section
;
1674 el
->info
.name
= s
->name
;
1675 el
->line
= currentline
;
1676 el
->filename
= stabs_filename
;
1679 stabslines
->last
->next
= el
;
1680 stabslines
->last
= el
;
1683 stabslines
->last
= el
;
1690 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1692 static void stabs64_generate(void)
1694 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1695 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1699 struct linelist
*ptr
;
1703 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1704 for (i
= 0; i
< numlinestabs
; i
++)
1708 if (numfiles
== 0) {
1709 allfiles
[0] = ptr
->filename
;
1712 for (i
= 0; i
< numfiles
; i
++) {
1713 if (!strcmp(allfiles
[i
], ptr
->filename
))
1716 if (i
>= numfiles
) {
1717 allfiles
[i
] = ptr
->filename
;
1724 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1725 for (i
= 0; i
< numfiles
; i
++) {
1726 fileidx
[i
] = strsize
;
1727 strsize
+= strlen(allfiles
[i
]) + 1;
1730 for (i
= 0; i
< numfiles
; i
++) {
1731 if (!strcmp(allfiles
[i
], elf_module
)) {
1738 * worst case size of the stab buffer would be:
1739 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1740 * plus one "ending" entry
1742 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1743 sizeof(struct stabentry
));
1744 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1745 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1748 for (i
= 0; i
< numfiles
; i
++)
1749 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1752 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1760 * this is the first stab, its strx points to the filename of the
1761 * the source-file, the n_desc field should be set to the number
1762 * of remaining stabs
1764 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1766 /* this is the stab for the main source file */
1767 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1769 /* relocation table entry */
1772 * Since the symbol table has two entries before
1773 * the section symbols, the index in the info.section
1774 * member must be adjusted by adding 2
1777 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1778 WRITELONG(rptr
, R_X86_64_32
);
1779 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1782 currfile
= mainfileindex
;
1786 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1787 /* oops file has changed... */
1788 for (i
= 0; i
< numfiles
; i
++)
1789 if (!strcmp(allfiles
[i
], ptr
->filename
))
1792 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1796 /* relocation table entry */
1798 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1799 WRITELONG(rptr
, R_X86_64_32
);
1800 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1803 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1806 /* relocation table entry */
1808 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1809 WRITELONG(rptr
, R_X86_64_32
);
1810 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1816 /* this is an "ending" token */
1817 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1820 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1822 nasm_free(allfiles
);
1825 stablen
= (sptr
- sbuf
);
1826 stabrellen
= (rptr
- rbuf
);
1832 static void stabs64_cleanup(void)
1834 struct linelist
*ptr
, *del
;
1846 nasm_free(stabrelbuf
);
1847 nasm_free(stabstrbuf
);
1850 /* dwarf routines */
1852 static void dwarf64_init(void)
1854 ndebugs
= 3; /* 3 debug symbols */
1857 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1861 dwarf64_findfile(filename
);
1863 currentline
= linenumber
;
1866 /* called from elf_out with type == TY_DEBUGSYMLIN */
1867 static void dwarf64_output(int type
, void *param
)
1869 int ln
, aa
, inx
, maxln
, soc
;
1870 struct symlininfo
*s
;
1875 s
= (struct symlininfo
*)param
;
1877 /* line number info is only gathered for executable sections */
1878 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1881 /* Check if section index has changed */
1882 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1883 dwarf64_findsect(s
->section
);
1885 /* do nothing unless line or file has changed */
1889 ln
= currentline
- dwarf_csect
->line
;
1890 aa
= s
->offset
- dwarf_csect
->offset
;
1891 inx
= dwarf_clist
->line
;
1892 plinep
= dwarf_csect
->psaa
;
1893 /* check for file change */
1894 if (!(inx
== dwarf_csect
->file
)) {
1895 saa_write8(plinep
,DW_LNS_set_file
);
1896 saa_write8(plinep
,inx
);
1897 dwarf_csect
->file
= inx
;
1899 /* check for line change */
1901 /* test if in range of special op code */
1902 maxln
= line_base
+ line_range
;
1903 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1904 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1905 saa_write8(plinep
,soc
);
1907 saa_write8(plinep
,DW_LNS_advance_line
);
1908 saa_wleb128s(plinep
,ln
);
1910 saa_write8(plinep
,DW_LNS_advance_pc
);
1911 saa_wleb128u(plinep
,aa
);
1914 dwarf_csect
->line
= currentline
;
1915 dwarf_csect
->offset
= s
->offset
;
1918 /* show change handled */
1923 static void dwarf64_generate(void)
1927 struct linelist
*ftentry
;
1928 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1929 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1930 struct sectlist
*psect
;
1931 size_t saalen
, linepoff
, totlen
, highaddr
;
1933 /* write epilogues for each line program range */
1934 /* and build aranges section */
1935 paranges
= saa_init(1L);
1936 parangesrel
= saa_init(1L);
1937 saa_write16(paranges
,3); /* dwarf version */
1938 saa_write64(parangesrel
, paranges
->datalen
+4);
1939 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1940 saa_write64(parangesrel
, 0);
1941 saa_write32(paranges
,0); /* offset into info */
1942 saa_write8(paranges
,8); /* pointer size */
1943 saa_write8(paranges
,0); /* not segmented */
1944 saa_write32(paranges
,0); /* padding */
1945 /* iterate though sectlist entries */
1946 psect
= dwarf_fsect
;
1949 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1951 plinep
= psect
->psaa
;
1952 /* Line Number Program Epilogue */
1953 saa_write8(plinep
,2); /* std op 2 */
1954 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1955 saa_write8(plinep
,DW_LNS_extended_op
);
1956 saa_write8(plinep
,1); /* operand length */
1957 saa_write8(plinep
,DW_LNE_end_sequence
);
1958 totlen
+= plinep
->datalen
;
1959 /* range table relocation entry */
1960 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1961 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1962 saa_write64(parangesrel
, (uint64_t) 0);
1963 /* range table entry */
1964 saa_write64(paranges
,0x0000); /* range start */
1965 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1966 highaddr
+= sects
[psect
->section
]->len
;
1967 /* done with this entry */
1968 psect
= psect
->next
;
1970 saa_write64(paranges
,0); /* null address */
1971 saa_write64(paranges
,0); /* null length */
1972 saalen
= paranges
->datalen
;
1973 arangeslen
= saalen
+ 4;
1974 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1975 WRITELONG(pbuf
,saalen
); /* initial length */
1976 saa_rnbytes(paranges
, pbuf
, saalen
);
1979 /* build rela.aranges section */
1980 arangesrellen
= saalen
= parangesrel
->datalen
;
1981 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1982 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1983 saa_free(parangesrel
);
1985 /* build pubnames section */
1986 ppubnames
= saa_init(1L);
1987 saa_write16(ppubnames
,3); /* dwarf version */
1988 saa_write32(ppubnames
,0); /* offset into info */
1989 saa_write32(ppubnames
,0); /* space used in info */
1990 saa_write32(ppubnames
,0); /* end of list */
1991 saalen
= ppubnames
->datalen
;
1992 pubnameslen
= saalen
+ 4;
1993 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1994 WRITELONG(pbuf
,saalen
); /* initial length */
1995 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1996 saa_free(ppubnames
);
1998 /* build info section */
1999 pinfo
= saa_init(1L);
2000 pinforel
= saa_init(1L);
2001 saa_write16(pinfo
,3); /* dwarf version */
2002 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2003 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2004 saa_write64(pinforel
, 0);
2005 saa_write32(pinfo
,0); /* offset into abbrev */
2006 saa_write8(pinfo
,8); /* pointer size */
2007 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2008 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2009 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2010 saa_write64(pinforel
, 0);
2011 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2012 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2013 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2014 saa_write64(pinforel
, 0);
2015 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2016 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2017 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2018 saa_write64(pinforel
, 0);
2019 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2020 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2021 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2022 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2023 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2024 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2025 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2026 saa_write64(pinforel
, 0);
2027 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2028 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2029 saa_write8(pinfo
,0); /* end of entries */
2030 saalen
= pinfo
->datalen
;
2031 infolen
= saalen
+ 4;
2032 infobuf
= pbuf
= nasm_malloc(infolen
);
2033 WRITELONG(pbuf
,saalen
); /* initial length */
2034 saa_rnbytes(pinfo
, pbuf
, saalen
);
2037 /* build rela.info section */
2038 inforellen
= saalen
= pinforel
->datalen
;
2039 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2040 saa_rnbytes(pinforel
, pbuf
, saalen
);
2043 /* build abbrev section */
2044 pabbrev
= saa_init(1L);
2045 saa_write8(pabbrev
,1); /* entry number LEB128u */
2046 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2047 saa_write8(pabbrev
,1); /* has children */
2048 /* the following attributes and forms are all LEB128u values */
2049 saa_write8(pabbrev
,DW_AT_low_pc
);
2050 saa_write8(pabbrev
,DW_FORM_addr
);
2051 saa_write8(pabbrev
,DW_AT_high_pc
);
2052 saa_write8(pabbrev
,DW_FORM_addr
);
2053 saa_write8(pabbrev
,DW_AT_stmt_list
);
2054 saa_write8(pabbrev
,DW_FORM_data4
);
2055 saa_write8(pabbrev
,DW_AT_name
);
2056 saa_write8(pabbrev
,DW_FORM_string
);
2057 saa_write8(pabbrev
,DW_AT_producer
);
2058 saa_write8(pabbrev
,DW_FORM_string
);
2059 saa_write8(pabbrev
,DW_AT_language
);
2060 saa_write8(pabbrev
,DW_FORM_data2
);
2061 saa_write16(pabbrev
,0); /* end of entry */
2062 /* LEB128u usage same as above */
2063 saa_write8(pabbrev
,2); /* entry number */
2064 saa_write8(pabbrev
,DW_TAG_subprogram
);
2065 saa_write8(pabbrev
,0); /* no children */
2066 saa_write8(pabbrev
,DW_AT_low_pc
);
2067 saa_write8(pabbrev
,DW_FORM_addr
);
2068 saa_write8(pabbrev
,DW_AT_frame_base
);
2069 saa_write8(pabbrev
,DW_FORM_data4
);
2070 saa_write16(pabbrev
,0); /* end of entry */
2071 abbrevlen
= saalen
= pabbrev
->datalen
;
2072 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2073 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2076 /* build line section */
2078 plines
= saa_init(1L);
2079 saa_write8(plines
,1); /* Minimum Instruction Length */
2080 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2081 saa_write8(plines
,line_base
); /* Line Base */
2082 saa_write8(plines
,line_range
); /* Line Range */
2083 saa_write8(plines
,opcode_base
); /* Opcode Base */
2084 /* standard opcode lengths (# of LEB128u operands) */
2085 saa_write8(plines
,0); /* Std opcode 1 length */
2086 saa_write8(plines
,1); /* Std opcode 2 length */
2087 saa_write8(plines
,1); /* Std opcode 3 length */
2088 saa_write8(plines
,1); /* Std opcode 4 length */
2089 saa_write8(plines
,1); /* Std opcode 5 length */
2090 saa_write8(plines
,0); /* Std opcode 6 length */
2091 saa_write8(plines
,0); /* Std opcode 7 length */
2092 saa_write8(plines
,0); /* Std opcode 8 length */
2093 saa_write8(plines
,1); /* Std opcode 9 length */
2094 saa_write8(plines
,0); /* Std opcode 10 length */
2095 saa_write8(plines
,0); /* Std opcode 11 length */
2096 saa_write8(plines
,1); /* Std opcode 12 length */
2097 /* Directory Table */
2098 saa_write8(plines
,0); /* End of table */
2099 /* File Name Table */
2100 ftentry
= dwarf_flist
;
2101 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2103 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2104 saa_write8(plines
,0); /* directory LEB128u */
2105 saa_write8(plines
,0); /* time LEB128u */
2106 saa_write8(plines
,0); /* size LEB128u */
2107 ftentry
= ftentry
->next
;
2109 saa_write8(plines
,0); /* End of table */
2110 linepoff
= plines
->datalen
;
2111 linelen
= linepoff
+ totlen
+ 10;
2112 linebuf
= pbuf
= nasm_malloc(linelen
);
2113 WRITELONG(pbuf
,linelen
-4); /* initial length */
2114 WRITESHORT(pbuf
,3); /* dwarf version */
2115 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2116 /* write line header */
2118 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2121 /* concatonate line program ranges */
2123 plinesrel
= saa_init(1L);
2124 psect
= dwarf_fsect
;
2125 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2126 saa_write64(plinesrel
, linepoff
);
2127 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2128 saa_write64(plinesrel
, (uint64_t) 0);
2129 plinep
= psect
->psaa
;
2130 saalen
= plinep
->datalen
;
2131 saa_rnbytes(plinep
, pbuf
, saalen
);
2135 /* done with this entry */
2136 psect
= psect
->next
;
2140 /* build rela.lines section */
2141 linerellen
=saalen
= plinesrel
->datalen
;
2142 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2143 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2144 saa_free(plinesrel
);
2146 /* build frame section */
2148 framebuf
= pbuf
= nasm_malloc(framelen
);
2149 WRITELONG(pbuf
,framelen
-4); /* initial length */
2151 /* build loc section */
2153 locbuf
= pbuf
= nasm_malloc(loclen
);
2154 WRITEDLONG(pbuf
,0); /* null beginning offset */
2155 WRITEDLONG(pbuf
,0); /* null ending offset */
2158 static void dwarf64_cleanup(void)
2160 nasm_free(arangesbuf
);
2161 nasm_free(arangesrelbuf
);
2162 nasm_free(pubnamesbuf
);
2164 nasm_free(inforelbuf
);
2165 nasm_free(abbrevbuf
);
2167 nasm_free(linerelbuf
);
2168 nasm_free(framebuf
);
2172 static void dwarf64_findfile(const char * fname
)
2175 struct linelist
*match
;
2177 /* return if fname is current file name */
2178 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2181 /* search for match */
2184 match
= dwarf_flist
;
2185 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2186 if (!(strcmp(fname
, match
->filename
))) {
2187 dwarf_clist
= match
;
2193 /* add file name to end of list */
2194 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2196 dwarf_clist
->line
= dwarf_numfiles
;
2197 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2198 strcpy(dwarf_clist
->filename
,fname
);
2199 dwarf_clist
->next
= 0;
2200 if (!dwarf_flist
) { /* if first entry */
2201 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2202 dwarf_clist
->last
= 0;
2203 } else { /* chain to previous entry */
2204 dwarf_elist
->next
= dwarf_clist
;
2205 dwarf_elist
= dwarf_clist
;
2209 static void dwarf64_findsect(const int index
)
2212 struct sectlist
*match
;
2215 /* return if index is current section index */
2216 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2219 /* search for match */
2222 match
= dwarf_fsect
;
2223 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2224 if ((match
->section
== index
)) {
2225 dwarf_csect
= match
;
2228 match
= match
->next
;
2232 /* add entry to end of list */
2233 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2235 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2236 dwarf_csect
->line
= 1;
2237 dwarf_csect
->offset
= 0;
2238 dwarf_csect
->file
= 1;
2239 dwarf_csect
->section
= index
;
2240 dwarf_csect
->next
= 0;
2241 /* set relocatable address at start of line program */
2242 saa_write8(plinep
,DW_LNS_extended_op
);
2243 saa_write8(plinep
,9); /* operand length */
2244 saa_write8(plinep
,DW_LNE_set_address
);
2245 saa_write64(plinep
,0); /* Start Address */
2247 if (!dwarf_fsect
) { /* if first entry */
2248 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2249 dwarf_csect
->last
= 0;
2250 } else { /* chain to previous entry */
2251 dwarf_esect
->next
= dwarf_csect
;
2252 dwarf_esect
= dwarf_csect
;