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] == '.' && name
[2] != '@') {
294 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
298 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
299 strslen
+= 1+strlen(name
);
301 sym
= saa_wstruct (syms
);
304 sym
->type
= is_global
? SYM_GLOBAL
: 0;
305 if (segment
== NO_SEG
)
306 sym
->section
= SHN_ABS
;
309 sym
->section
= SHN_UNDEF
;
310 if (nsects
== 0 && segment
== def_seg
) {
312 if (segment
!= elf_section_names (".text", 2, &tempint
))
313 error (ERR_PANIC
, "strange segment conditions in ELF driver");
314 sym
->section
= nsects
;
316 for (i
=0; i
<nsects
; i
++)
317 if (segment
== sects
[i
]->index
) {
324 if (is_global
== 2) {
326 sym
->section
= SHN_COMMON
;
328 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
330 if (sym
->type
== SYM_GLOBAL
) {
331 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
332 bsym
= raa_write (bsym
, segment
, nglobs
);
338 static void elf_add_reloc (struct Section
*sect
, long segment
,
342 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
343 sect
->tail
= &r
->next
;
346 r
->address
= sect
->len
;
347 if (segment
== NO_SEG
)
352 for (i
=0; i
<nsects
; i
++)
353 if (segment
== sects
[i
]->index
)
356 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
358 r
->relative
= relative
;
363 static void elf_out (long segto
, void *data
, unsigned long type
,
364 long segment
, long wrt
) {
366 long realbytes
= type
& OUT_SIZMASK
;
367 unsigned char mydata
[4], *p
;
371 wrt
= NO_SEG
; /* continue to do _something_ */
372 error (ERR_NONFATAL
, "WRT not supported by ELF output format");
378 * handle absolute-assembly (structure definitions)
380 if (segto
== NO_SEG
) {
381 if (type
!= OUT_RESERVE
)
382 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
388 for (i
=0; i
<nsects
; i
++)
389 if (segto
== sects
[i
]->index
) {
394 int tempint
; /* ignored */
395 if (segto
!= elf_section_names (".text", 2, &tempint
))
396 error (ERR_PANIC
, "strange segment conditions in ELF driver");
401 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
402 error(ERR_WARNING
, "attempt to initialise memory in"
403 " BSS section `%s': ignored", s
->name
);
404 if (type
== OUT_REL2ADR
)
406 else if (type
== OUT_REL4ADR
)
412 if (type
== OUT_RESERVE
) {
413 if (s
->type
== SHT_PROGBITS
) {
414 error(ERR_WARNING
, "uninitialised space declared in"
415 " non-BSS section `%s': zeroing", s
->name
);
416 elf_sect_write (s
, NULL
, realbytes
);
419 } else if (type
== OUT_RAWDATA
) {
420 if (segment
!= NO_SEG
)
421 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
422 elf_sect_write (s
, data
, realbytes
);
423 } else if (type
== OUT_ADDRESS
) {
425 error(ERR_NONFATAL
, "ELF format does not support WRT types");
426 if (segment
!= NO_SEG
) {
428 error(ERR_NONFATAL
, "ELF format does not support"
429 " segment base references");
431 elf_add_reloc (s
, segment
, FALSE
);
434 if (realbytes
== 2 && segment
!= NO_SEG
)
435 error (ERR_NONFATAL
, "ELF format does not support 16-bit"
437 WRITELONG (p
, *(long *)data
);
438 elf_sect_write (s
, mydata
, realbytes
);
439 } else if (type
== OUT_REL2ADR
) {
440 error (ERR_NONFATAL
, "ELF format does not support 16-bit"
442 } else if (type
== OUT_REL4ADR
) {
443 if (segment
== segto
)
444 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
445 if (segment
!= NO_SEG
&& segment
% 2) {
446 error(ERR_NONFATAL
, "ELF format does not support"
447 " segment base references");
449 elf_add_reloc (s
, segment
, TRUE
);
451 WRITELONG (p
, *(long*)data
- realbytes
);
452 elf_sect_write (s
, mydata
, 4L);
456 static void elf_write(void) {
457 int nsections
, align
;
464 long symtablen
, symtablocal
;
467 * Work out how many sections we will have. We have SHN_UNDEF,
468 * then the flexible user sections, then the four fixed
469 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
470 * then optionally relocation sections for the user sections.
472 nsections
= 5; /* SHN_UNDEF and the fixed ones */
473 add_sectname ("", ".comment");
474 add_sectname ("", ".shstrtab");
475 add_sectname ("", ".symtab");
476 add_sectname ("", ".strtab");
477 for (i
=0; i
<nsects
; i
++) {
478 nsections
++; /* for the section itself */
479 if (sects
[i
]->head
) {
480 nsections
++; /* for its relocations */
481 add_sectname (".rel", sects
[i
]->name
);
489 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
492 * Output the ELF header.
494 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
495 fwriteshort (1, elffp
); /* ET_REL relocatable file */
496 fwriteshort (3, elffp
); /* EM_386 processor ID */
497 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
498 fwritelong (0L, elffp
); /* no entry point */
499 fwritelong (0L, elffp
); /* no program header table */
500 fwritelong (0x40L
, elffp
); /* section headers straight after
501 * ELF header plus alignment */
502 fwritelong (0L, elffp
); /* 386 defines no special flags */
503 fwriteshort (0x34, elffp
); /* size of ELF header */
504 fwriteshort (0, elffp
); /* no program header table, again */
505 fwriteshort (0, elffp
); /* still no program header table */
506 fwriteshort (0x28, elffp
); /* size of section header */
507 fwriteshort (nsections
, elffp
); /* number of sections */
508 fwriteshort (nsects
+2, elffp
); /* string table section index for
509 * section header table */
510 fwritelong (0L, elffp
); /* align to 0x40 bytes */
511 fwritelong (0L, elffp
);
512 fwritelong (0L, elffp
);
515 * Build the symbol table and relocation tables.
517 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
518 for (i
=0; i
<nsects
; i
++)
520 sects
[i
]->rel
= elf_build_reltab (§s
[i
]->rellen
,
524 * Now output the section header table.
527 elf_foffs
= 0x40 + 0x28 * nsections
;
528 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
532 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
534 for (i
=0; i
<nsects
; i
++) {
535 elf_section_header (p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
536 (sects
[i
]->type
== SHT_PROGBITS
?
537 sects
[i
]->data
: NULL
), TRUE
,
538 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
541 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
542 (long)commlen
, 0, 0, 1, 0);/* .comment */
544 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
545 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
547 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
548 symtablen
, nsects
+4, symtablocal
, 4, 16);/* .symtab */
550 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
551 strslen
, 0, 0, 1, 0); /* .strtab */
552 for (i
=0; i
<nsects
; i
++) if (sects
[i
]->head
) {
554 elf_section_header (p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
555 sects
[i
]->rellen
, nsects
+3, i
+1, 4, 8);
558 fwrite (align_str
, align
, 1, elffp
);
561 * Now output the sections.
563 elf_write_sections();
568 static struct SAA
*elf_build_symtab (long *len
, long *local
) {
569 struct SAA
*s
= saa_init(1L);
571 unsigned char entry
[16], *p
;
577 * First, an all-zeros entry, required by the ELF spec.
579 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
584 * Next, an entry for the file name.
587 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
588 WRITELONG (p
, 0); /* no value */
589 WRITELONG (p
, 0); /* no size either */
590 WRITESHORT (p
, 4); /* type FILE */
591 WRITESHORT (p
, SHN_ABS
);
592 saa_wbytes (s
, entry
, 16L);
597 * Now some standard symbols defining the segments, for relocation
600 for (i
= 1; i
<= nsects
+1; i
++) {
602 WRITELONG (p
, 0); /* no symbol name */
603 WRITELONG (p
, 0); /* offset zero */
604 WRITELONG (p
, 0); /* size zero */
605 WRITESHORT (p
, 3); /* local section-type thing */
606 WRITESHORT (p
, (i
==1 ? SHN_ABS
: i
-1)); /* the section id */
607 saa_wbytes (s
, entry
, 16L);
613 * Now the other local symbols.
616 while ( (sym
= saa_rstruct (syms
)) ) {
617 if (sym
->type
== SYM_GLOBAL
)
620 WRITELONG (p
, sym
->strpos
);
621 WRITELONG (p
, sym
->value
);
622 if (sym
->section
== SHN_COMMON
)
623 WRITELONG (p
, sym
->value
);
626 WRITESHORT (p
, 0); /* local non-typed thing */
627 WRITESHORT (p
, sym
->section
);
628 saa_wbytes (s
, entry
, 16L);
634 * Now the global symbols.
637 while ( (sym
= saa_rstruct (syms
)) ) {
638 if (sym
->type
!= SYM_GLOBAL
)
641 WRITELONG (p
, sym
->strpos
);
642 WRITELONG (p
, sym
->value
);
643 if (sym
->section
== SHN_COMMON
)
644 WRITELONG (p
, sym
->value
);
647 WRITESHORT (p
, SYM_GLOBAL
); /* global non-typed thing */
648 WRITESHORT (p
, sym
->section
);
649 saa_wbytes (s
, entry
, 16L);
656 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
658 unsigned char *p
, entry
[8];
667 long sym
= r
->symbol
;
669 if (sym
>= GLOBAL_TEMP_BASE
)
670 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+3) + nlocals
;
673 WRITELONG (p
, r
->address
);
674 WRITELONG (p
, (sym
<< 8) + (r
->relative
? 2 : 1));
675 saa_wbytes (s
, entry
, 8L);
684 static void elf_section_header (int name
, int type
, int flags
,
685 void *data
, int is_saa
, long datalen
,
686 int link
, int info
, int align
, int eltsize
) {
687 elf_sects
[elf_nsect
].data
= data
;
688 elf_sects
[elf_nsect
].len
= datalen
;
689 elf_sects
[elf_nsect
].is_saa
= is_saa
;
692 fwritelong ((long)name
, elffp
);
693 fwritelong ((long)type
, elffp
);
694 fwritelong ((long)flags
, elffp
);
695 fwritelong (0L, elffp
); /* no address, ever, in object files */
696 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
697 fwritelong (datalen
, elffp
);
699 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
700 fwritelong ((long)link
, elffp
);
701 fwritelong ((long)info
, elffp
);
702 fwritelong ((long)align
, elffp
);
703 fwritelong ((long)eltsize
, elffp
);
706 static void elf_write_sections (void) {
708 for (i
= 0; i
< elf_nsect
; i
++)
709 if (elf_sects
[i
].data
) {
710 long len
= elf_sects
[i
].len
;
711 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
712 long align
= reallen
- len
;
713 if (elf_sects
[i
].is_saa
)
714 saa_fpwrite (elf_sects
[i
].data
, elffp
);
716 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
717 fwrite (align_str
, align
, 1, elffp
);
721 static void elf_sect_write (struct Section
*sect
,
722 unsigned char *data
, unsigned long len
) {
723 saa_wbytes (sect
->data
, data
, len
);
727 static long elf_segbase (long segment
) {
731 static int elf_directive (char *directive
, char *value
, int pass
) {
735 static void elf_filename (char *inname
, char *outname
, efunc error
) {
736 strcpy(elf_module
, inname
);
737 standard_extension (inname
, outname
, ".o", error
);
740 struct ofmt of_elf
= {
741 "ELF32 (i386) object files (e.g. Linux)",