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.
33 R_386_32
= 1, /* ordinary absolute relocation */
34 R_386_PC32
= 2, /* PC-relative relocation */
35 R_386_GOT32
= 3, /* an offset into GOT */
36 R_386_PLT32
= 4, /* a PC-relative offset into PLT */
37 R_386_COPY
= 5, /* ??? */
38 R_386_GLOB_DAT
= 6, /* ??? */
39 R_386_JUMP_SLOT
= 7, /* ??? */
40 R_386_RELATIVE
= 8, /* ??? */
41 R_386_GOTOFF
= 9, /* an offset from GOT base */
42 R_386_GOTPC
= 10, /* a PC-relative offset _to_ GOT */
43 R_386_TLS_TPOFF
= 14, /* Offset in static TLS block */
44 R_386_TLS_IE
= 15, /* Address of GOT entry for static TLS
46 /* These are GNU extensions, but useful */
47 R_386_16
= 20, /* A 16-bit absolute relocation */
48 R_386_PC16
= 21, /* A 16-bit PC-relative relocation */
49 R_386_8
= 22, /* An 8-bit absolute relocation */
50 R_386_PC8
= 23 /* An 8-bit PC-relative relocation */
55 int32_t address
; /* relative to _start_ of section */
56 int32_t symbol
; /* symbol index */
57 int type
; /* type of relocation */
61 struct rbtree symv
; /* symbol value and symbol rbtree */
62 int32_t strpos
; /* string table position of name */
63 int32_t section
; /* section ID of the symbol */
64 int type
; /* symbol type */
65 int other
; /* symbol visibility */
66 int32_t size
; /* size of symbol */
67 int32_t globnum
; /* symbol table offset if global */
68 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
69 char *name
; /* used temporarily if in above list */
72 #define SHT_PROGBITS 1
77 #define SHF_EXECINSTR 4
78 #define SHF_TLS (1 << 10) /* Section holds thread-local data. */
82 uint32_t len
, size
, nrelocs
;
84 int type
; /* SHT_PROGBITS or SHT_NOBITS */
85 int align
; /* alignment: power of two */
86 uint32_t flags
; /* section flags */
90 struct Reloc
*head
, **tail
;
91 struct rbtree
*gsyms
; /* global symbols in section */
95 static struct Section
**sects
;
96 static int nsects
, sectlen
;
98 #define SHSTR_DELTA 256
99 static char *shstrtab
;
100 static int shstrtablen
, shstrtabsize
;
102 static struct SAA
*syms
;
103 static uint32_t nlocals
, nglobs
;
105 static int32_t def_seg
;
107 static struct RAA
*bsym
;
109 static struct SAA
*strs
;
110 static uint32_t strslen
;
114 static evalfunc evaluate
;
116 static struct Symbol
*fwds
;
118 static char elf_module
[FILENAME_MAX
];
120 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
121 static uint8_t elf_abiver
= 0; /* Current ABI version */
123 extern struct ofmt of_elf32
;
124 extern struct ofmt of_elf
;
126 #define SHN_ABS 0xFFF1
127 #define SHN_COMMON 0xFFF2
130 #define SYM_GLOBAL 0x10
132 #define SHT_RELA 4 /* Relocation entries with addends */
134 #define STT_NOTYPE 0 /* Symbol type is unspecified */
135 #define STT_OBJECT 1 /* Symbol is a data object */
136 #define STT_FUNC 2 /* Symbol is a code object */
137 #define STT_SECTION 3 /* Symbol associated with a section */
138 #define STT_FILE 4 /* Symbol's name is file name */
139 #define STT_COMMON 5 /* Symbol is a common data object */
140 #define STT_TLS 6 /* Symbol is thread-local data object*/
141 #define STT_NUM 7 /* Number of defined types. */
143 #define STV_DEFAULT 0
144 #define STV_INTERNAL 1
146 #define STV_PROTECTED 3
148 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
150 #define SEG_ALIGN 16 /* alignment of sections in file */
151 #define SEG_ALIGN_1 (SEG_ALIGN-1)
153 /* Definitions in lieu of dwarf.h */
154 #define DW_TAG_compile_unit 0x11
155 #define DW_TAG_subprogram 0x2e
156 #define DW_AT_name 0x03
157 #define DW_AT_stmt_list 0x10
158 #define DW_AT_low_pc 0x11
159 #define DW_AT_high_pc 0x12
160 #define DW_AT_language 0x13
161 #define DW_AT_producer 0x25
162 #define DW_AT_frame_base 0x40
163 #define DW_FORM_addr 0x01
164 #define DW_FORM_data2 0x05
165 #define DW_FORM_data4 0x06
166 #define DW_FORM_string 0x08
167 #define DW_LNS_extended_op 0
168 #define DW_LNS_advance_pc 2
169 #define DW_LNS_advance_line 3
170 #define DW_LNS_set_file 4
171 #define DW_LNE_end_sequence 1
172 #define DW_LNE_set_address 2
173 #define DW_LNE_define_file 3
174 #define DW_LANG_Mips_Assembler 0x8001
176 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
178 static struct ELF_SECTDATA
{
183 static int elf_nsect
, nsections
;
184 static int32_t elf_foffs
;
186 static void elf_write(void);
187 static void elf_sect_write(struct Section
*, const uint8_t *,
189 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
191 static void elf_write_sections(void);
192 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
193 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
194 static void add_sectname(char *, char *);
196 /* this stuff is needed for the stabs debugging format */
197 #define N_SO 0x64 /* ID for main source file */
198 #define N_SOL 0x84 /* ID for sub-source file */
202 #define TY_STABSSYMLIN 0x40 /* ouch */
218 int section
; /* section index */
219 char *name
; /* shallow-copied pointer of section name */
223 struct symlininfo info
;
226 struct linelist
*next
;
227 struct linelist
*last
;
236 struct sectlist
*next
;
237 struct sectlist
*last
;
240 /* common debug variables */
241 static int currentline
= 1;
242 static int debug_immcall
= 0;
244 /* stabs debug variables */
245 static struct linelist
*stabslines
= 0;
246 static int numlinestabs
= 0;
247 static char *stabs_filename
= 0;
248 static int symtabsection
;
249 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
250 static int stablen
, stabstrlen
, stabrellen
;
252 /* dwarf debug variables */
253 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
254 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
255 static int dwarf_numfiles
= 0, dwarf_nsections
;
256 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
257 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
258 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
259 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
260 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
261 static int32_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
263 static struct dfmt df_dwarf
;
264 static struct dfmt df_stabs
;
265 static struct Symbol
*lastsym
;
267 /* common debugging routines */
268 void debug32_typevalue(int32_t);
269 void debug32_init(struct ofmt
*, void *, FILE *, efunc
);
270 void debug32_deflabel(char *, int32_t, int64_t, int, char *);
271 void debug32_directive(const char *, const char *);
273 /* stabs debugging routines */
274 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
275 void stabs32_output(int, void *);
276 void stabs32_generate(void);
277 void stabs32_cleanup(void);
279 /* dwarf debugging routines */
280 void dwarf32_linenum(const char *filename
, int32_t linenumber
, int32_t);
281 void dwarf32_output(int, void *);
282 void dwarf32_generate(void);
283 void dwarf32_cleanup(void);
284 void dwarf32_findfile(const char *);
285 void dwarf32_findsect(const int);
286 void saa_wleb128u(struct SAA
*, int);
287 void saa_wleb128s(struct SAA
*, int);
290 * Special section numbers which are used to define ELF special
291 * symbols, which can be used with WRT to provide PIC and TLS
294 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
295 static int32_t elf_got_sect
, elf_plt_sect
;
296 static int32_t elf_sym_sect
, elf_tlsie_sect
;
298 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
303 (void)ldef
; /* placate optimisers */
305 nsects
= sectlen
= 0;
306 syms
= saa_init((int32_t)sizeof(struct Symbol
));
307 nlocals
= nglobs
= 0;
310 saa_wbytes(strs
, "\0", 1L);
311 saa_wbytes(strs
, elf_module
, strlen(elf_module
)+1);
312 strslen
= 2 + strlen(elf_module
);
314 shstrtablen
= shstrtabsize
= 0;;
315 add_sectname("", "");
319 elf_gotpc_sect
= seg_alloc();
320 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
322 elf_gotoff_sect
= seg_alloc();
323 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
325 elf_got_sect
= seg_alloc();
326 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
328 elf_plt_sect
= seg_alloc();
329 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
331 elf_sym_sect
= seg_alloc();
332 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
334 elf_tlsie_sect
= seg_alloc();
335 ldef("..tlsie", elf_tlsie_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
338 def_seg
= seg_alloc();
341 static void elf_init_hack(FILE * fp
, efunc errfunc
, ldfunc ldef
,
344 of_elf32
.current_dfmt
= of_elf
.current_dfmt
; /* Sync debugging format */
345 elf_init(fp
, errfunc
, ldef
, eval
);
348 static void elf_cleanup(int debuginfo
)
357 for (i
= 0; i
< nsects
; i
++) {
358 if (sects
[i
]->type
!= SHT_NOBITS
)
359 saa_free(sects
[i
]->data
);
361 saa_free(sects
[i
]->rel
);
362 while (sects
[i
]->head
) {
364 sects
[i
]->head
= sects
[i
]->head
->next
;
372 if (of_elf32
.current_dfmt
) {
373 of_elf32
.current_dfmt
->cleanup();
377 static void add_sectname(char *firsthalf
, char *secondhalf
)
379 int len
= strlen(firsthalf
) + strlen(secondhalf
);
380 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
381 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
382 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
383 strcat(shstrtab
+ shstrtablen
, secondhalf
);
384 shstrtablen
+= len
+ 1;
387 static int elf_make_section(char *name
, int type
, int flags
, int align
)
391 s
= nasm_malloc(sizeof(*s
));
393 if (type
!= SHT_NOBITS
)
394 s
->data
= saa_init(1L);
397 s
->len
= s
->size
= 0;
399 if (!strcmp(name
, ".text"))
402 s
->index
= seg_alloc();
403 add_sectname("", name
);
404 s
->name
= nasm_malloc(1 + strlen(name
));
405 strcpy(s
->name
, name
);
411 if (nsects
>= sectlen
)
413 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
419 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
422 unsigned flags_and
, flags_or
;
426 * Default is 32 bits.
434 while (*p
&& !nasm_isspace(*p
))
438 flags_and
= flags_or
= type
= align
= 0;
440 while (*p
&& nasm_isspace(*p
))
444 while (*p
&& !nasm_isspace(*p
))
448 while (*p
&& nasm_isspace(*p
))
451 if (!nasm_strnicmp(q
, "align=", 6)) {
455 if ((align
- 1) & align
) { /* means it's not a power of two */
456 error(ERR_NONFATAL
, "section alignment %d is not"
457 " a power of two", align
);
460 } else if (!nasm_stricmp(q
, "alloc")) {
461 flags_and
|= SHF_ALLOC
;
462 flags_or
|= SHF_ALLOC
;
463 } else if (!nasm_stricmp(q
, "noalloc")) {
464 flags_and
|= SHF_ALLOC
;
465 flags_or
&= ~SHF_ALLOC
;
466 } else if (!nasm_stricmp(q
, "exec")) {
467 flags_and
|= SHF_EXECINSTR
;
468 flags_or
|= SHF_EXECINSTR
;
469 } else if (!nasm_stricmp(q
, "noexec")) {
470 flags_and
|= SHF_EXECINSTR
;
471 flags_or
&= ~SHF_EXECINSTR
;
472 } else if (!nasm_stricmp(q
, "write")) {
473 flags_and
|= SHF_WRITE
;
474 flags_or
|= SHF_WRITE
;
475 } else if (!nasm_stricmp(q
, "tls")) {
476 flags_and
|= SHF_TLS
;
478 } else if (!nasm_stricmp(q
, "nowrite")) {
479 flags_and
|= SHF_WRITE
;
480 flags_or
&= ~SHF_WRITE
;
481 } else if (!nasm_stricmp(q
, "progbits")) {
483 } else if (!nasm_stricmp(q
, "nobits")) {
485 } else if (pass
== 1) error(ERR_WARNING
, "Unknown section attribute '%s' ignored on"
486 " declaration of section `%s'", q
, name
);
489 if (!strcmp(name
, ".comment") ||
490 !strcmp(name
, ".shstrtab") ||
491 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
492 error(ERR_NONFATAL
, "attempt to redefine reserved section"
497 for (i
= 0; i
< nsects
; i
++)
498 if (!strcmp(name
, sects
[i
]->name
))
501 if (!strcmp(name
, ".text"))
502 i
= elf_make_section(name
, SHT_PROGBITS
,
503 SHF_ALLOC
| SHF_EXECINSTR
, 16);
504 else if (!strcmp(name
, ".rodata"))
505 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
506 else if (!strcmp(name
, ".data"))
507 i
= elf_make_section(name
, SHT_PROGBITS
,
508 SHF_ALLOC
| SHF_WRITE
, 4);
509 else if (!strcmp(name
, ".bss"))
510 i
= elf_make_section(name
, SHT_NOBITS
,
511 SHF_ALLOC
| SHF_WRITE
, 4);
512 else if (!strcmp(name
, ".tdata"))
513 i
= elf_make_section(name
, SHT_PROGBITS
,
514 SHF_ALLOC
| SHF_WRITE
| SHF_TLS
, 4);
515 else if (!strcmp(name
, ".tbss"))
516 i
= elf_make_section(name
, SHT_NOBITS
,
517 SHF_ALLOC
| SHF_WRITE
| SHF_TLS
, 4);
519 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
521 sects
[i
]->type
= type
;
523 sects
[i
]->align
= align
;
524 sects
[i
]->flags
&= ~flags_and
;
525 sects
[i
]->flags
|= flags_or
;
526 } else if (pass
== 1) {
527 if ((type
&& sects
[i
]->type
!= type
)
528 || (align
&& sects
[i
]->align
!= align
)
529 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
530 error(ERR_WARNING
, "section attributes ignored on"
531 " redeclaration of section `%s'", name
);
534 return sects
[i
]->index
;
537 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
538 int is_global
, char *special
)
542 bool special_used
= false;
544 #if defined(DEBUG) && DEBUG>2
546 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
547 name
, segment
, offset
, is_global
, special
);
549 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
551 * This is a NASM special symbol. We never allow it into
552 * the ELF symbol table, even if it's a valid one. If it
553 * _isn't_ a valid one, we should barf immediately.
555 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
556 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
557 strcmp(name
, "..sym") && strcmp(name
, "..tlsie"))
558 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
562 if (is_global
== 3) {
565 * Fix up a forward-reference symbol size from the first
568 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
569 if (!strcmp((*s
)->name
, name
)) {
570 struct tokenval tokval
;
574 while (*p
&& !nasm_isspace(*p
))
576 while (*p
&& nasm_isspace(*p
))
580 tokval
.t_type
= TOKEN_INVALID
;
581 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
584 error(ERR_NONFATAL
, "cannot use relocatable"
585 " expression as symbol size");
587 (*s
)->size
= reloc_value(e
);
591 * Remove it from the list of unresolved sizes.
593 nasm_free((*s
)->name
);
597 return; /* it wasn't an important one */
600 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
601 strslen
+= 1 + strlen(name
);
603 lastsym
= sym
= saa_wstruct(syms
);
605 memset(&sym
->symv
, 0, sizeof(struct rbtree
));
608 sym
->type
= is_global
? SYM_GLOBAL
: 0;
609 sym
->other
= STV_DEFAULT
;
611 if (segment
== NO_SEG
)
612 sym
->section
= SHN_ABS
;
615 sym
->section
= SHN_UNDEF
;
616 if (nsects
== 0 && segment
== def_seg
) {
618 if (segment
!= elf_section_names(".text", 2, &tempint
))
620 "strange segment conditions in ELF driver");
621 sym
->section
= nsects
;
623 for (i
= 0; i
< nsects
; i
++)
624 if (segment
== sects
[i
]->index
) {
625 sym
->section
= i
+ 1;
631 if (is_global
== 2) {
634 sym
->section
= SHN_COMMON
;
636 * We have a common variable. Check the special text to see
637 * if it's a valid number and power of two; if so, store it
638 * as the alignment for the common variable.
642 sym
->symv
.key
= readnum(special
, &err
);
644 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
645 " valid number", special
);
646 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
647 != 2 * sym
->symv
.key
- 1)
648 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
649 " power of two", special
);
653 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
655 if (sym
->type
== SYM_GLOBAL
) {
657 * If sym->section == SHN_ABS, then the first line of the
658 * else section would cause a core dump, because its a reference
659 * beyond the end of the section array.
660 * This behaviour is exhibited by this code:
663 * To avoid such a crash, such requests are silently discarded.
664 * This may not be the best solution.
666 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
667 bsym
= raa_write(bsym
, segment
, nglobs
);
668 } else if (sym
->section
!= SHN_ABS
) {
670 * This is a global symbol; so we must add it to the rbtree
671 * of global symbols in its section.
673 * In addition, we check the special text for symbol
674 * type and size information.
676 sects
[sym
->section
-1]->gsyms
=
677 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
680 int n
= strcspn(special
, " \t");
682 if (!nasm_strnicmp(special
, "function", n
))
683 sym
->type
|= STT_FUNC
;
684 else if (!nasm_strnicmp(special
, "data", n
) ||
685 !nasm_strnicmp(special
, "object", n
))
686 sym
->type
|= STT_OBJECT
;
687 else if (!nasm_strnicmp(special
, "notype", n
))
688 sym
->type
|= STT_NOTYPE
;
690 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
694 while (nasm_isspace(*special
))
697 n
= strcspn(special
, " \t");
698 if (!nasm_strnicmp(special
, "default", n
))
699 sym
->other
= STV_DEFAULT
;
700 else if (!nasm_strnicmp(special
, "internal", n
))
701 sym
->other
= STV_INTERNAL
;
702 else if (!nasm_strnicmp(special
, "hidden", n
))
703 sym
->other
= STV_HIDDEN
;
704 else if (!nasm_strnicmp(special
, "protected", n
))
705 sym
->other
= STV_PROTECTED
;
712 struct tokenval tokval
;
715 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
717 while (special
[n
] && nasm_isspace(special
[n
]))
720 * We have a size expression; attempt to
724 stdscan_bufptr
= special
+ n
;
725 tokval
.t_type
= TOKEN_INVALID
;
726 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
731 sym
->name
= nasm_strdup(name
);
734 error(ERR_NONFATAL
, "cannot use relocatable"
735 " expression as symbol size");
737 sym
->size
= reloc_value(e
);
739 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
744 * If TLS segment, mark symbol accordingly.
746 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
748 sym
->type
|= STT_TLS
;
751 sym
->globnum
= nglobs
;
756 if (special
&& !special_used
)
757 error(ERR_NONFATAL
, "no special symbol features supported here");
760 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
764 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
765 sect
->tail
= &r
->next
;
768 r
->address
= sect
->len
;
769 if (segment
== NO_SEG
)
774 for (i
= 0; i
< nsects
; i
++)
775 if (segment
== sects
[i
]->index
)
778 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
786 * This routine deals with ..got and ..sym relocations: the more
787 * complicated kinds. In shared-library writing, some relocations
788 * with respect to global symbols must refer to the precise symbol
789 * rather than referring to an offset from the base of the section
790 * _containing_ the symbol. Such relocations call to this routine,
791 * which searches the symbol list for the symbol in question.
793 * R_386_GOT32 references require the _exact_ symbol address to be
794 * used; R_386_32 references can be at an offset from the symbol.
795 * The boolean argument `exact' tells us this.
797 * Return value is the adjusted value of `addr', having become an
798 * offset from the symbol rather than the section. Should always be
799 * zero when returning from an exact call.
801 * Limitation: if you define two symbols at the same place,
802 * confusion will occur.
804 * Inefficiency: we search, currently, using a linked list which
805 * isn't even necessarily sorted.
807 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
808 int32_t segment
, uint32_t offset
,
809 int type
, bool exact
)
818 * First look up the segment/offset pair and find a global
819 * symbol corresponding to it. If it's not one of our segments,
820 * then it must be an external symbol, in which case we're fine
821 * doing a normal elf_add_reloc after first sanity-checking
822 * that the offset from the symbol is zero.
825 for (i
= 0; i
< nsects
; i
++)
826 if (segment
== sects
[i
]->index
) {
831 if (exact
&& offset
!= 0)
832 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
833 " for this reference");
835 elf_add_reloc(sect
, segment
, type
);
839 srb
= rb_search(s
->gsyms
, offset
);
840 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
841 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
842 " for this reference");
845 sym
= container_of(srb
, struct Symbol
, symv
);
847 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
848 sect
->tail
= &r
->next
;
851 r
->address
= sect
->len
;
852 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
857 return offset
- sym
->symv
.key
;
860 static void elf_out(int32_t segto
, const void *data
,
861 enum out_type type
, uint64_t size
,
862 int32_t segment
, int32_t wrt
)
866 uint8_t mydata
[4], *p
;
868 static struct symlininfo sinfo
;
871 * handle absolute-assembly (structure definitions)
873 if (segto
== NO_SEG
) {
874 if (type
!= OUT_RESERVE
)
875 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
881 for (i
= 0; i
< nsects
; i
++)
882 if (segto
== sects
[i
]->index
) {
887 int tempint
; /* ignored */
888 if (segto
!= elf_section_names(".text", 2, &tempint
))
889 error(ERR_PANIC
, "strange segment conditions in ELF driver");
891 s
= sects
[nsects
- 1];
896 /* again some stabs debugging stuff */
897 if (of_elf32
.current_dfmt
) {
898 sinfo
.offset
= s
->len
;
900 sinfo
.name
= s
->name
;
901 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
903 /* end of debugging stuff */
905 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
906 error(ERR_WARNING
, "attempt to initialize memory in"
907 " BSS section `%s': ignored", s
->name
);
908 s
->len
+= realsize(type
, size
);
912 if (type
== OUT_RESERVE
) {
913 if (s
->type
== SHT_PROGBITS
) {
914 error(ERR_WARNING
, "uninitialized space declared in"
915 " non-BSS section `%s': zeroing", s
->name
);
916 elf_sect_write(s
, NULL
, size
);
919 } else if (type
== OUT_RAWDATA
) {
920 if (segment
!= NO_SEG
)
921 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
922 elf_sect_write(s
, data
, size
);
923 } else if (type
== OUT_ADDRESS
) {
925 addr
= *(int64_t *)data
;
926 if (segment
!= NO_SEG
) {
928 error(ERR_NONFATAL
, "ELF format does not support"
929 " segment base references");
934 elf_add_reloc(s
, segment
, R_386_16
);
936 elf_add_reloc(s
, segment
, R_386_32
);
938 } else if (wrt
== elf_gotpc_sect
+ 1) {
940 * The user will supply GOT relative to $$. ELF
941 * will let us have GOT relative to $. So we
942 * need to fix up the data item by $-$$.
945 elf_add_reloc(s
, segment
, R_386_GOTPC
);
946 } else if (wrt
== elf_gotoff_sect
+ 1) {
947 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
948 } else if (wrt
== elf_tlsie_sect
+ 1) {
949 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
951 } else if (wrt
== elf_got_sect
+ 1) {
952 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
954 } else if (wrt
== elf_sym_sect
+ 1) {
957 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
960 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
963 } else if (wrt
== elf_plt_sect
+ 1) {
964 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
965 "relative PLT references");
967 error(ERR_NONFATAL
, "ELF format does not support this"
969 wrt
= NO_SEG
; /* we can at least _try_ to continue */
975 error(ERR_WARNING
| ERR_WARN_GNUELF
,
976 "16-bit relocations in ELF is a GNU extension");
979 if (size
!= 4 && segment
!= NO_SEG
) {
981 "Unsupported non-32-bit ELF relocation");
985 elf_sect_write(s
, mydata
, size
);
986 } else if (type
== OUT_REL2ADR
) {
987 if (segment
== segto
)
988 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
989 if (segment
!= NO_SEG
&& segment
% 2) {
990 error(ERR_NONFATAL
, "ELF format does not support"
991 " segment base references");
994 error(ERR_WARNING
| ERR_WARN_GNUELF
,
995 "16-bit relocations in ELF is a GNU extension");
996 elf_add_reloc(s
, segment
, R_386_PC16
);
999 "Unsupported non-32-bit ELF relocation");
1003 WRITESHORT(p
, *(int64_t *)data
- size
);
1004 elf_sect_write(s
, mydata
, 2L);
1005 } else if (type
== OUT_REL4ADR
) {
1006 if (segment
== segto
)
1007 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
1008 if (segment
!= NO_SEG
&& segment
% 2) {
1009 error(ERR_NONFATAL
, "ELF format does not support"
1010 " segment base references");
1012 if (wrt
== NO_SEG
) {
1013 elf_add_reloc(s
, segment
, R_386_PC32
);
1014 } else if (wrt
== elf_plt_sect
+ 1) {
1015 elf_add_reloc(s
, segment
, R_386_PLT32
);
1016 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1017 wrt
== elf_gotoff_sect
+ 1 ||
1018 wrt
== elf_got_sect
+ 1) {
1019 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
1020 "relative GOT references");
1022 error(ERR_NONFATAL
, "ELF format does not support this"
1024 wrt
= NO_SEG
; /* we can at least _try_ to continue */
1028 WRITELONG(p
, *(int64_t *)data
- size
);
1029 elf_sect_write(s
, mydata
, 4L);
1033 static void elf_write(void)
1043 int32_t symtablen
, symtablocal
;
1046 * Work out how many sections we will have. We have SHN_UNDEF,
1047 * then the flexible user sections, then the four fixed
1048 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1049 * then optionally relocation sections for the user sections.
1051 if (of_elf32
.current_dfmt
== &df_stabs
)
1053 else if (of_elf32
.current_dfmt
== &df_dwarf
)
1056 nsections
= 5; /* SHN_UNDEF and the fixed ones */
1058 add_sectname("", ".comment");
1059 add_sectname("", ".shstrtab");
1060 add_sectname("", ".symtab");
1061 add_sectname("", ".strtab");
1062 for (i
= 0; i
< nsects
; i
++) {
1063 nsections
++; /* for the section itself */
1064 if (sects
[i
]->head
) {
1065 nsections
++; /* for its relocations */
1066 add_sectname(".rel", sects
[i
]->name
);
1070 if (of_elf32
.current_dfmt
== &df_stabs
) {
1071 /* in case the debug information is wanted, just add these three sections... */
1072 add_sectname("", ".stab");
1073 add_sectname("", ".stabstr");
1074 add_sectname(".rel", ".stab");
1077 else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1078 /* the dwarf debug standard specifies the following ten sections,
1079 not all of which are currently implemented,
1080 although all of them are defined. */
1081 #define debug_aranges (int32_t) (nsections-10)
1082 #define debug_info (int32_t) (nsections-7)
1083 #define debug_abbrev (int32_t) (nsections-5)
1084 #define debug_line (int32_t) (nsections-4)
1085 add_sectname("", ".debug_aranges");
1086 add_sectname(".rela", ".debug_aranges");
1087 add_sectname("", ".debug_pubnames");
1088 add_sectname("", ".debug_info");
1089 add_sectname(".rela", ".debug_info");
1090 add_sectname("", ".debug_abbrev");
1091 add_sectname("", ".debug_line");
1092 add_sectname(".rela", ".debug_line");
1093 add_sectname("", ".debug_frame");
1094 add_sectname("", ".debug_loc");
1101 commlen
= 2 + snprintf(comment
+1, sizeof comment
-1, "%s", nasm_comment
);
1104 * Output the ELF header.
1106 fwrite("\177ELF\1\1\1", 7, 1, elffp
);
1107 fputc(elf_osabi
, elffp
);
1108 fputc(elf_abiver
, elffp
);
1109 fwritezero(7, elffp
);
1110 fwriteint16_t(1, elffp
); /* ET_REL relocatable file */
1111 fwriteint16_t(3, elffp
); /* EM_386 processor ID */
1112 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1113 fwriteint32_t(0L, elffp
); /* no entry point */
1114 fwriteint32_t(0L, elffp
); /* no program header table */
1115 fwriteint32_t(0x40L
, elffp
); /* section headers straight after
1116 * ELF header plus alignment */
1117 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1118 fwriteint16_t(0x34, elffp
); /* size of ELF header */
1119 fwriteint16_t(0, elffp
); /* no program header table, again */
1120 fwriteint16_t(0, elffp
); /* still no program header table */
1121 fwriteint16_t(0x28, elffp
); /* size of section header */
1122 fwriteint16_t(nsections
, elffp
); /* number of sections */
1123 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1124 * section header table */
1125 fwriteint32_t(0L, elffp
); /* align to 0x40 bytes */
1126 fwriteint32_t(0L, elffp
);
1127 fwriteint32_t(0L, elffp
);
1130 * Build the symbol table and relocation tables.
1132 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1133 for (i
= 0; i
< nsects
; i
++)
1135 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1139 * Now output the section header table.
1142 elf_foffs
= 0x40 + 0x28 * nsections
;
1143 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1146 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1148 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1149 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1151 for (i
= 0; i
< nsects
; i
++) {
1152 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1153 (sects
[i
]->type
== SHT_PROGBITS
?
1154 sects
[i
]->data
: NULL
), true,
1155 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1157 scount
++; /* dito */
1159 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1160 scount
++; /* dito */
1162 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1163 scount
++; /* dito */
1165 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 16); /* .symtab */
1166 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1168 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1169 for (i
= 0; i
< nsects
; i
++)
1170 if (sects
[i
]->head
) {
1172 elf_section_header(p
- shstrtab
, 9, 0, sects
[i
]->rel
, true,
1173 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 8);
1175 if (of_elf32
.current_dfmt
== &df_stabs
) {
1176 /* for debugging information, create the last three sections
1177 which are the .stab , .stabstr and .rel.stab sections respectively */
1179 /* this function call creates the stab sections in memory */
1182 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1184 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1185 nsections
- 2, 0, 4, 12);
1188 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1189 stabstrlen
, 0, 0, 4, 0);
1192 /* link -> symtable info -> section to refer to */
1193 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1194 stabrellen
, symtabsection
, nsections
- 3, 4,
1198 else if (of_elf32
.current_dfmt
== &df_dwarf
) {
1199 /* for dwarf debugging information, create the ten dwarf sections */
1201 /* this function call creates the dwarf sections in memory */
1202 if (dwarf_fsect
) dwarf32_generate();
1205 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1206 arangeslen
, 0, 0, 1, 0);
1208 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1209 arangesrellen
, symtabsection
, debug_aranges
, 1, 12);
1211 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1212 pubnameslen
, 0, 0, 1, 0);
1214 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1215 infolen
, 0, 0, 1, 0);
1217 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1218 inforellen
, symtabsection
, debug_info
, 1, 12);
1220 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1221 abbrevlen
, 0, 0, 1, 0);
1223 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1224 linelen
, 0, 0, 1, 0);
1226 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1227 linerellen
, symtabsection
, debug_line
, 1, 12);
1229 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1230 framelen
, 0, 0, 8, 0);
1232 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1233 loclen
, 0, 0, 1, 0);
1236 fwritezero(align
, elffp
);
1239 * Now output the sections.
1241 elf_write_sections();
1243 nasm_free(elf_sects
);
1247 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1249 struct SAA
*s
= saa_init(1L);
1251 uint8_t entry
[16], *p
;
1257 * First, an all-zeros entry, required by the ELF spec.
1259 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1264 * Next, an entry for the file name.
1267 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1268 WRITELONG(p
, 0); /* no value */
1269 WRITELONG(p
, 0); /* no size either */
1270 WRITESHORT(p
, STT_FILE
); /* type FILE */
1271 WRITESHORT(p
, SHN_ABS
);
1272 saa_wbytes(s
, entry
, 16L);
1277 * Now some standard symbols defining the segments, for relocation
1280 for (i
= 1; i
<= nsects
; i
++) {
1282 WRITELONG(p
, 0); /* no symbol name */
1283 WRITELONG(p
, 0); /* offset zero */
1284 WRITELONG(p
, 0); /* size zero */
1285 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1286 WRITESHORT(p
, i
); /* section id */
1287 saa_wbytes(s
, entry
, 16L);
1293 * Now the other local symbols.
1296 while ((sym
= saa_rstruct(syms
))) {
1297 if (sym
->type
& SYM_GLOBAL
)
1300 WRITELONG(p
, sym
->strpos
);
1301 WRITELONG(p
, sym
->symv
.key
);
1302 WRITELONG(p
, sym
->size
);
1303 WRITECHAR(p
, sym
->type
); /* type and binding */
1304 WRITECHAR(p
, sym
->other
); /* visibility */
1305 WRITESHORT(p
, sym
->section
);
1306 saa_wbytes(s
, entry
, 16L);
1311 * dwarf needs symbols for debug sections
1312 * which are relocation targets.
1314 //*** fix for 32 bit
1315 if (of_elf32
.current_dfmt
== &df_dwarf
) {
1316 dwarf_infosym
= *local
;
1318 WRITELONG(p
, 0); /* no symbol name */
1319 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1320 WRITELONG(p
, (uint32_t) 0); /* size zero */
1321 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1322 WRITESHORT(p
, debug_info
); /* section id */
1323 saa_wbytes(s
, entry
, 16L);
1326 dwarf_abbrevsym
= *local
;
1328 WRITELONG(p
, 0); /* no symbol name */
1329 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1330 WRITELONG(p
, (uint32_t) 0); /* size zero */
1331 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1332 WRITESHORT(p
, debug_abbrev
); /* section id */
1333 saa_wbytes(s
, entry
, 16L);
1336 dwarf_linesym
= *local
;
1338 WRITELONG(p
, 0); /* no symbol name */
1339 WRITELONG(p
, (uint32_t) 0); /* offset zero */
1340 WRITELONG(p
, (uint32_t) 0); /* size zero */
1341 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1342 WRITESHORT(p
, debug_line
); /* section id */
1343 saa_wbytes(s
, entry
, 16L);
1349 * Now the global symbols.
1352 while ((sym
= saa_rstruct(syms
))) {
1353 if (!(sym
->type
& SYM_GLOBAL
))
1356 WRITELONG(p
, sym
->strpos
);
1357 WRITELONG(p
, sym
->symv
.key
);
1358 WRITELONG(p
, sym
->size
);
1359 WRITECHAR(p
, sym
->type
); /* type and binding */
1360 WRITECHAR(p
, sym
->other
); /* visibility */
1361 WRITESHORT(p
, sym
->section
);
1362 saa_wbytes(s
, entry
, 16L);
1369 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1372 uint8_t *p
, entry
[8];
1381 int32_t sym
= r
->symbol
;
1383 if (sym
>= GLOBAL_TEMP_BASE
)
1385 if (of_elf32
.current_dfmt
== &df_dwarf
)
1386 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 5) + nlocals
;
1387 else sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1391 WRITELONG(p
, r
->address
);
1392 WRITELONG(p
, (sym
<< 8) + r
->type
);
1393 saa_wbytes(s
, entry
, 8L);
1402 static void elf_section_header(int name
, int type
, int flags
,
1403 void *data
, bool is_saa
, int32_t datalen
,
1404 int link
, int info
, int align
, int eltsize
)
1406 elf_sects
[elf_nsect
].data
= data
;
1407 elf_sects
[elf_nsect
].len
= datalen
;
1408 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1411 fwriteint32_t((int32_t)name
, elffp
);
1412 fwriteint32_t((int32_t)type
, elffp
);
1413 fwriteint32_t((int32_t)flags
, elffp
);
1414 fwriteint32_t(0L, elffp
); /* no address, ever, in object files */
1415 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1416 fwriteint32_t(datalen
, elffp
);
1418 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1419 fwriteint32_t((int32_t)link
, elffp
);
1420 fwriteint32_t((int32_t)info
, elffp
);
1421 fwriteint32_t((int32_t)align
, elffp
);
1422 fwriteint32_t((int32_t)eltsize
, elffp
);
1425 static void elf_write_sections(void)
1428 for (i
= 0; i
< elf_nsect
; i
++)
1429 if (elf_sects
[i
].data
) {
1430 int32_t len
= elf_sects
[i
].len
;
1431 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1432 int32_t align
= reallen
- len
;
1433 if (elf_sects
[i
].is_saa
)
1434 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1436 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1437 fwritezero(align
, elffp
);
1441 static void elf_sect_write(struct Section
*sect
,
1442 const uint8_t *data
, uint32_t len
)
1444 saa_wbytes(sect
->data
, data
, len
);
1448 static int32_t elf_segbase(int32_t segment
)
1453 static int elf_directive(char *directive
, char *value
, int pass
)
1459 if (!strcmp(directive
, "osabi")) {
1461 return 1; /* ignore in pass 2 */
1463 n
= readnum(value
, &err
);
1465 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1468 if (n
< 0 || n
> 255) {
1469 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1475 if ((p
= strchr(value
,',')) == NULL
)
1478 n
= readnum(p
+1, &err
);
1479 if (err
|| n
< 0 || n
> 255) {
1480 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1491 static void elf_filename(char *inname
, char *outname
, efunc error
)
1493 strcpy(elf_module
, inname
);
1494 standard_extension(inname
, outname
, ".o", error
);
1497 extern macros_t elf_stdmac
[];
1499 static int elf_set_info(enum geninfo type
, char **val
)
1505 static struct dfmt df_dwarf
= {
1506 "ELF32 (i386) dwarf debug format for Linux",
1516 static struct dfmt df_stabs
= {
1517 "ELF32 (i386) stabs debug format for Linux",
1528 struct dfmt
*elf32_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1530 struct ofmt of_elf32
= {
1531 "ELF32 (i386) object files (e.g. Linux)",
1548 struct ofmt of_elf
= {
1549 "ELF (short name for ELF32) ",
1565 /* again, the stabs debugging stuff (code) */
1567 void debug32_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1575 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1579 if (!stabs_filename
) {
1580 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1581 strcpy(stabs_filename
, filename
);
1583 if (strcmp(stabs_filename
, filename
)) {
1584 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1585 in fact, this leak comes in quite handy to maintain a list of files
1586 encountered so far in the symbol lines... */
1588 /* why not nasm_free(stabs_filename); we're done with the old one */
1590 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1591 strcpy(stabs_filename
, filename
);
1595 currentline
= linenumber
;
1598 void debug32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1608 void debug32_directive(const char *directive
, const char *params
)
1614 void debug32_typevalue(int32_t type
)
1616 int32_t stype
, ssize
;
1617 switch (TYM_TYPE(type
)) {
1656 stype
= STT_SECTION
;
1671 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1672 lastsym
->size
= ssize
;
1673 lastsym
->type
= stype
;
1677 void stabs32_output(int type
, void *param
)
1679 struct symlininfo
*s
;
1680 struct linelist
*el
;
1681 if (type
== TY_STABSSYMLIN
) {
1682 if (debug_immcall
) {
1683 s
= (struct symlininfo
*)param
;
1684 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1685 return; /* we are only interested in the text stuff */
1687 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1688 el
->info
.offset
= s
->offset
;
1689 el
->info
.section
= s
->section
;
1690 el
->info
.name
= s
->name
;
1691 el
->line
= currentline
;
1692 el
->filename
= stabs_filename
;
1695 stabslines
->last
->next
= el
;
1696 stabslines
->last
= el
;
1699 stabslines
->last
= el
;
1706 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1708 WRITELONG(p,n_strx); \
1709 WRITECHAR(p,n_type); \
1710 WRITECHAR(p,n_other); \
1711 WRITESHORT(p,n_desc); \
1712 WRITELONG(p,n_value); \
1715 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1717 void stabs32_generate(void)
1719 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1720 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1724 struct linelist
*ptr
;
1728 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1729 for (i
= 0; i
< numlinestabs
; i
++)
1733 if (numfiles
== 0) {
1734 allfiles
[0] = ptr
->filename
;
1737 for (i
= 0; i
< numfiles
; i
++) {
1738 if (!strcmp(allfiles
[i
], ptr
->filename
))
1741 if (i
>= numfiles
) {
1742 allfiles
[i
] = ptr
->filename
;
1749 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1750 for (i
= 0; i
< numfiles
; i
++) {
1751 fileidx
[i
] = strsize
;
1752 strsize
+= strlen(allfiles
[i
]) + 1;
1755 for (i
= 0; i
< numfiles
; i
++) {
1756 if (!strcmp(allfiles
[i
], elf_module
)) {
1762 /* worst case size of the stab buffer would be:
1763 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1766 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1767 sizeof(struct stabentry
));
1769 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1771 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1774 for (i
= 0; i
< numfiles
; i
++) {
1775 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1779 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1786 /* this is the first stab, its strx points to the filename of the
1787 the source-file, the n_desc field should be set to the number
1790 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1792 /* this is the stab for the main source file */
1793 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1795 /* relocation table entry */
1797 /* Since the symbol table has two entries before */
1798 /* the section symbols, the index in the info.section */
1799 /* member must be adjusted by adding 2 */
1801 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1802 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1805 currfile
= mainfileindex
;
1809 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1810 /* oops file has changed... */
1811 for (i
= 0; i
< numfiles
; i
++)
1812 if (!strcmp(allfiles
[i
], ptr
->filename
))
1815 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1819 /* relocation table entry */
1820 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1821 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1824 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1827 /* relocation table entry */
1829 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1830 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1836 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1838 nasm_free(allfiles
);
1841 stablen
= (sptr
- sbuf
);
1842 stabrellen
= (rptr
- rbuf
);
1848 void stabs32_cleanup(void)
1850 struct linelist
*ptr
, *del
;
1862 nasm_free(stabrelbuf
);
1864 nasm_free(stabstrbuf
);
1866 /* dwarf routines */
1869 void dwarf32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1872 dwarf32_findfile(filename
);
1874 currentline
= linenumber
;
1877 /* called from elf_out with type == TY_DEBUGSYMLIN */
1878 void dwarf32_output(int type
, void *param
)
1880 int ln
, aa
, inx
, maxln
, soc
;
1881 struct symlininfo
*s
;
1886 s
= (struct symlininfo
*)param
;
1887 /* line number info is only gathered for executable sections */
1888 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1890 /* Check if section index has changed */
1891 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1893 dwarf32_findsect(s
->section
);
1895 /* do nothing unless line or file has changed */
1898 ln
= currentline
- dwarf_csect
->line
;
1899 aa
= s
->offset
- dwarf_csect
->offset
;
1900 inx
= dwarf_clist
->line
;
1901 plinep
= dwarf_csect
->psaa
;
1902 /* check for file change */
1903 if (!(inx
== dwarf_csect
->file
))
1905 saa_write8(plinep
,DW_LNS_set_file
);
1906 saa_write8(plinep
,inx
);
1907 dwarf_csect
->file
= inx
;
1909 /* check for line change */
1912 /* test if in range of special op code */
1913 maxln
= line_base
+ line_range
;
1914 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1915 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1917 saa_write8(plinep
,soc
);
1923 saa_write8(plinep
,DW_LNS_advance_line
);
1924 saa_wleb128s(plinep
,ln
);
1928 saa_write8(plinep
,DW_LNS_advance_pc
);
1929 saa_wleb128u(plinep
,aa
);
1932 dwarf_csect
->line
= currentline
;
1933 dwarf_csect
->offset
= s
->offset
;
1935 /* show change handled */
1941 void dwarf32_generate(void)
1945 struct linelist
*ftentry
;
1946 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1947 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1948 struct sectlist
*psect
;
1949 size_t saalen
, linepoff
, totlen
, highaddr
;
1951 /* write epilogues for each line program range */
1952 /* and build aranges section */
1953 paranges
= saa_init(1L);
1954 parangesrel
= saa_init(1L);
1955 saa_write16(paranges
,2); /* dwarf version */
1956 saa_write32(parangesrel
, paranges
->datalen
+4);
1957 saa_write32(parangesrel
, (dwarf_infosym
<< 8) + R_386_32
); /* reloc to info */
1958 saa_write32(parangesrel
, 0);
1959 saa_write32(paranges
,0); /* offset into info */
1960 saa_write8(paranges
,4); /* pointer size */
1961 saa_write8(paranges
,0); /* not segmented */
1962 saa_write32(paranges
,0); /* padding */
1963 /* iterate though sectlist entries */
1964 psect
= dwarf_fsect
;
1967 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1969 plinep
= psect
->psaa
;
1970 /* Line Number Program Epilogue */
1971 saa_write8(plinep
,2); /* std op 2 */
1972 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1973 saa_write8(plinep
,DW_LNS_extended_op
);
1974 saa_write8(plinep
,1); /* operand length */
1975 saa_write8(plinep
,DW_LNE_end_sequence
);
1976 totlen
+= plinep
->datalen
;
1977 /* range table relocation entry */
1978 saa_write32(parangesrel
, paranges
->datalen
+ 4);
1979 saa_write32(parangesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
1980 saa_write32(parangesrel
, (uint32_t) 0);
1981 /* range table entry */
1982 saa_write32(paranges
,0x0000); /* range start */
1983 saa_write32(paranges
,sects
[psect
->section
]->len
); /* range length */
1984 highaddr
+= sects
[psect
->section
]->len
;
1985 /* done with this entry */
1986 psect
= psect
->next
;
1988 saa_write32(paranges
,0); /* null address */
1989 saa_write32(paranges
,0); /* null length */
1990 saalen
= paranges
->datalen
;
1991 arangeslen
= saalen
+ 4;
1992 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1993 WRITELONG(pbuf
,saalen
); /* initial length */
1994 saa_rnbytes(paranges
, pbuf
, saalen
);
1997 /* build rela.aranges section */
1998 arangesrellen
= saalen
= parangesrel
->datalen
;
1999 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2000 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2001 saa_free(parangesrel
);
2003 /* build pubnames section */
2004 ppubnames
= saa_init(1L);
2005 saa_write16(ppubnames
,3); /* dwarf version */
2006 saa_write32(ppubnames
,0); /* offset into info */
2007 saa_write32(ppubnames
,0); /* space used in info */
2008 saa_write32(ppubnames
,0); /* end of list */
2009 saalen
= ppubnames
->datalen
;
2010 pubnameslen
= saalen
+ 4;
2011 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2012 WRITELONG(pbuf
,saalen
); /* initial length */
2013 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2014 saa_free(ppubnames
);
2016 /* build info section */
2017 pinfo
= saa_init(1L);
2018 pinforel
= saa_init(1L);
2019 saa_write16(pinfo
,2); /* dwarf version */
2020 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2021 saa_write32(pinforel
, (dwarf_abbrevsym
<< 8) + R_386_32
); /* reloc to abbrev */
2022 saa_write32(pinforel
, 0);
2023 saa_write32(pinfo
,0); /* offset into abbrev */
2024 saa_write8(pinfo
,4); /* pointer size */
2025 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2026 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2027 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2028 saa_write32(pinforel
, 0);
2029 saa_write32(pinfo
,0); /* DW_AT_low_pc */
2030 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2031 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2032 saa_write32(pinforel
, 0);
2033 saa_write32(pinfo
,highaddr
); /* DW_AT_high_pc */
2034 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2035 saa_write32(pinforel
, (dwarf_linesym
<< 8) + R_386_32
); /* reloc to line */
2036 saa_write32(pinforel
, 0);
2037 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2038 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2039 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2040 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2041 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2042 saa_write32(pinforel
, pinfo
->datalen
+ 4);
2043 saa_write32(pinforel
, ((dwarf_fsect
->section
+ 2) << 8) + R_386_32
);
2044 saa_write32(pinforel
, 0);
2045 saa_write32(pinfo
,0); /* DW_AT_low_pc */
2046 saa_write32(pinfo
,0); /* DW_AT_frame_base */
2047 saa_write8(pinfo
,0); /* end of entries */
2048 saalen
= pinfo
->datalen
;
2049 infolen
= saalen
+ 4;
2050 infobuf
= pbuf
= nasm_malloc(infolen
);
2051 WRITELONG(pbuf
,saalen
); /* initial length */
2052 saa_rnbytes(pinfo
, pbuf
, saalen
);
2055 /* build rela.info section */
2056 inforellen
= saalen
= pinforel
->datalen
;
2057 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2058 saa_rnbytes(pinforel
, pbuf
, saalen
);
2061 /* build abbrev section */
2062 pabbrev
= saa_init(1L);
2063 saa_write8(pabbrev
,1); /* entry number LEB128u */
2064 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2065 saa_write8(pabbrev
,1); /* has children */
2066 /* the following attributes and forms are all LEB128u values */
2067 saa_write8(pabbrev
,DW_AT_low_pc
);
2068 saa_write8(pabbrev
,DW_FORM_addr
);
2069 saa_write8(pabbrev
,DW_AT_high_pc
);
2070 saa_write8(pabbrev
,DW_FORM_addr
);
2071 saa_write8(pabbrev
,DW_AT_stmt_list
);
2072 saa_write8(pabbrev
,DW_FORM_data4
);
2073 saa_write8(pabbrev
,DW_AT_name
);
2074 saa_write8(pabbrev
,DW_FORM_string
);
2075 saa_write8(pabbrev
,DW_AT_producer
);
2076 saa_write8(pabbrev
,DW_FORM_string
);
2077 saa_write8(pabbrev
,DW_AT_language
);
2078 saa_write8(pabbrev
,DW_FORM_data2
);
2079 saa_write16(pabbrev
,0); /* end of entry */
2080 /* LEB128u usage same as above */
2081 saa_write8(pabbrev
,2); /* entry number */
2082 saa_write8(pabbrev
,DW_TAG_subprogram
);
2083 saa_write8(pabbrev
,0); /* no children */
2084 saa_write8(pabbrev
,DW_AT_low_pc
);
2085 saa_write8(pabbrev
,DW_FORM_addr
);
2086 saa_write8(pabbrev
,DW_AT_frame_base
);
2087 saa_write8(pabbrev
,DW_FORM_data4
);
2088 saa_write16(pabbrev
,0); /* end of entry */
2089 abbrevlen
= saalen
= pabbrev
->datalen
;
2090 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2091 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2094 /* build line section */
2096 plines
= saa_init(1L);
2097 saa_write8(plines
,1); /* Minimum Instruction Length */
2098 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2099 saa_write8(plines
,line_base
); /* Line Base */
2100 saa_write8(plines
,line_range
); /* Line Range */
2101 saa_write8(plines
,opcode_base
); /* Opcode Base */
2102 /* standard opcode lengths (# of LEB128u operands) */
2103 saa_write8(plines
,0); /* Std opcode 1 length */
2104 saa_write8(plines
,1); /* Std opcode 2 length */
2105 saa_write8(plines
,1); /* Std opcode 3 length */
2106 saa_write8(plines
,1); /* Std opcode 4 length */
2107 saa_write8(plines
,1); /* Std opcode 5 length */
2108 saa_write8(plines
,0); /* Std opcode 6 length */
2109 saa_write8(plines
,0); /* Std opcode 7 length */
2110 saa_write8(plines
,0); /* Std opcode 8 length */
2111 saa_write8(plines
,1); /* Std opcode 9 length */
2112 saa_write8(plines
,0); /* Std opcode 10 length */
2113 saa_write8(plines
,0); /* Std opcode 11 length */
2114 saa_write8(plines
,1); /* Std opcode 12 length */
2115 /* Directory Table */
2116 saa_write8(plines
,0); /* End of table */
2117 /* File Name Table */
2118 ftentry
= dwarf_flist
;
2119 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2121 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2122 saa_write8(plines
,0); /* directory LEB128u */
2123 saa_write8(plines
,0); /* time LEB128u */
2124 saa_write8(plines
,0); /* size LEB128u */
2125 ftentry
= ftentry
->next
;
2127 saa_write8(plines
,0); /* End of table */
2128 linepoff
= plines
->datalen
;
2129 linelen
= linepoff
+ totlen
+ 10;
2130 linebuf
= pbuf
= nasm_malloc(linelen
);
2131 WRITELONG(pbuf
,linelen
-4); /* initial length */
2132 WRITESHORT(pbuf
,3); /* dwarf version */
2133 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2134 /* write line header */
2136 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2139 /* concatonate line program ranges */
2141 plinesrel
= saa_init(1L);
2142 psect
= dwarf_fsect
;
2143 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2145 saa_write32(plinesrel
, linepoff
);
2146 saa_write32(plinesrel
, ((uint32_t) (psect
->section
+ 2) << 8) + R_386_32
);
2147 saa_write32(plinesrel
, (uint32_t) 0);
2148 plinep
= psect
->psaa
;
2149 saalen
= plinep
->datalen
;
2150 saa_rnbytes(plinep
, pbuf
, saalen
);
2154 /* done with this entry */
2155 psect
= psect
->next
;
2159 /* build rela.lines section */
2160 linerellen
=saalen
= plinesrel
->datalen
;
2161 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2162 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2163 saa_free(plinesrel
);
2165 /* build frame section */
2167 framebuf
= pbuf
= nasm_malloc(framelen
);
2168 WRITELONG(pbuf
,framelen
-4); /* initial length */
2170 /* build loc section */
2172 locbuf
= pbuf
= nasm_malloc(loclen
);
2173 WRITELONG(pbuf
,0); /* null beginning offset */
2174 WRITELONG(pbuf
,0); /* null ending offset */
2177 void dwarf32_cleanup(void)
2180 nasm_free(arangesbuf
);
2182 nasm_free(arangesrelbuf
);
2184 nasm_free(pubnamesbuf
);
2188 nasm_free(inforelbuf
);
2190 nasm_free(abbrevbuf
);
2194 nasm_free(linerelbuf
);
2196 nasm_free(framebuf
);
2200 void dwarf32_findfile(const char * fname
)
2203 struct linelist
*match
;
2205 /* return if fname is current file name */
2206 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2207 /* search for match */
2213 match
= dwarf_flist
;
2214 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2216 if (!(strcmp(fname
, match
->filename
)))
2218 dwarf_clist
= match
;
2223 /* add file name to end of list */
2224 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2226 dwarf_clist
->line
= dwarf_numfiles
;
2227 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2228 strcpy(dwarf_clist
->filename
,fname
);
2229 dwarf_clist
->next
= 0;
2230 /* if first entry */
2233 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2234 dwarf_clist
->last
= 0;
2236 /* chain to previous entry */
2239 dwarf_elist
->next
= dwarf_clist
;
2240 dwarf_elist
= dwarf_clist
;
2245 void dwarf32_findsect(const int index
)
2248 struct sectlist
*match
;
2250 /* return if index is current section index */
2251 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2255 /* search for match */
2261 match
= dwarf_fsect
;
2262 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2264 if ((match
->section
== index
))
2266 dwarf_csect
= match
;
2269 match
= match
->next
;
2272 /* add entry to end of list */
2273 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2275 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2276 dwarf_csect
->line
= 1;
2277 dwarf_csect
->offset
= 0;
2278 dwarf_csect
->file
= 1;
2279 dwarf_csect
->section
= index
;
2280 dwarf_csect
->next
= 0;
2281 /* set relocatable address at start of line program */
2282 saa_write8(plinep
,DW_LNS_extended_op
);
2283 saa_write8(plinep
,5); /* operand length */
2284 saa_write8(plinep
,DW_LNE_set_address
);
2285 saa_write32(plinep
,0); /* Start Address */
2286 /* if first entry */
2289 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2290 dwarf_csect
->last
= 0;
2292 /* chain to previous entry */
2295 dwarf_esect
->next
= dwarf_csect
;
2296 dwarf_esect
= dwarf_csect
;