NASM 0.98.09
[nasm.git] / outaout.c
blobb8e74be53027b617914305eeda1b6c9e02d25151
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.
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 #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
29 struct Reloc {
30 struct Reloc *next;
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 */
37 struct Symbol {
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
73 * size `value'.
76 struct Section {
77 struct SAA *data;
78 unsigned long len, size, nrelocs;
79 long index;
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;
96 static FILE *aoutfp;
97 static efunc error;
98 static evalfunc evaluate;
100 static int bsd;
101 static int is_pic;
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
113 * types.
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)
121 aoutfp = fp;
122 error = errfunc;
123 evaluate = 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));
135 nsyms = 0;
136 bsym = raa_init();
137 strs = saa_init(1L);
138 strslen = 0;
139 fwds = NULL;
142 #ifdef OF_AOUT
144 static void aout_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
146 bsd = FALSE;
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;
153 #endif
155 #ifdef OF_AOUTB
157 extern struct ofmt of_aoutb;
159 static void aoutb_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
161 bsd = TRUE;
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);
178 #endif
180 static void aout_cleanup(int debuginfo)
182 struct Reloc *r;
184 (void) debuginfo;
186 aout_pad_sections();
187 aout_fixup_relocs(&stext);
188 aout_fixup_relocs(&sdata);
189 aout_write();
190 fclose (aoutfp);
191 saa_free (stext.data);
192 while (stext.head) {
193 r = stext.head;
194 stext.head = stext.head->next;
195 nasm_free (r);
197 saa_free (sdata.data);
198 while (sdata.head) {
199 r = sdata.head;
200 sdata.head = sdata.head->next;
201 nasm_free (r);
203 saa_free (syms);
204 raa_free (bsym);
205 saa_free (strs);
208 static long aout_section_names (char *name, int pass, int *bits)
211 * Default to 32 bits.
213 if (!name)
214 *bits = 32;
216 if (!name)
217 return stext.index;
219 if (!strcmp(name, ".text"))
220 return stext.index;
221 else if (!strcmp(name, ".data"))
222 return sdata.index;
223 else if (!strcmp(name, ".bss"))
224 return sbss.index;
225 else
226 return NO_SEG;
229 static void aout_deflabel (char *name, long segment, long offset,
230 int is_global, char *special)
232 int pos = strslen+4;
233 struct Symbol *sym;
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);
246 return;
249 if (is_global == 3) {
250 struct Symbol **s;
252 * Fix up a forward-reference symbol size from the first
253 * pass.
255 for (s = &fwds; *s; s = &(*s)->nextfwd)
256 if (!strcmp((*s)->name, name)) {
257 struct tokenval tokval;
258 expr *e;
259 char *p = special;
261 while (*p && !isspace(*p)) p++;
262 while (*p && isspace(*p)) p++;
263 stdscan_reset();
264 stdscan_bufptr = p;
265 tokval.t_type = TOKEN_INVALID;
266 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
267 if (e) {
268 if (!is_simple(e))
269 error (ERR_NONFATAL, "cannot use relocatable"
270 " expression as symbol size");
271 else
272 (*s)->size = reloc_value(e);
276 * Remove it from the list of unresolved sizes.
278 nasm_free ((*s)->name);
279 *s = (*s)->nextfwd;
280 return;
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);
290 sym->strpos = pos;
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;
297 if (is_global) {
298 sym->next = stext.gsyms;
299 stext.gsyms = sym;
300 } else if (!stext.asym)
301 stext.asym = sym;
302 } else if (segment == sdata.index) {
303 sym->type |= SECT_DATA;
304 if (is_global) {
305 sym->next = sdata.gsyms;
306 sdata.gsyms = sym;
307 } else if (!sdata.asym)
308 sdata.asym = sym;
309 } else if (segment == sbss.index) {
310 sym->type |= SECT_BSS;
311 if (is_global) {
312 sym->next = sbss.gsyms;
313 sbss.gsyms = sym;
314 } else if (!sbss.asym)
315 sbss.asym = sym;
316 } else
317 sym->type = SYM_GLOBAL;
318 if (is_global == 2)
319 sym->value = offset;
320 else
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.
329 if (special) {
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;
337 else
338 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
339 n, special);
340 if (special[n]) {
341 struct tokenval tokval;
342 expr *e;
343 int fwd = FALSE;
344 char *saveme=stdscan_bufptr; /* bugfix? fbk 8/10/00 */
346 if (!bsd) {
347 error(ERR_NONFATAL, "Linux a.out does not support"
348 " symbol size information");
349 } else {
350 while (special[n] && isspace(special[n]))
351 n++;
353 * We have a size expression; attempt to
354 * evaluate it.
356 sym->type |= SYM_WITH_SIZE;
357 stdscan_reset();
358 stdscan_bufptr = special+n;
359 tokval.t_type = TOKEN_INVALID;
360 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error, NULL);
361 if (fwd) {
362 sym->nextfwd = fwds;
363 fwds = sym;
364 sym->name = nasm_strdup(name);
365 } else if (e) {
366 if (!is_simple(e))
367 error (ERR_NONFATAL, "cannot use relocatable"
368 " expression as symbol size");
369 else
370 sym->size = reloc_value(e);
373 stdscan_bufptr=saveme; /* bugfix? fbk 8/10/00 */
375 special_used = TRUE;
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);
386 sym->symnum = nsyms;
388 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)
399 struct Reloc *r;
401 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
402 sect->tail = &r->next;
403 r->next = NULL;
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;
412 if (r->symbol >= 0)
413 r->reltype |= RELTYPE_SYMFLAG;
414 r->bytes = bytes;
416 sect->nrelocs++;
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;
446 struct Reloc *r;
449 * First look up the segment to find whether it's text, data,
450 * bss or an external symbol.
452 shead = NULL;
453 if (segment == stext.index)
454 shead = stext.gsyms;
455 else if (segment == sdata.index)
456 shead = sdata.gsyms;
457 else if (segment == sbss.index)
458 shead = sbss.gsyms;
459 if (!shead) {
460 if (exact && offset != 0)
461 error (ERR_NONFATAL, "unable to find a suitable global symbol"
462 " for this reference");
463 else
464 aout_add_reloc (sect, segment, type, bytes);
465 return offset;
468 if (exact) {
470 * Find a symbol pointing _exactly_ at this one.
472 for (sym = shead; sym; sym = sym->next)
473 if (sym->value == offset)
474 break;
475 } else {
477 * Find the nearest symbol below this one.
479 sym = NULL;
480 for (sm = shead; sm; sm = sm->next)
481 if (sm->value <= offset && (!sym || sm->value > sym->value))
482 sym = sm;
484 if (!sym && exact) {
485 error (ERR_NONFATAL, "unable to find a suitable global symbol"
486 " for this reference");
487 return 0;
490 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
491 sect->tail = &r->next;
492 r->next = NULL;
494 r->address = sect->len;
495 r->symbol = sym->symnum;
496 r->reltype = type | RELTYPE_SYMFLAG;
497 r->bytes = bytes;
499 sect->nrelocs++;
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)
517 struct Reloc *r;
518 struct Symbol *asym;
521 * First look up the segment to find whether it's text, data,
522 * bss or an external symbol.
524 asym = NULL;
525 if (segment == stext.index)
526 asym = stext.asym;
527 else if (segment == sdata.index)
528 asym = sdata.asym;
529 else if (segment == sbss.index)
530 asym = sbss.asym;
531 if (!asym)
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;
537 r->next = NULL;
539 r->address = sect->len;
540 r->symbol = asym->symnum;
541 r->reltype = RELTYPE_GOTOFF;
542 r->bytes = bytes;
544 sect->nrelocs++;
546 return offset - asym->value;
549 static void aout_out (long segto, void *data, unsigned long type,
550 long segment, long wrt)
552 struct Section *s;
553 long realbytes = type & OUT_SIZMASK;
554 long addr;
555 unsigned char mydata[4], *p;
557 type &= OUT_TYPMASK;
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]"
565 " space");
566 return;
569 if (segto == stext.index)
570 s = &stext;
571 else if (segto == sdata.index)
572 s = &sdata;
573 else if (segto == sbss.index)
574 s = NULL;
575 else {
576 error(ERR_WARNING, "attempt to assemble code in"
577 " segment %d: defaulting to `.text'", segto);
578 s = &stext;
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)
585 realbytes = 2;
586 else if (type == OUT_REL4ADR)
587 realbytes = 4;
588 sbss.len += realbytes;
589 return;
592 if (type == OUT_RESERVE) {
593 if (s) {
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);
598 } else
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) {
607 if (segment % 2) {
608 error(ERR_NONFATAL, "a.out format does not support"
609 " segment base references");
610 } else {
611 if (wrt == NO_SEG) {
612 aout_add_reloc (s, segment, RELTYPE_ABSOLUTE, realbytes);
613 } else if (!bsd) {
614 error (ERR_NONFATAL, "Linux a.out format does not support"
615 " any use of WRT");
616 wrt = NO_SEG; /* we can at least _try_ to continue */
617 } else if (wrt == aout_gotpc_sect+1) {
618 is_pic = 0x40;
619 aout_add_reloc (s, segment, RELTYPE_GOTPC, realbytes);
620 } else if (wrt == aout_gotoff_sect+1) {
621 is_pic = 0x40;
622 addr = aout_add_gotoff_reloc (s, segment,
623 addr, realbytes);
624 } else if (wrt == aout_got_sect+1) {
625 is_pic = 0x40;
626 addr = aout_add_gsym_reloc (s, segment, addr, RELTYPE_GOT,
627 realbytes, TRUE);
628 } else if (wrt == aout_sym_sect+1) {
629 addr = aout_add_gsym_reloc (s, segment, addr,
630 RELTYPE_ABSOLUTE, realbytes,
631 FALSE);
632 } else if (wrt == aout_plt_sect+1) {
633 is_pic = 0x40;
634 error(ERR_NONFATAL, "a.out format cannot produce non-PC-"
635 "relative PLT references");
636 } else {
637 error (ERR_NONFATAL, "a.out format does not support this"
638 " use of WRT");
639 wrt = NO_SEG; /* we can at least _try_ to continue */
643 p = mydata;
644 if (realbytes == 2)
645 WRITESHORT (p, addr);
646 else
647 WRITELONG (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");
655 } else {
656 if (wrt == NO_SEG) {
657 aout_add_reloc (s, segment, RELTYPE_RELATIVE, 2);
658 } else if (!bsd) {
659 error (ERR_NONFATAL, "Linux a.out format does not support"
660 " any use of WRT");
661 wrt = NO_SEG; /* we can at least _try_ to continue */
662 } else if (wrt == aout_plt_sect+1) {
663 is_pic = 0x40;
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");
670 } else {
671 error (ERR_NONFATAL, "a.out format does not support this"
672 " use of WRT");
673 wrt = NO_SEG; /* we can at least _try_ to continue */
676 p = mydata;
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");
685 } else {
686 if (wrt == NO_SEG) {
687 aout_add_reloc (s, segment, RELTYPE_RELATIVE, 4);
688 } else if (!bsd) {
689 error (ERR_NONFATAL, "Linux a.out format does not support"
690 " any use of WRT");
691 wrt = NO_SEG; /* we can at least _try_ to continue */
692 } else if (wrt == aout_plt_sect+1) {
693 is_pic = 0x40;
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");
700 } else {
701 error (ERR_NONFATAL, "a.out format does not support this"
702 " use of WRT");
703 wrt = NO_SEG; /* we can at least _try_ to continue */
706 p = mydata;
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)
736 struct Reloc *r;
738 saa_rewind (sect->data);
739 for (r = sect->head; r; r = r->next) {
740 unsigned char *p, *q, blk[4];
741 long l;
743 saa_fread (sect->data, r->address, blk, (long)r->bytes);
744 p = q = blk;
745 l = *p++;
746 if (r->bytes > 1) {
747 l += ((long)*p++) << 8;
748 if (r->bytes == 4) {
749 l += ((long)*p++) << 16;
750 l += ((long)*p++) << 24;
753 if (r->symbol == -SECT_DATA)
754 l += stext.len;
755 else if (r->symbol == -SECT_BSS)
756 l += stext.len + sdata.len;
757 if (r->bytes == 4)
758 WRITELONG(q, l);
759 else if (r->bytes == 2)
760 WRITESHORT(q, l);
761 else
762 *q++ = l & 0xFF;
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.
797 aout_write_syms ();
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)
808 while (r) {
809 unsigned long word2;
811 fwritelong (r->address, aoutfp);
813 if (r->symbol >= 0)
814 word2 = r->symbol;
815 else
816 word2 = -r->symbol;
817 word2 |= r->reltype << 24;
818 word2 |= (r->bytes == 1 ? 0 :
819 r->bytes == 2 ? 0x2000000L : 0x4000000L);
820 fwritelong (word2, aoutfp);
822 r = r->next;
826 static void aout_write_syms (void)
828 unsigned long i;
830 saa_rewind (syms);
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
837 * sizes.
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);
860 sect->len += len;
863 static long aout_segbase (long segment)
865 return segment;
868 static int aout_directive (char *directive, char *value, int pass)
870 return 0;
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",
881 "%endmacro",
882 NULL
885 static int aout_set_info(enum geninfo type, char **val)
887 return 0;
889 #endif /* OF_AOUT || OF_AOUTB */
891 #ifdef OF_AOUT
893 struct ofmt of_aout = {
894 "Linux a.out object files",
895 "aout",
896 NULL,
897 null_debug_arr,
898 &null_debug_form,
899 aout_stdmac,
900 aout_init,
901 aout_set_info,
902 aout_out,
903 aout_deflabel,
904 aout_section_names,
905 aout_segbase,
906 aout_directive,
907 aout_filename,
908 aout_cleanup
911 #endif
913 #ifdef OF_AOUTB
915 struct ofmt of_aoutb = {
916 "NetBSD/FreeBSD a.out object files",
917 "aoutb",
918 NULL,
919 null_debug_arr,
920 &null_debug_form,
921 aout_stdmac,
922 aoutb_init,
923 aout_set_info,
924 aout_out,
925 aout_deflabel,
926 aout_section_names,
927 aout_segbase,
928 aout_directive,
929 aout_filename,
930 aout_cleanup
933 #endif