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 */
37 unsigned long len
, size
, nrelocs
;
39 struct Reloc
*head
, **tail
;
42 static struct Section stext
, sdata
;
43 static unsigned long bsslen
;
46 static struct SAA
*syms
;
47 static unsigned long nlocals
, nglobs
;
49 static struct RAA
*bsym
;
51 static struct SAA
*strs
;
52 static unsigned long strslen
;
57 static char elf_module
[FILENAME_MAX
];
59 #define SHN_ABS 0xFFF1
60 #define SHN_COMMON 0xFFF2
63 #define SYM_SECTION 0x04
64 #define SYM_GLOBAL 0x10
66 #define GLOBAL_TEMP_BASE 6 /* bigger than any constant sym id */
68 #define SEG_ALIGN 16 /* alignment of sections in file */
69 #define SEG_ALIGN_1 (SEG_ALIGN-1)
71 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
73 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
74 static struct ELF_SECTDATA
{
78 } elf_sects
[ELF_MAX_SECTIONS
];
80 static long elf_foffs
;
82 static void elf_write(void);
83 static void elf_sect_write(struct Section
*, unsigned char *, unsigned long);
84 static void elf_section_header (int, int, int, void *, int, long,
86 static void elf_write_sections (void);
87 static struct SAA
*elf_build_symtab (long *, long *);
88 static struct SAA
*elf_build_reltab (long *, struct Reloc
*);
90 static void elf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
) {
93 (void) ldef
; /* placate optimisers */
94 stext
.data
= saa_init(1L); stext
.head
= NULL
; stext
.tail
= &stext
.head
;
95 sdata
.data
= saa_init(1L); sdata
.head
= NULL
; sdata
.tail
= &sdata
.head
;
96 stext
.len
= stext
.size
= sdata
.len
= sdata
.size
= bsslen
= 0;
97 stext
.nrelocs
= sdata
.nrelocs
= 0;
98 stext
.index
= seg_alloc();
99 sdata
.index
= seg_alloc();
100 bssindex
= seg_alloc();
101 syms
= saa_init((long)sizeof(struct Symbol
));
102 nlocals
= nglobs
= 0;
106 saa_wbytes (strs
, "\0", 1L);
107 saa_wbytes (strs
, elf_module
, (long)(strlen(elf_module
)+1));
108 strslen
= 2+strlen(elf_module
);
111 static void elf_cleanup(void) {
116 saa_free (stext
.data
);
119 stext
.head
= stext
.head
->next
;
122 saa_free (sdata
.data
);
125 sdata
.head
= sdata
.head
->next
;
133 static long elf_section_names (char *name
, int pass
, int *bits
) {
135 * Default is 32 bits.
143 if (!strcmp(name
, ".text"))
145 else if (!strcmp(name
, ".data"))
147 else if (!strcmp(name
, ".bss"))
153 static void elf_deflabel (char *name
, long segment
, long offset
,
158 if (name
[0] == '.' && name
[1] == '.') {
162 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
163 strslen
+= 1+strlen(name
);
165 sym
= saa_wstruct (syms
);
168 sym
->type
= is_global
? SYM_GLOBAL
: 0;
169 if (segment
== NO_SEG
)
170 sym
->section
= SHN_ABS
;
171 else if (segment
== stext
.index
)
173 else if (segment
== sdata
.index
)
175 else if (segment
== bssindex
)
178 sym
->section
= SHN_UNDEF
;
180 if (is_global
== 2) {
182 sym
->section
= SHN_COMMON
;
184 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
186 if (sym
->type
== SYM_GLOBAL
) {
187 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
188 bsym
= raa_write (bsym
, segment
, nglobs
);
194 static void elf_add_reloc (struct Section
*sect
, long segment
,
198 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
199 sect
->tail
= &r
->next
;
202 r
->address
= sect
->len
;
203 r
->symbol
= (segment
== NO_SEG
? 5 :
204 segment
== stext
.index
? 2 :
205 segment
== sdata
.index
? 3 :
206 segment
== bssindex
? 4 :
207 GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
));
208 r
->relative
= relative
;
213 static void elf_out (long segto
, void *data
, unsigned long type
,
214 long segment
, long wrt
) {
216 long realbytes
= type
& OUT_SIZMASK
;
217 unsigned char mydata
[4], *p
;
220 wrt
= NO_SEG
; /* continue to do _something_ */
221 error (ERR_NONFATAL
, "WRT not supported by ELF output format");
227 * handle absolute-assembly (structure definitions)
229 if (segto
== NO_SEG
) {
230 if (type
!= OUT_RESERVE
)
231 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
236 if (segto
== stext
.index
)
238 else if (segto
== sdata
.index
)
240 else if (segto
== bssindex
)
243 error(ERR_WARNING
, "attempt to assemble code in"
244 " segment %d: defaulting to `.text'", segto
);
248 if (!s
&& type
!= OUT_RESERVE
) {
249 error(ERR_WARNING
, "attempt to initialise memory in the"
250 " BSS section: ignored");
251 if (type
== OUT_REL2ADR
)
253 else if (type
== OUT_REL4ADR
)
259 if (type
== OUT_RESERVE
) {
261 error(ERR_WARNING
, "uninitialised space declared in"
262 " %s section: zeroing",
263 (segto
== stext
.index
? "code" : "data"));
264 elf_sect_write (s
, NULL
, realbytes
);
267 } else if (type
== OUT_RAWDATA
) {
268 if (segment
!= NO_SEG
)
269 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
270 elf_sect_write (s
, data
, realbytes
);
271 } else if (type
== OUT_ADDRESS
) {
273 error(ERR_NONFATAL
, "ELF format does not support WRT types");
274 if (segment
!= NO_SEG
) {
276 error(ERR_NONFATAL
, "ELF format does not support"
277 " segment base references");
279 elf_add_reloc (s
, segment
, FALSE
);
282 if (realbytes
== 2 && segment
!= NO_SEG
)
283 error (ERR_NONFATAL
, "ELF format does not support 16-bit"
285 WRITELONG (p
, *(long *)data
);
286 elf_sect_write (s
, mydata
, realbytes
);
287 } else if (type
== OUT_REL2ADR
) {
288 error (ERR_NONFATAL
, "ELF format does not support 16-bit"
290 } else if (type
== OUT_REL4ADR
) {
291 if (segment
== segto
)
292 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
293 if (segment
!= NO_SEG
&& segment
% 2) {
294 error(ERR_NONFATAL
, "ELF format does not support"
295 " segment base references");
297 elf_add_reloc (s
, segment
, TRUE
);
299 WRITELONG (p
, *(long*)data
- realbytes
);
300 elf_sect_write (s
, mydata
, 4L);
304 static void elf_write(void) {
305 int nsections
, align
;
306 char shstrtab
[80], *p
;
307 int shstrtablen
, commlen
;
310 struct SAA
*symtab
, *reltext
, *reldata
;
311 long symtablen
, symtablocal
, reltextlen
, reldatalen
;
314 * Work out how many sections we will have.
316 * Fixed sections are:
317 * SHN_UNDEF .text .data .bss .comment .shstrtab .symtab .strtab
319 * Optional sections are:
320 * .rel.text .rel.data
322 * (.rel.bss makes very little sense;-)
327 shstrtablen
+= 1+sprintf(shstrtab
+shstrtablen
, ".text");
328 shstrtablen
+= 1+sprintf(shstrtab
+shstrtablen
, ".data");
329 shstrtablen
+= 1+sprintf(shstrtab
+shstrtablen
, ".bss");
330 shstrtablen
+= 1+sprintf(shstrtab
+shstrtablen
, ".comment");
331 shstrtablen
+= 1+sprintf(shstrtab
+shstrtablen
, ".shstrtab");
332 shstrtablen
+= 1+sprintf(shstrtab
+shstrtablen
, ".symtab");
333 shstrtablen
+= 1+sprintf(shstrtab
+shstrtablen
, ".strtab");
336 shstrtablen
+= 1+sprintf(shstrtab
+shstrtablen
, ".rel.text");
340 shstrtablen
+= 1+sprintf(shstrtab
+shstrtablen
, ".rel.data");
347 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
350 * Output the ELF header.
352 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
353 fwriteshort (1, elffp
); /* ET_REL relocatable file */
354 fwriteshort (3, elffp
); /* EM_386 processor ID */
355 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
356 fwritelong (0L, elffp
); /* no entry point */
357 fwritelong (0L, elffp
); /* no program header table */
358 fwritelong (0x40L
, elffp
); /* section headers straight after
359 * ELF header plus alignment */
360 fwritelong (0L, elffp
); /* 386 defines no special flags */
361 fwriteshort (0x34, elffp
); /* size of ELF header */
362 fwriteshort (0, elffp
); /* no program header table, again */
363 fwriteshort (0, elffp
); /* still no program header table */
364 fwriteshort (0x28, elffp
); /* size of section header */
365 fwriteshort (nsections
, elffp
); /* number of sections */
366 fwriteshort (5, elffp
); /* string table section index for
367 * section header table */
368 fwritelong (0L, elffp
); /* align to 0x40 bytes */
369 fwritelong (0L, elffp
);
370 fwritelong (0L, elffp
);
373 * Build the symbol table and relocation tables.
375 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
376 reltext
= elf_build_reltab (&reltextlen
, stext
.head
);
377 reldata
= elf_build_reltab (&reldatalen
, sdata
.head
);
380 * Now output the section header table.
383 elf_foffs
= 0x40 + 0x28 * nsections
;
384 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
388 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
390 elf_section_header (p
- shstrtab
, 1, 6, stext
.data
, TRUE
,
391 stext
.len
, 0, 0, 16, 0); /* .text */
393 elf_section_header (p
- shstrtab
, 1, 3, sdata
.data
, TRUE
,
394 sdata
.len
, 0, 0, 4, 0); /* .data */
396 elf_section_header (p
- shstrtab
, 8, 3, NULL
, TRUE
,
397 bsslen
, 0, 0, 4, 0); /* .bss */
399 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
400 (long)commlen
, 0, 0, 1, 0);/* .comment */
402 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
403 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
405 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
406 symtablen
, 7, symtablocal
, 4, 16);/* .symtab */
408 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
409 strslen
, 0, 0, 1, 0); /* .strtab */
412 elf_section_header (p
- shstrtab
, 9, 0, reltext
, TRUE
,
413 reltextlen
, 6, 1, 4, 8); /* .rel.text */
417 elf_section_header (p
- shstrtab
, 9, 0, reldata
, TRUE
,
418 reldatalen
, 6, 2, 4, 8); /* .rel.data */
421 fwrite (align_str
, align
, 1, elffp
);
424 * Now output the sections.
426 elf_write_sections();
435 static struct SAA
*elf_build_symtab (long *len
, long *local
) {
436 struct SAA
*s
= saa_init(1L);
438 unsigned char entry
[16], *p
;
444 * First, an all-zeros entry, required by the ELF spec.
446 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
451 * Next, an entry for the file name.
454 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
455 WRITELONG (p
, 0); /* no value */
456 WRITELONG (p
, 0); /* no size either */
457 WRITESHORT (p
, 4); /* type FILE */
458 WRITESHORT (p
, SHN_ABS
);
459 saa_wbytes (s
, entry
, 16L);
464 * Now four standard symbols defining segments, for relocation
467 for (i
= 1; i
<= 4; i
++) {
469 WRITELONG (p
, 0); /* no symbol name */
470 WRITELONG (p
, 0); /* offset zero */
471 WRITELONG (p
, 0); /* size zero */
472 WRITESHORT (p
, 3); /* local section-type thing */
473 WRITESHORT (p
, (i
==4 ? SHN_ABS
: i
)); /* the section id */
474 saa_wbytes (s
, entry
, 16L);
480 * Now the other local symbols.
483 while ( (sym
= saa_rstruct (syms
)) ) {
484 if (sym
->type
== SYM_GLOBAL
)
487 WRITELONG (p
, sym
->strpos
);
488 WRITELONG (p
, sym
->value
);
489 if (sym
->section
== SHN_COMMON
)
490 WRITELONG (p
, sym
->value
);
493 WRITESHORT (p
, 0); /* local non-typed thing */
494 WRITESHORT (p
, sym
->section
);
495 saa_wbytes (s
, entry
, 16L);
501 * Now the global symbols.
504 while ( (sym
= saa_rstruct (syms
)) ) {
505 if (sym
->type
!= SYM_GLOBAL
)
508 WRITELONG (p
, sym
->strpos
);
509 WRITELONG (p
, sym
->value
);
510 if (sym
->section
== SHN_COMMON
)
511 WRITELONG (p
, sym
->value
);
514 WRITESHORT (p
, SYM_GLOBAL
); /* global non-typed thing */
515 WRITESHORT (p
, sym
->section
);
516 saa_wbytes (s
, entry
, 16L);
523 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
525 unsigned char *p
, entry
[8];
534 long sym
= r
->symbol
;
536 if (sym
>= GLOBAL_TEMP_BASE
)
537 sym
+= -GLOBAL_TEMP_BASE
+ 6 + nlocals
;
540 WRITELONG (p
, r
->address
);
541 WRITELONG (p
, (sym
<< 8) + (r
->relative
? 2 : 1));
542 saa_wbytes (s
, entry
, 8L);
551 static void elf_section_header (int name
, int type
, int flags
,
552 void *data
, int is_saa
, long datalen
,
553 int link
, int info
, int align
, int eltsize
) {
554 elf_sects
[elf_nsect
].data
= data
;
555 elf_sects
[elf_nsect
].len
= datalen
;
556 elf_sects
[elf_nsect
].is_saa
= is_saa
;
559 fwritelong ((long)name
, elffp
);
560 fwritelong ((long)type
, elffp
);
561 fwritelong ((long)flags
, elffp
);
562 fwritelong (0L, elffp
); /* no address, ever, in object files */
563 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
564 fwritelong (datalen
, elffp
);
566 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
567 fwritelong ((long)link
, elffp
);
568 fwritelong ((long)info
, elffp
);
569 fwritelong ((long)align
, elffp
);
570 fwritelong ((long)eltsize
, elffp
);
573 static void elf_write_sections (void) {
575 for (i
= 0; i
< elf_nsect
; i
++)
576 if (elf_sects
[i
].data
) {
577 long len
= elf_sects
[i
].len
;
578 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
579 long align
= reallen
- len
;
580 if (elf_sects
[i
].is_saa
)
581 saa_fpwrite (elf_sects
[i
].data
, elffp
);
583 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
584 fwrite (align_str
, align
, 1, elffp
);
588 static void elf_sect_write (struct Section
*sect
,
589 unsigned char *data
, unsigned long len
) {
590 saa_wbytes (sect
->data
, data
, len
);
594 static long elf_segbase (long segment
) {
598 static int elf_directive (char *directive
, char *value
, int pass
) {
602 static void elf_filename (char *inname
, char *outname
, efunc error
) {
603 strcpy(elf_module
, inname
);
604 standard_extension (inname
, outname
, ".o", error
);
607 struct ofmt of_elf
= {
608 "ELF32 (i386) object files (e.g. Linux)",