1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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"
64 static struct elf_section
**sects
;
65 static int nsects
, sectlen
;
67 #define SHSTR_DELTA 256
68 static char *shstrtab
;
69 static int shstrtablen
, shstrtabsize
;
71 static struct SAA
*syms
;
72 static uint32_t nlocals
, nglobs
, ndebugs
; /* Symbol counts */
74 static int32_t def_seg
;
76 static struct RAA
*bsym
;
78 static struct SAA
*strs
;
79 static uint32_t strslen
;
81 static struct elf_symbol
*fwds
;
83 static char elf_module
[FILENAME_MAX
];
85 extern struct ofmt of_elf64
;
87 static struct ELF_SECTDATA
{
92 static int elf_nsect
, nsections
;
93 static int64_t elf_foffs
;
95 static void elf_write(void);
96 static void elf_sect_write(struct elf_section
*, const void *, size_t);
97 static void elf_sect_writeaddr(struct elf_section
*, int64_t, size_t);
98 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
100 static void elf_write_sections(void);
101 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
102 static struct SAA
*elf_build_reltab(uint64_t *, struct elf_reloc
*);
103 static void add_sectname(char *, char *);
111 int section
; /* index into sects[] */
112 int segto
; /* internal section number */
113 char *name
; /* shallow-copied pointer of section name */
117 struct linelist
*next
;
118 struct linelist
*last
;
119 struct symlininfo info
;
130 struct sectlist
*next
;
131 struct sectlist
*last
;
134 /* common debug variables */
135 static int currentline
= 1;
136 static int debug_immcall
= 0;
138 /* stabs debug variables */
139 static struct linelist
*stabslines
= 0;
140 static int numlinestabs
= 0;
141 static char *stabs_filename
= 0;
142 static int symtabsection
;
143 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
144 static int stablen
, stabstrlen
, stabrellen
;
146 /* dwarf debug variables */
147 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
148 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
149 static int dwarf_numfiles
= 0, dwarf_nsections
;
150 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
151 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
152 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
153 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
154 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
155 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
158 static struct dfmt df_dwarf
;
159 static struct dfmt df_stabs
;
160 static struct elf_symbol
*lastsym
;
162 /* common debugging routines */
163 static void debug64_typevalue(int32_t);
165 /* stabs debugging routines */
166 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
167 static void stabs64_output(int, void *);
168 static void stabs64_generate(void);
169 static void stabs64_cleanup(void);
171 /* dwarf debugging routines */
172 static void dwarf64_init(void);
173 static void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
174 static void dwarf64_output(int, void *);
175 static void dwarf64_generate(void);
176 static void dwarf64_cleanup(void);
177 static void dwarf64_findfile(const char *);
178 static void dwarf64_findsect(const int);
181 * Special section numbers which are used to define ELF special
182 * symbols, which can be used with WRT to provide PIC relocation
185 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
186 static int32_t elf_got_sect
, elf_plt_sect
;
187 static int32_t elf_sym_sect
;
188 static int32_t elf_gottpoff_sect
;
190 static void elf_init(void)
193 nsects
= sectlen
= 0;
194 syms
= saa_init((int32_t)sizeof(struct elf_symbol
));
195 nlocals
= nglobs
= ndebugs
= 0;
198 saa_wbytes(strs
, "\0", 1L);
199 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
200 strslen
= 2 + strlen(elf_module
);
202 shstrtablen
= shstrtabsize
= 0;;
203 add_sectname("", "");
207 elf_gotpc_sect
= seg_alloc();
208 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
209 elf_gotoff_sect
= seg_alloc();
210 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
211 elf_got_sect
= seg_alloc();
212 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
213 elf_plt_sect
= seg_alloc();
214 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
215 elf_sym_sect
= seg_alloc();
216 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
217 elf_gottpoff_sect
= seg_alloc();
218 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
220 def_seg
= seg_alloc();
224 static void elf_cleanup(void)
230 for (i
= 0; i
< nsects
; i
++) {
231 if (sects
[i
]->type
!= SHT_NOBITS
)
232 saa_free(sects
[i
]->data
);
234 saa_free(sects
[i
]->rel
);
235 while (sects
[i
]->head
) {
237 sects
[i
]->head
= sects
[i
]->head
->next
;
248 /* add entry to the elf .shstrtab section */
249 static void add_sectname(char *firsthalf
, char *secondhalf
)
251 int len
= strlen(firsthalf
) + strlen(secondhalf
);
252 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
253 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
254 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
255 strcat(shstrtab
+ shstrtablen
, secondhalf
);
256 shstrtablen
+= len
+ 1;
259 static int elf_make_section(char *name
, int type
, int flags
, int align
)
261 struct elf_section
*s
;
263 s
= nasm_zalloc(sizeof(*s
));
265 if (type
!= SHT_NOBITS
)
266 s
->data
= saa_init(1L);
268 if (!strcmp(name
, ".text"))
271 s
->index
= seg_alloc();
272 add_sectname("", name
);
274 s
->name
= nasm_strdup(name
);
279 if (nsects
>= sectlen
)
280 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
286 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
289 uint32_t flags
, flags_and
, flags_or
;
294 * Default is 64 bits.
301 p
= nasm_skip_word(name
);
304 flags_and
= flags_or
= type
= align
= 0;
306 elf_section_attrib(name
, p
, pass
, &flags_and
,
307 &flags_or
, &align
, &type
);
309 if (!strcmp(name
, ".shstrtab") ||
310 !strcmp(name
, ".symtab") ||
311 !strcmp(name
, ".strtab")) {
312 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
317 for (i
= 0; i
< nsects
; i
++)
318 if (!strcmp(name
, sects
[i
]->name
))
321 const struct elf_known_section
*ks
= elf_known_sections
;
324 if (!strcmp(name
, ks
->name
))
329 type
= type
? type
: ks
->type
;
330 align
= align
? align
: ks
->align
;
331 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
333 i
= elf_make_section(name
, type
, flags
, align
);
334 } else if (pass
== 1) {
335 if ((type
&& sects
[i
]->type
!= type
)
336 || (align
&& sects
[i
]->align
!= align
)
337 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
338 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
339 " redeclaration of section `%s'", name
);
342 return sects
[i
]->index
;
345 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
346 int is_global
, char *special
)
349 struct elf_symbol
*sym
;
350 bool special_used
= false;
352 #if defined(DEBUG) && DEBUG>2
353 nasm_error(ERR_DEBUG
,
354 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
355 name
, segment
, offset
, is_global
, special
);
357 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
359 * This is a NASM special symbol. We never allow it into
360 * the ELF symbol table, even if it's a valid one. If it
361 * _isn't_ a valid one, we should barf immediately.
363 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
364 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
365 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
366 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
370 if (is_global
== 3) {
371 struct elf_symbol
**s
;
373 * Fix up a forward-reference symbol size from the first
376 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
377 if (!strcmp((*s
)->name
, name
)) {
378 struct tokenval tokval
;
380 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
384 tokval
.t_type
= TOKEN_INVALID
;
385 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, NULL
);
388 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
389 " expression as symbol size");
391 (*s
)->size
= reloc_value(e
);
395 * Remove it from the list of unresolved sizes.
397 nasm_free((*s
)->name
);
401 return; /* it wasn't an important one */
404 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
405 strslen
+= 1 + strlen(name
);
407 lastsym
= sym
= saa_wstruct(syms
);
409 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
412 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
413 sym
->other
= STV_DEFAULT
;
415 if (segment
== NO_SEG
)
416 sym
->section
= SHN_ABS
;
419 sym
->section
= SHN_UNDEF
;
420 if (segment
== def_seg
) {
421 /* we have to be sure at least text section is there */
423 if (segment
!= elf_section_names(".text", 2, &tempint
))
424 nasm_panic(0, "strange segment conditions in ELF driver");
426 for (i
= 0; i
< nsects
; i
++) {
427 if (segment
== sects
[i
]->index
) {
428 sym
->section
= i
+ 1;
434 if (is_global
== 2) {
437 sym
->section
= SHN_COMMON
;
439 * We have a common variable. Check the special text to see
440 * if it's a valid number and power of two; if so, store it
441 * as the alignment for the common variable.
445 sym
->symv
.key
= readnum(special
, &err
);
447 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
448 " valid number", special
);
449 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
450 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
451 " power of two", special
);
455 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
457 if (sym
->type
== SYM_GLOBAL
) {
459 * If sym->section == SHN_ABS, then the first line of the
460 * else section would cause a core dump, because its a reference
461 * beyond the end of the section array.
462 * This behaviour is exhibited by this code:
465 * To avoid such a crash, such requests are silently discarded.
466 * This may not be the best solution.
468 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
469 bsym
= raa_write(bsym
, segment
, nglobs
);
470 } else if (sym
->section
!= SHN_ABS
) {
472 * This is a global symbol; so we must add it to the rbtree
473 * of global symbols in its section.
475 * In addition, we check the special text for symbol
476 * type and size information.
478 sects
[sym
->section
-1]->gsyms
=
479 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
482 int n
= strcspn(special
, " \t");
484 if (!nasm_strnicmp(special
, "function", n
))
485 sym
->type
|= STT_FUNC
;
486 else if (!nasm_strnicmp(special
, "data", n
) ||
487 !nasm_strnicmp(special
, "object", n
))
488 sym
->type
|= STT_OBJECT
;
489 else if (!nasm_strnicmp(special
, "notype", n
))
490 sym
->type
|= STT_NOTYPE
;
492 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
496 special
= nasm_skip_spaces(special
);
498 n
= strcspn(special
, " \t");
499 if (!nasm_strnicmp(special
, "default", n
))
500 sym
->other
= STV_DEFAULT
;
501 else if (!nasm_strnicmp(special
, "internal", n
))
502 sym
->other
= STV_INTERNAL
;
503 else if (!nasm_strnicmp(special
, "hidden", n
))
504 sym
->other
= STV_HIDDEN
;
505 else if (!nasm_strnicmp(special
, "protected", n
))
506 sym
->other
= STV_PROTECTED
;
513 struct tokenval tokval
;
516 char *saveme
= stdscan_get();
518 while (special
[n
] && nasm_isspace(special
[n
]))
521 * We have a size expression; attempt to
525 stdscan_set(special
+ n
);
526 tokval
.t_type
= TOKEN_INVALID
;
527 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, NULL
);
531 sym
->name
= nasm_strdup(name
);
534 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
535 " expression as symbol size");
537 sym
->size
= reloc_value(e
);
544 * If TLS segment, mark symbol accordingly.
546 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
548 sym
->type
|= STT_TLS
;
551 sym
->globnum
= nglobs
;
556 if (special
&& !special_used
)
557 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
560 static void elf_add_reloc(struct elf_section
*sect
, int32_t segment
,
561 int64_t offset
, int type
)
565 r
= *sect
->tail
= nasm_zalloc(sizeof(struct elf_reloc
));
566 sect
->tail
= &r
->next
;
568 r
->address
= sect
->len
;
571 if (segment
!= NO_SEG
) {
573 for (i
= 0; i
< nsects
; i
++)
574 if (segment
== sects
[i
]->index
)
577 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
585 * This routine deals with ..got and ..sym relocations: the more
586 * complicated kinds. In shared-library writing, some relocations
587 * with respect to global symbols must refer to the precise symbol
588 * rather than referring to an offset from the base of the section
589 * _containing_ the symbol. Such relocations call to this routine,
590 * which searches the symbol list for the symbol in question.
592 * R_386_GOT32 references require the _exact_ symbol address to be
593 * used; R_386_32 references can be at an offset from the symbol.
594 * The boolean argument `exact' tells us this.
596 * Return value is the adjusted value of `addr', having become an
597 * offset from the symbol rather than the section. Should always be
598 * zero when returning from an exact call.
600 * Limitation: if you define two symbols at the same place,
601 * confusion will occur.
603 * Inefficiency: we search, currently, using a linked list which
604 * isn't even necessarily sorted.
606 static void elf_add_gsym_reloc(struct elf_section
*sect
,
607 int32_t segment
, uint64_t offset
, int64_t pcrel
,
608 int type
, bool exact
)
611 struct elf_section
*s
;
612 struct elf_symbol
*sym
;
617 * First look up the segment/offset pair and find a global
618 * symbol corresponding to it. If it's not one of our segments,
619 * then it must be an external symbol, in which case we're fine
620 * doing a normal elf_add_reloc after first sanity-checking
621 * that the offset from the symbol is zero.
624 for (i
= 0; i
< nsects
; i
++)
625 if (segment
== sects
[i
]->index
) {
632 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
634 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
638 srb
= rb_search(s
->gsyms
, offset
);
639 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
640 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
641 " for this reference");
644 sym
= container_of(srb
, struct elf_symbol
, symv
);
646 r
= *sect
->tail
= nasm_malloc(sizeof(struct elf_reloc
));
647 sect
->tail
= &r
->next
;
650 r
->address
= sect
->len
;
651 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
652 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
658 static void elf_out(int32_t segto
, const void *data
,
659 enum out_type type
, uint64_t size
,
660 int32_t segment
, int32_t wrt
)
662 struct elf_section
*s
;
666 static struct symlininfo sinfo
;
668 #if defined(DEBUG) && DEBUG>2
670 nasm_error(ERR_DEBUG
,
671 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
672 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
674 nasm_error(ERR_DEBUG
,
675 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
676 currentline
, type
, segment
, segto
, size
);
680 * handle absolute-assembly (structure definitions)
682 if (segto
== NO_SEG
) {
683 if (type
!= OUT_RESERVE
)
684 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
690 for (i
= 0; i
< nsects
; i
++)
691 if (segto
== sects
[i
]->index
) {
696 int tempint
; /* ignored */
697 if (segto
!= elf_section_names(".text", 2, &tempint
))
698 nasm_panic(0, "strange segment conditions in ELF driver");
700 s
= sects
[nsects
- 1];
705 /* again some stabs debugging stuff */
706 sinfo
.offset
= s
->len
;
709 sinfo
.name
= s
->name
;
710 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
711 /* end of debugging stuff */
713 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
714 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
715 " BSS section `%s': ignored", s
->name
);
716 s
->len
+= realsize(type
, size
);
722 if (s
->type
== SHT_PROGBITS
) {
723 nasm_error(ERR_WARNING
, "uninitialized space declared in"
724 " non-BSS section `%s': zeroing", s
->name
);
725 elf_sect_write(s
, NULL
, size
);
731 if (segment
!= NO_SEG
)
732 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
733 elf_sect_write(s
, data
, size
);
738 int isize
= (int)size
;
739 int asize
= abs((int)size
);
741 addr
= *(int64_t *)data
;
742 if (segment
== NO_SEG
) {
744 } else if (segment
% 2) {
745 nasm_error(ERR_NONFATAL
, "ELF format does not support"
746 " segment base references");
752 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
756 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
759 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
762 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
766 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
769 nasm_panic(0, "internal error elf64-hpa-871");
773 } else if (wrt
== elf_gotpc_sect
+ 1) {
775 * The user will supply GOT relative to $$. ELF
776 * will let us have GOT relative to $. So we
777 * need to fix up the data item by $-$$.
780 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
782 } else if (wrt
== elf_gotoff_sect
+ 1) {
784 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
785 "references to be qword");
787 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
790 } else if (wrt
== elf_got_sect
+ 1) {
793 elf_add_gsym_reloc(s
, segment
, addr
, 0,
794 R_X86_64_GOT32
, true);
798 elf_add_gsym_reloc(s
, segment
, addr
, 0,
799 R_X86_64_GOT64
, true);
803 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
806 } else if (wrt
== elf_sym_sect
+ 1) {
810 elf_add_gsym_reloc(s
, segment
, addr
, 0,
816 elf_add_gsym_reloc(s
, segment
, addr
, 0,
821 elf_add_gsym_reloc(s
, segment
, addr
, 0,
826 elf_add_gsym_reloc(s
, segment
, addr
, 0,
827 R_X86_64_32S
, false);
832 elf_add_gsym_reloc(s
, segment
, addr
, 0,
837 nasm_panic(0, "internal error elf64-hpa-903");
840 } else if (wrt
== elf_plt_sect
+ 1) {
841 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
842 "relative PLT references");
844 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
848 elf_sect_writeaddr(s
, addr
, asize
);
853 reltype
= R_X86_64_PC8
;
858 reltype
= R_X86_64_PC16
;
863 addr
= *(int64_t *)data
- size
;
864 if (segment
== segto
)
865 nasm_panic(0, "intra-segment OUT_REL1ADR");
866 if (segment
== NO_SEG
) {
868 } else if (segment
% 2) {
869 nasm_error(ERR_NONFATAL
, "ELF format does not support"
870 " segment base references");
873 elf_add_reloc(s
, segment
, addr
, reltype
);
876 nasm_error(ERR_NONFATAL
,
877 "Unsupported non-32-bit ELF relocation");
880 elf_sect_writeaddr(s
, addr
, bytes
);
884 addr
= *(int64_t *)data
- size
;
885 if (segment
== segto
)
886 nasm_panic(0, "intra-segment OUT_REL4ADR");
887 if (segment
== NO_SEG
) {
889 } else if (segment
% 2) {
890 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
891 " segment base references");
894 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
896 } else if (wrt
== elf_plt_sect
+ 1) {
897 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
898 R_X86_64_PLT32
, true);
900 } else if (wrt
== elf_gotpc_sect
+ 1 ||
901 wrt
== elf_got_sect
+ 1) {
902 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
903 R_X86_64_GOTPCREL
, true);
905 } else if (wrt
== elf_gotoff_sect
+ 1 ||
906 wrt
== elf_got_sect
+ 1) {
907 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
909 } else if (wrt
== elf_gottpoff_sect
+ 1) {
910 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
911 R_X86_64_GOTTPOFF
, true);
914 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
918 elf_sect_writeaddr(s
, addr
, 4);
922 addr
= *(int64_t *)data
- size
;
923 if (segment
== segto
)
924 nasm_panic(0, "intra-segment OUT_REL8ADR");
925 if (segment
== NO_SEG
) {
927 } else if (segment
% 2) {
928 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
929 " segment base references");
932 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
934 } else if (wrt
== elf_gotpc_sect
+ 1 ||
935 wrt
== elf_got_sect
+ 1) {
936 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
937 R_X86_64_GOTPCREL64
, true);
939 } else if (wrt
== elf_gotoff_sect
+ 1 ||
940 wrt
== elf_got_sect
+ 1) {
941 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
943 } else if (wrt
== elf_gottpoff_sect
+ 1) {
944 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
947 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
951 elf_sect_writeaddr(s
, addr
, 8);
956 static void elf_write(void)
963 int32_t symtablen
, symtablocal
;
966 * Work out how many sections we will have. We have SHN_UNDEF,
967 * then the flexible user sections, then the fixed sections
968 * `.shstrtab', `.symtab' and `.strtab', then optionally
969 * relocation sections for the user sections.
971 nsections
= sec_numspecial
+ 1;
972 if (dfmt
== &df_stabs
)
974 else if (dfmt
== &df_dwarf
)
977 add_sectname("", ".shstrtab");
978 add_sectname("", ".symtab");
979 add_sectname("", ".strtab");
980 for (i
= 0; i
< nsects
; i
++) {
981 nsections
++; /* for the section itself */
982 if (sects
[i
]->head
) {
983 nsections
++; /* for its relocations */
984 add_sectname(".rela", sects
[i
]->name
);
988 if (dfmt
== &df_stabs
) {
989 /* in case the debug information is wanted, just add these three sections... */
990 add_sectname("", ".stab");
991 add_sectname("", ".stabstr");
992 add_sectname(".rel", ".stab");
995 else if (dfmt
== &df_dwarf
) {
996 /* the dwarf debug standard specifies the following ten sections,
997 not all of which are currently implemented,
998 although all of them are defined. */
999 #define debug_aranges (int64_t) (nsections-10)
1000 #define debug_info (int64_t) (nsections-7)
1001 #define debug_abbrev (int64_t) (nsections-5)
1002 #define debug_line (int64_t) (nsections-4)
1003 add_sectname("", ".debug_aranges");
1004 add_sectname(".rela", ".debug_aranges");
1005 add_sectname("", ".debug_pubnames");
1006 add_sectname("", ".debug_info");
1007 add_sectname(".rela", ".debug_info");
1008 add_sectname("", ".debug_abbrev");
1009 add_sectname("", ".debug_line");
1010 add_sectname(".rela", ".debug_line");
1011 add_sectname("", ".debug_frame");
1012 add_sectname("", ".debug_loc");
1016 * Output the ELF header.
1018 nasm_write("\177ELF\2\1\1", 7, ofile
);
1019 fputc(elf_osabi
, ofile
);
1020 fputc(elf_abiver
, ofile
);
1021 fwritezero(7, ofile
);
1022 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1023 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1024 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1025 fwriteint64_t(0L, ofile
); /* no entry point */
1026 fwriteint64_t(0L, ofile
); /* no program header table */
1027 fwriteint64_t(0x40L
, ofile
); /* section headers straight after
1028 * ELF header plus alignment */
1029 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
1030 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1031 fwriteint16_t(0, ofile
); /* no program header table, again */
1032 fwriteint16_t(0, ofile
); /* still no program header table */
1033 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1034 fwriteint16_t(nsections
, ofile
); /* number of sections */
1035 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1036 * section header table */
1039 * Build the symbol table and relocation tables.
1041 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1042 for (i
= 0; i
< nsects
; i
++)
1044 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1048 * Now output the section header table.
1051 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1052 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1055 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1058 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1061 /* The normal sections */
1062 for (i
= 0; i
< nsects
; i
++) {
1063 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1064 (sects
[i
]->type
== SHT_PROGBITS
?
1065 sects
[i
]->data
: NULL
), true,
1066 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1071 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1072 shstrtablen
, 0, 0, 1, 0);
1076 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1077 symtablen
, sec_strtab
, symtablocal
, 8, 24);
1081 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1082 strslen
, 0, 0, 1, 0);
1085 /* The relocation sections */
1086 for (i
= 0; i
< nsects
; i
++)
1087 if (sects
[i
]->head
) {
1088 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1089 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 8, 24);
1093 if (dfmt
== &df_stabs
) {
1094 /* for debugging information, create the last three sections
1095 which are the .stab , .stabstr and .rel.stab sections respectively */
1097 /* this function call creates the stab sections in memory */
1100 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1101 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1102 stablen
, sec_stabstr
, 0, 4, 12);
1105 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1106 stabstrlen
, 0, 0, 4, 0);
1109 /* link -> symtable info -> section to refer to */
1110 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1111 stabrellen
, symtabsection
, sec_stab
, 4, 16);
1114 } else if (dfmt
== &df_dwarf
) {
1115 /* for dwarf debugging information, create the ten dwarf sections */
1117 /* this function call creates the dwarf sections in memory */
1121 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1122 arangeslen
, 0, 0, 1, 0);
1125 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1126 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1129 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1130 pubnameslen
, 0, 0, 1, 0);
1133 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1134 infolen
, 0, 0, 1, 0);
1137 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1138 inforellen
, symtabsection
, debug_info
, 1, 24);
1141 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1142 abbrevlen
, 0, 0, 1, 0);
1145 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1146 linelen
, 0, 0, 1, 0);
1149 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1150 linerellen
, symtabsection
, debug_line
, 1, 24);
1153 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1154 framelen
, 0, 0, 8, 0);
1157 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1158 loclen
, 0, 0, 1, 0);
1161 fwritezero(align
, ofile
);
1164 * Now output the sections.
1166 elf_write_sections();
1168 nasm_free(elf_sects
);
1172 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1174 struct SAA
*s
= saa_init(1L);
1175 struct elf_symbol
*sym
;
1176 uint8_t entry
[24], *p
;
1182 * First, an all-zeros entry, required by the ELF spec.
1184 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1189 * Next, an entry for the file name.
1192 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1193 WRITESHORT(p
, STT_FILE
); /* type FILE */
1194 WRITESHORT(p
, SHN_ABS
);
1195 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1196 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1197 saa_wbytes(s
, entry
, 24L);
1202 * Now some standard symbols defining the segments, for relocation
1205 for (i
= 1; i
<= nsects
; i
++) {
1207 WRITELONG(p
, 0); /* no symbol name */
1208 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1209 WRITESHORT(p
, i
); /* section id */
1210 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1211 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1212 saa_wbytes(s
, entry
, 24L);
1219 * Now the other local symbols.
1222 while ((sym
= saa_rstruct(syms
))) {
1223 if (sym
->type
& SYM_GLOBAL
)
1226 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1227 WRITECHAR(p
, sym
->type
); /* type and binding */
1228 WRITECHAR(p
, sym
->other
); /* visibility */
1229 WRITESHORT(p
, sym
->section
); /* index into section header table */
1230 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1231 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1232 saa_wbytes(s
, entry
, 24L);
1237 * dwarf needs symbols for debug sections
1238 * which are relocation targets.
1240 if (dfmt
== &df_dwarf
) {
1241 dwarf_infosym
= *local
;
1243 WRITELONG(p
, 0); /* no symbol name */
1244 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1245 WRITESHORT(p
, debug_info
); /* section id */
1246 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1247 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1248 saa_wbytes(s
, entry
, 24L);
1251 dwarf_abbrevsym
= *local
;
1253 WRITELONG(p
, 0); /* no symbol name */
1254 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1255 WRITESHORT(p
, debug_abbrev
); /* section id */
1256 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1257 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1258 saa_wbytes(s
, entry
, 24L);
1261 dwarf_linesym
= *local
;
1263 WRITELONG(p
, 0); /* no symbol name */
1264 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1265 WRITESHORT(p
, debug_line
); /* section id */
1266 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1267 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1268 saa_wbytes(s
, entry
, 24L);
1274 * Now the global symbols.
1277 while ((sym
= saa_rstruct(syms
))) {
1278 if (!(sym
->type
& SYM_GLOBAL
))
1281 WRITELONG(p
, sym
->strpos
);
1282 WRITECHAR(p
, sym
->type
); /* type and binding */
1283 WRITECHAR(p
, sym
->other
); /* visibility */
1284 WRITESHORT(p
, sym
->section
);
1285 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1286 WRITEDLONG(p
, (int64_t)sym
->size
);
1287 saa_wbytes(s
, entry
, 24L);
1294 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
1297 uint8_t *p
, entry
[24];
1298 int32_t global_offset
;
1307 * How to onvert from a global placeholder to a real symbol index;
1308 * the +2 refers to the two special entries, the null entry and
1309 * the filename entry.
1311 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1314 int32_t sym
= r
->symbol
;
1316 if (sym
>= GLOBAL_TEMP_BASE
)
1317 sym
+= global_offset
;
1320 WRITEDLONG(p
, r
->address
);
1321 WRITELONG(p
, r
->type
);
1323 WRITEDLONG(p
, r
->offset
);
1324 saa_wbytes(s
, entry
, 24L);
1333 static void elf_section_header(int name
, int type
, uint64_t flags
,
1334 void *data
, bool is_saa
, uint64_t datalen
,
1335 int link
, int info
, int align
, int eltsize
)
1337 elf_sects
[elf_nsect
].data
= data
;
1338 elf_sects
[elf_nsect
].len
= datalen
;
1339 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1342 fwriteint32_t((int32_t)name
, ofile
);
1343 fwriteint32_t((int32_t)type
, ofile
);
1344 fwriteint64_t((int64_t)flags
, ofile
);
1345 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
1346 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1347 fwriteint64_t(datalen
, ofile
);
1349 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1350 fwriteint32_t((int32_t)link
, ofile
);
1351 fwriteint32_t((int32_t)info
, ofile
);
1352 fwriteint64_t((int64_t)align
, ofile
);
1353 fwriteint64_t((int64_t)eltsize
, ofile
);
1356 static void elf_write_sections(void)
1359 for (i
= 0; i
< elf_nsect
; i
++)
1360 if (elf_sects
[i
].data
) {
1361 int32_t len
= elf_sects
[i
].len
;
1362 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1363 int32_t align
= reallen
- len
;
1364 if (elf_sects
[i
].is_saa
)
1365 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1367 nasm_write(elf_sects
[i
].data
, len
, ofile
);
1368 fwritezero(align
, ofile
);
1372 static void elf_sect_write(struct elf_section
*sect
, const void *data
, size_t len
)
1374 saa_wbytes(sect
->data
, data
, len
);
1378 static void elf_sect_writeaddr(struct elf_section
*sect
, int64_t data
, size_t len
)
1380 saa_writeaddr(sect
->data
, data
, len
);
1384 static void elf_sectalign(int32_t seg
, unsigned int value
)
1386 struct elf_section
*s
= NULL
;
1389 for (i
= 0; i
< nsects
; i
++) {
1390 if (sects
[i
]->index
== seg
) {
1395 if (!s
|| !is_power2(value
))
1398 if (value
> s
->align
)
1402 static int32_t elf_segbase(int32_t segment
)
1407 static void elf_filename(char *inname
, char *outname
)
1409 strcpy(elf_module
, inname
);
1410 standard_extension(inname
, outname
, ".o");
1413 extern macros_t elf_stdmac
[];
1415 static int elf_set_info(enum geninfo type
, char **val
)
1421 static struct dfmt df_dwarf
= {
1422 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1426 null_debug_deflabel
,
1427 null_debug_directive
,
1432 static struct dfmt df_stabs
= {
1433 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1437 null_debug_deflabel
,
1438 null_debug_directive
,
1444 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1446 struct ofmt of_elf64
= {
1447 "ELF64 (x86_64) object files (e.g. Linux)",
1466 /* common debugging routines */
1467 static void debug64_typevalue(int32_t type
)
1469 int32_t stype
, ssize
;
1470 switch (TYM_TYPE(type
)) {
1513 stype
= STT_SECTION
;
1528 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1529 lastsym
->size
= ssize
;
1530 lastsym
->type
= stype
;
1534 /* stabs debugging routines */
1536 static void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1539 if (!stabs_filename
) {
1540 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1541 strcpy(stabs_filename
, filename
);
1543 if (strcmp(stabs_filename
, filename
)) {
1544 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1545 in fact, this leak comes in quite handy to maintain a list of files
1546 encountered so far in the symbol lines... */
1548 /* why not nasm_free(stabs_filename); we're done with the old one */
1550 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1551 strcpy(stabs_filename
, filename
);
1555 currentline
= linenumber
;
1559 static void stabs64_output(int type
, void *param
)
1561 struct symlininfo
*s
;
1562 struct linelist
*el
;
1563 if (type
== TY_DEBUGSYMLIN
) {
1564 if (debug_immcall
) {
1565 s
= (struct symlininfo
*)param
;
1566 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1567 return; /* line info is only collected for executable sections */
1569 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1570 el
->info
.offset
= s
->offset
;
1571 el
->info
.section
= s
->section
;
1572 el
->info
.name
= s
->name
;
1573 el
->line
= currentline
;
1574 el
->filename
= stabs_filename
;
1577 stabslines
->last
->next
= el
;
1578 stabslines
->last
= el
;
1581 stabslines
->last
= el
;
1588 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1590 static void stabs64_generate(void)
1592 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1593 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1597 struct linelist
*ptr
;
1601 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
1604 if (numfiles
== 0) {
1605 allfiles
[0] = ptr
->filename
;
1608 for (i
= 0; i
< numfiles
; i
++) {
1609 if (!strcmp(allfiles
[i
], ptr
->filename
))
1612 if (i
>= numfiles
) {
1613 allfiles
[i
] = ptr
->filename
;
1620 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1621 for (i
= 0; i
< numfiles
; i
++) {
1622 fileidx
[i
] = strsize
;
1623 strsize
+= strlen(allfiles
[i
]) + 1;
1626 for (i
= 0; i
< numfiles
; i
++) {
1627 if (!strcmp(allfiles
[i
], elf_module
)) {
1634 * worst case size of the stab buffer would be:
1635 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1636 * plus one "ending" entry
1638 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1639 sizeof(struct stabentry
));
1640 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1641 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1644 for (i
= 0; i
< numfiles
; i
++)
1645 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1648 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1656 * this is the first stab, its strx points to the filename of the
1657 * the source-file, the n_desc field should be set to the number
1658 * of remaining stabs
1660 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, stabstrlen
);
1662 /* this is the stab for the main source file */
1663 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1665 /* relocation table entry */
1668 * Since the symbol table has two entries before
1669 * the section symbols, the index in the info.section
1670 * member must be adjusted by adding 2
1673 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1674 WRITELONG(rptr
, R_X86_64_32
);
1675 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1678 currfile
= mainfileindex
;
1682 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1683 /* oops file has changed... */
1684 for (i
= 0; i
< numfiles
; i
++)
1685 if (!strcmp(allfiles
[i
], ptr
->filename
))
1688 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1692 /* relocation table entry */
1694 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1695 WRITELONG(rptr
, R_X86_64_32
);
1696 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1699 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1702 /* relocation table entry */
1704 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1705 WRITELONG(rptr
, R_X86_64_32
);
1706 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1712 /* this is an "ending" token */
1713 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1716 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1718 nasm_free(allfiles
);
1721 stablen
= (sptr
- sbuf
);
1722 stabrellen
= (rptr
- rbuf
);
1728 static void stabs64_cleanup(void)
1730 struct linelist
*ptr
, *del
;
1742 nasm_free(stabrelbuf
);
1743 nasm_free(stabstrbuf
);
1746 /* dwarf routines */
1748 static void dwarf64_init(void)
1750 ndebugs
= 3; /* 3 debug symbols */
1753 static void dwarf64_linenum(const char *filename
, int32_t linenumber
,
1757 dwarf64_findfile(filename
);
1759 currentline
= linenumber
;
1762 /* called from elf_out with type == TY_DEBUGSYMLIN */
1763 static void dwarf64_output(int type
, void *param
)
1765 int ln
, aa
, inx
, maxln
, soc
;
1766 struct symlininfo
*s
;
1771 s
= (struct symlininfo
*)param
;
1773 /* line number info is only gathered for executable sections */
1774 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1777 /* Check if section index has changed */
1778 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1779 dwarf64_findsect(s
->section
);
1781 /* do nothing unless line or file has changed */
1785 ln
= currentline
- dwarf_csect
->line
;
1786 aa
= s
->offset
- dwarf_csect
->offset
;
1787 inx
= dwarf_clist
->line
;
1788 plinep
= dwarf_csect
->psaa
;
1789 /* check for file change */
1790 if (!(inx
== dwarf_csect
->file
)) {
1791 saa_write8(plinep
,DW_LNS_set_file
);
1792 saa_write8(plinep
,inx
);
1793 dwarf_csect
->file
= inx
;
1795 /* check for line change */
1797 /* test if in range of special op code */
1798 maxln
= line_base
+ line_range
;
1799 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1800 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1801 saa_write8(plinep
,soc
);
1803 saa_write8(plinep
,DW_LNS_advance_line
);
1804 saa_wleb128s(plinep
,ln
);
1806 saa_write8(plinep
,DW_LNS_advance_pc
);
1807 saa_wleb128u(plinep
,aa
);
1810 dwarf_csect
->line
= currentline
;
1811 dwarf_csect
->offset
= s
->offset
;
1814 /* show change handled */
1819 static void dwarf64_generate(void)
1823 struct linelist
*ftentry
;
1824 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1825 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1826 struct sectlist
*psect
;
1827 size_t saalen
, linepoff
, totlen
, highaddr
;
1829 /* write epilogues for each line program range */
1830 /* and build aranges section */
1831 paranges
= saa_init(1L);
1832 parangesrel
= saa_init(1L);
1833 saa_write16(paranges
,3); /* dwarf version */
1834 saa_write64(parangesrel
, paranges
->datalen
+4);
1835 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1836 saa_write64(parangesrel
, 0);
1837 saa_write32(paranges
,0); /* offset into info */
1838 saa_write8(paranges
,8); /* pointer size */
1839 saa_write8(paranges
,0); /* not segmented */
1840 saa_write32(paranges
,0); /* padding */
1841 /* iterate though sectlist entries */
1842 psect
= dwarf_fsect
;
1845 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1847 plinep
= psect
->psaa
;
1848 /* Line Number Program Epilogue */
1849 saa_write8(plinep
,2); /* std op 2 */
1850 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1851 saa_write8(plinep
,DW_LNS_extended_op
);
1852 saa_write8(plinep
,1); /* operand length */
1853 saa_write8(plinep
,DW_LNE_end_sequence
);
1854 totlen
+= plinep
->datalen
;
1855 /* range table relocation entry */
1856 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1857 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1858 saa_write64(parangesrel
, (uint64_t) 0);
1859 /* range table entry */
1860 saa_write64(paranges
,0x0000); /* range start */
1861 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1862 highaddr
+= sects
[psect
->section
]->len
;
1863 /* done with this entry */
1864 psect
= psect
->next
;
1866 saa_write64(paranges
,0); /* null address */
1867 saa_write64(paranges
,0); /* null length */
1868 saalen
= paranges
->datalen
;
1869 arangeslen
= saalen
+ 4;
1870 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1871 WRITELONG(pbuf
,saalen
); /* initial length */
1872 saa_rnbytes(paranges
, pbuf
, saalen
);
1875 /* build rela.aranges section */
1876 arangesrellen
= saalen
= parangesrel
->datalen
;
1877 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1878 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1879 saa_free(parangesrel
);
1881 /* build pubnames section */
1882 ppubnames
= saa_init(1L);
1883 saa_write16(ppubnames
,3); /* dwarf version */
1884 saa_write32(ppubnames
,0); /* offset into info */
1885 saa_write32(ppubnames
,0); /* space used in info */
1886 saa_write32(ppubnames
,0); /* end of list */
1887 saalen
= ppubnames
->datalen
;
1888 pubnameslen
= saalen
+ 4;
1889 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1890 WRITELONG(pbuf
,saalen
); /* initial length */
1891 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1892 saa_free(ppubnames
);
1894 /* build info section */
1895 pinfo
= saa_init(1L);
1896 pinforel
= saa_init(1L);
1897 saa_write16(pinfo
,3); /* dwarf version */
1898 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1899 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
1900 saa_write64(pinforel
, 0);
1901 saa_write32(pinfo
,0); /* offset into abbrev */
1902 saa_write8(pinfo
,8); /* pointer size */
1903 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1904 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1905 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1906 saa_write64(pinforel
, 0);
1907 saa_write64(pinfo
,0); /* DW_AT_low_pc */
1908 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1909 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1910 saa_write64(pinforel
, 0);
1911 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
1912 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1913 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
1914 saa_write64(pinforel
, 0);
1915 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1916 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1917 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1918 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1919 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1920 saa_write64(pinforel
, pinfo
->datalen
+ 4);
1921 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
1922 saa_write64(pinforel
, 0);
1923 saa_write64(pinfo
,0); /* DW_AT_low_pc */
1924 saa_write64(pinfo
,0); /* DW_AT_frame_base */
1925 saa_write8(pinfo
,0); /* end of entries */
1926 saalen
= pinfo
->datalen
;
1927 infolen
= saalen
+ 4;
1928 infobuf
= pbuf
= nasm_malloc(infolen
);
1929 WRITELONG(pbuf
,saalen
); /* initial length */
1930 saa_rnbytes(pinfo
, pbuf
, saalen
);
1933 /* build rela.info section */
1934 inforellen
= saalen
= pinforel
->datalen
;
1935 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1936 saa_rnbytes(pinforel
, pbuf
, saalen
);
1939 /* build abbrev section */
1940 pabbrev
= saa_init(1L);
1941 saa_write8(pabbrev
,1); /* entry number LEB128u */
1942 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1943 saa_write8(pabbrev
,1); /* has children */
1944 /* the following attributes and forms are all LEB128u values */
1945 saa_write8(pabbrev
,DW_AT_low_pc
);
1946 saa_write8(pabbrev
,DW_FORM_addr
);
1947 saa_write8(pabbrev
,DW_AT_high_pc
);
1948 saa_write8(pabbrev
,DW_FORM_addr
);
1949 saa_write8(pabbrev
,DW_AT_stmt_list
);
1950 saa_write8(pabbrev
,DW_FORM_data4
);
1951 saa_write8(pabbrev
,DW_AT_name
);
1952 saa_write8(pabbrev
,DW_FORM_string
);
1953 saa_write8(pabbrev
,DW_AT_producer
);
1954 saa_write8(pabbrev
,DW_FORM_string
);
1955 saa_write8(pabbrev
,DW_AT_language
);
1956 saa_write8(pabbrev
,DW_FORM_data2
);
1957 saa_write16(pabbrev
,0); /* end of entry */
1958 /* LEB128u usage same as above */
1959 saa_write8(pabbrev
,2); /* entry number */
1960 saa_write8(pabbrev
,DW_TAG_subprogram
);
1961 saa_write8(pabbrev
,0); /* no children */
1962 saa_write8(pabbrev
,DW_AT_low_pc
);
1963 saa_write8(pabbrev
,DW_FORM_addr
);
1964 saa_write8(pabbrev
,DW_AT_frame_base
);
1965 saa_write8(pabbrev
,DW_FORM_data4
);
1966 saa_write16(pabbrev
,0); /* end of entry */
1967 abbrevlen
= saalen
= pabbrev
->datalen
;
1968 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
1969 saa_rnbytes(pabbrev
, pbuf
, saalen
);
1972 /* build line section */
1974 plines
= saa_init(1L);
1975 saa_write8(plines
,1); /* Minimum Instruction Length */
1976 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
1977 saa_write8(plines
,line_base
); /* Line Base */
1978 saa_write8(plines
,line_range
); /* Line Range */
1979 saa_write8(plines
,opcode_base
); /* Opcode Base */
1980 /* standard opcode lengths (# of LEB128u operands) */
1981 saa_write8(plines
,0); /* Std opcode 1 length */
1982 saa_write8(plines
,1); /* Std opcode 2 length */
1983 saa_write8(plines
,1); /* Std opcode 3 length */
1984 saa_write8(plines
,1); /* Std opcode 4 length */
1985 saa_write8(plines
,1); /* Std opcode 5 length */
1986 saa_write8(plines
,0); /* Std opcode 6 length */
1987 saa_write8(plines
,0); /* Std opcode 7 length */
1988 saa_write8(plines
,0); /* Std opcode 8 length */
1989 saa_write8(plines
,1); /* Std opcode 9 length */
1990 saa_write8(plines
,0); /* Std opcode 10 length */
1991 saa_write8(plines
,0); /* Std opcode 11 length */
1992 saa_write8(plines
,1); /* Std opcode 12 length */
1993 /* Directory Table */
1994 saa_write8(plines
,0); /* End of table */
1995 /* File Name Table */
1996 ftentry
= dwarf_flist
;
1997 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
1999 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2000 saa_write8(plines
,0); /* directory LEB128u */
2001 saa_write8(plines
,0); /* time LEB128u */
2002 saa_write8(plines
,0); /* size LEB128u */
2003 ftentry
= ftentry
->next
;
2005 saa_write8(plines
,0); /* End of table */
2006 linepoff
= plines
->datalen
;
2007 linelen
= linepoff
+ totlen
+ 10;
2008 linebuf
= pbuf
= nasm_malloc(linelen
);
2009 WRITELONG(pbuf
,linelen
-4); /* initial length */
2010 WRITESHORT(pbuf
,3); /* dwarf version */
2011 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2012 /* write line header */
2014 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2017 /* concatonate line program ranges */
2019 plinesrel
= saa_init(1L);
2020 psect
= dwarf_fsect
;
2021 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2022 saa_write64(plinesrel
, linepoff
);
2023 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2024 saa_write64(plinesrel
, (uint64_t) 0);
2025 plinep
= psect
->psaa
;
2026 saalen
= plinep
->datalen
;
2027 saa_rnbytes(plinep
, pbuf
, saalen
);
2031 /* done with this entry */
2032 psect
= psect
->next
;
2036 /* build rela.lines section */
2037 linerellen
=saalen
= plinesrel
->datalen
;
2038 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2039 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2040 saa_free(plinesrel
);
2042 /* build frame section */
2044 framebuf
= pbuf
= nasm_malloc(framelen
);
2045 WRITELONG(pbuf
,framelen
-4); /* initial length */
2047 /* build loc section */
2049 locbuf
= pbuf
= nasm_malloc(loclen
);
2050 WRITEDLONG(pbuf
,0); /* null beginning offset */
2051 WRITEDLONG(pbuf
,0); /* null ending offset */
2054 static void dwarf64_cleanup(void)
2056 nasm_free(arangesbuf
);
2057 nasm_free(arangesrelbuf
);
2058 nasm_free(pubnamesbuf
);
2060 nasm_free(inforelbuf
);
2061 nasm_free(abbrevbuf
);
2063 nasm_free(linerelbuf
);
2064 nasm_free(framebuf
);
2068 static void dwarf64_findfile(const char * fname
)
2071 struct linelist
*match
;
2073 /* return if fname is current file name */
2074 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2077 /* search for match */
2080 match
= dwarf_flist
;
2081 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2082 if (!(strcmp(fname
, match
->filename
))) {
2083 dwarf_clist
= match
;
2089 /* add file name to end of list */
2090 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2092 dwarf_clist
->line
= dwarf_numfiles
;
2093 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2094 strcpy(dwarf_clist
->filename
,fname
);
2095 dwarf_clist
->next
= 0;
2096 if (!dwarf_flist
) { /* if first entry */
2097 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2098 dwarf_clist
->last
= 0;
2099 } else { /* chain to previous entry */
2100 dwarf_elist
->next
= dwarf_clist
;
2101 dwarf_elist
= dwarf_clist
;
2105 static void dwarf64_findsect(const int index
)
2108 struct sectlist
*match
;
2111 /* return if index is current section index */
2112 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2115 /* search for match */
2118 match
= dwarf_fsect
;
2119 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2120 if (match
->section
== index
) {
2121 dwarf_csect
= match
;
2124 match
= match
->next
;
2128 /* add entry to end of list */
2129 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2131 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2132 dwarf_csect
->line
= 1;
2133 dwarf_csect
->offset
= 0;
2134 dwarf_csect
->file
= 1;
2135 dwarf_csect
->section
= index
;
2136 dwarf_csect
->next
= 0;
2137 /* set relocatable address at start of line program */
2138 saa_write8(plinep
,DW_LNS_extended_op
);
2139 saa_write8(plinep
,9); /* operand length */
2140 saa_write8(plinep
,DW_LNE_set_address
);
2141 saa_write64(plinep
,0); /* Start Address */
2143 if (!dwarf_fsect
) { /* if first entry */
2144 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2145 dwarf_csect
->last
= 0;
2146 } else { /* chain to previous entry */
2147 dwarf_esect
->next
= dwarf_csect
;
2148 dwarf_esect
= dwarf_csect
;