1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelf32.c output routines for the Netwide Assembler to produce
36 * ELF32 (i386 of course) object file format
54 #include "output/outform.h"
55 #include "output/outlib.h"
58 #include "output/dwarf.h"
59 #include "output/stabs.h"
60 #include "output/outelf.h"
65 static struct elf_section
**sects
;
66 static int nsects
, sectlen
;
68 #define SHSTR_DELTA 256
69 static char *shstrtab
;
70 static int shstrtablen
, shstrtabsize
;
72 static struct SAA
*syms
;
73 static uint32_t nlocals
, nglobs
, ndebugs
; /* Symbol counts */
75 static int32_t def_seg
;
77 static struct RAA
*bsym
;
79 static struct SAA
*strs
;
80 static uint32_t strslen
;
82 static struct elf_symbol
*fwds
;
84 static char elf_module
[FILENAME_MAX
];
86 extern struct ofmt of_elf32
;
88 static struct ELF_SECTDATA
{
93 static int elf_nsect
, nsections
;
94 static int32_t elf_foffs
;
96 static void elf_write(void);
97 static void elf_sect_write(struct elf_section
*, const uint8_t *,
99 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
101 static void elf_write_sections(void);
102 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
103 static struct SAA
*elf_build_reltab(uint64_t *, struct elf_reloc
*);
104 static void add_sectname(char *, char *);
112 int section
; /* section index */
113 char *name
; /* shallow-copied pointer of section name */
117 struct linelist
*next
;
118 struct linelist
*last
;
119 struct symlininfo info
;
130 struct sectlist
*next
;
131 struct sectlist
*last
;
134 /* common debug variables */
135 static int currentline
= 1;
136 static int debug_immcall
= 0;
138 /* stabs debug variables */
139 static struct linelist
*stabslines
= 0;
140 static int numlinestabs
= 0;
141 static char *stabs_filename
= 0;
142 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
143 static int stablen
, stabstrlen
, stabrellen
;
145 /* dwarf debug variables */
146 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
147 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
148 static int dwarf_numfiles
= 0, dwarf_nsections
;
149 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
150 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
151 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
152 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
153 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
154 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
156 static struct dfmt df_dwarf
;
157 static struct dfmt df_stabs
;
158 static struct elf_symbol
*lastsym
;
160 /* common debugging routines */
161 static void debug32_typevalue(int32_t);
162 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
163 static void debug32_directive(const char *, const char *);
165 /* stabs debugging routines */
166 static void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
167 static void stabs32_output(int, void *);
168 static void stabs32_generate(void);
169 static void stabs32_cleanup(void);
171 /* dwarf debugging routines */
172 static void dwarf32_init(void);
173 static void dwarf32_linenum(const char *filename
, int32_t linenumber
, int32_t);
174 static void dwarf32_output(int, void *);
175 static void dwarf32_generate(void);
176 static void dwarf32_cleanup(void);
177 static void dwarf32_findfile(const char *);
178 static void dwarf32_findsect(const int);
181 * Special NASM section numbers which are used to define ELF special
182 * symbols, which can be used with WRT to provide PIC and TLS
185 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
186 static int32_t elf_got_sect
, elf_plt_sect
;
187 static int32_t elf_sym_sect
, elf_tlsie_sect
;
189 static void elf_init(void)
192 nsects
= sectlen
= 0;
193 syms
= saa_init((int32_t)sizeof(struct elf_symbol
));
194 nlocals
= nglobs
= ndebugs
= 0;
197 saa_wbytes(strs
, "\0", 1L);
198 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
199 strslen
= 2 + strlen(elf_module
);
201 shstrtablen
= shstrtabsize
= 0;;
202 add_sectname("", "");
206 elf_gotpc_sect
= seg_alloc();
207 define_label("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false);
208 elf_gotoff_sect
= seg_alloc();
209 define_label("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false);
210 elf_got_sect
= seg_alloc();
211 define_label("..got", elf_got_sect
+ 1, 0L, NULL
, false, false);
212 elf_plt_sect
= seg_alloc();
213 define_label("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false);
214 elf_sym_sect
= seg_alloc();
215 define_label("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false);
216 elf_tlsie_sect
= seg_alloc();
217 define_label("..tlsie", elf_tlsie_sect
+ 1, 0L, NULL
, false, false);
219 def_seg
= seg_alloc();
222 static void elf_cleanup(int debuginfo
)
230 for (i
= 0; i
< nsects
; i
++) {
231 if (sects
[i
]->type
!= SHT_NOBITS
)
232 saa_free(sects
[i
]->data
);
234 saa_free(sects
[i
]->rel
);
235 while (sects
[i
]->head
) {
237 sects
[i
]->head
= sects
[i
]->head
->next
;
245 if (of_elf32
.current_dfmt
) {
246 of_elf32
.current_dfmt
->cleanup();
250 static void add_sectname(char *firsthalf
, char *secondhalf
)
252 int len
= strlen(firsthalf
) + strlen(secondhalf
);
253 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
254 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
255 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
256 strcat(shstrtab
+ shstrtablen
, secondhalf
);
257 shstrtablen
+= len
+ 1;
260 static int elf_make_section(char *name
, int type
, int flags
, int align
)
262 struct elf_section
*s
;
264 s
= nasm_zalloc(sizeof(*s
));
266 if (type
!= SHT_NOBITS
)
267 s
->data
= saa_init(1L);
269 if (!strcmp(name
, ".text"))
272 s
->index
= seg_alloc();
273 add_sectname("", name
);
275 s
->name
= nasm_strdup(name
);
280 if (nsects
>= sectlen
)
281 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
287 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
290 uint32_t flags
, flags_and
, flags_or
;
295 * Default is 32 bits.
302 p
= nasm_skip_word(name
);
305 flags_and
= flags_or
= type
= align
= 0;
307 elf_section_attrib(name
, p
, pass
, &flags_and
,
308 &flags_or
, &align
, &type
);
310 if (!strcmp(name
, ".shstrtab") ||
311 !strcmp(name
, ".symtab") ||
312 !strcmp(name
, ".strtab")) {
313 nasm_error(ERR_NONFATAL
, "attempt to redefine reserved section"
318 for (i
= 0; i
< nsects
; i
++)
319 if (!strcmp(name
, sects
[i
]->name
))
322 const struct elf_known_section
*ks
= elf_known_sections
;
325 if (!strcmp(name
, ks
->name
))
330 type
= type
? type
: ks
->type
;
331 align
= align
? align
: ks
->align
;
332 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
334 i
= elf_make_section(name
, type
, flags
, align
);
335 } else if (pass
== 1) {
336 if ((type
&& sects
[i
]->type
!= type
)
337 || (align
&& sects
[i
]->align
!= align
)
338 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
339 nasm_error(ERR_WARNING
, "incompatible section attributes ignored on"
340 " redeclaration of section `%s'", name
);
343 return sects
[i
]->index
;
346 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
347 int is_global
, char *special
)
350 struct elf_symbol
*sym
;
351 bool special_used
= false;
353 #if defined(DEBUG) && DEBUG>2
354 nasm_error(ERR_DEBUG
,
355 " elf_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
356 name
, segment
, offset
, is_global
, special
);
358 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
360 * This is a NASM special symbol. We never allow it into
361 * the ELF symbol table, even if it's a valid one. If it
362 * _isn't_ a valid one, we should barf immediately.
364 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
365 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
366 strcmp(name
, "..sym") && strcmp(name
, "..tlsie"))
367 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
371 if (is_global
== 3) {
372 struct elf_symbol
**s
;
374 * Fix up a forward-reference symbol size from the first
377 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
378 if (!strcmp((*s
)->name
, name
)) {
379 struct tokenval tokval
;
381 char *p
= nasm_skip_spaces(nasm_skip_word(special
));
385 tokval
.t_type
= TOKEN_INVALID
;
386 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
389 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
390 " expression as symbol size");
392 (*s
)->size
= reloc_value(e
);
396 * Remove it from the list of unresolved sizes.
398 nasm_free((*s
)->name
);
402 return; /* it wasn't an important one */
405 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
406 strslen
+= 1 + strlen(name
);
408 lastsym
= sym
= saa_wstruct(syms
);
410 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
413 sym
->type
= is_global
? SYM_GLOBAL
: SYM_LOCAL
;
414 sym
->other
= STV_DEFAULT
;
416 if (segment
== NO_SEG
)
417 sym
->section
= SHN_ABS
;
420 sym
->section
= SHN_UNDEF
;
421 if (segment
== def_seg
) {
422 /* we have to be sure at least text section is there */
424 if (segment
!= elf_section_names(".text", 2, &tempint
))
425 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
427 for (i
= 0; i
< nsects
; i
++) {
428 if (segment
== sects
[i
]->index
) {
429 sym
->section
= i
+ 1;
435 if (is_global
== 2) {
438 sym
->section
= SHN_COMMON
;
440 * We have a common variable. Check the special text to see
441 * if it's a valid number and power of two; if so, store it
442 * as the alignment for the common variable.
446 sym
->symv
.key
= readnum(special
, &err
);
448 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
449 " valid number", special
);
450 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1)) != 2 * sym
->symv
.key
- 1)
451 nasm_error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
452 " power of two", special
);
456 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
458 if (sym
->type
== SYM_GLOBAL
) {
460 * If sym->section == SHN_ABS, then the first line of the
461 * else section would cause a core dump, because its a reference
462 * beyond the end of the section array.
463 * This behaviour is exhibited by this code:
466 * To avoid such a crash, such requests are silently discarded.
467 * This may not be the best solution.
469 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
470 bsym
= raa_write(bsym
, segment
, nglobs
);
471 } else if (sym
->section
!= SHN_ABS
) {
473 * This is a global symbol; so we must add it to the rbtree
474 * of global symbols in its section.
476 * In addition, we check the special text for symbol
477 * type and size information.
479 sects
[sym
->section
-1]->gsyms
=
480 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
483 int n
= strcspn(special
, " \t");
485 if (!nasm_strnicmp(special
, "function", n
))
486 sym
->type
|= STT_FUNC
;
487 else if (!nasm_strnicmp(special
, "data", n
) ||
488 !nasm_strnicmp(special
, "object", n
))
489 sym
->type
|= STT_OBJECT
;
490 else if (!nasm_strnicmp(special
, "notype", n
))
491 sym
->type
|= STT_NOTYPE
;
493 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
497 special
= nasm_skip_spaces(special
);
499 n
= strcspn(special
, " \t");
500 if (!nasm_strnicmp(special
, "default", n
))
501 sym
->other
= STV_DEFAULT
;
502 else if (!nasm_strnicmp(special
, "internal", n
))
503 sym
->other
= STV_INTERNAL
;
504 else if (!nasm_strnicmp(special
, "hidden", n
))
505 sym
->other
= STV_HIDDEN
;
506 else if (!nasm_strnicmp(special
, "protected", n
))
507 sym
->other
= STV_PROTECTED
;
514 struct tokenval tokval
;
517 char *saveme
= stdscan_get();
519 while (special
[n
] && nasm_isspace(special
[n
]))
522 * We have a size expression; attempt to
526 stdscan_set(special
+ n
);
527 tokval
.t_type
= TOKEN_INVALID
;
528 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, nasm_error
,
533 sym
->name
= nasm_strdup(name
);
536 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
537 " expression as symbol size");
539 sym
->size
= reloc_value(e
);
546 * If TLS segment, mark symbol accordingly.
548 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
550 sym
->type
|= STT_TLS
;
553 sym
->globnum
= nglobs
;
558 if (special
&& !special_used
)
559 nasm_error(ERR_NONFATAL
, "no special symbol features supported here");
562 static void elf_add_reloc(struct elf_section
*sect
, int32_t segment
, int type
)
566 r
= *sect
->tail
= nasm_zalloc(sizeof(struct elf_reloc
));
567 sect
->tail
= &r
->next
;
569 r
->address
= sect
->len
;
570 if (segment
!= NO_SEG
) {
572 for (i
= 0; i
< nsects
; i
++)
573 if (segment
== sects
[i
]->index
)
576 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
584 * This routine deals with ..got and ..sym relocations: the more
585 * complicated kinds. In shared-library writing, some relocations
586 * with respect to global symbols must refer to the precise symbol
587 * rather than referring to an offset from the base of the section
588 * _containing_ the symbol. Such relocations call to this routine,
589 * which searches the symbol list for the symbol in question.
591 * R_386_GOT32 references require the _exact_ symbol address to be
592 * used; R_386_32 references can be at an offset from the symbol.
593 * The boolean argument `exact' tells us this.
595 * Return value is the adjusted value of `addr', having become an
596 * offset from the symbol rather than the section. Should always be
597 * zero when returning from an exact call.
599 * Limitation: if you define two symbols at the same place,
600 * confusion will occur.
602 * Inefficiency: we search, currently, using a linked list which
603 * isn't even necessarily sorted.
605 static int32_t elf_add_gsym_reloc(struct elf_section
*sect
,
606 int32_t segment
, uint32_t offset
,
607 int type
, bool exact
)
610 struct elf_section
*s
;
611 struct elf_symbol
*sym
;
616 * First look up the segment/offset pair and find a global
617 * symbol corresponding to it. If it's not one of our segments,
618 * then it must be an external symbol, in which case we're fine
619 * doing a normal elf_add_reloc after first sanity-checking
620 * that the offset from the symbol is zero.
623 for (i
= 0; i
< nsects
; i
++)
624 if (segment
== sects
[i
]->index
) {
631 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
632 " for this reference");
634 elf_add_reloc(sect
, segment
, type
);
638 srb
= rb_search(s
->gsyms
, offset
);
639 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
640 nasm_error(ERR_NONFATAL
, "unable to find a suitable global symbol"
641 " for this reference");
644 sym
= container_of(srb
, struct elf_symbol
, symv
);
646 r
= *sect
->tail
= nasm_malloc(sizeof(struct elf_reloc
));
647 sect
->tail
= &r
->next
;
650 r
->address
= sect
->len
;
651 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
656 return offset
- sym
->symv
.key
;
659 static void elf_out(int32_t segto
, const void *data
,
660 enum out_type type
, uint64_t size
,
661 int32_t segment
, int32_t wrt
)
663 struct elf_section
*s
;
665 uint8_t mydata
[8], *p
;
668 static struct symlininfo sinfo
;
671 * handle absolute-assembly (structure definitions)
673 if (segto
== NO_SEG
) {
674 if (type
!= OUT_RESERVE
)
675 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
681 for (i
= 0; i
< nsects
; i
++)
682 if (segto
== sects
[i
]->index
) {
687 int tempint
; /* ignored */
688 if (segto
!= elf_section_names(".text", 2, &tempint
))
689 nasm_error(ERR_PANIC
, "strange segment conditions in ELF driver");
691 s
= sects
[nsects
- 1];
696 /* again some stabs debugging stuff */
697 if (of_elf32
.current_dfmt
) {
698 sinfo
.offset
= s
->len
;
700 sinfo
.name
= s
->name
;
701 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
703 /* end of debugging stuff */
705 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
706 nasm_error(ERR_WARNING
, "attempt to initialize memory in"
707 " BSS section `%s': ignored", s
->name
);
708 s
->len
+= realsize(type
, size
);
712 memset(mydata
, 0, sizeof(mydata
));
716 if (s
->type
== SHT_PROGBITS
) {
717 nasm_error(ERR_WARNING
, "uninitialized space declared in"
718 " non-BSS section `%s': zeroing", s
->name
);
719 elf_sect_write(s
, NULL
, size
);
725 if (segment
!= NO_SEG
)
726 nasm_error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
727 elf_sect_write(s
, data
, size
);
733 int asize
= abs((int)size
);
734 addr
= *(int64_t *)data
;
735 if (segment
!= NO_SEG
) {
737 nasm_error(ERR_NONFATAL
, "ELF format does not support"
738 " segment base references");
742 * The if() is a hack to deal with compilers which
743 * don't handle switch() statements with 64-bit
749 elf_add_reloc(s
, segment
, R_386_8
);
753 elf_add_reloc(s
, segment
, R_386_16
);
756 elf_add_reloc(s
, segment
, R_386_32
);
758 default: /* Error issued further down */
761 } else if (wrt
== elf_gotpc_sect
+ 1) {
763 * The user will supply GOT relative to $$. ELF
764 * will let us have GOT relative to $. So we
765 * need to fix up the data item by $-$$.
768 elf_add_reloc(s
, segment
, R_386_GOTPC
);
769 } else if (wrt
== elf_gotoff_sect
+ 1) {
770 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
771 } else if (wrt
== elf_tlsie_sect
+ 1) {
772 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
774 } else if (wrt
== elf_got_sect
+ 1) {
775 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
777 } else if (wrt
== elf_sym_sect
+ 1) {
781 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
786 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
790 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
796 } else if (wrt
== elf_plt_sect
+ 1) {
797 nasm_error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
798 "relative PLT references");
800 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
802 wrt
= NO_SEG
; /* we can at least _try_ to continue */
808 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
809 "8- or 16-bit relocations in ELF32 is a GNU extension");
810 } else if (asize
!= 4 && segment
!= NO_SEG
) {
811 nasm_error(ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
813 WRITEADDR(p
, addr
, asize
);
814 elf_sect_write(s
, mydata
, asize
);
824 reltype
= R_386_PC16
;
828 nasm_assert(segment
!= segto
);
829 if (segment
!= NO_SEG
&& segment
% 2) {
830 nasm_error(ERR_NONFATAL
, "ELF format does not support"
831 " segment base references");
834 nasm_error(ERR_WARNING
| ERR_WARN_GNUELF
,
835 "8- or 16-bit relocations in ELF is a GNU extension");
836 elf_add_reloc(s
, segment
, reltype
);
838 nasm_error(ERR_NONFATAL
,
839 "Unsupported non-32-bit ELF relocation");
843 WRITESHORT(p
, *(int64_t *)data
- size
);
844 elf_sect_write(s
, mydata
, bytes
);
848 if (segment
== segto
)
849 nasm_error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
850 if (segment
!= NO_SEG
&& segment
% 2) {
851 nasm_error(ERR_NONFATAL
, "ELF format does not support"
852 " segment base references");
855 elf_add_reloc(s
, segment
, R_386_PC32
);
856 } else if (wrt
== elf_plt_sect
+ 1) {
857 elf_add_reloc(s
, segment
, R_386_PLT32
);
858 } else if (wrt
== elf_gotpc_sect
+ 1 ||
859 wrt
== elf_gotoff_sect
+ 1 ||
860 wrt
== elf_got_sect
+ 1) {
861 nasm_error(ERR_NONFATAL
, "ELF format cannot produce PC-"
862 "relative GOT references");
864 nasm_error(ERR_NONFATAL
, "ELF format does not support this"
866 wrt
= NO_SEG
; /* we can at least _try_ to continue */
870 WRITELONG(p
, *(int64_t *)data
- size
);
871 elf_sect_write(s
, mydata
, 4L);
875 nasm_error(ERR_NONFATAL
,
876 "32-bit ELF format does not support 64-bit relocations");
879 elf_sect_write(s
, mydata
, 8L);
884 static void elf_write(void)
891 int32_t symtablen
, symtablocal
;
894 * Work out how many sections we will have. We have SHN_UNDEF,
895 * then the flexible user sections, then the fixed sections
896 * `.shstrtab', `.symtab' and `.strtab', then optionally
897 * relocation sections for the user sections.
899 nsections
= sec_numspecial
+ 1;
900 if (of_elf32
.current_dfmt
== &df_stabs
)
902 else if (of_elf32
.current_dfmt
== &df_dwarf
)
905 add_sectname("", ".shstrtab");
906 add_sectname("", ".symtab");
907 add_sectname("", ".strtab");
908 for (i
= 0; i
< nsects
; i
++) {
909 nsections
++; /* for the section itself */
910 if (sects
[i
]->head
) {
911 nsections
++; /* for its relocations */
912 add_sectname(".rel", sects
[i
]->name
);
916 if (of_elf32
.current_dfmt
== &df_stabs
) {
917 /* in case the debug information is wanted, just add these three sections... */
918 add_sectname("", ".stab");
919 add_sectname("", ".stabstr");
920 add_sectname(".rel", ".stab");
921 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
922 /* the dwarf debug standard specifies the following ten sections,
923 not all of which are currently implemented,
924 although all of them are defined. */
925 add_sectname("", ".debug_aranges");
926 add_sectname(".rela", ".debug_aranges");
927 add_sectname("", ".debug_pubnames");
928 add_sectname("", ".debug_info");
929 add_sectname(".rela", ".debug_info");
930 add_sectname("", ".debug_abbrev");
931 add_sectname("", ".debug_line");
932 add_sectname(".rela", ".debug_line");
933 add_sectname("", ".debug_frame");
934 add_sectname("", ".debug_loc");
938 * Output the ELF header.
940 nasm_write("\177ELF\1\1\1", 7, ofile
);
941 fputc(elf_osabi
, ofile
);
942 fputc(elf_abiver
, ofile
);
943 fwritezero(7, ofile
);
944 fwriteint16_t(1, ofile
); /* ET_REL relocatable file */
945 fwriteint16_t(3, ofile
); /* EM_386 processor ID */
946 fwriteint32_t(1L, ofile
); /* EV_CURRENT file format version */
947 fwriteint32_t(0L, ofile
); /* no entry point */
948 fwriteint32_t(0L, ofile
); /* no program header table */
949 fwriteint32_t(0x40L
, ofile
); /* section headers straight after
950 * ELF header plus alignment */
951 fwriteint32_t(0L, ofile
); /* 386 defines no special flags */
952 fwriteint16_t(0x34, ofile
); /* size of ELF header */
953 fwriteint16_t(0, ofile
); /* no program header table, again */
954 fwriteint16_t(0, ofile
); /* still no program header table */
955 fwriteint16_t(0x28, ofile
); /* size of section header */
956 fwriteint16_t(nsections
, ofile
); /* number of sections */
957 fwriteint16_t(sec_shstrtab
, ofile
); /* string table section index for
958 * section header table */
959 fwriteint32_t(0L, ofile
); /* align to 0x40 bytes */
960 fwriteint32_t(0L, ofile
);
961 fwriteint32_t(0L, ofile
);
964 * Build the symbol table and relocation tables.
966 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
967 for (i
= 0; i
< nsects
; i
++)
969 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
973 * Now output the section header table.
976 elf_foffs
= 0x40 + 0x28 * nsections
;
977 align
= ALIGN(elf_foffs
, SEC_FILEALIGN
) - elf_foffs
;
980 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
983 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
986 /* The normal sections */
987 for (i
= 0; i
< nsects
; i
++) {
988 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
989 (sects
[i
]->type
== SHT_PROGBITS
?
990 sects
[i
]->data
: NULL
), true,
991 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
996 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
997 shstrtablen
, 0, 0, 1, 0);
1001 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1002 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1006 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1007 strslen
, 0, 0, 1, 0);
1010 /* The relocation sections */
1011 for (i
= 0; i
< nsects
; i
++)
1012 if (sects
[i
]->head
) {
1013 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1014 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1018 if (of_elf32
.current_dfmt
== &df_stabs
) {
1019 /* for debugging information, create the last three sections
1020 which are the .stab , .stabstr and .rel.stab sections respectively */
1022 /* this function call creates the stab sections in memory */
1025 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1026 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1027 stablen
, sec_stabstr
, 0, 4, 12);
1030 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1031 stabstrlen
, 0, 0, 4, 0);
1034 /* link -> symtable info -> section to refer to */
1035 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1036 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1039 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1040 /* for dwarf debugging information, create the ten dwarf sections */
1042 /* this function call creates the dwarf sections in memory */
1046 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1047 arangeslen
, 0, 0, 1, 0);
1050 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1051 arangesrellen
, sec_symtab
, sec_debug_aranges
,
1055 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1056 false, pubnameslen
, 0, 0, 1, 0);
1059 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1060 infolen
, 0, 0, 1, 0);
1063 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1064 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1067 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1068 abbrevlen
, 0, 0, 1, 0);
1071 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1072 linelen
, 0, 0, 1, 0);
1075 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1076 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1079 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1080 framelen
, 0, 0, 8, 0);
1083 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1084 loclen
, 0, 0, 1, 0);
1087 fwritezero(align
, ofile
);
1090 * Now output the sections.
1092 elf_write_sections();
1094 nasm_free(elf_sects
);
1098 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1100 struct SAA
*s
= saa_init(1L);
1101 struct elf_symbol
*sym
;
1102 uint8_t entry
[16], *p
;
1108 * First, an all-zeros entry, required by the ELF spec.
1110 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1115 * Next, an entry for the file name.
1118 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1119 WRITELONG(p
, 0); /* no value */
1120 WRITELONG(p
, 0); /* no size either */
1121 WRITESHORT(p
, STT_FILE
); /* type FILE */
1122 WRITESHORT(p
, SHN_ABS
);
1123 saa_wbytes(s
, entry
, 16L);
1128 * Now some standard symbols defining the segments, for relocation
1131 for (i
= 1; i
<= nsects
; i
++) {
1133 WRITELONG(p
, 0); /* no symbol name */
1134 WRITELONG(p
, 0); /* offset zero */
1135 WRITELONG(p
, 0); /* size zero */
1136 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1137 WRITESHORT(p
, i
); /* section id */
1138 saa_wbytes(s
, entry
, 16L);
1144 * Now the other local symbols.
1147 while ((sym
= saa_rstruct(syms
))) {
1148 if (sym
->type
& SYM_GLOBAL
)
1151 WRITELONG(p
, sym
->strpos
);
1152 WRITELONG(p
, sym
->symv
.key
);
1153 WRITELONG(p
, sym
->size
);
1154 WRITECHAR(p
, sym
->type
); /* type and binding */
1155 WRITECHAR(p
, sym
->other
); /* visibility */
1156 WRITESHORT(p
, sym
->section
);
1157 saa_wbytes(s
, entry
, 16L);
1162 * dwarf needs symbols for debug sections
1163 * which are relocation targets.
1165 //*** fix for 32 bit
1166 if (of_elf32
.current_dfmt
== &df_dwarf
) {
1167 dwarf_infosym
= *local
;
1169 WRITELONG(p
, 0); /* no symbol name */
1170 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1171 WRITELONG(p
, (uint32_t) 0); /* size zero */
1172 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1173 WRITESHORT(p
, sec_debug_info
); /* section id */
1174 saa_wbytes(s
, entry
, 16L);
1177 dwarf_abbrevsym
= *local
;
1179 WRITELONG(p
, 0); /* no symbol name */
1180 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1181 WRITELONG(p
, (uint32_t) 0); /* size zero */
1182 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1183 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1184 saa_wbytes(s
, entry
, 16L);
1187 dwarf_linesym
= *local
;
1189 WRITELONG(p
, 0); /* no symbol name */
1190 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1191 WRITELONG(p
, (uint32_t) 0); /* size zero */
1192 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1193 WRITESHORT(p
, sec_debug_line
); /* section id */
1194 saa_wbytes(s
, entry
, 16L);
1200 * Now the global symbols.
1203 while ((sym
= saa_rstruct(syms
))) {
1204 if (!(sym
->type
& SYM_GLOBAL
))
1207 WRITELONG(p
, sym
->strpos
);
1208 WRITELONG(p
, sym
->symv
.key
);
1209 WRITELONG(p
, sym
->size
);
1210 WRITECHAR(p
, sym
->type
); /* type and binding */
1211 WRITECHAR(p
, sym
->other
); /* visibility */
1212 WRITESHORT(p
, sym
->section
);
1213 saa_wbytes(s
, entry
, 16L);
1220 static struct SAA
*elf_build_reltab(uint64_t *len
, struct elf_reloc
*r
)
1223 uint8_t *p
, entry
[8];
1224 int32_t global_offset
;
1233 * How to onvert from a global placeholder to a real symbol index;
1234 * the +2 refers to the two special entries, the null entry and
1235 * the filename entry.
1237 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1240 int32_t sym
= r
->symbol
;
1242 if (sym
>= GLOBAL_TEMP_BASE
)
1243 sym
+= global_offset
;
1246 WRITELONG(p
, r
->address
);
1247 WRITELONG(p
, (sym
<< 8) + r
->type
);
1248 saa_wbytes(s
, entry
, 8L);
1257 static void elf_section_header(int name
, int type
, int flags
,
1258 void *data
, bool is_saa
, int32_t datalen
,
1259 int link
, int info
, int align
, int eltsize
)
1261 elf_sects
[elf_nsect
].data
= data
;
1262 elf_sects
[elf_nsect
].len
= datalen
;
1263 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1266 fwriteint32_t((int32_t)name
, ofile
);
1267 fwriteint32_t((int32_t)type
, ofile
);
1268 fwriteint32_t((int32_t)flags
, ofile
);
1269 fwriteint32_t(0L, ofile
); /* no address, ever, in object files */
1270 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, ofile
);
1271 fwriteint32_t(datalen
, ofile
);
1273 elf_foffs
+= ALIGN(datalen
, SEC_FILEALIGN
);
1274 fwriteint32_t((int32_t)link
, ofile
);
1275 fwriteint32_t((int32_t)info
, ofile
);
1276 fwriteint32_t((int32_t)align
, ofile
);
1277 fwriteint32_t((int32_t)eltsize
, ofile
);
1280 static void elf_write_sections(void)
1283 for (i
= 0; i
< elf_nsect
; i
++)
1284 if (elf_sects
[i
].data
) {
1285 int32_t len
= elf_sects
[i
].len
;
1286 int32_t reallen
= ALIGN(len
, SEC_FILEALIGN
);
1287 int32_t align
= reallen
- len
;
1288 if (elf_sects
[i
].is_saa
)
1289 saa_fpwrite(elf_sects
[i
].data
, ofile
);
1291 nasm_write(elf_sects
[i
].data
, len
, ofile
);
1292 fwritezero(align
, ofile
);
1296 static void elf_sect_write(struct elf_section
*sect
,
1297 const uint8_t *data
, uint32_t len
)
1299 saa_wbytes(sect
->data
, data
, len
);
1303 static void elf_sectalign(int32_t seg
, unsigned int value
)
1305 struct elf_section
*s
= NULL
;
1308 for (i
= 0; i
< nsects
; i
++) {
1309 if (sects
[i
]->index
== seg
) {
1314 if (!s
|| !is_power2(value
))
1317 if (value
> s
->align
)
1321 static int32_t elf_segbase(int32_t segment
)
1326 static void elf_filename(char *inname
, char *outname
)
1328 strcpy(elf_module
, inname
);
1329 standard_extension(inname
, outname
, ".o");
1332 extern macros_t elf_stdmac
[];
1334 static int elf_set_info(enum geninfo type
, char **val
)
1340 static struct dfmt df_dwarf
= {
1341 "ELF32 (i386) dwarf debug format for Linux/Unix",
1351 static struct dfmt df_stabs
= {
1352 "ELF32 (i386) stabs debug format for Linux/Unix",
1363 struct dfmt
*elf32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1365 struct ofmt of_elf32
= {
1366 "ELF32 (i386) object files (e.g. Linux)",
1385 /* again, the stabs debugging stuff (code) */
1387 static void stabs32_linenum(const char *filename
, int32_t linenumber
,
1392 if (!stabs_filename
) {
1393 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1394 strcpy(stabs_filename
, filename
);
1396 if (strcmp(stabs_filename
, filename
)) {
1398 * yep, a memory leak...this program is one-shot anyway, so who cares...
1399 * in fact, this leak comes in quite handy to maintain a list of files
1400 * encountered so far in the symbol lines...
1403 /* why not nasm_free(stabs_filename); we're done with the old one */
1405 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1406 strcpy(stabs_filename
, filename
);
1410 currentline
= linenumber
;
1413 static void debug32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1423 static void debug32_directive(const char *directive
, const char *params
)
1429 static void debug32_typevalue(int32_t type
)
1431 int32_t stype
, ssize
;
1432 switch (TYM_TYPE(type
)) {
1475 stype
= STT_SECTION
;
1490 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1491 lastsym
->size
= ssize
;
1492 lastsym
->type
= stype
;
1496 static void stabs32_output(int type
, void *param
)
1498 struct symlininfo
*s
;
1499 struct linelist
*el
;
1500 if (type
== TY_STABSSYMLIN
) {
1501 if (debug_immcall
) {
1502 s
= (struct symlininfo
*)param
;
1503 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1504 return; /* line info is only collected for executable sections */
1506 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1507 el
->info
.offset
= s
->offset
;
1508 el
->info
.section
= s
->section
;
1509 el
->info
.name
= s
->name
;
1510 el
->line
= currentline
;
1511 el
->filename
= stabs_filename
;
1514 stabslines
->last
->next
= el
;
1515 stabslines
->last
= el
;
1518 stabslines
->last
= el
;
1525 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1527 static void stabs32_generate(void)
1529 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1530 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1534 struct linelist
*ptr
;
1538 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1539 for (i
= 0; i
< numlinestabs
; i
++)
1543 if (numfiles
== 0) {
1544 allfiles
[0] = ptr
->filename
;
1547 for (i
= 0; i
< numfiles
; i
++) {
1548 if (!strcmp(allfiles
[i
], ptr
->filename
))
1551 if (i
>= numfiles
) {
1552 allfiles
[i
] = ptr
->filename
;
1559 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1560 for (i
= 0; i
< numfiles
; i
++) {
1561 fileidx
[i
] = strsize
;
1562 strsize
+= strlen(allfiles
[i
]) + 1;
1565 for (i
= 0; i
< numfiles
; i
++) {
1566 if (!strcmp(allfiles
[i
], elf_module
)) {
1573 * worst case size of the stab buffer would be:
1574 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1575 * plus one "ending" entry
1577 sbuf
= (uint8_t *)nasm_malloc((numlinestabs
* 2 + 4) *
1578 sizeof(struct stabentry
));
1579 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1580 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1583 for (i
= 0; i
< numfiles
; i
++)
1584 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1587 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1595 * this is the first stab, its strx points to the filename of the
1596 * the source-file, the n_desc field should be set to the number
1597 * of remaining stabs
1599 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, stabstrlen
);
1601 /* this is the stab for the main source file */
1602 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1604 /* relocation table entry */
1607 * Since the symbol table has two entries before
1608 * the section symbols, the index in the info.section
1609 * member must be adjusted by adding 2
1612 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1613 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1616 currfile
= mainfileindex
;
1620 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1621 /* oops file has changed... */
1622 for (i
= 0; i
< numfiles
; i
++)
1623 if (!strcmp(allfiles
[i
], ptr
->filename
))
1626 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1630 /* relocation table entry */
1631 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1632 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1635 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1638 /* relocation table entry */
1640 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1641 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1647 /* this is an "ending" token */
1648 WRITE_STAB(sptr
, 0, N_SO
, 0, 0, 0);
1651 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1653 nasm_free(allfiles
);
1656 stablen
= (sptr
- sbuf
);
1657 stabrellen
= (rptr
- rbuf
);
1663 static void stabs32_cleanup(void)
1665 struct linelist
*ptr
, *del
;
1677 nasm_free(stabrelbuf
);
1678 nasm_free(stabstrbuf
);
1681 /* dwarf routines */
1683 static void dwarf32_init(void)
1685 ndebugs
= 3; /* 3 debug symbols */
1688 static void dwarf32_linenum(const char *filename
, int32_t linenumber
,
1692 dwarf32_findfile(filename
);
1694 currentline
= linenumber
;
1697 /* called from elf_out with type == TY_DEBUGSYMLIN */
1698 static void dwarf32_output(int type
, void *param
)
1700 int ln
, aa
, inx
, maxln
, soc
;
1701 struct symlininfo
*s
;
1706 s
= (struct symlininfo
*)param
;
1708 /* line number info is only gathered for executable sections */
1709 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1712 /* Check if section index has changed */
1713 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1714 dwarf32_findsect(s
->section
);
1716 /* do nothing unless line or file has changed */
1720 ln
= currentline
- dwarf_csect
->line
;
1721 aa
= s
->offset
- dwarf_csect
->offset
;
1722 inx
= dwarf_clist
->line
;
1723 plinep
= dwarf_csect
->psaa
;
1724 /* check for file change */
1725 if (!(inx
== dwarf_csect
->file
)) {
1726 saa_write8(plinep
,DW_LNS_set_file
);
1727 saa_write8(plinep
,inx
);
1728 dwarf_csect
->file
= inx
;
1730 /* check for line change */
1732 /* test if in range of special op code */
1733 maxln
= line_base
+ line_range
;
1734 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1735 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256) {
1736 saa_write8(plinep
,soc
);
1738 saa_write8(plinep
,DW_LNS_advance_line
);
1739 saa_wleb128s(plinep
,ln
);
1741 saa_write8(plinep
,DW_LNS_advance_pc
);
1742 saa_wleb128u(plinep
,aa
);
1745 dwarf_csect
->line
= currentline
;
1746 dwarf_csect
->offset
= s
->offset
;
1749 /* show change handled */
1754 static void dwarf32_generate(void)
1758 struct linelist
*ftentry
;
1759 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1760 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1761 struct sectlist
*psect
;
1762 size_t saalen
, linepoff
, totlen
, highaddr
;
1764 /* write epilogues for each line program range */
1765 /* and build aranges section */
1766 paranges
= saa_init(1L);
1767 parangesrel
= saa_init(1L);
1768 saa_write16(paranges
,2); /* dwarf version */
1769 saa_write32(parangesrel
, paranges
->datalen
+4);
1770 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
1771 saa_write32(parangesrel
, 0);
1772 saa_write32(paranges
,0); /* offset into info */
1773 saa_write8(paranges
,4); /* pointer size */
1774 saa_write8(paranges
,0); /* not segmented */
1775 saa_write32(paranges
,0); /* padding */
1776 /* iterate though sectlist entries */
1777 psect
= dwarf_fsect
;
1780 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
1781 plinep
= psect
->psaa
;
1782 /* Line Number Program Epilogue */
1783 saa_write8(plinep
,2); /* std op 2 */
1784 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1785 saa_write8(plinep
,DW_LNS_extended_op
);
1786 saa_write8(plinep
,1); /* operand length */
1787 saa_write8(plinep
,DW_LNE_end_sequence
);
1788 totlen
+= plinep
->datalen
;
1789 /* range table relocation entry */
1790 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1791 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
1792 saa_write32(parangesrel
, (uint32_t) 0);
1793 /* range table entry */
1794 saa_write32(paranges
,0x0000); /* range start */
1795 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1796 highaddr
+= sects
[psect
->section
]->len
;
1797 /* done with this entry */
1798 psect
= psect
->next
;
1800 saa_write32(paranges
,0); /* null address */
1801 saa_write32(paranges
,0); /* null length */
1802 saalen
= paranges
->datalen
;
1803 arangeslen
= saalen
+ 4;
1804 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1805 WRITELONG(pbuf
,saalen
); /* initial length */
1806 saa_rnbytes(paranges
, pbuf
, saalen
);
1809 /* build rela.aranges section */
1810 arangesrellen
= saalen
= parangesrel
->datalen
;
1811 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1812 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1813 saa_free(parangesrel
);
1815 /* build pubnames section */
1816 ppubnames
= saa_init(1L);
1817 saa_write16(ppubnames
,3); /* dwarf version */
1818 saa_write32(ppubnames
,0); /* offset into info */
1819 saa_write32(ppubnames
,0); /* space used in info */
1820 saa_write32(ppubnames
,0); /* end of list */
1821 saalen
= ppubnames
->datalen
;
1822 pubnameslen
= saalen
+ 4;
1823 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1824 WRITELONG(pbuf
,saalen
); /* initial length */
1825 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1826 saa_free(ppubnames
);
1828 /* build info section */
1829 pinfo
= saa_init(1L);
1830 pinforel
= saa_init(1L);
1831 saa_write16(pinfo
,2); /* dwarf version */
1832 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1833 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
1834 saa_write32(pinforel
, 0);
1835 saa_write32(pinfo
,0); /* offset into abbrev */
1836 saa_write8(pinfo
,4); /* pointer size */
1837 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1838 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1839 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1840 saa_write32(pinforel
, 0);
1841 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1842 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1843 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1844 saa_write32(pinforel
, 0);
1845 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1846 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1847 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
1848 saa_write32(pinforel
, 0);
1849 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1850 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1851 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1852 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1853 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1854 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1855 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1856 saa_write32(pinforel
, 0);
1857 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1858 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1859 saa_write8(pinfo
,0); /* end of entries */
1860 saalen
= pinfo
->datalen
;
1861 infolen
= saalen
+ 4;
1862 infobuf
= pbuf
= nasm_malloc(infolen
);
1863 WRITELONG(pbuf
,saalen
); /* initial length */
1864 saa_rnbytes(pinfo
, pbuf
, saalen
);
1867 /* build rela.info section */
1868 inforellen
= saalen
= pinforel
->datalen
;
1869 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
1870 saa_rnbytes(pinforel
, pbuf
, saalen
);
1873 /* build abbrev section */
1874 pabbrev
= saa_init(1L);
1875 saa_write8(pabbrev
,1); /* entry number LEB128u */
1876 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
1877 saa_write8(pabbrev
,1); /* has children */
1878 /* the following attributes and forms are all LEB128u values */
1879 saa_write8(pabbrev
,DW_AT_low_pc
);
1880 saa_write8(pabbrev
,DW_FORM_addr
);
1881 saa_write8(pabbrev
,DW_AT_high_pc
);
1882 saa_write8(pabbrev
,DW_FORM_addr
);
1883 saa_write8(pabbrev
,DW_AT_stmt_list
);
1884 saa_write8(pabbrev
,DW_FORM_data4
);
1885 saa_write8(pabbrev
,DW_AT_name
);
1886 saa_write8(pabbrev
,DW_FORM_string
);
1887 saa_write8(pabbrev
,DW_AT_producer
);
1888 saa_write8(pabbrev
,DW_FORM_string
);
1889 saa_write8(pabbrev
,DW_AT_language
);
1890 saa_write8(pabbrev
,DW_FORM_data2
);
1891 saa_write16(pabbrev
,0); /* end of entry */
1892 /* LEB128u usage same as above */
1893 saa_write8(pabbrev
,2); /* entry number */
1894 saa_write8(pabbrev
,DW_TAG_subprogram
);
1895 saa_write8(pabbrev
,0); /* no children */
1896 saa_write8(pabbrev
,DW_AT_low_pc
);
1897 saa_write8(pabbrev
,DW_FORM_addr
);
1898 saa_write8(pabbrev
,DW_AT_frame_base
);
1899 saa_write8(pabbrev
,DW_FORM_data4
);
1900 saa_write16(pabbrev
,0); /* end of entry */
1901 abbrevlen
= saalen
= pabbrev
->datalen
;
1902 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
1903 saa_rnbytes(pabbrev
, pbuf
, saalen
);
1906 /* build line section */
1908 plines
= saa_init(1L);
1909 saa_write8(plines
,1); /* Minimum Instruction Length */
1910 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
1911 saa_write8(plines
,line_base
); /* Line Base */
1912 saa_write8(plines
,line_range
); /* Line Range */
1913 saa_write8(plines
,opcode_base
); /* Opcode Base */
1914 /* standard opcode lengths (# of LEB128u operands) */
1915 saa_write8(plines
,0); /* Std opcode 1 length */
1916 saa_write8(plines
,1); /* Std opcode 2 length */
1917 saa_write8(plines
,1); /* Std opcode 3 length */
1918 saa_write8(plines
,1); /* Std opcode 4 length */
1919 saa_write8(plines
,1); /* Std opcode 5 length */
1920 saa_write8(plines
,0); /* Std opcode 6 length */
1921 saa_write8(plines
,0); /* Std opcode 7 length */
1922 saa_write8(plines
,0); /* Std opcode 8 length */
1923 saa_write8(plines
,1); /* Std opcode 9 length */
1924 saa_write8(plines
,0); /* Std opcode 10 length */
1925 saa_write8(plines
,0); /* Std opcode 11 length */
1926 saa_write8(plines
,1); /* Std opcode 12 length */
1927 /* Directory Table */
1928 saa_write8(plines
,0); /* End of table */
1929 /* File Name Table */
1930 ftentry
= dwarf_flist
;
1931 for (indx
= 0; indx
< dwarf_numfiles
; indx
++) {
1932 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
1933 saa_write8(plines
,0); /* directory LEB128u */
1934 saa_write8(plines
,0); /* time LEB128u */
1935 saa_write8(plines
,0); /* size LEB128u */
1936 ftentry
= ftentry
->next
;
1938 saa_write8(plines
,0); /* End of table */
1939 linepoff
= plines
->datalen
;
1940 linelen
= linepoff
+ totlen
+ 10;
1941 linebuf
= pbuf
= nasm_malloc(linelen
);
1942 WRITELONG(pbuf
,linelen
-4); /* initial length */
1943 WRITESHORT(pbuf
,3); /* dwarf version */
1944 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
1945 /* write line header */
1947 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
1950 /* concatonate line program ranges */
1952 plinesrel
= saa_init(1L);
1953 psect
= dwarf_fsect
;
1954 for (indx
= 0; indx
< dwarf_nsections
; indx
++) {
1955 saa_write32(plinesrel
, linepoff
);
1956 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
1957 saa_write32(plinesrel
, (uint32_t) 0);
1958 plinep
= psect
->psaa
;
1959 saalen
= plinep
->datalen
;
1960 saa_rnbytes(plinep
, pbuf
, saalen
);
1964 /* done with this entry */
1965 psect
= psect
->next
;
1969 /* build rela.lines section */
1970 linerellen
=saalen
= plinesrel
->datalen
;
1971 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
1972 saa_rnbytes(plinesrel
, pbuf
, saalen
);
1973 saa_free(plinesrel
);
1975 /* build frame section */
1977 framebuf
= pbuf
= nasm_malloc(framelen
);
1978 WRITELONG(pbuf
,framelen
-4); /* initial length */
1980 /* build loc section */
1982 locbuf
= pbuf
= nasm_malloc(loclen
);
1983 WRITELONG(pbuf
,0); /* null beginning offset */
1984 WRITELONG(pbuf
,0); /* null ending offset */
1987 static void dwarf32_cleanup(void)
1989 nasm_free(arangesbuf
);
1990 nasm_free(arangesrelbuf
);
1991 nasm_free(pubnamesbuf
);
1993 nasm_free(inforelbuf
);
1994 nasm_free(abbrevbuf
);
1996 nasm_free(linerelbuf
);
1997 nasm_free(framebuf
);
2001 static void dwarf32_findfile(const char * fname
)
2004 struct linelist
*match
;
2006 /* return if fname is current file name */
2007 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
)))
2010 /* search for match */
2013 match
= dwarf_flist
;
2014 for (finx
= 0; finx
< dwarf_numfiles
; finx
++) {
2015 if (!(strcmp(fname
, match
->filename
))) {
2016 dwarf_clist
= match
;
2022 /* add file name to end of list */
2023 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2025 dwarf_clist
->line
= dwarf_numfiles
;
2026 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2027 strcpy(dwarf_clist
->filename
,fname
);
2028 dwarf_clist
->next
= 0;
2029 if (!dwarf_flist
) { /* if first entry */
2030 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2031 dwarf_clist
->last
= 0;
2032 } else { /* chain to previous entry */
2033 dwarf_elist
->next
= dwarf_clist
;
2034 dwarf_elist
= dwarf_clist
;
2038 static void dwarf32_findsect(const int index
)
2041 struct sectlist
*match
;
2044 /* return if index is current section index */
2045 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2048 /* search for match */
2051 match
= dwarf_fsect
;
2052 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++) {
2053 if (match
->section
== index
) {
2054 dwarf_csect
= match
;
2057 match
= match
->next
;
2061 /* add entry to end of list */
2062 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2064 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2065 dwarf_csect
->line
= 1;
2066 dwarf_csect
->offset
= 0;
2067 dwarf_csect
->file
= 1;
2068 dwarf_csect
->section
= index
;
2069 dwarf_csect
->next
= 0;
2070 /* set relocatable address at start of line program */
2071 saa_write8(plinep
,DW_LNS_extended_op
);
2072 saa_write8(plinep
,5); /* operand length */
2073 saa_write8(plinep
,DW_LNE_set_address
);
2074 saa_write32(plinep
,0); /* Start Address */
2076 if (!dwarf_fsect
) { /* if first entry */
2077 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2078 dwarf_csect
->last
= 0;
2079 } else { /* chain to previous entry */
2080 dwarf_esect
->next
= dwarf_csect
;
2081 dwarf_esect
= dwarf_csect
;