1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * 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);
164 /* stabs debugging routines */
165 static void stabsx32_linenum(const char *filename
, int32_t linenumber
, int32_t);
166 static void stabsx32_output(int, void *);
167 static void stabsx32_generate(void);
168 static void stabsx32_cleanup(void);
170 /* dwarf debugging routines */
171 static void dwarfx32_init(void);
172 static void dwarfx32_linenum(const char *filename
, int32_t linenumber
, int32_t);
173 static void dwarfx32_output(int, void *);
174 static void dwarfx32_generate(void);
175 static void dwarfx32_cleanup(void);
176 static void dwarfx32_findfile(const char *);
177 static void dwarfx32_findsect(const int);
180 * Special section numbers which are used to define ELF special
181 * symbols, which can be used with WRT to provide PIC relocation
184 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
185 static int32_t elf_got_sect
, elf_plt_sect
;
186 static int32_t elf_sym_sect
;
187 static int32_t elf_gottpoff_sect
;
189 static void elf_init(void)
192 nsects
= sectlen
= 0;
193 syms
= saa_init((int32_t)sizeof(struct elf_symbol
));
194 nlocals
= nglobs
= ndebugs
= 0;
197 saa_wbytes(strs
, "\0", 1L);
198 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
199 strslen
= 2 + strlen(elf_module
);
201 shstrtablen
= shstrtabsize
= 0;;
202 add_sectname("", "");
206 elf_gotpc_sect
= seg_alloc();
207 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
208 elf_gotoff_sect
= seg_alloc();
209 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
210 elf_got_sect
= seg_alloc();
211 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
212 elf_plt_sect
= seg_alloc();
213 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
214 elf_sym_sect
= seg_alloc();
215 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
216 elf_gottpoff_sect
= seg_alloc();
217 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
219 def_seg
= seg_alloc();
223 static void elf_cleanup(void)
229 for (i
= 0; i
< nsects
; i
++) {
230 if (sects
[i
]->type
!= SHT_NOBITS
)
231 saa_free(sects
[i
]->data
);
233 saa_free(sects
[i
]->rel
);
234 while (sects
[i
]->head
) {
236 sects
[i
]->head
= sects
[i
]->head
->next
;
247 /* add entry to the elf .shstrtab section */
248 static void add_sectname(char *firsthalf
, char *secondhalf
)
250 int len
= strlen(firsthalf
) + strlen(secondhalf
);
251 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
252 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
253 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
254 strcat(shstrtab
+ shstrtablen
, secondhalf
);
255 shstrtablen
+= len
+ 1;
258 static int elf_make_section(char *name
, int type
, int flags
, int align
)
260 struct elf_section
*s
;
262 s
= nasm_zalloc(sizeof(*s
));
264 if (type
!= SHT_NOBITS
)
265 s
->data
= saa_init(1L);
267 if (!strcmp(name
, ".text"))
270 s
->index
= seg_alloc();
271 add_sectname("", name
);
273 s
->name
= nasm_strdup(name
);
278 if (nsects
>= sectlen
)
279 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
285 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
288 uint32_t flags
, flags_and
, flags_or
;
293 * Default is 64 bits.
300 p
= nasm_skip_word(name
);
303 flags_and
= flags_or
= type
= align
= 0;
305 elf_section_attrib(name
, p
, pass
, &flags_and
,
306 &flags_or
, &align
, &type
);
308 if (!strcmp(name
, ".shstrtab") ||
309 !strcmp(name
, ".symtab") ||
310 !strcmp(name
, ".strtab")) {
311 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
316 for (i
= 0; i
< nsects
; i
++)
317 if (!strcmp(name
, sects
[i
]->name
))
320 const struct elf_known_section
*ks
= elf_known_sections
;
323 if (!strcmp(name
, ks
->name
))
328 type
= type
? type
: ks
->type
;
329 align
= align
? align
: ks
->align
;
330 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
332 i
= elf_make_section(name
, type
, flags
, align
);
333 } else if (pass
== 1) {
334 if ((type
&& sects
[i
]->type
!= type
)
335 || (align
&& sects
[i
]->align
!= align
)
336 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
337 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
338 " redeclaration of section `%s'", name
);
341 return sects
[i
]->index
;
344 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
345 int is_global
, char *special
)
348 struct elf_symbol
*sym
;
349 bool special_used
= false;
351 #if defined(DEBUG) && DEBUG>2
352 nasm_error(ERR_DEBUG
,
353 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
354 name
, segment
, offset
, is_global
, special
);
356 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
358 * This is a NASM special symbol. We never allow it into
359 * the ELF symbol table, even if it's a valid one. If it
360 * _isn't_ a valid one, we should barf immediately.
362 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
363 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
364 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
365 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
369 if (is_global
== 3) {
370 struct elf_symbol
**s
;
372 * Fix up a forward-reference symbol size from the first
375 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
376 if (!strcmp((*s
)->name
, name
)) {
377 struct tokenval tokval
;
379 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
383 tokval
.t_type
= TOKEN_INVALID
;
384 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, NULL
);
387 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
388 " expression as symbol size");
390 (*s
)->size
= reloc_value(e
);
394 * Remove it from the list of unresolved sizes.
396 nasm_free((*s
)->name
);
400 return; /* it wasn't an important one */
403 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
404 strslen
+= 1 + strlen(name
);
406 lastsym
= sym
= saa_wstruct(syms
);
408 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
411 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
412 sym
->other
= STV_DEFAULT
;
414 if (segment
== NO_SEG
)
415 sym
->section
= SHN_ABS
;
418 sym
->section
= SHN_UNDEF
;
419 if (segment
== def_seg
) {
420 /* we have to be sure at least text section is there */
422 if (segment
!= elf_section_names(".text", 2, &tempint
))
423 nasm_panic(0, "strange segment conditions in ELF driver");
425 for (i
= 0; i
< nsects
; i
++) {
426 if (segment
== sects
[i
]->index
) {
427 sym
->section
= i
+ 1;
433 if (is_global
== 2) {
436 sym
->section
= SHN_COMMON
;
438 * We have a common variable. Check the special text to see
439 * if it's a valid number and power of two; if so, store it
440 * as the alignment for the common variable.
444 sym
->symv
.key
= readnum(special
, &err
);
446 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
447 " valid number", special
);
448 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
449 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
450 " power of two", special
);
454 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
456 if (sym
->type
== SYM_GLOBAL
) {
458 * If sym->section == SHN_ABS, then the first line of the
459 * else section would cause a core dump, because its a reference
460 * beyond the end of the section array.
461 * This behaviour is exhibited by this code:
464 * To avoid such a crash, such requests are silently discarded.
465 * This may not be the best solution.
467 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
468 bsym
= raa_write(bsym
, segment
, nglobs
);
469 } else if (sym
->section
!= SHN_ABS
) {
471 * This is a global symbol; so we must add it to the rbtree
472 * of global symbols in its section.
474 * In addition, we check the special text for symbol
475 * type and size information.
477 sects
[sym
->section
-1]->gsyms
=
478 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
481 int n
= strcspn(special
, " \t");
483 if (!nasm_strnicmp(special
, "function", n
))
484 sym
->type
|= STT_FUNC
;
485 else if (!nasm_strnicmp(special
, "data", n
) ||
486 !nasm_strnicmp(special
, "object", n
))
487 sym
->type
|= STT_OBJECT
;
488 else if (!nasm_strnicmp(special
, "notype", n
))
489 sym
->type
|= STT_NOTYPE
;
491 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
495 special
= nasm_skip_spaces(special
);
497 n
= strcspn(special
, " \t");
498 if (!nasm_strnicmp(special
, "default", n
))
499 sym
->other
= STV_DEFAULT
;
500 else if (!nasm_strnicmp(special
, "internal", n
))
501 sym
->other
= STV_INTERNAL
;
502 else if (!nasm_strnicmp(special
, "hidden", n
))
503 sym
->other
= STV_HIDDEN
;
504 else if (!nasm_strnicmp(special
, "protected", n
))
505 sym
->other
= STV_PROTECTED
;
512 struct tokenval tokval
;
515 char *saveme
= stdscan_get();
517 while (special
[n
] && nasm_isspace(special
[n
]))
520 * We have a size expression; attempt to
524 stdscan_set(special
+ n
);
525 tokval
.t_type
= TOKEN_INVALID
;
526 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, NULL
);
530 sym
->name
= nasm_strdup(name
);
533 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
534 " expression as symbol size");
536 sym
->size
= reloc_value(e
);
543 * If TLS segment, mark symbol accordingly.
545 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
547 sym
->type
|= STT_TLS
;
550 sym
->globnum
= nglobs
;
555 if (special
&& !special_used
)
556 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
559 static void elf_add_reloc(struct elf_section
*sect
, int32_t segment
,
560 int32_t offset
, int type
)
564 r
= *sect
->tail
= nasm_zalloc(sizeof(struct elf_reloc
));
565 sect
->tail
= &r
->next
;
567 r
->address
= sect
->len
;
570 if (segment
!= NO_SEG
) {
572 for (i
= 0; i
< nsects
; i
++)
573 if (segment
== sects
[i
]->index
)
576 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
584 * This routine deals with ..got and ..sym relocations: the more
585 * complicated kinds. In shared-library writing, some relocations
586 * with respect to global symbols must refer to the precise symbol
587 * rather than referring to an offset from the base of the section
588 * _containing_ the symbol. Such relocations call to this routine,
589 * which searches the symbol list for the symbol in question.
591 * R_X86_64_GOT32 references require the _exact_ symbol address to be
592 * used; R_X86_64_32 references can be at an offset from the symbol.
593 * The boolean argument `exact' tells us this.
595 * Return value is the adjusted value of `addr', having become an
596 * offset from the symbol rather than the section. Should always be
597 * zero when returning from an exact call.
599 * Limitation: if you define two symbols at the same place,
600 * confusion will occur.
602 * Inefficiency: we search, currently, using a linked list which
603 * isn't even necessarily sorted.
605 static void elf_add_gsym_reloc(struct elf_section
*sect
,
606 int32_t segment
, uint32_t offset
, int32_t pcrel
,
607 int type
, bool exact
)
610 struct elf_section
*s
;
611 struct elf_symbol
*sym
;
616 * First look up the segment/offset pair and find a global
617 * symbol corresponding to it. If it's not one of our segments,
618 * then it must be an external symbol, in which case we're fine
619 * doing a normal elf_add_reloc after first sanity-checking
620 * that the offset from the symbol is zero.
623 for (i
= 0; i
< nsects
; i
++)
624 if (segment
== sects
[i
]->index
) {
631 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
633 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
637 srb
= rb_search(s
->gsyms
, offset
);
638 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
639 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
640 " for this reference");
643 sym
= container_of(srb
, struct elf_symbol
, symv
);
645 r
= *sect
->tail
= nasm_malloc(sizeof(struct elf_reloc
));
646 sect
->tail
= &r
->next
;
649 r
->address
= sect
->len
;
650 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
651 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
657 static void elf_out(int32_t segto
, const void *data
,
658 enum out_type type
, uint64_t size
,
659 int32_t segment
, int32_t wrt
)
661 struct elf_section
*s
;
665 static struct symlininfo sinfo
;
667 #if defined(DEBUG) && DEBUG>2
669 nasm_error(ERR_DEBUG
,
670 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
671 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
673 nasm_error(ERR_DEBUG
,
674 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
675 currentline
, type
, segment
, segto
, size
);
679 * handle absolute-assembly (structure definitions)
681 if (segto
== NO_SEG
) {
682 if (type
!= OUT_RESERVE
)
683 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
689 for (i
= 0; i
< nsects
; i
++)
690 if (segto
== sects
[i
]->index
) {
695 int tempint
; /* ignored */
696 if (segto
!= elf_section_names(".text", 2, &tempint
))
697 nasm_panic(0, "strange segment conditions in ELF driver");
699 s
= sects
[nsects
- 1];
704 /* again some stabs debugging stuff */
705 sinfo
.offset
= s
->len
;
708 sinfo
.name
= s
->name
;
709 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
710 /* end of debugging stuff */
712 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
713 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
714 " BSS section `%s': ignored", s
->name
);
715 s
->len
+= realsize(type
, size
);
721 if (s
->type
== SHT_PROGBITS
) {
722 nasm_error(ERR_WARNING
, "uninitialized space declared in"
723 " non-BSS section `%s': zeroing", s
->name
);
724 elf_sect_write(s
, NULL
, size
);
730 if (segment
!= NO_SEG
)
731 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
732 elf_sect_write(s
, data
, size
);
737 int isize
= (int)size
;
738 int asize
= abs((int)size
);
740 addr
= *(int64_t *)data
;
741 if (segment
== NO_SEG
) {
743 } else if (segment
% 2) {
744 nasm_error(ERR_NONFATAL
, "ELF format does not support"
745 " segment base references");
751 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
755 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
758 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
761 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
765 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
768 nasm_panic(0, "internal error elfx32-hpa-871");
772 } else if (wrt
== elf_gotpc_sect
+ 1) {
774 * The user will supply GOT relative to $$. ELF
775 * will let us have GOT relative to $. So we
776 * need to fix up the data item by $-$$.
779 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
781 } else if (wrt
== elf_gotoff_sect
+ 1) {
782 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
783 "R_X86_64_GOTOFF64");
784 } else if (wrt
== elf_got_sect
+ 1) {
787 elf_add_gsym_reloc(s
, segment
, addr
, 0,
788 R_X86_64_GOT32
, true);
792 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
795 } else if (wrt
== elf_sym_sect
+ 1) {
799 elf_add_gsym_reloc(s
, segment
, addr
, 0,
805 elf_add_gsym_reloc(s
, segment
, addr
, 0,
810 elf_add_gsym_reloc(s
, segment
, addr
, 0,
815 elf_add_gsym_reloc(s
, segment
, addr
, 0,
816 R_X86_64_32S
, false);
821 elf_add_gsym_reloc(s
, segment
, addr
, 0,
826 nasm_panic(0, "internal error elfx32-hpa-903");
829 } else if (wrt
== elf_plt_sect
+ 1) {
830 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
831 "relative PLT references");
833 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
837 elf_sect_writeaddr(s
, addr
, asize
);
842 reltype
= R_X86_64_PC8
;
847 reltype
= R_X86_64_PC16
;
852 addr
= *(int64_t *)data
- size
;
853 if (segment
== segto
)
854 nasm_panic(0, "intra-segment OUT_REL1ADR");
855 if (segment
== NO_SEG
) {
857 } else if (segment
% 2) {
858 nasm_error(ERR_NONFATAL
, "ELF format does not support"
859 " segment base references");
862 elf_add_reloc(s
, segment
, addr
, reltype
);
865 nasm_error(ERR_NONFATAL
,
866 "Unsupported non-32-bit ELF relocation");
869 elf_sect_writeaddr(s
, addr
, bytes
);
873 addr
= *(int64_t *)data
- size
;
874 if (segment
== segto
)
875 nasm_panic(0, "intra-segment OUT_REL4ADR");
876 if (segment
== NO_SEG
) {
878 } else if (segment
% 2) {
879 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
880 " segment base references");
883 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
885 } else if (wrt
== elf_plt_sect
+ 1) {
886 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
887 R_X86_64_PLT32
, true);
889 } else if (wrt
== elf_gotpc_sect
+ 1 ||
890 wrt
== elf_got_sect
+ 1) {
891 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
892 R_X86_64_GOTPCREL
, true);
894 } else if (wrt
== elf_gotoff_sect
+ 1 ||
895 wrt
== elf_got_sect
+ 1) {
896 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
897 } else if (wrt
== elf_gottpoff_sect
+ 1) {
898 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
899 R_X86_64_GOTTPOFF
, true);
902 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
906 elf_sect_writeaddr(s
, addr
, 4);
910 nasm_error(ERR_NONFATAL
,
911 "32-bit ELF format does not support 64-bit relocations");
913 elf_sect_writeaddr(s
, addr
, 8);
918 static void elf_write(void)
925 int32_t symtablen
, symtablocal
;
928 * Work out how many sections we will have. We have SHN_UNDEF,
929 * then the flexible user sections, then the fixed sections
930 * `.shstrtab', `.symtab' and `.strtab', then optionally
931 * relocation sections for the user sections.
933 nsections
= sec_numspecial
+ 1;
934 if (dfmt
== &df_stabs
)
936 else if (dfmt
== &df_dwarf
)
939 add_sectname("", ".shstrtab");
940 add_sectname("", ".symtab");
941 add_sectname("", ".strtab");
942 for (i
= 0; i
< nsects
; i
++) {
943 nsections
++; /* for the section itself */
944 if (sects
[i
]->head
) {
945 nsections
++; /* for its relocations */
946 add_sectname(".rela", sects
[i
]->name
);
950 if (dfmt
== &df_stabs
) {
951 /* in case the debug information is wanted, just add these three sections... */
952 add_sectname("", ".stab");
953 add_sectname("", ".stabstr");
954 add_sectname(".rel", ".stab");
957 else if (dfmt
== &df_dwarf
) {
958 /* the dwarf debug standard specifies the following ten sections,
959 not all of which are currently implemented,
960 although all of them are defined. */
961 add_sectname("", ".debug_aranges");
962 add_sectname(".rela", ".debug_aranges");
963 add_sectname("", ".debug_pubnames");
964 add_sectname("", ".debug_info");
965 add_sectname(".rela", ".debug_info");
966 add_sectname("", ".debug_abbrev");
967 add_sectname("", ".debug_line");
968 add_sectname(".rela", ".debug_line");
969 add_sectname("", ".debug_frame");
970 add_sectname("", ".debug_loc");
974 * Output the ELF header.
976 nasm_write("\177ELF\1\1\1", 7, ofile
);
977 fputc(elf_osabi
, ofile
);
978 fputc(elf_abiver
, ofile
);
979 fwritezero(7, ofile
);
980 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
981 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
982 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
983 fwriteint32_t(0L, ofile
); /* no entry point */
984 fwriteint32_t(0L, ofile
); /* no program header table */
985 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
986 * ELF header plus alignment */
987 fwriteint32_t(0L, ofile
); /* X86_64 defines no special flags */
988 fwriteint16_t(0x34, ofile
); /* size of ELF header */
989 fwriteint16_t(0, ofile
); /* no program header table, again */
990 fwriteint16_t(0, ofile
); /* still no program header table */
991 fwriteint16_t(sizeof(Elf32_Shdr
), ofile
); /* size of section header */
992 fwriteint16_t(nsections
, ofile
); /* number of sections */
993 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
994 * section header table */
995 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
996 fwriteint32_t(0L, ofile
);
997 fwriteint32_t(0L, ofile
);
1000 * Build the symbol table and relocation tables.
1002 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1003 for (i
= 0; i
< nsects
; i
++)
1005 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1009 * Now output the section header table.
1012 elf_foffs
= 0x40 + sizeof(Elf32_Shdr
) * nsections
;
1013 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1016 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1019 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1022 /* The normal sections */
1023 for (i
= 0; i
< nsects
; i
++) {
1024 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1025 (sects
[i
]->type
== SHT_PROGBITS
?
1026 sects
[i
]->data
: NULL
), true,
1027 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1032 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1033 shstrtablen
, 0, 0, 1, 0);
1037 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1038 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1042 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1043 strslen
, 0, 0, 1, 0);
1046 /* The relocation sections */
1047 for (i
= 0; i
< nsects
; i
++)
1048 if (sects
[i
]->head
) {
1049 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1050 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 12);
1054 if (dfmt
== &df_stabs
) {
1055 /* for debugging information, create the last three sections
1056 which are the .stab , .stabstr and .rel.stab sections respectively */
1058 /* this function call creates the stab sections in memory */
1059 stabsx32_generate();
1061 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1062 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1063 stablen
, sec_stabstr
, 0, 4, 12);
1066 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1067 stabstrlen
, 0, 0, 4, 0);
1070 /* link -> symtable info -> section to refer to */
1071 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1072 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1075 } else if (dfmt
== &df_dwarf
) {
1076 /* for dwarf debugging information, create the ten dwarf sections */
1078 /* this function call creates the dwarf sections in memory */
1080 dwarfx32_generate();
1082 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1083 arangeslen
, 0, 0, 1, 0);
1086 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1087 arangesrellen
, sec_symtab
, sec_debug_aranges
, 1, 12);
1090 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1091 pubnameslen
, 0, 0, 1, 0);
1094 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1095 infolen
, 0, 0, 1, 0);
1098 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1099 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1102 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1103 abbrevlen
, 0, 0, 1, 0);
1106 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1107 linelen
, 0, 0, 1, 0);
1110 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1111 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1114 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1115 framelen
, 0, 0, 8, 0);
1118 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1119 loclen
, 0, 0, 1, 0);
1122 fwritezero(align
, ofile
);
1125 * Now output the sections.
1127 elf_write_sections();
1129 nasm_free(elf_sects
);
1133 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1135 struct SAA
*s
= saa_init(1L);
1136 struct elf_symbol
*sym
;
1137 uint8_t entry
[24], *p
;
1143 * First, an all-zeros entry, required by the ELF spec.
1145 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1150 * Next, an entry for the file name.
1153 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1154 WRITELONG(p
, 0); /* no value */
1155 WRITELONG(p
, 0); /* no size either */
1156 WRITESHORT(p
, STT_FILE
); /* type FILE */
1157 WRITESHORT(p
, SHN_ABS
);
1158 saa_wbytes(s
, entry
, 16L);
1163 * Now some standard symbols defining the segments, for relocation
1166 for (i
= 1; i
<= nsects
; i
++) {
1168 WRITELONG(p
, 0); /* no symbol name */
1169 WRITELONG(p
, 0); /* offset zero */
1170 WRITELONG(p
, 0); /* size zero */
1171 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1172 WRITESHORT(p
, i
); /* section id */
1173 saa_wbytes(s
, entry
, 16L);
1180 * Now the other local symbols.
1183 while ((sym
= saa_rstruct(syms
))) {
1184 if (sym
->type
& SYM_GLOBAL
)
1187 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1188 WRITELONG(p
, sym
->symv
.key
); /* value of symbol */
1189 WRITELONG(p
, sym
->size
); /* size of symbol */
1190 WRITECHAR(p
, sym
->type
); /* type and binding */
1191 WRITECHAR(p
, sym
->other
); /* visibility */
1192 WRITESHORT(p
, sym
->section
); /* index into section header table */
1193 saa_wbytes(s
, entry
, 16L);
1198 * dwarf needs symbols for debug sections
1199 * which are relocation targets.
1201 if (dfmt
== &df_dwarf
) {
1202 dwarf_infosym
= *local
;
1204 WRITELONG(p
, 0); /* no symbol name */
1205 WRITELONG(p
, 0); /* offset zero */
1206 WRITELONG(p
, 0); /* size zero */
1207 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1208 WRITESHORT(p
, sec_debug_info
); /* section id */
1209 saa_wbytes(s
, entry
, 16L);
1212 dwarf_abbrevsym
= *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_abbrev
); /* section id */
1219 saa_wbytes(s
, entry
, 16L);
1222 dwarf_linesym
= *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_line
); /* section id */
1229 saa_wbytes(s
, entry
, 16L);
1235 * Now the global symbols.
1238 while ((sym
= saa_rstruct(syms
))) {
1239 if (!(sym
->type
& SYM_GLOBAL
))
1242 WRITELONG(p
, sym
->strpos
);
1243 WRITELONG(p
, sym
->symv
.key
);
1244 WRITELONG(p
, sym
->size
);
1245 WRITECHAR(p
, sym
->type
); /* type and binding */
1246 WRITECHAR(p
, sym
->other
); /* visibility */
1247 WRITESHORT(p
, sym
->section
);
1248 saa_wbytes(s
, entry
, 16L);
1255 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
1258 uint8_t *p
, entry
[12];
1259 int32_t global_offset
;
1268 * How to onvert from a global placeholder to a real symbol index;
1269 * the +2 refers to the two special entries, the null entry and
1270 * the filename entry.
1272 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1275 int32_t sym
= r
->symbol
;
1277 if (sym
>= GLOBAL_TEMP_BASE
)
1278 sym
+= global_offset
;
1281 WRITELONG(p
, r
->address
);
1282 WRITELONG(p
, (sym
<< 8) + r
->type
);
1283 WRITELONG(p
, r
->offset
);
1284 saa_wbytes(s
, entry
, 12L);
1293 static void elf_section_header(int name
, int type
, uint32_t flags
,
1294 void *data
, bool is_saa
, uint32_t datalen
,
1295 int link
, int info
, int align
, int eltsize
)
1297 elf_sects
[elf_nsect
].data
= data
;
1298 elf_sects
[elf_nsect
].len
= datalen
;
1299 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1302 fwriteint32_t((int32_t)name
, ofile
);
1303 fwriteint32_t((int32_t)type
, ofile
);
1304 fwriteint32_t((int32_t)flags
, ofile
);
1305 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1306 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1307 fwriteint32_t(datalen
, ofile
);
1309 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1310 fwriteint32_t((int32_t)link
, ofile
);
1311 fwriteint32_t((int32_t)info
, ofile
);
1312 fwriteint32_t((int32_t)align
, ofile
);
1313 fwriteint32_t((int32_t)eltsize
, ofile
);
1316 static void elf_write_sections(void)
1319 for (i
= 0; i
< elf_nsect
; i
++)
1320 if (elf_sects
[i
].data
) {
1321 int32_t len
= elf_sects
[i
].len
;
1322 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1323 int32_t align
= reallen
- len
;
1324 if (elf_sects
[i
].is_saa
)
1325 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1327 nasm_write(elf_sects
[i
].data
, len
, ofile
);
1328 fwritezero(align
, ofile
);
1332 static void elf_sect_write(struct elf_section
*sect
, const void *data
, size_t len
)
1334 saa_wbytes(sect
->data
, data
, len
);
1337 static void elf_sect_writeaddr(struct elf_section
*sect
, int32_t data
, size_t len
)
1339 saa_writeaddr(sect
->data
, data
, len
);
1343 static void elf_sectalign(int32_t seg
, unsigned int value
)
1345 struct elf_section
*s
= NULL
;
1348 for (i
= 0; i
< nsects
; i
++) {
1349 if (sects
[i
]->index
== seg
) {
1354 if (!s
|| !is_power2(value
))
1357 if (value
> s
->align
)
1361 static int32_t elf_segbase(int32_t segment
)
1366 static void elf_filename(char *inname
, char *outname
)
1368 strcpy(elf_module
, inname
);
1369 standard_extension(inname
, outname
, ".o");
1372 extern macros_t elf_stdmac
[];
1374 static int elf_set_info(enum geninfo type
, char **val
)
1380 static struct dfmt df_dwarf
= {
1381 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1385 null_debug_deflabel
,
1386 null_debug_directive
,
1391 static struct dfmt df_stabs
= {
1392 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1396 null_debug_deflabel
,
1397 null_debug_directive
,
1403 struct dfmt
*elfx32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1405 struct ofmt of_elfx32
= {
1406 "ELFX32 (x86_64) object files (e.g. Linux)",
1425 /* common debugging routines */
1426 static void debugx32_typevalue(int32_t type
)
1428 int32_t stype
, ssize
;
1429 switch (TYM_TYPE(type
)) {
1472 stype
= STT_SECTION
;
1487 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1488 lastsym
->size
= ssize
;
1489 lastsym
->type
= stype
;
1493 /* stabs debugging routines */
1495 static void stabsx32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1498 if (!stabs_filename
) {
1499 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1500 strcpy(stabs_filename
, filename
);
1502 if (strcmp(stabs_filename
, filename
)) {
1503 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1504 in fact, this leak comes in quite handy to maintain a list of files
1505 encountered so far in the symbol lines... */
1507 /* why not nasm_free(stabs_filename); we're done with the old one */
1509 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1510 strcpy(stabs_filename
, filename
);
1514 currentline
= linenumber
;
1518 static void stabsx32_output(int type
, void *param
)
1520 struct symlininfo
*s
;
1521 struct linelist
*el
;
1522 if (type
== TY_DEBUGSYMLIN
) {
1523 if (debug_immcall
) {
1524 s
= (struct symlininfo
*)param
;
1525 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1526 return; /* line info is only collected for executable sections */
1528 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1529 el
->info
.offset
= s
->offset
;
1530 el
->info
.section
= s
->section
;
1531 el
->info
.name
= s
->name
;
1532 el
->line
= currentline
;
1533 el
->filename
= stabs_filename
;
1536 stabslines
->last
->next
= el
;
1537 stabslines
->last
= el
;
1540 stabslines
->last
= el
;
1547 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1549 static void stabsx32_generate(void)
1551 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1552 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1556 struct linelist
*ptr
;
1560 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
1563 if (numfiles
== 0) {
1564 allfiles
[0] = ptr
->filename
;
1567 for (i
= 0; i
< numfiles
; i
++) {
1568 if (!strcmp(allfiles
[i
], ptr
->filename
))
1571 if (i
>= numfiles
) {
1572 allfiles
[i
] = ptr
->filename
;
1579 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1580 for (i
= 0; i
< numfiles
; i
++) {
1581 fileidx
[i
] = strsize
;
1582 strsize
+= strlen(allfiles
[i
]) + 1;
1585 for (i
= 0; i
< numfiles
; i
++) {
1586 if (!strcmp(allfiles
[i
], elf_module
)) {
1593 * worst case size of the stab buffer would be:
1594 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1595 * plus one "ending" entry
1597 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1598 sizeof(struct stabentry
));
1599 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1600 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1603 for (i
= 0; i
< numfiles
; i
++)
1604 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1607 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1615 * this is the first stab, its strx points to the filename of the
1616 * the source-file, the n_desc field should be set to the number
1617 * of remaining stabs
1619 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, stabstrlen
);
1621 /* this is the stab for the main source file */
1622 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1624 /* relocation table entry */
1627 * Since the symbol table has two entries before
1628 * the section symbols, the index in the info.section
1629 * member must be adjusted by adding 2
1632 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1633 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1636 currfile
= mainfileindex
;
1640 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1641 /* oops file has changed... */
1642 for (i
= 0; i
< numfiles
; i
++)
1643 if (!strcmp(allfiles
[i
], ptr
->filename
))
1646 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1650 /* relocation table entry */
1652 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1653 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1656 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1659 /* relocation table entry */
1661 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1662 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1668 /* this is an "ending" token */
1669 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1672 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1674 nasm_free(allfiles
);
1677 stablen
= (sptr
- sbuf
);
1678 stabrellen
= (rptr
- rbuf
);
1684 static void stabsx32_cleanup(void)
1686 struct linelist
*ptr
, *del
;
1698 nasm_free(stabrelbuf
);
1699 nasm_free(stabstrbuf
);
1702 /* dwarf routines */
1704 static void dwarfx32_init(void)
1706 ndebugs
= 3; /* 3 debug symbols */
1709 static void dwarfx32_linenum(const char *filename
, int32_t linenumber
,
1713 dwarfx32_findfile(filename
);
1715 currentline
= linenumber
;
1718 /* called from elf_out with type == TY_DEBUGSYMLIN */
1719 static void dwarfx32_output(int type
, void *param
)
1721 int ln
, aa
, inx
, maxln
, soc
;
1722 struct symlininfo
*s
;
1727 s
= (struct symlininfo
*)param
;
1729 /* line number info is only gathered for executable sections */
1730 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1733 /* Check if section index has changed */
1734 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1735 dwarfx32_findsect(s
->section
);
1737 /* do nothing unless line or file has changed */
1741 ln
= currentline
- dwarf_csect
->line
;
1742 aa
= s
->offset
- dwarf_csect
->offset
;
1743 inx
= dwarf_clist
->line
;
1744 plinep
= dwarf_csect
->psaa
;
1745 /* check for file change */
1746 if (!(inx
== dwarf_csect
->file
)) {
1747 saa_write8(plinep
,DW_LNS_set_file
);
1748 saa_write8(plinep
,inx
);
1749 dwarf_csect
->file
= inx
;
1751 /* check for line change */
1753 /* test if in range of special op code */
1754 maxln
= line_base
+ line_range
;
1755 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1756 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1757 saa_write8(plinep
,soc
);
1759 saa_write8(plinep
,DW_LNS_advance_line
);
1760 saa_wleb128s(plinep
,ln
);
1762 saa_write8(plinep
,DW_LNS_advance_pc
);
1763 saa_wleb128u(plinep
,aa
);
1766 dwarf_csect
->line
= currentline
;
1767 dwarf_csect
->offset
= s
->offset
;
1770 /* show change handled */
1775 static void dwarfx32_generate(void)
1779 struct linelist
*ftentry
;
1780 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1781 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1782 struct sectlist
*psect
;
1783 size_t saalen
, linepoff
, totlen
, highaddr
;
1785 /* write epilogues for each line program range */
1786 /* and build aranges section */
1787 paranges
= saa_init(1L);
1788 parangesrel
= saa_init(1L);
1789 saa_write16(paranges
,3); /* dwarf version */
1790 saa_write32(parangesrel
, paranges
->datalen
+4);
1791 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_X86_64_32
); /* reloc to info */
1792 saa_write32(parangesrel
, 0);
1793 saa_write32(paranges
,0); /* offset into info */
1794 saa_write8(paranges
,4); /* pointer size */
1795 saa_write8(paranges
,0); /* not segmented */
1796 saa_write32(paranges
,0); /* padding */
1797 /* iterate though sectlist entries */
1798 psect
= dwarf_fsect
;
1801 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1803 plinep
= psect
->psaa
;
1804 /* Line Number Program Epilogue */
1805 saa_write8(plinep
,2); /* std op 2 */
1806 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1807 saa_write8(plinep
,DW_LNS_extended_op
);
1808 saa_write8(plinep
,1); /* operand length */
1809 saa_write8(plinep
,DW_LNE_end_sequence
);
1810 totlen
+= plinep
->datalen
;
1811 /* range table relocation entry */
1812 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1813 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_X86_64_32
);
1814 saa_write32(parangesrel
, (uint32_t) 0);
1815 /* range table entry */
1816 saa_write32(paranges
,0x0000); /* range start */
1817 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1818 highaddr
+= sects
[psect
->section
]->len
;
1819 /* done with this entry */
1820 psect
= psect
->next
;
1822 saa_write32(paranges
,0); /* null address */
1823 saa_write32(paranges
,0); /* null length */
1824 saalen
= paranges
->datalen
;
1825 arangeslen
= saalen
+ 4;
1826 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1827 WRITELONG(pbuf
,saalen
); /* initial length */
1828 saa_rnbytes(paranges
, pbuf
, saalen
);
1831 /* build rela.aranges section */
1832 arangesrellen
= saalen
= parangesrel
->datalen
;
1833 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1834 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1835 saa_free(parangesrel
);
1837 /* build pubnames section */
1838 ppubnames
= saa_init(1L);
1839 saa_write16(ppubnames
,3); /* dwarf version */
1840 saa_write32(ppubnames
,0); /* offset into info */
1841 saa_write32(ppubnames
,0); /* space used in info */
1842 saa_write32(ppubnames
,0); /* end of list */
1843 saalen
= ppubnames
->datalen
;
1844 pubnameslen
= saalen
+ 4;
1845 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1846 WRITELONG(pbuf
,saalen
); /* initial length */
1847 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1848 saa_free(ppubnames
);
1850 /* build info section */
1851 pinfo
= saa_init(1L);
1852 pinforel
= saa_init(1L);
1853 saa_write16(pinfo
,3); /* dwarf version */
1854 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1855 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_X86_64_32
); /* reloc to abbrev */
1856 saa_write32(pinforel
, 0);
1857 saa_write32(pinfo
,0); /* offset into abbrev */
1858 saa_write8(pinfo
,4); /* pointer size */
1859 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1860 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1861 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1862 saa_write32(pinforel
, 0);
1863 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1864 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1865 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1866 saa_write32(pinforel
, 0);
1867 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1868 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1869 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_X86_64_32
); /* reloc to line */
1870 saa_write32(pinforel
, 0);
1871 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1872 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1873 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1874 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1875 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1876 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1877 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1878 saa_write32(pinforel
, 0);
1879 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1880 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1881 saa_write8(pinfo
,0); /* end of entries */
1882 saalen
= pinfo
->datalen
;
1883 infolen
= saalen
+ 4;
1884 infobuf
= pbuf
= nasm_malloc(infolen
);
1885 WRITELONG(pbuf
,saalen
); /* initial length */
1886 saa_rnbytes(pinfo
, pbuf
, saalen
);
1889 /* build rela.info section */
1890 inforellen
= saalen
= pinforel
->datalen
;
1891 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1892 saa_rnbytes(pinforel
, pbuf
, saalen
);
1895 /* build abbrev section */
1896 pabbrev
= saa_init(1L);
1897 saa_write8(pabbrev
,1); /* entry number LEB128u */
1898 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1899 saa_write8(pabbrev
,1); /* has children */
1900 /* the following attributes and forms are all LEB128u values */
1901 saa_write8(pabbrev
,DW_AT_low_pc
);
1902 saa_write8(pabbrev
,DW_FORM_addr
);
1903 saa_write8(pabbrev
,DW_AT_high_pc
);
1904 saa_write8(pabbrev
,DW_FORM_addr
);
1905 saa_write8(pabbrev
,DW_AT_stmt_list
);
1906 saa_write8(pabbrev
,DW_FORM_data4
);
1907 saa_write8(pabbrev
,DW_AT_name
);
1908 saa_write8(pabbrev
,DW_FORM_string
);
1909 saa_write8(pabbrev
,DW_AT_producer
);
1910 saa_write8(pabbrev
,DW_FORM_string
);
1911 saa_write8(pabbrev
,DW_AT_language
);
1912 saa_write8(pabbrev
,DW_FORM_data2
);
1913 saa_write16(pabbrev
,0); /* end of entry */
1914 /* LEB128u usage same as above */
1915 saa_write8(pabbrev
,2); /* entry number */
1916 saa_write8(pabbrev
,DW_TAG_subprogram
);
1917 saa_write8(pabbrev
,0); /* no children */
1918 saa_write8(pabbrev
,DW_AT_low_pc
);
1919 saa_write8(pabbrev
,DW_FORM_addr
);
1920 saa_write8(pabbrev
,DW_AT_frame_base
);
1921 saa_write8(pabbrev
,DW_FORM_data4
);
1922 saa_write16(pabbrev
,0); /* end of entry */
1923 abbrevlen
= saalen
= pabbrev
->datalen
;
1924 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
1925 saa_rnbytes(pabbrev
, pbuf
, saalen
);
1928 /* build line section */
1930 plines
= saa_init(1L);
1931 saa_write8(plines
,1); /* Minimum Instruction Length */
1932 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
1933 saa_write8(plines
,line_base
); /* Line Base */
1934 saa_write8(plines
,line_range
); /* Line Range */
1935 saa_write8(plines
,opcode_base
); /* Opcode Base */
1936 /* standard opcode lengths (# of LEB128u operands) */
1937 saa_write8(plines
,0); /* Std opcode 1 length */
1938 saa_write8(plines
,1); /* Std opcode 2 length */
1939 saa_write8(plines
,1); /* Std opcode 3 length */
1940 saa_write8(plines
,1); /* Std opcode 4 length */
1941 saa_write8(plines
,1); /* Std opcode 5 length */
1942 saa_write8(plines
,0); /* Std opcode 6 length */
1943 saa_write8(plines
,0); /* Std opcode 7 length */
1944 saa_write8(plines
,0); /* Std opcode 8 length */
1945 saa_write8(plines
,1); /* Std opcode 9 length */
1946 saa_write8(plines
,0); /* Std opcode 10 length */
1947 saa_write8(plines
,0); /* Std opcode 11 length */
1948 saa_write8(plines
,1); /* Std opcode 12 length */
1949 /* Directory Table */
1950 saa_write8(plines
,0); /* End of table */
1951 /* File Name Table */
1952 ftentry
= dwarf_flist
;
1953 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
1955 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
1956 saa_write8(plines
,0); /* directory LEB128u */
1957 saa_write8(plines
,0); /* time LEB128u */
1958 saa_write8(plines
,0); /* size LEB128u */
1959 ftentry
= ftentry
->next
;
1961 saa_write8(plines
,0); /* End of table */
1962 linepoff
= plines
->datalen
;
1963 linelen
= linepoff
+ totlen
+ 10;
1964 linebuf
= pbuf
= nasm_malloc(linelen
);
1965 WRITELONG(pbuf
,linelen
-4); /* initial length */
1966 WRITESHORT(pbuf
,3); /* dwarf version */
1967 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
1968 /* write line header */
1970 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
1973 /* concatonate line program ranges */
1975 plinesrel
= saa_init(1L);
1976 psect
= dwarf_fsect
;
1977 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
1978 saa_write32(plinesrel
, linepoff
);
1979 saa_write32(plinesrel
, ((psect
->section
+ 2) << 8) + R_X86_64_32
);
1980 saa_write32(plinesrel
, 0);
1981 plinep
= psect
->psaa
;
1982 saalen
= plinep
->datalen
;
1983 saa_rnbytes(plinep
, pbuf
, saalen
);
1987 /* done with this entry */
1988 psect
= psect
->next
;
1992 /* build rela.lines section */
1993 linerellen
=saalen
= plinesrel
->datalen
;
1994 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
1995 saa_rnbytes(plinesrel
, pbuf
, saalen
);
1996 saa_free(plinesrel
);
1998 /* build frame section */
2000 framebuf
= pbuf
= nasm_malloc(framelen
);
2001 WRITELONG(pbuf
,framelen
-4); /* initial length */
2003 /* build loc section */
2005 locbuf
= pbuf
= nasm_malloc(loclen
);
2006 WRITELONG(pbuf
,0); /* null beginning offset */
2007 WRITELONG(pbuf
,0); /* null ending offset */
2010 static void dwarfx32_cleanup(void)
2012 nasm_free(arangesbuf
);
2013 nasm_free(arangesrelbuf
);
2014 nasm_free(pubnamesbuf
);
2016 nasm_free(inforelbuf
);
2017 nasm_free(abbrevbuf
);
2019 nasm_free(linerelbuf
);
2020 nasm_free(framebuf
);
2024 static void dwarfx32_findfile(const char * fname
)
2027 struct linelist
*match
;
2029 /* return if fname is current file name */
2030 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2033 /* search for match */
2036 match
= dwarf_flist
;
2037 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2038 if (!(strcmp(fname
, match
->filename
))) {
2039 dwarf_clist
= match
;
2045 /* add file name to end of list */
2046 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2048 dwarf_clist
->line
= dwarf_numfiles
;
2049 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2050 strcpy(dwarf_clist
->filename
,fname
);
2051 dwarf_clist
->next
= 0;
2052 if (!dwarf_flist
) { /* if first entry */
2053 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2054 dwarf_clist
->last
= 0;
2055 } else { /* chain to previous entry */
2056 dwarf_elist
->next
= dwarf_clist
;
2057 dwarf_elist
= dwarf_clist
;
2061 static void dwarfx32_findsect(const int index
)
2064 struct sectlist
*match
;
2067 /* return if index is current section index */
2068 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2071 /* search for match */
2074 match
= dwarf_fsect
;
2075 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2076 if (match
->section
== index
) {
2077 dwarf_csect
= match
;
2080 match
= match
->next
;
2084 /* add entry to end of list */
2085 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2087 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2088 dwarf_csect
->line
= 1;
2089 dwarf_csect
->offset
= 0;
2090 dwarf_csect
->file
= 1;
2091 dwarf_csect
->section
= index
;
2092 dwarf_csect
->next
= 0;
2093 /* set relocatable address at start of line program */
2094 saa_write8(plinep
,DW_LNS_extended_op
);
2095 saa_write8(plinep
,5); /* operand length */
2096 saa_write8(plinep
,DW_LNE_set_address
);
2097 saa_write32(plinep
,0); /* Start Address */
2099 if (!dwarf_fsect
) { /* if first entry */
2100 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2101 dwarf_csect
->last
= 0;
2102 } else { /* chain to previous entry */
2103 dwarf_esect
->next
= dwarf_csect
;
2104 dwarf_esect
= dwarf_csect
;
2108 #endif /* OF_ELFX32 */