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);
1008 static void elf64_out(int32_t segto
, const void *data
,
1009 enum out_type type
, uint64_t size
,
1010 int32_t segment
, int32_t wrt
)
1012 struct elf_section
*s
;
1016 static struct symlininfo sinfo
;
1019 * handle absolute-assembly (structure definitions)
1021 if (segto
== NO_SEG
) {
1022 if (type
!= OUT_RESERVE
)
1023 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
1029 for (i
= 0; i
< nsects
; i
++)
1030 if (segto
== sects
[i
]->index
) {
1035 int tempint
; /* ignored */
1036 if (segto
!= elf_section_names(".text", 2, &tempint
))
1037 nasm_panic(0, "strange segment conditions in ELF driver");
1039 s
= sects
[nsects
- 1];
1044 /* again some stabs debugging stuff */
1045 sinfo
.offset
= s
->len
;
1047 sinfo
.segto
= segto
;
1048 sinfo
.name
= s
->name
;
1049 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
1050 /* end of debugging stuff */
1052 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
1053 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
1054 " BSS section `%s': ignored", s
->name
);
1055 s
->len
+= realsize(type
, size
);
1061 if (s
->type
== SHT_PROGBITS
) {
1062 nasm_error(ERR_WARNING
, "uninitialized space declared in"
1063 " non-BSS section `%s': zeroing", s
->name
);
1064 elf_sect_write(s
, NULL
, size
);
1070 if (segment
!= NO_SEG
)
1071 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1072 elf_sect_write(s
, data
, size
);
1077 int isize
= (int)size
;
1078 int asize
= abs((int)size
);
1080 addr
= *(int64_t *)data
;
1081 if (segment
== NO_SEG
) {
1083 } else if (segment
% 2) {
1084 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1085 " segment base references");
1087 if (wrt
== NO_SEG
) {
1091 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
1095 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
1098 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
1101 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
1105 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
1108 nasm_panic(0, "internal error elf64-hpa-871");
1112 } else if (wrt
== elf_gotpc_sect
+ 1) {
1114 * The user will supply GOT relative to $$. ELF
1115 * will let us have GOT relative to $. So we
1116 * need to fix up the data item by $-$$.
1119 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1121 } else if (wrt
== elf_gotoff_sect
+ 1) {
1123 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
1124 "references to be qword");
1126 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
1129 } else if (wrt
== elf_got_sect
+ 1) {
1132 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1133 R_X86_64_GOT32
, true);
1137 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1138 R_X86_64_GOT64
, true);
1142 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
1145 } else if (wrt
== elf_sym_sect
+ 1) {
1149 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1155 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1156 R_X86_64_16
, false);
1160 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1161 R_X86_64_32
, false);
1165 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1166 R_X86_64_32S
, false);
1171 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1172 R_X86_64_64
, false);
1176 nasm_panic(0, "internal error elf64-hpa-903");
1179 } else if (wrt
== elf_plt_sect
+ 1) {
1180 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1181 "relative PLT references");
1183 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
1187 elf_sect_writeaddr(s
, addr
, asize
);
1192 reltype
= R_X86_64_PC8
;
1197 reltype
= R_X86_64_PC16
;
1202 addr
= *(int64_t *)data
- size
;
1203 if (segment
== segto
)
1204 nasm_panic(0, "intra-segment OUT_REL1ADR");
1205 if (segment
== NO_SEG
) {
1207 } else if (segment
% 2) {
1208 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1209 " segment base references");
1211 if (wrt
== NO_SEG
) {
1212 elf_add_reloc(s
, segment
, addr
, reltype
);
1215 nasm_error(ERR_NONFATAL
,
1216 "Unsupported non-32-bit ELF relocation");
1219 elf_sect_writeaddr(s
, addr
, bytes
);
1223 addr
= *(int64_t *)data
- size
;
1224 if (segment
== segto
)
1225 nasm_panic(0, "intra-segment OUT_REL4ADR");
1226 if (segment
== NO_SEG
) {
1228 } else if (segment
% 2) {
1229 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
1230 " segment base references");
1232 if (wrt
== NO_SEG
) {
1233 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1235 } else if (wrt
== elf_plt_sect
+ 1) {
1236 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1237 R_X86_64_PLT32
, true);
1239 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1240 wrt
== elf_got_sect
+ 1) {
1241 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1242 R_X86_64_GOTPCREL
, true);
1244 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1245 wrt
== elf_got_sect
+ 1) {
1246 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1248 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1249 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1250 R_X86_64_GOTTPOFF
, true);
1253 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1257 elf_sect_writeaddr(s
, addr
, 4);
1261 addr
= *(int64_t *)data
- size
;
1262 if (segment
== segto
)
1263 nasm_panic(0, "intra-segment OUT_REL8ADR");
1264 if (segment
== NO_SEG
) {
1266 } else if (segment
% 2) {
1267 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
1268 " segment base references");
1270 if (wrt
== NO_SEG
) {
1271 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1273 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1274 wrt
== elf_got_sect
+ 1) {
1275 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1276 R_X86_64_GOTPCREL64
, true);
1278 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1279 wrt
== elf_got_sect
+ 1) {
1280 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1282 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1283 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1286 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1290 elf_sect_writeaddr(s
, addr
, 8);
1298 static void elfx32_out(int32_t segto
, const void *data
,
1299 enum out_type type
, uint64_t size
,
1300 int32_t segment
, int32_t wrt
)
1302 struct elf_section
*s
;
1306 static struct symlininfo sinfo
;
1309 * handle absolute-assembly (structure definitions)
1311 if (segto
== NO_SEG
) {
1312 if (type
!= OUT_RESERVE
)
1313 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
1319 for (i
= 0; i
< nsects
; i
++)
1320 if (segto
== sects
[i
]->index
) {
1325 int tempint
; /* ignored */
1326 if (segto
!= elf_section_names(".text", 2, &tempint
))
1327 nasm_panic(0, "strange segment conditions in ELF driver");
1329 s
= sects
[nsects
- 1];
1334 /* again some stabs debugging stuff */
1335 sinfo
.offset
= s
->len
;
1337 sinfo
.segto
= segto
;
1338 sinfo
.name
= s
->name
;
1339 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
1340 /* end of debugging stuff */
1342 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
1343 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
1344 " BSS section `%s': ignored", s
->name
);
1345 s
->len
+= realsize(type
, size
);
1351 if (s
->type
== SHT_PROGBITS
) {
1352 nasm_error(ERR_WARNING
, "uninitialized space declared in"
1353 " non-BSS section `%s': zeroing", s
->name
);
1354 elf_sect_write(s
, NULL
, size
);
1360 if (segment
!= NO_SEG
)
1361 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1362 elf_sect_write(s
, data
, size
);
1367 int isize
= (int)size
;
1368 int asize
= abs((int)size
);
1370 addr
= *(int64_t *)data
;
1371 if (segment
== NO_SEG
) {
1373 } else if (segment
% 2) {
1374 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1375 " segment base references");
1377 if (wrt
== NO_SEG
) {
1381 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
1385 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
1388 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
1391 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
1395 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
1398 nasm_panic(0, "internal error elfx32-hpa-871");
1402 } else if (wrt
== elf_gotpc_sect
+ 1) {
1404 * The user will supply GOT relative to $$. ELF
1405 * will let us have GOT relative to $. So we
1406 * need to fix up the data item by $-$$.
1409 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1411 } else if (wrt
== elf_gotoff_sect
+ 1) {
1412 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
1413 "R_X86_64_GOTOFF64");
1414 } else if (wrt
== elf_got_sect
+ 1) {
1417 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1418 R_X86_64_GOT32
, true);
1422 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
1425 } else if (wrt
== elf_sym_sect
+ 1) {
1429 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1435 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1436 R_X86_64_16
, false);
1440 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1441 R_X86_64_32
, false);
1445 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1446 R_X86_64_32S
, false);
1451 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1452 R_X86_64_64
, false);
1456 nasm_panic(0, "internal error elfx32-hpa-903");
1459 } else if (wrt
== elf_plt_sect
+ 1) {
1460 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1461 "relative PLT references");
1463 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
1467 elf_sect_writeaddr(s
, addr
, asize
);
1472 reltype
= R_X86_64_PC8
;
1477 reltype
= R_X86_64_PC16
;
1482 addr
= *(int64_t *)data
- size
;
1483 if (segment
== segto
)
1484 nasm_panic(0, "intra-segment OUT_REL1ADR");
1485 if (segment
== NO_SEG
) {
1487 } else if (segment
% 2) {
1488 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1489 " segment base references");
1491 if (wrt
== NO_SEG
) {
1492 elf_add_reloc(s
, segment
, addr
, reltype
);
1495 nasm_error(ERR_NONFATAL
,
1496 "Unsupported non-32-bit ELF relocation");
1499 elf_sect_writeaddr(s
, addr
, bytes
);
1503 addr
= *(int64_t *)data
- size
;
1504 if (segment
== segto
)
1505 nasm_panic(0, "intra-segment OUT_REL4ADR");
1506 if (segment
== NO_SEG
) {
1508 } else if (segment
% 2) {
1509 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
1510 " segment base references");
1512 if (wrt
== NO_SEG
) {
1513 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1515 } else if (wrt
== elf_plt_sect
+ 1) {
1516 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1517 R_X86_64_PLT32
, true);
1519 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1520 wrt
== elf_got_sect
+ 1) {
1521 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1522 R_X86_64_GOTPCREL
, true);
1524 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1525 wrt
== elf_got_sect
+ 1) {
1526 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
1527 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1528 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1529 R_X86_64_GOTTPOFF
, true);
1532 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
1536 elf_sect_writeaddr(s
, addr
, 4);
1540 nasm_error(ERR_NONFATAL
, "32-bit ELF format does not support 64-bit relocations");
1542 elf_sect_writeaddr(s
, addr
, 8);
1550 static void elf_write(void)
1557 int32_t symtablen
, symtablocal
;
1560 * Work out how many sections we will have. We have SHN_UNDEF,
1561 * then the flexible user sections, then the fixed sections
1562 * `.shstrtab', `.symtab' and `.strtab', then optionally
1563 * relocation sections for the user sections.
1565 nsections
= sec_numspecial
+ 1;
1566 if (dfmt_is_stabs())
1568 else if (dfmt_is_dwarf())
1571 add_sectname("", ".shstrtab");
1572 add_sectname("", ".symtab");
1573 add_sectname("", ".strtab");
1574 for (i
= 0; i
< nsects
; i
++) {
1575 nsections
++; /* for the section itself */
1576 if (sects
[i
]->head
) {
1577 nsections
++; /* for its relocations */
1578 add_sectname(is_elf32() ? ".rel" : ".rela", sects
[i
]->name
);
1582 if (dfmt_is_stabs()) {
1583 /* in case the debug information is wanted, just add these three sections... */
1584 add_sectname("", ".stab");
1585 add_sectname("", ".stabstr");
1586 add_sectname(is_elf32() ? ".rel" : ".rela", ".stab");
1587 } else if (dfmt_is_dwarf()) {
1588 /* the dwarf debug standard specifies the following ten sections,
1589 not all of which are currently implemented,
1590 although all of them are defined. */
1591 #define debug_aranges (int64_t) (nsections-10)
1592 #define debug_info (int64_t) (nsections-7)
1593 #define debug_abbrev (int64_t) (nsections-5)
1594 #define debug_line (int64_t) (nsections-4)
1595 add_sectname("", ".debug_aranges");
1596 add_sectname(".rela", ".debug_aranges");
1597 add_sectname("", ".debug_pubnames");
1598 add_sectname("", ".debug_info");
1599 add_sectname(".rela", ".debug_info");
1600 add_sectname("", ".debug_abbrev");
1601 add_sectname("", ".debug_line");
1602 add_sectname(".rela", ".debug_line");
1603 add_sectname("", ".debug_frame");
1604 add_sectname("", ".debug_loc");
1608 * Output the ELF header.
1610 if (is_elf32() || is_elfx32()) {
1611 nasm_write("\177ELF\1\1\1", 7, ofile
);
1612 fputc(elf_osabi
, ofile
);
1613 fputc(elf_abiver
, ofile
);
1614 fwritezero(7, ofile
);
1615 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1616 fwriteint16_t(is_elf32() ? EM_386
: EM_X86_64
, ofile
); /* processor ID */
1617 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1618 fwriteint32_t(0L, ofile
); /* no entry point */
1619 fwriteint32_t(0L, ofile
); /* no program header table */
1620 fwriteint32_t(0x40L
, ofile
); /* section headers straight after ELF header plus alignment */
1621 fwriteint32_t(0L, ofile
); /* no special flags */
1622 fwriteint16_t(0x34, ofile
); /* size of ELF header */
1623 fwriteint16_t(0, ofile
); /* no program header table, again */
1624 fwriteint16_t(0, ofile
); /* still no program header table */
1625 fwriteint16_t(sizeof(Elf32_Shdr
), ofile
); /* size of section header */
1626 fwriteint16_t(nsections
, ofile
); /* number of sections */
1627 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for section header table */
1629 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
1630 fwriteint32_t(0L, ofile
);
1631 fwriteint32_t(0L, ofile
);
1633 nasm_assert(is_elf64());
1634 nasm_write("\177ELF\2\1\1", 7, ofile
);
1635 fputc(elf_osabi
, ofile
);
1636 fputc(elf_abiver
, ofile
);
1637 fwritezero(7, ofile
);
1638 fwriteint16_t(ET_REL
, ofile
); /* relocatable file */
1639 fwriteint16_t(EM_X86_64
, ofile
); /* processor ID */
1640 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
1641 fwriteint64_t(0L, ofile
); /* no entry point */
1642 fwriteint64_t(0L, ofile
); /* no program header table */
1643 fwriteint64_t(0x40L
, ofile
); /* section headers straight after ELF header plus alignment */
1644 fwriteint32_t(0L, ofile
); /* no special flags */
1645 fwriteint16_t(0x40, ofile
); /* size of ELF header */
1646 fwriteint16_t(0, ofile
); /* no program header table, again */
1647 fwriteint16_t(0, ofile
); /* still no program header table */
1648 fwriteint16_t(sizeof(Elf64_Shdr
), ofile
); /* size of section header */
1649 fwriteint16_t(nsections
, ofile
); /* number of sections */
1650 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for section header table */
1654 * Build the symbol table and relocation tables.
1656 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1657 for (i
= 0; i
< nsects
; i
++)
1659 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1663 * Now output the section header table.
1666 elf_foffs
= 0x40 + (is_elf64() ? sizeof(Elf64_Shdr
): sizeof(Elf32_Shdr
)) * nsections
;
1667 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1670 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1673 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1676 /* The normal sections */
1677 for (i
= 0; i
< nsects
; i
++) {
1678 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1679 (sects
[i
]->type
== SHT_PROGBITS
?
1680 sects
[i
]->data
: NULL
), true,
1681 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1686 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1687 shstrtablen
, 0, 0, 1, 0);
1692 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1693 symtablen
, sec_strtab
, symtablocal
, 8, 24);
1695 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1696 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1700 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1701 strslen
, 0, 0, 1, 0);
1704 /* The relocation sections */
1706 for (i
= 0; i
< nsects
; i
++) {
1707 if (sects
[i
]->head
) {
1708 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1709 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1713 } else if (is_elfx32()) {
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, 4, 12);
1722 nasm_assert(is_elf64());
1723 for (i
= 0; i
< nsects
; i
++) {
1724 if (sects
[i
]->head
) {
1725 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1726 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 8, 24);
1732 if (dfmt_is_stabs()) {
1733 /* for debugging information, create the last three sections
1734 which are the .stab , .stabstr and .rel.stab sections respectively */
1736 /* this function call creates the stab sections in memory */
1739 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1740 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1741 stablen
, sec_stabstr
, 0, 4, 12);
1744 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1745 stabstrlen
, 0, 0, 4, 0);
1748 /* link -> symtable info -> section to refer to */
1750 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1751 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1753 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, stabrelbuf
, false,
1754 stabrellen
, sec_symtab
, sec_stab
, 4, is_elf64() ? 24 : 12);
1758 } else if (dfmt_is_dwarf()) {
1759 /* for dwarf debugging information, create the ten dwarf sections */
1761 /* this function call creates the dwarf sections in memory */
1765 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1766 arangeslen
, 0, 0, 1, 0);
1769 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1770 arangesrellen
, sec_symtab
,
1771 is_elf64() ? debug_aranges
: sec_debug_aranges
,
1772 1, is_elf64() ? 24 : 12);
1775 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1776 false, pubnameslen
, 0, 0, 1, 0);
1779 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1780 infolen
, 0, 0, 1, 0);
1783 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1784 inforellen
, sec_symtab
,
1785 is_elf64() ? debug_info
: sec_debug_info
,
1786 1, is_elf64() ? 24 : 12);
1789 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1790 abbrevlen
, 0, 0, 1, 0);
1793 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1794 linelen
, 0, 0, 1, 0);
1797 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1798 linerellen
, sec_symtab
,
1799 is_elf64() ? debug_line
: sec_debug_line
,
1800 1, is_elf64() ? 24 : 12);
1803 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1804 framelen
, 0, 0, 8, 0);
1807 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1808 loclen
, 0, 0, 1, 0);
1811 fwritezero(align
, ofile
);
1814 * Now output the sections.
1816 elf_write_sections();
1818 nasm_free(elf_sects
);
1822 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1824 struct SAA
*s
= saa_init(1L);
1825 struct elf_symbol
*sym
;
1826 uint8_t entry
[24], *p
;
1832 * First, an all-zeros entry, required by the ELF spec.
1834 saa_wbytes(s
, NULL
, is_elf64() ? 24L : 16L); /* null symbol table entry */
1835 *len
+= is_elf64() ? 24L : 16L;
1839 * Next, an entry for the file name.
1843 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1844 WRITESHORT(p
, STT_FILE
); /* type FILE */
1845 WRITESHORT(p
, SHN_ABS
);
1846 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1847 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1848 saa_wbytes(s
, entry
, 24L);
1852 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1853 WRITELONG(p
, 0); /* no value */
1854 WRITELONG(p
, 0); /* no size either */
1855 WRITESHORT(p
, STT_FILE
); /* type FILE */
1856 WRITESHORT(p
, SHN_ABS
);
1857 saa_wbytes(s
, entry
, 16L);
1863 * Now some standard symbols defining the segments, for relocation
1867 for (i
= 1; i
<= nsects
; i
++) {
1869 WRITELONG(p
, 0); /* no symbol name */
1870 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1871 WRITESHORT(p
, i
); /* section id */
1872 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1873 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1874 saa_wbytes(s
, entry
, 24L);
1879 for (i
= 1; i
<= nsects
; i
++) {
1881 WRITELONG(p
, 0); /* no symbol name */
1882 WRITELONG(p
, 0); /* offset zero */
1883 WRITELONG(p
, 0); /* size zero */
1884 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1885 WRITESHORT(p
, i
); /* section id */
1886 saa_wbytes(s
, entry
, 16L);
1893 * Now the other local symbols.
1897 while ((sym
= saa_rstruct(syms
))) {
1898 if (sym
->type
& SYM_GLOBAL
)
1901 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1902 WRITECHAR(p
, sym
->type
); /* type and binding */
1903 WRITECHAR(p
, sym
->other
); /* visibility */
1904 WRITESHORT(p
, sym
->section
); /* index into section header table */
1905 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1906 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1907 saa_wbytes(s
, entry
, 24L);
1912 * dwarf needs symbols for debug sections
1913 * which are relocation targets.
1915 if (dfmt_is_dwarf()) {
1916 dwarf_infosym
= *local
;
1918 WRITELONG(p
, 0); /* no symbol name */
1919 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1920 WRITESHORT(p
, debug_info
); /* section id */
1921 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1922 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1923 saa_wbytes(s
, entry
, 24L);
1926 dwarf_abbrevsym
= *local
;
1928 WRITELONG(p
, 0); /* no symbol name */
1929 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1930 WRITESHORT(p
, debug_abbrev
); /* section id */
1931 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1932 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1933 saa_wbytes(s
, entry
, 24L);
1936 dwarf_linesym
= *local
;
1938 WRITELONG(p
, 0); /* no symbol name */
1939 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1940 WRITESHORT(p
, debug_line
); /* section id */
1941 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1942 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1943 saa_wbytes(s
, entry
, 24L);
1948 while ((sym
= saa_rstruct(syms
))) {
1949 if (sym
->type
& SYM_GLOBAL
)
1952 WRITELONG(p
, sym
->strpos
);
1953 WRITELONG(p
, sym
->symv
.key
);
1954 WRITELONG(p
, sym
->size
);
1955 WRITECHAR(p
, sym
->type
); /* type and binding */
1956 WRITECHAR(p
, sym
->other
); /* visibility */
1957 WRITESHORT(p
, sym
->section
);
1958 saa_wbytes(s
, entry
, 16L);
1963 * dwarf needs symbols for debug sections
1964 * which are relocation targets.
1966 if (dfmt_is_dwarf()) {
1967 dwarf_infosym
= *local
;
1969 WRITELONG(p
, 0); /* no symbol name */
1970 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1971 WRITELONG(p
, (uint32_t) 0); /* size zero */
1972 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1973 WRITESHORT(p
, sec_debug_info
); /* section id */
1974 saa_wbytes(s
, entry
, 16L);
1977 dwarf_abbrevsym
= *local
;
1979 WRITELONG(p
, 0); /* no symbol name */
1980 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1981 WRITELONG(p
, (uint32_t) 0); /* size zero */
1982 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1983 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1984 saa_wbytes(s
, entry
, 16L);
1987 dwarf_linesym
= *local
;
1989 WRITELONG(p
, 0); /* no symbol name */
1990 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1991 WRITELONG(p
, (uint32_t) 0); /* size zero */
1992 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1993 WRITESHORT(p
, sec_debug_line
); /* section id */
1994 saa_wbytes(s
, entry
, 16L);
2001 * Now the global symbols.
2005 while ((sym
= saa_rstruct(syms
))) {
2006 if (!(sym
->type
& SYM_GLOBAL
))
2009 WRITELONG(p
, sym
->strpos
);
2010 WRITECHAR(p
, sym
->type
); /* type and binding */
2011 WRITECHAR(p
, sym
->other
); /* visibility */
2012 WRITESHORT(p
, sym
->section
);
2013 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
2014 WRITEDLONG(p
, (int64_t)sym
->size
);
2015 saa_wbytes(s
, entry
, 24L);
2019 while ((sym
= saa_rstruct(syms
))) {
2020 if (!(sym
->type
& SYM_GLOBAL
))
2023 WRITELONG(p
, sym
->strpos
);
2024 WRITELONG(p
, sym
->symv
.key
);
2025 WRITELONG(p
, sym
->size
);
2026 WRITECHAR(p
, sym
->type
); /* type and binding */
2027 WRITECHAR(p
, sym
->other
); /* visibility */
2028 WRITESHORT(p
, sym
->section
);
2029 saa_wbytes(s
, entry
, 16L);
2037 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
2040 uint8_t *p
, entry
[24];
2041 int32_t global_offset
;
2050 * How to onvert from a global placeholder to a real symbol index;
2051 * the +2 refers to the two special entries, the null entry and
2052 * the filename entry.
2054 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
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 saa_wbytes(s
, entry
, 8L);
2071 } else if (is_elfx32()) {
2073 int32_t sym
= r
->symbol
;
2075 if (sym
>= GLOBAL_TEMP_BASE
)
2076 sym
+= global_offset
;
2079 WRITELONG(p
, r
->address
);
2080 WRITELONG(p
, (sym
<< 8) + r
->type
);
2081 WRITELONG(p
, r
->offset
);
2082 saa_wbytes(s
, entry
, 12L);
2088 nasm_assert(is_elf64());
2090 int32_t sym
= r
->symbol
;
2092 if (sym
>= GLOBAL_TEMP_BASE
)
2093 sym
+= global_offset
;
2096 WRITEDLONG(p
, r
->address
);
2097 WRITELONG(p
, r
->type
);
2099 WRITEDLONG(p
, r
->offset
);
2100 saa_wbytes(s
, entry
, 24L);
2110 static void elf_section_header(int name
, int type
, uint64_t flags
,
2111 void *data
, bool is_saa
, uint64_t datalen
,
2112 int link
, int info
, int align
, int eltsize
)
2114 elf_sects
[elf_nsect
].data
= data
;
2115 elf_sects
[elf_nsect
].len
= datalen
;
2116 elf_sects
[elf_nsect
].is_saa
= is_saa
;
2120 fwriteint32_t((int32_t)name
, ofile
);
2121 fwriteint32_t((int32_t)type
, ofile
);
2122 fwriteint32_t((int32_t)flags
, ofile
);
2123 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
2124 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
2125 fwriteint32_t(datalen
, ofile
);
2127 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2128 fwriteint32_t((int32_t)link
, ofile
);
2129 fwriteint32_t((int32_t)info
, ofile
);
2130 fwriteint32_t((int32_t)align
, ofile
);
2131 fwriteint32_t((int32_t)eltsize
, ofile
);
2132 } else if (is_elfx32()) {
2133 fwriteint32_t((int32_t)name
, ofile
);
2134 fwriteint32_t((int32_t)type
, ofile
);
2135 fwriteint32_t((int32_t)flags
, ofile
);
2136 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
2137 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
2138 fwriteint32_t(datalen
, ofile
);
2140 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2141 fwriteint32_t((int32_t)link
, ofile
);
2142 fwriteint32_t((int32_t)info
, ofile
);
2143 fwriteint32_t((int32_t)align
, ofile
);
2144 fwriteint32_t((int32_t)eltsize
, ofile
);
2146 nasm_assert(is_elf64());
2147 fwriteint32_t((int32_t)name
, ofile
);
2148 fwriteint32_t((int32_t)type
, ofile
);
2149 fwriteint64_t((int64_t)flags
, ofile
);
2150 fwriteint64_t(0L, ofile
); /* no address, ever, in object files */
2151 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, ofile
);
2152 fwriteint64_t(datalen
, ofile
);
2154 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2155 fwriteint32_t((int32_t)link
, ofile
);
2156 fwriteint32_t((int32_t)info
, ofile
);
2157 fwriteint64_t((int64_t)align
, ofile
);
2158 fwriteint64_t((int64_t)eltsize
, ofile
);
2162 static void elf_write_sections(void)
2165 for (i
= 0; i
< elf_nsect
; i
++)
2166 if (elf_sects
[i
].data
) {
2167 int32_t len
= elf_sects
[i
].len
;
2168 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
2169 int32_t align
= reallen
- len
;
2170 if (elf_sects
[i
].is_saa
)
2171 saa_fpwrite(elf_sects
[i
].data
, ofile
);
2173 nasm_write(elf_sects
[i
].data
, len
, ofile
);
2174 fwritezero(align
, ofile
);
2178 static void elf_sect_write(struct elf_section
*sect
, const void *data
, size_t len
)
2180 saa_wbytes(sect
->data
, data
, len
);
2184 static void elf_sect_writeaddr(struct elf_section
*sect
, int64_t data
, size_t len
)
2186 saa_writeaddr(sect
->data
, data
, len
);
2190 static void elf_sectalign(int32_t seg
, unsigned int value
)
2192 struct elf_section
*s
= NULL
;
2195 for (i
= 0; i
< nsects
; i
++) {
2196 if (sects
[i
]->index
== seg
) {
2201 if (!s
|| !is_power2(value
))
2204 if (value
> s
->align
)
2208 static int32_t elf_segbase(int32_t segment
)
2213 static void elf_filename(char *inname
, char *outname
)
2215 strcpy(elf_module
, inname
);
2216 standard_extension(inname
, outname
, ".o");
2219 extern macros_t elf_stdmac
[];
2221 static int elf_set_info(enum geninfo type
, char **val
)
2228 static const struct dfmt elf32_df_dwarf
= {
2229 "ELF32 (i386) dwarf debug format for Linux/Unix",
2233 null_debug_deflabel
,
2234 null_debug_directive
,
2240 static const struct dfmt elf32_df_stabs
= {
2241 "ELF32 (i386) stabs debug format for Linux/Unix",
2245 null_debug_deflabel
,
2246 null_debug_directive
,
2252 static const struct dfmt
* const elf32_debugs_arr
[3] =
2253 { &elf32_df_dwarf
, &elf32_df_stabs
, NULL
};
2255 const struct ofmt of_elf32
= {
2256 "ELF32 (i386) object files (e.g. Linux)",
2265 nasm_do_legacy_output
,
2276 static const struct dfmt elf64_df_dwarf
= {
2277 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
2281 null_debug_deflabel
,
2282 null_debug_directive
,
2288 static const struct dfmt elf64_df_stabs
= {
2289 "ELF64 (x86-64) stabs debug format for Linux/Unix",
2293 null_debug_deflabel
,
2294 null_debug_directive
,
2300 static const struct dfmt
* const elf64_debugs_arr
[3] =
2301 { &elf64_df_dwarf
, &elf64_df_stabs
, NULL
};
2303 const struct ofmt of_elf64
= {
2304 "ELF64 (x86_64) object files (e.g. Linux)",
2313 nasm_do_legacy_output
,
2324 static const struct dfmt elfx32_df_dwarf
= {
2325 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
2329 null_debug_deflabel
,
2330 null_debug_directive
,
2336 static const struct dfmt elfx32_df_stabs
= {
2337 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
2341 null_debug_deflabel
,
2342 null_debug_directive
,
2348 static const struct dfmt
* const elfx32_debugs_arr
[3] =
2349 { &elfx32_df_dwarf
, &elfx32_df_stabs
, NULL
};
2351 const struct ofmt of_elfx32
= {
2352 "ELFX32 (x86_64) object files (e.g. Linux)",
2361 nasm_do_legacy_output
,
2372 static bool is_elf64(void)
2374 return ofmt
== &of_elf64
;
2377 static bool is_elf32(void)
2379 return ofmt
== &of_elf32
;
2382 static bool is_elfx32(void)
2384 return ofmt
== &of_elfx32
;
2387 static bool dfmt_is_stabs(void)
2389 return dfmt
== &elf32_df_stabs
|| dfmt
== &elfx32_df_stabs
|| dfmt
== &elf64_df_stabs
;
2392 static bool dfmt_is_dwarf(void)
2394 return dfmt
== &elf32_df_dwarf
|| dfmt
== &elfx32_df_dwarf
|| dfmt
== &elf64_df_dwarf
;
2397 /* common debugging routines */
2398 static void debug_typevalue(int32_t type
)
2400 int32_t stype
, ssize
;
2401 switch (TYM_TYPE(type
)) {
2444 stype
= STT_SECTION
;
2459 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
2460 lastsym
->size
= ssize
;
2461 lastsym
->type
= stype
;
2465 /* stabs debugging routines */
2467 static void stabs_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
2470 if (!stabs_filename
) {
2471 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
2472 strcpy(stabs_filename
, filename
);
2474 if (strcmp(stabs_filename
, filename
)) {
2475 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2476 in fact, this leak comes in quite handy to maintain a list of files
2477 encountered so far in the symbol lines... */
2479 /* why not nasm_free(stabs_filename); we're done with the old one */
2481 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
2482 strcpy(stabs_filename
, filename
);
2486 currentline
= linenumber
;
2489 static void stabs_output(int type
, void *param
)
2491 struct symlininfo
*s
;
2492 struct linelist
*el
;
2493 if (type
== TY_DEBUGSYMLIN
) {
2494 if (debug_immcall
) {
2495 s
= (struct symlininfo
*)param
;
2496 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2497 return; /* line info is only collected for executable sections */
2499 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2500 el
->info
.offset
= s
->offset
;
2501 el
->info
.section
= s
->section
;
2502 el
->info
.name
= s
->name
;
2503 el
->line
= currentline
;
2504 el
->filename
= stabs_filename
;
2507 stabslines
->last
->next
= el
;
2508 stabslines
->last
= el
;
2511 stabslines
->last
= el
;
2518 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2520 static void stabs_generate(void)
2522 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
2523 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
2527 struct linelist
*ptr
;
2531 allfiles
= (char **)nasm_zalloc(numlinestabs
* sizeof(char *));
2534 if (numfiles
== 0) {
2535 allfiles
[0] = ptr
->filename
;
2538 for (i
= 0; i
< numfiles
; i
++) {
2539 if (!strcmp(allfiles
[i
], ptr
->filename
))
2542 if (i
>= numfiles
) {
2543 allfiles
[i
] = ptr
->filename
;
2550 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
2551 for (i
= 0; i
< numfiles
; i
++) {
2552 fileidx
[i
] = strsize
;
2553 strsize
+= strlen(allfiles
[i
]) + 1;
2556 for (i
= 0; i
< numfiles
; i
++) {
2557 if (!strcmp(allfiles
[i
], elf_module
)) {
2564 * worst case size of the stab buffer would be:
2565 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2566 * plus one "ending" entry
2568 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
2569 sizeof(struct stabentry
));
2570 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
2571 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* (is_elf64() ? 16 : 8) * (2 + 3));
2574 for (i
= 0; i
< numfiles
; i
++)
2575 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
2578 stabstrlen
= strsize
; /* set global variable for length of stab strings */
2586 * this is the first stab, its strx points to the filename of the
2587 * the source-file, the n_desc field should be set to the number
2588 * of remaining stabs
2590 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, stabstrlen
);
2592 /* this is the stab for the main source file */
2593 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
2595 /* relocation table entry */
2598 * Since the symbol table has two entries before
2599 * the section symbols, the index in the info.section
2600 * member must be adjusted by adding 2
2604 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2605 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2606 } else if (is_elfx32()) {
2607 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2608 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2611 nasm_assert(is_elf64());
2612 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2613 WRITELONG(rptr
, R_X86_64_32
);
2614 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2615 WRITEDLONG(rptr
, 0);
2618 currfile
= mainfileindex
;
2623 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2624 /* oops file has changed... */
2625 for (i
= 0; i
< numfiles
; i
++)
2626 if (!strcmp(allfiles
[i
], ptr
->filename
))
2629 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2633 /* relocation table entry */
2634 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2635 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2638 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2641 /* relocation table entry */
2642 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2643 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2647 } else if (is_elfx32()) {
2649 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2650 /* oops file has changed... */
2651 for (i
= 0; i
< numfiles
; i
++)
2652 if (!strcmp(allfiles
[i
], ptr
->filename
))
2655 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2659 /* relocation table entry */
2660 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2661 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2662 WRITELONG(rptr
, ptr
->info
.offset
);
2665 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2668 /* relocation table entry */
2669 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2670 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2671 WRITELONG(rptr
, ptr
->info
.offset
);
2676 nasm_assert(is_elf64());
2678 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2679 /* oops file has changed... */
2680 for (i
= 0; i
< numfiles
; i
++)
2681 if (!strcmp(allfiles
[i
], ptr
->filename
))
2684 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2688 /* relocation table entry */
2689 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2690 WRITELONG(rptr
, R_X86_64_32
);
2691 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2692 WRITEDLONG(rptr
, ptr
->info
.offset
);
2695 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2698 /* relocation table entry */
2699 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2700 WRITELONG(rptr
, R_X86_64_32
);
2701 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2702 WRITEDLONG(rptr
, ptr
->info
.offset
);
2708 /* this is an "ending" token */
2709 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
2712 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
2714 nasm_free(allfiles
);
2717 stablen
= (sptr
- sbuf
);
2718 stabrellen
= (rptr
- rbuf
);
2724 static void stabs_cleanup(void)
2726 struct linelist
*ptr
, *del
;
2738 nasm_free(stabrelbuf
);
2739 nasm_free(stabstrbuf
);
2742 /* dwarf routines */
2744 static void dwarf_init(void)
2746 ndebugs
= 3; /* 3 debug symbols */
2749 static void dwarf_linenum(const char *filename
, int32_t linenumber
,
2753 dwarf_findfile(filename
);
2755 currentline
= linenumber
;
2758 /* called from elf_out with type == TY_DEBUGSYMLIN */
2759 static void dwarf_output(int type
, void *param
)
2761 int ln
, aa
, inx
, maxln
, soc
;
2762 struct symlininfo
*s
;
2767 s
= (struct symlininfo
*)param
;
2769 /* line number info is only gathered for executable sections */
2770 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2773 /* Check if section index has changed */
2774 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
2775 dwarf_findsect(s
->section
);
2777 /* do nothing unless line or file has changed */
2781 ln
= currentline
- dwarf_csect
->line
;
2782 aa
= s
->offset
- dwarf_csect
->offset
;
2783 inx
= dwarf_clist
->line
;
2784 plinep
= dwarf_csect
->psaa
;
2785 /* check for file change */
2786 if (!(inx
== dwarf_csect
->file
)) {
2787 saa_write8(plinep
,DW_LNS_set_file
);
2788 saa_write8(plinep
,inx
);
2789 dwarf_csect
->file
= inx
;
2791 /* check for line change */
2793 /* test if in range of special op code */
2794 maxln
= line_base
+ line_range
;
2795 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
2796 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
2797 saa_write8(plinep
,soc
);
2799 saa_write8(plinep
,DW_LNS_advance_line
);
2800 saa_wleb128s(plinep
,ln
);
2802 saa_write8(plinep
,DW_LNS_advance_pc
);
2803 saa_wleb128u(plinep
,aa
);
2806 dwarf_csect
->line
= currentline
;
2807 dwarf_csect
->offset
= s
->offset
;
2810 /* show change handled */
2815 static void dwarf_generate(void)
2819 struct linelist
*ftentry
;
2820 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
2821 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
2822 struct sectlist
*psect
;
2823 size_t saalen
, linepoff
, totlen
, highaddr
;
2826 /* write epilogues for each line program range */
2827 /* and build aranges section */
2828 paranges
= saa_init(1L);
2829 parangesrel
= saa_init(1L);
2830 saa_write16(paranges
,2); /* dwarf version */
2831 saa_write32(parangesrel
, paranges
->datalen
+4);
2832 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
2833 saa_write32(parangesrel
, 0);
2834 saa_write32(paranges
,0); /* offset into info */
2835 saa_write8(paranges
,4); /* pointer size */
2836 saa_write8(paranges
,0); /* not segmented */
2837 saa_write32(paranges
,0); /* padding */
2838 /* iterate though sectlist entries */
2839 psect
= dwarf_fsect
;
2842 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2843 plinep
= psect
->psaa
;
2844 /* Line Number Program Epilogue */
2845 saa_write8(plinep
,2); /* std op 2 */
2846 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2847 saa_write8(plinep
,DW_LNS_extended_op
);
2848 saa_write8(plinep
,1); /* operand length */
2849 saa_write8(plinep
,DW_LNE_end_sequence
);
2850 totlen
+= plinep
->datalen
;
2851 /* range table relocation entry */
2852 saa_write32(parangesrel
, paranges
->datalen
+ 4);
2853 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2854 saa_write32(parangesrel
, (uint32_t) 0);
2855 /* range table entry */
2856 saa_write32(paranges
,0x0000); /* range start */
2857 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
2858 highaddr
+= sects
[psect
->section
]->len
;
2859 /* done with this entry */
2860 psect
= psect
->next
;
2862 saa_write32(paranges
,0); /* null address */
2863 saa_write32(paranges
,0); /* null length */
2864 saalen
= paranges
->datalen
;
2865 arangeslen
= saalen
+ 4;
2866 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2867 WRITELONG(pbuf
,saalen
); /* initial length */
2868 saa_rnbytes(paranges
, pbuf
, saalen
);
2870 } else if (is_elfx32()) {
2871 /* write epilogues for each line program range */
2872 /* and build aranges section */
2873 paranges
= saa_init(1L);
2874 parangesrel
= saa_init(1L);
2875 saa_write16(paranges
,3); /* dwarf version */
2876 saa_write32(parangesrel
, paranges
->datalen
+4);
2877 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_X86_64_32
); /* reloc to info */
2878 saa_write32(parangesrel
, 0);
2879 saa_write32(paranges
,0); /* offset into info */
2880 saa_write8(paranges
,4); /* pointer size */
2881 saa_write8(paranges
,0); /* not segmented */
2882 saa_write32(paranges
,0); /* padding */
2883 /* iterate though sectlist entries */
2884 psect
= dwarf_fsect
;
2887 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2888 plinep
= psect
->psaa
;
2889 /* Line Number Program Epilogue */
2890 saa_write8(plinep
,2); /* std op 2 */
2891 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2892 saa_write8(plinep
,DW_LNS_extended_op
);
2893 saa_write8(plinep
,1); /* operand length */
2894 saa_write8(plinep
,DW_LNE_end_sequence
);
2895 totlen
+= plinep
->datalen
;
2896 /* range table relocation entry */
2897 saa_write32(parangesrel
, paranges
->datalen
+ 4);
2898 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_X86_64_32
);
2899 saa_write32(parangesrel
, (uint32_t) 0);
2900 /* range table entry */
2901 saa_write32(paranges
,0x0000); /* range start */
2902 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
2903 highaddr
+= sects
[psect
->section
]->len
;
2904 /* done with this entry */
2905 psect
= psect
->next
;
2907 saa_write32(paranges
,0); /* null address */
2908 saa_write32(paranges
,0); /* null length */
2909 saalen
= paranges
->datalen
;
2910 arangeslen
= saalen
+ 4;
2911 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2912 WRITELONG(pbuf
,saalen
); /* initial length */
2913 saa_rnbytes(paranges
, pbuf
, saalen
);
2916 nasm_assert(is_elf64());
2917 /* write epilogues for each line program range */
2918 /* and build aranges section */
2919 paranges
= saa_init(1L);
2920 parangesrel
= saa_init(1L);
2921 saa_write16(paranges
,3); /* dwarf version */
2922 saa_write64(parangesrel
, paranges
->datalen
+4);
2923 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
2924 saa_write64(parangesrel
, 0);
2925 saa_write32(paranges
,0); /* offset into info */
2926 saa_write8(paranges
,8); /* pointer size */
2927 saa_write8(paranges
,0); /* not segmented */
2928 saa_write32(paranges
,0); /* padding */
2929 /* iterate though sectlist entries */
2930 psect
= dwarf_fsect
;
2933 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2934 plinep
= psect
->psaa
;
2935 /* Line Number Program Epilogue */
2936 saa_write8(plinep
,2); /* std op 2 */
2937 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2938 saa_write8(plinep
,DW_LNS_extended_op
);
2939 saa_write8(plinep
,1); /* operand length */
2940 saa_write8(plinep
,DW_LNE_end_sequence
);
2941 totlen
+= plinep
->datalen
;
2942 /* range table relocation entry */
2943 saa_write64(parangesrel
, paranges
->datalen
+ 4);
2944 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2945 saa_write64(parangesrel
, (uint64_t) 0);
2946 /* range table entry */
2947 saa_write64(paranges
,0x0000); /* range start */
2948 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
2949 highaddr
+= sects
[psect
->section
]->len
;
2950 /* done with this entry */
2951 psect
= psect
->next
;
2953 saa_write64(paranges
,0); /* null address */
2954 saa_write64(paranges
,0); /* null length */
2955 saalen
= paranges
->datalen
;
2956 arangeslen
= saalen
+ 4;
2957 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2958 WRITELONG(pbuf
,saalen
); /* initial length */
2959 saa_rnbytes(paranges
, pbuf
, saalen
);
2963 /* build rela.aranges section */
2964 arangesrellen
= saalen
= parangesrel
->datalen
;
2965 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2966 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2967 saa_free(parangesrel
);
2969 /* build pubnames section */
2970 ppubnames
= saa_init(1L);
2971 saa_write16(ppubnames
,3); /* dwarf version */
2972 saa_write32(ppubnames
,0); /* offset into info */
2973 saa_write32(ppubnames
,0); /* space used in info */
2974 saa_write32(ppubnames
,0); /* end of list */
2975 saalen
= ppubnames
->datalen
;
2976 pubnameslen
= saalen
+ 4;
2977 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2978 WRITELONG(pbuf
,saalen
); /* initial length */
2979 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2980 saa_free(ppubnames
);
2983 /* build info section */
2984 pinfo
= saa_init(1L);
2985 pinforel
= saa_init(1L);
2986 saa_write16(pinfo
,2); /* dwarf version */
2987 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2988 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
2989 saa_write32(pinforel
, 0);
2990 saa_write32(pinfo
,0); /* offset into abbrev */
2991 saa_write8(pinfo
,4); /* pointer size */
2992 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2993 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2994 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2995 saa_write32(pinforel
, 0);
2996 saa_write32(pinfo
,0); /* DW_AT_low_pc */
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
,highaddr
); /* DW_AT_high_pc */
3001 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3002 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
3003 saa_write32(pinforel
, 0);
3004 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3005 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3006 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3007 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3008 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3009 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3010 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
3011 saa_write32(pinforel
, 0);
3012 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3013 saa_write32(pinfo
,0); /* DW_AT_frame_base */
3014 saa_write8(pinfo
,0); /* end of entries */
3015 saalen
= pinfo
->datalen
;
3016 infolen
= saalen
+ 4;
3017 infobuf
= pbuf
= nasm_malloc(infolen
);
3018 WRITELONG(pbuf
,saalen
); /* initial length */
3019 saa_rnbytes(pinfo
, pbuf
, saalen
);
3021 } else if (is_elfx32()) {
3022 /* build info section */
3023 pinfo
= saa_init(1L);
3024 pinforel
= saa_init(1L);
3025 saa_write16(pinfo
,3); /* dwarf version */
3026 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3027 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_X86_64_32
); /* reloc to abbrev */
3028 saa_write32(pinforel
, 0);
3029 saa_write32(pinfo
,0); /* offset into abbrev */
3030 saa_write8(pinfo
,4); /* pointer size */
3031 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
3032 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3033 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3034 saa_write32(pinforel
, 0);
3035 saa_write32(pinfo
,0); /* DW_AT_low_pc */
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
,highaddr
); /* DW_AT_high_pc */
3040 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3041 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_X86_64_32
); /* reloc to line */
3042 saa_write32(pinforel
, 0);
3043 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3044 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3045 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3046 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3047 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3048 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3049 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3050 saa_write32(pinforel
, 0);
3051 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3052 saa_write32(pinfo
,0); /* DW_AT_frame_base */
3053 saa_write8(pinfo
,0); /* end of entries */
3054 saalen
= pinfo
->datalen
;
3055 infolen
= saalen
+ 4;
3056 infobuf
= pbuf
= nasm_malloc(infolen
);
3057 WRITELONG(pbuf
,saalen
); /* initial length */
3058 saa_rnbytes(pinfo
, pbuf
, saalen
);
3061 nasm_assert(is_elf64());
3062 /* build info section */
3063 pinfo
= saa_init(1L);
3064 pinforel
= saa_init(1L);
3065 saa_write16(pinfo
,3); /* dwarf version */
3066 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3067 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
3068 saa_write64(pinforel
, 0);
3069 saa_write32(pinfo
,0); /* offset into abbrev */
3070 saa_write8(pinfo
,8); /* pointer size */
3071 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
3072 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3073 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3074 saa_write64(pinforel
, 0);
3075 saa_write64(pinfo
,0); /* DW_AT_low_pc */
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
,highaddr
); /* DW_AT_high_pc */
3080 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3081 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
3082 saa_write64(pinforel
, 0);
3083 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3084 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3085 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3086 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3087 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3088 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3089 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3090 saa_write64(pinforel
, 0);
3091 saa_write64(pinfo
,0); /* DW_AT_low_pc */
3092 saa_write64(pinfo
,0); /* DW_AT_frame_base */
3093 saa_write8(pinfo
,0); /* end of entries */
3094 saalen
= pinfo
->datalen
;
3095 infolen
= saalen
+ 4;
3096 infobuf
= pbuf
= nasm_malloc(infolen
);
3097 WRITELONG(pbuf
,saalen
); /* initial length */
3098 saa_rnbytes(pinfo
, pbuf
, saalen
);
3102 /* build rela.info section */
3103 inforellen
= saalen
= pinforel
->datalen
;
3104 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
3105 saa_rnbytes(pinforel
, pbuf
, saalen
);
3108 /* build abbrev section */
3109 pabbrev
= saa_init(1L);
3110 saa_write8(pabbrev
,1); /* entry number LEB128u */
3111 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
3112 saa_write8(pabbrev
,1); /* has children */
3113 /* the following attributes and forms are all LEB128u values */
3114 saa_write8(pabbrev
,DW_AT_low_pc
);
3115 saa_write8(pabbrev
,DW_FORM_addr
);
3116 saa_write8(pabbrev
,DW_AT_high_pc
);
3117 saa_write8(pabbrev
,DW_FORM_addr
);
3118 saa_write8(pabbrev
,DW_AT_stmt_list
);
3119 saa_write8(pabbrev
,DW_FORM_data4
);
3120 saa_write8(pabbrev
,DW_AT_name
);
3121 saa_write8(pabbrev
,DW_FORM_string
);
3122 saa_write8(pabbrev
,DW_AT_producer
);
3123 saa_write8(pabbrev
,DW_FORM_string
);
3124 saa_write8(pabbrev
,DW_AT_language
);
3125 saa_write8(pabbrev
,DW_FORM_data2
);
3126 saa_write16(pabbrev
,0); /* end of entry */
3127 /* LEB128u usage same as above */
3128 saa_write8(pabbrev
,2); /* entry number */
3129 saa_write8(pabbrev
,DW_TAG_subprogram
);
3130 saa_write8(pabbrev
,0); /* no children */
3131 saa_write8(pabbrev
,DW_AT_low_pc
);
3132 saa_write8(pabbrev
,DW_FORM_addr
);
3133 saa_write8(pabbrev
,DW_AT_frame_base
);
3134 saa_write8(pabbrev
,DW_FORM_data4
);
3135 saa_write16(pabbrev
,0); /* end of entry */
3136 abbrevlen
= saalen
= pabbrev
->datalen
;
3137 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
3138 saa_rnbytes(pabbrev
, pbuf
, saalen
);
3141 /* build line section */
3143 plines
= saa_init(1L);
3144 saa_write8(plines
,1); /* Minimum Instruction Length */
3145 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
3146 saa_write8(plines
,line_base
); /* Line Base */
3147 saa_write8(plines
,line_range
); /* Line Range */
3148 saa_write8(plines
,opcode_base
); /* Opcode Base */
3149 /* standard opcode lengths (# of LEB128u operands) */
3150 saa_write8(plines
,0); /* Std opcode 1 length */
3151 saa_write8(plines
,1); /* Std opcode 2 length */
3152 saa_write8(plines
,1); /* Std opcode 3 length */
3153 saa_write8(plines
,1); /* Std opcode 4 length */
3154 saa_write8(plines
,1); /* Std opcode 5 length */
3155 saa_write8(plines
,0); /* Std opcode 6 length */
3156 saa_write8(plines
,0); /* Std opcode 7 length */
3157 saa_write8(plines
,0); /* Std opcode 8 length */
3158 saa_write8(plines
,1); /* Std opcode 9 length */
3159 saa_write8(plines
,0); /* Std opcode 10 length */
3160 saa_write8(plines
,0); /* Std opcode 11 length */
3161 saa_write8(plines
,1); /* Std opcode 12 length */
3162 /* Directory Table */
3163 saa_write8(plines
,0); /* End of table */
3164 /* File Name Table */
3165 ftentry
= dwarf_flist
;
3166 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
3167 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
3168 saa_write8(plines
,0); /* directory LEB128u */
3169 saa_write8(plines
,0); /* time LEB128u */
3170 saa_write8(plines
,0); /* size LEB128u */
3171 ftentry
= ftentry
->next
;
3173 saa_write8(plines
,0); /* End of table */
3174 linepoff
= plines
->datalen
;
3175 linelen
= linepoff
+ totlen
+ 10;
3176 linebuf
= pbuf
= nasm_malloc(linelen
);
3177 WRITELONG(pbuf
,linelen
-4); /* initial length */
3178 WRITESHORT(pbuf
,3); /* dwarf version */
3179 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
3180 /* write line header */
3182 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
3185 /* concatonate line program ranges */
3187 plinesrel
= saa_init(1L);
3188 psect
= dwarf_fsect
;
3190 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3191 saa_write32(plinesrel
, linepoff
);
3192 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
3193 saa_write32(plinesrel
, (uint32_t) 0);
3194 plinep
= psect
->psaa
;
3195 saalen
= plinep
->datalen
;
3196 saa_rnbytes(plinep
, pbuf
, saalen
);
3200 /* done with this entry */
3201 psect
= psect
->next
;
3203 } else if (is_elfx32()) {
3204 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3205 saa_write32(plinesrel
, linepoff
);
3206 saa_write32(plinesrel
, ((psect
->section
+ 2) << 8) + R_X86_64_32
);
3207 saa_write32(plinesrel
, 0);
3208 plinep
= psect
->psaa
;
3209 saalen
= plinep
->datalen
;
3210 saa_rnbytes(plinep
, pbuf
, saalen
);
3214 /* done with this entry */
3215 psect
= psect
->next
;
3218 nasm_assert(is_elf64());
3219 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3220 saa_write64(plinesrel
, linepoff
);
3221 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
3222 saa_write64(plinesrel
, (uint64_t) 0);
3223 plinep
= psect
->psaa
;
3224 saalen
= plinep
->datalen
;
3225 saa_rnbytes(plinep
, pbuf
, saalen
);
3229 /* done with this entry */
3230 psect
= psect
->next
;
3234 /* build rela.lines section */
3235 linerellen
=saalen
= plinesrel
->datalen
;
3236 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
3237 saa_rnbytes(plinesrel
, pbuf
, saalen
);
3238 saa_free(plinesrel
);
3240 /* build frame section */
3242 framebuf
= pbuf
= nasm_malloc(framelen
);
3243 WRITELONG(pbuf
,framelen
-4); /* initial length */
3245 /* build loc section */
3247 locbuf
= pbuf
= nasm_malloc(loclen
);
3249 WRITELONG(pbuf
,0); /* null beginning offset */
3250 WRITELONG(pbuf
,0); /* null ending offset */
3252 nasm_assert(is_elf64());
3253 WRITEDLONG(pbuf
,0); /* null beginning offset */
3254 WRITEDLONG(pbuf
,0); /* null ending offset */
3258 static void dwarf_cleanup(void)
3260 nasm_free(arangesbuf
);
3261 nasm_free(arangesrelbuf
);
3262 nasm_free(pubnamesbuf
);
3264 nasm_free(inforelbuf
);
3265 nasm_free(abbrevbuf
);
3267 nasm_free(linerelbuf
);
3268 nasm_free(framebuf
);
3272 static void dwarf_findfile(const char * fname
)
3275 struct linelist
*match
;
3277 /* return if fname is current file name */
3278 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
3281 /* search for match */
3284 match
= dwarf_flist
;
3285 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
3286 if (!(strcmp(fname
, match
->filename
))) {
3287 dwarf_clist
= match
;
3293 /* add file name to end of list */
3294 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
3296 dwarf_clist
->line
= dwarf_numfiles
;
3297 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
3298 strcpy(dwarf_clist
->filename
,fname
);
3299 dwarf_clist
->next
= 0;
3300 if (!dwarf_flist
) { /* if first entry */
3301 dwarf_flist
= dwarf_elist
= dwarf_clist
;
3302 dwarf_clist
->last
= 0;
3303 } else { /* chain to previous entry */
3304 dwarf_elist
->next
= dwarf_clist
;
3305 dwarf_elist
= dwarf_clist
;
3309 static void dwarf_findsect(const int index
)
3312 struct sectlist
*match
;
3315 /* return if index is current section index */
3316 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
3319 /* search for match */
3322 match
= dwarf_fsect
;
3323 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
3324 if (match
->section
== index
) {
3325 dwarf_csect
= match
;
3328 match
= match
->next
;
3332 /* add entry to end of list */
3333 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
3335 dwarf_csect
->psaa
= plinep
= saa_init(1L);
3336 dwarf_csect
->line
= 1;
3337 dwarf_csect
->offset
= 0;
3338 dwarf_csect
->file
= 1;
3339 dwarf_csect
->section
= index
;
3340 dwarf_csect
->next
= 0;
3341 /* set relocatable address at start of line program */
3342 saa_write8(plinep
,DW_LNS_extended_op
);
3343 saa_write8(plinep
,is_elf64() ? 9 : 5); /* operand length */
3344 saa_write8(plinep
,DW_LNE_set_address
);
3346 saa_write64(plinep
,0); /* Start Address */
3348 saa_write32(plinep
,0); /* Start Address */
3350 if (!dwarf_fsect
) { /* if first entry */
3351 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
3352 dwarf_csect
->last
= 0;
3353 } else { /* chain to previous entry */
3354 dwarf_esect
->next
= dwarf_csect
;
3355 dwarf_esect
= dwarf_csect
;
3359 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */