Move cleanup before work directory creation
[nasm/avx512.git] / output / outelf.c
blobddd87fe2ca01e7017e2d1d1220e8d0d7ca57ad56
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
22 * Relocation types.
24 enum reloc_type {
25 R_386_32 = 1, /* ordinary absolute relocation */
26 R_386_PC32 = 2, /* PC-relative relocation */
27 R_386_GOT32 = 3, /* an offset into GOT */
28 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
29 R_386_COPY = 5, /* ??? */
30 R_386_GLOB_DAT = 6, /* ??? */
31 R_386_JUMP_SLOT = 7, /* ??? */
32 R_386_RELATIVE = 8, /* ??? */
33 R_386_GOTOFF = 9, /* an offset from GOT base */
34 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
35 /* These are GNU extensions, but useful */
36 R_386_16 = 20, /* A 16-bit absolute relocation */
37 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
38 R_386_8 = 22, /* An 8-bit absolute relocation */
39 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
42 struct Reloc {
43 struct Reloc *next;
44 long address; /* relative to _start_ of section */
45 long symbol; /* ELF symbol info thingy */
46 int type; /* type of relocation */
49 struct Symbol {
50 long strpos; /* string table position of name */
51 long section; /* section ID of the symbol */
52 int type; /* symbol type */
53 long value; /* address, or COMMON variable align */
54 long size; /* size of symbol */
55 long globnum; /* symbol table offset if global */
56 struct Symbol *next; /* list of globals in each section */
57 struct Symbol *nextfwd; /* list of unresolved-size symbols */
58 char *name; /* used temporarily if in above list */
61 #define SHT_PROGBITS 1
62 #define SHT_NOBITS 8
64 #define SHF_WRITE 1
65 #define SHF_ALLOC 2
66 #define SHF_EXECINSTR 4
68 struct Section {
69 struct SAA *data;
70 unsigned long len, size, nrelocs;
71 long index;
72 int type; /* SHT_PROGBITS or SHT_NOBITS */
73 int align; /* alignment: power of two */
74 unsigned long flags; /* section flags */
75 char *name;
76 struct SAA *rel;
77 long rellen;
78 struct Reloc *head, **tail;
79 struct Symbol *gsyms; /* global symbols in section */
82 #define SECT_DELTA 32
83 static struct Section **sects;
84 static int nsects, sectlen;
86 #define SHSTR_DELTA 256
87 static char *shstrtab;
88 static int shstrtablen, shstrtabsize;
90 static struct SAA *syms;
91 static unsigned long nlocals, nglobs;
93 static long def_seg;
95 static struct RAA *bsym;
97 static struct SAA *strs;
98 static unsigned long strslen;
100 static FILE *elffp;
101 static efunc error;
102 static evalfunc evaluate;
104 static struct Symbol *fwds;
106 static char elf_module[FILENAME_MAX];
108 extern struct ofmt of_elf;
110 #define SHN_ABS 0xFFF1
111 #define SHN_COMMON 0xFFF2
112 #define SHN_UNDEF 0
114 #define SYM_SECTION 0x04
115 #define SYM_GLOBAL 0x10
116 #define SYM_DATA 0x01
117 #define SYM_FUNCTION 0x02
119 #define GLOBAL_TEMP_BASE 15 /* bigger than any constant sym id */
121 #define SEG_ALIGN 16 /* alignment of sections in file */
122 #define SEG_ALIGN_1 (SEG_ALIGN-1)
124 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
126 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
127 static struct ELF_SECTDATA {
128 void *data;
129 long len;
130 int is_saa;
131 } *elf_sects;
132 static int elf_nsect;
133 static long elf_foffs;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section *, unsigned char *, unsigned long);
137 static void elf_section_header (int, int, int, void *, int, long,
138 int, int, int, int);
139 static void elf_write_sections (void);
140 static struct SAA *elf_build_symtab (long *, long *);
141 static struct SAA *elf_build_reltab (long *, struct Reloc *);
142 static void add_sectname (char *, char *);
145 * Special section numbers which are used to define ELF special
146 * symbols, which can be used with WRT to provide PIC relocation
147 * types.
149 static long elf_gotpc_sect, elf_gotoff_sect;
150 static long elf_got_sect, elf_plt_sect;
151 static long elf_sym_sect;
153 static void elf_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
155 elffp = fp;
156 error = errfunc;
157 evaluate = eval;
158 (void) ldef; /* placate optimisers */
159 sects = NULL;
160 nsects = sectlen = 0;
161 syms = saa_init((long)sizeof(struct Symbol));
162 nlocals = nglobs = 0;
163 bsym = raa_init();
164 strs = saa_init(1L);
165 saa_wbytes (strs, "\0", 1L);
166 saa_wbytes (strs, elf_module, (long)(strlen(elf_module)+1));
167 strslen = 2+strlen(elf_module);
168 shstrtab = NULL;
169 shstrtablen = shstrtabsize = 0;;
170 add_sectname ("", "");
172 fwds = NULL;
174 elf_gotpc_sect = seg_alloc();
175 ldef("..gotpc", elf_gotpc_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
176 elf_gotoff_sect = seg_alloc();
177 ldef("..gotoff", elf_gotoff_sect+1, 0L, NULL, FALSE, FALSE,&of_elf,error);
178 elf_got_sect = seg_alloc();
179 ldef("..got", elf_got_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
180 elf_plt_sect = seg_alloc();
181 ldef("..plt", elf_plt_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
182 elf_sym_sect = seg_alloc();
183 ldef("..sym", elf_sym_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
185 def_seg = seg_alloc();
188 static void elf_cleanup(int debuginfo)
190 struct Reloc *r;
191 int i;
193 (void) debuginfo;
195 elf_write();
196 fclose (elffp);
197 for (i=0; i<nsects; i++) {
198 if (sects[i]->type != SHT_NOBITS)
199 saa_free (sects[i]->data);
200 if (sects[i]->head)
201 saa_free (sects[i]->rel);
202 while (sects[i]->head) {
203 r = sects[i]->head;
204 sects[i]->head = sects[i]->head->next;
205 nasm_free (r);
208 nasm_free (sects);
209 saa_free (syms);
210 raa_free (bsym);
211 saa_free (strs);
214 static void add_sectname (char *firsthalf, char *secondhalf)
216 int len = strlen(firsthalf)+strlen(secondhalf);
217 while (shstrtablen + len + 1 > shstrtabsize)
218 shstrtab = nasm_realloc (shstrtab, (shstrtabsize += SHSTR_DELTA));
219 strcpy (shstrtab+shstrtablen, firsthalf);
220 strcat (shstrtab+shstrtablen, secondhalf);
221 shstrtablen += len+1;
224 static int elf_make_section (char *name, int type, int flags, int align)
226 struct Section *s;
228 s = nasm_malloc (sizeof(*s));
230 if (type != SHT_NOBITS)
231 s->data = saa_init (1L);
232 s->head = NULL;
233 s->tail = &s->head;
234 s->len = s->size = 0;
235 s->nrelocs = 0;
236 if (!strcmp(name, ".text"))
237 s->index = def_seg;
238 else
239 s->index = seg_alloc();
240 add_sectname ("", name);
241 s->name = nasm_malloc (1+strlen(name));
242 strcpy (s->name, name);
243 s->type = type;
244 s->flags = flags;
245 s->align = align;
246 s->gsyms = NULL;
248 if (nsects >= sectlen)
249 sects = nasm_realloc (sects, (sectlen += SECT_DELTA)*sizeof(*sects));
250 sects[nsects++] = s;
252 return nsects-1;
255 static long elf_section_names (char *name, int pass, int *bits)
257 char *p;
258 int flags_and, flags_or, type, align, i;
261 * Default is 32 bits.
263 if (!name) {
264 *bits = 32;
265 return def_seg;
268 p = name;
269 while (*p && !isspace(*p)) p++;
270 if (*p) *p++ = '\0';
271 flags_and = flags_or = type = align = 0;
273 while (*p && isspace(*p)) p++;
274 while (*p) {
275 char *q = p;
276 while (*p && !isspace(*p)) p++;
277 if (*p) *p++ = '\0';
278 while (*p && isspace(*p)) p++;
280 if (!nasm_strnicmp(q, "align=", 6)) {
281 align = atoi(q+6);
282 if (align == 0)
283 align = 1;
284 if ( (align-1) & align ) { /* means it's not a power of two */
285 error (ERR_NONFATAL, "section alignment %d is not"
286 " a power of two", align);
287 align = 1;
289 } else if (!nasm_stricmp(q, "alloc")) {
290 flags_and |= SHF_ALLOC;
291 flags_or |= SHF_ALLOC;
292 } else if (!nasm_stricmp(q, "noalloc")) {
293 flags_and |= SHF_ALLOC;
294 flags_or &= ~SHF_ALLOC;
295 } else if (!nasm_stricmp(q, "exec")) {
296 flags_and |= SHF_EXECINSTR;
297 flags_or |= SHF_EXECINSTR;
298 } else if (!nasm_stricmp(q, "noexec")) {
299 flags_and |= SHF_EXECINSTR;
300 flags_or &= ~SHF_EXECINSTR;
301 } else if (!nasm_stricmp(q, "write")) {
302 flags_and |= SHF_WRITE;
303 flags_or |= SHF_WRITE;
304 } else if (!nasm_stricmp(q, "nowrite")) {
305 flags_and |= SHF_WRITE;
306 flags_or &= ~SHF_WRITE;
307 } else if (!nasm_stricmp(q, "progbits")) {
308 type = SHT_PROGBITS;
309 } else if (!nasm_stricmp(q, "nobits")) {
310 type = SHT_NOBITS;
314 if (!strcmp(name, ".comment") ||
315 !strcmp(name, ".shstrtab") ||
316 !strcmp(name, ".symtab") ||
317 !strcmp(name, ".strtab")) {
318 error (ERR_NONFATAL, "attempt to redefine reserved section"
319 "name `%s'", name);
320 return NO_SEG;
323 for (i=0; i<nsects; i++)
324 if (!strcmp(name, sects[i]->name))
325 break;
326 if (i == nsects) {
327 if (!strcmp(name, ".text"))
328 i = elf_make_section (name, SHT_PROGBITS,
329 SHF_ALLOC | SHF_EXECINSTR, 16);
330 else if (!strcmp(name, ".data"))
331 i = elf_make_section (name, SHT_PROGBITS,
332 SHF_ALLOC | SHF_WRITE, 4);
333 else if (!strcmp(name, ".bss"))
334 i = elf_make_section (name, SHT_NOBITS,
335 SHF_ALLOC | SHF_WRITE, 4);
336 else
337 i = elf_make_section (name, SHT_PROGBITS, SHF_ALLOC, 1);
338 if (type)
339 sects[i]->type = type;
340 if (align)
341 sects[i]->align = align;
342 sects[i]->flags &= ~flags_and;
343 sects[i]->flags |= flags_or;
344 } else if (pass == 1) {
345 if (type || align || flags_and)
346 error (ERR_WARNING, "section attributes ignored on"
347 " redeclaration of section `%s'", name);
350 return sects[i]->index;
353 static void elf_deflabel (char *name, long segment, long offset,
354 int is_global, char *special)
356 int pos = strslen;
357 struct Symbol *sym;
358 int special_used = FALSE;
360 #if defined(DEBUG) && DEBUG>2
361 fprintf(stderr, " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
362 name, segment, offset, is_global, special);
363 #endif
364 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
366 * This is a NASM special symbol. We never allow it into
367 * the ELF symbol table, even if it's a valid one. If it
368 * _isn't_ a valid one, we should barf immediately.
370 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
371 strcmp(name, "..got") && strcmp(name, "..plt") &&
372 strcmp(name, "..sym"))
373 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
374 return;
377 if (is_global == 3) {
378 struct Symbol **s;
380 * Fix up a forward-reference symbol size from the first
381 * pass.
383 for (s = &fwds; *s; s = &(*s)->nextfwd)
384 if (!strcmp((*s)->name, name)) {
385 struct tokenval tokval;
386 expr *e;
387 char *p = special;
389 while (*p && !isspace(*p)) p++;
390 while (*p && isspace(*p)) p++;
391 stdscan_reset();
392 stdscan_bufptr = p;
393 tokval.t_type = TOKEN_INVALID;
394 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
395 if (e) {
396 if (!is_simple(e))
397 error (ERR_NONFATAL, "cannot use relocatable"
398 " expression as symbol size");
399 else
400 (*s)->size = reloc_value(e);
404 * Remove it from the list of unresolved sizes.
406 nasm_free ((*s)->name);
407 *s = (*s)->nextfwd;
408 return;
410 return; /* it wasn't an important one */
413 saa_wbytes (strs, name, (long)(1+strlen(name)));
414 strslen += 1+strlen(name);
416 sym = saa_wstruct (syms);
418 sym->strpos = pos;
419 sym->type = is_global ? SYM_GLOBAL : 0;
420 sym->size = 0;
421 if (segment == NO_SEG)
422 sym->section = SHN_ABS;
423 else {
424 int i;
425 sym->section = SHN_UNDEF;
426 if (nsects == 0 && segment == def_seg) {
427 int tempint;
428 if (segment != elf_section_names (".text", 2, &tempint))
429 error (ERR_PANIC, "strange segment conditions in ELF driver");
430 sym->section = nsects;
431 } else {
432 for (i=0; i<nsects; i++)
433 if (segment == sects[i]->index) {
434 sym->section = i+1;
435 break;
440 if (is_global == 2) {
441 sym->size = offset;
442 sym->value = 0;
443 sym->section = SHN_COMMON;
445 * We have a common variable. Check the special text to see
446 * if it's a valid number and power of two; if so, store it
447 * as the alignment for the common variable.
449 if (special) {
450 int err;
451 sym->value = readnum (special, &err);
452 if (err)
453 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
454 " valid number", special);
455 else if ( (sym->value | (sym->value-1)) != 2*sym->value - 1)
456 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
457 " power of two", special);
459 special_used = TRUE;
460 } else
461 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
463 if (sym->type == SYM_GLOBAL) {
465 * There's a problem here that needs fixing.
466 * If sym->section == SHN_ABS, then the first line of the
467 * else section causes a core dump, because its a reference
468 * beyond the end of the section array.
469 * This behaviour is exhibited by this code:
470 * GLOBAL crash_nasm
471 * crash_nasm equ 0
473 * I'm not sure how to procede, because I haven't got the
474 * first clue about how ELF works, so I don't know what to
475 * do with it. Furthermore, I'm not sure what the rest of this
476 * section of code does. Help?
478 * For now, I'll see if doing absolutely nothing with it will
479 * work...
481 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON)
483 bsym = raa_write (bsym, segment, nglobs);
485 else if (sym->section != SHN_ABS)
488 * This is a global symbol; so we must add it to the linked
489 * list of global symbols in its section. We'll push it on
490 * the beginning of the list, because it doesn't matter
491 * much which end we put it on and it's easier like this.
493 * In addition, we check the special text for symbol
494 * type and size information.
496 sym->next = sects[sym->section-1]->gsyms;
497 sects[sym->section-1]->gsyms = sym;
499 if (special) {
500 int n = strcspn(special, " ");
502 if (!nasm_strnicmp(special, "function", n))
503 sym->type |= SYM_FUNCTION;
504 else if (!nasm_strnicmp(special, "data", n) ||
505 !nasm_strnicmp(special, "object", n))
506 sym->type |= SYM_DATA;
507 else
508 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
509 n, special);
510 if (special[n]) {
511 struct tokenval tokval;
512 expr *e;
513 int fwd = FALSE;
514 char *saveme=stdscan_bufptr; /* bugfix? fbk 8/10/00 */
516 while (special[n] && isspace(special[n]))
517 n++;
519 * We have a size expression; attempt to
520 * evaluate it.
522 stdscan_reset();
523 stdscan_bufptr = special+n;
524 tokval.t_type = TOKEN_INVALID;
525 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error, NULL);
526 if (fwd) {
527 sym->nextfwd = fwds;
528 fwds = sym;
529 sym->name = nasm_strdup(name);
530 } else if (e) {
531 if (!is_simple(e))
532 error (ERR_NONFATAL, "cannot use relocatable"
533 " expression as symbol size");
534 else
535 sym->size = reloc_value(e);
537 stdscan_bufptr=saveme; /* bugfix? fbk 8/10/00 */
539 special_used = TRUE;
542 sym->globnum = nglobs;
543 nglobs++;
544 } else
545 nlocals++;
547 if (special && !special_used)
548 error(ERR_NONFATAL, "no special symbol features supported here");
551 static void elf_add_reloc (struct Section *sect, long segment,
552 int type)
554 struct Reloc *r;
556 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
557 sect->tail = &r->next;
558 r->next = NULL;
560 r->address = sect->len;
561 if (segment == NO_SEG)
562 r->symbol = 2;
563 else {
564 int i;
565 r->symbol = 0;
566 for (i=0; i<nsects; i++)
567 if (segment == sects[i]->index)
568 r->symbol = i+3;
569 if (!r->symbol)
570 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
572 r->type = type;
574 sect->nrelocs++;
578 * This routine deals with ..got and ..sym relocations: the more
579 * complicated kinds. In shared-library writing, some relocations
580 * with respect to global symbols must refer to the precise symbol
581 * rather than referring to an offset from the base of the section
582 * _containing_ the symbol. Such relocations call to this routine,
583 * which searches the symbol list for the symbol in question.
585 * R_386_GOT32 references require the _exact_ symbol address to be
586 * used; R_386_32 references can be at an offset from the symbol.
587 * The boolean argument `exact' tells us this.
589 * Return value is the adjusted value of `addr', having become an
590 * offset from the symbol rather than the section. Should always be
591 * zero when returning from an exact call.
593 * Limitation: if you define two symbols at the same place,
594 * confusion will occur.
596 * Inefficiency: we search, currently, using a linked list which
597 * isn't even necessarily sorted.
599 static long elf_add_gsym_reloc (struct Section *sect,
600 long segment, long offset,
601 int type, int exact)
603 struct Reloc *r;
604 struct Section *s;
605 struct Symbol *sym, *sm;
606 int i;
609 * First look up the segment/offset pair and find a global
610 * symbol corresponding to it. If it's not one of our segments,
611 * then it must be an external symbol, in which case we're fine
612 * doing a normal elf_add_reloc after first sanity-checking
613 * that the offset from the symbol is zero.
615 s = NULL;
616 for (i=0; i<nsects; i++)
617 if (segment == sects[i]->index) {
618 s = sects[i];
619 break;
621 if (!s) {
622 if (exact && offset != 0)
623 error (ERR_NONFATAL, "unable to find a suitable global symbol"
624 " for this reference");
625 else
626 elf_add_reloc (sect, segment, type);
627 return offset;
630 if (exact) {
632 * Find a symbol pointing _exactly_ at this one.
634 for (sym = s->gsyms; sym; sym = sym->next)
635 if (sym->value == offset)
636 break;
637 } else {
639 * Find the nearest symbol below this one.
641 sym = NULL;
642 for (sm = s->gsyms; sm; sm = sm->next)
643 if (sm->value <= offset && (!sym || sm->value > sym->value))
644 sym = sm;
646 if (!sym && exact) {
647 error (ERR_NONFATAL, "unable to find a suitable global symbol"
648 " for this reference");
649 return 0;
652 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
653 sect->tail = &r->next;
654 r->next = NULL;
656 r->address = sect->len;
657 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
658 r->type = type;
660 sect->nrelocs++;
662 return offset - sym->value;
665 static void elf_out (long segto, void *data, unsigned long type,
666 long segment, long wrt)
668 struct Section *s;
669 long realbytes = type & OUT_SIZMASK;
670 long addr;
671 unsigned char mydata[4], *p;
672 int i;
674 type &= OUT_TYPMASK;
677 * handle absolute-assembly (structure definitions)
679 if (segto == NO_SEG) {
680 if (type != OUT_RESERVE)
681 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
682 " space");
683 return;
686 s = NULL;
687 for (i=0; i<nsects; i++)
688 if (segto == sects[i]->index) {
689 s = sects[i];
690 break;
692 if (!s) {
693 int tempint; /* ignored */
694 if (segto != elf_section_names (".text", 2, &tempint))
695 error (ERR_PANIC, "strange segment conditions in ELF driver");
696 else
697 s = sects[nsects-1];
700 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
701 error(ERR_WARNING, "attempt to initialise memory in"
702 " BSS section `%s': ignored", s->name);
703 if (type == OUT_REL2ADR)
704 realbytes = 2;
705 else if (type == OUT_REL4ADR)
706 realbytes = 4;
707 s->len += realbytes;
708 return;
711 if (type == OUT_RESERVE) {
712 if (s->type == SHT_PROGBITS) {
713 error(ERR_WARNING, "uninitialised space declared in"
714 " non-BSS section `%s': zeroing", s->name);
715 elf_sect_write (s, NULL, realbytes);
716 } else
717 s->len += realbytes;
718 } else if (type == OUT_RAWDATA) {
719 if (segment != NO_SEG)
720 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
721 elf_sect_write (s, data, realbytes);
722 } else if (type == OUT_ADDRESS) {
723 int gnu16 = 0;
724 addr = *(long *)data;
725 if (segment != NO_SEG) {
726 if (segment % 2) {
727 error(ERR_NONFATAL, "ELF format does not support"
728 " segment base references");
729 } else {
730 if (wrt == NO_SEG) {
731 if ( realbytes == 2 ) {
732 gnu16 = 1;
733 elf_add_reloc (s, segment, R_386_16);
734 } else {
735 elf_add_reloc (s, segment, R_386_32);
737 } else if (wrt == elf_gotpc_sect+1) {
739 * The user will supply GOT relative to $$. ELF
740 * will let us have GOT relative to $. So we
741 * need to fix up the data item by $-$$.
743 addr += s->len;
744 elf_add_reloc (s, segment, R_386_GOTPC);
745 } else if (wrt == elf_gotoff_sect+1) {
746 elf_add_reloc (s, segment, R_386_GOTOFF);
747 } else if (wrt == elf_got_sect+1) {
748 addr = elf_add_gsym_reloc (s, segment, addr,
749 R_386_GOT32, TRUE);
750 } else if (wrt == elf_sym_sect+1) {
751 if ( realbytes == 2 ) {
752 gnu16 = 1;
753 addr = elf_add_gsym_reloc (s, segment, addr,
754 R_386_16, FALSE);
755 } else {
756 addr = elf_add_gsym_reloc (s, segment, addr,
757 R_386_32, FALSE);
759 } else if (wrt == elf_plt_sect+1) {
760 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
761 "relative PLT references");
762 } else {
763 error (ERR_NONFATAL, "ELF format does not support this"
764 " use of WRT");
765 wrt = NO_SEG; /* we can at least _try_ to continue */
769 p = mydata;
770 if (gnu16) {
771 error(ERR_WARNING|ERR_WARN_GNUELF,
772 "16-bit relocations in ELF is a GNU extension");
773 WRITESHORT (p, addr);
774 } else {
775 if (realbytes != 4 && segment != NO_SEG) {
776 error (ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
778 WRITELONG (p, addr);
780 elf_sect_write (s, mydata, realbytes);
781 } else if (type == OUT_REL2ADR) {
782 if (segment == segto)
783 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
784 if (segment != NO_SEG && segment % 2) {
785 error(ERR_NONFATAL, "ELF format does not support"
786 " segment base references");
787 } else {
788 if (wrt == NO_SEG) {
789 error (ERR_WARNING|ERR_WARN_GNUELF,
790 "16-bit relocations in ELF is a GNU extension");
791 elf_add_reloc (s, segment, R_386_PC16);
792 } else {
793 error (ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
796 p = mydata;
797 WRITESHORT (p, *(long*)data - realbytes);
798 elf_sect_write (s, mydata, 2L);
799 } else if (type == OUT_REL4ADR) {
800 if (segment == segto)
801 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
802 if (segment != NO_SEG && segment % 2) {
803 error(ERR_NONFATAL, "ELF format does not support"
804 " segment base references");
805 } else {
806 if (wrt == NO_SEG) {
807 elf_add_reloc (s, segment, R_386_PC32);
808 } else if (wrt == elf_plt_sect+1) {
809 elf_add_reloc (s, segment, R_386_PLT32);
810 } else if (wrt == elf_gotpc_sect+1 ||
811 wrt == elf_gotoff_sect+1 ||
812 wrt == elf_got_sect+1) {
813 error(ERR_NONFATAL, "ELF format cannot produce PC-"
814 "relative GOT references");
815 } else {
816 error (ERR_NONFATAL, "ELF format does not support this"
817 " use of WRT");
818 wrt = NO_SEG; /* we can at least _try_ to continue */
821 p = mydata;
822 WRITELONG (p, *(long*)data - realbytes);
823 elf_sect_write (s, mydata, 4L);
827 static void elf_write(void)
829 int nsections, align;
830 char *p;
831 int commlen;
832 char comment[64];
833 int i;
835 struct SAA *symtab;
836 long symtablen, symtablocal;
839 * Work out how many sections we will have. We have SHN_UNDEF,
840 * then the flexible user sections, then the four fixed
841 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
842 * then optionally relocation sections for the user sections.
844 nsections = 5; /* SHN_UNDEF and the fixed ones */
845 add_sectname ("", ".comment");
846 add_sectname ("", ".shstrtab");
847 add_sectname ("", ".symtab");
848 add_sectname ("", ".strtab");
849 for (i=0; i<nsects; i++) {
850 nsections++; /* for the section itself */
851 if (sects[i]->head) {
852 nsections++; /* for its relocations */
853 add_sectname (".rel", sects[i]->name);
858 * Do the comment.
860 *comment = '\0';
861 commlen = 2+sprintf(comment+1, "The Netwide Assembler %s", NASM_VER);
864 * Output the ELF header.
866 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
867 fwriteshort (1, elffp); /* ET_REL relocatable file */
868 fwriteshort (3, elffp); /* EM_386 processor ID */
869 fwritelong (1L, elffp); /* EV_CURRENT file format version */
870 fwritelong (0L, elffp); /* no entry point */
871 fwritelong (0L, elffp); /* no program header table */
872 fwritelong (0x40L, elffp); /* section headers straight after
873 * ELF header plus alignment */
874 fwritelong (0L, elffp); /* 386 defines no special flags */
875 fwriteshort (0x34, elffp); /* size of ELF header */
876 fwriteshort (0, elffp); /* no program header table, again */
877 fwriteshort (0, elffp); /* still no program header table */
878 fwriteshort (0x28, elffp); /* size of section header */
879 fwriteshort (nsections, elffp); /* number of sections */
880 fwriteshort (nsects+2, elffp); /* string table section index for
881 * section header table */
882 fwritelong (0L, elffp); /* align to 0x40 bytes */
883 fwritelong (0L, elffp);
884 fwritelong (0L, elffp);
887 * Build the symbol table and relocation tables.
889 symtab = elf_build_symtab (&symtablen, &symtablocal);
890 for (i=0; i<nsects; i++)
891 if (sects[i]->head)
892 sects[i]->rel = elf_build_reltab (&sects[i]->rellen,
893 sects[i]->head);
896 * Now output the section header table.
899 elf_foffs = 0x40 + 0x28 * nsections;
900 align = ((elf_foffs+SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
901 elf_foffs += align;
902 elf_nsect = 0;
903 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
905 elf_section_header (0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
906 p = shstrtab+1;
907 for (i=0; i<nsects; i++) {
908 elf_section_header (p - shstrtab, sects[i]->type, sects[i]->flags,
909 (sects[i]->type == SHT_PROGBITS ?
910 sects[i]->data : NULL), TRUE,
911 sects[i]->len, 0, 0, sects[i]->align, 0);
912 p += strlen(p)+1;
914 elf_section_header (p - shstrtab, 1, 0, comment, FALSE,
915 (long)commlen, 0, 0, 1, 0);/* .comment */
916 p += strlen(p)+1;
917 elf_section_header (p - shstrtab, 3, 0, shstrtab, FALSE,
918 (long)shstrtablen, 0, 0, 1, 0);/* .shstrtab */
919 p += strlen(p)+1;
920 elf_section_header (p - shstrtab, 2, 0, symtab, TRUE,
921 symtablen, nsects+4, symtablocal, 4, 16);/* .symtab */
922 p += strlen(p)+1;
923 elf_section_header (p - shstrtab, 3, 0, strs, TRUE,
924 strslen, 0, 0, 1, 0); /* .strtab */
925 for (i=0; i<nsects; i++) if (sects[i]->head) {
926 p += strlen(p)+1;
927 elf_section_header (p - shstrtab, 9, 0, sects[i]->rel, TRUE,
928 sects[i]->rellen, nsects+3, i+1, 4, 8);
931 fwrite (align_str, align, 1, elffp);
934 * Now output the sections.
936 elf_write_sections();
938 nasm_free (elf_sects);
939 saa_free (symtab);
942 static struct SAA *elf_build_symtab (long *len, long *local)
944 struct SAA *s = saa_init(1L);
945 struct Symbol *sym;
946 unsigned char entry[16], *p;
947 int i;
949 *len = *local = 0;
952 * First, an all-zeros entry, required by the ELF spec.
954 saa_wbytes (s, NULL, 16L); /* null symbol table entry */
955 *len += 16;
956 (*local)++;
959 * Next, an entry for the file name.
961 p = entry;
962 WRITELONG (p, 1); /* we know it's 1st thing in strtab */
963 WRITELONG (p, 0); /* no value */
964 WRITELONG (p, 0); /* no size either */
965 WRITESHORT (p, 4); /* type FILE */
966 WRITESHORT (p, SHN_ABS);
967 saa_wbytes (s, entry, 16L);
968 *len += 16;
969 (*local)++;
972 * Now some standard symbols defining the segments, for relocation
973 * purposes.
975 for (i = 1; i <= nsects+1; i++) {
976 p = entry;
977 WRITELONG (p, 0); /* no symbol name */
978 WRITELONG (p, 0); /* offset zero */
979 WRITELONG (p, 0); /* size zero */
980 WRITESHORT (p, 3); /* local section-type thing */
981 WRITESHORT (p, (i==1 ? SHN_ABS : i-1)); /* the section id */
982 saa_wbytes (s, entry, 16L);
983 *len += 16;
984 (*local)++;
988 * Now the other local symbols.
990 saa_rewind (syms);
991 while ( (sym = saa_rstruct (syms)) ) {
992 if (sym->type & SYM_GLOBAL)
993 continue;
994 p = entry;
995 WRITELONG (p, sym->strpos);
996 WRITELONG (p, sym->value);
997 WRITELONG (p, sym->size);
998 WRITESHORT (p, sym->type); /* local non-typed thing */
999 WRITESHORT (p, sym->section);
1000 saa_wbytes (s, entry, 16L);
1001 *len += 16;
1002 (*local)++;
1006 * Now the global symbols.
1008 saa_rewind (syms);
1009 while ( (sym = saa_rstruct (syms)) ) {
1010 if (!(sym->type & SYM_GLOBAL))
1011 continue;
1012 p = entry;
1013 WRITELONG (p, sym->strpos);
1014 WRITELONG (p, sym->value);
1015 WRITELONG (p, sym->size);
1016 WRITESHORT (p, sym->type); /* global non-typed thing */
1017 WRITESHORT (p, sym->section);
1018 saa_wbytes (s, entry, 16L);
1019 *len += 16;
1022 return s;
1025 static struct SAA *elf_build_reltab (long *len, struct Reloc *r) {
1026 struct SAA *s;
1027 unsigned char *p, entry[8];
1029 if (!r)
1030 return NULL;
1032 s = saa_init(1L);
1033 *len = 0;
1035 while (r) {
1036 long sym = r->symbol;
1038 if (sym >= GLOBAL_TEMP_BASE)
1039 sym += -GLOBAL_TEMP_BASE + (nsects+3) + nlocals;
1041 p = entry;
1042 WRITELONG (p, r->address);
1043 WRITELONG (p, (sym << 8) + r->type);
1044 saa_wbytes (s, entry, 8L);
1045 *len += 8;
1047 r = r->next;
1050 return s;
1053 static void elf_section_header (int name, int type, int flags,
1054 void *data, int is_saa, long datalen,
1055 int link, int info, int align, int eltsize)
1057 elf_sects[elf_nsect].data = data;
1058 elf_sects[elf_nsect].len = datalen;
1059 elf_sects[elf_nsect].is_saa = is_saa;
1060 elf_nsect++;
1062 fwritelong ((long)name, elffp);
1063 fwritelong ((long)type, elffp);
1064 fwritelong ((long)flags, elffp);
1065 fwritelong (0L, elffp); /* no address, ever, in object files */
1066 fwritelong (type == 0 ? 0L : elf_foffs, elffp);
1067 fwritelong (datalen, elffp);
1068 if (data)
1069 elf_foffs += (datalen+SEG_ALIGN_1) & ~SEG_ALIGN_1;
1070 fwritelong ((long)link, elffp);
1071 fwritelong ((long)info, elffp);
1072 fwritelong ((long)align, elffp);
1073 fwritelong ((long)eltsize, elffp);
1076 static void elf_write_sections (void)
1078 int i;
1079 for (i = 0; i < elf_nsect; i++)
1080 if (elf_sects[i].data) {
1081 long len = elf_sects[i].len;
1082 long reallen = (len+SEG_ALIGN_1) & ~SEG_ALIGN_1;
1083 long align = reallen - len;
1084 if (elf_sects[i].is_saa)
1085 saa_fpwrite (elf_sects[i].data, elffp);
1086 else
1087 fwrite (elf_sects[i].data, len, 1, elffp);
1088 fwrite (align_str, align, 1, elffp);
1092 static void elf_sect_write (struct Section *sect,
1093 unsigned char *data, unsigned long len)
1095 saa_wbytes (sect->data, data, len);
1096 sect->len += len;
1099 static long elf_segbase (long segment)
1101 return segment;
1104 static int elf_directive (char *directive, char *value, int pass)
1106 return 0;
1109 static void elf_filename (char *inname, char *outname, efunc error)
1111 strcpy(elf_module, inname);
1112 standard_extension (inname, outname, ".o", error);
1115 static char *elf_stdmac[] = {
1116 "%define __SECT__ [section .text]",
1117 "%macro __NASM_CDecl__ 1",
1118 "%define $_%1 $%1",
1119 "%endmacro",
1120 NULL
1122 static int elf_set_info(enum geninfo type, char **val)
1124 return 0;
1127 struct ofmt of_elf = {
1128 "ELF32 (i386) object files (e.g. Linux)",
1129 "elf",
1130 NULL,
1131 null_debug_arr,
1132 &null_debug_form,
1133 elf_stdmac,
1134 elf_init,
1135 elf_set_info,
1136 elf_out,
1137 elf_deflabel,
1138 elf_section_names,
1139 elf_segbase,
1140 elf_directive,
1141 elf_filename,
1142 elf_cleanup
1145 #endif /* OF_ELF */