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 *);
152 int section
; /* index into sects[] */
153 int segto
; /* internal section number */
154 char *name
; /* shallow-copied pointer of section name */
158 struct symlininfo info
;
161 struct linelist
*next
;
162 struct linelist
*last
;
171 struct sectlist
*next
;
172 struct sectlist
*last
;
175 /* common debug variables */
176 static int currentline
= 1;
177 static int debug_immcall
= 0;
179 /* stabs debug variables */
180 static struct linelist
*stabslines
= 0;
181 static int numlinestabs
= 0;
182 static char *stabs_filename
= 0;
183 static int symtabsection
;
184 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
185 static int stablen
, stabstrlen
, stabrellen
;
187 /* dwarf debug variables */
188 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
189 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
190 static int dwarf_numfiles
= 0, dwarf_nsections
;
191 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
192 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
193 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
194 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
195 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
196 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
199 static struct dfmt df_dwarf
;
200 static struct dfmt df_stabs
;
201 static struct Symbol
*lastsym
;
203 /* common debugging routines */
204 static void debug64_typevalue(int32_t);
205 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
206 static void debug64_directive(const char *, const char *);
208 /* stabs debugging routines */
209 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
210 static void stabs64_output(int, void *);
211 static void stabs64_generate(void);
212 static void stabs64_cleanup(void);
214 /* dwarf debugging routines */
215 static void dwarf64_init(void);
216 static void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
217 static void dwarf64_output(int, void *);
218 static void dwarf64_generate(void);
219 static void dwarf64_cleanup(void);
220 static void dwarf64_findfile(const char *);
221 static void dwarf64_findsect(const int);
224 * Special section numbers which are used to define ELF special
225 * symbols, which can be used with WRT to provide PIC relocation
228 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
229 static int32_t elf_got_sect
, elf_plt_sect
;
230 static int32_t elf_sym_sect
;
231 static int32_t elf_gottpoff_sect
;
233 static void elf_init(void)
237 nsects
= sectlen
= 0;
238 syms
= saa_init((int32_t)sizeof(struct Symbol
));
239 nlocals
= nglobs
= ndebugs
= 0;
242 saa_wbytes(strs
, "\0", 1L);
243 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
244 strslen
= 2 + strlen(elf_module
);
246 shstrtablen
= shstrtabsize
= 0;;
247 add_sectname("", "");
251 elf_gotpc_sect
= seg_alloc();
252 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
253 elf_gotoff_sect
= seg_alloc();
254 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
255 elf_got_sect
= seg_alloc();
256 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
257 elf_plt_sect
= seg_alloc();
258 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
259 elf_sym_sect
= seg_alloc();
260 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
261 elf_gottpoff_sect
= seg_alloc();
262 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
264 def_seg
= seg_alloc();
268 static void elf_cleanup(int debuginfo
)
276 for (i
= 0; i
< nsects
; i
++) {
277 if (sects
[i
]->type
!= SHT_NOBITS
)
278 saa_free(sects
[i
]->data
);
280 saa_free(sects
[i
]->rel
);
281 while (sects
[i
]->head
) {
283 sects
[i
]->head
= sects
[i
]->head
->next
;
291 if (of_elf64
.current_dfmt
) {
292 of_elf64
.current_dfmt
->cleanup();
296 /* add entry to the elf .shstrtab section */
297 static void add_sectname(char *firsthalf
, char *secondhalf
)
299 int len
= strlen(firsthalf
) + strlen(secondhalf
);
300 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
301 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
302 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
303 strcat(shstrtab
+ shstrtablen
, secondhalf
);
304 shstrtablen
+= len
+ 1;
307 static int elf_make_section(char *name
, int type
, int flags
, int align
)
311 s
= nasm_zalloc(sizeof(*s
));
313 if (type
!= SHT_NOBITS
)
314 s
->data
= saa_init(1L);
316 if (!strcmp(name
, ".text"))
319 s
->index
= seg_alloc();
320 add_sectname("", name
);
322 s
->name
= nasm_strdup(name
);
327 if (nsects
>= sectlen
)
328 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
334 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
337 uint32_t flags
, flags_and
, flags_or
;
342 * Default is 64 bits.
349 p
= nasm_skip_word(name
);
352 flags_and
= flags_or
= type
= align
= 0;
354 section_attrib(name
, p
, pass
, &flags_and
,
355 &flags_or
, &align
, &type
);
357 if (!strcmp(name
, ".shstrtab") ||
358 !strcmp(name
, ".symtab") ||
359 !strcmp(name
, ".strtab")) {
360 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
365 for (i
= 0; i
< nsects
; i
++)
366 if (!strcmp(name
, sects
[i
]->name
))
369 const struct elf_known_section
*ks
= elf_known_sections
;
372 if (!strcmp(name
, ks
->name
))
377 type
= type
? type
: ks
->type
;
378 align
= align
? align
: ks
->align
;
379 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
381 i
= elf_make_section(name
, type
, flags
, align
);
382 } else if (pass
== 1) {
383 if ((type
&& sects
[i
]->type
!= type
)
384 || (align
&& sects
[i
]->align
!= align
)
385 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
386 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
387 " redeclaration of section `%s'", name
);
390 return sects
[i
]->index
;
393 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
394 int is_global
, char *special
)
398 bool special_used
= false;
400 #if defined(DEBUG) && DEBUG>2
401 nasm_error(ERR_DEBUG
,
402 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
403 name
, segment
, offset
, is_global
, special
);
405 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
407 * This is a NASM special symbol. We never allow it into
408 * the ELF symbol table, even if it's a valid one. If it
409 * _isn't_ a valid one, we should barf immediately.
411 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
412 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
413 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
414 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
418 if (is_global
== 3) {
421 * Fix up a forward-reference symbol size from the first
424 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
425 if (!strcmp((*s
)->name
, name
)) {
426 struct tokenval tokval
;
428 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
432 tokval
.t_type
= TOKEN_INVALID
;
433 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
436 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
437 " expression as symbol size");
439 (*s
)->size
= reloc_value(e
);
443 * Remove it from the list of unresolved sizes.
445 nasm_free((*s
)->name
);
449 return; /* it wasn't an important one */
452 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
453 strslen
+= 1 + strlen(name
);
455 lastsym
= sym
= saa_wstruct(syms
);
457 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
460 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
461 sym
->other
= STV_DEFAULT
;
463 if (segment
== NO_SEG
)
464 sym
->section
= SHN_ABS
;
467 sym
->section
= SHN_UNDEF
;
468 if (segment
== def_seg
) {
469 /* we have to be sure at least text section is there */
471 if (segment
!= elf_section_names(".text", 2, &tempint
))
472 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
474 for (i
= 0; i
< nsects
; i
++) {
475 if (segment
== sects
[i
]->index
) {
476 sym
->section
= i
+ 1;
482 if (is_global
== 2) {
485 sym
->section
= SHN_COMMON
;
487 * We have a common variable. Check the special text to see
488 * if it's a valid number and power of two; if so, store it
489 * as the alignment for the common variable.
493 sym
->symv
.key
= readnum(special
, &err
);
495 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
496 " valid number", special
);
497 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
498 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
499 " power of two", special
);
503 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
505 if (sym
->type
== SYM_GLOBAL
) {
507 * If sym->section == SHN_ABS, then the first line of the
508 * else section would cause a core dump, because its a reference
509 * beyond the end of the section array.
510 * This behaviour is exhibited by this code:
513 * To avoid such a crash, such requests are silently discarded.
514 * This may not be the best solution.
516 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
517 bsym
= raa_write(bsym
, segment
, nglobs
);
518 } else if (sym
->section
!= SHN_ABS
) {
520 * This is a global symbol; so we must add it to the rbtree
521 * of global symbols in its section.
523 * In addition, we check the special text for symbol
524 * type and size information.
526 sects
[sym
->section
-1]->gsyms
=
527 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
530 int n
= strcspn(special
, " \t");
532 if (!nasm_strnicmp(special
, "function", n
))
533 sym
->type
|= STT_FUNC
;
534 else if (!nasm_strnicmp(special
, "data", n
) ||
535 !nasm_strnicmp(special
, "object", n
))
536 sym
->type
|= STT_OBJECT
;
537 else if (!nasm_strnicmp(special
, "notype", n
))
538 sym
->type
|= STT_NOTYPE
;
540 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
544 special
= nasm_skip_spaces(special
);
546 n
= strcspn(special
, " \t");
547 if (!nasm_strnicmp(special
, "default", n
))
548 sym
->other
= STV_DEFAULT
;
549 else if (!nasm_strnicmp(special
, "internal", n
))
550 sym
->other
= STV_INTERNAL
;
551 else if (!nasm_strnicmp(special
, "hidden", n
))
552 sym
->other
= STV_HIDDEN
;
553 else if (!nasm_strnicmp(special
, "protected", n
))
554 sym
->other
= STV_PROTECTED
;
561 struct tokenval tokval
;
564 char *saveme
= stdscan_get();
566 while (special
[n
] && nasm_isspace(special
[n
]))
569 * We have a size expression; attempt to
573 stdscan_set(special
+ n
);
574 tokval
.t_type
= TOKEN_INVALID
;
575 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
580 sym
->name
= nasm_strdup(name
);
583 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
584 " expression as symbol size");
586 sym
->size
= reloc_value(e
);
593 * If TLS segment, mark symbol accordingly.
595 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
597 sym
->type
|= STT_TLS
;
600 sym
->globnum
= nglobs
;
605 if (special
&& !special_used
)
606 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
609 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
610 int64_t offset
, int type
)
614 r
= *sect
->tail
= nasm_zalloc(sizeof(struct Reloc
));
615 sect
->tail
= &r
->next
;
617 r
->address
= sect
->len
;
620 if (segment
!= NO_SEG
) {
622 for (i
= 0; i
< nsects
; i
++)
623 if (segment
== sects
[i
]->index
)
626 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
634 * This routine deals with ..got and ..sym relocations: the more
635 * complicated kinds. In shared-library writing, some relocations
636 * with respect to global symbols must refer to the precise symbol
637 * rather than referring to an offset from the base of the section
638 * _containing_ the symbol. Such relocations call to this routine,
639 * which searches the symbol list for the symbol in question.
641 * R_386_GOT32 references require the _exact_ symbol address to be
642 * used; R_386_32 references can be at an offset from the symbol.
643 * The boolean argument `exact' tells us this.
645 * Return value is the adjusted value of `addr', having become an
646 * offset from the symbol rather than the section. Should always be
647 * zero when returning from an exact call.
649 * Limitation: if you define two symbols at the same place,
650 * confusion will occur.
652 * Inefficiency: we search, currently, using a linked list which
653 * isn't even necessarily sorted.
655 static void elf_add_gsym_reloc(struct Section
*sect
,
656 int32_t segment
, uint64_t offset
, int64_t pcrel
,
657 int type
, bool exact
)
666 * First look up the segment/offset pair and find a global
667 * symbol corresponding to it. If it's not one of our segments,
668 * then it must be an external symbol, in which case we're fine
669 * doing a normal elf_add_reloc after first sanity-checking
670 * that the offset from the symbol is zero.
673 for (i
= 0; i
< nsects
; i
++)
674 if (segment
== sects
[i
]->index
) {
681 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
683 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
687 srb
= rb_search(s
->gsyms
, offset
);
688 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
689 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
690 " for this reference");
693 sym
= container_of(srb
, struct Symbol
, symv
);
695 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
696 sect
->tail
= &r
->next
;
699 r
->address
= sect
->len
;
700 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
701 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
707 static void elf_out(int32_t segto
, const void *data
,
708 enum out_type type
, uint64_t size
,
709 int32_t segment
, int32_t wrt
)
715 static struct symlininfo sinfo
;
717 #if defined(DEBUG) && DEBUG>2
719 nasm_error(ERR_DEBUG
,
720 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
721 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
723 nasm_error(ERR_DEBUG
,
724 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
725 currentline
, type
, segment
, segto
, size
);
729 * handle absolute-assembly (structure definitions)
731 if (segto
== NO_SEG
) {
732 if (type
!= OUT_RESERVE
)
733 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
739 for (i
= 0; i
< nsects
; i
++)
740 if (segto
== sects
[i
]->index
) {
745 int tempint
; /* ignored */
746 if (segto
!= elf_section_names(".text", 2, &tempint
))
747 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
749 s
= sects
[nsects
- 1];
754 /* again some stabs debugging stuff */
755 if (of_elf64
.current_dfmt
) {
756 sinfo
.offset
= s
->len
;
759 sinfo
.name
= s
->name
;
760 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
762 /* end of debugging stuff */
764 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
765 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
766 " BSS section `%s': ignored", s
->name
);
767 s
->len
+= realsize(type
, size
);
773 if (s
->type
== SHT_PROGBITS
) {
774 nasm_error(ERR_WARNING
, "uninitialized space declared in"
775 " non-BSS section `%s': zeroing", s
->name
);
776 elf_sect_write(s
, NULL
, size
);
782 if (segment
!= NO_SEG
)
783 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
784 elf_sect_write(s
, data
, size
);
788 addr
= *(int64_t *)data
;
789 if (segment
== NO_SEG
) {
791 } else if (segment
% 2) {
792 nasm_error(ERR_NONFATAL
, "ELF format does not support"
793 " segment base references");
798 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
801 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
804 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
807 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
810 nasm_error(ERR_PANIC
, "internal error elf64-hpa-871");
814 } else if (wrt
== elf_gotpc_sect
+ 1) {
816 * The user will supply GOT relative to $$. ELF
817 * will let us have GOT relative to $. So we
818 * need to fix up the data item by $-$$.
821 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
823 } else if (wrt
== elf_gotoff_sect
+ 1) {
825 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
826 "references to be qword");
828 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
831 } else if (wrt
== elf_got_sect
+ 1) {
834 elf_add_gsym_reloc(s
, segment
, addr
, 0,
835 R_X86_64_GOT32
, true);
839 elf_add_gsym_reloc(s
, segment
, addr
, 0,
840 R_X86_64_GOT64
, true);
844 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
847 } else if (wrt
== elf_sym_sect
+ 1) {
850 elf_add_gsym_reloc(s
, segment
, addr
, 0,
855 elf_add_gsym_reloc(s
, segment
, addr
, 0,
860 elf_add_gsym_reloc(s
, segment
, addr
, 0,
865 elf_add_gsym_reloc(s
, segment
, addr
, 0,
870 nasm_error(ERR_PANIC
, "internal error elf64-hpa-903");
873 } else if (wrt
== elf_plt_sect
+ 1) {
874 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
875 "relative PLT references");
877 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
881 elf_sect_writeaddr(s
, addr
, size
);
885 reltype
= R_X86_64_PC8
;
890 reltype
= R_X86_64_PC16
;
895 addr
= *(int64_t *)data
- size
;
896 if (segment
== segto
)
897 nasm_error(ERR_PANIC
, "intra-segment OUT_REL1ADR");
898 if (segment
== NO_SEG
) {
900 } else if (segment
% 2) {
901 nasm_error(ERR_NONFATAL
, "ELF format does not support"
902 " segment base references");
905 elf_add_reloc(s
, segment
, addr
, reltype
);
908 nasm_error(ERR_NONFATAL
,
909 "Unsupported non-32-bit ELF relocation");
912 elf_sect_writeaddr(s
, addr
, bytes
);
916 addr
= *(int64_t *)data
- size
;
917 if (segment
== segto
)
918 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
919 if (segment
== NO_SEG
) {
921 } else if (segment
% 2) {
922 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
923 " segment base references");
926 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
928 } else if (wrt
== elf_plt_sect
+ 1) {
929 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
930 R_X86_64_PLT32
, true);
932 } else if (wrt
== elf_gotpc_sect
+ 1 ||
933 wrt
== elf_got_sect
+ 1) {
934 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
935 R_X86_64_GOTPCREL
, true);
937 } else if (wrt
== elf_gotoff_sect
+ 1 ||
938 wrt
== elf_got_sect
+ 1) {
939 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
941 } else if (wrt
== elf_gottpoff_sect
+ 1) {
942 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
943 R_X86_64_GOTTPOFF
, true);
946 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
950 elf_sect_writeaddr(s
, addr
, 4);
954 addr
= *(int64_t *)data
- size
;
955 if (segment
== segto
)
956 nasm_error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
957 if (segment
== NO_SEG
) {
959 } else if (segment
% 2) {
960 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
961 " segment base references");
964 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
966 } else if (wrt
== elf_gotpc_sect
+ 1 ||
967 wrt
== elf_got_sect
+ 1) {
968 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
969 R_X86_64_GOTPCREL64
, true);
971 } else if (wrt
== elf_gotoff_sect
+ 1 ||
972 wrt
== elf_got_sect
+ 1) {
973 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
975 } else if (wrt
== elf_gottpoff_sect
+ 1) {
976 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
979 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
983 elf_sect_writeaddr(s
, addr
, 8);
988 static void elf_write(void)
995 int32_t symtablen
, symtablocal
;
998 * Work out how many sections we will have. We have SHN_UNDEF,
999 * then the flexible user sections, then the fixed sections
1000 * `.shstrtab', `.symtab' and `.strtab', then optionally
1001 * relocation sections for the user sections.
1003 nsections
= sec_numspecial
+ 1;
1004 if (of_elf64
.current_dfmt
== &df_stabs
)
1006 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1009 add_sectname("", ".shstrtab");
1010 add_sectname("", ".symtab");
1011 add_sectname("", ".strtab");
1012 for (i
= 0; i
< nsects
; i
++) {
1013 nsections
++; /* for the section itself */
1014 if (sects
[i
]->head
) {
1015 nsections
++; /* for its relocations */
1016 add_sectname(".rela", sects
[i
]->name
);
1020 if (of_elf64
.current_dfmt
== &df_stabs
) {
1021 /* in case the debug information is wanted, just add these three sections... */
1022 add_sectname("", ".stab");
1023 add_sectname("", ".stabstr");
1024 add_sectname(".rel", ".stab");
1027 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1028 /* the dwarf debug standard specifies the following ten sections,
1029 not all of which are currently implemented,
1030 although all of them are defined. */
1031 #define debug_aranges (int64_t) (nsections-10)
1032 #define debug_info (int64_t) (nsections-7)
1033 #define debug_abbrev (int64_t) (nsections-5)
1034 #define debug_line (int64_t) (nsections-4)
1035 add_sectname("", ".debug_aranges");
1036 add_sectname(".rela", ".debug_aranges");
1037 add_sectname("", ".debug_pubnames");
1038 add_sectname("", ".debug_info");
1039 add_sectname(".rela", ".debug_info");
1040 add_sectname("", ".debug_abbrev");
1041 add_sectname("", ".debug_line");
1042 add_sectname(".rela", ".debug_line");
1043 add_sectname("", ".debug_frame");
1044 add_sectname("", ".debug_loc");
1048 * Output the ELF header.
1050 fwrite("\177ELF\2\1\1", 7, 1, ofile
);
1051 fputc(elf_osabi
, ofile
);
1052 fputc(elf_abiver
, ofile
);
1053 fwritezero(7, ofile
);
1054 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1055 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1056 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1057 fwriteint64_t(0L, ofile
); /* no entry point */
1058 fwriteint64_t(0L, ofile
); /* no program header table */
1059 fwriteint64_t(0x40L
, ofile
); /* section headers straight after
1060 * ELF header plus alignment */
1061 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
1062 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1063 fwriteint16_t(0, ofile
); /* no program header table, again */
1064 fwriteint16_t(0, ofile
); /* still no program header table */
1065 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1066 fwriteint16_t(nsections
, ofile
); /* number of sections */
1067 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1068 * section header table */
1071 * Build the symbol table and relocation tables.
1073 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1074 for (i
= 0; i
< nsects
; i
++)
1076 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1080 * Now output the section header table.
1083 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1084 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1087 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1090 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1093 /* The normal sections */
1094 for (i
= 0; i
< nsects
; i
++) {
1095 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1096 (sects
[i
]->type
== SHT_PROGBITS
?
1097 sects
[i
]->data
: NULL
), true,
1098 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1103 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1104 shstrtablen
, 0, 0, 1, 0);
1108 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1109 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1113 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1114 strslen
, 0, 0, 1, 0);
1117 /* The relocation sections */
1118 for (i
= 0; i
< nsects
; i
++)
1119 if (sects
[i
]->head
) {
1120 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1121 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1125 if (of_elf64
.current_dfmt
== &df_stabs
) {
1126 /* for debugging information, create the last three sections
1127 which are the .stab , .stabstr and .rel.stab sections respectively */
1129 /* this function call creates the stab sections in memory */
1132 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1133 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1134 stablen
, sec_stabstr
, 0, 4, 12);
1137 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1138 stabstrlen
, 0, 0, 4, 0);
1141 /* link -> symtable info -> section to refer to */
1142 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1143 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1146 } else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1147 /* for dwarf debugging information, create the ten dwarf sections */
1149 /* this function call creates the dwarf sections in memory */
1153 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1154 arangeslen
, 0, 0, 1, 0);
1157 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1158 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1161 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1162 pubnameslen
, 0, 0, 1, 0);
1165 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1166 infolen
, 0, 0, 1, 0);
1169 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1170 inforellen
, symtabsection
, debug_info
, 1, 24);
1173 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1174 abbrevlen
, 0, 0, 1, 0);
1177 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1178 linelen
, 0, 0, 1, 0);
1181 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1182 linerellen
, symtabsection
, debug_line
, 1, 24);
1185 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1186 framelen
, 0, 0, 8, 0);
1189 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1190 loclen
, 0, 0, 1, 0);
1193 fwritezero(align
, ofile
);
1196 * Now output the sections.
1198 elf_write_sections();
1200 nasm_free(elf_sects
);
1204 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1206 struct SAA
*s
= saa_init(1L);
1208 uint8_t entry
[24], *p
;
1214 * First, an all-zeros entry, required by the ELF spec.
1216 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1221 * Next, an entry for the file name.
1224 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1225 WRITESHORT(p
, STT_FILE
); /* type FILE */
1226 WRITESHORT(p
, SHN_ABS
);
1227 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1228 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1229 saa_wbytes(s
, entry
, 24L);
1234 * Now some standard symbols defining the segments, for relocation
1237 for (i
= 1; i
<= nsects
; i
++) {
1239 WRITELONG(p
, 0); /* no symbol name */
1240 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1241 WRITESHORT(p
, i
); /* section id */
1242 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1243 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1244 saa_wbytes(s
, entry
, 24L);
1251 * Now the other local symbols.
1254 while ((sym
= saa_rstruct(syms
))) {
1255 if (sym
->type
& SYM_GLOBAL
)
1258 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1259 WRITECHAR(p
, sym
->type
); /* type and binding */
1260 WRITECHAR(p
, sym
->other
); /* visibility */
1261 WRITESHORT(p
, sym
->section
); /* index into section header table */
1262 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1263 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1264 saa_wbytes(s
, entry
, 24L);
1269 * dwarf needs symbols for debug sections
1270 * which are relocation targets.
1272 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1273 dwarf_infosym
= *local
;
1275 WRITELONG(p
, 0); /* no symbol name */
1276 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1277 WRITESHORT(p
, debug_info
); /* section id */
1278 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1279 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1280 saa_wbytes(s
, entry
, 24L);
1283 dwarf_abbrevsym
= *local
;
1285 WRITELONG(p
, 0); /* no symbol name */
1286 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1287 WRITESHORT(p
, debug_abbrev
); /* section id */
1288 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1289 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1290 saa_wbytes(s
, entry
, 24L);
1293 dwarf_linesym
= *local
;
1295 WRITELONG(p
, 0); /* no symbol name */
1296 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1297 WRITESHORT(p
, debug_line
); /* section id */
1298 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1299 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1300 saa_wbytes(s
, entry
, 24L);
1306 * Now the global symbols.
1309 while ((sym
= saa_rstruct(syms
))) {
1310 if (!(sym
->type
& SYM_GLOBAL
))
1313 WRITELONG(p
, sym
->strpos
);
1314 WRITECHAR(p
, sym
->type
); /* type and binding */
1315 WRITECHAR(p
, sym
->other
); /* visibility */
1316 WRITESHORT(p
, sym
->section
);
1317 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1318 WRITEDLONG(p
, (int64_t)sym
->size
);
1319 saa_wbytes(s
, entry
, 24L);
1326 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1329 uint8_t *p
, entry
[24];
1330 int32_t global_offset
;
1339 * How to onvert from a global placeholder to a real symbol index;
1340 * the +2 refers to the two special entries, the null entry and
1341 * the filename entry.
1343 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1346 int32_t sym
= r
->symbol
;
1348 if (sym
>= GLOBAL_TEMP_BASE
)
1349 sym
+= global_offset
;
1352 WRITEDLONG(p
, r
->address
);
1353 WRITELONG(p
, r
->type
);
1355 WRITEDLONG(p
, r
->offset
);
1356 saa_wbytes(s
, entry
, 24L);
1365 static void elf_section_header(int name
, int type
, uint64_t flags
,
1366 void *data
, bool is_saa
, uint64_t datalen
,
1367 int link
, int info
, int align
, int eltsize
)
1369 elf_sects
[elf_nsect
].data
= data
;
1370 elf_sects
[elf_nsect
].len
= datalen
;
1371 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1374 fwriteint32_t((int32_t)name
, ofile
);
1375 fwriteint32_t((int32_t)type
, ofile
);
1376 fwriteint64_t((int64_t)flags
, ofile
);
1377 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
1378 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1379 fwriteint64_t(datalen
, ofile
);
1381 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1382 fwriteint32_t((int32_t)link
, ofile
);
1383 fwriteint32_t((int32_t)info
, ofile
);
1384 fwriteint64_t((int64_t)align
, ofile
);
1385 fwriteint64_t((int64_t)eltsize
, ofile
);
1388 static void elf_write_sections(void)
1391 for (i
= 0; i
< elf_nsect
; i
++)
1392 if (elf_sects
[i
].data
) {
1393 int32_t len
= elf_sects
[i
].len
;
1394 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1395 int32_t align
= reallen
- len
;
1396 if (elf_sects
[i
].is_saa
)
1397 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1399 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1400 fwritezero(align
, ofile
);
1404 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1406 saa_wbytes(sect
->data
, data
, len
);
1409 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1411 saa_writeaddr(sect
->data
, data
, len
);
1415 static void elf_sectalign(int32_t seg
, unsigned int value
)
1417 struct Section
*s
= NULL
;
1420 for (i
= 0; i
< nsects
; i
++) {
1421 if (sects
[i
]->index
== seg
) {
1426 if (!s
|| !is_power2(value
))
1429 if (value
> s
->align
)
1433 static int32_t elf_segbase(int32_t segment
)
1438 static int elf_directive(enum directives directive
, char *value
, int pass
)
1444 switch (directive
) {
1447 return 1; /* ignore in pass 2 */
1449 n
= readnum(value
, &err
);
1451 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1454 if (n
< 0 || n
> 255) {
1455 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1461 if ((p
= strchr(value
,',')) == NULL
)
1464 n
= readnum(p
+1, &err
);
1465 if (err
|| n
< 0 || n
> 255) {
1466 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1478 static void elf_filename(char *inname
, char *outname
)
1480 strcpy(elf_module
, inname
);
1481 standard_extension(inname
, outname
, ".o");
1484 extern macros_t elf_stdmac
[];
1486 static int elf_set_info(enum geninfo type
, char **val
)
1492 static struct dfmt df_dwarf
= {
1493 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1503 static struct dfmt df_stabs
= {
1504 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1515 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1517 struct ofmt of_elf64
= {
1518 "ELF64 (x86_64) object files (e.g. Linux)",
1536 /* common debugging routines */
1537 static void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
,
1538 int is_global
, char *special
)
1547 static void debug64_directive(const char *directive
, const char *params
)
1553 static void debug64_typevalue(int32_t type
)
1555 int32_t stype
, ssize
;
1556 switch (TYM_TYPE(type
)) {
1599 stype
= STT_SECTION
;
1614 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1615 lastsym
->size
= ssize
;
1616 lastsym
->type
= stype
;
1620 /* stabs debugging routines */
1622 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1625 if (!stabs_filename
) {
1626 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1627 strcpy(stabs_filename
, filename
);
1629 if (strcmp(stabs_filename
, filename
)) {
1630 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1631 in fact, this leak comes in quite handy to maintain a list of files
1632 encountered so far in the symbol lines... */
1634 /* why not nasm_free(stabs_filename); we're done with the old one */
1636 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1637 strcpy(stabs_filename
, filename
);
1641 currentline
= linenumber
;
1645 static void stabs64_output(int type
, void *param
)
1647 struct symlininfo
*s
;
1648 struct linelist
*el
;
1649 if (type
== TY_DEBUGSYMLIN
) {
1650 if (debug_immcall
) {
1651 s
= (struct symlininfo
*)param
;
1652 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1653 return; /* line info is only collected for executable sections */
1655 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1656 el
->info
.offset
= s
->offset
;
1657 el
->info
.section
= s
->section
;
1658 el
->info
.name
= s
->name
;
1659 el
->line
= currentline
;
1660 el
->filename
= stabs_filename
;
1663 stabslines
->last
->next
= el
;
1664 stabslines
->last
= el
;
1667 stabslines
->last
= el
;
1674 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1676 static void stabs64_generate(void)
1678 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1679 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1683 struct linelist
*ptr
;
1687 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
1690 if (numfiles
== 0) {
1691 allfiles
[0] = ptr
->filename
;
1694 for (i
= 0; i
< numfiles
; i
++) {
1695 if (!strcmp(allfiles
[i
], ptr
->filename
))
1698 if (i
>= numfiles
) {
1699 allfiles
[i
] = ptr
->filename
;
1706 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1707 for (i
= 0; i
< numfiles
; i
++) {
1708 fileidx
[i
] = strsize
;
1709 strsize
+= strlen(allfiles
[i
]) + 1;
1712 for (i
= 0; i
< numfiles
; i
++) {
1713 if (!strcmp(allfiles
[i
], elf_module
)) {
1720 * worst case size of the stab buffer would be:
1721 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1722 * plus one "ending" entry
1724 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1725 sizeof(struct stabentry
));
1726 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1727 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1730 for (i
= 0; i
< numfiles
; i
++)
1731 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1734 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1742 * this is the first stab, its strx points to the filename of the
1743 * the source-file, the n_desc field should be set to the number
1744 * of remaining stabs
1746 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1748 /* this is the stab for the main source file */
1749 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1751 /* relocation table entry */
1754 * Since the symbol table has two entries before
1755 * the section symbols, the index in the info.section
1756 * member must be adjusted by adding 2
1759 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1760 WRITELONG(rptr
, R_X86_64_32
);
1761 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1764 currfile
= mainfileindex
;
1768 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1769 /* oops file has changed... */
1770 for (i
= 0; i
< numfiles
; i
++)
1771 if (!strcmp(allfiles
[i
], ptr
->filename
))
1774 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1778 /* relocation table entry */
1780 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1781 WRITELONG(rptr
, R_X86_64_32
);
1782 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1785 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1788 /* relocation table entry */
1790 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1791 WRITELONG(rptr
, R_X86_64_32
);
1792 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1798 /* this is an "ending" token */
1799 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1802 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1804 nasm_free(allfiles
);
1807 stablen
= (sptr
- sbuf
);
1808 stabrellen
= (rptr
- rbuf
);
1814 static void stabs64_cleanup(void)
1816 struct linelist
*ptr
, *del
;
1828 nasm_free(stabrelbuf
);
1829 nasm_free(stabstrbuf
);
1832 /* dwarf routines */
1834 static void dwarf64_init(void)
1836 ndebugs
= 3; /* 3 debug symbols */
1839 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1843 dwarf64_findfile(filename
);
1845 currentline
= linenumber
;
1848 /* called from elf_out with type == TY_DEBUGSYMLIN */
1849 static void dwarf64_output(int type
, void *param
)
1851 int ln
, aa
, inx
, maxln
, soc
;
1852 struct symlininfo
*s
;
1857 s
= (struct symlininfo
*)param
;
1859 /* line number info is only gathered for executable sections */
1860 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1863 /* Check if section index has changed */
1864 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1865 dwarf64_findsect(s
->section
);
1867 /* do nothing unless line or file has changed */
1871 ln
= currentline
- dwarf_csect
->line
;
1872 aa
= s
->offset
- dwarf_csect
->offset
;
1873 inx
= dwarf_clist
->line
;
1874 plinep
= dwarf_csect
->psaa
;
1875 /* check for file change */
1876 if (!(inx
== dwarf_csect
->file
)) {
1877 saa_write8(plinep
,DW_LNS_set_file
);
1878 saa_write8(plinep
,inx
);
1879 dwarf_csect
->file
= inx
;
1881 /* check for line change */
1883 /* test if in range of special op code */
1884 maxln
= line_base
+ line_range
;
1885 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1886 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1887 saa_write8(plinep
,soc
);
1889 saa_write8(plinep
,DW_LNS_advance_line
);
1890 saa_wleb128s(plinep
,ln
);
1892 saa_write8(plinep
,DW_LNS_advance_pc
);
1893 saa_wleb128u(plinep
,aa
);
1896 dwarf_csect
->line
= currentline
;
1897 dwarf_csect
->offset
= s
->offset
;
1900 /* show change handled */
1905 static void dwarf64_generate(void)
1909 struct linelist
*ftentry
;
1910 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1911 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1912 struct sectlist
*psect
;
1913 size_t saalen
, linepoff
, totlen
, highaddr
;
1915 /* write epilogues for each line program range */
1916 /* and build aranges section */
1917 paranges
= saa_init(1L);
1918 parangesrel
= saa_init(1L);
1919 saa_write16(paranges
,3); /* dwarf version */
1920 saa_write64(parangesrel
, paranges
->datalen
+4);
1921 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1922 saa_write64(parangesrel
, 0);
1923 saa_write32(paranges
,0); /* offset into info */
1924 saa_write8(paranges
,8); /* pointer size */
1925 saa_write8(paranges
,0); /* not segmented */
1926 saa_write32(paranges
,0); /* padding */
1927 /* iterate though sectlist entries */
1928 psect
= dwarf_fsect
;
1931 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1933 plinep
= psect
->psaa
;
1934 /* Line Number Program Epilogue */
1935 saa_write8(plinep
,2); /* std op 2 */
1936 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1937 saa_write8(plinep
,DW_LNS_extended_op
);
1938 saa_write8(plinep
,1); /* operand length */
1939 saa_write8(plinep
,DW_LNE_end_sequence
);
1940 totlen
+= plinep
->datalen
;
1941 /* range table relocation entry */
1942 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1943 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1944 saa_write64(parangesrel
, (uint64_t) 0);
1945 /* range table entry */
1946 saa_write64(paranges
,0x0000); /* range start */
1947 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1948 highaddr
+= sects
[psect
->section
]->len
;
1949 /* done with this entry */
1950 psect
= psect
->next
;
1952 saa_write64(paranges
,0); /* null address */
1953 saa_write64(paranges
,0); /* null length */
1954 saalen
= paranges
->datalen
;
1955 arangeslen
= saalen
+ 4;
1956 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1957 WRITELONG(pbuf
,saalen
); /* initial length */
1958 saa_rnbytes(paranges
, pbuf
, saalen
);
1961 /* build rela.aranges section */
1962 arangesrellen
= saalen
= parangesrel
->datalen
;
1963 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1964 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1965 saa_free(parangesrel
);
1967 /* build pubnames section */
1968 ppubnames
= saa_init(1L);
1969 saa_write16(ppubnames
,3); /* dwarf version */
1970 saa_write32(ppubnames
,0); /* offset into info */
1971 saa_write32(ppubnames
,0); /* space used in info */
1972 saa_write32(ppubnames
,0); /* end of list */
1973 saalen
= ppubnames
->datalen
;
1974 pubnameslen
= saalen
+ 4;
1975 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1976 WRITELONG(pbuf
,saalen
); /* initial length */
1977 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1978 saa_free(ppubnames
);
1980 /* build info section */
1981 pinfo
= saa_init(1L);
1982 pinforel
= saa_init(1L);
1983 saa_write16(pinfo
,3); /* dwarf version */
1984 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1985 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
1986 saa_write64(pinforel
, 0);
1987 saa_write32(pinfo
,0); /* offset into abbrev */
1988 saa_write8(pinfo
,8); /* pointer size */
1989 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1990 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1991 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1992 saa_write64(pinforel
, 0);
1993 saa_write64(pinfo
,0); /* DW_AT_low_pc */
1994 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1995 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1996 saa_write64(pinforel
, 0);
1997 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
1998 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1999 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2000 saa_write64(pinforel
, 0);
2001 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2002 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2003 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2004 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2005 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2006 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2007 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2008 saa_write64(pinforel
, 0);
2009 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2010 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2011 saa_write8(pinfo
,0); /* end of entries */
2012 saalen
= pinfo
->datalen
;
2013 infolen
= saalen
+ 4;
2014 infobuf
= pbuf
= nasm_malloc(infolen
);
2015 WRITELONG(pbuf
,saalen
); /* initial length */
2016 saa_rnbytes(pinfo
, pbuf
, saalen
);
2019 /* build rela.info section */
2020 inforellen
= saalen
= pinforel
->datalen
;
2021 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2022 saa_rnbytes(pinforel
, pbuf
, saalen
);
2025 /* build abbrev section */
2026 pabbrev
= saa_init(1L);
2027 saa_write8(pabbrev
,1); /* entry number LEB128u */
2028 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2029 saa_write8(pabbrev
,1); /* has children */
2030 /* the following attributes and forms are all LEB128u values */
2031 saa_write8(pabbrev
,DW_AT_low_pc
);
2032 saa_write8(pabbrev
,DW_FORM_addr
);
2033 saa_write8(pabbrev
,DW_AT_high_pc
);
2034 saa_write8(pabbrev
,DW_FORM_addr
);
2035 saa_write8(pabbrev
,DW_AT_stmt_list
);
2036 saa_write8(pabbrev
,DW_FORM_data4
);
2037 saa_write8(pabbrev
,DW_AT_name
);
2038 saa_write8(pabbrev
,DW_FORM_string
);
2039 saa_write8(pabbrev
,DW_AT_producer
);
2040 saa_write8(pabbrev
,DW_FORM_string
);
2041 saa_write8(pabbrev
,DW_AT_language
);
2042 saa_write8(pabbrev
,DW_FORM_data2
);
2043 saa_write16(pabbrev
,0); /* end of entry */
2044 /* LEB128u usage same as above */
2045 saa_write8(pabbrev
,2); /* entry number */
2046 saa_write8(pabbrev
,DW_TAG_subprogram
);
2047 saa_write8(pabbrev
,0); /* no children */
2048 saa_write8(pabbrev
,DW_AT_low_pc
);
2049 saa_write8(pabbrev
,DW_FORM_addr
);
2050 saa_write8(pabbrev
,DW_AT_frame_base
);
2051 saa_write8(pabbrev
,DW_FORM_data4
);
2052 saa_write16(pabbrev
,0); /* end of entry */
2053 abbrevlen
= saalen
= pabbrev
->datalen
;
2054 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2055 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2058 /* build line section */
2060 plines
= saa_init(1L);
2061 saa_write8(plines
,1); /* Minimum Instruction Length */
2062 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2063 saa_write8(plines
,line_base
); /* Line Base */
2064 saa_write8(plines
,line_range
); /* Line Range */
2065 saa_write8(plines
,opcode_base
); /* Opcode Base */
2066 /* standard opcode lengths (# of LEB128u operands) */
2067 saa_write8(plines
,0); /* Std opcode 1 length */
2068 saa_write8(plines
,1); /* Std opcode 2 length */
2069 saa_write8(plines
,1); /* Std opcode 3 length */
2070 saa_write8(plines
,1); /* Std opcode 4 length */
2071 saa_write8(plines
,1); /* Std opcode 5 length */
2072 saa_write8(plines
,0); /* Std opcode 6 length */
2073 saa_write8(plines
,0); /* Std opcode 7 length */
2074 saa_write8(plines
,0); /* Std opcode 8 length */
2075 saa_write8(plines
,1); /* Std opcode 9 length */
2076 saa_write8(plines
,0); /* Std opcode 10 length */
2077 saa_write8(plines
,0); /* Std opcode 11 length */
2078 saa_write8(plines
,1); /* Std opcode 12 length */
2079 /* Directory Table */
2080 saa_write8(plines
,0); /* End of table */
2081 /* File Name Table */
2082 ftentry
= dwarf_flist
;
2083 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2085 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2086 saa_write8(plines
,0); /* directory LEB128u */
2087 saa_write8(plines
,0); /* time LEB128u */
2088 saa_write8(plines
,0); /* size LEB128u */
2089 ftentry
= ftentry
->next
;
2091 saa_write8(plines
,0); /* End of table */
2092 linepoff
= plines
->datalen
;
2093 linelen
= linepoff
+ totlen
+ 10;
2094 linebuf
= pbuf
= nasm_malloc(linelen
);
2095 WRITELONG(pbuf
,linelen
-4); /* initial length */
2096 WRITESHORT(pbuf
,3); /* dwarf version */
2097 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2098 /* write line header */
2100 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2103 /* concatonate line program ranges */
2105 plinesrel
= saa_init(1L);
2106 psect
= dwarf_fsect
;
2107 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2108 saa_write64(plinesrel
, linepoff
);
2109 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2110 saa_write64(plinesrel
, (uint64_t) 0);
2111 plinep
= psect
->psaa
;
2112 saalen
= plinep
->datalen
;
2113 saa_rnbytes(plinep
, pbuf
, saalen
);
2117 /* done with this entry */
2118 psect
= psect
->next
;
2122 /* build rela.lines section */
2123 linerellen
=saalen
= plinesrel
->datalen
;
2124 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2125 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2126 saa_free(plinesrel
);
2128 /* build frame section */
2130 framebuf
= pbuf
= nasm_malloc(framelen
);
2131 WRITELONG(pbuf
,framelen
-4); /* initial length */
2133 /* build loc section */
2135 locbuf
= pbuf
= nasm_malloc(loclen
);
2136 WRITEDLONG(pbuf
,0); /* null beginning offset */
2137 WRITEDLONG(pbuf
,0); /* null ending offset */
2140 static void dwarf64_cleanup(void)
2142 nasm_free(arangesbuf
);
2143 nasm_free(arangesrelbuf
);
2144 nasm_free(pubnamesbuf
);
2146 nasm_free(inforelbuf
);
2147 nasm_free(abbrevbuf
);
2149 nasm_free(linerelbuf
);
2150 nasm_free(framebuf
);
2154 static void dwarf64_findfile(const char * fname
)
2157 struct linelist
*match
;
2159 /* return if fname is current file name */
2160 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2163 /* search for match */
2166 match
= dwarf_flist
;
2167 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2168 if (!(strcmp(fname
, match
->filename
))) {
2169 dwarf_clist
= match
;
2175 /* add file name to end of list */
2176 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2178 dwarf_clist
->line
= dwarf_numfiles
;
2179 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2180 strcpy(dwarf_clist
->filename
,fname
);
2181 dwarf_clist
->next
= 0;
2182 if (!dwarf_flist
) { /* if first entry */
2183 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2184 dwarf_clist
->last
= 0;
2185 } else { /* chain to previous entry */
2186 dwarf_elist
->next
= dwarf_clist
;
2187 dwarf_elist
= dwarf_clist
;
2191 static void dwarf64_findsect(const int index
)
2194 struct sectlist
*match
;
2197 /* return if index is current section index */
2198 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2201 /* search for match */
2204 match
= dwarf_fsect
;
2205 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2206 if ((match
->section
== index
)) {
2207 dwarf_csect
= match
;
2210 match
= match
->next
;
2214 /* add entry to end of list */
2215 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2217 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2218 dwarf_csect
->line
= 1;
2219 dwarf_csect
->offset
= 0;
2220 dwarf_csect
->file
= 1;
2221 dwarf_csect
->section
= index
;
2222 dwarf_csect
->next
= 0;
2223 /* set relocatable address at start of line program */
2224 saa_write8(plinep
,DW_LNS_extended_op
);
2225 saa_write8(plinep
,9); /* operand length */
2226 saa_write8(plinep
,DW_LNE_set_address
);
2227 saa_write64(plinep
,0); /* Start Address */
2229 if (!dwarf_fsect
) { /* if first entry */
2230 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2231 dwarf_csect
->last
= 0;
2232 } else { /* chain to previous entry */
2233 dwarf_esect
->next
= dwarf_csect
;
2234 dwarf_esect
= dwarf_csect
;