1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 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 * outelf32.c output routines for the Netwide Assembler to produce
36 * ELF32 (i386 of course) object file format
54 #include "output/outform.h"
55 #include "output/outlib.h"
58 #include "output/dwarf.h"
59 #include "output/stabs.h"
60 #include "output/outelf.h"
69 int32_t address
; /* relative to _start_ of section */
70 int32_t symbol
; /* symbol index */
71 int type
; /* type of relocation */
75 struct rbtree symv
; /* symbol value and symbol rbtree */
76 int32_t strpos
; /* string table position of name */
77 int32_t section
; /* section ID of the symbol */
78 int type
; /* symbol type */
79 int other
; /* symbol visibility */
80 int32_t size
; /* size of symbol */
81 int32_t globnum
; /* symbol table offset if global */
82 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
83 char *name
; /* used temporarily if in above list */
88 uint32_t len
, size
, nrelocs
;
90 int type
; /* SHT_PROGBITS or SHT_NOBITS */
91 uint32_t align
; /* alignment: power of two */
92 uint32_t flags
; /* section flags */
96 struct Reloc
*head
, **tail
;
97 struct rbtree
*gsyms
; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section
**sects
;
102 static int nsects
, sectlen
;
104 #define SHSTR_DELTA 256
105 static char *shstrtab
;
106 static int shstrtablen
, shstrtabsize
;
108 static struct SAA
*syms
;
109 static uint32_t nlocals
, nglobs
, ndebugs
; /* Symbol counts */
111 static int32_t def_seg
;
113 static struct RAA
*bsym
;
115 static struct SAA
*strs
;
116 static uint32_t strslen
;
118 static struct Symbol
*fwds
;
120 static char elf_module
[FILENAME_MAX
];
122 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
123 static uint8_t elf_abiver
= 0; /* Current ABI version */
125 extern struct ofmt of_elf32
;
127 static struct ELF_SECTDATA
{
132 static int elf_nsect
, nsections
;
133 static int32_t elf_foffs
;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section
*, const uint8_t *,
138 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
140 static void elf_write_sections(void);
141 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
142 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
143 static void add_sectname(char *, char *);
151 int section
; /* section index */
152 char *name
; /* shallow-copied pointer of section name */
156 struct linelist
*next
;
157 struct linelist
*last
;
158 struct symlininfo info
;
169 struct sectlist
*next
;
170 struct sectlist
*last
;
173 /* common debug variables */
174 static int currentline
= 1;
175 static int debug_immcall
= 0;
177 /* stabs debug variables */
178 static struct linelist
*stabslines
= 0;
179 static int numlinestabs
= 0;
180 static char *stabs_filename
= 0;
181 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
182 static int stablen
, stabstrlen
, stabrellen
;
184 /* dwarf debug variables */
185 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
186 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
187 static int dwarf_numfiles
= 0, dwarf_nsections
;
188 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
189 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
190 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
191 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
192 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
193 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
195 static struct dfmt df_dwarf
;
196 static struct dfmt df_stabs
;
197 static struct Symbol
*lastsym
;
199 /* common debugging routines */
200 static void debug32_typevalue(int32_t);
201 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
202 static void debug32_directive(const char *, const char *);
204 /* stabs debugging routines */
205 static void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
206 static void stabs32_output(int, void *);
207 static void stabs32_generate(void);
208 static void stabs32_cleanup(void);
210 /* dwarf debugging routines */
211 static void dwarf32_init(void);
212 static void dwarf32_linenum(const char *filename
, int32_t linenumber
, int32_t);
213 static void dwarf32_output(int, void *);
214 static void dwarf32_generate(void);
215 static void dwarf32_cleanup(void);
216 static void dwarf32_findfile(const char *);
217 static void dwarf32_findsect(const int);
220 * Special NASM section numbers which are used to define ELF special
221 * symbols, which can be used with WRT to provide PIC and TLS
224 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
225 static int32_t elf_got_sect
, elf_plt_sect
;
226 static int32_t elf_sym_sect
, elf_tlsie_sect
;
228 static void elf_init(void)
231 nsects
= sectlen
= 0;
232 syms
= saa_init((int32_t)sizeof(struct Symbol
));
233 nlocals
= nglobs
= ndebugs
= 0;
236 saa_wbytes(strs
, "\0", 1L);
237 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
238 strslen
= 2 + strlen(elf_module
);
240 shstrtablen
= shstrtabsize
= 0;;
241 add_sectname("", "");
245 elf_gotpc_sect
= seg_alloc();
246 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
247 elf_gotoff_sect
= seg_alloc();
248 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
249 elf_got_sect
= seg_alloc();
250 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
251 elf_plt_sect
= seg_alloc();
252 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
253 elf_sym_sect
= seg_alloc();
254 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
255 elf_tlsie_sect
= seg_alloc();
256 define_label("..tlsie", elf_tlsie_sect
+ 1, 0L, NULL
, false, false);
258 def_seg
= seg_alloc();
261 static void elf_cleanup(int debuginfo
)
269 for (i
= 0; i
< nsects
; i
++) {
270 if (sects
[i
]->type
!= SHT_NOBITS
)
271 saa_free(sects
[i
]->data
);
273 saa_free(sects
[i
]->rel
);
274 while (sects
[i
]->head
) {
276 sects
[i
]->head
= sects
[i
]->head
->next
;
284 if (of_elf32
.current_dfmt
) {
285 of_elf32
.current_dfmt
->cleanup();
289 static void add_sectname(char *firsthalf
, char *secondhalf
)
291 int len
= strlen(firsthalf
) + strlen(secondhalf
);
292 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
293 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
294 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
295 strcat(shstrtab
+ shstrtablen
, secondhalf
);
296 shstrtablen
+= len
+ 1;
299 static int elf_make_section(char *name
, int type
, int flags
, int align
)
303 s
= nasm_zalloc(sizeof(*s
));
305 if (type
!= SHT_NOBITS
)
306 s
->data
= saa_init(1L);
308 if (!strcmp(name
, ".text"))
311 s
->index
= seg_alloc();
312 add_sectname("", name
);
314 s
->name
= nasm_strdup(name
);
319 if (nsects
>= sectlen
)
320 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
326 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
329 uint32_t flags
, flags_and
, flags_or
;
334 * Default is 32 bits.
341 p
= nasm_skip_word(name
);
344 flags_and
= flags_or
= type
= align
= 0;
346 section_attrib(name
, p
, pass
, &flags_and
,
347 &flags_or
, &align
, &type
);
349 if (!strcmp(name
, ".shstrtab") ||
350 !strcmp(name
, ".symtab") ||
351 !strcmp(name
, ".strtab")) {
352 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
357 for (i
= 0; i
< nsects
; i
++)
358 if (!strcmp(name
, sects
[i
]->name
))
361 const struct elf_known_section
*ks
= elf_known_sections
;
364 if (!strcmp(name
, ks
->name
))
369 type
= type
? type
: ks
->type
;
370 align
= align
? align
: ks
->align
;
371 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
373 i
= elf_make_section(name
, type
, flags
, align
);
374 } else if (pass
== 1) {
375 if ((type
&& sects
[i
]->type
!= type
)
376 || (align
&& sects
[i
]->align
!= align
)
377 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
378 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
379 " redeclaration of section `%s'", name
);
382 return sects
[i
]->index
;
385 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
386 int is_global
, char *special
)
390 bool special_used
= false;
392 #if defined(DEBUG) && DEBUG>2
393 nasm_error(ERR_DEBUG
,
394 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
395 name
, segment
, offset
, is_global
, special
);
397 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
399 * This is a NASM special symbol. We never allow it into
400 * the ELF symbol table, even if it's a valid one. If it
401 * _isn't_ a valid one, we should barf immediately.
403 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
404 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
405 strcmp(name
, "..sym") && strcmp(name
, "..tlsie"))
406 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
410 if (is_global
== 3) {
413 * Fix up a forward-reference symbol size from the first
416 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
417 if (!strcmp((*s
)->name
, name
)) {
418 struct tokenval tokval
;
420 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
424 tokval
.t_type
= TOKEN_INVALID
;
425 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
428 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
429 " expression as symbol size");
431 (*s
)->size
= reloc_value(e
);
435 * Remove it from the list of unresolved sizes.
437 nasm_free((*s
)->name
);
441 return; /* it wasn't an important one */
444 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
445 strslen
+= 1 + strlen(name
);
447 lastsym
= sym
= saa_wstruct(syms
);
449 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
452 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
453 sym
->other
= STV_DEFAULT
;
455 if (segment
== NO_SEG
)
456 sym
->section
= SHN_ABS
;
459 sym
->section
= SHN_UNDEF
;
460 if (segment
== def_seg
) {
461 /* we have to be sure at least text section is there */
463 if (segment
!= elf_section_names(".text", 2, &tempint
))
464 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
466 for (i
= 0; i
< nsects
; i
++) {
467 if (segment
== sects
[i
]->index
) {
468 sym
->section
= i
+ 1;
474 if (is_global
== 2) {
477 sym
->section
= SHN_COMMON
;
479 * We have a common variable. Check the special text to see
480 * if it's a valid number and power of two; if so, store it
481 * as the alignment for the common variable.
485 sym
->symv
.key
= readnum(special
, &err
);
487 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
488 " valid number", special
);
489 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
490 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
491 " power of two", special
);
495 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
497 if (sym
->type
== SYM_GLOBAL
) {
499 * If sym->section == SHN_ABS, then the first line of the
500 * else section would cause a core dump, because its a reference
501 * beyond the end of the section array.
502 * This behaviour is exhibited by this code:
505 * To avoid such a crash, such requests are silently discarded.
506 * This may not be the best solution.
508 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
509 bsym
= raa_write(bsym
, segment
, nglobs
);
510 } else if (sym
->section
!= SHN_ABS
) {
512 * This is a global symbol; so we must add it to the rbtree
513 * of global symbols in its section.
515 * In addition, we check the special text for symbol
516 * type and size information.
518 sects
[sym
->section
-1]->gsyms
=
519 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
522 int n
= strcspn(special
, " \t");
524 if (!nasm_strnicmp(special
, "function", n
))
525 sym
->type
|= STT_FUNC
;
526 else if (!nasm_strnicmp(special
, "data", n
) ||
527 !nasm_strnicmp(special
, "object", n
))
528 sym
->type
|= STT_OBJECT
;
529 else if (!nasm_strnicmp(special
, "notype", n
))
530 sym
->type
|= STT_NOTYPE
;
532 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
536 special
= nasm_skip_spaces(special
);
538 n
= strcspn(special
, " \t");
539 if (!nasm_strnicmp(special
, "default", n
))
540 sym
->other
= STV_DEFAULT
;
541 else if (!nasm_strnicmp(special
, "internal", n
))
542 sym
->other
= STV_INTERNAL
;
543 else if (!nasm_strnicmp(special
, "hidden", n
))
544 sym
->other
= STV_HIDDEN
;
545 else if (!nasm_strnicmp(special
, "protected", n
))
546 sym
->other
= STV_PROTECTED
;
553 struct tokenval tokval
;
556 char *saveme
= stdscan_get();
558 while (special
[n
] && nasm_isspace(special
[n
]))
561 * We have a size expression; attempt to
565 stdscan_set(special
+ n
);
566 tokval
.t_type
= TOKEN_INVALID
;
567 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
572 sym
->name
= nasm_strdup(name
);
575 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
576 " expression as symbol size");
578 sym
->size
= reloc_value(e
);
585 * If TLS segment, mark symbol accordingly.
587 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
589 sym
->type
|= STT_TLS
;
592 sym
->globnum
= nglobs
;
597 if (special
&& !special_used
)
598 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
601 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
605 r
= *sect
->tail
= nasm_zalloc(sizeof(struct Reloc
));
606 sect
->tail
= &r
->next
;
608 r
->address
= sect
->len
;
609 if (segment
!= NO_SEG
) {
611 for (i
= 0; i
< nsects
; i
++)
612 if (segment
== sects
[i
]->index
)
615 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
623 * This routine deals with ..got and ..sym relocations: the more
624 * complicated kinds. In shared-library writing, some relocations
625 * with respect to global symbols must refer to the precise symbol
626 * rather than referring to an offset from the base of the section
627 * _containing_ the symbol. Such relocations call to this routine,
628 * which searches the symbol list for the symbol in question.
630 * R_386_GOT32 references require the _exact_ symbol address to be
631 * used; R_386_32 references can be at an offset from the symbol.
632 * The boolean argument `exact' tells us this.
634 * Return value is the adjusted value of `addr', having become an
635 * offset from the symbol rather than the section. Should always be
636 * zero when returning from an exact call.
638 * Limitation: if you define two symbols at the same place,
639 * confusion will occur.
641 * Inefficiency: we search, currently, using a linked list which
642 * isn't even necessarily sorted.
644 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
645 int32_t segment
, uint32_t offset
,
646 int type
, bool exact
)
655 * First look up the segment/offset pair and find a global
656 * symbol corresponding to it. If it's not one of our segments,
657 * then it must be an external symbol, in which case we're fine
658 * doing a normal elf_add_reloc after first sanity-checking
659 * that the offset from the symbol is zero.
662 for (i
= 0; i
< nsects
; i
++)
663 if (segment
== sects
[i
]->index
) {
670 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
671 " for this reference");
673 elf_add_reloc(sect
, segment
, type
);
677 srb
= rb_search(s
->gsyms
, offset
);
678 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
679 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
680 " for this reference");
683 sym
= container_of(srb
, struct Symbol
, symv
);
685 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
686 sect
->tail
= &r
->next
;
689 r
->address
= sect
->len
;
690 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
695 return offset
- sym
->symv
.key
;
698 static void elf_out(int32_t segto
, const void *data
,
699 enum out_type type
, uint64_t size
,
700 int32_t segment
, int32_t wrt
)
704 uint8_t mydata
[8], *p
;
707 static struct symlininfo sinfo
;
710 * handle absolute-assembly (structure definitions)
712 if (segto
== NO_SEG
) {
713 if (type
!= OUT_RESERVE
)
714 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
720 for (i
= 0; i
< nsects
; i
++)
721 if (segto
== sects
[i
]->index
) {
726 int tempint
; /* ignored */
727 if (segto
!= elf_section_names(".text", 2, &tempint
))
728 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
730 s
= sects
[nsects
- 1];
735 /* again some stabs debugging stuff */
736 if (of_elf32
.current_dfmt
) {
737 sinfo
.offset
= s
->len
;
739 sinfo
.name
= s
->name
;
740 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
742 /* end of debugging stuff */
744 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
745 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
746 " BSS section `%s': ignored", s
->name
);
747 s
->len
+= realsize(type
, size
);
753 if (s
->type
== SHT_PROGBITS
) {
754 nasm_error(ERR_WARNING
, "uninitialized space declared in"
755 " non-BSS section `%s': zeroing", s
->name
);
756 elf_sect_write(s
, NULL
, size
);
762 if (segment
!= NO_SEG
)
763 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
764 elf_sect_write(s
, data
, size
);
770 addr
= *(int64_t *)data
;
771 if (segment
!= NO_SEG
) {
773 nasm_error(ERR_NONFATAL
, "ELF format does not support"
774 " segment base references");
778 * The if() is a hack to deal with compilers which
779 * don't handle switch() statements with 64-bit
782 if (size
< UINT_MAX
) {
783 switch ((unsigned int)size
) {
786 elf_add_reloc(s
, segment
, R_386_8
);
790 elf_add_reloc(s
, segment
, R_386_16
);
793 elf_add_reloc(s
, segment
, R_386_32
);
795 default: /* Error issued further down */
799 } else if (wrt
== elf_gotpc_sect
+ 1) {
801 * The user will supply GOT relative to $$. ELF
802 * will let us have GOT relative to $. So we
803 * need to fix up the data item by $-$$.
806 elf_add_reloc(s
, segment
, R_386_GOTPC
);
807 } else if (wrt
== elf_gotoff_sect
+ 1) {
808 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
809 } else if (wrt
== elf_tlsie_sect
+ 1) {
810 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
812 } else if (wrt
== elf_got_sect
+ 1) {
813 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
815 } else if (wrt
== elf_sym_sect
+ 1) {
818 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
821 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
824 } else if (wrt
== elf_plt_sect
+ 1) {
825 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
826 "relative PLT references");
828 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
830 wrt
= NO_SEG
; /* we can at least _try_ to continue */
836 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
837 "8- or 16-bit relocations in ELF32 is a GNU extension");
838 } else if (size
!= 4 && segment
!= NO_SEG
) {
839 nasm_error(ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
841 WRITEADDR(p
, addr
, size
);
842 elf_sect_write(s
, mydata
, size
);
852 reltype
= R_386_PC16
;
856 nasm_assert(segment
!= segto
);
857 if (segment
!= NO_SEG
&& segment
% 2) {
858 nasm_error(ERR_NONFATAL
, "ELF format does not support"
859 " segment base references");
862 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
863 "8- or 16-bit relocations in ELF is a GNU extension");
864 elf_add_reloc(s
, segment
, reltype
);
866 nasm_error(ERR_NONFATAL
,
867 "Unsupported non-32-bit ELF relocation");
871 WRITESHORT(p
, *(int64_t *)data
- size
);
872 elf_sect_write(s
, mydata
, bytes
);
876 if (segment
== segto
)
877 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
878 if (segment
!= NO_SEG
&& segment
% 2) {
879 nasm_error(ERR_NONFATAL
, "ELF format does not support"
880 " segment base references");
883 elf_add_reloc(s
, segment
, R_386_PC32
);
884 } else if (wrt
== elf_plt_sect
+ 1) {
885 elf_add_reloc(s
, segment
, R_386_PLT32
);
886 } else if (wrt
== elf_gotpc_sect
+ 1 ||
887 wrt
== elf_gotoff_sect
+ 1 ||
888 wrt
== elf_got_sect
+ 1) {
889 nasm_error(ERR_NONFATAL
, "ELF format cannot produce PC-"
890 "relative GOT references");
892 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
894 wrt
= NO_SEG
; /* we can at least _try_ to continue */
898 WRITELONG(p
, *(int64_t *)data
- size
);
899 elf_sect_write(s
, mydata
, 4L);
903 nasm_error(ERR_NONFATAL
,
904 "32-bit ELF format does not support 64-bit relocations");
907 elf_sect_write(s
, mydata
, 8L);
912 static void elf_write(void)
919 int32_t symtablen
, symtablocal
;
922 * Work out how many sections we will have. We have SHN_UNDEF,
923 * then the flexible user sections, then the fixed sections
924 * `.shstrtab', `.symtab' and `.strtab', then optionally
925 * relocation sections for the user sections.
927 nsections
= sec_numspecial
+ 1;
928 if (of_elf32
.current_dfmt
== &df_stabs
)
930 else if (of_elf32
.current_dfmt
== &df_dwarf
)
933 add_sectname("", ".shstrtab");
934 add_sectname("", ".symtab");
935 add_sectname("", ".strtab");
936 for (i
= 0; i
< nsects
; i
++) {
937 nsections
++; /* for the section itself */
938 if (sects
[i
]->head
) {
939 nsections
++; /* for its relocations */
940 add_sectname(".rel", sects
[i
]->name
);
944 if (of_elf32
.current_dfmt
== &df_stabs
) {
945 /* in case the debug information is wanted, just add these three sections... */
946 add_sectname("", ".stab");
947 add_sectname("", ".stabstr");
948 add_sectname(".rel", ".stab");
949 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
950 /* the dwarf debug standard specifies the following ten sections,
951 not all of which are currently implemented,
952 although all of them are defined. */
953 add_sectname("", ".debug_aranges");
954 add_sectname(".rela", ".debug_aranges");
955 add_sectname("", ".debug_pubnames");
956 add_sectname("", ".debug_info");
957 add_sectname(".rela", ".debug_info");
958 add_sectname("", ".debug_abbrev");
959 add_sectname("", ".debug_line");
960 add_sectname(".rela", ".debug_line");
961 add_sectname("", ".debug_frame");
962 add_sectname("", ".debug_loc");
966 * Output the ELF header.
968 fwrite("\177ELF\1\1\1", 7, 1, ofile
);
969 fputc(elf_osabi
, ofile
);
970 fputc(elf_abiver
, ofile
);
971 fwritezero(7, ofile
);
972 fwriteint16_t(1, ofile
); /* ET_REL relocatable file */
973 fwriteint16_t(3, ofile
); /* EM_386 processor ID */
974 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
975 fwriteint32_t(0L, ofile
); /* no entry point */
976 fwriteint32_t(0L, ofile
); /* no program header table */
977 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
978 * ELF header plus alignment */
979 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
980 fwriteint16_t(0x34, ofile
); /* size of ELF header */
981 fwriteint16_t(0, ofile
); /* no program header table, again */
982 fwriteint16_t(0, ofile
); /* still no program header table */
983 fwriteint16_t(0x28, ofile
); /* size of section header */
984 fwriteint16_t(nsections
, ofile
); /* number of sections */
985 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
986 * section header table */
987 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
988 fwriteint32_t(0L, ofile
);
989 fwriteint32_t(0L, ofile
);
992 * Build the symbol table and relocation tables.
994 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
995 for (i
= 0; i
< nsects
; i
++)
997 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1001 * Now output the section header table.
1004 elf_foffs
= 0x40 + 0x28 * nsections
;
1005 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1008 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1011 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1014 /* The normal sections */
1015 for (i
= 0; i
< nsects
; i
++) {
1016 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1017 (sects
[i
]->type
== SHT_PROGBITS
?
1018 sects
[i
]->data
: NULL
), true,
1019 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1024 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1025 shstrtablen
, 0, 0, 1, 0);
1029 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1030 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1034 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1035 strslen
, 0, 0, 1, 0);
1038 /* The relocation sections */
1039 for (i
= 0; i
< nsects
; i
++)
1040 if (sects
[i
]->head
) {
1041 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1042 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1046 if (of_elf32
.current_dfmt
== &df_stabs
) {
1047 /* for debugging information, create the last three sections
1048 which are the .stab , .stabstr and .rel.stab sections respectively */
1050 /* this function call creates the stab sections in memory */
1053 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1054 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1055 stablen
, sec_stabstr
, 0, 4, 12);
1058 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1059 stabstrlen
, 0, 0, 4, 0);
1062 /* link -> symtable info -> section to refer to */
1063 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1064 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1067 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1068 /* for dwarf debugging information, create the ten dwarf sections */
1070 /* this function call creates the dwarf sections in memory */
1074 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1075 arangeslen
, 0, 0, 1, 0);
1078 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1079 arangesrellen
, sec_symtab
, sec_debug_aranges
,
1083 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1084 false, pubnameslen
, 0, 0, 1, 0);
1087 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1088 infolen
, 0, 0, 1, 0);
1091 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1092 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1095 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1096 abbrevlen
, 0, 0, 1, 0);
1099 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1100 linelen
, 0, 0, 1, 0);
1103 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1104 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1107 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1108 framelen
, 0, 0, 8, 0);
1111 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1112 loclen
, 0, 0, 1, 0);
1115 fwritezero(align
, ofile
);
1118 * Now output the sections.
1120 elf_write_sections();
1122 nasm_free(elf_sects
);
1126 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1128 struct SAA
*s
= saa_init(1L);
1130 uint8_t entry
[16], *p
;
1136 * First, an all-zeros entry, required by the ELF spec.
1138 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1143 * Next, an entry for the file name.
1146 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1147 WRITELONG(p
, 0); /* no value */
1148 WRITELONG(p
, 0); /* no size either */
1149 WRITESHORT(p
, STT_FILE
); /* type FILE */
1150 WRITESHORT(p
, SHN_ABS
);
1151 saa_wbytes(s
, entry
, 16L);
1156 * Now some standard symbols defining the segments, for relocation
1159 for (i
= 1; i
<= nsects
; i
++) {
1161 WRITELONG(p
, 0); /* no symbol name */
1162 WRITELONG(p
, 0); /* offset zero */
1163 WRITELONG(p
, 0); /* size zero */
1164 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1165 WRITESHORT(p
, i
); /* section id */
1166 saa_wbytes(s
, entry
, 16L);
1172 * Now the other local symbols.
1175 while ((sym
= saa_rstruct(syms
))) {
1176 if (sym
->type
& SYM_GLOBAL
)
1179 WRITELONG(p
, sym
->strpos
);
1180 WRITELONG(p
, sym
->symv
.key
);
1181 WRITELONG(p
, sym
->size
);
1182 WRITECHAR(p
, sym
->type
); /* type and binding */
1183 WRITECHAR(p
, sym
->other
); /* visibility */
1184 WRITESHORT(p
, sym
->section
);
1185 saa_wbytes(s
, entry
, 16L);
1190 * dwarf needs symbols for debug sections
1191 * which are relocation targets.
1193 //*** fix for 32 bit
1194 if (of_elf32
.current_dfmt
== &df_dwarf
) {
1195 dwarf_infosym
= *local
;
1197 WRITELONG(p
, 0); /* no symbol name */
1198 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1199 WRITELONG(p
, (uint32_t) 0); /* size zero */
1200 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1201 WRITESHORT(p
, sec_debug_info
); /* section id */
1202 saa_wbytes(s
, entry
, 16L);
1205 dwarf_abbrevsym
= *local
;
1207 WRITELONG(p
, 0); /* no symbol name */
1208 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1209 WRITELONG(p
, (uint32_t) 0); /* size zero */
1210 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1211 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1212 saa_wbytes(s
, entry
, 16L);
1215 dwarf_linesym
= *local
;
1217 WRITELONG(p
, 0); /* no symbol name */
1218 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1219 WRITELONG(p
, (uint32_t) 0); /* size zero */
1220 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1221 WRITESHORT(p
, sec_debug_line
); /* section id */
1222 saa_wbytes(s
, entry
, 16L);
1228 * Now the global symbols.
1231 while ((sym
= saa_rstruct(syms
))) {
1232 if (!(sym
->type
& SYM_GLOBAL
))
1235 WRITELONG(p
, sym
->strpos
);
1236 WRITELONG(p
, sym
->symv
.key
);
1237 WRITELONG(p
, sym
->size
);
1238 WRITECHAR(p
, sym
->type
); /* type and binding */
1239 WRITECHAR(p
, sym
->other
); /* visibility */
1240 WRITESHORT(p
, sym
->section
);
1241 saa_wbytes(s
, entry
, 16L);
1248 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1251 uint8_t *p
, entry
[8];
1252 int32_t global_offset
;
1261 * How to onvert from a global placeholder to a real symbol index;
1262 * the +2 refers to the two special entries, the null entry and
1263 * the filename entry.
1265 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1268 int32_t sym
= r
->symbol
;
1270 if (sym
>= GLOBAL_TEMP_BASE
)
1271 sym
+= global_offset
;
1274 WRITELONG(p
, r
->address
);
1275 WRITELONG(p
, (sym
<< 8) + r
->type
);
1276 saa_wbytes(s
, entry
, 8L);
1285 static void elf_section_header(int name
, int type
, int flags
,
1286 void *data
, bool is_saa
, int32_t datalen
,
1287 int link
, int info
, int align
, int eltsize
)
1289 elf_sects
[elf_nsect
].data
= data
;
1290 elf_sects
[elf_nsect
].len
= datalen
;
1291 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1294 fwriteint32_t((int32_t)name
, ofile
);
1295 fwriteint32_t((int32_t)type
, ofile
);
1296 fwriteint32_t((int32_t)flags
, ofile
);
1297 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1298 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1299 fwriteint32_t(datalen
, ofile
);
1301 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1302 fwriteint32_t((int32_t)link
, ofile
);
1303 fwriteint32_t((int32_t)info
, ofile
);
1304 fwriteint32_t((int32_t)align
, ofile
);
1305 fwriteint32_t((int32_t)eltsize
, ofile
);
1308 static void elf_write_sections(void)
1311 for (i
= 0; i
< elf_nsect
; i
++)
1312 if (elf_sects
[i
].data
) {
1313 int32_t len
= elf_sects
[i
].len
;
1314 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1315 int32_t align
= reallen
- len
;
1316 if (elf_sects
[i
].is_saa
)
1317 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1319 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1320 fwritezero(align
, ofile
);
1324 static void elf_sect_write(struct Section
*sect
,
1325 const uint8_t *data
, uint32_t len
)
1327 saa_wbytes(sect
->data
, data
, len
);
1331 static void elf_sectalign(int32_t seg
, unsigned int value
)
1333 struct Section
*s
= NULL
;
1336 for (i
= 0; i
< nsects
; i
++) {
1337 if (sects
[i
]->index
== seg
) {
1342 if (!s
|| !is_power2(value
))
1345 if (value
> s
->align
)
1349 static int32_t elf_segbase(int32_t segment
)
1354 static int elf_directive(enum directives directive
, char *value
, int pass
)
1360 switch (directive
) {
1363 return 1; /* ignore in pass 2 */
1365 n
= readnum(value
, &err
);
1367 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1370 if (n
< 0 || n
> 255) {
1371 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1377 if ((p
= strchr(value
,',')) == NULL
)
1380 n
= readnum(p
+1, &err
);
1381 if (err
|| n
< 0 || n
> 255) {
1382 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1394 static void elf_filename(char *inname
, char *outname
)
1396 strcpy(elf_module
, inname
);
1397 standard_extension(inname
, outname
, ".o");
1400 extern macros_t elf_stdmac
[];
1402 static int elf_set_info(enum geninfo type
, char **val
)
1408 static struct dfmt df_dwarf
= {
1409 "ELF32 (i386) dwarf debug format for Linux/Unix",
1419 static struct dfmt df_stabs
= {
1420 "ELF32 (i386) stabs debug format for Linux/Unix",
1431 struct dfmt
*elf32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1433 struct ofmt of_elf32
= {
1434 "ELF32 (i386) object files (e.g. Linux)",
1452 /* again, the stabs debugging stuff (code) */
1454 static void stabs32_linenum(const char *filename
, int32_t linenumber
,
1459 if (!stabs_filename
) {
1460 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1461 strcpy(stabs_filename
, filename
);
1463 if (strcmp(stabs_filename
, filename
)) {
1465 * yep, a memory leak...this program is one-shot anyway, so who cares...
1466 * in fact, this leak comes in quite handy to maintain a list of files
1467 * encountered so far in the symbol lines...
1470 /* why not nasm_free(stabs_filename); we're done with the old one */
1472 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1473 strcpy(stabs_filename
, filename
);
1477 currentline
= linenumber
;
1480 static void debug32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1490 static void debug32_directive(const char *directive
, const char *params
)
1496 static void debug32_typevalue(int32_t type
)
1498 int32_t stype
, ssize
;
1499 switch (TYM_TYPE(type
)) {
1542 stype
= STT_SECTION
;
1557 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1558 lastsym
->size
= ssize
;
1559 lastsym
->type
= stype
;
1563 static void stabs32_output(int type
, void *param
)
1565 struct symlininfo
*s
;
1566 struct linelist
*el
;
1567 if (type
== TY_STABSSYMLIN
) {
1568 if (debug_immcall
) {
1569 s
= (struct symlininfo
*)param
;
1570 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1571 return; /* line info is only collected for executable sections */
1573 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1574 el
->info
.offset
= s
->offset
;
1575 el
->info
.section
= s
->section
;
1576 el
->info
.name
= s
->name
;
1577 el
->line
= currentline
;
1578 el
->filename
= stabs_filename
;
1581 stabslines
->last
->next
= el
;
1582 stabslines
->last
= el
;
1585 stabslines
->last
= el
;
1592 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1594 static void stabs32_generate(void)
1596 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1597 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1601 struct linelist
*ptr
;
1605 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1606 for (i
= 0; i
< numlinestabs
; i
++)
1610 if (numfiles
== 0) {
1611 allfiles
[0] = ptr
->filename
;
1614 for (i
= 0; i
< numfiles
; i
++) {
1615 if (!strcmp(allfiles
[i
], ptr
->filename
))
1618 if (i
>= numfiles
) {
1619 allfiles
[i
] = ptr
->filename
;
1626 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1627 for (i
= 0; i
< numfiles
; i
++) {
1628 fileidx
[i
] = strsize
;
1629 strsize
+= strlen(allfiles
[i
]) + 1;
1632 for (i
= 0; i
< numfiles
; i
++) {
1633 if (!strcmp(allfiles
[i
], elf_module
)) {
1640 * worst case size of the stab buffer would be:
1641 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1642 * plus one "ending" entry
1644 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1645 sizeof(struct stabentry
));
1646 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1647 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1650 for (i
= 0; i
< numfiles
; i
++)
1651 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1654 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1662 * this is the first stab, its strx points to the filename of the
1663 * the source-file, the n_desc field should be set to the number
1664 * of remaining stabs
1666 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1668 /* this is the stab for the main source file */
1669 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1671 /* relocation table entry */
1674 * Since the symbol table has two entries before
1675 * the section symbols, the index in the info.section
1676 * member must be adjusted by adding 2
1679 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1680 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1683 currfile
= mainfileindex
;
1687 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1688 /* oops file has changed... */
1689 for (i
= 0; i
< numfiles
; i
++)
1690 if (!strcmp(allfiles
[i
], ptr
->filename
))
1693 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1697 /* relocation table entry */
1698 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1699 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1702 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1705 /* relocation table entry */
1707 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1708 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1714 /* this is an "ending" token */
1715 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1718 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1720 nasm_free(allfiles
);
1723 stablen
= (sptr
- sbuf
);
1724 stabrellen
= (rptr
- rbuf
);
1730 static void stabs32_cleanup(void)
1732 struct linelist
*ptr
, *del
;
1744 nasm_free(stabrelbuf
);
1745 nasm_free(stabstrbuf
);
1748 /* dwarf routines */
1750 static void dwarf32_init(void)
1752 ndebugs
= 3; /* 3 debug symbols */
1755 static void dwarf32_linenum(const char *filename
, int32_t linenumber
,
1759 dwarf32_findfile(filename
);
1761 currentline
= linenumber
;
1764 /* called from elf_out with type == TY_DEBUGSYMLIN */
1765 static void dwarf32_output(int type
, void *param
)
1767 int ln
, aa
, inx
, maxln
, soc
;
1768 struct symlininfo
*s
;
1773 s
= (struct symlininfo
*)param
;
1775 /* line number info is only gathered for executable sections */
1776 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1779 /* Check if section index has changed */
1780 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1781 dwarf32_findsect(s
->section
);
1783 /* do nothing unless line or file has changed */
1787 ln
= currentline
- dwarf_csect
->line
;
1788 aa
= s
->offset
- dwarf_csect
->offset
;
1789 inx
= dwarf_clist
->line
;
1790 plinep
= dwarf_csect
->psaa
;
1791 /* check for file change */
1792 if (!(inx
== dwarf_csect
->file
)) {
1793 saa_write8(plinep
,DW_LNS_set_file
);
1794 saa_write8(plinep
,inx
);
1795 dwarf_csect
->file
= inx
;
1797 /* check for line change */
1799 /* test if in range of special op code */
1800 maxln
= line_base
+ line_range
;
1801 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1802 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1803 saa_write8(plinep
,soc
);
1805 saa_write8(plinep
,DW_LNS_advance_line
);
1806 saa_wleb128s(plinep
,ln
);
1808 saa_write8(plinep
,DW_LNS_advance_pc
);
1809 saa_wleb128u(plinep
,aa
);
1812 dwarf_csect
->line
= currentline
;
1813 dwarf_csect
->offset
= s
->offset
;
1816 /* show change handled */
1821 static void dwarf32_generate(void)
1825 struct linelist
*ftentry
;
1826 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1827 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1828 struct sectlist
*psect
;
1829 size_t saalen
, linepoff
, totlen
, highaddr
;
1831 /* write epilogues for each line program range */
1832 /* and build aranges section */
1833 paranges
= saa_init(1L);
1834 parangesrel
= saa_init(1L);
1835 saa_write16(paranges
,2); /* dwarf version */
1836 saa_write32(parangesrel
, paranges
->datalen
+4);
1837 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
1838 saa_write32(parangesrel
, 0);
1839 saa_write32(paranges
,0); /* offset into info */
1840 saa_write8(paranges
,4); /* pointer size */
1841 saa_write8(paranges
,0); /* not segmented */
1842 saa_write32(paranges
,0); /* padding */
1843 /* iterate though sectlist entries */
1844 psect
= dwarf_fsect
;
1847 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
1848 plinep
= psect
->psaa
;
1849 /* Line Number Program Epilogue */
1850 saa_write8(plinep
,2); /* std op 2 */
1851 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1852 saa_write8(plinep
,DW_LNS_extended_op
);
1853 saa_write8(plinep
,1); /* operand length */
1854 saa_write8(plinep
,DW_LNE_end_sequence
);
1855 totlen
+= plinep
->datalen
;
1856 /* range table relocation entry */
1857 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1858 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
1859 saa_write32(parangesrel
, (uint32_t) 0);
1860 /* range table entry */
1861 saa_write32(paranges
,0x0000); /* range start */
1862 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1863 highaddr
+= sects
[psect
->section
]->len
;
1864 /* done with this entry */
1865 psect
= psect
->next
;
1867 saa_write32(paranges
,0); /* null address */
1868 saa_write32(paranges
,0); /* null length */
1869 saalen
= paranges
->datalen
;
1870 arangeslen
= saalen
+ 4;
1871 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1872 WRITELONG(pbuf
,saalen
); /* initial length */
1873 saa_rnbytes(paranges
, pbuf
, saalen
);
1876 /* build rela.aranges section */
1877 arangesrellen
= saalen
= parangesrel
->datalen
;
1878 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1879 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1880 saa_free(parangesrel
);
1882 /* build pubnames section */
1883 ppubnames
= saa_init(1L);
1884 saa_write16(ppubnames
,3); /* dwarf version */
1885 saa_write32(ppubnames
,0); /* offset into info */
1886 saa_write32(ppubnames
,0); /* space used in info */
1887 saa_write32(ppubnames
,0); /* end of list */
1888 saalen
= ppubnames
->datalen
;
1889 pubnameslen
= saalen
+ 4;
1890 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1891 WRITELONG(pbuf
,saalen
); /* initial length */
1892 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1893 saa_free(ppubnames
);
1895 /* build info section */
1896 pinfo
= saa_init(1L);
1897 pinforel
= saa_init(1L);
1898 saa_write16(pinfo
,2); /* dwarf version */
1899 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1900 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
1901 saa_write32(pinforel
, 0);
1902 saa_write32(pinfo
,0); /* offset into abbrev */
1903 saa_write8(pinfo
,4); /* pointer size */
1904 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1905 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1906 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1907 saa_write32(pinforel
, 0);
1908 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1909 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1910 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1911 saa_write32(pinforel
, 0);
1912 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1913 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1914 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
1915 saa_write32(pinforel
, 0);
1916 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1917 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1918 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1919 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1920 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1921 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1922 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1923 saa_write32(pinforel
, 0);
1924 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1925 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1926 saa_write8(pinfo
,0); /* end of entries */
1927 saalen
= pinfo
->datalen
;
1928 infolen
= saalen
+ 4;
1929 infobuf
= pbuf
= nasm_malloc(infolen
);
1930 WRITELONG(pbuf
,saalen
); /* initial length */
1931 saa_rnbytes(pinfo
, pbuf
, saalen
);
1934 /* build rela.info section */
1935 inforellen
= saalen
= pinforel
->datalen
;
1936 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1937 saa_rnbytes(pinforel
, pbuf
, saalen
);
1940 /* build abbrev section */
1941 pabbrev
= saa_init(1L);
1942 saa_write8(pabbrev
,1); /* entry number LEB128u */
1943 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1944 saa_write8(pabbrev
,1); /* has children */
1945 /* the following attributes and forms are all LEB128u values */
1946 saa_write8(pabbrev
,DW_AT_low_pc
);
1947 saa_write8(pabbrev
,DW_FORM_addr
);
1948 saa_write8(pabbrev
,DW_AT_high_pc
);
1949 saa_write8(pabbrev
,DW_FORM_addr
);
1950 saa_write8(pabbrev
,DW_AT_stmt_list
);
1951 saa_write8(pabbrev
,DW_FORM_data4
);
1952 saa_write8(pabbrev
,DW_AT_name
);
1953 saa_write8(pabbrev
,DW_FORM_string
);
1954 saa_write8(pabbrev
,DW_AT_producer
);
1955 saa_write8(pabbrev
,DW_FORM_string
);
1956 saa_write8(pabbrev
,DW_AT_language
);
1957 saa_write8(pabbrev
,DW_FORM_data2
);
1958 saa_write16(pabbrev
,0); /* end of entry */
1959 /* LEB128u usage same as above */
1960 saa_write8(pabbrev
,2); /* entry number */
1961 saa_write8(pabbrev
,DW_TAG_subprogram
);
1962 saa_write8(pabbrev
,0); /* no children */
1963 saa_write8(pabbrev
,DW_AT_low_pc
);
1964 saa_write8(pabbrev
,DW_FORM_addr
);
1965 saa_write8(pabbrev
,DW_AT_frame_base
);
1966 saa_write8(pabbrev
,DW_FORM_data4
);
1967 saa_write16(pabbrev
,0); /* end of entry */
1968 abbrevlen
= saalen
= pabbrev
->datalen
;
1969 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
1970 saa_rnbytes(pabbrev
, pbuf
, saalen
);
1973 /* build line section */
1975 plines
= saa_init(1L);
1976 saa_write8(plines
,1); /* Minimum Instruction Length */
1977 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
1978 saa_write8(plines
,line_base
); /* Line Base */
1979 saa_write8(plines
,line_range
); /* Line Range */
1980 saa_write8(plines
,opcode_base
); /* Opcode Base */
1981 /* standard opcode lengths (# of LEB128u operands) */
1982 saa_write8(plines
,0); /* Std opcode 1 length */
1983 saa_write8(plines
,1); /* Std opcode 2 length */
1984 saa_write8(plines
,1); /* Std opcode 3 length */
1985 saa_write8(plines
,1); /* Std opcode 4 length */
1986 saa_write8(plines
,1); /* Std opcode 5 length */
1987 saa_write8(plines
,0); /* Std opcode 6 length */
1988 saa_write8(plines
,0); /* Std opcode 7 length */
1989 saa_write8(plines
,0); /* Std opcode 8 length */
1990 saa_write8(plines
,1); /* Std opcode 9 length */
1991 saa_write8(plines
,0); /* Std opcode 10 length */
1992 saa_write8(plines
,0); /* Std opcode 11 length */
1993 saa_write8(plines
,1); /* Std opcode 12 length */
1994 /* Directory Table */
1995 saa_write8(plines
,0); /* End of table */
1996 /* File Name Table */
1997 ftentry
= dwarf_flist
;
1998 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
1999 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2000 saa_write8(plines
,0); /* directory LEB128u */
2001 saa_write8(plines
,0); /* time LEB128u */
2002 saa_write8(plines
,0); /* size LEB128u */
2003 ftentry
= ftentry
->next
;
2005 saa_write8(plines
,0); /* End of table */
2006 linepoff
= plines
->datalen
;
2007 linelen
= linepoff
+ totlen
+ 10;
2008 linebuf
= pbuf
= nasm_malloc(linelen
);
2009 WRITELONG(pbuf
,linelen
-4); /* initial length */
2010 WRITESHORT(pbuf
,3); /* dwarf version */
2011 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2012 /* write line header */
2014 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2017 /* concatonate line program ranges */
2019 plinesrel
= saa_init(1L);
2020 psect
= dwarf_fsect
;
2021 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2022 saa_write32(plinesrel
, linepoff
);
2023 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2024 saa_write32(plinesrel
, (uint32_t) 0);
2025 plinep
= psect
->psaa
;
2026 saalen
= plinep
->datalen
;
2027 saa_rnbytes(plinep
, pbuf
, saalen
);
2031 /* done with this entry */
2032 psect
= psect
->next
;
2036 /* build rela.lines section */
2037 linerellen
=saalen
= plinesrel
->datalen
;
2038 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2039 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2040 saa_free(plinesrel
);
2042 /* build frame section */
2044 framebuf
= pbuf
= nasm_malloc(framelen
);
2045 WRITELONG(pbuf
,framelen
-4); /* initial length */
2047 /* build loc section */
2049 locbuf
= pbuf
= nasm_malloc(loclen
);
2050 WRITELONG(pbuf
,0); /* null beginning offset */
2051 WRITELONG(pbuf
,0); /* null ending offset */
2054 static void dwarf32_cleanup(void)
2056 nasm_free(arangesbuf
);
2057 nasm_free(arangesrelbuf
);
2058 nasm_free(pubnamesbuf
);
2060 nasm_free(inforelbuf
);
2061 nasm_free(abbrevbuf
);
2063 nasm_free(linerelbuf
);
2064 nasm_free(framebuf
);
2068 static void dwarf32_findfile(const char * fname
)
2071 struct linelist
*match
;
2073 /* return if fname is current file name */
2074 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2077 /* search for match */
2080 match
= dwarf_flist
;
2081 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2082 if (!(strcmp(fname
, match
->filename
))) {
2083 dwarf_clist
= match
;
2089 /* add file name to end of list */
2090 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2092 dwarf_clist
->line
= dwarf_numfiles
;
2093 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2094 strcpy(dwarf_clist
->filename
,fname
);
2095 dwarf_clist
->next
= 0;
2096 if (!dwarf_flist
) { /* if first entry */
2097 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2098 dwarf_clist
->last
= 0;
2099 } else { /* chain to previous entry */
2100 dwarf_elist
->next
= dwarf_clist
;
2101 dwarf_elist
= dwarf_clist
;
2105 static void dwarf32_findsect(const int index
)
2108 struct sectlist
*match
;
2111 /* return if index is current section index */
2112 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2115 /* search for match */
2118 match
= dwarf_fsect
;
2119 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2120 if (match
->section
== index
) {
2121 dwarf_csect
= match
;
2124 match
= match
->next
;
2128 /* add entry to end of list */
2129 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2131 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2132 dwarf_csect
->line
= 1;
2133 dwarf_csect
->offset
= 0;
2134 dwarf_csect
->file
= 1;
2135 dwarf_csect
->section
= index
;
2136 dwarf_csect
->next
= 0;
2137 /* set relocatable address at start of line program */
2138 saa_write8(plinep
,DW_LNS_extended_op
);
2139 saa_write8(plinep
,5); /* operand length */
2140 saa_write8(plinep
,DW_LNE_set_address
);
2141 saa_write32(plinep
,0); /* Start Address */
2143 if (!dwarf_fsect
) { /* if first entry */
2144 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2145 dwarf_csect
->last
= 0;
2146 } else { /* chain to previous entry */
2147 dwarf_esect
->next
= dwarf_csect
;
2148 dwarf_esect
= dwarf_csect
;