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 licence given in the file "Licence"
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 16 /* bigger than any constant 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 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
147 static struct ELF_SECTDATA
{
152 static int elf_nsect
;
153 static int32_t elf_foffs
;
155 static void elf_write(void);
156 static void elf_sect_write(struct Section
*, const uint8_t *,
158 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
160 static void elf_write_sections(void);
161 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
162 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
163 static void add_sectname(char *, char *);
165 /* this stuff is needed for the stabs debugging format */
166 #define N_SO 0x64 /* ID for main source file */
167 #define N_SOL 0x84 /* ID for sub-source file */
171 #define TY_STABSSYMLIN 0x40 /* ouch */
187 int section
; /* section index */
188 char *name
; /* shallow-copied pointer of section name */
192 struct symlininfo info
;
195 struct linelist
*next
;
196 struct linelist
*last
;
199 static struct linelist
*stabslines
= 0;
200 static int stabs_immcall
= 0;
201 static int currentline
= 0;
202 static int numlinestabs
= 0;
203 static char *stabs_filename
= 0;
204 static int symtabsection
;
205 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
206 static int stablen
, stabstrlen
, stabrellen
;
208 static struct dfmt df_stabs
;
209 static struct Symbol
*lastsym
;
211 void stabs32_init(struct ofmt
*, void *, FILE *, efunc
);
212 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
213 void stabs32_deflabel(char *, int32_t, int64_t, int, char *);
214 void stabs32_directive(const char *, const char *);
215 void stabs32_typevalue(int32_t);
216 void stabs32_output(int, void *);
217 void stabs32_generate(void);
218 void stabs32_cleanup(void);
220 /* end of stabs debugging stuff */
223 * Special section numbers which are used to define ELF special
224 * symbols, which can be used with WRT to provide PIC relocation
227 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
228 static int32_t elf_got_sect
, elf_plt_sect
;
229 static int32_t elf_sym_sect
;
231 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
236 (void)ldef
; /* placate optimisers */
238 nsects
= sectlen
= 0;
239 syms
= saa_init((int32_t)sizeof(struct Symbol
));
240 nlocals
= nglobs
= 0;
243 saa_wbytes(strs
, "\0", 1L);
244 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
245 strslen
= 2 + strlen(elf_module
);
247 shstrtablen
= shstrtabsize
= 0;;
248 add_sectname("", "");
252 elf_gotpc_sect
= seg_alloc();
253 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
255 elf_gotoff_sect
= seg_alloc();
256 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
258 elf_got_sect
= seg_alloc();
259 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
261 elf_plt_sect
= seg_alloc();
262 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
264 elf_sym_sect
= seg_alloc();
265 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
268 def_seg
= seg_alloc();
271 static void elf_cleanup(int debuginfo
)
280 for (i
= 0; i
< nsects
; i
++) {
281 if (sects
[i
]->type
!= SHT_NOBITS
)
282 saa_free(sects
[i
]->data
);
284 saa_free(sects
[i
]->rel
);
285 while (sects
[i
]->head
) {
287 sects
[i
]->head
= sects
[i
]->head
->next
;
295 if (of_elf32
.current_dfmt
) {
296 of_elf32
.current_dfmt
->cleanup();
300 static void add_sectname(char *firsthalf
, char *secondhalf
)
302 int len
= strlen(firsthalf
) + strlen(secondhalf
);
303 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
304 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
305 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
306 strcat(shstrtab
+ shstrtablen
, secondhalf
);
307 shstrtablen
+= len
+ 1;
310 static int elf_make_section(char *name
, int type
, int flags
, int align
)
314 s
= nasm_malloc(sizeof(*s
));
316 if (type
!= SHT_NOBITS
)
317 s
->data
= saa_init(1L);
320 s
->len
= s
->size
= 0;
322 if (!strcmp(name
, ".text"))
325 s
->index
= seg_alloc();
326 add_sectname("", name
);
327 s
->name
= nasm_malloc(1 + strlen(name
));
328 strcpy(s
->name
, name
);
334 if (nsects
>= sectlen
)
336 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
342 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
345 unsigned flags_and
, flags_or
;
349 * Default is 32 bits.
357 while (*p
&& !isspace(*p
))
361 flags_and
= flags_or
= type
= align
= 0;
363 while (*p
&& isspace(*p
))
367 while (*p
&& !isspace(*p
))
371 while (*p
&& isspace(*p
))
374 if (!nasm_strnicmp(q
, "align=", 6)) {
378 if ((align
- 1) & align
) { /* means it's not a power of two */
379 error(ERR_NONFATAL
, "section alignment %d is not"
380 " a power of two", align
);
383 } else if (!nasm_stricmp(q
, "alloc")) {
384 flags_and
|= SHF_ALLOC
;
385 flags_or
|= SHF_ALLOC
;
386 } else if (!nasm_stricmp(q
, "noalloc")) {
387 flags_and
|= SHF_ALLOC
;
388 flags_or
&= ~SHF_ALLOC
;
389 } else if (!nasm_stricmp(q
, "exec")) {
390 flags_and
|= SHF_EXECINSTR
;
391 flags_or
|= SHF_EXECINSTR
;
392 } else if (!nasm_stricmp(q
, "noexec")) {
393 flags_and
|= SHF_EXECINSTR
;
394 flags_or
&= ~SHF_EXECINSTR
;
395 } else if (!nasm_stricmp(q
, "write")) {
396 flags_and
|= SHF_WRITE
;
397 flags_or
|= SHF_WRITE
;
398 } else if (!nasm_stricmp(q
, "nowrite")) {
399 flags_and
|= SHF_WRITE
;
400 flags_or
&= ~SHF_WRITE
;
401 } else if (!nasm_stricmp(q
, "progbits")) {
403 } else if (!nasm_stricmp(q
, "nobits")) {
408 if (!strcmp(name
, ".comment") ||
409 !strcmp(name
, ".shstrtab") ||
410 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
411 error(ERR_NONFATAL
, "attempt to redefine reserved section"
416 for (i
= 0; i
< nsects
; i
++)
417 if (!strcmp(name
, sects
[i
]->name
))
420 if (!strcmp(name
, ".text"))
421 i
= elf_make_section(name
, SHT_PROGBITS
,
422 SHF_ALLOC
| SHF_EXECINSTR
, 16);
423 else if (!strcmp(name
, ".rodata"))
424 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
425 else if (!strcmp(name
, ".data"))
426 i
= elf_make_section(name
, SHT_PROGBITS
,
427 SHF_ALLOC
| SHF_WRITE
, 4);
428 else if (!strcmp(name
, ".bss"))
429 i
= elf_make_section(name
, SHT_NOBITS
,
430 SHF_ALLOC
| SHF_WRITE
, 4);
432 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
434 sects
[i
]->type
= type
;
436 sects
[i
]->align
= align
;
437 sects
[i
]->flags
&= ~flags_and
;
438 sects
[i
]->flags
|= flags_or
;
439 } else if (pass
== 1) {
440 if ((type
&& sects
[i
]->type
!= type
)
441 || (align
&& sects
[i
]->align
!= align
)
442 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
443 error(ERR_WARNING
, "section attributes ignored on"
444 " redeclaration of section `%s'", name
);
447 return sects
[i
]->index
;
450 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
451 int is_global
, char *special
)
455 bool special_used
= false;
457 #if defined(DEBUG) && DEBUG>2
459 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
460 name
, segment
, offset
, is_global
, special
);
462 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
464 * This is a NASM special symbol. We never allow it into
465 * the ELF symbol table, even if it's a valid one. If it
466 * _isn't_ a valid one, we should barf immediately.
468 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
469 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
470 strcmp(name
, "..sym"))
471 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
475 if (is_global
== 3) {
478 * Fix up a forward-reference symbol size from the first
481 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
482 if (!strcmp((*s
)->name
, name
)) {
483 struct tokenval tokval
;
487 while (*p
&& !isspace(*p
))
489 while (*p
&& isspace(*p
))
493 tokval
.t_type
= TOKEN_INVALID
;
494 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
497 error(ERR_NONFATAL
, "cannot use relocatable"
498 " expression as symbol size");
500 (*s
)->size
= reloc_value(e
);
504 * Remove it from the list of unresolved sizes.
506 nasm_free((*s
)->name
);
510 return; /* it wasn't an important one */
513 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
514 strslen
+= 1 + strlen(name
);
516 lastsym
= sym
= saa_wstruct(syms
);
519 sym
->type
= is_global
? SYM_GLOBAL
: 0;
520 sym
->other
= STV_DEFAULT
;
522 if (segment
== NO_SEG
)
523 sym
->section
= SHN_ABS
;
526 sym
->section
= SHN_UNDEF
;
527 if (nsects
== 0 && segment
== def_seg
) {
529 if (segment
!= elf_section_names(".text", 2, &tempint
))
531 "strange segment conditions in ELF driver");
532 sym
->section
= nsects
;
534 for (i
= 0; i
< nsects
; i
++)
535 if (segment
== sects
[i
]->index
) {
536 sym
->section
= i
+ 1;
542 if (is_global
== 2) {
545 sym
->section
= SHN_COMMON
;
547 * We have a common variable. Check the special text to see
548 * if it's a valid number and power of two; if so, store it
549 * as the alignment for the common variable.
553 sym
->value
= readnum(special
, &err
);
555 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
556 " valid number", special
);
557 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
558 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
559 " power of two", special
);
563 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
565 if (sym
->type
== SYM_GLOBAL
) {
567 * If sym->section == SHN_ABS, then the first line of the
568 * else section would cause a core dump, because its a reference
569 * beyond the end of the section array.
570 * This behaviour is exhibited by this code:
573 * To avoid such a crash, such requests are silently discarded.
574 * This may not be the best solution.
576 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
577 bsym
= raa_write(bsym
, segment
, nglobs
);
578 } else if (sym
->section
!= SHN_ABS
) {
580 * This is a global symbol; so we must add it to the linked
581 * list of global symbols in its section. We'll push it on
582 * the beginning of the list, because it doesn't matter
583 * much which end we put it on and it's easier like this.
585 * In addition, we check the special text for symbol
586 * type and size information.
588 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
589 sects
[sym
->section
- 1]->gsyms
= sym
;
592 int n
= strcspn(special
, " \t");
594 if (!nasm_strnicmp(special
, "function", n
))
595 sym
->type
|= STT_FUNC
;
596 else if (!nasm_strnicmp(special
, "data", n
) ||
597 !nasm_strnicmp(special
, "object", n
))
598 sym
->type
|= STT_OBJECT
;
599 else if (!nasm_strnicmp(special
, "notype", n
))
600 sym
->type
|= STT_NOTYPE
;
602 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
606 while (isspace(*special
))
609 n
= strcspn(special
, " \t");
610 if (!nasm_strnicmp(special
, "default", n
))
611 sym
->other
= STV_DEFAULT
;
612 else if (!nasm_strnicmp(special
, "internal", n
))
613 sym
->other
= STV_INTERNAL
;
614 else if (!nasm_strnicmp(special
, "hidden", n
))
615 sym
->other
= STV_HIDDEN
;
616 else if (!nasm_strnicmp(special
, "protected", n
))
617 sym
->other
= STV_PROTECTED
;
624 struct tokenval tokval
;
627 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
629 while (special
[n
] && isspace(special
[n
]))
632 * We have a size expression; attempt to
636 stdscan_bufptr
= special
+ n
;
637 tokval
.t_type
= TOKEN_INVALID
;
638 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
643 sym
->name
= nasm_strdup(name
);
646 error(ERR_NONFATAL
, "cannot use relocatable"
647 " expression as symbol size");
649 sym
->size
= reloc_value(e
);
651 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
656 sym
->globnum
= nglobs
;
661 if (special
&& !special_used
)
662 error(ERR_NONFATAL
, "no special symbol features supported here");
665 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
669 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
670 sect
->tail
= &r
->next
;
673 r
->address
= sect
->len
;
674 if (segment
== NO_SEG
)
679 for (i
= 0; i
< nsects
; i
++)
680 if (segment
== sects
[i
]->index
)
683 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
691 * This routine deals with ..got and ..sym relocations: the more
692 * complicated kinds. In shared-library writing, some relocations
693 * with respect to global symbols must refer to the precise symbol
694 * rather than referring to an offset from the base of the section
695 * _containing_ the symbol. Such relocations call to this routine,
696 * which searches the symbol list for the symbol in question.
698 * R_386_GOT32 references require the _exact_ symbol address to be
699 * used; R_386_32 references can be at an offset from the symbol.
700 * The boolean argument `exact' tells us this.
702 * Return value is the adjusted value of `addr', having become an
703 * offset from the symbol rather than the section. Should always be
704 * zero when returning from an exact call.
706 * Limitation: if you define two symbols at the same place,
707 * confusion will occur.
709 * Inefficiency: we search, currently, using a linked list which
710 * isn't even necessarily sorted.
712 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
713 int32_t segment
, int32_t offset
,
714 int type
, bool exact
)
718 struct Symbol
*sym
, *sm
;
722 * First look up the segment/offset pair and find a global
723 * symbol corresponding to it. If it's not one of our segments,
724 * then it must be an external symbol, in which case we're fine
725 * doing a normal elf_add_reloc after first sanity-checking
726 * that the offset from the symbol is zero.
729 for (i
= 0; i
< nsects
; i
++)
730 if (segment
== sects
[i
]->index
) {
735 if (exact
&& offset
!= 0)
736 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
737 " for this reference");
739 elf_add_reloc(sect
, segment
, type
);
745 * Find a symbol pointing _exactly_ at this one.
747 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
748 if (sym
->value
== offset
)
752 * Find the nearest symbol below this one.
755 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
756 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
760 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
761 " for this reference");
765 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
766 sect
->tail
= &r
->next
;
769 r
->address
= sect
->len
;
770 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
775 return offset
- sym
->value
;
778 static void elf_out(int32_t segto
, const void *data
,
779 enum out_type type
, uint64_t size
,
780 int32_t segment
, int32_t wrt
)
784 uint8_t mydata
[4], *p
;
786 static struct symlininfo sinfo
;
789 * handle absolute-assembly (structure definitions)
791 if (segto
== NO_SEG
) {
792 if (type
!= OUT_RESERVE
)
793 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
799 for (i
= 0; i
< nsects
; i
++)
800 if (segto
== sects
[i
]->index
) {
805 int tempint
; /* ignored */
806 if (segto
!= elf_section_names(".text", 2, &tempint
))
807 error(ERR_PANIC
, "strange segment conditions in ELF driver");
809 s
= sects
[nsects
- 1];
814 /* again some stabs debugging stuff */
815 if (of_elf32
.current_dfmt
) {
816 sinfo
.offset
= s
->len
;
818 sinfo
.name
= s
->name
;
819 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
821 /* end of debugging stuff */
823 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
824 error(ERR_WARNING
, "attempt to initialize memory in"
825 " BSS section `%s': ignored", s
->name
);
826 if (type
== OUT_REL2ADR
)
828 else if (type
== OUT_REL4ADR
)
834 if (type
== OUT_RESERVE
) {
835 if (s
->type
== SHT_PROGBITS
) {
836 error(ERR_WARNING
, "uninitialized space declared in"
837 " non-BSS section `%s': zeroing", s
->name
);
838 elf_sect_write(s
, NULL
, size
);
841 } else if (type
== OUT_RAWDATA
) {
842 if (segment
!= NO_SEG
)
843 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
844 elf_sect_write(s
, data
, size
);
845 } else if (type
== OUT_ADDRESS
) {
847 addr
= *(int64_t *)data
;
848 if (segment
!= NO_SEG
) {
850 error(ERR_NONFATAL
, "ELF format does not support"
851 " segment base references");
856 elf_add_reloc(s
, segment
, R_386_16
);
858 elf_add_reloc(s
, segment
, R_386_32
);
860 } else if (wrt
== elf_gotpc_sect
+ 1) {
862 * The user will supply GOT relative to $$. ELF
863 * will let us have GOT relative to $. So we
864 * need to fix up the data item by $-$$.
867 elf_add_reloc(s
, segment
, R_386_GOTPC
);
868 } else if (wrt
== elf_gotoff_sect
+ 1) {
869 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
870 } else if (wrt
== elf_got_sect
+ 1) {
871 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
873 } else if (wrt
== elf_sym_sect
+ 1) {
876 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
879 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
882 } else if (wrt
== elf_plt_sect
+ 1) {
883 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
884 "relative PLT references");
886 error(ERR_NONFATAL
, "ELF format does not support this"
888 wrt
= NO_SEG
; /* we can at least _try_ to continue */
894 error(ERR_WARNING
| ERR_WARN_GNUELF
,
895 "16-bit relocations in ELF is a GNU extension");
898 if (size
!= 4 && segment
!= NO_SEG
) {
900 "Unsupported non-32-bit ELF relocation");
904 elf_sect_write(s
, mydata
, size
);
905 } else if (type
== OUT_REL2ADR
) {
906 if (segment
== segto
)
907 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
908 if (segment
!= NO_SEG
&& segment
% 2) {
909 error(ERR_NONFATAL
, "ELF format does not support"
910 " segment base references");
913 error(ERR_WARNING
| ERR_WARN_GNUELF
,
914 "16-bit relocations in ELF is a GNU extension");
915 elf_add_reloc(s
, segment
, R_386_PC16
);
918 "Unsupported non-32-bit ELF relocation");
922 WRITESHORT(p
, *(int64_t *)data
- size
);
923 elf_sect_write(s
, mydata
, 2L);
924 } else if (type
== OUT_REL4ADR
) {
925 if (segment
== segto
)
926 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
927 if (segment
!= NO_SEG
&& segment
% 2) {
928 error(ERR_NONFATAL
, "ELF format does not support"
929 " segment base references");
932 elf_add_reloc(s
, segment
, R_386_PC32
);
933 } else if (wrt
== elf_plt_sect
+ 1) {
934 elf_add_reloc(s
, segment
, R_386_PLT32
);
935 } else if (wrt
== elf_gotpc_sect
+ 1 ||
936 wrt
== elf_gotoff_sect
+ 1 ||
937 wrt
== elf_got_sect
+ 1) {
938 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
939 "relative GOT references");
941 error(ERR_NONFATAL
, "ELF format does not support this"
943 wrt
= NO_SEG
; /* we can at least _try_ to continue */
947 WRITELONG(p
, *(int64_t *)data
- size
);
948 elf_sect_write(s
, mydata
, 4L);
952 static void elf_write(void)
954 int nsections
, align
;
962 int32_t symtablen
, symtablocal
;
965 * Work out how many sections we will have. We have SHN_UNDEF,
966 * then the flexible user sections, then the four fixed
967 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
968 * then optionally relocation sections for the user sections.
970 if (of_elf32
.current_dfmt
== &df_stabs
)
973 nsections
= 5; /* SHN_UNDEF and the fixed ones */
975 add_sectname("", ".comment");
976 add_sectname("", ".shstrtab");
977 add_sectname("", ".symtab");
978 add_sectname("", ".strtab");
979 for (i
= 0; i
< nsects
; i
++) {
980 nsections
++; /* for the section itself */
981 if (sects
[i
]->head
) {
982 nsections
++; /* for its relocations */
983 add_sectname(".rel", sects
[i
]->name
);
987 if (of_elf32
.current_dfmt
== &df_stabs
) {
988 /* in case the debug information is wanted, just add these three sections... */
989 add_sectname("", ".stab");
990 add_sectname("", ".stabstr");
991 add_sectname(".rel", ".stab");
999 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1002 * Output the ELF header.
1004 fwrite("\177ELF\1\1\1", 7, 1, elffp
);
1005 fputc(elf_osabi
, elffp
);
1006 fputc(elf_abiver
, elffp
);
1007 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp
);
1008 fwriteint16_t(1, elffp
); /* ET_REL relocatable file */
1009 fwriteint16_t(3, elffp
); /* EM_386 processor ID */
1010 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1011 fwriteint32_t(0L, elffp
); /* no entry point */
1012 fwriteint32_t(0L, elffp
); /* no program header table */
1013 fwriteint32_t(0x40L
, elffp
); /* section headers straight after
1014 * ELF header plus alignment */
1015 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1016 fwriteint16_t(0x34, elffp
); /* size of ELF header */
1017 fwriteint16_t(0, elffp
); /* no program header table, again */
1018 fwriteint16_t(0, elffp
); /* still no program header table */
1019 fwriteint16_t(0x28, elffp
); /* size of section header */
1020 fwriteint16_t(nsections
, elffp
); /* number of sections */
1021 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1022 * section header table */
1023 fwriteint32_t(0L, elffp
); /* align to 0x40 bytes */
1024 fwriteint32_t(0L, elffp
);
1025 fwriteint32_t(0L, elffp
);
1028 * Build the symbol table and relocation tables.
1030 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1031 for (i
= 0; i
< nsects
; i
++)
1033 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1037 * Now output the section header table.
1040 elf_foffs
= 0x40 + 0x28 * nsections
;
1041 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1044 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1046 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1047 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1049 for (i
= 0; i
< nsects
; i
++) {
1050 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1051 (sects
[i
]->type
== SHT_PROGBITS
?
1052 sects
[i
]->data
: NULL
), true,
1053 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1055 scount
++; /* dito */
1057 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1058 scount
++; /* dito */
1060 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1061 scount
++; /* dito */
1063 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 16); /* .symtab */
1064 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1066 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1067 for (i
= 0; i
< nsects
; i
++)
1068 if (sects
[i
]->head
) {
1070 elf_section_header(p
- shstrtab
, 9, 0, sects
[i
]->rel
, true,
1071 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 8);
1073 if (of_elf32
.current_dfmt
== &df_stabs
) {
1074 /* for debugging information, create the last three sections
1075 which are the .stab , .stabstr and .rel.stab sections respectively */
1077 /* this function call creates the stab sections in memory */
1080 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1082 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1083 nsections
- 2, 0, 4, 12);
1086 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1087 stabstrlen
, 0, 0, 4, 0);
1090 /* link -> symtable info -> section to refer to */
1091 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1092 stabrellen
, symtabsection
, nsections
- 3, 4,
1096 fwrite(align_str
, align
, 1, elffp
);
1099 * Now output the sections.
1101 elf_write_sections();
1103 nasm_free(elf_sects
);
1107 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1109 struct SAA
*s
= saa_init(1L);
1111 uint8_t entry
[16], *p
;
1117 * First, an all-zeros entry, required by the ELF spec.
1119 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1124 * Next, an entry for the file name.
1127 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1128 WRITELONG(p
, 0); /* no value */
1129 WRITELONG(p
, 0); /* no size either */
1130 WRITESHORT(p
, STT_FILE
); /* type FILE */
1131 WRITESHORT(p
, SHN_ABS
);
1132 saa_wbytes(s
, entry
, 16L);
1137 * Now some standard symbols defining the segments, for relocation
1140 for (i
= 1; i
<= nsects
; i
++) {
1142 WRITELONG(p
, 0); /* no symbol name */
1143 WRITELONG(p
, 0); /* offset zero */
1144 WRITELONG(p
, 0); /* size zero */
1145 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1146 WRITESHORT(p
, i
); /* section id */
1147 saa_wbytes(s
, entry
, 16L);
1153 * Now the other local symbols.
1156 while ((sym
= saa_rstruct(syms
))) {
1157 if (sym
->type
& SYM_GLOBAL
)
1160 WRITELONG(p
, sym
->strpos
);
1161 WRITELONG(p
, sym
->value
);
1162 WRITELONG(p
, sym
->size
);
1163 WRITECHAR(p
, sym
->type
); /* type and binding */
1164 WRITECHAR(p
, sym
->other
); /* visibility */
1165 WRITESHORT(p
, sym
->section
);
1166 saa_wbytes(s
, entry
, 16L);
1172 * Now the global symbols.
1175 while ((sym
= saa_rstruct(syms
))) {
1176 if (!(sym
->type
& SYM_GLOBAL
))
1179 WRITELONG(p
, sym
->strpos
);
1180 WRITELONG(p
, sym
->value
);
1181 WRITELONG(p
, sym
->size
);
1182 WRITECHAR(p
, sym
->type
); /* type and binding */
1183 WRITECHAR(p
, sym
->other
); /* visibility */
1184 WRITESHORT(p
, sym
->section
);
1185 saa_wbytes(s
, entry
, 16L);
1192 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1195 uint8_t *p
, entry
[8];
1204 int32_t sym
= r
->symbol
;
1206 if (sym
>= GLOBAL_TEMP_BASE
)
1207 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1210 WRITELONG(p
, r
->address
);
1211 WRITELONG(p
, (sym
<< 8) + r
->type
);
1212 saa_wbytes(s
, entry
, 8L);
1221 static void elf_section_header(int name
, int type
, int flags
,
1222 void *data
, bool is_saa
, int32_t datalen
,
1223 int link
, int info
, int align
, int eltsize
)
1225 elf_sects
[elf_nsect
].data
= data
;
1226 elf_sects
[elf_nsect
].len
= datalen
;
1227 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1230 fwriteint32_t((int32_t)name
, elffp
);
1231 fwriteint32_t((int32_t)type
, elffp
);
1232 fwriteint32_t((int32_t)flags
, elffp
);
1233 fwriteint32_t(0L, elffp
); /* no address, ever, in object files */
1234 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1235 fwriteint32_t(datalen
, elffp
);
1237 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1238 fwriteint32_t((int32_t)link
, elffp
);
1239 fwriteint32_t((int32_t)info
, elffp
);
1240 fwriteint32_t((int32_t)align
, elffp
);
1241 fwriteint32_t((int32_t)eltsize
, elffp
);
1244 static void elf_write_sections(void)
1247 for (i
= 0; i
< elf_nsect
; i
++)
1248 if (elf_sects
[i
].data
) {
1249 int32_t len
= elf_sects
[i
].len
;
1250 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1251 int32_t align
= reallen
- len
;
1252 if (elf_sects
[i
].is_saa
)
1253 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1255 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1256 fwrite(align_str
, align
, 1, elffp
);
1260 static void elf_sect_write(struct Section
*sect
,
1261 const uint8_t *data
, uint32_t len
)
1263 saa_wbytes(sect
->data
, data
, len
);
1267 static int32_t elf_segbase(int32_t segment
)
1272 static int elf_directive(char *directive
, char *value
, int pass
)
1278 if (!strcmp(directive
, "osabi")) {
1280 return 1; /* ignore in pass 2 */
1282 n
= readnum(value
, &err
);
1284 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1287 if (n
< 0 || n
> 255) {
1288 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1294 if ((p
= strchr(value
,',')) == NULL
)
1297 n
= readnum(p
+1, &err
);
1298 if (err
|| n
< 0 || n
> 255) {
1299 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1310 static void elf_filename(char *inname
, char *outname
, efunc error
)
1312 strcpy(elf_module
, inname
);
1313 standard_extension(inname
, outname
, ".o", error
);
1316 static const char *elf_stdmac
[] = {
1317 "%define __SECT__ [section .text]",
1318 "%macro __NASM_CDecl__ 1",
1321 "%macro osabi 1+.nolist",
1326 static int elf_set_info(enum geninfo type
, char **val
)
1333 static struct dfmt df_stabs
= {
1334 "ELF32 (i386) stabs debug format for Linux",
1345 struct dfmt
*elf32_debugs_arr
[2] = { &df_stabs
, NULL
};
1347 struct ofmt of_elf32
= {
1348 "ELF32 (i386) object files (e.g. Linux)",
1365 struct ofmt of_elf
= {
1366 "ELF (short name for ELF32) ",
1382 /* again, the stabs debugging stuff (code) */
1384 void stabs32_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1392 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1396 if (!stabs_filename
) {
1397 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1398 strcpy(stabs_filename
, filename
);
1400 if (strcmp(stabs_filename
, filename
)) {
1401 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1402 in fact, this leak comes in quite handy to maintain a list of files
1403 encountered so far in the symbol lines... */
1405 /* why not nasm_free(stabs_filename); we're done with the old one */
1407 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1408 strcpy(stabs_filename
, filename
);
1412 currentline
= linenumber
;
1415 void stabs32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1425 void stabs32_directive(const char *directive
, const char *params
)
1431 void stabs32_typevalue(int32_t type
)
1433 int32_t stype
, ssize
;
1434 switch (TYM_TYPE(type
)) {
1473 stype
= STT_SECTION
;
1488 if (stype
== STT_OBJECT
&& !lastsym
->type
) {
1489 lastsym
->size
= ssize
;
1490 lastsym
->type
= stype
;
1494 void stabs32_output(int type
, void *param
)
1496 struct symlininfo
*s
;
1497 struct linelist
*el
;
1498 if (type
== TY_STABSSYMLIN
) {
1499 if (stabs_immcall
) {
1500 s
= (struct symlininfo
*)param
;
1501 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1502 return; /* we are only interested in the text stuff */
1504 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1505 el
->info
.offset
= s
->offset
;
1506 el
->info
.section
= s
->section
;
1507 el
->info
.name
= s
->name
;
1508 el
->line
= currentline
;
1509 el
->filename
= stabs_filename
;
1512 stabslines
->last
->next
= el
;
1513 stabslines
->last
= el
;
1516 stabslines
->last
= el
;
1523 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1525 WRITELONG(p,n_strx); \
1526 WRITECHAR(p,n_type); \
1527 WRITECHAR(p,n_other); \
1528 WRITESHORT(p,n_desc); \
1529 WRITELONG(p,n_value); \
1532 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1534 void stabs32_generate(void)
1536 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1537 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1541 struct linelist
*ptr
;
1545 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1546 for (i
= 0; i
< numlinestabs
; i
++)
1550 if (numfiles
== 0) {
1551 allfiles
[0] = ptr
->filename
;
1554 for (i
= 0; i
< numfiles
; i
++) {
1555 if (!strcmp(allfiles
[i
], ptr
->filename
))
1558 if (i
>= numfiles
) {
1559 allfiles
[i
] = ptr
->filename
;
1566 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1567 for (i
= 0; i
< numfiles
; i
++) {
1568 fileidx
[i
] = strsize
;
1569 strsize
+= strlen(allfiles
[i
]) + 1;
1572 for (i
= 0; i
< numfiles
; i
++) {
1573 if (!strcmp(allfiles
[i
], elf_module
)) {
1579 /* worst case size of the stab buffer would be:
1580 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1583 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1584 sizeof(struct stabentry
));
1586 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1588 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1591 for (i
= 0; i
< numfiles
; i
++) {
1592 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1596 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1603 /* this is the first stab, its strx points to the filename of the
1604 the source-file, the n_desc field should be set to the number
1607 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1609 /* this is the stab for the main source file */
1610 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1612 /* relocation table entry */
1614 /* Since the symbol table has two entries before */
1615 /* the section symbols, the index in the info.section */
1616 /* member must be adjusted by adding 2 */
1618 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1619 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1622 currfile
= mainfileindex
;
1626 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1627 /* oops file has changed... */
1628 for (i
= 0; i
< numfiles
; i
++)
1629 if (!strcmp(allfiles
[i
], ptr
->filename
))
1632 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1636 /* relocation table entry */
1637 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1638 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1641 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1644 /* relocation table entry */
1646 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1647 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1653 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1655 nasm_free(allfiles
);
1658 stablen
= (sptr
- sbuf
);
1659 stabrellen
= (rptr
- rbuf
);
1665 void stabs32_cleanup(void)
1667 struct linelist
*ptr
, *del
;
1679 nasm_free(stabrelbuf
);
1681 nasm_free(stabstrbuf
);