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.
23 long address
; /* relative to _start_ of section */
24 long symbol
; /* ELF symbol info thingy */
25 int relative
; /* TRUE or FALSE */
29 long strpos
; /* string table position of name */
30 long section
; /* section ID of the symbol */
31 int type
; /* TRUE or FALSE */
32 long value
; /* address, or COMMON variable size */
35 #define SHT_PROGBITS 1
40 #define SHF_EXECINSTR 4
44 unsigned long len
, size
, nrelocs
;
46 int type
; /* SHT_PROGBITS or SHT_NOBITS */
47 int align
; /* alignment: power of two */
48 unsigned long flags
; /* section flags */
52 struct Reloc
*head
, **tail
;
56 static struct Section
**sects
;
57 static int nsects
, sectlen
;
59 #define SHSTR_DELTA 256
60 static char *shstrtab
;
61 static int shstrtablen
, shstrtabsize
;
63 static struct SAA
*syms
;
64 static unsigned long nlocals
, nglobs
;
68 static struct RAA
*bsym
;
70 static struct SAA
*strs
;
71 static unsigned long strslen
;
76 static char elf_module
[FILENAME_MAX
];
78 #define SHN_ABS 0xFFF1
79 #define SHN_COMMON 0xFFF2
82 #define SYM_SECTION 0x04
83 #define SYM_GLOBAL 0x10
85 #define GLOBAL_TEMP_BASE 6 /* bigger than any constant sym id */
87 #define SEG_ALIGN 16 /* alignment of sections in file */
88 #define SEG_ALIGN_1 (SEG_ALIGN-1)
90 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
92 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
93 static struct ELF_SECTDATA
{
97 } elf_sects
[ELF_MAX_SECTIONS
];
99 static long elf_foffs
;
101 static void elf_write(void);
102 static void elf_sect_write(struct Section
*, unsigned char *, unsigned long);
103 static void elf_section_header (int, int, int, void *, int, long,
105 static void elf_write_sections (void);
106 static struct SAA
*elf_build_symtab (long *, long *);
107 static struct SAA
*elf_build_reltab (long *, struct Reloc
*);
108 static void add_sectname (char *, char *);
110 static void elf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
) {
113 (void) ldef
; /* placate optimisers */
115 nsects
= sectlen
= 0;
116 syms
= saa_init((long)sizeof(struct Symbol
));
117 nlocals
= nglobs
= 0;
120 saa_wbytes (strs
, "\0", 1L);
121 saa_wbytes (strs
, elf_module
, (long)(strlen(elf_module
)+1));
122 strslen
= 2+strlen(elf_module
);
124 shstrtablen
= shstrtabsize
= 0;;
125 add_sectname ("", "");
126 def_seg
= seg_alloc();
129 static void elf_cleanup(void) {
135 for (i
=0; i
<nsects
; i
++) {
136 if (sects
[i
]->type
!= SHT_NOBITS
)
137 saa_free (sects
[i
]->data
);
139 saa_free (sects
[i
]->rel
);
140 while (sects
[i
]->head
) {
142 sects
[i
]->head
= sects
[i
]->head
->next
;
152 static void add_sectname (char *firsthalf
, char *secondhalf
) {
153 int len
= strlen(firsthalf
)+strlen(secondhalf
);
154 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
155 shstrtab
= nasm_realloc (shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
156 strcpy (shstrtab
+shstrtablen
, firsthalf
);
157 strcat (shstrtab
+shstrtablen
, secondhalf
);
158 shstrtablen
+= len
+1;
161 static int elf_make_section (char *name
, int type
, int flags
, int align
) {
164 s
= nasm_malloc (sizeof(*s
));
166 if (type
!= SHT_NOBITS
)
167 s
->data
= saa_init (1L);
170 s
->len
= s
->size
= 0;
172 if (!strcmp(name
, ".text"))
175 s
->index
= seg_alloc();
176 add_sectname ("", name
);
177 s
->name
= nasm_malloc (1+strlen(name
));
178 strcpy (s
->name
, name
);
183 if (nsects
>= sectlen
)
184 sects
= nasm_realloc (sects
, (sectlen
+= SECT_DELTA
)*sizeof(*sects
));
190 static long elf_section_names (char *name
, int pass
, int *bits
) {
192 int flags_and
, flags_or
, type
, align
, i
;
195 * Default is 32 bits.
204 while (*p
&& !isspace(*p
)) p
++;
206 flags_and
= flags_or
= type
= align
= 0;
208 while (*p
&& isspace(*p
)) p
++;
211 while (*p
&& !isspace(*p
)) p
++;
213 while (*p
&& isspace(*p
)) p
++;
215 if (!nasm_strnicmp(q
, "align=", 6)) {
219 if ( (align
-1) & align
) { /* means it's not a power of two */
220 error (ERR_NONFATAL
, "section alignment %d is not"
221 " a power of two", align
);
224 } else if (!nasm_stricmp(q
, "alloc")) {
225 flags_and
|= SHF_ALLOC
;
226 flags_or
|= SHF_ALLOC
;
227 } else if (!nasm_stricmp(q
, "noalloc")) {
228 flags_and
|= SHF_ALLOC
;
229 flags_or
&= ~SHF_ALLOC
;
230 } else if (!nasm_stricmp(q
, "exec")) {
231 flags_and
|= SHF_EXECINSTR
;
232 flags_or
|= SHF_EXECINSTR
;
233 } else if (!nasm_stricmp(q
, "noexec")) {
234 flags_and
|= SHF_EXECINSTR
;
235 flags_or
&= ~SHF_EXECINSTR
;
236 } else if (!nasm_stricmp(q
, "write")) {
237 flags_and
|= SHF_WRITE
;
238 flags_or
|= SHF_WRITE
;
239 } else if (!nasm_stricmp(q
, "nowrite")) {
240 flags_and
|= SHF_WRITE
;
241 flags_or
&= ~SHF_WRITE
;
242 } else if (!nasm_stricmp(q
, "progbits")) {
244 } else if (!nasm_stricmp(q
, "nobits")) {
249 if (!strcmp(name
, ".comment") ||
250 !strcmp(name
, ".shstrtab") ||
251 !strcmp(name
, ".symtab") ||
252 !strcmp(name
, ".strtab")) {
253 error (ERR_NONFATAL
, "attempt to redefine reserved section"
258 for (i
=0; i
<nsects
; i
++)
259 if (!strcmp(name
, sects
[i
]->name
))
262 if (!strcmp(name
, ".text"))
263 i
= elf_make_section (name
, SHT_PROGBITS
,
264 SHF_ALLOC
| SHF_EXECINSTR
, 16);
265 else if (!strcmp(name
, ".data"))
266 i
= elf_make_section (name
, SHT_PROGBITS
,
267 SHF_ALLOC
| SHF_WRITE
, 4);
268 else if (!strcmp(name
, ".bss"))
269 i
= elf_make_section (name
, SHT_NOBITS
,
270 SHF_ALLOC
| SHF_WRITE
, 4);
272 i
= elf_make_section (name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
274 sects
[i
]->type
= type
;
276 sects
[i
]->align
= align
;
277 sects
[i
]->flags
&= ~flags_and
;
278 sects
[i
]->flags
|= flags_or
;
279 } else if (pass
== 1) {
280 if (type
|| align
|| flags_and
)
281 error (ERR_WARNING
, "section attributes ignored on"
282 " redeclaration of section `%s'", name
);
285 return sects
[i
]->index
;
288 static void elf_deflabel (char *name
, long segment
, long offset
,
293 if (name
[0] == '.' && name
[1] == '.') {
297 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
298 strslen
+= 1+strlen(name
);
300 sym
= saa_wstruct (syms
);
303 sym
->type
= is_global
? SYM_GLOBAL
: 0;
304 if (segment
== NO_SEG
)
305 sym
->section
= SHN_ABS
;
308 sym
->section
= SHN_UNDEF
;
309 for (i
=0; i
<nsects
; i
++)
310 if (segment
== sects
[i
]->index
) {
316 if (is_global
== 2) {
318 sym
->section
= SHN_COMMON
;
320 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
322 if (sym
->type
== SYM_GLOBAL
) {
323 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
324 bsym
= raa_write (bsym
, segment
, nglobs
);
330 static void elf_add_reloc (struct Section
*sect
, long segment
,
334 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
335 sect
->tail
= &r
->next
;
338 r
->address
= sect
->len
;
339 if (segment
== NO_SEG
)
344 for (i
=0; i
<nsects
; i
++)
345 if (segment
== sects
[i
]->index
)
348 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
350 r
->relative
= relative
;
355 static void elf_out (long segto
, void *data
, unsigned long type
,
356 long segment
, long wrt
) {
358 long realbytes
= type
& OUT_SIZMASK
;
359 unsigned char mydata
[4], *p
;
363 wrt
= NO_SEG
; /* continue to do _something_ */
364 error (ERR_NONFATAL
, "WRT not supported by ELF output format");
370 * handle absolute-assembly (structure definitions)
372 if (segto
== NO_SEG
) {
373 if (type
!= OUT_RESERVE
)
374 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
380 for (i
=0; i
<nsects
; i
++)
381 if (segto
== sects
[i
]->index
) {
386 int tempint
; /* ignored */
387 if (segto
!= elf_section_names (".text", 2, &tempint
))
388 error (ERR_PANIC
, "strange segment conditions in ELF driver");
393 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
394 error(ERR_WARNING
, "attempt to initialise memory in"
395 " BSS section `%s': ignored", s
->name
);
396 if (type
== OUT_REL2ADR
)
398 else if (type
== OUT_REL4ADR
)
404 if (type
== OUT_RESERVE
) {
405 if (s
->type
== SHT_PROGBITS
) {
406 error(ERR_WARNING
, "uninitialised space declared in"
407 " non-BSS section `%s': zeroing", s
->name
);
408 elf_sect_write (s
, NULL
, realbytes
);
411 } else if (type
== OUT_RAWDATA
) {
412 if (segment
!= NO_SEG
)
413 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
414 elf_sect_write (s
, data
, realbytes
);
415 } else if (type
== OUT_ADDRESS
) {
417 error(ERR_NONFATAL
, "ELF format does not support WRT types");
418 if (segment
!= NO_SEG
) {
420 error(ERR_NONFATAL
, "ELF format does not support"
421 " segment base references");
423 elf_add_reloc (s
, segment
, FALSE
);
426 if (realbytes
== 2 && segment
!= NO_SEG
)
427 error (ERR_NONFATAL
, "ELF format does not support 16-bit"
429 WRITELONG (p
, *(long *)data
);
430 elf_sect_write (s
, mydata
, realbytes
);
431 } else if (type
== OUT_REL2ADR
) {
432 error (ERR_NONFATAL
, "ELF format does not support 16-bit"
434 } else if (type
== OUT_REL4ADR
) {
435 if (segment
== segto
)
436 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
437 if (segment
!= NO_SEG
&& segment
% 2) {
438 error(ERR_NONFATAL
, "ELF format does not support"
439 " segment base references");
441 elf_add_reloc (s
, segment
, TRUE
);
443 WRITELONG (p
, *(long*)data
- realbytes
);
444 elf_sect_write (s
, mydata
, 4L);
448 static void elf_write(void) {
449 int nsections
, align
;
456 long symtablen
, symtablocal
;
459 * Work out how many sections we will have. We have SHN_UNDEF,
460 * then the flexible user sections, then the four fixed
461 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
462 * then optionally relocation sections for the user sections.
464 nsections
= 5; /* SHN_UNDEF and the fixed ones */
465 add_sectname ("", ".comment");
466 add_sectname ("", ".shstrtab");
467 add_sectname ("", ".symtab");
468 add_sectname ("", ".strtab");
469 for (i
=0; i
<nsects
; i
++) {
470 nsections
++; /* for the section itself */
471 if (sects
[i
]->head
) {
472 nsections
++; /* for its relocations */
473 add_sectname (".rel", sects
[i
]->name
);
481 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
484 * Output the ELF header.
486 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
487 fwriteshort (1, elffp
); /* ET_REL relocatable file */
488 fwriteshort (3, elffp
); /* EM_386 processor ID */
489 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
490 fwritelong (0L, elffp
); /* no entry point */
491 fwritelong (0L, elffp
); /* no program header table */
492 fwritelong (0x40L
, elffp
); /* section headers straight after
493 * ELF header plus alignment */
494 fwritelong (0L, elffp
); /* 386 defines no special flags */
495 fwriteshort (0x34, elffp
); /* size of ELF header */
496 fwriteshort (0, elffp
); /* no program header table, again */
497 fwriteshort (0, elffp
); /* still no program header table */
498 fwriteshort (0x28, elffp
); /* size of section header */
499 fwriteshort (nsections
, elffp
); /* number of sections */
500 fwriteshort (nsects
+2, elffp
); /* string table section index for
501 * section header table */
502 fwritelong (0L, elffp
); /* align to 0x40 bytes */
503 fwritelong (0L, elffp
);
504 fwritelong (0L, elffp
);
507 * Build the symbol table and relocation tables.
509 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
510 for (i
=0; i
<nsects
; i
++)
512 sects
[i
]->rel
= elf_build_reltab (§s
[i
]->rellen
,
516 * Now output the section header table.
519 elf_foffs
= 0x40 + 0x28 * nsections
;
520 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
524 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
526 for (i
=0; i
<nsects
; i
++) {
527 elf_section_header (p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
528 (sects
[i
]->type
== SHT_PROGBITS
?
529 sects
[i
]->data
: NULL
), TRUE
,
530 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
533 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
534 (long)commlen
, 0, 0, 1, 0);/* .comment */
536 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
537 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
539 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
540 symtablen
, nsects
+4, symtablocal
, 4, 16);/* .symtab */
542 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
543 strslen
, 0, 0, 1, 0); /* .strtab */
544 for (i
=0; i
<nsects
; i
++) if (sects
[i
]->head
) {
546 elf_section_header (p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
547 sects
[i
]->rellen
, nsects
+3, i
+1, 4, 8);
550 fwrite (align_str
, align
, 1, elffp
);
553 * Now output the sections.
555 elf_write_sections();
560 static struct SAA
*elf_build_symtab (long *len
, long *local
) {
561 struct SAA
*s
= saa_init(1L);
563 unsigned char entry
[16], *p
;
569 * First, an all-zeros entry, required by the ELF spec.
571 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
576 * Next, an entry for the file name.
579 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
580 WRITELONG (p
, 0); /* no value */
581 WRITELONG (p
, 0); /* no size either */
582 WRITESHORT (p
, 4); /* type FILE */
583 WRITESHORT (p
, SHN_ABS
);
584 saa_wbytes (s
, entry
, 16L);
589 * Now some standard symbols defining the segments, for relocation
592 for (i
= 1; i
<= nsects
+1; i
++) {
594 WRITELONG (p
, 0); /* no symbol name */
595 WRITELONG (p
, 0); /* offset zero */
596 WRITELONG (p
, 0); /* size zero */
597 WRITESHORT (p
, 3); /* local section-type thing */
598 WRITESHORT (p
, (i
==1 ? SHN_ABS
: i
-1)); /* the section id */
599 saa_wbytes (s
, entry
, 16L);
605 * Now the other local symbols.
608 while ( (sym
= saa_rstruct (syms
)) ) {
609 if (sym
->type
== SYM_GLOBAL
)
612 WRITELONG (p
, sym
->strpos
);
613 WRITELONG (p
, sym
->value
);
614 if (sym
->section
== SHN_COMMON
)
615 WRITELONG (p
, sym
->value
);
618 WRITESHORT (p
, 0); /* local non-typed thing */
619 WRITESHORT (p
, sym
->section
);
620 saa_wbytes (s
, entry
, 16L);
626 * Now the global symbols.
629 while ( (sym
= saa_rstruct (syms
)) ) {
630 if (sym
->type
!= SYM_GLOBAL
)
633 WRITELONG (p
, sym
->strpos
);
634 WRITELONG (p
, sym
->value
);
635 if (sym
->section
== SHN_COMMON
)
636 WRITELONG (p
, sym
->value
);
639 WRITESHORT (p
, SYM_GLOBAL
); /* global non-typed thing */
640 WRITESHORT (p
, sym
->section
);
641 saa_wbytes (s
, entry
, 16L);
648 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
650 unsigned char *p
, entry
[8];
659 long sym
= r
->symbol
;
661 if (sym
>= GLOBAL_TEMP_BASE
)
662 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+3) + nlocals
;
665 WRITELONG (p
, r
->address
);
666 WRITELONG (p
, (sym
<< 8) + (r
->relative
? 2 : 1));
667 saa_wbytes (s
, entry
, 8L);
676 static void elf_section_header (int name
, int type
, int flags
,
677 void *data
, int is_saa
, long datalen
,
678 int link
, int info
, int align
, int eltsize
) {
679 elf_sects
[elf_nsect
].data
= data
;
680 elf_sects
[elf_nsect
].len
= datalen
;
681 elf_sects
[elf_nsect
].is_saa
= is_saa
;
684 fwritelong ((long)name
, elffp
);
685 fwritelong ((long)type
, elffp
);
686 fwritelong ((long)flags
, elffp
);
687 fwritelong (0L, elffp
); /* no address, ever, in object files */
688 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
689 fwritelong (datalen
, elffp
);
691 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
692 fwritelong ((long)link
, elffp
);
693 fwritelong ((long)info
, elffp
);
694 fwritelong ((long)align
, elffp
);
695 fwritelong ((long)eltsize
, elffp
);
698 static void elf_write_sections (void) {
700 for (i
= 0; i
< elf_nsect
; i
++)
701 if (elf_sects
[i
].data
) {
702 long len
= elf_sects
[i
].len
;
703 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
704 long align
= reallen
- len
;
705 if (elf_sects
[i
].is_saa
)
706 saa_fpwrite (elf_sects
[i
].data
, elffp
);
708 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
709 fwrite (align_str
, align
, 1, elffp
);
713 static void elf_sect_write (struct Section
*sect
,
714 unsigned char *data
, unsigned long len
) {
715 saa_wbytes (sect
->data
, data
, len
);
719 static long elf_segbase (long segment
) {
723 static int elf_directive (char *directive
, char *value
, int pass
) {
727 static void elf_filename (char *inname
, char *outname
, efunc error
) {
728 strcpy(elf_module
, inname
);
729 standard_extension (inname
, outname
, ".o", error
);
732 struct ofmt of_elf
= {
733 "ELF32 (i386) object files (e.g. Linux)",