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
)
233 if (of_elf
.current_dfmt
!= &null_debug_form
)
234 of_elf32
.current_dfmt
= of_elf
.current_dfmt
;
238 (void)ldef
; /* placate optimisers */
240 nsects
= sectlen
= 0;
241 syms
= saa_init((int32_t)sizeof(struct Symbol
));
242 nlocals
= nglobs
= 0;
245 saa_wbytes(strs
, "\0", 1L);
246 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
247 strslen
= 2 + strlen(elf_module
);
249 shstrtablen
= shstrtabsize
= 0;;
250 add_sectname("", "");
254 elf_gotpc_sect
= seg_alloc();
255 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
257 elf_gotoff_sect
= seg_alloc();
258 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
260 elf_got_sect
= seg_alloc();
261 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
263 elf_plt_sect
= seg_alloc();
264 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
266 elf_sym_sect
= seg_alloc();
267 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
270 def_seg
= seg_alloc();
273 static void elf_cleanup(int debuginfo
)
282 for (i
= 0; i
< nsects
; i
++) {
283 if (sects
[i
]->type
!= SHT_NOBITS
)
284 saa_free(sects
[i
]->data
);
286 saa_free(sects
[i
]->rel
);
287 while (sects
[i
]->head
) {
289 sects
[i
]->head
= sects
[i
]->head
->next
;
297 if (of_elf32
.current_dfmt
) {
298 of_elf32
.current_dfmt
->cleanup();
302 static void add_sectname(char *firsthalf
, char *secondhalf
)
304 int len
= strlen(firsthalf
) + strlen(secondhalf
);
305 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
306 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
307 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
308 strcat(shstrtab
+ shstrtablen
, secondhalf
);
309 shstrtablen
+= len
+ 1;
312 static int elf_make_section(char *name
, int type
, int flags
, int align
)
316 s
= nasm_malloc(sizeof(*s
));
318 if (type
!= SHT_NOBITS
)
319 s
->data
= saa_init(1L);
322 s
->len
= s
->size
= 0;
324 if (!strcmp(name
, ".text"))
327 s
->index
= seg_alloc();
328 add_sectname("", name
);
329 s
->name
= nasm_malloc(1 + strlen(name
));
330 strcpy(s
->name
, name
);
336 if (nsects
>= sectlen
)
338 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
344 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
347 unsigned flags_and
, flags_or
;
351 * Default is 32 bits.
359 while (*p
&& !isspace(*p
))
363 flags_and
= flags_or
= type
= align
= 0;
365 while (*p
&& isspace(*p
))
369 while (*p
&& !isspace(*p
))
373 while (*p
&& isspace(*p
))
376 if (!nasm_strnicmp(q
, "align=", 6)) {
380 if ((align
- 1) & align
) { /* means it's not a power of two */
381 error(ERR_NONFATAL
, "section alignment %d is not"
382 " a power of two", align
);
385 } else if (!nasm_stricmp(q
, "alloc")) {
386 flags_and
|= SHF_ALLOC
;
387 flags_or
|= SHF_ALLOC
;
388 } else if (!nasm_stricmp(q
, "noalloc")) {
389 flags_and
|= SHF_ALLOC
;
390 flags_or
&= ~SHF_ALLOC
;
391 } else if (!nasm_stricmp(q
, "exec")) {
392 flags_and
|= SHF_EXECINSTR
;
393 flags_or
|= SHF_EXECINSTR
;
394 } else if (!nasm_stricmp(q
, "noexec")) {
395 flags_and
|= SHF_EXECINSTR
;
396 flags_or
&= ~SHF_EXECINSTR
;
397 } else if (!nasm_stricmp(q
, "write")) {
398 flags_and
|= SHF_WRITE
;
399 flags_or
|= SHF_WRITE
;
400 } else if (!nasm_stricmp(q
, "nowrite")) {
401 flags_and
|= SHF_WRITE
;
402 flags_or
&= ~SHF_WRITE
;
403 } else if (!nasm_stricmp(q
, "progbits")) {
405 } else if (!nasm_stricmp(q
, "nobits")) {
410 if (!strcmp(name
, ".comment") ||
411 !strcmp(name
, ".shstrtab") ||
412 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
413 error(ERR_NONFATAL
, "attempt to redefine reserved section"
418 for (i
= 0; i
< nsects
; i
++)
419 if (!strcmp(name
, sects
[i
]->name
))
422 if (!strcmp(name
, ".text"))
423 i
= elf_make_section(name
, SHT_PROGBITS
,
424 SHF_ALLOC
| SHF_EXECINSTR
, 16);
425 else if (!strcmp(name
, ".rodata"))
426 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
427 else if (!strcmp(name
, ".data"))
428 i
= elf_make_section(name
, SHT_PROGBITS
,
429 SHF_ALLOC
| SHF_WRITE
, 4);
430 else if (!strcmp(name
, ".bss"))
431 i
= elf_make_section(name
, SHT_NOBITS
,
432 SHF_ALLOC
| SHF_WRITE
, 4);
434 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
436 sects
[i
]->type
= type
;
438 sects
[i
]->align
= align
;
439 sects
[i
]->flags
&= ~flags_and
;
440 sects
[i
]->flags
|= flags_or
;
441 } else if (pass
== 1) {
442 if ((type
&& sects
[i
]->type
!= type
)
443 || (align
&& sects
[i
]->align
!= align
)
444 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
445 error(ERR_WARNING
, "section attributes ignored on"
446 " redeclaration of section `%s'", name
);
449 return sects
[i
]->index
;
452 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
453 int is_global
, char *special
)
457 bool special_used
= false;
459 #if defined(DEBUG) && DEBUG>2
461 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
462 name
, segment
, offset
, is_global
, special
);
464 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
466 * This is a NASM special symbol. We never allow it into
467 * the ELF symbol table, even if it's a valid one. If it
468 * _isn't_ a valid one, we should barf immediately.
470 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
471 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
472 strcmp(name
, "..sym"))
473 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
477 if (is_global
== 3) {
480 * Fix up a forward-reference symbol size from the first
483 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
484 if (!strcmp((*s
)->name
, name
)) {
485 struct tokenval tokval
;
489 while (*p
&& !isspace(*p
))
491 while (*p
&& isspace(*p
))
495 tokval
.t_type
= TOKEN_INVALID
;
496 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
499 error(ERR_NONFATAL
, "cannot use relocatable"
500 " expression as symbol size");
502 (*s
)->size
= reloc_value(e
);
506 * Remove it from the list of unresolved sizes.
508 nasm_free((*s
)->name
);
512 return; /* it wasn't an important one */
515 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
516 strslen
+= 1 + strlen(name
);
518 lastsym
= sym
= saa_wstruct(syms
);
521 sym
->type
= is_global
? SYM_GLOBAL
: 0;
522 sym
->other
= STV_DEFAULT
;
524 if (segment
== NO_SEG
)
525 sym
->section
= SHN_ABS
;
528 sym
->section
= SHN_UNDEF
;
529 if (nsects
== 0 && segment
== def_seg
) {
531 if (segment
!= elf_section_names(".text", 2, &tempint
))
533 "strange segment conditions in ELF driver");
534 sym
->section
= nsects
;
536 for (i
= 0; i
< nsects
; i
++)
537 if (segment
== sects
[i
]->index
) {
538 sym
->section
= i
+ 1;
544 if (is_global
== 2) {
547 sym
->section
= SHN_COMMON
;
549 * We have a common variable. Check the special text to see
550 * if it's a valid number and power of two; if so, store it
551 * as the alignment for the common variable.
555 sym
->value
= readnum(special
, &err
);
557 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
558 " valid number", special
);
559 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
560 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
561 " power of two", special
);
565 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
567 if (sym
->type
== SYM_GLOBAL
) {
569 * If sym->section == SHN_ABS, then the first line of the
570 * else section would cause a core dump, because its a reference
571 * beyond the end of the section array.
572 * This behaviour is exhibited by this code:
575 * To avoid such a crash, such requests are silently discarded.
576 * This may not be the best solution.
578 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
579 bsym
= raa_write(bsym
, segment
, nglobs
);
580 } else if (sym
->section
!= SHN_ABS
) {
582 * This is a global symbol; so we must add it to the linked
583 * list of global symbols in its section. We'll push it on
584 * the beginning of the list, because it doesn't matter
585 * much which end we put it on and it's easier like this.
587 * In addition, we check the special text for symbol
588 * type and size information.
590 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
591 sects
[sym
->section
- 1]->gsyms
= sym
;
594 int n
= strcspn(special
, " \t");
596 if (!nasm_strnicmp(special
, "function", n
))
597 sym
->type
|= STT_FUNC
;
598 else if (!nasm_strnicmp(special
, "data", n
) ||
599 !nasm_strnicmp(special
, "object", n
))
600 sym
->type
|= STT_OBJECT
;
601 else if (!nasm_strnicmp(special
, "notype", n
))
602 sym
->type
|= STT_NOTYPE
;
604 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
608 while (isspace(*special
))
611 n
= strcspn(special
, " \t");
612 if (!nasm_strnicmp(special
, "default", n
))
613 sym
->other
= STV_DEFAULT
;
614 else if (!nasm_strnicmp(special
, "internal", n
))
615 sym
->other
= STV_INTERNAL
;
616 else if (!nasm_strnicmp(special
, "hidden", n
))
617 sym
->other
= STV_HIDDEN
;
618 else if (!nasm_strnicmp(special
, "protected", n
))
619 sym
->other
= STV_PROTECTED
;
626 struct tokenval tokval
;
629 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
631 while (special
[n
] && isspace(special
[n
]))
634 * We have a size expression; attempt to
638 stdscan_bufptr
= special
+ n
;
639 tokval
.t_type
= TOKEN_INVALID
;
640 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
645 sym
->name
= nasm_strdup(name
);
648 error(ERR_NONFATAL
, "cannot use relocatable"
649 " expression as symbol size");
651 sym
->size
= reloc_value(e
);
653 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
658 sym
->globnum
= nglobs
;
663 if (special
&& !special_used
)
664 error(ERR_NONFATAL
, "no special symbol features supported here");
667 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
671 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
672 sect
->tail
= &r
->next
;
675 r
->address
= sect
->len
;
676 if (segment
== NO_SEG
)
681 for (i
= 0; i
< nsects
; i
++)
682 if (segment
== sects
[i
]->index
)
685 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
693 * This routine deals with ..got and ..sym relocations: the more
694 * complicated kinds. In shared-library writing, some relocations
695 * with respect to global symbols must refer to the precise symbol
696 * rather than referring to an offset from the base of the section
697 * _containing_ the symbol. Such relocations call to this routine,
698 * which searches the symbol list for the symbol in question.
700 * R_386_GOT32 references require the _exact_ symbol address to be
701 * used; R_386_32 references can be at an offset from the symbol.
702 * The boolean argument `exact' tells us this.
704 * Return value is the adjusted value of `addr', having become an
705 * offset from the symbol rather than the section. Should always be
706 * zero when returning from an exact call.
708 * Limitation: if you define two symbols at the same place,
709 * confusion will occur.
711 * Inefficiency: we search, currently, using a linked list which
712 * isn't even necessarily sorted.
714 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
715 int32_t segment
, int32_t offset
,
716 int type
, bool exact
)
720 struct Symbol
*sym
, *sm
;
724 * First look up the segment/offset pair and find a global
725 * symbol corresponding to it. If it's not one of our segments,
726 * then it must be an external symbol, in which case we're fine
727 * doing a normal elf_add_reloc after first sanity-checking
728 * that the offset from the symbol is zero.
731 for (i
= 0; i
< nsects
; i
++)
732 if (segment
== sects
[i
]->index
) {
737 if (exact
&& offset
!= 0)
738 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
739 " for this reference");
741 elf_add_reloc(sect
, segment
, type
);
747 * Find a symbol pointing _exactly_ at this one.
749 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
750 if (sym
->value
== offset
)
754 * Find the nearest symbol below this one.
757 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
758 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
762 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
763 " for this reference");
767 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
768 sect
->tail
= &r
->next
;
771 r
->address
= sect
->len
;
772 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
777 return offset
- sym
->value
;
780 static void elf_out(int32_t segto
, const void *data
,
781 enum out_type type
, uint64_t size
,
782 int32_t segment
, int32_t wrt
)
786 uint8_t mydata
[4], *p
;
788 static struct symlininfo sinfo
;
791 * handle absolute-assembly (structure definitions)
793 if (segto
== NO_SEG
) {
794 if (type
!= OUT_RESERVE
)
795 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
801 for (i
= 0; i
< nsects
; i
++)
802 if (segto
== sects
[i
]->index
) {
807 int tempint
; /* ignored */
808 if (segto
!= elf_section_names(".text", 2, &tempint
))
809 error(ERR_PANIC
, "strange segment conditions in ELF driver");
811 s
= sects
[nsects
- 1];
816 /* again some stabs debugging stuff */
817 if (of_elf32
.current_dfmt
) {
818 sinfo
.offset
= s
->len
;
820 sinfo
.name
= s
->name
;
821 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
823 /* end of debugging stuff */
825 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
826 error(ERR_WARNING
, "attempt to initialize memory in"
827 " BSS section `%s': ignored", s
->name
);
828 if (type
== OUT_REL2ADR
)
830 else if (type
== OUT_REL4ADR
)
836 if (type
== OUT_RESERVE
) {
837 if (s
->type
== SHT_PROGBITS
) {
838 error(ERR_WARNING
, "uninitialized space declared in"
839 " non-BSS section `%s': zeroing", s
->name
);
840 elf_sect_write(s
, NULL
, size
);
843 } else if (type
== OUT_RAWDATA
) {
844 if (segment
!= NO_SEG
)
845 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
846 elf_sect_write(s
, data
, size
);
847 } else if (type
== OUT_ADDRESS
) {
849 addr
= *(int64_t *)data
;
850 if (segment
!= NO_SEG
) {
852 error(ERR_NONFATAL
, "ELF format does not support"
853 " segment base references");
858 elf_add_reloc(s
, segment
, R_386_16
);
860 elf_add_reloc(s
, segment
, R_386_32
);
862 } else if (wrt
== elf_gotpc_sect
+ 1) {
864 * The user will supply GOT relative to $$. ELF
865 * will let us have GOT relative to $. So we
866 * need to fix up the data item by $-$$.
869 elf_add_reloc(s
, segment
, R_386_GOTPC
);
870 } else if (wrt
== elf_gotoff_sect
+ 1) {
871 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
872 } else if (wrt
== elf_got_sect
+ 1) {
873 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
875 } else if (wrt
== elf_sym_sect
+ 1) {
878 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
881 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
884 } else if (wrt
== elf_plt_sect
+ 1) {
885 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
886 "relative PLT references");
888 error(ERR_NONFATAL
, "ELF format does not support this"
890 wrt
= NO_SEG
; /* we can at least _try_ to continue */
896 error(ERR_WARNING
| ERR_WARN_GNUELF
,
897 "16-bit relocations in ELF is a GNU extension");
900 if (size
!= 4 && segment
!= NO_SEG
) {
902 "Unsupported non-32-bit ELF relocation");
906 elf_sect_write(s
, mydata
, size
);
907 } else if (type
== OUT_REL2ADR
) {
908 if (segment
== segto
)
909 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
910 if (segment
!= NO_SEG
&& segment
% 2) {
911 error(ERR_NONFATAL
, "ELF format does not support"
912 " segment base references");
915 error(ERR_WARNING
| ERR_WARN_GNUELF
,
916 "16-bit relocations in ELF is a GNU extension");
917 elf_add_reloc(s
, segment
, R_386_PC16
);
920 "Unsupported non-32-bit ELF relocation");
924 WRITESHORT(p
, *(int64_t *)data
- size
);
925 elf_sect_write(s
, mydata
, 2L);
926 } else if (type
== OUT_REL4ADR
) {
927 if (segment
== segto
)
928 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
929 if (segment
!= NO_SEG
&& segment
% 2) {
930 error(ERR_NONFATAL
, "ELF format does not support"
931 " segment base references");
934 elf_add_reloc(s
, segment
, R_386_PC32
);
935 } else if (wrt
== elf_plt_sect
+ 1) {
936 elf_add_reloc(s
, segment
, R_386_PLT32
);
937 } else if (wrt
== elf_gotpc_sect
+ 1 ||
938 wrt
== elf_gotoff_sect
+ 1 ||
939 wrt
== elf_got_sect
+ 1) {
940 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
941 "relative GOT references");
943 error(ERR_NONFATAL
, "ELF format does not support this"
945 wrt
= NO_SEG
; /* we can at least _try_ to continue */
949 WRITELONG(p
, *(int64_t *)data
- size
);
950 elf_sect_write(s
, mydata
, 4L);
954 static void elf_write(void)
956 int nsections
, align
;
964 int32_t symtablen
, symtablocal
;
967 * Work out how many sections we will have. We have SHN_UNDEF,
968 * then the flexible user sections, then the four fixed
969 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
970 * then optionally relocation sections for the user sections.
972 if (of_elf32
.current_dfmt
== &df_stabs
)
975 nsections
= 5; /* SHN_UNDEF and the fixed ones */
977 add_sectname("", ".comment");
978 add_sectname("", ".shstrtab");
979 add_sectname("", ".symtab");
980 add_sectname("", ".strtab");
981 for (i
= 0; i
< nsects
; i
++) {
982 nsections
++; /* for the section itself */
983 if (sects
[i
]->head
) {
984 nsections
++; /* for its relocations */
985 add_sectname(".rel", sects
[i
]->name
);
989 if (of_elf32
.current_dfmt
== &df_stabs
) {
990 /* in case the debug information is wanted, just add these three sections... */
991 add_sectname("", ".stab");
992 add_sectname("", ".stabstr");
993 add_sectname(".rel", ".stab");
1001 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1004 * Output the ELF header.
1006 fwrite("\177ELF\1\1\1", 7, 1, elffp
);
1007 fputc(elf_osabi
, elffp
);
1008 fputc(elf_abiver
, elffp
);
1009 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp
);
1010 fwriteint16_t(1, elffp
); /* ET_REL relocatable file */
1011 fwriteint16_t(3, elffp
); /* EM_386 processor ID */
1012 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1013 fwriteint32_t(0L, elffp
); /* no entry point */
1014 fwriteint32_t(0L, elffp
); /* no program header table */
1015 fwriteint32_t(0x40L
, elffp
); /* section headers straight after
1016 * ELF header plus alignment */
1017 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1018 fwriteint16_t(0x34, elffp
); /* size of ELF header */
1019 fwriteint16_t(0, elffp
); /* no program header table, again */
1020 fwriteint16_t(0, elffp
); /* still no program header table */
1021 fwriteint16_t(0x28, elffp
); /* size of section header */
1022 fwriteint16_t(nsections
, elffp
); /* number of sections */
1023 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1024 * section header table */
1025 fwriteint32_t(0L, elffp
); /* align to 0x40 bytes */
1026 fwriteint32_t(0L, elffp
);
1027 fwriteint32_t(0L, elffp
);
1030 * Build the symbol table and relocation tables.
1032 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1033 for (i
= 0; i
< nsects
; i
++)
1035 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1039 * Now output the section header table.
1042 elf_foffs
= 0x40 + 0x28 * nsections
;
1043 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1046 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1048 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1049 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1051 for (i
= 0; i
< nsects
; i
++) {
1052 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1053 (sects
[i
]->type
== SHT_PROGBITS
?
1054 sects
[i
]->data
: NULL
), true,
1055 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1057 scount
++; /* dito */
1059 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1060 scount
++; /* dito */
1062 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1063 scount
++; /* dito */
1065 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 16); /* .symtab */
1066 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1068 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1069 for (i
= 0; i
< nsects
; i
++)
1070 if (sects
[i
]->head
) {
1072 elf_section_header(p
- shstrtab
, 9, 0, sects
[i
]->rel
, true,
1073 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 8);
1075 if (of_elf32
.current_dfmt
== &df_stabs
) {
1076 /* for debugging information, create the last three sections
1077 which are the .stab , .stabstr and .rel.stab sections respectively */
1079 /* this function call creates the stab sections in memory */
1082 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1084 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1085 nsections
- 2, 0, 4, 12);
1088 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1089 stabstrlen
, 0, 0, 4, 0);
1092 /* link -> symtable info -> section to refer to */
1093 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1094 stabrellen
, symtabsection
, nsections
- 3, 4,
1098 fwrite(align_str
, align
, 1, elffp
);
1101 * Now output the sections.
1103 elf_write_sections();
1105 nasm_free(elf_sects
);
1109 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1111 struct SAA
*s
= saa_init(1L);
1113 uint8_t entry
[16], *p
;
1119 * First, an all-zeros entry, required by the ELF spec.
1121 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1126 * Next, an entry for the file name.
1129 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1130 WRITELONG(p
, 0); /* no value */
1131 WRITELONG(p
, 0); /* no size either */
1132 WRITESHORT(p
, STT_FILE
); /* type FILE */
1133 WRITESHORT(p
, SHN_ABS
);
1134 saa_wbytes(s
, entry
, 16L);
1139 * Now some standard symbols defining the segments, for relocation
1142 for (i
= 1; i
<= nsects
; i
++) {
1144 WRITELONG(p
, 0); /* no symbol name */
1145 WRITELONG(p
, 0); /* offset zero */
1146 WRITELONG(p
, 0); /* size zero */
1147 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1148 WRITESHORT(p
, i
); /* section id */
1149 saa_wbytes(s
, entry
, 16L);
1155 * Now the other local symbols.
1158 while ((sym
= saa_rstruct(syms
))) {
1159 if (sym
->type
& SYM_GLOBAL
)
1162 WRITELONG(p
, sym
->strpos
);
1163 WRITELONG(p
, sym
->value
);
1164 WRITELONG(p
, sym
->size
);
1165 WRITECHAR(p
, sym
->type
); /* type and binding */
1166 WRITECHAR(p
, sym
->other
); /* visibility */
1167 WRITESHORT(p
, sym
->section
);
1168 saa_wbytes(s
, entry
, 16L);
1174 * Now the global symbols.
1177 while ((sym
= saa_rstruct(syms
))) {
1178 if (!(sym
->type
& SYM_GLOBAL
))
1181 WRITELONG(p
, sym
->strpos
);
1182 WRITELONG(p
, sym
->value
);
1183 WRITELONG(p
, sym
->size
);
1184 WRITECHAR(p
, sym
->type
); /* type and binding */
1185 WRITECHAR(p
, sym
->other
); /* visibility */
1186 WRITESHORT(p
, sym
->section
);
1187 saa_wbytes(s
, entry
, 16L);
1194 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1197 uint8_t *p
, entry
[8];
1206 int32_t sym
= r
->symbol
;
1208 if (sym
>= GLOBAL_TEMP_BASE
)
1209 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1212 WRITELONG(p
, r
->address
);
1213 WRITELONG(p
, (sym
<< 8) + r
->type
);
1214 saa_wbytes(s
, entry
, 8L);
1223 static void elf_section_header(int name
, int type
, int flags
,
1224 void *data
, bool is_saa
, int32_t datalen
,
1225 int link
, int info
, int align
, int eltsize
)
1227 elf_sects
[elf_nsect
].data
= data
;
1228 elf_sects
[elf_nsect
].len
= datalen
;
1229 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1232 fwriteint32_t((int32_t)name
, elffp
);
1233 fwriteint32_t((int32_t)type
, elffp
);
1234 fwriteint32_t((int32_t)flags
, elffp
);
1235 fwriteint32_t(0L, elffp
); /* no address, ever, in object files */
1236 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1237 fwriteint32_t(datalen
, elffp
);
1239 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1240 fwriteint32_t((int32_t)link
, elffp
);
1241 fwriteint32_t((int32_t)info
, elffp
);
1242 fwriteint32_t((int32_t)align
, elffp
);
1243 fwriteint32_t((int32_t)eltsize
, elffp
);
1246 static void elf_write_sections(void)
1249 for (i
= 0; i
< elf_nsect
; i
++)
1250 if (elf_sects
[i
].data
) {
1251 int32_t len
= elf_sects
[i
].len
;
1252 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1253 int32_t align
= reallen
- len
;
1254 if (elf_sects
[i
].is_saa
)
1255 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1257 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1258 fwrite(align_str
, align
, 1, elffp
);
1262 static void elf_sect_write(struct Section
*sect
,
1263 const uint8_t *data
, uint32_t len
)
1265 saa_wbytes(sect
->data
, data
, len
);
1269 static int32_t elf_segbase(int32_t segment
)
1274 static int elf_directive(char *directive
, char *value
, int pass
)
1280 if (!strcmp(directive
, "osabi")) {
1282 return 1; /* ignore in pass 2 */
1284 n
= readnum(value
, &err
);
1286 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1289 if (n
< 0 || n
> 255) {
1290 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1296 if ((p
= strchr(value
,',')) == NULL
)
1299 n
= readnum(p
+1, &err
);
1300 if (err
|| n
< 0 || n
> 255) {
1301 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1312 static void elf_filename(char *inname
, char *outname
, efunc error
)
1314 strcpy(elf_module
, inname
);
1315 standard_extension(inname
, outname
, ".o", error
);
1318 static const char *elf_stdmac
[] = {
1319 "%define __SECT__ [section .text]",
1320 "%macro __NASM_CDecl__ 1",
1323 "%macro osabi 1+.nolist",
1328 static int elf_set_info(enum geninfo type
, char **val
)
1335 static struct dfmt df_stabs
= {
1336 "ELF32 (i386) stabs debug format for Linux",
1347 struct dfmt
*elf32_debugs_arr
[2] = { &df_stabs
, NULL
};
1349 struct ofmt of_elf32
= {
1350 "ELF32 (i386) object files (e.g. Linux)",
1367 struct ofmt of_elf
= {
1368 "ELF (short name for ELF32) ",
1384 /* again, the stabs debugging stuff (code) */
1386 void stabs32_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1394 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1398 if (!stabs_filename
) {
1399 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1400 strcpy(stabs_filename
, filename
);
1402 if (strcmp(stabs_filename
, filename
)) {
1403 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1404 in fact, this leak comes in quite handy to maintain a list of files
1405 encountered so far in the symbol lines... */
1407 /* why not nasm_free(stabs_filename); we're done with the old one */
1409 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1410 strcpy(stabs_filename
, filename
);
1414 currentline
= linenumber
;
1417 void stabs32_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1427 void stabs32_directive(const char *directive
, const char *params
)
1433 void stabs32_typevalue(int32_t type
)
1435 int32_t stype
, ssize
;
1436 switch (TYM_TYPE(type
)) {
1475 stype
= STT_SECTION
;
1490 if (stype
== STT_OBJECT
&& !lastsym
->type
) {
1491 lastsym
->size
= ssize
;
1492 lastsym
->type
= stype
;
1496 void stabs32_output(int type
, void *param
)
1498 struct symlininfo
*s
;
1499 struct linelist
*el
;
1500 if (type
== TY_STABSSYMLIN
) {
1501 if (stabs_immcall
) {
1502 s
= (struct symlininfo
*)param
;
1503 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1504 return; /* we are only interested in the text stuff */
1506 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1507 el
->info
.offset
= s
->offset
;
1508 el
->info
.section
= s
->section
;
1509 el
->info
.name
= s
->name
;
1510 el
->line
= currentline
;
1511 el
->filename
= stabs_filename
;
1514 stabslines
->last
->next
= el
;
1515 stabslines
->last
= el
;
1518 stabslines
->last
= el
;
1525 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1527 WRITELONG(p,n_strx); \
1528 WRITECHAR(p,n_type); \
1529 WRITECHAR(p,n_other); \
1530 WRITESHORT(p,n_desc); \
1531 WRITELONG(p,n_value); \
1534 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1536 void stabs32_generate(void)
1538 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1539 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1543 struct linelist
*ptr
;
1547 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1548 for (i
= 0; i
< numlinestabs
; i
++)
1552 if (numfiles
== 0) {
1553 allfiles
[0] = ptr
->filename
;
1556 for (i
= 0; i
< numfiles
; i
++) {
1557 if (!strcmp(allfiles
[i
], ptr
->filename
))
1560 if (i
>= numfiles
) {
1561 allfiles
[i
] = ptr
->filename
;
1568 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1569 for (i
= 0; i
< numfiles
; i
++) {
1570 fileidx
[i
] = strsize
;
1571 strsize
+= strlen(allfiles
[i
]) + 1;
1574 for (i
= 0; i
< numfiles
; i
++) {
1575 if (!strcmp(allfiles
[i
], elf_module
)) {
1581 /* worst case size of the stab buffer would be:
1582 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1585 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1586 sizeof(struct stabentry
));
1588 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1590 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1593 for (i
= 0; i
< numfiles
; i
++) {
1594 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1598 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1605 /* this is the first stab, its strx points to the filename of the
1606 the source-file, the n_desc field should be set to the number
1609 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1611 /* this is the stab for the main source file */
1612 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1614 /* relocation table entry */
1616 /* Since the symbol table has two entries before */
1617 /* the section symbols, the index in the info.section */
1618 /* member must be adjusted by adding 2 */
1620 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1621 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1624 currfile
= mainfileindex
;
1628 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1629 /* oops file has changed... */
1630 for (i
= 0; i
< numfiles
; i
++)
1631 if (!strcmp(allfiles
[i
], ptr
->filename
))
1634 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1638 /* relocation table entry */
1639 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1640 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1643 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1646 /* relocation table entry */
1648 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1649 WRITELONG(rptr
, ((ptr
->info
.section
+ 2) << 8) | R_386_32
);
1655 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1657 nasm_free(allfiles
);
1660 stablen
= (sptr
- sbuf
);
1661 stabrellen
= (rptr
- rbuf
);
1667 void stabs32_cleanup(void)
1669 struct linelist
*ptr
, *del
;
1681 nasm_free(stabrelbuf
);
1683 nasm_free(stabstrbuf
);