NASM 0.91
[nasm/avx512.git] / outelf.c
blobb84bae3aa197796259753d556ea9d6ef41c47a6c
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.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "outform.h"
19 #ifdef OF_ELF
21 struct Reloc {
22 struct Reloc *next;
23 long address; /* relative to _start_ of section */
24 long symbol; /* ELF symbol info thingy */
25 int relative; /* TRUE or FALSE */
28 struct Symbol {
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 struct Section {
36 struct SAA *data;
37 unsigned long len, size, nrelocs;
38 long index;
39 struct Reloc *head, **tail;
42 static struct Section stext, sdata;
43 static unsigned long bsslen;
44 static long bssindex;
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;
54 static FILE *elffp;
55 static efunc error;
57 static char elf_module[FILENAME_MAX];
59 #define SHN_ABS 0xFFF1
60 #define SHN_COMMON 0xFFF2
61 #define SHN_UNDEF 0
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 {
75 void *data;
76 long len;
77 int is_saa;
78 } elf_sects[ELF_MAX_SECTIONS];
79 static int elf_nsect;
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,
85 int, int, int, int);
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) {
91 elffp = fp;
92 error = errfunc;
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;
103 bsym = raa_init();
105 strs = saa_init(1L);
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) {
112 struct Reloc *r;
114 elf_write();
115 fclose (elffp);
116 saa_free (stext.data);
117 while (stext.head) {
118 r = stext.head;
119 stext.head = stext.head->next;
120 nasm_free (r);
122 saa_free (sdata.data);
123 while (sdata.head) {
124 r = sdata.head;
125 sdata.head = sdata.head->next;
126 nasm_free (r);
128 saa_free (syms);
129 raa_free (bsym);
130 saa_free (strs);
133 static long elf_section_names (char *name, int pass, int *bits) {
135 * Default is 32 bits.
137 if (!name)
138 *bits = 32;
140 if (!name)
141 return stext.index;
143 if (!strcmp(name, ".text"))
144 return stext.index;
145 else if (!strcmp(name, ".data"))
146 return sdata.index;
147 else if (!strcmp(name, ".bss"))
148 return bssindex;
149 else
150 return NO_SEG;
153 static void elf_deflabel (char *name, long segment, long offset,
154 int is_global) {
155 int pos = strslen;
156 struct Symbol *sym;
158 if (name[0] == '.' && name[1] == '.') {
159 return;
162 saa_wbytes (strs, name, (long)(1+strlen(name)));
163 strslen += 1+strlen(name);
165 sym = saa_wstruct (syms);
167 sym->strpos = pos;
168 sym->type = is_global ? SYM_GLOBAL : 0;
169 if (segment == NO_SEG)
170 sym->section = SHN_ABS;
171 else if (segment == stext.index)
172 sym->section = 1;
173 else if (segment == sdata.index)
174 sym->section = 2;
175 else if (segment == bssindex)
176 sym->section = 3;
177 else
178 sym->section = SHN_UNDEF;
180 if (is_global == 2) {
181 sym->value = offset;
182 sym->section = SHN_COMMON;
183 } else
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);
189 nglobs++;
190 } else
191 nlocals++;
194 static void elf_add_reloc (struct Section *sect, long segment,
195 int relative) {
196 struct Reloc *r;
198 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
199 sect->tail = &r->next;
200 r->next = NULL;
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;
210 sect->nrelocs++;
213 static void elf_out (long segto, void *data, unsigned long type,
214 long segment, long wrt) {
215 struct Section *s;
216 long realbytes = type & OUT_SIZMASK;
217 unsigned char mydata[4], *p;
219 if (wrt != NO_SEG) {
220 wrt = NO_SEG; /* continue to do _something_ */
221 error (ERR_NONFATAL, "WRT not supported by ELF output format");
224 type &= OUT_TYPMASK;
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]"
232 " space");
233 return;
236 if (segto == stext.index)
237 s = &stext;
238 else if (segto == sdata.index)
239 s = &sdata;
240 else if (segto == bssindex)
241 s = NULL;
242 else {
243 error(ERR_WARNING, "attempt to assemble code in"
244 " segment %d: defaulting to `.text'", segto);
245 s = &stext;
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)
252 realbytes = 2;
253 else if (type == OUT_REL4ADR)
254 realbytes = 4;
255 bsslen += realbytes;
256 return;
259 if (type == OUT_RESERVE) {
260 if (s) {
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);
265 } else
266 bsslen += 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) {
272 if (wrt != NO_SEG)
273 error(ERR_NONFATAL, "ELF format does not support WRT types");
274 if (segment != NO_SEG) {
275 if (segment % 2) {
276 error(ERR_NONFATAL, "ELF format does not support"
277 " segment base references");
278 } else
279 elf_add_reloc (s, segment, FALSE);
281 p = mydata;
282 if (realbytes == 2 && segment != NO_SEG)
283 error (ERR_NONFATAL, "ELF format does not support 16-bit"
284 " relocations");
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"
289 " relocations");
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");
296 } else
297 elf_add_reloc (s, segment, TRUE);
298 p = mydata;
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;
308 char comment[64];
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;-)
324 nsections = 8;
325 *shstrtab = '\0';
326 shstrtablen = 1;
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");
334 if (stext.head) {
335 nsections++;
336 shstrtablen += 1+sprintf(shstrtab+shstrtablen, ".rel.text");
338 if (sdata.head) {
339 nsections++;
340 shstrtablen += 1+sprintf(shstrtab+shstrtablen, ".rel.data");
344 * Do the comment.
346 *comment = '\0';
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;
385 elf_foffs += align;
386 elf_nsect = 0;
388 elf_section_header (0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
389 p = shstrtab+1;
390 elf_section_header (p - shstrtab, 1, 6, stext.data, TRUE,
391 stext.len, 0, 0, 16, 0); /* .text */
392 p += strlen(p)+1;
393 elf_section_header (p - shstrtab, 1, 3, sdata.data, TRUE,
394 sdata.len, 0, 0, 4, 0); /* .data */
395 p += strlen(p)+1;
396 elf_section_header (p - shstrtab, 8, 3, NULL, TRUE,
397 bsslen, 0, 0, 4, 0); /* .bss */
398 p += strlen(p)+1;
399 elf_section_header (p - shstrtab, 1, 0, comment, FALSE,
400 (long)commlen, 0, 0, 1, 0);/* .comment */
401 p += strlen(p)+1;
402 elf_section_header (p - shstrtab, 3, 0, shstrtab, FALSE,
403 (long)shstrtablen, 0, 0, 1, 0);/* .shstrtab */
404 p += strlen(p)+1;
405 elf_section_header (p - shstrtab, 2, 0, symtab, TRUE,
406 symtablen, 7, symtablocal, 4, 16);/* .symtab */
407 p += strlen(p)+1;
408 elf_section_header (p - shstrtab, 3, 0, strs, TRUE,
409 strslen, 0, 0, 1, 0); /* .strtab */
410 if (reltext) {
411 p += strlen(p)+1;
412 elf_section_header (p - shstrtab, 9, 0, reltext, TRUE,
413 reltextlen, 6, 1, 4, 8); /* .rel.text */
415 if (reldata) {
416 p += strlen(p)+1;
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();
428 saa_free (symtab);
429 if (reltext)
430 saa_free (reltext);
431 if (reldata)
432 saa_free (reldata);
435 static struct SAA *elf_build_symtab (long *len, long *local) {
436 struct SAA *s = saa_init(1L);
437 struct Symbol *sym;
438 unsigned char entry[16], *p;
439 int i;
441 *len = *local = 0;
444 * First, an all-zeros entry, required by the ELF spec.
446 saa_wbytes (s, NULL, 16L); /* null symbol table entry */
447 *len += 16;
448 (*local)++;
451 * Next, an entry for the file name.
453 p = entry;
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);
460 *len += 16;
461 (*local)++;
464 * Now four standard symbols defining segments, for relocation
465 * purposes.
467 for (i = 1; i <= 4; i++) {
468 p = entry;
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);
475 *len += 16;
476 (*local)++;
480 * Now the other local symbols.
482 saa_rewind (syms);
483 while ( (sym = saa_rstruct (syms)) ) {
484 if (sym->type == SYM_GLOBAL)
485 continue;
486 p = entry;
487 WRITELONG (p, sym->strpos);
488 WRITELONG (p, sym->value);
489 if (sym->section == SHN_COMMON)
490 WRITELONG (p, sym->value);
491 else
492 WRITELONG (p, 0);
493 WRITESHORT (p, 0); /* local non-typed thing */
494 WRITESHORT (p, sym->section);
495 saa_wbytes (s, entry, 16L);
496 *len += 16;
497 (*local)++;
501 * Now the global symbols.
503 saa_rewind (syms);
504 while ( (sym = saa_rstruct (syms)) ) {
505 if (sym->type != SYM_GLOBAL)
506 continue;
507 p = entry;
508 WRITELONG (p, sym->strpos);
509 WRITELONG (p, sym->value);
510 if (sym->section == SHN_COMMON)
511 WRITELONG (p, sym->value);
512 else
513 WRITELONG (p, 0);
514 WRITESHORT (p, SYM_GLOBAL); /* global non-typed thing */
515 WRITESHORT (p, sym->section);
516 saa_wbytes (s, entry, 16L);
517 *len += 16;
520 return s;
523 static struct SAA *elf_build_reltab (long *len, struct Reloc *r) {
524 struct SAA *s;
525 unsigned char *p, entry[8];
527 if (!r)
528 return NULL;
530 s = saa_init(1L);
531 *len = 0;
533 while (r) {
534 long sym = r->symbol;
536 if (sym >= GLOBAL_TEMP_BASE)
537 sym += -GLOBAL_TEMP_BASE + 6 + nlocals;
539 p = entry;
540 WRITELONG (p, r->address);
541 WRITELONG (p, (sym << 8) + (r->relative ? 2 : 1));
542 saa_wbytes (s, entry, 8L);
543 *len += 8;
545 r = r->next;
548 return s;
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;
557 elf_nsect++;
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);
565 if (data)
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) {
574 int i;
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);
582 else
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);
591 sect->len += len;
594 static long elf_segbase (long segment) {
595 return segment;
598 static int elf_directive (char *directive, char *value, int pass) {
599 return 0;
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)",
609 "elf",
610 elf_init,
611 elf_out,
612 elf_deflabel,
613 elf_section_names,
614 elf_segbase,
615 elf_directive,
616 elf_filename,
617 elf_cleanup
620 #endif /* OF_ELF */