1 /* outaout.c output routines for the Netwide Assembler to produce
2 * Linux a.out object files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
23 #if defined OF_AOUT || defined OF_AOUTB
25 #define RELTYPE_ABSOLUTE 0x00
26 #define RELTYPE_RELATIVE 0x01
27 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
28 #define RELTYPE_GOTOFF 0x10
29 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
30 #define RELTYPE_PLT 0x21
31 #define RELTYPE_SYMFLAG 0x08
35 int32_t address; /* relative to _start_ of section */
36 int32_t symbol; /* symbol number or -ve section id */
37 int bytes; /* 2 or 4 */
38 int reltype; /* see above */
42 int32_t strpos; /* string table position of name */
43 int type; /* symbol type - see flags below */
44 int32_t value; /* address, or COMMON variable size */
45 int32_t size; /* size for data or function exports */
46 int32_t segment; /* back-reference used by gsym_reloc */
47 struct Symbol *next; /* list of globals in each section */
48 struct Symbol *nextfwd; /* list of unresolved-size symbols */
49 char *name; /* for unresolved-size symbols */
50 int32_t symnum; /* index into symbol table */
54 * Section IDs - used in Reloc.symbol when negative, and in
55 * Symbol.type when positive.
57 #define SECT_ABS 2 /* absolute value */
58 #define SECT_TEXT 4 /* text section */
59 #define SECT_DATA 6 /* data section */
60 #define SECT_BSS 8 /* bss section */
61 #define SECT_MASK 0xE /* mask out any of the above */
64 * More flags used in Symbol.type.
66 #define SYM_GLOBAL 1 /* it's a global symbol */
67 #define SYM_DATA 0x100 /* used for shared libs */
68 #define SYM_FUNCTION 0x200 /* used for shared libs */
69 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
72 * Bit more explanation of symbol types: SECT_xxx denotes a local
73 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
74 * this module. Just SYM_GLOBAL, with zero value, denotes an
75 * external symbol referenced in this module. And just SYM_GLOBAL,
76 * but with a non-zero value, declares a C `common' variable, of
82 uint32_t len, size, nrelocs;
84 struct Reloc *head, **tail;
85 struct Symbol *gsyms, *asym;
88 static struct Section stext, sdata, sbss;
90 static struct SAA *syms;
91 static uint32_t nsyms;
93 static struct RAA *bsym;
95 static struct SAA *strs;
96 static uint32_t strslen;
98 static struct Symbol *fwds;
102 static evalfunc evaluate;
107 static void aout_write(void);
108 static void aout_write_relocs(struct Reloc *);
109 static void aout_write_syms(void);
110 static void aout_sect_write(struct Section *, const uint8_t *,
112 static void aout_pad_sections(void);
113 static void aout_fixup_relocs(struct Section *);
116 * Special section numbers which are used to define special
117 * symbols, which can be used with WRT to provide PIC relocation
120 static int32_t aout_gotpc_sect, aout_gotoff_sect;
121 static int32_t aout_got_sect, aout_plt_sect;
122 static int32_t aout_sym_sect;
124 static void aoutg_init(FILE * fp, efunc errfunc, ldfunc ldef,
130 (void)ldef; /* placate optimisers */
131 stext.data = saa_init(1L);
133 stext.tail = &stext.head;
134 sdata.data = saa_init(1L);
136 sdata.tail = &sdata.head;
137 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
138 stext.nrelocs = sdata.nrelocs = 0;
139 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
140 stext.index = seg_alloc();
141 sdata.index = seg_alloc();
142 sbss.index = seg_alloc();
143 stext.asym = sdata.asym = sbss.asym = NULL;
144 syms = saa_init((int32_t)sizeof(struct Symbol));
154 static void aout_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
157 aoutg_init(fp, errfunc, ldef, eval);
159 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
160 aout_plt_sect = aout_sym_sect = NO_SEG;
167 extern struct ofmt of_aoutb;
169 static void aoutb_init(FILE * fp, efunc errfunc, ldfunc ldef,
173 aoutg_init(fp, errfunc, ldef, eval);
175 is_pic = 0x00; /* may become 0x40 */
177 aout_gotpc_sect = seg_alloc();
178 ldef("..gotpc", aout_gotpc_sect + 1, 0L, NULL, false, false, &of_aoutb,
180 aout_gotoff_sect = seg_alloc();
181 ldef("..gotoff", aout_gotoff_sect + 1, 0L, NULL, false, false,
183 aout_got_sect = seg_alloc();
184 ldef("..got", aout_got_sect + 1, 0L, NULL, false, false, &of_aoutb,
186 aout_plt_sect = seg_alloc();
187 ldef("..plt", aout_plt_sect + 1, 0L, NULL, false, false, &of_aoutb,
189 aout_sym_sect = seg_alloc();
190 ldef("..sym", aout_sym_sect + 1, 0L, NULL, false, false, &of_aoutb,
196 static void aout_cleanup(int debuginfo)
203 aout_fixup_relocs(&stext);
204 aout_fixup_relocs(&sdata);
207 saa_free(stext.data);
210 stext.head = stext.head->next;
213 saa_free(sdata.data);
216 sdata.head = sdata.head->next;
224 static int32_t aout_section_names(char *name, int pass, int *bits)
230 * Default to 32 bits.
238 if (!strcmp(name, ".text"))
240 else if (!strcmp(name, ".data"))
242 else if (!strcmp(name, ".bss"))
248 static void aout_deflabel(char *name, int32_t segment, int32_t offset,
249 int is_global, char *special)
251 int pos = strslen + 4;
253 int special_used = false;
255 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
257 * This is a NASM special symbol. We never allow it into
258 * the a.out symbol table, even if it's a valid one. If it
259 * _isn't_ a valid one, we should barf immediately.
261 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
262 strcmp(name, "..got") && strcmp(name, "..plt") &&
263 strcmp(name, "..sym"))
264 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
268 if (is_global == 3) {
271 * Fix up a forward-reference symbol size from the first
274 for (s = &fwds; *s; s = &(*s)->nextfwd)
275 if (!strcmp((*s)->name, name)) {
276 struct tokenval tokval;
280 while (*p && !isspace(*p))
282 while (*p && isspace(*p))
286 tokval.t_type = TOKEN_INVALID;
287 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
290 error(ERR_NONFATAL, "cannot use relocatable"
291 " expression as symbol size");
293 (*s)->size = reloc_value(e);
297 * Remove it from the list of unresolved sizes.
299 nasm_free((*s)->name);
303 return; /* it wasn't an important one */
306 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
307 strslen += 1 + strlen(name);
309 sym = saa_wstruct(syms);
312 sym->type = is_global ? SYM_GLOBAL : 0;
313 sym->segment = segment;
314 if (segment == NO_SEG)
315 sym->type |= SECT_ABS;
316 else if (segment == stext.index) {
317 sym->type |= SECT_TEXT;
319 sym->next = stext.gsyms;
321 } else if (!stext.asym)
323 } else if (segment == sdata.index) {
324 sym->type |= SECT_DATA;
326 sym->next = sdata.gsyms;
328 } else if (!sdata.asym)
330 } else if (segment == sbss.index) {
331 sym->type |= SECT_BSS;
333 sym->next = sbss.gsyms;
335 } else if (!sbss.asym)
338 sym->type = SYM_GLOBAL;
342 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
344 if (is_global && sym->type != SYM_GLOBAL) {
346 * Global symbol exported _from_ this module. We must check
347 * the special text for type information.
351 int n = strcspn(special, " ");
353 if (!nasm_strnicmp(special, "function", n))
354 sym->type |= SYM_FUNCTION;
355 else if (!nasm_strnicmp(special, "data", n) ||
356 !nasm_strnicmp(special, "object", n))
357 sym->type |= SYM_DATA;
359 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
362 struct tokenval tokval;
365 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
368 error(ERR_NONFATAL, "Linux a.out does not support"
369 " symbol size information");
371 while (special[n] && isspace(special[n]))
374 * We have a size expression; attempt to
377 sym->type |= SYM_WITH_SIZE;
379 stdscan_bufptr = special + n;
380 tokval.t_type = TOKEN_INVALID;
381 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
386 sym->name = nasm_strdup(name);
389 error(ERR_NONFATAL, "cannot use relocatable"
390 " expression as symbol size");
392 sym->size = reloc_value(e);
395 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
402 * define the references from external-symbol segment numbers
403 * to these symbol records.
405 if (segment != NO_SEG && segment != stext.index &&
406 segment != sdata.index && segment != sbss.index)
407 bsym = raa_write(bsym, segment, nsyms);
411 if (sym->type & SYM_WITH_SIZE)
412 nsyms++; /* and another for the size */
414 if (special && !special_used)
415 error(ERR_NONFATAL, "no special symbol features supported here");
418 static void aout_add_reloc(struct Section *sect, int32_t segment,
419 int reltype, int bytes)
423 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
424 sect->tail = &r->next;
427 r->address = sect->len;
428 r->symbol = (segment == NO_SEG ? -SECT_ABS :
429 segment == stext.index ? -SECT_TEXT :
430 segment == sdata.index ? -SECT_DATA :
431 segment == sbss.index ? -SECT_BSS :
432 raa_read(bsym, segment));
433 r->reltype = reltype;
435 r->reltype |= RELTYPE_SYMFLAG;
442 * This routine deals with ..got and ..sym relocations: the more
443 * complicated kinds. In shared-library writing, some relocations
444 * with respect to global symbols must refer to the precise symbol
445 * rather than referring to an offset from the base of the section
446 * _containing_ the symbol. Such relocations call to this routine,
447 * which searches the symbol list for the symbol in question.
449 * RELTYPE_GOT references require the _exact_ symbol address to be
450 * used; RELTYPE_ABSOLUTE references can be at an offset from the
451 * symbol. The boolean argument `exact' tells us this.
453 * Return value is the adjusted value of `addr', having become an
454 * offset from the symbol rather than the section. Should always be
455 * zero when returning from an exact call.
457 * Limitation: if you define two symbols at the same place,
458 * confusion will occur.
460 * Inefficiency: we search, currently, using a linked list which
461 * isn't even necessarily sorted.
463 static int32_t aout_add_gsym_reloc(struct Section *sect,
464 int32_t segment, int32_t offset,
465 int type, int bytes, int exact)
467 struct Symbol *sym, *sm, *shead;
471 * First look up the segment to find whether it's text, data,
472 * bss or an external symbol.
475 if (segment == stext.index)
477 else if (segment == sdata.index)
479 else if (segment == sbss.index)
482 if (exact && offset != 0)
483 error(ERR_NONFATAL, "unable to find a suitable global symbol"
484 " for this reference");
486 aout_add_reloc(sect, segment, type, bytes);
492 * Find a symbol pointing _exactly_ at this one.
494 for (sym = shead; sym; sym = sym->next)
495 if (sym->value == offset)
499 * Find the nearest symbol below this one.
502 for (sm = shead; sm; sm = sm->next)
503 if (sm->value <= offset && (!sym || sm->value > sym->value))
507 error(ERR_NONFATAL, "unable to find a suitable global symbol"
508 " for this reference");
512 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
513 sect->tail = &r->next;
516 r->address = sect->len;
517 r->symbol = sym->symnum;
518 r->reltype = type | RELTYPE_SYMFLAG;
523 return offset - sym->value;
527 * This routine deals with ..gotoff relocations. These _must_ refer
528 * to a symbol, due to a perversity of *BSD's PIC implementation,
529 * and it must be a non-global one as well; so we store `asym', the
530 * first nonglobal symbol defined in each section, and always work
531 * from that. Relocation type is always RELTYPE_GOTOFF.
533 * Return value is the adjusted value of `addr', having become an
534 * offset from the `asym' symbol rather than the section.
536 static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
537 int32_t offset, int bytes)
543 * First look up the segment to find whether it's text, data,
544 * bss or an external symbol.
547 if (segment == stext.index)
549 else if (segment == sdata.index)
551 else if (segment == sbss.index)
554 error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
555 " symbol in the section");
557 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
558 sect->tail = &r->next;
561 r->address = sect->len;
562 r->symbol = asym->symnum;
563 r->reltype = RELTYPE_GOTOFF;
568 return offset - asym->value;
571 static void aout_out(int32_t segto, const void *data, uint32_t type,
572 int32_t segment, int32_t wrt)
575 int32_t realbytes = type & OUT_SIZMASK;
577 uint8_t mydata[4], *p;
582 * handle absolute-assembly (structure definitions)
584 if (segto == NO_SEG) {
585 if (type != OUT_RESERVE)
586 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
591 if (segto == stext.index)
593 else if (segto == sdata.index)
595 else if (segto == sbss.index)
598 error(ERR_WARNING, "attempt to assemble code in"
599 " segment %d: defaulting to `.text'", segto);
603 if (!s && type != OUT_RESERVE) {
604 error(ERR_WARNING, "attempt to initialize memory in the"
605 " BSS section: ignored");
606 if (type == OUT_REL2ADR)
608 else if (type == OUT_REL4ADR)
610 sbss.len += realbytes;
614 if (type == OUT_RESERVE) {
616 error(ERR_WARNING, "uninitialized space declared in"
617 " %s section: zeroing",
618 (segto == stext.index ? "code" : "data"));
619 aout_sect_write(s, NULL, realbytes);
621 sbss.len += realbytes;
622 } else if (type == OUT_RAWDATA) {
623 if (segment != NO_SEG)
624 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
625 aout_sect_write(s, data, realbytes);
626 } else if (type == OUT_ADDRESS) {
627 addr = *(int32_t *)data;
628 if (segment != NO_SEG) {
630 error(ERR_NONFATAL, "a.out format does not support"
631 " segment base references");
634 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE,
638 "Linux a.out format does not support"
640 wrt = NO_SEG; /* we can at least _try_ to continue */
641 } else if (wrt == aout_gotpc_sect + 1) {
643 aout_add_reloc(s, segment, RELTYPE_GOTPC, realbytes);
644 } else if (wrt == aout_gotoff_sect + 1) {
646 addr = aout_add_gotoff_reloc(s, segment,
648 } else if (wrt == aout_got_sect + 1) {
651 aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
653 } else if (wrt == aout_sym_sect + 1) {
654 addr = aout_add_gsym_reloc(s, segment, addr,
655 RELTYPE_ABSOLUTE, realbytes,
657 } else if (wrt == aout_plt_sect + 1) {
660 "a.out format cannot produce non-PC-"
661 "relative PLT references");
664 "a.out format does not support this"
666 wrt = NO_SEG; /* we can at least _try_ to continue */
675 aout_sect_write(s, mydata, realbytes);
676 } else if (type == OUT_REL2ADR) {
677 if (segment == segto)
678 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
679 if (segment != NO_SEG && segment % 2) {
680 error(ERR_NONFATAL, "a.out format does not support"
681 " segment base references");
684 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
686 error(ERR_NONFATAL, "Linux a.out format does not support"
688 wrt = NO_SEG; /* we can at least _try_ to continue */
689 } else if (wrt == aout_plt_sect + 1) {
691 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
692 } else if (wrt == aout_gotpc_sect + 1 ||
693 wrt == aout_gotoff_sect + 1 ||
694 wrt == aout_got_sect + 1) {
695 error(ERR_NONFATAL, "a.out format cannot produce PC-"
696 "relative GOT references");
698 error(ERR_NONFATAL, "a.out format does not support this"
700 wrt = NO_SEG; /* we can at least _try_ to continue */
704 WRITESHORT(p, *(int32_t *)data - (realbytes + s->len));
705 aout_sect_write(s, mydata, 2L);
706 } else if (type == OUT_REL4ADR) {
707 if (segment == segto)
708 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
709 if (segment != NO_SEG && segment % 2) {
710 error(ERR_NONFATAL, "a.out format does not support"
711 " segment base references");
714 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
716 error(ERR_NONFATAL, "Linux a.out format does not support"
718 wrt = NO_SEG; /* we can at least _try_ to continue */
719 } else if (wrt == aout_plt_sect + 1) {
721 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
722 } else if (wrt == aout_gotpc_sect + 1 ||
723 wrt == aout_gotoff_sect + 1 ||
724 wrt == aout_got_sect + 1) {
725 error(ERR_NONFATAL, "a.out format cannot produce PC-"
726 "relative GOT references");
728 error(ERR_NONFATAL, "a.out format does not support this"
730 wrt = NO_SEG; /* we can at least _try_ to continue */
734 WRITELONG(p, *(int32_t *)data - (realbytes + s->len));
735 aout_sect_write(s, mydata, 4L);
739 static void aout_pad_sections(void)
741 static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
743 * Pad each of the text and data sections with NOPs until their
744 * length is a multiple of four. (NOP == 0x90.) Also increase
745 * the length of the BSS section similarly.
747 aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
748 aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
749 sbss.len = (sbss.len + 3) & ~3;
753 * a.out files have the curious property that all references to
754 * things in the data or bss sections are done by addresses which
755 * are actually relative to the start of the _text_ section, in the
756 * _file_. (No relation to what happens after linking. No idea why
757 * this should be so. It's very strange.) So we have to go through
758 * the relocation table, _after_ the final size of each section is
759 * known, and fix up the relocations pointed to.
761 static void aout_fixup_relocs(struct Section *sect)
765 saa_rewind(sect->data);
766 for (r = sect->head; r; r = r->next) {
767 uint8_t *p, *q, blk[4];
770 saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
774 l += ((int32_t)*p++) << 8;
776 l += ((int32_t)*p++) << 16;
777 l += ((int32_t)*p++) << 24;
780 if (r->symbol == -SECT_DATA)
782 else if (r->symbol == -SECT_BSS)
783 l += stext.len + sdata.len;
786 else if (r->bytes == 2)
790 saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
794 static void aout_write(void)
797 * Emit the a.out header.
799 /* OMAGIC, M_386 or MID_I386, no flags */
800 fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
801 fwriteint32_t(stext.len, aoutfp);
802 fwriteint32_t(sdata.len, aoutfp);
803 fwriteint32_t(sbss.len, aoutfp);
804 fwriteint32_t(nsyms * 12, aoutfp); /* length of symbol table */
805 fwriteint32_t(0L, aoutfp); /* object files have no entry point */
806 fwriteint32_t(stext.nrelocs * 8, aoutfp); /* size of text relocs */
807 fwriteint32_t(sdata.nrelocs * 8, aoutfp); /* size of data relocs */
810 * Write out the code section and the data section.
812 saa_fpwrite(stext.data, aoutfp);
813 saa_fpwrite(sdata.data, aoutfp);
816 * Write out the relocations.
818 aout_write_relocs(stext.head);
819 aout_write_relocs(sdata.head);
822 * Write the symbol table.
827 * And the string table.
829 fwriteint32_t(strslen + 4, aoutfp); /* length includes length count */
830 saa_fpwrite(strs, aoutfp);
833 static void aout_write_relocs(struct Reloc *r)
838 fwriteint32_t(r->address, aoutfp);
844 word2 |= r->reltype << 24;
845 word2 |= (r->bytes == 1 ? 0 :
846 r->bytes == 2 ? 0x2000000L : 0x4000000L);
847 fwriteint32_t(word2, aoutfp);
853 static void aout_write_syms(void)
858 for (i = 0; i < nsyms; i++) {
859 struct Symbol *sym = saa_rstruct(syms);
860 fwriteint32_t(sym->strpos, aoutfp);
861 fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, aoutfp);
863 * Fix up the symbol value now we know the final section
866 if ((sym->type & SECT_MASK) == SECT_DATA)
867 sym->value += stext.len;
868 if ((sym->type & SECT_MASK) == SECT_BSS)
869 sym->value += stext.len + sdata.len;
870 fwriteint32_t(sym->value, aoutfp);
872 * Output a size record if necessary.
874 if (sym->type & SYM_WITH_SIZE) {
875 fwriteint32_t(sym->strpos, aoutfp);
876 fwriteint32_t(0x0DL, aoutfp); /* special value: means size */
877 fwriteint32_t(sym->size, aoutfp);
878 i++; /* use up another of `nsyms' */
883 static void aout_sect_write(struct Section *sect,
884 const uint8_t *data, uint32_t len)
886 saa_wbytes(sect->data, data, len);
890 static int32_t aout_segbase(int32_t segment)
895 static int aout_directive(char *directive, char *value, int pass)
903 static void aout_filename(char *inname, char *outname, efunc error)
905 standard_extension(inname, outname, ".o", error);
908 static const char *aout_stdmac[] = {
909 "%define __SECT__ [section .text]",
910 "%macro __NASM_CDecl__ 1",
915 static int aout_set_info(enum geninfo type, char **val)
921 #endif /* OF_AOUT || OF_AOUTB */
925 struct ofmt of_aout = {
926 "Linux a.out object files",
947 struct ofmt of_aoutb = {
948 "NetBSD/FreeBSD a.out object files",