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 symlininfo info
;
159 struct linelist
*next
;
160 struct linelist
*last
;
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_malloc(sizeof(*s
));
305 if (type
!= SHT_NOBITS
)
306 s
->data
= saa_init(1L);
309 s
->len
= s
->size
= 0;
311 if (!strcmp(name
, ".text"))
314 s
->index
= seg_alloc();
315 add_sectname("", name
);
316 s
->name
= nasm_malloc(1 + strlen(name
));
317 strcpy(s
->name
, name
);
323 if (nsects
>= sectlen
)
324 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
330 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
333 uint32_t flags
, flags_and
, flags_or
;
338 * Default is 32 bits.
345 p
= nasm_skip_word(name
);
348 flags_and
= flags_or
= type
= align
= 0;
350 section_attrib(name
, p
, pass
, &flags_and
,
351 &flags_or
, &align
, &type
);
353 if (!strcmp(name
, ".shstrtab") ||
354 !strcmp(name
, ".symtab") ||
355 !strcmp(name
, ".strtab")) {
356 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
361 for (i
= 0; i
< nsects
; i
++)
362 if (!strcmp(name
, sects
[i
]->name
))
365 const struct elf_known_section
*ks
= elf_known_sections
;
368 if (!strcmp(name
, ks
->name
))
373 type
= type
? type
: ks
->type
;
374 align
= align
? align
: ks
->align
;
375 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
377 i
= elf_make_section(name
, type
, flags
, align
);
378 } else if (pass
== 1) {
379 if ((type
&& sects
[i
]->type
!= type
)
380 || (align
&& sects
[i
]->align
!= align
)
381 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
382 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
383 " redeclaration of section `%s'", name
);
386 return sects
[i
]->index
;
389 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
390 int is_global
, char *special
)
394 bool special_used
= false;
396 #if defined(DEBUG) && DEBUG>2
397 nasm_error(ERR_DEBUG
,
398 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
399 name
, segment
, offset
, is_global
, special
);
401 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
403 * This is a NASM special symbol. We never allow it into
404 * the ELF symbol table, even if it's a valid one. If it
405 * _isn't_ a valid one, we should barf immediately.
407 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
408 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
409 strcmp(name
, "..sym") && strcmp(name
, "..tlsie"))
410 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
414 if (is_global
== 3) {
417 * Fix up a forward-reference symbol size from the first
420 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
421 if (!strcmp((*s
)->name
, name
)) {
422 struct tokenval tokval
;
424 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
428 tokval
.t_type
= TOKEN_INVALID
;
429 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
432 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
433 " expression as symbol size");
435 (*s
)->size
= reloc_value(e
);
439 * Remove it from the list of unresolved sizes.
441 nasm_free((*s
)->name
);
445 return; /* it wasn't an important one */
448 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
449 strslen
+= 1 + strlen(name
);
451 lastsym
= sym
= saa_wstruct(syms
);
453 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
456 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
457 sym
->other
= STV_DEFAULT
;
459 if (segment
== NO_SEG
)
460 sym
->section
= SHN_ABS
;
463 sym
->section
= SHN_UNDEF
;
464 if (segment
== def_seg
) {
465 /* we have to be sure at least text section is there */
467 if (segment
!= elf_section_names(".text", 2, &tempint
))
468 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
470 for (i
= 0; i
< nsects
; i
++) {
471 if (segment
== sects
[i
]->index
) {
472 sym
->section
= i
+ 1;
478 if (is_global
== 2) {
481 sym
->section
= SHN_COMMON
;
483 * We have a common variable. Check the special text to see
484 * if it's a valid number and power of two; if so, store it
485 * as the alignment for the common variable.
489 sym
->symv
.key
= readnum(special
, &err
);
491 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
492 " valid number", special
);
493 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
494 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
495 " power of two", special
);
499 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
501 if (sym
->type
== SYM_GLOBAL
) {
503 * If sym->section == SHN_ABS, then the first line of the
504 * else section would cause a core dump, because its a reference
505 * beyond the end of the section array.
506 * This behaviour is exhibited by this code:
509 * To avoid such a crash, such requests are silently discarded.
510 * This may not be the best solution.
512 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
513 bsym
= raa_write(bsym
, segment
, nglobs
);
514 } else if (sym
->section
!= SHN_ABS
) {
516 * This is a global symbol; so we must add it to the rbtree
517 * of global symbols in its section.
519 * In addition, we check the special text for symbol
520 * type and size information.
522 sects
[sym
->section
-1]->gsyms
=
523 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
526 int n
= strcspn(special
, " \t");
528 if (!nasm_strnicmp(special
, "function", n
))
529 sym
->type
|= STT_FUNC
;
530 else if (!nasm_strnicmp(special
, "data", n
) ||
531 !nasm_strnicmp(special
, "object", n
))
532 sym
->type
|= STT_OBJECT
;
533 else if (!nasm_strnicmp(special
, "notype", n
))
534 sym
->type
|= STT_NOTYPE
;
536 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
540 special
= nasm_skip_spaces(special
);
542 n
= strcspn(special
, " \t");
543 if (!nasm_strnicmp(special
, "default", n
))
544 sym
->other
= STV_DEFAULT
;
545 else if (!nasm_strnicmp(special
, "internal", n
))
546 sym
->other
= STV_INTERNAL
;
547 else if (!nasm_strnicmp(special
, "hidden", n
))
548 sym
->other
= STV_HIDDEN
;
549 else if (!nasm_strnicmp(special
, "protected", n
))
550 sym
->other
= STV_PROTECTED
;
557 struct tokenval tokval
;
560 char *saveme
= stdscan_get();
562 while (special
[n
] && nasm_isspace(special
[n
]))
565 * We have a size expression; attempt to
569 stdscan_set(special
+ n
);
570 tokval
.t_type
= TOKEN_INVALID
;
571 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
576 sym
->name
= nasm_strdup(name
);
579 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
580 " expression as symbol size");
582 sym
->size
= reloc_value(e
);
589 * If TLS segment, mark symbol accordingly.
591 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
593 sym
->type
|= STT_TLS
;
596 sym
->globnum
= nglobs
;
601 if (special
&& !special_used
)
602 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
605 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
609 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
610 sect
->tail
= &r
->next
;
613 r
->address
= sect
->len
;
614 if (segment
== NO_SEG
)
619 for (i
= 0; i
< nsects
; i
++)
620 if (segment
== sects
[i
]->index
)
623 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
631 * This routine deals with ..got and ..sym relocations: the more
632 * complicated kinds. In shared-library writing, some relocations
633 * with respect to global symbols must refer to the precise symbol
634 * rather than referring to an offset from the base of the section
635 * _containing_ the symbol. Such relocations call to this routine,
636 * which searches the symbol list for the symbol in question.
638 * R_386_GOT32 references require the _exact_ symbol address to be
639 * used; R_386_32 references can be at an offset from the symbol.
640 * The boolean argument `exact' tells us this.
642 * Return value is the adjusted value of `addr', having become an
643 * offset from the symbol rather than the section. Should always be
644 * zero when returning from an exact call.
646 * Limitation: if you define two symbols at the same place,
647 * confusion will occur.
649 * Inefficiency: we search, currently, using a linked list which
650 * isn't even necessarily sorted.
652 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
653 int32_t segment
, uint32_t offset
,
654 int type
, bool exact
)
663 * First look up the segment/offset pair and find a global
664 * symbol corresponding to it. If it's not one of our segments,
665 * then it must be an external symbol, in which case we're fine
666 * doing a normal elf_add_reloc after first sanity-checking
667 * that the offset from the symbol is zero.
670 for (i
= 0; i
< nsects
; i
++)
671 if (segment
== sects
[i
]->index
) {
676 if (exact
&& offset
!= 0)
677 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
678 " for this reference");
680 elf_add_reloc(sect
, segment
, type
);
684 srb
= rb_search(s
->gsyms
, offset
);
685 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
686 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
687 " for this reference");
690 sym
= container_of(srb
, struct Symbol
, symv
);
692 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
693 sect
->tail
= &r
->next
;
696 r
->address
= sect
->len
;
697 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
702 return offset
- sym
->symv
.key
;
705 static void elf_out(int32_t segto
, const void *data
,
706 enum out_type type
, uint64_t size
,
707 int32_t segment
, int32_t wrt
)
711 uint8_t mydata
[8], *p
;
714 static struct symlininfo sinfo
;
717 * handle absolute-assembly (structure definitions)
719 if (segto
== NO_SEG
) {
720 if (type
!= OUT_RESERVE
)
721 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
727 for (i
= 0; i
< nsects
; i
++)
728 if (segto
== sects
[i
]->index
) {
733 int tempint
; /* ignored */
734 if (segto
!= elf_section_names(".text", 2, &tempint
))
735 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
737 s
= sects
[nsects
- 1];
742 /* again some stabs debugging stuff */
743 if (of_elf32
.current_dfmt
) {
744 sinfo
.offset
= s
->len
;
746 sinfo
.name
= s
->name
;
747 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
749 /* end of debugging stuff */
751 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
752 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
753 " BSS section `%s': ignored", s
->name
);
754 s
->len
+= realsize(type
, size
);
760 if (s
->type
== SHT_PROGBITS
) {
761 nasm_error(ERR_WARNING
, "uninitialized space declared in"
762 " non-BSS section `%s': zeroing", s
->name
);
763 elf_sect_write(s
, NULL
, size
);
769 if (segment
!= NO_SEG
)
770 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
771 elf_sect_write(s
, data
, size
);
777 addr
= *(int64_t *)data
;
778 if (segment
!= NO_SEG
) {
780 nasm_error(ERR_NONFATAL
, "ELF format does not support"
781 " segment base references");
785 * The if() is a hack to deal with compilers which
786 * don't handle switch() statements with 64-bit
789 if (size
< UINT_MAX
) {
790 switch ((unsigned int)size
) {
793 elf_add_reloc(s
, segment
, R_386_8
);
797 elf_add_reloc(s
, segment
, R_386_16
);
800 elf_add_reloc(s
, segment
, R_386_32
);
802 default: /* Error issued further down */
806 } else if (wrt
== elf_gotpc_sect
+ 1) {
808 * The user will supply GOT relative to $$. ELF
809 * will let us have GOT relative to $. So we
810 * need to fix up the data item by $-$$.
813 elf_add_reloc(s
, segment
, R_386_GOTPC
);
814 } else if (wrt
== elf_gotoff_sect
+ 1) {
815 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
816 } else if (wrt
== elf_tlsie_sect
+ 1) {
817 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
819 } else if (wrt
== elf_got_sect
+ 1) {
820 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
822 } else if (wrt
== elf_sym_sect
+ 1) {
825 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
828 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
831 } else if (wrt
== elf_plt_sect
+ 1) {
832 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
833 "relative PLT references");
835 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
837 wrt
= NO_SEG
; /* we can at least _try_ to continue */
843 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
844 "8- or 16-bit relocations in ELF32 is a GNU extension");
845 } else if (size
!= 4 && segment
!= NO_SEG
) {
846 nasm_error(ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
848 WRITEADDR(p
, addr
, size
);
849 elf_sect_write(s
, mydata
, size
);
859 reltype
= R_386_PC16
;
863 nasm_assert(segment
!= segto
);
864 if (segment
!= NO_SEG
&& segment
% 2) {
865 nasm_error(ERR_NONFATAL
, "ELF format does not support"
866 " segment base references");
869 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
870 "8- or 16-bit relocations in ELF is a GNU extension");
871 elf_add_reloc(s
, segment
, reltype
);
873 nasm_error(ERR_NONFATAL
,
874 "Unsupported non-32-bit ELF relocation");
878 WRITESHORT(p
, *(int64_t *)data
- size
);
879 elf_sect_write(s
, mydata
, bytes
);
883 if (segment
== segto
)
884 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
885 if (segment
!= NO_SEG
&& segment
% 2) {
886 nasm_error(ERR_NONFATAL
, "ELF format does not support"
887 " segment base references");
890 elf_add_reloc(s
, segment
, R_386_PC32
);
891 } else if (wrt
== elf_plt_sect
+ 1) {
892 elf_add_reloc(s
, segment
, R_386_PLT32
);
893 } else if (wrt
== elf_gotpc_sect
+ 1 ||
894 wrt
== elf_gotoff_sect
+ 1 ||
895 wrt
== elf_got_sect
+ 1) {
896 nasm_error(ERR_NONFATAL
, "ELF format cannot produce PC-"
897 "relative GOT references");
899 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
901 wrt
= NO_SEG
; /* we can at least _try_ to continue */
905 WRITELONG(p
, *(int64_t *)data
- size
);
906 elf_sect_write(s
, mydata
, 4L);
910 nasm_error(ERR_NONFATAL
,
911 "32-bit ELF format does not support 64-bit relocations");
914 elf_sect_write(s
, mydata
, 8L);
919 static void elf_write(void)
926 int32_t symtablen
, symtablocal
;
929 * Work out how many sections we will have. We have SHN_UNDEF,
930 * then the flexible user sections, then the fixed sections
931 * `.shstrtab', `.symtab' and `.strtab', then optionally
932 * relocation sections for the user sections.
934 nsections
= sec_numspecial
+ 1;
935 if (of_elf32
.current_dfmt
== &df_stabs
)
937 else if (of_elf32
.current_dfmt
== &df_dwarf
)
940 add_sectname("", ".shstrtab");
941 add_sectname("", ".symtab");
942 add_sectname("", ".strtab");
943 for (i
= 0; i
< nsects
; i
++) {
944 nsections
++; /* for the section itself */
945 if (sects
[i
]->head
) {
946 nsections
++; /* for its relocations */
947 add_sectname(".rel", sects
[i
]->name
);
951 if (of_elf32
.current_dfmt
== &df_stabs
) {
952 /* in case the debug information is wanted, just add these three sections... */
953 add_sectname("", ".stab");
954 add_sectname("", ".stabstr");
955 add_sectname(".rel", ".stab");
956 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
957 /* the dwarf debug standard specifies the following ten sections,
958 not all of which are currently implemented,
959 although all of them are defined. */
960 add_sectname("", ".debug_aranges");
961 add_sectname(".rela", ".debug_aranges");
962 add_sectname("", ".debug_pubnames");
963 add_sectname("", ".debug_info");
964 add_sectname(".rela", ".debug_info");
965 add_sectname("", ".debug_abbrev");
966 add_sectname("", ".debug_line");
967 add_sectname(".rela", ".debug_line");
968 add_sectname("", ".debug_frame");
969 add_sectname("", ".debug_loc");
973 * Output the ELF header.
975 fwrite("\177ELF\1\1\1", 7, 1, ofile
);
976 fputc(elf_osabi
, ofile
);
977 fputc(elf_abiver
, ofile
);
978 fwritezero(7, ofile
);
979 fwriteint16_t(1, ofile
); /* ET_REL relocatable file */
980 fwriteint16_t(3, ofile
); /* EM_386 processor ID */
981 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
982 fwriteint32_t(0L, ofile
); /* no entry point */
983 fwriteint32_t(0L, ofile
); /* no program header table */
984 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
985 * ELF header plus alignment */
986 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
987 fwriteint16_t(0x34, ofile
); /* size of ELF header */
988 fwriteint16_t(0, ofile
); /* no program header table, again */
989 fwriteint16_t(0, ofile
); /* still no program header table */
990 fwriteint16_t(0x28, ofile
); /* size of section header */
991 fwriteint16_t(nsections
, ofile
); /* number of sections */
992 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
993 * section header table */
994 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
995 fwriteint32_t(0L, ofile
);
996 fwriteint32_t(0L, ofile
);
999 * Build the symbol table and relocation tables.
1001 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1002 for (i
= 0; i
< nsects
; i
++)
1004 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1008 * Now output the section header table.
1011 elf_foffs
= 0x40 + 0x28 * nsections
;
1012 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
1015 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1018 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1021 /* The normal sections */
1022 for (i
= 0; i
< nsects
; i
++) {
1023 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1024 (sects
[i
]->type
== SHT_PROGBITS
?
1025 sects
[i
]->data
: NULL
), true,
1026 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1031 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1032 shstrtablen
, 0, 0, 1, 0);
1036 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1037 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1041 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1042 strslen
, 0, 0, 1, 0);
1045 /* The relocation sections */
1046 for (i
= 0; i
< nsects
; i
++)
1047 if (sects
[i
]->head
) {
1048 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1049 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1053 if (of_elf32
.current_dfmt
== &df_stabs
) {
1054 /* for debugging information, create the last three sections
1055 which are the .stab , .stabstr and .rel.stab sections respectively */
1057 /* this function call creates the stab sections in memory */
1060 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1061 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1062 stablen
, sec_stabstr
, 0, 4, 12);
1065 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1066 stabstrlen
, 0, 0, 4, 0);
1069 /* link -> symtable info -> section to refer to */
1070 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1071 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1074 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1075 /* for dwarf debugging information, create the ten dwarf sections */
1077 /* this function call creates the dwarf sections in memory */
1081 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1082 arangeslen
, 0, 0, 1, 0);
1085 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1086 arangesrellen
, sec_symtab
, sec_debug_aranges
,
1090 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1091 false, pubnameslen
, 0, 0, 1, 0);
1094 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1095 infolen
, 0, 0, 1, 0);
1098 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1099 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1102 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1103 abbrevlen
, 0, 0, 1, 0);
1106 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1107 linelen
, 0, 0, 1, 0);
1110 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1111 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1114 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1115 framelen
, 0, 0, 8, 0);
1118 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1119 loclen
, 0, 0, 1, 0);
1122 fwritezero(align
, ofile
);
1125 * Now output the sections.
1127 elf_write_sections();
1129 nasm_free(elf_sects
);
1133 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1135 struct SAA
*s
= saa_init(1L);
1137 uint8_t entry
[16], *p
;
1143 * First, an all-zeros entry, required by the ELF spec.
1145 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1150 * Next, an entry for the file name.
1153 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1154 WRITELONG(p
, 0); /* no value */
1155 WRITELONG(p
, 0); /* no size either */
1156 WRITESHORT(p
, STT_FILE
); /* type FILE */
1157 WRITESHORT(p
, SHN_ABS
);
1158 saa_wbytes(s
, entry
, 16L);
1163 * Now some standard symbols defining the segments, for relocation
1166 for (i
= 1; i
<= nsects
; i
++) {
1168 WRITELONG(p
, 0); /* no symbol name */
1169 WRITELONG(p
, 0); /* offset zero */
1170 WRITELONG(p
, 0); /* size zero */
1171 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1172 WRITESHORT(p
, i
); /* section id */
1173 saa_wbytes(s
, entry
, 16L);
1179 * Now the other local symbols.
1182 while ((sym
= saa_rstruct(syms
))) {
1183 if (sym
->type
& SYM_GLOBAL
)
1186 WRITELONG(p
, sym
->strpos
);
1187 WRITELONG(p
, sym
->symv
.key
);
1188 WRITELONG(p
, sym
->size
);
1189 WRITECHAR(p
, sym
->type
); /* type and binding */
1190 WRITECHAR(p
, sym
->other
); /* visibility */
1191 WRITESHORT(p
, sym
->section
);
1192 saa_wbytes(s
, entry
, 16L);
1197 * dwarf needs symbols for debug sections
1198 * which are relocation targets.
1200 //*** fix for 32 bit
1201 if (of_elf32
.current_dfmt
== &df_dwarf
) {
1202 dwarf_infosym
= *local
;
1204 WRITELONG(p
, 0); /* no symbol name */
1205 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1206 WRITELONG(p
, (uint32_t) 0); /* size zero */
1207 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1208 WRITESHORT(p
, sec_debug_info
); /* section id */
1209 saa_wbytes(s
, entry
, 16L);
1212 dwarf_abbrevsym
= *local
;
1214 WRITELONG(p
, 0); /* no symbol name */
1215 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1216 WRITELONG(p
, (uint32_t) 0); /* size zero */
1217 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1218 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1219 saa_wbytes(s
, entry
, 16L);
1222 dwarf_linesym
= *local
;
1224 WRITELONG(p
, 0); /* no symbol name */
1225 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1226 WRITELONG(p
, (uint32_t) 0); /* size zero */
1227 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1228 WRITESHORT(p
, sec_debug_line
); /* section id */
1229 saa_wbytes(s
, entry
, 16L);
1235 * Now the global symbols.
1238 while ((sym
= saa_rstruct(syms
))) {
1239 if (!(sym
->type
& SYM_GLOBAL
))
1242 WRITELONG(p
, sym
->strpos
);
1243 WRITELONG(p
, sym
->symv
.key
);
1244 WRITELONG(p
, sym
->size
);
1245 WRITECHAR(p
, sym
->type
); /* type and binding */
1246 WRITECHAR(p
, sym
->other
); /* visibility */
1247 WRITESHORT(p
, sym
->section
);
1248 saa_wbytes(s
, entry
, 16L);
1255 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1258 uint8_t *p
, entry
[8];
1259 int32_t global_offset
;
1268 * How to onvert from a global placeholder to a real symbol index;
1269 * the +2 refers to the two special entries, the null entry and
1270 * the filename entry.
1272 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1275 int32_t sym
= r
->symbol
;
1278 * Create a real symbol index; the +2 refers to the two special
1279 * entries, the null entry and the filename entry.
1281 if (sym
>= GLOBAL_TEMP_BASE
)
1282 sym
+= global_offset
;
1285 WRITELONG(p
, r
->address
);
1286 WRITELONG(p
, (sym
<< 8) + r
->type
);
1287 saa_wbytes(s
, entry
, 8L);
1296 static void elf_section_header(int name
, int type
, int flags
,
1297 void *data
, bool is_saa
, int32_t datalen
,
1298 int link
, int info
, int align
, int eltsize
)
1300 elf_sects
[elf_nsect
].data
= data
;
1301 elf_sects
[elf_nsect
].len
= datalen
;
1302 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1305 fwriteint32_t((int32_t)name
, ofile
);
1306 fwriteint32_t((int32_t)type
, ofile
);
1307 fwriteint32_t((int32_t)flags
, ofile
);
1308 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1309 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1310 fwriteint32_t(datalen
, ofile
);
1312 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1313 fwriteint32_t((int32_t)link
, ofile
);
1314 fwriteint32_t((int32_t)info
, ofile
);
1315 fwriteint32_t((int32_t)align
, ofile
);
1316 fwriteint32_t((int32_t)eltsize
, ofile
);
1319 static void elf_write_sections(void)
1322 for (i
= 0; i
< elf_nsect
; i
++)
1323 if (elf_sects
[i
].data
) {
1324 int32_t len
= elf_sects
[i
].len
;
1325 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1326 int32_t align
= reallen
- len
;
1327 if (elf_sects
[i
].is_saa
)
1328 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1330 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1331 fwritezero(align
, ofile
);
1335 static void elf_sect_write(struct Section
*sect
,
1336 const uint8_t *data
, uint32_t len
)
1338 saa_wbytes(sect
->data
, data
, len
);
1342 static void elf_sectalign(int32_t seg
, unsigned int value
)
1344 struct Section
*s
= NULL
;
1347 for (i
= 0; i
< nsects
; i
++) {
1348 if (sects
[i
]->index
== seg
) {
1353 if (!s
|| !is_power2(value
))
1356 if (value
> s
->align
)
1360 static int32_t elf_segbase(int32_t segment
)
1365 static int elf_directive(enum directives directive
, char *value
, int pass
)
1371 switch (directive
) {
1374 return 1; /* ignore in pass 2 */
1376 n
= readnum(value
, &err
);
1378 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1381 if (n
< 0 || n
> 255) {
1382 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1388 if ((p
= strchr(value
,',')) == NULL
)
1391 n
= readnum(p
+1, &err
);
1392 if (err
|| n
< 0 || n
> 255) {
1393 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1405 static void elf_filename(char *inname
, char *outname
)
1407 strcpy(elf_module
, inname
);
1408 standard_extension(inname
, outname
, ".o");
1411 extern macros_t elf_stdmac
[];
1413 static int elf_set_info(enum geninfo type
, char **val
)
1419 static struct dfmt df_dwarf
= {
1420 "ELF32 (i386) dwarf debug format for Linux/Unix",
1430 static struct dfmt df_stabs
= {
1431 "ELF32 (i386) stabs debug format for Linux/Unix",
1442 struct dfmt
*elf32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1444 struct ofmt of_elf32
= {
1445 "ELF32 (i386) object files (e.g. Linux)",
1463 /* again, the stabs debugging stuff (code) */
1465 static void stabs32_linenum(const char *filename
, int32_t linenumber
,
1470 if (!stabs_filename
) {
1471 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1472 strcpy(stabs_filename
, filename
);
1474 if (strcmp(stabs_filename
, filename
)) {
1476 * yep, a memory leak...this program is one-shot anyway, so who cares...
1477 * in fact, this leak comes in quite handy to maintain a list of files
1478 * encountered so far in the symbol lines...
1481 /* why not nasm_free(stabs_filename); we're done with the old one */
1483 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1484 strcpy(stabs_filename
, filename
);
1488 currentline
= linenumber
;
1491 static void debug32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1501 static void debug32_directive(const char *directive
, const char *params
)
1507 static void debug32_typevalue(int32_t type
)
1509 int32_t stype
, ssize
;
1510 switch (TYM_TYPE(type
)) {
1553 stype
= STT_SECTION
;
1568 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1569 lastsym
->size
= ssize
;
1570 lastsym
->type
= stype
;
1574 static void stabs32_output(int type
, void *param
)
1576 struct symlininfo
*s
;
1577 struct linelist
*el
;
1578 if (type
== TY_STABSSYMLIN
) {
1579 if (debug_immcall
) {
1580 s
= (struct symlininfo
*)param
;
1581 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1582 return; /* line info is only collected for executable sections */
1584 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1585 el
->info
.offset
= s
->offset
;
1586 el
->info
.section
= s
->section
;
1587 el
->info
.name
= s
->name
;
1588 el
->line
= currentline
;
1589 el
->filename
= stabs_filename
;
1592 stabslines
->last
->next
= el
;
1593 stabslines
->last
= el
;
1596 stabslines
->last
= el
;
1603 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1605 static void stabs32_generate(void)
1607 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1608 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1612 struct linelist
*ptr
;
1616 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1617 for (i
= 0; i
< numlinestabs
; i
++)
1621 if (numfiles
== 0) {
1622 allfiles
[0] = ptr
->filename
;
1625 for (i
= 0; i
< numfiles
; i
++) {
1626 if (!strcmp(allfiles
[i
], ptr
->filename
))
1629 if (i
>= numfiles
) {
1630 allfiles
[i
] = ptr
->filename
;
1637 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1638 for (i
= 0; i
< numfiles
; i
++) {
1639 fileidx
[i
] = strsize
;
1640 strsize
+= strlen(allfiles
[i
]) + 1;
1643 for (i
= 0; i
< numfiles
; i
++) {
1644 if (!strcmp(allfiles
[i
], elf_module
)) {
1651 * worst case size of the stab buffer would be:
1652 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1653 * plus one "ending" entry
1655 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1656 sizeof(struct stabentry
));
1657 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1658 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1661 for (i
= 0; i
< numfiles
; i
++)
1662 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1665 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1673 * this is the first stab, its strx points to the filename of the
1674 * the source-file, the n_desc field should be set to the number
1675 * of remaining stabs
1677 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1679 /* this is the stab for the main source file */
1680 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1682 /* relocation table entry */
1685 * Since the symbol table has two entries before
1686 * the section symbols, the index in the info.section
1687 * member must be adjusted by adding 2
1690 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1691 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1694 currfile
= mainfileindex
;
1698 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1699 /* oops file has changed... */
1700 for (i
= 0; i
< numfiles
; i
++)
1701 if (!strcmp(allfiles
[i
], ptr
->filename
))
1704 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1708 /* relocation table entry */
1709 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1710 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1713 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1716 /* relocation table entry */
1718 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1719 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1725 /* this is an "ending" token */
1726 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1729 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1731 nasm_free(allfiles
);
1734 stablen
= (sptr
- sbuf
);
1735 stabrellen
= (rptr
- rbuf
);
1741 static void stabs32_cleanup(void)
1743 struct linelist
*ptr
, *del
;
1755 nasm_free(stabrelbuf
);
1756 nasm_free(stabstrbuf
);
1759 /* dwarf routines */
1761 static void dwarf32_init(void)
1763 ndebugs
= 3; /* 3 debug symbols */
1766 static void dwarf32_linenum(const char *filename
, int32_t linenumber
,
1770 dwarf32_findfile(filename
);
1772 currentline
= linenumber
;
1775 /* called from elf_out with type == TY_DEBUGSYMLIN */
1776 static void dwarf32_output(int type
, void *param
)
1778 int ln
, aa
, inx
, maxln
, soc
;
1779 struct symlininfo
*s
;
1784 s
= (struct symlininfo
*)param
;
1786 /* line number info is only gathered for executable sections */
1787 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1790 /* Check if section index has changed */
1791 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1792 dwarf32_findsect(s
->section
);
1794 /* do nothing unless line or file has changed */
1798 ln
= currentline
- dwarf_csect
->line
;
1799 aa
= s
->offset
- dwarf_csect
->offset
;
1800 inx
= dwarf_clist
->line
;
1801 plinep
= dwarf_csect
->psaa
;
1802 /* check for file change */
1803 if (!(inx
== dwarf_csect
->file
)) {
1804 saa_write8(plinep
,DW_LNS_set_file
);
1805 saa_write8(plinep
,inx
);
1806 dwarf_csect
->file
= inx
;
1808 /* check for line change */
1810 /* test if in range of special op code */
1811 maxln
= line_base
+ line_range
;
1812 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1813 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1814 saa_write8(plinep
,soc
);
1816 saa_write8(plinep
,DW_LNS_advance_line
);
1817 saa_wleb128s(plinep
,ln
);
1819 saa_write8(plinep
,DW_LNS_advance_pc
);
1820 saa_wleb128u(plinep
,aa
);
1823 dwarf_csect
->line
= currentline
;
1824 dwarf_csect
->offset
= s
->offset
;
1827 /* show change handled */
1832 static void dwarf32_generate(void)
1836 struct linelist
*ftentry
;
1837 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1838 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1839 struct sectlist
*psect
;
1840 size_t saalen
, linepoff
, totlen
, highaddr
;
1842 /* write epilogues for each line program range */
1843 /* and build aranges section */
1844 paranges
= saa_init(1L);
1845 parangesrel
= saa_init(1L);
1846 saa_write16(paranges
,2); /* dwarf version */
1847 saa_write32(parangesrel
, paranges
->datalen
+4);
1848 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
1849 saa_write32(parangesrel
, 0);
1850 saa_write32(paranges
,0); /* offset into info */
1851 saa_write8(paranges
,4); /* pointer size */
1852 saa_write8(paranges
,0); /* not segmented */
1853 saa_write32(paranges
,0); /* padding */
1854 /* iterate though sectlist entries */
1855 psect
= dwarf_fsect
;
1858 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
1859 plinep
= psect
->psaa
;
1860 /* Line Number Program Epilogue */
1861 saa_write8(plinep
,2); /* std op 2 */
1862 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1863 saa_write8(plinep
,DW_LNS_extended_op
);
1864 saa_write8(plinep
,1); /* operand length */
1865 saa_write8(plinep
,DW_LNE_end_sequence
);
1866 totlen
+= plinep
->datalen
;
1867 /* range table relocation entry */
1868 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1869 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
1870 saa_write32(parangesrel
, (uint32_t) 0);
1871 /* range table entry */
1872 saa_write32(paranges
,0x0000); /* range start */
1873 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1874 highaddr
+= sects
[psect
->section
]->len
;
1875 /* done with this entry */
1876 psect
= psect
->next
;
1878 saa_write32(paranges
,0); /* null address */
1879 saa_write32(paranges
,0); /* null length */
1880 saalen
= paranges
->datalen
;
1881 arangeslen
= saalen
+ 4;
1882 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1883 WRITELONG(pbuf
,saalen
); /* initial length */
1884 saa_rnbytes(paranges
, pbuf
, saalen
);
1887 /* build rela.aranges section */
1888 arangesrellen
= saalen
= parangesrel
->datalen
;
1889 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1890 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1891 saa_free(parangesrel
);
1893 /* build pubnames section */
1894 ppubnames
= saa_init(1L);
1895 saa_write16(ppubnames
,3); /* dwarf version */
1896 saa_write32(ppubnames
,0); /* offset into info */
1897 saa_write32(ppubnames
,0); /* space used in info */
1898 saa_write32(ppubnames
,0); /* end of list */
1899 saalen
= ppubnames
->datalen
;
1900 pubnameslen
= saalen
+ 4;
1901 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1902 WRITELONG(pbuf
,saalen
); /* initial length */
1903 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1904 saa_free(ppubnames
);
1906 /* build info section */
1907 pinfo
= saa_init(1L);
1908 pinforel
= saa_init(1L);
1909 saa_write16(pinfo
,2); /* dwarf version */
1910 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1911 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
1912 saa_write32(pinforel
, 0);
1913 saa_write32(pinfo
,0); /* offset into abbrev */
1914 saa_write8(pinfo
,4); /* pointer size */
1915 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1916 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1917 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1918 saa_write32(pinforel
, 0);
1919 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1920 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1921 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1922 saa_write32(pinforel
, 0);
1923 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1924 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1925 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
1926 saa_write32(pinforel
, 0);
1927 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1928 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1929 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1930 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1931 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1932 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1933 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1934 saa_write32(pinforel
, 0);
1935 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1936 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1937 saa_write8(pinfo
,0); /* end of entries */
1938 saalen
= pinfo
->datalen
;
1939 infolen
= saalen
+ 4;
1940 infobuf
= pbuf
= nasm_malloc(infolen
);
1941 WRITELONG(pbuf
,saalen
); /* initial length */
1942 saa_rnbytes(pinfo
, pbuf
, saalen
);
1945 /* build rela.info section */
1946 inforellen
= saalen
= pinforel
->datalen
;
1947 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1948 saa_rnbytes(pinforel
, pbuf
, saalen
);
1951 /* build abbrev section */
1952 pabbrev
= saa_init(1L);
1953 saa_write8(pabbrev
,1); /* entry number LEB128u */
1954 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1955 saa_write8(pabbrev
,1); /* has children */
1956 /* the following attributes and forms are all LEB128u values */
1957 saa_write8(pabbrev
,DW_AT_low_pc
);
1958 saa_write8(pabbrev
,DW_FORM_addr
);
1959 saa_write8(pabbrev
,DW_AT_high_pc
);
1960 saa_write8(pabbrev
,DW_FORM_addr
);
1961 saa_write8(pabbrev
,DW_AT_stmt_list
);
1962 saa_write8(pabbrev
,DW_FORM_data4
);
1963 saa_write8(pabbrev
,DW_AT_name
);
1964 saa_write8(pabbrev
,DW_FORM_string
);
1965 saa_write8(pabbrev
,DW_AT_producer
);
1966 saa_write8(pabbrev
,DW_FORM_string
);
1967 saa_write8(pabbrev
,DW_AT_language
);
1968 saa_write8(pabbrev
,DW_FORM_data2
);
1969 saa_write16(pabbrev
,0); /* end of entry */
1970 /* LEB128u usage same as above */
1971 saa_write8(pabbrev
,2); /* entry number */
1972 saa_write8(pabbrev
,DW_TAG_subprogram
);
1973 saa_write8(pabbrev
,0); /* no children */
1974 saa_write8(pabbrev
,DW_AT_low_pc
);
1975 saa_write8(pabbrev
,DW_FORM_addr
);
1976 saa_write8(pabbrev
,DW_AT_frame_base
);
1977 saa_write8(pabbrev
,DW_FORM_data4
);
1978 saa_write16(pabbrev
,0); /* end of entry */
1979 abbrevlen
= saalen
= pabbrev
->datalen
;
1980 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
1981 saa_rnbytes(pabbrev
, pbuf
, saalen
);
1984 /* build line section */
1986 plines
= saa_init(1L);
1987 saa_write8(plines
,1); /* Minimum Instruction Length */
1988 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
1989 saa_write8(plines
,line_base
); /* Line Base */
1990 saa_write8(plines
,line_range
); /* Line Range */
1991 saa_write8(plines
,opcode_base
); /* Opcode Base */
1992 /* standard opcode lengths (# of LEB128u operands) */
1993 saa_write8(plines
,0); /* Std opcode 1 length */
1994 saa_write8(plines
,1); /* Std opcode 2 length */
1995 saa_write8(plines
,1); /* Std opcode 3 length */
1996 saa_write8(plines
,1); /* Std opcode 4 length */
1997 saa_write8(plines
,1); /* Std opcode 5 length */
1998 saa_write8(plines
,0); /* Std opcode 6 length */
1999 saa_write8(plines
,0); /* Std opcode 7 length */
2000 saa_write8(plines
,0); /* Std opcode 8 length */
2001 saa_write8(plines
,1); /* Std opcode 9 length */
2002 saa_write8(plines
,0); /* Std opcode 10 length */
2003 saa_write8(plines
,0); /* Std opcode 11 length */
2004 saa_write8(plines
,1); /* Std opcode 12 length */
2005 /* Directory Table */
2006 saa_write8(plines
,0); /* End of table */
2007 /* File Name Table */
2008 ftentry
= dwarf_flist
;
2009 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
2010 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2011 saa_write8(plines
,0); /* directory LEB128u */
2012 saa_write8(plines
,0); /* time LEB128u */
2013 saa_write8(plines
,0); /* size LEB128u */
2014 ftentry
= ftentry
->next
;
2016 saa_write8(plines
,0); /* End of table */
2017 linepoff
= plines
->datalen
;
2018 linelen
= linepoff
+ totlen
+ 10;
2019 linebuf
= pbuf
= nasm_malloc(linelen
);
2020 WRITELONG(pbuf
,linelen
-4); /* initial length */
2021 WRITESHORT(pbuf
,3); /* dwarf version */
2022 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2023 /* write line header */
2025 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2028 /* concatonate line program ranges */
2030 plinesrel
= saa_init(1L);
2031 psect
= dwarf_fsect
;
2032 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2033 saa_write32(plinesrel
, linepoff
);
2034 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2035 saa_write32(plinesrel
, (uint32_t) 0);
2036 plinep
= psect
->psaa
;
2037 saalen
= plinep
->datalen
;
2038 saa_rnbytes(plinep
, pbuf
, saalen
);
2042 /* done with this entry */
2043 psect
= psect
->next
;
2047 /* build rela.lines section */
2048 linerellen
=saalen
= plinesrel
->datalen
;
2049 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2050 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2051 saa_free(plinesrel
);
2053 /* build frame section */
2055 framebuf
= pbuf
= nasm_malloc(framelen
);
2056 WRITELONG(pbuf
,framelen
-4); /* initial length */
2058 /* build loc section */
2060 locbuf
= pbuf
= nasm_malloc(loclen
);
2061 WRITELONG(pbuf
,0); /* null beginning offset */
2062 WRITELONG(pbuf
,0); /* null ending offset */
2065 static void dwarf32_cleanup(void)
2067 nasm_free(arangesbuf
);
2068 nasm_free(arangesrelbuf
);
2069 nasm_free(pubnamesbuf
);
2071 nasm_free(inforelbuf
);
2072 nasm_free(abbrevbuf
);
2074 nasm_free(linerelbuf
);
2075 nasm_free(framebuf
);
2079 static void dwarf32_findfile(const char * fname
)
2082 struct linelist
*match
;
2084 /* return if fname is current file name */
2085 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2088 /* search for match */
2091 match
= dwarf_flist
;
2092 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2093 if (!(strcmp(fname
, match
->filename
))) {
2094 dwarf_clist
= match
;
2100 /* add file name to end of list */
2101 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2103 dwarf_clist
->line
= dwarf_numfiles
;
2104 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2105 strcpy(dwarf_clist
->filename
,fname
);
2106 dwarf_clist
->next
= 0;
2107 if (!dwarf_flist
) { /* if first entry */
2108 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2109 dwarf_clist
->last
= 0;
2110 } else { /* chain to previous entry */
2111 dwarf_elist
->next
= dwarf_clist
;
2112 dwarf_elist
= dwarf_clist
;
2116 static void dwarf32_findsect(const int index
)
2119 struct sectlist
*match
;
2122 /* return if index is current section index */
2123 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2126 /* search for match */
2129 match
= dwarf_fsect
;
2130 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2131 if ((match
->section
== index
)) {
2132 dwarf_csect
= match
;
2135 match
= match
->next
;
2139 /* add entry to end of list */
2140 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2142 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2143 dwarf_csect
->line
= 1;
2144 dwarf_csect
->offset
= 0;
2145 dwarf_csect
->file
= 1;
2146 dwarf_csect
->section
= index
;
2147 dwarf_csect
->next
= 0;
2148 /* set relocatable address at start of line program */
2149 saa_write8(plinep
,DW_LNS_extended_op
);
2150 saa_write8(plinep
,5); /* operand length */
2151 saa_write8(plinep
,DW_LNE_set_address
);
2152 saa_write32(plinep
,0); /* Start Address */
2154 if (!dwarf_fsect
) { /* if first entry */
2155 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2156 dwarf_csect
->last
= 0;
2157 } else { /* chain to previous entry */
2158 dwarf_esect
->next
= dwarf_csect
;
2159 dwarf_esect
= dwarf_csect
;