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
;
125 extern struct ofmt of_elf
;
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 *);
159 int section
; /* section index */
160 char *name
; /* shallow-copied pointer of section name */
164 struct symlininfo info
;
167 struct linelist
*next
;
168 struct linelist
*last
;
177 struct sectlist
*next
;
178 struct sectlist
*last
;
181 /* common debug variables */
182 static int currentline
= 1;
183 static int debug_immcall
= 0;
185 /* stabs debug variables */
186 static struct linelist
*stabslines
= 0;
187 static int numlinestabs
= 0;
188 static char *stabs_filename
= 0;
189 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
190 static int stablen
, stabstrlen
, stabrellen
;
192 /* dwarf debug variables */
193 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
194 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
195 static int dwarf_numfiles
= 0, dwarf_nsections
;
196 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
197 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
198 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
199 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
200 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
201 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
203 static struct dfmt df_dwarf
;
204 static struct dfmt df_stabs
;
205 static struct Symbol
*lastsym
;
207 /* common debugging routines */
208 static void debug32_typevalue(int32_t);
209 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
210 static void debug32_directive(const char *, const char *);
212 /* stabs debugging routines */
213 static void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
214 static void stabs32_output(int, void *);
215 static void stabs32_generate(void);
216 static void stabs32_cleanup(void);
218 /* dwarf debugging routines */
219 static void dwarf32_init(void);
220 static void dwarf32_linenum(const char *filename
, int32_t linenumber
, int32_t);
221 static void dwarf32_output(int, void *);
222 static void dwarf32_generate(void);
223 static void dwarf32_cleanup(void);
224 static void dwarf32_findfile(const char *);
225 static void dwarf32_findsect(const int);
228 * Special NASM section numbers which are used to define ELF special
229 * symbols, which can be used with WRT to provide PIC and TLS
232 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
233 static int32_t elf_got_sect
, elf_plt_sect
;
234 static int32_t elf_sym_sect
, elf_tlsie_sect
;
236 static void elf_init(void)
239 nsects
= sectlen
= 0;
240 syms
= saa_init((int32_t)sizeof(struct Symbol
));
241 nlocals
= nglobs
= ndebugs
= 0;
244 saa_wbytes(strs
, "\0", 1L);
245 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
246 strslen
= 2 + strlen(elf_module
);
248 shstrtablen
= shstrtabsize
= 0;;
249 add_sectname("", "");
253 elf_gotpc_sect
= seg_alloc();
254 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
255 elf_gotoff_sect
= seg_alloc();
256 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
257 elf_got_sect
= seg_alloc();
258 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
259 elf_plt_sect
= seg_alloc();
260 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
261 elf_sym_sect
= seg_alloc();
262 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
263 elf_tlsie_sect
= seg_alloc();
264 define_label("..tlsie", elf_tlsie_sect
+ 1, 0L, NULL
, false, false);
266 def_seg
= seg_alloc();
269 static void elf_init_hack(void)
271 of_elf32
.current_dfmt
= of_elf
.current_dfmt
; /* Sync debugging format */
275 static void elf_cleanup(int debuginfo
)
283 for (i
= 0; i
< nsects
; i
++) {
284 if (sects
[i
]->type
!= SHT_NOBITS
)
285 saa_free(sects
[i
]->data
);
287 saa_free(sects
[i
]->rel
);
288 while (sects
[i
]->head
) {
290 sects
[i
]->head
= sects
[i
]->head
->next
;
298 if (of_elf32
.current_dfmt
) {
299 of_elf32
.current_dfmt
->cleanup();
303 static void add_sectname(char *firsthalf
, char *secondhalf
)
305 int len
= strlen(firsthalf
) + strlen(secondhalf
);
306 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
307 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
308 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
309 strcat(shstrtab
+ shstrtablen
, secondhalf
);
310 shstrtablen
+= len
+ 1;
313 static int elf_make_section(char *name
, int type
, int flags
, int align
)
317 s
= nasm_malloc(sizeof(*s
));
319 if (type
!= SHT_NOBITS
)
320 s
->data
= saa_init(1L);
323 s
->len
= s
->size
= 0;
325 if (!strcmp(name
, ".text"))
328 s
->index
= seg_alloc();
329 add_sectname("", name
);
330 s
->name
= nasm_malloc(1 + strlen(name
));
331 strcpy(s
->name
, name
);
337 if (nsects
>= sectlen
)
338 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
344 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
347 uint32_t flags
, flags_and
, flags_or
;
352 * Default is 32 bits.
359 p
= nasm_skip_word(name
);
362 flags_and
= flags_or
= type
= align
= 0;
364 section_attrib(name
, p
, pass
, &flags_and
,
365 &flags_or
, &align
, &type
);
367 if (!strcmp(name
, ".shstrtab") ||
368 !strcmp(name
, ".symtab") ||
369 !strcmp(name
, ".strtab")) {
370 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
375 for (i
= 0; i
< nsects
; i
++)
376 if (!strcmp(name
, sects
[i
]->name
))
379 const struct elf_known_section
*ks
= elf_known_sections
;
382 if (!strcmp(name
, ks
->name
))
387 type
= type
? type
: ks
->type
;
388 align
= align
? align
: ks
->align
;
389 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
391 i
= elf_make_section(name
, type
, flags
, align
);
392 } else if (pass
== 1) {
393 if ((type
&& sects
[i
]->type
!= type
)
394 || (align
&& sects
[i
]->align
!= align
)
395 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
396 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
397 " redeclaration of section `%s'", name
);
400 return sects
[i
]->index
;
403 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
404 int is_global
, char *special
)
408 bool special_used
= false;
410 #if defined(DEBUG) && DEBUG>2
411 nasm_error(ERR_DEBUG
,
412 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
413 name
, segment
, offset
, is_global
, special
);
415 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
417 * This is a NASM special symbol. We never allow it into
418 * the ELF symbol table, even if it's a valid one. If it
419 * _isn't_ a valid one, we should barf immediately.
421 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
422 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
423 strcmp(name
, "..sym") && strcmp(name
, "..tlsie"))
424 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
428 if (is_global
== 3) {
431 * Fix up a forward-reference symbol size from the first
434 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
435 if (!strcmp((*s
)->name
, name
)) {
436 struct tokenval tokval
;
438 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
442 tokval
.t_type
= TOKEN_INVALID
;
443 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
446 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
447 " expression as symbol size");
449 (*s
)->size
= reloc_value(e
);
453 * Remove it from the list of unresolved sizes.
455 nasm_free((*s
)->name
);
459 return; /* it wasn't an important one */
462 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
463 strslen
+= 1 + strlen(name
);
465 lastsym
= sym
= saa_wstruct(syms
);
467 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
470 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
471 sym
->other
= STV_DEFAULT
;
473 if (segment
== NO_SEG
)
474 sym
->section
= SHN_ABS
;
477 sym
->section
= SHN_UNDEF
;
478 if (segment
== def_seg
) {
479 /* we have to be sure at least text section is there */
481 if (segment
!= elf_section_names(".text", 2, &tempint
))
482 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
484 for (i
= 0; i
< nsects
; i
++) {
485 if (segment
== sects
[i
]->index
) {
486 sym
->section
= i
+ 1;
492 if (is_global
== 2) {
495 sym
->section
= SHN_COMMON
;
497 * We have a common variable. Check the special text to see
498 * if it's a valid number and power of two; if so, store it
499 * as the alignment for the common variable.
503 sym
->symv
.key
= readnum(special
, &err
);
505 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
506 " valid number", special
);
507 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
508 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
509 " power of two", special
);
513 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
515 if (sym
->type
== SYM_GLOBAL
) {
517 * If sym->section == SHN_ABS, then the first line of the
518 * else section would cause a core dump, because its a reference
519 * beyond the end of the section array.
520 * This behaviour is exhibited by this code:
523 * To avoid such a crash, such requests are silently discarded.
524 * This may not be the best solution.
526 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
527 bsym
= raa_write(bsym
, segment
, nglobs
);
528 } else if (sym
->section
!= SHN_ABS
) {
530 * This is a global symbol; so we must add it to the rbtree
531 * of global symbols in its section.
533 * In addition, we check the special text for symbol
534 * type and size information.
536 sects
[sym
->section
-1]->gsyms
=
537 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
540 int n
= strcspn(special
, " \t");
542 if (!nasm_strnicmp(special
, "function", n
))
543 sym
->type
|= STT_FUNC
;
544 else if (!nasm_strnicmp(special
, "data", n
) ||
545 !nasm_strnicmp(special
, "object", n
))
546 sym
->type
|= STT_OBJECT
;
547 else if (!nasm_strnicmp(special
, "notype", n
))
548 sym
->type
|= STT_NOTYPE
;
550 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
554 special
= nasm_skip_spaces(special
);
556 n
= strcspn(special
, " \t");
557 if (!nasm_strnicmp(special
, "default", n
))
558 sym
->other
= STV_DEFAULT
;
559 else if (!nasm_strnicmp(special
, "internal", n
))
560 sym
->other
= STV_INTERNAL
;
561 else if (!nasm_strnicmp(special
, "hidden", n
))
562 sym
->other
= STV_HIDDEN
;
563 else if (!nasm_strnicmp(special
, "protected", n
))
564 sym
->other
= STV_PROTECTED
;
571 struct tokenval tokval
;
574 char *saveme
= stdscan_get();
576 while (special
[n
] && nasm_isspace(special
[n
]))
579 * We have a size expression; attempt to
583 stdscan_set(special
+ n
);
584 tokval
.t_type
= TOKEN_INVALID
;
585 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
590 sym
->name
= nasm_strdup(name
);
593 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
594 " expression as symbol size");
596 sym
->size
= reloc_value(e
);
603 * If TLS segment, mark symbol accordingly.
605 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
607 sym
->type
|= STT_TLS
;
610 sym
->globnum
= nglobs
;
615 if (special
&& !special_used
)
616 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
619 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
623 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
624 sect
->tail
= &r
->next
;
627 r
->address
= sect
->len
;
628 if (segment
== NO_SEG
)
633 for (i
= 0; i
< nsects
; i
++)
634 if (segment
== sects
[i
]->index
)
637 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
645 * This routine deals with ..got and ..sym relocations: the more
646 * complicated kinds. In shared-library writing, some relocations
647 * with respect to global symbols must refer to the precise symbol
648 * rather than referring to an offset from the base of the section
649 * _containing_ the symbol. Such relocations call to this routine,
650 * which searches the symbol list for the symbol in question.
652 * R_386_GOT32 references require the _exact_ symbol address to be
653 * used; R_386_32 references can be at an offset from the symbol.
654 * The boolean argument `exact' tells us this.
656 * Return value is the adjusted value of `addr', having become an
657 * offset from the symbol rather than the section. Should always be
658 * zero when returning from an exact call.
660 * Limitation: if you define two symbols at the same place,
661 * confusion will occur.
663 * Inefficiency: we search, currently, using a linked list which
664 * isn't even necessarily sorted.
666 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
667 int32_t segment
, uint32_t offset
,
668 int type
, bool exact
)
677 * First look up the segment/offset pair and find a global
678 * symbol corresponding to it. If it's not one of our segments,
679 * then it must be an external symbol, in which case we're fine
680 * doing a normal elf_add_reloc after first sanity-checking
681 * that the offset from the symbol is zero.
684 for (i
= 0; i
< nsects
; i
++)
685 if (segment
== sects
[i
]->index
) {
690 if (exact
&& offset
!= 0)
691 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
692 " for this reference");
694 elf_add_reloc(sect
, segment
, type
);
698 srb
= rb_search(s
->gsyms
, offset
);
699 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
700 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
701 " for this reference");
704 sym
= container_of(srb
, struct Symbol
, symv
);
706 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
707 sect
->tail
= &r
->next
;
710 r
->address
= sect
->len
;
711 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
716 return offset
- sym
->symv
.key
;
719 static void elf_out(int32_t segto
, const void *data
,
720 enum out_type type
, uint64_t size
,
721 int32_t segment
, int32_t wrt
)
725 uint8_t mydata
[4], *p
;
727 static struct symlininfo sinfo
;
730 * handle absolute-assembly (structure definitions)
732 if (segto
== NO_SEG
) {
733 if (type
!= OUT_RESERVE
)
734 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
740 for (i
= 0; i
< nsects
; i
++)
741 if (segto
== sects
[i
]->index
) {
746 int tempint
; /* ignored */
747 if (segto
!= elf_section_names(".text", 2, &tempint
))
748 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
750 s
= sects
[nsects
- 1];
755 /* again some stabs debugging stuff */
756 if (of_elf32
.current_dfmt
) {
757 sinfo
.offset
= s
->len
;
759 sinfo
.name
= s
->name
;
760 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
762 /* end of debugging stuff */
764 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
765 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
766 " BSS section `%s': ignored", s
->name
);
767 s
->len
+= realsize(type
, size
);
771 if (type
== OUT_RESERVE
) {
772 if (s
->type
== SHT_PROGBITS
) {
773 nasm_error(ERR_WARNING
, "uninitialized space declared in"
774 " non-BSS section `%s': zeroing", s
->name
);
775 elf_sect_write(s
, NULL
, size
);
778 } else if (type
== OUT_RAWDATA
) {
779 if (segment
!= NO_SEG
)
780 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
781 elf_sect_write(s
, data
, size
);
782 } else if (type
== OUT_ADDRESS
) {
784 addr
= *(int64_t *)data
;
785 if (segment
!= NO_SEG
) {
787 nasm_error(ERR_NONFATAL
, "ELF format does not support"
788 " segment base references");
793 elf_add_reloc(s
, segment
, R_386_16
);
795 elf_add_reloc(s
, segment
, R_386_32
);
797 } else if (wrt
== elf_gotpc_sect
+ 1) {
799 * The user will supply GOT relative to $$. ELF
800 * will let us have GOT relative to $. So we
801 * need to fix up the data item by $-$$.
804 elf_add_reloc(s
, segment
, R_386_GOTPC
);
805 } else if (wrt
== elf_gotoff_sect
+ 1) {
806 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
807 } else if (wrt
== elf_tlsie_sect
+ 1) {
808 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
810 } else if (wrt
== elf_got_sect
+ 1) {
811 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
813 } else if (wrt
== elf_sym_sect
+ 1) {
816 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
819 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
822 } else if (wrt
== elf_plt_sect
+ 1) {
823 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
824 "relative PLT references");
826 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
828 wrt
= NO_SEG
; /* we can at least _try_ to continue */
834 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
835 "16-bit relocations in ELF is a GNU extension");
838 if (size
!= 4 && segment
!= NO_SEG
) {
839 nasm_error(ERR_NONFATAL
,
840 "Unsupported non-32-bit ELF relocation");
844 elf_sect_write(s
, mydata
, size
);
845 } else if (type
== OUT_REL2ADR
) {
846 if (segment
== segto
)
847 nasm_error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
848 if (segment
!= NO_SEG
&& segment
% 2) {
849 nasm_error(ERR_NONFATAL
, "ELF format does not support"
850 " segment base references");
853 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
854 "16-bit relocations in ELF is a GNU extension");
855 elf_add_reloc(s
, segment
, R_386_PC16
);
857 nasm_error(ERR_NONFATAL
,
858 "Unsupported non-32-bit ELF relocation");
862 WRITESHORT(p
, *(int64_t *)data
- size
);
863 elf_sect_write(s
, mydata
, 2L);
864 } else if (type
== OUT_REL4ADR
) {
865 if (segment
== segto
)
866 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
867 if (segment
!= NO_SEG
&& segment
% 2) {
868 nasm_error(ERR_NONFATAL
, "ELF format does not support"
869 " segment base references");
872 elf_add_reloc(s
, segment
, R_386_PC32
);
873 } else if (wrt
== elf_plt_sect
+ 1) {
874 elf_add_reloc(s
, segment
, R_386_PLT32
);
875 } else if (wrt
== elf_gotpc_sect
+ 1 ||
876 wrt
== elf_gotoff_sect
+ 1 ||
877 wrt
== elf_got_sect
+ 1) {
878 nasm_error(ERR_NONFATAL
, "ELF format cannot produce PC-"
879 "relative GOT references");
881 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
883 wrt
= NO_SEG
; /* we can at least _try_ to continue */
887 WRITELONG(p
, *(int64_t *)data
- size
);
888 elf_sect_write(s
, mydata
, 4L);
892 static void elf_write(void)
899 int32_t symtablen
, symtablocal
;
902 * Work out how many sections we will have. We have SHN_UNDEF,
903 * then the flexible user sections, then the fixed sections
904 * `.shstrtab', `.symtab' and `.strtab', then optionally
905 * relocation sections for the user sections.
907 nsections
= sec_numspecial
+ 1;
908 if (of_elf32
.current_dfmt
== &df_stabs
)
910 else if (of_elf32
.current_dfmt
== &df_dwarf
)
913 add_sectname("", ".shstrtab");
914 add_sectname("", ".symtab");
915 add_sectname("", ".strtab");
916 for (i
= 0; i
< nsects
; i
++) {
917 nsections
++; /* for the section itself */
918 if (sects
[i
]->head
) {
919 nsections
++; /* for its relocations */
920 add_sectname(".rel", sects
[i
]->name
);
924 if (of_elf32
.current_dfmt
== &df_stabs
) {
925 /* in case the debug information is wanted, just add these three sections... */
926 add_sectname("", ".stab");
927 add_sectname("", ".stabstr");
928 add_sectname(".rel", ".stab");
929 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
930 /* the dwarf debug standard specifies the following ten sections,
931 not all of which are currently implemented,
932 although all of them are defined. */
933 add_sectname("", ".debug_aranges");
934 add_sectname(".rela", ".debug_aranges");
935 add_sectname("", ".debug_pubnames");
936 add_sectname("", ".debug_info");
937 add_sectname(".rela", ".debug_info");
938 add_sectname("", ".debug_abbrev");
939 add_sectname("", ".debug_line");
940 add_sectname(".rela", ".debug_line");
941 add_sectname("", ".debug_frame");
942 add_sectname("", ".debug_loc");
946 * Output the ELF header.
948 fwrite("\177ELF\1\1\1", 7, 1, ofile
);
949 fputc(elf_osabi
, ofile
);
950 fputc(elf_abiver
, ofile
);
951 fwritezero(7, ofile
);
952 fwriteint16_t(1, ofile
); /* ET_REL relocatable file */
953 fwriteint16_t(3, ofile
); /* EM_386 processor ID */
954 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
955 fwriteint32_t(0L, ofile
); /* no entry point */
956 fwriteint32_t(0L, ofile
); /* no program header table */
957 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
958 * ELF header plus alignment */
959 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
960 fwriteint16_t(0x34, ofile
); /* size of ELF header */
961 fwriteint16_t(0, ofile
); /* no program header table, again */
962 fwriteint16_t(0, ofile
); /* still no program header table */
963 fwriteint16_t(0x28, ofile
); /* size of section header */
964 fwriteint16_t(nsections
, ofile
); /* number of sections */
965 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
966 * section header table */
967 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
968 fwriteint32_t(0L, ofile
);
969 fwriteint32_t(0L, ofile
);
972 * Build the symbol table and relocation tables.
974 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
975 for (i
= 0; i
< nsects
; i
++)
977 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
981 * Now output the section header table.
984 elf_foffs
= 0x40 + 0x28 * nsections
;
985 align
= ALIGN(elf_foffs
, SEG_ALIGN
) - elf_foffs
;
988 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
991 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
994 /* The normal sections */
995 for (i
= 0; i
< nsects
; i
++) {
996 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
997 (sects
[i
]->type
== SHT_PROGBITS
?
998 sects
[i
]->data
: NULL
), true,
999 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1004 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1005 shstrtablen
, 0, 0, 1, 0);
1009 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1010 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1014 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1015 strslen
, 0, 0, 1, 0);
1018 /* The relocation sections */
1019 for (i
= 0; i
< nsects
; i
++)
1020 if (sects
[i
]->head
) {
1021 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1022 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1026 if (of_elf32
.current_dfmt
== &df_stabs
) {
1027 /* for debugging information, create the last three sections
1028 which are the .stab , .stabstr and .rel.stab sections respectively */
1030 /* this function call creates the stab sections in memory */
1033 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1034 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1035 stablen
, sec_stabstr
, 0, 4, 12);
1038 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1039 stabstrlen
, 0, 0, 4, 0);
1042 /* link -> symtable info -> section to refer to */
1043 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1044 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1047 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1048 /* for dwarf debugging information, create the ten dwarf sections */
1050 /* this function call creates the dwarf sections in memory */
1054 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1055 arangeslen
, 0, 0, 1, 0);
1058 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1059 arangesrellen
, sec_symtab
, sec_debug_aranges
,
1063 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1064 false, pubnameslen
, 0, 0, 1, 0);
1067 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1068 infolen
, 0, 0, 1, 0);
1071 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1072 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1075 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1076 abbrevlen
, 0, 0, 1, 0);
1079 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1080 linelen
, 0, 0, 1, 0);
1083 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1084 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1087 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1088 framelen
, 0, 0, 8, 0);
1091 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1092 loclen
, 0, 0, 1, 0);
1095 fwritezero(align
, ofile
);
1098 * Now output the sections.
1100 elf_write_sections();
1102 nasm_free(elf_sects
);
1106 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1108 struct SAA
*s
= saa_init(1L);
1110 uint8_t entry
[16], *p
;
1116 * First, an all-zeros entry, required by the ELF spec.
1118 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1123 * Next, an entry for the file name.
1126 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1127 WRITELONG(p
, 0); /* no value */
1128 WRITELONG(p
, 0); /* no size either */
1129 WRITESHORT(p
, STT_FILE
); /* type FILE */
1130 WRITESHORT(p
, SHN_ABS
);
1131 saa_wbytes(s
, entry
, 16L);
1136 * Now some standard symbols defining the segments, for relocation
1139 for (i
= 1; i
<= nsects
; i
++) {
1141 WRITELONG(p
, 0); /* no symbol name */
1142 WRITELONG(p
, 0); /* offset zero */
1143 WRITELONG(p
, 0); /* size zero */
1144 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1145 WRITESHORT(p
, i
); /* section id */
1146 saa_wbytes(s
, entry
, 16L);
1152 * Now the other local symbols.
1155 while ((sym
= saa_rstruct(syms
))) {
1156 if (sym
->type
& SYM_GLOBAL
)
1159 WRITELONG(p
, sym
->strpos
);
1160 WRITELONG(p
, sym
->symv
.key
);
1161 WRITELONG(p
, sym
->size
);
1162 WRITECHAR(p
, sym
->type
); /* type and binding */
1163 WRITECHAR(p
, sym
->other
); /* visibility */
1164 WRITESHORT(p
, sym
->section
);
1165 saa_wbytes(s
, entry
, 16L);
1170 * dwarf needs symbols for debug sections
1171 * which are relocation targets.
1173 //*** fix for 32 bit
1174 if (of_elf32
.current_dfmt
== &df_dwarf
) {
1175 dwarf_infosym
= *local
;
1177 WRITELONG(p
, 0); /* no symbol name */
1178 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1179 WRITELONG(p
, (uint32_t) 0); /* size zero */
1180 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1181 WRITESHORT(p
, sec_debug_info
); /* section id */
1182 saa_wbytes(s
, entry
, 16L);
1185 dwarf_abbrevsym
= *local
;
1187 WRITELONG(p
, 0); /* no symbol name */
1188 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1189 WRITELONG(p
, (uint32_t) 0); /* size zero */
1190 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1191 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1192 saa_wbytes(s
, entry
, 16L);
1195 dwarf_linesym
= *local
;
1197 WRITELONG(p
, 0); /* no symbol name */
1198 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1199 WRITELONG(p
, (uint32_t) 0); /* size zero */
1200 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1201 WRITESHORT(p
, sec_debug_line
); /* section id */
1202 saa_wbytes(s
, entry
, 16L);
1208 * Now the global symbols.
1211 while ((sym
= saa_rstruct(syms
))) {
1212 if (!(sym
->type
& SYM_GLOBAL
))
1215 WRITELONG(p
, sym
->strpos
);
1216 WRITELONG(p
, sym
->symv
.key
);
1217 WRITELONG(p
, sym
->size
);
1218 WRITECHAR(p
, sym
->type
); /* type and binding */
1219 WRITECHAR(p
, sym
->other
); /* visibility */
1220 WRITESHORT(p
, sym
->section
);
1221 saa_wbytes(s
, entry
, 16L);
1228 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1231 uint8_t *p
, entry
[8];
1232 int32_t global_offset
;
1241 * How to onvert from a global placeholder to a real symbol index;
1242 * the +2 refers to the two special entries, the null entry and
1243 * the filename entry.
1245 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1248 int32_t sym
= r
->symbol
;
1251 * Create a real symbol index; the +2 refers to the two special
1252 * entries, the null entry and the filename entry.
1254 if (sym
>= GLOBAL_TEMP_BASE
)
1255 sym
+= global_offset
;
1258 WRITELONG(p
, r
->address
);
1259 WRITELONG(p
, (sym
<< 8) + r
->type
);
1260 saa_wbytes(s
, entry
, 8L);
1269 static void elf_section_header(int name
, int type
, int flags
,
1270 void *data
, bool is_saa
, int32_t datalen
,
1271 int link
, int info
, int align
, int eltsize
)
1273 elf_sects
[elf_nsect
].data
= data
;
1274 elf_sects
[elf_nsect
].len
= datalen
;
1275 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1278 fwriteint32_t((int32_t)name
, ofile
);
1279 fwriteint32_t((int32_t)type
, ofile
);
1280 fwriteint32_t((int32_t)flags
, ofile
);
1281 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1282 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1283 fwriteint32_t(datalen
, ofile
);
1285 elf_foffs
+= ALIGN(datalen
, SEG_ALIGN
);
1286 fwriteint32_t((int32_t)link
, ofile
);
1287 fwriteint32_t((int32_t)info
, ofile
);
1288 fwriteint32_t((int32_t)align
, ofile
);
1289 fwriteint32_t((int32_t)eltsize
, ofile
);
1292 static void elf_write_sections(void)
1295 for (i
= 0; i
< elf_nsect
; i
++)
1296 if (elf_sects
[i
].data
) {
1297 int32_t len
= elf_sects
[i
].len
;
1298 int32_t reallen
= ALIGN(len
, SEG_ALIGN
);
1299 int32_t align
= reallen
- len
;
1300 if (elf_sects
[i
].is_saa
)
1301 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1303 fwrite(elf_sects
[i
].data
, len
, 1, ofile
);
1304 fwritezero(align
, ofile
);
1308 static void elf_sect_write(struct Section
*sect
,
1309 const uint8_t *data
, uint32_t len
)
1311 saa_wbytes(sect
->data
, data
, len
);
1315 static int32_t elf_segbase(int32_t segment
)
1320 static int elf_directive(enum directives directive
, char *value
, int pass
)
1326 switch (directive
) {
1329 return 1; /* ignore in pass 2 */
1331 n
= readnum(value
, &err
);
1333 nasm_error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1336 if (n
< 0 || n
> 255) {
1337 nasm_error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1343 if ((p
= strchr(value
,',')) == NULL
)
1346 n
= readnum(p
+1, &err
);
1347 if (err
|| n
< 0 || n
> 255) {
1348 nasm_error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1360 static void elf_filename(char *inname
, char *outname
)
1362 strcpy(elf_module
, inname
);
1363 standard_extension(inname
, outname
, ".o");
1366 extern macros_t elf_stdmac
[];
1368 static int elf_set_info(enum geninfo type
, char **val
)
1374 static struct dfmt df_dwarf
= {
1375 "ELF32 (i386) dwarf debug format for Linux/Unix",
1385 static struct dfmt df_stabs
= {
1386 "ELF32 (i386) stabs debug format for Linux/Unix",
1397 struct dfmt
*elf32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1399 struct ofmt of_elf32
= {
1400 "ELF32 (i386) object files (e.g. Linux)",
1417 struct ofmt of_elf
= {
1418 "ELF (short name for ELF32) ",
1434 /* again, the stabs debugging stuff (code) */
1436 static void stabs32_linenum(const char *filename
, int32_t linenumber
,
1441 if (!stabs_filename
) {
1442 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1443 strcpy(stabs_filename
, filename
);
1445 if (strcmp(stabs_filename
, filename
)) {
1447 * yep, a memory leak...this program is one-shot anyway, so who cares...
1448 * in fact, this leak comes in quite handy to maintain a list of files
1449 * encountered so far in the symbol lines...
1452 /* why not nasm_free(stabs_filename); we're done with the old one */
1454 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1455 strcpy(stabs_filename
, filename
);
1459 currentline
= linenumber
;
1462 static void debug32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1472 static void debug32_directive(const char *directive
, const char *params
)
1478 static void debug32_typevalue(int32_t type
)
1480 int32_t stype
, ssize
;
1481 switch (TYM_TYPE(type
)) {
1524 stype
= STT_SECTION
;
1539 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1540 lastsym
->size
= ssize
;
1541 lastsym
->type
= stype
;
1545 static void stabs32_output(int type
, void *param
)
1547 struct symlininfo
*s
;
1548 struct linelist
*el
;
1549 if (type
== TY_STABSSYMLIN
) {
1550 if (debug_immcall
) {
1551 s
= (struct symlininfo
*)param
;
1552 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1553 return; /* line info is only collected for executable sections */
1555 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1556 el
->info
.offset
= s
->offset
;
1557 el
->info
.section
= s
->section
;
1558 el
->info
.name
= s
->name
;
1559 el
->line
= currentline
;
1560 el
->filename
= stabs_filename
;
1563 stabslines
->last
->next
= el
;
1564 stabslines
->last
= el
;
1567 stabslines
->last
= el
;
1574 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1576 static void stabs32_generate(void)
1578 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1579 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1583 struct linelist
*ptr
;
1587 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1588 for (i
= 0; i
< numlinestabs
; i
++)
1592 if (numfiles
== 0) {
1593 allfiles
[0] = ptr
->filename
;
1596 for (i
= 0; i
< numfiles
; i
++) {
1597 if (!strcmp(allfiles
[i
], ptr
->filename
))
1600 if (i
>= numfiles
) {
1601 allfiles
[i
] = ptr
->filename
;
1608 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1609 for (i
= 0; i
< numfiles
; i
++) {
1610 fileidx
[i
] = strsize
;
1611 strsize
+= strlen(allfiles
[i
]) + 1;
1614 for (i
= 0; i
< numfiles
; i
++) {
1615 if (!strcmp(allfiles
[i
], elf_module
)) {
1622 * worst case size of the stab buffer would be:
1623 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1624 * plus one "ending" entry
1626 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1627 sizeof(struct stabentry
));
1628 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1629 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1632 for (i
= 0; i
< numfiles
; i
++)
1633 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1636 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1644 * this is the first stab, its strx points to the filename of the
1645 * the source-file, the n_desc field should be set to the number
1646 * of remaining stabs
1648 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1650 /* this is the stab for the main source file */
1651 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1653 /* relocation table entry */
1656 * Since the symbol table has two entries before
1657 * the section symbols, the index in the info.section
1658 * member must be adjusted by adding 2
1661 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1662 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1665 currfile
= mainfileindex
;
1669 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1670 /* oops file has changed... */
1671 for (i
= 0; i
< numfiles
; i
++)
1672 if (!strcmp(allfiles
[i
], ptr
->filename
))
1675 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1679 /* relocation table entry */
1680 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1681 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1684 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1687 /* relocation table entry */
1689 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1690 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1696 /* this is an "ending" token */
1697 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1700 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1702 nasm_free(allfiles
);
1705 stablen
= (sptr
- sbuf
);
1706 stabrellen
= (rptr
- rbuf
);
1712 static void stabs32_cleanup(void)
1714 struct linelist
*ptr
, *del
;
1726 nasm_free(stabrelbuf
);
1727 nasm_free(stabstrbuf
);
1730 /* dwarf routines */
1732 static void dwarf32_init(void)
1734 ndebugs
= 3; /* 3 debug symbols */
1737 static void dwarf32_linenum(const char *filename
, int32_t linenumber
,
1741 dwarf32_findfile(filename
);
1743 currentline
= linenumber
;
1746 /* called from elf_out with type == TY_DEBUGSYMLIN */
1747 static void dwarf32_output(int type
, void *param
)
1749 int ln
, aa
, inx
, maxln
, soc
;
1750 struct symlininfo
*s
;
1755 s
= (struct symlininfo
*)param
;
1757 /* line number info is only gathered for executable sections */
1758 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1761 /* Check if section index has changed */
1762 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1763 dwarf32_findsect(s
->section
);
1765 /* do nothing unless line or file has changed */
1769 ln
= currentline
- dwarf_csect
->line
;
1770 aa
= s
->offset
- dwarf_csect
->offset
;
1771 inx
= dwarf_clist
->line
;
1772 plinep
= dwarf_csect
->psaa
;
1773 /* check for file change */
1774 if (!(inx
== dwarf_csect
->file
)) {
1775 saa_write8(plinep
,DW_LNS_set_file
);
1776 saa_write8(plinep
,inx
);
1777 dwarf_csect
->file
= inx
;
1779 /* check for line change */
1781 /* test if in range of special op code */
1782 maxln
= line_base
+ line_range
;
1783 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1784 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1785 saa_write8(plinep
,soc
);
1787 saa_write8(plinep
,DW_LNS_advance_line
);
1788 saa_wleb128s(plinep
,ln
);
1790 saa_write8(plinep
,DW_LNS_advance_pc
);
1791 saa_wleb128u(plinep
,aa
);
1794 dwarf_csect
->line
= currentline
;
1795 dwarf_csect
->offset
= s
->offset
;
1798 /* show change handled */
1803 static void dwarf32_generate(void)
1807 struct linelist
*ftentry
;
1808 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1809 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1810 struct sectlist
*psect
;
1811 size_t saalen
, linepoff
, totlen
, highaddr
;
1813 /* write epilogues for each line program range */
1814 /* and build aranges section */
1815 paranges
= saa_init(1L);
1816 parangesrel
= saa_init(1L);
1817 saa_write16(paranges
,2); /* dwarf version */
1818 saa_write32(parangesrel
, paranges
->datalen
+4);
1819 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
1820 saa_write32(parangesrel
, 0);
1821 saa_write32(paranges
,0); /* offset into info */
1822 saa_write8(paranges
,4); /* pointer size */
1823 saa_write8(paranges
,0); /* not segmented */
1824 saa_write32(paranges
,0); /* padding */
1825 /* iterate though sectlist entries */
1826 psect
= dwarf_fsect
;
1829 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
1830 plinep
= psect
->psaa
;
1831 /* Line Number Program Epilogue */
1832 saa_write8(plinep
,2); /* std op 2 */
1833 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1834 saa_write8(plinep
,DW_LNS_extended_op
);
1835 saa_write8(plinep
,1); /* operand length */
1836 saa_write8(plinep
,DW_LNE_end_sequence
);
1837 totlen
+= plinep
->datalen
;
1838 /* range table relocation entry */
1839 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1840 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
1841 saa_write32(parangesrel
, (uint32_t) 0);
1842 /* range table entry */
1843 saa_write32(paranges
,0x0000); /* range start */
1844 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1845 highaddr
+= sects
[psect
->section
]->len
;
1846 /* done with this entry */
1847 psect
= psect
->next
;
1849 saa_write32(paranges
,0); /* null address */
1850 saa_write32(paranges
,0); /* null length */
1851 saalen
= paranges
->datalen
;
1852 arangeslen
= saalen
+ 4;
1853 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1854 WRITELONG(pbuf
,saalen
); /* initial length */
1855 saa_rnbytes(paranges
, pbuf
, saalen
);
1858 /* build rela.aranges section */
1859 arangesrellen
= saalen
= parangesrel
->datalen
;
1860 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1861 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1862 saa_free(parangesrel
);
1864 /* build pubnames section */
1865 ppubnames
= saa_init(1L);
1866 saa_write16(ppubnames
,3); /* dwarf version */
1867 saa_write32(ppubnames
,0); /* offset into info */
1868 saa_write32(ppubnames
,0); /* space used in info */
1869 saa_write32(ppubnames
,0); /* end of list */
1870 saalen
= ppubnames
->datalen
;
1871 pubnameslen
= saalen
+ 4;
1872 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1873 WRITELONG(pbuf
,saalen
); /* initial length */
1874 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1875 saa_free(ppubnames
);
1877 /* build info section */
1878 pinfo
= saa_init(1L);
1879 pinforel
= saa_init(1L);
1880 saa_write16(pinfo
,2); /* dwarf version */
1881 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1882 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
1883 saa_write32(pinforel
, 0);
1884 saa_write32(pinfo
,0); /* offset into abbrev */
1885 saa_write8(pinfo
,4); /* pointer size */
1886 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1887 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1888 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1889 saa_write32(pinforel
, 0);
1890 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1891 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1892 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1893 saa_write32(pinforel
, 0);
1894 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1895 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1896 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
1897 saa_write32(pinforel
, 0);
1898 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1899 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1900 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1901 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1902 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1903 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1904 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1905 saa_write32(pinforel
, 0);
1906 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1907 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1908 saa_write8(pinfo
,0); /* end of entries */
1909 saalen
= pinfo
->datalen
;
1910 infolen
= saalen
+ 4;
1911 infobuf
= pbuf
= nasm_malloc(infolen
);
1912 WRITELONG(pbuf
,saalen
); /* initial length */
1913 saa_rnbytes(pinfo
, pbuf
, saalen
);
1916 /* build rela.info section */
1917 inforellen
= saalen
= pinforel
->datalen
;
1918 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1919 saa_rnbytes(pinforel
, pbuf
, saalen
);
1922 /* build abbrev section */
1923 pabbrev
= saa_init(1L);
1924 saa_write8(pabbrev
,1); /* entry number LEB128u */
1925 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1926 saa_write8(pabbrev
,1); /* has children */
1927 /* the following attributes and forms are all LEB128u values */
1928 saa_write8(pabbrev
,DW_AT_low_pc
);
1929 saa_write8(pabbrev
,DW_FORM_addr
);
1930 saa_write8(pabbrev
,DW_AT_high_pc
);
1931 saa_write8(pabbrev
,DW_FORM_addr
);
1932 saa_write8(pabbrev
,DW_AT_stmt_list
);
1933 saa_write8(pabbrev
,DW_FORM_data4
);
1934 saa_write8(pabbrev
,DW_AT_name
);
1935 saa_write8(pabbrev
,DW_FORM_string
);
1936 saa_write8(pabbrev
,DW_AT_producer
);
1937 saa_write8(pabbrev
,DW_FORM_string
);
1938 saa_write8(pabbrev
,DW_AT_language
);
1939 saa_write8(pabbrev
,DW_FORM_data2
);
1940 saa_write16(pabbrev
,0); /* end of entry */
1941 /* LEB128u usage same as above */
1942 saa_write8(pabbrev
,2); /* entry number */
1943 saa_write8(pabbrev
,DW_TAG_subprogram
);
1944 saa_write8(pabbrev
,0); /* no children */
1945 saa_write8(pabbrev
,DW_AT_low_pc
);
1946 saa_write8(pabbrev
,DW_FORM_addr
);
1947 saa_write8(pabbrev
,DW_AT_frame_base
);
1948 saa_write8(pabbrev
,DW_FORM_data4
);
1949 saa_write16(pabbrev
,0); /* end of entry */
1950 abbrevlen
= saalen
= pabbrev
->datalen
;
1951 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
1952 saa_rnbytes(pabbrev
, pbuf
, saalen
);
1955 /* build line section */
1957 plines
= saa_init(1L);
1958 saa_write8(plines
,1); /* Minimum Instruction Length */
1959 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
1960 saa_write8(plines
,line_base
); /* Line Base */
1961 saa_write8(plines
,line_range
); /* Line Range */
1962 saa_write8(plines
,opcode_base
); /* Opcode Base */
1963 /* standard opcode lengths (# of LEB128u operands) */
1964 saa_write8(plines
,0); /* Std opcode 1 length */
1965 saa_write8(plines
,1); /* Std opcode 2 length */
1966 saa_write8(plines
,1); /* Std opcode 3 length */
1967 saa_write8(plines
,1); /* Std opcode 4 length */
1968 saa_write8(plines
,1); /* Std opcode 5 length */
1969 saa_write8(plines
,0); /* Std opcode 6 length */
1970 saa_write8(plines
,0); /* Std opcode 7 length */
1971 saa_write8(plines
,0); /* Std opcode 8 length */
1972 saa_write8(plines
,1); /* Std opcode 9 length */
1973 saa_write8(plines
,0); /* Std opcode 10 length */
1974 saa_write8(plines
,0); /* Std opcode 11 length */
1975 saa_write8(plines
,1); /* Std opcode 12 length */
1976 /* Directory Table */
1977 saa_write8(plines
,0); /* End of table */
1978 /* File Name Table */
1979 ftentry
= dwarf_flist
;
1980 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
1981 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
1982 saa_write8(plines
,0); /* directory LEB128u */
1983 saa_write8(plines
,0); /* time LEB128u */
1984 saa_write8(plines
,0); /* size LEB128u */
1985 ftentry
= ftentry
->next
;
1987 saa_write8(plines
,0); /* End of table */
1988 linepoff
= plines
->datalen
;
1989 linelen
= linepoff
+ totlen
+ 10;
1990 linebuf
= pbuf
= nasm_malloc(linelen
);
1991 WRITELONG(pbuf
,linelen
-4); /* initial length */
1992 WRITESHORT(pbuf
,3); /* dwarf version */
1993 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
1994 /* write line header */
1996 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
1999 /* concatonate line program ranges */
2001 plinesrel
= saa_init(1L);
2002 psect
= dwarf_fsect
;
2003 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
2004 saa_write32(plinesrel
, linepoff
);
2005 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2006 saa_write32(plinesrel
, (uint32_t) 0);
2007 plinep
= psect
->psaa
;
2008 saalen
= plinep
->datalen
;
2009 saa_rnbytes(plinep
, pbuf
, saalen
);
2013 /* done with this entry */
2014 psect
= psect
->next
;
2018 /* build rela.lines section */
2019 linerellen
=saalen
= plinesrel
->datalen
;
2020 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2021 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2022 saa_free(plinesrel
);
2024 /* build frame section */
2026 framebuf
= pbuf
= nasm_malloc(framelen
);
2027 WRITELONG(pbuf
,framelen
-4); /* initial length */
2029 /* build loc section */
2031 locbuf
= pbuf
= nasm_malloc(loclen
);
2032 WRITELONG(pbuf
,0); /* null beginning offset */
2033 WRITELONG(pbuf
,0); /* null ending offset */
2036 static void dwarf32_cleanup(void)
2038 nasm_free(arangesbuf
);
2039 nasm_free(arangesrelbuf
);
2040 nasm_free(pubnamesbuf
);
2042 nasm_free(inforelbuf
);
2043 nasm_free(abbrevbuf
);
2045 nasm_free(linerelbuf
);
2046 nasm_free(framebuf
);
2050 static void dwarf32_findfile(const char * fname
)
2053 struct linelist
*match
;
2055 /* return if fname is current file name */
2056 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2059 /* search for match */
2062 match
= dwarf_flist
;
2063 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2064 if (!(strcmp(fname
, match
->filename
))) {
2065 dwarf_clist
= match
;
2071 /* add file name to end of list */
2072 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2074 dwarf_clist
->line
= dwarf_numfiles
;
2075 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2076 strcpy(dwarf_clist
->filename
,fname
);
2077 dwarf_clist
->next
= 0;
2078 if (!dwarf_flist
) { /* if first entry */
2079 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2080 dwarf_clist
->last
= 0;
2081 } else { /* chain to previous entry */
2082 dwarf_elist
->next
= dwarf_clist
;
2083 dwarf_elist
= dwarf_clist
;
2087 static void dwarf32_findsect(const int index
)
2090 struct sectlist
*match
;
2093 /* return if index is current section index */
2094 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2097 /* search for match */
2100 match
= dwarf_fsect
;
2101 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2102 if ((match
->section
== index
)) {
2103 dwarf_csect
= match
;
2106 match
= match
->next
;
2110 /* add entry to end of list */
2111 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2113 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2114 dwarf_csect
->line
= 1;
2115 dwarf_csect
->offset
= 0;
2116 dwarf_csect
->file
= 1;
2117 dwarf_csect
->section
= index
;
2118 dwarf_csect
->next
= 0;
2119 /* set relocatable address at start of line program */
2120 saa_write8(plinep
,DW_LNS_extended_op
);
2121 saa_write8(plinep
,5); /* operand length */
2122 saa_write8(plinep
,DW_LNE_set_address
);
2123 saa_write32(plinep
,0); /* Start Address */
2125 if (!dwarf_fsect
) { /* if first entry */
2126 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2127 dwarf_csect
->last
= 0;
2128 } else { /* chain to previous entry */
2129 dwarf_esect
->next
= dwarf_csect
;
2130 dwarf_esect
= dwarf_csect
;