NASM 0.98p3-hpa
[nasm/avx512.git] / outelf.c
blob0b77c017c8f745af81e3db5808be8136388dcfad
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 #define R_386_32 1 /* ordinary absolute relocation */
25 #define R_386_PC32 2 /* PC-relative relocation */
26 #define R_386_GOT32 3 /* an offset into GOT */
27 #define R_386_PLT32 4 /* a PC-relative offset into PLT */
28 #define R_386_GOTOFF 9 /* an offset from GOT base */
29 #define R_386_GOTPC 10 /* a PC-relative offset _to_ GOT */
31 struct Reloc {
32 struct Reloc *next;
33 long address; /* relative to _start_ of section */
34 long symbol; /* ELF symbol info thingy */
35 int type; /* type of relocation */
38 struct Symbol {
39 long strpos; /* string table position of name */
40 long section; /* section ID of the symbol */
41 int type; /* symbol type */
42 long value; /* address, or COMMON variable align */
43 long size; /* size of symbol */
44 long globnum; /* symbol table offset if global */
45 struct Symbol *next; /* list of globals in each section */
46 struct Symbol *nextfwd; /* list of unresolved-size symbols */
47 char *name; /* used temporarily if in above list */
50 #define SHT_PROGBITS 1
51 #define SHT_NOBITS 8
53 #define SHF_WRITE 1
54 #define SHF_ALLOC 2
55 #define SHF_EXECINSTR 4
57 struct Section {
58 struct SAA *data;
59 unsigned long len, size, nrelocs;
60 long index;
61 int type; /* SHT_PROGBITS or SHT_NOBITS */
62 int align; /* alignment: power of two */
63 unsigned long flags; /* section flags */
64 char *name;
65 struct SAA *rel;
66 long rellen;
67 struct Reloc *head, **tail;
68 struct Symbol *gsyms; /* global symbols in section */
71 #define SECT_DELTA 32
72 static struct Section **sects;
73 static int nsects, sectlen;
75 #define SHSTR_DELTA 256
76 static char *shstrtab;
77 static int shstrtablen, shstrtabsize;
79 static struct SAA *syms;
80 static unsigned long nlocals, nglobs;
82 static long def_seg;
84 static struct RAA *bsym;
86 static struct SAA *strs;
87 static unsigned long strslen;
89 static FILE *elffp;
90 static efunc error;
91 static evalfunc evaluate;
93 static struct Symbol *fwds;
95 static char elf_module[FILENAME_MAX];
97 extern struct ofmt of_elf;
99 #define SHN_ABS 0xFFF1
100 #define SHN_COMMON 0xFFF2
101 #define SHN_UNDEF 0
103 #define SYM_SECTION 0x04
104 #define SYM_GLOBAL 0x10
105 #define SYM_DATA 0x01
106 #define SYM_FUNCTION 0x02
108 #define GLOBAL_TEMP_BASE 6 /* bigger than any constant sym id */
110 #define SEG_ALIGN 16 /* alignment of sections in file */
111 #define SEG_ALIGN_1 (SEG_ALIGN-1)
113 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
115 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
116 static struct ELF_SECTDATA {
117 void *data;
118 long len;
119 int is_saa;
120 } *elf_sects;
121 static int elf_nsect;
122 static long elf_foffs;
124 static void elf_write(void);
125 static void elf_sect_write(struct Section *, unsigned char *, unsigned long);
126 static void elf_section_header (int, int, int, void *, int, long,
127 int, int, int, int);
128 static void elf_write_sections (void);
129 static struct SAA *elf_build_symtab (long *, long *);
130 static struct SAA *elf_build_reltab (long *, struct Reloc *);
131 static void add_sectname (char *, char *);
134 * Special section numbers which are used to define ELF special
135 * symbols, which can be used with WRT to provide PIC relocation
136 * types.
138 static long elf_gotpc_sect, elf_gotoff_sect;
139 static long elf_got_sect, elf_plt_sect;
140 static long elf_sym_sect;
142 static void elf_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
144 elffp = fp;
145 error = errfunc;
146 evaluate = eval;
147 (void) ldef; /* placate optimisers */
148 sects = NULL;
149 nsects = sectlen = 0;
150 syms = saa_init((long)sizeof(struct Symbol));
151 nlocals = nglobs = 0;
152 bsym = raa_init();
153 strs = saa_init(1L);
154 saa_wbytes (strs, "\0", 1L);
155 saa_wbytes (strs, elf_module, (long)(strlen(elf_module)+1));
156 strslen = 2+strlen(elf_module);
157 shstrtab = NULL;
158 shstrtablen = shstrtabsize = 0;;
159 add_sectname ("", "");
161 fwds = NULL;
163 elf_gotpc_sect = seg_alloc();
164 ldef("..gotpc", elf_gotpc_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
165 elf_gotoff_sect = seg_alloc();
166 ldef("..gotoff", elf_gotoff_sect+1, 0L, NULL, FALSE, FALSE,&of_elf,error);
167 elf_got_sect = seg_alloc();
168 ldef("..got", elf_got_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
169 elf_plt_sect = seg_alloc();
170 ldef("..plt", elf_plt_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
171 elf_sym_sect = seg_alloc();
172 ldef("..sym", elf_sym_sect+1, 0L, NULL, FALSE, FALSE, &of_elf, error);
174 def_seg = seg_alloc();
177 static void elf_cleanup(int debuginfo)
179 struct Reloc *r;
180 int i;
182 (void) debuginfo;
184 elf_write();
185 fclose (elffp);
186 for (i=0; i<nsects; i++) {
187 if (sects[i]->type != SHT_NOBITS)
188 saa_free (sects[i]->data);
189 if (sects[i]->head)
190 saa_free (sects[i]->rel);
191 while (sects[i]->head) {
192 r = sects[i]->head;
193 sects[i]->head = sects[i]->head->next;
194 nasm_free (r);
197 nasm_free (sects);
198 saa_free (syms);
199 raa_free (bsym);
200 saa_free (strs);
203 static void add_sectname (char *firsthalf, char *secondhalf)
205 int len = strlen(firsthalf)+strlen(secondhalf);
206 while (shstrtablen + len + 1 > shstrtabsize)
207 shstrtab = nasm_realloc (shstrtab, (shstrtabsize += SHSTR_DELTA));
208 strcpy (shstrtab+shstrtablen, firsthalf);
209 strcat (shstrtab+shstrtablen, secondhalf);
210 shstrtablen += len+1;
213 static int elf_make_section (char *name, int type, int flags, int align)
215 struct Section *s;
217 s = nasm_malloc (sizeof(*s));
219 if (type != SHT_NOBITS)
220 s->data = saa_init (1L);
221 s->head = NULL;
222 s->tail = &s->head;
223 s->len = s->size = 0;
224 s->nrelocs = 0;
225 if (!strcmp(name, ".text"))
226 s->index = def_seg;
227 else
228 s->index = seg_alloc();
229 add_sectname ("", name);
230 s->name = nasm_malloc (1+strlen(name));
231 strcpy (s->name, name);
232 s->type = type;
233 s->flags = flags;
234 s->align = align;
235 s->gsyms = NULL;
237 if (nsects >= sectlen)
238 sects = nasm_realloc (sects, (sectlen += SECT_DELTA)*sizeof(*sects));
239 sects[nsects++] = s;
241 return nsects-1;
244 static long elf_section_names (char *name, int pass, int *bits)
246 char *p;
247 int flags_and, flags_or, type, align, i;
250 * Default is 32 bits.
252 if (!name) {
253 *bits = 32;
254 return def_seg;
257 p = name;
258 while (*p && !isspace(*p)) p++;
259 if (*p) *p++ = '\0';
260 flags_and = flags_or = type = align = 0;
262 while (*p && isspace(*p)) p++;
263 while (*p) {
264 char *q = p;
265 while (*p && !isspace(*p)) p++;
266 if (*p) *p++ = '\0';
267 while (*p && isspace(*p)) p++;
269 if (!nasm_strnicmp(q, "align=", 6)) {
270 align = atoi(q+6);
271 if (align == 0)
272 align = 1;
273 if ( (align-1) & align ) { /* means it's not a power of two */
274 error (ERR_NONFATAL, "section alignment %d is not"
275 " a power of two", align);
276 align = 1;
278 } else if (!nasm_stricmp(q, "alloc")) {
279 flags_and |= SHF_ALLOC;
280 flags_or |= SHF_ALLOC;
281 } else if (!nasm_stricmp(q, "noalloc")) {
282 flags_and |= SHF_ALLOC;
283 flags_or &= ~SHF_ALLOC;
284 } else if (!nasm_stricmp(q, "exec")) {
285 flags_and |= SHF_EXECINSTR;
286 flags_or |= SHF_EXECINSTR;
287 } else if (!nasm_stricmp(q, "noexec")) {
288 flags_and |= SHF_EXECINSTR;
289 flags_or &= ~SHF_EXECINSTR;
290 } else if (!nasm_stricmp(q, "write")) {
291 flags_and |= SHF_WRITE;
292 flags_or |= SHF_WRITE;
293 } else if (!nasm_stricmp(q, "nowrite")) {
294 flags_and |= SHF_WRITE;
295 flags_or &= ~SHF_WRITE;
296 } else if (!nasm_stricmp(q, "progbits")) {
297 type = SHT_PROGBITS;
298 } else if (!nasm_stricmp(q, "nobits")) {
299 type = SHT_NOBITS;
303 if (!strcmp(name, ".comment") ||
304 !strcmp(name, ".shstrtab") ||
305 !strcmp(name, ".symtab") ||
306 !strcmp(name, ".strtab")) {
307 error (ERR_NONFATAL, "attempt to redefine reserved section"
308 "name `%s'", name);
309 return NO_SEG;
312 for (i=0; i<nsects; i++)
313 if (!strcmp(name, sects[i]->name))
314 break;
315 if (i == nsects) {
316 if (!strcmp(name, ".text"))
317 i = elf_make_section (name, SHT_PROGBITS,
318 SHF_ALLOC | SHF_EXECINSTR, 16);
319 else if (!strcmp(name, ".data"))
320 i = elf_make_section (name, SHT_PROGBITS,
321 SHF_ALLOC | SHF_WRITE, 4);
322 else if (!strcmp(name, ".bss"))
323 i = elf_make_section (name, SHT_NOBITS,
324 SHF_ALLOC | SHF_WRITE, 4);
325 else
326 i = elf_make_section (name, SHT_PROGBITS, SHF_ALLOC, 1);
327 if (type)
328 sects[i]->type = type;
329 if (align)
330 sects[i]->align = align;
331 sects[i]->flags &= ~flags_and;
332 sects[i]->flags |= flags_or;
333 } else if (pass == 1) {
334 if (type || align || flags_and)
335 error (ERR_WARNING, "section attributes ignored on"
336 " redeclaration of section `%s'", name);
339 return sects[i]->index;
342 static void elf_deflabel (char *name, long segment, long offset,
343 int is_global, char *special)
345 int pos = strslen;
346 struct Symbol *sym;
347 int special_used = FALSE;
349 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
351 * This is a NASM special symbol. We never allow it into
352 * the ELF symbol table, even if it's a valid one. If it
353 * _isn't_ a valid one, we should barf immediately.
355 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
356 strcmp(name, "..got") && strcmp(name, "..plt") &&
357 strcmp(name, "..sym"))
358 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
359 return;
362 if (is_global == 3) {
363 struct Symbol **s;
365 * Fix up a forward-reference symbol size from the first
366 * pass.
368 for (s = &fwds; *s; s = &(*s)->nextfwd)
369 if (!strcmp((*s)->name, name)) {
370 struct tokenval tokval;
371 expr *e;
372 char *p = special;
374 while (*p && !isspace(*p)) p++;
375 while (*p && isspace(*p)) p++;
376 stdscan_reset();
377 stdscan_bufptr = p;
378 tokval.t_type = TOKEN_INVALID;
379 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
380 if (e) {
381 if (!is_simple(e))
382 error (ERR_NONFATAL, "cannot use relocatable"
383 " expression as symbol size");
384 else
385 (*s)->size = reloc_value(e);
389 * Remove it from the list of unresolved sizes.
391 nasm_free ((*s)->name);
392 *s = (*s)->nextfwd;
393 return;
395 return; /* it wasn't an important one */
398 saa_wbytes (strs, name, (long)(1+strlen(name)));
399 strslen += 1+strlen(name);
401 sym = saa_wstruct (syms);
403 sym->strpos = pos;
404 sym->type = is_global ? SYM_GLOBAL : 0;
405 sym->size = 0;
406 if (segment == NO_SEG)
407 sym->section = SHN_ABS;
408 else {
409 int i;
410 sym->section = SHN_UNDEF;
411 if (nsects == 0 && segment == def_seg) {
412 int tempint;
413 if (segment != elf_section_names (".text", 2, &tempint))
414 error (ERR_PANIC, "strange segment conditions in ELF driver");
415 sym->section = nsects;
416 } else {
417 for (i=0; i<nsects; i++)
418 if (segment == sects[i]->index) {
419 sym->section = i+1;
420 break;
425 if (is_global == 2) {
426 sym->size = offset;
427 sym->value = 0;
428 sym->section = SHN_COMMON;
430 * We have a common variable. Check the special text to see
431 * if it's a valid number and power of two; if so, store it
432 * as the alignment for the common variable.
434 if (special) {
435 int err;
436 sym->value = readnum (special, &err);
437 if (err)
438 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
439 " valid number", special);
440 else if ( (sym->value | (sym->value-1)) != 2*sym->value - 1)
441 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
442 " power of two", special);
444 special_used = TRUE;
445 } else
446 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
448 if (sym->type == SYM_GLOBAL) {
450 * There's a problem here that needs fixing.
451 * If sym->section == SHN_ABS, then the first line of the
452 * else section causes a core dump, because its a reference
453 * beyond the end of the section array.
454 * This behaviour is exhibited by this code:
455 * GLOBAL crash_nasm
456 * crash_nasm equ 0
458 * I'm not sure how to procede, because I haven't got the
459 * first clue about how ELF works, so I don't know what to
460 * do with it. Furthermore, I'm not sure what the rest of this
461 * section of code does. Help?
463 * For now, I'll see if doing absolutely nothing with it will
464 * work...
466 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON)
468 bsym = raa_write (bsym, segment, nglobs);
470 else if (sym->section != SHN_ABS)
473 * This is a global symbol; so we must add it to the linked
474 * list of global symbols in its section. We'll push it on
475 * the beginning of the list, because it doesn't matter
476 * much which end we put it on and it's easier like this.
478 * In addition, we check the special text for symbol
479 * type and size information.
481 sym->next = sects[sym->section-1]->gsyms;
482 sects[sym->section-1]->gsyms = sym;
484 if (special) {
485 int n = strcspn(special, " ");
487 if (!nasm_strnicmp(special, "function", n))
488 sym->type |= SYM_FUNCTION;
489 else if (!nasm_strnicmp(special, "data", n) ||
490 !nasm_strnicmp(special, "object", n))
491 sym->type |= SYM_DATA;
492 else
493 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
494 n, special);
495 if (special[n]) {
496 struct tokenval tokval;
497 expr *e;
498 int fwd = FALSE;
500 while (special[n] && isspace(special[n]))
501 n++;
503 * We have a size expression; attempt to
504 * evaluate it.
506 stdscan_reset();
507 stdscan_bufptr = special+n;
508 tokval.t_type = TOKEN_INVALID;
509 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error, NULL);
510 if (fwd) {
511 sym->nextfwd = fwds;
512 fwds = sym;
513 sym->name = nasm_strdup(name);
514 } else if (e) {
515 if (!is_simple(e))
516 error (ERR_NONFATAL, "cannot use relocatable"
517 " expression as symbol size");
518 else
519 sym->size = reloc_value(e);
522 special_used = TRUE;
525 sym->globnum = nglobs;
526 nglobs++;
527 } else
528 nlocals++;
530 if (special && !special_used)
531 error(ERR_NONFATAL, "no special symbol features supported here");
534 static void elf_add_reloc (struct Section *sect, long segment,
535 int type)
537 struct Reloc *r;
539 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
540 sect->tail = &r->next;
541 r->next = NULL;
543 r->address = sect->len;
544 if (segment == NO_SEG)
545 r->symbol = 2;
546 else {
547 int i;
548 r->symbol = 0;
549 for (i=0; i<nsects; i++)
550 if (segment == sects[i]->index)
551 r->symbol = i+3;
552 if (!r->symbol)
553 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
555 r->type = type;
557 sect->nrelocs++;
561 * This routine deals with ..got and ..sym relocations: the more
562 * complicated kinds. In shared-library writing, some relocations
563 * with respect to global symbols must refer to the precise symbol
564 * rather than referring to an offset from the base of the section
565 * _containing_ the symbol. Such relocations call to this routine,
566 * which searches the symbol list for the symbol in question.
568 * R_386_GOT32 references require the _exact_ symbol address to be
569 * used; R_386_32 references can be at an offset from the symbol.
570 * The boolean argument `exact' tells us this.
572 * Return value is the adjusted value of `addr', having become an
573 * offset from the symbol rather than the section. Should always be
574 * zero when returning from an exact call.
576 * Limitation: if you define two symbols at the same place,
577 * confusion will occur.
579 * Inefficiency: we search, currently, using a linked list which
580 * isn't even necessarily sorted.
582 static long elf_add_gsym_reloc (struct Section *sect,
583 long segment, long offset,
584 int type, int exact)
586 struct Reloc *r;
587 struct Section *s;
588 struct Symbol *sym, *sm;
589 int i;
592 * First look up the segment/offset pair and find a global
593 * symbol corresponding to it. If it's not one of our segments,
594 * then it must be an external symbol, in which case we're fine
595 * doing a normal elf_add_reloc after first sanity-checking
596 * that the offset from the symbol is zero.
598 s = NULL;
599 for (i=0; i<nsects; i++)
600 if (segment == sects[i]->index) {
601 s = sects[i];
602 break;
604 if (!s) {
605 if (exact && offset != 0)
606 error (ERR_NONFATAL, "unable to find a suitable global symbol"
607 " for this reference");
608 else
609 elf_add_reloc (sect, segment, type);
610 return offset;
613 if (exact) {
615 * Find a symbol pointing _exactly_ at this one.
617 for (sym = s->gsyms; sym; sym = sym->next)
618 if (sym->value == offset)
619 break;
620 } else {
622 * Find the nearest symbol below this one.
624 sym = NULL;
625 for (sm = s->gsyms; sm; sm = sm->next)
626 if (sm->value <= offset && (!sym || sm->value > sym->value))
627 sym = sm;
629 if (!sym && exact) {
630 error (ERR_NONFATAL, "unable to find a suitable global symbol"
631 " for this reference");
632 return 0;
635 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
636 sect->tail = &r->next;
637 r->next = NULL;
639 r->address = sect->len;
640 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
641 r->type = type;
643 sect->nrelocs++;
645 return offset - sym->value;
648 static void elf_out (long segto, void *data, unsigned long type,
649 long segment, long wrt)
651 struct Section *s;
652 long realbytes = type & OUT_SIZMASK;
653 long addr;
654 unsigned char mydata[4], *p;
655 int i;
657 type &= OUT_TYPMASK;
660 * handle absolute-assembly (structure definitions)
662 if (segto == NO_SEG) {
663 if (type != OUT_RESERVE)
664 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
665 " space");
666 return;
669 s = NULL;
670 for (i=0; i<nsects; i++)
671 if (segto == sects[i]->index) {
672 s = sects[i];
673 break;
675 if (!s) {
676 int tempint; /* ignored */
677 if (segto != elf_section_names (".text", 2, &tempint))
678 error (ERR_PANIC, "strange segment conditions in ELF driver");
679 else
680 s = sects[nsects-1];
683 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
684 error(ERR_WARNING, "attempt to initialise memory in"
685 " BSS section `%s': ignored", s->name);
686 if (type == OUT_REL2ADR)
687 realbytes = 2;
688 else if (type == OUT_REL4ADR)
689 realbytes = 4;
690 s->len += realbytes;
691 return;
694 if (type == OUT_RESERVE) {
695 if (s->type == SHT_PROGBITS) {
696 error(ERR_WARNING, "uninitialised space declared in"
697 " non-BSS section `%s': zeroing", s->name);
698 elf_sect_write (s, NULL, realbytes);
699 } else
700 s->len += realbytes;
701 } else if (type == OUT_RAWDATA) {
702 if (segment != NO_SEG)
703 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
704 elf_sect_write (s, data, realbytes);
705 } else if (type == OUT_ADDRESS) {
706 addr = *(long *)data;
707 if (segment != NO_SEG) {
708 if (segment % 2) {
709 error(ERR_NONFATAL, "ELF format does not support"
710 " segment base references");
711 } else {
712 if (wrt == NO_SEG) {
713 elf_add_reloc (s, segment, R_386_32);
714 } else if (wrt == elf_gotpc_sect+1) {
716 * The user will supply GOT relative to $$. ELF
717 * will let us have GOT relative to $. So we
718 * need to fix up the data item by $-$$.
720 addr += s->len;
721 elf_add_reloc (s, segment, R_386_GOTPC);
722 } else if (wrt == elf_gotoff_sect+1) {
723 elf_add_reloc (s, segment, R_386_GOTOFF);
724 } else if (wrt == elf_got_sect+1) {
725 addr = elf_add_gsym_reloc (s, segment, addr,
726 R_386_GOT32, TRUE);
727 } else if (wrt == elf_sym_sect+1) {
728 addr = elf_add_gsym_reloc (s, segment, addr,
729 R_386_32, FALSE);
730 } else if (wrt == elf_plt_sect+1) {
731 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
732 "relative PLT references");
733 } else {
734 error (ERR_NONFATAL, "ELF format does not support this"
735 " use of WRT");
736 wrt = NO_SEG; /* we can at least _try_ to continue */
740 p = mydata;
741 if (realbytes != 4 && segment != NO_SEG)
742 error (ERR_NONFATAL, "ELF format does not support non-32-bit"
743 " relocations");
744 WRITELONG (p, addr);
745 elf_sect_write (s, mydata, realbytes);
746 } else if (type == OUT_REL2ADR) {
747 error (ERR_NONFATAL, "ELF format does not support 16-bit"
748 " relocations");
749 } else if (type == OUT_REL4ADR) {
750 if (segment == segto)
751 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
752 if (segment != NO_SEG && segment % 2) {
753 error(ERR_NONFATAL, "ELF format does not support"
754 " segment base references");
755 } else {
756 if (wrt == NO_SEG) {
757 elf_add_reloc (s, segment, R_386_PC32);
758 } else if (wrt == elf_plt_sect+1) {
759 elf_add_reloc (s, segment, R_386_PLT32);
760 } else if (wrt == elf_gotpc_sect+1 ||
761 wrt == elf_gotoff_sect+1 ||
762 wrt == elf_got_sect+1) {
763 error(ERR_NONFATAL, "ELF format cannot produce PC-"
764 "relative GOT 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 */
771 p = mydata;
772 WRITELONG (p, *(long*)data - realbytes);
773 elf_sect_write (s, mydata, 4L);
777 static void elf_write(void)
779 int nsections, align;
780 char *p;
781 int commlen;
782 char comment[64];
783 int i;
785 struct SAA *symtab;
786 long symtablen, symtablocal;
789 * Work out how many sections we will have. We have SHN_UNDEF,
790 * then the flexible user sections, then the four fixed
791 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
792 * then optionally relocation sections for the user sections.
794 nsections = 5; /* SHN_UNDEF and the fixed ones */
795 add_sectname ("", ".comment");
796 add_sectname ("", ".shstrtab");
797 add_sectname ("", ".symtab");
798 add_sectname ("", ".strtab");
799 for (i=0; i<nsects; i++) {
800 nsections++; /* for the section itself */
801 if (sects[i]->head) {
802 nsections++; /* for its relocations */
803 add_sectname (".rel", sects[i]->name);
808 * Do the comment.
810 *comment = '\0';
811 commlen = 2+sprintf(comment+1, "The Netwide Assembler %s", NASM_VER);
814 * Output the ELF header.
816 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
817 fwriteshort (1, elffp); /* ET_REL relocatable file */
818 fwriteshort (3, elffp); /* EM_386 processor ID */
819 fwritelong (1L, elffp); /* EV_CURRENT file format version */
820 fwritelong (0L, elffp); /* no entry point */
821 fwritelong (0L, elffp); /* no program header table */
822 fwritelong (0x40L, elffp); /* section headers straight after
823 * ELF header plus alignment */
824 fwritelong (0L, elffp); /* 386 defines no special flags */
825 fwriteshort (0x34, elffp); /* size of ELF header */
826 fwriteshort (0, elffp); /* no program header table, again */
827 fwriteshort (0, elffp); /* still no program header table */
828 fwriteshort (0x28, elffp); /* size of section header */
829 fwriteshort (nsections, elffp); /* number of sections */
830 fwriteshort (nsects+2, elffp); /* string table section index for
831 * section header table */
832 fwritelong (0L, elffp); /* align to 0x40 bytes */
833 fwritelong (0L, elffp);
834 fwritelong (0L, elffp);
837 * Build the symbol table and relocation tables.
839 symtab = elf_build_symtab (&symtablen, &symtablocal);
840 for (i=0; i<nsects; i++)
841 if (sects[i]->head)
842 sects[i]->rel = elf_build_reltab (&sects[i]->rellen,
843 sects[i]->head);
846 * Now output the section header table.
849 elf_foffs = 0x40 + 0x28 * nsections;
850 align = ((elf_foffs+SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
851 elf_foffs += align;
852 elf_nsect = 0;
853 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
855 elf_section_header (0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
856 p = shstrtab+1;
857 for (i=0; i<nsects; i++) {
858 elf_section_header (p - shstrtab, sects[i]->type, sects[i]->flags,
859 (sects[i]->type == SHT_PROGBITS ?
860 sects[i]->data : NULL), TRUE,
861 sects[i]->len, 0, 0, sects[i]->align, 0);
862 p += strlen(p)+1;
864 elf_section_header (p - shstrtab, 1, 0, comment, FALSE,
865 (long)commlen, 0, 0, 1, 0);/* .comment */
866 p += strlen(p)+1;
867 elf_section_header (p - shstrtab, 3, 0, shstrtab, FALSE,
868 (long)shstrtablen, 0, 0, 1, 0);/* .shstrtab */
869 p += strlen(p)+1;
870 elf_section_header (p - shstrtab, 2, 0, symtab, TRUE,
871 symtablen, nsects+4, symtablocal, 4, 16);/* .symtab */
872 p += strlen(p)+1;
873 elf_section_header (p - shstrtab, 3, 0, strs, TRUE,
874 strslen, 0, 0, 1, 0); /* .strtab */
875 for (i=0; i<nsects; i++) if (sects[i]->head) {
876 p += strlen(p)+1;
877 elf_section_header (p - shstrtab, 9, 0, sects[i]->rel, TRUE,
878 sects[i]->rellen, nsects+3, i+1, 4, 8);
881 fwrite (align_str, align, 1, elffp);
884 * Now output the sections.
886 elf_write_sections();
888 nasm_free (elf_sects);
889 saa_free (symtab);
892 static struct SAA *elf_build_symtab (long *len, long *local)
894 struct SAA *s = saa_init(1L);
895 struct Symbol *sym;
896 unsigned char entry[16], *p;
897 int i;
899 *len = *local = 0;
902 * First, an all-zeros entry, required by the ELF spec.
904 saa_wbytes (s, NULL, 16L); /* null symbol table entry */
905 *len += 16;
906 (*local)++;
909 * Next, an entry for the file name.
911 p = entry;
912 WRITELONG (p, 1); /* we know it's 1st thing in strtab */
913 WRITELONG (p, 0); /* no value */
914 WRITELONG (p, 0); /* no size either */
915 WRITESHORT (p, 4); /* type FILE */
916 WRITESHORT (p, SHN_ABS);
917 saa_wbytes (s, entry, 16L);
918 *len += 16;
919 (*local)++;
922 * Now some standard symbols defining the segments, for relocation
923 * purposes.
925 for (i = 1; i <= nsects+1; i++) {
926 p = entry;
927 WRITELONG (p, 0); /* no symbol name */
928 WRITELONG (p, 0); /* offset zero */
929 WRITELONG (p, 0); /* size zero */
930 WRITESHORT (p, 3); /* local section-type thing */
931 WRITESHORT (p, (i==1 ? SHN_ABS : i-1)); /* the section id */
932 saa_wbytes (s, entry, 16L);
933 *len += 16;
934 (*local)++;
938 * Now the other local symbols.
940 saa_rewind (syms);
941 while ( (sym = saa_rstruct (syms)) ) {
942 if (sym->type & SYM_GLOBAL)
943 continue;
944 p = entry;
945 WRITELONG (p, sym->strpos);
946 WRITELONG (p, sym->value);
947 WRITELONG (p, sym->size);
948 WRITESHORT (p, sym->type); /* local non-typed thing */
949 WRITESHORT (p, sym->section);
950 saa_wbytes (s, entry, 16L);
951 *len += 16;
952 (*local)++;
956 * Now the global symbols.
958 saa_rewind (syms);
959 while ( (sym = saa_rstruct (syms)) ) {
960 if (!(sym->type & SYM_GLOBAL))
961 continue;
962 p = entry;
963 WRITELONG (p, sym->strpos);
964 WRITELONG (p, sym->value);
965 WRITELONG (p, sym->size);
966 WRITESHORT (p, sym->type); /* global non-typed thing */
967 WRITESHORT (p, sym->section);
968 saa_wbytes (s, entry, 16L);
969 *len += 16;
972 return s;
975 static struct SAA *elf_build_reltab (long *len, struct Reloc *r) {
976 struct SAA *s;
977 unsigned char *p, entry[8];
979 if (!r)
980 return NULL;
982 s = saa_init(1L);
983 *len = 0;
985 while (r) {
986 long sym = r->symbol;
988 if (sym >= GLOBAL_TEMP_BASE)
989 sym += -GLOBAL_TEMP_BASE + (nsects+3) + nlocals;
991 p = entry;
992 WRITELONG (p, r->address);
993 WRITELONG (p, (sym << 8) + r->type);
994 saa_wbytes (s, entry, 8L);
995 *len += 8;
997 r = r->next;
1000 return s;
1003 static void elf_section_header (int name, int type, int flags,
1004 void *data, int is_saa, long datalen,
1005 int link, int info, int align, int eltsize)
1007 elf_sects[elf_nsect].data = data;
1008 elf_sects[elf_nsect].len = datalen;
1009 elf_sects[elf_nsect].is_saa = is_saa;
1010 elf_nsect++;
1012 fwritelong ((long)name, elffp);
1013 fwritelong ((long)type, elffp);
1014 fwritelong ((long)flags, elffp);
1015 fwritelong (0L, elffp); /* no address, ever, in object files */
1016 fwritelong (type == 0 ? 0L : elf_foffs, elffp);
1017 fwritelong (datalen, elffp);
1018 if (data)
1019 elf_foffs += (datalen+SEG_ALIGN_1) & ~SEG_ALIGN_1;
1020 fwritelong ((long)link, elffp);
1021 fwritelong ((long)info, elffp);
1022 fwritelong ((long)align, elffp);
1023 fwritelong ((long)eltsize, elffp);
1026 static void elf_write_sections (void)
1028 int i;
1029 for (i = 0; i < elf_nsect; i++)
1030 if (elf_sects[i].data) {
1031 long len = elf_sects[i].len;
1032 long reallen = (len+SEG_ALIGN_1) & ~SEG_ALIGN_1;
1033 long align = reallen - len;
1034 if (elf_sects[i].is_saa)
1035 saa_fpwrite (elf_sects[i].data, elffp);
1036 else
1037 fwrite (elf_sects[i].data, len, 1, elffp);
1038 fwrite (align_str, align, 1, elffp);
1042 static void elf_sect_write (struct Section *sect,
1043 unsigned char *data, unsigned long len)
1045 saa_wbytes (sect->data, data, len);
1046 sect->len += len;
1049 static long elf_segbase (long segment)
1051 return segment;
1054 static int elf_directive (char *directive, char *value, int pass)
1056 return 0;
1059 static void elf_filename (char *inname, char *outname, efunc error)
1061 strcpy(elf_module, inname);
1062 standard_extension (inname, outname, ".o", error);
1065 static char *elf_stdmac[] = {
1066 "%define __SECT__ [section .text]",
1067 "%macro __NASM_CDecl__ 1",
1068 "%define $_%1 $%1",
1069 "%endmacro",
1070 NULL
1072 static int elf_set_info(enum geninfo type, char **val)
1074 return 0;
1077 struct ofmt of_elf = {
1078 "ELF32 (i386) object files (e.g. Linux)",
1079 "elf",
1080 NULL,
1081 null_debug_arr,
1082 &null_debug_form,
1083 elf_stdmac,
1084 elf_init,
1085 elf_set_info,
1086 elf_out,
1087 elf_deflabel,
1088 elf_section_names,
1089 elf_segbase,
1090 elf_directive,
1091 elf_filename,
1092 elf_cleanup
1095 #endif /* OF_ELF */