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
52 #include "output/outform.h"
53 #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_elf32
;
86 extern const struct ofmt of_elf64
;
87 extern const struct ofmt of_elfx32
;
89 static struct ELF_SECTDATA
{
94 static int elf_nsect
, nsections
;
95 static int64_t elf_foffs
;
97 static void elf_write(void);
98 static void elf_sect_write(struct elf_section
*, const void *, size_t);
99 static void elf_sect_writeaddr(struct elf_section
*, int64_t, size_t);
100 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
102 static void elf_write_sections(void);
103 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
104 static struct SAA
*elf_build_reltab(uint64_t *, struct elf_reloc
*);
105 static void add_sectname(char *, char *);
113 int section
; /* index into sects[] */
114 int segto
; /* internal section number */
115 char *name
; /* shallow-copied pointer of section name */
119 struct linelist
*next
;
120 struct linelist
*last
;
121 struct symlininfo info
;
132 struct sectlist
*next
;
133 struct sectlist
*last
;
136 /* common debug variables */
137 static int currentline
= 1;
138 static int debug_immcall
= 0;
140 /* stabs debug variables */
141 static struct linelist
*stabslines
= 0;
142 static int numlinestabs
= 0;
143 static char *stabs_filename
= 0;
144 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
145 static int stablen
, stabstrlen
, stabrellen
;
147 /* dwarf debug variables */
148 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
149 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
150 static int dwarf_numfiles
= 0, dwarf_nsections
;
151 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
152 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
153 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
154 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
155 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
156 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
159 static const struct dfmt df_dwarf
;
160 static const struct dfmt df_stabs
;
161 static struct elf_symbol
*lastsym
;
163 /* common debugging routines */
164 static void debug_typevalue(int32_t);
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 NASM section numbers which are used to define ELF special
185 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
186 static int32_t elf_got_sect
, elf_plt_sect
;
187 static int32_t elf_sym_sect
, elf_gottpoff_sect
, elf_tlsie_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("", "");
207 * FIXME: tlsie is Elf32 only and
208 * gottpoff is Elfx32|64 only.
211 elf_gotpc_sect
= seg_alloc();
212 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
213 elf_gotoff_sect
= seg_alloc();
214 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
215 elf_got_sect
= seg_alloc();
216 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
217 elf_plt_sect
= seg_alloc();
218 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
219 elf_sym_sect
= seg_alloc();
220 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
221 elf_gottpoff_sect
= seg_alloc();
222 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
223 elf_tlsie_sect
= seg_alloc();
224 define_label("..tlsie", elf_tlsie_sect
+ 1, 0L, NULL
, false, false);
226 def_seg
= seg_alloc();
229 static void elf_cleanup(void)
235 for (i
= 0; i
< nsects
; i
++) {
236 if (sects
[i
]->type
!= SHT_NOBITS
)
237 saa_free(sects
[i
]->data
);
239 saa_free(sects
[i
]->rel
);
240 while (sects
[i
]->head
) {
242 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 *bits
= ofmt
->maxbits
;
303 p
= nasm_skip_word(name
);
306 flags_and
= flags_or
= type
= align
= 0;
308 elf_section_attrib(name
, p
, pass
, &flags_and
,
309 &flags_or
, &align
, &type
);
311 if (!strcmp(name
, ".shstrtab") ||
312 !strcmp(name
, ".symtab") ||
313 !strcmp(name
, ".strtab")) {
314 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
319 for (i
= 0; i
< nsects
; i
++)
320 if (!strcmp(name
, sects
[i
]->name
))
323 const struct elf_known_section
*ks
= elf_known_sections
;
326 if (!strcmp(name
, ks
->name
))
331 type
= type
? type
: ks
->type
;
332 align
= align
? align
: ks
->align
;
333 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
335 i
= elf_make_section(name
, type
, flags
, align
);
336 } else if (pass
== 1) {
337 if ((type
&& sects
[i
]->type
!= type
)
338 || (align
&& sects
[i
]->align
!= align
)
339 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
340 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
341 " redeclaration of section `%s'", name
);
344 return sects
[i
]->index
;
347 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
348 int is_global
, char *special
)
351 struct elf_symbol
*sym
;
352 bool special_used
= false;
354 #if defined(DEBUG) && DEBUG>2
355 nasm_error(ERR_DEBUG
,
356 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
357 name
, segment
, offset
, is_global
, special
);
359 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
361 * This is a NASM special symbol. We never allow it into
362 * the ELF symbol table, even if it's a valid one. If it
363 * _isn't_ a valid one, we should barf immediately.
365 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
367 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
368 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
369 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff") &&
370 strcmp(name
, "..tlsie"))
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 int64_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
;
674 * handle absolute-assembly (structure definitions)
676 if (segto
== NO_SEG
) {
677 if (type
!= OUT_RESERVE
)
678 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
684 for (i
= 0; i
< nsects
; i
++)
685 if (segto
== sects
[i
]->index
) {
690 int tempint
; /* ignored */
691 if (segto
!= elf_section_names(".text", 2, &tempint
))
692 nasm_panic(0, "strange segment conditions in ELF driver");
694 s
= sects
[nsects
- 1];
699 /* again some stabs debugging stuff */
700 sinfo
.offset
= s
->len
;
703 sinfo
.name
= s
->name
;
704 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
705 /* end of debugging stuff */
707 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
708 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
709 " BSS section `%s': ignored", s
->name
);
710 s
->len
+= realsize(type
, size
);
716 if (s
->type
== SHT_PROGBITS
) {
717 nasm_error(ERR_WARNING
, "uninitialized space declared in"
718 " non-BSS section `%s': zeroing", s
->name
);
719 elf_sect_write(s
, NULL
, size
);
725 if (segment
!= NO_SEG
)
726 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
727 elf_sect_write(s
, data
, size
);
732 int isize
= (int)size
;
733 int asize
= abs((int)size
);
735 addr
= *(int64_t *)data
;
736 if (segment
== NO_SEG
) {
738 } else if (segment
% 2) {
739 nasm_error(ERR_NONFATAL
, "ELF format does not support"
740 " segment base references");
746 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
750 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
753 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
756 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
760 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
763 nasm_panic(0, "internal error elfx32-hpa-871");
767 } else if (wrt
== elf_gotpc_sect
+ 1) {
769 * The user will supply GOT relative to $$. ELF
770 * will let us have GOT relative to $. So we
771 * need to fix up the data item by $-$$.
774 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
776 } else if (wrt
== elf_gotoff_sect
+ 1) {
777 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
778 "R_X86_64_GOTOFF64");
779 } else if (wrt
== elf_got_sect
+ 1) {
782 elf_add_gsym_reloc(s
, segment
, addr
, 0,
783 R_X86_64_GOT32
, true);
787 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
790 } else if (wrt
== elf_sym_sect
+ 1) {
794 elf_add_gsym_reloc(s
, segment
, addr
, 0,
800 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,
811 R_X86_64_32S
, false);
816 elf_add_gsym_reloc(s
, segment
, addr
, 0,
821 nasm_panic(0, "internal error elfx32-hpa-903");
824 } else if (wrt
== elf_plt_sect
+ 1) {
825 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
826 "relative PLT references");
828 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
832 elf_sect_writeaddr(s
, addr
, asize
);
837 reltype
= R_X86_64_PC8
;
842 reltype
= R_X86_64_PC16
;
847 addr
= *(int64_t *)data
- size
;
848 if (segment
== segto
)
849 nasm_panic(0, "intra-segment OUT_REL1ADR");
850 if (segment
== NO_SEG
) {
852 } else if (segment
% 2) {
853 nasm_error(ERR_NONFATAL
, "ELF format does not support"
854 " segment base references");
857 elf_add_reloc(s
, segment
, addr
, reltype
);
860 nasm_error(ERR_NONFATAL
,
861 "Unsupported non-32-bit ELF relocation");
864 elf_sect_writeaddr(s
, addr
, bytes
);
868 addr
= *(int64_t *)data
- size
;
869 if (segment
== segto
)
870 nasm_panic(0, "intra-segment OUT_REL4ADR");
871 if (segment
== NO_SEG
) {
873 } else if (segment
% 2) {
874 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
875 " segment base references");
878 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
880 } else if (wrt
== elf_plt_sect
+ 1) {
881 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
882 R_X86_64_PLT32
, true);
884 } else if (wrt
== elf_gotpc_sect
+ 1 ||
885 wrt
== elf_got_sect
+ 1) {
886 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
887 R_X86_64_GOTPCREL
, true);
889 } else if (wrt
== elf_gotoff_sect
+ 1 ||
890 wrt
== elf_got_sect
+ 1) {
891 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
892 } else if (wrt
== elf_gottpoff_sect
+ 1) {
893 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
894 R_X86_64_GOTTPOFF
, true);
897 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
901 elf_sect_writeaddr(s
, addr
, 4);
905 nasm_error(ERR_NONFATAL
,
906 "32-bit ELF format does not support 64-bit relocations");
908 elf_sect_writeaddr(s
, addr
, 8);
913 static void elf_write(void)
920 int32_t symtablen
, symtablocal
;
923 * Work out how many sections we will have. We have SHN_UNDEF,
924 * then the flexible user sections, then the fixed sections
925 * `.shstrtab', `.symtab' and `.strtab', then optionally
926 * relocation sections for the user sections.
928 nsections
= sec_numspecial
+ 1;
929 if (dfmt
== &df_stabs
)
931 else if (dfmt
== &df_dwarf
)
934 add_sectname("", ".shstrtab");
935 add_sectname("", ".symtab");
936 add_sectname("", ".strtab");
937 for (i
= 0; i
< nsects
; i
++) {
938 nsections
++; /* for the section itself */
939 if (sects
[i
]->head
) {
940 nsections
++; /* for its relocations */
941 add_sectname(".rela", sects
[i
]->name
);
945 if (dfmt
== &df_stabs
) {
946 /* in case the debug information is wanted, just add these three sections... */
947 add_sectname("", ".stab");
948 add_sectname("", ".stabstr");
949 add_sectname(".rel", ".stab");
950 } else if (dfmt
== &df_dwarf
) {
951 /* the dwarf debug standard specifies the following ten sections,
952 not all of which are currently implemented,
953 although all of them are defined. */
954 add_sectname("", ".debug_aranges");
955 add_sectname(".rela", ".debug_aranges");
956 add_sectname("", ".debug_pubnames");
957 add_sectname("", ".debug_info");
958 add_sectname(".rela", ".debug_info");
959 add_sectname("", ".debug_abbrev");
960 add_sectname("", ".debug_line");
961 add_sectname(".rela", ".debug_line");
962 add_sectname("", ".debug_frame");
963 add_sectname("", ".debug_loc");
967 * Output the ELF header.
969 nasm_write("\177ELF\1\1\1", 7, ofile
);
970 fputc(elf_osabi
, ofile
);
971 fputc(elf_abiver
, ofile
);
972 fwritezero(7, ofile
);
973 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
974 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
975 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
976 fwriteint32_t(0L, ofile
); /* no entry point */
977 fwriteint32_t(0L, ofile
); /* no program header table */
978 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
979 * ELF header plus alignment */
980 fwriteint32_t(0L, ofile
); /* X86_64 defines no special flags */
981 fwriteint16_t(0x34, ofile
); /* size of ELF header */
982 fwriteint16_t(0, ofile
); /* no program header table, again */
983 fwriteint16_t(0, ofile
); /* still no program header table */
984 fwriteint16_t(sizeof(Elf32_Shdr
), ofile
); /* size of section header */
985 fwriteint16_t(nsections
, ofile
); /* number of sections */
986 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
987 * section header table */
988 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
989 fwriteint32_t(0L, ofile
);
990 fwriteint32_t(0L, ofile
);
993 * Build the symbol table and relocation tables.
995 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
996 for (i
= 0; i
< nsects
; i
++)
998 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1002 * Now output the section header table.
1005 elf_foffs
= 0x40 + sizeof(Elf32_Shdr
) * nsections
;
1006 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1009 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1012 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1015 /* The normal sections */
1016 for (i
= 0; i
< nsects
; i
++) {
1017 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1018 (sects
[i
]->type
== SHT_PROGBITS
?
1019 sects
[i
]->data
: NULL
), true,
1020 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1025 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1026 shstrtablen
, 0, 0, 1, 0);
1030 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1031 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1035 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1036 strslen
, 0, 0, 1, 0);
1039 /* The relocation sections */
1040 for (i
= 0; i
< nsects
; i
++)
1041 if (sects
[i
]->head
) {
1042 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1043 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 12);
1047 if (dfmt
== &df_stabs
) {
1048 /* for debugging information, create the last three sections
1049 which are the .stab , .stabstr and .rel.stab sections respectively */
1051 /* this function call creates the stab sections in memory */
1054 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1055 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1056 stablen
, sec_stabstr
, 0, 4, 12);
1059 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1060 stabstrlen
, 0, 0, 4, 0);
1063 /* link -> symtable info -> section to refer to */
1064 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1065 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1068 } else if (dfmt
== &df_dwarf
) {
1069 /* for dwarf debugging information, create the ten dwarf sections */
1071 /* this function call creates the dwarf sections in memory */
1075 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1076 arangeslen
, 0, 0, 1, 0);
1079 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1080 arangesrellen
, sec_symtab
, sec_debug_aranges
, 1, 12);
1083 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1084 pubnameslen
, 0, 0, 1, 0);
1087 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1088 infolen
, 0, 0, 1, 0);
1091 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1092 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1095 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1096 abbrevlen
, 0, 0, 1, 0);
1099 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1100 linelen
, 0, 0, 1, 0);
1103 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1104 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1107 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1108 framelen
, 0, 0, 8, 0);
1111 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1112 loclen
, 0, 0, 1, 0);
1115 fwritezero(align
, ofile
);
1118 * Now output the sections.
1120 elf_write_sections();
1122 nasm_free(elf_sects
);
1126 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1128 struct SAA
*s
= saa_init(1L);
1129 struct elf_symbol
*sym
;
1130 uint8_t entry
[24], *p
;
1136 * First, an all-zeros entry, required by the ELF spec.
1138 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1143 * Next, an entry for the file name.
1146 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1147 WRITELONG(p
, 0); /* no value */
1148 WRITELONG(p
, 0); /* no size either */
1149 WRITESHORT(p
, STT_FILE
); /* type FILE */
1150 WRITESHORT(p
, SHN_ABS
);
1151 saa_wbytes(s
, entry
, 16L);
1156 * Now some standard symbols defining the segments, for relocation
1159 for (i
= 1; i
<= nsects
; i
++) {
1161 WRITELONG(p
, 0); /* no symbol name */
1162 WRITELONG(p
, 0); /* offset zero */
1163 WRITELONG(p
, 0); /* size zero */
1164 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1165 WRITESHORT(p
, i
); /* section id */
1166 saa_wbytes(s
, entry
, 16L);
1172 * Now the other local symbols.
1175 while ((sym
= saa_rstruct(syms
))) {
1176 if (sym
->type
& SYM_GLOBAL
)
1179 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1180 WRITELONG(p
, sym
->symv
.key
); /* value of symbol */
1181 WRITELONG(p
, sym
->size
); /* size of symbol */
1182 WRITECHAR(p
, sym
->type
); /* type and binding */
1183 WRITECHAR(p
, sym
->other
); /* visibility */
1184 WRITESHORT(p
, sym
->section
); /* index into section header table */
1185 saa_wbytes(s
, entry
, 16L);
1190 * dwarf needs symbols for debug sections
1191 * which are relocation targets.
1193 if (dfmt
== &df_dwarf
) {
1194 dwarf_infosym
= *local
;
1196 WRITELONG(p
, 0); /* no symbol name */
1197 WRITELONG(p
, 0); /* offset zero */
1198 WRITELONG(p
, 0); /* size zero */
1199 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1200 WRITESHORT(p
, sec_debug_info
); /* section id */
1201 saa_wbytes(s
, entry
, 16L);
1204 dwarf_abbrevsym
= *local
;
1206 WRITELONG(p
, 0); /* no symbol name */
1207 WRITELONG(p
, 0); /* offset zero */
1208 WRITELONG(p
, 0); /* size zero */
1209 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1210 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1211 saa_wbytes(s
, entry
, 16L);
1214 dwarf_linesym
= *local
;
1216 WRITELONG(p
, 0); /* no symbol name */
1217 WRITELONG(p
, 0); /* offset zero */
1218 WRITELONG(p
, 0); /* size zero */
1219 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1220 WRITESHORT(p
, sec_debug_line
); /* section id */
1221 saa_wbytes(s
, entry
, 16L);
1227 * Now the global symbols.
1230 while ((sym
= saa_rstruct(syms
))) {
1231 if (!(sym
->type
& SYM_GLOBAL
))
1234 WRITELONG(p
, sym
->strpos
);
1235 WRITELONG(p
, sym
->symv
.key
);
1236 WRITELONG(p
, sym
->size
);
1237 WRITECHAR(p
, sym
->type
); /* type and binding */
1238 WRITECHAR(p
, sym
->other
); /* visibility */
1239 WRITESHORT(p
, sym
->section
);
1240 saa_wbytes(s
, entry
, 16L);
1247 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
1250 uint8_t *p
, entry
[12];
1251 int32_t global_offset
;
1260 * How to onvert from a global placeholder to a real symbol index;
1261 * the +2 refers to the two special entries, the null entry and
1262 * the filename entry.
1264 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1267 int32_t sym
= r
->symbol
;
1269 if (sym
>= GLOBAL_TEMP_BASE
)
1270 sym
+= global_offset
;
1273 WRITELONG(p
, r
->address
);
1274 WRITELONG(p
, (sym
<< 8) + r
->type
);
1275 WRITELONG(p
, r
->offset
);
1276 saa_wbytes(s
, entry
, 12L);
1285 static void elf_section_header(int name
, int type
, uint64_t flags
,
1286 void *data
, bool is_saa
, uint64_t datalen
,
1287 int link
, int info
, int align
, int eltsize
)
1289 elf_sects
[elf_nsect
].data
= data
;
1290 elf_sects
[elf_nsect
].len
= datalen
;
1291 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1294 fwriteint32_t((int32_t)name
, ofile
);
1295 fwriteint32_t((int32_t)type
, ofile
);
1296 fwriteint32_t((int32_t)flags
, ofile
);
1297 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1298 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1299 fwriteint32_t(datalen
, ofile
);
1301 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1302 fwriteint32_t((int32_t)link
, ofile
);
1303 fwriteint32_t((int32_t)info
, ofile
);
1304 fwriteint32_t((int32_t)align
, ofile
);
1305 fwriteint32_t((int32_t)eltsize
, ofile
);
1308 static void elf_write_sections(void)
1311 for (i
= 0; i
< elf_nsect
; i
++)
1312 if (elf_sects
[i
].data
) {
1313 int32_t len
= elf_sects
[i
].len
;
1314 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1315 int32_t align
= reallen
- len
;
1316 if (elf_sects
[i
].is_saa
)
1317 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1319 nasm_write(elf_sects
[i
].data
, len
, ofile
);
1320 fwritezero(align
, ofile
);
1324 static void elf_sect_write(struct elf_section
*sect
, const void *data
, size_t len
)
1326 saa_wbytes(sect
->data
, data
, len
);
1330 static void elf_sect_writeaddr(struct elf_section
*sect
, int64_t data
, size_t len
)
1332 saa_writeaddr(sect
->data
, data
, len
);
1336 static void elf_sectalign(int32_t seg
, unsigned int value
)
1338 struct elf_section
*s
= NULL
;
1341 for (i
= 0; i
< nsects
; i
++) {
1342 if (sects
[i
]->index
== seg
) {
1347 if (!s
|| !is_power2(value
))
1350 if (value
> s
->align
)
1354 static int32_t elf_segbase(int32_t segment
)
1359 static void elf_filename(char *inname
, char *outname
)
1361 strcpy(elf_module
, inname
);
1362 standard_extension(inname
, outname
, ".o");
1365 extern macros_t elf_stdmac
[];
1367 static int elf_set_info(enum geninfo type
, char **val
)
1373 static const struct dfmt df_dwarf
= {
1374 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1378 null_debug_deflabel
,
1379 null_debug_directive
,
1384 static const struct dfmt df_stabs
= {
1385 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1389 null_debug_deflabel
,
1390 null_debug_directive
,
1396 static const struct dfmt
* const elfx32_debugs_arr
[3] =
1397 { &df_dwarf
, &df_stabs
, NULL
};
1399 const struct ofmt of_elfx32
= {
1400 "ELFX32 (x86_64) object files (e.g. Linux)",
1419 /* common debugging routines */
1420 static void debug_typevalue(int32_t type
)
1422 int32_t stype
, ssize
;
1423 switch (TYM_TYPE(type
)) {
1466 stype
= STT_SECTION
;
1481 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1482 lastsym
->size
= ssize
;
1483 lastsym
->type
= stype
;
1487 /* stabs debugging routines */
1489 static void stabs_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1492 if (!stabs_filename
) {
1493 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1494 strcpy(stabs_filename
, filename
);
1496 if (strcmp(stabs_filename
, filename
)) {
1497 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1498 in fact, this leak comes in quite handy to maintain a list of files
1499 encountered so far in the symbol lines... */
1501 /* why not nasm_free(stabs_filename); we're done with the old one */
1503 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1504 strcpy(stabs_filename
, filename
);
1508 currentline
= linenumber
;
1511 static void stabs_output(int type
, void *param
)
1513 struct symlininfo
*s
;
1514 struct linelist
*el
;
1515 if (type
== TY_DEBUGSYMLIN
) {
1516 if (debug_immcall
) {
1517 s
= (struct symlininfo
*)param
;
1518 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1519 return; /* line info is only collected for executable sections */
1521 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1522 el
->info
.offset
= s
->offset
;
1523 el
->info
.section
= s
->section
;
1524 el
->info
.name
= s
->name
;
1525 el
->line
= currentline
;
1526 el
->filename
= stabs_filename
;
1529 stabslines
->last
->next
= el
;
1530 stabslines
->last
= el
;
1533 stabslines
->last
= el
;
1540 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1542 static void stabs_generate(void)
1544 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1545 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1549 struct linelist
*ptr
;
1553 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
1556 if (numfiles
== 0) {
1557 allfiles
[0] = ptr
->filename
;
1560 for (i
= 0; i
< numfiles
; i
++) {
1561 if (!strcmp(allfiles
[i
], ptr
->filename
))
1564 if (i
>= numfiles
) {
1565 allfiles
[i
] = ptr
->filename
;
1572 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1573 for (i
= 0; i
< numfiles
; i
++) {
1574 fileidx
[i
] = strsize
;
1575 strsize
+= strlen(allfiles
[i
]) + 1;
1578 for (i
= 0; i
< numfiles
; i
++) {
1579 if (!strcmp(allfiles
[i
], elf_module
)) {
1586 * worst case size of the stab buffer would be:
1587 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1588 * plus one "ending" entry
1590 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1591 sizeof(struct stabentry
));
1592 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1593 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1596 for (i
= 0; i
< numfiles
; i
++)
1597 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1600 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1608 * this is the first stab, its strx points to the filename of the
1609 * the source-file, the n_desc field should be set to the number
1610 * of remaining stabs
1612 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, stabstrlen
);
1614 /* this is the stab for the main source file */
1615 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1617 /* relocation table entry */
1620 * Since the symbol table has two entries before
1621 * the section symbols, the index in the info.section
1622 * member must be adjusted by adding 2
1625 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1626 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1629 currfile
= mainfileindex
;
1633 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1634 /* oops file has changed... */
1635 for (i
= 0; i
< numfiles
; i
++)
1636 if (!strcmp(allfiles
[i
], ptr
->filename
))
1639 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1643 /* relocation table entry */
1645 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1646 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1649 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1652 /* relocation table entry */
1654 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1655 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
1661 /* this is an "ending" token */
1662 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1665 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1667 nasm_free(allfiles
);
1670 stablen
= (sptr
- sbuf
);
1671 stabrellen
= (rptr
- rbuf
);
1677 static void stabs_cleanup(void)
1679 struct linelist
*ptr
, *del
;
1691 nasm_free(stabrelbuf
);
1692 nasm_free(stabstrbuf
);
1695 /* dwarf routines */
1697 static void dwarf_init(void)
1699 ndebugs
= 3; /* 3 debug symbols */
1702 static void dwarf_linenum(const char *filename
, int32_t linenumber
,
1706 dwarf_findfile(filename
);
1708 currentline
= linenumber
;
1711 /* called from elf_out with type == TY_DEBUGSYMLIN */
1712 static void dwarf_output(int type
, void *param
)
1714 int ln
, aa
, inx
, maxln
, soc
;
1715 struct symlininfo
*s
;
1720 s
= (struct symlininfo
*)param
;
1722 /* line number info is only gathered for executable sections */
1723 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1726 /* Check if section index has changed */
1727 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1728 dwarf_findsect(s
->section
);
1730 /* do nothing unless line or file has changed */
1734 ln
= currentline
- dwarf_csect
->line
;
1735 aa
= s
->offset
- dwarf_csect
->offset
;
1736 inx
= dwarf_clist
->line
;
1737 plinep
= dwarf_csect
->psaa
;
1738 /* check for file change */
1739 if (!(inx
== dwarf_csect
->file
)) {
1740 saa_write8(plinep
,DW_LNS_set_file
);
1741 saa_write8(plinep
,inx
);
1742 dwarf_csect
->file
= inx
;
1744 /* check for line change */
1746 /* test if in range of special op code */
1747 maxln
= line_base
+ line_range
;
1748 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1749 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1750 saa_write8(plinep
,soc
);
1752 saa_write8(plinep
,DW_LNS_advance_line
);
1753 saa_wleb128s(plinep
,ln
);
1755 saa_write8(plinep
,DW_LNS_advance_pc
);
1756 saa_wleb128u(plinep
,aa
);
1759 dwarf_csect
->line
= currentline
;
1760 dwarf_csect
->offset
= s
->offset
;
1763 /* show change handled */
1768 static void dwarf_generate(void)
1772 struct linelist
*ftentry
;
1773 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1774 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1775 struct sectlist
*psect
;
1776 size_t saalen
, linepoff
, totlen
, highaddr
;
1778 /* write epilogues for each line program range */
1779 /* and build aranges section */
1780 paranges
= saa_init(1L);
1781 parangesrel
= saa_init(1L);
1782 saa_write16(paranges
,3); /* dwarf version */
1783 saa_write32(parangesrel
, paranges
->datalen
+4);
1784 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_X86_64_32
); /* reloc to info */
1785 saa_write32(parangesrel
, 0);
1786 saa_write32(paranges
,0); /* offset into info */
1787 saa_write8(paranges
,4); /* pointer size */
1788 saa_write8(paranges
,0); /* not segmented */
1789 saa_write32(paranges
,0); /* padding */
1790 /* iterate though sectlist entries */
1791 psect
= dwarf_fsect
;
1794 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1796 plinep
= psect
->psaa
;
1797 /* Line Number Program Epilogue */
1798 saa_write8(plinep
,2); /* std op 2 */
1799 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1800 saa_write8(plinep
,DW_LNS_extended_op
);
1801 saa_write8(plinep
,1); /* operand length */
1802 saa_write8(plinep
,DW_LNE_end_sequence
);
1803 totlen
+= plinep
->datalen
;
1804 /* range table relocation entry */
1805 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1806 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_X86_64_32
);
1807 saa_write32(parangesrel
, (uint32_t) 0);
1808 /* range table entry */
1809 saa_write32(paranges
,0x0000); /* range start */
1810 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1811 highaddr
+= sects
[psect
->section
]->len
;
1812 /* done with this entry */
1813 psect
= psect
->next
;
1815 saa_write32(paranges
,0); /* null address */
1816 saa_write32(paranges
,0); /* null length */
1817 saalen
= paranges
->datalen
;
1818 arangeslen
= saalen
+ 4;
1819 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1820 WRITELONG(pbuf
,saalen
); /* initial length */
1821 saa_rnbytes(paranges
, pbuf
, saalen
);
1824 /* build rela.aranges section */
1825 arangesrellen
= saalen
= parangesrel
->datalen
;
1826 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1827 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1828 saa_free(parangesrel
);
1830 /* build pubnames section */
1831 ppubnames
= saa_init(1L);
1832 saa_write16(ppubnames
,3); /* dwarf version */
1833 saa_write32(ppubnames
,0); /* offset into info */
1834 saa_write32(ppubnames
,0); /* space used in info */
1835 saa_write32(ppubnames
,0); /* end of list */
1836 saalen
= ppubnames
->datalen
;
1837 pubnameslen
= saalen
+ 4;
1838 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1839 WRITELONG(pbuf
,saalen
); /* initial length */
1840 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1841 saa_free(ppubnames
);
1843 /* build info section */
1844 pinfo
= saa_init(1L);
1845 pinforel
= saa_init(1L);
1846 saa_write16(pinfo
,3); /* dwarf version */
1847 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1848 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_X86_64_32
); /* reloc to abbrev */
1849 saa_write32(pinforel
, 0);
1850 saa_write32(pinfo
,0); /* offset into abbrev */
1851 saa_write8(pinfo
,4); /* pointer size */
1852 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1853 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1854 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1855 saa_write32(pinforel
, 0);
1856 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1857 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1858 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1859 saa_write32(pinforel
, 0);
1860 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1861 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1862 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_X86_64_32
); /* reloc to line */
1863 saa_write32(pinforel
, 0);
1864 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1865 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1866 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1867 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1868 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1869 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1870 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
1871 saa_write32(pinforel
, 0);
1872 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1873 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1874 saa_write8(pinfo
,0); /* end of entries */
1875 saalen
= pinfo
->datalen
;
1876 infolen
= saalen
+ 4;
1877 infobuf
= pbuf
= nasm_malloc(infolen
);
1878 WRITELONG(pbuf
,saalen
); /* initial length */
1879 saa_rnbytes(pinfo
, pbuf
, saalen
);
1882 /* build rela.info section */
1883 inforellen
= saalen
= pinforel
->datalen
;
1884 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1885 saa_rnbytes(pinforel
, pbuf
, saalen
);
1888 /* build abbrev section */
1889 pabbrev
= saa_init(1L);
1890 saa_write8(pabbrev
,1); /* entry number LEB128u */
1891 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1892 saa_write8(pabbrev
,1); /* has children */
1893 /* the following attributes and forms are all LEB128u values */
1894 saa_write8(pabbrev
,DW_AT_low_pc
);
1895 saa_write8(pabbrev
,DW_FORM_addr
);
1896 saa_write8(pabbrev
,DW_AT_high_pc
);
1897 saa_write8(pabbrev
,DW_FORM_addr
);
1898 saa_write8(pabbrev
,DW_AT_stmt_list
);
1899 saa_write8(pabbrev
,DW_FORM_data4
);
1900 saa_write8(pabbrev
,DW_AT_name
);
1901 saa_write8(pabbrev
,DW_FORM_string
);
1902 saa_write8(pabbrev
,DW_AT_producer
);
1903 saa_write8(pabbrev
,DW_FORM_string
);
1904 saa_write8(pabbrev
,DW_AT_language
);
1905 saa_write8(pabbrev
,DW_FORM_data2
);
1906 saa_write16(pabbrev
,0); /* end of entry */
1907 /* LEB128u usage same as above */
1908 saa_write8(pabbrev
,2); /* entry number */
1909 saa_write8(pabbrev
,DW_TAG_subprogram
);
1910 saa_write8(pabbrev
,0); /* no children */
1911 saa_write8(pabbrev
,DW_AT_low_pc
);
1912 saa_write8(pabbrev
,DW_FORM_addr
);
1913 saa_write8(pabbrev
,DW_AT_frame_base
);
1914 saa_write8(pabbrev
,DW_FORM_data4
);
1915 saa_write16(pabbrev
,0); /* end of entry */
1916 abbrevlen
= saalen
= pabbrev
->datalen
;
1917 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
1918 saa_rnbytes(pabbrev
, pbuf
, saalen
);
1921 /* build line section */
1923 plines
= saa_init(1L);
1924 saa_write8(plines
,1); /* Minimum Instruction Length */
1925 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
1926 saa_write8(plines
,line_base
); /* Line Base */
1927 saa_write8(plines
,line_range
); /* Line Range */
1928 saa_write8(plines
,opcode_base
); /* Opcode Base */
1929 /* standard opcode lengths (# of LEB128u operands) */
1930 saa_write8(plines
,0); /* Std opcode 1 length */
1931 saa_write8(plines
,1); /* Std opcode 2 length */
1932 saa_write8(plines
,1); /* Std opcode 3 length */
1933 saa_write8(plines
,1); /* Std opcode 4 length */
1934 saa_write8(plines
,1); /* Std opcode 5 length */
1935 saa_write8(plines
,0); /* Std opcode 6 length */
1936 saa_write8(plines
,0); /* Std opcode 7 length */
1937 saa_write8(plines
,0); /* Std opcode 8 length */
1938 saa_write8(plines
,1); /* Std opcode 9 length */
1939 saa_write8(plines
,0); /* Std opcode 10 length */
1940 saa_write8(plines
,0); /* Std opcode 11 length */
1941 saa_write8(plines
,1); /* Std opcode 12 length */
1942 /* Directory Table */
1943 saa_write8(plines
,0); /* End of table */
1944 /* File Name Table */
1945 ftentry
= dwarf_flist
;
1946 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
1947 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
1948 saa_write8(plines
,0); /* directory LEB128u */
1949 saa_write8(plines
,0); /* time LEB128u */
1950 saa_write8(plines
,0); /* size LEB128u */
1951 ftentry
= ftentry
->next
;
1953 saa_write8(plines
,0); /* End of table */
1954 linepoff
= plines
->datalen
;
1955 linelen
= linepoff
+ totlen
+ 10;
1956 linebuf
= pbuf
= nasm_malloc(linelen
);
1957 WRITELONG(pbuf
,linelen
-4); /* initial length */
1958 WRITESHORT(pbuf
,3); /* dwarf version */
1959 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
1960 /* write line header */
1962 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
1965 /* concatonate line program ranges */
1967 plinesrel
= saa_init(1L);
1968 psect
= dwarf_fsect
;
1969 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
1970 saa_write32(plinesrel
, linepoff
);
1971 saa_write32(plinesrel
, ((psect
->section
+ 2) << 8) + R_X86_64_32
);
1972 saa_write32(plinesrel
, 0);
1973 plinep
= psect
->psaa
;
1974 saalen
= plinep
->datalen
;
1975 saa_rnbytes(plinep
, pbuf
, saalen
);
1979 /* done with this entry */
1980 psect
= psect
->next
;
1984 /* build rela.lines section */
1985 linerellen
=saalen
= plinesrel
->datalen
;
1986 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
1987 saa_rnbytes(plinesrel
, pbuf
, saalen
);
1988 saa_free(plinesrel
);
1990 /* build frame section */
1992 framebuf
= pbuf
= nasm_malloc(framelen
);
1993 WRITELONG(pbuf
,framelen
-4); /* initial length */
1995 /* build loc section */
1997 locbuf
= pbuf
= nasm_malloc(loclen
);
1998 WRITELONG(pbuf
,0); /* null beginning offset */
1999 WRITELONG(pbuf
,0); /* null ending offset */
2002 static void dwarf_cleanup(void)
2004 nasm_free(arangesbuf
);
2005 nasm_free(arangesrelbuf
);
2006 nasm_free(pubnamesbuf
);
2008 nasm_free(inforelbuf
);
2009 nasm_free(abbrevbuf
);
2011 nasm_free(linerelbuf
);
2012 nasm_free(framebuf
);
2016 static void dwarf_findfile(const char * fname
)
2019 struct linelist
*match
;
2021 /* return if fname is current file name */
2022 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2025 /* search for match */
2028 match
= dwarf_flist
;
2029 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2030 if (!(strcmp(fname
, match
->filename
))) {
2031 dwarf_clist
= match
;
2037 /* add file name to end of list */
2038 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2040 dwarf_clist
->line
= dwarf_numfiles
;
2041 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2042 strcpy(dwarf_clist
->filename
,fname
);
2043 dwarf_clist
->next
= 0;
2044 if (!dwarf_flist
) { /* if first entry */
2045 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2046 dwarf_clist
->last
= 0;
2047 } else { /* chain to previous entry */
2048 dwarf_elist
->next
= dwarf_clist
;
2049 dwarf_elist
= dwarf_clist
;
2053 static void dwarf_findsect(const int index
)
2056 struct sectlist
*match
;
2059 /* return if index is current section index */
2060 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2063 /* search for match */
2066 match
= dwarf_fsect
;
2067 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2068 if (match
->section
== index
) {
2069 dwarf_csect
= match
;
2072 match
= match
->next
;
2076 /* add entry to end of list */
2077 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2079 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2080 dwarf_csect
->line
= 1;
2081 dwarf_csect
->offset
= 0;
2082 dwarf_csect
->file
= 1;
2083 dwarf_csect
->section
= index
;
2084 dwarf_csect
->next
= 0;
2085 /* set relocatable address at start of line program */
2086 saa_write8(plinep
,DW_LNS_extended_op
);
2087 saa_write8(plinep
,5); /* operand length */
2088 saa_write8(plinep
,DW_LNE_set_address
);
2089 saa_write32(plinep
,0); /* Start Address */
2091 if (!dwarf_fsect
) { /* if first entry */
2092 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2093 dwarf_csect
->last
= 0;
2094 } else { /* chain to previous entry */
2095 dwarf_esect
->next
= dwarf_csect
;
2096 dwarf_esect
= dwarf_csect
;
2100 #endif /* OF_ELFX32 */