Move all the SAA code out of nasmlib
[nasm/nasm.git] / output / outaout.c
blobd0f29a642bab8760fa138ec51422e75b0e12b2d2
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 license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "saa.h"
21 #include "stdscan.h"
22 #include "outform.h"
24 #if defined OF_AOUT || defined OF_AOUTB
26 #define RELTYPE_ABSOLUTE 0x00
27 #define RELTYPE_RELATIVE 0x01
28 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
29 #define RELTYPE_GOTOFF 0x10
30 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
31 #define RELTYPE_PLT 0x21
32 #define RELTYPE_SYMFLAG 0x08
34 struct Reloc {
35 struct Reloc *next;
36 int32_t address; /* relative to _start_ of section */
37 int32_t symbol; /* symbol number or -ve section id */
38 int bytes; /* 2 or 4 */
39 int reltype; /* see above */
42 struct Symbol {
43 int32_t strpos; /* string table position of name */
44 int type; /* symbol type - see flags below */
45 int32_t value; /* address, or COMMON variable size */
46 int32_t size; /* size for data or function exports */
47 int32_t segment; /* back-reference used by gsym_reloc */
48 struct Symbol *next; /* list of globals in each section */
49 struct Symbol *nextfwd; /* list of unresolved-size symbols */
50 char *name; /* for unresolved-size symbols */
51 int32_t symnum; /* index into symbol table */
55 * Section IDs - used in Reloc.symbol when negative, and in
56 * Symbol.type when positive.
58 #define SECT_ABS 2 /* absolute value */
59 #define SECT_TEXT 4 /* text section */
60 #define SECT_DATA 6 /* data section */
61 #define SECT_BSS 8 /* bss section */
62 #define SECT_MASK 0xE /* mask out any of the above */
65 * More flags used in Symbol.type.
67 #define SYM_GLOBAL 1 /* it's a global symbol */
68 #define SYM_DATA 0x100 /* used for shared libs */
69 #define SYM_FUNCTION 0x200 /* used for shared libs */
70 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
73 * Bit more explanation of symbol types: SECT_xxx denotes a local
74 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
75 * this module. Just SYM_GLOBAL, with zero value, denotes an
76 * external symbol referenced in this module. And just SYM_GLOBAL,
77 * but with a non-zero value, declares a C `common' variable, of
78 * size `value'.
81 struct Section {
82 struct SAA *data;
83 uint32_t len, size, nrelocs;
84 int32_t index;
85 struct Reloc *head, **tail;
86 struct Symbol *gsyms, *asym;
89 static struct Section stext, sdata, sbss;
91 static struct SAA *syms;
92 static uint32_t nsyms;
94 static struct RAA *bsym;
96 static struct SAA *strs;
97 static uint32_t strslen;
99 static struct Symbol *fwds;
101 static FILE *aoutfp;
102 static efunc error;
103 static evalfunc evaluate;
105 static int bsd;
106 static int is_pic;
108 static void aout_write(void);
109 static void aout_write_relocs(struct Reloc *);
110 static void aout_write_syms(void);
111 static void aout_sect_write(struct Section *, const uint8_t *,
112 uint32_t);
113 static void aout_pad_sections(void);
114 static void aout_fixup_relocs(struct Section *);
117 * Special section numbers which are used to define special
118 * symbols, which can be used with WRT to provide PIC relocation
119 * types.
121 static int32_t aout_gotpc_sect, aout_gotoff_sect;
122 static int32_t aout_got_sect, aout_plt_sect;
123 static int32_t aout_sym_sect;
125 static void aoutg_init(FILE * fp, efunc errfunc, ldfunc ldef,
126 evalfunc eval)
128 aoutfp = fp;
129 error = errfunc;
130 evaluate = eval;
131 (void)ldef; /* placate optimisers */
132 stext.data = saa_init(1L);
133 stext.head = NULL;
134 stext.tail = &stext.head;
135 sdata.data = saa_init(1L);
136 sdata.head = NULL;
137 sdata.tail = &sdata.head;
138 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
139 stext.nrelocs = sdata.nrelocs = 0;
140 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
141 stext.index = seg_alloc();
142 sdata.index = seg_alloc();
143 sbss.index = seg_alloc();
144 stext.asym = sdata.asym = sbss.asym = NULL;
145 syms = saa_init((int32_t)sizeof(struct Symbol));
146 nsyms = 0;
147 bsym = raa_init();
148 strs = saa_init(1L);
149 strslen = 0;
150 fwds = NULL;
153 #ifdef OF_AOUT
155 static void aout_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
157 bsd = false;
158 aoutg_init(fp, errfunc, ldef, eval);
160 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
161 aout_plt_sect = aout_sym_sect = NO_SEG;
164 #endif
166 #ifdef OF_AOUTB
168 extern struct ofmt of_aoutb;
170 static void aoutb_init(FILE * fp, efunc errfunc, ldfunc ldef,
171 evalfunc eval)
173 bsd = true;
174 aoutg_init(fp, errfunc, ldef, eval);
176 is_pic = 0x00; /* may become 0x40 */
178 aout_gotpc_sect = seg_alloc();
179 ldef("..gotpc", aout_gotpc_sect + 1, 0L, NULL, false, false, &of_aoutb,
180 error);
181 aout_gotoff_sect = seg_alloc();
182 ldef("..gotoff", aout_gotoff_sect + 1, 0L, NULL, false, false,
183 &of_aoutb, error);
184 aout_got_sect = seg_alloc();
185 ldef("..got", aout_got_sect + 1, 0L, NULL, false, false, &of_aoutb,
186 error);
187 aout_plt_sect = seg_alloc();
188 ldef("..plt", aout_plt_sect + 1, 0L, NULL, false, false, &of_aoutb,
189 error);
190 aout_sym_sect = seg_alloc();
191 ldef("..sym", aout_sym_sect + 1, 0L, NULL, false, false, &of_aoutb,
192 error);
195 #endif
197 static void aout_cleanup(int debuginfo)
199 struct Reloc *r;
201 (void)debuginfo;
203 aout_pad_sections();
204 aout_fixup_relocs(&stext);
205 aout_fixup_relocs(&sdata);
206 aout_write();
207 fclose(aoutfp);
208 saa_free(stext.data);
209 while (stext.head) {
210 r = stext.head;
211 stext.head = stext.head->next;
212 nasm_free(r);
214 saa_free(sdata.data);
215 while (sdata.head) {
216 r = sdata.head;
217 sdata.head = sdata.head->next;
218 nasm_free(r);
220 saa_free(syms);
221 raa_free(bsym);
222 saa_free(strs);
225 static int32_t aout_section_names(char *name, int pass, int *bits)
228 (void)pass;
231 * Default to 32 bits.
233 if (!name)
234 *bits = 32;
236 if (!name)
237 return stext.index;
239 if (!strcmp(name, ".text"))
240 return stext.index;
241 else if (!strcmp(name, ".data"))
242 return sdata.index;
243 else if (!strcmp(name, ".bss"))
244 return sbss.index;
245 else
246 return NO_SEG;
249 static void aout_deflabel(char *name, int32_t segment, int64_t offset,
250 int is_global, char *special)
252 int pos = strslen + 4;
253 struct Symbol *sym;
254 int special_used = false;
256 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
258 * This is a NASM special symbol. We never allow it into
259 * the a.out symbol table, even if it's a valid one. If it
260 * _isn't_ a valid one, we should barf immediately.
262 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
263 strcmp(name, "..got") && strcmp(name, "..plt") &&
264 strcmp(name, "..sym"))
265 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
266 return;
269 if (is_global == 3) {
270 struct Symbol **s;
272 * Fix up a forward-reference symbol size from the first
273 * pass.
275 for (s = &fwds; *s; s = &(*s)->nextfwd)
276 if (!strcmp((*s)->name, name)) {
277 struct tokenval tokval;
278 expr *e;
279 char *p = special;
281 while (*p && !isspace(*p))
282 p++;
283 while (*p && isspace(*p))
284 p++;
285 stdscan_reset();
286 stdscan_bufptr = p;
287 tokval.t_type = TOKEN_INVALID;
288 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
289 if (e) {
290 if (!is_simple(e))
291 error(ERR_NONFATAL, "cannot use relocatable"
292 " expression as symbol size");
293 else
294 (*s)->size = reloc_value(e);
298 * Remove it from the list of unresolved sizes.
300 nasm_free((*s)->name);
301 *s = (*s)->nextfwd;
302 return;
304 return; /* it wasn't an important one */
307 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
308 strslen += 1 + strlen(name);
310 sym = saa_wstruct(syms);
312 sym->strpos = pos;
313 sym->type = is_global ? SYM_GLOBAL : 0;
314 sym->segment = segment;
315 if (segment == NO_SEG)
316 sym->type |= SECT_ABS;
317 else if (segment == stext.index) {
318 sym->type |= SECT_TEXT;
319 if (is_global) {
320 sym->next = stext.gsyms;
321 stext.gsyms = sym;
322 } else if (!stext.asym)
323 stext.asym = sym;
324 } else if (segment == sdata.index) {
325 sym->type |= SECT_DATA;
326 if (is_global) {
327 sym->next = sdata.gsyms;
328 sdata.gsyms = sym;
329 } else if (!sdata.asym)
330 sdata.asym = sym;
331 } else if (segment == sbss.index) {
332 sym->type |= SECT_BSS;
333 if (is_global) {
334 sym->next = sbss.gsyms;
335 sbss.gsyms = sym;
336 } else if (!sbss.asym)
337 sbss.asym = sym;
338 } else
339 sym->type = SYM_GLOBAL;
340 if (is_global == 2)
341 sym->value = offset;
342 else
343 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
345 if (is_global && sym->type != SYM_GLOBAL) {
347 * Global symbol exported _from_ this module. We must check
348 * the special text for type information.
351 if (special) {
352 int n = strcspn(special, " ");
354 if (!nasm_strnicmp(special, "function", n))
355 sym->type |= SYM_FUNCTION;
356 else if (!nasm_strnicmp(special, "data", n) ||
357 !nasm_strnicmp(special, "object", n))
358 sym->type |= SYM_DATA;
359 else
360 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
361 n, special);
362 if (special[n]) {
363 struct tokenval tokval;
364 expr *e;
365 int fwd = false;
366 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
368 if (!bsd) {
369 error(ERR_NONFATAL, "Linux a.out does not support"
370 " symbol size information");
371 } else {
372 while (special[n] && isspace(special[n]))
373 n++;
375 * We have a size expression; attempt to
376 * evaluate it.
378 sym->type |= SYM_WITH_SIZE;
379 stdscan_reset();
380 stdscan_bufptr = special + n;
381 tokval.t_type = TOKEN_INVALID;
382 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
383 NULL);
384 if (fwd) {
385 sym->nextfwd = fwds;
386 fwds = sym;
387 sym->name = nasm_strdup(name);
388 } else if (e) {
389 if (!is_simple(e))
390 error(ERR_NONFATAL, "cannot use relocatable"
391 " expression as symbol size");
392 else
393 sym->size = reloc_value(e);
396 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
398 special_used = true;
403 * define the references from external-symbol segment numbers
404 * to these symbol records.
406 if (segment != NO_SEG && segment != stext.index &&
407 segment != sdata.index && segment != sbss.index)
408 bsym = raa_write(bsym, segment, nsyms);
409 sym->symnum = nsyms;
411 nsyms++;
412 if (sym->type & SYM_WITH_SIZE)
413 nsyms++; /* and another for the size */
415 if (special && !special_used)
416 error(ERR_NONFATAL, "no special symbol features supported here");
419 static void aout_add_reloc(struct Section *sect, int32_t segment,
420 int reltype, int bytes)
422 struct Reloc *r;
424 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
425 sect->tail = &r->next;
426 r->next = NULL;
428 r->address = sect->len;
429 r->symbol = (segment == NO_SEG ? -SECT_ABS :
430 segment == stext.index ? -SECT_TEXT :
431 segment == sdata.index ? -SECT_DATA :
432 segment == sbss.index ? -SECT_BSS :
433 raa_read(bsym, segment));
434 r->reltype = reltype;
435 if (r->symbol >= 0)
436 r->reltype |= RELTYPE_SYMFLAG;
437 r->bytes = bytes;
439 sect->nrelocs++;
443 * This routine deals with ..got and ..sym relocations: the more
444 * complicated kinds. In shared-library writing, some relocations
445 * with respect to global symbols must refer to the precise symbol
446 * rather than referring to an offset from the base of the section
447 * _containing_ the symbol. Such relocations call to this routine,
448 * which searches the symbol list for the symbol in question.
450 * RELTYPE_GOT references require the _exact_ symbol address to be
451 * used; RELTYPE_ABSOLUTE references can be at an offset from the
452 * symbol. The boolean argument `exact' tells us this.
454 * Return value is the adjusted value of `addr', having become an
455 * offset from the symbol rather than the section. Should always be
456 * zero when returning from an exact call.
458 * Limitation: if you define two symbols at the same place,
459 * confusion will occur.
461 * Inefficiency: we search, currently, using a linked list which
462 * isn't even necessarily sorted.
464 static int32_t aout_add_gsym_reloc(struct Section *sect,
465 int32_t segment, int32_t offset,
466 int type, int bytes, int exact)
468 struct Symbol *sym, *sm, *shead;
469 struct Reloc *r;
472 * First look up the segment to find whether it's text, data,
473 * bss or an external symbol.
475 shead = NULL;
476 if (segment == stext.index)
477 shead = stext.gsyms;
478 else if (segment == sdata.index)
479 shead = sdata.gsyms;
480 else if (segment == sbss.index)
481 shead = sbss.gsyms;
482 if (!shead) {
483 if (exact && offset != 0)
484 error(ERR_NONFATAL, "unable to find a suitable global symbol"
485 " for this reference");
486 else
487 aout_add_reloc(sect, segment, type, bytes);
488 return offset;
491 if (exact) {
493 * Find a symbol pointing _exactly_ at this one.
495 for (sym = shead; sym; sym = sym->next)
496 if (sym->value == offset)
497 break;
498 } else {
500 * Find the nearest symbol below this one.
502 sym = NULL;
503 for (sm = shead; sm; sm = sm->next)
504 if (sm->value <= offset && (!sym || sm->value > sym->value))
505 sym = sm;
507 if (!sym && exact) {
508 error(ERR_NONFATAL, "unable to find a suitable global symbol"
509 " for this reference");
510 return 0;
513 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
514 sect->tail = &r->next;
515 r->next = NULL;
517 r->address = sect->len;
518 r->symbol = sym->symnum;
519 r->reltype = type | RELTYPE_SYMFLAG;
520 r->bytes = bytes;
522 sect->nrelocs++;
524 return offset - sym->value;
528 * This routine deals with ..gotoff relocations. These _must_ refer
529 * to a symbol, due to a perversity of *BSD's PIC implementation,
530 * and it must be a non-global one as well; so we store `asym', the
531 * first nonglobal symbol defined in each section, and always work
532 * from that. Relocation type is always RELTYPE_GOTOFF.
534 * Return value is the adjusted value of `addr', having become an
535 * offset from the `asym' symbol rather than the section.
537 static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
538 int32_t offset, int bytes)
540 struct Reloc *r;
541 struct Symbol *asym;
544 * First look up the segment to find whether it's text, data,
545 * bss or an external symbol.
547 asym = NULL;
548 if (segment == stext.index)
549 asym = stext.asym;
550 else if (segment == sdata.index)
551 asym = sdata.asym;
552 else if (segment == sbss.index)
553 asym = sbss.asym;
554 if (!asym)
555 error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
556 " symbol in the section");
558 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
559 sect->tail = &r->next;
560 r->next = NULL;
562 r->address = sect->len;
563 r->symbol = asym->symnum;
564 r->reltype = RELTYPE_GOTOFF;
565 r->bytes = bytes;
567 sect->nrelocs++;
569 return offset - asym->value;
572 static void aout_out(int32_t segto, const void *data,
573 enum out_type type, uint64_t size,
574 int32_t segment, int32_t wrt)
576 struct Section *s;
577 int32_t addr;
578 uint8_t mydata[4], *p;
581 * handle absolute-assembly (structure definitions)
583 if (segto == NO_SEG) {
584 if (type != OUT_RESERVE)
585 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
586 " space");
587 return;
590 if (segto == stext.index)
591 s = &stext;
592 else if (segto == sdata.index)
593 s = &sdata;
594 else if (segto == sbss.index)
595 s = NULL;
596 else {
597 error(ERR_WARNING, "attempt to assemble code in"
598 " segment %d: defaulting to `.text'", segto);
599 s = &stext;
602 if (!s && type != OUT_RESERVE) {
603 error(ERR_WARNING, "attempt to initialize memory in the"
604 " BSS section: ignored");
605 if (type == OUT_REL2ADR)
606 size = 2;
607 else if (type == OUT_REL4ADR)
608 size = 4;
609 sbss.len += size;
610 return;
613 if (type == OUT_RESERVE) {
614 if (s) {
615 error(ERR_WARNING, "uninitialized space declared in"
616 " %s section: zeroing",
617 (segto == stext.index ? "code" : "data"));
618 aout_sect_write(s, NULL, size);
619 } else
620 sbss.len += size;
621 } else if (type == OUT_RAWDATA) {
622 if (segment != NO_SEG)
623 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
624 aout_sect_write(s, data, size);
625 } else if (type == OUT_ADDRESS) {
626 addr = *(int64_t *)data;
627 if (segment != NO_SEG) {
628 if (segment % 2) {
629 error(ERR_NONFATAL, "a.out format does not support"
630 " segment base references");
631 } else {
632 if (wrt == NO_SEG) {
633 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE,
634 size);
635 } else if (!bsd) {
636 error(ERR_NONFATAL,
637 "Linux a.out format does not support"
638 " any use of WRT");
639 wrt = NO_SEG; /* we can at least _try_ to continue */
640 } else if (wrt == aout_gotpc_sect + 1) {
641 is_pic = 0x40;
642 aout_add_reloc(s, segment, RELTYPE_GOTPC, size);
643 } else if (wrt == aout_gotoff_sect + 1) {
644 is_pic = 0x40;
645 addr = aout_add_gotoff_reloc(s, segment,
646 addr, size);
647 } else if (wrt == aout_got_sect + 1) {
648 is_pic = 0x40;
649 addr =
650 aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
651 size, true);
652 } else if (wrt == aout_sym_sect + 1) {
653 addr = aout_add_gsym_reloc(s, segment, addr,
654 RELTYPE_ABSOLUTE, size,
655 false);
656 } else if (wrt == aout_plt_sect + 1) {
657 is_pic = 0x40;
658 error(ERR_NONFATAL,
659 "a.out format cannot produce non-PC-"
660 "relative PLT references");
661 } else {
662 error(ERR_NONFATAL,
663 "a.out format does not support this"
664 " use of WRT");
665 wrt = NO_SEG; /* we can at least _try_ to continue */
669 p = mydata;
670 if (size == 2)
671 WRITESHORT(p, addr);
672 else
673 WRITELONG(p, addr);
674 aout_sect_write(s, mydata, size);
675 } else if (type == OUT_REL2ADR) {
676 if (segment == segto)
677 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
678 if (segment != NO_SEG && segment % 2) {
679 error(ERR_NONFATAL, "a.out format does not support"
680 " segment base references");
681 } else {
682 if (wrt == NO_SEG) {
683 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
684 } else if (!bsd) {
685 error(ERR_NONFATAL, "Linux a.out format does not support"
686 " any use of WRT");
687 wrt = NO_SEG; /* we can at least _try_ to continue */
688 } else if (wrt == aout_plt_sect + 1) {
689 is_pic = 0x40;
690 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
691 } else if (wrt == aout_gotpc_sect + 1 ||
692 wrt == aout_gotoff_sect + 1 ||
693 wrt == aout_got_sect + 1) {
694 error(ERR_NONFATAL, "a.out format cannot produce PC-"
695 "relative GOT references");
696 } else {
697 error(ERR_NONFATAL, "a.out format does not support this"
698 " use of WRT");
699 wrt = NO_SEG; /* we can at least _try_ to continue */
702 p = mydata;
703 WRITESHORT(p, *(int64_t *)data - (size + s->len));
704 aout_sect_write(s, mydata, 2L);
705 } else if (type == OUT_REL4ADR) {
706 if (segment == segto)
707 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
708 if (segment != NO_SEG && segment % 2) {
709 error(ERR_NONFATAL, "a.out format does not support"
710 " segment base references");
711 } else {
712 if (wrt == NO_SEG) {
713 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
714 } else if (!bsd) {
715 error(ERR_NONFATAL, "Linux a.out format does not support"
716 " any use of WRT");
717 wrt = NO_SEG; /* we can at least _try_ to continue */
718 } else if (wrt == aout_plt_sect + 1) {
719 is_pic = 0x40;
720 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
721 } else if (wrt == aout_gotpc_sect + 1 ||
722 wrt == aout_gotoff_sect + 1 ||
723 wrt == aout_got_sect + 1) {
724 error(ERR_NONFATAL, "a.out format cannot produce PC-"
725 "relative GOT references");
726 } else {
727 error(ERR_NONFATAL, "a.out format does not support this"
728 " use of WRT");
729 wrt = NO_SEG; /* we can at least _try_ to continue */
732 p = mydata;
733 WRITELONG(p, *(int64_t *)data - (size + s->len));
734 aout_sect_write(s, mydata, 4L);
738 static void aout_pad_sections(void)
740 static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
742 * Pad each of the text and data sections with NOPs until their
743 * length is a multiple of four. (NOP == 0x90.) Also increase
744 * the length of the BSS section similarly.
746 aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
747 aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
748 sbss.len = (sbss.len + 3) & ~3;
752 * a.out files have the curious property that all references to
753 * things in the data or bss sections are done by addresses which
754 * are actually relative to the start of the _text_ section, in the
755 * _file_. (No relation to what happens after linking. No idea why
756 * this should be so. It's very strange.) So we have to go through
757 * the relocation table, _after_ the final size of each section is
758 * known, and fix up the relocations pointed to.
760 static void aout_fixup_relocs(struct Section *sect)
762 struct Reloc *r;
764 saa_rewind(sect->data);
765 for (r = sect->head; r; r = r->next) {
766 uint8_t *p, *q, blk[4];
767 int32_t l;
769 saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
770 p = q = blk;
771 l = *p++;
772 if (r->bytes > 1) {
773 l += ((int32_t)*p++) << 8;
774 if (r->bytes == 4) {
775 l += ((int32_t)*p++) << 16;
776 l += ((int32_t)*p++) << 24;
779 if (r->symbol == -SECT_DATA)
780 l += stext.len;
781 else if (r->symbol == -SECT_BSS)
782 l += stext.len + sdata.len;
783 if (r->bytes == 4)
784 WRITELONG(q, l);
785 else if (r->bytes == 2)
786 WRITESHORT(q, l);
787 else
788 *q++ = l & 0xFF;
789 saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
793 static void aout_write(void)
796 * Emit the a.out header.
798 /* OMAGIC, M_386 or MID_I386, no flags */
799 fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
800 fwriteint32_t(stext.len, aoutfp);
801 fwriteint32_t(sdata.len, aoutfp);
802 fwriteint32_t(sbss.len, aoutfp);
803 fwriteint32_t(nsyms * 12, aoutfp); /* length of symbol table */
804 fwriteint32_t(0L, aoutfp); /* object files have no entry point */
805 fwriteint32_t(stext.nrelocs * 8, aoutfp); /* size of text relocs */
806 fwriteint32_t(sdata.nrelocs * 8, aoutfp); /* size of data relocs */
809 * Write out the code section and the data section.
811 saa_fpwrite(stext.data, aoutfp);
812 saa_fpwrite(sdata.data, aoutfp);
815 * Write out the relocations.
817 aout_write_relocs(stext.head);
818 aout_write_relocs(sdata.head);
821 * Write the symbol table.
823 aout_write_syms();
826 * And the string table.
828 fwriteint32_t(strslen + 4, aoutfp); /* length includes length count */
829 saa_fpwrite(strs, aoutfp);
832 static void aout_write_relocs(struct Reloc *r)
834 while (r) {
835 uint32_t word2;
837 fwriteint32_t(r->address, aoutfp);
839 if (r->symbol >= 0)
840 word2 = r->symbol;
841 else
842 word2 = -r->symbol;
843 word2 |= r->reltype << 24;
844 word2 |= (r->bytes == 1 ? 0 :
845 r->bytes == 2 ? 0x2000000L : 0x4000000L);
846 fwriteint32_t(word2, aoutfp);
848 r = r->next;
852 static void aout_write_syms(void)
854 uint32_t i;
856 saa_rewind(syms);
857 for (i = 0; i < nsyms; i++) {
858 struct Symbol *sym = saa_rstruct(syms);
859 fwriteint32_t(sym->strpos, aoutfp);
860 fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, aoutfp);
862 * Fix up the symbol value now we know the final section
863 * sizes.
865 if ((sym->type & SECT_MASK) == SECT_DATA)
866 sym->value += stext.len;
867 if ((sym->type & SECT_MASK) == SECT_BSS)
868 sym->value += stext.len + sdata.len;
869 fwriteint32_t(sym->value, aoutfp);
871 * Output a size record if necessary.
873 if (sym->type & SYM_WITH_SIZE) {
874 fwriteint32_t(sym->strpos, aoutfp);
875 fwriteint32_t(0x0DL, aoutfp); /* special value: means size */
876 fwriteint32_t(sym->size, aoutfp);
877 i++; /* use up another of `nsyms' */
882 static void aout_sect_write(struct Section *sect,
883 const uint8_t *data, uint32_t len)
885 saa_wbytes(sect->data, data, len);
886 sect->len += len;
889 static int32_t aout_segbase(int32_t segment)
891 return segment;
894 static int aout_directive(char *directive, char *value, int pass)
896 (void)directive;
897 (void)value;
898 (void)pass;
899 return 0;
902 static void aout_filename(char *inname, char *outname, efunc error)
904 standard_extension(inname, outname, ".o", error);
907 static const char *aout_stdmac[] = {
908 "%define __SECT__ [section .text]",
909 "%macro __NASM_CDecl__ 1",
910 "%endmacro",
911 NULL
914 static int aout_set_info(enum geninfo type, char **val)
916 (void)type;
917 (void)val;
918 return 0;
920 #endif /* OF_AOUT || OF_AOUTB */
922 #ifdef OF_AOUT
924 struct ofmt of_aout = {
925 "Linux a.out object files",
926 "aout",
927 NULL,
928 null_debug_arr,
929 &null_debug_form,
930 aout_stdmac,
931 aout_init,
932 aout_set_info,
933 aout_out,
934 aout_deflabel,
935 aout_section_names,
936 aout_segbase,
937 aout_directive,
938 aout_filename,
939 aout_cleanup
942 #endif
944 #ifdef OF_AOUTB
946 struct ofmt of_aoutb = {
947 "NetBSD/FreeBSD a.out object files",
948 "aoutb",
949 NULL,
950 null_debug_arr,
951 &null_debug_form,
952 aout_stdmac,
953 aoutb_init,
954 aout_set_info,
955 aout_out,
956 aout_deflabel,
957 aout_section_names,
958 aout_segbase,
959 aout_directive,
960 aout_filename,
961 aout_cleanup
964 #endif