1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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
53 #include "output/outform.h"
54 #include "output/outlib.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
68 int32_t address
; /* relative to _start_ of section */
69 int32_t symbol
; /* symbol index */
70 int type
; /* type of relocation */
74 struct rbtree symv
; /* symbol value and symbol rbtree */
75 int32_t strpos
; /* string table position of name */
76 int32_t section
; /* section ID of the symbol */
77 int type
; /* symbol type */
78 int other
; /* symbol visibility */
79 int32_t size
; /* size of symbol */
80 int32_t globnum
; /* symbol table offset if global */
81 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
82 char *name
; /* used temporarily if in above list */
87 uint32_t len
, size
, nrelocs
;
89 int type
; /* SHT_PROGBITS or SHT_NOBITS */
90 uint32_t align
; /* alignment: power of two */
91 uint32_t flags
; /* section flags */
95 struct Reloc
*head
, **tail
;
96 struct rbtree
*gsyms
; /* global symbols in section */
100 static struct Section
**sects
;
101 static int nsects
, sectlen
;
103 #define SHSTR_DELTA 256
104 static char *shstrtab
;
105 static int shstrtablen
, shstrtabsize
;
107 static struct SAA
*syms
;
108 static uint32_t nlocals
, nglobs
, ndebugs
; /* Symbol counts */
110 static int32_t def_seg
;
112 static struct RAA
*bsym
;
114 static struct SAA
*strs
;
115 static uint32_t strslen
;
117 static struct Symbol
*fwds
;
119 static char elf_module
[FILENAME_MAX
];
121 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
122 static uint8_t elf_abiver
= 0; /* Current ABI version */
124 extern struct ofmt of_elf32
;
126 static struct ELF_SECTDATA
{
131 static int elf_nsect
, nsections
;
132 static int32_t elf_foffs
;
134 static void elf_write(void);
135 static void elf_sect_write(struct Section
*, const uint8_t *,
137 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
139 static void elf_write_sections(void);
140 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
141 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
142 static void add_sectname(char *, char *);
158 int section
; /* section index */
159 char *name
; /* shallow-copied pointer of section name */
163 struct symlininfo info
;
166 struct linelist
*next
;
167 struct linelist
*last
;
176 struct sectlist
*next
;
177 struct sectlist
*last
;
180 /* common debug variables */
181 static int currentline
= 1;
182 static int debug_immcall
= 0;
184 /* stabs debug variables */
185 static struct linelist
*stabslines
= 0;
186 static int numlinestabs
= 0;
187 static char *stabs_filename
= 0;
188 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
189 static int stablen
, stabstrlen
, stabrellen
;
191 /* dwarf debug variables */
192 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
193 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
194 static int dwarf_numfiles
= 0, dwarf_nsections
;
195 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
196 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
197 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
198 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
199 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
200 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
202 static struct dfmt df_dwarf
;
203 static struct dfmt df_stabs
;
204 static struct Symbol
*lastsym
;
206 /* common debugging routines */
207 static void debug32_typevalue(int32_t);
208 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
209 static void debug32_directive(const char *, const char *);
211 /* stabs debugging routines */
212 static void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
213 static void stabs32_output(int, void *);
214 static void stabs32_generate(void);
215 static void stabs32_cleanup(void);
217 /* dwarf debugging routines */
218 static void dwarf32_init(void);
219 static void dwarf32_linenum(const char *filename
, int32_t linenumber
, int32_t);
220 static void dwarf32_output(int, void *);
221 static void dwarf32_generate(void);
222 static void dwarf32_cleanup(void);
223 static void dwarf32_findfile(const char *);
224 static void dwarf32_findsect(const int);
227 * Special NASM section numbers which are used to define ELF special
228 * symbols, which can be used with WRT to provide PIC and TLS
231 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
232 static int32_t elf_got_sect
, elf_plt_sect
;
233 static int32_t elf_sym_sect
, elf_tlsie_sect
;
235 static void elf_init(void)
238 nsects
= sectlen
= 0;
239 syms
= saa_init((int32_t)sizeof(struct Symbol
));
240 nlocals
= nglobs
= ndebugs
= 0;
243 saa_wbytes(strs
, "\0", 1L);
244 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
245 strslen
= 2 + strlen(elf_module
);
247 shstrtablen
= shstrtabsize
= 0;;
248 add_sectname("", "");
252 elf_gotpc_sect
= seg_alloc();
253 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
254 elf_gotoff_sect
= seg_alloc();
255 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
256 elf_got_sect
= seg_alloc();
257 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
258 elf_plt_sect
= seg_alloc();
259 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
260 elf_sym_sect
= seg_alloc();
261 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
262 elf_tlsie_sect
= seg_alloc();
263 define_label("..tlsie", elf_tlsie_sect
+ 1, 0L, NULL
, false, false);
265 def_seg
= seg_alloc();
268 static void elf_cleanup(int debuginfo
)
276 for (i
= 0; i
< nsects
; i
++) {
277 if (sects
[i
]->type
!= SHT_NOBITS
)
278 saa_free(sects
[i
]->data
);
280 saa_free(sects
[i
]->rel
);
281 while (sects
[i
]->head
) {
283 sects
[i
]->head
= sects
[i
]->head
->next
;
291 if (of_elf32
.current_dfmt
) {
292 of_elf32
.current_dfmt
->cleanup();
296 static void add_sectname(char *firsthalf
, char *secondhalf
)
298 int len
= strlen(firsthalf
) + strlen(secondhalf
);
299 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
300 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
301 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
302 strcat(shstrtab
+ shstrtablen
, secondhalf
);
303 shstrtablen
+= len
+ 1;
306 static int elf_make_section(char *name
, int type
, int flags
, int align
)
310 s
= nasm_malloc(sizeof(*s
));
312 if (type
!= SHT_NOBITS
)
313 s
->data
= saa_init(1L);
316 s
->len
= s
->size
= 0;
318 if (!strcmp(name
, ".text"))
321 s
->index
= seg_alloc();
322 add_sectname("", name
);
323 s
->name
= nasm_malloc(1 + strlen(name
));
324 strcpy(s
->name
, name
);
330 if (nsects
>= sectlen
)
331 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
337 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
340 uint32_t flags
, flags_and
, flags_or
;
345 * Default is 32 bits.
352 p
= nasm_skip_word(name
);
355 flags_and
= flags_or
= type
= align
= 0;
357 section_attrib(name
, p
, pass
, &flags_and
,
358 &flags_or
, &align
, &type
);
360 if (!strcmp(name
, ".shstrtab") ||
361 !strcmp(name
, ".symtab") ||
362 !strcmp(name
, ".strtab")) {
363 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
368 for (i
= 0; i
< nsects
; i
++)
369 if (!strcmp(name
, sects
[i
]->name
))
372 const struct elf_known_section
*ks
= elf_known_sections
;
375 if (!strcmp(name
, ks
->name
))
380 type
= type
? type
: ks
->type
;
381 align
= align
? align
: ks
->align
;
382 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
384 i
= elf_make_section(name
, type
, flags
, align
);
385 } else if (pass
== 1) {
386 if ((type
&& sects
[i
]->type
!= type
)
387 || (align
&& sects
[i
]->align
!= align
)
388 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
389 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
390 " redeclaration of section `%s'", name
);
393 return sects
[i
]->index
;
396 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
397 int is_global
, char *special
)
401 bool special_used
= false;
403 #if defined(DEBUG) && DEBUG>2
404 nasm_error(ERR_DEBUG
,
405 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
406 name
, segment
, offset
, is_global
, special
);
408 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
410 * This is a NASM special symbol. We never allow it into
411 * the ELF symbol table, even if it's a valid one. If it
412 * _isn't_ a valid one, we should barf immediately.
414 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
415 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
416 strcmp(name
, "..sym") && strcmp(name
, "..tlsie"))
417 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
421 if (is_global
== 3) {
424 * Fix up a forward-reference symbol size from the first
427 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
428 if (!strcmp((*s
)->name
, name
)) {
429 struct tokenval tokval
;
431 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
435 tokval
.t_type
= TOKEN_INVALID
;
436 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
439 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
440 " expression as symbol size");
442 (*s
)->size
= reloc_value(e
);
446 * Remove it from the list of unresolved sizes.
448 nasm_free((*s
)->name
);
452 return; /* it wasn't an important one */
455 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
456 strslen
+= 1 + strlen(name
);
458 lastsym
= sym
= saa_wstruct(syms
);
460 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
463 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
464 sym
->other
= STV_DEFAULT
;
466 if (segment
== NO_SEG
)
467 sym
->section
= SHN_ABS
;
470 sym
->section
= SHN_UNDEF
;
471 if (segment
== def_seg
) {
472 /* we have to be sure at least text section is there */
474 if (segment
!= elf_section_names(".text", 2, &tempint
))
475 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
477 for (i
= 0; i
< nsects
; i
++) {
478 if (segment
== sects
[i
]->index
) {
479 sym
->section
= i
+ 1;
485 if (is_global
== 2) {
488 sym
->section
= SHN_COMMON
;
490 * We have a common variable. Check the special text to see
491 * if it's a valid number and power of two; if so, store it
492 * as the alignment for the common variable.
496 sym
->symv
.key
= readnum(special
, &err
);
498 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
499 " valid number", special
);
500 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
501 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
502 " power of two", special
);
506 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
508 if (sym
->type
== SYM_GLOBAL
) {
510 * If sym->section == SHN_ABS, then the first line of the
511 * else section would cause a core dump, because its a reference
512 * beyond the end of the section array.
513 * This behaviour is exhibited by this code:
516 * To avoid such a crash, such requests are silently discarded.
517 * This may not be the best solution.
519 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
520 bsym
= raa_write(bsym
, segment
, nglobs
);
521 } else if (sym
->section
!= SHN_ABS
) {
523 * This is a global symbol; so we must add it to the rbtree
524 * of global symbols in its section.
526 * In addition, we check the special text for symbol
527 * type and size information.
529 sects
[sym
->section
-1]->gsyms
=
530 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
533 int n
= strcspn(special
, " \t");
535 if (!nasm_strnicmp(special
, "function", n
))
536 sym
->type
|= STT_FUNC
;
537 else if (!nasm_strnicmp(special
, "data", n
) ||
538 !nasm_strnicmp(special
, "object", n
))
539 sym
->type
|= STT_OBJECT
;
540 else if (!nasm_strnicmp(special
, "notype", n
))
541 sym
->type
|= STT_NOTYPE
;
543 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
547 special
= nasm_skip_spaces(special
);
549 n
= strcspn(special
, " \t");
550 if (!nasm_strnicmp(special
, "default", n
))
551 sym
->other
= STV_DEFAULT
;
552 else if (!nasm_strnicmp(special
, "internal", n
))
553 sym
->other
= STV_INTERNAL
;
554 else if (!nasm_strnicmp(special
, "hidden", n
))
555 sym
->other
= STV_HIDDEN
;
556 else if (!nasm_strnicmp(special
, "protected", n
))
557 sym
->other
= STV_PROTECTED
;
564 struct tokenval tokval
;
567 char *saveme
= stdscan_get();
569 while (special
[n
] && nasm_isspace(special
[n
]))
572 * We have a size expression; attempt to
576 stdscan_set(special
+ n
);
577 tokval
.t_type
= TOKEN_INVALID
;
578 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
583 sym
->name
= nasm_strdup(name
);
586 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
587 " expression as symbol size");
589 sym
->size
= reloc_value(e
);
596 * If TLS segment, mark symbol accordingly.
598 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
600 sym
->type
|= STT_TLS
;
603 sym
->globnum
= nglobs
;
608 if (special
&& !special_used
)
609 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
612 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
616 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
617 sect
->tail
= &r
->next
;
620 r
->address
= sect
->len
;
621 if (segment
== NO_SEG
)
626 for (i
= 0; i
< nsects
; i
++)
627 if (segment
== sects
[i
]->index
)
630 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
638 * This routine deals with ..got and ..sym relocations: the more
639 * complicated kinds. In shared-library writing, some relocations
640 * with respect to global symbols must refer to the precise symbol
641 * rather than referring to an offset from the base of the section
642 * _containing_ the symbol. Such relocations call to this routine,
643 * which searches the symbol list for the symbol in question.
645 * R_386_GOT32 references require the _exact_ symbol address to be
646 * used; R_386_32 references can be at an offset from the symbol.
647 * The boolean argument `exact' tells us this.
649 * Return value is the adjusted value of `addr', having become an
650 * offset from the symbol rather than the section. Should always be
651 * zero when returning from an exact call.
653 * Limitation: if you define two symbols at the same place,
654 * confusion will occur.
656 * Inefficiency: we search, currently, using a linked list which
657 * isn't even necessarily sorted.
659 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
660 int32_t segment
, uint32_t offset
,
661 int type
, bool exact
)
670 * First look up the segment/offset pair and find a global
671 * symbol corresponding to it. If it's not one of our segments,
672 * then it must be an external symbol, in which case we're fine
673 * doing a normal elf_add_reloc after first sanity-checking
674 * that the offset from the symbol is zero.
677 for (i
= 0; i
< nsects
; i
++)
678 if (segment
== sects
[i
]->index
) {
683 if (exact
&& offset
!= 0)
684 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
685 " for this reference");
687 elf_add_reloc(sect
, segment
, type
);
691 srb
= rb_search(s
->gsyms
, offset
);
692 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
693 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
694 " for this reference");
697 sym
= container_of(srb
, struct Symbol
, symv
);
699 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
700 sect
->tail
= &r
->next
;
703 r
->address
= sect
->len
;
704 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
709 return offset
- sym
->symv
.key
;
712 static void elf_out(int32_t segto
, const void *data
,
713 enum out_type type
, uint64_t size
,
714 int32_t segment
, int32_t wrt
)
718 uint8_t mydata
[4], *p
;
720 static struct symlininfo sinfo
;
723 * handle absolute-assembly (structure definitions)
725 if (segto
== NO_SEG
) {
726 if (type
!= OUT_RESERVE
)
727 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
733 for (i
= 0; i
< nsects
; i
++)
734 if (segto
== sects
[i
]->index
) {
739 int tempint
; /* ignored */
740 if (segto
!= elf_section_names(".text", 2, &tempint
))
741 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
743 s
= sects
[nsects
- 1];
748 /* again some stabs debugging stuff */
749 if (of_elf32
.current_dfmt
) {
750 sinfo
.offset
= s
->len
;
752 sinfo
.name
= s
->name
;
753 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
755 /* end of debugging stuff */
757 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
758 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
759 " BSS section `%s': ignored", s
->name
);
760 s
->len
+= realsize(type
, size
);
764 if (type
== OUT_RESERVE
) {
765 if (s
->type
== SHT_PROGBITS
) {
766 nasm_error(ERR_WARNING
, "uninitialized space declared in"
767 " non-BSS section `%s': zeroing", s
->name
);
768 elf_sect_write(s
, NULL
, size
);
771 } else if (type
== OUT_RAWDATA
) {
772 if (segment
!= NO_SEG
)
773 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
774 elf_sect_write(s
, data
, size
);
775 } else if (type
== OUT_ADDRESS
) {
777 addr
= *(int64_t *)data
;
778 if (segment
!= NO_SEG
) {
780 nasm_error(ERR_NONFATAL
, "ELF format does not support"
781 " segment base references");
786 elf_add_reloc(s
, segment
, R_386_16
);
788 elf_add_reloc(s
, segment
, R_386_32
);
790 } else if (wrt
== elf_gotpc_sect
+ 1) {
792 * The user will supply GOT relative to $$. ELF
793 * will let us have GOT relative to $. So we
794 * need to fix up the data item by $-$$.
797 elf_add_reloc(s
, segment
, R_386_GOTPC
);
798 } else if (wrt
== elf_gotoff_sect
+ 1) {
799 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
800 } else if (wrt
== elf_tlsie_sect
+ 1) {
801 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
803 } else if (wrt
== elf_got_sect
+ 1) {
804 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
806 } else if (wrt
== elf_sym_sect
+ 1) {
809 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
812 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
815 } else if (wrt
== elf_plt_sect
+ 1) {
816 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
817 "relative PLT references");
819 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
821 wrt
= NO_SEG
; /* we can at least _try_ to continue */
827 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
828 "16-bit relocations in ELF is a GNU extension");
831 if (size
!= 4 && segment
!= NO_SEG
) {
832 nasm_error(ERR_NONFATAL
,
833 "Unsupported non-32-bit ELF relocation");
837 elf_sect_write(s
, mydata
, size
);
838 } else if (type
== OUT_REL2ADR
) {
839 if (segment
== segto
)
840 nasm_error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
841 if (segment
!= NO_SEG
&& segment
% 2) {
842 nasm_error(ERR_NONFATAL
, "ELF format does not support"
843 " segment base references");
846 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
847 "16-bit relocations in ELF is a GNU extension");
848 elf_add_reloc(s
, segment
, R_386_PC16
);
850 nasm_error(ERR_NONFATAL
,
851 "Unsupported non-32-bit ELF relocation");
855 WRITESHORT(p
, *(int64_t *)data
- size
);
856 elf_sect_write(s
, mydata
, 2L);
857 } else if (type
== OUT_REL4ADR
) {
858 if (segment
== segto
)
859 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
860 if (segment
!= NO_SEG
&& segment
% 2) {
861 nasm_error(ERR_NONFATAL
, "ELF format does not support"
862 " segment base references");
865 elf_add_reloc(s
, segment
, R_386_PC32
);
866 } else if (wrt
== elf_plt_sect
+ 1) {
867 elf_add_reloc(s
, segment
, R_386_PLT32
);
868 } else if (wrt
== elf_gotpc_sect
+ 1 ||
869 wrt
== elf_gotoff_sect
+ 1 ||
870 wrt
== elf_got_sect
+ 1) {
871 nasm_error(ERR_NONFATAL
, "ELF format cannot produce PC-"
872 "relative GOT references");
874 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
876 wrt
= NO_SEG
; /* we can at least _try_ to continue */
880 WRITELONG(p
, *(int64_t *)data
- size
);
881 elf_sect_write(s
, mydata
, 4L);
885 static void elf_write(void)
892 int32_t symtablen
, symtablocal
;
895 * Work out how many sections we will have. We have SHN_UNDEF,
896 * then the flexible user sections, then the fixed sections
897 * `.shstrtab', `.symtab' and `.strtab', then optionally
898 * relocation sections for the user sections.
900 nsections
= sec_numspecial
+ 1;
901 if (of_elf32
.current_dfmt
== &df_stabs
)
903 else if (of_elf32
.current_dfmt
== &df_dwarf
)
906 add_sectname("", ".shstrtab");
907 add_sectname("", ".symtab");
908 add_sectname("", ".strtab");
909 for (i
= 0; i
< nsects
; i
++) {
910 nsections
++; /* for the section itself */
911 if (sects
[i
]->head
) {
912 nsections
++; /* for its relocations */
913 add_sectname(".rel", sects
[i
]->name
);
917 if (of_elf32
.current_dfmt
== &df_stabs
) {
918 /* in case the debug information is wanted, just add these three sections... */
919 add_sectname("", ".stab");
920 add_sectname("", ".stabstr");
921 add_sectname(".rel", ".stab");
922 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
923 /* the dwarf debug standard specifies the following ten sections,
924 not all of which are currently implemented,
925 although all of them are defined. */
926 add_sectname("", ".debug_aranges");
927 add_sectname(".rela", ".debug_aranges");
928 add_sectname("", ".debug_pubnames");
929 add_sectname("", ".debug_info");
930 add_sectname(".rela", ".debug_info");
931 add_sectname("", ".debug_abbrev");
932 add_sectname("", ".debug_line");
933 add_sectname(".rela", ".debug_line");
934 add_sectname("", ".debug_frame");
935 add_sectname("", ".debug_loc");
939 * Output the ELF header.
941 fwrite("\177ELF\1\1\1", 7, 1, ofile
);
942 fputc(elf_osabi
, ofile
);
943 fputc(elf_abiver
, ofile
);
944 fwritezero(7, ofile
);
945 fwriteint16_t(1, ofile
); /* ET_REL relocatable file */
946 fwriteint16_t(3, ofile
); /* EM_386 processor ID */
947 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
948 fwriteint32_t(0L, ofile
); /* no entry point */
949 fwriteint32_t(0L, ofile
); /* no program header table */
950 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
951 * ELF header plus alignment */
952 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
953 fwriteint16_t(0x34, ofile
); /* size of ELF header */
954 fwriteint16_t(0, ofile
); /* no program header table, again */
955 fwriteint16_t(0, ofile
); /* still no program header table */
956 fwriteint16_t(0x28, ofile
); /* size of section header */
957 fwriteint16_t(nsections
, ofile
); /* number of sections */
958 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
959 * section header table */
960 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
961 fwriteint32_t(0L, ofile
);
962 fwriteint32_t(0L, ofile
);
965 * Build the symbol table and relocation tables.
967 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
968 for (i
= 0; i
< nsects
; i
++)
970 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
974 * Now output the section header table.
977 elf_foffs
= 0x40 + 0x28 * nsections
;
978 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
981 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
984 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
987 /* The normal sections */
988 for (i
= 0; i
< nsects
; i
++) {
989 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
990 (sects
[i
]->type
== SHT_PROGBITS
?
991 sects
[i
]->data
: NULL
), true,
992 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
997 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
998 shstrtablen
, 0, 0, 1, 0);
1002 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1003 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1007 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1008 strslen
, 0, 0, 1, 0);
1011 /* The relocation sections */
1012 for (i
= 0; i
< nsects
; i
++)
1013 if (sects
[i
]->head
) {
1014 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1015 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1019 if (of_elf32
.current_dfmt
== &df_stabs
) {
1020 /* for debugging information, create the last three sections
1021 which are the .stab , .stabstr and .rel.stab sections respectively */
1023 /* this function call creates the stab sections in memory */
1026 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1027 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1028 stablen
, sec_stabstr
, 0, 4, 12);
1031 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1032 stabstrlen
, 0, 0, 4, 0);
1035 /* link -> symtable info -> section to refer to */
1036 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1037 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1040 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1041 /* for dwarf debugging information, create the ten dwarf sections */
1043 /* this function call creates the dwarf sections in memory */
1047 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1048 arangeslen
, 0, 0, 1, 0);
1051 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1052 arangesrellen
, sec_symtab
, sec_debug_aranges
,
1056 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1057 false, pubnameslen
, 0, 0, 1, 0);
1060 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1061 infolen
, 0, 0, 1, 0);
1064 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1065 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1068 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1069 abbrevlen
, 0, 0, 1, 0);
1072 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1073 linelen
, 0, 0, 1, 0);
1076 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1077 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1080 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1081 framelen
, 0, 0, 8, 0);
1084 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1085 loclen
, 0, 0, 1, 0);
1088 fwritezero(align
, ofile
);
1091 * Now output the sections.
1093 elf_write_sections();
1095 nasm_free(elf_sects
);
1099 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1101 struct SAA
*s
= saa_init(1L);
1103 uint8_t entry
[16], *p
;
1109 * First, an all-zeros entry, required by the ELF spec.
1111 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1116 * Next, an entry for the file name.
1119 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1120 WRITELONG(p
, 0); /* no value */
1121 WRITELONG(p
, 0); /* no size either */
1122 WRITESHORT(p
, STT_FILE
); /* type FILE */
1123 WRITESHORT(p
, SHN_ABS
);
1124 saa_wbytes(s
, entry
, 16L);
1129 * Now some standard symbols defining the segments, for relocation
1132 for (i
= 1; i
<= nsects
; i
++) {
1134 WRITELONG(p
, 0); /* no symbol name */
1135 WRITELONG(p
, 0); /* offset zero */
1136 WRITELONG(p
, 0); /* size zero */
1137 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1138 WRITESHORT(p
, i
); /* section id */
1139 saa_wbytes(s
, entry
, 16L);
1145 * Now the other local symbols.
1148 while ((sym
= saa_rstruct(syms
))) {
1149 if (sym
->type
& SYM_GLOBAL
)
1152 WRITELONG(p
, sym
->strpos
);
1153 WRITELONG(p
, sym
->symv
.key
);
1154 WRITELONG(p
, sym
->size
);
1155 WRITECHAR(p
, sym
->type
); /* type and binding */
1156 WRITECHAR(p
, sym
->other
); /* visibility */
1157 WRITESHORT(p
, sym
->section
);
1158 saa_wbytes(s
, entry
, 16L);
1163 * dwarf needs symbols for debug sections
1164 * which are relocation targets.
1166 //*** fix for 32 bit
1167 if (of_elf32
.current_dfmt
== &df_dwarf
) {
1168 dwarf_infosym
= *local
;
1170 WRITELONG(p
, 0); /* no symbol name */
1171 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1172 WRITELONG(p
, (uint32_t) 0); /* size zero */
1173 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1174 WRITESHORT(p
, sec_debug_info
); /* section id */
1175 saa_wbytes(s
, entry
, 16L);
1178 dwarf_abbrevsym
= *local
;
1180 WRITELONG(p
, 0); /* no symbol name */
1181 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1182 WRITELONG(p
, (uint32_t) 0); /* size zero */
1183 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1184 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1185 saa_wbytes(s
, entry
, 16L);
1188 dwarf_linesym
= *local
;
1190 WRITELONG(p
, 0); /* no symbol name */
1191 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1192 WRITELONG(p
, (uint32_t) 0); /* size zero */
1193 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1194 WRITESHORT(p
, sec_debug_line
); /* section id */
1195 saa_wbytes(s
, entry
, 16L);
1201 * Now the global symbols.
1204 while ((sym
= saa_rstruct(syms
))) {
1205 if (!(sym
->type
& SYM_GLOBAL
))
1208 WRITELONG(p
, sym
->strpos
);
1209 WRITELONG(p
, sym
->symv
.key
);
1210 WRITELONG(p
, sym
->size
);
1211 WRITECHAR(p
, sym
->type
); /* type and binding */
1212 WRITECHAR(p
, sym
->other
); /* visibility */
1213 WRITESHORT(p
, sym
->section
);
1214 saa_wbytes(s
, entry
, 16L);
1221 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1224 uint8_t *p
, entry
[8];
1225 int32_t global_offset
;
1234 * How to onvert from a global placeholder to a real symbol index;
1235 * the +2 refers to the two special entries, the null entry and
1236 * the filename entry.
1238 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1241 int32_t sym
= r
->symbol
;
1244 * Create a real symbol index; the +2 refers to the two special
1245 * entries, the null entry and the filename entry.
1247 if (sym
>= GLOBAL_TEMP_BASE
)
1248 sym
+= global_offset
;
1251 WRITELONG(p
, r
->address
);
1252 WRITELONG(p
, (sym
<< 8) + r
->type
);
1253 saa_wbytes(s
, entry
, 8L);
1262 static void elf_section_header(int name
, int type
, int flags
,
1263 void *data
, bool is_saa
, int32_t datalen
,
1264 int link
, int info
, int align
, int eltsize
)
1266 elf_sects
[elf_nsect
].data
= data
;
1267 elf_sects
[elf_nsect
].len
= datalen
;
1268 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1271 fwriteint32_t((int32_t)name
, ofile
);
1272 fwriteint32_t((int32_t)type
, ofile
);
1273 fwriteint32_t((int32_t)flags
, ofile
);
1274 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1275 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1276 fwriteint32_t(datalen
, ofile
);
1278 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1279 fwriteint32_t((int32_t)link
, ofile
);
1280 fwriteint32_t((int32_t)info
, ofile
);
1281 fwriteint32_t((int32_t)align
, ofile
);
1282 fwriteint32_t((int32_t)eltsize
, ofile
);
1285 static void elf_write_sections(void)
1288 for (i
= 0; i
< elf_nsect
; i
++)
1289 if (elf_sects
[i
].data
) {
1290 int32_t len
= elf_sects
[i
].len
;
1291 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1292 int32_t align
= reallen
- len
;
1293 if (elf_sects
[i
].is_saa
)
1294 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1296 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1297 fwritezero(align
, ofile
);
1301 static void elf_sect_write(struct Section
*sect
,
1302 const uint8_t *data
, uint32_t len
)
1304 saa_wbytes(sect
->data
, data
, len
);
1308 static void elf_segalign(int32_t seg
, int value
)
1310 struct Section
*s
= NULL
;
1313 for (i
= 0; i
< nsects
; i
++) {
1314 if (sects
[i
]->index
== seg
) {
1319 if (!s
|| !is_power2(value
))
1322 if ((unsigned int)value
> s
->align
)
1326 static int32_t elf_segbase(int32_t segment
)
1331 static int elf_directive(enum directives directive
, char *value
, int pass
)
1337 switch (directive
) {
1340 return 1; /* ignore in pass 2 */
1342 n
= readnum(value
, &err
);
1344 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1347 if (n
< 0 || n
> 255) {
1348 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1354 if ((p
= strchr(value
,',')) == NULL
)
1357 n
= readnum(p
+1, &err
);
1358 if (err
|| n
< 0 || n
> 255) {
1359 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1371 static void elf_filename(char *inname
, char *outname
)
1373 strcpy(elf_module
, inname
);
1374 standard_extension(inname
, outname
, ".o");
1377 extern macros_t elf_stdmac
[];
1379 static int elf_set_info(enum geninfo type
, char **val
)
1385 static struct dfmt df_dwarf
= {
1386 "ELF32 (i386) dwarf debug format for Linux/Unix",
1396 static struct dfmt df_stabs
= {
1397 "ELF32 (i386) stabs debug format for Linux/Unix",
1408 struct dfmt
*elf32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1410 struct ofmt of_elf32
= {
1411 "ELF32 (i386) object files (e.g. Linux)",
1429 /* again, the stabs debugging stuff (code) */
1431 static void stabs32_linenum(const char *filename
, int32_t linenumber
,
1436 if (!stabs_filename
) {
1437 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1438 strcpy(stabs_filename
, filename
);
1440 if (strcmp(stabs_filename
, filename
)) {
1442 * yep, a memory leak...this program is one-shot anyway, so who cares...
1443 * in fact, this leak comes in quite handy to maintain a list of files
1444 * encountered so far in the symbol lines...
1447 /* why not nasm_free(stabs_filename); we're done with the old one */
1449 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1450 strcpy(stabs_filename
, filename
);
1454 currentline
= linenumber
;
1457 static void debug32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1467 static void debug32_directive(const char *directive
, const char *params
)
1473 static void debug32_typevalue(int32_t type
)
1475 int32_t stype
, ssize
;
1476 switch (TYM_TYPE(type
)) {
1519 stype
= STT_SECTION
;
1534 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1535 lastsym
->size
= ssize
;
1536 lastsym
->type
= stype
;
1540 static void stabs32_output(int type
, void *param
)
1542 struct symlininfo
*s
;
1543 struct linelist
*el
;
1544 if (type
== TY_STABSSYMLIN
) {
1545 if (debug_immcall
) {
1546 s
= (struct symlininfo
*)param
;
1547 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1548 return; /* line info is only collected for executable sections */
1550 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1551 el
->info
.offset
= s
->offset
;
1552 el
->info
.section
= s
->section
;
1553 el
->info
.name
= s
->name
;
1554 el
->line
= currentline
;
1555 el
->filename
= stabs_filename
;
1558 stabslines
->last
->next
= el
;
1559 stabslines
->last
= el
;
1562 stabslines
->last
= el
;
1569 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1571 static void stabs32_generate(void)
1573 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1574 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1578 struct linelist
*ptr
;
1582 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1583 for (i
= 0; i
< numlinestabs
; i
++)
1587 if (numfiles
== 0) {
1588 allfiles
[0] = ptr
->filename
;
1591 for (i
= 0; i
< numfiles
; i
++) {
1592 if (!strcmp(allfiles
[i
], ptr
->filename
))
1595 if (i
>= numfiles
) {
1596 allfiles
[i
] = ptr
->filename
;
1603 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1604 for (i
= 0; i
< numfiles
; i
++) {
1605 fileidx
[i
] = strsize
;
1606 strsize
+= strlen(allfiles
[i
]) + 1;
1609 for (i
= 0; i
< numfiles
; i
++) {
1610 if (!strcmp(allfiles
[i
], elf_module
)) {
1617 * worst case size of the stab buffer would be:
1618 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1619 * plus one "ending" entry
1621 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1622 sizeof(struct stabentry
));
1623 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1624 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1627 for (i
= 0; i
< numfiles
; i
++)
1628 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1631 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1639 * this is the first stab, its strx points to the filename of the
1640 * the source-file, the n_desc field should be set to the number
1641 * of remaining stabs
1643 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1645 /* this is the stab for the main source file */
1646 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1648 /* relocation table entry */
1651 * Since the symbol table has two entries before
1652 * the section symbols, the index in the info.section
1653 * member must be adjusted by adding 2
1656 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1657 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1660 currfile
= mainfileindex
;
1664 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1665 /* oops file has changed... */
1666 for (i
= 0; i
< numfiles
; i
++)
1667 if (!strcmp(allfiles
[i
], ptr
->filename
))
1670 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1674 /* relocation table entry */
1675 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1676 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1679 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1682 /* relocation table entry */
1684 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1685 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1691 /* this is an "ending" token */
1692 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1695 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1697 nasm_free(allfiles
);
1700 stablen
= (sptr
- sbuf
);
1701 stabrellen
= (rptr
- rbuf
);
1707 static void stabs32_cleanup(void)
1709 struct linelist
*ptr
, *del
;
1721 nasm_free(stabrelbuf
);
1722 nasm_free(stabstrbuf
);
1725 /* dwarf routines */
1727 static void dwarf32_init(void)
1729 ndebugs
= 3; /* 3 debug symbols */
1732 static void dwarf32_linenum(const char *filename
, int32_t linenumber
,
1736 dwarf32_findfile(filename
);
1738 currentline
= linenumber
;
1741 /* called from elf_out with type == TY_DEBUGSYMLIN */
1742 static void dwarf32_output(int type
, void *param
)
1744 int ln
, aa
, inx
, maxln
, soc
;
1745 struct symlininfo
*s
;
1750 s
= (struct symlininfo
*)param
;
1752 /* line number info is only gathered for executable sections */
1753 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1756 /* Check if section index has changed */
1757 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1758 dwarf32_findsect(s
->section
);
1760 /* do nothing unless line or file has changed */
1764 ln
= currentline
- dwarf_csect
->line
;
1765 aa
= s
->offset
- dwarf_csect
->offset
;
1766 inx
= dwarf_clist
->line
;
1767 plinep
= dwarf_csect
->psaa
;
1768 /* check for file change */
1769 if (!(inx
== dwarf_csect
->file
)) {
1770 saa_write8(plinep
,DW_LNS_set_file
);
1771 saa_write8(plinep
,inx
);
1772 dwarf_csect
->file
= inx
;
1774 /* check for line change */
1776 /* test if in range of special op code */
1777 maxln
= line_base
+ line_range
;
1778 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1779 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1780 saa_write8(plinep
,soc
);
1782 saa_write8(plinep
,DW_LNS_advance_line
);
1783 saa_wleb128s(plinep
,ln
);
1785 saa_write8(plinep
,DW_LNS_advance_pc
);
1786 saa_wleb128u(plinep
,aa
);
1789 dwarf_csect
->line
= currentline
;
1790 dwarf_csect
->offset
= s
->offset
;
1793 /* show change handled */
1798 static void dwarf32_generate(void)
1802 struct linelist
*ftentry
;
1803 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1804 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1805 struct sectlist
*psect
;
1806 size_t saalen
, linepoff
, totlen
, highaddr
;
1808 /* write epilogues for each line program range */
1809 /* and build aranges section */
1810 paranges
= saa_init(1L);
1811 parangesrel
= saa_init(1L);
1812 saa_write16(paranges
,2); /* dwarf version */
1813 saa_write32(parangesrel
, paranges
->datalen
+4);
1814 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
1815 saa_write32(parangesrel
, 0);
1816 saa_write32(paranges
,0); /* offset into info */
1817 saa_write8(paranges
,4); /* pointer size */
1818 saa_write8(paranges
,0); /* not segmented */
1819 saa_write32(paranges
,0); /* padding */
1820 /* iterate though sectlist entries */
1821 psect
= dwarf_fsect
;
1824 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
1825 plinep
= psect
->psaa
;
1826 /* Line Number Program Epilogue */
1827 saa_write8(plinep
,2); /* std op 2 */
1828 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1829 saa_write8(plinep
,DW_LNS_extended_op
);
1830 saa_write8(plinep
,1); /* operand length */
1831 saa_write8(plinep
,DW_LNE_end_sequence
);
1832 totlen
+= plinep
->datalen
;
1833 /* range table relocation entry */
1834 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1835 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
1836 saa_write32(parangesrel
, (uint32_t) 0);
1837 /* range table entry */
1838 saa_write32(paranges
,0x0000); /* range start */
1839 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1840 highaddr
+= sects
[psect
->section
]->len
;
1841 /* done with this entry */
1842 psect
= psect
->next
;
1844 saa_write32(paranges
,0); /* null address */
1845 saa_write32(paranges
,0); /* null length */
1846 saalen
= paranges
->datalen
;
1847 arangeslen
= saalen
+ 4;
1848 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1849 WRITELONG(pbuf
,saalen
); /* initial length */
1850 saa_rnbytes(paranges
, pbuf
, saalen
);
1853 /* build rela.aranges section */
1854 arangesrellen
= saalen
= parangesrel
->datalen
;
1855 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1856 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1857 saa_free(parangesrel
);
1859 /* build pubnames section */
1860 ppubnames
= saa_init(1L);
1861 saa_write16(ppubnames
,3); /* dwarf version */
1862 saa_write32(ppubnames
,0); /* offset into info */
1863 saa_write32(ppubnames
,0); /* space used in info */
1864 saa_write32(ppubnames
,0); /* end of list */
1865 saalen
= ppubnames
->datalen
;
1866 pubnameslen
= saalen
+ 4;
1867 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1868 WRITELONG(pbuf
,saalen
); /* initial length */
1869 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1870 saa_free(ppubnames
);
1872 /* build info section */
1873 pinfo
= saa_init(1L);
1874 pinforel
= saa_init(1L);
1875 saa_write16(pinfo
,2); /* dwarf version */
1876 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1877 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
1878 saa_write32(pinforel
, 0);
1879 saa_write32(pinfo
,0); /* offset into abbrev */
1880 saa_write8(pinfo
,4); /* pointer size */
1881 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1882 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1883 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1884 saa_write32(pinforel
, 0);
1885 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1886 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1887 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1888 saa_write32(pinforel
, 0);
1889 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1890 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1891 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
1892 saa_write32(pinforel
, 0);
1893 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1894 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1895 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1896 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1897 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1898 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1899 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1900 saa_write32(pinforel
, 0);
1901 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1902 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1903 saa_write8(pinfo
,0); /* end of entries */
1904 saalen
= pinfo
->datalen
;
1905 infolen
= saalen
+ 4;
1906 infobuf
= pbuf
= nasm_malloc(infolen
);
1907 WRITELONG(pbuf
,saalen
); /* initial length */
1908 saa_rnbytes(pinfo
, pbuf
, saalen
);
1911 /* build rela.info section */
1912 inforellen
= saalen
= pinforel
->datalen
;
1913 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1914 saa_rnbytes(pinforel
, pbuf
, saalen
);
1917 /* build abbrev section */
1918 pabbrev
= saa_init(1L);
1919 saa_write8(pabbrev
,1); /* entry number LEB128u */
1920 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1921 saa_write8(pabbrev
,1); /* has children */
1922 /* the following attributes and forms are all LEB128u values */
1923 saa_write8(pabbrev
,DW_AT_low_pc
);
1924 saa_write8(pabbrev
,DW_FORM_addr
);
1925 saa_write8(pabbrev
,DW_AT_high_pc
);
1926 saa_write8(pabbrev
,DW_FORM_addr
);
1927 saa_write8(pabbrev
,DW_AT_stmt_list
);
1928 saa_write8(pabbrev
,DW_FORM_data4
);
1929 saa_write8(pabbrev
,DW_AT_name
);
1930 saa_write8(pabbrev
,DW_FORM_string
);
1931 saa_write8(pabbrev
,DW_AT_producer
);
1932 saa_write8(pabbrev
,DW_FORM_string
);
1933 saa_write8(pabbrev
,DW_AT_language
);
1934 saa_write8(pabbrev
,DW_FORM_data2
);
1935 saa_write16(pabbrev
,0); /* end of entry */
1936 /* LEB128u usage same as above */
1937 saa_write8(pabbrev
,2); /* entry number */
1938 saa_write8(pabbrev
,DW_TAG_subprogram
);
1939 saa_write8(pabbrev
,0); /* no children */
1940 saa_write8(pabbrev
,DW_AT_low_pc
);
1941 saa_write8(pabbrev
,DW_FORM_addr
);
1942 saa_write8(pabbrev
,DW_AT_frame_base
);
1943 saa_write8(pabbrev
,DW_FORM_data4
);
1944 saa_write16(pabbrev
,0); /* end of entry */
1945 abbrevlen
= saalen
= pabbrev
->datalen
;
1946 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
1947 saa_rnbytes(pabbrev
, pbuf
, saalen
);
1950 /* build line section */
1952 plines
= saa_init(1L);
1953 saa_write8(plines
,1); /* Minimum Instruction Length */
1954 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
1955 saa_write8(plines
,line_base
); /* Line Base */
1956 saa_write8(plines
,line_range
); /* Line Range */
1957 saa_write8(plines
,opcode_base
); /* Opcode Base */
1958 /* standard opcode lengths (# of LEB128u operands) */
1959 saa_write8(plines
,0); /* Std opcode 1 length */
1960 saa_write8(plines
,1); /* Std opcode 2 length */
1961 saa_write8(plines
,1); /* Std opcode 3 length */
1962 saa_write8(plines
,1); /* Std opcode 4 length */
1963 saa_write8(plines
,1); /* Std opcode 5 length */
1964 saa_write8(plines
,0); /* Std opcode 6 length */
1965 saa_write8(plines
,0); /* Std opcode 7 length */
1966 saa_write8(plines
,0); /* Std opcode 8 length */
1967 saa_write8(plines
,1); /* Std opcode 9 length */
1968 saa_write8(plines
,0); /* Std opcode 10 length */
1969 saa_write8(plines
,0); /* Std opcode 11 length */
1970 saa_write8(plines
,1); /* Std opcode 12 length */
1971 /* Directory Table */
1972 saa_write8(plines
,0); /* End of table */
1973 /* File Name Table */
1974 ftentry
= dwarf_flist
;
1975 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
1976 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
1977 saa_write8(plines
,0); /* directory LEB128u */
1978 saa_write8(plines
,0); /* time LEB128u */
1979 saa_write8(plines
,0); /* size LEB128u */
1980 ftentry
= ftentry
->next
;
1982 saa_write8(plines
,0); /* End of table */
1983 linepoff
= plines
->datalen
;
1984 linelen
= linepoff
+ totlen
+ 10;
1985 linebuf
= pbuf
= nasm_malloc(linelen
);
1986 WRITELONG(pbuf
,linelen
-4); /* initial length */
1987 WRITESHORT(pbuf
,3); /* dwarf version */
1988 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
1989 /* write line header */
1991 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
1994 /* concatonate line program ranges */
1996 plinesrel
= saa_init(1L);
1997 psect
= dwarf_fsect
;
1998 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
1999 saa_write32(plinesrel
, linepoff
);
2000 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2001 saa_write32(plinesrel
, (uint32_t) 0);
2002 plinep
= psect
->psaa
;
2003 saalen
= plinep
->datalen
;
2004 saa_rnbytes(plinep
, pbuf
, saalen
);
2008 /* done with this entry */
2009 psect
= psect
->next
;
2013 /* build rela.lines section */
2014 linerellen
=saalen
= plinesrel
->datalen
;
2015 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2016 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2017 saa_free(plinesrel
);
2019 /* build frame section */
2021 framebuf
= pbuf
= nasm_malloc(framelen
);
2022 WRITELONG(pbuf
,framelen
-4); /* initial length */
2024 /* build loc section */
2026 locbuf
= pbuf
= nasm_malloc(loclen
);
2027 WRITELONG(pbuf
,0); /* null beginning offset */
2028 WRITELONG(pbuf
,0); /* null ending offset */
2031 static void dwarf32_cleanup(void)
2033 nasm_free(arangesbuf
);
2034 nasm_free(arangesrelbuf
);
2035 nasm_free(pubnamesbuf
);
2037 nasm_free(inforelbuf
);
2038 nasm_free(abbrevbuf
);
2040 nasm_free(linerelbuf
);
2041 nasm_free(framebuf
);
2045 static void dwarf32_findfile(const char * fname
)
2048 struct linelist
*match
;
2050 /* return if fname is current file name */
2051 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2054 /* search for match */
2057 match
= dwarf_flist
;
2058 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2059 if (!(strcmp(fname
, match
->filename
))) {
2060 dwarf_clist
= match
;
2066 /* add file name to end of list */
2067 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2069 dwarf_clist
->line
= dwarf_numfiles
;
2070 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2071 strcpy(dwarf_clist
->filename
,fname
);
2072 dwarf_clist
->next
= 0;
2073 if (!dwarf_flist
) { /* if first entry */
2074 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2075 dwarf_clist
->last
= 0;
2076 } else { /* chain to previous entry */
2077 dwarf_elist
->next
= dwarf_clist
;
2078 dwarf_elist
= dwarf_clist
;
2082 static void dwarf32_findsect(const int index
)
2085 struct sectlist
*match
;
2088 /* return if index is current section index */
2089 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2092 /* search for match */
2095 match
= dwarf_fsect
;
2096 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2097 if ((match
->section
== index
)) {
2098 dwarf_csect
= match
;
2101 match
= match
->next
;
2105 /* add entry to end of list */
2106 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2108 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2109 dwarf_csect
->line
= 1;
2110 dwarf_csect
->offset
= 0;
2111 dwarf_csect
->file
= 1;
2112 dwarf_csect
->section
= index
;
2113 dwarf_csect
->next
= 0;
2114 /* set relocatable address at start of line program */
2115 saa_write8(plinep
,DW_LNS_extended_op
);
2116 saa_write8(plinep
,5); /* operand length */
2117 saa_write8(plinep
,DW_LNE_set_address
);
2118 saa_write32(plinep
,0); /* Start Address */
2120 if (!dwarf_fsect
) { /* if first entry */
2121 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2122 dwarf_csect
->last
= 0;
2123 } else { /* chain to previous entry */
2124 dwarf_esect
->next
= dwarf_csect
;
2125 dwarf_esect
= dwarf_csect
;