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
52 #include "output/outform.h"
53 #include "output/outlib.h"
56 #include "output/elf32.h"
57 #include "output/dwarf.h"
58 #include "output/outelf.h"
67 int32_t address
; /* relative to _start_ of section */
68 int32_t symbol
; /* symbol index */
69 int type
; /* type of relocation */
73 struct rbtree symv
; /* symbol value and symbol rbtree */
74 int32_t strpos
; /* string table position of name */
75 int32_t section
; /* section ID of the symbol */
76 int type
; /* symbol type */
77 int other
; /* symbol visibility */
78 int32_t size
; /* size of symbol */
79 int32_t globnum
; /* symbol table offset if global */
80 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
81 char *name
; /* used temporarily if in above list */
86 uint32_t len
, size
, nrelocs
;
88 int type
; /* SHT_PROGBITS or SHT_NOBITS */
89 uint32_t align
; /* alignment: power of two */
90 uint32_t flags
; /* section flags */
94 struct Reloc
*head
, **tail
;
95 struct rbtree
*gsyms
; /* global symbols in section */
99 static struct Section
**sects
;
100 static int nsects
, sectlen
;
102 #define SHSTR_DELTA 256
103 static char *shstrtab
;
104 static int shstrtablen
, shstrtabsize
;
106 static struct SAA
*syms
;
107 static uint32_t nlocals
, nglobs
, ndebugs
; /* Symbol counts */
109 static int32_t def_seg
;
111 static struct RAA
*bsym
;
113 static struct SAA
*strs
;
114 static uint32_t strslen
;
118 static evalfunc evaluate
;
120 static struct Symbol
*fwds
;
122 static char elf_module
[FILENAME_MAX
];
124 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
125 static uint8_t elf_abiver
= 0; /* Current ABI version */
127 extern struct ofmt of_elf32
;
128 extern struct ofmt of_elf
;
130 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
132 static struct ELF_SECTDATA
{
137 static int elf_nsect
, nsections
;
138 static int32_t elf_foffs
;
140 static void elf_write(void);
141 static void elf_sect_write(struct Section
*, const uint8_t *,
143 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
145 static void elf_write_sections(void);
146 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
147 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
148 static void add_sectname(char *, char *);
164 int section
; /* section index */
165 char *name
; /* shallow-copied pointer of section name */
169 struct symlininfo info
;
172 struct linelist
*next
;
173 struct linelist
*last
;
182 struct sectlist
*next
;
183 struct sectlist
*last
;
186 /* common debug variables */
187 static int currentline
= 1;
188 static int debug_immcall
= 0;
190 /* stabs debug variables */
191 static struct linelist
*stabslines
= 0;
192 static int numlinestabs
= 0;
193 static char *stabs_filename
= 0;
194 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
195 static int stablen
, stabstrlen
, stabrellen
;
197 /* dwarf debug variables */
198 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
199 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
200 static int dwarf_numfiles
= 0, dwarf_nsections
;
201 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
202 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
203 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
204 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
205 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
206 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
208 static struct dfmt df_dwarf
;
209 static struct dfmt df_stabs
;
210 static struct Symbol
*lastsym
;
212 /* common debugging routines */
213 static void debug32_typevalue(int32_t);
214 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
215 static void debug32_directive(const char *, const char *);
217 /* stabs debugging routines */
218 static void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
219 static void stabs32_output(int, void *);
220 static void stabs32_generate(void);
221 static void stabs32_cleanup(void);
223 /* dwarf debugging routines */
224 static void dwarf32_init(struct ofmt
*, void *, FILE *, efunc
);
225 static void dwarf32_linenum(const char *filename
, int32_t linenumber
, int32_t);
226 static void dwarf32_output(int, void *);
227 static void dwarf32_generate(void);
228 static void dwarf32_cleanup(void);
229 static void dwarf32_findfile(const char *);
230 static void dwarf32_findsect(const int);
233 * Special NASM section numbers which are used to define ELF special
234 * symbols, which can be used with WRT to provide PIC and TLS
237 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
238 static int32_t elf_got_sect
, elf_plt_sect
;
239 static int32_t elf_sym_sect
, elf_tlsie_sect
;
241 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
246 (void)ldef
; /* placate optimisers */
248 nsects
= sectlen
= 0;
249 syms
= saa_init((int32_t)sizeof(struct Symbol
));
250 nlocals
= nglobs
= ndebugs
= 0;
253 saa_wbytes(strs
, "\0", 1L);
254 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
255 strslen
= 2 + strlen(elf_module
);
257 shstrtablen
= shstrtabsize
= 0;;
258 add_sectname("", "");
262 elf_gotpc_sect
= seg_alloc();
263 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
265 elf_gotoff_sect
= seg_alloc();
266 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
268 elf_got_sect
= seg_alloc();
269 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
271 elf_plt_sect
= seg_alloc();
272 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
274 elf_sym_sect
= seg_alloc();
275 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
277 elf_tlsie_sect
= seg_alloc();
278 ldef("..tlsie", elf_tlsie_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
281 def_seg
= seg_alloc();
284 static void elf_init_hack(FILE * fp
, efunc errfunc
, ldfunc ldef
,
287 of_elf32
.current_dfmt
= of_elf
.current_dfmt
; /* Sync debugging format */
288 elf_init(fp
, errfunc
, ldef
, eval
);
291 static void elf_cleanup(int debuginfo
)
299 for (i
= 0; i
< nsects
; i
++) {
300 if (sects
[i
]->type
!= SHT_NOBITS
)
301 saa_free(sects
[i
]->data
);
303 saa_free(sects
[i
]->rel
);
304 while (sects
[i
]->head
) {
306 sects
[i
]->head
= sects
[i
]->head
->next
;
314 if (of_elf32
.current_dfmt
) {
315 of_elf32
.current_dfmt
->cleanup();
319 static void add_sectname(char *firsthalf
, char *secondhalf
)
321 int len
= strlen(firsthalf
) + strlen(secondhalf
);
322 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
323 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
324 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
325 strcat(shstrtab
+ shstrtablen
, secondhalf
);
326 shstrtablen
+= len
+ 1;
329 static int elf_make_section(char *name
, int type
, int flags
, int align
)
333 s
= nasm_malloc(sizeof(*s
));
335 if (type
!= SHT_NOBITS
)
336 s
->data
= saa_init(1L);
339 s
->len
= s
->size
= 0;
341 if (!strcmp(name
, ".text"))
344 s
->index
= seg_alloc();
345 add_sectname("", name
);
346 s
->name
= nasm_malloc(1 + strlen(name
));
347 strcpy(s
->name
, name
);
353 if (nsects
>= sectlen
)
354 sects
= nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
361 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
364 uint32_t flags
, flags_and
, flags_or
;
369 * Default is 32 bits.
377 while (*p
&& !nasm_isspace(*p
))
381 flags_and
= flags_or
= type
= align
= 0;
383 while (*p
&& nasm_isspace(*p
))
387 while (*p
&& !nasm_isspace(*p
))
391 while (*p
&& nasm_isspace(*p
))
394 if (!nasm_strnicmp(q
, "align=", 6)) {
398 if ((align
- 1) & align
) { /* means it's not a power of two */
399 error(ERR_NONFATAL
, "section alignment %d is not"
400 " a power of two", align
);
403 } else if (!nasm_stricmp(q
, "alloc")) {
404 flags_and
|= SHF_ALLOC
;
405 flags_or
|= SHF_ALLOC
;
406 } else if (!nasm_stricmp(q
, "noalloc")) {
407 flags_and
|= SHF_ALLOC
;
408 flags_or
&= ~SHF_ALLOC
;
409 } else if (!nasm_stricmp(q
, "exec")) {
410 flags_and
|= SHF_EXECINSTR
;
411 flags_or
|= SHF_EXECINSTR
;
412 } else if (!nasm_stricmp(q
, "noexec")) {
413 flags_and
|= SHF_EXECINSTR
;
414 flags_or
&= ~SHF_EXECINSTR
;
415 } else if (!nasm_stricmp(q
, "write")) {
416 flags_and
|= SHF_WRITE
;
417 flags_or
|= SHF_WRITE
;
418 } else if (!nasm_stricmp(q
, "tls")) {
419 flags_and
|= SHF_TLS
;
421 } else if (!nasm_stricmp(q
, "nowrite")) {
422 flags_and
|= SHF_WRITE
;
423 flags_or
&= ~SHF_WRITE
;
424 } else if (!nasm_stricmp(q
, "progbits")) {
426 } else if (!nasm_stricmp(q
, "nobits")) {
428 } else if (pass
== 1) {
429 error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
430 " declaration of section `%s'", q
, name
);
434 if (!strcmp(name
, ".shstrtab") ||
435 !strcmp(name
, ".symtab") ||
436 !strcmp(name
, ".strtab")) {
437 error(ERR_NONFATAL
, "attempt to redefine reserved section"
442 for (i
= 0; i
< nsects
; i
++)
443 if (!strcmp(name
, sects
[i
]->name
))
446 const struct elf_known_section
*ks
= elf_known_sections
;
449 if (!strcmp(name
, ks
->name
))
454 type
= type
? type
: ks
->type
;
455 align
= align
? align
: ks
->align
;
456 flags
= (ks
->flags
& ~flags_and
) | flags_or
;
458 i
= elf_make_section(name
, type
, flags
, align
);
459 } else if (pass
== 1) {
460 if ((type
&& sects
[i
]->type
!= type
)
461 || (align
&& sects
[i
]->align
!= align
)
462 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
463 error(ERR_WARNING
, "section attributes ignored on"
464 " redeclaration of section `%s'", name
);
467 return sects
[i
]->index
;
470 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
471 int is_global
, char *special
)
475 bool special_used
= false;
477 #if defined(DEBUG) && DEBUG>2
479 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
480 name
, segment
, offset
, is_global
, special
);
482 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
484 * This is a NASM special symbol. We never allow it into
485 * the ELF symbol table, even if it's a valid one. If it
486 * _isn't_ a valid one, we should barf immediately.
488 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
489 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
490 strcmp(name
, "..sym") && strcmp(name
, "..tlsie"))
491 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
495 if (is_global
== 3) {
498 * Fix up a forward-reference symbol size from the first
501 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
502 if (!strcmp((*s
)->name
, name
)) {
503 struct tokenval tokval
;
507 while (*p
&& !nasm_isspace(*p
))
509 while (*p
&& nasm_isspace(*p
))
513 tokval
.t_type
= TOKEN_INVALID
;
514 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
517 error(ERR_NONFATAL
, "cannot use relocatable"
518 " expression as symbol size");
520 (*s
)->size
= reloc_value(e
);
524 * Remove it from the list of unresolved sizes.
526 nasm_free((*s
)->name
);
530 return; /* it wasn't an important one */
533 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
534 strslen
+= 1 + strlen(name
);
536 lastsym
= sym
= saa_wstruct(syms
);
538 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
541 sym
->type
= is_global
? SYM_GLOBAL
: 0;
542 sym
->other
= STV_DEFAULT
;
544 if (segment
== NO_SEG
)
545 sym
->section
= SHN_ABS
;
548 sym
->section
= SHN_UNDEF
;
549 if (nsects
== 0 && segment
== def_seg
) {
551 if (segment
!= elf_section_names(".text", 2, &tempint
))
553 "strange segment conditions in ELF driver");
554 sym
->section
= nsects
;
556 for (i
= 0; i
< nsects
; i
++)
557 if (segment
== sects
[i
]->index
) {
558 sym
->section
= i
+ 1;
564 if (is_global
== 2) {
567 sym
->section
= SHN_COMMON
;
569 * We have a common variable. Check the special text to see
570 * if it's a valid number and power of two; if so, store it
571 * as the alignment for the common variable.
575 sym
->symv
.key
= readnum(special
, &err
);
577 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
578 " valid number", special
);
579 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
580 != 2 * sym
->symv
.key
- 1)
581 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
582 " power of two", special
);
586 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
588 if (sym
->type
== SYM_GLOBAL
) {
590 * If sym->section == SHN_ABS, then the first line of the
591 * else section would cause a core dump, because its a reference
592 * beyond the end of the section array.
593 * This behaviour is exhibited by this code:
596 * To avoid such a crash, such requests are silently discarded.
597 * This may not be the best solution.
599 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
600 bsym
= raa_write(bsym
, segment
, nglobs
);
601 } else if (sym
->section
!= SHN_ABS
) {
603 * This is a global symbol; so we must add it to the rbtree
604 * of global symbols in its section.
606 * In addition, we check the special text for symbol
607 * type and size information.
609 sects
[sym
->section
-1]->gsyms
=
610 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
613 int n
= strcspn(special
, " \t");
615 if (!nasm_strnicmp(special
, "function", n
))
616 sym
->type
|= STT_FUNC
;
617 else if (!nasm_strnicmp(special
, "data", n
) ||
618 !nasm_strnicmp(special
, "object", n
))
619 sym
->type
|= STT_OBJECT
;
620 else if (!nasm_strnicmp(special
, "notype", n
))
621 sym
->type
|= STT_NOTYPE
;
623 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
627 while (nasm_isspace(*special
))
630 n
= strcspn(special
, " \t");
631 if (!nasm_strnicmp(special
, "default", n
))
632 sym
->other
= STV_DEFAULT
;
633 else if (!nasm_strnicmp(special
, "internal", n
))
634 sym
->other
= STV_INTERNAL
;
635 else if (!nasm_strnicmp(special
, "hidden", n
))
636 sym
->other
= STV_HIDDEN
;
637 else if (!nasm_strnicmp(special
, "protected", n
))
638 sym
->other
= STV_PROTECTED
;
645 struct tokenval tokval
;
648 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
650 while (special
[n
] && nasm_isspace(special
[n
]))
653 * We have a size expression; attempt to
657 stdscan_bufptr
= special
+ n
;
658 tokval
.t_type
= TOKEN_INVALID
;
659 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
664 sym
->name
= nasm_strdup(name
);
667 error(ERR_NONFATAL
, "cannot use relocatable"
668 " expression as symbol size");
670 sym
->size
= reloc_value(e
);
672 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
677 * If TLS segment, mark symbol accordingly.
679 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
681 sym
->type
|= STT_TLS
;
684 sym
->globnum
= nglobs
;
689 if (special
&& !special_used
)
690 error(ERR_NONFATAL
, "no special symbol features supported here");
693 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
697 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
698 sect
->tail
= &r
->next
;
701 r
->address
= sect
->len
;
702 if (segment
== NO_SEG
)
707 for (i
= 0; i
< nsects
; i
++)
708 if (segment
== sects
[i
]->index
)
711 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
719 * This routine deals with ..got and ..sym relocations: the more
720 * complicated kinds. In shared-library writing, some relocations
721 * with respect to global symbols must refer to the precise symbol
722 * rather than referring to an offset from the base of the section
723 * _containing_ the symbol. Such relocations call to this routine,
724 * which searches the symbol list for the symbol in question.
726 * R_386_GOT32 references require the _exact_ symbol address to be
727 * used; R_386_32 references can be at an offset from the symbol.
728 * The boolean argument `exact' tells us this.
730 * Return value is the adjusted value of `addr', having become an
731 * offset from the symbol rather than the section. Should always be
732 * zero when returning from an exact call.
734 * Limitation: if you define two symbols at the same place,
735 * confusion will occur.
737 * Inefficiency: we search, currently, using a linked list which
738 * isn't even necessarily sorted.
740 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
741 int32_t segment
, uint32_t offset
,
742 int type
, bool exact
)
751 * First look up the segment/offset pair and find a global
752 * symbol corresponding to it. If it's not one of our segments,
753 * then it must be an external symbol, in which case we're fine
754 * doing a normal elf_add_reloc after first sanity-checking
755 * that the offset from the symbol is zero.
758 for (i
= 0; i
< nsects
; i
++)
759 if (segment
== sects
[i
]->index
) {
764 if (exact
&& offset
!= 0)
765 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
766 " for this reference");
768 elf_add_reloc(sect
, segment
, type
);
772 srb
= rb_search(s
->gsyms
, offset
);
773 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
774 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
775 " for this reference");
778 sym
= container_of(srb
, struct Symbol
, symv
);
780 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
781 sect
->tail
= &r
->next
;
784 r
->address
= sect
->len
;
785 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
790 return offset
- sym
->symv
.key
;
793 static void elf_out(int32_t segto
, const void *data
,
794 enum out_type type
, uint64_t size
,
795 int32_t segment
, int32_t wrt
)
799 uint8_t mydata
[4], *p
;
801 static struct symlininfo sinfo
;
804 * handle absolute-assembly (structure definitions)
806 if (segto
== NO_SEG
) {
807 if (type
!= OUT_RESERVE
)
808 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
814 for (i
= 0; i
< nsects
; i
++)
815 if (segto
== sects
[i
]->index
) {
820 int tempint
; /* ignored */
821 if (segto
!= elf_section_names(".text", 2, &tempint
))
822 error(ERR_PANIC
, "strange segment conditions in ELF driver");
824 s
= sects
[nsects
- 1];
829 /* again some stabs debugging stuff */
830 if (of_elf32
.current_dfmt
) {
831 sinfo
.offset
= s
->len
;
833 sinfo
.name
= s
->name
;
834 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
836 /* end of debugging stuff */
838 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
839 error(ERR_WARNING
, "attempt to initialize memory in"
840 " BSS section `%s': ignored", s
->name
);
841 s
->len
+= realsize(type
, size
);
845 if (type
== OUT_RESERVE
) {
846 if (s
->type
== SHT_PROGBITS
) {
847 error(ERR_WARNING
, "uninitialized space declared in"
848 " non-BSS section `%s': zeroing", s
->name
);
849 elf_sect_write(s
, NULL
, size
);
852 } else if (type
== OUT_RAWDATA
) {
853 if (segment
!= NO_SEG
)
854 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
855 elf_sect_write(s
, data
, size
);
856 } else if (type
== OUT_ADDRESS
) {
858 addr
= *(int64_t *)data
;
859 if (segment
!= NO_SEG
) {
861 error(ERR_NONFATAL
, "ELF format does not support"
862 " segment base references");
867 elf_add_reloc(s
, segment
, R_386_16
);
869 elf_add_reloc(s
, segment
, R_386_32
);
871 } else if (wrt
== elf_gotpc_sect
+ 1) {
873 * The user will supply GOT relative to $$. ELF
874 * will let us have GOT relative to $. So we
875 * need to fix up the data item by $-$$.
878 elf_add_reloc(s
, segment
, R_386_GOTPC
);
879 } else if (wrt
== elf_gotoff_sect
+ 1) {
880 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
881 } else if (wrt
== elf_tlsie_sect
+ 1) {
882 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
884 } else if (wrt
== elf_got_sect
+ 1) {
885 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
887 } else if (wrt
== elf_sym_sect
+ 1) {
890 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
893 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
896 } else if (wrt
== elf_plt_sect
+ 1) {
897 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
898 "relative PLT references");
900 error(ERR_NONFATAL
, "ELF format does not support this"
902 wrt
= NO_SEG
; /* we can at least _try_ to continue */
908 error(ERR_WARNING
| ERR_WARN_GNUELF
,
909 "16-bit relocations in ELF is a GNU extension");
912 if (size
!= 4 && segment
!= NO_SEG
) {
914 "Unsupported non-32-bit ELF relocation");
918 elf_sect_write(s
, mydata
, size
);
919 } else if (type
== OUT_REL2ADR
) {
920 if (segment
== segto
)
921 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
922 if (segment
!= NO_SEG
&& segment
% 2) {
923 error(ERR_NONFATAL
, "ELF format does not support"
924 " segment base references");
927 error(ERR_WARNING
| ERR_WARN_GNUELF
,
928 "16-bit relocations in ELF is a GNU extension");
929 elf_add_reloc(s
, segment
, R_386_PC16
);
932 "Unsupported non-32-bit ELF relocation");
936 WRITESHORT(p
, *(int64_t *)data
- size
);
937 elf_sect_write(s
, mydata
, 2L);
938 } else if (type
== OUT_REL4ADR
) {
939 if (segment
== segto
)
940 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
941 if (segment
!= NO_SEG
&& segment
% 2) {
942 error(ERR_NONFATAL
, "ELF format does not support"
943 " segment base references");
946 elf_add_reloc(s
, segment
, R_386_PC32
);
947 } else if (wrt
== elf_plt_sect
+ 1) {
948 elf_add_reloc(s
, segment
, R_386_PLT32
);
949 } else if (wrt
== elf_gotpc_sect
+ 1 ||
950 wrt
== elf_gotoff_sect
+ 1 ||
951 wrt
== elf_got_sect
+ 1) {
952 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
953 "relative GOT references");
955 error(ERR_NONFATAL
, "ELF format does not support this"
957 wrt
= NO_SEG
; /* we can at least _try_ to continue */
961 WRITELONG(p
, *(int64_t *)data
- size
);
962 elf_sect_write(s
, mydata
, 4L);
966 static void elf_write(void)
973 int32_t symtablen
, symtablocal
;
976 * Work out how many sections we will have. We have SHN_UNDEF,
977 * then the flexible user sections, then the fixed sections
978 * `.shstrtab', `.symtab' and `.strtab', then optionally
979 * relocation sections for the user sections.
981 nsections
= sec_numspecial
+ 1;
982 if (of_elf32
.current_dfmt
== &df_stabs
)
984 else if (of_elf32
.current_dfmt
== &df_dwarf
)
987 add_sectname("", ".shstrtab");
988 add_sectname("", ".symtab");
989 add_sectname("", ".strtab");
990 for (i
= 0; i
< nsects
; i
++) {
991 nsections
++; /* for the section itself */
992 if (sects
[i
]->head
) {
993 nsections
++; /* for its relocations */
994 add_sectname(".rel", sects
[i
]->name
);
998 if (of_elf32
.current_dfmt
== &df_stabs
) {
999 /* in case the debug information is wanted, just add these three sections... */
1000 add_sectname("", ".stab");
1001 add_sectname("", ".stabstr");
1002 add_sectname(".rel", ".stab");
1003 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1004 /* the dwarf debug standard specifies the following ten sections,
1005 not all of which are currently implemented,
1006 although all of them are defined. */
1007 add_sectname("", ".debug_aranges");
1008 add_sectname(".rela", ".debug_aranges");
1009 add_sectname("", ".debug_pubnames");
1010 add_sectname("", ".debug_info");
1011 add_sectname(".rela", ".debug_info");
1012 add_sectname("", ".debug_abbrev");
1013 add_sectname("", ".debug_line");
1014 add_sectname(".rela", ".debug_line");
1015 add_sectname("", ".debug_frame");
1016 add_sectname("", ".debug_loc");
1020 * Output the ELF header.
1022 fwrite("\177ELF\1\1\1", 7, 1, elffp
);
1023 fputc(elf_osabi
, elffp
);
1024 fputc(elf_abiver
, elffp
);
1025 fwritezero(7, elffp
);
1026 fwriteint16_t(1, elffp
); /* ET_REL relocatable file */
1027 fwriteint16_t(3, elffp
); /* EM_386 processor ID */
1028 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1029 fwriteint32_t(0L, elffp
); /* no entry point */
1030 fwriteint32_t(0L, elffp
); /* no program header table */
1031 fwriteint32_t(0x40L
, elffp
); /* section headers straight after
1032 * ELF header plus alignment */
1033 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1034 fwriteint16_t(0x34, elffp
); /* size of ELF header */
1035 fwriteint16_t(0, elffp
); /* no program header table, again */
1036 fwriteint16_t(0, elffp
); /* still no program header table */
1037 fwriteint16_t(0x28, elffp
); /* size of section header */
1038 fwriteint16_t(nsections
, elffp
); /* number of sections */
1039 fwriteint16_t(sec_shstrtab
, elffp
); /* string table section index for
1040 * section header table */
1041 fwriteint32_t(0L, elffp
); /* align to 0x40 bytes */
1042 fwriteint32_t(0L, elffp
);
1043 fwriteint32_t(0L, elffp
);
1046 * Build the symbol table and relocation tables.
1048 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1049 for (i
= 0; i
< nsects
; i
++)
1051 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1055 * Now output the section header table.
1058 elf_foffs
= 0x40 + 0x28 * nsections
;
1059 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1062 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1065 elf_section_header(0, SHT_NULL
, 0, NULL
, false, 0, SHN_UNDEF
, 0, 0, 0);
1068 /* The normal sections */
1069 for (i
= 0; i
< nsects
; i
++) {
1070 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1071 (sects
[i
]->type
== SHT_PROGBITS
?
1072 sects
[i
]->data
: NULL
), true,
1073 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1078 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, shstrtab
, false,
1079 shstrtablen
, 0, 0, 1, 0);
1083 elf_section_header(p
- shstrtab
, SHT_SYMTAB
, 0, symtab
, true,
1084 symtablen
, sec_strtab
, symtablocal
, 4, 16);
1088 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, strs
, true,
1089 strslen
, 0, 0, 1, 0);
1092 /* The relocation sections */
1093 for (i
= 0; i
< nsects
; i
++)
1094 if (sects
[i
]->head
) {
1095 elf_section_header(p
- shstrtab
, SHT_REL
, 0, sects
[i
]->rel
, true,
1096 sects
[i
]->rellen
, sec_symtab
, i
+ 1, 4, 8);
1101 if (of_elf32
.current_dfmt
== &df_stabs
) {
1102 /* for debugging information, create the last three sections
1103 which are the .stab , .stabstr and .rel.stab sections respectively */
1105 /* this function call creates the stab sections in memory */
1108 if (stabbuf
&& stabstrbuf
&& stabrelbuf
) {
1109 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, stabbuf
, false,
1110 stablen
, sec_stabstr
, 0, 4, 12);
1113 elf_section_header(p
- shstrtab
, SHT_STRTAB
, 0, stabstrbuf
, false,
1114 stabstrlen
, 0, 0, 4, 0);
1117 /* link -> symtable info -> section to refer to */
1118 elf_section_header(p
- shstrtab
, SHT_REL
, 0, stabrelbuf
, false,
1119 stabrellen
, sec_symtab
, sec_stab
, 4, 8);
1122 } else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1123 /* for dwarf debugging information, create the ten dwarf sections */
1125 /* this function call creates the dwarf sections in memory */
1129 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1130 arangeslen
, 0, 0, 1, 0);
1133 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1134 arangesrellen
, sec_symtab
, sec_debug_aranges
,
1138 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
,
1139 false, pubnameslen
, 0, 0, 1, 0);
1142 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1143 infolen
, 0, 0, 1, 0);
1146 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1147 inforellen
, sec_symtab
, sec_debug_info
, 1, 12);
1150 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1151 abbrevlen
, 0, 0, 1, 0);
1154 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1155 linelen
, 0, 0, 1, 0);
1158 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1159 linerellen
, sec_symtab
, sec_debug_line
, 1, 12);
1162 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1163 framelen
, 0, 0, 8, 0);
1166 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1167 loclen
, 0, 0, 1, 0);
1170 fwritezero(align
, elffp
);
1173 * Now output the sections.
1175 elf_write_sections();
1177 nasm_free(elf_sects
);
1181 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1183 struct SAA
*s
= saa_init(1L);
1185 uint8_t entry
[16], *p
;
1191 * First, an all-zeros entry, required by the ELF spec.
1193 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1198 * Next, an entry for the file name.
1201 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1202 WRITELONG(p
, 0); /* no value */
1203 WRITELONG(p
, 0); /* no size either */
1204 WRITESHORT(p
, STT_FILE
); /* type FILE */
1205 WRITESHORT(p
, SHN_ABS
);
1206 saa_wbytes(s
, entry
, 16L);
1211 * Now some standard symbols defining the segments, for relocation
1214 for (i
= 1; i
<= nsects
; i
++) {
1216 WRITELONG(p
, 0); /* no symbol name */
1217 WRITELONG(p
, 0); /* offset zero */
1218 WRITELONG(p
, 0); /* size zero */
1219 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1220 WRITESHORT(p
, i
); /* section id */
1221 saa_wbytes(s
, entry
, 16L);
1227 * Now the other local symbols.
1230 while ((sym
= saa_rstruct(syms
))) {
1231 if (sym
->type
& SYM_GLOBAL
)
1234 WRITELONG(p
, sym
->strpos
);
1235 WRITELONG(p
, sym
->symv
.key
);
1236 WRITELONG(p
, sym
->size
);
1237 WRITECHAR(p
, sym
->type
); /* type and binding */
1238 WRITECHAR(p
, sym
->other
); /* visibility */
1239 WRITESHORT(p
, sym
->section
);
1240 saa_wbytes(s
, entry
, 16L);
1245 * dwarf needs symbols for debug sections
1246 * which are relocation targets.
1248 //*** fix for 32 bit
1249 if (of_elf32
.current_dfmt
== &df_dwarf
) {
1250 dwarf_infosym
= *local
;
1252 WRITELONG(p
, 0); /* no symbol name */
1253 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1254 WRITELONG(p
, (uint32_t) 0); /* size zero */
1255 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1256 WRITESHORT(p
, sec_debug_info
); /* section id */
1257 saa_wbytes(s
, entry
, 16L);
1260 dwarf_abbrevsym
= *local
;
1262 WRITELONG(p
, 0); /* no symbol name */
1263 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1264 WRITELONG(p
, (uint32_t) 0); /* size zero */
1265 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1266 WRITESHORT(p
, sec_debug_abbrev
); /* section id */
1267 saa_wbytes(s
, entry
, 16L);
1270 dwarf_linesym
= *local
;
1272 WRITELONG(p
, 0); /* no symbol name */
1273 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1274 WRITELONG(p
, (uint32_t) 0); /* size zero */
1275 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1276 WRITESHORT(p
, sec_debug_line
); /* section id */
1277 saa_wbytes(s
, entry
, 16L);
1283 * Now the global symbols.
1286 while ((sym
= saa_rstruct(syms
))) {
1287 if (!(sym
->type
& SYM_GLOBAL
))
1290 WRITELONG(p
, sym
->strpos
);
1291 WRITELONG(p
, sym
->symv
.key
);
1292 WRITELONG(p
, sym
->size
);
1293 WRITECHAR(p
, sym
->type
); /* type and binding */
1294 WRITECHAR(p
, sym
->other
); /* visibility */
1295 WRITESHORT(p
, sym
->section
);
1296 saa_wbytes(s
, entry
, 16L);
1303 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1306 uint8_t *p
, entry
[8];
1307 int32_t global_offset
;
1316 * How to onvert from a global placeholder to a real symbol index;
1317 * the +2 refers to the two special entries, the null entry and
1318 * the filename entry.
1320 global_offset
= -GLOBAL_TEMP_BASE
+ nsects
+ nlocals
+ ndebugs
+ 2;
1323 int32_t sym
= r
->symbol
;
1326 * Create a real symbol index; the +2 refers to the two special
1327 * entries, the null entry and the filename entry.
1329 if (sym
>= GLOBAL_TEMP_BASE
)
1330 sym
+= global_offset
;
1333 WRITELONG(p
, r
->address
);
1334 WRITELONG(p
, (sym
<< 8) + r
->type
);
1335 saa_wbytes(s
, entry
, 8L);
1344 static void elf_section_header(int name
, int type
, int flags
,
1345 void *data
, bool is_saa
, int32_t datalen
,
1346 int link
, int info
, int align
, int eltsize
)
1348 elf_sects
[elf_nsect
].data
= data
;
1349 elf_sects
[elf_nsect
].len
= datalen
;
1350 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1353 fwriteint32_t((int32_t)name
, elffp
);
1354 fwriteint32_t((int32_t)type
, elffp
);
1355 fwriteint32_t((int32_t)flags
, elffp
);
1356 fwriteint32_t(0L, elffp
); /* no address, ever, in object files */
1357 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1358 fwriteint32_t(datalen
, elffp
);
1360 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1361 fwriteint32_t((int32_t)link
, elffp
);
1362 fwriteint32_t((int32_t)info
, elffp
);
1363 fwriteint32_t((int32_t)align
, elffp
);
1364 fwriteint32_t((int32_t)eltsize
, elffp
);
1367 static void elf_write_sections(void)
1370 for (i
= 0; i
< elf_nsect
; i
++)
1371 if (elf_sects
[i
].data
) {
1372 int32_t len
= elf_sects
[i
].len
;
1373 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1374 int32_t align
= reallen
- len
;
1375 if (elf_sects
[i
].is_saa
)
1376 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1378 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1379 fwritezero(align
, elffp
);
1383 static void elf_sect_write(struct Section
*sect
,
1384 const uint8_t *data
, uint32_t len
)
1386 saa_wbytes(sect
->data
, data
, len
);
1390 static int32_t elf_segbase(int32_t segment
)
1395 static int elf_directive(enum directives directive
, char *value
, int pass
)
1401 switch (directive
) {
1404 return 1; /* ignore in pass 2 */
1406 n
= readnum(value
, &err
);
1408 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1411 if (n
< 0 || n
> 255) {
1412 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1418 if ((p
= strchr(value
,',')) == NULL
)
1421 n
= readnum(p
+1, &err
);
1422 if (err
|| n
< 0 || n
> 255) {
1423 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1435 static void elf_filename(char *inname
, char *outname
, efunc error
)
1437 strcpy(elf_module
, inname
);
1438 standard_extension(inname
, outname
, ".o", error
);
1441 extern macros_t elf_stdmac
[];
1443 static int elf_set_info(enum geninfo type
, char **val
)
1449 static struct dfmt df_dwarf
= {
1450 "ELF32 (i386) dwarf debug format for Linux/Unix",
1460 static struct dfmt df_stabs
= {
1461 "ELF32 (i386) stabs debug format for Linux/Unix",
1472 struct dfmt
*elf32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1474 struct ofmt of_elf32
= {
1475 "ELF32 (i386) object files (e.g. Linux)",
1492 struct ofmt of_elf
= {
1493 "ELF (short name for ELF32) ",
1509 /* again, the stabs debugging stuff (code) */
1511 static void stabs32_linenum(const char *filename
, int32_t linenumber
,
1516 if (!stabs_filename
) {
1517 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1518 strcpy(stabs_filename
, filename
);
1520 if (strcmp(stabs_filename
, filename
)) {
1521 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1522 in fact, this leak comes in quite handy to maintain a list of files
1523 encountered so far in the symbol lines... */
1525 /* why not nasm_free(stabs_filename); we're done with the old one */
1527 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1528 strcpy(stabs_filename
, filename
);
1532 currentline
= linenumber
;
1535 static void debug32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1545 static void debug32_directive(const char *directive
, const char *params
)
1551 static void debug32_typevalue(int32_t type
)
1553 int32_t stype
, ssize
;
1554 switch (TYM_TYPE(type
)) {
1597 stype
= STT_SECTION
;
1612 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1613 lastsym
->size
= ssize
;
1614 lastsym
->type
= stype
;
1618 static void stabs32_output(int type
, void *param
)
1620 struct symlininfo
*s
;
1621 struct linelist
*el
;
1622 if (type
== TY_STABSSYMLIN
) {
1623 if (debug_immcall
) {
1624 s
= (struct symlininfo
*)param
;
1625 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1626 return; /* we are only interested in the text stuff */
1628 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1629 el
->info
.offset
= s
->offset
;
1630 el
->info
.section
= s
->section
;
1631 el
->info
.name
= s
->name
;
1632 el
->line
= currentline
;
1633 el
->filename
= stabs_filename
;
1636 stabslines
->last
->next
= el
;
1637 stabslines
->last
= el
;
1640 stabslines
->last
= el
;
1647 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1649 WRITELONG(p,n_strx); \
1650 WRITECHAR(p,n_type); \
1651 WRITECHAR(p,n_other); \
1652 WRITESHORT(p,n_desc); \
1653 WRITELONG(p,n_value); \
1656 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1658 static void stabs32_generate(void)
1660 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1661 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1665 struct linelist
*ptr
;
1669 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1670 for (i
= 0; i
< numlinestabs
; i
++)
1674 if (numfiles
== 0) {
1675 allfiles
[0] = ptr
->filename
;
1678 for (i
= 0; i
< numfiles
; i
++) {
1679 if (!strcmp(allfiles
[i
], ptr
->filename
))
1682 if (i
>= numfiles
) {
1683 allfiles
[i
] = ptr
->filename
;
1690 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1691 for (i
= 0; i
< numfiles
; i
++) {
1692 fileidx
[i
] = strsize
;
1693 strsize
+= strlen(allfiles
[i
]) + 1;
1696 for (i
= 0; i
< numfiles
; i
++) {
1697 if (!strcmp(allfiles
[i
], elf_module
)) {
1703 /* worst case size of the stab buffer would be:
1704 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1707 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1708 sizeof(struct stabentry
));
1710 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1712 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1715 for (i
= 0; i
< numfiles
; i
++) {
1716 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1720 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1727 /* this is the first stab, its strx points to the filename of the
1728 the source-file, the n_desc field should be set to the number
1731 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1733 /* this is the stab for the main source file */
1734 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1736 /* relocation table entry */
1738 /* Since the symbol table has two entries before */
1739 /* the section symbols, the index in the info.section */
1740 /* member must be adjusted by adding 2 */
1742 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1743 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1746 currfile
= mainfileindex
;
1750 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1751 /* oops file has changed... */
1752 for (i
= 0; i
< numfiles
; i
++)
1753 if (!strcmp(allfiles
[i
], ptr
->filename
))
1756 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1760 /* relocation table entry */
1761 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1762 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1765 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1768 /* relocation table entry */
1770 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1771 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1777 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1779 nasm_free(allfiles
);
1782 stablen
= (sptr
- sbuf
);
1783 stabrellen
= (rptr
- rbuf
);
1789 static void stabs32_cleanup(void)
1791 struct linelist
*ptr
, *del
;
1803 nasm_free(stabrelbuf
);
1805 nasm_free(stabstrbuf
);
1808 /* dwarf routines */
1810 static void dwarf32_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1817 ndebugs
= 3; /* 3 debug symbols */
1820 static void dwarf32_linenum(const char *filename
, int32_t linenumber
,
1824 dwarf32_findfile(filename
);
1826 currentline
= linenumber
;
1829 /* called from elf_out with type == TY_DEBUGSYMLIN */
1830 static void dwarf32_output(int type
, void *param
)
1832 int ln
, aa
, inx
, maxln
, soc
;
1833 struct symlininfo
*s
;
1838 s
= (struct symlininfo
*)param
;
1839 /* line number info is only gathered for executable sections */
1840 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1842 /* Check if section index has changed */
1843 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1845 dwarf32_findsect(s
->section
);
1847 /* do nothing unless line or file has changed */
1850 ln
= currentline
- dwarf_csect
->line
;
1851 aa
= s
->offset
- dwarf_csect
->offset
;
1852 inx
= dwarf_clist
->line
;
1853 plinep
= dwarf_csect
->psaa
;
1854 /* check for file change */
1855 if (!(inx
== dwarf_csect
->file
))
1857 saa_write8(plinep
,DW_LNS_set_file
);
1858 saa_write8(plinep
,inx
);
1859 dwarf_csect
->file
= inx
;
1861 /* check for line change */
1864 /* test if in range of special op code */
1865 maxln
= line_base
+ line_range
;
1866 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1867 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1869 saa_write8(plinep
,soc
);
1875 saa_write8(plinep
,DW_LNS_advance_line
);
1876 saa_wleb128s(plinep
,ln
);
1880 saa_write8(plinep
,DW_LNS_advance_pc
);
1881 saa_wleb128u(plinep
,aa
);
1884 dwarf_csect
->line
= currentline
;
1885 dwarf_csect
->offset
= s
->offset
;
1887 /* show change handled */
1893 static void dwarf32_generate(void)
1897 struct linelist
*ftentry
;
1898 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1899 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1900 struct sectlist
*psect
;
1901 size_t saalen
, linepoff
, totlen
, highaddr
;
1903 /* write epilogues for each line program range */
1904 /* and build aranges section */
1905 paranges
= saa_init(1L);
1906 parangesrel
= saa_init(1L);
1907 saa_write16(paranges
,2); /* dwarf version */
1908 saa_write32(parangesrel
, paranges
->datalen
+4);
1909 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
1910 saa_write32(parangesrel
, 0);
1911 saa_write32(paranges
,0); /* offset into info */
1912 saa_write8(paranges
,4); /* pointer size */
1913 saa_write8(paranges
,0); /* not segmented */
1914 saa_write32(paranges
,0); /* padding */
1915 /* iterate though sectlist entries */
1916 psect
= dwarf_fsect
;
1919 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1921 plinep
= psect
->psaa
;
1922 /* Line Number Program Epilogue */
1923 saa_write8(plinep
,2); /* std op 2 */
1924 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1925 saa_write8(plinep
,DW_LNS_extended_op
);
1926 saa_write8(plinep
,1); /* operand length */
1927 saa_write8(plinep
,DW_LNE_end_sequence
);
1928 totlen
+= plinep
->datalen
;
1929 /* range table relocation entry */
1930 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1931 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
1932 saa_write32(parangesrel
, (uint32_t) 0);
1933 /* range table entry */
1934 saa_write32(paranges
,0x0000); /* range start */
1935 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1936 highaddr
+= sects
[psect
->section
]->len
;
1937 /* done with this entry */
1938 psect
= psect
->next
;
1940 saa_write32(paranges
,0); /* null address */
1941 saa_write32(paranges
,0); /* null length */
1942 saalen
= paranges
->datalen
;
1943 arangeslen
= saalen
+ 4;
1944 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1945 WRITELONG(pbuf
,saalen
); /* initial length */
1946 saa_rnbytes(paranges
, pbuf
, saalen
);
1949 /* build rela.aranges section */
1950 arangesrellen
= saalen
= parangesrel
->datalen
;
1951 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1952 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1953 saa_free(parangesrel
);
1955 /* build pubnames section */
1956 ppubnames
= saa_init(1L);
1957 saa_write16(ppubnames
,3); /* dwarf version */
1958 saa_write32(ppubnames
,0); /* offset into info */
1959 saa_write32(ppubnames
,0); /* space used in info */
1960 saa_write32(ppubnames
,0); /* end of list */
1961 saalen
= ppubnames
->datalen
;
1962 pubnameslen
= saalen
+ 4;
1963 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1964 WRITELONG(pbuf
,saalen
); /* initial length */
1965 saa_rnbytes(ppubnames
, pbuf
, saalen
);
1966 saa_free(ppubnames
);
1968 /* build info section */
1969 pinfo
= saa_init(1L);
1970 pinforel
= saa_init(1L);
1971 saa_write16(pinfo
,2); /* dwarf version */
1972 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1973 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
1974 saa_write32(pinforel
, 0);
1975 saa_write32(pinfo
,0); /* offset into abbrev */
1976 saa_write8(pinfo
,4); /* pointer size */
1977 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
1978 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1979 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1980 saa_write32(pinforel
, 0);
1981 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1982 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1983 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1984 saa_write32(pinforel
, 0);
1985 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
1986 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1987 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
1988 saa_write32(pinforel
, 0);
1989 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
1990 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
1991 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
1992 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
1993 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
1994 saa_write32(pinforel
, pinfo
->datalen
+ 4);
1995 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
1996 saa_write32(pinforel
, 0);
1997 saa_write32(pinfo
,0); /* DW_AT_low_pc */
1998 saa_write32(pinfo
,0); /* DW_AT_frame_base */
1999 saa_write8(pinfo
,0); /* end of entries */
2000 saalen
= pinfo
->datalen
;
2001 infolen
= saalen
+ 4;
2002 infobuf
= pbuf
= nasm_malloc(infolen
);
2003 WRITELONG(pbuf
,saalen
); /* initial length */
2004 saa_rnbytes(pinfo
, pbuf
, saalen
);
2007 /* build rela.info section */
2008 inforellen
= saalen
= pinforel
->datalen
;
2009 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2010 saa_rnbytes(pinforel
, pbuf
, saalen
);
2013 /* build abbrev section */
2014 pabbrev
= saa_init(1L);
2015 saa_write8(pabbrev
,1); /* entry number LEB128u */
2016 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2017 saa_write8(pabbrev
,1); /* has children */
2018 /* the following attributes and forms are all LEB128u values */
2019 saa_write8(pabbrev
,DW_AT_low_pc
);
2020 saa_write8(pabbrev
,DW_FORM_addr
);
2021 saa_write8(pabbrev
,DW_AT_high_pc
);
2022 saa_write8(pabbrev
,DW_FORM_addr
);
2023 saa_write8(pabbrev
,DW_AT_stmt_list
);
2024 saa_write8(pabbrev
,DW_FORM_data4
);
2025 saa_write8(pabbrev
,DW_AT_name
);
2026 saa_write8(pabbrev
,DW_FORM_string
);
2027 saa_write8(pabbrev
,DW_AT_producer
);
2028 saa_write8(pabbrev
,DW_FORM_string
);
2029 saa_write8(pabbrev
,DW_AT_language
);
2030 saa_write8(pabbrev
,DW_FORM_data2
);
2031 saa_write16(pabbrev
,0); /* end of entry */
2032 /* LEB128u usage same as above */
2033 saa_write8(pabbrev
,2); /* entry number */
2034 saa_write8(pabbrev
,DW_TAG_subprogram
);
2035 saa_write8(pabbrev
,0); /* no children */
2036 saa_write8(pabbrev
,DW_AT_low_pc
);
2037 saa_write8(pabbrev
,DW_FORM_addr
);
2038 saa_write8(pabbrev
,DW_AT_frame_base
);
2039 saa_write8(pabbrev
,DW_FORM_data4
);
2040 saa_write16(pabbrev
,0); /* end of entry */
2041 abbrevlen
= saalen
= pabbrev
->datalen
;
2042 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2043 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2046 /* build line section */
2048 plines
= saa_init(1L);
2049 saa_write8(plines
,1); /* Minimum Instruction Length */
2050 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2051 saa_write8(plines
,line_base
); /* Line Base */
2052 saa_write8(plines
,line_range
); /* Line Range */
2053 saa_write8(plines
,opcode_base
); /* Opcode Base */
2054 /* standard opcode lengths (# of LEB128u operands) */
2055 saa_write8(plines
,0); /* Std opcode 1 length */
2056 saa_write8(plines
,1); /* Std opcode 2 length */
2057 saa_write8(plines
,1); /* Std opcode 3 length */
2058 saa_write8(plines
,1); /* Std opcode 4 length */
2059 saa_write8(plines
,1); /* Std opcode 5 length */
2060 saa_write8(plines
,0); /* Std opcode 6 length */
2061 saa_write8(plines
,0); /* Std opcode 7 length */
2062 saa_write8(plines
,0); /* Std opcode 8 length */
2063 saa_write8(plines
,1); /* Std opcode 9 length */
2064 saa_write8(plines
,0); /* Std opcode 10 length */
2065 saa_write8(plines
,0); /* Std opcode 11 length */
2066 saa_write8(plines
,1); /* Std opcode 12 length */
2067 /* Directory Table */
2068 saa_write8(plines
,0); /* End of table */
2069 /* File Name Table */
2070 ftentry
= dwarf_flist
;
2071 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2073 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2074 saa_write8(plines
,0); /* directory LEB128u */
2075 saa_write8(plines
,0); /* time LEB128u */
2076 saa_write8(plines
,0); /* size LEB128u */
2077 ftentry
= ftentry
->next
;
2079 saa_write8(plines
,0); /* End of table */
2080 linepoff
= plines
->datalen
;
2081 linelen
= linepoff
+ totlen
+ 10;
2082 linebuf
= pbuf
= nasm_malloc(linelen
);
2083 WRITELONG(pbuf
,linelen
-4); /* initial length */
2084 WRITESHORT(pbuf
,3); /* dwarf version */
2085 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2086 /* write line header */
2088 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2091 /* concatonate line program ranges */
2093 plinesrel
= saa_init(1L);
2094 psect
= dwarf_fsect
;
2095 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2097 saa_write32(plinesrel
, linepoff
);
2098 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2099 saa_write32(plinesrel
, (uint32_t) 0);
2100 plinep
= psect
->psaa
;
2101 saalen
= plinep
->datalen
;
2102 saa_rnbytes(plinep
, pbuf
, saalen
);
2106 /* done with this entry */
2107 psect
= psect
->next
;
2111 /* build rela.lines section */
2112 linerellen
=saalen
= plinesrel
->datalen
;
2113 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2114 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2115 saa_free(plinesrel
);
2117 /* build frame section */
2119 framebuf
= pbuf
= nasm_malloc(framelen
);
2120 WRITELONG(pbuf
,framelen
-4); /* initial length */
2122 /* build loc section */
2124 locbuf
= pbuf
= nasm_malloc(loclen
);
2125 WRITELONG(pbuf
,0); /* null beginning offset */
2126 WRITELONG(pbuf
,0); /* null ending offset */
2129 static void dwarf32_cleanup(void)
2132 nasm_free(arangesbuf
);
2134 nasm_free(arangesrelbuf
);
2136 nasm_free(pubnamesbuf
);
2140 nasm_free(inforelbuf
);
2142 nasm_free(abbrevbuf
);
2146 nasm_free(linerelbuf
);
2148 nasm_free(framebuf
);
2152 static void dwarf32_findfile(const char * fname
)
2155 struct linelist
*match
;
2157 /* return if fname is current file name */
2158 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2159 /* search for match */
2165 match
= dwarf_flist
;
2166 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2168 if (!(strcmp(fname
, match
->filename
)))
2170 dwarf_clist
= match
;
2175 /* add file name to end of list */
2176 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2178 dwarf_clist
->line
= dwarf_numfiles
;
2179 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2180 strcpy(dwarf_clist
->filename
,fname
);
2181 dwarf_clist
->next
= 0;
2182 /* if first entry */
2185 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2186 dwarf_clist
->last
= 0;
2188 /* chain to previous entry */
2191 dwarf_elist
->next
= dwarf_clist
;
2192 dwarf_elist
= dwarf_clist
;
2197 static void dwarf32_findsect(const int index
)
2200 struct sectlist
*match
;
2202 /* return if index is current section index */
2203 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2207 /* search for match */
2213 match
= dwarf_fsect
;
2214 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2216 if ((match
->section
== index
))
2218 dwarf_csect
= match
;
2221 match
= match
->next
;
2224 /* add entry to end of list */
2225 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2227 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2228 dwarf_csect
->line
= 1;
2229 dwarf_csect
->offset
= 0;
2230 dwarf_csect
->file
= 1;
2231 dwarf_csect
->section
= index
;
2232 dwarf_csect
->next
= 0;
2233 /* set relocatable address at start of line program */
2234 saa_write8(plinep
,DW_LNS_extended_op
);
2235 saa_write8(plinep
,5); /* operand length */
2236 saa_write8(plinep
,DW_LNE_set_address
);
2237 saa_write32(plinep
,0); /* Start Address */
2238 /* if first entry */
2241 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2242 dwarf_csect
->last
= 0;
2244 /* chain to previous entry */
2247 dwarf_esect
->next
= dwarf_csect
;
2248 dwarf_esect
= dwarf_csect
;