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.
27 R_386_32
= 1, /* ordinary absolute relocation */
28 R_386_PC32
= 2, /* PC-relative relocation */
29 R_386_GOT32
= 3, /* an offset into GOT */
30 R_386_PLT32
= 4, /* a PC-relative offset into PLT */
31 R_386_COPY
= 5, /* ??? */
32 R_386_GLOB_DAT
= 6, /* ??? */
33 R_386_JUMP_SLOT
= 7, /* ??? */
34 R_386_RELATIVE
= 8, /* ??? */
35 R_386_GOTOFF
= 9, /* an offset from GOT base */
36 R_386_GOTPC
= 10, /* a PC-relative offset _to_ GOT */
37 /* These are GNU extensions, but useful */
38 R_386_16
= 20, /* A 16-bit absolute relocation */
39 R_386_PC16
= 21, /* A 16-bit PC-relative relocation */
40 R_386_8
= 22, /* An 8-bit absolute relocation */
41 R_386_PC8
= 23 /* An 8-bit PC-relative relocation */
46 int32_t address
; /* relative to _start_ of section */
47 int32_t symbol
; /* ELF symbol info thingy */
48 int type
; /* type of relocation */
52 int32_t strpos
; /* string table position of name */
53 int32_t section
; /* section ID of the symbol */
54 int type
; /* symbol type */
55 int other
; /* symbol visibility */
56 int32_t value
; /* address, or COMMON variable align */
57 int32_t size
; /* size of symbol */
58 int32_t globnum
; /* symbol table offset if global */
59 struct Symbol
*next
; /* list of globals in each section */
60 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
61 char *name
; /* used temporarily if in above list */
64 #define SHT_PROGBITS 1
69 #define SHF_EXECINSTR 4
73 uint32_t len
, size
, nrelocs
;
75 int type
; /* SHT_PROGBITS or SHT_NOBITS */
76 int align
; /* alignment: power of two */
77 uint32_t flags
; /* section flags */
81 struct Reloc
*head
, **tail
;
82 struct Symbol
*gsyms
; /* global symbols in section */
86 static struct Section
**sects
;
87 static int nsects
, sectlen
;
89 #define SHSTR_DELTA 256
90 static char *shstrtab
;
91 static int shstrtablen
, shstrtabsize
;
93 static struct SAA
*syms
;
94 static uint32_t nlocals
, nglobs
;
96 static int32_t def_seg
;
98 static struct RAA
*bsym
;
100 static struct SAA
*strs
;
101 static uint32_t strslen
;
105 static evalfunc evaluate
;
107 static struct Symbol
*fwds
;
109 static char elf_module
[FILENAME_MAX
];
111 extern struct ofmt of_elf32
;
112 extern struct ofmt of_elf
;
114 #define SHN_ABS 0xFFF1
115 #define SHN_COMMON 0xFFF2
118 #define SYM_SECTION 0x04
119 #define SYM_GLOBAL 0x10
120 #define SYM_NOTYPE 0x00
121 #define SYM_DATA 0x01
122 #define SYM_FUNCTION 0x02
124 #define STV_DEFAULT 0
125 #define STV_INTERNAL 1
127 #define STV_PROTECTED 3
129 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
131 #define SEG_ALIGN 16 /* alignment of sections in file */
132 #define SEG_ALIGN_1 (SEG_ALIGN-1)
134 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
136 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
137 static struct ELF_SECTDATA
{
142 static int elf_nsect
;
143 static int32_t elf_foffs
;
145 static void elf_write(void);
146 static void elf_sect_write(struct Section
*, const uint8_t *,
148 static void elf_section_header(int, int, int, void *, int, int32_t, int, int,
150 static void elf_write_sections(void);
151 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
152 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
153 static void add_sectname(char *, char *);
155 /* this stuff is needed for the stabs debugging format */
156 #define N_SO 0x64 /* ID for main source file */
157 #define N_SOL 0x84 /* ID for sub-source file */
161 #define TY_STABSSYMLIN 0x40 /* ouch */
177 int section
; /* section index */
178 char *name
; /* shallow-copied pointer of section name */
182 struct symlininfo info
;
185 struct linelist
*next
;
186 struct linelist
*last
;
189 static struct linelist
*stabslines
= 0;
190 static int stabs_immcall
= 0;
191 static int currentline
= 0;
192 static int numlinestabs
= 0;
193 static char *stabs_filename
= 0;
194 static int symtabsection
;
195 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
196 static int stablen
, stabstrlen
, stabrellen
;
198 static struct dfmt df_stabs
;
200 void stabs32_init(struct ofmt
*, void *, FILE *, efunc
);
201 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
202 void stabs32_deflabel(char *, int32_t, int32_t, int, char *);
203 void stabs32_directive(const char *, const char *);
204 void stabs32_typevalue(int32_t);
205 void stabs32_output(int, void *);
206 void stabs32_generate();
207 void stabs32_cleanup();
209 /* end of stabs debugging stuff */
212 * Special section numbers which are used to define ELF special
213 * symbols, which can be used with WRT to provide PIC relocation
216 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
217 static int32_t elf_got_sect
, elf_plt_sect
;
218 static int32_t elf_sym_sect
;
220 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
225 (void)ldef
; /* placate optimisers */
227 nsects
= sectlen
= 0;
228 syms
= saa_init((int32_t)sizeof(struct Symbol
));
229 nlocals
= nglobs
= 0;
232 saa_wbytes(strs
, "\0", 1L);
233 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
234 strslen
= 2 + strlen(elf_module
);
236 shstrtablen
= shstrtabsize
= 0;;
237 add_sectname("", "");
241 elf_gotpc_sect
= seg_alloc();
242 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
244 elf_gotoff_sect
= seg_alloc();
245 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
247 elf_got_sect
= seg_alloc();
248 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
250 elf_plt_sect
= seg_alloc();
251 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
253 elf_sym_sect
= seg_alloc();
254 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
257 def_seg
= seg_alloc();
260 static void elf_cleanup(int debuginfo
)
269 for (i
= 0; i
< nsects
; i
++) {
270 if (sects
[i
]->type
!= SHT_NOBITS
)
271 saa_free(sects
[i
]->data
);
273 saa_free(sects
[i
]->rel
);
274 while (sects
[i
]->head
) {
276 sects
[i
]->head
= sects
[i
]->head
->next
;
284 if (of_elf32
.current_dfmt
) {
285 of_elf32
.current_dfmt
->cleanup();
289 static void add_sectname(char *firsthalf
, char *secondhalf
)
291 int len
= strlen(firsthalf
) + strlen(secondhalf
);
292 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
293 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
294 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
295 strcat(shstrtab
+ shstrtablen
, secondhalf
);
296 shstrtablen
+= len
+ 1;
299 static int elf_make_section(char *name
, int type
, int flags
, int align
)
303 s
= nasm_malloc(sizeof(*s
));
305 if (type
!= SHT_NOBITS
)
306 s
->data
= saa_init(1L);
309 s
->len
= s
->size
= 0;
311 if (!strcmp(name
, ".text"))
314 s
->index
= seg_alloc();
315 add_sectname("", name
);
316 s
->name
= nasm_malloc(1 + strlen(name
));
317 strcpy(s
->name
, name
);
323 if (nsects
>= sectlen
)
325 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
331 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
334 int flags_and
, flags_or
, type
, align
, i
;
337 * Default is 32 bits.
345 while (*p
&& !isspace(*p
))
349 flags_and
= flags_or
= type
= align
= 0;
351 while (*p
&& isspace(*p
))
355 while (*p
&& !isspace(*p
))
359 while (*p
&& isspace(*p
))
362 if (!nasm_strnicmp(q
, "align=", 6)) {
366 if ((align
- 1) & align
) { /* means it's not a power of two */
367 error(ERR_NONFATAL
, "section alignment %d is not"
368 " a power of two", align
);
371 } else if (!nasm_stricmp(q
, "alloc")) {
372 flags_and
|= SHF_ALLOC
;
373 flags_or
|= SHF_ALLOC
;
374 } else if (!nasm_stricmp(q
, "noalloc")) {
375 flags_and
|= SHF_ALLOC
;
376 flags_or
&= ~SHF_ALLOC
;
377 } else if (!nasm_stricmp(q
, "exec")) {
378 flags_and
|= SHF_EXECINSTR
;
379 flags_or
|= SHF_EXECINSTR
;
380 } else if (!nasm_stricmp(q
, "noexec")) {
381 flags_and
|= SHF_EXECINSTR
;
382 flags_or
&= ~SHF_EXECINSTR
;
383 } else if (!nasm_stricmp(q
, "write")) {
384 flags_and
|= SHF_WRITE
;
385 flags_or
|= SHF_WRITE
;
386 } else if (!nasm_stricmp(q
, "nowrite")) {
387 flags_and
|= SHF_WRITE
;
388 flags_or
&= ~SHF_WRITE
;
389 } else if (!nasm_stricmp(q
, "progbits")) {
391 } else if (!nasm_stricmp(q
, "nobits")) {
396 if (!strcmp(name
, ".comment") ||
397 !strcmp(name
, ".shstrtab") ||
398 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
399 error(ERR_NONFATAL
, "attempt to redefine reserved section"
404 for (i
= 0; i
< nsects
; i
++)
405 if (!strcmp(name
, sects
[i
]->name
))
408 if (!strcmp(name
, ".text"))
409 i
= elf_make_section(name
, SHT_PROGBITS
,
410 SHF_ALLOC
| SHF_EXECINSTR
, 16);
411 else if (!strcmp(name
, ".rodata"))
412 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
413 else if (!strcmp(name
, ".data"))
414 i
= elf_make_section(name
, SHT_PROGBITS
,
415 SHF_ALLOC
| SHF_WRITE
, 4);
416 else if (!strcmp(name
, ".bss"))
417 i
= elf_make_section(name
, SHT_NOBITS
,
418 SHF_ALLOC
| SHF_WRITE
, 4);
420 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
422 sects
[i
]->type
= type
;
424 sects
[i
]->align
= align
;
425 sects
[i
]->flags
&= ~flags_and
;
426 sects
[i
]->flags
|= flags_or
;
427 } else if (pass
== 1) {
428 if (type
|| align
|| flags_and
)
429 error(ERR_WARNING
, "section attributes ignored on"
430 " redeclaration of section `%s'", name
);
433 return sects
[i
]->index
;
436 static void elf_deflabel(char *name
, int32_t segment
, int32_t offset
,
437 int is_global
, char *special
)
441 int special_used
= FALSE
;
443 #if defined(DEBUG) && DEBUG>2
445 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
446 name
, segment
, offset
, is_global
, special
);
448 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
450 * This is a NASM special symbol. We never allow it into
451 * the ELF symbol table, even if it's a valid one. If it
452 * _isn't_ a valid one, we should barf immediately.
454 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
455 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
456 strcmp(name
, "..sym"))
457 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
461 if (is_global
== 3) {
464 * Fix up a forward-reference symbol size from the first
467 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
468 if (!strcmp((*s
)->name
, name
)) {
469 struct tokenval tokval
;
473 while (*p
&& !isspace(*p
))
475 while (*p
&& isspace(*p
))
479 tokval
.t_type
= TOKEN_INVALID
;
480 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
483 error(ERR_NONFATAL
, "cannot use relocatable"
484 " expression as symbol size");
486 (*s
)->size
= reloc_value(e
);
490 * Remove it from the list of unresolved sizes.
492 nasm_free((*s
)->name
);
496 return; /* it wasn't an important one */
499 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
500 strslen
+= 1 + strlen(name
);
502 sym
= saa_wstruct(syms
);
505 sym
->type
= is_global
? SYM_GLOBAL
: 0;
506 sym
->other
= STV_DEFAULT
;
508 if (segment
== NO_SEG
)
509 sym
->section
= SHN_ABS
;
512 sym
->section
= SHN_UNDEF
;
513 if (nsects
== 0 && segment
== def_seg
) {
515 if (segment
!= elf_section_names(".text", 2, &tempint
))
517 "strange segment conditions in ELF driver");
518 sym
->section
= nsects
;
520 for (i
= 0; i
< nsects
; i
++)
521 if (segment
== sects
[i
]->index
) {
522 sym
->section
= i
+ 1;
528 if (is_global
== 2) {
531 sym
->section
= SHN_COMMON
;
533 * We have a common variable. Check the special text to see
534 * if it's a valid number and power of two; if so, store it
535 * as the alignment for the common variable.
539 sym
->value
= readnum(special
, &err
);
541 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
542 " valid number", special
);
543 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
544 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
545 " power of two", special
);
549 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
551 if (sym
->type
== SYM_GLOBAL
) {
553 * If sym->section == SHN_ABS, then the first line of the
554 * else section would cause a core dump, because its a reference
555 * beyond the end of the section array.
556 * This behaviour is exhibited by this code:
559 * To avoid such a crash, such requests are silently discarded.
560 * This may not be the best solution.
562 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
563 bsym
= raa_write(bsym
, segment
, nglobs
);
564 } else if (sym
->section
!= SHN_ABS
) {
566 * This is a global symbol; so we must add it to the linked
567 * list of global symbols in its section. We'll push it on
568 * the beginning of the list, because it doesn't matter
569 * much which end we put it on and it's easier like this.
571 * In addition, we check the special text for symbol
572 * type and size information.
574 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
575 sects
[sym
->section
- 1]->gsyms
= sym
;
578 int n
= strcspn(special
, " \t");
580 if (!nasm_strnicmp(special
, "function", n
))
581 sym
->type
|= SYM_FUNCTION
;
582 else if (!nasm_strnicmp(special
, "data", n
) ||
583 !nasm_strnicmp(special
, "object", n
))
584 sym
->type
|= SYM_DATA
;
585 else if (!nasm_strnicmp(special
, "notype", n
))
586 sym
->type
|= SYM_NOTYPE
;
588 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
592 while (isspace(*special
))
595 n
= strcspn(special
, " \t");
596 if (!nasm_strnicmp(special
, "default", n
))
597 sym
->other
= STV_DEFAULT
;
598 else if (!nasm_strnicmp(special
, "internal", n
))
599 sym
->other
= STV_INTERNAL
;
600 else if (!nasm_strnicmp(special
, "hidden", n
))
601 sym
->other
= STV_HIDDEN
;
602 else if (!nasm_strnicmp(special
, "protected", n
))
603 sym
->other
= STV_PROTECTED
;
610 struct tokenval tokval
;
613 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
615 while (special
[n
] && isspace(special
[n
]))
618 * We have a size expression; attempt to
622 stdscan_bufptr
= special
+ n
;
623 tokval
.t_type
= TOKEN_INVALID
;
624 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
629 sym
->name
= nasm_strdup(name
);
632 error(ERR_NONFATAL
, "cannot use relocatable"
633 " expression as symbol size");
635 sym
->size
= reloc_value(e
);
637 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
642 sym
->globnum
= nglobs
;
647 if (special
&& !special_used
)
648 error(ERR_NONFATAL
, "no special symbol features supported here");
651 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
655 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
656 sect
->tail
= &r
->next
;
659 r
->address
= sect
->len
;
660 if (segment
== NO_SEG
)
665 for (i
= 0; i
< nsects
; i
++)
666 if (segment
== sects
[i
]->index
)
669 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
677 * This routine deals with ..got and ..sym relocations: the more
678 * complicated kinds. In shared-library writing, some relocations
679 * with respect to global symbols must refer to the precise symbol
680 * rather than referring to an offset from the base of the section
681 * _containing_ the symbol. Such relocations call to this routine,
682 * which searches the symbol list for the symbol in question.
684 * R_386_GOT32 references require the _exact_ symbol address to be
685 * used; R_386_32 references can be at an offset from the symbol.
686 * The boolean argument `exact' tells us this.
688 * Return value is the adjusted value of `addr', having become an
689 * offset from the symbol rather than the section. Should always be
690 * zero when returning from an exact call.
692 * Limitation: if you define two symbols at the same place,
693 * confusion will occur.
695 * Inefficiency: we search, currently, using a linked list which
696 * isn't even necessarily sorted.
698 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
699 int32_t segment
, int32_t offset
,
704 struct Symbol
*sym
, *sm
;
708 * First look up the segment/offset pair and find a global
709 * symbol corresponding to it. If it's not one of our segments,
710 * then it must be an external symbol, in which case we're fine
711 * doing a normal elf_add_reloc after first sanity-checking
712 * that the offset from the symbol is zero.
715 for (i
= 0; i
< nsects
; i
++)
716 if (segment
== sects
[i
]->index
) {
721 if (exact
&& offset
!= 0)
722 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
723 " for this reference");
725 elf_add_reloc(sect
, segment
, type
);
731 * Find a symbol pointing _exactly_ at this one.
733 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
734 if (sym
->value
== offset
)
738 * Find the nearest symbol below this one.
741 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
742 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
746 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
747 " for this reference");
751 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
752 sect
->tail
= &r
->next
;
755 r
->address
= sect
->len
;
756 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
761 return offset
- sym
->value
;
764 static void elf_out(int32_t segto
, const void *data
, uint32_t type
,
765 int32_t segment
, int32_t wrt
)
768 int32_t realbytes
= type
& OUT_SIZMASK
;
770 uint8_t mydata
[4], *p
;
772 static struct symlininfo sinfo
;
777 * handle absolute-assembly (structure definitions)
779 if (segto
== NO_SEG
) {
780 if (type
!= OUT_RESERVE
)
781 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
787 for (i
= 0; i
< nsects
; i
++)
788 if (segto
== sects
[i
]->index
) {
793 int tempint
; /* ignored */
794 if (segto
!= elf_section_names(".text", 2, &tempint
))
795 error(ERR_PANIC
, "strange segment conditions in ELF driver");
797 s
= sects
[nsects
- 1];
802 /* again some stabs debugging stuff */
803 if (of_elf32
.current_dfmt
) {
804 sinfo
.offset
= s
->len
;
806 sinfo
.name
= s
->name
;
807 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
809 /* end of debugging stuff */
811 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
812 error(ERR_WARNING
, "attempt to initialize memory in"
813 " BSS section `%s': ignored", s
->name
);
814 if (type
== OUT_REL2ADR
)
816 else if (type
== OUT_REL4ADR
)
822 if (type
== OUT_RESERVE
) {
823 if (s
->type
== SHT_PROGBITS
) {
824 error(ERR_WARNING
, "uninitialized space declared in"
825 " non-BSS section `%s': zeroing", s
->name
);
826 elf_sect_write(s
, NULL
, realbytes
);
829 } else if (type
== OUT_RAWDATA
) {
830 if (segment
!= NO_SEG
)
831 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
832 elf_sect_write(s
, data
, realbytes
);
833 } else if (type
== OUT_ADDRESS
) {
835 addr
= *(int32_t *)data
;
836 if (segment
!= NO_SEG
) {
838 error(ERR_NONFATAL
, "ELF format does not support"
839 " segment base references");
842 if (realbytes
== 2) {
844 elf_add_reloc(s
, segment
, R_386_16
);
846 elf_add_reloc(s
, segment
, R_386_32
);
848 } else if (wrt
== elf_gotpc_sect
+ 1) {
850 * The user will supply GOT relative to $$. ELF
851 * will let us have GOT relative to $. So we
852 * need to fix up the data item by $-$$.
855 elf_add_reloc(s
, segment
, R_386_GOTPC
);
856 } else if (wrt
== elf_gotoff_sect
+ 1) {
857 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
858 } else if (wrt
== elf_got_sect
+ 1) {
859 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
861 } else if (wrt
== elf_sym_sect
+ 1) {
862 if (realbytes
== 2) {
864 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
867 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
870 } else if (wrt
== elf_plt_sect
+ 1) {
871 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
872 "relative PLT references");
874 error(ERR_NONFATAL
, "ELF format does not support this"
876 wrt
= NO_SEG
; /* we can at least _try_ to continue */
882 error(ERR_WARNING
| ERR_WARN_GNUELF
,
883 "16-bit relocations in ELF is a GNU extension");
886 if (realbytes
!= 4 && segment
!= NO_SEG
) {
888 "Unsupported non-32-bit ELF relocation");
892 elf_sect_write(s
, mydata
, realbytes
);
893 } else if (type
== OUT_REL2ADR
) {
894 if (segment
== segto
)
895 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
896 if (segment
!= NO_SEG
&& segment
% 2) {
897 error(ERR_NONFATAL
, "ELF format does not support"
898 " segment base references");
901 error(ERR_WARNING
| ERR_WARN_GNUELF
,
902 "16-bit relocations in ELF is a GNU extension");
903 elf_add_reloc(s
, segment
, R_386_PC16
);
906 "Unsupported non-32-bit ELF relocation");
910 WRITESHORT(p
, *(int32_t *)data
- realbytes
);
911 elf_sect_write(s
, mydata
, 2L);
912 } else if (type
== OUT_REL4ADR
) {
913 if (segment
== segto
)
914 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
915 if (segment
!= NO_SEG
&& segment
% 2) {
916 error(ERR_NONFATAL
, "ELF format does not support"
917 " segment base references");
920 elf_add_reloc(s
, segment
, R_386_PC32
);
921 } else if (wrt
== elf_plt_sect
+ 1) {
922 elf_add_reloc(s
, segment
, R_386_PLT32
);
923 } else if (wrt
== elf_gotpc_sect
+ 1 ||
924 wrt
== elf_gotoff_sect
+ 1 ||
925 wrt
== elf_got_sect
+ 1) {
926 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
927 "relative GOT references");
929 error(ERR_NONFATAL
, "ELF format does not support this"
931 wrt
= NO_SEG
; /* we can at least _try_ to continue */
935 WRITELONG(p
, *(int32_t *)data
- realbytes
);
936 elf_sect_write(s
, mydata
, 4L);
940 static void elf_write(void)
942 int nsections
, align
;
950 int32_t symtablen
, symtablocal
;
953 * Work out how many sections we will have. We have SHN_UNDEF,
954 * then the flexible user sections, then the four fixed
955 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
956 * then optionally relocation sections for the user sections.
958 if (of_elf32
.current_dfmt
== &df_stabs
)
961 nsections
= 5; /* SHN_UNDEF and the fixed ones */
963 add_sectname("", ".comment");
964 add_sectname("", ".shstrtab");
965 add_sectname("", ".symtab");
966 add_sectname("", ".strtab");
967 for (i
= 0; i
< nsects
; i
++) {
968 nsections
++; /* for the section itself */
969 if (sects
[i
]->head
) {
970 nsections
++; /* for its relocations */
971 add_sectname(".rel", sects
[i
]->name
);
975 if (of_elf32
.current_dfmt
== &df_stabs
) {
976 /* in case the debug information is wanted, just add these three sections... */
977 add_sectname("", ".stab");
978 add_sectname("", ".stabstr");
979 add_sectname(".rel", ".stab");
987 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
990 * Output the ELF header.
992 fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
993 fwriteint16_t(1, elffp
); /* ET_REL relocatable file */
994 fwriteint16_t(3, elffp
); /* EM_386 processor ID */
995 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
996 fwriteint32_t(0L, elffp
); /* no entry point */
997 fwriteint32_t(0L, elffp
); /* no program header table */
998 fwriteint32_t(0x40L
, elffp
); /* section headers straight after
999 * ELF header plus alignment */
1000 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1001 fwriteint16_t(0x34, elffp
); /* size of ELF header */
1002 fwriteint16_t(0, elffp
); /* no program header table, again */
1003 fwriteint16_t(0, elffp
); /* still no program header table */
1004 fwriteint16_t(0x28, elffp
); /* size of section header */
1005 fwriteint16_t(nsections
, elffp
); /* number of sections */
1006 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1007 * section header table */
1008 fwriteint32_t(0L, elffp
); /* align to 0x40 bytes */
1009 fwriteint32_t(0L, elffp
);
1010 fwriteint32_t(0L, elffp
);
1013 * Build the symbol table and relocation tables.
1015 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1016 for (i
= 0; i
< nsects
; i
++)
1018 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1022 * Now output the section header table.
1025 elf_foffs
= 0x40 + 0x28 * nsections
;
1026 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1029 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1031 elf_section_header(0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1032 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1034 for (i
= 0; i
< nsects
; i
++) {
1035 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1036 (sects
[i
]->type
== SHT_PROGBITS
?
1037 sects
[i
]->data
: NULL
), TRUE
,
1038 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1040 scount
++; /* dito */
1042 elf_section_header(p
- shstrtab
, 1, 0, comment
, FALSE
, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1043 scount
++; /* dito */
1045 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, FALSE
, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1046 scount
++; /* dito */
1048 elf_section_header(p
- shstrtab
, 2, 0, symtab
, TRUE
, symtablen
, nsects
+ 4, symtablocal
, 4, 16); /* .symtab */
1049 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1051 elf_section_header(p
- shstrtab
, 3, 0, strs
, TRUE
, strslen
, 0, 0, 1, 0); /* .strtab */
1052 for (i
= 0; i
< nsects
; i
++)
1053 if (sects
[i
]->head
) {
1055 elf_section_header(p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
1056 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 8);
1058 if (of_elf32
.current_dfmt
== &df_stabs
) {
1059 /* for debugging information, create the last three sections
1060 which are the .stab , .stabstr and .rel.stab sections respectively */
1062 /* this function call creates the stab sections in memory */
1065 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1067 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, 0, stablen
,
1068 nsections
- 2, 0, 4, 12);
1071 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, 0,
1072 stabstrlen
, 0, 0, 4, 0);
1075 /* link -> symtable info -> section to refer to */
1076 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, 0,
1077 stabrellen
, symtabsection
, nsections
- 3, 4,
1081 fwrite(align_str
, align
, 1, elffp
);
1084 * Now output the sections.
1086 elf_write_sections();
1088 nasm_free(elf_sects
);
1092 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1094 struct SAA
*s
= saa_init(1L);
1096 uint8_t entry
[16], *p
;
1102 * First, an all-zeros entry, required by the ELF spec.
1104 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1109 * Next, an entry for the file name.
1112 WRITELONG(p
, 1); /* we know it's 1st thing in strtab */
1113 WRITELONG(p
, 0); /* no value */
1114 WRITELONG(p
, 0); /* no size either */
1115 WRITESHORT(p
, 4); /* type FILE */
1116 WRITESHORT(p
, SHN_ABS
);
1117 saa_wbytes(s
, entry
, 16L);
1122 * Now some standard symbols defining the segments, for relocation
1125 for (i
= 1; i
<= nsects
+ 1; i
++) {
1127 WRITELONG(p
, 0); /* no symbol name */
1128 WRITELONG(p
, 0); /* offset zero */
1129 WRITELONG(p
, 0); /* size zero */
1130 WRITESHORT(p
, 3); /* local section-type thing */
1131 WRITESHORT(p
, (i
== 1 ? SHN_ABS
: i
- 1)); /* the section id */
1132 saa_wbytes(s
, entry
, 16L);
1138 * Now the other local symbols.
1141 while ((sym
= saa_rstruct(syms
))) {
1142 if (sym
->type
& SYM_GLOBAL
)
1145 WRITELONG(p
, sym
->strpos
);
1146 WRITELONG(p
, sym
->value
);
1147 WRITELONG(p
, sym
->size
);
1148 WRITECHAR(p
, sym
->type
); /* local non-typed thing */
1149 WRITECHAR(p
, sym
->other
);
1150 WRITESHORT(p
, sym
->section
);
1151 saa_wbytes(s
, entry
, 16L);
1157 * Now the global symbols.
1160 while ((sym
= saa_rstruct(syms
))) {
1161 if (!(sym
->type
& SYM_GLOBAL
))
1164 WRITELONG(p
, sym
->strpos
);
1165 WRITELONG(p
, sym
->value
);
1166 WRITELONG(p
, sym
->size
);
1167 WRITECHAR(p
, sym
->type
); /* global non-typed thing */
1168 WRITECHAR(p
, sym
->other
);
1169 WRITESHORT(p
, sym
->section
);
1170 saa_wbytes(s
, entry
, 16L);
1177 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1180 uint8_t *p
, entry
[8];
1189 int32_t sym
= r
->symbol
;
1191 if (sym
>= GLOBAL_TEMP_BASE
)
1192 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 3) + nlocals
;
1195 WRITELONG(p
, r
->address
);
1196 WRITELONG(p
, (sym
<< 8) + r
->type
);
1197 saa_wbytes(s
, entry
, 8L);
1206 static void elf_section_header(int name
, int type
, int flags
,
1207 void *data
, int is_saa
, int32_t datalen
,
1208 int link
, int info
, int align
, int eltsize
)
1210 elf_sects
[elf_nsect
].data
= data
;
1211 elf_sects
[elf_nsect
].len
= datalen
;
1212 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1215 fwriteint32_t((int32_t)name
, elffp
);
1216 fwriteint32_t((int32_t)type
, elffp
);
1217 fwriteint32_t((int32_t)flags
, elffp
);
1218 fwriteint32_t(0L, elffp
); /* no address, ever, in object files */
1219 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1220 fwriteint32_t(datalen
, elffp
);
1222 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1223 fwriteint32_t((int32_t)link
, elffp
);
1224 fwriteint32_t((int32_t)info
, elffp
);
1225 fwriteint32_t((int32_t)align
, elffp
);
1226 fwriteint32_t((int32_t)eltsize
, elffp
);
1229 static void elf_write_sections(void)
1232 for (i
= 0; i
< elf_nsect
; i
++)
1233 if (elf_sects
[i
].data
) {
1234 int32_t len
= elf_sects
[i
].len
;
1235 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1236 int32_t align
= reallen
- len
;
1237 if (elf_sects
[i
].is_saa
)
1238 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1240 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1241 fwrite(align_str
, align
, 1, elffp
);
1245 static void elf_sect_write(struct Section
*sect
,
1246 const uint8_t *data
, uint32_t len
)
1248 saa_wbytes(sect
->data
, data
, len
);
1252 static int32_t elf_segbase(int32_t segment
)
1257 static int elf_directive(char *directive
, char *value
, int pass
)
1265 static void elf_filename(char *inname
, char *outname
, efunc error
)
1267 strcpy(elf_module
, inname
);
1268 standard_extension(inname
, outname
, ".o", error
);
1271 static const char *elf_stdmac
[] = {
1272 "%define __SECT__ [section .text]",
1273 "%macro __NASM_CDecl__ 1",
1278 static int elf_set_info(enum geninfo type
, char **val
)
1285 static struct dfmt df_stabs
= {
1286 "ELF32 (i386) stabs debug format for Linux",
1297 struct dfmt
*elf32_debugs_arr
[2] = { &df_stabs
, NULL
};
1299 struct ofmt of_elf32
= {
1300 "ELF32 (i386) object files (e.g. Linux)",
1317 struct ofmt of_elf
= {
1318 "ELF (short name for ELF32) ",
1334 /* again, the stabs debugging stuff (code) */
1336 void stabs32_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1344 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1347 if (!stabs_filename
) {
1348 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1349 strcpy(stabs_filename
, filename
);
1351 if (strcmp(stabs_filename
, filename
)) {
1352 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1353 in fact, this leak comes in quite handy to maintain a list of files
1354 encountered so far in the symbol lines... */
1356 /* why not nasm_free(stabs_filename); we're done with the old one */
1358 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1359 strcpy(stabs_filename
, filename
);
1363 currentline
= linenumber
;
1366 void stabs32_deflabel(char *name
, int32_t segment
, int32_t offset
, int is_global
,
1376 void stabs32_directive(const char *directive
, const char *params
)
1382 void stabs32_typevalue(int32_t type
)
1387 void stabs32_output(int type
, void *param
)
1389 struct symlininfo
*s
;
1390 struct linelist
*el
;
1391 if (type
== TY_STABSSYMLIN
) {
1392 if (stabs_immcall
) {
1393 s
= (struct symlininfo
*)param
;
1394 if (strcmp(s
->name
, ".text"))
1395 return; /* we are only interested in the text stuff */
1397 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1398 el
->info
.offset
= s
->offset
;
1399 el
->info
.section
= s
->section
;
1400 el
->info
.name
= s
->name
;
1401 el
->line
= currentline
;
1402 el
->filename
= stabs_filename
;
1405 stabslines
->last
->next
= el
;
1406 stabslines
->last
= el
;
1409 stabslines
->last
= el
;
1416 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1418 WRITELONG(p,n_strx); \
1419 WRITECHAR(p,n_type); \
1420 WRITECHAR(p,n_other); \
1421 WRITESHORT(p,n_desc); \
1422 WRITELONG(p,n_value); \
1425 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1427 void stabs32_generate(void)
1429 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1430 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1434 struct linelist
*ptr
;
1438 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1439 for (i
= 0; i
< numlinestabs
; i
++)
1443 if (numfiles
== 0) {
1444 allfiles
[0] = ptr
->filename
;
1447 for (i
= 0; i
< numfiles
; i
++) {
1448 if (!strcmp(allfiles
[i
], ptr
->filename
))
1451 if (i
>= numfiles
) {
1452 allfiles
[i
] = ptr
->filename
;
1459 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1460 for (i
= 0; i
< numfiles
; i
++) {
1461 fileidx
[i
] = strsize
;
1462 strsize
+= strlen(allfiles
[i
]) + 1;
1465 for (i
= 0; i
< numfiles
; i
++) {
1466 if (!strcmp(allfiles
[i
], elf_module
)) {
1472 /* worst case size of the stab buffer would be:
1473 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1476 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1477 sizeof(struct stabentry
));
1479 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1481 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1484 for (i
= 0; i
< numfiles
; i
++) {
1485 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1489 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1492 /* this is the first stab, its strx points to the filename of the
1493 the source-file, the n_desc field should be set to the number
1496 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1502 /* this is the stab for the main source file */
1503 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1505 /* relocation table entry */
1507 /* Since the above WRITE_STAB calls have already */
1508 /* created two entries, the index in the info.section */
1509 /* member must be adjusted by adding 3 */
1511 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1512 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1515 currfile
= mainfileindex
;
1519 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1520 /* oops file has changed... */
1521 for (i
= 0; i
< numfiles
; i
++)
1522 if (!strcmp(allfiles
[i
], ptr
->filename
))
1525 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1529 /* relocation table entry */
1530 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1531 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1534 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1537 /* relocation table entry */
1539 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1540 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1546 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1548 nasm_free(allfiles
);
1551 stablen
= (sptr
- sbuf
);
1552 stabrellen
= (rptr
- rbuf
);
1558 void stabs32_cleanup()
1560 struct linelist
*ptr
, *del
;
1572 nasm_free(stabrelbuf
);
1574 nasm_free(stabstrbuf
);