Document new Cygwin compatibility
[nasm/avx512.git] / output / outelf.c
blob152e1d2ecdfea22cb711552386657776b9b04742
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, ".rodata"))
331 i = elf_make_section (name, SHT_PROGBITS,
332 SHF_ALLOC, 4);
333 else if (!strcmp(name, ".data"))
334 i = elf_make_section (name, SHT_PROGBITS,
335 SHF_ALLOC | SHF_WRITE, 4);
336 else if (!strcmp(name, ".bss"))
337 i = elf_make_section (name, SHT_NOBITS,
338 SHF_ALLOC | SHF_WRITE, 4);
339 else
340 i = elf_make_section (name, SHT_PROGBITS, SHF_ALLOC, 1);
341 if (type)
342 sects[i]->type = type;
343 if (align)
344 sects[i]->align = align;
345 sects[i]->flags &= ~flags_and;
346 sects[i]->flags |= flags_or;
347 } else if (pass == 1) {
348 if (type || align || flags_and)
349 error (ERR_WARNING, "section attributes ignored on"
350 " redeclaration of section `%s'", name);
353 return sects[i]->index;
356 static void elf_deflabel (char *name, long segment, long offset,
357 int is_global, char *special)
359 int pos = strslen;
360 struct Symbol *sym;
361 int special_used = FALSE;
363 #if defined(DEBUG) && DEBUG>2
364 fprintf(stderr, " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
365 name, segment, offset, is_global, special);
366 #endif
367 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
369 * This is a NASM special symbol. We never allow it into
370 * the ELF symbol table, even if it's a valid one. If it
371 * _isn't_ a valid one, we should barf immediately.
373 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
374 strcmp(name, "..got") && strcmp(name, "..plt") &&
375 strcmp(name, "..sym"))
376 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
377 return;
380 if (is_global == 3) {
381 struct Symbol **s;
383 * Fix up a forward-reference symbol size from the first
384 * pass.
386 for (s = &fwds; *s; s = &(*s)->nextfwd)
387 if (!strcmp((*s)->name, name)) {
388 struct tokenval tokval;
389 expr *e;
390 char *p = special;
392 while (*p && !isspace(*p)) p++;
393 while (*p && isspace(*p)) p++;
394 stdscan_reset();
395 stdscan_bufptr = p;
396 tokval.t_type = TOKEN_INVALID;
397 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
398 if (e) {
399 if (!is_simple(e))
400 error (ERR_NONFATAL, "cannot use relocatable"
401 " expression as symbol size");
402 else
403 (*s)->size = reloc_value(e);
407 * Remove it from the list of unresolved sizes.
409 nasm_free ((*s)->name);
410 *s = (*s)->nextfwd;
411 return;
413 return; /* it wasn't an important one */
416 saa_wbytes (strs, name, (long)(1+strlen(name)));
417 strslen += 1+strlen(name);
419 sym = saa_wstruct (syms);
421 sym->strpos = pos;
422 sym->type = is_global ? SYM_GLOBAL : 0;
423 sym->size = 0;
424 if (segment == NO_SEG)
425 sym->section = SHN_ABS;
426 else {
427 int i;
428 sym->section = SHN_UNDEF;
429 if (nsects == 0 && segment == def_seg) {
430 int tempint;
431 if (segment != elf_section_names (".text", 2, &tempint))
432 error (ERR_PANIC, "strange segment conditions in ELF driver");
433 sym->section = nsects;
434 } else {
435 for (i=0; i<nsects; i++)
436 if (segment == sects[i]->index) {
437 sym->section = i+1;
438 break;
443 if (is_global == 2) {
444 sym->size = offset;
445 sym->value = 0;
446 sym->section = SHN_COMMON;
448 * We have a common variable. Check the special text to see
449 * if it's a valid number and power of two; if so, store it
450 * as the alignment for the common variable.
452 if (special) {
453 int err;
454 sym->value = readnum (special, &err);
455 if (err)
456 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
457 " valid number", special);
458 else if ( (sym->value | (sym->value-1)) != 2*sym->value - 1)
459 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
460 " power of two", special);
462 special_used = TRUE;
463 } else
464 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
466 if (sym->type == SYM_GLOBAL) {
468 * There's a problem here that needs fixing.
469 * If sym->section == SHN_ABS, then the first line of the
470 * else section causes a core dump, because its a reference
471 * beyond the end of the section array.
472 * This behaviour is exhibited by this code:
473 * GLOBAL crash_nasm
474 * crash_nasm equ 0
476 * I'm not sure how to procede, because I haven't got the
477 * first clue about how ELF works, so I don't know what to
478 * do with it. Furthermore, I'm not sure what the rest of this
479 * section of code does. Help?
481 * For now, I'll see if doing absolutely nothing with it will
482 * work...
484 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON)
486 bsym = raa_write (bsym, segment, nglobs);
488 else if (sym->section != SHN_ABS)
491 * This is a global symbol; so we must add it to the linked
492 * list of global symbols in its section. We'll push it on
493 * the beginning of the list, because it doesn't matter
494 * much which end we put it on and it's easier like this.
496 * In addition, we check the special text for symbol
497 * type and size information.
499 sym->next = sects[sym->section-1]->gsyms;
500 sects[sym->section-1]->gsyms = sym;
502 if (special) {
503 int n = strcspn(special, " ");
505 if (!nasm_strnicmp(special, "function", n))
506 sym->type |= SYM_FUNCTION;
507 else if (!nasm_strnicmp(special, "data", n) ||
508 !nasm_strnicmp(special, "object", n))
509 sym->type |= SYM_DATA;
510 else
511 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
512 n, special);
513 if (special[n]) {
514 struct tokenval tokval;
515 expr *e;
516 int fwd = FALSE;
517 char *saveme=stdscan_bufptr; /* bugfix? fbk 8/10/00 */
519 while (special[n] && isspace(special[n]))
520 n++;
522 * We have a size expression; attempt to
523 * evaluate it.
525 stdscan_reset();
526 stdscan_bufptr = special+n;
527 tokval.t_type = TOKEN_INVALID;
528 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error, NULL);
529 if (fwd) {
530 sym->nextfwd = fwds;
531 fwds = sym;
532 sym->name = nasm_strdup(name);
533 } else if (e) {
534 if (!is_simple(e))
535 error (ERR_NONFATAL, "cannot use relocatable"
536 " expression as symbol size");
537 else
538 sym->size = reloc_value(e);
540 stdscan_bufptr=saveme; /* bugfix? fbk 8/10/00 */
542 special_used = TRUE;
545 sym->globnum = nglobs;
546 nglobs++;
547 } else
548 nlocals++;
550 if (special && !special_used)
551 error(ERR_NONFATAL, "no special symbol features supported here");
554 static void elf_add_reloc (struct Section *sect, long segment,
555 int type)
557 struct Reloc *r;
559 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
560 sect->tail = &r->next;
561 r->next = NULL;
563 r->address = sect->len;
564 if (segment == NO_SEG)
565 r->symbol = 2;
566 else {
567 int i;
568 r->symbol = 0;
569 for (i=0; i<nsects; i++)
570 if (segment == sects[i]->index)
571 r->symbol = i+3;
572 if (!r->symbol)
573 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
575 r->type = type;
577 sect->nrelocs++;
581 * This routine deals with ..got and ..sym relocations: the more
582 * complicated kinds. In shared-library writing, some relocations
583 * with respect to global symbols must refer to the precise symbol
584 * rather than referring to an offset from the base of the section
585 * _containing_ the symbol. Such relocations call to this routine,
586 * which searches the symbol list for the symbol in question.
588 * R_386_GOT32 references require the _exact_ symbol address to be
589 * used; R_386_32 references can be at an offset from the symbol.
590 * The boolean argument `exact' tells us this.
592 * Return value is the adjusted value of `addr', having become an
593 * offset from the symbol rather than the section. Should always be
594 * zero when returning from an exact call.
596 * Limitation: if you define two symbols at the same place,
597 * confusion will occur.
599 * Inefficiency: we search, currently, using a linked list which
600 * isn't even necessarily sorted.
602 static long elf_add_gsym_reloc (struct Section *sect,
603 long segment, long offset,
604 int type, int exact)
606 struct Reloc *r;
607 struct Section *s;
608 struct Symbol *sym, *sm;
609 int i;
612 * First look up the segment/offset pair and find a global
613 * symbol corresponding to it. If it's not one of our segments,
614 * then it must be an external symbol, in which case we're fine
615 * doing a normal elf_add_reloc after first sanity-checking
616 * that the offset from the symbol is zero.
618 s = NULL;
619 for (i=0; i<nsects; i++)
620 if (segment == sects[i]->index) {
621 s = sects[i];
622 break;
624 if (!s) {
625 if (exact && offset != 0)
626 error (ERR_NONFATAL, "unable to find a suitable global symbol"
627 " for this reference");
628 else
629 elf_add_reloc (sect, segment, type);
630 return offset;
633 if (exact) {
635 * Find a symbol pointing _exactly_ at this one.
637 for (sym = s->gsyms; sym; sym = sym->next)
638 if (sym->value == offset)
639 break;
640 } else {
642 * Find the nearest symbol below this one.
644 sym = NULL;
645 for (sm = s->gsyms; sm; sm = sm->next)
646 if (sm->value <= offset && (!sym || sm->value > sym->value))
647 sym = sm;
649 if (!sym && exact) {
650 error (ERR_NONFATAL, "unable to find a suitable global symbol"
651 " for this reference");
652 return 0;
655 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
656 sect->tail = &r->next;
657 r->next = NULL;
659 r->address = sect->len;
660 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
661 r->type = type;
663 sect->nrelocs++;
665 return offset - sym->value;
668 static void elf_out (long segto, void *data, unsigned long type,
669 long segment, long wrt)
671 struct Section *s;
672 long realbytes = type & OUT_SIZMASK;
673 long addr;
674 unsigned char mydata[4], *p;
675 int i;
677 type &= OUT_TYPMASK;
680 * handle absolute-assembly (structure definitions)
682 if (segto == NO_SEG) {
683 if (type != OUT_RESERVE)
684 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
685 " space");
686 return;
689 s = NULL;
690 for (i=0; i<nsects; i++)
691 if (segto == sects[i]->index) {
692 s = sects[i];
693 break;
695 if (!s) {
696 int tempint; /* ignored */
697 if (segto != elf_section_names (".text", 2, &tempint))
698 error (ERR_PANIC, "strange segment conditions in ELF driver");
699 else
700 s = sects[nsects-1];
703 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
704 error(ERR_WARNING, "attempt to initialise memory in"
705 " BSS section `%s': ignored", s->name);
706 if (type == OUT_REL2ADR)
707 realbytes = 2;
708 else if (type == OUT_REL4ADR)
709 realbytes = 4;
710 s->len += realbytes;
711 return;
714 if (type == OUT_RESERVE) {
715 if (s->type == SHT_PROGBITS) {
716 error(ERR_WARNING, "uninitialised space declared in"
717 " non-BSS section `%s': zeroing", s->name);
718 elf_sect_write (s, NULL, realbytes);
719 } else
720 s->len += realbytes;
721 } else if (type == OUT_RAWDATA) {
722 if (segment != NO_SEG)
723 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
724 elf_sect_write (s, data, realbytes);
725 } else if (type == OUT_ADDRESS) {
726 int gnu16 = 0;
727 addr = *(long *)data;
728 if (segment != NO_SEG) {
729 if (segment % 2) {
730 error(ERR_NONFATAL, "ELF format does not support"
731 " segment base references");
732 } else {
733 if (wrt == NO_SEG) {
734 if ( realbytes == 2 ) {
735 gnu16 = 1;
736 elf_add_reloc (s, segment, R_386_16);
737 } else {
738 elf_add_reloc (s, segment, R_386_32);
740 } else if (wrt == elf_gotpc_sect+1) {
742 * The user will supply GOT relative to $$. ELF
743 * will let us have GOT relative to $. So we
744 * need to fix up the data item by $-$$.
746 addr += s->len;
747 elf_add_reloc (s, segment, R_386_GOTPC);
748 } else if (wrt == elf_gotoff_sect+1) {
749 elf_add_reloc (s, segment, R_386_GOTOFF);
750 } else if (wrt == elf_got_sect+1) {
751 addr = elf_add_gsym_reloc (s, segment, addr,
752 R_386_GOT32, TRUE);
753 } else if (wrt == elf_sym_sect+1) {
754 if ( realbytes == 2 ) {
755 gnu16 = 1;
756 addr = elf_add_gsym_reloc (s, segment, addr,
757 R_386_16, FALSE);
758 } else {
759 addr = elf_add_gsym_reloc (s, segment, addr,
760 R_386_32, FALSE);
762 } else if (wrt == elf_plt_sect+1) {
763 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
764 "relative PLT references");
765 } else {
766 error (ERR_NONFATAL, "ELF format does not support this"
767 " use of WRT");
768 wrt = NO_SEG; /* we can at least _try_ to continue */
772 p = mydata;
773 if (gnu16) {
774 error(ERR_WARNING|ERR_WARN_GNUELF,
775 "16-bit relocations in ELF is a GNU extension");
776 WRITESHORT (p, addr);
777 } else {
778 if (realbytes != 4 && segment != NO_SEG) {
779 error (ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
781 WRITELONG (p, addr);
783 elf_sect_write (s, mydata, realbytes);
784 } else if (type == OUT_REL2ADR) {
785 if (segment == segto)
786 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
787 if (segment != NO_SEG && segment % 2) {
788 error(ERR_NONFATAL, "ELF format does not support"
789 " segment base references");
790 } else {
791 if (wrt == NO_SEG) {
792 error (ERR_WARNING|ERR_WARN_GNUELF,
793 "16-bit relocations in ELF is a GNU extension");
794 elf_add_reloc (s, segment, R_386_PC16);
795 } else {
796 error (ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
799 p = mydata;
800 WRITESHORT (p, *(long*)data - realbytes);
801 elf_sect_write (s, mydata, 2L);
802 } else if (type == OUT_REL4ADR) {
803 if (segment == segto)
804 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
805 if (segment != NO_SEG && segment % 2) {
806 error(ERR_NONFATAL, "ELF format does not support"
807 " segment base references");
808 } else {
809 if (wrt == NO_SEG) {
810 elf_add_reloc (s, segment, R_386_PC32);
811 } else if (wrt == elf_plt_sect+1) {
812 elf_add_reloc (s, segment, R_386_PLT32);
813 } else if (wrt == elf_gotpc_sect+1 ||
814 wrt == elf_gotoff_sect+1 ||
815 wrt == elf_got_sect+1) {
816 error(ERR_NONFATAL, "ELF format cannot produce PC-"
817 "relative GOT references");
818 } else {
819 error (ERR_NONFATAL, "ELF format does not support this"
820 " use of WRT");
821 wrt = NO_SEG; /* we can at least _try_ to continue */
824 p = mydata;
825 WRITELONG (p, *(long*)data - realbytes);
826 elf_sect_write (s, mydata, 4L);
830 static void elf_write(void)
832 int nsections, align;
833 char *p;
834 int commlen;
835 char comment[64];
836 int i;
838 struct SAA *symtab;
839 long symtablen, symtablocal;
842 * Work out how many sections we will have. We have SHN_UNDEF,
843 * then the flexible user sections, then the four fixed
844 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
845 * then optionally relocation sections for the user sections.
847 nsections = 5; /* SHN_UNDEF and the fixed ones */
848 add_sectname ("", ".comment");
849 add_sectname ("", ".shstrtab");
850 add_sectname ("", ".symtab");
851 add_sectname ("", ".strtab");
852 for (i=0; i<nsects; i++) {
853 nsections++; /* for the section itself */
854 if (sects[i]->head) {
855 nsections++; /* for its relocations */
856 add_sectname (".rel", sects[i]->name);
861 * Do the comment.
863 *comment = '\0';
864 commlen = 2+sprintf(comment+1, "The Netwide Assembler %s", NASM_VER);
867 * Output the ELF header.
869 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
870 fwriteshort (1, elffp); /* ET_REL relocatable file */
871 fwriteshort (3, elffp); /* EM_386 processor ID */
872 fwritelong (1L, elffp); /* EV_CURRENT file format version */
873 fwritelong (0L, elffp); /* no entry point */
874 fwritelong (0L, elffp); /* no program header table */
875 fwritelong (0x40L, elffp); /* section headers straight after
876 * ELF header plus alignment */
877 fwritelong (0L, elffp); /* 386 defines no special flags */
878 fwriteshort (0x34, elffp); /* size of ELF header */
879 fwriteshort (0, elffp); /* no program header table, again */
880 fwriteshort (0, elffp); /* still no program header table */
881 fwriteshort (0x28, elffp); /* size of section header */
882 fwriteshort (nsections, elffp); /* number of sections */
883 fwriteshort (nsects+2, elffp); /* string table section index for
884 * section header table */
885 fwritelong (0L, elffp); /* align to 0x40 bytes */
886 fwritelong (0L, elffp);
887 fwritelong (0L, elffp);
890 * Build the symbol table and relocation tables.
892 symtab = elf_build_symtab (&symtablen, &symtablocal);
893 for (i=0; i<nsects; i++)
894 if (sects[i]->head)
895 sects[i]->rel = elf_build_reltab (&sects[i]->rellen,
896 sects[i]->head);
899 * Now output the section header table.
902 elf_foffs = 0x40 + 0x28 * nsections;
903 align = ((elf_foffs+SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
904 elf_foffs += align;
905 elf_nsect = 0;
906 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
908 elf_section_header (0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
909 p = shstrtab+1;
910 for (i=0; i<nsects; i++) {
911 elf_section_header (p - shstrtab, sects[i]->type, sects[i]->flags,
912 (sects[i]->type == SHT_PROGBITS ?
913 sects[i]->data : NULL), TRUE,
914 sects[i]->len, 0, 0, sects[i]->align, 0);
915 p += strlen(p)+1;
917 elf_section_header (p - shstrtab, 1, 0, comment, FALSE,
918 (long)commlen, 0, 0, 1, 0);/* .comment */
919 p += strlen(p)+1;
920 elf_section_header (p - shstrtab, 3, 0, shstrtab, FALSE,
921 (long)shstrtablen, 0, 0, 1, 0);/* .shstrtab */
922 p += strlen(p)+1;
923 elf_section_header (p - shstrtab, 2, 0, symtab, TRUE,
924 symtablen, nsects+4, symtablocal, 4, 16);/* .symtab */
925 p += strlen(p)+1;
926 elf_section_header (p - shstrtab, 3, 0, strs, TRUE,
927 strslen, 0, 0, 1, 0); /* .strtab */
928 for (i=0; i<nsects; i++) if (sects[i]->head) {
929 p += strlen(p)+1;
930 elf_section_header (p - shstrtab, 9, 0, sects[i]->rel, TRUE,
931 sects[i]->rellen, nsects+3, i+1, 4, 8);
934 fwrite (align_str, align, 1, elffp);
937 * Now output the sections.
939 elf_write_sections();
941 nasm_free (elf_sects);
942 saa_free (symtab);
945 static struct SAA *elf_build_symtab (long *len, long *local)
947 struct SAA *s = saa_init(1L);
948 struct Symbol *sym;
949 unsigned char entry[16], *p;
950 int i;
952 *len = *local = 0;
955 * First, an all-zeros entry, required by the ELF spec.
957 saa_wbytes (s, NULL, 16L); /* null symbol table entry */
958 *len += 16;
959 (*local)++;
962 * Next, an entry for the file name.
964 p = entry;
965 WRITELONG (p, 1); /* we know it's 1st thing in strtab */
966 WRITELONG (p, 0); /* no value */
967 WRITELONG (p, 0); /* no size either */
968 WRITESHORT (p, 4); /* type FILE */
969 WRITESHORT (p, SHN_ABS);
970 saa_wbytes (s, entry, 16L);
971 *len += 16;
972 (*local)++;
975 * Now some standard symbols defining the segments, for relocation
976 * purposes.
978 for (i = 1; i <= nsects+1; i++) {
979 p = entry;
980 WRITELONG (p, 0); /* no symbol name */
981 WRITELONG (p, 0); /* offset zero */
982 WRITELONG (p, 0); /* size zero */
983 WRITESHORT (p, 3); /* local section-type thing */
984 WRITESHORT (p, (i==1 ? SHN_ABS : i-1)); /* the section id */
985 saa_wbytes (s, entry, 16L);
986 *len += 16;
987 (*local)++;
991 * Now the other local symbols.
993 saa_rewind (syms);
994 while ( (sym = saa_rstruct (syms)) ) {
995 if (sym->type & SYM_GLOBAL)
996 continue;
997 p = entry;
998 WRITELONG (p, sym->strpos);
999 WRITELONG (p, sym->value);
1000 WRITELONG (p, sym->size);
1001 WRITESHORT (p, sym->type); /* local non-typed thing */
1002 WRITESHORT (p, sym->section);
1003 saa_wbytes (s, entry, 16L);
1004 *len += 16;
1005 (*local)++;
1009 * Now the global symbols.
1011 saa_rewind (syms);
1012 while ( (sym = saa_rstruct (syms)) ) {
1013 if (!(sym->type & SYM_GLOBAL))
1014 continue;
1015 p = entry;
1016 WRITELONG (p, sym->strpos);
1017 WRITELONG (p, sym->value);
1018 WRITELONG (p, sym->size);
1019 WRITESHORT (p, sym->type); /* global non-typed thing */
1020 WRITESHORT (p, sym->section);
1021 saa_wbytes (s, entry, 16L);
1022 *len += 16;
1025 return s;
1028 static struct SAA *elf_build_reltab (long *len, struct Reloc *r) {
1029 struct SAA *s;
1030 unsigned char *p, entry[8];
1032 if (!r)
1033 return NULL;
1035 s = saa_init(1L);
1036 *len = 0;
1038 while (r) {
1039 long sym = r->symbol;
1041 if (sym >= GLOBAL_TEMP_BASE)
1042 sym += -GLOBAL_TEMP_BASE + (nsects+3) + nlocals;
1044 p = entry;
1045 WRITELONG (p, r->address);
1046 WRITELONG (p, (sym << 8) + r->type);
1047 saa_wbytes (s, entry, 8L);
1048 *len += 8;
1050 r = r->next;
1053 return s;
1056 static void elf_section_header (int name, int type, int flags,
1057 void *data, int is_saa, long datalen,
1058 int link, int info, int align, int eltsize)
1060 elf_sects[elf_nsect].data = data;
1061 elf_sects[elf_nsect].len = datalen;
1062 elf_sects[elf_nsect].is_saa = is_saa;
1063 elf_nsect++;
1065 fwritelong ((long)name, elffp);
1066 fwritelong ((long)type, elffp);
1067 fwritelong ((long)flags, elffp);
1068 fwritelong (0L, elffp); /* no address, ever, in object files */
1069 fwritelong (type == 0 ? 0L : elf_foffs, elffp);
1070 fwritelong (datalen, elffp);
1071 if (data)
1072 elf_foffs += (datalen+SEG_ALIGN_1) & ~SEG_ALIGN_1;
1073 fwritelong ((long)link, elffp);
1074 fwritelong ((long)info, elffp);
1075 fwritelong ((long)align, elffp);
1076 fwritelong ((long)eltsize, elffp);
1079 static void elf_write_sections (void)
1081 int i;
1082 for (i = 0; i < elf_nsect; i++)
1083 if (elf_sects[i].data) {
1084 long len = elf_sects[i].len;
1085 long reallen = (len+SEG_ALIGN_1) & ~SEG_ALIGN_1;
1086 long align = reallen - len;
1087 if (elf_sects[i].is_saa)
1088 saa_fpwrite (elf_sects[i].data, elffp);
1089 else
1090 fwrite (elf_sects[i].data, len, 1, elffp);
1091 fwrite (align_str, align, 1, elffp);
1095 static void elf_sect_write (struct Section *sect,
1096 unsigned char *data, unsigned long len)
1098 saa_wbytes (sect->data, data, len);
1099 sect->len += len;
1102 static long elf_segbase (long segment)
1104 return segment;
1107 static int elf_directive (char *directive, char *value, int pass)
1109 return 0;
1112 static void elf_filename (char *inname, char *outname, efunc error)
1114 strcpy(elf_module, inname);
1115 standard_extension (inname, outname, ".o", error);
1118 static const char *elf_stdmac[] = {
1119 "%define __SECT__ [section .text]",
1120 "%macro __NASM_CDecl__ 1",
1121 "%define $_%1 $%1",
1122 "%endmacro",
1123 NULL
1125 static int elf_set_info(enum geninfo type, char **val)
1127 return 0;
1130 struct ofmt of_elf = {
1131 "ELF32 (i386) object files (e.g. Linux)",
1132 "elf",
1133 NULL,
1134 null_debug_arr,
1135 &null_debug_form,
1136 elf_stdmac,
1137 elf_init,
1138 elf_set_info,
1139 elf_out,
1140 elf_deflabel,
1141 elf_section_names,
1142 elf_segbase,
1143 elf_directive,
1144 elf_filename,
1145 elf_cleanup
1148 #endif /* OF_ELF */