1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelf64.c output routines for the Netwide Assembler to produce
36 * ELF64 (x86_64 of course) object file format
53 #include "output/outform.h"
54 #include "output/outlib.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
65 int64_t address
; /* relative to _start_ of section */
66 int64_t symbol
; /* symbol index */
67 int64_t offset
; /* symbol addend */
68 int type
; /* type of relocation */
72 struct rbtree symv
; /* symbol value and rbtree of globals */
73 int32_t strpos
; /* string table position of name */
74 int32_t section
; /* section ID of the symbol */
75 int type
; /* symbol type */
76 int other
; /* symbol visibility */
77 int32_t size
; /* size of symbol */
78 int32_t globnum
; /* symbol table offset if global */
79 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
80 char *name
; /* used temporarily if in above list */
87 int32_t index
; /* index into sects array */
88 int type
; /* SHT_PROGBITS or SHT_NOBITS */
89 uint64_t align
; /* alignment: power of two */
90 uint64_t flags
; /* section flags */
94 struct Reloc
*head
, **tail
;
95 struct rbtree
*gsyms
; /* global symbols in section */
99 static struct Section
**sects
;
100 static int nsects
, sectlen
;
102 #define SHSTR_DELTA 256
103 static char *shstrtab
;
104 static int shstrtablen
, shstrtabsize
;
106 static struct SAA
*syms
;
107 static uint32_t nlocals
, nglobs
, ndebugs
;
109 static int32_t def_seg
;
111 static struct RAA
*bsym
;
113 static struct SAA
*strs
;
114 static uint32_t strslen
;
116 static struct Symbol
*fwds
;
118 static char elf_module
[FILENAME_MAX
];
120 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
121 static uint8_t elf_abiver
= 0; /* Current ABI version */
123 extern struct ofmt of_elf64
;
125 static struct ELF_SECTDATA
{
130 static int elf_nsect
, nsections
;
131 static int64_t elf_foffs
;
133 static void elf_write(void);
134 static void elf_sect_write(struct Section
*, const void *, size_t);
135 static void elf_sect_writeaddr(struct Section
*, int64_t, size_t);
136 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
138 static void elf_write_sections(void);
139 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
140 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
141 static void add_sectname(char *, char *);
157 int section
; /* index into sects[] */
158 int segto
; /* internal section number */
159 char *name
; /* shallow-copied pointer of section name */
163 struct symlininfo info
;
166 struct linelist
*next
;
167 struct linelist
*last
;
176 struct sectlist
*next
;
177 struct sectlist
*last
;
180 /* common debug variables */
181 static int currentline
= 1;
182 static int debug_immcall
= 0;
184 /* stabs debug variables */
185 static struct linelist
*stabslines
= 0;
186 static int numlinestabs
= 0;
187 static char *stabs_filename
= 0;
188 static int symtabsection
;
189 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
190 static int stablen
, stabstrlen
, stabrellen
;
192 /* dwarf debug variables */
193 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
194 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
195 static int dwarf_numfiles
= 0, dwarf_nsections
;
196 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
197 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
198 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
199 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
200 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
201 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
204 static struct dfmt df_dwarf
;
205 static struct dfmt df_stabs
;
206 static struct Symbol
*lastsym
;
208 /* common debugging routines */
209 static void debug64_typevalue(int32_t);
210 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
211 static void debug64_directive(const char *, const char *);
213 /* stabs debugging routines */
214 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
215 static void stabs64_output(int, void *);
216 static void stabs64_generate(void);
217 static void stabs64_cleanup(void);
219 /* dwarf debugging routines */
220 static void dwarf64_init(void);
221 static void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
222 static void dwarf64_output(int, void *);
223 static void dwarf64_generate(void);
224 static void dwarf64_cleanup(void);
225 static void dwarf64_findfile(const char *);
226 static void dwarf64_findsect(const int);
229 * Special section numbers which are used to define ELF special
230 * symbols, which can be used with WRT to provide PIC relocation
233 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
234 static int32_t elf_got_sect
, elf_plt_sect
;
235 static int32_t elf_sym_sect
;
236 static int32_t elf_gottpoff_sect
;
238 static void elf_init(void)
242 nsects
= sectlen
= 0;
243 syms
= saa_init((int32_t)sizeof(struct Symbol
));
244 nlocals
= nglobs
= ndebugs
= 0;
247 saa_wbytes(strs
, "\0", 1L);
248 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
249 strslen
= 2 + strlen(elf_module
);
251 shstrtablen
= shstrtabsize
= 0;;
252 add_sectname("", "");
256 elf_gotpc_sect
= seg_alloc();
257 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
258 elf_gotoff_sect
= seg_alloc();
259 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
260 elf_got_sect
= seg_alloc();
261 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
262 elf_plt_sect
= seg_alloc();
263 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
264 elf_sym_sect
= seg_alloc();
265 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
266 elf_gottpoff_sect
= seg_alloc();
267 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
269 def_seg
= seg_alloc();
273 static void elf_cleanup(int debuginfo
)
281 for (i
= 0; i
< nsects
; i
++) {
282 if (sects
[i
]->type
!= SHT_NOBITS
)
283 saa_free(sects
[i
]->data
);
285 saa_free(sects
[i
]->rel
);
286 while (sects
[i
]->head
) {
288 sects
[i
]->head
= sects
[i
]->head
->next
;
296 if (of_elf64
.current_dfmt
) {
297 of_elf64
.current_dfmt
->cleanup();
301 /* add entry to the elf .shstrtab section */
302 static void add_sectname(char *firsthalf
, char *secondhalf
)
304 int len
= strlen(firsthalf
) + strlen(secondhalf
);
305 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
306 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
307 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
308 strcat(shstrtab
+ shstrtablen
, secondhalf
);
309 shstrtablen
+= len
+ 1;
312 static int elf_make_section(char *name
, int type
, int flags
, int align
)
316 s
= nasm_malloc(sizeof(*s
));
318 if (type
!= SHT_NOBITS
)
319 s
->data
= saa_init(1L);
322 s
->len
= s
->size
= 0;
324 if (!strcmp(name
, ".text"))
327 s
->index
= seg_alloc();
328 add_sectname("", name
);
329 s
->name
= nasm_malloc(1 + strlen(name
));
330 strcpy(s
->name
, name
);
336 if (nsects
>= sectlen
)
337 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
343 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
346 uint32_t flags
, flags_and
, flags_or
;
351 * Default is 64 bits.
358 p
= nasm_skip_word(name
);
361 flags_and
= flags_or
= type
= align
= 0;
363 section_attrib(name
, p
, pass
, &flags_and
,
364 &flags_or
, &align
, &type
);
366 if (!strcmp(name
, ".shstrtab") ||
367 !strcmp(name
, ".symtab") ||
368 !strcmp(name
, ".strtab")) {
369 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
374 for (i
= 0; i
< nsects
; i
++)
375 if (!strcmp(name
, sects
[i
]->name
))
378 const struct elf_known_section
*ks
= elf_known_sections
;
381 if (!strcmp(name
, ks
->name
))
386 type
= type
? type
: ks
->type
;
387 align
= align
? align
: ks
->align
;
388 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
390 i
= elf_make_section(name
, type
, flags
, align
);
391 } else if (pass
== 1) {
392 if ((type
&& sects
[i
]->type
!= type
)
393 || (align
&& sects
[i
]->align
!= align
)
394 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
395 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
396 " redeclaration of section `%s'", name
);
399 return sects
[i
]->index
;
402 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
403 int is_global
, char *special
)
407 bool special_used
= false;
409 #if defined(DEBUG) && DEBUG>2
410 nasm_error(ERR_DEBUG
,
411 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
412 name
, segment
, offset
, is_global
, special
);
414 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
416 * This is a NASM special symbol. We never allow it into
417 * the ELF symbol table, even if it's a valid one. If it
418 * _isn't_ a valid one, we should barf immediately.
420 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
421 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
422 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
423 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
427 if (is_global
== 3) {
430 * Fix up a forward-reference symbol size from the first
433 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
434 if (!strcmp((*s
)->name
, name
)) {
435 struct tokenval tokval
;
437 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
441 tokval
.t_type
= TOKEN_INVALID
;
442 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
445 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
446 " expression as symbol size");
448 (*s
)->size
= reloc_value(e
);
452 * Remove it from the list of unresolved sizes.
454 nasm_free((*s
)->name
);
458 return; /* it wasn't an important one */
461 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
462 strslen
+= 1 + strlen(name
);
464 lastsym
= sym
= saa_wstruct(syms
);
466 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
469 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
470 sym
->other
= STV_DEFAULT
;
472 if (segment
== NO_SEG
)
473 sym
->section
= SHN_ABS
;
476 sym
->section
= SHN_UNDEF
;
477 if (segment
== def_seg
) {
478 /* we have to be sure at least text section is there */
480 if (segment
!= elf_section_names(".text", 2, &tempint
))
481 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
483 for (i
= 0; i
< nsects
; i
++) {
484 if (segment
== sects
[i
]->index
) {
485 sym
->section
= i
+ 1;
491 if (is_global
== 2) {
494 sym
->section
= SHN_COMMON
;
496 * We have a common variable. Check the special text to see
497 * if it's a valid number and power of two; if so, store it
498 * as the alignment for the common variable.
502 sym
->symv
.key
= readnum(special
, &err
);
504 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
505 " valid number", special
);
506 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
507 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
508 " power of two", special
);
512 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
514 if (sym
->type
== SYM_GLOBAL
) {
516 * If sym->section == SHN_ABS, then the first line of the
517 * else section would cause a core dump, because its a reference
518 * beyond the end of the section array.
519 * This behaviour is exhibited by this code:
522 * To avoid such a crash, such requests are silently discarded.
523 * This may not be the best solution.
525 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
526 bsym
= raa_write(bsym
, segment
, nglobs
);
527 } else if (sym
->section
!= SHN_ABS
) {
529 * This is a global symbol; so we must add it to the rbtree
530 * of global symbols in its section.
532 * In addition, we check the special text for symbol
533 * type and size information.
535 sects
[sym
->section
-1]->gsyms
=
536 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
539 int n
= strcspn(special
, " \t");
541 if (!nasm_strnicmp(special
, "function", n
))
542 sym
->type
|= STT_FUNC
;
543 else if (!nasm_strnicmp(special
, "data", n
) ||
544 !nasm_strnicmp(special
, "object", n
))
545 sym
->type
|= STT_OBJECT
;
546 else if (!nasm_strnicmp(special
, "notype", n
))
547 sym
->type
|= STT_NOTYPE
;
549 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
553 special
= nasm_skip_spaces(special
);
555 n
= strcspn(special
, " \t");
556 if (!nasm_strnicmp(special
, "default", n
))
557 sym
->other
= STV_DEFAULT
;
558 else if (!nasm_strnicmp(special
, "internal", n
))
559 sym
->other
= STV_INTERNAL
;
560 else if (!nasm_strnicmp(special
, "hidden", n
))
561 sym
->other
= STV_HIDDEN
;
562 else if (!nasm_strnicmp(special
, "protected", n
))
563 sym
->other
= STV_PROTECTED
;
570 struct tokenval tokval
;
573 char *saveme
= stdscan_get();
575 while (special
[n
] && nasm_isspace(special
[n
]))
578 * We have a size expression; attempt to
582 stdscan_set(special
+ n
);
583 tokval
.t_type
= TOKEN_INVALID
;
584 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
589 sym
->name
= nasm_strdup(name
);
592 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
593 " expression as symbol size");
595 sym
->size
= reloc_value(e
);
602 * If TLS segment, mark symbol accordingly.
604 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
606 sym
->type
|= STT_TLS
;
609 sym
->globnum
= nglobs
;
614 if (special
&& !special_used
)
615 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
618 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
619 int64_t offset
, int type
)
622 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
623 sect
->tail
= &r
->next
;
626 r
->address
= sect
->len
;
628 if (segment
== NO_SEG
)
633 for (i
= 0; i
< nsects
; i
++)
634 if (segment
== sects
[i
]->index
)
637 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
645 * This routine deals with ..got and ..sym relocations: the more
646 * complicated kinds. In shared-library writing, some relocations
647 * with respect to global symbols must refer to the precise symbol
648 * rather than referring to an offset from the base of the section
649 * _containing_ the symbol. Such relocations call to this routine,
650 * which searches the symbol list for the symbol in question.
652 * R_386_GOT32 references require the _exact_ symbol address to be
653 * used; R_386_32 references can be at an offset from the symbol.
654 * The boolean argument `exact' tells us this.
656 * Return value is the adjusted value of `addr', having become an
657 * offset from the symbol rather than the section. Should always be
658 * zero when returning from an exact call.
660 * Limitation: if you define two symbols at the same place,
661 * confusion will occur.
663 * Inefficiency: we search, currently, using a linked list which
664 * isn't even necessarily sorted.
666 static void elf_add_gsym_reloc(struct Section
*sect
,
667 int32_t segment
, uint64_t offset
, int64_t pcrel
,
668 int type
, bool exact
)
677 * First look up the segment/offset pair and find a global
678 * symbol corresponding to it. If it's not one of our segments,
679 * then it must be an external symbol, in which case we're fine
680 * doing a normal elf_add_reloc after first sanity-checking
681 * that the offset from the symbol is zero.
684 for (i
= 0; i
< nsects
; i
++)
685 if (segment
== sects
[i
]->index
) {
692 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
694 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
698 srb
= rb_search(s
->gsyms
, offset
);
699 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
700 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
701 " for this reference");
704 sym
= container_of(srb
, struct Symbol
, symv
);
706 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
707 sect
->tail
= &r
->next
;
710 r
->address
= sect
->len
;
711 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
712 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
718 static void elf_out(int32_t segto
, const void *data
,
719 enum out_type type
, uint64_t size
,
720 int32_t segment
, int32_t wrt
)
725 static struct symlininfo sinfo
;
729 #if defined(DEBUG) && DEBUG>2
731 nasm_error(ERR_DEBUG
,
732 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
733 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
735 nasm_error(ERR_DEBUG
,
736 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
737 currentline
, type
, segment
, segto
, size
);
741 * handle absolute-assembly (structure definitions)
743 if (segto
== NO_SEG
) {
744 if (type
!= OUT_RESERVE
)
745 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
751 for (i
= 0; i
< nsects
; i
++)
752 if (segto
== sects
[i
]->index
) {
757 int tempint
; /* ignored */
758 if (segto
!= elf_section_names(".text", 2, &tempint
))
759 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
761 s
= sects
[nsects
- 1];
765 /* invoke current debug_output routine */
766 if (of_elf64
.current_dfmt
) {
767 sinfo
.offset
= s
->len
;
770 sinfo
.name
= s
->name
;
771 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
773 /* end of debugging stuff */
775 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
776 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
777 " BSS section `%s': ignored", s
->name
);
778 s
->len
+= realsize(type
, size
);
782 if (type
== OUT_RESERVE
) {
783 if (s
->type
== SHT_PROGBITS
) {
784 nasm_error(ERR_WARNING
, "uninitialized space declared in"
785 " non-BSS section `%s': zeroing", s
->name
);
786 elf_sect_write(s
, NULL
, size
);
789 } else if (type
== OUT_RAWDATA
) {
790 if (segment
!= NO_SEG
)
791 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
792 elf_sect_write(s
, data
, size
);
793 } else if (type
== OUT_ADDRESS
) {
794 addr
= *(int64_t *)data
;
795 if (segment
== NO_SEG
) {
797 } else if (segment
% 2) {
798 nasm_error(ERR_NONFATAL
, "ELF format does not support"
799 " segment base references");
804 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
807 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
810 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
813 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
816 nasm_error(ERR_PANIC
, "internal error elf64-hpa-871");
820 } else if (wrt
== elf_gotpc_sect
+ 1) {
822 * The user will supply GOT relative to $$. ELF
823 * will let us have GOT relative to $. So we
824 * need to fix up the data item by $-$$.
827 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
829 } else if (wrt
== elf_gotoff_sect
+ 1) {
831 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
832 "references to be qword");
834 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
837 } else if (wrt
== elf_got_sect
+ 1) {
840 elf_add_gsym_reloc(s
, segment
, addr
, 0,
841 R_X86_64_GOT32
, true);
845 elf_add_gsym_reloc(s
, segment
, addr
, 0,
846 R_X86_64_GOT64
, true);
850 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
853 } else if (wrt
== elf_sym_sect
+ 1) {
856 elf_add_gsym_reloc(s
, segment
, addr
, 0,
861 elf_add_gsym_reloc(s
, segment
, addr
, 0,
866 elf_add_gsym_reloc(s
, segment
, addr
, 0,
871 elf_add_gsym_reloc(s
, segment
, addr
, 0,
876 nasm_error(ERR_PANIC
, "internal error elf64-hpa-903");
879 } else if (wrt
== elf_plt_sect
+ 1) {
880 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
881 "relative PLT references");
883 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
887 elf_sect_writeaddr(s
, addr
, size
);
888 } else if (type
== OUT_REL2ADR
) {
889 addr
= *(int64_t *)data
- size
;
890 if (segment
== segto
)
891 nasm_error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
892 if (segment
== NO_SEG
) {
894 } else if (segment
% 2) {
895 nasm_error(ERR_NONFATAL
, "ELF format does not support"
896 " segment base references");
899 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
902 nasm_error(ERR_NONFATAL
,
903 "Unsupported non-32-bit ELF relocation [2]");
906 elf_sect_writeaddr(s
, addr
, 2);
907 } else if (type
== OUT_REL4ADR
) {
908 addr
= *(int64_t *)data
- size
;
909 if (segment
== segto
)
910 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
911 if (segment
== NO_SEG
) {
913 } else if (segment
% 2) {
914 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
915 " segment base references");
918 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
920 } else if (wrt
== elf_plt_sect
+ 1) {
921 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
922 R_X86_64_PLT32
, true);
924 } else if (wrt
== elf_gotpc_sect
+ 1 ||
925 wrt
== elf_got_sect
+ 1) {
926 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
927 R_X86_64_GOTPCREL
, true);
929 } else if (wrt
== elf_gotoff_sect
+ 1 ||
930 wrt
== elf_got_sect
+ 1) {
931 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
933 } else if (wrt
== elf_gottpoff_sect
+ 1) {
934 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
935 R_X86_64_GOTTPOFF
, true);
938 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
942 elf_sect_writeaddr(s
, addr
, 4);
943 } else if (type
== OUT_REL8ADR
) {
944 addr
= *(int64_t *)data
- size
;
945 if (segment
== segto
)
946 nasm_error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
947 if (segment
== NO_SEG
) {
949 } else if (segment
% 2) {
950 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
951 " segment base references");
954 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
956 } else if (wrt
== elf_gotpc_sect
+ 1 ||
957 wrt
== elf_got_sect
+ 1) {
958 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
959 R_X86_64_GOTPCREL64
, true);
961 } else if (wrt
== elf_gotoff_sect
+ 1 ||
962 wrt
== elf_got_sect
+ 1) {
963 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
965 } else if (wrt
== elf_gottpoff_sect
+ 1) {
966 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
969 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
973 elf_sect_writeaddr(s
, addr
, 8);
977 static void elf_write(void)
984 int32_t symtablen
, symtablocal
;
987 * Work out how many sections we will have. We have SHN_UNDEF,
988 * then the flexible user sections, then the fixed sections
989 * `.shstrtab', `.symtab' and `.strtab', then optionally
990 * relocation sections for the user sections.
992 nsections
= sec_numspecial
+ 1;
993 if (of_elf64
.current_dfmt
== &df_stabs
)
995 else if (of_elf64
.current_dfmt
== &df_dwarf
)
998 add_sectname("", ".shstrtab");
999 add_sectname("", ".symtab");
1000 add_sectname("", ".strtab");
1001 for (i
= 0; i
< nsects
; i
++) {
1002 nsections
++; /* for the section itself */
1003 if (sects
[i
]->head
) {
1004 nsections
++; /* for its relocations */
1005 add_sectname(".rela", sects
[i
]->name
);
1009 if (of_elf64
.current_dfmt
== &df_stabs
) {
1010 /* in case the debug information is wanted, just add these three sections... */
1011 add_sectname("", ".stab");
1012 add_sectname("", ".stabstr");
1013 add_sectname(".rel", ".stab");
1016 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1017 /* the dwarf debug standard specifies the following ten sections,
1018 not all of which are currently implemented,
1019 although all of them are defined. */
1020 #define debug_aranges (int64_t) (nsections-10)
1021 #define debug_info (int64_t) (nsections-7)
1022 #define debug_abbrev (int64_t) (nsections-5)
1023 #define debug_line (int64_t) (nsections-4)
1024 add_sectname("", ".debug_aranges");
1025 add_sectname(".rela", ".debug_aranges");
1026 add_sectname("", ".debug_pubnames");
1027 add_sectname("", ".debug_info");
1028 add_sectname(".rela", ".debug_info");
1029 add_sectname("", ".debug_abbrev");
1030 add_sectname("", ".debug_line");
1031 add_sectname(".rela", ".debug_line");
1032 add_sectname("", ".debug_frame");
1033 add_sectname("", ".debug_loc");
1037 * Output the ELF header.
1039 fwrite("\177ELF\2\1\1", 7, 1, ofile
);
1040 fputc(elf_osabi
, ofile
);
1041 fputc(elf_abiver
, ofile
);
1042 fwritezero(7, ofile
);
1043 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1044 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1045 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1046 fwriteint64_t(0L, ofile
); /* no entry point */
1047 fwriteint64_t(0L, ofile
); /* no program header table */
1048 fwriteint64_t(0x40L
, ofile
); /* section headers straight after
1049 * ELF header plus alignment */
1050 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
1051 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1052 fwriteint16_t(0, ofile
); /* no program header table, again */
1053 fwriteint16_t(0, ofile
); /* still no program header table */
1054 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1055 fwriteint16_t(nsections
, ofile
); /* number of sections */
1056 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1057 * section header table */
1060 * Build the symbol table and relocation tables.
1062 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1063 for (i
= 0; i
< nsects
; i
++)
1065 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1069 * Now output the section header table.
1072 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1073 align
= ALIGN(elf_foffs
, SEG_ALIGN
) - elf_foffs
;
1076 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1079 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1082 /* The normal sections */
1083 for (i
= 0; i
< nsects
; i
++) {
1084 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1085 (sects
[i
]->type
== SHT_PROGBITS
?
1086 sects
[i
]->data
: NULL
), true,
1087 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1092 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1093 shstrtablen
, 0, 0, 1, 0);
1097 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1098 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1102 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1103 strslen
, 0, 0, 1, 0);
1106 /* The relocation sections */
1107 for (i
= 0; i
< nsects
; i
++)
1108 if (sects
[i
]->head
) {
1109 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1110 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1114 if (of_elf64
.current_dfmt
== &df_stabs
) {
1115 /* for debugging information, create the last three sections
1116 which are the .stab , .stabstr and .rel.stab sections respectively */
1118 /* this function call creates the stab sections in memory */
1121 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1122 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1123 stablen
, sec_stabstr
, 0, 4, 12);
1126 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1127 stabstrlen
, 0, 0, 4, 0);
1130 /* link -> symtable info -> section to refer to */
1131 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1132 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1136 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1137 /* for dwarf debugging information, create the ten dwarf sections */
1139 /* this function call creates the dwarf sections in memory */
1143 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1144 arangeslen
, 0, 0, 1, 0);
1147 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1148 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1151 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1152 pubnameslen
, 0, 0, 1, 0);
1155 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1156 infolen
, 0, 0, 1, 0);
1159 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1160 inforellen
, symtabsection
, debug_info
, 1, 24);
1163 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1164 abbrevlen
, 0, 0, 1, 0);
1167 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1168 linelen
, 0, 0, 1, 0);
1171 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1172 linerellen
, symtabsection
, debug_line
, 1, 24);
1175 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1176 framelen
, 0, 0, 8, 0);
1179 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1180 loclen
, 0, 0, 1, 0);
1183 fwritezero(align
, ofile
);
1186 * Now output the sections.
1188 elf_write_sections();
1190 nasm_free(elf_sects
);
1194 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1196 struct SAA
*s
= saa_init(1L);
1198 uint8_t entry
[24], *p
;
1204 * First, an all-zeros entry, required by the ELF spec.
1206 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1211 * Next, an entry for the file name.
1214 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1215 WRITESHORT(p
, STT_FILE
); /* type FILE */
1216 WRITESHORT(p
, SHN_ABS
);
1217 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1218 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1219 saa_wbytes(s
, entry
, 24L);
1224 * Now some standard symbols defining the segments, for relocation
1227 for (i
= 1; i
<= nsects
; i
++) {
1229 WRITELONG(p
, 0); /* no symbol name */
1230 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1231 WRITESHORT(p
, i
); /* section id */
1232 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1233 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1234 saa_wbytes(s
, entry
, 24L);
1241 * Now the other local symbols.
1244 while ((sym
= saa_rstruct(syms
))) {
1245 if (sym
->type
& SYM_GLOBAL
)
1248 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1249 WRITECHAR(p
, sym
->type
); /* type and binding */
1250 WRITECHAR(p
, sym
->other
); /* visibility */
1251 WRITESHORT(p
, sym
->section
); /* index into section header table */
1252 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1253 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1254 saa_wbytes(s
, entry
, 24L);
1259 * dwarf needs symbols for debug sections
1260 * which are relocation targets.
1262 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1263 dwarf_infosym
= *local
;
1265 WRITELONG(p
, 0); /* no symbol name */
1266 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1267 WRITESHORT(p
, debug_info
); /* section id */
1268 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1269 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1270 saa_wbytes(s
, entry
, 24L);
1273 dwarf_abbrevsym
= *local
;
1275 WRITELONG(p
, 0); /* no symbol name */
1276 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1277 WRITESHORT(p
, debug_abbrev
); /* 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_linesym
= *local
;
1285 WRITELONG(p
, 0); /* no symbol name */
1286 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1287 WRITESHORT(p
, debug_line
); /* section id */
1288 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1289 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1290 saa_wbytes(s
, entry
, 24L);
1296 * Now the global symbols.
1299 while ((sym
= saa_rstruct(syms
))) {
1300 if (!(sym
->type
& SYM_GLOBAL
))
1303 WRITELONG(p
, sym
->strpos
);
1304 WRITECHAR(p
, sym
->type
); /* type and binding */
1305 WRITECHAR(p
, sym
->other
); /* visibility */
1306 WRITESHORT(p
, sym
->section
);
1307 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1308 WRITEDLONG(p
, (int64_t)sym
->size
);
1309 saa_wbytes(s
, entry
, 24L);
1316 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1319 uint8_t *p
, entry
[24];
1320 int32_t global_offset
;
1329 * How to onvert from a global placeholder to a real symbol index;
1330 * the +2 refers to the two special entries, the null entry and
1331 * the filename entry.
1333 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1336 int32_t sym
= r
->symbol
;
1338 if (sym
>= GLOBAL_TEMP_BASE
)
1339 sym
+= global_offset
;
1342 WRITEDLONG(p
, r
->address
);
1343 WRITELONG(p
, r
->type
);
1345 WRITEDLONG(p
, r
->offset
);
1346 saa_wbytes(s
, entry
, 24L);
1355 static void elf_section_header(int name
, int type
, uint64_t flags
,
1356 void *data
, bool is_saa
, uint64_t datalen
,
1357 int link
, int info
, int align
, int eltsize
)
1359 elf_sects
[elf_nsect
].data
= data
;
1360 elf_sects
[elf_nsect
].len
= datalen
;
1361 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1364 fwriteint32_t((int32_t)name
, ofile
);
1365 fwriteint32_t((int32_t)type
, ofile
);
1366 fwriteint64_t((int64_t)flags
, ofile
);
1367 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
1368 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1369 fwriteint64_t(datalen
, ofile
);
1371 elf_foffs
+= ALIGN(datalen
, SEG_ALIGN
);
1372 fwriteint32_t((int32_t)link
, ofile
);
1373 fwriteint32_t((int32_t)info
, ofile
);
1374 fwriteint64_t((int64_t)align
, ofile
);
1375 fwriteint64_t((int64_t)eltsize
, ofile
);
1378 static void elf_write_sections(void)
1381 for (i
= 0; i
< elf_nsect
; i
++)
1382 if (elf_sects
[i
].data
) {
1383 int32_t len
= elf_sects
[i
].len
;
1384 int32_t reallen
= ALIGN(len
, SEG_ALIGN
);
1385 int32_t align
= reallen
- len
;
1386 if (elf_sects
[i
].is_saa
)
1387 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1389 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1390 fwritezero(align
, ofile
);
1394 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1396 saa_wbytes(sect
->data
, data
, len
);
1399 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1401 saa_writeaddr(sect
->data
, data
, len
);
1405 static int32_t elf_segbase(int32_t segment
)
1410 static int elf_directive(enum directives directive
, char *value
, int pass
)
1416 switch (directive
) {
1419 return 1; /* ignore in pass 2 */
1421 n
= readnum(value
, &err
);
1423 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1426 if (n
< 0 || n
> 255) {
1427 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1433 if ((p
= strchr(value
,',')) == NULL
)
1436 n
= readnum(p
+1, &err
);
1437 if (err
|| n
< 0 || n
> 255) {
1438 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1450 static void elf_filename(char *inname
, char *outname
)
1452 strcpy(elf_module
, inname
);
1453 standard_extension(inname
, outname
, ".o");
1456 extern macros_t elf_stdmac
[];
1458 static int elf_set_info(enum geninfo type
, char **val
)
1464 static struct dfmt df_dwarf
= {
1465 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1475 static struct dfmt df_stabs
= {
1476 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1487 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1489 struct ofmt of_elf64
= {
1490 "ELF64 (x86_64) object files (e.g. Linux)",
1507 /* common debugging routines */
1508 static void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
,
1509 int is_global
, char *special
)
1518 static void debug64_directive(const char *directive
, const char *params
)
1524 static void debug64_typevalue(int32_t type
)
1526 int32_t stype
, ssize
;
1527 switch (TYM_TYPE(type
)) {
1570 stype
= STT_SECTION
;
1585 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1586 lastsym
->size
= ssize
;
1587 lastsym
->type
= stype
;
1591 /* stabs debugging routines */
1593 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1596 if (!stabs_filename
) {
1597 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1598 strcpy(stabs_filename
, filename
);
1600 if (strcmp(stabs_filename
, filename
)) {
1601 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1602 in fact, this leak comes in quite handy to maintain a list of files
1603 encountered so far in the symbol lines... */
1605 /* why not nasm_free(stabs_filename); we're done with the old one */
1607 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1608 strcpy(stabs_filename
, filename
);
1612 currentline
= linenumber
;
1616 static void stabs64_output(int type
, void *param
)
1618 struct symlininfo
*s
;
1619 struct linelist
*el
;
1620 if (type
== TY_DEBUGSYMLIN
) {
1621 if (debug_immcall
) {
1622 s
= (struct symlininfo
*)param
;
1623 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1624 return; /* line info is only collected for executable sections */
1626 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1627 el
->info
.offset
= s
->offset
;
1628 el
->info
.section
= s
->section
;
1629 el
->info
.name
= s
->name
;
1630 el
->line
= currentline
;
1631 el
->filename
= stabs_filename
;
1634 stabslines
->last
->next
= el
;
1635 stabslines
->last
= el
;
1638 stabslines
->last
= el
;
1645 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1647 WRITELONG(p,n_strx); \
1648 WRITECHAR(p,n_type); \
1649 WRITECHAR(p,n_other); \
1650 WRITESHORT(p,n_desc); \
1651 WRITELONG(p,n_value); \
1654 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1656 static void stabs64_generate(void)
1658 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1659 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1663 struct linelist
*ptr
;
1667 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1668 for (i
= 0; i
< numlinestabs
; i
++)
1672 if (numfiles
== 0) {
1673 allfiles
[0] = ptr
->filename
;
1676 for (i
= 0; i
< numfiles
; i
++) {
1677 if (!strcmp(allfiles
[i
], ptr
->filename
))
1680 if (i
>= numfiles
) {
1681 allfiles
[i
] = ptr
->filename
;
1688 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1689 for (i
= 0; i
< numfiles
; i
++) {
1690 fileidx
[i
] = strsize
;
1691 strsize
+= strlen(allfiles
[i
]) + 1;
1694 for (i
= 0; i
< numfiles
; i
++) {
1695 if (!strcmp(allfiles
[i
], elf_module
)) {
1702 * worst case size of the stab buffer would be:
1703 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1704 * plus one "ending" entry
1706 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1707 sizeof(struct stabentry
));
1708 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1709 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1712 for (i
= 0; i
< numfiles
; i
++)
1713 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1716 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1724 * this is the first stab, its strx points to the filename of the
1725 * the source-file, the n_desc field should be set to the number
1726 * of remaining stabs
1728 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1730 /* this is the stab for the main source file */
1731 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1733 /* relocation table entry */
1736 * Since the symbol table has two entries before
1737 * the section symbols, the index in the info.section
1738 * member must be adjusted by adding 2
1741 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1742 WRITELONG(rptr
, R_X86_64_32
);
1743 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1746 currfile
= mainfileindex
;
1750 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1751 /* oops file has changed... */
1752 for (i
= 0; i
< numfiles
; i
++)
1753 if (!strcmp(allfiles
[i
], ptr
->filename
))
1756 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1760 /* relocation table entry */
1762 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1763 WRITELONG(rptr
, R_X86_64_32
);
1764 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1767 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1770 /* relocation table entry */
1772 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1773 WRITELONG(rptr
, R_X86_64_32
);
1774 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1780 /* this is an "ending" token */
1781 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1784 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1786 nasm_free(allfiles
);
1789 stablen
= (sptr
- sbuf
);
1790 stabrellen
= (rptr
- rbuf
);
1796 static void stabs64_cleanup(void)
1798 struct linelist
*ptr
, *del
;
1810 nasm_free(stabrelbuf
);
1811 nasm_free(stabstrbuf
);
1814 /* dwarf routines */
1816 static void dwarf64_init(void)
1818 ndebugs
= 3; /* 3 debug symbols */
1821 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1825 dwarf64_findfile(filename
);
1827 currentline
= linenumber
;
1830 /* called from elf_out with type == TY_DEBUGSYMLIN */
1831 static void dwarf64_output(int type
, void *param
)
1833 int ln
, aa
, inx
, maxln
, soc
;
1834 struct symlininfo
*s
;
1839 s
= (struct symlininfo
*)param
;
1841 /* line number info is only gathered for executable sections */
1842 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1845 /* Check if section index has changed */
1846 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1847 dwarf64_findsect(s
->section
);
1849 /* do nothing unless line or file has changed */
1853 ln
= currentline
- dwarf_csect
->line
;
1854 aa
= s
->offset
- dwarf_csect
->offset
;
1855 inx
= dwarf_clist
->line
;
1856 plinep
= dwarf_csect
->psaa
;
1857 /* check for file change */
1858 if (!(inx
== dwarf_csect
->file
)) {
1859 saa_write8(plinep
,DW_LNS_set_file
);
1860 saa_write8(plinep
,inx
);
1861 dwarf_csect
->file
= inx
;
1863 /* check for line change */
1865 /* test if in range of special op code */
1866 maxln
= line_base
+ line_range
;
1867 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1868 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1869 saa_write8(plinep
,soc
);
1871 saa_write8(plinep
,DW_LNS_advance_line
);
1872 saa_wleb128s(plinep
,ln
);
1874 saa_write8(plinep
,DW_LNS_advance_pc
);
1875 saa_wleb128u(plinep
,aa
);
1878 dwarf_csect
->line
= currentline
;
1879 dwarf_csect
->offset
= s
->offset
;
1882 /* show change handled */
1887 static void dwarf64_generate(void)
1891 struct linelist
*ftentry
;
1892 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1893 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1894 struct sectlist
*psect
;
1895 size_t saalen
, linepoff
, totlen
, highaddr
;
1897 /* write epilogues for each line program range */
1898 /* and build aranges section */
1899 paranges
= saa_init(1L);
1900 parangesrel
= saa_init(1L);
1901 saa_write16(paranges
,3); /* dwarf version */
1902 saa_write64(parangesrel
, paranges
->datalen
+4);
1903 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1904 saa_write64(parangesrel
, 0);
1905 saa_write32(paranges
,0); /* offset into info */
1906 saa_write8(paranges
,8); /* pointer size */
1907 saa_write8(paranges
,0); /* not segmented */
1908 saa_write32(paranges
,0); /* padding */
1909 /* iterate though sectlist entries */
1910 psect
= dwarf_fsect
;
1913 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1915 plinep
= psect
->psaa
;
1916 /* Line Number Program Epilogue */
1917 saa_write8(plinep
,2); /* std op 2 */
1918 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1919 saa_write8(plinep
,DW_LNS_extended_op
);
1920 saa_write8(plinep
,1); /* operand length */
1921 saa_write8(plinep
,DW_LNE_end_sequence
);
1922 totlen
+= plinep
->datalen
;
1923 /* range table relocation entry */
1924 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1925 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1926 saa_write64(parangesrel
, (uint64_t) 0);
1927 /* range table entry */
1928 saa_write64(paranges
,0x0000); /* range start */
1929 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1930 highaddr
+= sects
[psect
->section
]->len
;
1931 /* done with this entry */
1932 psect
= psect
->next
;
1934 saa_write64(paranges
,0); /* null address */
1935 saa_write64(paranges
,0); /* null length */
1936 saalen
= paranges
->datalen
;
1937 arangeslen
= saalen
+ 4;
1938 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1939 WRITELONG(pbuf
,saalen
); /* initial length */
1940 saa_rnbytes(paranges
, pbuf
, saalen
);
1943 /* build rela.aranges section */
1944 arangesrellen
= saalen
= parangesrel
->datalen
;
1945 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1946 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1947 saa_free(parangesrel
);
1949 /* build pubnames section */
1950 ppubnames
= saa_init(1L);
1951 saa_write16(ppubnames
,3); /* dwarf version */
1952 saa_write32(ppubnames
,0); /* offset into info */
1953 saa_write32(ppubnames
,0); /* space used in info */
1954 saa_write32(ppubnames
,0); /* end of list */
1955 saalen
= ppubnames
->datalen
;
1956 pubnameslen
= saalen
+ 4;
1957 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1958 WRITELONG(pbuf
,saalen
); /* initial length */
1959 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1960 saa_free(ppubnames
);
1962 /* build info section */
1963 pinfo
= saa_init(1L);
1964 pinforel
= saa_init(1L);
1965 saa_write16(pinfo
,3); /* dwarf version */
1966 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1967 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
1968 saa_write64(pinforel
, 0);
1969 saa_write32(pinfo
,0); /* offset into abbrev */
1970 saa_write8(pinfo
,8); /* pointer size */
1971 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1972 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1973 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1974 saa_write64(pinforel
, 0);
1975 saa_write64(pinfo
,0); /* DW_AT_low_pc */
1976 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1977 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1978 saa_write64(pinforel
, 0);
1979 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
1980 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1981 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
1982 saa_write64(pinforel
, 0);
1983 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1984 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1985 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1986 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1987 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1988 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1989 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1990 saa_write64(pinforel
, 0);
1991 saa_write64(pinfo
,0); /* DW_AT_low_pc */
1992 saa_write64(pinfo
,0); /* DW_AT_frame_base */
1993 saa_write8(pinfo
,0); /* end of entries */
1994 saalen
= pinfo
->datalen
;
1995 infolen
= saalen
+ 4;
1996 infobuf
= pbuf
= nasm_malloc(infolen
);
1997 WRITELONG(pbuf
,saalen
); /* initial length */
1998 saa_rnbytes(pinfo
, pbuf
, saalen
);
2001 /* build rela.info section */
2002 inforellen
= saalen
= pinforel
->datalen
;
2003 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2004 saa_rnbytes(pinforel
, pbuf
, saalen
);
2007 /* build abbrev section */
2008 pabbrev
= saa_init(1L);
2009 saa_write8(pabbrev
,1); /* entry number LEB128u */
2010 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2011 saa_write8(pabbrev
,1); /* has children */
2012 /* the following attributes and forms are all LEB128u values */
2013 saa_write8(pabbrev
,DW_AT_low_pc
);
2014 saa_write8(pabbrev
,DW_FORM_addr
);
2015 saa_write8(pabbrev
,DW_AT_high_pc
);
2016 saa_write8(pabbrev
,DW_FORM_addr
);
2017 saa_write8(pabbrev
,DW_AT_stmt_list
);
2018 saa_write8(pabbrev
,DW_FORM_data4
);
2019 saa_write8(pabbrev
,DW_AT_name
);
2020 saa_write8(pabbrev
,DW_FORM_string
);
2021 saa_write8(pabbrev
,DW_AT_producer
);
2022 saa_write8(pabbrev
,DW_FORM_string
);
2023 saa_write8(pabbrev
,DW_AT_language
);
2024 saa_write8(pabbrev
,DW_FORM_data2
);
2025 saa_write16(pabbrev
,0); /* end of entry */
2026 /* LEB128u usage same as above */
2027 saa_write8(pabbrev
,2); /* entry number */
2028 saa_write8(pabbrev
,DW_TAG_subprogram
);
2029 saa_write8(pabbrev
,0); /* no children */
2030 saa_write8(pabbrev
,DW_AT_low_pc
);
2031 saa_write8(pabbrev
,DW_FORM_addr
);
2032 saa_write8(pabbrev
,DW_AT_frame_base
);
2033 saa_write8(pabbrev
,DW_FORM_data4
);
2034 saa_write16(pabbrev
,0); /* end of entry */
2035 abbrevlen
= saalen
= pabbrev
->datalen
;
2036 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2037 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2040 /* build line section */
2042 plines
= saa_init(1L);
2043 saa_write8(plines
,1); /* Minimum Instruction Length */
2044 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2045 saa_write8(plines
,line_base
); /* Line Base */
2046 saa_write8(plines
,line_range
); /* Line Range */
2047 saa_write8(plines
,opcode_base
); /* Opcode Base */
2048 /* standard opcode lengths (# of LEB128u operands) */
2049 saa_write8(plines
,0); /* Std opcode 1 length */
2050 saa_write8(plines
,1); /* Std opcode 2 length */
2051 saa_write8(plines
,1); /* Std opcode 3 length */
2052 saa_write8(plines
,1); /* Std opcode 4 length */
2053 saa_write8(plines
,1); /* Std opcode 5 length */
2054 saa_write8(plines
,0); /* Std opcode 6 length */
2055 saa_write8(plines
,0); /* Std opcode 7 length */
2056 saa_write8(plines
,0); /* Std opcode 8 length */
2057 saa_write8(plines
,1); /* Std opcode 9 length */
2058 saa_write8(plines
,0); /* Std opcode 10 length */
2059 saa_write8(plines
,0); /* Std opcode 11 length */
2060 saa_write8(plines
,1); /* Std opcode 12 length */
2061 /* Directory Table */
2062 saa_write8(plines
,0); /* End of table */
2063 /* File Name Table */
2064 ftentry
= dwarf_flist
;
2065 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2067 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2068 saa_write8(plines
,0); /* directory LEB128u */
2069 saa_write8(plines
,0); /* time LEB128u */
2070 saa_write8(plines
,0); /* size LEB128u */
2071 ftentry
= ftentry
->next
;
2073 saa_write8(plines
,0); /* End of table */
2074 linepoff
= plines
->datalen
;
2075 linelen
= linepoff
+ totlen
+ 10;
2076 linebuf
= pbuf
= nasm_malloc(linelen
);
2077 WRITELONG(pbuf
,linelen
-4); /* initial length */
2078 WRITESHORT(pbuf
,3); /* dwarf version */
2079 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2080 /* write line header */
2082 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2085 /* concatonate line program ranges */
2087 plinesrel
= saa_init(1L);
2088 psect
= dwarf_fsect
;
2089 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2090 saa_write64(plinesrel
, linepoff
);
2091 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2092 saa_write64(plinesrel
, (uint64_t) 0);
2093 plinep
= psect
->psaa
;
2094 saalen
= plinep
->datalen
;
2095 saa_rnbytes(plinep
, pbuf
, saalen
);
2099 /* done with this entry */
2100 psect
= psect
->next
;
2104 /* build rela.lines section */
2105 linerellen
=saalen
= plinesrel
->datalen
;
2106 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2107 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2108 saa_free(plinesrel
);
2110 /* build frame section */
2112 framebuf
= pbuf
= nasm_malloc(framelen
);
2113 WRITELONG(pbuf
,framelen
-4); /* initial length */
2115 /* build loc section */
2117 locbuf
= pbuf
= nasm_malloc(loclen
);
2118 WRITEDLONG(pbuf
,0); /* null beginning offset */
2119 WRITEDLONG(pbuf
,0); /* null ending offset */
2122 static void dwarf64_cleanup(void)
2124 nasm_free(arangesbuf
);
2125 nasm_free(arangesrelbuf
);
2126 nasm_free(pubnamesbuf
);
2128 nasm_free(inforelbuf
);
2129 nasm_free(abbrevbuf
);
2131 nasm_free(linerelbuf
);
2132 nasm_free(framebuf
);
2136 static void dwarf64_findfile(const char * fname
)
2139 struct linelist
*match
;
2141 /* return if fname is current file name */
2142 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2145 /* search for match */
2148 match
= dwarf_flist
;
2149 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2150 if (!(strcmp(fname
, match
->filename
))) {
2151 dwarf_clist
= match
;
2157 /* add file name to end of list */
2158 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2160 dwarf_clist
->line
= dwarf_numfiles
;
2161 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2162 strcpy(dwarf_clist
->filename
,fname
);
2163 dwarf_clist
->next
= 0;
2164 if (!dwarf_flist
) { /* if first entry */
2165 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2166 dwarf_clist
->last
= 0;
2167 } else { /* chain to previous entry */
2168 dwarf_elist
->next
= dwarf_clist
;
2169 dwarf_elist
= dwarf_clist
;
2173 static void dwarf64_findsect(const int index
)
2176 struct sectlist
*match
;
2179 /* return if index is current section index */
2180 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2183 /* search for match */
2186 match
= dwarf_fsect
;
2187 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2188 if ((match
->section
== index
)) {
2189 dwarf_csect
= match
;
2192 match
= match
->next
;
2196 /* add entry to end of list */
2197 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2199 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2200 dwarf_csect
->line
= 1;
2201 dwarf_csect
->offset
= 0;
2202 dwarf_csect
->file
= 1;
2203 dwarf_csect
->section
= index
;
2204 dwarf_csect
->next
= 0;
2205 /* set relocatable address at start of line program */
2206 saa_write8(plinep
,DW_LNS_extended_op
);
2207 saa_write8(plinep
,9); /* operand length */
2208 saa_write8(plinep
,DW_LNE_set_address
);
2209 saa_write64(plinep
,0); /* Start Address */
2211 if (!dwarf_fsect
) { /* if first entry */
2212 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2213 dwarf_csect
->last
= 0;
2214 } else { /* chain to previous entry */
2215 dwarf_esect
->next
= dwarf_csect
;
2216 dwarf_esect
= dwarf_csect
;