1 /* outelf.c output routines for the Netwide Assembler to produce
2 * ELF32 (i386 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
31 R_386_32
= 1, /* ordinary absolute relocation */
32 R_386_PC32
= 2, /* PC-relative relocation */
33 R_386_GOT32
= 3, /* an offset into GOT */
34 R_386_PLT32
= 4, /* a PC-relative offset into PLT */
35 R_386_COPY
= 5, /* ??? */
36 R_386_GLOB_DAT
= 6, /* ??? */
37 R_386_JUMP_SLOT
= 7, /* ??? */
38 R_386_RELATIVE
= 8, /* ??? */
39 R_386_GOTOFF
= 9, /* an offset from GOT base */
40 R_386_GOTPC
= 10, /* a PC-relative offset _to_ GOT */
41 /* These are GNU extensions, but useful */
42 R_386_16
= 20, /* A 16-bit absolute relocation */
43 R_386_PC16
= 21, /* A 16-bit PC-relative relocation */
44 R_386_8
= 22, /* An 8-bit absolute relocation */
45 R_386_PC8
= 23 /* An 8-bit PC-relative relocation */
50 int32_t address
; /* relative to _start_ of section */
51 int32_t symbol
; /* symbol index */
52 int type
; /* type of relocation */
56 int32_t strpos
; /* string table position of name */
57 int32_t section
; /* section ID of the symbol */
58 int type
; /* symbol type */
59 int other
; /* symbol visibility */
60 int32_t value
; /* address, or COMMON variable align */
61 int32_t size
; /* size of symbol */
62 int32_t globnum
; /* symbol table offset if global */
63 struct Symbol
*next
; /* list of globals in each section */
64 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
65 char *name
; /* used temporarily if in above list */
68 #define SHT_PROGBITS 1
73 #define SHF_EXECINSTR 4
77 uint32_t len
, size
, nrelocs
;
79 int type
; /* SHT_PROGBITS or SHT_NOBITS */
80 int align
; /* alignment: power of two */
81 uint32_t flags
; /* section flags */
85 struct Reloc
*head
, **tail
;
86 struct Symbol
*gsyms
; /* global symbols in section */
90 static struct Section
**sects
;
91 static int nsects
, sectlen
;
93 #define SHSTR_DELTA 256
94 static char *shstrtab
;
95 static int shstrtablen
, shstrtabsize
;
97 static struct SAA
*syms
;
98 static uint32_t nlocals
, nglobs
;
100 static int32_t def_seg
;
102 static struct RAA
*bsym
;
104 static struct SAA
*strs
;
105 static uint32_t strslen
;
109 static evalfunc evaluate
;
111 static struct Symbol
*fwds
;
113 static char elf_module
[FILENAME_MAX
];
115 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
116 static uint8_t elf_abiver
= 0; /* Current ABI version */
118 extern struct ofmt of_elf32
;
119 extern struct ofmt of_elf
;
121 #define SHN_ABS 0xFFF1
122 #define SHN_COMMON 0xFFF2
125 #define SYM_GLOBAL 0x10
127 #define SHT_RELA 4 /* Relocation entries with addends */
129 #define STT_NOTYPE 0 /* Symbol type is unspecified */
130 #define STT_OBJECT 1 /* Symbol is a data object */
131 #define STT_FUNC 2 /* Symbol is a code object */
132 #define STT_SECTION 3 /* Symbol associated with a section */
133 #define STT_FILE 4 /* Symbol's name is file name */
134 #define STT_COMMON 5 /* Symbol is a common data object */
135 #define STT_TLS 6 /* Symbol is thread-local data object*/
136 #define STT_NUM 7 /* Number of defined types. */
138 #define STV_DEFAULT 0
139 #define STV_INTERNAL 1
141 #define STV_PROTECTED 3
143 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
145 #define SEG_ALIGN 16 /* alignment of sections in file */
146 #define SEG_ALIGN_1 (SEG_ALIGN-1)
148 /* Definitions in lieu of dwarf.h */
149 #define DW_TAG_compile_unit 0x11
150 #define DW_TAG_subprogram 0x2e
151 #define DW_AT_name 0x03
152 #define DW_AT_stmt_list 0x10
153 #define DW_AT_low_pc 0x11
154 #define DW_AT_high_pc 0x12
155 #define DW_AT_language 0x13
156 #define DW_AT_producer 0x25
157 #define DW_AT_frame_base 0x40
158 #define DW_FORM_addr 0x01
159 #define DW_FORM_data2 0x05
160 #define DW_FORM_data4 0x06
161 #define DW_FORM_string 0x08
162 #define DW_LNS_extended_op 0
163 #define DW_LNS_advance_pc 2
164 #define DW_LNS_advance_line 3
165 #define DW_LNS_set_file 4
166 #define DW_LNE_end_sequence 1
167 #define DW_LNE_set_address 2
168 #define DW_LNE_define_file 3
169 #define DW_LANG_Mips_Assembler 0x8001
171 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
173 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
175 static struct ELF_SECTDATA
{
180 static int elf_nsect
, nsections
;
181 static int32_t elf_foffs
;
183 static void elf_write(void);
184 static void elf_sect_write(struct Section
*, const uint8_t *,
186 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
188 static void elf_write_sections(void);
189 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
190 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
191 static void add_sectname(char *, char *);
193 /* this stuff is needed for the stabs debugging format */
194 #define N_SO 0x64 /* ID for main source file */
195 #define N_SOL 0x84 /* ID for sub-source file */
199 #define TY_STABSSYMLIN 0x40 /* ouch */
215 int section
; /* section index */
216 char *name
; /* shallow-copied pointer of section name */
220 struct symlininfo info
;
223 struct linelist
*next
;
224 struct linelist
*last
;
233 struct sectlist
*next
;
234 struct sectlist
*last
;
237 /* common debug variables */
238 static int currentline
= 1;
239 static int debug_immcall
= 0;
241 /* stabs debug variables */
242 static struct linelist
*stabslines
= 0;
243 static int numlinestabs
= 0;
244 static char *stabs_filename
= 0;
245 static int symtabsection
;
246 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
247 static int stablen
, stabstrlen
, stabrellen
;
249 /* dwarf debug variables */
250 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
251 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
252 static int dwarf_numfiles
= 0, dwarf_nsections
;
253 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
254 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
255 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
256 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
257 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
258 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
260 static struct dfmt df_dwarf
;
261 static struct dfmt df_stabs
;
262 static struct Symbol
*lastsym
;
264 /* common debugging routines */
265 void debug32_typevalue(int32_t);
266 void debug32_init(struct ofmt
*, void *, FILE *, efunc
);
267 void debug32_deflabel(char *, int32_t, int64_t, int, char *);
268 void debug32_directive(const char *, const char *);
270 /* stabs debugging routines */
271 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
272 void stabs32_output(int, void *);
273 void stabs32_generate(void);
274 void stabs32_cleanup(void);
276 /* dwarf debugging routines */
277 void dwarf32_linenum(const char *filename
, int32_t linenumber
, int32_t);
278 void dwarf32_output(int, void *);
279 void dwarf32_generate(void);
280 void dwarf32_cleanup(void);
281 void dwarf32_findfile(const char *);
282 void dwarf32_findsect(const int);
283 void saa_wleb128u(struct SAA
*, int);
284 void saa_wleb128s(struct SAA
*, int);
287 * Special section numbers which are used to define ELF special
288 * symbols, which can be used with WRT to provide PIC relocation
291 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
292 static int32_t elf_got_sect
, elf_plt_sect
;
293 static int32_t elf_sym_sect
;
295 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
297 if (of_elf
.current_dfmt
!= &null_debug_form
)
298 of_elf32
.current_dfmt
= of_elf
.current_dfmt
;
302 (void)ldef
; /* placate optimisers */
304 nsects
= sectlen
= 0;
305 syms
= saa_init((int32_t)sizeof(struct Symbol
));
306 nlocals
= nglobs
= 0;
309 saa_wbytes(strs
, "\0", 1L);
310 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
311 strslen
= 2 + strlen(elf_module
);
313 shstrtablen
= shstrtabsize
= 0;;
314 add_sectname("", "");
318 elf_gotpc_sect
= seg_alloc();
319 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
321 elf_gotoff_sect
= seg_alloc();
322 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
324 elf_got_sect
= seg_alloc();
325 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
327 elf_plt_sect
= seg_alloc();
328 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
330 elf_sym_sect
= seg_alloc();
331 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
334 def_seg
= seg_alloc();
337 static void elf_cleanup(int debuginfo
)
346 for (i
= 0; i
< nsects
; i
++) {
347 if (sects
[i
]->type
!= SHT_NOBITS
)
348 saa_free(sects
[i
]->data
);
350 saa_free(sects
[i
]->rel
);
351 while (sects
[i
]->head
) {
353 sects
[i
]->head
= sects
[i
]->head
->next
;
361 if (of_elf32
.current_dfmt
) {
362 of_elf32
.current_dfmt
->cleanup();
366 static void add_sectname(char *firsthalf
, char *secondhalf
)
368 int len
= strlen(firsthalf
) + strlen(secondhalf
);
369 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
370 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
371 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
372 strcat(shstrtab
+ shstrtablen
, secondhalf
);
373 shstrtablen
+= len
+ 1;
376 static int elf_make_section(char *name
, int type
, int flags
, int align
)
380 s
= nasm_malloc(sizeof(*s
));
382 if (type
!= SHT_NOBITS
)
383 s
->data
= saa_init(1L);
386 s
->len
= s
->size
= 0;
388 if (!strcmp(name
, ".text"))
391 s
->index
= seg_alloc();
392 add_sectname("", name
);
393 s
->name
= nasm_malloc(1 + strlen(name
));
394 strcpy(s
->name
, name
);
400 if (nsects
>= sectlen
)
402 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
408 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
411 unsigned flags_and
, flags_or
;
415 * Default is 32 bits.
423 while (*p
&& !isspace(*p
))
427 flags_and
= flags_or
= type
= align
= 0;
429 while (*p
&& isspace(*p
))
433 while (*p
&& !isspace(*p
))
437 while (*p
&& isspace(*p
))
440 if (!nasm_strnicmp(q
, "align=", 6)) {
444 if ((align
- 1) & align
) { /* means it's not a power of two */
445 error(ERR_NONFATAL
, "section alignment %d is not"
446 " a power of two", align
);
449 } else if (!nasm_stricmp(q
, "alloc")) {
450 flags_and
|= SHF_ALLOC
;
451 flags_or
|= SHF_ALLOC
;
452 } else if (!nasm_stricmp(q
, "noalloc")) {
453 flags_and
|= SHF_ALLOC
;
454 flags_or
&= ~SHF_ALLOC
;
455 } else if (!nasm_stricmp(q
, "exec")) {
456 flags_and
|= SHF_EXECINSTR
;
457 flags_or
|= SHF_EXECINSTR
;
458 } else if (!nasm_stricmp(q
, "noexec")) {
459 flags_and
|= SHF_EXECINSTR
;
460 flags_or
&= ~SHF_EXECINSTR
;
461 } else if (!nasm_stricmp(q
, "write")) {
462 flags_and
|= SHF_WRITE
;
463 flags_or
|= SHF_WRITE
;
464 } else if (!nasm_stricmp(q
, "nowrite")) {
465 flags_and
|= SHF_WRITE
;
466 flags_or
&= ~SHF_WRITE
;
467 } else if (!nasm_stricmp(q
, "progbits")) {
469 } else if (!nasm_stricmp(q
, "nobits")) {
474 if (!strcmp(name
, ".comment") ||
475 !strcmp(name
, ".shstrtab") ||
476 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
477 error(ERR_NONFATAL
, "attempt to redefine reserved section"
482 for (i
= 0; i
< nsects
; i
++)
483 if (!strcmp(name
, sects
[i
]->name
))
486 if (!strcmp(name
, ".text"))
487 i
= elf_make_section(name
, SHT_PROGBITS
,
488 SHF_ALLOC
| SHF_EXECINSTR
, 16);
489 else if (!strcmp(name
, ".rodata"))
490 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
491 else if (!strcmp(name
, ".data"))
492 i
= elf_make_section(name
, SHT_PROGBITS
,
493 SHF_ALLOC
| SHF_WRITE
, 4);
494 else if (!strcmp(name
, ".bss"))
495 i
= elf_make_section(name
, SHT_NOBITS
,
496 SHF_ALLOC
| SHF_WRITE
, 4);
498 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
500 sects
[i
]->type
= type
;
502 sects
[i
]->align
= align
;
503 sects
[i
]->flags
&= ~flags_and
;
504 sects
[i
]->flags
|= flags_or
;
505 } else if (pass
== 1) {
506 if ((type
&& sects
[i
]->type
!= type
)
507 || (align
&& sects
[i
]->align
!= align
)
508 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
509 error(ERR_WARNING
, "section attributes ignored on"
510 " redeclaration of section `%s'", name
);
513 return sects
[i
]->index
;
516 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
517 int is_global
, char *special
)
521 bool special_used
= false;
523 #if defined(DEBUG) && DEBUG>2
525 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
526 name
, segment
, offset
, is_global
, special
);
528 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
530 * This is a NASM special symbol. We never allow it into
531 * the ELF symbol table, even if it's a valid one. If it
532 * _isn't_ a valid one, we should barf immediately.
534 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
535 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
536 strcmp(name
, "..sym"))
537 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
541 if (is_global
== 3) {
544 * Fix up a forward-reference symbol size from the first
547 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
548 if (!strcmp((*s
)->name
, name
)) {
549 struct tokenval tokval
;
553 while (*p
&& !isspace(*p
))
555 while (*p
&& isspace(*p
))
559 tokval
.t_type
= TOKEN_INVALID
;
560 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
563 error(ERR_NONFATAL
, "cannot use relocatable"
564 " expression as symbol size");
566 (*s
)->size
= reloc_value(e
);
570 * Remove it from the list of unresolved sizes.
572 nasm_free((*s
)->name
);
576 return; /* it wasn't an important one */
579 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
580 strslen
+= 1 + strlen(name
);
582 lastsym
= sym
= saa_wstruct(syms
);
585 sym
->type
= is_global
? SYM_GLOBAL
: 0;
586 sym
->other
= STV_DEFAULT
;
588 if (segment
== NO_SEG
)
589 sym
->section
= SHN_ABS
;
592 sym
->section
= SHN_UNDEF
;
593 if (nsects
== 0 && segment
== def_seg
) {
595 if (segment
!= elf_section_names(".text", 2, &tempint
))
597 "strange segment conditions in ELF driver");
598 sym
->section
= nsects
;
600 for (i
= 0; i
< nsects
; i
++)
601 if (segment
== sects
[i
]->index
) {
602 sym
->section
= i
+ 1;
608 if (is_global
== 2) {
611 sym
->section
= SHN_COMMON
;
613 * We have a common variable. Check the special text to see
614 * if it's a valid number and power of two; if so, store it
615 * as the alignment for the common variable.
619 sym
->value
= readnum(special
, &err
);
621 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
622 " valid number", special
);
623 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
624 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
625 " power of two", special
);
629 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
631 if (sym
->type
== SYM_GLOBAL
) {
633 * If sym->section == SHN_ABS, then the first line of the
634 * else section would cause a core dump, because its a reference
635 * beyond the end of the section array.
636 * This behaviour is exhibited by this code:
639 * To avoid such a crash, such requests are silently discarded.
640 * This may not be the best solution.
642 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
643 bsym
= raa_write(bsym
, segment
, nglobs
);
644 } else if (sym
->section
!= SHN_ABS
) {
646 * This is a global symbol; so we must add it to the linked
647 * list of global symbols in its section. We'll push it on
648 * the beginning of the list, because it doesn't matter
649 * much which end we put it on and it's easier like this.
651 * In addition, we check the special text for symbol
652 * type and size information.
654 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
655 sects
[sym
->section
- 1]->gsyms
= sym
;
658 int n
= strcspn(special
, " \t");
660 if (!nasm_strnicmp(special
, "function", n
))
661 sym
->type
|= STT_FUNC
;
662 else if (!nasm_strnicmp(special
, "data", n
) ||
663 !nasm_strnicmp(special
, "object", n
))
664 sym
->type
|= STT_OBJECT
;
665 else if (!nasm_strnicmp(special
, "notype", n
))
666 sym
->type
|= STT_NOTYPE
;
668 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
672 while (isspace(*special
))
675 n
= strcspn(special
, " \t");
676 if (!nasm_strnicmp(special
, "default", n
))
677 sym
->other
= STV_DEFAULT
;
678 else if (!nasm_strnicmp(special
, "internal", n
))
679 sym
->other
= STV_INTERNAL
;
680 else if (!nasm_strnicmp(special
, "hidden", n
))
681 sym
->other
= STV_HIDDEN
;
682 else if (!nasm_strnicmp(special
, "protected", n
))
683 sym
->other
= STV_PROTECTED
;
690 struct tokenval tokval
;
693 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
695 while (special
[n
] && isspace(special
[n
]))
698 * We have a size expression; attempt to
702 stdscan_bufptr
= special
+ n
;
703 tokval
.t_type
= TOKEN_INVALID
;
704 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
709 sym
->name
= nasm_strdup(name
);
712 error(ERR_NONFATAL
, "cannot use relocatable"
713 " expression as symbol size");
715 sym
->size
= reloc_value(e
);
717 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
722 sym
->globnum
= nglobs
;
727 if (special
&& !special_used
)
728 error(ERR_NONFATAL
, "no special symbol features supported here");
731 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
735 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
736 sect
->tail
= &r
->next
;
739 r
->address
= sect
->len
;
740 if (segment
== NO_SEG
)
745 for (i
= 0; i
< nsects
; i
++)
746 if (segment
== sects
[i
]->index
)
749 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
757 * This routine deals with ..got and ..sym relocations: the more
758 * complicated kinds. In shared-library writing, some relocations
759 * with respect to global symbols must refer to the precise symbol
760 * rather than referring to an offset from the base of the section
761 * _containing_ the symbol. Such relocations call to this routine,
762 * which searches the symbol list for the symbol in question.
764 * R_386_GOT32 references require the _exact_ symbol address to be
765 * used; R_386_32 references can be at an offset from the symbol.
766 * The boolean argument `exact' tells us this.
768 * Return value is the adjusted value of `addr', having become an
769 * offset from the symbol rather than the section. Should always be
770 * zero when returning from an exact call.
772 * Limitation: if you define two symbols at the same place,
773 * confusion will occur.
775 * Inefficiency: we search, currently, using a linked list which
776 * isn't even necessarily sorted.
778 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
779 int32_t segment
, int32_t offset
,
780 int type
, bool exact
)
784 struct Symbol
*sym
, *sm
;
788 * First look up the segment/offset pair and find a global
789 * symbol corresponding to it. If it's not one of our segments,
790 * then it must be an external symbol, in which case we're fine
791 * doing a normal elf_add_reloc after first sanity-checking
792 * that the offset from the symbol is zero.
795 for (i
= 0; i
< nsects
; i
++)
796 if (segment
== sects
[i
]->index
) {
801 if (exact
&& offset
!= 0)
802 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
803 " for this reference");
805 elf_add_reloc(sect
, segment
, type
);
811 * Find a symbol pointing _exactly_ at this one.
813 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
814 if (sym
->value
== offset
)
818 * Find the nearest symbol below this one.
821 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
822 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
826 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
827 " for this reference");
831 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
832 sect
->tail
= &r
->next
;
835 r
->address
= sect
->len
;
836 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
841 return offset
- sym
->value
;
844 static void elf_out(int32_t segto
, const void *data
,
845 enum out_type type
, uint64_t size
,
846 int32_t segment
, int32_t wrt
)
850 uint8_t mydata
[4], *p
;
852 static struct symlininfo sinfo
;
855 * handle absolute-assembly (structure definitions)
857 if (segto
== NO_SEG
) {
858 if (type
!= OUT_RESERVE
)
859 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
865 for (i
= 0; i
< nsects
; i
++)
866 if (segto
== sects
[i
]->index
) {
871 int tempint
; /* ignored */
872 if (segto
!= elf_section_names(".text", 2, &tempint
))
873 error(ERR_PANIC
, "strange segment conditions in ELF driver");
875 s
= sects
[nsects
- 1];
880 /* again some stabs debugging stuff */
881 if (of_elf32
.current_dfmt
) {
882 sinfo
.offset
= s
->len
;
884 sinfo
.name
= s
->name
;
885 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
887 /* end of debugging stuff */
889 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
890 error(ERR_WARNING
, "attempt to initialize memory in"
891 " BSS section `%s': ignored", s
->name
);
892 if (type
== OUT_REL2ADR
)
894 else if (type
== OUT_REL4ADR
)
900 if (type
== OUT_RESERVE
) {
901 if (s
->type
== SHT_PROGBITS
) {
902 error(ERR_WARNING
, "uninitialized space declared in"
903 " non-BSS section `%s': zeroing", s
->name
);
904 elf_sect_write(s
, NULL
, size
);
907 } else if (type
== OUT_RAWDATA
) {
908 if (segment
!= NO_SEG
)
909 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
910 elf_sect_write(s
, data
, size
);
911 } else if (type
== OUT_ADDRESS
) {
913 addr
= *(int64_t *)data
;
914 if (segment
!= NO_SEG
) {
916 error(ERR_NONFATAL
, "ELF format does not support"
917 " segment base references");
922 elf_add_reloc(s
, segment
, R_386_16
);
924 elf_add_reloc(s
, segment
, R_386_32
);
926 } else if (wrt
== elf_gotpc_sect
+ 1) {
928 * The user will supply GOT relative to $$. ELF
929 * will let us have GOT relative to $. So we
930 * need to fix up the data item by $-$$.
933 elf_add_reloc(s
, segment
, R_386_GOTPC
);
934 } else if (wrt
== elf_gotoff_sect
+ 1) {
935 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
936 } else if (wrt
== elf_got_sect
+ 1) {
937 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
939 } else if (wrt
== elf_sym_sect
+ 1) {
942 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
945 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
948 } else if (wrt
== elf_plt_sect
+ 1) {
949 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
950 "relative PLT references");
952 error(ERR_NONFATAL
, "ELF format does not support this"
954 wrt
= NO_SEG
; /* we can at least _try_ to continue */
960 error(ERR_WARNING
| ERR_WARN_GNUELF
,
961 "16-bit relocations in ELF is a GNU extension");
964 if (size
!= 4 && segment
!= NO_SEG
) {
966 "Unsupported non-32-bit ELF relocation");
970 elf_sect_write(s
, mydata
, size
);
971 } else if (type
== OUT_REL2ADR
) {
972 if (segment
== segto
)
973 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
974 if (segment
!= NO_SEG
&& segment
% 2) {
975 error(ERR_NONFATAL
, "ELF format does not support"
976 " segment base references");
979 error(ERR_WARNING
| ERR_WARN_GNUELF
,
980 "16-bit relocations in ELF is a GNU extension");
981 elf_add_reloc(s
, segment
, R_386_PC16
);
984 "Unsupported non-32-bit ELF relocation");
988 WRITESHORT(p
, *(int64_t *)data
- size
);
989 elf_sect_write(s
, mydata
, 2L);
990 } else if (type
== OUT_REL4ADR
) {
991 if (segment
== segto
)
992 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
993 if (segment
!= NO_SEG
&& segment
% 2) {
994 error(ERR_NONFATAL
, "ELF format does not support"
995 " segment base references");
998 elf_add_reloc(s
, segment
, R_386_PC32
);
999 } else if (wrt
== elf_plt_sect
+ 1) {
1000 elf_add_reloc(s
, segment
, R_386_PLT32
);
1001 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1002 wrt
== elf_gotoff_sect
+ 1 ||
1003 wrt
== elf_got_sect
+ 1) {
1004 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
1005 "relative GOT references");
1007 error(ERR_NONFATAL
, "ELF format does not support this"
1009 wrt
= NO_SEG
; /* we can at least _try_ to continue */
1013 WRITELONG(p
, *(int64_t *)data
- size
);
1014 elf_sect_write(s
, mydata
, 4L);
1018 static void elf_write(void)
1028 int32_t symtablen
, symtablocal
;
1031 * Work out how many sections we will have. We have SHN_UNDEF,
1032 * then the flexible user sections, then the four fixed
1033 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1034 * then optionally relocation sections for the user sections.
1036 if (of_elf32
.current_dfmt
== &df_stabs
)
1038 else if (of_elf32
.current_dfmt
== &df_dwarf
)
1041 nsections
= 5; /* SHN_UNDEF and the fixed ones */
1043 add_sectname("", ".comment");
1044 add_sectname("", ".shstrtab");
1045 add_sectname("", ".symtab");
1046 add_sectname("", ".strtab");
1047 for (i
= 0; i
< nsects
; i
++) {
1048 nsections
++; /* for the section itself */
1049 if (sects
[i
]->head
) {
1050 nsections
++; /* for its relocations */
1051 add_sectname(".rel", sects
[i
]->name
);
1055 if (of_elf32
.current_dfmt
== &df_stabs
) {
1056 /* in case the debug information is wanted, just add these three sections... */
1057 add_sectname("", ".stab");
1058 add_sectname("", ".stabstr");
1059 add_sectname(".rel", ".stab");
1062 else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1063 /* the dwarf debug standard specifies the following ten sections,
1064 not all of which are currently implemented,
1065 although all of them are defined. */
1066 #define debug_aranges (int32_t) (nsections-10)
1067 #define debug_info (int32_t) (nsections-7)
1068 #define debug_abbrev (int32_t) (nsections-5)
1069 #define debug_line (int32_t) (nsections-4)
1070 add_sectname("", ".debug_aranges");
1071 add_sectname(".rela", ".debug_aranges");
1072 add_sectname("", ".debug_pubnames");
1073 add_sectname("", ".debug_info");
1074 add_sectname(".rela", ".debug_info");
1075 add_sectname("", ".debug_abbrev");
1076 add_sectname("", ".debug_line");
1077 add_sectname(".rela", ".debug_line");
1078 add_sectname("", ".debug_frame");
1079 add_sectname("", ".debug_loc");
1087 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1090 * Output the ELF header.
1092 fwrite("\177ELF\1\1\1", 7, 1, elffp
);
1093 fputc(elf_osabi
, elffp
);
1094 fputc(elf_abiver
, elffp
);
1095 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp
);
1096 fwriteint16_t(1, elffp
); /* ET_REL relocatable file */
1097 fwriteint16_t(3, elffp
); /* EM_386 processor ID */
1098 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1099 fwriteint32_t(0L, elffp
); /* no entry point */
1100 fwriteint32_t(0L, elffp
); /* no program header table */
1101 fwriteint32_t(0x40L
, elffp
); /* section headers straight after
1102 * ELF header plus alignment */
1103 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1104 fwriteint16_t(0x34, elffp
); /* size of ELF header */
1105 fwriteint16_t(0, elffp
); /* no program header table, again */
1106 fwriteint16_t(0, elffp
); /* still no program header table */
1107 fwriteint16_t(0x28, elffp
); /* size of section header */
1108 fwriteint16_t(nsections
, elffp
); /* number of sections */
1109 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1110 * section header table */
1111 fwriteint32_t(0L, elffp
); /* align to 0x40 bytes */
1112 fwriteint32_t(0L, elffp
);
1113 fwriteint32_t(0L, elffp
);
1116 * Build the symbol table and relocation tables.
1118 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1119 for (i
= 0; i
< nsects
; i
++)
1121 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1125 * Now output the section header table.
1128 elf_foffs
= 0x40 + 0x28 * nsections
;
1129 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1132 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1134 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1135 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1137 for (i
= 0; i
< nsects
; i
++) {
1138 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1139 (sects
[i
]->type
== SHT_PROGBITS
?
1140 sects
[i
]->data
: NULL
), true,
1141 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1143 scount
++; /* dito */
1145 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1146 scount
++; /* dito */
1148 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1149 scount
++; /* dito */
1151 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 16); /* .symtab */
1152 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1154 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1155 for (i
= 0; i
< nsects
; i
++)
1156 if (sects
[i
]->head
) {
1158 elf_section_header(p
- shstrtab
, 9, 0, sects
[i
]->rel
, true,
1159 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 8);
1161 if (of_elf32
.current_dfmt
== &df_stabs
) {
1162 /* for debugging information, create the last three sections
1163 which are the .stab , .stabstr and .rel.stab sections respectively */
1165 /* this function call creates the stab sections in memory */
1168 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1170 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1171 nsections
- 2, 0, 4, 12);
1174 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1175 stabstrlen
, 0, 0, 4, 0);
1178 /* link -> symtable info -> section to refer to */
1179 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1180 stabrellen
, symtabsection
, nsections
- 3, 4,
1184 else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1185 /* for dwarf debugging information, create the ten dwarf sections */
1187 /* this function call creates the dwarf sections in memory */
1188 if (dwarf_fsect
) dwarf32_generate();
1191 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1192 arangeslen
, 0, 0, 1, 0);
1194 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1195 arangesrellen
, symtabsection
, debug_aranges
, 1, 12);
1197 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1198 pubnameslen
, 0, 0, 1, 0);
1200 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1201 infolen
, 0, 0, 1, 0);
1203 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1204 inforellen
, symtabsection
, debug_info
, 1, 12);
1206 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1207 abbrevlen
, 0, 0, 1, 0);
1209 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1210 linelen
, 0, 0, 1, 0);
1212 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1213 linerellen
, symtabsection
, debug_line
, 1, 12);
1215 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1216 framelen
, 0, 0, 8, 0);
1218 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1219 loclen
, 0, 0, 1, 0);
1222 fwrite(align_str
, align
, 1, elffp
);
1225 * Now output the sections.
1227 elf_write_sections();
1229 nasm_free(elf_sects
);
1233 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1235 struct SAA
*s
= saa_init(1L);
1237 uint8_t entry
[16], *p
;
1243 * First, an all-zeros entry, required by the ELF spec.
1245 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1250 * Next, an entry for the file name.
1253 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1254 WRITELONG(p
, 0); /* no value */
1255 WRITELONG(p
, 0); /* no size either */
1256 WRITESHORT(p
, STT_FILE
); /* type FILE */
1257 WRITESHORT(p
, SHN_ABS
);
1258 saa_wbytes(s
, entry
, 16L);
1263 * Now some standard symbols defining the segments, for relocation
1266 for (i
= 1; i
<= nsects
; i
++) {
1268 WRITELONG(p
, 0); /* no symbol name */
1269 WRITELONG(p
, 0); /* offset zero */
1270 WRITELONG(p
, 0); /* size zero */
1271 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1272 WRITESHORT(p
, i
); /* section id */
1273 saa_wbytes(s
, entry
, 16L);
1279 * Now the other local symbols.
1282 while ((sym
= saa_rstruct(syms
))) {
1283 if (sym
->type
& SYM_GLOBAL
)
1286 WRITELONG(p
, sym
->strpos
);
1287 WRITELONG(p
, sym
->value
);
1288 WRITELONG(p
, sym
->size
);
1289 WRITECHAR(p
, sym
->type
); /* type and binding */
1290 WRITECHAR(p
, sym
->other
); /* visibility */
1291 WRITESHORT(p
, sym
->section
);
1292 saa_wbytes(s
, entry
, 16L);
1297 * dwarf needs symbols for debug sections
1298 * which are relocation targets.
1300 //*** fix for 32 bit
1301 if (of_elf32
.current_dfmt
== &df_dwarf
) {
1302 dwarf_infosym
= *local
;
1304 WRITELONG(p
, 0); /* no symbol name */
1305 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1306 WRITELONG(p
, (uint32_t) 0); /* size zero */
1307 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1308 WRITESHORT(p
, debug_info
); /* section id */
1309 saa_wbytes(s
, entry
, 16L);
1312 dwarf_abbrevsym
= *local
;
1314 WRITELONG(p
, 0); /* no symbol name */
1315 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1316 WRITELONG(p
, (uint32_t) 0); /* size zero */
1317 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1318 WRITESHORT(p
, debug_abbrev
); /* section id */
1319 saa_wbytes(s
, entry
, 16L);
1322 dwarf_linesym
= *local
;
1324 WRITELONG(p
, 0); /* no symbol name */
1325 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1326 WRITELONG(p
, (uint32_t) 0); /* size zero */
1327 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1328 WRITESHORT(p
, debug_line
); /* section id */
1329 saa_wbytes(s
, entry
, 16L);
1335 * Now the global symbols.
1338 while ((sym
= saa_rstruct(syms
))) {
1339 if (!(sym
->type
& SYM_GLOBAL
))
1342 WRITELONG(p
, sym
->strpos
);
1343 WRITELONG(p
, sym
->value
);
1344 WRITELONG(p
, sym
->size
);
1345 WRITECHAR(p
, sym
->type
); /* type and binding */
1346 WRITECHAR(p
, sym
->other
); /* visibility */
1347 WRITESHORT(p
, sym
->section
);
1348 saa_wbytes(s
, entry
, 16L);
1355 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1358 uint8_t *p
, entry
[8];
1367 int32_t sym
= r
->symbol
;
1369 if (sym
>= GLOBAL_TEMP_BASE
)
1371 if (of_elf32
.current_dfmt
== &df_dwarf
)
1372 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 5) + nlocals
;
1373 else sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1377 WRITELONG(p
, r
->address
);
1378 WRITELONG(p
, (sym
<< 8) + r
->type
);
1379 saa_wbytes(s
, entry
, 8L);
1388 static void elf_section_header(int name
, int type
, int flags
,
1389 void *data
, bool is_saa
, int32_t datalen
,
1390 int link
, int info
, int align
, int eltsize
)
1392 elf_sects
[elf_nsect
].data
= data
;
1393 elf_sects
[elf_nsect
].len
= datalen
;
1394 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1397 fwriteint32_t((int32_t)name
, elffp
);
1398 fwriteint32_t((int32_t)type
, elffp
);
1399 fwriteint32_t((int32_t)flags
, elffp
);
1400 fwriteint32_t(0L, elffp
); /* no address, ever, in object files */
1401 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1402 fwriteint32_t(datalen
, elffp
);
1404 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1405 fwriteint32_t((int32_t)link
, elffp
);
1406 fwriteint32_t((int32_t)info
, elffp
);
1407 fwriteint32_t((int32_t)align
, elffp
);
1408 fwriteint32_t((int32_t)eltsize
, elffp
);
1411 static void elf_write_sections(void)
1414 for (i
= 0; i
< elf_nsect
; i
++)
1415 if (elf_sects
[i
].data
) {
1416 int32_t len
= elf_sects
[i
].len
;
1417 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1418 int32_t align
= reallen
- len
;
1419 if (elf_sects
[i
].is_saa
)
1420 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1422 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1423 fwrite(align_str
, align
, 1, elffp
);
1427 static void elf_sect_write(struct Section
*sect
,
1428 const uint8_t *data
, uint32_t len
)
1430 saa_wbytes(sect
->data
, data
, len
);
1434 static int32_t elf_segbase(int32_t segment
)
1439 static int elf_directive(char *directive
, char *value
, int pass
)
1445 if (!strcmp(directive
, "osabi")) {
1447 return 1; /* ignore in pass 2 */
1449 n
= readnum(value
, &err
);
1451 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1454 if (n
< 0 || n
> 255) {
1455 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1461 if ((p
= strchr(value
,',')) == NULL
)
1464 n
= readnum(p
+1, &err
);
1465 if (err
|| n
< 0 || n
> 255) {
1466 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1477 static void elf_filename(char *inname
, char *outname
, efunc error
)
1479 strcpy(elf_module
, inname
);
1480 standard_extension(inname
, outname
, ".o", error
);
1483 extern macros_t elf_stdmac
[];
1485 static int elf_set_info(enum geninfo type
, char **val
)
1491 static struct dfmt df_dwarf
= {
1492 "ELF32 (i386) dwarf debug format for Linux",
1502 static struct dfmt df_stabs
= {
1503 "ELF32 (i386) stabs debug format for Linux",
1514 struct dfmt
*elf32_debugs_arr
[3] = { &df_stabs
, &df_dwarf
, NULL
};
1516 struct ofmt of_elf32
= {
1517 "ELF32 (i386) object files (e.g. Linux)",
1534 struct ofmt of_elf
= {
1535 "ELF (short name for ELF32) ",
1551 /* again, the stabs debugging stuff (code) */
1553 void debug32_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1561 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1565 if (!stabs_filename
) {
1566 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1567 strcpy(stabs_filename
, filename
);
1569 if (strcmp(stabs_filename
, filename
)) {
1570 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1571 in fact, this leak comes in quite handy to maintain a list of files
1572 encountered so far in the symbol lines... */
1574 /* why not nasm_free(stabs_filename); we're done with the old one */
1576 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1577 strcpy(stabs_filename
, filename
);
1581 currentline
= linenumber
;
1584 void debug32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1594 void debug32_directive(const char *directive
, const char *params
)
1600 void debug32_typevalue(int32_t type
)
1602 int32_t stype
, ssize
;
1603 switch (TYM_TYPE(type
)) {
1642 stype
= STT_SECTION
;
1657 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1658 lastsym
->size
= ssize
;
1659 lastsym
->type
= stype
;
1663 void stabs32_output(int type
, void *param
)
1665 struct symlininfo
*s
;
1666 struct linelist
*el
;
1667 if (type
== TY_STABSSYMLIN
) {
1668 if (debug_immcall
) {
1669 s
= (struct symlininfo
*)param
;
1670 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1671 return; /* we are only interested in the text stuff */
1673 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1674 el
->info
.offset
= s
->offset
;
1675 el
->info
.section
= s
->section
;
1676 el
->info
.name
= s
->name
;
1677 el
->line
= currentline
;
1678 el
->filename
= stabs_filename
;
1681 stabslines
->last
->next
= el
;
1682 stabslines
->last
= el
;
1685 stabslines
->last
= el
;
1692 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1694 WRITELONG(p,n_strx); \
1695 WRITECHAR(p,n_type); \
1696 WRITECHAR(p,n_other); \
1697 WRITESHORT(p,n_desc); \
1698 WRITELONG(p,n_value); \
1701 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1703 void stabs32_generate(void)
1705 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1706 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1710 struct linelist
*ptr
;
1714 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1715 for (i
= 0; i
< numlinestabs
; i
++)
1719 if (numfiles
== 0) {
1720 allfiles
[0] = ptr
->filename
;
1723 for (i
= 0; i
< numfiles
; i
++) {
1724 if (!strcmp(allfiles
[i
], ptr
->filename
))
1727 if (i
>= numfiles
) {
1728 allfiles
[i
] = ptr
->filename
;
1735 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1736 for (i
= 0; i
< numfiles
; i
++) {
1737 fileidx
[i
] = strsize
;
1738 strsize
+= strlen(allfiles
[i
]) + 1;
1741 for (i
= 0; i
< numfiles
; i
++) {
1742 if (!strcmp(allfiles
[i
], elf_module
)) {
1748 /* worst case size of the stab buffer would be:
1749 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1752 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1753 sizeof(struct stabentry
));
1755 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1757 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1760 for (i
= 0; i
< numfiles
; i
++) {
1761 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1765 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1772 /* this is the first stab, its strx points to the filename of the
1773 the source-file, the n_desc field should be set to the number
1776 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1778 /* this is the stab for the main source file */
1779 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1781 /* relocation table entry */
1783 /* Since the symbol table has two entries before */
1784 /* the section symbols, the index in the info.section */
1785 /* member must be adjusted by adding 2 */
1787 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1788 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1791 currfile
= mainfileindex
;
1795 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1796 /* oops file has changed... */
1797 for (i
= 0; i
< numfiles
; i
++)
1798 if (!strcmp(allfiles
[i
], ptr
->filename
))
1801 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1805 /* relocation table entry */
1806 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1807 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1810 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1813 /* relocation table entry */
1815 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1816 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1822 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1824 nasm_free(allfiles
);
1827 stablen
= (sptr
- sbuf
);
1828 stabrellen
= (rptr
- rbuf
);
1834 void stabs32_cleanup(void)
1836 struct linelist
*ptr
, *del
;
1848 nasm_free(stabrelbuf
);
1850 nasm_free(stabstrbuf
);
1852 /* dwarf routines */
1855 void dwarf32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1858 dwarf32_findfile(filename
);
1860 currentline
= linenumber
;
1863 /* called from elf_out with type == TY_DEBUGSYMLIN */
1864 void dwarf32_output(int type
, void *param
)
1866 int ln
, aa
, inx
, maxln
, soc
;
1867 struct symlininfo
*s
;
1872 s
= (struct symlininfo
*)param
;
1873 /* line number info is only gathered for executable sections */
1874 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1876 /* Check if section index has changed */
1877 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1879 dwarf32_findsect(s
->section
);
1881 /* do nothing unless line or file has changed */
1884 ln
= currentline
- dwarf_csect
->line
;
1885 aa
= s
->offset
- dwarf_csect
->offset
;
1886 inx
= dwarf_clist
->line
;
1887 plinep
= dwarf_csect
->psaa
;
1888 /* check for file change */
1889 if (!(inx
== dwarf_csect
->file
))
1891 saa_write8(plinep
,DW_LNS_set_file
);
1892 saa_write8(plinep
,inx
);
1893 dwarf_csect
->file
= inx
;
1895 /* check for line change */
1898 /* test if in range of special op code */
1899 maxln
= line_base
+ line_range
;
1900 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1901 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1903 saa_write8(plinep
,soc
);
1909 saa_write8(plinep
,DW_LNS_advance_line
);
1910 saa_wleb128s(plinep
,ln
);
1914 saa_write8(plinep
,DW_LNS_advance_pc
);
1915 saa_wleb128u(plinep
,aa
);
1918 dwarf_csect
->line
= currentline
;
1919 dwarf_csect
->offset
= s
->offset
;
1921 /* show change handled */
1927 void dwarf32_generate(void)
1929 static const char nasm_signature
[] = "NASM " NASM_VER
;
1932 struct linelist
*ftentry
;
1933 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1934 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1935 struct sectlist
*psect
;
1936 size_t saalen
, linepoff
, totlen
, highaddr
;
1938 /* write epilogues for each line program range */
1939 /* and build aranges section */
1940 paranges
= saa_init(1L);
1941 parangesrel
= saa_init(1L);
1942 saa_write16(paranges
,2); /* dwarf version */
1943 saa_write32(parangesrel
, paranges
->datalen
+4);
1944 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
1945 saa_write32(parangesrel
, 0);
1946 saa_write32(paranges
,0); /* offset into info */
1947 saa_write8(paranges
,4); /* pointer size */
1948 saa_write8(paranges
,0); /* not segmented */
1949 saa_write32(paranges
,0); /* padding */
1950 /* iterate though sectlist entries */
1951 psect
= dwarf_fsect
;
1954 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1956 plinep
= psect
->psaa
;
1957 /* Line Number Program Epilogue */
1958 saa_write8(plinep
,2); /* std op 2 */
1959 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1960 saa_write8(plinep
,DW_LNS_extended_op
);
1961 saa_write8(plinep
,1); /* operand length */
1962 saa_write8(plinep
,DW_LNE_end_sequence
);
1963 totlen
+= plinep
->datalen
;
1964 /* range table relocation entry */
1965 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1966 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
1967 saa_write32(parangesrel
, (uint32_t) 0);
1968 /* range table entry */
1969 saa_write32(paranges
,0x0000); /* range start */
1970 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1971 highaddr
+= sects
[psect
->section
]->len
;
1972 /* done with this entry */
1973 psect
= psect
->next
;
1975 saa_write32(paranges
,0); /* null address */
1976 saa_write32(paranges
,0); /* null length */
1977 saalen
= paranges
->datalen
;
1978 arangeslen
= saalen
+ 4;
1979 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1980 WRITELONG(pbuf
,saalen
); /* initial length */
1981 saa_rnbytes(paranges
, pbuf
, saalen
);
1984 /* build rela.aranges section */
1985 arangesrellen
= saalen
= parangesrel
->datalen
;
1986 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
1987 saa_rnbytes(parangesrel
, pbuf
, saalen
);
1988 saa_free(parangesrel
);
1990 /* build pubnames section */
1991 ppubnames
= saa_init(1L);
1992 saa_write16(ppubnames
,3); /* dwarf version */
1993 saa_write32(ppubnames
,0); /* offset into info */
1994 saa_write32(ppubnames
,0); /* space used in info */
1995 saa_write32(ppubnames
,0); /* end of list */
1996 saalen
= ppubnames
->datalen
;
1997 pubnameslen
= saalen
+ 4;
1998 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
1999 WRITELONG(pbuf
,saalen
); /* initial length */
2000 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2001 saa_free(ppubnames
);
2003 /* build info section */
2004 pinfo
= saa_init(1L);
2005 pinforel
= saa_init(1L);
2006 saa_write16(pinfo
,2); /* dwarf version */
2007 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2008 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
2009 saa_write32(pinforel
, 0);
2010 saa_write32(pinfo
,0); /* offset into abbrev */
2011 saa_write8(pinfo
,4); /* pointer size */
2012 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2013 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2014 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2015 saa_write32(pinforel
, 0);
2016 saa_write32(pinfo
,0); /* DW_AT_low_pc */
2017 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2018 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2019 saa_write32(pinforel
, 0);
2020 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
2021 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2022 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
2023 saa_write32(pinforel
, 0);
2024 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2025 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2026 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2027 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2028 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2029 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2030 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2031 saa_write32(pinforel
, 0);
2032 saa_write32(pinfo
,0); /* DW_AT_low_pc */
2033 saa_write32(pinfo
,0); /* DW_AT_frame_base */
2034 saa_write8(pinfo
,0); /* end of entries */
2035 saalen
= pinfo
->datalen
;
2036 infolen
= saalen
+ 4;
2037 infobuf
= pbuf
= nasm_malloc(infolen
);
2038 WRITELONG(pbuf
,saalen
); /* initial length */
2039 saa_rnbytes(pinfo
, pbuf
, saalen
);
2042 /* build rela.info section */
2043 inforellen
= saalen
= pinforel
->datalen
;
2044 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2045 saa_rnbytes(pinforel
, pbuf
, saalen
);
2048 /* build abbrev section */
2049 pabbrev
= saa_init(1L);
2050 saa_write8(pabbrev
,1); /* entry number LEB128u */
2051 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2052 saa_write8(pabbrev
,1); /* has children */
2053 /* the following attributes and forms are all LEB128u values */
2054 saa_write8(pabbrev
,DW_AT_low_pc
);
2055 saa_write8(pabbrev
,DW_FORM_addr
);
2056 saa_write8(pabbrev
,DW_AT_high_pc
);
2057 saa_write8(pabbrev
,DW_FORM_addr
);
2058 saa_write8(pabbrev
,DW_AT_stmt_list
);
2059 saa_write8(pabbrev
,DW_FORM_data4
);
2060 saa_write8(pabbrev
,DW_AT_name
);
2061 saa_write8(pabbrev
,DW_FORM_string
);
2062 saa_write8(pabbrev
,DW_AT_producer
);
2063 saa_write8(pabbrev
,DW_FORM_string
);
2064 saa_write8(pabbrev
,DW_AT_language
);
2065 saa_write8(pabbrev
,DW_FORM_data2
);
2066 saa_write16(pabbrev
,0); /* end of entry */
2067 /* LEB128u usage same as above */
2068 saa_write8(pabbrev
,2); /* entry number */
2069 saa_write8(pabbrev
,DW_TAG_subprogram
);
2070 saa_write8(pabbrev
,0); /* no children */
2071 saa_write8(pabbrev
,DW_AT_low_pc
);
2072 saa_write8(pabbrev
,DW_FORM_addr
);
2073 saa_write8(pabbrev
,DW_AT_frame_base
);
2074 saa_write8(pabbrev
,DW_FORM_data4
);
2075 saa_write16(pabbrev
,0); /* end of entry */
2076 abbrevlen
= saalen
= pabbrev
->datalen
;
2077 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2078 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2081 /* build line section */
2083 plines
= saa_init(1L);
2084 saa_write8(plines
,1); /* Minimum Instruction Length */
2085 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2086 saa_write8(plines
,line_base
); /* Line Base */
2087 saa_write8(plines
,line_range
); /* Line Range */
2088 saa_write8(plines
,opcode_base
); /* Opcode Base */
2089 /* standard opcode lengths (# of LEB128u operands) */
2090 saa_write8(plines
,0); /* Std opcode 1 length */
2091 saa_write8(plines
,1); /* Std opcode 2 length */
2092 saa_write8(plines
,1); /* Std opcode 3 length */
2093 saa_write8(plines
,1); /* Std opcode 4 length */
2094 saa_write8(plines
,1); /* Std opcode 5 length */
2095 saa_write8(plines
,0); /* Std opcode 6 length */
2096 saa_write8(plines
,0); /* Std opcode 7 length */
2097 saa_write8(plines
,0); /* Std opcode 8 length */
2098 saa_write8(plines
,1); /* Std opcode 9 length */
2099 saa_write8(plines
,0); /* Std opcode 10 length */
2100 saa_write8(plines
,0); /* Std opcode 11 length */
2101 saa_write8(plines
,1); /* Std opcode 12 length */
2102 /* Directory Table */
2103 saa_write8(plines
,0); /* End of table */
2104 /* File Name Table */
2105 ftentry
= dwarf_flist
;
2106 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2108 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2109 saa_write8(plines
,0); /* directory LEB128u */
2110 saa_write8(plines
,0); /* time LEB128u */
2111 saa_write8(plines
,0); /* size LEB128u */
2112 ftentry
= ftentry
->next
;
2114 saa_write8(plines
,0); /* End of table */
2115 linepoff
= plines
->datalen
;
2116 linelen
= linepoff
+ totlen
+ 10;
2117 linebuf
= pbuf
= nasm_malloc(linelen
);
2118 WRITELONG(pbuf
,linelen
-4); /* initial length */
2119 WRITESHORT(pbuf
,3); /* dwarf version */
2120 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2121 /* write line header */
2123 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2126 /* concatonate line program ranges */
2128 plinesrel
= saa_init(1L);
2129 psect
= dwarf_fsect
;
2130 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2132 saa_write32(plinesrel
, linepoff
);
2133 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2134 saa_write32(plinesrel
, (uint32_t) 0);
2135 plinep
= psect
->psaa
;
2136 saalen
= plinep
->datalen
;
2137 saa_rnbytes(plinep
, pbuf
, saalen
);
2141 /* done with this entry */
2142 psect
= psect
->next
;
2146 /* build rela.lines section */
2147 linerellen
=saalen
= plinesrel
->datalen
;
2148 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2149 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2150 saa_free(plinesrel
);
2152 /* build frame section */
2154 framebuf
= pbuf
= nasm_malloc(framelen
);
2155 WRITELONG(pbuf
,framelen
-4); /* initial length */
2157 /* build loc section */
2159 locbuf
= pbuf
= nasm_malloc(loclen
);
2160 WRITELONG(pbuf
,0); /* null beginning offset */
2161 WRITELONG(pbuf
,0); /* null ending offset */
2164 void dwarf32_cleanup(void)
2167 nasm_free(arangesbuf
);
2169 nasm_free(arangesrelbuf
);
2171 nasm_free(pubnamesbuf
);
2175 nasm_free(inforelbuf
);
2177 nasm_free(abbrevbuf
);
2181 nasm_free(linerelbuf
);
2183 nasm_free(framebuf
);
2187 void dwarf32_findfile(const char * fname
)
2190 struct linelist
*match
;
2192 /* return if fname is current file name */
2193 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2194 /* search for match */
2200 match
= dwarf_flist
;
2201 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2203 if (!(strcmp(fname
, match
->filename
)))
2205 dwarf_clist
= match
;
2210 /* add file name to end of list */
2211 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2213 dwarf_clist
->line
= dwarf_numfiles
;
2214 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2215 strcpy(dwarf_clist
->filename
,fname
);
2216 dwarf_clist
->next
= 0;
2217 /* if first entry */
2220 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2221 dwarf_clist
->last
= 0;
2223 /* chain to previous entry */
2226 dwarf_elist
->next
= dwarf_clist
;
2227 dwarf_elist
= dwarf_clist
;
2232 void dwarf32_findsect(const int index
)
2235 struct sectlist
*match
;
2237 /* return if index is current section index */
2238 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2242 /* search for match */
2248 match
= dwarf_fsect
;
2249 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2251 if ((match
->section
== index
))
2253 dwarf_csect
= match
;
2256 match
= match
->next
;
2259 /* add entry to end of list */
2260 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2262 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2263 dwarf_csect
->line
= 1;
2264 dwarf_csect
->offset
= 0;
2265 dwarf_csect
->file
= 1;
2266 dwarf_csect
->section
= index
;
2267 dwarf_csect
->next
= 0;
2268 /* set relocatable address at start of line program */
2269 saa_write8(plinep
,DW_LNS_extended_op
);
2270 saa_write8(plinep
,5); /* operand length */
2271 saa_write8(plinep
,DW_LNE_set_address
);
2272 saa_write32(plinep
,0); /* Start Address */
2273 /* if first entry */
2276 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2277 dwarf_csect
->last
= 0;
2279 /* chain to previous entry */
2282 dwarf_esect
->next
= dwarf_csect
;
2283 dwarf_esect
= dwarf_csect
;