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.
26 R_386_32
= 1, /* ordinary absolute relocation */
27 R_386_PC32
= 2, /* PC-relative relocation */
28 R_386_GOT32
= 3, /* an offset into GOT */
29 R_386_PLT32
= 4, /* a PC-relative offset into PLT */
30 R_386_COPY
= 5, /* ??? */
31 R_386_GLOB_DAT
= 6, /* ??? */
32 R_386_JUMP_SLOT
= 7, /* ??? */
33 R_386_RELATIVE
= 8, /* ??? */
34 R_386_GOTOFF
= 9, /* an offset from GOT base */
35 R_386_GOTPC
= 10, /* a PC-relative offset _to_ GOT */
36 /* These are GNU extensions, but useful */
37 R_386_16
= 20, /* A 16-bit absolute relocation */
38 R_386_PC16
= 21, /* A 16-bit PC-relative relocation */
39 R_386_8
= 22, /* An 8-bit absolute relocation */
40 R_386_PC8
= 23 /* An 8-bit PC-relative relocation */
45 int32_t address
; /* relative to _start_ of section */
46 int32_t symbol
; /* ELF symbol info thingy */
47 int type
; /* type of relocation */
51 int32_t strpos
; /* string table position of name */
52 int32_t section
; /* section ID of the symbol */
53 int type
; /* symbol type */
54 int other
; /* symbol visibility */
55 int32_t value
; /* address, or COMMON variable align */
56 int32_t size
; /* size of symbol */
57 int32_t globnum
; /* symbol table offset if global */
58 struct Symbol
*next
; /* list of globals in each section */
59 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
60 char *name
; /* used temporarily if in above list */
63 #define SHT_PROGBITS 1
68 #define SHF_EXECINSTR 4
72 uint32_t len
, size
, nrelocs
;
74 int type
; /* SHT_PROGBITS or SHT_NOBITS */
75 int align
; /* alignment: power of two */
76 uint32_t flags
; /* section flags */
80 struct Reloc
*head
, **tail
;
81 struct Symbol
*gsyms
; /* global symbols in section */
85 static struct Section
**sects
;
86 static int nsects
, sectlen
;
88 #define SHSTR_DELTA 256
89 static char *shstrtab
;
90 static int shstrtablen
, shstrtabsize
;
92 static struct SAA
*syms
;
93 static uint32_t nlocals
, nglobs
;
95 static int32_t def_seg
;
97 static struct RAA
*bsym
;
99 static struct SAA
*strs
;
100 static uint32_t strslen
;
104 static evalfunc evaluate
;
106 static struct Symbol
*fwds
;
108 static char elf_module
[FILENAME_MAX
];
110 extern struct ofmt of_elf32
;
111 extern struct ofmt of_elf
;
113 #define SHN_ABS 0xFFF1
114 #define SHN_COMMON 0xFFF2
117 #define SYM_SECTION 0x04
118 #define SYM_GLOBAL 0x10
119 #define SYM_NOTYPE 0x00
120 #define SYM_DATA 0x01
121 #define SYM_FUNCTION 0x02
123 #define STV_DEFAULT 0
124 #define STV_INTERNAL 1
126 #define STV_PROTECTED 3
128 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
130 #define SEG_ALIGN 16 /* alignment of sections in file */
131 #define SEG_ALIGN_1 (SEG_ALIGN-1)
133 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
135 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
136 static struct ELF_SECTDATA
{
141 static int elf_nsect
;
142 static int32_t elf_foffs
;
144 static void elf_write(void);
145 static void elf_sect_write(struct Section
*, const uint8_t *,
147 static void elf_section_header(int, int, int, void *, int, int32_t, int, int,
149 static void elf_write_sections(void);
150 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
151 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
152 static void add_sectname(char *, char *);
154 /* this stuff is needed for the stabs debugging format */
155 #define N_SO 0x64 /* ID for main source file */
156 #define N_SOL 0x84 /* ID for sub-source file */
160 #define TY_STABSSYMLIN 0x40 /* ouch */
176 int section
; /* section index */
177 char *name
; /* shallow-copied pointer of section name */
181 struct symlininfo info
;
184 struct linelist
*next
;
185 struct linelist
*last
;
188 static struct linelist
*stabslines
= 0;
189 static int stabs_immcall
= 0;
190 static int currentline
= 0;
191 static int numlinestabs
= 0;
192 static char *stabs_filename
= 0;
193 static int symtabsection
;
194 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
195 static int stablen
, stabstrlen
, stabrellen
;
197 static struct dfmt df_stabs
;
199 void stabs32_init(struct ofmt
*, void *, FILE *, efunc
);
200 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
201 void stabs32_deflabel(char *, int32_t, int32_t, int, char *);
202 void stabs32_directive(const char *, const char *);
203 void stabs32_typevalue(int32_t);
204 void stabs32_output(int, void *);
205 void stabs32_generate();
206 void stabs32_cleanup();
208 /* end of stabs debugging stuff */
211 * Special section numbers which are used to define ELF special
212 * symbols, which can be used with WRT to provide PIC relocation
215 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
216 static int32_t elf_got_sect
, elf_plt_sect
;
217 static int32_t elf_sym_sect
;
219 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
224 (void)ldef
; /* placate optimisers */
226 nsects
= sectlen
= 0;
227 syms
= saa_init((int32_t)sizeof(struct Symbol
));
228 nlocals
= nglobs
= 0;
231 saa_wbytes(strs
, "\0", 1L);
232 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
233 strslen
= 2 + strlen(elf_module
);
235 shstrtablen
= shstrtabsize
= 0;;
236 add_sectname("", "");
240 elf_gotpc_sect
= seg_alloc();
241 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
243 elf_gotoff_sect
= seg_alloc();
244 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
246 elf_got_sect
= seg_alloc();
247 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
249 elf_plt_sect
= seg_alloc();
250 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
252 elf_sym_sect
= seg_alloc();
253 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf32
,
256 def_seg
= seg_alloc();
259 static void elf_cleanup(int debuginfo
)
268 for (i
= 0; i
< nsects
; i
++) {
269 if (sects
[i
]->type
!= SHT_NOBITS
)
270 saa_free(sects
[i
]->data
);
272 saa_free(sects
[i
]->rel
);
273 while (sects
[i
]->head
) {
275 sects
[i
]->head
= sects
[i
]->head
->next
;
283 if (of_elf32
.current_dfmt
) {
284 of_elf32
.current_dfmt
->cleanup();
288 static void add_sectname(char *firsthalf
, char *secondhalf
)
290 int len
= strlen(firsthalf
) + strlen(secondhalf
);
291 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
292 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
293 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
294 strcat(shstrtab
+ shstrtablen
, secondhalf
);
295 shstrtablen
+= len
+ 1;
298 static int elf_make_section(char *name
, int type
, int flags
, int align
)
302 s
= nasm_malloc(sizeof(*s
));
304 if (type
!= SHT_NOBITS
)
305 s
->data
= saa_init(1L);
308 s
->len
= s
->size
= 0;
310 if (!strcmp(name
, ".text"))
313 s
->index
= seg_alloc();
314 add_sectname("", name
);
315 s
->name
= nasm_malloc(1 + strlen(name
));
316 strcpy(s
->name
, name
);
322 if (nsects
>= sectlen
)
324 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
330 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
333 int flags_and
, flags_or
, type
, align
, i
;
336 * Default is 32 bits.
344 while (*p
&& !isspace(*p
))
348 flags_and
= flags_or
= type
= align
= 0;
350 while (*p
&& isspace(*p
))
354 while (*p
&& !isspace(*p
))
358 while (*p
&& isspace(*p
))
361 if (!nasm_strnicmp(q
, "align=", 6)) {
365 if ((align
- 1) & align
) { /* means it's not a power of two */
366 error(ERR_NONFATAL
, "section alignment %d is not"
367 " a power of two", align
);
370 } else if (!nasm_stricmp(q
, "alloc")) {
371 flags_and
|= SHF_ALLOC
;
372 flags_or
|= SHF_ALLOC
;
373 } else if (!nasm_stricmp(q
, "noalloc")) {
374 flags_and
|= SHF_ALLOC
;
375 flags_or
&= ~SHF_ALLOC
;
376 } else if (!nasm_stricmp(q
, "exec")) {
377 flags_and
|= SHF_EXECINSTR
;
378 flags_or
|= SHF_EXECINSTR
;
379 } else if (!nasm_stricmp(q
, "noexec")) {
380 flags_and
|= SHF_EXECINSTR
;
381 flags_or
&= ~SHF_EXECINSTR
;
382 } else if (!nasm_stricmp(q
, "write")) {
383 flags_and
|= SHF_WRITE
;
384 flags_or
|= SHF_WRITE
;
385 } else if (!nasm_stricmp(q
, "nowrite")) {
386 flags_and
|= SHF_WRITE
;
387 flags_or
&= ~SHF_WRITE
;
388 } else if (!nasm_stricmp(q
, "progbits")) {
390 } else if (!nasm_stricmp(q
, "nobits")) {
395 if (!strcmp(name
, ".comment") ||
396 !strcmp(name
, ".shstrtab") ||
397 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
398 error(ERR_NONFATAL
, "attempt to redefine reserved section"
403 for (i
= 0; i
< nsects
; i
++)
404 if (!strcmp(name
, sects
[i
]->name
))
407 if (!strcmp(name
, ".text"))
408 i
= elf_make_section(name
, SHT_PROGBITS
,
409 SHF_ALLOC
| SHF_EXECINSTR
, 16);
410 else if (!strcmp(name
, ".rodata"))
411 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
412 else if (!strcmp(name
, ".data"))
413 i
= elf_make_section(name
, SHT_PROGBITS
,
414 SHF_ALLOC
| SHF_WRITE
, 4);
415 else if (!strcmp(name
, ".bss"))
416 i
= elf_make_section(name
, SHT_NOBITS
,
417 SHF_ALLOC
| SHF_WRITE
, 4);
419 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
421 sects
[i
]->type
= type
;
423 sects
[i
]->align
= align
;
424 sects
[i
]->flags
&= ~flags_and
;
425 sects
[i
]->flags
|= flags_or
;
426 } else if (pass
== 1) {
427 if (type
|| align
|| flags_and
)
428 error(ERR_WARNING
, "section attributes ignored on"
429 " redeclaration of section `%s'", name
);
432 return sects
[i
]->index
;
435 static void elf_deflabel(char *name
, int32_t segment
, int32_t offset
,
436 int is_global
, char *special
)
440 int special_used
= FALSE
;
442 #if defined(DEBUG) && DEBUG>2
444 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
445 name
, segment
, offset
, is_global
, special
);
447 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
449 * This is a NASM special symbol. We never allow it into
450 * the ELF symbol table, even if it's a valid one. If it
451 * _isn't_ a valid one, we should barf immediately.
453 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
454 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
455 strcmp(name
, "..sym"))
456 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
460 if (is_global
== 3) {
463 * Fix up a forward-reference symbol size from the first
466 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
467 if (!strcmp((*s
)->name
, name
)) {
468 struct tokenval tokval
;
472 while (*p
&& !isspace(*p
))
474 while (*p
&& isspace(*p
))
478 tokval
.t_type
= TOKEN_INVALID
;
479 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
482 error(ERR_NONFATAL
, "cannot use relocatable"
483 " expression as symbol size");
485 (*s
)->size
= reloc_value(e
);
489 * Remove it from the list of unresolved sizes.
491 nasm_free((*s
)->name
);
495 return; /* it wasn't an important one */
498 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
499 strslen
+= 1 + strlen(name
);
501 sym
= saa_wstruct(syms
);
504 sym
->type
= is_global
? SYM_GLOBAL
: 0;
505 sym
->other
= STV_DEFAULT
;
507 if (segment
== NO_SEG
)
508 sym
->section
= SHN_ABS
;
511 sym
->section
= SHN_UNDEF
;
512 if (nsects
== 0 && segment
== def_seg
) {
514 if (segment
!= elf_section_names(".text", 2, &tempint
))
516 "strange segment conditions in ELF driver");
517 sym
->section
= nsects
;
519 for (i
= 0; i
< nsects
; i
++)
520 if (segment
== sects
[i
]->index
) {
521 sym
->section
= i
+ 1;
527 if (is_global
== 2) {
530 sym
->section
= SHN_COMMON
;
532 * We have a common variable. Check the special text to see
533 * if it's a valid number and power of two; if so, store it
534 * as the alignment for the common variable.
538 sym
->value
= readnum(special
, &err
);
540 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
541 " valid number", special
);
542 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
543 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
544 " power of two", special
);
548 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
550 if (sym
->type
== SYM_GLOBAL
) {
552 * If sym->section == SHN_ABS, then the first line of the
553 * else section would cause a core dump, because its a reference
554 * beyond the end of the section array.
555 * This behaviour is exhibited by this code:
558 * To avoid such a crash, such requests are silently discarded.
559 * This may not be the best solution.
561 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
562 bsym
= raa_write(bsym
, segment
, nglobs
);
563 } else if (sym
->section
!= SHN_ABS
) {
565 * This is a global symbol; so we must add it to the linked
566 * list of global symbols in its section. We'll push it on
567 * the beginning of the list, because it doesn't matter
568 * much which end we put it on and it's easier like this.
570 * In addition, we check the special text for symbol
571 * type and size information.
573 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
574 sects
[sym
->section
- 1]->gsyms
= sym
;
577 int n
= strcspn(special
, " \t");
579 if (!nasm_strnicmp(special
, "function", n
))
580 sym
->type
|= SYM_FUNCTION
;
581 else if (!nasm_strnicmp(special
, "data", n
) ||
582 !nasm_strnicmp(special
, "object", n
))
583 sym
->type
|= SYM_DATA
;
584 else if (!nasm_strnicmp(special
, "notype", n
))
585 sym
->type
|= SYM_NOTYPE
;
587 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
591 while (isspace(*special
))
594 n
= strcspn(special
, " \t");
595 if (!nasm_strnicmp(special
, "default", n
))
596 sym
->other
= STV_DEFAULT
;
597 else if (!nasm_strnicmp(special
, "internal", n
))
598 sym
->other
= STV_INTERNAL
;
599 else if (!nasm_strnicmp(special
, "hidden", n
))
600 sym
->other
= STV_HIDDEN
;
601 else if (!nasm_strnicmp(special
, "protected", n
))
602 sym
->other
= STV_PROTECTED
;
609 struct tokenval tokval
;
612 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
614 while (special
[n
] && isspace(special
[n
]))
617 * We have a size expression; attempt to
621 stdscan_bufptr
= special
+ n
;
622 tokval
.t_type
= TOKEN_INVALID
;
623 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
628 sym
->name
= nasm_strdup(name
);
631 error(ERR_NONFATAL
, "cannot use relocatable"
632 " expression as symbol size");
634 sym
->size
= reloc_value(e
);
636 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
641 sym
->globnum
= nglobs
;
646 if (special
&& !special_used
)
647 error(ERR_NONFATAL
, "no special symbol features supported here");
650 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
654 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
655 sect
->tail
= &r
->next
;
658 r
->address
= sect
->len
;
659 if (segment
== NO_SEG
)
664 for (i
= 0; i
< nsects
; i
++)
665 if (segment
== sects
[i
]->index
)
668 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
676 * This routine deals with ..got and ..sym relocations: the more
677 * complicated kinds. In shared-library writing, some relocations
678 * with respect to global symbols must refer to the precise symbol
679 * rather than referring to an offset from the base of the section
680 * _containing_ the symbol. Such relocations call to this routine,
681 * which searches the symbol list for the symbol in question.
683 * R_386_GOT32 references require the _exact_ symbol address to be
684 * used; R_386_32 references can be at an offset from the symbol.
685 * The boolean argument `exact' tells us this.
687 * Return value is the adjusted value of `addr', having become an
688 * offset from the symbol rather than the section. Should always be
689 * zero when returning from an exact call.
691 * Limitation: if you define two symbols at the same place,
692 * confusion will occur.
694 * Inefficiency: we search, currently, using a linked list which
695 * isn't even necessarily sorted.
697 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
698 int32_t segment
, int32_t offset
,
703 struct Symbol
*sym
, *sm
;
707 * First look up the segment/offset pair and find a global
708 * symbol corresponding to it. If it's not one of our segments,
709 * then it must be an external symbol, in which case we're fine
710 * doing a normal elf_add_reloc after first sanity-checking
711 * that the offset from the symbol is zero.
714 for (i
= 0; i
< nsects
; i
++)
715 if (segment
== sects
[i
]->index
) {
720 if (exact
&& offset
!= 0)
721 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
722 " for this reference");
724 elf_add_reloc(sect
, segment
, type
);
730 * Find a symbol pointing _exactly_ at this one.
732 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
733 if (sym
->value
== offset
)
737 * Find the nearest symbol below this one.
740 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
741 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
745 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
746 " for this reference");
750 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
751 sect
->tail
= &r
->next
;
754 r
->address
= sect
->len
;
755 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
760 return offset
- sym
->value
;
763 static void elf_out(int32_t segto
, const void *data
, uint32_t type
,
764 int32_t segment
, int32_t wrt
)
767 int32_t realbytes
= type
& OUT_SIZMASK
;
769 uint8_t mydata
[4], *p
;
771 static struct symlininfo sinfo
;
776 * handle absolute-assembly (structure definitions)
778 if (segto
== NO_SEG
) {
779 if (type
!= OUT_RESERVE
)
780 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
786 for (i
= 0; i
< nsects
; i
++)
787 if (segto
== sects
[i
]->index
) {
792 int tempint
; /* ignored */
793 if (segto
!= elf_section_names(".text", 2, &tempint
))
794 error(ERR_PANIC
, "strange segment conditions in ELF driver");
796 s
= sects
[nsects
- 1];
801 /* again some stabs debugging stuff */
802 if (of_elf32
.current_dfmt
) {
803 sinfo
.offset
= s
->len
;
805 sinfo
.name
= s
->name
;
806 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
808 /* end of debugging stuff */
810 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
811 error(ERR_WARNING
, "attempt to initialize memory in"
812 " BSS section `%s': ignored", s
->name
);
813 if (type
== OUT_REL2ADR
)
815 else if (type
== OUT_REL4ADR
)
821 if (type
== OUT_RESERVE
) {
822 if (s
->type
== SHT_PROGBITS
) {
823 error(ERR_WARNING
, "uninitialized space declared in"
824 " non-BSS section `%s': zeroing", s
->name
);
825 elf_sect_write(s
, NULL
, realbytes
);
828 } else if (type
== OUT_RAWDATA
) {
829 if (segment
!= NO_SEG
)
830 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
831 elf_sect_write(s
, data
, realbytes
);
832 } else if (type
== OUT_ADDRESS
) {
834 addr
= *(int32_t *)data
;
835 if (segment
!= NO_SEG
) {
837 error(ERR_NONFATAL
, "ELF format does not support"
838 " segment base references");
841 if (realbytes
== 2) {
843 elf_add_reloc(s
, segment
, R_386_16
);
845 elf_add_reloc(s
, segment
, R_386_32
);
847 } else if (wrt
== elf_gotpc_sect
+ 1) {
849 * The user will supply GOT relative to $$. ELF
850 * will let us have GOT relative to $. So we
851 * need to fix up the data item by $-$$.
854 elf_add_reloc(s
, segment
, R_386_GOTPC
);
855 } else if (wrt
== elf_gotoff_sect
+ 1) {
856 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
857 } else if (wrt
== elf_got_sect
+ 1) {
858 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
860 } else if (wrt
== elf_sym_sect
+ 1) {
861 if (realbytes
== 2) {
863 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
866 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
869 } else if (wrt
== elf_plt_sect
+ 1) {
870 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
871 "relative PLT references");
873 error(ERR_NONFATAL
, "ELF format does not support this"
875 wrt
= NO_SEG
; /* we can at least _try_ to continue */
881 error(ERR_WARNING
| ERR_WARN_GNUELF
,
882 "16-bit relocations in ELF is a GNU extension");
885 if (realbytes
!= 4 && segment
!= NO_SEG
) {
887 "Unsupported non-32-bit ELF relocation");
891 elf_sect_write(s
, mydata
, realbytes
);
892 } else if (type
== OUT_REL2ADR
) {
893 if (segment
== segto
)
894 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
895 if (segment
!= NO_SEG
&& segment
% 2) {
896 error(ERR_NONFATAL
, "ELF format does not support"
897 " segment base references");
900 error(ERR_WARNING
| ERR_WARN_GNUELF
,
901 "16-bit relocations in ELF is a GNU extension");
902 elf_add_reloc(s
, segment
, R_386_PC16
);
905 "Unsupported non-32-bit ELF relocation");
909 WRITESHORT(p
, *(int32_t *)data
- realbytes
);
910 elf_sect_write(s
, mydata
, 2L);
911 } else if (type
== OUT_REL4ADR
) {
912 if (segment
== segto
)
913 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
914 if (segment
!= NO_SEG
&& segment
% 2) {
915 error(ERR_NONFATAL
, "ELF format does not support"
916 " segment base references");
919 elf_add_reloc(s
, segment
, R_386_PC32
);
920 } else if (wrt
== elf_plt_sect
+ 1) {
921 elf_add_reloc(s
, segment
, R_386_PLT32
);
922 } else if (wrt
== elf_gotpc_sect
+ 1 ||
923 wrt
== elf_gotoff_sect
+ 1 ||
924 wrt
== elf_got_sect
+ 1) {
925 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
926 "relative GOT references");
928 error(ERR_NONFATAL
, "ELF format does not support this"
930 wrt
= NO_SEG
; /* we can at least _try_ to continue */
934 WRITELONG(p
, *(int32_t *)data
- realbytes
);
935 elf_sect_write(s
, mydata
, 4L);
939 static void elf_write(void)
941 int nsections
, align
;
949 int32_t symtablen
, symtablocal
;
952 * Work out how many sections we will have. We have SHN_UNDEF,
953 * then the flexible user sections, then the four fixed
954 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
955 * then optionally relocation sections for the user sections.
957 if (of_elf32
.current_dfmt
== &df_stabs
)
960 nsections
= 5; /* SHN_UNDEF and the fixed ones */
962 add_sectname("", ".comment");
963 add_sectname("", ".shstrtab");
964 add_sectname("", ".symtab");
965 add_sectname("", ".strtab");
966 for (i
= 0; i
< nsects
; i
++) {
967 nsections
++; /* for the section itself */
968 if (sects
[i
]->head
) {
969 nsections
++; /* for its relocations */
970 add_sectname(".rel", sects
[i
]->name
);
974 if (of_elf32
.current_dfmt
== &df_stabs
) {
975 /* in case the debug information is wanted, just add these three sections... */
976 add_sectname("", ".stab");
977 add_sectname("", ".stabstr");
978 add_sectname(".rel", ".stab");
986 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
989 * Output the ELF header.
991 fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
992 fwriteint16_t(1, elffp
); /* ET_REL relocatable file */
993 fwriteint16_t(3, elffp
); /* EM_386 processor ID */
994 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
995 fwriteint32_t(0L, elffp
); /* no entry point */
996 fwriteint32_t(0L, elffp
); /* no program header table */
997 fwriteint32_t(0x40L
, elffp
); /* section headers straight after
998 * ELF header plus alignment */
999 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1000 fwriteint16_t(0x34, elffp
); /* size of ELF header */
1001 fwriteint16_t(0, elffp
); /* no program header table, again */
1002 fwriteint16_t(0, elffp
); /* still no program header table */
1003 fwriteint16_t(0x28, elffp
); /* size of section header */
1004 fwriteint16_t(nsections
, elffp
); /* number of sections */
1005 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1006 * section header table */
1007 fwriteint32_t(0L, elffp
); /* align to 0x40 bytes */
1008 fwriteint32_t(0L, elffp
);
1009 fwriteint32_t(0L, elffp
);
1012 * Build the symbol table and relocation tables.
1014 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1015 for (i
= 0; i
< nsects
; i
++)
1017 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1021 * Now output the section header table.
1024 elf_foffs
= 0x40 + 0x28 * nsections
;
1025 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1028 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1030 elf_section_header(0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1031 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1033 for (i
= 0; i
< nsects
; i
++) {
1034 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1035 (sects
[i
]->type
== SHT_PROGBITS
?
1036 sects
[i
]->data
: NULL
), TRUE
,
1037 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1039 scount
++; /* dito */
1041 elf_section_header(p
- shstrtab
, 1, 0, comment
, FALSE
, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1042 scount
++; /* dito */
1044 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, FALSE
, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1045 scount
++; /* dito */
1047 elf_section_header(p
- shstrtab
, 2, 0, symtab
, TRUE
, symtablen
, nsects
+ 4, symtablocal
, 4, 16); /* .symtab */
1048 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1050 elf_section_header(p
- shstrtab
, 3, 0, strs
, TRUE
, strslen
, 0, 0, 1, 0); /* .strtab */
1051 for (i
= 0; i
< nsects
; i
++)
1052 if (sects
[i
]->head
) {
1054 elf_section_header(p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
1055 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 8);
1057 if (of_elf32
.current_dfmt
== &df_stabs
) {
1058 /* for debugging information, create the last three sections
1059 which are the .stab , .stabstr and .rel.stab sections respectively */
1061 /* this function call creates the stab sections in memory */
1064 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1066 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, 0, stablen
,
1067 nsections
- 2, 0, 4, 12);
1070 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, 0,
1071 stabstrlen
, 0, 0, 4, 0);
1074 /* link -> symtable info -> section to refer to */
1075 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, 0,
1076 stabrellen
, symtabsection
, nsections
- 3, 4,
1080 fwrite(align_str
, align
, 1, elffp
);
1083 * Now output the sections.
1085 elf_write_sections();
1087 nasm_free(elf_sects
);
1091 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1093 struct SAA
*s
= saa_init(1L);
1095 uint8_t entry
[16], *p
;
1101 * First, an all-zeros entry, required by the ELF spec.
1103 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1108 * Next, an entry for the file name.
1111 WRITELONG(p
, 1); /* we know it's 1st thing in strtab */
1112 WRITELONG(p
, 0); /* no value */
1113 WRITELONG(p
, 0); /* no size either */
1114 WRITESHORT(p
, 4); /* type FILE */
1115 WRITESHORT(p
, SHN_ABS
);
1116 saa_wbytes(s
, entry
, 16L);
1121 * Now some standard symbols defining the segments, for relocation
1124 for (i
= 1; i
<= nsects
+ 1; i
++) {
1126 WRITELONG(p
, 0); /* no symbol name */
1127 WRITELONG(p
, 0); /* offset zero */
1128 WRITELONG(p
, 0); /* size zero */
1129 WRITESHORT(p
, 3); /* local section-type thing */
1130 WRITESHORT(p
, (i
== 1 ? SHN_ABS
: i
- 1)); /* the section id */
1131 saa_wbytes(s
, entry
, 16L);
1137 * Now the other local symbols.
1140 while ((sym
= saa_rstruct(syms
))) {
1141 if (sym
->type
& SYM_GLOBAL
)
1144 WRITELONG(p
, sym
->strpos
);
1145 WRITELONG(p
, sym
->value
);
1146 WRITELONG(p
, sym
->size
);
1147 WRITECHAR(p
, sym
->type
); /* local non-typed thing */
1148 WRITECHAR(p
, sym
->other
);
1149 WRITESHORT(p
, sym
->section
);
1150 saa_wbytes(s
, entry
, 16L);
1156 * Now the global symbols.
1159 while ((sym
= saa_rstruct(syms
))) {
1160 if (!(sym
->type
& SYM_GLOBAL
))
1163 WRITELONG(p
, sym
->strpos
);
1164 WRITELONG(p
, sym
->value
);
1165 WRITELONG(p
, sym
->size
);
1166 WRITECHAR(p
, sym
->type
); /* global non-typed thing */
1167 WRITECHAR(p
, sym
->other
);
1168 WRITESHORT(p
, sym
->section
);
1169 saa_wbytes(s
, entry
, 16L);
1176 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1179 uint8_t *p
, entry
[8];
1188 int32_t sym
= r
->symbol
;
1190 if (sym
>= GLOBAL_TEMP_BASE
)
1191 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 3) + nlocals
;
1194 WRITELONG(p
, r
->address
);
1195 WRITELONG(p
, (sym
<< 8) + r
->type
);
1196 saa_wbytes(s
, entry
, 8L);
1205 static void elf_section_header(int name
, int type
, int flags
,
1206 void *data
, int is_saa
, int32_t datalen
,
1207 int link
, int info
, int align
, int eltsize
)
1209 elf_sects
[elf_nsect
].data
= data
;
1210 elf_sects
[elf_nsect
].len
= datalen
;
1211 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1214 fwriteint32_t((int32_t)name
, elffp
);
1215 fwriteint32_t((int32_t)type
, elffp
);
1216 fwriteint32_t((int32_t)flags
, elffp
);
1217 fwriteint32_t(0L, elffp
); /* no address, ever, in object files */
1218 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1219 fwriteint32_t(datalen
, elffp
);
1221 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1222 fwriteint32_t((int32_t)link
, elffp
);
1223 fwriteint32_t((int32_t)info
, elffp
);
1224 fwriteint32_t((int32_t)align
, elffp
);
1225 fwriteint32_t((int32_t)eltsize
, elffp
);
1228 static void elf_write_sections(void)
1231 for (i
= 0; i
< elf_nsect
; i
++)
1232 if (elf_sects
[i
].data
) {
1233 int32_t len
= elf_sects
[i
].len
;
1234 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1235 int32_t align
= reallen
- len
;
1236 if (elf_sects
[i
].is_saa
)
1237 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1239 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1240 fwrite(align_str
, align
, 1, elffp
);
1244 static void elf_sect_write(struct Section
*sect
,
1245 const uint8_t *data
, uint32_t len
)
1247 saa_wbytes(sect
->data
, data
, len
);
1251 static int32_t elf_segbase(int32_t segment
)
1256 static int elf_directive(char *directive
, char *value
, int pass
)
1264 static void elf_filename(char *inname
, char *outname
, efunc error
)
1266 strcpy(elf_module
, inname
);
1267 standard_extension(inname
, outname
, ".o", error
);
1270 static const char *elf_stdmac
[] = {
1271 "%define __SECT__ [section .text]",
1272 "%macro __NASM_CDecl__ 1",
1277 static int elf_set_info(enum geninfo type
, char **val
)
1284 static struct dfmt df_stabs
= {
1285 "ELF32 (i386) stabs debug format for Linux",
1296 struct dfmt
*elf32_debugs_arr
[2] = { &df_stabs
, NULL
};
1298 struct ofmt of_elf32
= {
1299 "ELF32 (i386) object files (e.g. Linux)",
1316 struct ofmt of_elf
= {
1317 "ELF (short name for ELF32) ",
1333 /* again, the stabs debugging stuff (code) */
1335 void stabs32_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1343 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1346 if (!stabs_filename
) {
1347 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1348 strcpy(stabs_filename
, filename
);
1350 if (strcmp(stabs_filename
, filename
)) {
1351 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1352 in fact, this leak comes in quite handy to maintain a list of files
1353 encountered so far in the symbol lines... */
1355 /* why not nasm_free(stabs_filename); we're done with the old one */
1357 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1358 strcpy(stabs_filename
, filename
);
1362 currentline
= linenumber
;
1365 void stabs32_deflabel(char *name
, int32_t segment
, int32_t offset
, int is_global
,
1375 void stabs32_directive(const char *directive
, const char *params
)
1381 void stabs32_typevalue(int32_t type
)
1386 void stabs32_output(int type
, void *param
)
1388 struct symlininfo
*s
;
1389 struct linelist
*el
;
1390 if (type
== TY_STABSSYMLIN
) {
1391 if (stabs_immcall
) {
1392 s
= (struct symlininfo
*)param
;
1393 if (strcmp(s
->name
, ".text"))
1394 return; /* we are only interested in the text stuff */
1396 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1397 el
->info
.offset
= s
->offset
;
1398 el
->info
.section
= s
->section
;
1399 el
->info
.name
= s
->name
;
1400 el
->line
= currentline
;
1401 el
->filename
= stabs_filename
;
1404 stabslines
->last
->next
= el
;
1405 stabslines
->last
= el
;
1408 stabslines
->last
= el
;
1415 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1417 WRITELONG(p,n_strx); \
1418 WRITECHAR(p,n_type); \
1419 WRITECHAR(p,n_other); \
1420 WRITESHORT(p,n_desc); \
1421 WRITELONG(p,n_value); \
1424 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1426 void stabs32_generate(void)
1428 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1429 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1433 struct linelist
*ptr
;
1437 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1438 for (i
= 0; i
< numlinestabs
; i
++)
1442 if (numfiles
== 0) {
1443 allfiles
[0] = ptr
->filename
;
1446 for (i
= 0; i
< numfiles
; i
++) {
1447 if (!strcmp(allfiles
[i
], ptr
->filename
))
1450 if (i
>= numfiles
) {
1451 allfiles
[i
] = ptr
->filename
;
1458 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1459 for (i
= 0; i
< numfiles
; i
++) {
1460 fileidx
[i
] = strsize
;
1461 strsize
+= strlen(allfiles
[i
]) + 1;
1464 for (i
= 0; i
< numfiles
; i
++) {
1465 if (!strcmp(allfiles
[i
], elf_module
)) {
1471 /* worst case size of the stab buffer would be:
1472 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1475 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1476 sizeof(struct stabentry
));
1478 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1480 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1483 for (i
= 0; i
< numfiles
; i
++) {
1484 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1488 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1491 /* this is the first stab, its strx points to the filename of the
1492 the source-file, the n_desc field should be set to the number
1495 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1501 /* this is the stab for the main source file */
1502 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1504 /* relocation table entry */
1506 /* Since the above WRITE_STAB calls have already */
1507 /* created two entries, the index in the info.section */
1508 /* member must be adjusted by adding 3 */
1510 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1511 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1514 currfile
= mainfileindex
;
1518 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1519 /* oops file has changed... */
1520 for (i
= 0; i
< numfiles
; i
++)
1521 if (!strcmp(allfiles
[i
], ptr
->filename
))
1524 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1528 /* relocation table entry */
1529 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1530 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1533 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1536 /* relocation table entry */
1538 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1539 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1545 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1547 nasm_free(allfiles
);
1550 stablen
= (sptr
- sbuf
);
1551 stabrellen
= (rptr
- rbuf
);
1557 void stabs32_cleanup()
1559 struct linelist
*ptr
, *del
;
1571 nasm_free(stabrelbuf
);
1573 nasm_free(stabstrbuf
);