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.
19 #if defined OF_AOUT || defined OF_AOUTB
21 #define RELTYPE_ABSOLUTE 0x00
22 #define RELTYPE_RELATIVE 0x01
23 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
24 #define RELTYPE_GOTOFF 0x10
25 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
26 #define RELTYPE_PLT 0x21
27 #define RELTYPE_SYMFLAG 0x08
31 long address; /* relative to _start_ of section */
32 long symbol; /* symbol number or -ve section id */
33 int bytes; /* 2 or 4 */
34 int reltype; /* see above */
38 long strpos; /* string table position of name */
39 int type; /* symbol type - see flags below */
40 long value; /* address, or COMMON variable size */
41 long size; /* size for data or function exports */
42 long segment; /* back-reference used by gsym_reloc */
43 struct Symbol *next; /* list of globals in each section */
44 struct Symbol *nextfwd; /* list of unresolved-size symbols */
45 char *name; /* for unresolved-size symbols */
46 long symnum; /* index into symbol table */
50 * Section IDs - used in Reloc.symbol when negative, and in
51 * Symbol.type when positive.
53 #define SECT_ABS 2 /* absolute value */
54 #define SECT_TEXT 4 /* text section */
55 #define SECT_DATA 6 /* data section */
56 #define SECT_BSS 8 /* bss section */
57 #define SECT_MASK 0xE /* mask out any of the above */
60 * More flags used in Symbol.type.
62 #define SYM_GLOBAL 1 /* it's a global symbol */
63 #define SYM_DATA 0x100 /* used for shared libs */
64 #define SYM_FUNCTION 0x200 /* used for shared libs */
65 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
68 * Bit more explanation of symbol types: SECT_xxx denotes a local
69 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
70 * this module. Just SYM_GLOBAL, with zero value, denotes an
71 * external symbol referenced in this module. And just SYM_GLOBAL,
72 * but with a non-zero value, declares a C `common' variable, of
78 unsigned long len, size, nrelocs;
80 struct Reloc *head, **tail;
81 struct Symbol *gsyms, *asym;
84 static struct Section stext, sdata, sbss;
86 static struct SAA *syms;
87 static unsigned long nsyms;
89 static struct RAA *bsym;
91 static struct SAA *strs;
92 static unsigned long strslen;
94 static struct Symbol *fwds;
98 static evalfunc evaluate;
103 static void aout_write(void);
104 static void aout_write_relocs(struct Reloc *);
105 static void aout_write_syms(void);
106 static void aout_sect_write(struct Section *, unsigned char *, unsigned long);
107 static void aout_pad_sections(void);
108 static void aout_fixup_relocs(struct Section *);
111 * Special section numbers which are used to define special
112 * symbols, which can be used with WRT to provide PIC relocation
115 static long aout_gotpc_sect, aout_gotoff_sect;
116 static long aout_got_sect, aout_plt_sect;
117 static long aout_sym_sect;
119 static void aoutg_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
124 (void) ldef; /* placate optimisers */
125 stext.data = saa_init(1L); stext.head = NULL; stext.tail = &stext.head;
126 sdata.data = saa_init(1L); sdata.head = NULL; sdata.tail = &sdata.head;
127 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
128 stext.nrelocs = sdata.nrelocs = 0;
129 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
130 stext.index = seg_alloc();
131 sdata.index = seg_alloc();
132 sbss.index = seg_alloc();
133 stext.asym = sdata.asym = sbss.asym = NULL;
134 syms = saa_init((long)sizeof(struct Symbol));
144 static void aout_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
147 aoutg_init (fp, errfunc, ldef, eval);
149 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
150 aout_plt_sect = aout_sym_sect = NO_SEG;
157 extern struct ofmt of_aoutb;
159 static void aoutb_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
162 aoutg_init (fp, errfunc, ldef, eval);
164 is_pic = 0x00; /* may become 0x40 */
166 aout_gotpc_sect = seg_alloc();
167 ldef("..gotpc", aout_gotpc_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
168 aout_gotoff_sect = seg_alloc();
169 ldef("..gotoff", aout_gotoff_sect+1, 0L,NULL,FALSE,FALSE,&of_aoutb,error);
170 aout_got_sect = seg_alloc();
171 ldef("..got", aout_got_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
172 aout_plt_sect = seg_alloc();
173 ldef("..plt", aout_plt_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
174 aout_sym_sect = seg_alloc();
175 ldef("..sym", aout_sym_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
180 static void aout_cleanup(int debuginfo)
187 aout_fixup_relocs(&stext);
188 aout_fixup_relocs(&sdata);
191 saa_free (stext.data);
194 stext.head = stext.head->next;
197 saa_free (sdata.data);
200 sdata.head = sdata.head->next;
208 static long aout_section_names (char *name, int pass, int *bits)
211 * Default to 32 bits.
219 if (!strcmp(name, ".text"))
221 else if (!strcmp(name, ".data"))
223 else if (!strcmp(name, ".bss"))
229 static void aout_deflabel (char *name, long segment, long offset,
230 int is_global, char *special)
234 int special_used = FALSE;
236 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
238 * This is a NASM special symbol. We never allow it into
239 * the a.out symbol table, even if it's a valid one. If it
240 * _isn't_ a valid one, we should barf immediately.
242 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
243 strcmp(name, "..got") && strcmp(name, "..plt") &&
244 strcmp(name, "..sym"))
245 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
249 if (is_global == 3) {
252 * Fix up a forward-reference symbol size from the first
255 for (s = &fwds; *s; s = &(*s)->nextfwd)
256 if (!strcmp((*s)->name, name)) {
257 struct tokenval tokval;
261 while (*p && !isspace(*p)) p++;
262 while (*p && isspace(*p)) p++;
265 tokval.t_type = TOKEN_INVALID;
266 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
269 error (ERR_NONFATAL, "cannot use relocatable"
270 " expression as symbol size");
272 (*s)->size = reloc_value(e);
276 * Remove it from the list of unresolved sizes.
278 nasm_free ((*s)->name);
282 return; /* it wasn't an important one */
285 saa_wbytes (strs, name, (long)(1+strlen(name)));
286 strslen += 1+strlen(name);
288 sym = saa_wstruct (syms);
291 sym->type = is_global ? SYM_GLOBAL : 0;
292 sym->segment = segment;
293 if (segment == NO_SEG)
294 sym->type |= SECT_ABS;
295 else if (segment == stext.index) {
296 sym->type |= SECT_TEXT;
298 sym->next = stext.gsyms;
300 } else if (!stext.asym)
302 } else if (segment == sdata.index) {
303 sym->type |= SECT_DATA;
305 sym->next = sdata.gsyms;
307 } else if (!sdata.asym)
309 } else if (segment == sbss.index) {
310 sym->type |= SECT_BSS;
312 sym->next = sbss.gsyms;
314 } else if (!sbss.asym)
317 sym->type = SYM_GLOBAL;
321 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
323 if (is_global && sym->type != SYM_GLOBAL) {
325 * Global symbol exported _from_ this module. We must check
326 * the special text for type information.
330 int n = strcspn(special, " ");
332 if (!nasm_strnicmp(special, "function", n))
333 sym->type |= SYM_FUNCTION;
334 else if (!nasm_strnicmp(special, "data", n) ||
335 !nasm_strnicmp(special, "object", n))
336 sym->type |= SYM_DATA;
338 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
341 struct tokenval tokval;
344 char *saveme=stdscan_bufptr; /* bugfix? fbk 8/10/00 */
347 error(ERR_NONFATAL, "Linux a.out does not support"
348 " symbol size information");
350 while (special[n] && isspace(special[n]))
353 * We have a size expression; attempt to
356 sym->type |= SYM_WITH_SIZE;
358 stdscan_bufptr = special+n;
359 tokval.t_type = TOKEN_INVALID;
360 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error, NULL);
364 sym->name = nasm_strdup(name);
367 error (ERR_NONFATAL, "cannot use relocatable"
368 " expression as symbol size");
370 sym->size = reloc_value(e);
373 stdscan_bufptr=saveme; /* bugfix? fbk 8/10/00 */
380 * define the references from external-symbol segment numbers
381 * to these symbol records.
383 if (segment != NO_SEG && segment != stext.index &&
384 segment != sdata.index && segment != sbss.index)
385 bsym = raa_write (bsym, segment, nsyms);
389 if (sym->type & SYM_WITH_SIZE)
390 nsyms++; /* and another for the size */
392 if (special && !special_used)
393 error(ERR_NONFATAL, "no special symbol features supported here");
396 static void aout_add_reloc (struct Section *sect, long segment,
397 int reltype, int bytes)
401 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
402 sect->tail = &r->next;
405 r->address = sect->len;
406 r->symbol = (segment == NO_SEG ? -SECT_ABS :
407 segment == stext.index ? -SECT_TEXT :
408 segment == sdata.index ? -SECT_DATA :
409 segment == sbss.index ? -SECT_BSS :
410 raa_read(bsym, segment));
411 r->reltype = reltype;
413 r->reltype |= RELTYPE_SYMFLAG;
420 * This routine deals with ..got and ..sym relocations: the more
421 * complicated kinds. In shared-library writing, some relocations
422 * with respect to global symbols must refer to the precise symbol
423 * rather than referring to an offset from the base of the section
424 * _containing_ the symbol. Such relocations call to this routine,
425 * which searches the symbol list for the symbol in question.
427 * RELTYPE_GOT references require the _exact_ symbol address to be
428 * used; RELTYPE_ABSOLUTE references can be at an offset from the
429 * symbol. The boolean argument `exact' tells us this.
431 * Return value is the adjusted value of `addr', having become an
432 * offset from the symbol rather than the section. Should always be
433 * zero when returning from an exact call.
435 * Limitation: if you define two symbols at the same place,
436 * confusion will occur.
438 * Inefficiency: we search, currently, using a linked list which
439 * isn't even necessarily sorted.
441 static long aout_add_gsym_reloc (struct Section *sect,
442 long segment, long offset,
443 int type, int bytes, int exact)
445 struct Symbol *sym, *sm, *shead;
449 * First look up the segment to find whether it's text, data,
450 * bss or an external symbol.
453 if (segment == stext.index)
455 else if (segment == sdata.index)
457 else if (segment == sbss.index)
460 if (exact && offset != 0)
461 error (ERR_NONFATAL, "unable to find a suitable global symbol"
462 " for this reference");
464 aout_add_reloc (sect, segment, type, bytes);
470 * Find a symbol pointing _exactly_ at this one.
472 for (sym = shead; sym; sym = sym->next)
473 if (sym->value == offset)
477 * Find the nearest symbol below this one.
480 for (sm = shead; sm; sm = sm->next)
481 if (sm->value <= offset && (!sym || sm->value > sym->value))
485 error (ERR_NONFATAL, "unable to find a suitable global symbol"
486 " for this reference");
490 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
491 sect->tail = &r->next;
494 r->address = sect->len;
495 r->symbol = sym->symnum;
496 r->reltype = type | RELTYPE_SYMFLAG;
501 return offset - sym->value;
505 * This routine deals with ..gotoff relocations. These _must_ refer
506 * to a symbol, due to a perversity of *BSD's PIC implementation,
507 * and it must be a non-global one as well; so we store `asym', the
508 * first nonglobal symbol defined in each section, and always work
509 * from that. Relocation type is always RELTYPE_GOTOFF.
511 * Return value is the adjusted value of `addr', having become an
512 * offset from the `asym' symbol rather than the section.
514 static long aout_add_gotoff_reloc (struct Section *sect, long segment,
515 long offset, int bytes)
521 * First look up the segment to find whether it's text, data,
522 * bss or an external symbol.
525 if (segment == stext.index)
527 else if (segment == sdata.index)
529 else if (segment == sbss.index)
532 error (ERR_NONFATAL, "`..gotoff' relocations require a non-global"
533 " symbol in the section");
535 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
536 sect->tail = &r->next;
539 r->address = sect->len;
540 r->symbol = asym->symnum;
541 r->reltype = RELTYPE_GOTOFF;
546 return offset - asym->value;
549 static void aout_out (long segto, void *data, unsigned long type,
550 long segment, long wrt)
553 long realbytes = type & OUT_SIZMASK;
555 unsigned char mydata[4], *p;
560 * handle absolute-assembly (structure definitions)
562 if (segto == NO_SEG) {
563 if (type != OUT_RESERVE)
564 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
569 if (segto == stext.index)
571 else if (segto == sdata.index)
573 else if (segto == sbss.index)
576 error(ERR_WARNING, "attempt to assemble code in"
577 " segment %d: defaulting to `.text'", segto);
581 if (!s && type != OUT_RESERVE) {
582 error(ERR_WARNING, "attempt to initialise memory in the"
583 " BSS section: ignored");
584 if (type == OUT_REL2ADR)
586 else if (type == OUT_REL4ADR)
588 sbss.len += realbytes;
592 if (type == OUT_RESERVE) {
594 error(ERR_WARNING, "uninitialised space declared in"
595 " %s section: zeroing",
596 (segto == stext.index ? "code" : "data"));
597 aout_sect_write (s, NULL, realbytes);
599 sbss.len += realbytes;
600 } else if (type == OUT_RAWDATA) {
601 if (segment != NO_SEG)
602 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
603 aout_sect_write (s, data, realbytes);
604 } else if (type == OUT_ADDRESS) {
605 addr = *(long *)data;
606 if (segment != NO_SEG) {
608 error(ERR_NONFATAL, "a.out format does not support"
609 " segment base references");
612 aout_add_reloc (s, segment, RELTYPE_ABSOLUTE, realbytes);
614 error (ERR_NONFATAL, "Linux a.out format does not support"
616 wrt = NO_SEG; /* we can at least _try_ to continue */
617 } else if (wrt == aout_gotpc_sect+1) {
619 aout_add_reloc (s, segment, RELTYPE_GOTPC, realbytes);
620 } else if (wrt == aout_gotoff_sect+1) {
622 addr = aout_add_gotoff_reloc (s, segment,
624 } else if (wrt == aout_got_sect+1) {
626 addr = aout_add_gsym_reloc (s, segment, addr, RELTYPE_GOT,
628 } else if (wrt == aout_sym_sect+1) {
629 addr = aout_add_gsym_reloc (s, segment, addr,
630 RELTYPE_ABSOLUTE, realbytes,
632 } else if (wrt == aout_plt_sect+1) {
634 error(ERR_NONFATAL, "a.out format cannot produce non-PC-"
635 "relative PLT references");
637 error (ERR_NONFATAL, "a.out format does not support this"
639 wrt = NO_SEG; /* we can at least _try_ to continue */
645 WRITESHORT (p, addr);
648 aout_sect_write (s, mydata, realbytes);
649 } else if (type == OUT_REL2ADR) {
650 if (segment == segto)
651 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
652 if (segment != NO_SEG && segment % 2) {
653 error(ERR_NONFATAL, "a.out format does not support"
654 " segment base references");
657 aout_add_reloc (s, segment, RELTYPE_RELATIVE, 2);
659 error (ERR_NONFATAL, "Linux a.out format does not support"
661 wrt = NO_SEG; /* we can at least _try_ to continue */
662 } else if (wrt == aout_plt_sect+1) {
664 aout_add_reloc (s, segment, RELTYPE_PLT, 2);
665 } else if (wrt == aout_gotpc_sect+1 ||
666 wrt == aout_gotoff_sect+1 ||
667 wrt == aout_got_sect+1) {
668 error(ERR_NONFATAL, "a.out format cannot produce PC-"
669 "relative GOT references");
671 error (ERR_NONFATAL, "a.out format does not support this"
673 wrt = NO_SEG; /* we can at least _try_ to continue */
677 WRITESHORT (p, *(long*)data-(realbytes + s->len));
678 aout_sect_write (s, mydata, 2L);
679 } else if (type == OUT_REL4ADR) {
680 if (segment == segto)
681 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
682 if (segment != NO_SEG && segment % 2) {
683 error(ERR_NONFATAL, "a.out format does not support"
684 " segment base references");
687 aout_add_reloc (s, segment, RELTYPE_RELATIVE, 4);
689 error (ERR_NONFATAL, "Linux a.out format does not support"
691 wrt = NO_SEG; /* we can at least _try_ to continue */
692 } else if (wrt == aout_plt_sect+1) {
694 aout_add_reloc (s, segment, RELTYPE_PLT, 4);
695 } else if (wrt == aout_gotpc_sect+1 ||
696 wrt == aout_gotoff_sect+1 ||
697 wrt == aout_got_sect+1) {
698 error(ERR_NONFATAL, "a.out format cannot produce PC-"
699 "relative GOT references");
701 error (ERR_NONFATAL, "a.out format does not support this"
703 wrt = NO_SEG; /* we can at least _try_ to continue */
707 WRITELONG (p, *(long*)data-(realbytes + s->len));
708 aout_sect_write (s, mydata, 4L);
712 static void aout_pad_sections(void)
714 static unsigned char pad[] = { 0x90, 0x90, 0x90, 0x90 };
716 * Pad each of the text and data sections with NOPs until their
717 * length is a multiple of four. (NOP == 0x90.) Also increase
718 * the length of the BSS section similarly.
720 aout_sect_write (&stext, pad, (-(long)stext.len) & 3);
721 aout_sect_write (&sdata, pad, (-(long)sdata.len) & 3);
722 sbss.len = (sbss.len + 3) & ~3;
726 * a.out files have the curious property that all references to
727 * things in the data or bss sections are done by addresses which
728 * are actually relative to the start of the _text_ section, in the
729 * _file_. (No relation to what happens after linking. No idea why
730 * this should be so. It's very strange.) So we have to go through
731 * the relocation table, _after_ the final size of each section is
732 * known, and fix up the relocations pointed to.
734 static void aout_fixup_relocs(struct Section *sect)
738 saa_rewind (sect->data);
739 for (r = sect->head; r; r = r->next) {
740 unsigned char *p, *q, blk[4];
743 saa_fread (sect->data, r->address, blk, (long)r->bytes);
747 l += ((long)*p++) << 8;
749 l += ((long)*p++) << 16;
750 l += ((long)*p++) << 24;
753 if (r->symbol == -SECT_DATA)
755 else if (r->symbol == -SECT_BSS)
756 l += stext.len + sdata.len;
759 else if (r->bytes == 2)
763 saa_fwrite (sect->data, r->address, blk, (long)r->bytes);
767 static void aout_write(void)
770 * Emit the a.out header.
772 /* OMAGIC, M_386 or MID_I386, no flags */
773 fwritelong (bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
774 fwritelong (stext.len, aoutfp);
775 fwritelong (sdata.len, aoutfp);
776 fwritelong (sbss.len, aoutfp);
777 fwritelong (nsyms * 12, aoutfp); /* length of symbol table */
778 fwritelong (0L, aoutfp); /* object files have no entry point */
779 fwritelong (stext.nrelocs * 8, aoutfp); /* size of text relocs */
780 fwritelong (sdata.nrelocs * 8, aoutfp); /* size of data relocs */
783 * Write out the code section and the data section.
785 saa_fpwrite (stext.data, aoutfp);
786 saa_fpwrite (sdata.data, aoutfp);
789 * Write out the relocations.
791 aout_write_relocs (stext.head);
792 aout_write_relocs (sdata.head);
795 * Write the symbol table.
800 * And the string table.
802 fwritelong (strslen+4, aoutfp); /* length includes length count */
803 saa_fpwrite (strs, aoutfp);
806 static void aout_write_relocs (struct Reloc *r)
811 fwritelong (r->address, aoutfp);
817 word2 |= r->reltype << 24;
818 word2 |= (r->bytes == 1 ? 0 :
819 r->bytes == 2 ? 0x2000000L : 0x4000000L);
820 fwritelong (word2, aoutfp);
826 static void aout_write_syms (void)
831 for (i = 0; i < nsyms; i++) {
832 struct Symbol *sym = saa_rstruct(syms);
833 fwritelong (sym->strpos, aoutfp);
834 fwritelong ((long)sym->type & ~SYM_WITH_SIZE, aoutfp);
836 * Fix up the symbol value now we know the final section
839 if ((sym->type & SECT_MASK) == SECT_DATA)
840 sym->value += stext.len;
841 if ((sym->type & SECT_MASK) == SECT_BSS)
842 sym->value += stext.len + sdata.len;
843 fwritelong (sym->value, aoutfp);
845 * Output a size record if necessary.
847 if (sym->type & SYM_WITH_SIZE) {
848 fwritelong(sym->strpos, aoutfp);
849 fwritelong(0x0DL, aoutfp); /* special value: means size */
850 fwritelong(sym->size, aoutfp);
851 i++; /* use up another of `nsyms' */
856 static void aout_sect_write (struct Section *sect,
857 unsigned char *data, unsigned long len)
859 saa_wbytes (sect->data, data, len);
863 static long aout_segbase (long segment)
868 static int aout_directive (char *directive, char *value, int pass)
873 static void aout_filename (char *inname, char *outname, efunc error)
875 standard_extension (inname, outname, ".o", error);
878 static char *aout_stdmac[] = {
879 "%define __SECT__ [section .text]",
880 "%macro __NASM_CDecl__ 1",
885 static int aout_set_info(enum geninfo type, char **val)
889 #endif /* OF_AOUT || OF_AOUTB */
893 struct ofmt of_aout = {
894 "Linux a.out object files",
915 struct ofmt of_aoutb = {
916 "NetBSD/FreeBSD a.out object files",