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"
64 static struct elf_section
**sects
;
65 static int nsects
, sectlen
;
67 #define SHSTR_DELTA 256
68 static char *shstrtab
;
69 static int shstrtablen
, shstrtabsize
;
71 static struct SAA
*syms
;
72 static uint32_t nlocals
, nglobs
, ndebugs
; /* Symbol counts */
74 static int32_t def_seg
;
76 static struct RAA
*bsym
;
78 static struct SAA
*strs
;
79 static uint32_t strslen
;
81 static struct elf_symbol
*fwds
;
83 static char elf_module
[FILENAME_MAX
];
85 extern struct ofmt of_elfx32
;
87 static struct ELF_SECTDATA
{
92 static int elf_nsect
, nsections
;
93 static int32_t elf_foffs
;
95 static void elf_write(void);
96 static void elf_sect_write(struct elf_section
*, const void *, size_t);
97 static void elf_sect_writeaddr(struct elf_section
*, int32_t, size_t);
98 static void elf_section_header(int, int, uint32_t, void *, bool, uint32_t, int, int,
100 static void elf_write_sections(void);
101 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
102 static struct SAA
*elf_build_reltab(uint64_t *, struct elf_reloc
*);
103 static void add_sectname(char *, char *);
111 int section
; /* index into sects[] */
112 int segto
; /* internal section number */
113 char *name
; /* shallow-copied pointer of section name */
117 struct linelist
*next
;
118 struct linelist
*last
;
119 struct symlininfo info
;
130 struct sectlist
*next
;
131 struct sectlist
*last
;
134 /* common debug variables */
135 static int currentline
= 1;
136 static int debug_immcall
= 0;
138 /* stabs debug variables */
139 static struct linelist
*stabslines
= 0;
140 static int numlinestabs
= 0;
141 static char *stabs_filename
= 0;
142 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
143 static int stablen
, stabstrlen
, stabrellen
;
145 /* dwarf debug variables */
146 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
147 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
148 static int dwarf_numfiles
= 0, dwarf_nsections
;
149 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
150 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
151 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
152 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
153 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
154 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
157 static struct dfmt df_dwarf
;
158 static struct dfmt df_stabs
;
159 static struct elf_symbol
*lastsym
;
161 /* common debugging routines */
162 static void debugx32_typevalue(int32_t);
163 static void debugx32_deflabel(char *, int32_t, int64_t, int, char *);
164 static void debugx32_directive(const char *, const char *);
166 /* stabs debugging routines */
167 static void stabsx32_linenum(const char *filename
, int32_t linenumber
, int32_t);
168 static void stabsx32_output(int, void *);
169 static void stabsx32_generate(void);
170 static void stabsx32_cleanup(void);
172 /* dwarf debugging routines */
173 static void dwarfx32_init(void);
174 static void dwarfx32_linenum(const char *filename
, int32_t linenumber
, int32_t);
175 static void dwarfx32_output(int, void *);
176 static void dwarfx32_generate(void);
177 static void dwarfx32_cleanup(void);
178 static void dwarfx32_findfile(const char *);
179 static void dwarfx32_findsect(const int);
182 * Special section numbers which are used to define ELF special
183 * symbols, which can be used with WRT to provide PIC relocation
186 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
187 static int32_t elf_got_sect
, elf_plt_sect
;
188 static int32_t elf_sym_sect
;
189 static int32_t elf_gottpoff_sect
;
191 static void elf_init(void)
195 nsects
= sectlen
= 0;
196 syms
= saa_init((int32_t)sizeof(struct elf_symbol
));
197 nlocals
= nglobs
= ndebugs
= 0;
200 saa_wbytes(strs
, "\0", 1L);
201 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
202 strslen
= 2 + strlen(elf_module
);
204 shstrtablen
= shstrtabsize
= 0;;
205 add_sectname("", "");
209 elf_gotpc_sect
= seg_alloc();
210 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
211 elf_gotoff_sect
= seg_alloc();
212 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
213 elf_got_sect
= seg_alloc();
214 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
215 elf_plt_sect
= seg_alloc();
216 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
217 elf_sym_sect
= seg_alloc();
218 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
219 elf_gottpoff_sect
= seg_alloc();
220 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
222 def_seg
= seg_alloc();
226 static void elf_cleanup(int debuginfo
)
234 for (i
= 0; i
< nsects
; i
++) {
235 if (sects
[i
]->type
!= SHT_NOBITS
)
236 saa_free(sects
[i
]->data
);
238 saa_free(sects
[i
]->rel
);
239 while (sects
[i
]->head
) {
241 sects
[i
]->head
= sects
[i
]->head
->next
;
249 if (of_elfx32
.current_dfmt
) {
250 of_elfx32
.current_dfmt
->cleanup();
254 /* add entry to the elf .shstrtab section */
255 static void add_sectname(char *firsthalf
, char *secondhalf
)
257 int len
= strlen(firsthalf
) + strlen(secondhalf
);
258 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
259 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
260 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
261 strcat(shstrtab
+ shstrtablen
, secondhalf
);
262 shstrtablen
+= len
+ 1;
265 static int elf_make_section(char *name
, int type
, int flags
, int align
)
267 struct elf_section
*s
;
269 s
= nasm_zalloc(sizeof(*s
));
271 if (type
!= SHT_NOBITS
)
272 s
->data
= saa_init(1L);
274 if (!strcmp(name
, ".text"))
277 s
->index
= seg_alloc();
278 add_sectname("", name
);
280 s
->name
= nasm_strdup(name
);
285 if (nsects
>= sectlen
)
286 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
292 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
295 uint32_t flags
, flags_and
, flags_or
;
300 * Default is 64 bits.
307 p
= nasm_skip_word(name
);
310 flags_and
= flags_or
= type
= align
= 0;
312 elf_section_attrib(name
, p
, pass
, &flags_and
,
313 &flags_or
, &align
, &type
);
315 if (!strcmp(name
, ".shstrtab") ||
316 !strcmp(name
, ".symtab") ||
317 !strcmp(name
, ".strtab")) {
318 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
323 for (i
= 0; i
< nsects
; i
++)
324 if (!strcmp(name
, sects
[i
]->name
))
327 const struct elf_known_section
*ks
= elf_known_sections
;
330 if (!strcmp(name
, ks
->name
))
335 type
= type
? type
: ks
->type
;
336 align
= align
? align
: ks
->align
;
337 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
339 i
= elf_make_section(name
, type
, flags
, align
);
340 } else if (pass
== 1) {
341 if ((type
&& sects
[i
]->type
!= type
)
342 || (align
&& sects
[i
]->align
!= align
)
343 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
344 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
345 " redeclaration of section `%s'", name
);
348 return sects
[i
]->index
;
351 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
352 int is_global
, char *special
)
355 struct elf_symbol
*sym
;
356 bool special_used
= false;
358 #if defined(DEBUG) && DEBUG>2
359 nasm_error(ERR_DEBUG
,
360 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
361 name
, segment
, offset
, is_global
, special
);
363 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
365 * This is a NASM special symbol. We never allow it into
366 * the ELF symbol table, even if it's a valid one. If it
367 * _isn't_ a valid one, we should barf immediately.
369 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
370 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
371 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
372 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
376 if (is_global
== 3) {
377 struct elf_symbol
**s
;
379 * Fix up a forward-reference symbol size from the first
382 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
383 if (!strcmp((*s
)->name
, name
)) {
384 struct tokenval tokval
;
386 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
390 tokval
.t_type
= TOKEN_INVALID
;
391 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
394 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
395 " expression as symbol size");
397 (*s
)->size
= reloc_value(e
);
401 * Remove it from the list of unresolved sizes.
403 nasm_free((*s
)->name
);
407 return; /* it wasn't an important one */
410 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
411 strslen
+= 1 + strlen(name
);
413 lastsym
= sym
= saa_wstruct(syms
);
415 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
418 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
419 sym
->other
= STV_DEFAULT
;
421 if (segment
== NO_SEG
)
422 sym
->section
= SHN_ABS
;
425 sym
->section
= SHN_UNDEF
;
426 if (segment
== def_seg
) {
427 /* we have to be sure at least text section is there */
429 if (segment
!= elf_section_names(".text", 2, &tempint
))
430 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
432 for (i
= 0; i
< nsects
; i
++) {
433 if (segment
== sects
[i
]->index
) {
434 sym
->section
= i
+ 1;
440 if (is_global
== 2) {
443 sym
->section
= SHN_COMMON
;
445 * We have a common variable. Check the special text to see
446 * if it's a valid number and power of two; if so, store it
447 * as the alignment for the common variable.
451 sym
->symv
.key
= readnum(special
, &err
);
453 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
454 " valid number", special
);
455 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
456 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
457 " power of two", special
);
461 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
463 if (sym
->type
== SYM_GLOBAL
) {
465 * If sym->section == SHN_ABS, then the first line of the
466 * else section would cause a core dump, because its a reference
467 * beyond the end of the section array.
468 * This behaviour is exhibited by this code:
471 * To avoid such a crash, such requests are silently discarded.
472 * This may not be the best solution.
474 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
475 bsym
= raa_write(bsym
, segment
, nglobs
);
476 } else if (sym
->section
!= SHN_ABS
) {
478 * This is a global symbol; so we must add it to the rbtree
479 * of global symbols in its section.
481 * In addition, we check the special text for symbol
482 * type and size information.
484 sects
[sym
->section
-1]->gsyms
=
485 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
488 int n
= strcspn(special
, " \t");
490 if (!nasm_strnicmp(special
, "function", n
))
491 sym
->type
|= STT_FUNC
;
492 else if (!nasm_strnicmp(special
, "data", n
) ||
493 !nasm_strnicmp(special
, "object", n
))
494 sym
->type
|= STT_OBJECT
;
495 else if (!nasm_strnicmp(special
, "notype", n
))
496 sym
->type
|= STT_NOTYPE
;
498 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
502 special
= nasm_skip_spaces(special
);
504 n
= strcspn(special
, " \t");
505 if (!nasm_strnicmp(special
, "default", n
))
506 sym
->other
= STV_DEFAULT
;
507 else if (!nasm_strnicmp(special
, "internal", n
))
508 sym
->other
= STV_INTERNAL
;
509 else if (!nasm_strnicmp(special
, "hidden", n
))
510 sym
->other
= STV_HIDDEN
;
511 else if (!nasm_strnicmp(special
, "protected", n
))
512 sym
->other
= STV_PROTECTED
;
519 struct tokenval tokval
;
522 char *saveme
= stdscan_get();
524 while (special
[n
] && nasm_isspace(special
[n
]))
527 * We have a size expression; attempt to
531 stdscan_set(special
+ n
);
532 tokval
.t_type
= TOKEN_INVALID
;
533 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
538 sym
->name
= nasm_strdup(name
);
541 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
542 " expression as symbol size");
544 sym
->size
= reloc_value(e
);
551 * If TLS segment, mark symbol accordingly.
553 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
555 sym
->type
|= STT_TLS
;
558 sym
->globnum
= nglobs
;
563 if (special
&& !special_used
)
564 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
567 static void elf_add_reloc(struct elf_section
*sect
, int32_t segment
,
568 int32_t offset
, int type
)
572 r
= *sect
->tail
= nasm_zalloc(sizeof(struct elf_reloc
));
573 sect
->tail
= &r
->next
;
575 r
->address
= sect
->len
;
578 if (segment
!= NO_SEG
) {
580 for (i
= 0; i
< nsects
; i
++)
581 if (segment
== sects
[i
]->index
)
584 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
592 * This routine deals with ..got and ..sym relocations: the more
593 * complicated kinds. In shared-library writing, some relocations
594 * with respect to global symbols must refer to the precise symbol
595 * rather than referring to an offset from the base of the section
596 * _containing_ the symbol. Such relocations call to this routine,
597 * which searches the symbol list for the symbol in question.
599 * R_X86_64_GOT32 references require the _exact_ symbol address to be
600 * used; R_X86_64_32 references can be at an offset from the symbol.
601 * The boolean argument `exact' tells us this.
603 * Return value is the adjusted value of `addr', having become an
604 * offset from the symbol rather than the section. Should always be
605 * zero when returning from an exact call.
607 * Limitation: if you define two symbols at the same place,
608 * confusion will occur.
610 * Inefficiency: we search, currently, using a linked list which
611 * isn't even necessarily sorted.
613 static void elf_add_gsym_reloc(struct elf_section
*sect
,
614 int32_t segment
, uint32_t offset
, int32_t pcrel
,
615 int type
, bool exact
)
618 struct elf_section
*s
;
619 struct elf_symbol
*sym
;
624 * First look up the segment/offset pair and find a global
625 * symbol corresponding to it. If it's not one of our segments,
626 * then it must be an external symbol, in which case we're fine
627 * doing a normal elf_add_reloc after first sanity-checking
628 * that the offset from the symbol is zero.
631 for (i
= 0; i
< nsects
; i
++)
632 if (segment
== sects
[i
]->index
) {
639 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
641 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
645 srb
= rb_search(s
->gsyms
, offset
);
646 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
647 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
648 " for this reference");
651 sym
= container_of(srb
, struct elf_symbol
, symv
);
653 r
= *sect
->tail
= nasm_malloc(sizeof(struct elf_reloc
));
654 sect
->tail
= &r
->next
;
657 r
->address
= sect
->len
;
658 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
659 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
665 static void elf_out(int32_t segto
, const void *data
,
666 enum out_type type
, uint64_t size
,
667 int32_t segment
, int32_t wrt
)
669 struct elf_section
*s
;
673 static struct symlininfo sinfo
;
675 #if defined(DEBUG) && DEBUG>2
677 nasm_error(ERR_DEBUG
,
678 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
679 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
681 nasm_error(ERR_DEBUG
,
682 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
683 currentline
, type
, segment
, segto
, size
);
687 * handle absolute-assembly (structure definitions)
689 if (segto
== NO_SEG
) {
690 if (type
!= OUT_RESERVE
)
691 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
697 for (i
= 0; i
< nsects
; i
++)
698 if (segto
== sects
[i
]->index
) {
703 int tempint
; /* ignored */
704 if (segto
!= elf_section_names(".text", 2, &tempint
))
705 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
707 s
= sects
[nsects
- 1];
712 /* again some stabs debugging stuff */
713 if (of_elfx32
.current_dfmt
) {
714 sinfo
.offset
= s
->len
;
717 sinfo
.name
= s
->name
;
718 of_elfx32
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
720 /* end of debugging stuff */
722 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
723 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
724 " BSS section `%s': ignored", s
->name
);
725 s
->len
+= realsize(type
, size
);
731 if (s
->type
== SHT_PROGBITS
) {
732 nasm_error(ERR_WARNING
, "uninitialized space declared in"
733 " non-BSS section `%s': zeroing", s
->name
);
734 elf_sect_write(s
, NULL
, size
);
740 if (segment
!= NO_SEG
)
741 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
742 elf_sect_write(s
, data
, size
);
747 int isize
= (int)size
;
748 int asize
= abs(size
);
750 addr
= *(int64_t *)data
;
751 if (segment
== NO_SEG
) {
753 } else if (segment
% 2) {
754 nasm_error(ERR_NONFATAL
, "ELF format does not support"
755 " segment base references");
761 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
765 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
768 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
771 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
775 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
778 nasm_error(ERR_PANIC
, "internal error elfx32-hpa-871");
782 } else if (wrt
== elf_gotpc_sect
+ 1) {
784 * The user will supply GOT relative to $$. ELF
785 * will let us have GOT relative to $. So we
786 * need to fix up the data item by $-$$.
789 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
791 } else if (wrt
== elf_gotoff_sect
+ 1) {
792 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
793 "R_X86_64_GOTOFF64");
794 } else if (wrt
== elf_got_sect
+ 1) {
797 elf_add_gsym_reloc(s
, segment
, addr
, 0,
798 R_X86_64_GOT32
, true);
802 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
805 } else if (wrt
== elf_sym_sect
+ 1) {
809 elf_add_gsym_reloc(s
, segment
, addr
, 0,
815 elf_add_gsym_reloc(s
, segment
, addr
, 0,
820 elf_add_gsym_reloc(s
, segment
, addr
, 0,
825 elf_add_gsym_reloc(s
, segment
, addr
, 0,
826 R_X86_64_32S
, false);
831 elf_add_gsym_reloc(s
, segment
, addr
, 0,
836 nasm_error(ERR_PANIC
, "internal error elfx32-hpa-903");
839 } else if (wrt
== elf_plt_sect
+ 1) {
840 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
841 "relative PLT references");
843 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
847 elf_sect_writeaddr(s
, addr
, asize
);
852 reltype
= R_X86_64_PC8
;
857 reltype
= R_X86_64_PC16
;
862 addr
= *(int64_t *)data
- size
;
863 if (segment
== segto
)
864 nasm_error(ERR_PANIC
, "intra-segment OUT_REL1ADR");
865 if (segment
== NO_SEG
) {
867 } else if (segment
% 2) {
868 nasm_error(ERR_NONFATAL
, "ELF format does not support"
869 " segment base references");
872 elf_add_reloc(s
, segment
, addr
, reltype
);
875 nasm_error(ERR_NONFATAL
,
876 "Unsupported non-32-bit ELF relocation");
879 elf_sect_writeaddr(s
, addr
, bytes
);
883 addr
= *(int64_t *)data
- size
;
884 if (segment
== segto
)
885 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
886 if (segment
== NO_SEG
) {
888 } else if (segment
% 2) {
889 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
890 " segment base references");
893 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
895 } else if (wrt
== elf_plt_sect
+ 1) {
896 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
897 R_X86_64_PLT32
, true);
899 } else if (wrt
== elf_gotpc_sect
+ 1 ||
900 wrt
== elf_got_sect
+ 1) {
901 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
902 R_X86_64_GOTPCREL
, true);
904 } else if (wrt
== elf_gotoff_sect
+ 1 ||
905 wrt
== elf_got_sect
+ 1) {
906 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
907 } else if (wrt
== elf_gottpoff_sect
+ 1) {
908 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
909 R_X86_64_GOTTPOFF
, true);
912 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
916 elf_sect_writeaddr(s
, addr
, 4);
920 nasm_error(ERR_NONFATAL
,
921 "32-bit ELF format does not support 64-bit relocations");
923 elf_sect_writeaddr(s
, addr
, 8);
928 static void elf_write(void)
935 int32_t symtablen
, symtablocal
;
938 * Work out how many sections we will have. We have SHN_UNDEF,
939 * then the flexible user sections, then the fixed sections
940 * `.shstrtab', `.symtab' and `.strtab', then optionally
941 * relocation sections for the user sections.
943 nsections
= sec_numspecial
+ 1;
944 if (of_elfx32
.current_dfmt
== &df_stabs
)
946 else if (of_elfx32
.current_dfmt
== &df_dwarf
)
949 add_sectname("", ".shstrtab");
950 add_sectname("", ".symtab");
951 add_sectname("", ".strtab");
952 for (i
= 0; i
< nsects
; i
++) {
953 nsections
++; /* for the section itself */
954 if (sects
[i
]->head
) {
955 nsections
++; /* for its relocations */
956 add_sectname(".rela", sects
[i
]->name
);
960 if (of_elfx32
.current_dfmt
== &df_stabs
) {
961 /* in case the debug information is wanted, just add these three sections... */
962 add_sectname("", ".stab");
963 add_sectname("", ".stabstr");
964 add_sectname(".rel", ".stab");
967 else if (of_elfx32
.current_dfmt
== &df_dwarf
) {
968 /* the dwarf debug standard specifies the following ten sections,
969 not all of which are currently implemented,
970 although all of them are defined. */
971 add_sectname("", ".debug_aranges");
972 add_sectname(".rela", ".debug_aranges");
973 add_sectname("", ".debug_pubnames");
974 add_sectname("", ".debug_info");
975 add_sectname(".rela", ".debug_info");
976 add_sectname("", ".debug_abbrev");
977 add_sectname("", ".debug_line");
978 add_sectname(".rela", ".debug_line");
979 add_sectname("", ".debug_frame");
980 add_sectname("", ".debug_loc");
984 * Output the ELF header.
986 nasm_write("\177ELF\1\1\1", 7, ofile
);
987 fputc(elf_osabi
, ofile
);
988 fputc(elf_abiver
, ofile
);
989 fwritezero(7, ofile
);
990 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
991 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
992 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
993 fwriteint32_t(0L, ofile
); /* no entry point */
994 fwriteint32_t(0L, ofile
); /* no program header table */
995 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
996 * ELF header plus alignment */
997 fwriteint32_t(0L, ofile
); /* X86_64 defines no special flags */
998 fwriteint16_t(0x34, ofile
); /* size of ELF header */
999 fwriteint16_t(0, ofile
); /* no program header table, again */
1000 fwriteint16_t(0, ofile
); /* still no program header table */
1001 fwriteint16_t(sizeof(Elf32_Shdr
), ofile
); /* size of section header */
1002 fwriteint16_t(nsections
, ofile
); /* number of sections */
1003 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1004 * section header table */
1005 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
1006 fwriteint32_t(0L, ofile
);
1007 fwriteint32_t(0L, ofile
);
1010 * Build the symbol table and relocation tables.
1012 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1013 for (i
= 0; i
< nsects
; i
++)
1015 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1019 * Now output the section header table.
1022 elf_foffs
= 0x40 + sizeof(Elf32_Shdr
) * nsections
;
1023 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1026 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1029 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1032 /* The normal sections */
1033 for (i
= 0; i
< nsects
; i
++) {
1034 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1035 (sects
[i
]->type
== SHT_PROGBITS
?
1036 sects
[i
]->data
: NULL
), true,
1037 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1042 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1043 shstrtablen
, 0, 0, 1, 0);
1047 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1048 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1052 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1053 strslen
, 0, 0, 1, 0);
1056 /* The relocation sections */
1057 for (i
= 0; i
< nsects
; i
++)
1058 if (sects
[i
]->head
) {
1059 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1060 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 12);
1064 if (of_elfx32
.current_dfmt
== &df_stabs
) {
1065 /* for debugging information, create the last three sections
1066 which are the .stab , .stabstr and .rel.stab sections respectively */
1068 /* this function call creates the stab sections in memory */
1069 stabsx32_generate();
1071 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1072 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1073 stablen
, sec_stabstr
, 0, 4, 12);
1076 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1077 stabstrlen
, 0, 0, 4, 0);
1080 /* link -> symtable info -> section to refer to */
1081 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1082 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1085 } else if (of_elfx32
.current_dfmt
== &df_dwarf
) {
1086 /* for dwarf debugging information, create the ten dwarf sections */
1088 /* this function call creates the dwarf sections in memory */
1090 dwarfx32_generate();
1092 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1093 arangeslen
, 0, 0, 1, 0);
1096 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1097 arangesrellen
, sec_symtab
, sec_debug_aranges
, 1, 12);
1100 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1101 pubnameslen
, 0, 0, 1, 0);
1104 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1105 infolen
, 0, 0, 1, 0);
1108 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1109 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1112 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1113 abbrevlen
, 0, 0, 1, 0);
1116 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1117 linelen
, 0, 0, 1, 0);
1120 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1121 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1124 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1125 framelen
, 0, 0, 8, 0);
1128 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1129 loclen
, 0, 0, 1, 0);
1132 fwritezero(align
, ofile
);
1135 * Now output the sections.
1137 elf_write_sections();
1139 nasm_free(elf_sects
);
1143 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1145 struct SAA
*s
= saa_init(1L);
1146 struct elf_symbol
*sym
;
1147 uint8_t entry
[24], *p
;
1153 * First, an all-zeros entry, required by the ELF spec.
1155 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1160 * Next, an entry for the file name.
1163 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1164 WRITELONG(p
, 0); /* no value */
1165 WRITELONG(p
, 0); /* no size either */
1166 WRITESHORT(p
, STT_FILE
); /* type FILE */
1167 WRITESHORT(p
, SHN_ABS
);
1168 saa_wbytes(s
, entry
, 16L);
1173 * Now some standard symbols defining the segments, for relocation
1176 for (i
= 1; i
<= nsects
; i
++) {
1178 WRITELONG(p
, 0); /* no symbol name */
1179 WRITELONG(p
, 0); /* offset zero */
1180 WRITELONG(p
, 0); /* size zero */
1181 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1182 WRITESHORT(p
, i
); /* section id */
1183 saa_wbytes(s
, entry
, 16L);
1190 * Now the other local symbols.
1193 while ((sym
= saa_rstruct(syms
))) {
1194 if (sym
->type
& SYM_GLOBAL
)
1197 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1198 WRITELONG(p
, sym
->symv
.key
); /* value of symbol */
1199 WRITELONG(p
, sym
->size
); /* size of symbol */
1200 WRITECHAR(p
, sym
->type
); /* type and binding */
1201 WRITECHAR(p
, sym
->other
); /* visibility */
1202 WRITESHORT(p
, sym
->section
); /* index into section header table */
1203 saa_wbytes(s
, entry
, 16L);
1208 * dwarf needs symbols for debug sections
1209 * which are relocation targets.
1211 if (of_elfx32
.current_dfmt
== &df_dwarf
) {
1212 dwarf_infosym
= *local
;
1214 WRITELONG(p
, 0); /* no symbol name */
1215 WRITELONG(p
, 0); /* offset zero */
1216 WRITELONG(p
, 0); /* size zero */
1217 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1218 WRITESHORT(p
, sec_debug_info
); /* section id */
1219 saa_wbytes(s
, entry
, 16L);
1222 dwarf_abbrevsym
= *local
;
1224 WRITELONG(p
, 0); /* no symbol name */
1225 WRITELONG(p
, 0); /* offset zero */
1226 WRITELONG(p
, 0); /* size zero */
1227 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1228 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1229 saa_wbytes(s
, entry
, 16L);
1232 dwarf_linesym
= *local
;
1234 WRITELONG(p
, 0); /* no symbol name */
1235 WRITELONG(p
, 0); /* offset zero */
1236 WRITELONG(p
, 0); /* size zero */
1237 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1238 WRITESHORT(p
, sec_debug_line
); /* section id */
1239 saa_wbytes(s
, entry
, 16L);
1245 * Now the global symbols.
1248 while ((sym
= saa_rstruct(syms
))) {
1249 if (!(sym
->type
& SYM_GLOBAL
))
1252 WRITELONG(p
, sym
->strpos
);
1253 WRITELONG(p
, sym
->symv
.key
);
1254 WRITELONG(p
, sym
->size
);
1255 WRITECHAR(p
, sym
->type
); /* type and binding */
1256 WRITECHAR(p
, sym
->other
); /* visibility */
1257 WRITESHORT(p
, sym
->section
);
1258 saa_wbytes(s
, entry
, 16L);
1265 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
1268 uint8_t *p
, entry
[12];
1269 int32_t global_offset
;
1278 * How to onvert from a global placeholder to a real symbol index;
1279 * the +2 refers to the two special entries, the null entry and
1280 * the filename entry.
1282 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1285 int32_t sym
= r
->symbol
;
1287 if (sym
>= GLOBAL_TEMP_BASE
)
1288 sym
+= global_offset
;
1291 WRITELONG(p
, r
->address
);
1292 WRITELONG(p
, (sym
<< 8) + r
->type
);
1293 WRITELONG(p
, r
->offset
);
1294 saa_wbytes(s
, entry
, 12L);
1303 static void elf_section_header(int name
, int type
, uint32_t flags
,
1304 void *data
, bool is_saa
, uint32_t datalen
,
1305 int link
, int info
, int align
, int eltsize
)
1307 elf_sects
[elf_nsect
].data
= data
;
1308 elf_sects
[elf_nsect
].len
= datalen
;
1309 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1312 fwriteint32_t((int32_t)name
, ofile
);
1313 fwriteint32_t((int32_t)type
, ofile
);
1314 fwriteint32_t((int32_t)flags
, ofile
);
1315 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1316 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1317 fwriteint32_t(datalen
, ofile
);
1319 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1320 fwriteint32_t((int32_t)link
, ofile
);
1321 fwriteint32_t((int32_t)info
, ofile
);
1322 fwriteint32_t((int32_t)align
, ofile
);
1323 fwriteint32_t((int32_t)eltsize
, ofile
);
1326 static void elf_write_sections(void)
1329 for (i
= 0; i
< elf_nsect
; i
++)
1330 if (elf_sects
[i
].data
) {
1331 int32_t len
= elf_sects
[i
].len
;
1332 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1333 int32_t align
= reallen
- len
;
1334 if (elf_sects
[i
].is_saa
)
1335 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1337 nasm_write(elf_sects
[i
].data
, len
, ofile
);
1338 fwritezero(align
, ofile
);
1342 static void elf_sect_write(struct elf_section
*sect
, const void *data
, size_t len
)
1344 saa_wbytes(sect
->data
, data
, len
);
1347 static void elf_sect_writeaddr(struct elf_section
*sect
, int32_t data
, size_t len
)
1349 saa_writeaddr(sect
->data
, data
, len
);
1353 static void elf_sectalign(int32_t seg
, unsigned int value
)
1355 struct elf_section
*s
= NULL
;
1358 for (i
= 0; i
< nsects
; i
++) {
1359 if (sects
[i
]->index
== seg
) {
1364 if (!s
|| !is_power2(value
))
1367 if (value
> s
->align
)
1371 static int32_t elf_segbase(int32_t segment
)
1376 static void elf_filename(char *inname
, char *outname
)
1378 strcpy(elf_module
, inname
);
1379 standard_extension(inname
, outname
, ".o");
1382 extern macros_t elf_stdmac
[];
1384 static int elf_set_info(enum geninfo type
, char **val
)
1390 static struct dfmt df_dwarf
= {
1391 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1401 static struct dfmt df_stabs
= {
1402 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1413 struct dfmt
*elfx32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1415 struct ofmt of_elfx32
= {
1416 "ELFX32 (x86_64) object files (e.g. Linux)",
1434 /* common debugging routines */
1435 static void debugx32_deflabel(char *name
, int32_t segment
, int64_t offset
,
1436 int is_global
, char *special
)
1445 static void debugx32_directive(const char *directive
, const char *params
)
1451 static void debugx32_typevalue(int32_t type
)
1453 int32_t stype
, ssize
;
1454 switch (TYM_TYPE(type
)) {
1497 stype
= STT_SECTION
;
1512 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1513 lastsym
->size
= ssize
;
1514 lastsym
->type
= stype
;
1518 /* stabs debugging routines */
1520 static void stabsx32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1523 if (!stabs_filename
) {
1524 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1525 strcpy(stabs_filename
, filename
);
1527 if (strcmp(stabs_filename
, filename
)) {
1528 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1529 in fact, this leak comes in quite handy to maintain a list of files
1530 encountered so far in the symbol lines... */
1532 /* why not nasm_free(stabs_filename); we're done with the old one */
1534 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1535 strcpy(stabs_filename
, filename
);
1539 currentline
= linenumber
;
1543 static void stabsx32_output(int type
, void *param
)
1545 struct symlininfo
*s
;
1546 struct linelist
*el
;
1547 if (type
== TY_DEBUGSYMLIN
) {
1548 if (debug_immcall
) {
1549 s
= (struct symlininfo
*)param
;
1550 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1551 return; /* line info is only collected for executable sections */
1553 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1554 el
->info
.offset
= s
->offset
;
1555 el
->info
.section
= s
->section
;
1556 el
->info
.name
= s
->name
;
1557 el
->line
= currentline
;
1558 el
->filename
= stabs_filename
;
1561 stabslines
->last
->next
= el
;
1562 stabslines
->last
= el
;
1565 stabslines
->last
= el
;
1572 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1574 static void stabsx32_generate(void)
1576 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1577 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1581 struct linelist
*ptr
;
1585 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
1588 if (numfiles
== 0) {
1589 allfiles
[0] = ptr
->filename
;
1592 for (i
= 0; i
< numfiles
; i
++) {
1593 if (!strcmp(allfiles
[i
], ptr
->filename
))
1596 if (i
>= numfiles
) {
1597 allfiles
[i
] = ptr
->filename
;
1604 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1605 for (i
= 0; i
< numfiles
; i
++) {
1606 fileidx
[i
] = strsize
;
1607 strsize
+= strlen(allfiles
[i
]) + 1;
1610 for (i
= 0; i
< numfiles
; i
++) {
1611 if (!strcmp(allfiles
[i
], elf_module
)) {
1618 * worst case size of the stab buffer would be:
1619 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1620 * plus one "ending" entry
1622 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1623 sizeof(struct stabentry
));
1624 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1625 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1628 for (i
= 0; i
< numfiles
; i
++)
1629 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1632 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1640 * this is the first stab, its strx points to the filename of the
1641 * the source-file, the n_desc field should be set to the number
1642 * of remaining stabs
1644 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1646 /* this is the stab for the main source file */
1647 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1649 /* relocation table entry */
1652 * Since the symbol table has two entries before
1653 * the section symbols, the index in the info.section
1654 * member must be adjusted by adding 2
1657 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1658 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1661 currfile
= mainfileindex
;
1665 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1666 /* oops file has changed... */
1667 for (i
= 0; i
< numfiles
; i
++)
1668 if (!strcmp(allfiles
[i
], ptr
->filename
))
1671 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1675 /* relocation table entry */
1677 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1678 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1681 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1684 /* relocation table entry */
1686 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1687 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1693 /* this is an "ending" token */
1694 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1697 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1699 nasm_free(allfiles
);
1702 stablen
= (sptr
- sbuf
);
1703 stabrellen
= (rptr
- rbuf
);
1709 static void stabsx32_cleanup(void)
1711 struct linelist
*ptr
, *del
;
1723 nasm_free(stabrelbuf
);
1724 nasm_free(stabstrbuf
);
1727 /* dwarf routines */
1729 static void dwarfx32_init(void)
1731 ndebugs
= 3; /* 3 debug symbols */
1734 static void dwarfx32_linenum(const char *filename
, int32_t linenumber
,
1738 dwarfx32_findfile(filename
);
1740 currentline
= linenumber
;
1743 /* called from elf_out with type == TY_DEBUGSYMLIN */
1744 static void dwarfx32_output(int type
, void *param
)
1746 int ln
, aa
, inx
, maxln
, soc
;
1747 struct symlininfo
*s
;
1752 s
= (struct symlininfo
*)param
;
1754 /* line number info is only gathered for executable sections */
1755 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1758 /* Check if section index has changed */
1759 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1760 dwarfx32_findsect(s
->section
);
1762 /* do nothing unless line or file has changed */
1766 ln
= currentline
- dwarf_csect
->line
;
1767 aa
= s
->offset
- dwarf_csect
->offset
;
1768 inx
= dwarf_clist
->line
;
1769 plinep
= dwarf_csect
->psaa
;
1770 /* check for file change */
1771 if (!(inx
== dwarf_csect
->file
)) {
1772 saa_write8(plinep
,DW_LNS_set_file
);
1773 saa_write8(plinep
,inx
);
1774 dwarf_csect
->file
= inx
;
1776 /* check for line change */
1778 /* test if in range of special op code */
1779 maxln
= line_base
+ line_range
;
1780 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1781 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1782 saa_write8(plinep
,soc
);
1784 saa_write8(plinep
,DW_LNS_advance_line
);
1785 saa_wleb128s(plinep
,ln
);
1787 saa_write8(plinep
,DW_LNS_advance_pc
);
1788 saa_wleb128u(plinep
,aa
);
1791 dwarf_csect
->line
= currentline
;
1792 dwarf_csect
->offset
= s
->offset
;
1795 /* show change handled */
1800 static void dwarfx32_generate(void)
1804 struct linelist
*ftentry
;
1805 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1806 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1807 struct sectlist
*psect
;
1808 size_t saalen
, linepoff
, totlen
, highaddr
;
1810 /* write epilogues for each line program range */
1811 /* and build aranges section */
1812 paranges
= saa_init(1L);
1813 parangesrel
= saa_init(1L);
1814 saa_write16(paranges
,3); /* dwarf version */
1815 saa_write32(parangesrel
, paranges
->datalen
+4);
1816 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_X86_64_32
); /* reloc to info */
1817 saa_write32(parangesrel
, 0);
1818 saa_write32(paranges
,0); /* offset into info */
1819 saa_write8(paranges
,4); /* pointer size */
1820 saa_write8(paranges
,0); /* not segmented */
1821 saa_write32(paranges
,0); /* padding */
1822 /* iterate though sectlist entries */
1823 psect
= dwarf_fsect
;
1826 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1828 plinep
= psect
->psaa
;
1829 /* Line Number Program Epilogue */
1830 saa_write8(plinep
,2); /* std op 2 */
1831 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1832 saa_write8(plinep
,DW_LNS_extended_op
);
1833 saa_write8(plinep
,1); /* operand length */
1834 saa_write8(plinep
,DW_LNE_end_sequence
);
1835 totlen
+= plinep
->datalen
;
1836 /* range table relocation entry */
1837 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1838 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_X86_64_32
);
1839 saa_write32(parangesrel
, (uint32_t) 0);
1840 /* range table entry */
1841 saa_write32(paranges
,0x0000); /* range start */
1842 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1843 highaddr
+= sects
[psect
->section
]->len
;
1844 /* done with this entry */
1845 psect
= psect
->next
;
1847 saa_write32(paranges
,0); /* null address */
1848 saa_write32(paranges
,0); /* null length */
1849 saalen
= paranges
->datalen
;
1850 arangeslen
= saalen
+ 4;
1851 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1852 WRITELONG(pbuf
,saalen
); /* initial length */
1853 saa_rnbytes(paranges
, pbuf
, saalen
);
1856 /* build rela.aranges section */
1857 arangesrellen
= saalen
= parangesrel
->datalen
;
1858 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1859 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1860 saa_free(parangesrel
);
1862 /* build pubnames section */
1863 ppubnames
= saa_init(1L);
1864 saa_write16(ppubnames
,3); /* dwarf version */
1865 saa_write32(ppubnames
,0); /* offset into info */
1866 saa_write32(ppubnames
,0); /* space used in info */
1867 saa_write32(ppubnames
,0); /* end of list */
1868 saalen
= ppubnames
->datalen
;
1869 pubnameslen
= saalen
+ 4;
1870 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1871 WRITELONG(pbuf
,saalen
); /* initial length */
1872 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1873 saa_free(ppubnames
);
1875 /* build info section */
1876 pinfo
= saa_init(1L);
1877 pinforel
= saa_init(1L);
1878 saa_write16(pinfo
,3); /* dwarf version */
1879 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1880 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_X86_64_32
); /* reloc to abbrev */
1881 saa_write32(pinforel
, 0);
1882 saa_write32(pinfo
,0); /* offset into abbrev */
1883 saa_write8(pinfo
,4); /* pointer size */
1884 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1885 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1886 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1887 saa_write32(pinforel
, 0);
1888 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1889 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1890 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1891 saa_write32(pinforel
, 0);
1892 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1893 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1894 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_X86_64_32
); /* reloc to line */
1895 saa_write32(pinforel
, 0);
1896 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1897 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1898 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1899 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1900 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1901 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1902 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1903 saa_write32(pinforel
, 0);
1904 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1905 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1906 saa_write8(pinfo
,0); /* end of entries */
1907 saalen
= pinfo
->datalen
;
1908 infolen
= saalen
+ 4;
1909 infobuf
= pbuf
= nasm_malloc(infolen
);
1910 WRITELONG(pbuf
,saalen
); /* initial length */
1911 saa_rnbytes(pinfo
, pbuf
, saalen
);
1914 /* build rela.info section */
1915 inforellen
= saalen
= pinforel
->datalen
;
1916 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1917 saa_rnbytes(pinforel
, pbuf
, saalen
);
1920 /* build abbrev section */
1921 pabbrev
= saa_init(1L);
1922 saa_write8(pabbrev
,1); /* entry number LEB128u */
1923 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1924 saa_write8(pabbrev
,1); /* has children */
1925 /* the following attributes and forms are all LEB128u values */
1926 saa_write8(pabbrev
,DW_AT_low_pc
);
1927 saa_write8(pabbrev
,DW_FORM_addr
);
1928 saa_write8(pabbrev
,DW_AT_high_pc
);
1929 saa_write8(pabbrev
,DW_FORM_addr
);
1930 saa_write8(pabbrev
,DW_AT_stmt_list
);
1931 saa_write8(pabbrev
,DW_FORM_data4
);
1932 saa_write8(pabbrev
,DW_AT_name
);
1933 saa_write8(pabbrev
,DW_FORM_string
);
1934 saa_write8(pabbrev
,DW_AT_producer
);
1935 saa_write8(pabbrev
,DW_FORM_string
);
1936 saa_write8(pabbrev
,DW_AT_language
);
1937 saa_write8(pabbrev
,DW_FORM_data2
);
1938 saa_write16(pabbrev
,0); /* end of entry */
1939 /* LEB128u usage same as above */
1940 saa_write8(pabbrev
,2); /* entry number */
1941 saa_write8(pabbrev
,DW_TAG_subprogram
);
1942 saa_write8(pabbrev
,0); /* no children */
1943 saa_write8(pabbrev
,DW_AT_low_pc
);
1944 saa_write8(pabbrev
,DW_FORM_addr
);
1945 saa_write8(pabbrev
,DW_AT_frame_base
);
1946 saa_write8(pabbrev
,DW_FORM_data4
);
1947 saa_write16(pabbrev
,0); /* end of entry */
1948 abbrevlen
= saalen
= pabbrev
->datalen
;
1949 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
1950 saa_rnbytes(pabbrev
, pbuf
, saalen
);
1953 /* build line section */
1955 plines
= saa_init(1L);
1956 saa_write8(plines
,1); /* Minimum Instruction Length */
1957 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
1958 saa_write8(plines
,line_base
); /* Line Base */
1959 saa_write8(plines
,line_range
); /* Line Range */
1960 saa_write8(plines
,opcode_base
); /* Opcode Base */
1961 /* standard opcode lengths (# of LEB128u operands) */
1962 saa_write8(plines
,0); /* Std opcode 1 length */
1963 saa_write8(plines
,1); /* Std opcode 2 length */
1964 saa_write8(plines
,1); /* Std opcode 3 length */
1965 saa_write8(plines
,1); /* Std opcode 4 length */
1966 saa_write8(plines
,1); /* Std opcode 5 length */
1967 saa_write8(plines
,0); /* Std opcode 6 length */
1968 saa_write8(plines
,0); /* Std opcode 7 length */
1969 saa_write8(plines
,0); /* Std opcode 8 length */
1970 saa_write8(plines
,1); /* Std opcode 9 length */
1971 saa_write8(plines
,0); /* Std opcode 10 length */
1972 saa_write8(plines
,0); /* Std opcode 11 length */
1973 saa_write8(plines
,1); /* Std opcode 12 length */
1974 /* Directory Table */
1975 saa_write8(plines
,0); /* End of table */
1976 /* File Name Table */
1977 ftentry
= dwarf_flist
;
1978 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
1980 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
1981 saa_write8(plines
,0); /* directory LEB128u */
1982 saa_write8(plines
,0); /* time LEB128u */
1983 saa_write8(plines
,0); /* size LEB128u */
1984 ftentry
= ftentry
->next
;
1986 saa_write8(plines
,0); /* End of table */
1987 linepoff
= plines
->datalen
;
1988 linelen
= linepoff
+ totlen
+ 10;
1989 linebuf
= pbuf
= nasm_malloc(linelen
);
1990 WRITELONG(pbuf
,linelen
-4); /* initial length */
1991 WRITESHORT(pbuf
,3); /* dwarf version */
1992 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
1993 /* write line header */
1995 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
1998 /* concatonate line program ranges */
2000 plinesrel
= saa_init(1L);
2001 psect
= dwarf_fsect
;
2002 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2003 saa_write32(plinesrel
, linepoff
);
2004 saa_write32(plinesrel
, ((psect
->section
+ 2) << 8) + R_X86_64_32
);
2005 saa_write32(plinesrel
, 0);
2006 plinep
= psect
->psaa
;
2007 saalen
= plinep
->datalen
;
2008 saa_rnbytes(plinep
, pbuf
, saalen
);
2012 /* done with this entry */
2013 psect
= psect
->next
;
2017 /* build rela.lines section */
2018 linerellen
=saalen
= plinesrel
->datalen
;
2019 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2020 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2021 saa_free(plinesrel
);
2023 /* build frame section */
2025 framebuf
= pbuf
= nasm_malloc(framelen
);
2026 WRITELONG(pbuf
,framelen
-4); /* initial length */
2028 /* build loc section */
2030 locbuf
= pbuf
= nasm_malloc(loclen
);
2031 WRITELONG(pbuf
,0); /* null beginning offset */
2032 WRITELONG(pbuf
,0); /* null ending offset */
2035 static void dwarfx32_cleanup(void)
2037 nasm_free(arangesbuf
);
2038 nasm_free(arangesrelbuf
);
2039 nasm_free(pubnamesbuf
);
2041 nasm_free(inforelbuf
);
2042 nasm_free(abbrevbuf
);
2044 nasm_free(linerelbuf
);
2045 nasm_free(framebuf
);
2049 static void dwarfx32_findfile(const char * fname
)
2052 struct linelist
*match
;
2054 /* return if fname is current file name */
2055 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2058 /* search for match */
2061 match
= dwarf_flist
;
2062 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2063 if (!(strcmp(fname
, match
->filename
))) {
2064 dwarf_clist
= match
;
2070 /* add file name to end of list */
2071 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2073 dwarf_clist
->line
= dwarf_numfiles
;
2074 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2075 strcpy(dwarf_clist
->filename
,fname
);
2076 dwarf_clist
->next
= 0;
2077 if (!dwarf_flist
) { /* if first entry */
2078 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2079 dwarf_clist
->last
= 0;
2080 } else { /* chain to previous entry */
2081 dwarf_elist
->next
= dwarf_clist
;
2082 dwarf_elist
= dwarf_clist
;
2086 static void dwarfx32_findsect(const int index
)
2089 struct sectlist
*match
;
2092 /* return if index is current section index */
2093 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2096 /* search for match */
2099 match
= dwarf_fsect
;
2100 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2101 if (match
->section
== index
) {
2102 dwarf_csect
= match
;
2105 match
= match
->next
;
2109 /* add entry to end of list */
2110 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2112 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2113 dwarf_csect
->line
= 1;
2114 dwarf_csect
->offset
= 0;
2115 dwarf_csect
->file
= 1;
2116 dwarf_csect
->section
= index
;
2117 dwarf_csect
->next
= 0;
2118 /* set relocatable address at start of line program */
2119 saa_write8(plinep
,DW_LNS_extended_op
);
2120 saa_write8(plinep
,5); /* operand length */
2121 saa_write8(plinep
,DW_LNE_set_address
);
2122 saa_write32(plinep
,0); /* Start Address */
2124 if (!dwarf_fsect
) { /* if first entry */
2125 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2126 dwarf_csect
->last
= 0;
2127 } else { /* chain to previous entry */
2128 dwarf_esect
->next
= dwarf_csect
;
2129 dwarf_esect
= dwarf_csect
;
2133 #endif /* OF_ELFX32 */