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.
29 R_386_32
= 1, /* ordinary absolute relocation */
30 R_386_PC32
= 2, /* PC-relative relocation */
31 R_386_GOT32
= 3, /* an offset into GOT */
32 R_386_PLT32
= 4, /* a PC-relative offset into PLT */
33 R_386_COPY
= 5, /* ??? */
34 R_386_GLOB_DAT
= 6, /* ??? */
35 R_386_JUMP_SLOT
= 7, /* ??? */
36 R_386_RELATIVE
= 8, /* ??? */
37 R_386_GOTOFF
= 9, /* an offset from GOT base */
38 R_386_GOTPC
= 10, /* a PC-relative offset _to_ GOT */
39 /* These are GNU extensions, but useful */
40 R_386_16
= 20, /* A 16-bit absolute relocation */
41 R_386_PC16
= 21, /* A 16-bit PC-relative relocation */
42 R_386_8
= 22, /* An 8-bit absolute relocation */
43 R_386_PC8
= 23 /* An 8-bit PC-relative relocation */
48 int32_t address
; /* relative to _start_ of section */
49 int32_t symbol
; /* symbol index */
50 int type
; /* type of relocation */
54 int32_t strpos
; /* string table position of name */
55 int32_t section
; /* section ID of the symbol */
56 int type
; /* symbol type */
57 int other
; /* symbol visibility */
58 int32_t value
; /* address, or COMMON variable align */
59 int32_t size
; /* size of symbol */
60 int32_t globnum
; /* symbol table offset if global */
61 struct Symbol
*next
; /* list of globals in each section */
62 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
63 char *name
; /* used temporarily if in above list */
66 #define SHT_PROGBITS 1
71 #define SHF_EXECINSTR 4
75 uint32_t len
, size
, nrelocs
;
77 int type
; /* SHT_PROGBITS or SHT_NOBITS */
78 int align
; /* alignment: power of two */
79 uint32_t flags
; /* section flags */
83 struct Reloc
*head
, **tail
;
84 struct Symbol
*gsyms
; /* global symbols in section */
88 static struct Section
**sects
;
89 static int nsects
, sectlen
;
91 #define SHSTR_DELTA 256
92 static char *shstrtab
;
93 static int shstrtablen
, shstrtabsize
;
95 static struct SAA
*syms
;
96 static uint32_t nlocals
, nglobs
;
98 static int32_t def_seg
;
100 static struct RAA
*bsym
;
102 static struct SAA
*strs
;
103 static uint32_t strslen
;
107 static evalfunc evaluate
;
109 static struct Symbol
*fwds
;
111 static char elf_module
[FILENAME_MAX
];
113 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
114 static uint8_t elf_abiver
= 0; /* Current ABI version */
116 extern struct ofmt of_elf32
;
117 extern struct ofmt of_elf
;
119 #define SHN_ABS 0xFFF1
120 #define SHN_COMMON 0xFFF2
123 #define SYM_GLOBAL 0x10
125 #define STT_NOTYPE 0 /* Symbol type is unspecified */
126 #define STT_OBJECT 1 /* Symbol is a data object */
127 #define STT_FUNC 2 /* Symbol is a code object */
128 #define STT_SECTION 3 /* Symbol associated with a section */
129 #define STT_FILE 4 /* Symbol's name is file name */
130 #define STT_COMMON 5 /* Symbol is a common data object */
131 #define STT_TLS 6 /* Symbol is thread-local data object*/
132 #define STT_NUM 7 /* Number of defined types. */
134 #define STV_DEFAULT 0
135 #define STV_INTERNAL 1
137 #define STV_PROTECTED 3
139 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
141 #define SEG_ALIGN 16 /* alignment of sections in file */
142 #define SEG_ALIGN_1 (SEG_ALIGN-1)
144 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
146 static struct ELF_SECTDATA
{
151 static int elf_nsect
;
152 static int32_t elf_foffs
;
154 static void elf_write(void);
155 static void elf_sect_write(struct Section
*, const uint8_t *,
157 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
159 static void elf_write_sections(void);
160 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
161 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
162 static void add_sectname(char *, char *);
164 /* this stuff is needed for the stabs debugging format */
165 #define N_SO 0x64 /* ID for main source file */
166 #define N_SOL 0x84 /* ID for sub-source file */
170 #define TY_STABSSYMLIN 0x40 /* ouch */
186 int section
; /* section index */
187 char *name
; /* shallow-copied pointer of section name */
191 struct symlininfo info
;
194 struct linelist
*next
;
195 struct linelist
*last
;
198 static struct linelist
*stabslines
= 0;
199 static int stabs_immcall
= 0;
200 static int currentline
= 0;
201 static int numlinestabs
= 0;
202 static char *stabs_filename
= 0;
203 static int symtabsection
;
204 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
205 static int stablen
, stabstrlen
, stabrellen
;
207 static struct dfmt df_stabs
;
208 static struct Symbol
*lastsym
;
210 void stabs32_init(struct ofmt
*, void *, FILE *, efunc
);
211 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
212 void stabs32_deflabel(char *, int32_t, int64_t, int, char *);
213 void stabs32_directive(const char *, const char *);
214 void stabs32_typevalue(int32_t);
215 void stabs32_output(int, void *);
216 void stabs32_generate(void);
217 void stabs32_cleanup(void);
219 /* end of stabs debugging stuff */
222 * Special section numbers which are used to define ELF special
223 * symbols, which can be used with WRT to provide PIC relocation
226 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
227 static int32_t elf_got_sect
, elf_plt_sect
;
228 static int32_t elf_sym_sect
;
230 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
232 if (of_elf
.current_dfmt
!= &null_debug_form
)
233 of_elf32
.current_dfmt
= of_elf
.current_dfmt
;
237 (void)ldef
; /* placate optimisers */
239 nsects
= sectlen
= 0;
240 syms
= saa_init((int32_t)sizeof(struct Symbol
));
241 nlocals
= nglobs
= 0;
244 saa_wbytes(strs
, "\0", 1L);
245 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
246 strslen
= 2 + strlen(elf_module
);
248 shstrtablen
= shstrtabsize
= 0;;
249 add_sectname("", "");
253 elf_gotpc_sect
= seg_alloc();
254 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
256 elf_gotoff_sect
= seg_alloc();
257 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
259 elf_got_sect
= seg_alloc();
260 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
262 elf_plt_sect
= seg_alloc();
263 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
265 elf_sym_sect
= seg_alloc();
266 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
269 def_seg
= seg_alloc();
272 static void elf_cleanup(int debuginfo
)
281 for (i
= 0; i
< nsects
; i
++) {
282 if (sects
[i
]->type
!= SHT_NOBITS
)
283 saa_free(sects
[i
]->data
);
285 saa_free(sects
[i
]->rel
);
286 while (sects
[i
]->head
) {
288 sects
[i
]->head
= sects
[i
]->head
->next
;
296 if (of_elf32
.current_dfmt
) {
297 of_elf32
.current_dfmt
->cleanup();
301 static void add_sectname(char *firsthalf
, char *secondhalf
)
303 int len
= strlen(firsthalf
) + strlen(secondhalf
);
304 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
305 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
306 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
307 strcat(shstrtab
+ shstrtablen
, secondhalf
);
308 shstrtablen
+= len
+ 1;
311 static int elf_make_section(char *name
, int type
, int flags
, int align
)
315 s
= nasm_malloc(sizeof(*s
));
317 if (type
!= SHT_NOBITS
)
318 s
->data
= saa_init(1L);
321 s
->len
= s
->size
= 0;
323 if (!strcmp(name
, ".text"))
326 s
->index
= seg_alloc();
327 add_sectname("", name
);
328 s
->name
= nasm_malloc(1 + strlen(name
));
329 strcpy(s
->name
, name
);
335 if (nsects
>= sectlen
)
337 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
343 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
346 unsigned flags_and
, flags_or
;
350 * Default is 32 bits.
358 while (*p
&& !isspace(*p
))
362 flags_and
= flags_or
= type
= align
= 0;
364 while (*p
&& isspace(*p
))
368 while (*p
&& !isspace(*p
))
372 while (*p
&& isspace(*p
))
375 if (!nasm_strnicmp(q
, "align=", 6)) {
379 if ((align
- 1) & align
) { /* means it's not a power of two */
380 error(ERR_NONFATAL
, "section alignment %d is not"
381 " a power of two", align
);
384 } else if (!nasm_stricmp(q
, "alloc")) {
385 flags_and
|= SHF_ALLOC
;
386 flags_or
|= SHF_ALLOC
;
387 } else if (!nasm_stricmp(q
, "noalloc")) {
388 flags_and
|= SHF_ALLOC
;
389 flags_or
&= ~SHF_ALLOC
;
390 } else if (!nasm_stricmp(q
, "exec")) {
391 flags_and
|= SHF_EXECINSTR
;
392 flags_or
|= SHF_EXECINSTR
;
393 } else if (!nasm_stricmp(q
, "noexec")) {
394 flags_and
|= SHF_EXECINSTR
;
395 flags_or
&= ~SHF_EXECINSTR
;
396 } else if (!nasm_stricmp(q
, "write")) {
397 flags_and
|= SHF_WRITE
;
398 flags_or
|= SHF_WRITE
;
399 } else if (!nasm_stricmp(q
, "nowrite")) {
400 flags_and
|= SHF_WRITE
;
401 flags_or
&= ~SHF_WRITE
;
402 } else if (!nasm_stricmp(q
, "progbits")) {
404 } else if (!nasm_stricmp(q
, "nobits")) {
409 if (!strcmp(name
, ".comment") ||
410 !strcmp(name
, ".shstrtab") ||
411 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
412 error(ERR_NONFATAL
, "attempt to redefine reserved section"
417 for (i
= 0; i
< nsects
; i
++)
418 if (!strcmp(name
, sects
[i
]->name
))
421 if (!strcmp(name
, ".text"))
422 i
= elf_make_section(name
, SHT_PROGBITS
,
423 SHF_ALLOC
| SHF_EXECINSTR
, 16);
424 else if (!strcmp(name
, ".rodata"))
425 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
426 else if (!strcmp(name
, ".data"))
427 i
= elf_make_section(name
, SHT_PROGBITS
,
428 SHF_ALLOC
| SHF_WRITE
, 4);
429 else if (!strcmp(name
, ".bss"))
430 i
= elf_make_section(name
, SHT_NOBITS
,
431 SHF_ALLOC
| SHF_WRITE
, 4);
433 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
435 sects
[i
]->type
= type
;
437 sects
[i
]->align
= align
;
438 sects
[i
]->flags
&= ~flags_and
;
439 sects
[i
]->flags
|= flags_or
;
440 } else if (pass
== 1) {
441 if ((type
&& sects
[i
]->type
!= type
)
442 || (align
&& sects
[i
]->align
!= align
)
443 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
444 error(ERR_WARNING
, "section attributes ignored on"
445 " redeclaration of section `%s'", name
);
448 return sects
[i
]->index
;
451 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
452 int is_global
, char *special
)
456 bool special_used
= false;
458 #if defined(DEBUG) && DEBUG>2
460 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
461 name
, segment
, offset
, is_global
, special
);
463 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
465 * This is a NASM special symbol. We never allow it into
466 * the ELF symbol table, even if it's a valid one. If it
467 * _isn't_ a valid one, we should barf immediately.
469 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
470 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
471 strcmp(name
, "..sym"))
472 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
476 if (is_global
== 3) {
479 * Fix up a forward-reference symbol size from the first
482 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
483 if (!strcmp((*s
)->name
, name
)) {
484 struct tokenval tokval
;
488 while (*p
&& !isspace(*p
))
490 while (*p
&& isspace(*p
))
494 tokval
.t_type
= TOKEN_INVALID
;
495 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
498 error(ERR_NONFATAL
, "cannot use relocatable"
499 " expression as symbol size");
501 (*s
)->size
= reloc_value(e
);
505 * Remove it from the list of unresolved sizes.
507 nasm_free((*s
)->name
);
511 return; /* it wasn't an important one */
514 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
515 strslen
+= 1 + strlen(name
);
517 lastsym
= sym
= saa_wstruct(syms
);
520 sym
->type
= is_global
? SYM_GLOBAL
: 0;
521 sym
->other
= STV_DEFAULT
;
523 if (segment
== NO_SEG
)
524 sym
->section
= SHN_ABS
;
527 sym
->section
= SHN_UNDEF
;
528 if (nsects
== 0 && segment
== def_seg
) {
530 if (segment
!= elf_section_names(".text", 2, &tempint
))
532 "strange segment conditions in ELF driver");
533 sym
->section
= nsects
;
535 for (i
= 0; i
< nsects
; i
++)
536 if (segment
== sects
[i
]->index
) {
537 sym
->section
= i
+ 1;
543 if (is_global
== 2) {
546 sym
->section
= SHN_COMMON
;
548 * We have a common variable. Check the special text to see
549 * if it's a valid number and power of two; if so, store it
550 * as the alignment for the common variable.
554 sym
->value
= readnum(special
, &err
);
556 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
557 " valid number", special
);
558 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
559 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
560 " power of two", special
);
564 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
566 if (sym
->type
== SYM_GLOBAL
) {
568 * If sym->section == SHN_ABS, then the first line of the
569 * else section would cause a core dump, because its a reference
570 * beyond the end of the section array.
571 * This behaviour is exhibited by this code:
574 * To avoid such a crash, such requests are silently discarded.
575 * This may not be the best solution.
577 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
578 bsym
= raa_write(bsym
, segment
, nglobs
);
579 } else if (sym
->section
!= SHN_ABS
) {
581 * This is a global symbol; so we must add it to the linked
582 * list of global symbols in its section. We'll push it on
583 * the beginning of the list, because it doesn't matter
584 * much which end we put it on and it's easier like this.
586 * In addition, we check the special text for symbol
587 * type and size information.
589 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
590 sects
[sym
->section
- 1]->gsyms
= sym
;
593 int n
= strcspn(special
, " \t");
595 if (!nasm_strnicmp(special
, "function", n
))
596 sym
->type
|= STT_FUNC
;
597 else if (!nasm_strnicmp(special
, "data", n
) ||
598 !nasm_strnicmp(special
, "object", n
))
599 sym
->type
|= STT_OBJECT
;
600 else if (!nasm_strnicmp(special
, "notype", n
))
601 sym
->type
|= STT_NOTYPE
;
603 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
607 while (isspace(*special
))
610 n
= strcspn(special
, " \t");
611 if (!nasm_strnicmp(special
, "default", n
))
612 sym
->other
= STV_DEFAULT
;
613 else if (!nasm_strnicmp(special
, "internal", n
))
614 sym
->other
= STV_INTERNAL
;
615 else if (!nasm_strnicmp(special
, "hidden", n
))
616 sym
->other
= STV_HIDDEN
;
617 else if (!nasm_strnicmp(special
, "protected", n
))
618 sym
->other
= STV_PROTECTED
;
625 struct tokenval tokval
;
628 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
630 while (special
[n
] && isspace(special
[n
]))
633 * We have a size expression; attempt to
637 stdscan_bufptr
= special
+ n
;
638 tokval
.t_type
= TOKEN_INVALID
;
639 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
644 sym
->name
= nasm_strdup(name
);
647 error(ERR_NONFATAL
, "cannot use relocatable"
648 " expression as symbol size");
650 sym
->size
= reloc_value(e
);
652 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
657 sym
->globnum
= nglobs
;
662 if (special
&& !special_used
)
663 error(ERR_NONFATAL
, "no special symbol features supported here");
666 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
670 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
671 sect
->tail
= &r
->next
;
674 r
->address
= sect
->len
;
675 if (segment
== NO_SEG
)
680 for (i
= 0; i
< nsects
; i
++)
681 if (segment
== sects
[i
]->index
)
684 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
692 * This routine deals with ..got and ..sym relocations: the more
693 * complicated kinds. In shared-library writing, some relocations
694 * with respect to global symbols must refer to the precise symbol
695 * rather than referring to an offset from the base of the section
696 * _containing_ the symbol. Such relocations call to this routine,
697 * which searches the symbol list for the symbol in question.
699 * R_386_GOT32 references require the _exact_ symbol address to be
700 * used; R_386_32 references can be at an offset from the symbol.
701 * The boolean argument `exact' tells us this.
703 * Return value is the adjusted value of `addr', having become an
704 * offset from the symbol rather than the section. Should always be
705 * zero when returning from an exact call.
707 * Limitation: if you define two symbols at the same place,
708 * confusion will occur.
710 * Inefficiency: we search, currently, using a linked list which
711 * isn't even necessarily sorted.
713 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
714 int32_t segment
, int32_t offset
,
715 int type
, bool exact
)
719 struct Symbol
*sym
, *sm
;
723 * First look up the segment/offset pair and find a global
724 * symbol corresponding to it. If it's not one of our segments,
725 * then it must be an external symbol, in which case we're fine
726 * doing a normal elf_add_reloc after first sanity-checking
727 * that the offset from the symbol is zero.
730 for (i
= 0; i
< nsects
; i
++)
731 if (segment
== sects
[i
]->index
) {
736 if (exact
&& offset
!= 0)
737 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
738 " for this reference");
740 elf_add_reloc(sect
, segment
, type
);
746 * Find a symbol pointing _exactly_ at this one.
748 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
749 if (sym
->value
== offset
)
753 * Find the nearest symbol below this one.
756 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
757 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
761 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
762 " for this reference");
766 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
767 sect
->tail
= &r
->next
;
770 r
->address
= sect
->len
;
771 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
776 return offset
- sym
->value
;
779 static void elf_out(int32_t segto
, const void *data
,
780 enum out_type type
, uint64_t size
,
781 int32_t segment
, int32_t wrt
)
785 uint8_t mydata
[4], *p
;
787 static struct symlininfo sinfo
;
790 * handle absolute-assembly (structure definitions)
792 if (segto
== NO_SEG
) {
793 if (type
!= OUT_RESERVE
)
794 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
800 for (i
= 0; i
< nsects
; i
++)
801 if (segto
== sects
[i
]->index
) {
806 int tempint
; /* ignored */
807 if (segto
!= elf_section_names(".text", 2, &tempint
))
808 error(ERR_PANIC
, "strange segment conditions in ELF driver");
810 s
= sects
[nsects
- 1];
815 /* again some stabs debugging stuff */
816 if (of_elf32
.current_dfmt
) {
817 sinfo
.offset
= s
->len
;
819 sinfo
.name
= s
->name
;
820 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
822 /* end of debugging stuff */
824 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
825 error(ERR_WARNING
, "attempt to initialize memory in"
826 " BSS section `%s': ignored", s
->name
);
827 if (type
== OUT_REL2ADR
)
829 else if (type
== OUT_REL4ADR
)
835 if (type
== OUT_RESERVE
) {
836 if (s
->type
== SHT_PROGBITS
) {
837 error(ERR_WARNING
, "uninitialized space declared in"
838 " non-BSS section `%s': zeroing", s
->name
);
839 elf_sect_write(s
, NULL
, size
);
842 } else if (type
== OUT_RAWDATA
) {
843 if (segment
!= NO_SEG
)
844 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
845 elf_sect_write(s
, data
, size
);
846 } else if (type
== OUT_ADDRESS
) {
848 addr
= *(int64_t *)data
;
849 if (segment
!= NO_SEG
) {
851 error(ERR_NONFATAL
, "ELF format does not support"
852 " segment base references");
857 elf_add_reloc(s
, segment
, R_386_16
);
859 elf_add_reloc(s
, segment
, R_386_32
);
861 } else if (wrt
== elf_gotpc_sect
+ 1) {
863 * The user will supply GOT relative to $$. ELF
864 * will let us have GOT relative to $. So we
865 * need to fix up the data item by $-$$.
868 elf_add_reloc(s
, segment
, R_386_GOTPC
);
869 } else if (wrt
== elf_gotoff_sect
+ 1) {
870 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
871 } else if (wrt
== elf_got_sect
+ 1) {
872 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
874 } else if (wrt
== elf_sym_sect
+ 1) {
877 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
880 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
883 } else if (wrt
== elf_plt_sect
+ 1) {
884 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
885 "relative PLT references");
887 error(ERR_NONFATAL
, "ELF format does not support this"
889 wrt
= NO_SEG
; /* we can at least _try_ to continue */
895 error(ERR_WARNING
| ERR_WARN_GNUELF
,
896 "16-bit relocations in ELF is a GNU extension");
899 if (size
!= 4 && segment
!= NO_SEG
) {
901 "Unsupported non-32-bit ELF relocation");
905 elf_sect_write(s
, mydata
, size
);
906 } else if (type
== OUT_REL2ADR
) {
907 if (segment
== segto
)
908 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
909 if (segment
!= NO_SEG
&& segment
% 2) {
910 error(ERR_NONFATAL
, "ELF format does not support"
911 " segment base references");
914 error(ERR_WARNING
| ERR_WARN_GNUELF
,
915 "16-bit relocations in ELF is a GNU extension");
916 elf_add_reloc(s
, segment
, R_386_PC16
);
919 "Unsupported non-32-bit ELF relocation");
923 WRITESHORT(p
, *(int64_t *)data
- size
);
924 elf_sect_write(s
, mydata
, 2L);
925 } else if (type
== OUT_REL4ADR
) {
926 if (segment
== segto
)
927 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
928 if (segment
!= NO_SEG
&& segment
% 2) {
929 error(ERR_NONFATAL
, "ELF format does not support"
930 " segment base references");
933 elf_add_reloc(s
, segment
, R_386_PC32
);
934 } else if (wrt
== elf_plt_sect
+ 1) {
935 elf_add_reloc(s
, segment
, R_386_PLT32
);
936 } else if (wrt
== elf_gotpc_sect
+ 1 ||
937 wrt
== elf_gotoff_sect
+ 1 ||
938 wrt
== elf_got_sect
+ 1) {
939 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
940 "relative GOT references");
942 error(ERR_NONFATAL
, "ELF format does not support this"
944 wrt
= NO_SEG
; /* we can at least _try_ to continue */
948 WRITELONG(p
, *(int64_t *)data
- size
);
949 elf_sect_write(s
, mydata
, 4L);
953 static void elf_write(void)
955 int nsections
, align
;
963 int32_t symtablen
, symtablocal
;
966 * Work out how many sections we will have. We have SHN_UNDEF,
967 * then the flexible user sections, then the four fixed
968 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
969 * then optionally relocation sections for the user sections.
971 if (of_elf32
.current_dfmt
== &df_stabs
)
974 nsections
= 5; /* SHN_UNDEF and the fixed ones */
976 add_sectname("", ".comment");
977 add_sectname("", ".shstrtab");
978 add_sectname("", ".symtab");
979 add_sectname("", ".strtab");
980 for (i
= 0; i
< nsects
; i
++) {
981 nsections
++; /* for the section itself */
982 if (sects
[i
]->head
) {
983 nsections
++; /* for its relocations */
984 add_sectname(".rel", sects
[i
]->name
);
988 if (of_elf32
.current_dfmt
== &df_stabs
) {
989 /* in case the debug information is wanted, just add these three sections... */
990 add_sectname("", ".stab");
991 add_sectname("", ".stabstr");
992 add_sectname(".rel", ".stab");
1000 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1003 * Output the ELF header.
1005 fwrite("\177ELF\1\1\1", 7, 1, elffp
);
1006 fputc(elf_osabi
, elffp
);
1007 fputc(elf_abiver
, elffp
);
1008 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp
);
1009 fwriteint16_t(1, elffp
); /* ET_REL relocatable file */
1010 fwriteint16_t(3, elffp
); /* EM_386 processor ID */
1011 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1012 fwriteint32_t(0L, elffp
); /* no entry point */
1013 fwriteint32_t(0L, elffp
); /* no program header table */
1014 fwriteint32_t(0x40L
, elffp
); /* section headers straight after
1015 * ELF header plus alignment */
1016 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1017 fwriteint16_t(0x34, elffp
); /* size of ELF header */
1018 fwriteint16_t(0, elffp
); /* no program header table, again */
1019 fwriteint16_t(0, elffp
); /* still no program header table */
1020 fwriteint16_t(0x28, elffp
); /* size of section header */
1021 fwriteint16_t(nsections
, elffp
); /* number of sections */
1022 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1023 * section header table */
1024 fwriteint32_t(0L, elffp
); /* align to 0x40 bytes */
1025 fwriteint32_t(0L, elffp
);
1026 fwriteint32_t(0L, elffp
);
1029 * Build the symbol table and relocation tables.
1031 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1032 for (i
= 0; i
< nsects
; i
++)
1034 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1038 * Now output the section header table.
1041 elf_foffs
= 0x40 + 0x28 * nsections
;
1042 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1045 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1047 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1048 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1050 for (i
= 0; i
< nsects
; i
++) {
1051 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1052 (sects
[i
]->type
== SHT_PROGBITS
?
1053 sects
[i
]->data
: NULL
), true,
1054 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1056 scount
++; /* dito */
1058 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1059 scount
++; /* dito */
1061 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1062 scount
++; /* dito */
1064 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 16); /* .symtab */
1065 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1067 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1068 for (i
= 0; i
< nsects
; i
++)
1069 if (sects
[i
]->head
) {
1071 elf_section_header(p
- shstrtab
, 9, 0, sects
[i
]->rel
, true,
1072 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 8);
1074 if (of_elf32
.current_dfmt
== &df_stabs
) {
1075 /* for debugging information, create the last three sections
1076 which are the .stab , .stabstr and .rel.stab sections respectively */
1078 /* this function call creates the stab sections in memory */
1081 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1083 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1084 nsections
- 2, 0, 4, 12);
1087 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1088 stabstrlen
, 0, 0, 4, 0);
1091 /* link -> symtable info -> section to refer to */
1092 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1093 stabrellen
, symtabsection
, nsections
- 3, 4,
1097 fwrite(align_str
, align
, 1, elffp
);
1100 * Now output the sections.
1102 elf_write_sections();
1104 nasm_free(elf_sects
);
1108 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1110 struct SAA
*s
= saa_init(1L);
1112 uint8_t entry
[16], *p
;
1118 * First, an all-zeros entry, required by the ELF spec.
1120 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1125 * Next, an entry for the file name.
1128 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1129 WRITELONG(p
, 0); /* no value */
1130 WRITELONG(p
, 0); /* no size either */
1131 WRITESHORT(p
, STT_FILE
); /* type FILE */
1132 WRITESHORT(p
, SHN_ABS
);
1133 saa_wbytes(s
, entry
, 16L);
1138 * Now some standard symbols defining the segments, for relocation
1141 for (i
= 1; i
<= nsects
; i
++) {
1143 WRITELONG(p
, 0); /* no symbol name */
1144 WRITELONG(p
, 0); /* offset zero */
1145 WRITELONG(p
, 0); /* size zero */
1146 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1147 WRITESHORT(p
, i
); /* section id */
1148 saa_wbytes(s
, entry
, 16L);
1154 * Now the other local symbols.
1157 while ((sym
= saa_rstruct(syms
))) {
1158 if (sym
->type
& SYM_GLOBAL
)
1161 WRITELONG(p
, sym
->strpos
);
1162 WRITELONG(p
, sym
->value
);
1163 WRITELONG(p
, sym
->size
);
1164 WRITECHAR(p
, sym
->type
); /* type and binding */
1165 WRITECHAR(p
, sym
->other
); /* visibility */
1166 WRITESHORT(p
, sym
->section
);
1167 saa_wbytes(s
, entry
, 16L);
1173 * Now the global symbols.
1176 while ((sym
= saa_rstruct(syms
))) {
1177 if (!(sym
->type
& SYM_GLOBAL
))
1180 WRITELONG(p
, sym
->strpos
);
1181 WRITELONG(p
, sym
->value
);
1182 WRITELONG(p
, sym
->size
);
1183 WRITECHAR(p
, sym
->type
); /* type and binding */
1184 WRITECHAR(p
, sym
->other
); /* visibility */
1185 WRITESHORT(p
, sym
->section
);
1186 saa_wbytes(s
, entry
, 16L);
1193 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1196 uint8_t *p
, entry
[8];
1205 int32_t sym
= r
->symbol
;
1207 if (sym
>= GLOBAL_TEMP_BASE
)
1208 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1211 WRITELONG(p
, r
->address
);
1212 WRITELONG(p
, (sym
<< 8) + r
->type
);
1213 saa_wbytes(s
, entry
, 8L);
1222 static void elf_section_header(int name
, int type
, int flags
,
1223 void *data
, bool is_saa
, int32_t datalen
,
1224 int link
, int info
, int align
, int eltsize
)
1226 elf_sects
[elf_nsect
].data
= data
;
1227 elf_sects
[elf_nsect
].len
= datalen
;
1228 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1231 fwriteint32_t((int32_t)name
, elffp
);
1232 fwriteint32_t((int32_t)type
, elffp
);
1233 fwriteint32_t((int32_t)flags
, elffp
);
1234 fwriteint32_t(0L, elffp
); /* no address, ever, in object files */
1235 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1236 fwriteint32_t(datalen
, elffp
);
1238 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1239 fwriteint32_t((int32_t)link
, elffp
);
1240 fwriteint32_t((int32_t)info
, elffp
);
1241 fwriteint32_t((int32_t)align
, elffp
);
1242 fwriteint32_t((int32_t)eltsize
, elffp
);
1245 static void elf_write_sections(void)
1248 for (i
= 0; i
< elf_nsect
; i
++)
1249 if (elf_sects
[i
].data
) {
1250 int32_t len
= elf_sects
[i
].len
;
1251 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1252 int32_t align
= reallen
- len
;
1253 if (elf_sects
[i
].is_saa
)
1254 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1256 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1257 fwrite(align_str
, align
, 1, elffp
);
1261 static void elf_sect_write(struct Section
*sect
,
1262 const uint8_t *data
, uint32_t len
)
1264 saa_wbytes(sect
->data
, data
, len
);
1268 static int32_t elf_segbase(int32_t segment
)
1273 static int elf_directive(char *directive
, char *value
, int pass
)
1279 if (!strcmp(directive
, "osabi")) {
1281 return 1; /* ignore in pass 2 */
1283 n
= readnum(value
, &err
);
1285 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1288 if (n
< 0 || n
> 255) {
1289 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1295 if ((p
= strchr(value
,',')) == NULL
)
1298 n
= readnum(p
+1, &err
);
1299 if (err
|| n
< 0 || n
> 255) {
1300 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1311 static void elf_filename(char *inname
, char *outname
, efunc error
)
1313 strcpy(elf_module
, inname
);
1314 standard_extension(inname
, outname
, ".o", error
);
1317 static const char *elf_stdmac
[] = {
1318 "%define __SECT__ [section .text]",
1319 "%macro __NASM_CDecl__ 1",
1322 "%macro osabi 1+.nolist",
1327 static int elf_set_info(enum geninfo type
, char **val
)
1334 static struct dfmt df_stabs
= {
1335 "ELF32 (i386) stabs debug format for Linux",
1346 struct dfmt
*elf32_debugs_arr
[2] = { &df_stabs
, NULL
};
1348 struct ofmt of_elf32
= {
1349 "ELF32 (i386) object files (e.g. Linux)",
1366 struct ofmt of_elf
= {
1367 "ELF (short name for ELF32) ",
1383 /* again, the stabs debugging stuff (code) */
1385 void stabs32_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1393 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1397 if (!stabs_filename
) {
1398 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1399 strcpy(stabs_filename
, filename
);
1401 if (strcmp(stabs_filename
, filename
)) {
1402 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1403 in fact, this leak comes in quite handy to maintain a list of files
1404 encountered so far in the symbol lines... */
1406 /* why not nasm_free(stabs_filename); we're done with the old one */
1408 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1409 strcpy(stabs_filename
, filename
);
1413 currentline
= linenumber
;
1416 void stabs32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1426 void stabs32_directive(const char *directive
, const char *params
)
1432 void stabs32_typevalue(int32_t type
)
1434 int32_t stype
, ssize
;
1435 switch (TYM_TYPE(type
)) {
1474 stype
= STT_SECTION
;
1489 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1490 lastsym
->size
= ssize
;
1491 lastsym
->type
= stype
;
1495 void stabs32_output(int type
, void *param
)
1497 struct symlininfo
*s
;
1498 struct linelist
*el
;
1499 if (type
== TY_STABSSYMLIN
) {
1500 if (stabs_immcall
) {
1501 s
= (struct symlininfo
*)param
;
1502 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1503 return; /* we are only interested in the text stuff */
1505 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1506 el
->info
.offset
= s
->offset
;
1507 el
->info
.section
= s
->section
;
1508 el
->info
.name
= s
->name
;
1509 el
->line
= currentline
;
1510 el
->filename
= stabs_filename
;
1513 stabslines
->last
->next
= el
;
1514 stabslines
->last
= el
;
1517 stabslines
->last
= el
;
1524 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1526 WRITELONG(p,n_strx); \
1527 WRITECHAR(p,n_type); \
1528 WRITECHAR(p,n_other); \
1529 WRITESHORT(p,n_desc); \
1530 WRITELONG(p,n_value); \
1533 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1535 void stabs32_generate(void)
1537 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1538 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1542 struct linelist
*ptr
;
1546 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1547 for (i
= 0; i
< numlinestabs
; i
++)
1551 if (numfiles
== 0) {
1552 allfiles
[0] = ptr
->filename
;
1555 for (i
= 0; i
< numfiles
; i
++) {
1556 if (!strcmp(allfiles
[i
], ptr
->filename
))
1559 if (i
>= numfiles
) {
1560 allfiles
[i
] = ptr
->filename
;
1567 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1568 for (i
= 0; i
< numfiles
; i
++) {
1569 fileidx
[i
] = strsize
;
1570 strsize
+= strlen(allfiles
[i
]) + 1;
1573 for (i
= 0; i
< numfiles
; i
++) {
1574 if (!strcmp(allfiles
[i
], elf_module
)) {
1580 /* worst case size of the stab buffer would be:
1581 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1584 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1585 sizeof(struct stabentry
));
1587 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1589 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1592 for (i
= 0; i
< numfiles
; i
++) {
1593 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1597 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1604 /* this is the first stab, its strx points to the filename of the
1605 the source-file, the n_desc field should be set to the number
1608 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1610 /* this is the stab for the main source file */
1611 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1613 /* relocation table entry */
1615 /* Since the symbol table has two entries before */
1616 /* the section symbols, the index in the info.section */
1617 /* member must be adjusted by adding 2 */
1619 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1620 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1623 currfile
= mainfileindex
;
1627 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1628 /* oops file has changed... */
1629 for (i
= 0; i
< numfiles
; i
++)
1630 if (!strcmp(allfiles
[i
], ptr
->filename
))
1633 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1637 /* relocation table entry */
1638 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1639 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1642 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1645 /* relocation table entry */
1647 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1648 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1654 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1656 nasm_free(allfiles
);
1659 stablen
= (sptr
- sbuf
);
1660 stabrellen
= (rptr
- rbuf
);
1666 void stabs32_cleanup(void)
1668 struct linelist
*ptr
, *del
;
1680 nasm_free(stabrelbuf
);
1682 nasm_free(stabstrbuf
);