1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 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
);
787 int isize
= (int)size
;
788 int asize
= abs(size
);
790 addr
= *(int64_t *)data
;
791 if (segment
== NO_SEG
) {
793 } else if (segment
% 2) {
794 nasm_error(ERR_NONFATAL
, "ELF format does not support"
795 " segment base references");
801 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
805 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
808 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
811 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
815 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
818 nasm_error(ERR_PANIC
, "internal error elfx32-hpa-871");
822 } else if (wrt
== elf_gotpc_sect
+ 1) {
824 * The user will supply GOT relative to $$. ELF
825 * will let us have GOT relative to $. So we
826 * need to fix up the data item by $-$$.
829 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
831 } else if (wrt
== elf_gotoff_sect
+ 1) {
832 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
833 "R_X86_64_GOTOFF64");
834 } else if (wrt
== elf_got_sect
+ 1) {
837 elf_add_gsym_reloc(s
, segment
, addr
, 0,
838 R_X86_64_GOT32
, true);
842 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
845 } else if (wrt
== elf_sym_sect
+ 1) {
849 elf_add_gsym_reloc(s
, segment
, addr
, 0,
855 elf_add_gsym_reloc(s
, segment
, addr
, 0,
860 elf_add_gsym_reloc(s
, segment
, addr
, 0,
865 elf_add_gsym_reloc(s
, segment
, addr
, 0,
866 R_X86_64_32S
, false);
871 elf_add_gsym_reloc(s
, segment
, addr
, 0,
876 nasm_error(ERR_PANIC
, "internal error elfx32-hpa-903");
879 } else if (wrt
== elf_plt_sect
+ 1) {
880 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
881 "relative PLT references");
883 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
887 elf_sect_writeaddr(s
, addr
, asize
);
892 reltype
= R_X86_64_PC8
;
897 reltype
= R_X86_64_PC16
;
902 addr
= *(int64_t *)data
- size
;
903 if (segment
== segto
)
904 nasm_error(ERR_PANIC
, "intra-segment OUT_REL1ADR");
905 if (segment
== NO_SEG
) {
907 } else if (segment
% 2) {
908 nasm_error(ERR_NONFATAL
, "ELF format does not support"
909 " segment base references");
912 elf_add_reloc(s
, segment
, addr
, reltype
);
915 nasm_error(ERR_NONFATAL
,
916 "Unsupported non-32-bit ELF relocation");
919 elf_sect_writeaddr(s
, addr
, bytes
);
923 addr
= *(int64_t *)data
- size
;
924 if (segment
== segto
)
925 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
926 if (segment
== NO_SEG
) {
928 } else if (segment
% 2) {
929 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
930 " segment base references");
933 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
935 } else if (wrt
== elf_plt_sect
+ 1) {
936 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
937 R_X86_64_PLT32
, true);
939 } else if (wrt
== elf_gotpc_sect
+ 1 ||
940 wrt
== elf_got_sect
+ 1) {
941 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
942 R_X86_64_GOTPCREL
, true);
944 } else if (wrt
== elf_gotoff_sect
+ 1 ||
945 wrt
== elf_got_sect
+ 1) {
946 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
947 } else if (wrt
== elf_gottpoff_sect
+ 1) {
948 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
949 R_X86_64_GOTTPOFF
, true);
952 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
956 elf_sect_writeaddr(s
, addr
, 4);
960 nasm_error(ERR_NONFATAL
,
961 "32-bit ELF format does not support 64-bit relocations");
963 elf_sect_writeaddr(s
, addr
, 8);
968 static void elf_write(void)
975 int32_t symtablen
, symtablocal
;
978 * Work out how many sections we will have. We have SHN_UNDEF,
979 * then the flexible user sections, then the fixed sections
980 * `.shstrtab', `.symtab' and `.strtab', then optionally
981 * relocation sections for the user sections.
983 nsections
= sec_numspecial
+ 1;
984 if (of_elfx32
.current_dfmt
== &df_stabs
)
986 else if (of_elfx32
.current_dfmt
== &df_dwarf
)
989 add_sectname("", ".shstrtab");
990 add_sectname("", ".symtab");
991 add_sectname("", ".strtab");
992 for (i
= 0; i
< nsects
; i
++) {
993 nsections
++; /* for the section itself */
994 if (sects
[i
]->head
) {
995 nsections
++; /* for its relocations */
996 add_sectname(".rela", sects
[i
]->name
);
1000 if (of_elfx32
.current_dfmt
== &df_stabs
) {
1001 /* in case the debug information is wanted, just add these three sections... */
1002 add_sectname("", ".stab");
1003 add_sectname("", ".stabstr");
1004 add_sectname(".rel", ".stab");
1007 else if (of_elfx32
.current_dfmt
== &df_dwarf
) {
1008 /* the dwarf debug standard specifies the following ten sections,
1009 not all of which are currently implemented,
1010 although all of them are defined. */
1011 add_sectname("", ".debug_aranges");
1012 add_sectname(".rela", ".debug_aranges");
1013 add_sectname("", ".debug_pubnames");
1014 add_sectname("", ".debug_info");
1015 add_sectname(".rela", ".debug_info");
1016 add_sectname("", ".debug_abbrev");
1017 add_sectname("", ".debug_line");
1018 add_sectname(".rela", ".debug_line");
1019 add_sectname("", ".debug_frame");
1020 add_sectname("", ".debug_loc");
1024 * Output the ELF header.
1026 fwrite("\177ELF\1\1\1", 7, 1, ofile
);
1027 fputc(elf_osabi
, ofile
);
1028 fputc(elf_abiver
, ofile
);
1029 fwritezero(7, ofile
);
1030 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1031 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1032 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1033 fwriteint32_t(0L, ofile
); /* no entry point */
1034 fwriteint32_t(0L, ofile
); /* no program header table */
1035 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
1036 * ELF header plus alignment */
1037 fwriteint32_t(0L, ofile
); /* X86_64 defines no special flags */
1038 fwriteint16_t(0x34, ofile
); /* size of ELF header */
1039 fwriteint16_t(0, ofile
); /* no program header table, again */
1040 fwriteint16_t(0, ofile
); /* still no program header table */
1041 fwriteint16_t(sizeof(Elf32_Shdr
), ofile
); /* size of section header */
1042 fwriteint16_t(nsections
, ofile
); /* number of sections */
1043 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1044 * section header table */
1045 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
1046 fwriteint32_t(0L, ofile
);
1047 fwriteint32_t(0L, ofile
);
1050 * Build the symbol table and relocation tables.
1052 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1053 for (i
= 0; i
< nsects
; i
++)
1055 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1059 * Now output the section header table.
1062 elf_foffs
= 0x40 + sizeof(Elf32_Shdr
) * nsections
;
1063 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1066 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1069 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1072 /* The normal sections */
1073 for (i
= 0; i
< nsects
; i
++) {
1074 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1075 (sects
[i
]->type
== SHT_PROGBITS
?
1076 sects
[i
]->data
: NULL
), true,
1077 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1082 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1083 shstrtablen
, 0, 0, 1, 0);
1087 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1088 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1092 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1093 strslen
, 0, 0, 1, 0);
1096 /* The relocation sections */
1097 for (i
= 0; i
< nsects
; i
++)
1098 if (sects
[i
]->head
) {
1099 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1100 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 12);
1104 if (of_elfx32
.current_dfmt
== &df_stabs
) {
1105 /* for debugging information, create the last three sections
1106 which are the .stab , .stabstr and .rel.stab sections respectively */
1108 /* this function call creates the stab sections in memory */
1109 stabsx32_generate();
1111 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1112 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1113 stablen
, sec_stabstr
, 0, 4, 12);
1116 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1117 stabstrlen
, 0, 0, 4, 0);
1120 /* link -> symtable info -> section to refer to */
1121 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1122 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1125 } else if (of_elfx32
.current_dfmt
== &df_dwarf
) {
1126 /* for dwarf debugging information, create the ten dwarf sections */
1128 /* this function call creates the dwarf sections in memory */
1130 dwarfx32_generate();
1132 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1133 arangeslen
, 0, 0, 1, 0);
1136 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1137 arangesrellen
, sec_symtab
, sec_debug_aranges
, 1, 12);
1140 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1141 pubnameslen
, 0, 0, 1, 0);
1144 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1145 infolen
, 0, 0, 1, 0);
1148 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1149 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1152 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1153 abbrevlen
, 0, 0, 1, 0);
1156 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1157 linelen
, 0, 0, 1, 0);
1160 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1161 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1164 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1165 framelen
, 0, 0, 8, 0);
1168 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1169 loclen
, 0, 0, 1, 0);
1172 fwritezero(align
, ofile
);
1175 * Now output the sections.
1177 elf_write_sections();
1179 nasm_free(elf_sects
);
1183 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1185 struct SAA
*s
= saa_init(1L);
1187 uint8_t entry
[24], *p
;
1193 * First, an all-zeros entry, required by the ELF spec.
1195 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1200 * Next, an entry for the file name.
1203 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1204 WRITELONG(p
, 0); /* no value */
1205 WRITELONG(p
, 0); /* no size either */
1206 WRITESHORT(p
, STT_FILE
); /* type FILE */
1207 WRITESHORT(p
, SHN_ABS
);
1208 saa_wbytes(s
, entry
, 16L);
1213 * Now some standard symbols defining the segments, for relocation
1216 for (i
= 1; i
<= nsects
; i
++) {
1218 WRITELONG(p
, 0); /* no symbol name */
1219 WRITELONG(p
, 0); /* offset zero */
1220 WRITELONG(p
, 0); /* size zero */
1221 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1222 WRITESHORT(p
, i
); /* section id */
1223 saa_wbytes(s
, entry
, 16L);
1230 * Now the other local symbols.
1233 while ((sym
= saa_rstruct(syms
))) {
1234 if (sym
->type
& SYM_GLOBAL
)
1237 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1238 WRITELONG(p
, sym
->symv
.key
); /* value of symbol */
1239 WRITELONG(p
, sym
->size
); /* size of symbol */
1240 WRITECHAR(p
, sym
->type
); /* type and binding */
1241 WRITECHAR(p
, sym
->other
); /* visibility */
1242 WRITESHORT(p
, sym
->section
); /* index into section header table */
1243 saa_wbytes(s
, entry
, 16L);
1248 * dwarf needs symbols for debug sections
1249 * which are relocation targets.
1251 if (of_elfx32
.current_dfmt
== &df_dwarf
) {
1252 dwarf_infosym
= *local
;
1254 WRITELONG(p
, 0); /* no symbol name */
1255 WRITELONG(p
, 0); /* offset zero */
1256 WRITELONG(p
, 0); /* size zero */
1257 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1258 WRITESHORT(p
, sec_debug_info
); /* section id */
1259 saa_wbytes(s
, entry
, 16L);
1262 dwarf_abbrevsym
= *local
;
1264 WRITELONG(p
, 0); /* no symbol name */
1265 WRITELONG(p
, 0); /* offset zero */
1266 WRITELONG(p
, 0); /* size zero */
1267 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1268 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1269 saa_wbytes(s
, entry
, 16L);
1272 dwarf_linesym
= *local
;
1274 WRITELONG(p
, 0); /* no symbol name */
1275 WRITELONG(p
, 0); /* offset zero */
1276 WRITELONG(p
, 0); /* size zero */
1277 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1278 WRITESHORT(p
, sec_debug_line
); /* section id */
1279 saa_wbytes(s
, entry
, 16L);
1285 * Now the global symbols.
1288 while ((sym
= saa_rstruct(syms
))) {
1289 if (!(sym
->type
& SYM_GLOBAL
))
1292 WRITELONG(p
, sym
->strpos
);
1293 WRITELONG(p
, sym
->symv
.key
);
1294 WRITELONG(p
, sym
->size
);
1295 WRITECHAR(p
, sym
->type
); /* type and binding */
1296 WRITECHAR(p
, sym
->other
); /* visibility */
1297 WRITESHORT(p
, sym
->section
);
1298 saa_wbytes(s
, entry
, 16L);
1305 static struct SAA
*elf_build_reltab(uint32_t *len
, struct Reloc
*r
)
1308 uint8_t *p
, entry
[12];
1309 int32_t global_offset
;
1318 * How to onvert from a global placeholder to a real symbol index;
1319 * the +2 refers to the two special entries, the null entry and
1320 * the filename entry.
1322 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1325 int32_t sym
= r
->symbol
;
1327 if (sym
>= GLOBAL_TEMP_BASE
)
1328 sym
+= global_offset
;
1331 WRITELONG(p
, r
->address
);
1332 WRITELONG(p
, (sym
<< 8) + r
->type
);
1333 WRITELONG(p
, r
->offset
);
1334 saa_wbytes(s
, entry
, 12L);
1343 static void elf_section_header(int name
, int type
, uint32_t flags
,
1344 void *data
, bool is_saa
, uint32_t datalen
,
1345 int link
, int info
, int align
, int eltsize
)
1347 elf_sects
[elf_nsect
].data
= data
;
1348 elf_sects
[elf_nsect
].len
= datalen
;
1349 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1352 fwriteint32_t((int32_t)name
, ofile
);
1353 fwriteint32_t((int32_t)type
, ofile
);
1354 fwriteint32_t((int32_t)flags
, ofile
);
1355 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1356 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1357 fwriteint32_t(datalen
, ofile
);
1359 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1360 fwriteint32_t((int32_t)link
, ofile
);
1361 fwriteint32_t((int32_t)info
, ofile
);
1362 fwriteint32_t((int32_t)align
, ofile
);
1363 fwriteint32_t((int32_t)eltsize
, ofile
);
1366 static void elf_write_sections(void)
1369 for (i
= 0; i
< elf_nsect
; i
++)
1370 if (elf_sects
[i
].data
) {
1371 int32_t len
= elf_sects
[i
].len
;
1372 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1373 int32_t align
= reallen
- len
;
1374 if (elf_sects
[i
].is_saa
)
1375 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1377 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1378 fwritezero(align
, ofile
);
1382 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1384 saa_wbytes(sect
->data
, data
, len
);
1387 static void elf_sect_writeaddr(struct Section
*sect
, int32_t data
, size_t len
)
1389 saa_writeaddr(sect
->data
, data
, len
);
1393 static void elf_sectalign(int32_t seg
, unsigned int value
)
1395 struct Section
*s
= NULL
;
1398 for (i
= 0; i
< nsects
; i
++) {
1399 if (sects
[i
]->index
== seg
) {
1404 if (!s
|| !is_power2(value
))
1407 if (value
> s
->align
)
1411 static int32_t elf_segbase(int32_t segment
)
1416 static int elf_directive(enum directives directive
, char *value
, int pass
)
1422 switch (directive
) {
1425 return 1; /* ignore in pass 2 */
1427 n
= readnum(value
, &err
);
1429 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1432 if (n
< 0 || n
> 255) {
1433 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1439 if ((p
= strchr(value
,',')) == NULL
)
1442 n
= readnum(p
+1, &err
);
1443 if (err
|| n
< 0 || n
> 255) {
1444 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1456 static void elf_filename(char *inname
, char *outname
)
1458 strcpy(elf_module
, inname
);
1459 standard_extension(inname
, outname
, ".o");
1462 extern macros_t elf_stdmac
[];
1464 static int elf_set_info(enum geninfo type
, char **val
)
1470 static struct dfmt df_dwarf
= {
1471 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1481 static struct dfmt df_stabs
= {
1482 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1493 struct dfmt
*elfx32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1495 struct ofmt of_elfx32
= {
1496 "ELFX32 (x86_64) object files (e.g. Linux)",
1514 /* common debugging routines */
1515 static void debugx32_deflabel(char *name
, int32_t segment
, int64_t offset
,
1516 int is_global
, char *special
)
1525 static void debugx32_directive(const char *directive
, const char *params
)
1531 static void debugx32_typevalue(int32_t type
)
1533 int32_t stype
, ssize
;
1534 switch (TYM_TYPE(type
)) {
1577 stype
= STT_SECTION
;
1592 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1593 lastsym
->size
= ssize
;
1594 lastsym
->type
= stype
;
1598 /* stabs debugging routines */
1600 static void stabsx32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1603 if (!stabs_filename
) {
1604 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1605 strcpy(stabs_filename
, filename
);
1607 if (strcmp(stabs_filename
, filename
)) {
1608 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1609 in fact, this leak comes in quite handy to maintain a list of files
1610 encountered so far in the symbol lines... */
1612 /* why not nasm_free(stabs_filename); we're done with the old one */
1614 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1615 strcpy(stabs_filename
, filename
);
1619 currentline
= linenumber
;
1623 static void stabsx32_output(int type
, void *param
)
1625 struct symlininfo
*s
;
1626 struct linelist
*el
;
1627 if (type
== TY_DEBUGSYMLIN
) {
1628 if (debug_immcall
) {
1629 s
= (struct symlininfo
*)param
;
1630 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1631 return; /* line info is only collected for executable sections */
1633 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1634 el
->info
.offset
= s
->offset
;
1635 el
->info
.section
= s
->section
;
1636 el
->info
.name
= s
->name
;
1637 el
->line
= currentline
;
1638 el
->filename
= stabs_filename
;
1641 stabslines
->last
->next
= el
;
1642 stabslines
->last
= el
;
1645 stabslines
->last
= el
;
1652 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1654 static void stabsx32_generate(void)
1656 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1657 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1661 struct linelist
*ptr
;
1665 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
1668 if (numfiles
== 0) {
1669 allfiles
[0] = ptr
->filename
;
1672 for (i
= 0; i
< numfiles
; i
++) {
1673 if (!strcmp(allfiles
[i
], ptr
->filename
))
1676 if (i
>= numfiles
) {
1677 allfiles
[i
] = ptr
->filename
;
1684 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1685 for (i
= 0; i
< numfiles
; i
++) {
1686 fileidx
[i
] = strsize
;
1687 strsize
+= strlen(allfiles
[i
]) + 1;
1690 for (i
= 0; i
< numfiles
; i
++) {
1691 if (!strcmp(allfiles
[i
], elf_module
)) {
1698 * worst case size of the stab buffer would be:
1699 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1700 * plus one "ending" entry
1702 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1703 sizeof(struct stabentry
));
1704 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1705 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1708 for (i
= 0; i
< numfiles
; i
++)
1709 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1712 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1720 * this is the first stab, its strx points to the filename of the
1721 * the source-file, the n_desc field should be set to the number
1722 * of remaining stabs
1724 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1726 /* this is the stab for the main source file */
1727 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1729 /* relocation table entry */
1732 * Since the symbol table has two entries before
1733 * the section symbols, the index in the info.section
1734 * member must be adjusted by adding 2
1737 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1738 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1741 currfile
= mainfileindex
;
1745 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1746 /* oops file has changed... */
1747 for (i
= 0; i
< numfiles
; i
++)
1748 if (!strcmp(allfiles
[i
], ptr
->filename
))
1751 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1755 /* relocation table entry */
1757 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1758 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1761 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1764 /* relocation table entry */
1766 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1767 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1773 /* this is an "ending" token */
1774 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1777 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1779 nasm_free(allfiles
);
1782 stablen
= (sptr
- sbuf
);
1783 stabrellen
= (rptr
- rbuf
);
1789 static void stabsx32_cleanup(void)
1791 struct linelist
*ptr
, *del
;
1803 nasm_free(stabrelbuf
);
1804 nasm_free(stabstrbuf
);
1807 /* dwarf routines */
1809 static void dwarfx32_init(void)
1811 ndebugs
= 3; /* 3 debug symbols */
1814 static void dwarfx32_linenum(const char *filename
, int32_t linenumber
,
1818 dwarfx32_findfile(filename
);
1820 currentline
= linenumber
;
1823 /* called from elf_out with type == TY_DEBUGSYMLIN */
1824 static void dwarfx32_output(int type
, void *param
)
1826 int ln
, aa
, inx
, maxln
, soc
;
1827 struct symlininfo
*s
;
1832 s
= (struct symlininfo
*)param
;
1834 /* line number info is only gathered for executable sections */
1835 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1838 /* Check if section index has changed */
1839 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1840 dwarfx32_findsect(s
->section
);
1842 /* do nothing unless line or file has changed */
1846 ln
= currentline
- dwarf_csect
->line
;
1847 aa
= s
->offset
- dwarf_csect
->offset
;
1848 inx
= dwarf_clist
->line
;
1849 plinep
= dwarf_csect
->psaa
;
1850 /* check for file change */
1851 if (!(inx
== dwarf_csect
->file
)) {
1852 saa_write8(plinep
,DW_LNS_set_file
);
1853 saa_write8(plinep
,inx
);
1854 dwarf_csect
->file
= inx
;
1856 /* check for line change */
1858 /* test if in range of special op code */
1859 maxln
= line_base
+ line_range
;
1860 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1861 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1862 saa_write8(plinep
,soc
);
1864 saa_write8(plinep
,DW_LNS_advance_line
);
1865 saa_wleb128s(plinep
,ln
);
1867 saa_write8(plinep
,DW_LNS_advance_pc
);
1868 saa_wleb128u(plinep
,aa
);
1871 dwarf_csect
->line
= currentline
;
1872 dwarf_csect
->offset
= s
->offset
;
1875 /* show change handled */
1880 static void dwarfx32_generate(void)
1884 struct linelist
*ftentry
;
1885 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1886 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1887 struct sectlist
*psect
;
1888 size_t saalen
, linepoff
, totlen
, highaddr
;
1890 /* write epilogues for each line program range */
1891 /* and build aranges section */
1892 paranges
= saa_init(1L);
1893 parangesrel
= saa_init(1L);
1894 saa_write16(paranges
,3); /* dwarf version */
1895 saa_write32(parangesrel
, paranges
->datalen
+4);
1896 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_X86_64_32
); /* reloc to info */
1897 saa_write32(parangesrel
, 0);
1898 saa_write32(paranges
,0); /* offset into info */
1899 saa_write8(paranges
,4); /* pointer size */
1900 saa_write8(paranges
,0); /* not segmented */
1901 saa_write32(paranges
,0); /* padding */
1902 /* iterate though sectlist entries */
1903 psect
= dwarf_fsect
;
1906 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1908 plinep
= psect
->psaa
;
1909 /* Line Number Program Epilogue */
1910 saa_write8(plinep
,2); /* std op 2 */
1911 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1912 saa_write8(plinep
,DW_LNS_extended_op
);
1913 saa_write8(plinep
,1); /* operand length */
1914 saa_write8(plinep
,DW_LNE_end_sequence
);
1915 totlen
+= plinep
->datalen
;
1916 /* range table relocation entry */
1917 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1918 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_X86_64_32
);
1919 saa_write32(parangesrel
, (uint32_t) 0);
1920 /* range table entry */
1921 saa_write32(paranges
,0x0000); /* range start */
1922 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1923 highaddr
+= sects
[psect
->section
]->len
;
1924 /* done with this entry */
1925 psect
= psect
->next
;
1927 saa_write32(paranges
,0); /* null address */
1928 saa_write32(paranges
,0); /* null length */
1929 saalen
= paranges
->datalen
;
1930 arangeslen
= saalen
+ 4;
1931 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1932 WRITELONG(pbuf
,saalen
); /* initial length */
1933 saa_rnbytes(paranges
, pbuf
, saalen
);
1936 /* build rela.aranges section */
1937 arangesrellen
= saalen
= parangesrel
->datalen
;
1938 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1939 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1940 saa_free(parangesrel
);
1942 /* build pubnames section */
1943 ppubnames
= saa_init(1L);
1944 saa_write16(ppubnames
,3); /* dwarf version */
1945 saa_write32(ppubnames
,0); /* offset into info */
1946 saa_write32(ppubnames
,0); /* space used in info */
1947 saa_write32(ppubnames
,0); /* end of list */
1948 saalen
= ppubnames
->datalen
;
1949 pubnameslen
= saalen
+ 4;
1950 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1951 WRITELONG(pbuf
,saalen
); /* initial length */
1952 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1953 saa_free(ppubnames
);
1955 /* build info section */
1956 pinfo
= saa_init(1L);
1957 pinforel
= saa_init(1L);
1958 saa_write16(pinfo
,3); /* dwarf version */
1959 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1960 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_X86_64_32
); /* reloc to abbrev */
1961 saa_write32(pinforel
, 0);
1962 saa_write32(pinfo
,0); /* offset into abbrev */
1963 saa_write8(pinfo
,4); /* pointer size */
1964 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1965 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1966 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1967 saa_write32(pinforel
, 0);
1968 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1969 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1970 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1971 saa_write32(pinforel
, 0);
1972 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1973 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1974 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_X86_64_32
); /* reloc to line */
1975 saa_write32(pinforel
, 0);
1976 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1977 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1978 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1979 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1980 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1981 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1982 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1983 saa_write32(pinforel
, 0);
1984 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1985 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1986 saa_write8(pinfo
,0); /* end of entries */
1987 saalen
= pinfo
->datalen
;
1988 infolen
= saalen
+ 4;
1989 infobuf
= pbuf
= nasm_malloc(infolen
);
1990 WRITELONG(pbuf
,saalen
); /* initial length */
1991 saa_rnbytes(pinfo
, pbuf
, saalen
);
1994 /* build rela.info section */
1995 inforellen
= saalen
= pinforel
->datalen
;
1996 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1997 saa_rnbytes(pinforel
, pbuf
, saalen
);
2000 /* build abbrev section */
2001 pabbrev
= saa_init(1L);
2002 saa_write8(pabbrev
,1); /* entry number LEB128u */
2003 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2004 saa_write8(pabbrev
,1); /* has children */
2005 /* the following attributes and forms are all LEB128u values */
2006 saa_write8(pabbrev
,DW_AT_low_pc
);
2007 saa_write8(pabbrev
,DW_FORM_addr
);
2008 saa_write8(pabbrev
,DW_AT_high_pc
);
2009 saa_write8(pabbrev
,DW_FORM_addr
);
2010 saa_write8(pabbrev
,DW_AT_stmt_list
);
2011 saa_write8(pabbrev
,DW_FORM_data4
);
2012 saa_write8(pabbrev
,DW_AT_name
);
2013 saa_write8(pabbrev
,DW_FORM_string
);
2014 saa_write8(pabbrev
,DW_AT_producer
);
2015 saa_write8(pabbrev
,DW_FORM_string
);
2016 saa_write8(pabbrev
,DW_AT_language
);
2017 saa_write8(pabbrev
,DW_FORM_data2
);
2018 saa_write16(pabbrev
,0); /* end of entry */
2019 /* LEB128u usage same as above */
2020 saa_write8(pabbrev
,2); /* entry number */
2021 saa_write8(pabbrev
,DW_TAG_subprogram
);
2022 saa_write8(pabbrev
,0); /* no children */
2023 saa_write8(pabbrev
,DW_AT_low_pc
);
2024 saa_write8(pabbrev
,DW_FORM_addr
);
2025 saa_write8(pabbrev
,DW_AT_frame_base
);
2026 saa_write8(pabbrev
,DW_FORM_data4
);
2027 saa_write16(pabbrev
,0); /* end of entry */
2028 abbrevlen
= saalen
= pabbrev
->datalen
;
2029 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2030 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2033 /* build line section */
2035 plines
= saa_init(1L);
2036 saa_write8(plines
,1); /* Minimum Instruction Length */
2037 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2038 saa_write8(plines
,line_base
); /* Line Base */
2039 saa_write8(plines
,line_range
); /* Line Range */
2040 saa_write8(plines
,opcode_base
); /* Opcode Base */
2041 /* standard opcode lengths (# of LEB128u operands) */
2042 saa_write8(plines
,0); /* Std opcode 1 length */
2043 saa_write8(plines
,1); /* Std opcode 2 length */
2044 saa_write8(plines
,1); /* Std opcode 3 length */
2045 saa_write8(plines
,1); /* Std opcode 4 length */
2046 saa_write8(plines
,1); /* Std opcode 5 length */
2047 saa_write8(plines
,0); /* Std opcode 6 length */
2048 saa_write8(plines
,0); /* Std opcode 7 length */
2049 saa_write8(plines
,0); /* Std opcode 8 length */
2050 saa_write8(plines
,1); /* Std opcode 9 length */
2051 saa_write8(plines
,0); /* Std opcode 10 length */
2052 saa_write8(plines
,0); /* Std opcode 11 length */
2053 saa_write8(plines
,1); /* Std opcode 12 length */
2054 /* Directory Table */
2055 saa_write8(plines
,0); /* End of table */
2056 /* File Name Table */
2057 ftentry
= dwarf_flist
;
2058 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2060 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2061 saa_write8(plines
,0); /* directory LEB128u */
2062 saa_write8(plines
,0); /* time LEB128u */
2063 saa_write8(plines
,0); /* size LEB128u */
2064 ftentry
= ftentry
->next
;
2066 saa_write8(plines
,0); /* End of table */
2067 linepoff
= plines
->datalen
;
2068 linelen
= linepoff
+ totlen
+ 10;
2069 linebuf
= pbuf
= nasm_malloc(linelen
);
2070 WRITELONG(pbuf
,linelen
-4); /* initial length */
2071 WRITESHORT(pbuf
,3); /* dwarf version */
2072 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2073 /* write line header */
2075 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2078 /* concatonate line program ranges */
2080 plinesrel
= saa_init(1L);
2081 psect
= dwarf_fsect
;
2082 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2083 saa_write32(plinesrel
, linepoff
);
2084 saa_write32(plinesrel
, ((psect
->section
+ 2) << 8) + R_X86_64_32
);
2085 saa_write32(plinesrel
, 0);
2086 plinep
= psect
->psaa
;
2087 saalen
= plinep
->datalen
;
2088 saa_rnbytes(plinep
, pbuf
, saalen
);
2092 /* done with this entry */
2093 psect
= psect
->next
;
2097 /* build rela.lines section */
2098 linerellen
=saalen
= plinesrel
->datalen
;
2099 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2100 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2101 saa_free(plinesrel
);
2103 /* build frame section */
2105 framebuf
= pbuf
= nasm_malloc(framelen
);
2106 WRITELONG(pbuf
,framelen
-4); /* initial length */
2108 /* build loc section */
2110 locbuf
= pbuf
= nasm_malloc(loclen
);
2111 WRITELONG(pbuf
,0); /* null beginning offset */
2112 WRITELONG(pbuf
,0); /* null ending offset */
2115 static void dwarfx32_cleanup(void)
2117 nasm_free(arangesbuf
);
2118 nasm_free(arangesrelbuf
);
2119 nasm_free(pubnamesbuf
);
2121 nasm_free(inforelbuf
);
2122 nasm_free(abbrevbuf
);
2124 nasm_free(linerelbuf
);
2125 nasm_free(framebuf
);
2129 static void dwarfx32_findfile(const char * fname
)
2132 struct linelist
*match
;
2134 /* return if fname is current file name */
2135 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2138 /* search for match */
2141 match
= dwarf_flist
;
2142 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2143 if (!(strcmp(fname
, match
->filename
))) {
2144 dwarf_clist
= match
;
2150 /* add file name to end of list */
2151 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2153 dwarf_clist
->line
= dwarf_numfiles
;
2154 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2155 strcpy(dwarf_clist
->filename
,fname
);
2156 dwarf_clist
->next
= 0;
2157 if (!dwarf_flist
) { /* if first entry */
2158 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2159 dwarf_clist
->last
= 0;
2160 } else { /* chain to previous entry */
2161 dwarf_elist
->next
= dwarf_clist
;
2162 dwarf_elist
= dwarf_clist
;
2166 static void dwarfx32_findsect(const int index
)
2169 struct sectlist
*match
;
2172 /* return if index is current section index */
2173 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2176 /* search for match */
2179 match
= dwarf_fsect
;
2180 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2181 if (match
->section
== index
) {
2182 dwarf_csect
= match
;
2185 match
= match
->next
;
2189 /* add entry to end of list */
2190 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2192 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2193 dwarf_csect
->line
= 1;
2194 dwarf_csect
->offset
= 0;
2195 dwarf_csect
->file
= 1;
2196 dwarf_csect
->section
= index
;
2197 dwarf_csect
->next
= 0;
2198 /* set relocatable address at start of line program */
2199 saa_write8(plinep
,DW_LNS_extended_op
);
2200 saa_write8(plinep
,5); /* operand length */
2201 saa_write8(plinep
,DW_LNE_set_address
);
2202 saa_write32(plinep
,0); /* Start Address */
2204 if (!dwarf_fsect
) { /* if first entry */
2205 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2206 dwarf_csect
->last
= 0;
2207 } else { /* chain to previous entry */
2208 dwarf_esect
->next
= dwarf_csect
;
2209 dwarf_esect
= dwarf_csect
;
2213 #endif /* OF_ELFX32 */