1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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
60 #if defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32)
63 static struct elf_section
**sects
;
64 static int nsects
, sectlen
;
66 #define SHSTR_DELTA 256
67 static char *shstrtab
;
68 static int shstrtablen
, shstrtabsize
;
70 static struct SAA
*syms
;
71 static uint32_t nlocals
, nglobs
, ndebugs
; /* Symbol counts */
73 static int32_t def_seg
;
75 static struct RAA
*bsym
;
77 static struct SAA
*strs
;
78 static uint32_t strslen
;
80 static struct elf_symbol
*fwds
;
82 static char elf_module
[FILENAME_MAX
];
84 extern const struct ofmt of_elf32
;
85 extern const struct ofmt of_elf64
;
86 extern const struct ofmt of_elfx32
;
88 static struct ELF_SECTDATA
{
94 static int elf_nsect
, nsections
;
95 static int64_t elf_foffs
;
97 static void elf_write(void);
98 static void elf_sect_write(struct elf_section
*, const void *, size_t);
99 static void elf_sect_writeaddr(struct elf_section
*, int64_t, size_t);
100 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
102 static void elf_write_sections(void);
103 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
104 static struct SAA
*elf_build_reltab(uint64_t *, struct elf_reloc
*);
105 static void add_sectname(const char *, const char *);
114 int section
; /* index into sects[] */
115 int segto
; /* internal section number */
116 char *name
; /* shallow-copied pointer of section name */
120 struct linelist
*next
;
121 struct linelist
*last
;
122 struct symlininfo info
;
133 struct sectlist
*next
;
134 struct sectlist
*last
;
137 /* common debug variables */
138 static int currentline
= 1;
139 static int debug_immcall
= 0;
141 /* stabs debug variables */
142 static struct linelist
*stabslines
= 0;
143 static int numlinestabs
= 0;
144 static char *stabs_filename
= 0;
145 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
146 static int stablen
, stabstrlen
, stabrellen
;
148 /* dwarf debug variables */
149 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
150 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
151 static int dwarf_numfiles
= 0, dwarf_nsections
;
152 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
153 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
154 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
155 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
156 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
157 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
159 static struct elf_symbol
*lastsym
;
161 /* common debugging routines */
162 static void debug_typevalue(int32_t);
164 /* stabs debugging routines */
165 static void stabs_linenum(const char *filename
, int32_t linenumber
, int32_t);
166 static void stabs_output(int, void *);
167 static void stabs_generate(void);
168 static void stabs_cleanup(void);
170 /* dwarf debugging routines */
171 static void dwarf_init(void);
172 static void dwarf_linenum(const char *filename
, int32_t linenumber
, int32_t);
173 static void dwarf_output(int, void *);
174 static void dwarf_generate(void);
175 static void dwarf_cleanup(void);
176 static void dwarf_findfile(const char *);
177 static void dwarf_findsect(const int);
179 static bool is_elf64(void);
180 static bool is_elf32(void);
181 static bool is_elfx32(void);
183 static bool dfmt_is_stabs(void);
184 static bool dfmt_is_dwarf(void);
187 * Special NASM section numbers which are used to define ELF special
190 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
191 static int32_t elf_got_sect
, elf_plt_sect
;
192 static int32_t elf_sym_sect
, elf_gottpoff_sect
, elf_tlsie_sect
;
194 uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
195 uint8_t elf_abiver
= 0; /* Current ABI version */
197 const struct elf_known_section elf_known_sections
[] = {
198 { ".text", SHT_PROGBITS
, SHF_ALLOC
|SHF_EXECINSTR
, 16 },
199 { ".rodata", SHT_PROGBITS
, SHF_ALLOC
, 4 },
200 { ".lrodata", SHT_PROGBITS
, SHF_ALLOC
, 4 },
201 { ".data", SHT_PROGBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
202 { ".ldata", SHT_PROGBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
203 { ".bss", SHT_NOBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
204 { ".lbss", SHT_NOBITS
, SHF_ALLOC
|SHF_WRITE
, 4 },
205 { ".tdata", SHT_PROGBITS
, SHF_ALLOC
|SHF_WRITE
|SHF_TLS
, 4 },
206 { ".tbss", SHT_NOBITS
, SHF_ALLOC
|SHF_WRITE
|SHF_TLS
, 4 },
207 { ".comment", SHT_PROGBITS
, 0, 1 },
208 { NULL
, SHT_PROGBITS
, SHF_ALLOC
, 1 } /* default */
211 /* parse section attributes */
212 static void elf_section_attrib(char *name
, char *attr
, int pass
,
213 uint32_t *flags_and
, uint32_t *flags_or
,
214 uint64_t *align
, int *type
)
216 char *opt
, *val
, *next
;
218 opt
= nasm_skip_spaces(attr
);
222 while ((opt
= nasm_opt_val(opt
, &val
, &next
))) {
223 if (!nasm_stricmp(opt
, "align")) {
225 nasm_error(ERR_NONFATAL
,
226 "section align without value specified");
231 } else if (!is_power2(*align
)) {
232 nasm_error(ERR_NONFATAL
,
233 "section alignment %"PRId64
" is not a power of two",
238 } else if (!nasm_stricmp(opt
, "alloc")) {
239 *flags_and
|= SHF_ALLOC
;
240 *flags_or
|= SHF_ALLOC
;
241 } else if (!nasm_stricmp(opt
, "noalloc")) {
242 *flags_and
|= SHF_ALLOC
;
243 *flags_or
&= ~SHF_ALLOC
;
244 } else if (!nasm_stricmp(opt
, "exec")) {
245 *flags_and
|= SHF_EXECINSTR
;
246 *flags_or
|= SHF_EXECINSTR
;
247 } else if (!nasm_stricmp(opt
, "noexec")) {
248 *flags_and
|= SHF_EXECINSTR
;
249 *flags_or
&= ~SHF_EXECINSTR
;
250 } else if (!nasm_stricmp(opt
, "write")) {
251 *flags_and
|= SHF_WRITE
;
252 *flags_or
|= SHF_WRITE
;
253 } else if (!nasm_stricmp(opt
, "tls")) {
254 *flags_and
|= SHF_TLS
;
255 *flags_or
|= SHF_TLS
;
256 } else if (!nasm_stricmp(opt
, "nowrite")) {
257 *flags_and
|= SHF_WRITE
;
258 *flags_or
&= ~SHF_WRITE
;
259 } else if (!nasm_stricmp(opt
, "progbits")) {
260 *type
= SHT_PROGBITS
;
261 } else if (!nasm_stricmp(opt
, "nobits")) {
263 } else if (pass
== 1) {
264 nasm_error(ERR_WARNING
,
265 "Unknown section attribute '%s' ignored on"
266 " declaration of section `%s'", opt
, name
);
272 static enum directive_result
273 elf_directive(enum directive directive
, char *value
, int pass
)
282 return DIRR_OK
; /* ignore in pass 2 */
284 n
= readnum(value
, &err
);
286 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
290 if (n
< 0 || n
> 255) {
291 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
298 p
= strchr(value
,',');
302 n
= readnum(p
+ 1, &err
);
303 if (err
|| n
< 0 || n
> 255) {
304 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
316 static void elf_init(void)
318 strlcpy(elf_module
, inname
, sizeof(elf_module
));
320 nsects
= sectlen
= 0;
321 syms
= saa_init((int32_t)sizeof(struct elf_symbol
));
322 nlocals
= nglobs
= ndebugs
= 0;
325 saa_wbytes(strs
, "\0", 1L);
326 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
327 strslen
= 2 + strlen(elf_module
);
329 shstrtablen
= shstrtabsize
= 0;;
330 add_sectname("", "");
335 * FIXME: tlsie is Elf32 only and
336 * gottpoff is Elfx32|64 only.
339 elf_gotpc_sect
= seg_alloc();
340 backend_label("..gotpc", elf_gotpc_sect
+ 1, 0L);
341 elf_gotoff_sect
= seg_alloc();
342 backend_label("..gotoff", elf_gotoff_sect
+ 1, 0L);
343 elf_got_sect
= seg_alloc();
344 backend_label("..got", elf_got_sect
+ 1, 0L);
345 elf_plt_sect
= seg_alloc();
346 backend_label("..plt", elf_plt_sect
+ 1, 0L);
347 elf_sym_sect
= seg_alloc();
348 backend_label("..sym", elf_sym_sect
+ 1, 0L);
349 elf_gottpoff_sect
= seg_alloc();
350 backend_label("..gottpoff", elf_gottpoff_sect
+ 1, 0L);
351 elf_tlsie_sect
= seg_alloc();
352 backend_label("..tlsie", elf_tlsie_sect
+ 1, 0L);
354 def_seg
= seg_alloc();
357 static void elf_cleanup(void)
363 for (i
= 0; i
< nsects
; i
++) {
364 if (sects
[i
]->type
!= SHT_NOBITS
)
365 saa_free(sects
[i
]->data
);
367 saa_free(sects
[i
]->rel
);
368 while (sects
[i
]->head
) {
370 sects
[i
]->head
= sects
[i
]->head
->next
;
381 /* add entry to the elf .shstrtab section */
382 static void add_sectname(const char *firsthalf
, const char *secondhalf
)
384 int len
= strlen(firsthalf
) + strlen(secondhalf
);
385 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
386 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
387 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
388 strcat(shstrtab
+ shstrtablen
, secondhalf
);
389 shstrtablen
+= len
+ 1;
392 static int elf_make_section(char *name
, int type
, int flags
, int align
)
394 struct elf_section
*s
;
396 s
= nasm_zalloc(sizeof(*s
));
398 if (type
!= SHT_NOBITS
)
399 s
->data
= saa_init(1L);
401 if (!strcmp(name
, ".text"))
404 s
->index
= seg_alloc();
405 add_sectname("", name
);
407 s
->name
= nasm_strdup(name
);
412 if (nsects
>= sectlen
)
413 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
419 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
422 uint32_t flags
, flags_and
, flags_or
;
427 *bits
= ofmt
->maxbits
;
431 p
= nasm_skip_word(name
);
434 flags_and
= flags_or
= type
= align
= 0;
436 elf_section_attrib(name
, p
, pass
, &flags_and
,
437 &flags_or
, &align
, &type
);
439 if (!strcmp(name
, ".shstrtab") ||
440 !strcmp(name
, ".symtab") ||
441 !strcmp(name
, ".strtab")) {
442 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
447 for (i
= 0; i
< nsects
; i
++)
448 if (!strcmp(name
, sects
[i
]->name
))
451 const struct elf_known_section
*ks
= elf_known_sections
;
454 if (!strcmp(name
, ks
->name
))
459 type
= type
? type
: ks
->type
;
460 align
= align
? align
: ks
->align
;
461 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
463 i
= elf_make_section(name
, type
, flags
, align
);
464 } else if (pass
== 1) {
465 if ((type
&& sects
[i
]->type
!= type
)
466 || (align
&& sects
[i
]->align
!= align
)
467 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
468 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
469 " redeclaration of section `%s'", name
);
472 return sects
[i
]->index
;
475 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
476 int is_global
, char *special
)
479 struct elf_symbol
*sym
;
480 bool special_used
= false;
482 #if defined(DEBUG) && DEBUG>2
483 nasm_error(ERR_DEBUG
,
484 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
485 name
, segment
, offset
, is_global
, special
);
487 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
489 * This is a NASM special symbol. We never allow it into
490 * the ELF symbol table, even if it's a valid one. If it
491 * _isn't_ a valid one, we should barf immediately.
493 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
495 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
496 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
497 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff") &&
498 strcmp(name
, "..tlsie"))
499 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
503 if (is_global
== 3) {
504 struct elf_symbol
**s
;
506 * Fix up a forward-reference symbol size from the first
509 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
510 if (!strcmp((*s
)->name
, name
)) {
511 struct tokenval tokval
;
513 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
517 tokval
.t_type
= TOKEN_INVALID
;
518 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, NULL
);
521 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
522 " expression as symbol size");
524 (*s
)->size
= reloc_value(e
);
528 * Remove it from the list of unresolved sizes.
530 nasm_free((*s
)->name
);
534 return; /* it wasn't an important one */
537 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
538 strslen
+= 1 + strlen(name
);
540 lastsym
= sym
= saa_wstruct(syms
);
542 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
545 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
546 sym
->other
= STV_DEFAULT
;
548 if (segment
== NO_SEG
)
549 sym
->section
= SHN_ABS
;
552 sym
->section
= SHN_UNDEF
;
553 if (segment
== def_seg
) {
554 /* we have to be sure at least text section is there */
556 if (segment
!= elf_section_names(".text", 2, &tempint
))
557 nasm_panic(0, "strange segment conditions in ELF driver");
559 for (i
= 0; i
< nsects
; i
++) {
560 if (segment
== sects
[i
]->index
) {
561 sym
->section
= i
+ 1;
567 if (is_global
== 2) {
570 sym
->section
= SHN_COMMON
;
572 * We have a common variable. Check the special text to see
573 * if it's a valid number and power of two; if so, store it
574 * as the alignment for the common variable.
578 sym
->symv
.key
= readnum(special
, &err
);
580 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
581 " valid number", special
);
582 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
583 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
584 " power of two", special
);
588 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
590 if (sym
->type
== SYM_GLOBAL
) {
592 * If sym->section == SHN_ABS, then the first line of the
593 * else section would cause a core dump, because its a reference
594 * beyond the end of the section array.
595 * This behaviour is exhibited by this code:
598 * To avoid such a crash, such requests are silently discarded.
599 * This may not be the best solution.
601 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
602 bsym
= raa_write(bsym
, segment
, nglobs
);
603 } else if (sym
->section
!= SHN_ABS
) {
605 * This is a global symbol; so we must add it to the rbtree
606 * of global symbols in its section.
608 * In addition, we check the special text for symbol
609 * type and size information.
611 sects
[sym
->section
-1]->gsyms
=
612 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
615 int n
= strcspn(special
, " \t");
617 if (!nasm_strnicmp(special
, "function", n
))
618 sym
->type
|= STT_FUNC
;
619 else if (!nasm_strnicmp(special
, "data", n
) ||
620 !nasm_strnicmp(special
, "object", n
))
621 sym
->type
|= STT_OBJECT
;
622 else if (!nasm_strnicmp(special
, "notype", n
))
623 sym
->type
|= STT_NOTYPE
;
625 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
629 special
= nasm_skip_spaces(special
);
631 n
= strcspn(special
, " \t");
632 if (!nasm_strnicmp(special
, "default", n
))
633 sym
->other
= STV_DEFAULT
;
634 else if (!nasm_strnicmp(special
, "internal", n
))
635 sym
->other
= STV_INTERNAL
;
636 else if (!nasm_strnicmp(special
, "hidden", n
))
637 sym
->other
= STV_HIDDEN
;
638 else if (!nasm_strnicmp(special
, "protected", n
))
639 sym
->other
= STV_PROTECTED
;
646 struct tokenval tokval
;
649 char *saveme
= stdscan_get();
651 while (special
[n
] && nasm_isspace(special
[n
]))
654 * We have a size expression; attempt to
658 stdscan_set(special
+ n
);
659 tokval
.t_type
= TOKEN_INVALID
;
660 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, NULL
);
664 sym
->name
= nasm_strdup(name
);
667 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
668 " expression as symbol size");
670 sym
->size
= reloc_value(e
);
677 * If TLS segment, mark symbol accordingly.
679 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
681 sym
->type
|= STT_TLS
;
684 sym
->globnum
= nglobs
;
689 if (special
&& !special_used
)
690 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
693 static void elf_add_reloc(struct elf_section
*sect
, int32_t segment
,
694 int64_t offset
, int type
)
698 r
= *sect
->tail
= nasm_zalloc(sizeof(struct elf_reloc
));
699 sect
->tail
= &r
->next
;
701 r
->address
= sect
->len
;
704 if (segment
!= NO_SEG
) {
706 for (i
= 0; i
< nsects
; i
++)
707 if (segment
== sects
[i
]->index
)
710 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
718 * This routine deals with ..got and ..sym relocations: the more
719 * complicated kinds. In shared-library writing, some relocations
720 * with respect to global symbols must refer to the precise symbol
721 * rather than referring to an offset from the base of the section
722 * _containing_ the symbol. Such relocations call to this routine,
723 * which searches the symbol list for the symbol in question.
725 * R_386_GOT32 | R_X86_64_GOT32 references require the _exact_ symbol address to be
726 * used; R_386_32 | R_X86_64_32 references can be at an offset from the symbol.
727 * The boolean argument `exact' tells us this.
729 * Return value is the adjusted value of `addr', having become an
730 * offset from the symbol rather than the section. Should always be
731 * zero when returning from an exact call.
733 * Limitation: if you define two symbols at the same place,
734 * confusion will occur.
736 * Inefficiency: we search, currently, using a linked list which
737 * isn't even necessarily sorted.
739 static int64_t elf_add_gsym_reloc(struct elf_section
*sect
,
740 int32_t segment
, uint64_t offset
,
741 int64_t pcrel
, int type
, bool exact
)
744 struct elf_section
*s
;
745 struct elf_symbol
*sym
;
750 * First look up the segment/offset pair and find a global
751 * symbol corresponding to it. If it's not one of our segments,
752 * then it must be an external symbol, in which case we're fine
753 * doing a normal elf_add_reloc after first sanity-checking
754 * that the offset from the symbol is zero.
757 for (i
= 0; i
< nsects
; i
++)
758 if (segment
== sects
[i
]->index
) {
765 nasm_error(ERR_NONFATAL
, "invalid access to an external symbol");
767 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
771 srb
= rb_search(s
->gsyms
, offset
);
772 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
773 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
774 " for this reference");
777 sym
= container_of(srb
, struct elf_symbol
, symv
);
779 r
= *sect
->tail
= nasm_malloc(sizeof(struct elf_reloc
));
780 sect
->tail
= &r
->next
;
783 r
->address
= sect
->len
;
784 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
785 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
792 static void elf32_out(int32_t segto
, const void *data
,
793 enum out_type type
, uint64_t size
,
794 int32_t segment
, int32_t wrt
)
796 struct elf_section
*s
;
800 static struct symlininfo sinfo
;
803 for (i
= 0; i
< nsects
; i
++)
804 if (segto
== sects
[i
]->index
) {
809 int tempint
; /* ignored */
810 if (segto
!= elf_section_names(".text", 2, &tempint
))
811 nasm_panic(0, "strange segment conditions in ELF driver");
813 s
= sects
[nsects
- 1];
818 /* again some stabs debugging stuff */
819 sinfo
.offset
= s
->len
;
822 sinfo
.name
= s
->name
;
823 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
824 /* end of debugging stuff */
826 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
827 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
828 " BSS section `%s': ignored", s
->name
);
829 s
->len
+= realsize(type
, size
);
835 if (s
->type
== SHT_PROGBITS
) {
836 nasm_error(ERR_WARNING
, "uninitialized space declared in"
837 " non-BSS section `%s': zeroing", s
->name
);
838 elf_sect_write(s
, NULL
, size
);
844 elf_sect_write(s
, data
, size
);
850 int asize
= abs((int)size
);
852 addr
= *(int64_t *)data
;
853 if (segment
!= NO_SEG
) {
855 nasm_error(ERR_NONFATAL
, "ELF format does not support"
856 " segment base references");
860 * The if() is a hack to deal with compilers which
861 * don't handle switch() statements with 64-bit
867 elf_add_reloc(s
, segment
, 0, R_386_8
);
871 elf_add_reloc(s
, segment
, 0, R_386_16
);
874 elf_add_reloc(s
, segment
, 0, R_386_32
);
876 default: /* Error issued further down */
879 } else if (wrt
== elf_gotpc_sect
+ 1) {
881 * The user will supply GOT relative to $$. ELF
882 * will let us have GOT relative to $. So we
883 * need to fix up the data item by $-$$.
886 elf_add_reloc(s
, segment
, 0, R_386_GOTPC
);
887 } else if (wrt
== elf_gotoff_sect
+ 1) {
888 elf_add_reloc(s
, segment
, 0, R_386_GOTOFF
);
889 } else if (wrt
== elf_tlsie_sect
+ 1) {
890 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
892 } else if (wrt
== elf_got_sect
+ 1) {
893 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
895 } else if (wrt
== elf_sym_sect
+ 1) {
899 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
904 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
908 addr
= elf_add_gsym_reloc(s
, segment
, addr
, 0,
914 } else if (wrt
== elf_plt_sect
+ 1) {
915 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
916 "relative PLT references");
918 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
920 wrt
= NO_SEG
; /* we can at least _try_ to continue */
926 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
927 "8- or 16-bit relocations in ELF32 is a GNU extension");
928 } else if (asize
!= 4 && segment
!= NO_SEG
) {
929 nasm_error(ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
931 elf_sect_writeaddr(s
, addr
, asize
);
940 reltype
= R_386_PC16
;
945 addr
= *(int64_t *)data
- size
;
946 nasm_assert(segment
!= segto
);
947 if (segment
!= NO_SEG
&& segment
% 2) {
948 nasm_error(ERR_NONFATAL
, "ELF format does not support"
949 " segment base references");
952 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
953 "8- or 16-bit relocations in ELF is a GNU extension");
954 elf_add_reloc(s
, segment
, 0, reltype
);
956 nasm_error(ERR_NONFATAL
,
957 "Unsupported non-32-bit ELF relocation");
960 elf_sect_writeaddr(s
, addr
, bytes
);
964 addr
= *(int64_t *)data
- size
;
965 if (segment
== segto
)
966 nasm_panic(0, "intra-segment OUT_REL4ADR");
967 if (segment
!= NO_SEG
&& segment
% 2) {
968 nasm_error(ERR_NONFATAL
, "ELF format does not support"
969 " segment base references");
972 elf_add_reloc(s
, segment
, 0, R_386_PC32
);
973 } else if (wrt
== elf_plt_sect
+ 1) {
974 elf_add_reloc(s
, segment
, 0, R_386_PLT32
);
975 } else if (wrt
== elf_gotpc_sect
+ 1 ||
976 wrt
== elf_gotoff_sect
+ 1 ||
977 wrt
== elf_got_sect
+ 1) {
978 nasm_error(ERR_NONFATAL
, "ELF format cannot produce PC-"
979 "relative GOT references");
981 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
983 wrt
= NO_SEG
; /* we can at least _try_ to continue */
986 elf_sect_writeaddr(s
, addr
, 4);
990 nasm_error(ERR_NONFATAL
, "32-bit ELF format does not support 64-bit relocations");
992 elf_sect_writeaddr(s
, addr
, 8);
999 static void elf64_out(int32_t segto
, const void *data
,
1000 enum out_type type
, uint64_t size
,
1001 int32_t segment
, int32_t wrt
)
1003 struct elf_section
*s
;
1007 static struct symlininfo sinfo
;
1010 * handle absolute-assembly (structure definitions)
1012 if (segto
== NO_SEG
) {
1013 if (type
!= OUT_RESERVE
)
1014 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
1020 for (i
= 0; i
< nsects
; i
++)
1021 if (segto
== sects
[i
]->index
) {
1026 int tempint
; /* ignored */
1027 if (segto
!= elf_section_names(".text", 2, &tempint
))
1028 nasm_panic(0, "strange segment conditions in ELF driver");
1030 s
= sects
[nsects
- 1];
1035 /* again some stabs debugging stuff */
1036 sinfo
.offset
= s
->len
;
1038 sinfo
.segto
= segto
;
1039 sinfo
.name
= s
->name
;
1040 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
1041 /* end of debugging stuff */
1043 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
1044 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
1045 " BSS section `%s': ignored", s
->name
);
1046 s
->len
+= realsize(type
, size
);
1052 if (s
->type
== SHT_PROGBITS
) {
1053 nasm_error(ERR_WARNING
, "uninitialized space declared in"
1054 " non-BSS section `%s': zeroing", s
->name
);
1055 elf_sect_write(s
, NULL
, size
);
1061 if (segment
!= NO_SEG
)
1062 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1063 elf_sect_write(s
, data
, size
);
1068 int isize
= (int)size
;
1069 int asize
= abs((int)size
);
1071 addr
= *(int64_t *)data
;
1072 if (segment
== NO_SEG
) {
1074 } else if (segment
% 2) {
1075 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1076 " segment base references");
1078 if (wrt
== NO_SEG
) {
1082 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
1086 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
1089 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
1092 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
1096 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
1099 nasm_panic(0, "internal error elf64-hpa-871");
1103 } else if (wrt
== elf_gotpc_sect
+ 1) {
1105 * The user will supply GOT relative to $$. ELF
1106 * will let us have GOT relative to $. So we
1107 * need to fix up the data item by $-$$.
1110 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1112 } else if (wrt
== elf_gotoff_sect
+ 1) {
1114 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
1115 "references to be qword");
1117 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
1120 } else if (wrt
== elf_got_sect
+ 1) {
1123 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1124 R_X86_64_GOT32
, true);
1128 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1129 R_X86_64_GOT64
, true);
1133 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
1136 } else if (wrt
== elf_sym_sect
+ 1) {
1140 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1146 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1147 R_X86_64_16
, false);
1151 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1152 R_X86_64_32
, false);
1156 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1157 R_X86_64_32S
, false);
1162 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1163 R_X86_64_64
, false);
1167 nasm_panic(0, "internal error elf64-hpa-903");
1170 } else if (wrt
== elf_plt_sect
+ 1) {
1171 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1172 "relative PLT references");
1174 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
1178 elf_sect_writeaddr(s
, addr
, asize
);
1183 reltype
= R_X86_64_PC8
;
1188 reltype
= R_X86_64_PC16
;
1193 addr
= *(int64_t *)data
- size
;
1194 if (segment
== segto
)
1195 nasm_panic(0, "intra-segment OUT_REL1ADR");
1196 if (segment
== NO_SEG
) {
1198 } else if (segment
% 2) {
1199 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1200 " segment base references");
1202 if (wrt
== NO_SEG
) {
1203 elf_add_reloc(s
, segment
, addr
, reltype
);
1206 nasm_error(ERR_NONFATAL
,
1207 "Unsupported non-32-bit ELF relocation");
1210 elf_sect_writeaddr(s
, addr
, bytes
);
1214 addr
= *(int64_t *)data
- size
;
1215 if (segment
== segto
)
1216 nasm_panic(0, "intra-segment OUT_REL4ADR");
1217 if (segment
== NO_SEG
) {
1219 } else if (segment
% 2) {
1220 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
1221 " segment base references");
1223 if (wrt
== NO_SEG
) {
1224 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1226 } else if (wrt
== elf_plt_sect
+ 1) {
1227 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1228 R_X86_64_PLT32
, true);
1230 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1231 wrt
== elf_got_sect
+ 1) {
1232 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1233 R_X86_64_GOTPCREL
, true);
1235 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1236 wrt
== elf_got_sect
+ 1) {
1237 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1239 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1240 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1241 R_X86_64_GOTTPOFF
, true);
1244 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1248 elf_sect_writeaddr(s
, addr
, 4);
1252 addr
= *(int64_t *)data
- size
;
1253 if (segment
== segto
)
1254 nasm_panic(0, "intra-segment OUT_REL8ADR");
1255 if (segment
== NO_SEG
) {
1257 } else if (segment
% 2) {
1258 nasm_error(ERR_NONFATAL
, "ELF64 format does not support"
1259 " segment base references");
1261 if (wrt
== NO_SEG
) {
1262 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1264 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1265 wrt
== elf_got_sect
+ 1) {
1266 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1267 R_X86_64_GOTPCREL64
, true);
1269 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1270 wrt
== elf_got_sect
+ 1) {
1271 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1273 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1274 nasm_error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1277 nasm_error(ERR_NONFATAL
, "ELF64 format does not support this"
1281 elf_sect_writeaddr(s
, addr
, 8);
1289 static void elfx32_out(int32_t segto
, const void *data
,
1290 enum out_type type
, uint64_t size
,
1291 int32_t segment
, int32_t wrt
)
1293 struct elf_section
*s
;
1297 static struct symlininfo sinfo
;
1300 * handle absolute-assembly (structure definitions)
1302 if (segto
== NO_SEG
) {
1303 if (type
!= OUT_RESERVE
)
1304 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
1310 for (i
= 0; i
< nsects
; i
++)
1311 if (segto
== sects
[i
]->index
) {
1316 int tempint
; /* ignored */
1317 if (segto
!= elf_section_names(".text", 2, &tempint
))
1318 nasm_panic(0, "strange segment conditions in ELF driver");
1320 s
= sects
[nsects
- 1];
1325 /* again some stabs debugging stuff */
1326 sinfo
.offset
= s
->len
;
1328 sinfo
.segto
= segto
;
1329 sinfo
.name
= s
->name
;
1330 dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
1331 /* end of debugging stuff */
1333 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
1334 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
1335 " BSS section `%s': ignored", s
->name
);
1336 s
->len
+= realsize(type
, size
);
1342 if (s
->type
== SHT_PROGBITS
) {
1343 nasm_error(ERR_WARNING
, "uninitialized space declared in"
1344 " non-BSS section `%s': zeroing", s
->name
);
1345 elf_sect_write(s
, NULL
, size
);
1351 if (segment
!= NO_SEG
)
1352 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1353 elf_sect_write(s
, data
, size
);
1358 int isize
= (int)size
;
1359 int asize
= abs((int)size
);
1361 addr
= *(int64_t *)data
;
1362 if (segment
== NO_SEG
) {
1364 } else if (segment
% 2) {
1365 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1366 " segment base references");
1368 if (wrt
== NO_SEG
) {
1372 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
1376 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
1379 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
1382 elf_add_reloc(s
, segment
, addr
, R_X86_64_32S
);
1386 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
1389 nasm_panic(0, "internal error elfx32-hpa-871");
1393 } else if (wrt
== elf_gotpc_sect
+ 1) {
1395 * The user will supply GOT relative to $$. ELF
1396 * will let us have GOT relative to $. So we
1397 * need to fix up the data item by $-$$.
1400 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1402 } else if (wrt
== elf_gotoff_sect
+ 1) {
1403 nasm_error(ERR_NONFATAL
, "ELFX32 doesn't support "
1404 "R_X86_64_GOTOFF64");
1405 } else if (wrt
== elf_got_sect
+ 1) {
1408 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1409 R_X86_64_GOT32
, true);
1413 nasm_error(ERR_NONFATAL
, "invalid ..got reference");
1416 } else if (wrt
== elf_sym_sect
+ 1) {
1420 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1426 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1427 R_X86_64_16
, false);
1431 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1432 R_X86_64_32
, false);
1436 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1437 R_X86_64_32S
, false);
1442 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1443 R_X86_64_64
, false);
1447 nasm_panic(0, "internal error elfx32-hpa-903");
1450 } else if (wrt
== elf_plt_sect
+ 1) {
1451 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1452 "relative PLT references");
1454 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
1458 elf_sect_writeaddr(s
, addr
, asize
);
1463 reltype
= R_X86_64_PC8
;
1468 reltype
= R_X86_64_PC16
;
1473 addr
= *(int64_t *)data
- size
;
1474 if (segment
== segto
)
1475 nasm_panic(0, "intra-segment OUT_REL1ADR");
1476 if (segment
== NO_SEG
) {
1478 } else if (segment
% 2) {
1479 nasm_error(ERR_NONFATAL
, "ELF format does not support"
1480 " segment base references");
1482 if (wrt
== NO_SEG
) {
1483 elf_add_reloc(s
, segment
, addr
, reltype
);
1486 nasm_error(ERR_NONFATAL
,
1487 "Unsupported non-32-bit ELF relocation");
1490 elf_sect_writeaddr(s
, addr
, bytes
);
1494 addr
= *(int64_t *)data
- size
;
1495 if (segment
== segto
)
1496 nasm_panic(0, "intra-segment OUT_REL4ADR");
1497 if (segment
== NO_SEG
) {
1499 } else if (segment
% 2) {
1500 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support"
1501 " segment base references");
1503 if (wrt
== NO_SEG
) {
1504 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1506 } else if (wrt
== elf_plt_sect
+ 1) {
1507 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1508 R_X86_64_PLT32
, true);
1510 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1511 wrt
== elf_got_sect
+ 1) {
1512 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1513 R_X86_64_GOTPCREL
, true);
1515 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1516 wrt
== elf_got_sect
+ 1) {
1517 nasm_error(ERR_NONFATAL
, "invalid ..gotoff reference");
1518 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1519 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1520 R_X86_64_GOTTPOFF
, true);
1523 nasm_error(ERR_NONFATAL
, "ELFX32 format does not support this"
1527 elf_sect_writeaddr(s
, addr
, 4);
1531 nasm_error(ERR_NONFATAL
, "32-bit ELF format does not support 64-bit relocations");
1533 elf_sect_writeaddr(s
, addr
, 8);
1541 static void elf_write(void)
1548 int32_t symtablen
, symtablocal
;
1551 * Work out how many sections we will have. We have SHN_UNDEF,
1552 * then the flexible user sections, then the fixed sections
1553 * `.shstrtab', `.symtab' and `.strtab', then optionally
1554 * relocation sections for the user sections.
1556 nsections
= sec_numspecial
+ 1;
1557 if (dfmt_is_stabs())
1559 else if (dfmt_is_dwarf())
1562 add_sectname("", ".shstrtab");
1563 add_sectname("", ".symtab");
1564 add_sectname("", ".strtab");
1565 for (i
= 0; i
< nsects
; i
++) {
1566 nsections
++; /* for the section itself */
1567 if (sects
[i
]->head
) {
1568 nsections
++; /* for its relocations */
1569 add_sectname(is_elf32() ? ".rel" : ".rela", sects
[i
]->name
);
1573 if (dfmt_is_stabs()) {
1574 /* in case the debug information is wanted, just add these three sections... */
1575 add_sectname("", ".stab");
1576 add_sectname("", ".stabstr");
1577 add_sectname(is_elf32() ? ".rel" : ".rela", ".stab");
1578 } else if (dfmt_is_dwarf()) {
1579 /* the dwarf debug standard specifies the following ten sections,
1580 not all of which are currently implemented,
1581 although all of them are defined. */
1582 #define debug_aranges (int64_t) (nsections-10)
1583 #define debug_info (int64_t) (nsections-7)
1584 #define debug_abbrev (int64_t) (nsections-5)
1585 #define debug_line (int64_t) (nsections-4)
1586 add_sectname("", ".debug_aranges");
1587 add_sectname(".rela", ".debug_aranges");
1588 add_sectname("", ".debug_pubnames");
1589 add_sectname("", ".debug_info");
1590 add_sectname(".rela", ".debug_info");
1591 add_sectname("", ".debug_abbrev");
1592 add_sectname("", ".debug_line");
1593 add_sectname(".rela", ".debug_line");
1594 add_sectname("", ".debug_frame");
1595 add_sectname("", ".debug_loc");
1599 * Output the ELF header.
1601 if (is_elf32() || is_elfx32()) {
1604 nasm_zero(ehdr
.e_ident
);
1605 memcpy(ehdr
.e_ident
, ELFMAG
, SELFMAG
);
1606 ehdr
.e_ident
[EI_CLASS
] = ELFCLASS32
;
1607 ehdr
.e_ident
[EI_DATA
] = ELFDATA2LSB
;
1608 ehdr
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1609 ehdr
.e_ident
[EI_OSABI
] = elf_osabi
;
1610 ehdr
.e_ident
[EI_ABIVERSION
] = elf_abiver
;
1612 ehdr
.e_type
= cpu_to_le16(ET_REL
);
1613 ehdr
.e_machine
= cpu_to_le16(is_elf32() ? EM_386
: EM_X86_64
);
1614 ehdr
.e_version
= cpu_to_le16(EV_CURRENT
);
1617 ehdr
.e_shoff
= sizeof(Elf64_Ehdr
);
1619 ehdr
.e_ehsize
= cpu_to_le16(sizeof(Elf32_Ehdr
));
1620 ehdr
.e_phentsize
= 0;
1622 ehdr
.e_shentsize
= cpu_to_le16(sizeof(Elf32_Shdr
));
1623 ehdr
.e_shnum
= cpu_to_le16(nsections
);
1624 ehdr
.e_shstrndx
= cpu_to_le16(sec_shstrtab
);
1626 nasm_write(&ehdr
, sizeof(ehdr
), ofile
);
1627 fwritezero(sizeof(Elf64_Ehdr
) - sizeof(Elf32_Ehdr
), ofile
);
1631 nasm_assert(is_elf64());
1633 nasm_zero(ehdr
.e_ident
);
1634 memcpy(ehdr
.e_ident
, ELFMAG
, SELFMAG
);
1635 ehdr
.e_ident
[EI_CLASS
] = ELFCLASS64
;
1636 ehdr
.e_ident
[EI_DATA
] = ELFDATA2LSB
;
1637 ehdr
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1638 ehdr
.e_ident
[EI_OSABI
] = elf_osabi
;
1639 ehdr
.e_ident
[EI_ABIVERSION
] = elf_abiver
;
1641 ehdr
.e_type
= cpu_to_le16(ET_REL
);
1642 ehdr
.e_machine
= cpu_to_le16(EM_X86_64
);
1643 ehdr
.e_version
= cpu_to_le16(EV_CURRENT
);
1646 ehdr
.e_shoff
= sizeof(Elf64_Ehdr
);
1648 ehdr
.e_ehsize
= cpu_to_le16(sizeof(Elf64_Ehdr
));
1649 ehdr
.e_phentsize
= 0;
1651 ehdr
.e_shentsize
= cpu_to_le16(sizeof(Elf64_Shdr
));
1652 ehdr
.e_shnum
= cpu_to_le16(nsections
);
1653 ehdr
.e_shstrndx
= cpu_to_le16(sec_shstrtab
);
1655 nasm_write(&ehdr
, sizeof(ehdr
), ofile
);
1659 * Build the symbol table and relocation tables.
1661 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1662 for (i
= 0; i
< nsects
; i
++)
1664 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1668 * Now output the section header table.
1671 elf_foffs
= sizeof(Elf64_Ehdr
) + (is_elf64() ? sizeof(Elf64_Shdr
): sizeof(Elf32_Shdr
)) * nsections
;
1672 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1675 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1678 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1681 /* The normal sections */
1682 for (i
= 0; i
< nsects
; i
++) {
1683 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1684 (sects
[i
]->type
== SHT_PROGBITS
?
1685 sects
[i
]->data
: NULL
), true,
1686 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1691 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1692 shstrtablen
, 0, 0, 1, 0);
1697 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1698 symtablen
, sec_strtab
, symtablocal
, 8, 24);
1700 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1701 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1705 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1706 strslen
, 0, 0, 1, 0);
1709 /* The relocation sections */
1711 for (i
= 0; i
< nsects
; i
++) {
1712 if (sects
[i
]->head
) {
1713 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1714 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1718 } else if (is_elfx32()) {
1719 for (i
= 0; i
< nsects
; i
++) {
1720 if (sects
[i
]->head
) {
1721 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1722 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 12);
1727 nasm_assert(is_elf64());
1728 for (i
= 0; i
< nsects
; i
++) {
1729 if (sects
[i
]->head
) {
1730 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, sects
[i
]->rel
, true,
1731 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 8, 24);
1737 if (dfmt_is_stabs()) {
1738 /* for debugging information, create the last three sections
1739 which are the .stab , .stabstr and .rel.stab sections respectively */
1741 /* this function call creates the stab sections in memory */
1744 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1745 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1746 stablen
, sec_stabstr
, 0, 4, 12);
1749 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1750 stabstrlen
, 0, 0, 4, 0);
1753 /* link -> symtable info -> section to refer to */
1755 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1756 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1758 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, stabrelbuf
, false,
1759 stabrellen
, sec_symtab
, sec_stab
, 4, is_elf64() ? 24 : 12);
1763 } else if (dfmt_is_dwarf()) {
1764 /* for dwarf debugging information, create the ten dwarf sections */
1766 /* this function call creates the dwarf sections in memory */
1770 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1771 arangeslen
, 0, 0, 1, 0);
1774 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1775 arangesrellen
, sec_symtab
,
1776 is_elf64() ? debug_aranges
: sec_debug_aranges
,
1777 1, is_elf64() ? 24 : 12);
1780 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1781 false, pubnameslen
, 0, 0, 1, 0);
1784 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1785 infolen
, 0, 0, 1, 0);
1788 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1789 inforellen
, sec_symtab
,
1790 is_elf64() ? debug_info
: sec_debug_info
,
1791 1, is_elf64() ? 24 : 12);
1794 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1795 abbrevlen
, 0, 0, 1, 0);
1798 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1799 linelen
, 0, 0, 1, 0);
1802 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1803 linerellen
, sec_symtab
,
1804 is_elf64() ? debug_line
: sec_debug_line
,
1805 1, is_elf64() ? 24 : 12);
1808 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1809 framelen
, 0, 0, 8, 0);
1812 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1813 loclen
, 0, 0, 1, 0);
1816 fwritezero(align
, ofile
);
1819 * Now output the sections.
1821 elf_write_sections();
1823 nasm_free(elf_sects
);
1827 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1829 struct SAA
*s
= saa_init(1L);
1830 struct elf_symbol
*sym
;
1833 size_t usize
= is_elf64() ? sizeof(Elf64_Sym
) : sizeof(Elf32_Sym
);
1842 * Zero symbol first as required by spec.
1844 saa_wbytes(s
, NULL
, usize
);
1849 * Next, an entry for the file name.
1852 u
.sym64
.st_name
= cpu_to_le32(1);
1853 u
.sym64
.st_info
= ELF64_ST_INFO(STB_LOCAL
, STT_FILE
);
1854 u
.sym64
.st_other
= 0;
1855 u
.sym64
.st_shndx
= cpu_to_le16(SHN_ABS
);
1856 u
.sym64
.st_value
= 0;
1857 u
.sym64
.st_size
= 0;
1859 u
.sym32
.st_name
= cpu_to_le32(1);
1860 u
.sym32
.st_value
= 0;
1861 u
.sym32
.st_size
= 0;
1862 u
.sym32
.st_info
= ELF32_ST_INFO(STB_LOCAL
, STT_FILE
);
1863 u
.sym32
.st_other
= 0;
1864 u
.sym32
.st_shndx
= cpu_to_le16(SHN_ABS
);
1866 saa_wbytes(s
, &u
, usize
);
1872 * Now some standard symbols defining the segments, for relocation
1876 u
.sym64
.st_name
= 0;
1877 u
.sym64
.st_other
= 0;
1878 u
.sym64
.st_value
= 0;
1879 u
.sym64
.st_size
= 0;
1880 for (i
= 1; i
<= nsects
; i
++) {
1881 u
.sym64
.st_info
= ELF64_ST_INFO(STB_LOCAL
, STT_SECTION
);
1882 u
.sym64
.st_shndx
= cpu_to_le16(i
);
1883 saa_wbytes(s
, &u
, usize
);
1888 u
.sym32
.st_name
= 0;
1889 u
.sym32
.st_value
= 0;
1890 u
.sym32
.st_size
= 0;
1891 u
.sym32
.st_other
= 0;
1892 for (i
= 1; i
<= nsects
; i
++) {
1893 u
.sym32
.st_info
= ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
);
1894 u
.sym32
.st_shndx
= cpu_to_le16(i
);
1895 saa_wbytes(s
, &u
, usize
);
1902 * Now the other local symbols.
1906 while ((sym
= saa_rstruct(syms
))) {
1907 if (sym
->type
& SYM_GLOBAL
)
1909 u
.sym64
.st_name
= cpu_to_le32(sym
->strpos
);
1910 u
.sym64
.st_info
= sym
->type
;
1911 u
.sym64
.st_other
= sym
->other
;
1912 u
.sym64
.st_shndx
= cpu_to_le16(sym
->section
);
1913 u
.sym64
.st_value
= cpu_to_le64(sym
->symv
.key
);
1914 u
.sym64
.st_size
= cpu_to_le64(sym
->size
);
1915 saa_wbytes(s
, &u
, usize
);
1920 * dwarf needs symbols for debug sections
1921 * which are relocation targets.
1923 if (dfmt_is_dwarf()) {
1924 dwarf_infosym
= *local
;
1925 u
.sym64
.st_name
= 0;
1926 u
.sym64
.st_info
= ELF64_ST_INFO(STB_LOCAL
, STT_SECTION
);
1927 u
.sym64
.st_other
= 0;
1928 u
.sym64
.st_shndx
= cpu_to_le16(debug_info
);
1929 u
.sym64
.st_value
= 0;
1930 u
.sym64
.st_size
= 0;
1931 saa_wbytes(s
, &u
, usize
);
1934 dwarf_abbrevsym
= *local
;
1935 u
.sym64
.st_name
= 0;
1936 u
.sym64
.st_info
= ELF64_ST_INFO(STB_LOCAL
, STT_SECTION
);
1937 u
.sym64
.st_other
= 0;
1938 u
.sym64
.st_shndx
= cpu_to_le16(debug_abbrev
);
1939 u
.sym64
.st_value
= 0;
1940 u
.sym64
.st_size
= 0;
1941 saa_wbytes(s
, &u
, usize
);
1944 dwarf_linesym
= *local
;
1945 u
.sym64
.st_name
= 0;
1946 u
.sym64
.st_info
= ELF64_ST_INFO(STB_LOCAL
, STT_SECTION
);
1947 u
.sym64
.st_other
= 0;
1948 u
.sym64
.st_shndx
= cpu_to_le16(debug_line
);
1949 u
.sym64
.st_value
= 0;
1950 u
.sym64
.st_size
= 0;
1951 saa_wbytes(s
, &u
, usize
);
1956 while ((sym
= saa_rstruct(syms
))) {
1957 if (sym
->type
& SYM_GLOBAL
)
1959 u
.sym32
.st_name
= cpu_to_le32(sym
->strpos
);
1960 u
.sym32
.st_value
= cpu_to_le32(sym
->symv
.key
);
1961 u
.sym32
.st_size
= cpu_to_le32(sym
->size
);
1962 u
.sym32
.st_info
= sym
->type
;
1963 u
.sym32
.st_other
= sym
->other
;
1964 u
.sym32
.st_shndx
= cpu_to_le16(sym
->section
);
1965 saa_wbytes(s
, &u
, usize
);
1970 * dwarf needs symbols for debug sections
1971 * which are relocation targets.
1973 if (dfmt_is_dwarf()) {
1974 dwarf_infosym
= *local
;
1975 u
.sym32
.st_name
= 0;
1976 u
.sym32
.st_value
= 0;
1977 u
.sym32
.st_size
= 0;
1978 u
.sym32
.st_info
= ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
);
1979 u
.sym32
.st_other
= 0;
1980 u
.sym32
.st_shndx
= cpu_to_le16(sec_debug_info
);
1981 saa_wbytes(s
, &u
, usize
);
1984 dwarf_abbrevsym
= *local
;
1985 u
.sym32
.st_name
= 0;
1986 u
.sym32
.st_value
= 0;
1987 u
.sym32
.st_size
= 0;
1988 u
.sym32
.st_info
= ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
);
1989 u
.sym32
.st_other
= 0;
1990 u
.sym32
.st_shndx
= cpu_to_le16(sec_debug_abbrev
);
1991 saa_wbytes(s
, &u
, usize
);
1994 dwarf_linesym
= *local
;
1995 u
.sym32
.st_name
= 0;
1996 u
.sym32
.st_value
= 0;
1997 u
.sym32
.st_size
= 0;
1998 u
.sym32
.st_info
= ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
);
1999 u
.sym32
.st_other
= 0;
2000 u
.sym32
.st_shndx
= cpu_to_le16(sec_debug_line
);
2001 saa_wbytes(s
, &u
, usize
);
2008 * Now the global symbols.
2012 while ((sym
= saa_rstruct(syms
))) {
2013 if (!(sym
->type
& SYM_GLOBAL
))
2015 u
.sym64
.st_name
= cpu_to_le32(sym
->strpos
);
2016 u
.sym64
.st_info
= sym
->type
;
2017 u
.sym64
.st_other
= sym
->other
;
2018 u
.sym64
.st_shndx
= cpu_to_le16(sym
->section
);
2019 u
.sym64
.st_value
= cpu_to_le64(sym
->symv
.key
);
2020 u
.sym64
.st_size
= cpu_to_le64(sym
->size
);
2021 saa_wbytes(s
, &u
, usize
);
2025 while ((sym
= saa_rstruct(syms
))) {
2026 if (!(sym
->type
& SYM_GLOBAL
))
2028 u
.sym32
.st_name
= cpu_to_le32(sym
->strpos
);
2029 u
.sym32
.st_value
= cpu_to_le32(sym
->symv
.key
);
2030 u
.sym32
.st_size
= cpu_to_le32(sym
->size
);
2031 u
.sym32
.st_info
= sym
->type
;
2032 u
.sym32
.st_other
= sym
->other
;
2033 u
.sym32
.st_shndx
= cpu_to_le16(sym
->section
);
2034 saa_wbytes(s
, &u
, usize
);
2042 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
2045 int32_t global_offset
;
2047 size_t usize
= is_elf64() ? sizeof(Elf64_Rela
) :
2048 (is_elfx32() ? sizeof(Elf32_Rela
) : sizeof(Elf32_Rel
));
2062 * How to onvert from a global placeholder to a real symbol index;
2063 * the +2 refers to the two special entries, the null entry and
2064 * the filename entry.
2066 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
2070 int32_t sym
= r
->symbol
;
2072 if (sym
>= GLOBAL_TEMP_BASE
)
2073 sym
+= global_offset
;
2075 u
.rel32
.r_offset
= cpu_to_le32(r
->address
);
2076 u
.rel32
.r_info
= cpu_to_le32(ELF32_R_INFO(sym
, r
->type
));
2077 saa_wbytes(s
, &u
, usize
);
2082 } else if (is_elfx32()) {
2084 int32_t sym
= r
->symbol
;
2086 if (sym
>= GLOBAL_TEMP_BASE
)
2087 sym
+= global_offset
;
2089 u
.rela32
.r_offset
= cpu_to_le32(r
->address
);
2090 u
.rela32
.r_info
= cpu_to_le32(ELF32_R_INFO(sym
, r
->type
));
2091 u
.rela32
.r_addend
= cpu_to_le32(r
->offset
);
2092 saa_wbytes(s
, &u
, usize
);
2098 nasm_assert(is_elf64());
2100 int32_t sym
= r
->symbol
;
2102 if (sym
>= GLOBAL_TEMP_BASE
)
2103 sym
+= global_offset
;
2105 u
.rela64
.r_offset
= cpu_to_le64(r
->address
);
2106 u
.rela64
.r_info
= cpu_to_le64(ELF64_R_INFO(sym
, r
->type
));
2107 u
.rela64
.r_addend
= cpu_to_le64(r
->offset
);
2108 saa_wbytes(s
, &u
, usize
);
2118 static void elf_section_header(int name
, int type
, uint64_t flags
,
2119 void *data
, bool is_saa
, uint64_t datalen
,
2120 int link
, int info
, int align
, int eltsize
)
2127 elf_sects
[elf_nsect
].data
= data
;
2128 elf_sects
[elf_nsect
].len
= datalen
;
2129 elf_sects
[elf_nsect
].is_saa
= is_saa
;
2132 if (is_elf32() || is_elfx32()) {
2133 shdr
.shdr32
.sh_name
= cpu_to_le32(name
);
2134 shdr
.shdr32
.sh_type
= cpu_to_le32(type
);
2135 shdr
.shdr32
.sh_flags
= cpu_to_le32(flags
);
2136 shdr
.shdr32
.sh_addr
= 0;
2137 shdr
.shdr32
.sh_offset
= cpu_to_le32(type
== SHT_NULL
? 0 : elf_foffs
);
2138 shdr
.shdr32
.sh_size
= cpu_to_le32(datalen
);
2140 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2141 shdr
.shdr32
.sh_link
= cpu_to_le32(link
);
2142 shdr
.shdr32
.sh_info
= cpu_to_le32(info
);
2143 shdr
.shdr32
.sh_addralign
= cpu_to_le32(align
);
2144 shdr
.shdr32
.sh_entsize
= cpu_to_le32(eltsize
);
2146 nasm_assert(is_elf64());
2148 shdr
.shdr64
.sh_name
= cpu_to_le32(name
);
2149 shdr
.shdr64
.sh_type
= cpu_to_le32(type
);
2150 shdr
.shdr64
.sh_flags
= cpu_to_le64(flags
);
2151 shdr
.shdr64
.sh_addr
= 0;
2152 shdr
.shdr64
.sh_offset
= cpu_to_le64(type
== SHT_NULL
? 0 : elf_foffs
);
2153 shdr
.shdr64
.sh_size
= cpu_to_le32(datalen
);
2155 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
2156 shdr
.shdr64
.sh_link
= cpu_to_le32(link
);
2157 shdr
.shdr64
.sh_info
= cpu_to_le32(info
);
2158 shdr
.shdr64
.sh_addralign
= cpu_to_le64(align
);
2159 shdr
.shdr64
.sh_entsize
= cpu_to_le64(eltsize
);
2162 nasm_write(&shdr
, is_elf64() ? sizeof(shdr
.shdr64
) : sizeof(shdr
.shdr32
), ofile
);
2165 static void elf_write_sections(void)
2168 for (i
= 0; i
< elf_nsect
; i
++)
2169 if (elf_sects
[i
].data
) {
2170 int32_t len
= elf_sects
[i
].len
;
2171 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
2172 int32_t align
= reallen
- len
;
2173 if (elf_sects
[i
].is_saa
)
2174 saa_fpwrite(elf_sects
[i
].data
, ofile
);
2176 nasm_write(elf_sects
[i
].data
, len
, ofile
);
2177 fwritezero(align
, ofile
);
2181 static void elf_sect_write(struct elf_section
*sect
, const void *data
, size_t len
)
2183 saa_wbytes(sect
->data
, data
, len
);
2187 static void elf_sect_writeaddr(struct elf_section
*sect
, int64_t data
, size_t len
)
2189 saa_writeaddr(sect
->data
, data
, len
);
2193 static void elf_sectalign(int32_t seg
, unsigned int value
)
2195 struct elf_section
*s
= NULL
;
2198 for (i
= 0; i
< nsects
; i
++) {
2199 if (sects
[i
]->index
== seg
) {
2204 if (!s
|| !is_power2(value
))
2207 if (value
> s
->align
)
2211 extern macros_t elf_stdmac
[];
2213 /* Claim "elf" as a pragma namespace, for the future */
2214 static const struct pragma_facility elf_pragma_list
[] =
2217 { NULL
, NULL
} /* Implements the canonical output name */
2221 static const struct dfmt elf32_df_dwarf
= {
2222 "ELF32 (i386) dwarf debug format for Linux/Unix",
2226 null_debug_deflabel
,
2227 null_debug_directive
,
2231 NULL
/* pragma list */
2234 static const struct dfmt elf32_df_stabs
= {
2235 "ELF32 (i386) stabs debug format for Linux/Unix",
2239 null_debug_deflabel
,
2240 null_debug_directive
,
2244 NULL
/* pragma list */
2247 static const struct dfmt
* const elf32_debugs_arr
[3] =
2248 { &elf32_df_dwarf
, &elf32_df_stabs
, NULL
};
2250 const struct ofmt of_elf32
= {
2251 "ELF32 (i386) object files (e.g. Linux)",
2261 nasm_do_legacy_output
,
2273 static const struct dfmt elf64_df_dwarf
= {
2274 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
2278 null_debug_deflabel
,
2279 null_debug_directive
,
2283 NULL
/* pragma list */
2286 static const struct dfmt elf64_df_stabs
= {
2287 "ELF64 (x86-64) stabs debug format for Linux/Unix",
2291 null_debug_deflabel
,
2292 null_debug_directive
,
2296 NULL
/* pragma list */
2299 static const struct dfmt
* const elf64_debugs_arr
[3] =
2300 { &elf64_df_dwarf
, &elf64_df_stabs
, NULL
};
2302 const struct ofmt of_elf64
= {
2303 "ELF64 (x86_64) object files (e.g. Linux)",
2313 nasm_do_legacy_output
,
2325 static const struct dfmt elfx32_df_dwarf
= {
2326 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
2330 null_debug_deflabel
,
2331 null_debug_directive
,
2335 NULL
/* pragma list */
2338 static const struct dfmt elfx32_df_stabs
= {
2339 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
2343 null_debug_deflabel
,
2344 null_debug_directive
,
2351 static const struct dfmt
* const elfx32_debugs_arr
[3] =
2352 { &elfx32_df_dwarf
, &elfx32_df_stabs
, NULL
};
2354 const struct ofmt of_elfx32
= {
2355 "ELFX32 (x86_64) object files (e.g. Linux)",
2365 nasm_do_legacy_output
,
2374 NULL
/* pragma list */
2377 static bool is_elf64(void)
2379 return ofmt
== &of_elf64
;
2382 static bool is_elf32(void)
2384 return ofmt
== &of_elf32
;
2387 static bool is_elfx32(void)
2389 return ofmt
== &of_elfx32
;
2392 static bool dfmt_is_stabs(void)
2394 return dfmt
== &elf32_df_stabs
||
2395 dfmt
== &elfx32_df_stabs
||
2396 dfmt
== &elf64_df_stabs
;
2399 static bool dfmt_is_dwarf(void)
2401 return dfmt
== &elf32_df_dwarf
||
2402 dfmt
== &elfx32_df_dwarf
||
2403 dfmt
== &elf64_df_dwarf
;
2406 /* common debugging routines */
2407 static void debug_typevalue(int32_t type
)
2409 int32_t stype
, ssize
;
2410 switch (TYM_TYPE(type
)) {
2457 stype
= STT_SECTION
;
2472 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
2473 lastsym
->size
= ssize
;
2474 lastsym
->type
= stype
;
2478 /* stabs debugging routines */
2480 static void stabs_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
2483 if (!stabs_filename
) {
2484 stabs_filename
= nasm_malloc(strlen(filename
) + 1);
2485 strcpy(stabs_filename
, filename
);
2487 if (strcmp(stabs_filename
, filename
)) {
2488 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2489 in fact, this leak comes in quite handy to maintain a list of files
2490 encountered so far in the symbol lines... */
2492 /* why not nasm_free(stabs_filename); we're done with the old one */
2494 stabs_filename
= nasm_malloc(strlen(filename
) + 1);
2495 strcpy(stabs_filename
, filename
);
2499 currentline
= linenumber
;
2502 static void stabs_output(int type
, void *param
)
2504 struct symlininfo
*s
;
2505 struct linelist
*el
;
2506 if (type
== TY_DEBUGSYMLIN
) {
2507 if (debug_immcall
) {
2508 s
= (struct symlininfo
*)param
;
2509 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2510 return; /* line info is only collected for executable sections */
2512 el
= nasm_malloc(sizeof(struct linelist
));
2513 el
->info
.offset
= s
->offset
;
2514 el
->info
.section
= s
->section
;
2515 el
->info
.name
= s
->name
;
2516 el
->line
= currentline
;
2517 el
->filename
= stabs_filename
;
2520 stabslines
->last
->next
= el
;
2521 stabslines
->last
= el
;
2524 stabslines
->last
= el
;
2531 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2533 static void stabs_generate(void)
2535 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
2536 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
2540 struct linelist
*ptr
;
2544 allfiles
= nasm_zalloc(numlinestabs
* sizeof(char *));
2547 if (numfiles
== 0) {
2548 allfiles
[0] = ptr
->filename
;
2551 for (i
= 0; i
< numfiles
; i
++) {
2552 if (!strcmp(allfiles
[i
], ptr
->filename
))
2555 if (i
>= numfiles
) {
2556 allfiles
[i
] = ptr
->filename
;
2563 fileidx
= nasm_malloc(numfiles
* sizeof(int));
2564 for (i
= 0; i
< numfiles
; i
++) {
2565 fileidx
[i
] = strsize
;
2566 strsize
+= strlen(allfiles
[i
]) + 1;
2568 currfile
= mainfileindex
= 0;
2569 for (i
= 0; i
< numfiles
; i
++) {
2570 if (!strcmp(allfiles
[i
], elf_module
)) {
2571 currfile
= mainfileindex
= i
;
2577 * worst case size of the stab buffer would be:
2578 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2579 * plus one "ending" entry
2581 sbuf
= nasm_malloc((numlinestabs
* 2 + 4) *
2582 sizeof(struct stabentry
));
2583 ssbuf
= nasm_malloc(strsize
);
2584 rbuf
= nasm_malloc(numlinestabs
* (is_elf64() ? 16 : 8) * (2 + 3));
2587 for (i
= 0; i
< numfiles
; i
++)
2588 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
2591 stabstrlen
= strsize
; /* set global variable for length of stab strings */
2599 * this is the first stab, its strx points to the filename of the
2600 * the source-file, the n_desc field should be set to the number
2601 * of remaining stabs
2603 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, stabstrlen
);
2605 /* this is the stab for the main source file */
2606 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
2608 /* relocation table entry */
2611 * Since the symbol table has two entries before
2612 * the section symbols, the index in the info.section
2613 * member must be adjusted by adding 2
2617 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2618 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2619 } else if (is_elfx32()) {
2620 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2621 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2624 nasm_assert(is_elf64());
2625 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2626 WRITELONG(rptr
, R_X86_64_32
);
2627 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2628 WRITEDLONG(rptr
, 0);
2635 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2636 /* oops file has changed... */
2637 for (i
= 0; i
< numfiles
; i
++)
2638 if (!strcmp(allfiles
[i
], ptr
->filename
))
2641 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2645 /* relocation table entry */
2646 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2647 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2650 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2653 /* relocation table entry */
2654 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2655 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
2659 } else if (is_elfx32()) {
2661 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2662 /* oops file has changed... */
2663 for (i
= 0; i
< numfiles
; i
++)
2664 if (!strcmp(allfiles
[i
], ptr
->filename
))
2667 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2671 /* relocation table entry */
2672 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2673 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2674 WRITELONG(rptr
, ptr
->info
.offset
);
2677 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2680 /* relocation table entry */
2681 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
2682 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_X86_64_32
);
2683 WRITELONG(rptr
, ptr
->info
.offset
);
2688 nasm_assert(is_elf64());
2690 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
2691 /* oops file has changed... */
2692 for (i
= 0; i
< numfiles
; i
++)
2693 if (!strcmp(allfiles
[i
], ptr
->filename
))
2696 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
2700 /* relocation table entry */
2701 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2702 WRITELONG(rptr
, R_X86_64_32
);
2703 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2704 WRITEDLONG(rptr
, ptr
->info
.offset
);
2707 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
2710 /* relocation table entry */
2711 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
2712 WRITELONG(rptr
, R_X86_64_32
);
2713 WRITELONG(rptr
, ptr
->info
.section
+ 2);
2714 WRITEDLONG(rptr
, ptr
->info
.offset
);
2720 /* this is an "ending" token */
2721 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
2724 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
2726 nasm_free(allfiles
);
2729 stablen
= (sptr
- sbuf
);
2730 stabrellen
= (rptr
- rbuf
);
2736 static void stabs_cleanup(void)
2738 struct linelist
*ptr
, *del
;
2750 nasm_free(stabrelbuf
);
2751 nasm_free(stabstrbuf
);
2754 /* dwarf routines */
2756 static void dwarf_init(void)
2758 ndebugs
= 3; /* 3 debug symbols */
2761 static void dwarf_linenum(const char *filename
, int32_t linenumber
,
2765 dwarf_findfile(filename
);
2767 currentline
= linenumber
;
2770 /* called from elf_out with type == TY_DEBUGSYMLIN */
2771 static void dwarf_output(int type
, void *param
)
2773 int ln
, aa
, inx
, maxln
, soc
;
2774 struct symlininfo
*s
;
2779 s
= (struct symlininfo
*)param
;
2781 /* line number info is only gathered for executable sections */
2782 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2785 /* Check if section index has changed */
2786 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
2787 dwarf_findsect(s
->section
);
2789 /* do nothing unless line or file has changed */
2793 ln
= currentline
- dwarf_csect
->line
;
2794 aa
= s
->offset
- dwarf_csect
->offset
;
2795 inx
= dwarf_clist
->line
;
2796 plinep
= dwarf_csect
->psaa
;
2797 /* check for file change */
2798 if (!(inx
== dwarf_csect
->file
)) {
2799 saa_write8(plinep
,DW_LNS_set_file
);
2800 saa_write8(plinep
,inx
);
2801 dwarf_csect
->file
= inx
;
2803 /* check for line change */
2805 /* test if in range of special op code */
2806 maxln
= line_base
+ line_range
;
2807 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
2808 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
2809 saa_write8(plinep
,soc
);
2811 saa_write8(plinep
,DW_LNS_advance_line
);
2812 saa_wleb128s(plinep
,ln
);
2814 saa_write8(plinep
,DW_LNS_advance_pc
);
2815 saa_wleb128u(plinep
,aa
);
2817 saa_write8(plinep
,DW_LNS_copy
);
2819 dwarf_csect
->line
= currentline
;
2820 dwarf_csect
->offset
= s
->offset
;
2823 /* show change handled */
2828 static void dwarf_generate(void)
2832 struct linelist
*ftentry
;
2833 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
2834 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
2835 struct sectlist
*psect
;
2836 size_t saalen
, linepoff
, totlen
, highaddr
;
2839 /* write epilogues for each line program range */
2840 /* and build aranges section */
2841 paranges
= saa_init(1L);
2842 parangesrel
= saa_init(1L);
2843 saa_write16(paranges
,2); /* dwarf version */
2844 saa_write32(parangesrel
, paranges
->datalen
+4);
2845 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
2846 saa_write32(parangesrel
, 0);
2847 saa_write32(paranges
,0); /* offset into info */
2848 saa_write8(paranges
,4); /* pointer size */
2849 saa_write8(paranges
,0); /* not segmented */
2850 saa_write32(paranges
,0); /* padding */
2851 /* iterate though sectlist entries */
2852 psect
= dwarf_fsect
;
2855 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2856 plinep
= psect
->psaa
;
2857 /* Line Number Program Epilogue */
2858 saa_write8(plinep
,2); /* std op 2 */
2859 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2860 saa_write8(plinep
,DW_LNS_extended_op
);
2861 saa_write8(plinep
,1); /* operand length */
2862 saa_write8(plinep
,DW_LNE_end_sequence
);
2863 totlen
+= plinep
->datalen
;
2864 /* range table relocation entry */
2865 saa_write32(parangesrel
, paranges
->datalen
+ 4);
2866 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2867 saa_write32(parangesrel
, (uint32_t) 0);
2868 /* range table entry */
2869 saa_write32(paranges
,0x0000); /* range start */
2870 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
2871 highaddr
+= sects
[psect
->section
]->len
;
2872 /* done with this entry */
2873 psect
= psect
->next
;
2875 saa_write32(paranges
,0); /* null address */
2876 saa_write32(paranges
,0); /* null length */
2877 saalen
= paranges
->datalen
;
2878 arangeslen
= saalen
+ 4;
2879 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2880 WRITELONG(pbuf
,saalen
); /* initial length */
2881 saa_rnbytes(paranges
, pbuf
, saalen
);
2883 } else if (is_elfx32()) {
2884 /* write epilogues for each line program range */
2885 /* and build aranges section */
2886 paranges
= saa_init(1L);
2887 parangesrel
= saa_init(1L);
2888 saa_write16(paranges
,3); /* dwarf version */
2889 saa_write32(parangesrel
, paranges
->datalen
+4);
2890 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_X86_64_32
); /* reloc to info */
2891 saa_write32(parangesrel
, 0);
2892 saa_write32(paranges
,0); /* offset into info */
2893 saa_write8(paranges
,4); /* pointer size */
2894 saa_write8(paranges
,0); /* not segmented */
2895 saa_write32(paranges
,0); /* padding */
2896 /* iterate though sectlist entries */
2897 psect
= dwarf_fsect
;
2900 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2901 plinep
= psect
->psaa
;
2902 /* Line Number Program Epilogue */
2903 saa_write8(plinep
,2); /* std op 2 */
2904 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2905 saa_write8(plinep
,DW_LNS_extended_op
);
2906 saa_write8(plinep
,1); /* operand length */
2907 saa_write8(plinep
,DW_LNE_end_sequence
);
2908 totlen
+= plinep
->datalen
;
2909 /* range table relocation entry */
2910 saa_write32(parangesrel
, paranges
->datalen
+ 4);
2911 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_X86_64_32
);
2912 saa_write32(parangesrel
, (uint32_t) 0);
2913 /* range table entry */
2914 saa_write32(paranges
,0x0000); /* range start */
2915 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
2916 highaddr
+= sects
[psect
->section
]->len
;
2917 /* done with this entry */
2918 psect
= psect
->next
;
2920 saa_write32(paranges
,0); /* null address */
2921 saa_write32(paranges
,0); /* null length */
2922 saalen
= paranges
->datalen
;
2923 arangeslen
= saalen
+ 4;
2924 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2925 WRITELONG(pbuf
,saalen
); /* initial length */
2926 saa_rnbytes(paranges
, pbuf
, saalen
);
2929 nasm_assert(is_elf64());
2930 /* write epilogues for each line program range */
2931 /* and build aranges section */
2932 paranges
= saa_init(1L);
2933 parangesrel
= saa_init(1L);
2934 saa_write16(paranges
,3); /* dwarf version */
2935 saa_write64(parangesrel
, paranges
->datalen
+4);
2936 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
2937 saa_write64(parangesrel
, 0);
2938 saa_write32(paranges
,0); /* offset into info */
2939 saa_write8(paranges
,8); /* pointer size */
2940 saa_write8(paranges
,0); /* not segmented */
2941 saa_write32(paranges
,0); /* padding */
2942 /* iterate though sectlist entries */
2943 psect
= dwarf_fsect
;
2946 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2947 plinep
= psect
->psaa
;
2948 /* Line Number Program Epilogue */
2949 saa_write8(plinep
,2); /* std op 2 */
2950 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2951 saa_write8(plinep
,DW_LNS_extended_op
);
2952 saa_write8(plinep
,1); /* operand length */
2953 saa_write8(plinep
,DW_LNE_end_sequence
);
2954 totlen
+= plinep
->datalen
;
2955 /* range table relocation entry */
2956 saa_write64(parangesrel
, paranges
->datalen
+ 4);
2957 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2958 saa_write64(parangesrel
, (uint64_t) 0);
2959 /* range table entry */
2960 saa_write64(paranges
,0x0000); /* range start */
2961 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
2962 highaddr
+= sects
[psect
->section
]->len
;
2963 /* done with this entry */
2964 psect
= psect
->next
;
2966 saa_write64(paranges
,0); /* null address */
2967 saa_write64(paranges
,0); /* null length */
2968 saalen
= paranges
->datalen
;
2969 arangeslen
= saalen
+ 4;
2970 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2971 WRITELONG(pbuf
,saalen
); /* initial length */
2972 saa_rnbytes(paranges
, pbuf
, saalen
);
2976 /* build rela.aranges section */
2977 arangesrellen
= saalen
= parangesrel
->datalen
;
2978 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2979 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2980 saa_free(parangesrel
);
2982 /* build pubnames section */
2983 ppubnames
= saa_init(1L);
2984 saa_write16(ppubnames
,3); /* dwarf version */
2985 saa_write32(ppubnames
,0); /* offset into info */
2986 saa_write32(ppubnames
,0); /* space used in info */
2987 saa_write32(ppubnames
,0); /* end of list */
2988 saalen
= ppubnames
->datalen
;
2989 pubnameslen
= saalen
+ 4;
2990 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2991 WRITELONG(pbuf
,saalen
); /* initial length */
2992 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2993 saa_free(ppubnames
);
2996 /* build info section */
2997 pinfo
= saa_init(1L);
2998 pinforel
= saa_init(1L);
2999 saa_write16(pinfo
,2); /* dwarf version */
3000 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3001 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
3002 saa_write32(pinforel
, 0);
3003 saa_write32(pinfo
,0); /* offset into abbrev */
3004 saa_write8(pinfo
,4); /* pointer size */
3005 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
3006 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3007 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
3008 saa_write32(pinforel
, 0);
3009 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3010 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3011 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
3012 saa_write32(pinforel
, 0);
3013 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
3014 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3015 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
3016 saa_write32(pinforel
, 0);
3017 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3018 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3019 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3020 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3021 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3022 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3023 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
3024 saa_write32(pinforel
, 0);
3025 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3026 saa_write32(pinfo
,0); /* DW_AT_frame_base */
3027 saa_write8(pinfo
,0); /* end of entries */
3028 saalen
= pinfo
->datalen
;
3029 infolen
= saalen
+ 4;
3030 infobuf
= pbuf
= nasm_malloc(infolen
);
3031 WRITELONG(pbuf
,saalen
); /* initial length */
3032 saa_rnbytes(pinfo
, pbuf
, saalen
);
3034 } else if (is_elfx32()) {
3035 /* build info section */
3036 pinfo
= saa_init(1L);
3037 pinforel
= saa_init(1L);
3038 saa_write16(pinfo
,3); /* dwarf version */
3039 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3040 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_X86_64_32
); /* reloc to abbrev */
3041 saa_write32(pinforel
, 0);
3042 saa_write32(pinfo
,0); /* offset into abbrev */
3043 saa_write8(pinfo
,4); /* pointer size */
3044 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
3045 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3046 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3047 saa_write32(pinforel
, 0);
3048 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3049 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3050 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3051 saa_write32(pinforel
, 0);
3052 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
3053 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3054 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_X86_64_32
); /* reloc to line */
3055 saa_write32(pinforel
, 0);
3056 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3057 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3058 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3059 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3060 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3061 saa_write32(pinforel
, pinfo
->datalen
+ 4);
3062 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_X86_64_32
);
3063 saa_write32(pinforel
, 0);
3064 saa_write32(pinfo
,0); /* DW_AT_low_pc */
3065 saa_write32(pinfo
,0); /* DW_AT_frame_base */
3066 saa_write8(pinfo
,0); /* end of entries */
3067 saalen
= pinfo
->datalen
;
3068 infolen
= saalen
+ 4;
3069 infobuf
= pbuf
= nasm_malloc(infolen
);
3070 WRITELONG(pbuf
,saalen
); /* initial length */
3071 saa_rnbytes(pinfo
, pbuf
, saalen
);
3074 nasm_assert(is_elf64());
3075 /* build info section */
3076 pinfo
= saa_init(1L);
3077 pinforel
= saa_init(1L);
3078 saa_write16(pinfo
,3); /* dwarf version */
3079 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3080 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
3081 saa_write64(pinforel
, 0);
3082 saa_write32(pinfo
,0); /* offset into abbrev */
3083 saa_write8(pinfo
,8); /* pointer size */
3084 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
3085 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3086 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3087 saa_write64(pinforel
, 0);
3088 saa_write64(pinfo
,0); /* DW_AT_low_pc */
3089 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3090 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3091 saa_write64(pinforel
, 0);
3092 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
3093 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3094 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
3095 saa_write64(pinforel
, 0);
3096 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
3097 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
3098 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
3099 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
3100 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
3101 saa_write64(pinforel
, pinfo
->datalen
+ 4);
3102 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
3103 saa_write64(pinforel
, 0);
3104 saa_write64(pinfo
,0); /* DW_AT_low_pc */
3105 saa_write64(pinfo
,0); /* DW_AT_frame_base */
3106 saa_write8(pinfo
,0); /* end of entries */
3107 saalen
= pinfo
->datalen
;
3108 infolen
= saalen
+ 4;
3109 infobuf
= pbuf
= nasm_malloc(infolen
);
3110 WRITELONG(pbuf
,saalen
); /* initial length */
3111 saa_rnbytes(pinfo
, pbuf
, saalen
);
3115 /* build rela.info section */
3116 inforellen
= saalen
= pinforel
->datalen
;
3117 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
3118 saa_rnbytes(pinforel
, pbuf
, saalen
);
3121 /* build abbrev section */
3122 pabbrev
= saa_init(1L);
3123 saa_write8(pabbrev
,1); /* entry number LEB128u */
3124 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
3125 saa_write8(pabbrev
,1); /* has children */
3126 /* the following attributes and forms are all LEB128u values */
3127 saa_write8(pabbrev
,DW_AT_low_pc
);
3128 saa_write8(pabbrev
,DW_FORM_addr
);
3129 saa_write8(pabbrev
,DW_AT_high_pc
);
3130 saa_write8(pabbrev
,DW_FORM_addr
);
3131 saa_write8(pabbrev
,DW_AT_stmt_list
);
3132 saa_write8(pabbrev
,DW_FORM_data4
);
3133 saa_write8(pabbrev
,DW_AT_name
);
3134 saa_write8(pabbrev
,DW_FORM_string
);
3135 saa_write8(pabbrev
,DW_AT_producer
);
3136 saa_write8(pabbrev
,DW_FORM_string
);
3137 saa_write8(pabbrev
,DW_AT_language
);
3138 saa_write8(pabbrev
,DW_FORM_data2
);
3139 saa_write16(pabbrev
,0); /* end of entry */
3140 /* LEB128u usage same as above */
3141 saa_write8(pabbrev
,2); /* entry number */
3142 saa_write8(pabbrev
,DW_TAG_subprogram
);
3143 saa_write8(pabbrev
,0); /* no children */
3144 saa_write8(pabbrev
,DW_AT_low_pc
);
3145 saa_write8(pabbrev
,DW_FORM_addr
);
3146 saa_write8(pabbrev
,DW_AT_frame_base
);
3147 saa_write8(pabbrev
,DW_FORM_data4
);
3148 saa_write16(pabbrev
,0); /* end of entry */
3149 /* Terminal zero entry */
3150 saa_write8(pabbrev
,0);
3151 abbrevlen
= saalen
= pabbrev
->datalen
;
3152 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
3153 saa_rnbytes(pabbrev
, pbuf
, saalen
);
3156 /* build line section */
3158 plines
= saa_init(1L);
3159 saa_write8(plines
,1); /* Minimum Instruction Length */
3160 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
3161 saa_write8(plines
,line_base
); /* Line Base */
3162 saa_write8(plines
,line_range
); /* Line Range */
3163 saa_write8(plines
,opcode_base
); /* Opcode Base */
3164 /* standard opcode lengths (# of LEB128u operands) */
3165 saa_write8(plines
,0); /* Std opcode 1 length */
3166 saa_write8(plines
,1); /* Std opcode 2 length */
3167 saa_write8(plines
,1); /* Std opcode 3 length */
3168 saa_write8(plines
,1); /* Std opcode 4 length */
3169 saa_write8(plines
,1); /* Std opcode 5 length */
3170 saa_write8(plines
,0); /* Std opcode 6 length */
3171 saa_write8(plines
,0); /* Std opcode 7 length */
3172 saa_write8(plines
,0); /* Std opcode 8 length */
3173 saa_write8(plines
,1); /* Std opcode 9 length */
3174 saa_write8(plines
,0); /* Std opcode 10 length */
3175 saa_write8(plines
,0); /* Std opcode 11 length */
3176 saa_write8(plines
,1); /* Std opcode 12 length */
3177 /* Directory Table */
3178 saa_write8(plines
,0); /* End of table */
3179 /* File Name Table */
3180 ftentry
= dwarf_flist
;
3181 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
3182 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
3183 saa_write8(plines
,0); /* directory LEB128u */
3184 saa_write8(plines
,0); /* time LEB128u */
3185 saa_write8(plines
,0); /* size LEB128u */
3186 ftentry
= ftentry
->next
;
3188 saa_write8(plines
,0); /* End of table */
3189 linepoff
= plines
->datalen
;
3190 linelen
= linepoff
+ totlen
+ 10;
3191 linebuf
= pbuf
= nasm_malloc(linelen
);
3192 WRITELONG(pbuf
,linelen
-4); /* initial length */
3193 WRITESHORT(pbuf
,3); /* dwarf version */
3194 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
3195 /* write line header */
3197 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
3200 /* concatonate line program ranges */
3202 plinesrel
= saa_init(1L);
3203 psect
= dwarf_fsect
;
3205 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3206 saa_write32(plinesrel
, linepoff
);
3207 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
3208 saa_write32(plinesrel
, (uint32_t) 0);
3209 plinep
= psect
->psaa
;
3210 saalen
= plinep
->datalen
;
3211 saa_rnbytes(plinep
, pbuf
, saalen
);
3215 /* done with this entry */
3216 psect
= psect
->next
;
3218 } else if (is_elfx32()) {
3219 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3220 saa_write32(plinesrel
, linepoff
);
3221 saa_write32(plinesrel
, ((psect
->section
+ 2) << 8) + R_X86_64_32
);
3222 saa_write32(plinesrel
, 0);
3223 plinep
= psect
->psaa
;
3224 saalen
= plinep
->datalen
;
3225 saa_rnbytes(plinep
, pbuf
, saalen
);
3229 /* done with this entry */
3230 psect
= psect
->next
;
3233 nasm_assert(is_elf64());
3234 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
3235 saa_write64(plinesrel
, linepoff
);
3236 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
3237 saa_write64(plinesrel
, (uint64_t) 0);
3238 plinep
= psect
->psaa
;
3239 saalen
= plinep
->datalen
;
3240 saa_rnbytes(plinep
, pbuf
, saalen
);
3244 /* done with this entry */
3245 psect
= psect
->next
;
3249 /* build rela.lines section */
3250 linerellen
=saalen
= plinesrel
->datalen
;
3251 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
3252 saa_rnbytes(plinesrel
, pbuf
, saalen
);
3253 saa_free(plinesrel
);
3255 /* build frame section */
3257 framebuf
= pbuf
= nasm_malloc(framelen
);
3258 WRITELONG(pbuf
,framelen
-4); /* initial length */
3260 /* build loc section */
3262 locbuf
= pbuf
= nasm_malloc(loclen
);
3264 WRITELONG(pbuf
,0); /* null beginning offset */
3265 WRITELONG(pbuf
,0); /* null ending offset */
3266 } else if (is_elfx32()) {
3267 WRITELONG(pbuf
,0); /* null beginning offset */
3268 WRITELONG(pbuf
,0); /* null ending offset */
3270 nasm_assert(is_elf64());
3271 WRITEDLONG(pbuf
,0); /* null beginning offset */
3272 WRITEDLONG(pbuf
,0); /* null ending offset */
3276 static void dwarf_cleanup(void)
3278 nasm_free(arangesbuf
);
3279 nasm_free(arangesrelbuf
);
3280 nasm_free(pubnamesbuf
);
3282 nasm_free(inforelbuf
);
3283 nasm_free(abbrevbuf
);
3285 nasm_free(linerelbuf
);
3286 nasm_free(framebuf
);
3290 static void dwarf_findfile(const char * fname
)
3293 struct linelist
*match
;
3295 /* return if fname is current file name */
3296 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
3299 /* search for match */
3302 match
= dwarf_flist
;
3303 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
3304 if (!(strcmp(fname
, match
->filename
))) {
3305 dwarf_clist
= match
;
3308 match
= match
->next
;
3312 /* add file name to end of list */
3313 dwarf_clist
= nasm_malloc(sizeof(struct linelist
));
3315 dwarf_clist
->line
= dwarf_numfiles
;
3316 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
3317 strcpy(dwarf_clist
->filename
,fname
);
3318 dwarf_clist
->next
= 0;
3319 if (!dwarf_flist
) { /* if first entry */
3320 dwarf_flist
= dwarf_elist
= dwarf_clist
;
3321 dwarf_clist
->last
= 0;
3322 } else { /* chain to previous entry */
3323 dwarf_elist
->next
= dwarf_clist
;
3324 dwarf_elist
= dwarf_clist
;
3328 static void dwarf_findsect(const int index
)
3331 struct sectlist
*match
;
3334 /* return if index is current section index */
3335 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
3338 /* search for match */
3341 match
= dwarf_fsect
;
3342 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
3343 if (match
->section
== index
) {
3344 dwarf_csect
= match
;
3347 match
= match
->next
;
3351 /* add entry to end of list */
3352 dwarf_csect
= nasm_malloc(sizeof(struct sectlist
));
3354 dwarf_csect
->psaa
= plinep
= saa_init(1L);
3355 dwarf_csect
->line
= 1;
3356 dwarf_csect
->offset
= 0;
3357 dwarf_csect
->file
= 1;
3358 dwarf_csect
->section
= index
;
3359 dwarf_csect
->next
= 0;
3360 /* set relocatable address at start of line program */
3361 saa_write8(plinep
,DW_LNS_extended_op
);
3362 saa_write8(plinep
,is_elf64() ? 9 : 5); /* operand length */
3363 saa_write8(plinep
,DW_LNE_set_address
);
3365 saa_write64(plinep
,0); /* Start Address */
3367 saa_write32(plinep
,0); /* Start Address */
3369 if (!dwarf_fsect
) { /* if first entry */
3370 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
3371 dwarf_csect
->last
= 0;
3372 } else { /* chain to previous entry */
3373 dwarf_esect
->next
= dwarf_csect
;
3374 dwarf_esect
= dwarf_csect
;
3378 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */