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"
63 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
67 int64_t address
; /* relative to _start_ of section */
68 int64_t symbol
; /* symbol index */
69 int64_t offset
; /* symbol addend */
70 int type
; /* type of relocation */
74 struct rbtree symv
; /* symbol value and rbtree of globals */
75 int32_t strpos
; /* string table position of name */
76 int32_t section
; /* section ID of the symbol */
77 int type
; /* symbol type */
78 int other
; /* symbol visibility */
79 int32_t size
; /* size of symbol */
80 int32_t globnum
; /* symbol table offset if global */
81 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
82 char *name
; /* used temporarily if in above list */
89 int32_t index
; /* index into sects array */
90 int type
; /* SHT_PROGBITS or SHT_NOBITS */
91 uint64_t align
; /* alignment: power of two */
92 uint64_t flags
; /* section flags */
96 struct Reloc
*head
, **tail
;
97 struct rbtree
*gsyms
; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section
**sects
;
102 static int nsects
, sectlen
;
104 #define SHSTR_DELTA 256
105 static char *shstrtab
;
106 static int shstrtablen
, shstrtabsize
;
108 static struct SAA
*syms
;
109 static uint32_t nlocals
, nglobs
, ndebugs
;
111 static int32_t def_seg
;
113 static struct RAA
*bsym
;
115 static struct SAA
*strs
;
116 static uint32_t strslen
;
118 static struct Symbol
*fwds
;
120 static char elf_module
[FILENAME_MAX
];
122 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
123 static uint8_t elf_abiver
= 0; /* Current ABI version */
125 extern struct ofmt of_elf64
;
127 static struct ELF_SECTDATA
{
132 static int elf_nsect
, nsections
;
133 static int64_t elf_foffs
;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section
*, const void *, size_t);
137 static void elf_sect_writeaddr(struct Section
*, int64_t, size_t);
138 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
140 static void elf_write_sections(void);
141 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
142 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
143 static void add_sectname(char *, char *);
159 int section
; /* index into sects[] */
160 int segto
; /* internal section number */
161 char *name
; /* shallow-copied pointer of section name */
165 struct symlininfo info
;
168 struct linelist
*next
;
169 struct linelist
*last
;
178 struct sectlist
*next
;
179 struct sectlist
*last
;
182 /* common debug variables */
183 static int currentline
= 1;
184 static int debug_immcall
= 0;
186 /* stabs debug variables */
187 static struct linelist
*stabslines
= 0;
188 static int numlinestabs
= 0;
189 static char *stabs_filename
= 0;
190 static int symtabsection
;
191 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
192 static int stablen
, stabstrlen
, stabrellen
;
194 /* dwarf debug variables */
195 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
196 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
197 static int dwarf_numfiles
= 0, dwarf_nsections
;
198 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
199 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
200 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
201 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
202 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
203 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
206 static struct dfmt df_dwarf
;
207 static struct dfmt df_stabs
;
208 static struct Symbol
*lastsym
;
210 /* common debugging routines */
211 static void debug64_typevalue(int32_t);
212 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
213 static void debug64_directive(const char *, const char *);
215 /* stabs debugging routines */
216 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
217 static void stabs64_output(int, void *);
218 static void stabs64_generate(void);
219 static void stabs64_cleanup(void);
221 /* dwarf debugging routines */
222 static void dwarf64_init(void);
223 static void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
224 static void dwarf64_output(int, void *);
225 static void dwarf64_generate(void);
226 static void dwarf64_cleanup(void);
227 static void dwarf64_findfile(const char *);
228 static void dwarf64_findsect(const int);
231 * Special section numbers which are used to define ELF special
232 * symbols, which can be used with WRT to provide PIC relocation
235 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
236 static int32_t elf_got_sect
, elf_plt_sect
;
237 static int32_t elf_sym_sect
;
238 static int32_t elf_gottpoff_sect
;
240 static void elf_init(void)
244 nsects
= sectlen
= 0;
245 syms
= saa_init((int32_t)sizeof(struct Symbol
));
246 nlocals
= nglobs
= ndebugs
= 0;
249 saa_wbytes(strs
, "\0", 1L);
250 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
251 strslen
= 2 + strlen(elf_module
);
253 shstrtablen
= shstrtabsize
= 0;;
254 add_sectname("", "");
258 elf_gotpc_sect
= seg_alloc();
259 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
260 elf_gotoff_sect
= seg_alloc();
261 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
262 elf_got_sect
= seg_alloc();
263 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
264 elf_plt_sect
= seg_alloc();
265 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
266 elf_sym_sect
= seg_alloc();
267 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
268 elf_gottpoff_sect
= seg_alloc();
269 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
271 def_seg
= seg_alloc();
275 static void elf_cleanup(int debuginfo
)
283 for (i
= 0; i
< nsects
; i
++) {
284 if (sects
[i
]->type
!= SHT_NOBITS
)
285 saa_free(sects
[i
]->data
);
287 saa_free(sects
[i
]->rel
);
288 while (sects
[i
]->head
) {
290 sects
[i
]->head
= sects
[i
]->head
->next
;
298 if (of_elf64
.current_dfmt
) {
299 of_elf64
.current_dfmt
->cleanup();
303 /* add entry to the elf .shstrtab section */
304 static void add_sectname(char *firsthalf
, char *secondhalf
)
306 int len
= strlen(firsthalf
) + strlen(secondhalf
);
307 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
308 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
309 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
310 strcat(shstrtab
+ shstrtablen
, secondhalf
);
311 shstrtablen
+= len
+ 1;
314 static int elf_make_section(char *name
, int type
, int flags
, int align
)
318 s
= nasm_malloc(sizeof(*s
));
320 if (type
!= SHT_NOBITS
)
321 s
->data
= saa_init(1L);
324 s
->len
= s
->size
= 0;
326 if (!strcmp(name
, ".text"))
329 s
->index
= seg_alloc();
330 add_sectname("", name
);
331 s
->name
= nasm_malloc(1 + strlen(name
));
332 strcpy(s
->name
, name
);
338 if (nsects
>= sectlen
)
339 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
345 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
348 uint32_t flags
, flags_and
, flags_or
;
353 * Default is 64 bits.
360 p
= nasm_skip_word(name
);
363 flags_and
= flags_or
= type
= align
= 0;
365 p
= nasm_skip_spaces(p
);
368 p
= nasm_skip_word(p
);
371 p
= nasm_skip_spaces(p
);
373 if (!nasm_strnicmp(q
, "align=", 6)) {
377 if ((align
- 1) & align
) { /* means it's not a power of two */
378 nasm_error(ERR_NONFATAL
, "section alignment %"PRId64
" is not"
379 " a power of two", align
);
382 } else if (!nasm_stricmp(q
, "alloc")) {
383 flags_and
|= SHF_ALLOC
;
384 flags_or
|= SHF_ALLOC
;
385 } else if (!nasm_stricmp(q
, "noalloc")) {
386 flags_and
|= SHF_ALLOC
;
387 flags_or
&= ~SHF_ALLOC
;
388 } else if (!nasm_stricmp(q
, "exec")) {
389 flags_and
|= SHF_EXECINSTR
;
390 flags_or
|= SHF_EXECINSTR
;
391 } else if (!nasm_stricmp(q
, "noexec")) {
392 flags_and
|= SHF_EXECINSTR
;
393 flags_or
&= ~SHF_EXECINSTR
;
394 } else if (!nasm_stricmp(q
, "write")) {
395 flags_and
|= SHF_WRITE
;
396 flags_or
|= SHF_WRITE
;
397 } else if (!nasm_stricmp(q
, "tls")) {
398 flags_and
|= SHF_TLS
;
400 } else if (!nasm_stricmp(q
, "nowrite")) {
401 flags_and
|= SHF_WRITE
;
402 flags_or
&= ~SHF_WRITE
;
403 } else if (!nasm_stricmp(q
, "progbits")) {
405 } else if (!nasm_stricmp(q
, "nobits")) {
407 } else if (pass
== 1) {
408 nasm_error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
409 " declaration of section `%s'", q
, name
);
413 if (!strcmp(name
, ".shstrtab") ||
414 !strcmp(name
, ".symtab") ||
415 !strcmp(name
, ".strtab")) {
416 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
421 for (i
= 0; i
< nsects
; i
++)
422 if (!strcmp(name
, sects
[i
]->name
))
425 const struct elf_known_section
*ks
= elf_known_sections
;
428 if (!strcmp(name
, ks
->name
))
433 type
= type
? type
: ks
->type
;
434 align
= align
? align
: ks
->align
;
435 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
437 i
= elf_make_section(name
, type
, flags
, align
);
438 } else if (pass
== 1) {
439 if ((type
&& sects
[i
]->type
!= type
)
440 || (align
&& sects
[i
]->align
!= align
)
441 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
442 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
443 " redeclaration of section `%s'", name
);
446 return sects
[i
]->index
;
449 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
450 int is_global
, char *special
)
454 bool special_used
= false;
456 #if defined(DEBUG) && DEBUG>2
457 nasm_error(ERR_DEBUG
,
458 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
459 name
, segment
, offset
, is_global
, special
);
461 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
463 * This is a NASM special symbol. We never allow it into
464 * the ELF symbol table, even if it's a valid one. If it
465 * _isn't_ a valid one, we should barf immediately.
467 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
468 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
469 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
470 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
474 if (is_global
== 3) {
477 * Fix up a forward-reference symbol size from the first
480 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
481 if (!strcmp((*s
)->name
, name
)) {
482 struct tokenval tokval
;
484 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
488 tokval
.t_type
= TOKEN_INVALID
;
489 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
492 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
493 " expression as symbol size");
495 (*s
)->size
= reloc_value(e
);
499 * Remove it from the list of unresolved sizes.
501 nasm_free((*s
)->name
);
505 return; /* it wasn't an important one */
508 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
509 strslen
+= 1 + strlen(name
);
511 lastsym
= sym
= saa_wstruct(syms
);
513 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
516 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
517 sym
->other
= STV_DEFAULT
;
519 if (segment
== NO_SEG
)
520 sym
->section
= SHN_ABS
;
523 sym
->section
= SHN_UNDEF
;
524 if (segment
== def_seg
) {
525 /* we have to be sure at least text section is there */
527 if (segment
!= elf_section_names(".text", 2, &tempint
))
528 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
530 for (i
= 0; i
< nsects
; i
++) {
531 if (segment
== sects
[i
]->index
) {
532 sym
->section
= i
+ 1;
538 if (is_global
== 2) {
541 sym
->section
= SHN_COMMON
;
543 * We have a common variable. Check the special text to see
544 * if it's a valid number and power of two; if so, store it
545 * as the alignment for the common variable.
549 sym
->symv
.key
= readnum(special
, &err
);
551 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
552 " valid number", special
);
553 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
554 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
555 " power of two", special
);
559 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
561 if (sym
->type
== SYM_GLOBAL
) {
563 * If sym->section == SHN_ABS, then the first line of the
564 * else section would cause a core dump, because its a reference
565 * beyond the end of the section array.
566 * This behaviour is exhibited by this code:
569 * To avoid such a crash, such requests are silently discarded.
570 * This may not be the best solution.
572 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
573 bsym
= raa_write(bsym
, segment
, nglobs
);
574 } else if (sym
->section
!= SHN_ABS
) {
576 * This is a global symbol; so we must add it to the rbtree
577 * of global symbols in its section.
579 * In addition, we check the special text for symbol
580 * type and size information.
582 sects
[sym
->section
-1]->gsyms
=
583 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
586 int n
= strcspn(special
, " \t");
588 if (!nasm_strnicmp(special
, "function", n
))
589 sym
->type
|= STT_FUNC
;
590 else if (!nasm_strnicmp(special
, "data", n
) ||
591 !nasm_strnicmp(special
, "object", n
))
592 sym
->type
|= STT_OBJECT
;
593 else if (!nasm_strnicmp(special
, "notype", n
))
594 sym
->type
|= STT_NOTYPE
;
596 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
600 special
= nasm_skip_spaces(special
);
602 n
= strcspn(special
, " \t");
603 if (!nasm_strnicmp(special
, "default", n
))
604 sym
->other
= STV_DEFAULT
;
605 else if (!nasm_strnicmp(special
, "internal", n
))
606 sym
->other
= STV_INTERNAL
;
607 else if (!nasm_strnicmp(special
, "hidden", n
))
608 sym
->other
= STV_HIDDEN
;
609 else if (!nasm_strnicmp(special
, "protected", n
))
610 sym
->other
= STV_PROTECTED
;
617 struct tokenval tokval
;
620 char *saveme
= stdscan_get();
622 while (special
[n
] && nasm_isspace(special
[n
]))
625 * We have a size expression; attempt to
629 stdscan_set(special
+ n
);
630 tokval
.t_type
= TOKEN_INVALID
;
631 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
636 sym
->name
= nasm_strdup(name
);
639 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
640 " expression as symbol size");
642 sym
->size
= reloc_value(e
);
649 * If TLS segment, mark symbol accordingly.
651 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
653 sym
->type
|= STT_TLS
;
656 sym
->globnum
= nglobs
;
661 if (special
&& !special_used
)
662 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
665 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
666 int64_t offset
, int type
)
669 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
670 sect
->tail
= &r
->next
;
673 r
->address
= sect
->len
;
675 if (segment
== NO_SEG
)
680 for (i
= 0; i
< nsects
; i
++)
681 if (segment
== sects
[i
]->index
)
684 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
692 * This routine deals with ..got and ..sym relocations: the more
693 * complicated kinds. In shared-library writing, some relocations
694 * with respect to global symbols must refer to the precise symbol
695 * rather than referring to an offset from the base of the section
696 * _containing_ the symbol. Such relocations call to this routine,
697 * which searches the symbol list for the symbol in question.
699 * R_386_GOT32 references require the _exact_ symbol address to be
700 * used; R_386_32 references can be at an offset from the symbol.
701 * The boolean argument `exact' tells us this.
703 * Return value is the adjusted value of `addr', having become an
704 * offset from the symbol rather than the section. Should always be
705 * zero when returning from an exact call.
707 * Limitation: if you define two symbols at the same place,
708 * confusion will occur.
710 * Inefficiency: we search, currently, using a linked list which
711 * isn't even necessarily sorted.
713 static void elf_add_gsym_reloc(struct Section
*sect
,
714 int32_t segment
, uint64_t offset
, int64_t pcrel
,
715 int type
, bool exact
)
724 * First look up the segment/offset pair and find a global
725 * symbol corresponding to it. If it's not one of our segments,
726 * then it must be an external symbol, in which case we're fine
727 * doing a normal elf_add_reloc after first sanity-checking
728 * that the offset from the symbol is zero.
731 for (i
= 0; i
< nsects
; i
++)
732 if (segment
== sects
[i
]->index
) {
739 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
741 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
745 srb
= rb_search(s
->gsyms
, offset
);
746 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
747 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
748 " for this reference");
751 sym
= container_of(srb
, struct Symbol
, symv
);
753 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
754 sect
->tail
= &r
->next
;
757 r
->address
= sect
->len
;
758 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
759 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
765 static void elf_out(int32_t segto
, const void *data
,
766 enum out_type type
, uint64_t size
,
767 int32_t segment
, int32_t wrt
)
772 static struct symlininfo sinfo
;
776 #if defined(DEBUG) && DEBUG>2
778 nasm_error(ERR_DEBUG
,
779 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
780 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
782 nasm_error(ERR_DEBUG
,
783 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
784 currentline
, type
, segment
, segto
, size
);
788 * handle absolute-assembly (structure definitions)
790 if (segto
== NO_SEG
) {
791 if (type
!= OUT_RESERVE
)
792 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
798 for (i
= 0; i
< nsects
; i
++)
799 if (segto
== sects
[i
]->index
) {
804 int tempint
; /* ignored */
805 if (segto
!= elf_section_names(".text", 2, &tempint
))
806 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
808 s
= sects
[nsects
- 1];
812 /* invoke current debug_output routine */
813 if (of_elf64
.current_dfmt
) {
814 sinfo
.offset
= s
->len
;
817 sinfo
.name
= s
->name
;
818 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
820 /* end of debugging stuff */
822 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
823 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
824 " BSS section `%s': ignored", s
->name
);
825 s
->len
+= realsize(type
, size
);
829 if (type
== OUT_RESERVE
) {
830 if (s
->type
== SHT_PROGBITS
) {
831 nasm_error(ERR_WARNING
, "uninitialized space declared in"
832 " non-BSS section `%s': zeroing", s
->name
);
833 elf_sect_write(s
, NULL
, size
);
836 } else if (type
== OUT_RAWDATA
) {
837 if (segment
!= NO_SEG
)
838 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
839 elf_sect_write(s
, data
, size
);
840 } else if (type
== OUT_ADDRESS
) {
841 addr
= *(int64_t *)data
;
842 if (segment
== NO_SEG
) {
844 } else if (segment
% 2) {
845 nasm_error(ERR_NONFATAL
, "ELF format does not support"
846 " segment base references");
851 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
854 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
857 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
860 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
863 nasm_error(ERR_PANIC
, "internal error elf64-hpa-871");
867 } else if (wrt
== elf_gotpc_sect
+ 1) {
869 * The user will supply GOT relative to $$. ELF
870 * will let us have GOT relative to $. So we
871 * need to fix up the data item by $-$$.
874 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
876 } else if (wrt
== elf_gotoff_sect
+ 1) {
878 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
879 "references to be qword");
881 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
884 } else if (wrt
== elf_got_sect
+ 1) {
887 elf_add_gsym_reloc(s
, segment
, addr
, 0,
888 R_X86_64_GOT32
, true);
892 elf_add_gsym_reloc(s
, segment
, addr
, 0,
893 R_X86_64_GOT64
, true);
897 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
900 } else if (wrt
== elf_sym_sect
+ 1) {
903 elf_add_gsym_reloc(s
, segment
, addr
, 0,
908 elf_add_gsym_reloc(s
, segment
, addr
, 0,
913 elf_add_gsym_reloc(s
, segment
, addr
, 0,
918 elf_add_gsym_reloc(s
, segment
, addr
, 0,
923 nasm_error(ERR_PANIC
, "internal error elf64-hpa-903");
926 } else if (wrt
== elf_plt_sect
+ 1) {
927 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
928 "relative PLT references");
930 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
934 elf_sect_writeaddr(s
, addr
, size
);
935 } else if (type
== OUT_REL2ADR
) {
936 addr
= *(int64_t *)data
- size
;
937 if (segment
== segto
)
938 nasm_error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
939 if (segment
== NO_SEG
) {
941 } else if (segment
% 2) {
942 nasm_error(ERR_NONFATAL
, "ELF format does not support"
943 " segment base references");
946 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
949 nasm_error(ERR_NONFATAL
,
950 "Unsupported non-32-bit ELF relocation [2]");
953 elf_sect_writeaddr(s
, addr
, 2);
954 } else if (type
== OUT_REL4ADR
) {
955 addr
= *(int64_t *)data
- size
;
956 if (segment
== segto
)
957 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
958 if (segment
== NO_SEG
) {
960 } else if (segment
% 2) {
961 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
962 " segment base references");
965 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
967 } else if (wrt
== elf_plt_sect
+ 1) {
968 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
969 R_X86_64_PLT32
, true);
971 } else if (wrt
== elf_gotpc_sect
+ 1 ||
972 wrt
== elf_got_sect
+ 1) {
973 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
974 R_X86_64_GOTPCREL
, true);
976 } else if (wrt
== elf_gotoff_sect
+ 1 ||
977 wrt
== elf_got_sect
+ 1) {
978 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
980 } else if (wrt
== elf_gottpoff_sect
+ 1) {
981 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
982 R_X86_64_GOTTPOFF
, true);
985 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
989 elf_sect_writeaddr(s
, addr
, 4);
990 } else if (type
== OUT_REL8ADR
) {
991 addr
= *(int64_t *)data
- size
;
992 if (segment
== segto
)
993 nasm_error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
994 if (segment
== NO_SEG
) {
996 } else if (segment
% 2) {
997 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
998 " segment base references");
1000 if (wrt
== NO_SEG
) {
1001 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1003 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1004 wrt
== elf_got_sect
+ 1) {
1005 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1006 R_X86_64_GOTPCREL64
, true);
1008 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1009 wrt
== elf_got_sect
+ 1) {
1010 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1012 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1013 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1016 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1020 elf_sect_writeaddr(s
, addr
, 8);
1024 static void elf_write(void)
1031 int32_t symtablen
, symtablocal
;
1034 * Work out how many sections we will have. We have SHN_UNDEF,
1035 * then the flexible user sections, then the fixed sections
1036 * `.shstrtab', `.symtab' and `.strtab', then optionally
1037 * relocation sections for the user sections.
1039 nsections
= sec_numspecial
+ 1;
1040 if (of_elf64
.current_dfmt
== &df_stabs
)
1042 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1045 add_sectname("", ".shstrtab");
1046 add_sectname("", ".symtab");
1047 add_sectname("", ".strtab");
1048 for (i
= 0; i
< nsects
; i
++) {
1049 nsections
++; /* for the section itself */
1050 if (sects
[i
]->head
) {
1051 nsections
++; /* for its relocations */
1052 add_sectname(".rela", sects
[i
]->name
);
1056 if (of_elf64
.current_dfmt
== &df_stabs
) {
1057 /* in case the debug information is wanted, just add these three sections... */
1058 add_sectname("", ".stab");
1059 add_sectname("", ".stabstr");
1060 add_sectname(".rel", ".stab");
1063 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1064 /* the dwarf debug standard specifies the following ten sections,
1065 not all of which are currently implemented,
1066 although all of them are defined. */
1067 #define debug_aranges (int64_t) (nsections-10)
1068 #define debug_info (int64_t) (nsections-7)
1069 #define debug_abbrev (int64_t) (nsections-5)
1070 #define debug_line (int64_t) (nsections-4)
1071 add_sectname("", ".debug_aranges");
1072 add_sectname(".rela", ".debug_aranges");
1073 add_sectname("", ".debug_pubnames");
1074 add_sectname("", ".debug_info");
1075 add_sectname(".rela", ".debug_info");
1076 add_sectname("", ".debug_abbrev");
1077 add_sectname("", ".debug_line");
1078 add_sectname(".rela", ".debug_line");
1079 add_sectname("", ".debug_frame");
1080 add_sectname("", ".debug_loc");
1084 * Output the ELF header.
1086 fwrite("\177ELF\2\1\1", 7, 1, ofile
);
1087 fputc(elf_osabi
, ofile
);
1088 fputc(elf_abiver
, ofile
);
1089 fwritezero(7, ofile
);
1090 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1091 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1092 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1093 fwriteint64_t(0L, ofile
); /* no entry point */
1094 fwriteint64_t(0L, ofile
); /* no program header table */
1095 fwriteint64_t(0x40L
, ofile
); /* section headers straight after
1096 * ELF header plus alignment */
1097 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
1098 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1099 fwriteint16_t(0, ofile
); /* no program header table, again */
1100 fwriteint16_t(0, ofile
); /* still no program header table */
1101 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1102 fwriteint16_t(nsections
, ofile
); /* number of sections */
1103 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1104 * section header table */
1107 * Build the symbol table and relocation tables.
1109 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1110 for (i
= 0; i
< nsects
; i
++)
1112 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1116 * Now output the section header table.
1119 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1120 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1123 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1126 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1129 /* The normal sections */
1130 for (i
= 0; i
< nsects
; i
++) {
1131 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1132 (sects
[i
]->type
== SHT_PROGBITS
?
1133 sects
[i
]->data
: NULL
), true,
1134 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1139 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1140 shstrtablen
, 0, 0, 1, 0);
1144 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1145 symtablen
, sec_strtab
, symtablocal
, 4, 24);
1149 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1150 strslen
, 0, 0, 1, 0);
1153 /* The relocation sections */
1154 for (i
= 0; i
< nsects
; i
++)
1155 if (sects
[i
]->head
) {
1156 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1157 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 24);
1161 if (of_elf64
.current_dfmt
== &df_stabs
) {
1162 /* for debugging information, create the last three sections
1163 which are the .stab , .stabstr and .rel.stab sections respectively */
1165 /* this function call creates the stab sections in memory */
1168 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1169 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1170 stablen
, sec_stabstr
, 0, 4, 12);
1173 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1174 stabstrlen
, 0, 0, 4, 0);
1177 /* link -> symtable info -> section to refer to */
1178 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1179 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1183 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1184 /* for dwarf debugging information, create the ten dwarf sections */
1186 /* this function call creates the dwarf sections in memory */
1190 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1191 arangeslen
, 0, 0, 1, 0);
1194 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1195 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1198 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1199 pubnameslen
, 0, 0, 1, 0);
1202 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1203 infolen
, 0, 0, 1, 0);
1206 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1207 inforellen
, symtabsection
, debug_info
, 1, 24);
1210 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1211 abbrevlen
, 0, 0, 1, 0);
1214 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1215 linelen
, 0, 0, 1, 0);
1218 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1219 linerellen
, symtabsection
, debug_line
, 1, 24);
1222 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1223 framelen
, 0, 0, 8, 0);
1226 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1227 loclen
, 0, 0, 1, 0);
1230 fwritezero(align
, ofile
);
1233 * Now output the sections.
1235 elf_write_sections();
1237 nasm_free(elf_sects
);
1241 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1243 struct SAA
*s
= saa_init(1L);
1245 uint8_t entry
[24], *p
;
1251 * First, an all-zeros entry, required by the ELF spec.
1253 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1258 * Next, an entry for the file name.
1261 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1262 WRITESHORT(p
, STT_FILE
); /* type FILE */
1263 WRITESHORT(p
, SHN_ABS
);
1264 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1265 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1266 saa_wbytes(s
, entry
, 24L);
1271 * Now some standard symbols defining the segments, for relocation
1274 for (i
= 1; i
<= nsects
; i
++) {
1276 WRITELONG(p
, 0); /* no symbol name */
1277 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1278 WRITESHORT(p
, i
); /* section id */
1279 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1280 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1281 saa_wbytes(s
, entry
, 24L);
1288 * Now the other local symbols.
1291 while ((sym
= saa_rstruct(syms
))) {
1292 if (sym
->type
& SYM_GLOBAL
)
1295 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1296 WRITECHAR(p
, sym
->type
); /* type and binding */
1297 WRITECHAR(p
, sym
->other
); /* visibility */
1298 WRITESHORT(p
, sym
->section
); /* index into section header table */
1299 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1300 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1301 saa_wbytes(s
, entry
, 24L);
1306 * dwarf needs symbols for debug sections
1307 * which are relocation targets.
1309 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1310 dwarf_infosym
= *local
;
1312 WRITELONG(p
, 0); /* no symbol name */
1313 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1314 WRITESHORT(p
, debug_info
); /* section id */
1315 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1316 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1317 saa_wbytes(s
, entry
, 24L);
1320 dwarf_abbrevsym
= *local
;
1322 WRITELONG(p
, 0); /* no symbol name */
1323 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1324 WRITESHORT(p
, debug_abbrev
); /* section id */
1325 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1326 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1327 saa_wbytes(s
, entry
, 24L);
1330 dwarf_linesym
= *local
;
1332 WRITELONG(p
, 0); /* no symbol name */
1333 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1334 WRITESHORT(p
, debug_line
); /* section id */
1335 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1336 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1337 saa_wbytes(s
, entry
, 24L);
1343 * Now the global symbols.
1346 while ((sym
= saa_rstruct(syms
))) {
1347 if (!(sym
->type
& SYM_GLOBAL
))
1350 WRITELONG(p
, sym
->strpos
);
1351 WRITECHAR(p
, sym
->type
); /* type and binding */
1352 WRITECHAR(p
, sym
->other
); /* visibility */
1353 WRITESHORT(p
, sym
->section
);
1354 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1355 WRITEDLONG(p
, (int64_t)sym
->size
);
1356 saa_wbytes(s
, entry
, 24L);
1363 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1366 uint8_t *p
, entry
[24];
1367 int32_t global_offset
;
1376 * How to onvert from a global placeholder to a real symbol index;
1377 * the +2 refers to the two special entries, the null entry and
1378 * the filename entry.
1380 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1383 int32_t sym
= r
->symbol
;
1385 if (sym
>= GLOBAL_TEMP_BASE
)
1386 sym
+= global_offset
;
1389 WRITEDLONG(p
, r
->address
);
1390 WRITELONG(p
, r
->type
);
1392 WRITEDLONG(p
, r
->offset
);
1393 saa_wbytes(s
, entry
, 24L);
1402 static void elf_section_header(int name
, int type
, uint64_t flags
,
1403 void *data
, bool is_saa
, uint64_t datalen
,
1404 int link
, int info
, int align
, int eltsize
)
1406 elf_sects
[elf_nsect
].data
= data
;
1407 elf_sects
[elf_nsect
].len
= datalen
;
1408 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1411 fwriteint32_t((int32_t)name
, ofile
);
1412 fwriteint32_t((int32_t)type
, ofile
);
1413 fwriteint64_t((int64_t)flags
, ofile
);
1414 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
1415 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1416 fwriteint64_t(datalen
, ofile
);
1418 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1419 fwriteint32_t((int32_t)link
, ofile
);
1420 fwriteint32_t((int32_t)info
, ofile
);
1421 fwriteint64_t((int64_t)align
, ofile
);
1422 fwriteint64_t((int64_t)eltsize
, ofile
);
1425 static void elf_write_sections(void)
1428 for (i
= 0; i
< elf_nsect
; i
++)
1429 if (elf_sects
[i
].data
) {
1430 int32_t len
= elf_sects
[i
].len
;
1431 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1432 int32_t align
= reallen
- len
;
1433 if (elf_sects
[i
].is_saa
)
1434 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1436 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1437 fwritezero(align
, ofile
);
1441 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1443 saa_wbytes(sect
->data
, data
, len
);
1446 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1448 saa_writeaddr(sect
->data
, data
, len
);
1452 static int32_t elf_segbase(int32_t segment
)
1457 static int elf_directive(enum directives directive
, char *value
, int pass
)
1463 switch (directive
) {
1466 return 1; /* ignore in pass 2 */
1468 n
= readnum(value
, &err
);
1470 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1473 if (n
< 0 || n
> 255) {
1474 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1480 if ((p
= strchr(value
,',')) == NULL
)
1483 n
= readnum(p
+1, &err
);
1484 if (err
|| n
< 0 || n
> 255) {
1485 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1497 static void elf_filename(char *inname
, char *outname
)
1499 strcpy(elf_module
, inname
);
1500 standard_extension(inname
, outname
, ".o");
1503 extern macros_t elf_stdmac
[];
1505 static int elf_set_info(enum geninfo type
, char **val
)
1511 static struct dfmt df_dwarf
= {
1512 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1522 static struct dfmt df_stabs
= {
1523 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1534 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1536 struct ofmt of_elf64
= {
1537 "ELF64 (x86_64) object files (e.g. Linux)",
1554 /* common debugging routines */
1555 static void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
,
1556 int is_global
, char *special
)
1565 static void debug64_directive(const char *directive
, const char *params
)
1571 static void debug64_typevalue(int32_t type
)
1573 int32_t stype
, ssize
;
1574 switch (TYM_TYPE(type
)) {
1617 stype
= STT_SECTION
;
1632 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1633 lastsym
->size
= ssize
;
1634 lastsym
->type
= stype
;
1638 /* stabs debugging routines */
1640 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1643 if (!stabs_filename
) {
1644 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1645 strcpy(stabs_filename
, filename
);
1647 if (strcmp(stabs_filename
, filename
)) {
1648 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1649 in fact, this leak comes in quite handy to maintain a list of files
1650 encountered so far in the symbol lines... */
1652 /* why not nasm_free(stabs_filename); we're done with the old one */
1654 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1655 strcpy(stabs_filename
, filename
);
1659 currentline
= linenumber
;
1663 static void stabs64_output(int type
, void *param
)
1665 struct symlininfo
*s
;
1666 struct linelist
*el
;
1667 if (type
== TY_DEBUGSYMLIN
) {
1668 if (debug_immcall
) {
1669 s
= (struct symlininfo
*)param
;
1670 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1671 return; /* line info is only collected for executable sections */
1673 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1674 el
->info
.offset
= s
->offset
;
1675 el
->info
.section
= s
->section
;
1676 el
->info
.name
= s
->name
;
1677 el
->line
= currentline
;
1678 el
->filename
= stabs_filename
;
1681 stabslines
->last
->next
= el
;
1682 stabslines
->last
= el
;
1685 stabslines
->last
= el
;
1692 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1694 WRITELONG(p,n_strx); \
1695 WRITECHAR(p,n_type); \
1696 WRITECHAR(p,n_other); \
1697 WRITESHORT(p,n_desc); \
1698 WRITELONG(p,n_value); \
1701 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1703 static void stabs64_generate(void)
1705 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1706 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1710 struct linelist
*ptr
;
1714 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1715 for (i
= 0; i
< numlinestabs
; i
++)
1719 if (numfiles
== 0) {
1720 allfiles
[0] = ptr
->filename
;
1723 for (i
= 0; i
< numfiles
; i
++) {
1724 if (!strcmp(allfiles
[i
], ptr
->filename
))
1727 if (i
>= numfiles
) {
1728 allfiles
[i
] = ptr
->filename
;
1735 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1736 for (i
= 0; i
< numfiles
; i
++) {
1737 fileidx
[i
] = strsize
;
1738 strsize
+= strlen(allfiles
[i
]) + 1;
1741 for (i
= 0; i
< numfiles
; i
++) {
1742 if (!strcmp(allfiles
[i
], elf_module
)) {
1749 * worst case size of the stab buffer would be:
1750 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1751 * plus one "ending" entry
1753 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1754 sizeof(struct stabentry
));
1755 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1756 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1759 for (i
= 0; i
< numfiles
; i
++)
1760 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1763 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1771 * this is the first stab, its strx points to the filename of the
1772 * the source-file, the n_desc field should be set to the number
1773 * of remaining stabs
1775 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1777 /* this is the stab for the main source file */
1778 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1780 /* relocation table entry */
1783 * Since the symbol table has two entries before
1784 * the section symbols, the index in the info.section
1785 * member must be adjusted by adding 2
1788 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1789 WRITELONG(rptr
, R_X86_64_32
);
1790 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1793 currfile
= mainfileindex
;
1797 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1798 /* oops file has changed... */
1799 for (i
= 0; i
< numfiles
; i
++)
1800 if (!strcmp(allfiles
[i
], ptr
->filename
))
1803 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1807 /* relocation table entry */
1809 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1810 WRITELONG(rptr
, R_X86_64_32
);
1811 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1814 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1817 /* relocation table entry */
1819 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1820 WRITELONG(rptr
, R_X86_64_32
);
1821 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1827 /* this is an "ending" token */
1828 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1831 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1833 nasm_free(allfiles
);
1836 stablen
= (sptr
- sbuf
);
1837 stabrellen
= (rptr
- rbuf
);
1843 static void stabs64_cleanup(void)
1845 struct linelist
*ptr
, *del
;
1857 nasm_free(stabrelbuf
);
1858 nasm_free(stabstrbuf
);
1861 /* dwarf routines */
1863 static void dwarf64_init(void)
1865 ndebugs
= 3; /* 3 debug symbols */
1868 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1872 dwarf64_findfile(filename
);
1874 currentline
= linenumber
;
1877 /* called from elf_out with type == TY_DEBUGSYMLIN */
1878 static void dwarf64_output(int type
, void *param
)
1880 int ln
, aa
, inx
, maxln
, soc
;
1881 struct symlininfo
*s
;
1886 s
= (struct symlininfo
*)param
;
1888 /* line number info is only gathered for executable sections */
1889 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1892 /* Check if section index has changed */
1893 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1894 dwarf64_findsect(s
->section
);
1896 /* do nothing unless line or file has changed */
1900 ln
= currentline
- dwarf_csect
->line
;
1901 aa
= s
->offset
- dwarf_csect
->offset
;
1902 inx
= dwarf_clist
->line
;
1903 plinep
= dwarf_csect
->psaa
;
1904 /* check for file change */
1905 if (!(inx
== dwarf_csect
->file
)) {
1906 saa_write8(plinep
,DW_LNS_set_file
);
1907 saa_write8(plinep
,inx
);
1908 dwarf_csect
->file
= inx
;
1910 /* check for line change */
1912 /* test if in range of special op code */
1913 maxln
= line_base
+ line_range
;
1914 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1915 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1916 saa_write8(plinep
,soc
);
1918 saa_write8(plinep
,DW_LNS_advance_line
);
1919 saa_wleb128s(plinep
,ln
);
1921 saa_write8(plinep
,DW_LNS_advance_pc
);
1922 saa_wleb128u(plinep
,aa
);
1925 dwarf_csect
->line
= currentline
;
1926 dwarf_csect
->offset
= s
->offset
;
1929 /* show change handled */
1934 static void dwarf64_generate(void)
1938 struct linelist
*ftentry
;
1939 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1940 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1941 struct sectlist
*psect
;
1942 size_t saalen
, linepoff
, totlen
, highaddr
;
1944 /* write epilogues for each line program range */
1945 /* and build aranges section */
1946 paranges
= saa_init(1L);
1947 parangesrel
= saa_init(1L);
1948 saa_write16(paranges
,3); /* dwarf version */
1949 saa_write64(parangesrel
, paranges
->datalen
+4);
1950 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1951 saa_write64(parangesrel
, 0);
1952 saa_write32(paranges
,0); /* offset into info */
1953 saa_write8(paranges
,8); /* pointer size */
1954 saa_write8(paranges
,0); /* not segmented */
1955 saa_write32(paranges
,0); /* padding */
1956 /* iterate though sectlist entries */
1957 psect
= dwarf_fsect
;
1960 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1962 plinep
= psect
->psaa
;
1963 /* Line Number Program Epilogue */
1964 saa_write8(plinep
,2); /* std op 2 */
1965 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1966 saa_write8(plinep
,DW_LNS_extended_op
);
1967 saa_write8(plinep
,1); /* operand length */
1968 saa_write8(plinep
,DW_LNE_end_sequence
);
1969 totlen
+= plinep
->datalen
;
1970 /* range table relocation entry */
1971 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1972 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1973 saa_write64(parangesrel
, (uint64_t) 0);
1974 /* range table entry */
1975 saa_write64(paranges
,0x0000); /* range start */
1976 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1977 highaddr
+= sects
[psect
->section
]->len
;
1978 /* done with this entry */
1979 psect
= psect
->next
;
1981 saa_write64(paranges
,0); /* null address */
1982 saa_write64(paranges
,0); /* null length */
1983 saalen
= paranges
->datalen
;
1984 arangeslen
= saalen
+ 4;
1985 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1986 WRITELONG(pbuf
,saalen
); /* initial length */
1987 saa_rnbytes(paranges
, pbuf
, saalen
);
1990 /* build rela.aranges section */
1991 arangesrellen
= saalen
= parangesrel
->datalen
;
1992 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1993 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1994 saa_free(parangesrel
);
1996 /* build pubnames section */
1997 ppubnames
= saa_init(1L);
1998 saa_write16(ppubnames
,3); /* dwarf version */
1999 saa_write32(ppubnames
,0); /* offset into info */
2000 saa_write32(ppubnames
,0); /* space used in info */
2001 saa_write32(ppubnames
,0); /* end of list */
2002 saalen
= ppubnames
->datalen
;
2003 pubnameslen
= saalen
+ 4;
2004 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2005 WRITELONG(pbuf
,saalen
); /* initial length */
2006 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2007 saa_free(ppubnames
);
2009 /* build info section */
2010 pinfo
= saa_init(1L);
2011 pinforel
= saa_init(1L);
2012 saa_write16(pinfo
,3); /* dwarf version */
2013 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2014 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2015 saa_write64(pinforel
, 0);
2016 saa_write32(pinfo
,0); /* offset into abbrev */
2017 saa_write8(pinfo
,8); /* pointer size */
2018 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2019 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2020 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2021 saa_write64(pinforel
, 0);
2022 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2023 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2024 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2025 saa_write64(pinforel
, 0);
2026 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2027 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2028 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2029 saa_write64(pinforel
, 0);
2030 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2031 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2032 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2033 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2034 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2035 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2036 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2037 saa_write64(pinforel
, 0);
2038 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2039 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2040 saa_write8(pinfo
,0); /* end of entries */
2041 saalen
= pinfo
->datalen
;
2042 infolen
= saalen
+ 4;
2043 infobuf
= pbuf
= nasm_malloc(infolen
);
2044 WRITELONG(pbuf
,saalen
); /* initial length */
2045 saa_rnbytes(pinfo
, pbuf
, saalen
);
2048 /* build rela.info section */
2049 inforellen
= saalen
= pinforel
->datalen
;
2050 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2051 saa_rnbytes(pinforel
, pbuf
, saalen
);
2054 /* build abbrev section */
2055 pabbrev
= saa_init(1L);
2056 saa_write8(pabbrev
,1); /* entry number LEB128u */
2057 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2058 saa_write8(pabbrev
,1); /* has children */
2059 /* the following attributes and forms are all LEB128u values */
2060 saa_write8(pabbrev
,DW_AT_low_pc
);
2061 saa_write8(pabbrev
,DW_FORM_addr
);
2062 saa_write8(pabbrev
,DW_AT_high_pc
);
2063 saa_write8(pabbrev
,DW_FORM_addr
);
2064 saa_write8(pabbrev
,DW_AT_stmt_list
);
2065 saa_write8(pabbrev
,DW_FORM_data4
);
2066 saa_write8(pabbrev
,DW_AT_name
);
2067 saa_write8(pabbrev
,DW_FORM_string
);
2068 saa_write8(pabbrev
,DW_AT_producer
);
2069 saa_write8(pabbrev
,DW_FORM_string
);
2070 saa_write8(pabbrev
,DW_AT_language
);
2071 saa_write8(pabbrev
,DW_FORM_data2
);
2072 saa_write16(pabbrev
,0); /* end of entry */
2073 /* LEB128u usage same as above */
2074 saa_write8(pabbrev
,2); /* entry number */
2075 saa_write8(pabbrev
,DW_TAG_subprogram
);
2076 saa_write8(pabbrev
,0); /* no children */
2077 saa_write8(pabbrev
,DW_AT_low_pc
);
2078 saa_write8(pabbrev
,DW_FORM_addr
);
2079 saa_write8(pabbrev
,DW_AT_frame_base
);
2080 saa_write8(pabbrev
,DW_FORM_data4
);
2081 saa_write16(pabbrev
,0); /* end of entry */
2082 abbrevlen
= saalen
= pabbrev
->datalen
;
2083 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2084 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2087 /* build line section */
2089 plines
= saa_init(1L);
2090 saa_write8(plines
,1); /* Minimum Instruction Length */
2091 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2092 saa_write8(plines
,line_base
); /* Line Base */
2093 saa_write8(plines
,line_range
); /* Line Range */
2094 saa_write8(plines
,opcode_base
); /* Opcode Base */
2095 /* standard opcode lengths (# of LEB128u operands) */
2096 saa_write8(plines
,0); /* Std opcode 1 length */
2097 saa_write8(plines
,1); /* Std opcode 2 length */
2098 saa_write8(plines
,1); /* Std opcode 3 length */
2099 saa_write8(plines
,1); /* Std opcode 4 length */
2100 saa_write8(plines
,1); /* Std opcode 5 length */
2101 saa_write8(plines
,0); /* Std opcode 6 length */
2102 saa_write8(plines
,0); /* Std opcode 7 length */
2103 saa_write8(plines
,0); /* Std opcode 8 length */
2104 saa_write8(plines
,1); /* Std opcode 9 length */
2105 saa_write8(plines
,0); /* Std opcode 10 length */
2106 saa_write8(plines
,0); /* Std opcode 11 length */
2107 saa_write8(plines
,1); /* Std opcode 12 length */
2108 /* Directory Table */
2109 saa_write8(plines
,0); /* End of table */
2110 /* File Name Table */
2111 ftentry
= dwarf_flist
;
2112 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2114 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2115 saa_write8(plines
,0); /* directory LEB128u */
2116 saa_write8(plines
,0); /* time LEB128u */
2117 saa_write8(plines
,0); /* size LEB128u */
2118 ftentry
= ftentry
->next
;
2120 saa_write8(plines
,0); /* End of table */
2121 linepoff
= plines
->datalen
;
2122 linelen
= linepoff
+ totlen
+ 10;
2123 linebuf
= pbuf
= nasm_malloc(linelen
);
2124 WRITELONG(pbuf
,linelen
-4); /* initial length */
2125 WRITESHORT(pbuf
,3); /* dwarf version */
2126 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2127 /* write line header */
2129 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2132 /* concatonate line program ranges */
2134 plinesrel
= saa_init(1L);
2135 psect
= dwarf_fsect
;
2136 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2137 saa_write64(plinesrel
, linepoff
);
2138 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2139 saa_write64(plinesrel
, (uint64_t) 0);
2140 plinep
= psect
->psaa
;
2141 saalen
= plinep
->datalen
;
2142 saa_rnbytes(plinep
, pbuf
, saalen
);
2146 /* done with this entry */
2147 psect
= psect
->next
;
2151 /* build rela.lines section */
2152 linerellen
=saalen
= plinesrel
->datalen
;
2153 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2154 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2155 saa_free(plinesrel
);
2157 /* build frame section */
2159 framebuf
= pbuf
= nasm_malloc(framelen
);
2160 WRITELONG(pbuf
,framelen
-4); /* initial length */
2162 /* build loc section */
2164 locbuf
= pbuf
= nasm_malloc(loclen
);
2165 WRITEDLONG(pbuf
,0); /* null beginning offset */
2166 WRITEDLONG(pbuf
,0); /* null ending offset */
2169 static void dwarf64_cleanup(void)
2171 nasm_free(arangesbuf
);
2172 nasm_free(arangesrelbuf
);
2173 nasm_free(pubnamesbuf
);
2175 nasm_free(inforelbuf
);
2176 nasm_free(abbrevbuf
);
2178 nasm_free(linerelbuf
);
2179 nasm_free(framebuf
);
2183 static void dwarf64_findfile(const char * fname
)
2186 struct linelist
*match
;
2188 /* return if fname is current file name */
2189 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2192 /* search for match */
2195 match
= dwarf_flist
;
2196 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2197 if (!(strcmp(fname
, match
->filename
))) {
2198 dwarf_clist
= match
;
2204 /* add file name to end of list */
2205 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2207 dwarf_clist
->line
= dwarf_numfiles
;
2208 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2209 strcpy(dwarf_clist
->filename
,fname
);
2210 dwarf_clist
->next
= 0;
2211 if (!dwarf_flist
) { /* if first entry */
2212 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2213 dwarf_clist
->last
= 0;
2214 } else { /* chain to previous entry */
2215 dwarf_elist
->next
= dwarf_clist
;
2216 dwarf_elist
= dwarf_clist
;
2220 static void dwarf64_findsect(const int index
)
2223 struct sectlist
*match
;
2226 /* return if index is current section index */
2227 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2230 /* search for match */
2233 match
= dwarf_fsect
;
2234 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2235 if ((match
->section
== index
)) {
2236 dwarf_csect
= match
;
2239 match
= match
->next
;
2243 /* add entry to end of list */
2244 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2246 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2247 dwarf_csect
->line
= 1;
2248 dwarf_csect
->offset
= 0;
2249 dwarf_csect
->file
= 1;
2250 dwarf_csect
->section
= index
;
2251 dwarf_csect
->next
= 0;
2252 /* set relocatable address at start of line program */
2253 saa_write8(plinep
,DW_LNS_extended_op
);
2254 saa_write8(plinep
,9); /* operand length */
2255 saa_write8(plinep
,DW_LNE_set_address
);
2256 saa_write64(plinep
,0); /* Start Address */
2258 if (!dwarf_fsect
) { /* if first entry */
2259 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2260 dwarf_csect
->last
= 0;
2261 } else { /* chain to previous entry */
2262 dwarf_esect
->next
= dwarf_csect
;
2263 dwarf_esect
= dwarf_csect
;