1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 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 * Common code for outelf32 and outelf64
59 #if defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32)
62 static struct elf_section
**sects
;
63 static int nsects
, sectlen
;
65 #define SHSTR_DELTA 256
66 static char *shstrtab
;
67 static int shstrtablen
, shstrtabsize
;
69 static struct SAA
*syms
;
70 static uint32_t nlocals
, nglobs
, ndebugs
; /* Symbol counts */
72 static int32_t def_seg
;
74 static struct RAA
*bsym
;
76 static struct SAA
*strs
;
77 static uint32_t strslen
;
79 static struct elf_symbol
*fwds
;
81 static char elf_module
[FILENAME_MAX
];
83 extern const struct ofmt of_elf32
;
84 extern const struct ofmt of_elf64
;
85 extern const struct ofmt of_elfx32
;
87 static struct ELF_SECTDATA
{
93 static int elf_nsect
, nsections
;
94 static int64_t elf_foffs
;
96 static void elf_write(void);
97 static void elf_sect_write(struct elf_section
*, const void *, size_t);
98 static void elf_sect_writeaddr(struct elf_section
*, int64_t, size_t);
99 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
101 static void elf_write_sections(void);
102 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
103 static struct SAA
*elf_build_reltab(uint64_t *, struct elf_reloc
*);
104 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 int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
158 static struct elf_symbol
*lastsym
;
160 /* common debugging routines */
161 static void debug_typevalue(int32_t);
163 /* stabs debugging routines */
164 static void stabs_linenum(const char *filename
, int32_t linenumber
, int32_t);
165 static void stabs_output(int, void *);
166 static void stabs_generate(void);
167 static void stabs_cleanup(void);
169 /* dwarf debugging routines */
170 static void dwarf_init(void);
171 static void dwarf_linenum(const char *filename
, int32_t linenumber
, int32_t);
172 static void dwarf_output(int, void *);
173 static void dwarf_generate(void);
174 static void dwarf_cleanup(void);
175 static void dwarf_findfile(const char *);
176 static void dwarf_findsect(const int);
178 static bool is_elf64(void);
179 static bool is_elf32(void);
180 static bool is_elfx32(void);
182 static bool dfmt_is_stabs(void);
183 static bool dfmt_is_dwarf(void);
186 * Special NASM section numbers which are used to define ELF special
189 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
190 static int32_t elf_got_sect
, elf_plt_sect
;
191 static int32_t elf_sym_sect
, elf_gottpoff_sect
, elf_tlsie_sect
;
193 uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
194 uint8_t elf_abiver
= 0; /* Current ABI version */
196 const struct elf_known_section elf_known_sections
[] = {
197 { ".text", SHT_PROGBITS
, SHF_ALLOC
|SHF_EXECINSTR
, 16 },
198 { ".rodata", SHT_PROGBITS
, SHF_ALLOC
, 4 },
199 { ".lrodata", SHT_PROGBITS
, SHF_ALLOC
, 4 },
200 { ".data", SHT_PROGBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
201 { ".ldata", SHT_PROGBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
202 { ".bss", SHT_NOBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
203 { ".lbss", SHT_NOBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
204 { ".tdata", SHT_PROGBITS
, SHF_ALLOC
|SHF_WRITE
|SHF_TLS
, 4 },
205 { ".tbss", SHT_NOBITS
, SHF_ALLOC
|SHF_WRITE
|SHF_TLS
, 4 },
206 { ".comment", SHT_PROGBITS
, 0, 1 },
207 { NULL
, SHT_PROGBITS
, SHF_ALLOC
, 1 } /* default */
210 /* parse section attributes */
211 void elf_section_attrib(char *name
, char *attr
, int pass
,
212 uint32_t *flags_and
, uint32_t *flags_or
,
213 uint64_t *align
, int *type
)
215 char *opt
, *val
, *next
;
217 opt
= nasm_skip_spaces(attr
);
221 while ((opt
= nasm_opt_val(opt
, &val
, &next
))) {
222 if (!nasm_stricmp(opt
, "align")) {
224 nasm_error(ERR_NONFATAL
,
225 "section align without value specified");
230 } else if (!is_power2(*align
)) {
231 nasm_error(ERR_NONFATAL
,
232 "section alignment %"PRId64
" is not a power of two",
237 } else if (!nasm_stricmp(opt
, "alloc")) {
238 *flags_and
|= SHF_ALLOC
;
239 *flags_or
|= SHF_ALLOC
;
240 } else if (!nasm_stricmp(opt
, "noalloc")) {
241 *flags_and
|= SHF_ALLOC
;
242 *flags_or
&= ~SHF_ALLOC
;
243 } else if (!nasm_stricmp(opt
, "exec")) {
244 *flags_and
|= SHF_EXECINSTR
;
245 *flags_or
|= SHF_EXECINSTR
;
246 } else if (!nasm_stricmp(opt
, "noexec")) {
247 *flags_and
|= SHF_EXECINSTR
;
248 *flags_or
&= ~SHF_EXECINSTR
;
249 } else if (!nasm_stricmp(opt
, "write")) {
250 *flags_and
|= SHF_WRITE
;
251 *flags_or
|= SHF_WRITE
;
252 } else if (!nasm_stricmp(opt
, "tls")) {
253 *flags_and
|= SHF_TLS
;
254 *flags_or
|= SHF_TLS
;
255 } else if (!nasm_stricmp(opt
, "nowrite")) {
256 *flags_and
|= SHF_WRITE
;
257 *flags_or
&= ~SHF_WRITE
;
258 } else if (!nasm_stricmp(opt
, "progbits")) {
259 *type
= SHT_PROGBITS
;
260 } else if (!nasm_stricmp(opt
, "nobits")) {
262 } else if (pass
== 1) {
263 nasm_error(ERR_WARNING
,
264 "Unknown section attribute '%s' ignored on"
265 " declaration of section `%s'", opt
, name
);
271 int elf_directive(enum directives directive
, char *value
, int pass
)
280 return 1; /* ignore in pass 2 */
282 n
= readnum(value
, &err
);
284 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
288 if (n
< 0 || n
> 255) {
289 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
296 p
= strchr(value
,',');
300 n
= readnum(p
+ 1, &err
);
301 if (err
|| n
< 0 || n
> 255) {
302 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
314 static void elf_init(void)
317 nsects
= sectlen
= 0;
318 syms
= saa_init((int32_t)sizeof(struct elf_symbol
));
319 nlocals
= nglobs
= ndebugs
= 0;
322 saa_wbytes(strs
, "\0", 1L);
323 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
324 strslen
= 2 + strlen(elf_module
);
326 shstrtablen
= shstrtabsize
= 0;;
327 add_sectname("", "");
332 * FIXME: tlsie is Elf32 only and
333 * gottpoff is Elfx32|64 only.
336 elf_gotpc_sect
= seg_alloc();
337 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
338 elf_gotoff_sect
= seg_alloc();
339 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
340 elf_got_sect
= seg_alloc();
341 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
342 elf_plt_sect
= seg_alloc();
343 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
344 elf_sym_sect
= seg_alloc();
345 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
346 elf_gottpoff_sect
= seg_alloc();
347 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
348 elf_tlsie_sect
= seg_alloc();
349 define_label("..tlsie", elf_tlsie_sect
+ 1, 0L, NULL
, false, false);
351 def_seg
= seg_alloc();
354 static void elf_cleanup(void)
360 for (i
= 0; i
< nsects
; i
++) {
361 if (sects
[i
]->type
!= SHT_NOBITS
)
362 saa_free(sects
[i
]->data
);
364 saa_free(sects
[i
]->rel
);
365 while (sects
[i
]->head
) {
367 sects
[i
]->head
= sects
[i
]->head
->next
;
378 /* add entry to the elf .shstrtab section */
379 static void add_sectname(char *firsthalf
, char *secondhalf
)
381 int len
= strlen(firsthalf
) + strlen(secondhalf
);
382 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
383 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
384 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
385 strcat(shstrtab
+ shstrtablen
, secondhalf
);
386 shstrtablen
+= len
+ 1;
389 static int elf_make_section(char *name
, int type
, int flags
, int align
)
391 struct elf_section
*s
;
393 s
= nasm_zalloc(sizeof(*s
));
395 if (type
!= SHT_NOBITS
)
396 s
->data
= saa_init(1L);
398 if (!strcmp(name
, ".text"))
401 s
->index
= seg_alloc();
402 add_sectname("", name
);
404 s
->name
= nasm_strdup(name
);
409 if (nsects
>= sectlen
)
410 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
416 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
419 uint32_t flags
, flags_and
, flags_or
;
424 *bits
= ofmt
->maxbits
;
428 p
= nasm_skip_word(name
);
431 flags_and
= flags_or
= type
= align
= 0;
433 elf_section_attrib(name
, p
, pass
, &flags_and
,
434 &flags_or
, &align
, &type
);
436 if (!strcmp(name
, ".shstrtab") ||
437 !strcmp(name
, ".symtab") ||
438 !strcmp(name
, ".strtab")) {
439 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
444 for (i
= 0; i
< nsects
; i
++)
445 if (!strcmp(name
, sects
[i
]->name
))
448 const struct elf_known_section
*ks
= elf_known_sections
;
451 if (!strcmp(name
, ks
->name
))
456 type
= type
? type
: ks
->type
;
457 align
= align
? align
: ks
->align
;
458 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
460 i
= elf_make_section(name
, type
, flags
, align
);
461 } else if (pass
== 1) {
462 if ((type
&& sects
[i
]->type
!= type
)
463 || (align
&& sects
[i
]->align
!= align
)
464 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
465 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
466 " redeclaration of section `%s'", name
);
469 return sects
[i
]->index
;
472 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
473 int is_global
, char *special
)
476 struct elf_symbol
*sym
;
477 bool special_used
= false;
479 #if defined(DEBUG) && DEBUG>2
480 nasm_error(ERR_DEBUG
,
481 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
482 name
, segment
, offset
, is_global
, special
);
484 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
486 * This is a NASM special symbol. We never allow it into
487 * the ELF symbol table, even if it's a valid one. If it
488 * _isn't_ a valid one, we should barf immediately.
490 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
492 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
493 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
494 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff") &&
495 strcmp(name
, "..tlsie"))
496 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
500 if (is_global
== 3) {
501 struct elf_symbol
**s
;
503 * Fix up a forward-reference symbol size from the first
506 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
507 if (!strcmp((*s
)->name
, name
)) {
508 struct tokenval tokval
;
510 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
514 tokval
.t_type
= TOKEN_INVALID
;
515 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, NULL
);
518 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
519 " expression as symbol size");
521 (*s
)->size
= reloc_value(e
);
525 * Remove it from the list of unresolved sizes.
527 nasm_free((*s
)->name
);
531 return; /* it wasn't an important one */
534 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
535 strslen
+= 1 + strlen(name
);
537 lastsym
= sym
= saa_wstruct(syms
);
539 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
542 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
543 sym
->other
= STV_DEFAULT
;
545 if (segment
== NO_SEG
)
546 sym
->section
= SHN_ABS
;
549 sym
->section
= SHN_UNDEF
;
550 if (segment
== def_seg
) {
551 /* we have to be sure at least text section is there */
553 if (segment
!= elf_section_names(".text", 2, &tempint
))
554 nasm_panic(0, "strange segment conditions in ELF driver");
556 for (i
= 0; i
< nsects
; i
++) {
557 if (segment
== sects
[i
]->index
) {
558 sym
->section
= i
+ 1;
564 if (is_global
== 2) {
567 sym
->section
= SHN_COMMON
;
569 * We have a common variable. Check the special text to see
570 * if it's a valid number and power of two; if so, store it
571 * as the alignment for the common variable.
575 sym
->symv
.key
= readnum(special
, &err
);
577 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
578 " valid number", special
);
579 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
580 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
581 " power of two", special
);
585 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
587 if (sym
->type
== SYM_GLOBAL
) {
589 * If sym->section == SHN_ABS, then the first line of the
590 * else section would cause a core dump, because its a reference
591 * beyond the end of the section array.
592 * This behaviour is exhibited by this code:
595 * To avoid such a crash, such requests are silently discarded.
596 * This may not be the best solution.
598 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
599 bsym
= raa_write(bsym
, segment
, nglobs
);
600 } else if (sym
->section
!= SHN_ABS
) {
602 * This is a global symbol; so we must add it to the rbtree
603 * of global symbols in its section.
605 * In addition, we check the special text for symbol
606 * type and size information.
608 sects
[sym
->section
-1]->gsyms
=
609 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
612 int n
= strcspn(special
, " \t");
614 if (!nasm_strnicmp(special
, "function", n
))
615 sym
->type
|= STT_FUNC
;
616 else if (!nasm_strnicmp(special
, "data", n
) ||
617 !nasm_strnicmp(special
, "object", n
))
618 sym
->type
|= STT_OBJECT
;
619 else if (!nasm_strnicmp(special
, "notype", n
))
620 sym
->type
|= STT_NOTYPE
;
622 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
626 special
= nasm_skip_spaces(special
);
628 n
= strcspn(special
, " \t");
629 if (!nasm_strnicmp(special
, "default", n
))
630 sym
->other
= STV_DEFAULT
;
631 else if (!nasm_strnicmp(special
, "internal", n
))
632 sym
->other
= STV_INTERNAL
;
633 else if (!nasm_strnicmp(special
, "hidden", n
))
634 sym
->other
= STV_HIDDEN
;
635 else if (!nasm_strnicmp(special
, "protected", n
))
636 sym
->other
= STV_PROTECTED
;
643 struct tokenval tokval
;
646 char *saveme
= stdscan_get();
648 while (special
[n
] && nasm_isspace(special
[n
]))
651 * We have a size expression; attempt to
655 stdscan_set(special
+ n
);
656 tokval
.t_type
= TOKEN_INVALID
;
657 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, NULL
);
661 sym
->name
= nasm_strdup(name
);
664 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
665 " expression as symbol size");
667 sym
->size
= reloc_value(e
);
674 * If TLS segment, mark symbol accordingly.
676 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
678 sym
->type
|= STT_TLS
;
681 sym
->globnum
= nglobs
;
686 if (special
&& !special_used
)
687 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
690 static void elf_add_reloc(struct elf_section
*sect
, int32_t segment
,
691 int64_t offset
, int type
)
695 r
= *sect
->tail
= nasm_zalloc(sizeof(struct elf_reloc
));
696 sect
->tail
= &r
->next
;
698 r
->address
= sect
->len
;
701 if (segment
!= NO_SEG
) {
703 for (i
= 0; i
< nsects
; i
++)
704 if (segment
== sects
[i
]->index
)
707 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
715 * This routine deals with ..got and ..sym relocations: the more
716 * complicated kinds. In shared-library writing, some relocations
717 * with respect to global symbols must refer to the precise symbol
718 * rather than referring to an offset from the base of the section
719 * _containing_ the symbol. Such relocations call to this routine,
720 * which searches the symbol list for the symbol in question.
722 * R_386_GOT32 | R_X86_64_GOT32 references require the _exact_ symbol address to be
723 * used; R_386_32 | R_X86_64_32 references can be at an offset from the symbol.
724 * The boolean argument `exact' tells us this.
726 * Return value is the adjusted value of `addr', having become an
727 * offset from the symbol rather than the section. Should always be
728 * zero when returning from an exact call.
730 * Limitation: if you define two symbols at the same place,
731 * confusion will occur.
733 * Inefficiency: we search, currently, using a linked list which
734 * isn't even necessarily sorted.
736 static int64_t elf_add_gsym_reloc(struct elf_section
*sect
,
737 int32_t segment
, uint64_t offset
,
738 int64_t pcrel
, int type
, bool exact
)
741 struct elf_section
*s
;
742 struct elf_symbol
*sym
;
747 * First look up the segment/offset pair and find a global
748 * symbol corresponding to it. If it's not one of our segments,
749 * then it must be an external symbol, in which case we're fine
750 * doing a normal elf_add_reloc after first sanity-checking
751 * that the offset from the symbol is zero.
754 for (i
= 0; i
< nsects
; i
++)
755 if (segment
== sects
[i
]->index
) {
762 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
764 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
768 srb
= rb_search(s
->gsyms
, offset
);
769 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
770 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
771 " for this reference");
774 sym
= container_of(srb
, struct elf_symbol
, symv
);
776 r
= *sect
->tail
= nasm_malloc(sizeof(struct elf_reloc
));
777 sect
->tail
= &r
->next
;
780 r
->address
= sect
->len
;
781 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
782 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
789 static void elf32_out(int32_t segto
, const void *data
,
790 enum out_type type
, uint64_t size
,
791 int32_t segment
, int32_t wrt
)
793 struct elf_section
*s
;
797 static struct symlininfo sinfo
;
800 * handle absolute-assembly (structure definitions)
802 if (segto
== NO_SEG
) {
803 if (type
!= OUT_RESERVE
)
804 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
810 for (i
= 0; i
< nsects
; i
++)
811 if (segto
== sects
[i
]->index
) {
816 int tempint
; /* ignored */
817 if (segto
!= elf_section_names(".text", 2, &tempint
))
818 nasm_panic(0, "strange segment conditions in ELF driver");
820 s
= sects
[nsects
- 1];
825 /* again some stabs debugging stuff */
826 sinfo
.offset
= s
->len
;
829 sinfo
.name
= s
->name
;
830 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
831 /* end of debugging stuff */
833 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
834 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
835 " BSS section `%s': ignored", s
->name
);
836 s
->len
+= realsize(type
, size
);
842 if (s
->type
== SHT_PROGBITS
) {
843 nasm_error(ERR_WARNING
, "uninitialized space declared in"
844 " non-BSS section `%s': zeroing", s
->name
);
845 elf_sect_write(s
, NULL
, size
);
851 if (segment
!= NO_SEG
)
852 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
853 elf_sect_write(s
, data
, size
);
859 int asize
= abs((int)size
);
861 addr
= *(int64_t *)data
;
862 if (segment
!= NO_SEG
) {
864 nasm_error(ERR_NONFATAL
, "ELF format does not support"
865 " segment base references");
869 * The if() is a hack to deal with compilers which
870 * don't handle switch() statements with 64-bit
876 elf_add_reloc(s
, segment
, 0, R_386_8
);
880 elf_add_reloc(s
, segment
, 0, R_386_16
);
883 elf_add_reloc(s
, segment
, 0, R_386_32
);
885 default: /* Error issued further down */
888 } else if (wrt
== elf_gotpc_sect
+ 1) {
890 * The user will supply GOT relative to $$. ELF
891 * will let us have GOT relative to $. So we
892 * need to fix up the data item by $-$$.
895 elf_add_reloc(s
, segment
, 0, R_386_GOTPC
);
896 } else if (wrt
== elf_gotoff_sect
+ 1) {
897 elf_add_reloc(s
, segment
, 0, R_386_GOTOFF
);
898 } else if (wrt
== elf_tlsie_sect
+ 1) {
899 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
901 } else if (wrt
== elf_got_sect
+ 1) {
902 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
904 } else if (wrt
== elf_sym_sect
+ 1) {
908 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
913 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
917 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
923 } else if (wrt
== elf_plt_sect
+ 1) {
924 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
925 "relative PLT references");
927 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
929 wrt
= NO_SEG
; /* we can at least _try_ to continue */
935 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
936 "8- or 16-bit relocations in ELF32 is a GNU extension");
937 } else if (asize
!= 4 && segment
!= NO_SEG
) {
938 nasm_error(ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
940 elf_sect_writeaddr(s
, addr
, asize
);
949 reltype
= R_386_PC16
;
954 addr
= *(int64_t *)data
- size
;
955 nasm_assert(segment
!= segto
);
956 if (segment
!= NO_SEG
&& segment
% 2) {
957 nasm_error(ERR_NONFATAL
, "ELF format does not support"
958 " segment base references");
961 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
962 "8- or 16-bit relocations in ELF is a GNU extension");
963 elf_add_reloc(s
, segment
, 0, reltype
);
965 nasm_error(ERR_NONFATAL
,
966 "Unsupported non-32-bit ELF relocation");
969 elf_sect_writeaddr(s
, addr
, bytes
);
973 addr
= *(int64_t *)data
- size
;
974 if (segment
== segto
)
975 nasm_panic(0, "intra-segment OUT_REL4ADR");
976 if (segment
!= NO_SEG
&& segment
% 2) {
977 nasm_error(ERR_NONFATAL
, "ELF format does not support"
978 " segment base references");
981 elf_add_reloc(s
, segment
, 0, R_386_PC32
);
982 } else if (wrt
== elf_plt_sect
+ 1) {
983 elf_add_reloc(s
, segment
, 0, R_386_PLT32
);
984 } else if (wrt
== elf_gotpc_sect
+ 1 ||
985 wrt
== elf_gotoff_sect
+ 1 ||
986 wrt
== elf_got_sect
+ 1) {
987 nasm_error(ERR_NONFATAL
, "ELF format cannot produce PC-"
988 "relative GOT references");
990 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
992 wrt
= NO_SEG
; /* we can at least _try_ to continue */
995 elf_sect_writeaddr(s
, addr
, 4);
999 nasm_error(ERR_NONFATAL
, "32-bit ELF format does not support 64-bit relocations");
1001 elf_sect_writeaddr(s
, addr
, 8);
1005 static void elf64_out(int32_t segto
, const void *data
,
1006 enum out_type type
, uint64_t size
,
1007 int32_t segment
, int32_t wrt
)
1009 struct elf_section
*s
;
1013 static struct symlininfo sinfo
;
1016 * handle absolute-assembly (structure definitions)
1018 if (segto
== NO_SEG
) {
1019 if (type
!= OUT_RESERVE
)
1020 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
1026 for (i
= 0; i
< nsects
; i
++)
1027 if (segto
== sects
[i
]->index
) {
1032 int tempint
; /* ignored */
1033 if (segto
!= elf_section_names(".text", 2, &tempint
))
1034 nasm_panic(0, "strange segment conditions in ELF driver");
1036 s
= sects
[nsects
- 1];
1041 /* again some stabs debugging stuff */
1042 sinfo
.offset
= s
->len
;
1044 sinfo
.segto
= segto
;
1045 sinfo
.name
= s
->name
;
1046 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
1047 /* end of debugging stuff */
1049 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
1050 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
1051 " BSS section `%s': ignored", s
->name
);
1052 s
->len
+= realsize(type
, size
);
1058 if (s
->type
== SHT_PROGBITS
) {
1059 nasm_error(ERR_WARNING
, "uninitialized space declared in"
1060 " non-BSS section `%s': zeroing", s
->name
);
1061 elf_sect_write(s
, NULL
, size
);
1067 if (segment
!= NO_SEG
)
1068 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1069 elf_sect_write(s
, data
, size
);
1074 int isize
= (int)size
;
1075 int asize
= abs((int)size
);
1077 addr
= *(int64_t *)data
;
1078 if (segment
== NO_SEG
) {
1080 } else if (segment
% 2) {
1081 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1082 " segment base references");
1084 if (wrt
== NO_SEG
) {
1088 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
1092 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
1095 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
1098 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
1102 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
1105 nasm_panic(0, "internal error elf64-hpa-871");
1109 } else if (wrt
== elf_gotpc_sect
+ 1) {
1111 * The user will supply GOT relative to $$. ELF
1112 * will let us have GOT relative to $. So we
1113 * need to fix up the data item by $-$$.
1116 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1118 } else if (wrt
== elf_gotoff_sect
+ 1) {
1120 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
1121 "references to be qword");
1123 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
1126 } else if (wrt
== elf_got_sect
+ 1) {
1129 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1130 R_X86_64_GOT32
, true);
1134 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1135 R_X86_64_GOT64
, true);
1139 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
1142 } else if (wrt
== elf_sym_sect
+ 1) {
1146 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1152 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1153 R_X86_64_16
, false);
1157 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1158 R_X86_64_32
, false);
1162 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1163 R_X86_64_32S
, false);
1168 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1169 R_X86_64_64
, false);
1173 nasm_panic(0, "internal error elf64-hpa-903");
1176 } else if (wrt
== elf_plt_sect
+ 1) {
1177 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1178 "relative PLT references");
1180 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
1184 elf_sect_writeaddr(s
, addr
, asize
);
1189 reltype
= R_X86_64_PC8
;
1194 reltype
= R_X86_64_PC16
;
1199 addr
= *(int64_t *)data
- size
;
1200 if (segment
== segto
)
1201 nasm_panic(0, "intra-segment OUT_REL1ADR");
1202 if (segment
== NO_SEG
) {
1204 } else if (segment
% 2) {
1205 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1206 " segment base references");
1208 if (wrt
== NO_SEG
) {
1209 elf_add_reloc(s
, segment
, addr
, reltype
);
1212 nasm_error(ERR_NONFATAL
,
1213 "Unsupported non-32-bit ELF relocation");
1216 elf_sect_writeaddr(s
, addr
, bytes
);
1220 addr
= *(int64_t *)data
- size
;
1221 if (segment
== segto
)
1222 nasm_panic(0, "intra-segment OUT_REL4ADR");
1223 if (segment
== NO_SEG
) {
1225 } else if (segment
% 2) {
1226 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
1227 " segment base references");
1229 if (wrt
== NO_SEG
) {
1230 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1232 } else if (wrt
== elf_plt_sect
+ 1) {
1233 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1234 R_X86_64_PLT32
, true);
1236 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1237 wrt
== elf_got_sect
+ 1) {
1238 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1239 R_X86_64_GOTPCREL
, true);
1241 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1242 wrt
== elf_got_sect
+ 1) {
1243 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1245 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1246 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1247 R_X86_64_GOTTPOFF
, true);
1250 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1254 elf_sect_writeaddr(s
, addr
, 4);
1258 addr
= *(int64_t *)data
- size
;
1259 if (segment
== segto
)
1260 nasm_panic(0, "intra-segment OUT_REL8ADR");
1261 if (segment
== NO_SEG
) {
1263 } else if (segment
% 2) {
1264 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
1265 " segment base references");
1267 if (wrt
== NO_SEG
) {
1268 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1270 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1271 wrt
== elf_got_sect
+ 1) {
1272 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1273 R_X86_64_GOTPCREL64
, true);
1275 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1276 wrt
== elf_got_sect
+ 1) {
1277 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1279 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1280 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1283 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1287 elf_sect_writeaddr(s
, addr
, 8);
1292 static void elfx32_out(int32_t segto
, const void *data
,
1293 enum out_type type
, uint64_t size
,
1294 int32_t segment
, int32_t wrt
)
1296 struct elf_section
*s
;
1300 static struct symlininfo sinfo
;
1303 * handle absolute-assembly (structure definitions)
1305 if (segto
== NO_SEG
) {
1306 if (type
!= OUT_RESERVE
)
1307 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
1313 for (i
= 0; i
< nsects
; i
++)
1314 if (segto
== sects
[i
]->index
) {
1319 int tempint
; /* ignored */
1320 if (segto
!= elf_section_names(".text", 2, &tempint
))
1321 nasm_panic(0, "strange segment conditions in ELF driver");
1323 s
= sects
[nsects
- 1];
1328 /* again some stabs debugging stuff */
1329 sinfo
.offset
= s
->len
;
1331 sinfo
.segto
= segto
;
1332 sinfo
.name
= s
->name
;
1333 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
1334 /* end of debugging stuff */
1336 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
1337 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
1338 " BSS section `%s': ignored", s
->name
);
1339 s
->len
+= realsize(type
, size
);
1345 if (s
->type
== SHT_PROGBITS
) {
1346 nasm_error(ERR_WARNING
, "uninitialized space declared in"
1347 " non-BSS section `%s': zeroing", s
->name
);
1348 elf_sect_write(s
, NULL
, size
);
1354 if (segment
!= NO_SEG
)
1355 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1356 elf_sect_write(s
, data
, size
);
1361 int isize
= (int)size
;
1362 int asize
= abs((int)size
);
1364 addr
= *(int64_t *)data
;
1365 if (segment
== NO_SEG
) {
1367 } else if (segment
% 2) {
1368 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1369 " segment base references");
1371 if (wrt
== NO_SEG
) {
1375 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
1379 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
1382 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
1385 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
1389 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
1392 nasm_panic(0, "internal error elfx32-hpa-871");
1396 } else if (wrt
== elf_gotpc_sect
+ 1) {
1398 * The user will supply GOT relative to $$. ELF
1399 * will let us have GOT relative to $. So we
1400 * need to fix up the data item by $-$$.
1403 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1405 } else if (wrt
== elf_gotoff_sect
+ 1) {
1406 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
1407 "R_X86_64_GOTOFF64");
1408 } else if (wrt
== elf_got_sect
+ 1) {
1411 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1412 R_X86_64_GOT32
, true);
1416 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
1419 } else if (wrt
== elf_sym_sect
+ 1) {
1423 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1429 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1430 R_X86_64_16
, false);
1434 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1435 R_X86_64_32
, false);
1439 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1440 R_X86_64_32S
, false);
1445 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1446 R_X86_64_64
, false);
1450 nasm_panic(0, "internal error elfx32-hpa-903");
1453 } else if (wrt
== elf_plt_sect
+ 1) {
1454 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1455 "relative PLT references");
1457 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
1461 elf_sect_writeaddr(s
, addr
, asize
);
1466 reltype
= R_X86_64_PC8
;
1471 reltype
= R_X86_64_PC16
;
1476 addr
= *(int64_t *)data
- size
;
1477 if (segment
== segto
)
1478 nasm_panic(0, "intra-segment OUT_REL1ADR");
1479 if (segment
== NO_SEG
) {
1481 } else if (segment
% 2) {
1482 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1483 " segment base references");
1485 if (wrt
== NO_SEG
) {
1486 elf_add_reloc(s
, segment
, addr
, reltype
);
1489 nasm_error(ERR_NONFATAL
,
1490 "Unsupported non-32-bit ELF relocation");
1493 elf_sect_writeaddr(s
, addr
, bytes
);
1497 addr
= *(int64_t *)data
- size
;
1498 if (segment
== segto
)
1499 nasm_panic(0, "intra-segment OUT_REL4ADR");
1500 if (segment
== NO_SEG
) {
1502 } else if (segment
% 2) {
1503 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
1504 " segment base references");
1506 if (wrt
== NO_SEG
) {
1507 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1509 } else if (wrt
== elf_plt_sect
+ 1) {
1510 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1511 R_X86_64_PLT32
, true);
1513 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1514 wrt
== elf_got_sect
+ 1) {
1515 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1516 R_X86_64_GOTPCREL
, true);
1518 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1519 wrt
== elf_got_sect
+ 1) {
1520 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
1521 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1522 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1523 R_X86_64_GOTTPOFF
, true);
1526 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
1530 elf_sect_writeaddr(s
, addr
, 4);
1534 nasm_error(ERR_NONFATAL
, "32-bit ELF format does not support 64-bit relocations");
1536 elf_sect_writeaddr(s
, addr
, 8);
1541 static void elf_write(void)
1548 int32_t symtablen
, symtablocal
;
1551 * Work out how many sections we will have. We have SHN_UNDEF,
1552 * then the flexible user sections, then the fixed sections
1553 * `.shstrtab', `.symtab' and `.strtab', then optionally
1554 * relocation sections for the user sections.
1556 nsections
= sec_numspecial
+ 1;
1557 if (dfmt_is_stabs())
1559 else if (dfmt_is_dwarf())
1562 add_sectname("", ".shstrtab");
1563 add_sectname("", ".symtab");
1564 add_sectname("", ".strtab");
1565 for (i
= 0; i
< nsects
; i
++) {
1566 nsections
++; /* for the section itself */
1567 if (sects
[i
]->head
) {
1568 nsections
++; /* for its relocations */
1569 add_sectname(is_elf32() ? ".rel" : ".rela", sects
[i
]->name
);
1573 if (dfmt_is_stabs()) {
1574 /* in case the debug information is wanted, just add these three sections... */
1575 add_sectname("", ".stab");
1576 add_sectname("", ".stabstr");
1577 add_sectname(is_elf32() ? ".rel" : ".rela", ".stab");
1578 } else if (dfmt_is_dwarf()) {
1579 /* the dwarf debug standard specifies the following ten sections,
1580 not all of which are currently implemented,
1581 although all of them are defined. */
1582 #define debug_aranges (int64_t) (nsections-10)
1583 #define debug_info (int64_t) (nsections-7)
1584 #define debug_abbrev (int64_t) (nsections-5)
1585 #define debug_line (int64_t) (nsections-4)
1586 add_sectname("", ".debug_aranges");
1587 add_sectname(".rela", ".debug_aranges");
1588 add_sectname("", ".debug_pubnames");
1589 add_sectname("", ".debug_info");
1590 add_sectname(".rela", ".debug_info");
1591 add_sectname("", ".debug_abbrev");
1592 add_sectname("", ".debug_line");
1593 add_sectname(".rela", ".debug_line");
1594 add_sectname("", ".debug_frame");
1595 add_sectname("", ".debug_loc");
1599 * Output the ELF header.
1601 if (is_elf32() || is_elfx32()) {
1602 nasm_write("\177ELF\1\1\1", 7, ofile
);
1603 fputc(elf_osabi
, ofile
);
1604 fputc(elf_abiver
, ofile
);
1605 fwritezero(7, ofile
);
1606 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1607 fwriteint16_t(is_elf32() ? EM_386
: EM_X86_64
, ofile
); /* processor ID */
1608 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1609 fwriteint32_t(0L, ofile
); /* no entry point */
1610 fwriteint32_t(0L, ofile
); /* no program header table */
1611 fwriteint32_t(0x40L
, ofile
); /* section headers straight after ELF header plus alignment */
1612 fwriteint32_t(0L, ofile
); /* no special flags */
1613 fwriteint16_t(0x34, ofile
); /* size of ELF header */
1614 fwriteint16_t(0, ofile
); /* no program header table, again */
1615 fwriteint16_t(0, ofile
); /* still no program header table */
1616 fwriteint16_t(sizeof(Elf32_Shdr
), ofile
); /* size of section header */
1617 fwriteint16_t(nsections
, ofile
); /* number of sections */
1618 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for section header table */
1620 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
1621 fwriteint32_t(0L, ofile
);
1622 fwriteint32_t(0L, ofile
);
1624 nasm_assert(is_elf64());
1625 nasm_write("\177ELF\2\1\1", 7, ofile
);
1626 fputc(elf_osabi
, ofile
);
1627 fputc(elf_abiver
, ofile
);
1628 fwritezero(7, ofile
);
1629 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1630 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1631 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1632 fwriteint64_t(0L, ofile
); /* no entry point */
1633 fwriteint64_t(0L, ofile
); /* no program header table */
1634 fwriteint64_t(0x40L
, ofile
); /* section headers straight after ELF header plus alignment */
1635 fwriteint32_t(0L, ofile
); /* no special flags */
1636 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1637 fwriteint16_t(0, ofile
); /* no program header table, again */
1638 fwriteint16_t(0, ofile
); /* still no program header table */
1639 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1640 fwriteint16_t(nsections
, ofile
); /* number of sections */
1641 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for section header table */
1645 * Build the symbol table and relocation tables.
1647 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1648 for (i
= 0; i
< nsects
; i
++)
1650 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1654 * Now output the section header table.
1657 elf_foffs
= 0x40 + (is_elf64() ? sizeof(Elf64_Shdr
): sizeof(Elf32_Shdr
)) * nsections
;
1658 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1661 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1664 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1667 /* The normal sections */
1668 for (i
= 0; i
< nsects
; i
++) {
1669 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1670 (sects
[i
]->type
== SHT_PROGBITS
?
1671 sects
[i
]->data
: NULL
), true,
1672 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1677 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1678 shstrtablen
, 0, 0, 1, 0);
1683 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1684 symtablen
, sec_strtab
, symtablocal
, 8, 24);
1686 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1687 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1691 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1692 strslen
, 0, 0, 1, 0);
1695 /* The relocation sections */
1697 for (i
= 0; i
< nsects
; i
++) {
1698 if (sects
[i
]->head
) {
1699 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1700 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1704 } else if (is_elfx32()) {
1705 for (i
= 0; i
< nsects
; i
++) {
1706 if (sects
[i
]->head
) {
1707 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1708 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 12);
1713 nasm_assert(is_elf64());
1714 for (i
= 0; i
< nsects
; i
++) {
1715 if (sects
[i
]->head
) {
1716 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1717 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 8, 24);
1723 if (dfmt_is_stabs()) {
1724 /* for debugging information, create the last three sections
1725 which are the .stab , .stabstr and .rel.stab sections respectively */
1727 /* this function call creates the stab sections in memory */
1730 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1731 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1732 stablen
, sec_stabstr
, 0, 4, 12);
1735 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1736 stabstrlen
, 0, 0, 4, 0);
1739 /* link -> symtable info -> section to refer to */
1741 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1742 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1744 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, stabrelbuf
, false,
1745 stabrellen
, sec_symtab
, sec_stab
, 4, is_elf64() ? 24 : 12);
1749 } else if (dfmt_is_dwarf()) {
1750 /* for dwarf debugging information, create the ten dwarf sections */
1752 /* this function call creates the dwarf sections in memory */
1756 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1757 arangeslen
, 0, 0, 1, 0);
1760 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1761 arangesrellen
, sec_symtab
,
1762 is_elf64() ? debug_aranges
: sec_debug_aranges
,
1763 1, is_elf64() ? 24 : 12);
1766 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1767 false, pubnameslen
, 0, 0, 1, 0);
1770 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1771 infolen
, 0, 0, 1, 0);
1774 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1775 inforellen
, sec_symtab
,
1776 is_elf64() ? debug_info
: sec_debug_info
,
1777 1, is_elf64() ? 24 : 12);
1780 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1781 abbrevlen
, 0, 0, 1, 0);
1784 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1785 linelen
, 0, 0, 1, 0);
1788 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1789 linerellen
, sec_symtab
,
1790 is_elf64() ? debug_line
: sec_debug_line
,
1791 1, is_elf64() ? 24 : 12);
1794 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1795 framelen
, 0, 0, 8, 0);
1798 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1799 loclen
, 0, 0, 1, 0);
1802 fwritezero(align
, ofile
);
1805 * Now output the sections.
1807 elf_write_sections();
1809 nasm_free(elf_sects
);
1813 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1815 struct SAA
*s
= saa_init(1L);
1816 struct elf_symbol
*sym
;
1817 uint8_t entry
[24], *p
;
1823 * First, an all-zeros entry, required by the ELF spec.
1825 saa_wbytes(s
, NULL
, is_elf64() ? 24L : 16L); /* null symbol table entry */
1826 *len
+= is_elf64() ? 24L : 16L;
1830 * Next, an entry for the file name.
1834 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1835 WRITESHORT(p
, STT_FILE
); /* type FILE */
1836 WRITESHORT(p
, SHN_ABS
);
1837 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1838 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1839 saa_wbytes(s
, entry
, 24L);
1843 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1844 WRITELONG(p
, 0); /* no value */
1845 WRITELONG(p
, 0); /* no size either */
1846 WRITESHORT(p
, STT_FILE
); /* type FILE */
1847 WRITESHORT(p
, SHN_ABS
);
1848 saa_wbytes(s
, entry
, 16L);
1854 * Now some standard symbols defining the segments, for relocation
1858 for (i
= 1; i
<= nsects
; i
++) {
1860 WRITELONG(p
, 0); /* no symbol name */
1861 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1862 WRITESHORT(p
, i
); /* section id */
1863 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1864 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1865 saa_wbytes(s
, entry
, 24L);
1870 for (i
= 1; i
<= nsects
; i
++) {
1872 WRITELONG(p
, 0); /* no symbol name */
1873 WRITELONG(p
, 0); /* offset zero */
1874 WRITELONG(p
, 0); /* size zero */
1875 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1876 WRITESHORT(p
, i
); /* section id */
1877 saa_wbytes(s
, entry
, 16L);
1884 * Now the other local symbols.
1888 while ((sym
= saa_rstruct(syms
))) {
1889 if (sym
->type
& SYM_GLOBAL
)
1892 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1893 WRITECHAR(p
, sym
->type
); /* type and binding */
1894 WRITECHAR(p
, sym
->other
); /* visibility */
1895 WRITESHORT(p
, sym
->section
); /* index into section header table */
1896 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1897 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1898 saa_wbytes(s
, entry
, 24L);
1903 * dwarf needs symbols for debug sections
1904 * which are relocation targets.
1906 if (dfmt_is_dwarf()) {
1907 dwarf_infosym
= *local
;
1909 WRITELONG(p
, 0); /* no symbol name */
1910 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1911 WRITESHORT(p
, debug_info
); /* section id */
1912 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1913 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1914 saa_wbytes(s
, entry
, 24L);
1917 dwarf_abbrevsym
= *local
;
1919 WRITELONG(p
, 0); /* no symbol name */
1920 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1921 WRITESHORT(p
, debug_abbrev
); /* section id */
1922 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1923 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1924 saa_wbytes(s
, entry
, 24L);
1927 dwarf_linesym
= *local
;
1929 WRITELONG(p
, 0); /* no symbol name */
1930 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1931 WRITESHORT(p
, debug_line
); /* section id */
1932 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1933 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1934 saa_wbytes(s
, entry
, 24L);
1939 while ((sym
= saa_rstruct(syms
))) {
1940 if (sym
->type
& SYM_GLOBAL
)
1943 WRITELONG(p
, sym
->strpos
);
1944 WRITELONG(p
, sym
->symv
.key
);
1945 WRITELONG(p
, sym
->size
);
1946 WRITECHAR(p
, sym
->type
); /* type and binding */
1947 WRITECHAR(p
, sym
->other
); /* visibility */
1948 WRITESHORT(p
, sym
->section
);
1949 saa_wbytes(s
, entry
, 16L);
1954 * dwarf needs symbols for debug sections
1955 * which are relocation targets.
1957 if (dfmt_is_dwarf()) {
1958 dwarf_infosym
= *local
;
1960 WRITELONG(p
, 0); /* no symbol name */
1961 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1962 WRITELONG(p
, (uint32_t) 0); /* size zero */
1963 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1964 WRITESHORT(p
, sec_debug_info
); /* section id */
1965 saa_wbytes(s
, entry
, 16L);
1968 dwarf_abbrevsym
= *local
;
1970 WRITELONG(p
, 0); /* no symbol name */
1971 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1972 WRITELONG(p
, (uint32_t) 0); /* size zero */
1973 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1974 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1975 saa_wbytes(s
, entry
, 16L);
1978 dwarf_linesym
= *local
;
1980 WRITELONG(p
, 0); /* no symbol name */
1981 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1982 WRITELONG(p
, (uint32_t) 0); /* size zero */
1983 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1984 WRITESHORT(p
, sec_debug_line
); /* section id */
1985 saa_wbytes(s
, entry
, 16L);
1992 * Now the global symbols.
1996 while ((sym
= saa_rstruct(syms
))) {
1997 if (!(sym
->type
& SYM_GLOBAL
))
2000 WRITELONG(p
, sym
->strpos
);
2001 WRITECHAR(p
, sym
->type
); /* type and binding */
2002 WRITECHAR(p
, sym
->other
); /* visibility */
2003 WRITESHORT(p
, sym
->section
);
2004 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
2005 WRITEDLONG(p
, (int64_t)sym
->size
);
2006 saa_wbytes(s
, entry
, 24L);
2010 while ((sym
= saa_rstruct(syms
))) {
2011 if (!(sym
->type
& SYM_GLOBAL
))
2014 WRITELONG(p
, sym
->strpos
);
2015 WRITELONG(p
, sym
->symv
.key
);
2016 WRITELONG(p
, sym
->size
);
2017 WRITECHAR(p
, sym
->type
); /* type and binding */
2018 WRITECHAR(p
, sym
->other
); /* visibility */
2019 WRITESHORT(p
, sym
->section
);
2020 saa_wbytes(s
, entry
, 16L);
2028 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
2031 uint8_t *p
, entry
[24];
2032 int32_t global_offset
;
2041 * How to onvert from a global placeholder to a real symbol index;
2042 * the +2 refers to the two special entries, the null entry and
2043 * the filename entry.
2045 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
2049 int32_t sym
= r
->symbol
;
2051 if (sym
>= GLOBAL_TEMP_BASE
)
2052 sym
+= global_offset
;
2055 WRITELONG(p
, r
->address
);
2056 WRITELONG(p
, (sym
<< 8) + r
->type
);
2057 saa_wbytes(s
, entry
, 8L);
2062 } else if (is_elfx32()) {
2064 int32_t sym
= r
->symbol
;
2066 if (sym
>= GLOBAL_TEMP_BASE
)
2067 sym
+= global_offset
;
2070 WRITELONG(p
, r
->address
);
2071 WRITELONG(p
, (sym
<< 8) + r
->type
);
2072 WRITELONG(p
, r
->offset
);
2073 saa_wbytes(s
, entry
, 12L);
2079 nasm_assert(is_elf64());
2081 int32_t sym
= r
->symbol
;
2083 if (sym
>= GLOBAL_TEMP_BASE
)
2084 sym
+= global_offset
;
2087 WRITEDLONG(p
, r
->address
);
2088 WRITELONG(p
, r
->type
);
2090 WRITEDLONG(p
, r
->offset
);
2091 saa_wbytes(s
, entry
, 24L);
2101 static void elf_section_header(int name
, int type
, uint64_t flags
,
2102 void *data
, bool is_saa
, uint64_t datalen
,
2103 int link
, int info
, int align
, int eltsize
)
2105 elf_sects
[elf_nsect
].data
= data
;
2106 elf_sects
[elf_nsect
].len
= datalen
;
2107 elf_sects
[elf_nsect
].is_saa
= is_saa
;
2111 fwriteint32_t((int32_t)name
, ofile
);
2112 fwriteint32_t((int32_t)type
, ofile
);
2113 fwriteint32_t((int32_t)flags
, ofile
);
2114 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
2115 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
2116 fwriteint32_t(datalen
, ofile
);
2118 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2119 fwriteint32_t((int32_t)link
, ofile
);
2120 fwriteint32_t((int32_t)info
, ofile
);
2121 fwriteint32_t((int32_t)align
, ofile
);
2122 fwriteint32_t((int32_t)eltsize
, ofile
);
2123 } else if (is_elfx32()) {
2124 fwriteint32_t((int32_t)name
, ofile
);
2125 fwriteint32_t((int32_t)type
, ofile
);
2126 fwriteint32_t((int32_t)flags
, ofile
);
2127 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
2128 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
2129 fwriteint32_t(datalen
, ofile
);
2131 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2132 fwriteint32_t((int32_t)link
, ofile
);
2133 fwriteint32_t((int32_t)info
, ofile
);
2134 fwriteint32_t((int32_t)align
, ofile
);
2135 fwriteint32_t((int32_t)eltsize
, ofile
);
2137 nasm_assert(is_elf64());
2138 fwriteint32_t((int32_t)name
, ofile
);
2139 fwriteint32_t((int32_t)type
, ofile
);
2140 fwriteint64_t((int64_t)flags
, ofile
);
2141 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
2142 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
2143 fwriteint64_t(datalen
, ofile
);
2145 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2146 fwriteint32_t((int32_t)link
, ofile
);
2147 fwriteint32_t((int32_t)info
, ofile
);
2148 fwriteint64_t((int64_t)align
, ofile
);
2149 fwriteint64_t((int64_t)eltsize
, ofile
);
2153 static void elf_write_sections(void)
2156 for (i
= 0; i
< elf_nsect
; i
++)
2157 if (elf_sects
[i
].data
) {
2158 int32_t len
= elf_sects
[i
].len
;
2159 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
2160 int32_t align
= reallen
- len
;
2161 if (elf_sects
[i
].is_saa
)
2162 saa_fpwrite(elf_sects
[i
].data
, ofile
);
2164 nasm_write(elf_sects
[i
].data
, len
, ofile
);
2165 fwritezero(align
, ofile
);
2169 static void elf_sect_write(struct elf_section
*sect
, const void *data
, size_t len
)
2171 saa_wbytes(sect
->data
, data
, len
);
2175 static void elf_sect_writeaddr(struct elf_section
*sect
, int64_t data
, size_t len
)
2177 saa_writeaddr(sect
->data
, data
, len
);
2181 static void elf_sectalign(int32_t seg
, unsigned int value
)
2183 struct elf_section
*s
= NULL
;
2186 for (i
= 0; i
< nsects
; i
++) {
2187 if (sects
[i
]->index
== seg
) {
2192 if (!s
|| !is_power2(value
))
2195 if (value
> s
->align
)
2199 static int32_t elf_segbase(int32_t segment
)
2204 static void elf_filename(char *inname
, char *outname
)
2206 strcpy(elf_module
, inname
);
2207 standard_extension(inname
, outname
, ".o");
2210 extern macros_t elf_stdmac
[];
2212 static int elf_set_info(enum geninfo type
, char **val
)
2219 static const struct dfmt elf32_df_dwarf
= {
2220 "ELF32 (i386) dwarf debug format for Linux/Unix",
2224 null_debug_deflabel
,
2225 null_debug_directive
,
2231 static const struct dfmt elf32_df_stabs
= {
2232 "ELF32 (i386) stabs debug format for Linux/Unix",
2236 null_debug_deflabel
,
2237 null_debug_directive
,
2243 static const struct dfmt
* const elf32_debugs_arr
[3] =
2244 { &elf32_df_dwarf
, &elf32_df_stabs
, NULL
};
2246 const struct ofmt of_elf32
= {
2247 "ELF32 (i386) object files (e.g. Linux)",
2266 static const struct dfmt elf64_df_dwarf
= {
2267 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
2271 null_debug_deflabel
,
2272 null_debug_directive
,
2278 static const struct dfmt elf64_df_stabs
= {
2279 "ELF64 (x86-64) stabs debug format for Linux/Unix",
2283 null_debug_deflabel
,
2284 null_debug_directive
,
2290 static const struct dfmt
* const elf64_debugs_arr
[3] =
2291 { &elf64_df_dwarf
, &elf64_df_stabs
, NULL
};
2293 const struct ofmt of_elf64
= {
2294 "ELF64 (x86_64) object files (e.g. Linux)",
2313 static const struct dfmt elfx32_df_dwarf
= {
2314 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
2318 null_debug_deflabel
,
2319 null_debug_directive
,
2325 static const struct dfmt elfx32_df_stabs
= {
2326 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
2330 null_debug_deflabel
,
2331 null_debug_directive
,
2337 static const struct dfmt
* const elfx32_debugs_arr
[3] =
2338 { &elfx32_df_dwarf
, &elfx32_df_stabs
, NULL
};
2340 const struct ofmt of_elfx32
= {
2341 "ELFX32 (x86_64) object files (e.g. Linux)",
2360 static bool is_elf64(void)
2362 return ofmt
== &of_elf64
;
2365 static bool is_elf32(void)
2367 return ofmt
== &of_elf32
;
2370 static bool is_elfx32(void)
2372 return ofmt
== &of_elfx32
;
2375 static bool dfmt_is_stabs(void)
2377 return dfmt
== &elf32_df_stabs
|| dfmt
== &elfx32_df_stabs
|| dfmt
== &elf64_df_stabs
;
2380 static bool dfmt_is_dwarf(void)
2382 return dfmt
== &elf32_df_dwarf
|| dfmt
== &elfx32_df_dwarf
|| dfmt
== &elf64_df_dwarf
;
2385 /* common debugging routines */
2386 static void debug_typevalue(int32_t type
)
2388 int32_t stype
, ssize
;
2389 switch (TYM_TYPE(type
)) {
2432 stype
= STT_SECTION
;
2447 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
2448 lastsym
->size
= ssize
;
2449 lastsym
->type
= stype
;
2453 /* stabs debugging routines */
2455 static void stabs_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
2458 if (!stabs_filename
) {
2459 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
2460 strcpy(stabs_filename
, filename
);
2462 if (strcmp(stabs_filename
, filename
)) {
2463 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2464 in fact, this leak comes in quite handy to maintain a list of files
2465 encountered so far in the symbol lines... */
2467 /* why not nasm_free(stabs_filename); we're done with the old one */
2469 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
2470 strcpy(stabs_filename
, filename
);
2474 currentline
= linenumber
;
2477 static void stabs_output(int type
, void *param
)
2479 struct symlininfo
*s
;
2480 struct linelist
*el
;
2481 if (type
== TY_DEBUGSYMLIN
) {
2482 if (debug_immcall
) {
2483 s
= (struct symlininfo
*)param
;
2484 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2485 return; /* line info is only collected for executable sections */
2487 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2488 el
->info
.offset
= s
->offset
;
2489 el
->info
.section
= s
->section
;
2490 el
->info
.name
= s
->name
;
2491 el
->line
= currentline
;
2492 el
->filename
= stabs_filename
;
2495 stabslines
->last
->next
= el
;
2496 stabslines
->last
= el
;
2499 stabslines
->last
= el
;
2506 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2508 static void stabs_generate(void)
2510 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
2511 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
2515 struct linelist
*ptr
;
2519 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
2522 if (numfiles
== 0) {
2523 allfiles
[0] = ptr
->filename
;
2526 for (i
= 0; i
< numfiles
; i
++) {
2527 if (!strcmp(allfiles
[i
], ptr
->filename
))
2530 if (i
>= numfiles
) {
2531 allfiles
[i
] = ptr
->filename
;
2538 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
2539 for (i
= 0; i
< numfiles
; i
++) {
2540 fileidx
[i
] = strsize
;
2541 strsize
+= strlen(allfiles
[i
]) + 1;
2544 for (i
= 0; i
< numfiles
; i
++) {
2545 if (!strcmp(allfiles
[i
], elf_module
)) {
2552 * worst case size of the stab buffer would be:
2553 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2554 * plus one "ending" entry
2556 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
2557 sizeof(struct stabentry
));
2558 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
2559 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* (is_elf64() ? 16 : 8) * (2 + 3));
2562 for (i
= 0; i
< numfiles
; i
++)
2563 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
2566 stabstrlen
= strsize
; /* set global variable for length of stab strings */
2574 * this is the first stab, its strx points to the filename of the
2575 * the source-file, the n_desc field should be set to the number
2576 * of remaining stabs
2578 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, stabstrlen
);
2580 /* this is the stab for the main source file */
2581 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
2583 /* relocation table entry */
2586 * Since the symbol table has two entries before
2587 * the section symbols, the index in the info.section
2588 * member must be adjusted by adding 2
2592 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2593 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2594 } else if (is_elfx32()) {
2595 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2596 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2599 nasm_assert(is_elf64());
2600 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2601 WRITELONG(rptr
, R_X86_64_32
);
2602 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2603 WRITEDLONG(rptr
, 0);
2606 currfile
= mainfileindex
;
2611 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2612 /* oops file has changed... */
2613 for (i
= 0; i
< numfiles
; i
++)
2614 if (!strcmp(allfiles
[i
], ptr
->filename
))
2617 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2621 /* relocation table entry */
2622 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2623 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2626 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2629 /* relocation table entry */
2630 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2631 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2635 } else if (is_elfx32()) {
2637 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2638 /* oops file has changed... */
2639 for (i
= 0; i
< numfiles
; i
++)
2640 if (!strcmp(allfiles
[i
], ptr
->filename
))
2643 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2647 /* relocation table entry */
2648 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2649 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2650 WRITELONG(rptr
, ptr
->info
.offset
);
2653 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2656 /* relocation table entry */
2657 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2658 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2659 WRITELONG(rptr
, ptr
->info
.offset
);
2664 nasm_assert(is_elf64());
2666 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2667 /* oops file has changed... */
2668 for (i
= 0; i
< numfiles
; i
++)
2669 if (!strcmp(allfiles
[i
], ptr
->filename
))
2672 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2676 /* relocation table entry */
2677 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2678 WRITELONG(rptr
, R_X86_64_32
);
2679 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2680 WRITEDLONG(rptr
, ptr
->info
.offset
);
2683 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2686 /* relocation table entry */
2687 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2688 WRITELONG(rptr
, R_X86_64_32
);
2689 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2690 WRITEDLONG(rptr
, ptr
->info
.offset
);
2696 /* this is an "ending" token */
2697 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
2700 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
2702 nasm_free(allfiles
);
2705 stablen
= (sptr
- sbuf
);
2706 stabrellen
= (rptr
- rbuf
);
2712 static void stabs_cleanup(void)
2714 struct linelist
*ptr
, *del
;
2726 nasm_free(stabrelbuf
);
2727 nasm_free(stabstrbuf
);
2730 /* dwarf routines */
2732 static void dwarf_init(void)
2734 ndebugs
= 3; /* 3 debug symbols */
2737 static void dwarf_linenum(const char *filename
, int32_t linenumber
,
2741 dwarf_findfile(filename
);
2743 currentline
= linenumber
;
2746 /* called from elf_out with type == TY_DEBUGSYMLIN */
2747 static void dwarf_output(int type
, void *param
)
2749 int ln
, aa
, inx
, maxln
, soc
;
2750 struct symlininfo
*s
;
2755 s
= (struct symlininfo
*)param
;
2757 /* line number info is only gathered for executable sections */
2758 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2761 /* Check if section index has changed */
2762 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
2763 dwarf_findsect(s
->section
);
2765 /* do nothing unless line or file has changed */
2769 ln
= currentline
- dwarf_csect
->line
;
2770 aa
= s
->offset
- dwarf_csect
->offset
;
2771 inx
= dwarf_clist
->line
;
2772 plinep
= dwarf_csect
->psaa
;
2773 /* check for file change */
2774 if (!(inx
== dwarf_csect
->file
)) {
2775 saa_write8(plinep
,DW_LNS_set_file
);
2776 saa_write8(plinep
,inx
);
2777 dwarf_csect
->file
= inx
;
2779 /* check for line change */
2781 /* test if in range of special op code */
2782 maxln
= line_base
+ line_range
;
2783 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
2784 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
2785 saa_write8(plinep
,soc
);
2787 saa_write8(plinep
,DW_LNS_advance_line
);
2788 saa_wleb128s(plinep
,ln
);
2790 saa_write8(plinep
,DW_LNS_advance_pc
);
2791 saa_wleb128u(plinep
,aa
);
2794 dwarf_csect
->line
= currentline
;
2795 dwarf_csect
->offset
= s
->offset
;
2798 /* show change handled */
2803 static void dwarf_generate(void)
2807 struct linelist
*ftentry
;
2808 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
2809 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
2810 struct sectlist
*psect
;
2811 size_t saalen
, linepoff
, totlen
, highaddr
;
2814 /* write epilogues for each line program range */
2815 /* and build aranges section */
2816 paranges
= saa_init(1L);
2817 parangesrel
= saa_init(1L);
2818 saa_write16(paranges
,2); /* dwarf version */
2819 saa_write32(parangesrel
, paranges
->datalen
+4);
2820 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
2821 saa_write32(parangesrel
, 0);
2822 saa_write32(paranges
,0); /* offset into info */
2823 saa_write8(paranges
,4); /* pointer size */
2824 saa_write8(paranges
,0); /* not segmented */
2825 saa_write32(paranges
,0); /* padding */
2826 /* iterate though sectlist entries */
2827 psect
= dwarf_fsect
;
2830 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2831 plinep
= psect
->psaa
;
2832 /* Line Number Program Epilogue */
2833 saa_write8(plinep
,2); /* std op 2 */
2834 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2835 saa_write8(plinep
,DW_LNS_extended_op
);
2836 saa_write8(plinep
,1); /* operand length */
2837 saa_write8(plinep
,DW_LNE_end_sequence
);
2838 totlen
+= plinep
->datalen
;
2839 /* range table relocation entry */
2840 saa_write32(parangesrel
, paranges
->datalen
+ 4);
2841 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2842 saa_write32(parangesrel
, (uint32_t) 0);
2843 /* range table entry */
2844 saa_write32(paranges
,0x0000); /* range start */
2845 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
2846 highaddr
+= sects
[psect
->section
]->len
;
2847 /* done with this entry */
2848 psect
= psect
->next
;
2850 saa_write32(paranges
,0); /* null address */
2851 saa_write32(paranges
,0); /* null length */
2852 saalen
= paranges
->datalen
;
2853 arangeslen
= saalen
+ 4;
2854 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2855 WRITELONG(pbuf
,saalen
); /* initial length */
2856 saa_rnbytes(paranges
, pbuf
, saalen
);
2858 } else if (is_elfx32()) {
2859 /* write epilogues for each line program range */
2860 /* and build aranges section */
2861 paranges
= saa_init(1L);
2862 parangesrel
= saa_init(1L);
2863 saa_write16(paranges
,3); /* dwarf version */
2864 saa_write32(parangesrel
, paranges
->datalen
+4);
2865 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_X86_64_32
); /* reloc to info */
2866 saa_write32(parangesrel
, 0);
2867 saa_write32(paranges
,0); /* offset into info */
2868 saa_write8(paranges
,4); /* pointer size */
2869 saa_write8(paranges
,0); /* not segmented */
2870 saa_write32(paranges
,0); /* padding */
2871 /* iterate though sectlist entries */
2872 psect
= dwarf_fsect
;
2875 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2876 plinep
= psect
->psaa
;
2877 /* Line Number Program Epilogue */
2878 saa_write8(plinep
,2); /* std op 2 */
2879 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2880 saa_write8(plinep
,DW_LNS_extended_op
);
2881 saa_write8(plinep
,1); /* operand length */
2882 saa_write8(plinep
,DW_LNE_end_sequence
);
2883 totlen
+= plinep
->datalen
;
2884 /* range table relocation entry */
2885 saa_write32(parangesrel
, paranges
->datalen
+ 4);
2886 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_X86_64_32
);
2887 saa_write32(parangesrel
, (uint32_t) 0);
2888 /* range table entry */
2889 saa_write32(paranges
,0x0000); /* range start */
2890 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
2891 highaddr
+= sects
[psect
->section
]->len
;
2892 /* done with this entry */
2893 psect
= psect
->next
;
2895 saa_write32(paranges
,0); /* null address */
2896 saa_write32(paranges
,0); /* null length */
2897 saalen
= paranges
->datalen
;
2898 arangeslen
= saalen
+ 4;
2899 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2900 WRITELONG(pbuf
,saalen
); /* initial length */
2901 saa_rnbytes(paranges
, pbuf
, saalen
);
2904 nasm_assert(is_elf64());
2905 /* write epilogues for each line program range */
2906 /* and build aranges section */
2907 paranges
= saa_init(1L);
2908 parangesrel
= saa_init(1L);
2909 saa_write16(paranges
,3); /* dwarf version */
2910 saa_write64(parangesrel
, paranges
->datalen
+4);
2911 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
2912 saa_write64(parangesrel
, 0);
2913 saa_write32(paranges
,0); /* offset into info */
2914 saa_write8(paranges
,8); /* pointer size */
2915 saa_write8(paranges
,0); /* not segmented */
2916 saa_write32(paranges
,0); /* padding */
2917 /* iterate though sectlist entries */
2918 psect
= dwarf_fsect
;
2921 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2922 plinep
= psect
->psaa
;
2923 /* Line Number Program Epilogue */
2924 saa_write8(plinep
,2); /* std op 2 */
2925 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2926 saa_write8(plinep
,DW_LNS_extended_op
);
2927 saa_write8(plinep
,1); /* operand length */
2928 saa_write8(plinep
,DW_LNE_end_sequence
);
2929 totlen
+= plinep
->datalen
;
2930 /* range table relocation entry */
2931 saa_write64(parangesrel
, paranges
->datalen
+ 4);
2932 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2933 saa_write64(parangesrel
, (uint64_t) 0);
2934 /* range table entry */
2935 saa_write64(paranges
,0x0000); /* range start */
2936 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
2937 highaddr
+= sects
[psect
->section
]->len
;
2938 /* done with this entry */
2939 psect
= psect
->next
;
2941 saa_write64(paranges
,0); /* null address */
2942 saa_write64(paranges
,0); /* null length */
2943 saalen
= paranges
->datalen
;
2944 arangeslen
= saalen
+ 4;
2945 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2946 WRITELONG(pbuf
,saalen
); /* initial length */
2947 saa_rnbytes(paranges
, pbuf
, saalen
);
2951 /* build rela.aranges section */
2952 arangesrellen
= saalen
= parangesrel
->datalen
;
2953 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2954 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2955 saa_free(parangesrel
);
2957 /* build pubnames section */
2958 ppubnames
= saa_init(1L);
2959 saa_write16(ppubnames
,3); /* dwarf version */
2960 saa_write32(ppubnames
,0); /* offset into info */
2961 saa_write32(ppubnames
,0); /* space used in info */
2962 saa_write32(ppubnames
,0); /* end of list */
2963 saalen
= ppubnames
->datalen
;
2964 pubnameslen
= saalen
+ 4;
2965 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2966 WRITELONG(pbuf
,saalen
); /* initial length */
2967 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2968 saa_free(ppubnames
);
2971 /* build info section */
2972 pinfo
= saa_init(1L);
2973 pinforel
= saa_init(1L);
2974 saa_write16(pinfo
,2); /* dwarf version */
2975 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2976 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
2977 saa_write32(pinforel
, 0);
2978 saa_write32(pinfo
,0); /* offset into abbrev */
2979 saa_write8(pinfo
,4); /* pointer size */
2980 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2981 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2982 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2983 saa_write32(pinforel
, 0);
2984 saa_write32(pinfo
,0); /* DW_AT_low_pc */
2985 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2986 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2987 saa_write32(pinforel
, 0);
2988 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
2989 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2990 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
2991 saa_write32(pinforel
, 0);
2992 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2993 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2994 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2995 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2996 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2997 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2998 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2999 saa_write32(pinforel
, 0);
3000 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3001 saa_write32(pinfo
,0); /* DW_AT_frame_base */
3002 saa_write8(pinfo
,0); /* end of entries */
3003 saalen
= pinfo
->datalen
;
3004 infolen
= saalen
+ 4;
3005 infobuf
= pbuf
= nasm_malloc(infolen
);
3006 WRITELONG(pbuf
,saalen
); /* initial length */
3007 saa_rnbytes(pinfo
, pbuf
, saalen
);
3009 } else if (is_elfx32()) {
3010 /* build info section */
3011 pinfo
= saa_init(1L);
3012 pinforel
= saa_init(1L);
3013 saa_write16(pinfo
,3); /* dwarf version */
3014 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3015 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_X86_64_32
); /* reloc to abbrev */
3016 saa_write32(pinforel
, 0);
3017 saa_write32(pinfo
,0); /* offset into abbrev */
3018 saa_write8(pinfo
,4); /* pointer size */
3019 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
3020 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3021 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3022 saa_write32(pinforel
, 0);
3023 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3024 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3025 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3026 saa_write32(pinforel
, 0);
3027 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
3028 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3029 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_X86_64_32
); /* reloc to line */
3030 saa_write32(pinforel
, 0);
3031 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3032 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3033 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3034 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3035 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3036 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3037 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3038 saa_write32(pinforel
, 0);
3039 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3040 saa_write32(pinfo
,0); /* DW_AT_frame_base */
3041 saa_write8(pinfo
,0); /* end of entries */
3042 saalen
= pinfo
->datalen
;
3043 infolen
= saalen
+ 4;
3044 infobuf
= pbuf
= nasm_malloc(infolen
);
3045 WRITELONG(pbuf
,saalen
); /* initial length */
3046 saa_rnbytes(pinfo
, pbuf
, saalen
);
3049 nasm_assert(is_elf64());
3050 /* build info section */
3051 pinfo
= saa_init(1L);
3052 pinforel
= saa_init(1L);
3053 saa_write16(pinfo
,3); /* dwarf version */
3054 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3055 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
3056 saa_write64(pinforel
, 0);
3057 saa_write32(pinfo
,0); /* offset into abbrev */
3058 saa_write8(pinfo
,8); /* pointer size */
3059 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
3060 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3061 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3062 saa_write64(pinforel
, 0);
3063 saa_write64(pinfo
,0); /* DW_AT_low_pc */
3064 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3065 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3066 saa_write64(pinforel
, 0);
3067 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
3068 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3069 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
3070 saa_write64(pinforel
, 0);
3071 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3072 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3073 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3074 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3075 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3076 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3077 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3078 saa_write64(pinforel
, 0);
3079 saa_write64(pinfo
,0); /* DW_AT_low_pc */
3080 saa_write64(pinfo
,0); /* DW_AT_frame_base */
3081 saa_write8(pinfo
,0); /* end of entries */
3082 saalen
= pinfo
->datalen
;
3083 infolen
= saalen
+ 4;
3084 infobuf
= pbuf
= nasm_malloc(infolen
);
3085 WRITELONG(pbuf
,saalen
); /* initial length */
3086 saa_rnbytes(pinfo
, pbuf
, saalen
);
3090 /* build rela.info section */
3091 inforellen
= saalen
= pinforel
->datalen
;
3092 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
3093 saa_rnbytes(pinforel
, pbuf
, saalen
);
3096 /* build abbrev section */
3097 pabbrev
= saa_init(1L);
3098 saa_write8(pabbrev
,1); /* entry number LEB128u */
3099 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
3100 saa_write8(pabbrev
,1); /* has children */
3101 /* the following attributes and forms are all LEB128u values */
3102 saa_write8(pabbrev
,DW_AT_low_pc
);
3103 saa_write8(pabbrev
,DW_FORM_addr
);
3104 saa_write8(pabbrev
,DW_AT_high_pc
);
3105 saa_write8(pabbrev
,DW_FORM_addr
);
3106 saa_write8(pabbrev
,DW_AT_stmt_list
);
3107 saa_write8(pabbrev
,DW_FORM_data4
);
3108 saa_write8(pabbrev
,DW_AT_name
);
3109 saa_write8(pabbrev
,DW_FORM_string
);
3110 saa_write8(pabbrev
,DW_AT_producer
);
3111 saa_write8(pabbrev
,DW_FORM_string
);
3112 saa_write8(pabbrev
,DW_AT_language
);
3113 saa_write8(pabbrev
,DW_FORM_data2
);
3114 saa_write16(pabbrev
,0); /* end of entry */
3115 /* LEB128u usage same as above */
3116 saa_write8(pabbrev
,2); /* entry number */
3117 saa_write8(pabbrev
,DW_TAG_subprogram
);
3118 saa_write8(pabbrev
,0); /* no children */
3119 saa_write8(pabbrev
,DW_AT_low_pc
);
3120 saa_write8(pabbrev
,DW_FORM_addr
);
3121 saa_write8(pabbrev
,DW_AT_frame_base
);
3122 saa_write8(pabbrev
,DW_FORM_data4
);
3123 saa_write16(pabbrev
,0); /* end of entry */
3124 abbrevlen
= saalen
= pabbrev
->datalen
;
3125 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
3126 saa_rnbytes(pabbrev
, pbuf
, saalen
);
3129 /* build line section */
3131 plines
= saa_init(1L);
3132 saa_write8(plines
,1); /* Minimum Instruction Length */
3133 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
3134 saa_write8(plines
,line_base
); /* Line Base */
3135 saa_write8(plines
,line_range
); /* Line Range */
3136 saa_write8(plines
,opcode_base
); /* Opcode Base */
3137 /* standard opcode lengths (# of LEB128u operands) */
3138 saa_write8(plines
,0); /* Std opcode 1 length */
3139 saa_write8(plines
,1); /* Std opcode 2 length */
3140 saa_write8(plines
,1); /* Std opcode 3 length */
3141 saa_write8(plines
,1); /* Std opcode 4 length */
3142 saa_write8(plines
,1); /* Std opcode 5 length */
3143 saa_write8(plines
,0); /* Std opcode 6 length */
3144 saa_write8(plines
,0); /* Std opcode 7 length */
3145 saa_write8(plines
,0); /* Std opcode 8 length */
3146 saa_write8(plines
,1); /* Std opcode 9 length */
3147 saa_write8(plines
,0); /* Std opcode 10 length */
3148 saa_write8(plines
,0); /* Std opcode 11 length */
3149 saa_write8(plines
,1); /* Std opcode 12 length */
3150 /* Directory Table */
3151 saa_write8(plines
,0); /* End of table */
3152 /* File Name Table */
3153 ftentry
= dwarf_flist
;
3154 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
3155 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
3156 saa_write8(plines
,0); /* directory LEB128u */
3157 saa_write8(plines
,0); /* time LEB128u */
3158 saa_write8(plines
,0); /* size LEB128u */
3159 ftentry
= ftentry
->next
;
3161 saa_write8(plines
,0); /* End of table */
3162 linepoff
= plines
->datalen
;
3163 linelen
= linepoff
+ totlen
+ 10;
3164 linebuf
= pbuf
= nasm_malloc(linelen
);
3165 WRITELONG(pbuf
,linelen
-4); /* initial length */
3166 WRITESHORT(pbuf
,3); /* dwarf version */
3167 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
3168 /* write line header */
3170 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
3173 /* concatonate line program ranges */
3175 plinesrel
= saa_init(1L);
3176 psect
= dwarf_fsect
;
3178 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3179 saa_write32(plinesrel
, linepoff
);
3180 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
3181 saa_write32(plinesrel
, (uint32_t) 0);
3182 plinep
= psect
->psaa
;
3183 saalen
= plinep
->datalen
;
3184 saa_rnbytes(plinep
, pbuf
, saalen
);
3188 /* done with this entry */
3189 psect
= psect
->next
;
3191 } else if (is_elfx32()) {
3192 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3193 saa_write32(plinesrel
, linepoff
);
3194 saa_write32(plinesrel
, ((psect
->section
+ 2) << 8) + R_X86_64_32
);
3195 saa_write32(plinesrel
, 0);
3196 plinep
= psect
->psaa
;
3197 saalen
= plinep
->datalen
;
3198 saa_rnbytes(plinep
, pbuf
, saalen
);
3202 /* done with this entry */
3203 psect
= psect
->next
;
3206 nasm_assert(is_elf64());
3207 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3208 saa_write64(plinesrel
, linepoff
);
3209 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
3210 saa_write64(plinesrel
, (uint64_t) 0);
3211 plinep
= psect
->psaa
;
3212 saalen
= plinep
->datalen
;
3213 saa_rnbytes(plinep
, pbuf
, saalen
);
3217 /* done with this entry */
3218 psect
= psect
->next
;
3222 /* build rela.lines section */
3223 linerellen
=saalen
= plinesrel
->datalen
;
3224 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
3225 saa_rnbytes(plinesrel
, pbuf
, saalen
);
3226 saa_free(plinesrel
);
3228 /* build frame section */
3230 framebuf
= pbuf
= nasm_malloc(framelen
);
3231 WRITELONG(pbuf
,framelen
-4); /* initial length */
3233 /* build loc section */
3235 locbuf
= pbuf
= nasm_malloc(loclen
);
3237 WRITELONG(pbuf
,0); /* null beginning offset */
3238 WRITELONG(pbuf
,0); /* null ending offset */
3240 nasm_assert(is_elf64());
3241 WRITEDLONG(pbuf
,0); /* null beginning offset */
3242 WRITEDLONG(pbuf
,0); /* null ending offset */
3246 static void dwarf_cleanup(void)
3248 nasm_free(arangesbuf
);
3249 nasm_free(arangesrelbuf
);
3250 nasm_free(pubnamesbuf
);
3252 nasm_free(inforelbuf
);
3253 nasm_free(abbrevbuf
);
3255 nasm_free(linerelbuf
);
3256 nasm_free(framebuf
);
3260 static void dwarf_findfile(const char * fname
)
3263 struct linelist
*match
;
3265 /* return if fname is current file name */
3266 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
3269 /* search for match */
3272 match
= dwarf_flist
;
3273 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
3274 if (!(strcmp(fname
, match
->filename
))) {
3275 dwarf_clist
= match
;
3281 /* add file name to end of list */
3282 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
3284 dwarf_clist
->line
= dwarf_numfiles
;
3285 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
3286 strcpy(dwarf_clist
->filename
,fname
);
3287 dwarf_clist
->next
= 0;
3288 if (!dwarf_flist
) { /* if first entry */
3289 dwarf_flist
= dwarf_elist
= dwarf_clist
;
3290 dwarf_clist
->last
= 0;
3291 } else { /* chain to previous entry */
3292 dwarf_elist
->next
= dwarf_clist
;
3293 dwarf_elist
= dwarf_clist
;
3297 static void dwarf_findsect(const int index
)
3300 struct sectlist
*match
;
3303 /* return if index is current section index */
3304 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
3307 /* search for match */
3310 match
= dwarf_fsect
;
3311 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
3312 if (match
->section
== index
) {
3313 dwarf_csect
= match
;
3316 match
= match
->next
;
3320 /* add entry to end of list */
3321 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
3323 dwarf_csect
->psaa
= plinep
= saa_init(1L);
3324 dwarf_csect
->line
= 1;
3325 dwarf_csect
->offset
= 0;
3326 dwarf_csect
->file
= 1;
3327 dwarf_csect
->section
= index
;
3328 dwarf_csect
->next
= 0;
3329 /* set relocatable address at start of line program */
3330 saa_write8(plinep
,DW_LNS_extended_op
);
3331 saa_write8(plinep
,is_elf64() ? 9 : 5); /* operand length */
3332 saa_write8(plinep
,DW_LNE_set_address
);
3334 saa_write64(plinep
,0); /* Start Address */
3336 saa_write32(plinep
,0); /* Start Address */
3338 if (!dwarf_fsect
) { /* if first entry */
3339 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
3340 dwarf_csect
->last
= 0;
3341 } else { /* chain to previous entry */
3342 dwarf_esect
->next
= dwarf_csect
;
3343 dwarf_esect
= dwarf_csect
;
3347 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */