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 const 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 const struct dfmt df_dwarf
;
158 static const struct dfmt df_stabs
;
159 static struct elf_symbol
*lastsym
;
161 /* common debugging routines */
162 static void debug_typevalue(int32_t);
163 static void debug_deflabel(char *, int32_t, int64_t, int, char *);
164 static void debug_directive(const char *, const char *);
166 /* stabs debugging routines */
167 static void stabs_linenum(const char *filename
, int32_t linenumber
, int32_t);
168 static void stabs_output(int, void *);
169 static void stabs_generate(void);
170 static void stabs_cleanup(void);
172 /* dwarf debugging routines */
173 static void dwarf_init(void);
174 static void dwarf_linenum(const char *filename
, int32_t linenumber
, int32_t);
175 static void dwarf_output(int, void *);
176 static void dwarf_generate(void);
177 static void dwarf_cleanup(void);
178 static void dwarf_findfile(const char *);
179 static void dwarf_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)
194 nsects
= sectlen
= 0;
195 syms
= saa_init((int32_t)sizeof(struct elf_symbol
));
196 nlocals
= nglobs
= ndebugs
= 0;
199 saa_wbytes(strs
, "\0", 1L);
200 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
201 strslen
= 2 + strlen(elf_module
);
203 shstrtablen
= shstrtabsize
= 0;;
204 add_sectname("", "");
208 elf_gotpc_sect
= seg_alloc();
209 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
210 elf_gotoff_sect
= seg_alloc();
211 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
212 elf_got_sect
= seg_alloc();
213 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
214 elf_plt_sect
= seg_alloc();
215 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
216 elf_sym_sect
= seg_alloc();
217 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
218 elf_gottpoff_sect
= seg_alloc();
219 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
221 def_seg
= seg_alloc();
225 static void elf_cleanup(int debuginfo
)
233 for (i
= 0; i
< nsects
; i
++) {
234 if (sects
[i
]->type
!= SHT_NOBITS
)
235 saa_free(sects
[i
]->data
);
237 saa_free(sects
[i
]->rel
);
238 while (sects
[i
]->head
) {
240 sects
[i
]->head
= sects
[i
]->head
->next
;
253 /* add entry to the elf .shstrtab section */
254 static void add_sectname(char *firsthalf
, char *secondhalf
)
256 int len
= strlen(firsthalf
) + strlen(secondhalf
);
257 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
258 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
259 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
260 strcat(shstrtab
+ shstrtablen
, secondhalf
);
261 shstrtablen
+= len
+ 1;
264 static int elf_make_section(char *name
, int type
, int flags
, int align
)
266 struct elf_section
*s
;
268 s
= nasm_zalloc(sizeof(*s
));
270 if (type
!= SHT_NOBITS
)
271 s
->data
= saa_init(1L);
273 if (!strcmp(name
, ".text"))
276 s
->index
= seg_alloc();
277 add_sectname("", name
);
279 s
->name
= nasm_strdup(name
);
284 if (nsects
>= sectlen
)
285 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
291 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
294 uint32_t flags
, flags_and
, flags_or
;
299 * Default is 64 bits.
306 p
= nasm_skip_word(name
);
309 flags_and
= flags_or
= type
= align
= 0;
311 elf_section_attrib(name
, p
, pass
, &flags_and
,
312 &flags_or
, &align
, &type
);
314 if (!strcmp(name
, ".shstrtab") ||
315 !strcmp(name
, ".symtab") ||
316 !strcmp(name
, ".strtab")) {
317 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
322 for (i
= 0; i
< nsects
; i
++)
323 if (!strcmp(name
, sects
[i
]->name
))
326 const struct elf_known_section
*ks
= elf_known_sections
;
329 if (!strcmp(name
, ks
->name
))
334 type
= type
? type
: ks
->type
;
335 align
= align
? align
: ks
->align
;
336 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
338 i
= elf_make_section(name
, type
, flags
, align
);
339 } else if (pass
== 1) {
340 if ((type
&& sects
[i
]->type
!= type
)
341 || (align
&& sects
[i
]->align
!= align
)
342 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
343 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
344 " redeclaration of section `%s'", name
);
347 return sects
[i
]->index
;
350 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
351 int is_global
, char *special
)
354 struct elf_symbol
*sym
;
355 bool special_used
= false;
357 #if defined(DEBUG) && DEBUG>2
358 nasm_error(ERR_DEBUG
,
359 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
360 name
, segment
, offset
, is_global
, special
);
362 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
364 * This is a NASM special symbol. We never allow it into
365 * the ELF symbol table, even if it's a valid one. If it
366 * _isn't_ a valid one, we should barf immediately.
368 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
369 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
370 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
371 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
375 if (is_global
== 3) {
376 struct elf_symbol
**s
;
378 * Fix up a forward-reference symbol size from the first
381 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
382 if (!strcmp((*s
)->name
, name
)) {
383 struct tokenval tokval
;
385 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
389 tokval
.t_type
= TOKEN_INVALID
;
390 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, NULL
);
393 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
394 " expression as symbol size");
396 (*s
)->size
= reloc_value(e
);
400 * Remove it from the list of unresolved sizes.
402 nasm_free((*s
)->name
);
406 return; /* it wasn't an important one */
409 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
410 strslen
+= 1 + strlen(name
);
412 lastsym
= sym
= saa_wstruct(syms
);
414 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
417 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
418 sym
->other
= STV_DEFAULT
;
420 if (segment
== NO_SEG
)
421 sym
->section
= SHN_ABS
;
424 sym
->section
= SHN_UNDEF
;
425 if (segment
== def_seg
) {
426 /* we have to be sure at least text section is there */
428 if (segment
!= elf_section_names(".text", 2, &tempint
))
429 nasm_panic(0, "strange segment conditions in ELF driver");
431 for (i
= 0; i
< nsects
; i
++) {
432 if (segment
== sects
[i
]->index
) {
433 sym
->section
= i
+ 1;
439 if (is_global
== 2) {
442 sym
->section
= SHN_COMMON
;
444 * We have a common variable. Check the special text to see
445 * if it's a valid number and power of two; if so, store it
446 * as the alignment for the common variable.
450 sym
->symv
.key
= readnum(special
, &err
);
452 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
453 " valid number", special
);
454 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
455 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
456 " power of two", special
);
460 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
462 if (sym
->type
== SYM_GLOBAL
) {
464 * If sym->section == SHN_ABS, then the first line of the
465 * else section would cause a core dump, because its a reference
466 * beyond the end of the section array.
467 * This behaviour is exhibited by this code:
470 * To avoid such a crash, such requests are silently discarded.
471 * This may not be the best solution.
473 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
474 bsym
= raa_write(bsym
, segment
, nglobs
);
475 } else if (sym
->section
!= SHN_ABS
) {
477 * This is a global symbol; so we must add it to the rbtree
478 * of global symbols in its section.
480 * In addition, we check the special text for symbol
481 * type and size information.
483 sects
[sym
->section
-1]->gsyms
=
484 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
487 int n
= strcspn(special
, " \t");
489 if (!nasm_strnicmp(special
, "function", n
))
490 sym
->type
|= STT_FUNC
;
491 else if (!nasm_strnicmp(special
, "data", n
) ||
492 !nasm_strnicmp(special
, "object", n
))
493 sym
->type
|= STT_OBJECT
;
494 else if (!nasm_strnicmp(special
, "notype", n
))
495 sym
->type
|= STT_NOTYPE
;
497 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
501 special
= nasm_skip_spaces(special
);
503 n
= strcspn(special
, " \t");
504 if (!nasm_strnicmp(special
, "default", n
))
505 sym
->other
= STV_DEFAULT
;
506 else if (!nasm_strnicmp(special
, "internal", n
))
507 sym
->other
= STV_INTERNAL
;
508 else if (!nasm_strnicmp(special
, "hidden", n
))
509 sym
->other
= STV_HIDDEN
;
510 else if (!nasm_strnicmp(special
, "protected", n
))
511 sym
->other
= STV_PROTECTED
;
518 struct tokenval tokval
;
521 char *saveme
= stdscan_get();
523 while (special
[n
] && nasm_isspace(special
[n
]))
526 * We have a size expression; attempt to
530 stdscan_set(special
+ n
);
531 tokval
.t_type
= TOKEN_INVALID
;
532 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, NULL
);
536 sym
->name
= nasm_strdup(name
);
539 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
540 " expression as symbol size");
542 sym
->size
= reloc_value(e
);
549 * If TLS segment, mark symbol accordingly.
551 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
553 sym
->type
|= STT_TLS
;
556 sym
->globnum
= nglobs
;
561 if (special
&& !special_used
)
562 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
565 static void elf_add_reloc(struct elf_section
*sect
, int32_t segment
,
566 int32_t offset
, int type
)
570 r
= *sect
->tail
= nasm_zalloc(sizeof(struct elf_reloc
));
571 sect
->tail
= &r
->next
;
573 r
->address
= sect
->len
;
576 if (segment
!= NO_SEG
) {
578 for (i
= 0; i
< nsects
; i
++)
579 if (segment
== sects
[i
]->index
)
582 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
590 * This routine deals with ..got and ..sym relocations: the more
591 * complicated kinds. In shared-library writing, some relocations
592 * with respect to global symbols must refer to the precise symbol
593 * rather than referring to an offset from the base of the section
594 * _containing_ the symbol. Such relocations call to this routine,
595 * which searches the symbol list for the symbol in question.
597 * R_X86_64_GOT32 references require the _exact_ symbol address to be
598 * used; R_X86_64_32 references can be at an offset from the symbol.
599 * The boolean argument `exact' tells us this.
601 * Return value is the adjusted value of `addr', having become an
602 * offset from the symbol rather than the section. Should always be
603 * zero when returning from an exact call.
605 * Limitation: if you define two symbols at the same place,
606 * confusion will occur.
608 * Inefficiency: we search, currently, using a linked list which
609 * isn't even necessarily sorted.
611 static void elf_add_gsym_reloc(struct elf_section
*sect
,
612 int32_t segment
, uint32_t offset
, int32_t pcrel
,
613 int type
, bool exact
)
616 struct elf_section
*s
;
617 struct elf_symbol
*sym
;
622 * First look up the segment/offset pair and find a global
623 * symbol corresponding to it. If it's not one of our segments,
624 * then it must be an external symbol, in which case we're fine
625 * doing a normal elf_add_reloc after first sanity-checking
626 * that the offset from the symbol is zero.
629 for (i
= 0; i
< nsects
; i
++)
630 if (segment
== sects
[i
]->index
) {
637 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
639 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
643 srb
= rb_search(s
->gsyms
, offset
);
644 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
645 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
646 " for this reference");
649 sym
= container_of(srb
, struct elf_symbol
, symv
);
651 r
= *sect
->tail
= nasm_malloc(sizeof(struct elf_reloc
));
652 sect
->tail
= &r
->next
;
655 r
->address
= sect
->len
;
656 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
657 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
663 static void elf_out(int32_t segto
, const void *data
,
664 enum out_type type
, uint64_t size
,
665 int32_t segment
, int32_t wrt
)
667 struct elf_section
*s
;
671 static struct symlininfo sinfo
;
673 #if defined(DEBUG) && DEBUG>2
675 nasm_error(ERR_DEBUG
,
676 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
" data: %"PRIx64
"\n",
677 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
679 nasm_error(ERR_DEBUG
,
680 " elf_out line: %d type: %x seg: %"PRIx32
" segto: %"PRIx32
" bytes: %"PRIx64
"\n",
681 currentline
, type
, segment
, segto
, size
);
685 * handle absolute-assembly (structure definitions)
687 if (segto
== NO_SEG
) {
688 if (type
!= OUT_RESERVE
)
689 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
695 for (i
= 0; i
< nsects
; i
++)
696 if (segto
== sects
[i
]->index
) {
701 int tempint
; /* ignored */
702 if (segto
!= elf_section_names(".text", 2, &tempint
))
703 nasm_panic(0, "strange segment conditions in ELF driver");
705 s
= sects
[nsects
- 1];
710 /* again some stabs debugging stuff */
712 sinfo
.offset
= s
->len
;
715 sinfo
.name
= s
->name
;
716 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
718 /* end of debugging stuff */
720 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
721 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
722 " BSS section `%s': ignored", s
->name
);
723 s
->len
+= realsize(type
, size
);
729 if (s
->type
== SHT_PROGBITS
) {
730 nasm_error(ERR_WARNING
, "uninitialized space declared in"
731 " non-BSS section `%s': zeroing", s
->name
);
732 elf_sect_write(s
, NULL
, size
);
738 if (segment
!= NO_SEG
)
739 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
740 elf_sect_write(s
, data
, size
);
745 int isize
= (int)size
;
746 int asize
= abs((int)size
);
748 addr
= *(int64_t *)data
;
749 if (segment
== NO_SEG
) {
751 } else if (segment
% 2) {
752 nasm_error(ERR_NONFATAL
, "ELF format does not support"
753 " segment base references");
759 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
763 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
766 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
769 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
773 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
776 nasm_panic(0, "internal error elfx32-hpa-871");
780 } else if (wrt
== elf_gotpc_sect
+ 1) {
782 * The user will supply GOT relative to $$. ELF
783 * will let us have GOT relative to $. So we
784 * need to fix up the data item by $-$$.
787 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
789 } else if (wrt
== elf_gotoff_sect
+ 1) {
790 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
791 "R_X86_64_GOTOFF64");
792 } else if (wrt
== elf_got_sect
+ 1) {
795 elf_add_gsym_reloc(s
, segment
, addr
, 0,
796 R_X86_64_GOT32
, true);
800 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
803 } else if (wrt
== elf_sym_sect
+ 1) {
807 elf_add_gsym_reloc(s
, segment
, addr
, 0,
813 elf_add_gsym_reloc(s
, segment
, addr
, 0,
818 elf_add_gsym_reloc(s
, segment
, addr
, 0,
823 elf_add_gsym_reloc(s
, segment
, addr
, 0,
824 R_X86_64_32S
, false);
829 elf_add_gsym_reloc(s
, segment
, addr
, 0,
834 nasm_panic(0, "internal error elfx32-hpa-903");
837 } else if (wrt
== elf_plt_sect
+ 1) {
838 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
839 "relative PLT references");
841 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
845 elf_sect_writeaddr(s
, addr
, asize
);
850 reltype
= R_X86_64_PC8
;
855 reltype
= R_X86_64_PC16
;
860 addr
= *(int64_t *)data
- size
;
861 if (segment
== segto
)
862 nasm_panic(0, "intra-segment OUT_REL1ADR");
863 if (segment
== NO_SEG
) {
865 } else if (segment
% 2) {
866 nasm_error(ERR_NONFATAL
, "ELF format does not support"
867 " segment base references");
870 elf_add_reloc(s
, segment
, addr
, reltype
);
873 nasm_error(ERR_NONFATAL
,
874 "Unsupported non-32-bit ELF relocation");
877 elf_sect_writeaddr(s
, addr
, bytes
);
881 addr
= *(int64_t *)data
- size
;
882 if (segment
== segto
)
883 nasm_panic(0, "intra-segment OUT_REL4ADR");
884 if (segment
== NO_SEG
) {
886 } else if (segment
% 2) {
887 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
888 " segment base references");
891 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
893 } else if (wrt
== elf_plt_sect
+ 1) {
894 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
895 R_X86_64_PLT32
, true);
897 } else if (wrt
== elf_gotpc_sect
+ 1 ||
898 wrt
== elf_got_sect
+ 1) {
899 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
900 R_X86_64_GOTPCREL
, true);
902 } else if (wrt
== elf_gotoff_sect
+ 1 ||
903 wrt
== elf_got_sect
+ 1) {
904 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
905 } else if (wrt
== elf_gottpoff_sect
+ 1) {
906 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
907 R_X86_64_GOTTPOFF
, true);
910 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
914 elf_sect_writeaddr(s
, addr
, 4);
918 nasm_error(ERR_NONFATAL
,
919 "32-bit ELF format does not support 64-bit relocations");
921 elf_sect_writeaddr(s
, addr
, 8);
926 static void elf_write(void)
933 int32_t symtablen
, symtablocal
;
936 * Work out how many sections we will have. We have SHN_UNDEF,
937 * then the flexible user sections, then the fixed sections
938 * `.shstrtab', `.symtab' and `.strtab', then optionally
939 * relocation sections for the user sections.
941 nsections
= sec_numspecial
+ 1;
942 if (dfmt
== &df_stabs
)
944 else if (dfmt
== &df_dwarf
)
947 add_sectname("", ".shstrtab");
948 add_sectname("", ".symtab");
949 add_sectname("", ".strtab");
950 for (i
= 0; i
< nsects
; i
++) {
951 nsections
++; /* for the section itself */
952 if (sects
[i
]->head
) {
953 nsections
++; /* for its relocations */
954 add_sectname(".rela", sects
[i
]->name
);
958 if (dfmt
== &df_stabs
) {
959 /* in case the debug information is wanted, just add these three sections... */
960 add_sectname("", ".stab");
961 add_sectname("", ".stabstr");
962 add_sectname(".rel", ".stab");
965 else if (dfmt
== &df_dwarf
) {
966 /* the dwarf debug standard specifies the following ten sections,
967 not all of which are currently implemented,
968 although all of them are defined. */
969 add_sectname("", ".debug_aranges");
970 add_sectname(".rela", ".debug_aranges");
971 add_sectname("", ".debug_pubnames");
972 add_sectname("", ".debug_info");
973 add_sectname(".rela", ".debug_info");
974 add_sectname("", ".debug_abbrev");
975 add_sectname("", ".debug_line");
976 add_sectname(".rela", ".debug_line");
977 add_sectname("", ".debug_frame");
978 add_sectname("", ".debug_loc");
982 * Output the ELF header.
984 nasm_write("\177ELF\1\1\1", 7, ofile
);
985 fputc(elf_osabi
, ofile
);
986 fputc(elf_abiver
, ofile
);
987 fwritezero(7, ofile
);
988 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
989 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
990 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
991 fwriteint32_t(0L, ofile
); /* no entry point */
992 fwriteint32_t(0L, ofile
); /* no program header table */
993 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
994 * ELF header plus alignment */
995 fwriteint32_t(0L, ofile
); /* X86_64 defines no special flags */
996 fwriteint16_t(0x34, ofile
); /* size of ELF header */
997 fwriteint16_t(0, ofile
); /* no program header table, again */
998 fwriteint16_t(0, ofile
); /* still no program header table */
999 fwriteint16_t(sizeof(Elf32_Shdr
), ofile
); /* size of section header */
1000 fwriteint16_t(nsections
, ofile
); /* number of sections */
1001 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
1002 * section header table */
1003 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
1004 fwriteint32_t(0L, ofile
);
1005 fwriteint32_t(0L, ofile
);
1008 * Build the symbol table and relocation tables.
1010 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1011 for (i
= 0; i
< nsects
; i
++)
1013 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1017 * Now output the section header table.
1020 elf_foffs
= 0x40 + sizeof(Elf32_Shdr
) * nsections
;
1021 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1024 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1027 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1030 /* The normal sections */
1031 for (i
= 0; i
< nsects
; i
++) {
1032 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1033 (sects
[i
]->type
== SHT_PROGBITS
?
1034 sects
[i
]->data
: NULL
), true,
1035 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1040 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1041 shstrtablen
, 0, 0, 1, 0);
1045 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1046 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1050 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1051 strslen
, 0, 0, 1, 0);
1054 /* The relocation sections */
1055 for (i
= 0; i
< nsects
; i
++)
1056 if (sects
[i
]->head
) {
1057 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1058 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 12);
1062 if (dfmt
== &df_stabs
) {
1063 /* for debugging information, create the last three sections
1064 which are the .stab , .stabstr and .rel.stab sections respectively */
1066 /* this function call creates the stab sections in memory */
1069 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1070 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1071 stablen
, sec_stabstr
, 0, 4, 12);
1074 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1075 stabstrlen
, 0, 0, 4, 0);
1078 /* link -> symtable info -> section to refer to */
1079 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1080 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1083 } else if (dfmt
== &df_dwarf
) {
1084 /* for dwarf debugging information, create the ten dwarf sections */
1086 /* this function call creates the dwarf sections in memory */
1090 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1091 arangeslen
, 0, 0, 1, 0);
1094 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1095 arangesrellen
, sec_symtab
, sec_debug_aranges
, 1, 12);
1098 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1099 pubnameslen
, 0, 0, 1, 0);
1102 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1103 infolen
, 0, 0, 1, 0);
1106 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1107 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1110 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1111 abbrevlen
, 0, 0, 1, 0);
1114 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1115 linelen
, 0, 0, 1, 0);
1118 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1119 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1122 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1123 framelen
, 0, 0, 8, 0);
1126 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1127 loclen
, 0, 0, 1, 0);
1130 fwritezero(align
, ofile
);
1133 * Now output the sections.
1135 elf_write_sections();
1137 nasm_free(elf_sects
);
1141 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1143 struct SAA
*s
= saa_init(1L);
1144 struct elf_symbol
*sym
;
1145 uint8_t entry
[24], *p
;
1151 * First, an all-zeros entry, required by the ELF spec.
1153 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1158 * Next, an entry for the file name.
1161 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1162 WRITELONG(p
, 0); /* no value */
1163 WRITELONG(p
, 0); /* no size either */
1164 WRITESHORT(p
, STT_FILE
); /* type FILE */
1165 WRITESHORT(p
, SHN_ABS
);
1166 saa_wbytes(s
, entry
, 16L);
1171 * Now some standard symbols defining the segments, for relocation
1174 for (i
= 1; i
<= nsects
; i
++) {
1176 WRITELONG(p
, 0); /* no symbol name */
1177 WRITELONG(p
, 0); /* offset zero */
1178 WRITELONG(p
, 0); /* size zero */
1179 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1180 WRITESHORT(p
, i
); /* section id */
1181 saa_wbytes(s
, entry
, 16L);
1188 * Now the other local symbols.
1191 while ((sym
= saa_rstruct(syms
))) {
1192 if (sym
->type
& SYM_GLOBAL
)
1195 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1196 WRITELONG(p
, sym
->symv
.key
); /* value of symbol */
1197 WRITELONG(p
, sym
->size
); /* size of symbol */
1198 WRITECHAR(p
, sym
->type
); /* type and binding */
1199 WRITECHAR(p
, sym
->other
); /* visibility */
1200 WRITESHORT(p
, sym
->section
); /* index into section header table */
1201 saa_wbytes(s
, entry
, 16L);
1206 * dwarf needs symbols for debug sections
1207 * which are relocation targets.
1209 if (dfmt
== &df_dwarf
) {
1210 dwarf_infosym
= *local
;
1212 WRITELONG(p
, 0); /* no symbol name */
1213 WRITELONG(p
, 0); /* offset zero */
1214 WRITELONG(p
, 0); /* size zero */
1215 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1216 WRITESHORT(p
, sec_debug_info
); /* section id */
1217 saa_wbytes(s
, entry
, 16L);
1220 dwarf_abbrevsym
= *local
;
1222 WRITELONG(p
, 0); /* no symbol name */
1223 WRITELONG(p
, 0); /* offset zero */
1224 WRITELONG(p
, 0); /* size zero */
1225 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1226 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1227 saa_wbytes(s
, entry
, 16L);
1230 dwarf_linesym
= *local
;
1232 WRITELONG(p
, 0); /* no symbol name */
1233 WRITELONG(p
, 0); /* offset zero */
1234 WRITELONG(p
, 0); /* size zero */
1235 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1236 WRITESHORT(p
, sec_debug_line
); /* section id */
1237 saa_wbytes(s
, entry
, 16L);
1243 * Now the global symbols.
1246 while ((sym
= saa_rstruct(syms
))) {
1247 if (!(sym
->type
& SYM_GLOBAL
))
1250 WRITELONG(p
, sym
->strpos
);
1251 WRITELONG(p
, sym
->symv
.key
);
1252 WRITELONG(p
, sym
->size
);
1253 WRITECHAR(p
, sym
->type
); /* type and binding */
1254 WRITECHAR(p
, sym
->other
); /* visibility */
1255 WRITESHORT(p
, sym
->section
);
1256 saa_wbytes(s
, entry
, 16L);
1263 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
1266 uint8_t *p
, entry
[12];
1267 int32_t global_offset
;
1276 * How to onvert from a global placeholder to a real symbol index;
1277 * the +2 refers to the two special entries, the null entry and
1278 * the filename entry.
1280 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1283 int32_t sym
= r
->symbol
;
1285 if (sym
>= GLOBAL_TEMP_BASE
)
1286 sym
+= global_offset
;
1289 WRITELONG(p
, r
->address
);
1290 WRITELONG(p
, (sym
<< 8) + r
->type
);
1291 WRITELONG(p
, r
->offset
);
1292 saa_wbytes(s
, entry
, 12L);
1301 static void elf_section_header(int name
, int type
, uint32_t flags
,
1302 void *data
, bool is_saa
, uint32_t datalen
,
1303 int link
, int info
, int align
, int eltsize
)
1305 elf_sects
[elf_nsect
].data
= data
;
1306 elf_sects
[elf_nsect
].len
= datalen
;
1307 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1310 fwriteint32_t((int32_t)name
, ofile
);
1311 fwriteint32_t((int32_t)type
, ofile
);
1312 fwriteint32_t((int32_t)flags
, ofile
);
1313 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1314 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1315 fwriteint32_t(datalen
, ofile
);
1317 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1318 fwriteint32_t((int32_t)link
, ofile
);
1319 fwriteint32_t((int32_t)info
, ofile
);
1320 fwriteint32_t((int32_t)align
, ofile
);
1321 fwriteint32_t((int32_t)eltsize
, ofile
);
1324 static void elf_write_sections(void)
1327 for (i
= 0; i
< elf_nsect
; i
++)
1328 if (elf_sects
[i
].data
) {
1329 int32_t len
= elf_sects
[i
].len
;
1330 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1331 int32_t align
= reallen
- len
;
1332 if (elf_sects
[i
].is_saa
)
1333 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1335 nasm_write(elf_sects
[i
].data
, len
, ofile
);
1336 fwritezero(align
, ofile
);
1340 static void elf_sect_write(struct elf_section
*sect
, const void *data
, size_t len
)
1342 saa_wbytes(sect
->data
, data
, len
);
1345 static void elf_sect_writeaddr(struct elf_section
*sect
, int32_t data
, size_t len
)
1347 saa_writeaddr(sect
->data
, data
, len
);
1351 static void elf_sectalign(int32_t seg
, unsigned int value
)
1353 struct elf_section
*s
= NULL
;
1356 for (i
= 0; i
< nsects
; i
++) {
1357 if (sects
[i
]->index
== seg
) {
1362 if (!s
|| !is_power2(value
))
1365 if (value
> s
->align
)
1369 static int32_t elf_segbase(int32_t segment
)
1374 static void elf_filename(char *inname
, char *outname
)
1376 strcpy(elf_module
, inname
);
1377 standard_extension(inname
, outname
, ".o");
1380 extern macros_t elf_stdmac
[];
1382 static int elf_set_info(enum geninfo type
, char **val
)
1388 static const struct dfmt df_dwarf
= {
1389 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1399 static const struct dfmt df_stabs
= {
1400 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1411 static const struct dfmt
* const elfx32_debugs_arr
[3] =
1412 { &df_dwarf
, &df_stabs
, NULL
};
1414 const struct ofmt of_elfx32
= {
1415 "ELFX32 (x86_64) object files (e.g. Linux)",
1434 /* common debugging routines */
1435 static void debug_deflabel(char *name
, int32_t segment
, int64_t offset
,
1436 int is_global
, char *special
)
1445 static void debug_directive(const char *directive
, const char *params
)
1451 static void debug_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 stabs_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 stabs_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 stabs_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, stabstrlen
);
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 stabs_cleanup(void)
1711 struct linelist
*ptr
, *del
;
1723 nasm_free(stabrelbuf
);
1724 nasm_free(stabstrbuf
);
1727 /* dwarf routines */
1729 static void dwarf_init(void)
1731 ndebugs
= 3; /* 3 debug symbols */
1734 static void dwarf_linenum(const char *filename
, int32_t linenumber
,
1738 dwarf_findfile(filename
);
1740 currentline
= linenumber
;
1743 /* called from elf_out with type == TY_DEBUGSYMLIN */
1744 static void dwarf_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 dwarf_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 dwarf_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 dwarf_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 dwarf_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 dwarf_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 */