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
49 #include "output/outform.h"
50 #include "output/outlib.h"
54 #include "output/dwarf.h"
55 #include "output/stabs.h"
56 #include "output/outelf.h"
57 #include "output/elf.h"
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 const struct dfmt df_dwarf
;
159 static const struct dfmt df_stabs
;
160 static struct elf_symbol
*lastsym
;
162 /* common debugging routines */
163 static void debug_typevalue(int32_t);
165 /* stabs debugging routines */
166 static void stabs_linenum(const char *filename
, int32_t linenumber
, int32_t);
167 static void stabs_output(int, void *);
168 static void stabs_generate(void);
169 static void stabs_cleanup(void);
171 /* dwarf debugging routines */
172 static void dwarf_init(void);
173 static void dwarf_linenum(const char *filename
, int32_t linenumber
, int32_t);
174 static void dwarf_output(int, void *);
175 static void dwarf_generate(void);
176 static void dwarf_cleanup(void);
177 static void dwarf_findfile(const char *);
178 static void dwarf_findsect(const int);
180 static bool is_elf64(void);
181 static bool is_elf32(void);
182 static bool is_elfx32(void);
185 * Special NASM section numbers which are used to define ELF special
188 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
189 static int32_t elf_got_sect
, elf_plt_sect
;
190 static int32_t elf_sym_sect
, elf_gottpoff_sect
, elf_tlsie_sect
;
192 uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
193 uint8_t elf_abiver
= 0; /* Current ABI version */
195 const struct elf_known_section elf_known_sections
[] = {
196 { ".text", SHT_PROGBITS
, SHF_ALLOC
|SHF_EXECINSTR
, 16 },
197 { ".rodata", SHT_PROGBITS
, SHF_ALLOC
, 4 },
198 { ".lrodata", SHT_PROGBITS
, SHF_ALLOC
, 4 },
199 { ".data", SHT_PROGBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
200 { ".ldata", SHT_PROGBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
201 { ".bss", SHT_NOBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
202 { ".lbss", SHT_NOBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
203 { ".tdata", SHT_PROGBITS
, SHF_ALLOC
|SHF_WRITE
|SHF_TLS
, 4 },
204 { ".tbss", SHT_NOBITS
, SHF_ALLOC
|SHF_WRITE
|SHF_TLS
, 4 },
205 { ".comment", SHT_PROGBITS
, 0, 1 },
206 { NULL
, SHT_PROGBITS
, SHF_ALLOC
, 1 } /* default */
209 /* parse section attributes */
210 void elf_section_attrib(char *name
, char *attr
, int pass
,
211 uint32_t *flags_and
, uint32_t *flags_or
,
212 uint64_t *align
, int *type
)
214 char *opt
, *val
, *next
;
216 opt
= nasm_skip_spaces(attr
);
220 while ((opt
= nasm_opt_val(opt
, &val
, &next
))) {
221 if (!nasm_stricmp(opt
, "align")) {
223 nasm_error(ERR_NONFATAL
,
224 "section align without value specified");
229 } else if (!is_power2(*align
)) {
230 nasm_error(ERR_NONFATAL
,
231 "section alignment %"PRId64
" is not a power of two",
236 } else if (!nasm_stricmp(opt
, "alloc")) {
237 *flags_and
|= SHF_ALLOC
;
238 *flags_or
|= SHF_ALLOC
;
239 } else if (!nasm_stricmp(opt
, "noalloc")) {
240 *flags_and
|= SHF_ALLOC
;
241 *flags_or
&= ~SHF_ALLOC
;
242 } else if (!nasm_stricmp(opt
, "exec")) {
243 *flags_and
|= SHF_EXECINSTR
;
244 *flags_or
|= SHF_EXECINSTR
;
245 } else if (!nasm_stricmp(opt
, "noexec")) {
246 *flags_and
|= SHF_EXECINSTR
;
247 *flags_or
&= ~SHF_EXECINSTR
;
248 } else if (!nasm_stricmp(opt
, "write")) {
249 *flags_and
|= SHF_WRITE
;
250 *flags_or
|= SHF_WRITE
;
251 } else if (!nasm_stricmp(opt
, "tls")) {
252 *flags_and
|= SHF_TLS
;
253 *flags_or
|= SHF_TLS
;
254 } else if (!nasm_stricmp(opt
, "nowrite")) {
255 *flags_and
|= SHF_WRITE
;
256 *flags_or
&= ~SHF_WRITE
;
257 } else if (!nasm_stricmp(opt
, "progbits")) {
258 *type
= SHT_PROGBITS
;
259 } else if (!nasm_stricmp(opt
, "nobits")) {
261 } else if (pass
== 1) {
262 nasm_error(ERR_WARNING
,
263 "Unknown section attribute '%s' ignored on"
264 " declaration of section `%s'", opt
, name
);
270 int elf_directive(enum directives directive
, char *value
, int pass
)
279 return 1; /* ignore in pass 2 */
281 n
= readnum(value
, &err
);
283 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
287 if (n
< 0 || n
> 255) {
288 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
295 p
= strchr(value
,',');
299 n
= readnum(p
+ 1, &err
);
300 if (err
|| n
< 0 || n
> 255) {
301 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
313 static void elf_init(void)
316 nsects
= sectlen
= 0;
317 syms
= saa_init((int32_t)sizeof(struct elf_symbol
));
318 nlocals
= nglobs
= ndebugs
= 0;
321 saa_wbytes(strs
, "\0", 1L);
322 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
323 strslen
= 2 + strlen(elf_module
);
325 shstrtablen
= shstrtabsize
= 0;;
326 add_sectname("", "");
331 * FIXME: tlsie is Elf32 only and
332 * gottpoff is Elfx32|64 only.
335 elf_gotpc_sect
= seg_alloc();
336 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
337 elf_gotoff_sect
= seg_alloc();
338 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
339 elf_got_sect
= seg_alloc();
340 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
341 elf_plt_sect
= seg_alloc();
342 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
343 elf_sym_sect
= seg_alloc();
344 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
345 elf_gottpoff_sect
= seg_alloc();
346 define_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false);
347 elf_tlsie_sect
= seg_alloc();
348 define_label("..tlsie", elf_tlsie_sect
+ 1, 0L, NULL
, false, false);
350 def_seg
= seg_alloc();
353 static void elf_cleanup(void)
359 for (i
= 0; i
< nsects
; i
++) {
360 if (sects
[i
]->type
!= SHT_NOBITS
)
361 saa_free(sects
[i
]->data
);
363 saa_free(sects
[i
]->rel
);
364 while (sects
[i
]->head
) {
366 sects
[i
]->head
= sects
[i
]->head
->next
;
377 /* add entry to the elf .shstrtab section */
378 static void add_sectname(char *firsthalf
, char *secondhalf
)
380 int len
= strlen(firsthalf
) + strlen(secondhalf
);
381 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
382 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
383 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
384 strcat(shstrtab
+ shstrtablen
, secondhalf
);
385 shstrtablen
+= len
+ 1;
388 static int elf_make_section(char *name
, int type
, int flags
, int align
)
390 struct elf_section
*s
;
392 s
= nasm_zalloc(sizeof(*s
));
394 if (type
!= SHT_NOBITS
)
395 s
->data
= saa_init(1L);
397 if (!strcmp(name
, ".text"))
400 s
->index
= seg_alloc();
401 add_sectname("", name
);
403 s
->name
= nasm_strdup(name
);
408 if (nsects
>= sectlen
)
409 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
415 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
418 uint32_t flags
, flags_and
, flags_or
;
423 *bits
= ofmt
->maxbits
;
427 p
= nasm_skip_word(name
);
430 flags_and
= flags_or
= type
= align
= 0;
432 elf_section_attrib(name
, p
, pass
, &flags_and
,
433 &flags_or
, &align
, &type
);
435 if (!strcmp(name
, ".shstrtab") ||
436 !strcmp(name
, ".symtab") ||
437 !strcmp(name
, ".strtab")) {
438 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
443 for (i
= 0; i
< nsects
; i
++)
444 if (!strcmp(name
, sects
[i
]->name
))
447 const struct elf_known_section
*ks
= elf_known_sections
;
450 if (!strcmp(name
, ks
->name
))
455 type
= type
? type
: ks
->type
;
456 align
= align
? align
: ks
->align
;
457 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
459 i
= elf_make_section(name
, type
, flags
, align
);
460 } else if (pass
== 1) {
461 if ((type
&& sects
[i
]->type
!= type
)
462 || (align
&& sects
[i
]->align
!= align
)
463 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
464 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
465 " redeclaration of section `%s'", name
);
468 return sects
[i
]->index
;
471 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
472 int is_global
, char *special
)
475 struct elf_symbol
*sym
;
476 bool special_used
= false;
478 #if defined(DEBUG) && DEBUG>2
479 nasm_error(ERR_DEBUG
,
480 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
481 name
, segment
, offset
, is_global
, special
);
483 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
485 * This is a NASM special symbol. We never allow it into
486 * the ELF symbol table, even if it's a valid one. If it
487 * _isn't_ a valid one, we should barf immediately.
489 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
491 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
492 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
493 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff") &&
494 strcmp(name
, "..tlsie"))
495 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
499 if (is_global
== 3) {
500 struct elf_symbol
**s
;
502 * Fix up a forward-reference symbol size from the first
505 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
506 if (!strcmp((*s
)->name
, name
)) {
507 struct tokenval tokval
;
509 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
513 tokval
.t_type
= TOKEN_INVALID
;
514 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, NULL
);
517 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
518 " expression as symbol size");
520 (*s
)->size
= reloc_value(e
);
524 * Remove it from the list of unresolved sizes.
526 nasm_free((*s
)->name
);
530 return; /* it wasn't an important one */
533 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
534 strslen
+= 1 + strlen(name
);
536 lastsym
= sym
= saa_wstruct(syms
);
538 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
541 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
542 sym
->other
= STV_DEFAULT
;
544 if (segment
== NO_SEG
)
545 sym
->section
= SHN_ABS
;
548 sym
->section
= SHN_UNDEF
;
549 if (segment
== def_seg
) {
550 /* we have to be sure at least text section is there */
552 if (segment
!= elf_section_names(".text", 2, &tempint
))
553 nasm_panic(0, "strange segment conditions in ELF driver");
555 for (i
= 0; i
< nsects
; i
++) {
556 if (segment
== sects
[i
]->index
) {
557 sym
->section
= i
+ 1;
563 if (is_global
== 2) {
566 sym
->section
= SHN_COMMON
;
568 * We have a common variable. Check the special text to see
569 * if it's a valid number and power of two; if so, store it
570 * as the alignment for the common variable.
574 sym
->symv
.key
= readnum(special
, &err
);
576 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
577 " valid number", special
);
578 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
579 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
580 " power of two", special
);
584 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
586 if (sym
->type
== SYM_GLOBAL
) {
588 * If sym->section == SHN_ABS, then the first line of the
589 * else section would cause a core dump, because its a reference
590 * beyond the end of the section array.
591 * This behaviour is exhibited by this code:
594 * To avoid such a crash, such requests are silently discarded.
595 * This may not be the best solution.
597 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
598 bsym
= raa_write(bsym
, segment
, nglobs
);
599 } else if (sym
->section
!= SHN_ABS
) {
601 * This is a global symbol; so we must add it to the rbtree
602 * of global symbols in its section.
604 * In addition, we check the special text for symbol
605 * type and size information.
607 sects
[sym
->section
-1]->gsyms
=
608 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
611 int n
= strcspn(special
, " \t");
613 if (!nasm_strnicmp(special
, "function", n
))
614 sym
->type
|= STT_FUNC
;
615 else if (!nasm_strnicmp(special
, "data", n
) ||
616 !nasm_strnicmp(special
, "object", n
))
617 sym
->type
|= STT_OBJECT
;
618 else if (!nasm_strnicmp(special
, "notype", n
))
619 sym
->type
|= STT_NOTYPE
;
621 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
625 special
= nasm_skip_spaces(special
);
627 n
= strcspn(special
, " \t");
628 if (!nasm_strnicmp(special
, "default", n
))
629 sym
->other
= STV_DEFAULT
;
630 else if (!nasm_strnicmp(special
, "internal", n
))
631 sym
->other
= STV_INTERNAL
;
632 else if (!nasm_strnicmp(special
, "hidden", n
))
633 sym
->other
= STV_HIDDEN
;
634 else if (!nasm_strnicmp(special
, "protected", n
))
635 sym
->other
= STV_PROTECTED
;
642 struct tokenval tokval
;
645 char *saveme
= stdscan_get();
647 while (special
[n
] && nasm_isspace(special
[n
]))
650 * We have a size expression; attempt to
654 stdscan_set(special
+ n
);
655 tokval
.t_type
= TOKEN_INVALID
;
656 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, NULL
);
660 sym
->name
= nasm_strdup(name
);
663 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
664 " expression as symbol size");
666 sym
->size
= reloc_value(e
);
673 * If TLS segment, mark symbol accordingly.
675 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
677 sym
->type
|= STT_TLS
;
680 sym
->globnum
= nglobs
;
685 if (special
&& !special_used
)
686 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
689 static void elf_add_reloc(struct elf_section
*sect
, int32_t segment
,
690 int64_t offset
, int type
)
694 r
= *sect
->tail
= nasm_zalloc(sizeof(struct elf_reloc
));
695 sect
->tail
= &r
->next
;
697 r
->address
= sect
->len
;
700 if (segment
!= NO_SEG
) {
702 for (i
= 0; i
< nsects
; i
++)
703 if (segment
== sects
[i
]->index
)
706 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
714 * This routine deals with ..got and ..sym relocations: the more
715 * complicated kinds. In shared-library writing, some relocations
716 * with respect to global symbols must refer to the precise symbol
717 * rather than referring to an offset from the base of the section
718 * _containing_ the symbol. Such relocations call to this routine,
719 * which searches the symbol list for the symbol in question.
721 * R_386_GOT32 | R_X86_64_GOT32 references require the _exact_ symbol address to be
722 * used; R_386_32 | R_X86_64_32 references can be at an offset from the symbol.
723 * The boolean argument `exact' tells us this.
725 * Return value is the adjusted value of `addr', having become an
726 * offset from the symbol rather than the section. Should always be
727 * zero when returning from an exact call.
729 * Limitation: if you define two symbols at the same place,
730 * confusion will occur.
732 * Inefficiency: we search, currently, using a linked list which
733 * isn't even necessarily sorted.
735 static int64_t elf_add_gsym_reloc(struct elf_section
*sect
,
736 int32_t segment
, uint64_t offset
,
737 int64_t pcrel
, int type
, bool exact
)
740 struct elf_section
*s
;
741 struct elf_symbol
*sym
;
746 * First look up the segment/offset pair and find a global
747 * symbol corresponding to it. If it's not one of our segments,
748 * then it must be an external symbol, in which case we're fine
749 * doing a normal elf_add_reloc after first sanity-checking
750 * that the offset from the symbol is zero.
753 for (i
= 0; i
< nsects
; i
++)
754 if (segment
== sects
[i
]->index
) {
761 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
763 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
767 srb
= rb_search(s
->gsyms
, offset
);
768 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
769 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
770 " for this reference");
773 sym
= container_of(srb
, struct elf_symbol
, symv
);
775 r
= *sect
->tail
= nasm_malloc(sizeof(struct elf_reloc
));
776 sect
->tail
= &r
->next
;
779 r
->address
= sect
->len
;
780 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
781 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
788 static void elf32_out(int32_t segto
, const void *data
,
789 enum out_type type
, uint64_t size
,
790 int32_t segment
, int32_t wrt
)
792 struct elf_section
*s
;
796 static struct symlininfo sinfo
;
799 * handle absolute-assembly (structure definitions)
801 if (segto
== NO_SEG
) {
802 if (type
!= OUT_RESERVE
)
803 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
809 for (i
= 0; i
< nsects
; i
++)
810 if (segto
== sects
[i
]->index
) {
815 int tempint
; /* ignored */
816 if (segto
!= elf_section_names(".text", 2, &tempint
))
817 nasm_panic(0, "strange segment conditions in ELF driver");
819 s
= sects
[nsects
- 1];
824 /* again some stabs debugging stuff */
825 sinfo
.offset
= s
->len
;
828 sinfo
.name
= s
->name
;
829 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
830 /* end of debugging stuff */
832 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
833 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
834 " BSS section `%s': ignored", s
->name
);
835 s
->len
+= realsize(type
, size
);
841 if (s
->type
== SHT_PROGBITS
) {
842 nasm_error(ERR_WARNING
, "uninitialized space declared in"
843 " non-BSS section `%s': zeroing", s
->name
);
844 elf_sect_write(s
, NULL
, size
);
850 if (segment
!= NO_SEG
)
851 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
852 elf_sect_write(s
, data
, size
);
858 int asize
= abs((int)size
);
860 addr
= *(int64_t *)data
;
861 if (segment
!= NO_SEG
) {
863 nasm_error(ERR_NONFATAL
, "ELF format does not support"
864 " segment base references");
868 * The if() is a hack to deal with compilers which
869 * don't handle switch() statements with 64-bit
875 elf_add_reloc(s
, segment
, 0, R_386_8
);
879 elf_add_reloc(s
, segment
, 0, R_386_16
);
882 elf_add_reloc(s
, segment
, 0, R_386_32
);
884 default: /* Error issued further down */
887 } else if (wrt
== elf_gotpc_sect
+ 1) {
889 * The user will supply GOT relative to $$. ELF
890 * will let us have GOT relative to $. So we
891 * need to fix up the data item by $-$$.
894 elf_add_reloc(s
, segment
, 0, R_386_GOTPC
);
895 } else if (wrt
== elf_gotoff_sect
+ 1) {
896 elf_add_reloc(s
, segment
, 0, R_386_GOTOFF
);
897 } else if (wrt
== elf_tlsie_sect
+ 1) {
898 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
900 } else if (wrt
== elf_got_sect
+ 1) {
901 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
903 } else if (wrt
== elf_sym_sect
+ 1) {
907 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
912 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
916 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
922 } else if (wrt
== elf_plt_sect
+ 1) {
923 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
924 "relative PLT references");
926 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
928 wrt
= NO_SEG
; /* we can at least _try_ to continue */
934 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
935 "8- or 16-bit relocations in ELF32 is a GNU extension");
936 } else if (asize
!= 4 && segment
!= NO_SEG
) {
937 nasm_error(ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
939 elf_sect_writeaddr(s
, addr
, asize
);
948 reltype
= R_386_PC16
;
953 addr
= *(int64_t *)data
- size
;
954 nasm_assert(segment
!= segto
);
955 if (segment
!= NO_SEG
&& segment
% 2) {
956 nasm_error(ERR_NONFATAL
, "ELF format does not support"
957 " segment base references");
960 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
961 "8- or 16-bit relocations in ELF is a GNU extension");
962 elf_add_reloc(s
, segment
, 0, reltype
);
964 nasm_error(ERR_NONFATAL
,
965 "Unsupported non-32-bit ELF relocation");
968 elf_sect_writeaddr(s
, addr
, bytes
);
972 addr
= *(int64_t *)data
- size
;
973 if (segment
== segto
)
974 nasm_panic(0, "intra-segment OUT_REL4ADR");
975 if (segment
!= NO_SEG
&& segment
% 2) {
976 nasm_error(ERR_NONFATAL
, "ELF format does not support"
977 " segment base references");
980 elf_add_reloc(s
, segment
, 0, R_386_PC32
);
981 } else if (wrt
== elf_plt_sect
+ 1) {
982 elf_add_reloc(s
, segment
, 0, R_386_PLT32
);
983 } else if (wrt
== elf_gotpc_sect
+ 1 ||
984 wrt
== elf_gotoff_sect
+ 1 ||
985 wrt
== elf_got_sect
+ 1) {
986 nasm_error(ERR_NONFATAL
, "ELF format cannot produce PC-"
987 "relative GOT references");
989 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
991 wrt
= NO_SEG
; /* we can at least _try_ to continue */
994 elf_sect_writeaddr(s
, addr
, 4);
998 nasm_error(ERR_NONFATAL
, "32-bit ELF format does not support 64-bit relocations");
1000 elf_sect_writeaddr(s
, addr
, 8);
1004 static void elf64_out(int32_t segto
, const void *data
,
1005 enum out_type type
, uint64_t size
,
1006 int32_t segment
, int32_t wrt
)
1008 struct elf_section
*s
;
1012 static struct symlininfo sinfo
;
1015 * handle absolute-assembly (structure definitions)
1017 if (segto
== NO_SEG
) {
1018 if (type
!= OUT_RESERVE
)
1019 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
1025 for (i
= 0; i
< nsects
; i
++)
1026 if (segto
== sects
[i
]->index
) {
1031 int tempint
; /* ignored */
1032 if (segto
!= elf_section_names(".text", 2, &tempint
))
1033 nasm_panic(0, "strange segment conditions in ELF driver");
1035 s
= sects
[nsects
- 1];
1040 /* again some stabs debugging stuff */
1041 sinfo
.offset
= s
->len
;
1043 sinfo
.segto
= segto
;
1044 sinfo
.name
= s
->name
;
1045 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
1046 /* end of debugging stuff */
1048 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
1049 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
1050 " BSS section `%s': ignored", s
->name
);
1051 s
->len
+= realsize(type
, size
);
1057 if (s
->type
== SHT_PROGBITS
) {
1058 nasm_error(ERR_WARNING
, "uninitialized space declared in"
1059 " non-BSS section `%s': zeroing", s
->name
);
1060 elf_sect_write(s
, NULL
, size
);
1066 if (segment
!= NO_SEG
)
1067 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1068 elf_sect_write(s
, data
, size
);
1073 int isize
= (int)size
;
1074 int asize
= abs((int)size
);
1076 addr
= *(int64_t *)data
;
1077 if (segment
== NO_SEG
) {
1079 } else if (segment
% 2) {
1080 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1081 " segment base references");
1083 if (wrt
== NO_SEG
) {
1087 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
1091 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
1094 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
1097 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
1101 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
1104 nasm_panic(0, "internal error elf64-hpa-871");
1108 } else if (wrt
== elf_gotpc_sect
+ 1) {
1110 * The user will supply GOT relative to $$. ELF
1111 * will let us have GOT relative to $. So we
1112 * need to fix up the data item by $-$$.
1115 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1117 } else if (wrt
== elf_gotoff_sect
+ 1) {
1119 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
1120 "references to be qword");
1122 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
1125 } else if (wrt
== elf_got_sect
+ 1) {
1128 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1129 R_X86_64_GOT32
, true);
1133 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1134 R_X86_64_GOT64
, true);
1138 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
1141 } else if (wrt
== elf_sym_sect
+ 1) {
1145 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1151 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1152 R_X86_64_16
, false);
1156 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1157 R_X86_64_32
, false);
1161 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1162 R_X86_64_32S
, false);
1167 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1168 R_X86_64_64
, false);
1172 nasm_panic(0, "internal error elf64-hpa-903");
1175 } else if (wrt
== elf_plt_sect
+ 1) {
1176 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1177 "relative PLT references");
1179 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
1183 elf_sect_writeaddr(s
, addr
, asize
);
1188 reltype
= R_X86_64_PC8
;
1193 reltype
= R_X86_64_PC16
;
1198 addr
= *(int64_t *)data
- size
;
1199 if (segment
== segto
)
1200 nasm_panic(0, "intra-segment OUT_REL1ADR");
1201 if (segment
== NO_SEG
) {
1203 } else if (segment
% 2) {
1204 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1205 " segment base references");
1207 if (wrt
== NO_SEG
) {
1208 elf_add_reloc(s
, segment
, addr
, reltype
);
1211 nasm_error(ERR_NONFATAL
,
1212 "Unsupported non-32-bit ELF relocation");
1215 elf_sect_writeaddr(s
, addr
, bytes
);
1219 addr
= *(int64_t *)data
- size
;
1220 if (segment
== segto
)
1221 nasm_panic(0, "intra-segment OUT_REL4ADR");
1222 if (segment
== NO_SEG
) {
1224 } else if (segment
% 2) {
1225 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
1226 " segment base references");
1228 if (wrt
== NO_SEG
) {
1229 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1231 } else if (wrt
== elf_plt_sect
+ 1) {
1232 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1233 R_X86_64_PLT32
, true);
1235 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1236 wrt
== elf_got_sect
+ 1) {
1237 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1238 R_X86_64_GOTPCREL
, true);
1240 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1241 wrt
== elf_got_sect
+ 1) {
1242 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1244 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1245 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1246 R_X86_64_GOTTPOFF
, true);
1249 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1253 elf_sect_writeaddr(s
, addr
, 4);
1257 addr
= *(int64_t *)data
- size
;
1258 if (segment
== segto
)
1259 nasm_panic(0, "intra-segment OUT_REL8ADR");
1260 if (segment
== NO_SEG
) {
1262 } else if (segment
% 2) {
1263 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
1264 " segment base references");
1266 if (wrt
== NO_SEG
) {
1267 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1269 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1270 wrt
== elf_got_sect
+ 1) {
1271 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1272 R_X86_64_GOTPCREL64
, true);
1274 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1275 wrt
== elf_got_sect
+ 1) {
1276 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1278 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1279 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1282 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1286 elf_sect_writeaddr(s
, addr
, 8);
1291 static void elfx32_out(int32_t segto
, const void *data
,
1292 enum out_type type
, uint64_t size
,
1293 int32_t segment
, int32_t wrt
)
1295 struct elf_section
*s
;
1299 static struct symlininfo sinfo
;
1302 * handle absolute-assembly (structure definitions)
1304 if (segto
== NO_SEG
) {
1305 if (type
!= OUT_RESERVE
)
1306 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
1312 for (i
= 0; i
< nsects
; i
++)
1313 if (segto
== sects
[i
]->index
) {
1318 int tempint
; /* ignored */
1319 if (segto
!= elf_section_names(".text", 2, &tempint
))
1320 nasm_panic(0, "strange segment conditions in ELF driver");
1322 s
= sects
[nsects
- 1];
1327 /* again some stabs debugging stuff */
1328 sinfo
.offset
= s
->len
;
1330 sinfo
.segto
= segto
;
1331 sinfo
.name
= s
->name
;
1332 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
1333 /* end of debugging stuff */
1335 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
1336 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
1337 " BSS section `%s': ignored", s
->name
);
1338 s
->len
+= realsize(type
, size
);
1344 if (s
->type
== SHT_PROGBITS
) {
1345 nasm_error(ERR_WARNING
, "uninitialized space declared in"
1346 " non-BSS section `%s': zeroing", s
->name
);
1347 elf_sect_write(s
, NULL
, size
);
1353 if (segment
!= NO_SEG
)
1354 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1355 elf_sect_write(s
, data
, size
);
1360 int isize
= (int)size
;
1361 int asize
= abs((int)size
);
1363 addr
= *(int64_t *)data
;
1364 if (segment
== NO_SEG
) {
1366 } else if (segment
% 2) {
1367 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1368 " segment base references");
1370 if (wrt
== NO_SEG
) {
1374 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
1378 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
1381 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
1384 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
1388 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
1391 nasm_panic(0, "internal error elfx32-hpa-871");
1395 } else if (wrt
== elf_gotpc_sect
+ 1) {
1397 * The user will supply GOT relative to $$. ELF
1398 * will let us have GOT relative to $. So we
1399 * need to fix up the data item by $-$$.
1402 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1404 } else if (wrt
== elf_gotoff_sect
+ 1) {
1405 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
1406 "R_X86_64_GOTOFF64");
1407 } else if (wrt
== elf_got_sect
+ 1) {
1410 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1411 R_X86_64_GOT32
, true);
1415 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
1418 } else if (wrt
== elf_sym_sect
+ 1) {
1422 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1428 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1429 R_X86_64_16
, false);
1433 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1434 R_X86_64_32
, false);
1438 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1439 R_X86_64_32S
, false);
1444 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1445 R_X86_64_64
, false);
1449 nasm_panic(0, "internal error elfx32-hpa-903");
1452 } else if (wrt
== elf_plt_sect
+ 1) {
1453 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1454 "relative PLT references");
1456 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
1460 elf_sect_writeaddr(s
, addr
, asize
);
1465 reltype
= R_X86_64_PC8
;
1470 reltype
= R_X86_64_PC16
;
1475 addr
= *(int64_t *)data
- size
;
1476 if (segment
== segto
)
1477 nasm_panic(0, "intra-segment OUT_REL1ADR");
1478 if (segment
== NO_SEG
) {
1480 } else if (segment
% 2) {
1481 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1482 " segment base references");
1484 if (wrt
== NO_SEG
) {
1485 elf_add_reloc(s
, segment
, addr
, reltype
);
1488 nasm_error(ERR_NONFATAL
,
1489 "Unsupported non-32-bit ELF relocation");
1492 elf_sect_writeaddr(s
, addr
, bytes
);
1496 addr
= *(int64_t *)data
- size
;
1497 if (segment
== segto
)
1498 nasm_panic(0, "intra-segment OUT_REL4ADR");
1499 if (segment
== NO_SEG
) {
1501 } else if (segment
% 2) {
1502 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
1503 " segment base references");
1505 if (wrt
== NO_SEG
) {
1506 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1508 } else if (wrt
== elf_plt_sect
+ 1) {
1509 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1510 R_X86_64_PLT32
, true);
1512 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1513 wrt
== elf_got_sect
+ 1) {
1514 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1515 R_X86_64_GOTPCREL
, true);
1517 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1518 wrt
== elf_got_sect
+ 1) {
1519 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
1520 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1521 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1522 R_X86_64_GOTTPOFF
, true);
1525 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
1529 elf_sect_writeaddr(s
, addr
, 4);
1533 nasm_error(ERR_NONFATAL
, "32-bit ELF format does not support 64-bit relocations");
1535 elf_sect_writeaddr(s
, addr
, 8);
1540 static void elf_write(void)
1547 int32_t symtablen
, symtablocal
;
1550 * Work out how many sections we will have. We have SHN_UNDEF,
1551 * then the flexible user sections, then the fixed sections
1552 * `.shstrtab', `.symtab' and `.strtab', then optionally
1553 * relocation sections for the user sections.
1555 nsections
= sec_numspecial
+ 1;
1556 if (dfmt
== &df_stabs
)
1558 else if (dfmt
== &df_dwarf
)
1561 add_sectname("", ".shstrtab");
1562 add_sectname("", ".symtab");
1563 add_sectname("", ".strtab");
1564 for (i
= 0; i
< nsects
; i
++) {
1565 nsections
++; /* for the section itself */
1566 if (sects
[i
]->head
) {
1567 nsections
++; /* for its relocations */
1568 add_sectname(is_elf32() ? ".rel" : ".rela", sects
[i
]->name
);
1572 if (dfmt
== &df_stabs
) {
1573 /* in case the debug information is wanted, just add these three sections... */
1574 add_sectname("", ".stab");
1575 add_sectname("", ".stabstr");
1576 add_sectname(".rel", ".stab");
1577 } else if (dfmt
== &df_dwarf
) {
1578 /* the dwarf debug standard specifies the following ten sections,
1579 not all of which are currently implemented,
1580 although all of them are defined. */
1581 #define debug_aranges (int64_t) (nsections-10)
1582 #define debug_info (int64_t) (nsections-7)
1583 #define debug_abbrev (int64_t) (nsections-5)
1584 #define debug_line (int64_t) (nsections-4)
1585 add_sectname("", ".debug_aranges");
1586 add_sectname(".rela", ".debug_aranges");
1587 add_sectname("", ".debug_pubnames");
1588 add_sectname("", ".debug_info");
1589 add_sectname(".rela", ".debug_info");
1590 add_sectname("", ".debug_abbrev");
1591 add_sectname("", ".debug_line");
1592 add_sectname(".rela", ".debug_line");
1593 add_sectname("", ".debug_frame");
1594 add_sectname("", ".debug_loc");
1598 * Output the ELF header.
1600 if (is_elf32() || is_elfx32()) {
1601 nasm_write("\177ELF\1\1\1", 7, ofile
);
1602 fputc(elf_osabi
, ofile
);
1603 fputc(elf_abiver
, ofile
);
1604 fwritezero(7, ofile
);
1605 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1606 fwriteint16_t(is_elf32() ? EM_386
: EM_X86_64
, ofile
); /* processor ID */
1607 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1608 fwriteint32_t(0L, ofile
); /* no entry point */
1609 fwriteint32_t(0L, ofile
); /* no program header table */
1610 fwriteint32_t(0x40L
, ofile
); /* section headers straight after ELF header plus alignment */
1611 fwriteint32_t(0L, ofile
); /* no special flags */
1612 fwriteint16_t(0x34, ofile
); /* size of ELF header */
1613 fwriteint16_t(0, ofile
); /* no program header table, again */
1614 fwriteint16_t(0, ofile
); /* still no program header table */
1615 fwriteint16_t(sizeof(Elf32_Shdr
), ofile
); /* size of section header */
1616 fwriteint16_t(nsections
, ofile
); /* number of sections */
1617 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for section header table */
1619 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
1620 fwriteint32_t(0L, ofile
);
1621 fwriteint32_t(0L, ofile
);
1623 nasm_assert(is_elf64());
1624 nasm_write("\177ELF\2\1\1", 7, ofile
);
1625 fputc(elf_osabi
, ofile
);
1626 fputc(elf_abiver
, ofile
);
1627 fwritezero(7, ofile
);
1628 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1629 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1630 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1631 fwriteint64_t(0L, ofile
); /* no entry point */
1632 fwriteint64_t(0L, ofile
); /* no program header table */
1633 fwriteint64_t(0x40L
, ofile
); /* section headers straight after ELF header plus alignment */
1634 fwriteint32_t(0L, ofile
); /* no special flags */
1635 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1636 fwriteint16_t(0, ofile
); /* no program header table, again */
1637 fwriteint16_t(0, ofile
); /* still no program header table */
1638 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1639 fwriteint16_t(nsections
, ofile
); /* number of sections */
1640 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for section header table */
1644 * Build the symbol table and relocation tables.
1646 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1647 for (i
= 0; i
< nsects
; i
++)
1649 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1653 * Now output the section header table.
1656 elf_foffs
= 0x40 + (is_elf64() ? sizeof(Elf64_Shdr
): sizeof(Elf32_Shdr
)) * nsections
;
1657 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1660 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1663 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1666 /* The normal sections */
1667 for (i
= 0; i
< nsects
; i
++) {
1668 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1669 (sects
[i
]->type
== SHT_PROGBITS
?
1670 sects
[i
]->data
: NULL
), true,
1671 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1676 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1677 shstrtablen
, 0, 0, 1, 0);
1682 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1683 symtablen
, sec_strtab
, symtablocal
, 8, 24);
1685 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1686 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1690 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1691 strslen
, 0, 0, 1, 0);
1694 /* The relocation sections */
1696 for (i
= 0; i
< nsects
; i
++) {
1697 if (sects
[i
]->head
) {
1698 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1699 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1703 } else if (is_elfx32()) {
1704 for (i
= 0; i
< nsects
; i
++) {
1705 if (sects
[i
]->head
) {
1706 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1707 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 12);
1712 nasm_assert(is_elf64());
1713 for (i
= 0; i
< nsects
; i
++) {
1714 if (sects
[i
]->head
) {
1715 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1716 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 8, 24);
1722 if (dfmt
== &df_stabs
) {
1723 /* for debugging information, create the last three sections
1724 which are the .stab , .stabstr and .rel.stab sections respectively */
1726 /* this function call creates the stab sections in memory */
1729 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1730 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1731 stablen
, sec_stabstr
, 0, 4, 12);
1734 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1735 stabstrlen
, 0, 0, 4, 0);
1738 /* link -> symtable info -> section to refer to */
1739 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1740 stabrellen
, sec_symtab
, sec_stab
, 4, is_elf64() ? 16 : 8);
1743 } else if (dfmt
== &df_dwarf
) {
1744 /* for dwarf debugging information, create the ten dwarf sections */
1746 /* this function call creates the dwarf sections in memory */
1750 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1751 arangeslen
, 0, 0, 1, 0);
1754 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1755 arangesrellen
, sec_symtab
,
1756 is_elf64() ? debug_aranges
: sec_debug_aranges
,
1757 1, is_elf64() ? 24 : 12);
1760 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1761 false, pubnameslen
, 0, 0, 1, 0);
1764 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1765 infolen
, 0, 0, 1, 0);
1768 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1769 inforellen
, sec_symtab
,
1770 is_elf64() ? debug_info
: sec_debug_info
,
1771 1, is_elf64() ? 24 : 12);
1774 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1775 abbrevlen
, 0, 0, 1, 0);
1778 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1779 linelen
, 0, 0, 1, 0);
1782 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1783 linerellen
, sec_symtab
,
1784 is_elf64() ? debug_line
: sec_debug_line
,
1785 1, is_elf64() ? 24 : 12);
1788 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1789 framelen
, 0, 0, 8, 0);
1792 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1793 loclen
, 0, 0, 1, 0);
1796 fwritezero(align
, ofile
);
1799 * Now output the sections.
1801 elf_write_sections();
1803 nasm_free(elf_sects
);
1807 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1809 struct SAA
*s
= saa_init(1L);
1810 struct elf_symbol
*sym
;
1811 uint8_t entry
[24], *p
;
1817 * First, an all-zeros entry, required by the ELF spec.
1819 saa_wbytes(s
, NULL
, is_elf64() ? 24L : 16L); /* null symbol table entry */
1820 *len
+= is_elf64() ? 24L : 16L;
1824 * Next, an entry for the file name.
1828 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1829 WRITESHORT(p
, STT_FILE
); /* type FILE */
1830 WRITESHORT(p
, SHN_ABS
);
1831 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1832 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1833 saa_wbytes(s
, entry
, 24L);
1837 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1838 WRITELONG(p
, 0); /* no value */
1839 WRITELONG(p
, 0); /* no size either */
1840 WRITESHORT(p
, STT_FILE
); /* type FILE */
1841 WRITESHORT(p
, SHN_ABS
);
1842 saa_wbytes(s
, entry
, 16L);
1848 * Now some standard symbols defining the segments, for relocation
1852 for (i
= 1; i
<= nsects
; i
++) {
1854 WRITELONG(p
, 0); /* no symbol name */
1855 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1856 WRITESHORT(p
, i
); /* section id */
1857 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1858 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1859 saa_wbytes(s
, entry
, 24L);
1864 for (i
= 1; i
<= nsects
; i
++) {
1866 WRITELONG(p
, 0); /* no symbol name */
1867 WRITELONG(p
, 0); /* offset zero */
1868 WRITELONG(p
, 0); /* size zero */
1869 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1870 WRITESHORT(p
, i
); /* section id */
1871 saa_wbytes(s
, entry
, 16L);
1878 * Now the other local symbols.
1882 while ((sym
= saa_rstruct(syms
))) {
1883 if (sym
->type
& SYM_GLOBAL
)
1886 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1887 WRITECHAR(p
, sym
->type
); /* type and binding */
1888 WRITECHAR(p
, sym
->other
); /* visibility */
1889 WRITESHORT(p
, sym
->section
); /* index into section header table */
1890 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1891 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1892 saa_wbytes(s
, entry
, 24L);
1897 * dwarf needs symbols for debug sections
1898 * which are relocation targets.
1900 if (dfmt
== &df_dwarf
) {
1901 dwarf_infosym
= *local
;
1903 WRITELONG(p
, 0); /* no symbol name */
1904 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1905 WRITESHORT(p
, debug_info
); /* section id */
1906 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1907 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1908 saa_wbytes(s
, entry
, 24L);
1911 dwarf_abbrevsym
= *local
;
1913 WRITELONG(p
, 0); /* no symbol name */
1914 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1915 WRITESHORT(p
, debug_abbrev
); /* section id */
1916 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1917 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1918 saa_wbytes(s
, entry
, 24L);
1921 dwarf_linesym
= *local
;
1923 WRITELONG(p
, 0); /* no symbol name */
1924 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1925 WRITESHORT(p
, debug_line
); /* section id */
1926 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1927 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1928 saa_wbytes(s
, entry
, 24L);
1933 while ((sym
= saa_rstruct(syms
))) {
1934 if (sym
->type
& SYM_GLOBAL
)
1937 WRITELONG(p
, sym
->strpos
);
1938 WRITELONG(p
, sym
->symv
.key
);
1939 WRITELONG(p
, sym
->size
);
1940 WRITECHAR(p
, sym
->type
); /* type and binding */
1941 WRITECHAR(p
, sym
->other
); /* visibility */
1942 WRITESHORT(p
, sym
->section
);
1943 saa_wbytes(s
, entry
, 16L);
1948 * dwarf needs symbols for debug sections
1949 * which are relocation targets.
1951 if (dfmt
== &df_dwarf
) {
1952 dwarf_infosym
= *local
;
1954 WRITELONG(p
, 0); /* no symbol name */
1955 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1956 WRITELONG(p
, (uint32_t) 0); /* size zero */
1957 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1958 WRITESHORT(p
, sec_debug_info
); /* section id */
1959 saa_wbytes(s
, entry
, 16L);
1962 dwarf_abbrevsym
= *local
;
1964 WRITELONG(p
, 0); /* no symbol name */
1965 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1966 WRITELONG(p
, (uint32_t) 0); /* size zero */
1967 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1968 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1969 saa_wbytes(s
, entry
, 16L);
1972 dwarf_linesym
= *local
;
1974 WRITELONG(p
, 0); /* no symbol name */
1975 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1976 WRITELONG(p
, (uint32_t) 0); /* size zero */
1977 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1978 WRITESHORT(p
, sec_debug_line
); /* section id */
1979 saa_wbytes(s
, entry
, 16L);
1986 * Now the global symbols.
1990 while ((sym
= saa_rstruct(syms
))) {
1991 if (!(sym
->type
& SYM_GLOBAL
))
1994 WRITELONG(p
, sym
->strpos
);
1995 WRITECHAR(p
, sym
->type
); /* type and binding */
1996 WRITECHAR(p
, sym
->other
); /* visibility */
1997 WRITESHORT(p
, sym
->section
);
1998 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1999 WRITEDLONG(p
, (int64_t)sym
->size
);
2000 saa_wbytes(s
, entry
, 24L);
2004 while ((sym
= saa_rstruct(syms
))) {
2005 if (!(sym
->type
& SYM_GLOBAL
))
2008 WRITELONG(p
, sym
->strpos
);
2009 WRITELONG(p
, sym
->symv
.key
);
2010 WRITELONG(p
, sym
->size
);
2011 WRITECHAR(p
, sym
->type
); /* type and binding */
2012 WRITECHAR(p
, sym
->other
); /* visibility */
2013 WRITESHORT(p
, sym
->section
);
2014 saa_wbytes(s
, entry
, 16L);
2022 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
2025 uint8_t *p
, entry
[24];
2026 int32_t global_offset
;
2035 * How to onvert from a global placeholder to a real symbol index;
2036 * the +2 refers to the two special entries, the null entry and
2037 * the filename entry.
2039 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
2043 int32_t sym
= r
->symbol
;
2045 if (sym
>= GLOBAL_TEMP_BASE
)
2046 sym
+= global_offset
;
2049 WRITELONG(p
, r
->address
);
2050 WRITELONG(p
, (sym
<< 8) + r
->type
);
2051 saa_wbytes(s
, entry
, 8L);
2056 } else if (is_elfx32()) {
2058 int32_t sym
= r
->symbol
;
2060 if (sym
>= GLOBAL_TEMP_BASE
)
2061 sym
+= global_offset
;
2064 WRITELONG(p
, r
->address
);
2065 WRITELONG(p
, (sym
<< 8) + r
->type
);
2066 WRITELONG(p
, r
->offset
);
2067 saa_wbytes(s
, entry
, 12L);
2073 nasm_assert(is_elf64());
2075 int32_t sym
= r
->symbol
;
2077 if (sym
>= GLOBAL_TEMP_BASE
)
2078 sym
+= global_offset
;
2081 WRITEDLONG(p
, r
->address
);
2082 WRITELONG(p
, r
->type
);
2084 WRITEDLONG(p
, r
->offset
);
2085 saa_wbytes(s
, entry
, 24L);
2095 static void elf_section_header(int name
, int type
, uint64_t flags
,
2096 void *data
, bool is_saa
, uint64_t datalen
,
2097 int link
, int info
, int align
, int eltsize
)
2099 elf_sects
[elf_nsect
].data
= data
;
2100 elf_sects
[elf_nsect
].len
= datalen
;
2101 elf_sects
[elf_nsect
].is_saa
= is_saa
;
2105 fwriteint32_t((int32_t)name
, ofile
);
2106 fwriteint32_t((int32_t)type
, ofile
);
2107 fwriteint32_t((int32_t)flags
, ofile
);
2108 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
2109 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
2110 fwriteint32_t(datalen
, ofile
);
2112 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2113 fwriteint32_t((int32_t)link
, ofile
);
2114 fwriteint32_t((int32_t)info
, ofile
);
2115 fwriteint32_t((int32_t)align
, ofile
);
2116 fwriteint32_t((int32_t)eltsize
, ofile
);
2117 } else if (is_elfx32()) {
2118 fwriteint32_t((int32_t)name
, ofile
);
2119 fwriteint32_t((int32_t)type
, ofile
);
2120 fwriteint32_t((int32_t)flags
, ofile
);
2121 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
2122 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
2123 fwriteint32_t(datalen
, ofile
);
2125 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2126 fwriteint32_t((int32_t)link
, ofile
);
2127 fwriteint32_t((int32_t)info
, ofile
);
2128 fwriteint32_t((int32_t)align
, ofile
);
2129 fwriteint32_t((int32_t)eltsize
, ofile
);
2131 nasm_assert(is_elf64());
2132 fwriteint32_t((int32_t)name
, ofile
);
2133 fwriteint32_t((int32_t)type
, ofile
);
2134 fwriteint64_t((int64_t)flags
, ofile
);
2135 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
2136 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
2137 fwriteint64_t(datalen
, ofile
);
2139 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2140 fwriteint32_t((int32_t)link
, ofile
);
2141 fwriteint32_t((int32_t)info
, ofile
);
2142 fwriteint64_t((int64_t)align
, ofile
);
2143 fwriteint64_t((int64_t)eltsize
, ofile
);
2147 static void elf_write_sections(void)
2150 for (i
= 0; i
< elf_nsect
; i
++)
2151 if (elf_sects
[i
].data
) {
2152 int32_t len
= elf_sects
[i
].len
;
2153 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
2154 int32_t align
= reallen
- len
;
2155 if (elf_sects
[i
].is_saa
)
2156 saa_fpwrite(elf_sects
[i
].data
, ofile
);
2158 nasm_write(elf_sects
[i
].data
, len
, ofile
);
2159 fwritezero(align
, ofile
);
2163 static void elf_sect_write(struct elf_section
*sect
, const void *data
, size_t len
)
2165 saa_wbytes(sect
->data
, data
, len
);
2169 static void elf_sect_writeaddr(struct elf_section
*sect
, int64_t data
, size_t len
)
2171 saa_writeaddr(sect
->data
, data
, len
);
2175 static void elf_sectalign(int32_t seg
, unsigned int value
)
2177 struct elf_section
*s
= NULL
;
2180 for (i
= 0; i
< nsects
; i
++) {
2181 if (sects
[i
]->index
== seg
) {
2186 if (!s
|| !is_power2(value
))
2189 if (value
> s
->align
)
2193 static int32_t elf_segbase(int32_t segment
)
2198 static void elf_filename(char *inname
, char *outname
)
2200 strcpy(elf_module
, inname
);
2201 standard_extension(inname
, outname
, ".o");
2204 extern macros_t elf_stdmac
[];
2206 static int elf_set_info(enum geninfo type
, char **val
)
2213 static const struct dfmt elf32_df_dwarf
= {
2214 "ELF32 (i386) dwarf debug format for Linux/Unix",
2218 null_debug_deflabel
,
2219 null_debug_directive
,
2225 static const struct dfmt elf32_df_stabs
= {
2226 "ELF32 (i386) stabs debug format for Linux/Unix",
2230 null_debug_deflabel
,
2231 null_debug_directive
,
2237 static const struct dfmt
* const elf32_debugs_arr
[3] =
2238 { &elf32_df_dwarf
, &elf32_df_stabs
, NULL
};
2240 const struct ofmt of_elf32
= {
2241 "ELF32 (i386) object files (e.g. Linux)",
2260 static const struct dfmt elf64_df_dwarf
= {
2261 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
2265 null_debug_deflabel
,
2266 null_debug_directive
,
2272 static const struct dfmt elf64_df_stabs
= {
2273 "ELF64 (x86-64) stabs debug format for Linux/Unix",
2277 null_debug_deflabel
,
2278 null_debug_directive
,
2284 static const struct dfmt
* const elf64_debugs_arr
[3] =
2285 { &elf64_df_dwarf
, &elf64_df_stabs
, NULL
};
2287 const struct ofmt of_elf64
= {
2288 "ELF64 (x86_64) object files (e.g. Linux)",
2307 static const struct dfmt elfx32_df_dwarf
= {
2308 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
2312 null_debug_deflabel
,
2313 null_debug_directive
,
2319 static const struct dfmt elfx32_df_stabs
= {
2320 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
2324 null_debug_deflabel
,
2325 null_debug_directive
,
2331 static const struct dfmt
* const elfx32_debugs_arr
[3] =
2332 { &elfx32_df_dwarf
, &elfx32_df_stabs
, NULL
};
2334 const struct ofmt of_elfx32
= {
2335 "ELFX32 (x86_64) object files (e.g. Linux)",
2354 static bool is_elf64(void)
2356 return ofmt
== &of_elf64
;
2359 static bool is_elf32(void)
2361 return ofmt
== &of_elf32
;
2364 static bool is_elfx32(void)
2366 return ofmt
== &of_elfx32
;
2369 /* common debugging routines */
2370 static void debug_typevalue(int32_t type
)
2372 int32_t stype
, ssize
;
2373 switch (TYM_TYPE(type
)) {
2416 stype
= STT_SECTION
;
2431 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
2432 lastsym
->size
= ssize
;
2433 lastsym
->type
= stype
;
2437 /* stabs debugging routines */
2439 static void stabs_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
2442 if (!stabs_filename
) {
2443 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
2444 strcpy(stabs_filename
, filename
);
2446 if (strcmp(stabs_filename
, filename
)) {
2447 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2448 in fact, this leak comes in quite handy to maintain a list of files
2449 encountered so far in the symbol lines... */
2451 /* why not nasm_free(stabs_filename); we're done with the old one */
2453 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
2454 strcpy(stabs_filename
, filename
);
2458 currentline
= linenumber
;
2461 static void stabs_output(int type
, void *param
)
2463 struct symlininfo
*s
;
2464 struct linelist
*el
;
2465 if (type
== TY_DEBUGSYMLIN
) {
2466 if (debug_immcall
) {
2467 s
= (struct symlininfo
*)param
;
2468 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2469 return; /* line info is only collected for executable sections */
2471 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2472 el
->info
.offset
= s
->offset
;
2473 el
->info
.section
= s
->section
;
2474 el
->info
.name
= s
->name
;
2475 el
->line
= currentline
;
2476 el
->filename
= stabs_filename
;
2479 stabslines
->last
->next
= el
;
2480 stabslines
->last
= el
;
2483 stabslines
->last
= el
;
2490 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2492 static void stabs_generate(void)
2494 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
2495 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
2499 struct linelist
*ptr
;
2503 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
2506 if (numfiles
== 0) {
2507 allfiles
[0] = ptr
->filename
;
2510 for (i
= 0; i
< numfiles
; i
++) {
2511 if (!strcmp(allfiles
[i
], ptr
->filename
))
2514 if (i
>= numfiles
) {
2515 allfiles
[i
] = ptr
->filename
;
2522 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
2523 for (i
= 0; i
< numfiles
; i
++) {
2524 fileidx
[i
] = strsize
;
2525 strsize
+= strlen(allfiles
[i
]) + 1;
2528 for (i
= 0; i
< numfiles
; i
++) {
2529 if (!strcmp(allfiles
[i
], elf_module
)) {
2536 * worst case size of the stab buffer would be:
2537 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2538 * plus one "ending" entry
2540 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
2541 sizeof(struct stabentry
));
2542 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
2543 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* (is_elf64() ? 16 : 8) * (2 + 3));
2546 for (i
= 0; i
< numfiles
; i
++)
2547 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
2550 stabstrlen
= strsize
; /* set global variable for length of stab strings */
2558 * this is the first stab, its strx points to the filename of the
2559 * the source-file, the n_desc field should be set to the number
2560 * of remaining stabs
2562 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, stabstrlen
);
2564 /* this is the stab for the main source file */
2565 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
2567 /* relocation table entry */
2570 * Since the symbol table has two entries before
2571 * the section symbols, the index in the info.section
2572 * member must be adjusted by adding 2
2576 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2577 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2578 } else if (is_elfx32()) {
2579 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2580 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2582 nasm_assert(is_elf64());
2583 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2584 WRITELONG(rptr
, R_X86_64_32
);
2585 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2588 currfile
= mainfileindex
;
2593 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2594 /* oops file has changed... */
2595 for (i
= 0; i
< numfiles
; i
++)
2596 if (!strcmp(allfiles
[i
], ptr
->filename
))
2599 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2603 /* relocation table entry */
2604 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2605 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2608 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2611 /* relocation table entry */
2612 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2613 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2617 } else if (is_elfx32()) {
2619 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2620 /* oops file has changed... */
2621 for (i
= 0; i
< numfiles
; i
++)
2622 if (!strcmp(allfiles
[i
], ptr
->filename
))
2625 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2629 /* relocation table entry */
2630 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2631 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2634 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2637 /* relocation table entry */
2638 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2639 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2644 nasm_assert(is_elf64());
2646 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2647 /* oops file has changed... */
2648 for (i
= 0; i
< numfiles
; i
++)
2649 if (!strcmp(allfiles
[i
], ptr
->filename
))
2652 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2656 /* relocation table entry */
2657 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2658 WRITELONG(rptr
, R_X86_64_32
);
2659 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2662 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2665 /* relocation table entry */
2666 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2667 WRITELONG(rptr
, R_X86_64_32
);
2668 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2674 /* this is an "ending" token */
2675 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
2678 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
2680 nasm_free(allfiles
);
2683 stablen
= (sptr
- sbuf
);
2684 stabrellen
= (rptr
- rbuf
);
2690 static void stabs_cleanup(void)
2692 struct linelist
*ptr
, *del
;
2704 nasm_free(stabrelbuf
);
2705 nasm_free(stabstrbuf
);
2708 /* dwarf routines */
2710 static void dwarf_init(void)
2712 ndebugs
= 3; /* 3 debug symbols */
2715 static void dwarf_linenum(const char *filename
, int32_t linenumber
,
2719 dwarf_findfile(filename
);
2721 currentline
= linenumber
;
2724 /* called from elf_out with type == TY_DEBUGSYMLIN */
2725 static void dwarf_output(int type
, void *param
)
2727 int ln
, aa
, inx
, maxln
, soc
;
2728 struct symlininfo
*s
;
2733 s
= (struct symlininfo
*)param
;
2735 /* line number info is only gathered for executable sections */
2736 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2739 /* Check if section index has changed */
2740 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
2741 dwarf_findsect(s
->section
);
2743 /* do nothing unless line or file has changed */
2747 ln
= currentline
- dwarf_csect
->line
;
2748 aa
= s
->offset
- dwarf_csect
->offset
;
2749 inx
= dwarf_clist
->line
;
2750 plinep
= dwarf_csect
->psaa
;
2751 /* check for file change */
2752 if (!(inx
== dwarf_csect
->file
)) {
2753 saa_write8(plinep
,DW_LNS_set_file
);
2754 saa_write8(plinep
,inx
);
2755 dwarf_csect
->file
= inx
;
2757 /* check for line change */
2759 /* test if in range of special op code */
2760 maxln
= line_base
+ line_range
;
2761 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
2762 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
2763 saa_write8(plinep
,soc
);
2765 saa_write8(plinep
,DW_LNS_advance_line
);
2766 saa_wleb128s(plinep
,ln
);
2768 saa_write8(plinep
,DW_LNS_advance_pc
);
2769 saa_wleb128u(plinep
,aa
);
2772 dwarf_csect
->line
= currentline
;
2773 dwarf_csect
->offset
= s
->offset
;
2776 /* show change handled */
2781 static void dwarf_generate(void)
2785 struct linelist
*ftentry
;
2786 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
2787 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
2788 struct sectlist
*psect
;
2789 size_t saalen
, linepoff
, totlen
, highaddr
;
2792 /* write epilogues for each line program range */
2793 /* and build aranges section */
2794 paranges
= saa_init(1L);
2795 parangesrel
= saa_init(1L);
2796 saa_write16(paranges
,2); /* dwarf version */
2797 saa_write32(parangesrel
, paranges
->datalen
+4);
2798 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
2799 saa_write32(parangesrel
, 0);
2800 saa_write32(paranges
,0); /* offset into info */
2801 saa_write8(paranges
,4); /* pointer size */
2802 saa_write8(paranges
,0); /* not segmented */
2803 saa_write32(paranges
,0); /* padding */
2804 /* iterate though sectlist entries */
2805 psect
= dwarf_fsect
;
2808 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2809 plinep
= psect
->psaa
;
2810 /* Line Number Program Epilogue */
2811 saa_write8(plinep
,2); /* std op 2 */
2812 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2813 saa_write8(plinep
,DW_LNS_extended_op
);
2814 saa_write8(plinep
,1); /* operand length */
2815 saa_write8(plinep
,DW_LNE_end_sequence
);
2816 totlen
+= plinep
->datalen
;
2817 /* range table relocation entry */
2818 saa_write32(parangesrel
, paranges
->datalen
+ 4);
2819 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2820 saa_write32(parangesrel
, (uint32_t) 0);
2821 /* range table entry */
2822 saa_write32(paranges
,0x0000); /* range start */
2823 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
2824 highaddr
+= sects
[psect
->section
]->len
;
2825 /* done with this entry */
2826 psect
= psect
->next
;
2828 saa_write32(paranges
,0); /* null address */
2829 saa_write32(paranges
,0); /* null length */
2830 saalen
= paranges
->datalen
;
2831 arangeslen
= saalen
+ 4;
2832 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2833 WRITELONG(pbuf
,saalen
); /* initial length */
2834 saa_rnbytes(paranges
, pbuf
, saalen
);
2836 } else if (is_elfx32()) {
2837 /* write epilogues for each line program range */
2838 /* and build aranges section */
2839 paranges
= saa_init(1L);
2840 parangesrel
= saa_init(1L);
2841 saa_write16(paranges
,3); /* dwarf version */
2842 saa_write32(parangesrel
, paranges
->datalen
+4);
2843 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_X86_64_32
); /* reloc to info */
2844 saa_write32(parangesrel
, 0);
2845 saa_write32(paranges
,0); /* offset into info */
2846 saa_write8(paranges
,4); /* pointer size */
2847 saa_write8(paranges
,0); /* not segmented */
2848 saa_write32(paranges
,0); /* padding */
2849 /* iterate though sectlist entries */
2850 psect
= dwarf_fsect
;
2853 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2854 plinep
= psect
->psaa
;
2855 /* Line Number Program Epilogue */
2856 saa_write8(plinep
,2); /* std op 2 */
2857 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2858 saa_write8(plinep
,DW_LNS_extended_op
);
2859 saa_write8(plinep
,1); /* operand length */
2860 saa_write8(plinep
,DW_LNE_end_sequence
);
2861 totlen
+= plinep
->datalen
;
2862 /* range table relocation entry */
2863 saa_write32(parangesrel
, paranges
->datalen
+ 4);
2864 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_X86_64_32
);
2865 saa_write32(parangesrel
, (uint32_t) 0);
2866 /* range table entry */
2867 saa_write32(paranges
,0x0000); /* range start */
2868 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
2869 highaddr
+= sects
[psect
->section
]->len
;
2870 /* done with this entry */
2871 psect
= psect
->next
;
2873 saa_write32(paranges
,0); /* null address */
2874 saa_write32(paranges
,0); /* null length */
2875 saalen
= paranges
->datalen
;
2876 arangeslen
= saalen
+ 4;
2877 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2878 WRITELONG(pbuf
,saalen
); /* initial length */
2879 saa_rnbytes(paranges
, pbuf
, saalen
);
2882 nasm_assert(is_elf64());
2883 /* write epilogues for each line program range */
2884 /* and build aranges section */
2885 paranges
= saa_init(1L);
2886 parangesrel
= saa_init(1L);
2887 saa_write16(paranges
,3); /* dwarf version */
2888 saa_write64(parangesrel
, paranges
->datalen
+4);
2889 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
2890 saa_write64(parangesrel
, 0);
2891 saa_write32(paranges
,0); /* offset into info */
2892 saa_write8(paranges
,8); /* pointer size */
2893 saa_write8(paranges
,0); /* not segmented */
2894 saa_write32(paranges
,0); /* padding */
2895 /* iterate though sectlist entries */
2896 psect
= dwarf_fsect
;
2899 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2900 plinep
= psect
->psaa
;
2901 /* Line Number Program Epilogue */
2902 saa_write8(plinep
,2); /* std op 2 */
2903 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2904 saa_write8(plinep
,DW_LNS_extended_op
);
2905 saa_write8(plinep
,1); /* operand length */
2906 saa_write8(plinep
,DW_LNE_end_sequence
);
2907 totlen
+= plinep
->datalen
;
2908 /* range table relocation entry */
2909 saa_write64(parangesrel
, paranges
->datalen
+ 4);
2910 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2911 saa_write64(parangesrel
, (uint64_t) 0);
2912 /* range table entry */
2913 saa_write64(paranges
,0x0000); /* range start */
2914 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
2915 highaddr
+= sects
[psect
->section
]->len
;
2916 /* done with this entry */
2917 psect
= psect
->next
;
2919 saa_write64(paranges
,0); /* null address */
2920 saa_write64(paranges
,0); /* null length */
2921 saalen
= paranges
->datalen
;
2922 arangeslen
= saalen
+ 4;
2923 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2924 WRITELONG(pbuf
,saalen
); /* initial length */
2925 saa_rnbytes(paranges
, pbuf
, saalen
);
2929 /* build rela.aranges section */
2930 arangesrellen
= saalen
= parangesrel
->datalen
;
2931 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2932 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2933 saa_free(parangesrel
);
2935 /* build pubnames section */
2936 ppubnames
= saa_init(1L);
2937 saa_write16(ppubnames
,3); /* dwarf version */
2938 saa_write32(ppubnames
,0); /* offset into info */
2939 saa_write32(ppubnames
,0); /* space used in info */
2940 saa_write32(ppubnames
,0); /* end of list */
2941 saalen
= ppubnames
->datalen
;
2942 pubnameslen
= saalen
+ 4;
2943 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2944 WRITELONG(pbuf
,saalen
); /* initial length */
2945 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2946 saa_free(ppubnames
);
2949 /* build info section */
2950 pinfo
= saa_init(1L);
2951 pinforel
= saa_init(1L);
2952 saa_write16(pinfo
,2); /* dwarf version */
2953 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2954 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
2955 saa_write32(pinforel
, 0);
2956 saa_write32(pinfo
,0); /* offset into abbrev */
2957 saa_write8(pinfo
,4); /* pointer size */
2958 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2959 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2960 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2961 saa_write32(pinforel
, 0);
2962 saa_write32(pinfo
,0); /* DW_AT_low_pc */
2963 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2964 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2965 saa_write32(pinforel
, 0);
2966 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
2967 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2968 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
2969 saa_write32(pinforel
, 0);
2970 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2971 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2972 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2973 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2974 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2975 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2976 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2977 saa_write32(pinforel
, 0);
2978 saa_write32(pinfo
,0); /* DW_AT_low_pc */
2979 saa_write32(pinfo
,0); /* DW_AT_frame_base */
2980 saa_write8(pinfo
,0); /* end of entries */
2981 saalen
= pinfo
->datalen
;
2982 infolen
= saalen
+ 4;
2983 infobuf
= pbuf
= nasm_malloc(infolen
);
2984 WRITELONG(pbuf
,saalen
); /* initial length */
2985 saa_rnbytes(pinfo
, pbuf
, saalen
);
2987 } else if (is_elfx32()) {
2988 /* build info section */
2989 pinfo
= saa_init(1L);
2990 pinforel
= saa_init(1L);
2991 saa_write16(pinfo
,3); /* dwarf version */
2992 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2993 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_X86_64_32
); /* reloc to abbrev */
2994 saa_write32(pinforel
, 0);
2995 saa_write32(pinfo
,0); /* offset into abbrev */
2996 saa_write8(pinfo
,4); /* pointer size */
2997 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2998 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2999 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3000 saa_write32(pinforel
, 0);
3001 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3002 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3003 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3004 saa_write32(pinforel
, 0);
3005 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
3006 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3007 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_X86_64_32
); /* reloc to line */
3008 saa_write32(pinforel
, 0);
3009 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3010 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3011 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3012 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3013 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3014 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3015 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3016 saa_write32(pinforel
, 0);
3017 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3018 saa_write32(pinfo
,0); /* DW_AT_frame_base */
3019 saa_write8(pinfo
,0); /* end of entries */
3020 saalen
= pinfo
->datalen
;
3021 infolen
= saalen
+ 4;
3022 infobuf
= pbuf
= nasm_malloc(infolen
);
3023 WRITELONG(pbuf
,saalen
); /* initial length */
3024 saa_rnbytes(pinfo
, pbuf
, saalen
);
3027 nasm_assert(is_elf64());
3028 /* build info section */
3029 pinfo
= saa_init(1L);
3030 pinforel
= saa_init(1L);
3031 saa_write16(pinfo
,3); /* dwarf version */
3032 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3033 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
3034 saa_write64(pinforel
, 0);
3035 saa_write32(pinfo
,0); /* offset into abbrev */
3036 saa_write8(pinfo
,8); /* pointer size */
3037 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
3038 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3039 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3040 saa_write64(pinforel
, 0);
3041 saa_write64(pinfo
,0); /* DW_AT_low_pc */
3042 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3043 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3044 saa_write64(pinforel
, 0);
3045 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
3046 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3047 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
3048 saa_write64(pinforel
, 0);
3049 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3050 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3051 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3052 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3053 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3054 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3055 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3056 saa_write64(pinforel
, 0);
3057 saa_write64(pinfo
,0); /* DW_AT_low_pc */
3058 saa_write64(pinfo
,0); /* DW_AT_frame_base */
3059 saa_write8(pinfo
,0); /* end of entries */
3060 saalen
= pinfo
->datalen
;
3061 infolen
= saalen
+ 4;
3062 infobuf
= pbuf
= nasm_malloc(infolen
);
3063 WRITELONG(pbuf
,saalen
); /* initial length */
3064 saa_rnbytes(pinfo
, pbuf
, saalen
);
3068 /* build rela.info section */
3069 inforellen
= saalen
= pinforel
->datalen
;
3070 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
3071 saa_rnbytes(pinforel
, pbuf
, saalen
);
3074 /* build abbrev section */
3075 pabbrev
= saa_init(1L);
3076 saa_write8(pabbrev
,1); /* entry number LEB128u */
3077 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
3078 saa_write8(pabbrev
,1); /* has children */
3079 /* the following attributes and forms are all LEB128u values */
3080 saa_write8(pabbrev
,DW_AT_low_pc
);
3081 saa_write8(pabbrev
,DW_FORM_addr
);
3082 saa_write8(pabbrev
,DW_AT_high_pc
);
3083 saa_write8(pabbrev
,DW_FORM_addr
);
3084 saa_write8(pabbrev
,DW_AT_stmt_list
);
3085 saa_write8(pabbrev
,DW_FORM_data4
);
3086 saa_write8(pabbrev
,DW_AT_name
);
3087 saa_write8(pabbrev
,DW_FORM_string
);
3088 saa_write8(pabbrev
,DW_AT_producer
);
3089 saa_write8(pabbrev
,DW_FORM_string
);
3090 saa_write8(pabbrev
,DW_AT_language
);
3091 saa_write8(pabbrev
,DW_FORM_data2
);
3092 saa_write16(pabbrev
,0); /* end of entry */
3093 /* LEB128u usage same as above */
3094 saa_write8(pabbrev
,2); /* entry number */
3095 saa_write8(pabbrev
,DW_TAG_subprogram
);
3096 saa_write8(pabbrev
,0); /* no children */
3097 saa_write8(pabbrev
,DW_AT_low_pc
);
3098 saa_write8(pabbrev
,DW_FORM_addr
);
3099 saa_write8(pabbrev
,DW_AT_frame_base
);
3100 saa_write8(pabbrev
,DW_FORM_data4
);
3101 saa_write16(pabbrev
,0); /* end of entry */
3102 abbrevlen
= saalen
= pabbrev
->datalen
;
3103 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
3104 saa_rnbytes(pabbrev
, pbuf
, saalen
);
3107 /* build line section */
3109 plines
= saa_init(1L);
3110 saa_write8(plines
,1); /* Minimum Instruction Length */
3111 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
3112 saa_write8(plines
,line_base
); /* Line Base */
3113 saa_write8(plines
,line_range
); /* Line Range */
3114 saa_write8(plines
,opcode_base
); /* Opcode Base */
3115 /* standard opcode lengths (# of LEB128u operands) */
3116 saa_write8(plines
,0); /* Std opcode 1 length */
3117 saa_write8(plines
,1); /* Std opcode 2 length */
3118 saa_write8(plines
,1); /* Std opcode 3 length */
3119 saa_write8(plines
,1); /* Std opcode 4 length */
3120 saa_write8(plines
,1); /* Std opcode 5 length */
3121 saa_write8(plines
,0); /* Std opcode 6 length */
3122 saa_write8(plines
,0); /* Std opcode 7 length */
3123 saa_write8(plines
,0); /* Std opcode 8 length */
3124 saa_write8(plines
,1); /* Std opcode 9 length */
3125 saa_write8(plines
,0); /* Std opcode 10 length */
3126 saa_write8(plines
,0); /* Std opcode 11 length */
3127 saa_write8(plines
,1); /* Std opcode 12 length */
3128 /* Directory Table */
3129 saa_write8(plines
,0); /* End of table */
3130 /* File Name Table */
3131 ftentry
= dwarf_flist
;
3132 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
3133 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
3134 saa_write8(plines
,0); /* directory LEB128u */
3135 saa_write8(plines
,0); /* time LEB128u */
3136 saa_write8(plines
,0); /* size LEB128u */
3137 ftentry
= ftentry
->next
;
3139 saa_write8(plines
,0); /* End of table */
3140 linepoff
= plines
->datalen
;
3141 linelen
= linepoff
+ totlen
+ 10;
3142 linebuf
= pbuf
= nasm_malloc(linelen
);
3143 WRITELONG(pbuf
,linelen
-4); /* initial length */
3144 WRITESHORT(pbuf
,3); /* dwarf version */
3145 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
3146 /* write line header */
3148 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
3151 /* concatonate line program ranges */
3153 plinesrel
= saa_init(1L);
3154 psect
= dwarf_fsect
;
3156 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3157 saa_write32(plinesrel
, linepoff
);
3158 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
3159 saa_write32(plinesrel
, (uint32_t) 0);
3160 plinep
= psect
->psaa
;
3161 saalen
= plinep
->datalen
;
3162 saa_rnbytes(plinep
, pbuf
, saalen
);
3166 /* done with this entry */
3167 psect
= psect
->next
;
3169 } else if (is_elfx32()) {
3170 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3171 saa_write32(plinesrel
, linepoff
);
3172 saa_write32(plinesrel
, ((psect
->section
+ 2) << 8) + R_X86_64_32
);
3173 saa_write32(plinesrel
, 0);
3174 plinep
= psect
->psaa
;
3175 saalen
= plinep
->datalen
;
3176 saa_rnbytes(plinep
, pbuf
, saalen
);
3180 /* done with this entry */
3181 psect
= psect
->next
;
3184 nasm_assert(is_elf64());
3185 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3186 saa_write64(plinesrel
, linepoff
);
3187 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
3188 saa_write64(plinesrel
, (uint64_t) 0);
3189 plinep
= psect
->psaa
;
3190 saalen
= plinep
->datalen
;
3191 saa_rnbytes(plinep
, pbuf
, saalen
);
3195 /* done with this entry */
3196 psect
= psect
->next
;
3200 /* build rela.lines section */
3201 linerellen
=saalen
= plinesrel
->datalen
;
3202 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
3203 saa_rnbytes(plinesrel
, pbuf
, saalen
);
3204 saa_free(plinesrel
);
3206 /* build frame section */
3208 framebuf
= pbuf
= nasm_malloc(framelen
);
3209 WRITELONG(pbuf
,framelen
-4); /* initial length */
3211 /* build loc section */
3213 locbuf
= pbuf
= nasm_malloc(loclen
);
3215 WRITELONG(pbuf
,0); /* null beginning offset */
3216 WRITELONG(pbuf
,0); /* null ending offset */
3218 nasm_assert(is_elf64());
3219 WRITEDLONG(pbuf
,0); /* null beginning offset */
3220 WRITEDLONG(pbuf
,0); /* null ending offset */
3224 static void dwarf_cleanup(void)
3226 nasm_free(arangesbuf
);
3227 nasm_free(arangesrelbuf
);
3228 nasm_free(pubnamesbuf
);
3230 nasm_free(inforelbuf
);
3231 nasm_free(abbrevbuf
);
3233 nasm_free(linerelbuf
);
3234 nasm_free(framebuf
);
3238 static void dwarf_findfile(const char * fname
)
3241 struct linelist
*match
;
3243 /* return if fname is current file name */
3244 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
3247 /* search for match */
3250 match
= dwarf_flist
;
3251 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
3252 if (!(strcmp(fname
, match
->filename
))) {
3253 dwarf_clist
= match
;
3259 /* add file name to end of list */
3260 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
3262 dwarf_clist
->line
= dwarf_numfiles
;
3263 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
3264 strcpy(dwarf_clist
->filename
,fname
);
3265 dwarf_clist
->next
= 0;
3266 if (!dwarf_flist
) { /* if first entry */
3267 dwarf_flist
= dwarf_elist
= dwarf_clist
;
3268 dwarf_clist
->last
= 0;
3269 } else { /* chain to previous entry */
3270 dwarf_elist
->next
= dwarf_clist
;
3271 dwarf_elist
= dwarf_clist
;
3275 static void dwarf_findsect(const int index
)
3278 struct sectlist
*match
;
3281 /* return if index is current section index */
3282 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
3285 /* search for match */
3288 match
= dwarf_fsect
;
3289 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
3290 if (match
->section
== index
) {
3291 dwarf_csect
= match
;
3294 match
= match
->next
;
3298 /* add entry to end of list */
3299 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
3301 dwarf_csect
->psaa
= plinep
= saa_init(1L);
3302 dwarf_csect
->line
= 1;
3303 dwarf_csect
->offset
= 0;
3304 dwarf_csect
->file
= 1;
3305 dwarf_csect
->section
= index
;
3306 dwarf_csect
->next
= 0;
3307 /* set relocatable address at start of line program */
3308 saa_write8(plinep
,DW_LNS_extended_op
);
3309 saa_write8(plinep
,is_elf64() ? 9 : 5); /* operand length */
3310 saa_write8(plinep
,DW_LNE_set_address
);
3312 saa_write64(plinep
,0); /* Start Address */
3314 saa_write32(plinep
,0); /* Start Address */
3316 if (!dwarf_fsect
) { /* if first entry */
3317 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
3318 dwarf_csect
->last
= 0;
3319 } else { /* chain to previous entry */
3320 dwarf_esect
->next
= dwarf_csect
;
3321 dwarf_esect
= dwarf_csect
;
3325 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */