1 /* ----------------------------------------------------------------------- *
3 * Copyright 2012 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 * outelfx32.c output routines for the Netwide Assembler to produce
36 * ELF32 (x86_64) object file format
53 #include "output/outform.h"
54 #include "output/outlib.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
68 int32_t address
; /* relative to _start_ of section */
69 int32_t symbol
; /* symbol index */
70 int32_t offset
; /* symbol addend */
71 int type
; /* type of relocation */
75 struct rbtree symv
; /* symbol value and rbtree of globals */
76 int32_t strpos
; /* string table position of name */
77 int32_t section
; /* section ID of the symbol */
78 int type
; /* symbol type */
79 int other
; /* symbol visibility */
80 int32_t size
; /* size of symbol */
81 int32_t globnum
; /* symbol table offset if global */
82 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
83 char *name
; /* used temporarily if in above list */
88 uint32_t len
, size
, nrelocs
;
89 int32_t index
; /* index into sects array */
90 int type
; /* SHT_PROGBITS or SHT_NOBITS */
91 uint32_t align
; /* alignment: power of two */
92 uint32_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
; /* Symbol counts */
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_elfx32
;
127 static struct ELF_SECTDATA
{
132 static int elf_nsect
, nsections
;
133 static int32_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
*, int32_t, size_t);
138 static void elf_section_header(int, int, uint32_t, void *, bool, uint32_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(uint32_t *, struct Reloc
*);
143 static void add_sectname(char *, char *);
151 int section
; /* index into sects[] */
152 int segto
; /* internal section number */
153 char *name
; /* shallow-copied pointer of section name */
157 struct linelist
*next
;
158 struct linelist
*last
;
159 struct symlininfo info
;
170 struct sectlist
*next
;
171 struct sectlist
*last
;
174 /* common debug variables */
175 static int currentline
= 1;
176 static int debug_immcall
= 0;
178 /* stabs debug variables */
179 static struct linelist
*stabslines
= 0;
180 static int numlinestabs
= 0;
181 static char *stabs_filename
= 0;
182 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
183 static int stablen
, stabstrlen
, stabrellen
;
185 /* dwarf debug variables */
186 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
187 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
188 static int dwarf_numfiles
= 0, dwarf_nsections
;
189 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
190 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
191 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
192 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
193 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
194 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
197 static struct dfmt df_dwarf
;
198 static struct dfmt df_stabs
;
199 static struct Symbol
*lastsym
;
201 /* common debugging routines */
202 static void debugx32_typevalue(int32_t);
203 static void debugx32_deflabel(char *, int32_t, int64_t, int, char *);
204 static void debugx32_directive(const char *, const char *);
206 /* stabs debugging routines */
207 static void stabsx32_linenum(const char *filename
, int32_t linenumber
, int32_t);
208 static void stabsx32_output(int, void *);
209 static void stabsx32_generate(void);
210 static void stabsx32_cleanup(void);
212 /* dwarf debugging routines */
213 static void dwarfx32_init(void);
214 static void dwarfx32_linenum(const char *filename
, int32_t linenumber
, int32_t);
215 static void dwarfx32_output(int, void *);
216 static void dwarfx32_generate(void);
217 static void dwarfx32_cleanup(void);
218 static void dwarfx32_findfile(const char *);
219 static void dwarfx32_findsect(const int);
222 * Special section numbers which are used to define ELF special
223 * symbols, which can be used with WRT to provide PIC relocation
226 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
227 static int32_t elf_got_sect
, elf_plt_sect
;
228 static int32_t elf_sym_sect
;
229 static int32_t elf_gottpoff_sect
;
231 static void elf_init(void)
235 nsects
= sectlen
= 0;
236 syms
= saa_init((int32_t)sizeof(struct Symbol
));
237 nlocals
= nglobs
= ndebugs
= 0;
240 saa_wbytes(strs
, "\0", 1L);
241 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
242 strslen
= 2 + strlen(elf_module
);
244 shstrtablen
= shstrtabsize
= 0;;
245 add_sectname("", "");
249 elf_gotpc_sect
= seg_alloc();
250 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
251 elf_gotoff_sect
= seg_alloc();
252 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
253 elf_got_sect
= seg_alloc();
254 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
255 elf_plt_sect
= seg_alloc();
256 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
257 elf_sym_sect
= seg_alloc();
258 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
259 elf_gottpoff_sect
= seg_alloc();
260 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
262 def_seg
= seg_alloc();
266 static void elf_cleanup(int debuginfo
)
274 for (i
= 0; i
< nsects
; i
++) {
275 if (sects
[i
]->type
!= SHT_NOBITS
)
276 saa_free(sects
[i
]->data
);
278 saa_free(sects
[i
]->rel
);
279 while (sects
[i
]->head
) {
281 sects
[i
]->head
= sects
[i
]->head
->next
;
289 if (of_elfx32
.current_dfmt
) {
290 of_elfx32
.current_dfmt
->cleanup();
294 /* add entry to the elf .shstrtab section */
295 static void add_sectname(char *firsthalf
, char *secondhalf
)
297 int len
= strlen(firsthalf
) + strlen(secondhalf
);
298 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
299 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
300 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
301 strcat(shstrtab
+ shstrtablen
, secondhalf
);
302 shstrtablen
+= len
+ 1;
305 static int elf_make_section(char *name
, int type
, int flags
, int align
)
309 s
= nasm_zalloc(sizeof(*s
));
311 if (type
!= SHT_NOBITS
)
312 s
->data
= saa_init(1L);
314 if (!strcmp(name
, ".text"))
317 s
->index
= seg_alloc();
318 add_sectname("", name
);
320 s
->name
= nasm_strdup(name
);
325 if (nsects
>= sectlen
)
326 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
332 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
335 uint32_t flags
, flags_and
, flags_or
;
340 * Default is 64 bits.
347 p
= nasm_skip_word(name
);
350 flags_and
= flags_or
= type
= align
= 0;
352 section_attrib(name
, p
, pass
, &flags_and
,
353 &flags_or
, &align
, &type
);
355 if (!strcmp(name
, ".shstrtab") ||
356 !strcmp(name
, ".symtab") ||
357 !strcmp(name
, ".strtab")) {
358 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
363 for (i
= 0; i
< nsects
; i
++)
364 if (!strcmp(name
, sects
[i
]->name
))
367 const struct elf_known_section
*ks
= elf_known_sections
;
370 if (!strcmp(name
, ks
->name
))
375 type
= type
? type
: ks
->type
;
376 align
= align
? align
: ks
->align
;
377 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
379 i
= elf_make_section(name
, type
, flags
, align
);
380 } else if (pass
== 1) {
381 if ((type
&& sects
[i
]->type
!= type
)
382 || (align
&& sects
[i
]->align
!= align
)
383 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
384 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
385 " redeclaration of section `%s'", name
);
388 return sects
[i
]->index
;
391 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
392 int is_global
, char *special
)
396 bool special_used
= false;
398 #if defined(DEBUG) && DEBUG>2
399 nasm_error(ERR_DEBUG
,
400 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
401 name
, segment
, offset
, is_global
, special
);
403 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
405 * This is a NASM special symbol. We never allow it into
406 * the ELF symbol table, even if it's a valid one. If it
407 * _isn't_ a valid one, we should barf immediately.
409 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
410 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
411 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
412 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
416 if (is_global
== 3) {
419 * Fix up a forward-reference symbol size from the first
422 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
423 if (!strcmp((*s
)->name
, name
)) {
424 struct tokenval tokval
;
426 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
430 tokval
.t_type
= TOKEN_INVALID
;
431 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
434 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
435 " expression as symbol size");
437 (*s
)->size
= reloc_value(e
);
441 * Remove it from the list of unresolved sizes.
443 nasm_free((*s
)->name
);
447 return; /* it wasn't an important one */
450 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
451 strslen
+= 1 + strlen(name
);
453 lastsym
= sym
= saa_wstruct(syms
);
455 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
458 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
459 sym
->other
= STV_DEFAULT
;
461 if (segment
== NO_SEG
)
462 sym
->section
= SHN_ABS
;
465 sym
->section
= SHN_UNDEF
;
466 if (segment
== def_seg
) {
467 /* we have to be sure at least text section is there */
469 if (segment
!= elf_section_names(".text", 2, &tempint
))
470 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
472 for (i
= 0; i
< nsects
; i
++) {
473 if (segment
== sects
[i
]->index
) {
474 sym
->section
= i
+ 1;
480 if (is_global
== 2) {
483 sym
->section
= SHN_COMMON
;
485 * We have a common variable. Check the special text to see
486 * if it's a valid number and power of two; if so, store it
487 * as the alignment for the common variable.
491 sym
->symv
.key
= readnum(special
, &err
);
493 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
494 " valid number", special
);
495 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
496 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
497 " power of two", special
);
501 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
503 if (sym
->type
== SYM_GLOBAL
) {
505 * If sym->section == SHN_ABS, then the first line of the
506 * else section would cause a core dump, because its a reference
507 * beyond the end of the section array.
508 * This behaviour is exhibited by this code:
511 * To avoid such a crash, such requests are silently discarded.
512 * This may not be the best solution.
514 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
515 bsym
= raa_write(bsym
, segment
, nglobs
);
516 } else if (sym
->section
!= SHN_ABS
) {
518 * This is a global symbol; so we must add it to the rbtree
519 * of global symbols in its section.
521 * In addition, we check the special text for symbol
522 * type and size information.
524 sects
[sym
->section
-1]->gsyms
=
525 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
528 int n
= strcspn(special
, " \t");
530 if (!nasm_strnicmp(special
, "function", n
))
531 sym
->type
|= STT_FUNC
;
532 else if (!nasm_strnicmp(special
, "data", n
) ||
533 !nasm_strnicmp(special
, "object", n
))
534 sym
->type
|= STT_OBJECT
;
535 else if (!nasm_strnicmp(special
, "notype", n
))
536 sym
->type
|= STT_NOTYPE
;
538 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
542 special
= nasm_skip_spaces(special
);
544 n
= strcspn(special
, " \t");
545 if (!nasm_strnicmp(special
, "default", n
))
546 sym
->other
= STV_DEFAULT
;
547 else if (!nasm_strnicmp(special
, "internal", n
))
548 sym
->other
= STV_INTERNAL
;
549 else if (!nasm_strnicmp(special
, "hidden", n
))
550 sym
->other
= STV_HIDDEN
;
551 else if (!nasm_strnicmp(special
, "protected", n
))
552 sym
->other
= STV_PROTECTED
;
559 struct tokenval tokval
;
562 char *saveme
= stdscan_get();
564 while (special
[n
] && nasm_isspace(special
[n
]))
567 * We have a size expression; attempt to
571 stdscan_set(special
+ n
);
572 tokval
.t_type
= TOKEN_INVALID
;
573 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
578 sym
->name
= nasm_strdup(name
);
581 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
582 " expression as symbol size");
584 sym
->size
= reloc_value(e
);
591 * If TLS segment, mark symbol accordingly.
593 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
595 sym
->type
|= STT_TLS
;
598 sym
->globnum
= nglobs
;
603 if (special
&& !special_used
)
604 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
607 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
608 int32_t offset
, int type
)
612 r
= *sect
->tail
= nasm_zalloc(sizeof(struct Reloc
));
613 sect
->tail
= &r
->next
;
615 r
->address
= sect
->len
;
618 if (segment
!= NO_SEG
) {
620 for (i
= 0; i
< nsects
; i
++)
621 if (segment
== sects
[i
]->index
)
624 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
632 * This routine deals with ..got and ..sym relocations: the more
633 * complicated kinds. In shared-library writing, some relocations
634 * with respect to global symbols must refer to the precise symbol
635 * rather than referring to an offset from the base of the section
636 * _containing_ the symbol. Such relocations call to this routine,
637 * which searches the symbol list for the symbol in question.
639 * R_X86_64_GOT32 references require the _exact_ symbol address to be
640 * used; R_X86_64_32 references can be at an offset from the symbol.
641 * The boolean argument `exact' tells us this.
643 * Return value is the adjusted value of `addr', having become an
644 * offset from the symbol rather than the section. Should always be
645 * zero when returning from an exact call.
647 * Limitation: if you define two symbols at the same place,
648 * confusion will occur.
650 * Inefficiency: we search, currently, using a linked list which
651 * isn't even necessarily sorted.
653 static void elf_add_gsym_reloc(struct Section
*sect
,
654 int32_t segment
, uint32_t offset
, int32_t pcrel
,
655 int type
, bool exact
)
664 * First look up the segment/offset pair and find a global
665 * symbol corresponding to it. If it's not one of our segments,
666 * then it must be an external symbol, in which case we're fine
667 * doing a normal elf_add_reloc after first sanity-checking
668 * that the offset from the symbol is zero.
671 for (i
= 0; i
< nsects
; i
++)
672 if (segment
== sects
[i
]->index
) {
679 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
681 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
685 srb
= rb_search(s
->gsyms
, offset
);
686 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
687 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
688 " for this reference");
691 sym
= container_of(srb
, struct Symbol
, symv
);
693 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
694 sect
->tail
= &r
->next
;
697 r
->address
= sect
->len
;
698 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
699 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
705 static void elf_out(int32_t segto
, const void *data
,
706 enum out_type type
, uint64_t size
,
707 int32_t segment
, int32_t wrt
)
713 static struct symlininfo sinfo
;
715 #if defined(DEBUG) && DEBUG>2
717 nasm_error(ERR_DEBUG
,
718 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
719 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
721 nasm_error(ERR_DEBUG
,
722 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
723 currentline
, type
, segment
, segto
, size
);
727 * handle absolute-assembly (structure definitions)
729 if (segto
== NO_SEG
) {
730 if (type
!= OUT_RESERVE
)
731 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
737 for (i
= 0; i
< nsects
; i
++)
738 if (segto
== sects
[i
]->index
) {
743 int tempint
; /* ignored */
744 if (segto
!= elf_section_names(".text", 2, &tempint
))
745 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
747 s
= sects
[nsects
- 1];
752 /* again some stabs debugging stuff */
753 if (of_elfx32
.current_dfmt
) {
754 sinfo
.offset
= s
->len
;
757 sinfo
.name
= s
->name
;
758 of_elfx32
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
760 /* end of debugging stuff */
762 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
763 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
764 " BSS section `%s': ignored", s
->name
);
765 s
->len
+= realsize(type
, size
);
771 if (s
->type
== SHT_PROGBITS
) {
772 nasm_error(ERR_WARNING
, "uninitialized space declared in"
773 " non-BSS section `%s': zeroing", s
->name
);
774 elf_sect_write(s
, NULL
, size
);
780 if (segment
!= NO_SEG
)
781 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
782 elf_sect_write(s
, data
, size
);
786 addr
= *(int64_t *)data
;
787 if (segment
== NO_SEG
) {
789 } else if (segment
% 2) {
790 nasm_error(ERR_NONFATAL
, "ELF format does not support"
791 " segment base references");
796 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
799 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
802 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
805 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
808 nasm_error(ERR_PANIC
, "internal error elfx32-hpa-871");
812 } else if (wrt
== elf_gotpc_sect
+ 1) {
814 * The user will supply GOT relative to $$. ELF
815 * will let us have GOT relative to $. So we
816 * need to fix up the data item by $-$$.
819 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
821 } else if (wrt
== elf_gotoff_sect
+ 1) {
822 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
823 "R_X86_64_GOTOFF64");
824 } else if (wrt
== elf_got_sect
+ 1) {
827 elf_add_gsym_reloc(s
, segment
, addr
, 0,
828 R_X86_64_GOT32
, true);
832 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
835 } else if (wrt
== elf_sym_sect
+ 1) {
838 elf_add_gsym_reloc(s
, segment
, addr
, 0,
843 elf_add_gsym_reloc(s
, segment
, addr
, 0,
848 elf_add_gsym_reloc(s
, segment
, addr
, 0,
853 elf_add_gsym_reloc(s
, segment
, addr
, 0,
858 nasm_error(ERR_PANIC
, "internal error elfx32-hpa-903");
861 } else if (wrt
== elf_plt_sect
+ 1) {
862 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
863 "relative PLT references");
865 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
869 elf_sect_writeaddr(s
, addr
, size
);
873 reltype
= R_X86_64_PC8
;
878 reltype
= R_X86_64_PC16
;
883 addr
= *(int64_t *)data
- size
;
884 if (segment
== segto
)
885 nasm_error(ERR_PANIC
, "intra-segment OUT_REL1ADR");
886 if (segment
== NO_SEG
) {
888 } else if (segment
% 2) {
889 nasm_error(ERR_NONFATAL
, "ELF format does not support"
890 " segment base references");
893 elf_add_reloc(s
, segment
, addr
, reltype
);
896 nasm_error(ERR_NONFATAL
,
897 "Unsupported non-32-bit ELF relocation");
900 elf_sect_writeaddr(s
, addr
, bytes
);
904 addr
= *(int64_t *)data
- size
;
905 if (segment
== segto
)
906 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
907 if (segment
== NO_SEG
) {
909 } else if (segment
% 2) {
910 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
911 " segment base references");
914 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
916 } else if (wrt
== elf_plt_sect
+ 1) {
917 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
918 R_X86_64_PLT32
, true);
920 } else if (wrt
== elf_gotpc_sect
+ 1 ||
921 wrt
== elf_got_sect
+ 1) {
922 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
923 R_X86_64_GOTPCREL
, true);
925 } else if (wrt
== elf_gotoff_sect
+ 1 ||
926 wrt
== elf_got_sect
+ 1) {
927 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
928 } else if (wrt
== elf_gottpoff_sect
+ 1) {
929 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
930 R_X86_64_GOTTPOFF
, true);
933 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
937 elf_sect_writeaddr(s
, addr
, 4);
941 nasm_error(ERR_NONFATAL
,
942 "32-bit ELF format does not support 64-bit relocations");
944 elf_sect_writeaddr(s
, addr
, 8);
949 static void elf_write(void)
956 int32_t symtablen
, symtablocal
;
959 * Work out how many sections we will have. We have SHN_UNDEF,
960 * then the flexible user sections, then the fixed sections
961 * `.shstrtab', `.symtab' and `.strtab', then optionally
962 * relocation sections for the user sections.
964 nsections
= sec_numspecial
+ 1;
965 if (of_elfx32
.current_dfmt
== &df_stabs
)
967 else if (of_elfx32
.current_dfmt
== &df_dwarf
)
970 add_sectname("", ".shstrtab");
971 add_sectname("", ".symtab");
972 add_sectname("", ".strtab");
973 for (i
= 0; i
< nsects
; i
++) {
974 nsections
++; /* for the section itself */
975 if (sects
[i
]->head
) {
976 nsections
++; /* for its relocations */
977 add_sectname(".rela", sects
[i
]->name
);
981 if (of_elfx32
.current_dfmt
== &df_stabs
) {
982 /* in case the debug information is wanted, just add these three sections... */
983 add_sectname("", ".stab");
984 add_sectname("", ".stabstr");
985 add_sectname(".rel", ".stab");
988 else if (of_elfx32
.current_dfmt
== &df_dwarf
) {
989 /* the dwarf debug standard specifies the following ten sections,
990 not all of which are currently implemented,
991 although all of them are defined. */
992 add_sectname("", ".debug_aranges");
993 add_sectname(".rela", ".debug_aranges");
994 add_sectname("", ".debug_pubnames");
995 add_sectname("", ".debug_info");
996 add_sectname(".rela", ".debug_info");
997 add_sectname("", ".debug_abbrev");
998 add_sectname("", ".debug_line");
999 add_sectname(".rela", ".debug_line");
1000 add_sectname("", ".debug_frame");
1001 add_sectname("", ".debug_loc");
1005 * Output the ELF header.
1007 fwrite("\177ELF\1\1\1", 7, 1, ofile
);
1008 fputc(elf_osabi
, ofile
);
1009 fputc(elf_abiver
, ofile
);
1010 fwritezero(7, ofile
);
1011 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1012 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1013 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1014 fwriteint32_t(0L, ofile
); /* no entry point */
1015 fwriteint32_t(0L, ofile
); /* no program header table */
1016 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
1017 * ELF header plus alignment */
1018 fwriteint32_t(0L, ofile
); /* X86_64 defines no special flags */
1019 fwriteint16_t(0x34, ofile
); /* size of ELF header */
1020 fwriteint16_t(0, ofile
); /* no program header table, again */
1021 fwriteint16_t(0, ofile
); /* still no program header table */
1022 fwriteint16_t(sizeof(Elf32_Shdr
), ofile
); /* size of section header */
1023 fwriteint16_t(nsections
, ofile
); /* number of sections */
1024 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1025 * section header table */
1026 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
1027 fwriteint32_t(0L, ofile
);
1028 fwriteint32_t(0L, ofile
);
1031 * Build the symbol table and relocation tables.
1033 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1034 for (i
= 0; i
< nsects
; i
++)
1036 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1040 * Now output the section header table.
1043 elf_foffs
= 0x40 + sizeof(Elf32_Shdr
) * nsections
;
1044 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1047 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1050 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1053 /* The normal sections */
1054 for (i
= 0; i
< nsects
; i
++) {
1055 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1056 (sects
[i
]->type
== SHT_PROGBITS
?
1057 sects
[i
]->data
: NULL
), true,
1058 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1063 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1064 shstrtablen
, 0, 0, 1, 0);
1068 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1069 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1073 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1074 strslen
, 0, 0, 1, 0);
1077 /* The relocation sections */
1078 for (i
= 0; i
< nsects
; i
++)
1079 if (sects
[i
]->head
) {
1080 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1081 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 12);
1085 if (of_elfx32
.current_dfmt
== &df_stabs
) {
1086 /* for debugging information, create the last three sections
1087 which are the .stab , .stabstr and .rel.stab sections respectively */
1089 /* this function call creates the stab sections in memory */
1090 stabsx32_generate();
1092 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1093 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1094 stablen
, sec_stabstr
, 0, 4, 12);
1097 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1098 stabstrlen
, 0, 0, 4, 0);
1101 /* link -> symtable info -> section to refer to */
1102 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1103 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1106 } else if (of_elfx32
.current_dfmt
== &df_dwarf
) {
1107 /* for dwarf debugging information, create the ten dwarf sections */
1109 /* this function call creates the dwarf sections in memory */
1111 dwarfx32_generate();
1113 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1114 arangeslen
, 0, 0, 1, 0);
1117 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1118 arangesrellen
, sec_symtab
, sec_debug_aranges
, 1, 12);
1121 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1122 pubnameslen
, 0, 0, 1, 0);
1125 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1126 infolen
, 0, 0, 1, 0);
1129 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1130 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1133 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1134 abbrevlen
, 0, 0, 1, 0);
1137 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1138 linelen
, 0, 0, 1, 0);
1141 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1142 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1145 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1146 framelen
, 0, 0, 8, 0);
1149 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1150 loclen
, 0, 0, 1, 0);
1153 fwritezero(align
, ofile
);
1156 * Now output the sections.
1158 elf_write_sections();
1160 nasm_free(elf_sects
);
1164 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1166 struct SAA
*s
= saa_init(1L);
1168 uint8_t entry
[24], *p
;
1174 * First, an all-zeros entry, required by the ELF spec.
1176 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1181 * Next, an entry for the file name.
1184 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1185 WRITELONG(p
, 0); /* no value */
1186 WRITELONG(p
, 0); /* no size either */
1187 WRITESHORT(p
, STT_FILE
); /* type FILE */
1188 WRITESHORT(p
, SHN_ABS
);
1189 saa_wbytes(s
, entry
, 16L);
1194 * Now some standard symbols defining the segments, for relocation
1197 for (i
= 1; i
<= nsects
; i
++) {
1199 WRITELONG(p
, 0); /* no symbol name */
1200 WRITELONG(p
, 0); /* offset zero */
1201 WRITELONG(p
, 0); /* size zero */
1202 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1203 WRITESHORT(p
, i
); /* section id */
1204 saa_wbytes(s
, entry
, 16L);
1211 * Now the other local symbols.
1214 while ((sym
= saa_rstruct(syms
))) {
1215 if (sym
->type
& SYM_GLOBAL
)
1218 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1219 WRITELONG(p
, sym
->symv
.key
); /* value of symbol */
1220 WRITELONG(p
, sym
->size
); /* size of symbol */
1221 WRITECHAR(p
, sym
->type
); /* type and binding */
1222 WRITECHAR(p
, sym
->other
); /* visibility */
1223 WRITESHORT(p
, sym
->section
); /* index into section header table */
1224 saa_wbytes(s
, entry
, 16L);
1229 * dwarf needs symbols for debug sections
1230 * which are relocation targets.
1232 if (of_elfx32
.current_dfmt
== &df_dwarf
) {
1233 dwarf_infosym
= *local
;
1235 WRITELONG(p
, 0); /* no symbol name */
1236 WRITELONG(p
, 0); /* offset zero */
1237 WRITELONG(p
, 0); /* size zero */
1238 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1239 WRITESHORT(p
, sec_debug_info
); /* section id */
1240 saa_wbytes(s
, entry
, 16L);
1243 dwarf_abbrevsym
= *local
;
1245 WRITELONG(p
, 0); /* no symbol name */
1246 WRITELONG(p
, 0); /* offset zero */
1247 WRITELONG(p
, 0); /* size zero */
1248 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1249 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1250 saa_wbytes(s
, entry
, 16L);
1253 dwarf_linesym
= *local
;
1255 WRITELONG(p
, 0); /* no symbol name */
1256 WRITELONG(p
, 0); /* offset zero */
1257 WRITELONG(p
, 0); /* size zero */
1258 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1259 WRITESHORT(p
, sec_debug_line
); /* section id */
1260 saa_wbytes(s
, entry
, 16L);
1266 * Now the global symbols.
1269 while ((sym
= saa_rstruct(syms
))) {
1270 if (!(sym
->type
& SYM_GLOBAL
))
1273 WRITELONG(p
, sym
->strpos
);
1274 WRITELONG(p
, sym
->symv
.key
);
1275 WRITELONG(p
, sym
->size
);
1276 WRITECHAR(p
, sym
->type
); /* type and binding */
1277 WRITECHAR(p
, sym
->other
); /* visibility */
1278 WRITESHORT(p
, sym
->section
);
1279 saa_wbytes(s
, entry
, 16L);
1286 static struct SAA
*elf_build_reltab(uint32_t *len
, struct Reloc
*r
)
1289 uint8_t *p
, entry
[12];
1290 int32_t global_offset
;
1299 * How to onvert from a global placeholder to a real symbol index;
1300 * the +2 refers to the two special entries, the null entry and
1301 * the filename entry.
1303 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1306 int32_t sym
= r
->symbol
;
1308 if (sym
>= GLOBAL_TEMP_BASE
)
1309 sym
+= global_offset
;
1312 WRITELONG(p
, r
->address
);
1313 WRITELONG(p
, (sym
<< 8) + r
->type
);
1314 WRITELONG(p
, r
->offset
);
1315 saa_wbytes(s
, entry
, 12L);
1324 static void elf_section_header(int name
, int type
, uint32_t flags
,
1325 void *data
, bool is_saa
, uint32_t datalen
,
1326 int link
, int info
, int align
, int eltsize
)
1328 elf_sects
[elf_nsect
].data
= data
;
1329 elf_sects
[elf_nsect
].len
= datalen
;
1330 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1333 fwriteint32_t((int32_t)name
, ofile
);
1334 fwriteint32_t((int32_t)type
, ofile
);
1335 fwriteint32_t((int32_t)flags
, ofile
);
1336 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1337 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1338 fwriteint32_t(datalen
, ofile
);
1340 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1341 fwriteint32_t((int32_t)link
, ofile
);
1342 fwriteint32_t((int32_t)info
, ofile
);
1343 fwriteint32_t((int32_t)align
, ofile
);
1344 fwriteint32_t((int32_t)eltsize
, ofile
);
1347 static void elf_write_sections(void)
1350 for (i
= 0; i
< elf_nsect
; i
++)
1351 if (elf_sects
[i
].data
) {
1352 int32_t len
= elf_sects
[i
].len
;
1353 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1354 int32_t align
= reallen
- len
;
1355 if (elf_sects
[i
].is_saa
)
1356 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1358 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1359 fwritezero(align
, ofile
);
1363 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1365 saa_wbytes(sect
->data
, data
, len
);
1368 static void elf_sect_writeaddr(struct Section
*sect
, int32_t data
, size_t len
)
1370 saa_writeaddr(sect
->data
, data
, len
);
1374 static void elf_sectalign(int32_t seg
, unsigned int value
)
1376 struct Section
*s
= NULL
;
1379 for (i
= 0; i
< nsects
; i
++) {
1380 if (sects
[i
]->index
== seg
) {
1385 if (!s
|| !is_power2(value
))
1388 if (value
> s
->align
)
1392 static int32_t elf_segbase(int32_t segment
)
1397 static int elf_directive(enum directives directive
, char *value
, int pass
)
1403 switch (directive
) {
1406 return 1; /* ignore in pass 2 */
1408 n
= readnum(value
, &err
);
1410 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1413 if (n
< 0 || n
> 255) {
1414 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1420 if ((p
= strchr(value
,',')) == NULL
)
1423 n
= readnum(p
+1, &err
);
1424 if (err
|| n
< 0 || n
> 255) {
1425 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1437 static void elf_filename(char *inname
, char *outname
)
1439 strcpy(elf_module
, inname
);
1440 standard_extension(inname
, outname
, ".o");
1443 extern macros_t elf_stdmac
[];
1445 static int elf_set_info(enum geninfo type
, char **val
)
1451 static struct dfmt df_dwarf
= {
1452 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1462 static struct dfmt df_stabs
= {
1463 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1474 struct dfmt
*elfx32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1476 struct ofmt of_elfx32
= {
1477 "ELFX32 (x86_64) object files (e.g. Linux)",
1495 /* common debugging routines */
1496 static void debugx32_deflabel(char *name
, int32_t segment
, int64_t offset
,
1497 int is_global
, char *special
)
1506 static void debugx32_directive(const char *directive
, const char *params
)
1512 static void debugx32_typevalue(int32_t type
)
1514 int32_t stype
, ssize
;
1515 switch (TYM_TYPE(type
)) {
1558 stype
= STT_SECTION
;
1573 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1574 lastsym
->size
= ssize
;
1575 lastsym
->type
= stype
;
1579 /* stabs debugging routines */
1581 static void stabsx32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1584 if (!stabs_filename
) {
1585 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1586 strcpy(stabs_filename
, filename
);
1588 if (strcmp(stabs_filename
, filename
)) {
1589 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1590 in fact, this leak comes in quite handy to maintain a list of files
1591 encountered so far in the symbol lines... */
1593 /* why not nasm_free(stabs_filename); we're done with the old one */
1595 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1596 strcpy(stabs_filename
, filename
);
1600 currentline
= linenumber
;
1604 static void stabsx32_output(int type
, void *param
)
1606 struct symlininfo
*s
;
1607 struct linelist
*el
;
1608 if (type
== TY_DEBUGSYMLIN
) {
1609 if (debug_immcall
) {
1610 s
= (struct symlininfo
*)param
;
1611 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1612 return; /* line info is only collected for executable sections */
1614 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1615 el
->info
.offset
= s
->offset
;
1616 el
->info
.section
= s
->section
;
1617 el
->info
.name
= s
->name
;
1618 el
->line
= currentline
;
1619 el
->filename
= stabs_filename
;
1622 stabslines
->last
->next
= el
;
1623 stabslines
->last
= el
;
1626 stabslines
->last
= el
;
1633 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1635 static void stabsx32_generate(void)
1637 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1638 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1642 struct linelist
*ptr
;
1646 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
1649 if (numfiles
== 0) {
1650 allfiles
[0] = ptr
->filename
;
1653 for (i
= 0; i
< numfiles
; i
++) {
1654 if (!strcmp(allfiles
[i
], ptr
->filename
))
1657 if (i
>= numfiles
) {
1658 allfiles
[i
] = ptr
->filename
;
1665 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1666 for (i
= 0; i
< numfiles
; i
++) {
1667 fileidx
[i
] = strsize
;
1668 strsize
+= strlen(allfiles
[i
]) + 1;
1671 for (i
= 0; i
< numfiles
; i
++) {
1672 if (!strcmp(allfiles
[i
], elf_module
)) {
1679 * worst case size of the stab buffer would be:
1680 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1681 * plus one "ending" entry
1683 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1684 sizeof(struct stabentry
));
1685 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1686 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1689 for (i
= 0; i
< numfiles
; i
++)
1690 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1693 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1701 * this is the first stab, its strx points to the filename of the
1702 * the source-file, the n_desc field should be set to the number
1703 * of remaining stabs
1705 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1707 /* this is the stab for the main source file */
1708 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1710 /* relocation table entry */
1713 * Since the symbol table has two entries before
1714 * the section symbols, the index in the info.section
1715 * member must be adjusted by adding 2
1718 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1719 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1722 currfile
= mainfileindex
;
1726 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1727 /* oops file has changed... */
1728 for (i
= 0; i
< numfiles
; i
++)
1729 if (!strcmp(allfiles
[i
], ptr
->filename
))
1732 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1736 /* relocation table entry */
1738 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1739 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1742 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1745 /* relocation table entry */
1747 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1748 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1754 /* this is an "ending" token */
1755 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1758 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1760 nasm_free(allfiles
);
1763 stablen
= (sptr
- sbuf
);
1764 stabrellen
= (rptr
- rbuf
);
1770 static void stabsx32_cleanup(void)
1772 struct linelist
*ptr
, *del
;
1784 nasm_free(stabrelbuf
);
1785 nasm_free(stabstrbuf
);
1788 /* dwarf routines */
1790 static void dwarfx32_init(void)
1792 ndebugs
= 3; /* 3 debug symbols */
1795 static void dwarfx32_linenum(const char *filename
, int32_t linenumber
,
1799 dwarfx32_findfile(filename
);
1801 currentline
= linenumber
;
1804 /* called from elf_out with type == TY_DEBUGSYMLIN */
1805 static void dwarfx32_output(int type
, void *param
)
1807 int ln
, aa
, inx
, maxln
, soc
;
1808 struct symlininfo
*s
;
1813 s
= (struct symlininfo
*)param
;
1815 /* line number info is only gathered for executable sections */
1816 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1819 /* Check if section index has changed */
1820 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1821 dwarfx32_findsect(s
->section
);
1823 /* do nothing unless line or file has changed */
1827 ln
= currentline
- dwarf_csect
->line
;
1828 aa
= s
->offset
- dwarf_csect
->offset
;
1829 inx
= dwarf_clist
->line
;
1830 plinep
= dwarf_csect
->psaa
;
1831 /* check for file change */
1832 if (!(inx
== dwarf_csect
->file
)) {
1833 saa_write8(plinep
,DW_LNS_set_file
);
1834 saa_write8(plinep
,inx
);
1835 dwarf_csect
->file
= inx
;
1837 /* check for line change */
1839 /* test if in range of special op code */
1840 maxln
= line_base
+ line_range
;
1841 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1842 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1843 saa_write8(plinep
,soc
);
1845 saa_write8(plinep
,DW_LNS_advance_line
);
1846 saa_wleb128s(plinep
,ln
);
1848 saa_write8(plinep
,DW_LNS_advance_pc
);
1849 saa_wleb128u(plinep
,aa
);
1852 dwarf_csect
->line
= currentline
;
1853 dwarf_csect
->offset
= s
->offset
;
1856 /* show change handled */
1861 static void dwarfx32_generate(void)
1865 struct linelist
*ftentry
;
1866 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1867 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1868 struct sectlist
*psect
;
1869 size_t saalen
, linepoff
, totlen
, highaddr
;
1871 /* write epilogues for each line program range */
1872 /* and build aranges section */
1873 paranges
= saa_init(1L);
1874 parangesrel
= saa_init(1L);
1875 saa_write16(paranges
,3); /* dwarf version */
1876 saa_write32(parangesrel
, paranges
->datalen
+4);
1877 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_X86_64_32
); /* reloc to info */
1878 saa_write32(parangesrel
, 0);
1879 saa_write32(paranges
,0); /* offset into info */
1880 saa_write8(paranges
,4); /* pointer size */
1881 saa_write8(paranges
,0); /* not segmented */
1882 saa_write32(paranges
,0); /* padding */
1883 /* iterate though sectlist entries */
1884 psect
= dwarf_fsect
;
1887 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1889 plinep
= psect
->psaa
;
1890 /* Line Number Program Epilogue */
1891 saa_write8(plinep
,2); /* std op 2 */
1892 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1893 saa_write8(plinep
,DW_LNS_extended_op
);
1894 saa_write8(plinep
,1); /* operand length */
1895 saa_write8(plinep
,DW_LNE_end_sequence
);
1896 totlen
+= plinep
->datalen
;
1897 /* range table relocation entry */
1898 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1899 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_X86_64_32
);
1900 saa_write32(parangesrel
, (uint32_t) 0);
1901 /* range table entry */
1902 saa_write32(paranges
,0x0000); /* range start */
1903 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1904 highaddr
+= sects
[psect
->section
]->len
;
1905 /* done with this entry */
1906 psect
= psect
->next
;
1908 saa_write32(paranges
,0); /* null address */
1909 saa_write32(paranges
,0); /* null length */
1910 saalen
= paranges
->datalen
;
1911 arangeslen
= saalen
+ 4;
1912 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1913 WRITELONG(pbuf
,saalen
); /* initial length */
1914 saa_rnbytes(paranges
, pbuf
, saalen
);
1917 /* build rela.aranges section */
1918 arangesrellen
= saalen
= parangesrel
->datalen
;
1919 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1920 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1921 saa_free(parangesrel
);
1923 /* build pubnames section */
1924 ppubnames
= saa_init(1L);
1925 saa_write16(ppubnames
,3); /* dwarf version */
1926 saa_write32(ppubnames
,0); /* offset into info */
1927 saa_write32(ppubnames
,0); /* space used in info */
1928 saa_write32(ppubnames
,0); /* end of list */
1929 saalen
= ppubnames
->datalen
;
1930 pubnameslen
= saalen
+ 4;
1931 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1932 WRITELONG(pbuf
,saalen
); /* initial length */
1933 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1934 saa_free(ppubnames
);
1936 /* build info section */
1937 pinfo
= saa_init(1L);
1938 pinforel
= saa_init(1L);
1939 saa_write16(pinfo
,3); /* dwarf version */
1940 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1941 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_X86_64_32
); /* reloc to abbrev */
1942 saa_write32(pinforel
, 0);
1943 saa_write32(pinfo
,0); /* offset into abbrev */
1944 saa_write8(pinfo
,4); /* pointer size */
1945 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1946 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1947 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1948 saa_write32(pinforel
, 0);
1949 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1950 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1951 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1952 saa_write32(pinforel
, 0);
1953 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1954 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1955 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_X86_64_32
); /* reloc to line */
1956 saa_write32(pinforel
, 0);
1957 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1958 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1959 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1960 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1961 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1962 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1963 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1964 saa_write32(pinforel
, 0);
1965 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1966 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1967 saa_write8(pinfo
,0); /* end of entries */
1968 saalen
= pinfo
->datalen
;
1969 infolen
= saalen
+ 4;
1970 infobuf
= pbuf
= nasm_malloc(infolen
);
1971 WRITELONG(pbuf
,saalen
); /* initial length */
1972 saa_rnbytes(pinfo
, pbuf
, saalen
);
1975 /* build rela.info section */
1976 inforellen
= saalen
= pinforel
->datalen
;
1977 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1978 saa_rnbytes(pinforel
, pbuf
, saalen
);
1981 /* build abbrev section */
1982 pabbrev
= saa_init(1L);
1983 saa_write8(pabbrev
,1); /* entry number LEB128u */
1984 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1985 saa_write8(pabbrev
,1); /* has children */
1986 /* the following attributes and forms are all LEB128u values */
1987 saa_write8(pabbrev
,DW_AT_low_pc
);
1988 saa_write8(pabbrev
,DW_FORM_addr
);
1989 saa_write8(pabbrev
,DW_AT_high_pc
);
1990 saa_write8(pabbrev
,DW_FORM_addr
);
1991 saa_write8(pabbrev
,DW_AT_stmt_list
);
1992 saa_write8(pabbrev
,DW_FORM_data4
);
1993 saa_write8(pabbrev
,DW_AT_name
);
1994 saa_write8(pabbrev
,DW_FORM_string
);
1995 saa_write8(pabbrev
,DW_AT_producer
);
1996 saa_write8(pabbrev
,DW_FORM_string
);
1997 saa_write8(pabbrev
,DW_AT_language
);
1998 saa_write8(pabbrev
,DW_FORM_data2
);
1999 saa_write16(pabbrev
,0); /* end of entry */
2000 /* LEB128u usage same as above */
2001 saa_write8(pabbrev
,2); /* entry number */
2002 saa_write8(pabbrev
,DW_TAG_subprogram
);
2003 saa_write8(pabbrev
,0); /* no children */
2004 saa_write8(pabbrev
,DW_AT_low_pc
);
2005 saa_write8(pabbrev
,DW_FORM_addr
);
2006 saa_write8(pabbrev
,DW_AT_frame_base
);
2007 saa_write8(pabbrev
,DW_FORM_data4
);
2008 saa_write16(pabbrev
,0); /* end of entry */
2009 abbrevlen
= saalen
= pabbrev
->datalen
;
2010 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2011 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2014 /* build line section */
2016 plines
= saa_init(1L);
2017 saa_write8(plines
,1); /* Minimum Instruction Length */
2018 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2019 saa_write8(plines
,line_base
); /* Line Base */
2020 saa_write8(plines
,line_range
); /* Line Range */
2021 saa_write8(plines
,opcode_base
); /* Opcode Base */
2022 /* standard opcode lengths (# of LEB128u operands) */
2023 saa_write8(plines
,0); /* Std opcode 1 length */
2024 saa_write8(plines
,1); /* Std opcode 2 length */
2025 saa_write8(plines
,1); /* Std opcode 3 length */
2026 saa_write8(plines
,1); /* Std opcode 4 length */
2027 saa_write8(plines
,1); /* Std opcode 5 length */
2028 saa_write8(plines
,0); /* Std opcode 6 length */
2029 saa_write8(plines
,0); /* Std opcode 7 length */
2030 saa_write8(plines
,0); /* Std opcode 8 length */
2031 saa_write8(plines
,1); /* Std opcode 9 length */
2032 saa_write8(plines
,0); /* Std opcode 10 length */
2033 saa_write8(plines
,0); /* Std opcode 11 length */
2034 saa_write8(plines
,1); /* Std opcode 12 length */
2035 /* Directory Table */
2036 saa_write8(plines
,0); /* End of table */
2037 /* File Name Table */
2038 ftentry
= dwarf_flist
;
2039 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2041 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2042 saa_write8(plines
,0); /* directory LEB128u */
2043 saa_write8(plines
,0); /* time LEB128u */
2044 saa_write8(plines
,0); /* size LEB128u */
2045 ftentry
= ftentry
->next
;
2047 saa_write8(plines
,0); /* End of table */
2048 linepoff
= plines
->datalen
;
2049 linelen
= linepoff
+ totlen
+ 10;
2050 linebuf
= pbuf
= nasm_malloc(linelen
);
2051 WRITELONG(pbuf
,linelen
-4); /* initial length */
2052 WRITESHORT(pbuf
,3); /* dwarf version */
2053 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2054 /* write line header */
2056 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2059 /* concatonate line program ranges */
2061 plinesrel
= saa_init(1L);
2062 psect
= dwarf_fsect
;
2063 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2064 saa_write32(plinesrel
, linepoff
);
2065 saa_write32(plinesrel
, ((psect
->section
+ 2) << 8) + R_X86_64_32
);
2066 saa_write32(plinesrel
, 0);
2067 plinep
= psect
->psaa
;
2068 saalen
= plinep
->datalen
;
2069 saa_rnbytes(plinep
, pbuf
, saalen
);
2073 /* done with this entry */
2074 psect
= psect
->next
;
2078 /* build rela.lines section */
2079 linerellen
=saalen
= plinesrel
->datalen
;
2080 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2081 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2082 saa_free(plinesrel
);
2084 /* build frame section */
2086 framebuf
= pbuf
= nasm_malloc(framelen
);
2087 WRITELONG(pbuf
,framelen
-4); /* initial length */
2089 /* build loc section */
2091 locbuf
= pbuf
= nasm_malloc(loclen
);
2092 WRITELONG(pbuf
,0); /* null beginning offset */
2093 WRITELONG(pbuf
,0); /* null ending offset */
2096 static void dwarfx32_cleanup(void)
2098 nasm_free(arangesbuf
);
2099 nasm_free(arangesrelbuf
);
2100 nasm_free(pubnamesbuf
);
2102 nasm_free(inforelbuf
);
2103 nasm_free(abbrevbuf
);
2105 nasm_free(linerelbuf
);
2106 nasm_free(framebuf
);
2110 static void dwarfx32_findfile(const char * fname
)
2113 struct linelist
*match
;
2115 /* return if fname is current file name */
2116 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2119 /* search for match */
2122 match
= dwarf_flist
;
2123 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2124 if (!(strcmp(fname
, match
->filename
))) {
2125 dwarf_clist
= match
;
2131 /* add file name to end of list */
2132 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2134 dwarf_clist
->line
= dwarf_numfiles
;
2135 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2136 strcpy(dwarf_clist
->filename
,fname
);
2137 dwarf_clist
->next
= 0;
2138 if (!dwarf_flist
) { /* if first entry */
2139 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2140 dwarf_clist
->last
= 0;
2141 } else { /* chain to previous entry */
2142 dwarf_elist
->next
= dwarf_clist
;
2143 dwarf_elist
= dwarf_clist
;
2147 static void dwarfx32_findsect(const int index
)
2150 struct sectlist
*match
;
2153 /* return if index is current section index */
2154 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2157 /* search for match */
2160 match
= dwarf_fsect
;
2161 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2162 if (match
->section
== index
) {
2163 dwarf_csect
= match
;
2166 match
= match
->next
;
2170 /* add entry to end of list */
2171 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2173 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2174 dwarf_csect
->line
= 1;
2175 dwarf_csect
->offset
= 0;
2176 dwarf_csect
->file
= 1;
2177 dwarf_csect
->section
= index
;
2178 dwarf_csect
->next
= 0;
2179 /* set relocatable address at start of line program */
2180 saa_write8(plinep
,DW_LNS_extended_op
);
2181 saa_write8(plinep
,5); /* operand length */
2182 saa_write8(plinep
,DW_LNE_set_address
);
2183 saa_write32(plinep
,0); /* Start Address */
2185 if (!dwarf_fsect
) { /* if first entry */
2186 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2187 dwarf_csect
->last
= 0;
2188 } else { /* chain to previous entry */
2189 dwarf_esect
->next
= dwarf_csect
;
2190 dwarf_esect
= dwarf_csect
;
2194 #endif /* OF_ELFX32 */