Reworked test files for better error reporting
[nasm/perl-rewrite.git] / output / outaout.c
blob467b20e3a8920af22569e712c2811fc6d28bef83
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 "raa.h"
22 #include "stdscan.h"
23 #include "outform.h"
24 #include "outlib.h"
26 #if defined OF_AOUT || defined OF_AOUTB
28 #define RELTYPE_ABSOLUTE 0x00
29 #define RELTYPE_RELATIVE 0x01
30 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
31 #define RELTYPE_GOTOFF 0x10
32 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
33 #define RELTYPE_PLT 0x21
34 #define RELTYPE_SYMFLAG 0x08
36 struct Reloc {
37 struct Reloc *next;
38 int32_t address; /* relative to _start_ of section */
39 int32_t symbol; /* symbol number or -ve section id */
40 int bytes; /* 2 or 4 */
41 int reltype; /* see above */
44 struct Symbol {
45 int32_t strpos; /* string table position of name */
46 int type; /* symbol type - see flags below */
47 int32_t value; /* address, or COMMON variable size */
48 int32_t size; /* size for data or function exports */
49 int32_t segment; /* back-reference used by gsym_reloc */
50 struct Symbol *next; /* list of globals in each section */
51 struct Symbol *nextfwd; /* list of unresolved-size symbols */
52 char *name; /* for unresolved-size symbols */
53 int32_t symnum; /* index into symbol table */
57 * Section IDs - used in Reloc.symbol when negative, and in
58 * Symbol.type when positive.
60 #define SECT_ABS 2 /* absolute value */
61 #define SECT_TEXT 4 /* text section */
62 #define SECT_DATA 6 /* data section */
63 #define SECT_BSS 8 /* bss section */
64 #define SECT_MASK 0xE /* mask out any of the above */
67 * More flags used in Symbol.type.
69 #define SYM_GLOBAL 1 /* it's a global symbol */
70 #define SYM_DATA 0x100 /* used for shared libs */
71 #define SYM_FUNCTION 0x200 /* used for shared libs */
72 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
75 * Bit more explanation of symbol types: SECT_xxx denotes a local
76 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
77 * this module. Just SYM_GLOBAL, with zero value, denotes an
78 * external symbol referenced in this module. And just SYM_GLOBAL,
79 * but with a non-zero value, declares a C `common' variable, of
80 * size `value'.
83 struct Section {
84 struct SAA *data;
85 uint32_t len, size, nrelocs;
86 int32_t index;
87 struct Reloc *head, **tail;
88 struct Symbol *gsyms, *asym;
91 static struct Section stext, sdata, sbss;
93 static struct SAA *syms;
94 static uint32_t nsyms;
96 static struct RAA *bsym;
98 static struct SAA *strs;
99 static uint32_t strslen;
101 static struct Symbol *fwds;
103 static FILE *aoutfp;
104 static efunc error;
105 static evalfunc evaluate;
107 static int bsd;
108 static int is_pic;
110 static void aout_write(void);
111 static void aout_write_relocs(struct Reloc *);
112 static void aout_write_syms(void);
113 static void aout_sect_write(struct Section *, const uint8_t *,
114 uint32_t);
115 static void aout_pad_sections(void);
116 static void aout_fixup_relocs(struct Section *);
119 * Special section numbers which are used to define special
120 * symbols, which can be used with WRT to provide PIC relocation
121 * types.
123 static int32_t aout_gotpc_sect, aout_gotoff_sect;
124 static int32_t aout_got_sect, aout_plt_sect;
125 static int32_t aout_sym_sect;
127 static void aoutg_init(FILE * fp, efunc errfunc, ldfunc ldef,
128 evalfunc eval)
130 aoutfp = fp;
131 error = errfunc;
132 evaluate = eval;
133 (void)ldef; /* placate optimisers */
134 stext.data = saa_init(1L);
135 stext.head = NULL;
136 stext.tail = &stext.head;
137 sdata.data = saa_init(1L);
138 sdata.head = NULL;
139 sdata.tail = &sdata.head;
140 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
141 stext.nrelocs = sdata.nrelocs = 0;
142 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
143 stext.index = seg_alloc();
144 sdata.index = seg_alloc();
145 sbss.index = seg_alloc();
146 stext.asym = sdata.asym = sbss.asym = NULL;
147 syms = saa_init((int32_t)sizeof(struct Symbol));
148 nsyms = 0;
149 bsym = raa_init();
150 strs = saa_init(1L);
151 strslen = 0;
152 fwds = NULL;
155 #ifdef OF_AOUT
157 static void aout_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
159 bsd = false;
160 aoutg_init(fp, errfunc, ldef, eval);
162 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
163 aout_plt_sect = aout_sym_sect = NO_SEG;
166 #endif
168 #ifdef OF_AOUTB
170 extern struct ofmt of_aoutb;
172 static void aoutb_init(FILE * fp, efunc errfunc, ldfunc ldef,
173 evalfunc eval)
175 bsd = true;
176 aoutg_init(fp, errfunc, ldef, eval);
178 is_pic = 0x00; /* may become 0x40 */
180 aout_gotpc_sect = seg_alloc();
181 ldef("..gotpc", aout_gotpc_sect + 1, 0L, NULL, false, false, &of_aoutb,
182 error);
183 aout_gotoff_sect = seg_alloc();
184 ldef("..gotoff", aout_gotoff_sect + 1, 0L, NULL, false, false,
185 &of_aoutb, error);
186 aout_got_sect = seg_alloc();
187 ldef("..got", aout_got_sect + 1, 0L, NULL, false, false, &of_aoutb,
188 error);
189 aout_plt_sect = seg_alloc();
190 ldef("..plt", aout_plt_sect + 1, 0L, NULL, false, false, &of_aoutb,
191 error);
192 aout_sym_sect = seg_alloc();
193 ldef("..sym", aout_sym_sect + 1, 0L, NULL, false, false, &of_aoutb,
194 error);
197 #endif
199 static void aout_cleanup(int debuginfo)
201 struct Reloc *r;
203 (void)debuginfo;
205 aout_pad_sections();
206 aout_fixup_relocs(&stext);
207 aout_fixup_relocs(&sdata);
208 aout_write();
209 fclose(aoutfp);
210 saa_free(stext.data);
211 while (stext.head) {
212 r = stext.head;
213 stext.head = stext.head->next;
214 nasm_free(r);
216 saa_free(sdata.data);
217 while (sdata.head) {
218 r = sdata.head;
219 sdata.head = sdata.head->next;
220 nasm_free(r);
222 saa_free(syms);
223 raa_free(bsym);
224 saa_free(strs);
227 static int32_t aout_section_names(char *name, int pass, int *bits)
230 (void)pass;
233 * Default to 32 bits.
235 if (!name)
236 *bits = 32;
238 if (!name)
239 return stext.index;
241 if (!strcmp(name, ".text"))
242 return stext.index;
243 else if (!strcmp(name, ".data"))
244 return sdata.index;
245 else if (!strcmp(name, ".bss"))
246 return sbss.index;
247 else
248 return NO_SEG;
251 static void aout_deflabel(char *name, int32_t segment, int64_t offset,
252 int is_global, char *special)
254 int pos = strslen + 4;
255 struct Symbol *sym;
256 int special_used = false;
258 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
260 * This is a NASM special symbol. We never allow it into
261 * the a.out symbol table, even if it's a valid one. If it
262 * _isn't_ a valid one, we should barf immediately.
264 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
265 strcmp(name, "..got") && strcmp(name, "..plt") &&
266 strcmp(name, "..sym"))
267 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
268 return;
271 if (is_global == 3) {
272 struct Symbol **s;
274 * Fix up a forward-reference symbol size from the first
275 * pass.
277 for (s = &fwds; *s; s = &(*s)->nextfwd)
278 if (!strcmp((*s)->name, name)) {
279 struct tokenval tokval;
280 expr *e;
281 char *p = special;
283 while (*p && !nasm_isspace(*p))
284 p++;
285 while (*p && nasm_isspace(*p))
286 p++;
287 stdscan_reset();
288 stdscan_bufptr = p;
289 tokval.t_type = TOKEN_INVALID;
290 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
291 if (e) {
292 if (!is_simple(e))
293 error(ERR_NONFATAL, "cannot use relocatable"
294 " expression as symbol size");
295 else
296 (*s)->size = reloc_value(e);
300 * Remove it from the list of unresolved sizes.
302 nasm_free((*s)->name);
303 *s = (*s)->nextfwd;
304 return;
306 return; /* it wasn't an important one */
309 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
310 strslen += 1 + strlen(name);
312 sym = saa_wstruct(syms);
314 sym->strpos = pos;
315 sym->type = is_global ? SYM_GLOBAL : 0;
316 sym->segment = segment;
317 if (segment == NO_SEG)
318 sym->type |= SECT_ABS;
319 else if (segment == stext.index) {
320 sym->type |= SECT_TEXT;
321 if (is_global) {
322 sym->next = stext.gsyms;
323 stext.gsyms = sym;
324 } else if (!stext.asym)
325 stext.asym = sym;
326 } else if (segment == sdata.index) {
327 sym->type |= SECT_DATA;
328 if (is_global) {
329 sym->next = sdata.gsyms;
330 sdata.gsyms = sym;
331 } else if (!sdata.asym)
332 sdata.asym = sym;
333 } else if (segment == sbss.index) {
334 sym->type |= SECT_BSS;
335 if (is_global) {
336 sym->next = sbss.gsyms;
337 sbss.gsyms = sym;
338 } else if (!sbss.asym)
339 sbss.asym = sym;
340 } else
341 sym->type = SYM_GLOBAL;
342 if (is_global == 2)
343 sym->value = offset;
344 else
345 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
347 if (is_global && sym->type != SYM_GLOBAL) {
349 * Global symbol exported _from_ this module. We must check
350 * the special text for type information.
353 if (special) {
354 int n = strcspn(special, " ");
356 if (!nasm_strnicmp(special, "function", n))
357 sym->type |= SYM_FUNCTION;
358 else if (!nasm_strnicmp(special, "data", n) ||
359 !nasm_strnicmp(special, "object", n))
360 sym->type |= SYM_DATA;
361 else
362 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
363 n, special);
364 if (special[n]) {
365 struct tokenval tokval;
366 expr *e;
367 int fwd = false;
368 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
370 if (!bsd) {
371 error(ERR_NONFATAL, "Linux a.out does not support"
372 " symbol size information");
373 } else {
374 while (special[n] && nasm_isspace(special[n]))
375 n++;
377 * We have a size expression; attempt to
378 * evaluate it.
380 sym->type |= SYM_WITH_SIZE;
381 stdscan_reset();
382 stdscan_bufptr = special + n;
383 tokval.t_type = TOKEN_INVALID;
384 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
385 NULL);
386 if (fwd) {
387 sym->nextfwd = fwds;
388 fwds = sym;
389 sym->name = nasm_strdup(name);
390 } else if (e) {
391 if (!is_simple(e))
392 error(ERR_NONFATAL, "cannot use relocatable"
393 " expression as symbol size");
394 else
395 sym->size = reloc_value(e);
398 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
400 special_used = true;
405 * define the references from external-symbol segment numbers
406 * to these symbol records.
408 if (segment != NO_SEG && segment != stext.index &&
409 segment != sdata.index && segment != sbss.index)
410 bsym = raa_write(bsym, segment, nsyms);
411 sym->symnum = nsyms;
413 nsyms++;
414 if (sym->type & SYM_WITH_SIZE)
415 nsyms++; /* and another for the size */
417 if (special && !special_used)
418 error(ERR_NONFATAL, "no special symbol features supported here");
421 static void aout_add_reloc(struct Section *sect, int32_t segment,
422 int reltype, int bytes)
424 struct Reloc *r;
426 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
427 sect->tail = &r->next;
428 r->next = NULL;
430 r->address = sect->len;
431 r->symbol = (segment == NO_SEG ? -SECT_ABS :
432 segment == stext.index ? -SECT_TEXT :
433 segment == sdata.index ? -SECT_DATA :
434 segment == sbss.index ? -SECT_BSS :
435 raa_read(bsym, segment));
436 r->reltype = reltype;
437 if (r->symbol >= 0)
438 r->reltype |= RELTYPE_SYMFLAG;
439 r->bytes = bytes;
441 sect->nrelocs++;
445 * This routine deals with ..got and ..sym relocations: the more
446 * complicated kinds. In shared-library writing, some relocations
447 * with respect to global symbols must refer to the precise symbol
448 * rather than referring to an offset from the base of the section
449 * _containing_ the symbol. Such relocations call to this routine,
450 * which searches the symbol list for the symbol in question.
452 * RELTYPE_GOT references require the _exact_ symbol address to be
453 * used; RELTYPE_ABSOLUTE references can be at an offset from the
454 * symbol. The boolean argument `exact' tells us this.
456 * Return value is the adjusted value of `addr', having become an
457 * offset from the symbol rather than the section. Should always be
458 * zero when returning from an exact call.
460 * Limitation: if you define two symbols at the same place,
461 * confusion will occur.
463 * Inefficiency: we search, currently, using a linked list which
464 * isn't even necessarily sorted.
466 static int32_t aout_add_gsym_reloc(struct Section *sect,
467 int32_t segment, int32_t offset,
468 int type, int bytes, int exact)
470 struct Symbol *sym, *sm, *shead;
471 struct Reloc *r;
474 * First look up the segment to find whether it's text, data,
475 * bss or an external symbol.
477 shead = NULL;
478 if (segment == stext.index)
479 shead = stext.gsyms;
480 else if (segment == sdata.index)
481 shead = sdata.gsyms;
482 else if (segment == sbss.index)
483 shead = sbss.gsyms;
484 if (!shead) {
485 if (exact && offset != 0)
486 error(ERR_NONFATAL, "unable to find a suitable global symbol"
487 " for this reference");
488 else
489 aout_add_reloc(sect, segment, type, bytes);
490 return offset;
493 if (exact) {
495 * Find a symbol pointing _exactly_ at this one.
497 for (sym = shead; sym; sym = sym->next)
498 if (sym->value == offset)
499 break;
500 } else {
502 * Find the nearest symbol below this one.
504 sym = NULL;
505 for (sm = shead; sm; sm = sm->next)
506 if (sm->value <= offset && (!sym || sm->value > sym->value))
507 sym = sm;
509 if (!sym && exact) {
510 error(ERR_NONFATAL, "unable to find a suitable global symbol"
511 " for this reference");
512 return 0;
515 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
516 sect->tail = &r->next;
517 r->next = NULL;
519 r->address = sect->len;
520 r->symbol = sym->symnum;
521 r->reltype = type | RELTYPE_SYMFLAG;
522 r->bytes = bytes;
524 sect->nrelocs++;
526 return offset - sym->value;
530 * This routine deals with ..gotoff relocations. These _must_ refer
531 * to a symbol, due to a perversity of *BSD's PIC implementation,
532 * and it must be a non-global one as well; so we store `asym', the
533 * first nonglobal symbol defined in each section, and always work
534 * from that. Relocation type is always RELTYPE_GOTOFF.
536 * Return value is the adjusted value of `addr', having become an
537 * offset from the `asym' symbol rather than the section.
539 static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
540 int32_t offset, int bytes)
542 struct Reloc *r;
543 struct Symbol *asym;
546 * First look up the segment to find whether it's text, data,
547 * bss or an external symbol.
549 asym = NULL;
550 if (segment == stext.index)
551 asym = stext.asym;
552 else if (segment == sdata.index)
553 asym = sdata.asym;
554 else if (segment == sbss.index)
555 asym = sbss.asym;
556 if (!asym)
557 error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
558 " symbol in the section");
560 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
561 sect->tail = &r->next;
562 r->next = NULL;
564 r->address = sect->len;
565 r->symbol = asym->symnum;
566 r->reltype = RELTYPE_GOTOFF;
567 r->bytes = bytes;
569 sect->nrelocs++;
571 return offset - asym->value;
574 static void aout_out(int32_t segto, const void *data,
575 enum out_type type, uint64_t size,
576 int32_t segment, int32_t wrt)
578 struct Section *s;
579 int32_t addr;
580 uint8_t mydata[4], *p;
583 * handle absolute-assembly (structure definitions)
585 if (segto == NO_SEG) {
586 if (type != OUT_RESERVE)
587 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
588 " space");
589 return;
592 if (segto == stext.index)
593 s = &stext;
594 else if (segto == sdata.index)
595 s = &sdata;
596 else if (segto == sbss.index)
597 s = NULL;
598 else {
599 error(ERR_WARNING, "attempt to assemble code in"
600 " segment %d: defaulting to `.text'", segto);
601 s = &stext;
604 if (!s && type != OUT_RESERVE) {
605 error(ERR_WARNING, "attempt to initialize memory in the"
606 " BSS section: ignored");
607 sbss.len += realsize(type, size);
608 return;
611 if (type == OUT_RESERVE) {
612 if (s) {
613 error(ERR_WARNING, "uninitialized space declared in"
614 " %s section: zeroing",
615 (segto == stext.index ? "code" : "data"));
616 aout_sect_write(s, NULL, size);
617 } else
618 sbss.len += size;
619 } else if (type == OUT_RAWDATA) {
620 if (segment != NO_SEG)
621 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
622 aout_sect_write(s, data, size);
623 } else if (type == OUT_ADDRESS) {
624 addr = *(int64_t *)data;
625 if (segment != NO_SEG) {
626 if (segment % 2) {
627 error(ERR_NONFATAL, "a.out format does not support"
628 " segment base references");
629 } else {
630 if (wrt == NO_SEG) {
631 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE,
632 size);
633 } else if (!bsd) {
634 error(ERR_NONFATAL,
635 "Linux a.out format does not support"
636 " any use of WRT");
637 wrt = NO_SEG; /* we can at least _try_ to continue */
638 } else if (wrt == aout_gotpc_sect + 1) {
639 is_pic = 0x40;
640 aout_add_reloc(s, segment, RELTYPE_GOTPC, size);
641 } else if (wrt == aout_gotoff_sect + 1) {
642 is_pic = 0x40;
643 addr = aout_add_gotoff_reloc(s, segment,
644 addr, size);
645 } else if (wrt == aout_got_sect + 1) {
646 is_pic = 0x40;
647 addr =
648 aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
649 size, true);
650 } else if (wrt == aout_sym_sect + 1) {
651 addr = aout_add_gsym_reloc(s, segment, addr,
652 RELTYPE_ABSOLUTE, size,
653 false);
654 } else if (wrt == aout_plt_sect + 1) {
655 is_pic = 0x40;
656 error(ERR_NONFATAL,
657 "a.out format cannot produce non-PC-"
658 "relative PLT references");
659 } else {
660 error(ERR_NONFATAL,
661 "a.out format does not support this"
662 " use of WRT");
663 wrt = NO_SEG; /* we can at least _try_ to continue */
667 p = mydata;
668 if (size == 2)
669 WRITESHORT(p, addr);
670 else
671 WRITELONG(p, addr);
672 aout_sect_write(s, mydata, size);
673 } else if (type == OUT_REL2ADR) {
674 if (segment == segto)
675 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
676 if (segment != NO_SEG && segment % 2) {
677 error(ERR_NONFATAL, "a.out format does not support"
678 " segment base references");
679 } else {
680 if (wrt == NO_SEG) {
681 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
682 } else if (!bsd) {
683 error(ERR_NONFATAL, "Linux a.out format does not support"
684 " any use of WRT");
685 wrt = NO_SEG; /* we can at least _try_ to continue */
686 } else if (wrt == aout_plt_sect + 1) {
687 is_pic = 0x40;
688 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
689 } else if (wrt == aout_gotpc_sect + 1 ||
690 wrt == aout_gotoff_sect + 1 ||
691 wrt == aout_got_sect + 1) {
692 error(ERR_NONFATAL, "a.out format cannot produce PC-"
693 "relative GOT references");
694 } else {
695 error(ERR_NONFATAL, "a.out format does not support this"
696 " use of WRT");
697 wrt = NO_SEG; /* we can at least _try_ to continue */
700 p = mydata;
701 WRITESHORT(p, *(int64_t *)data - (size + s->len));
702 aout_sect_write(s, mydata, 2L);
703 } else if (type == OUT_REL4ADR) {
704 if (segment == segto)
705 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
706 if (segment != NO_SEG && segment % 2) {
707 error(ERR_NONFATAL, "a.out format does not support"
708 " segment base references");
709 } else {
710 if (wrt == NO_SEG) {
711 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
712 } else if (!bsd) {
713 error(ERR_NONFATAL, "Linux a.out format does not support"
714 " any use of WRT");
715 wrt = NO_SEG; /* we can at least _try_ to continue */
716 } else if (wrt == aout_plt_sect + 1) {
717 is_pic = 0x40;
718 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
719 } else if (wrt == aout_gotpc_sect + 1 ||
720 wrt == aout_gotoff_sect + 1 ||
721 wrt == aout_got_sect + 1) {
722 error(ERR_NONFATAL, "a.out format cannot produce PC-"
723 "relative GOT references");
724 } else {
725 error(ERR_NONFATAL, "a.out format does not support this"
726 " use of WRT");
727 wrt = NO_SEG; /* we can at least _try_ to continue */
730 p = mydata;
731 WRITELONG(p, *(int64_t *)data - (size + s->len));
732 aout_sect_write(s, mydata, 4L);
736 static void aout_pad_sections(void)
738 static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
740 * Pad each of the text and data sections with NOPs until their
741 * length is a multiple of four. (NOP == 0x90.) Also increase
742 * the length of the BSS section similarly.
744 aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
745 aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
746 sbss.len = (sbss.len + 3) & ~3;
750 * a.out files have the curious property that all references to
751 * things in the data or bss sections are done by addresses which
752 * are actually relative to the start of the _text_ section, in the
753 * _file_. (No relation to what happens after linking. No idea why
754 * this should be so. It's very strange.) So we have to go through
755 * the relocation table, _after_ the final size of each section is
756 * known, and fix up the relocations pointed to.
758 static void aout_fixup_relocs(struct Section *sect)
760 struct Reloc *r;
762 saa_rewind(sect->data);
763 for (r = sect->head; r; r = r->next) {
764 uint8_t *p, *q, blk[4];
765 int32_t l;
767 saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
768 p = q = blk;
769 l = *p++;
770 if (r->bytes > 1) {
771 l += ((int32_t)*p++) << 8;
772 if (r->bytes == 4) {
773 l += ((int32_t)*p++) << 16;
774 l += ((int32_t)*p++) << 24;
777 if (r->symbol == -SECT_DATA)
778 l += stext.len;
779 else if (r->symbol == -SECT_BSS)
780 l += stext.len + sdata.len;
781 if (r->bytes == 4)
782 WRITELONG(q, l);
783 else if (r->bytes == 2)
784 WRITESHORT(q, l);
785 else
786 *q++ = l & 0xFF;
787 saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
791 static void aout_write(void)
794 * Emit the a.out header.
796 /* OMAGIC, M_386 or MID_I386, no flags */
797 fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
798 fwriteint32_t(stext.len, aoutfp);
799 fwriteint32_t(sdata.len, aoutfp);
800 fwriteint32_t(sbss.len, aoutfp);
801 fwriteint32_t(nsyms * 12, aoutfp); /* length of symbol table */
802 fwriteint32_t(0L, aoutfp); /* object files have no entry point */
803 fwriteint32_t(stext.nrelocs * 8, aoutfp); /* size of text relocs */
804 fwriteint32_t(sdata.nrelocs * 8, aoutfp); /* size of data relocs */
807 * Write out the code section and the data section.
809 saa_fpwrite(stext.data, aoutfp);
810 saa_fpwrite(sdata.data, aoutfp);
813 * Write out the relocations.
815 aout_write_relocs(stext.head);
816 aout_write_relocs(sdata.head);
819 * Write the symbol table.
821 aout_write_syms();
824 * And the string table.
826 fwriteint32_t(strslen + 4, aoutfp); /* length includes length count */
827 saa_fpwrite(strs, aoutfp);
830 static void aout_write_relocs(struct Reloc *r)
832 while (r) {
833 uint32_t word2;
835 fwriteint32_t(r->address, aoutfp);
837 if (r->symbol >= 0)
838 word2 = r->symbol;
839 else
840 word2 = -r->symbol;
841 word2 |= r->reltype << 24;
842 word2 |= (r->bytes == 1 ? 0 :
843 r->bytes == 2 ? 0x2000000L : 0x4000000L);
844 fwriteint32_t(word2, aoutfp);
846 r = r->next;
850 static void aout_write_syms(void)
852 uint32_t i;
854 saa_rewind(syms);
855 for (i = 0; i < nsyms; i++) {
856 struct Symbol *sym = saa_rstruct(syms);
857 fwriteint32_t(sym->strpos, aoutfp);
858 fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, aoutfp);
860 * Fix up the symbol value now we know the final section
861 * sizes.
863 if ((sym->type & SECT_MASK) == SECT_DATA)
864 sym->value += stext.len;
865 if ((sym->type & SECT_MASK) == SECT_BSS)
866 sym->value += stext.len + sdata.len;
867 fwriteint32_t(sym->value, aoutfp);
869 * Output a size record if necessary.
871 if (sym->type & SYM_WITH_SIZE) {
872 fwriteint32_t(sym->strpos, aoutfp);
873 fwriteint32_t(0x0DL, aoutfp); /* special value: means size */
874 fwriteint32_t(sym->size, aoutfp);
875 i++; /* use up another of `nsyms' */
880 static void aout_sect_write(struct Section *sect,
881 const uint8_t *data, uint32_t len)
883 saa_wbytes(sect->data, data, len);
884 sect->len += len;
887 static int32_t aout_segbase(int32_t segment)
889 return segment;
892 static int aout_directive(char *directive, char *value, int pass)
894 (void)directive;
895 (void)value;
896 (void)pass;
897 return 0;
900 static void aout_filename(char *inname, char *outname, efunc error)
902 standard_extension(inname, outname, ".o", error);
905 extern macros_t aout_stdmac[];
907 static int aout_set_info(enum geninfo type, char **val)
909 (void)type;
910 (void)val;
911 return 0;
913 #endif /* OF_AOUT || OF_AOUTB */
915 #ifdef OF_AOUT
917 struct ofmt of_aout = {
918 "Linux a.out object files",
919 "aout",
920 NULL,
921 null_debug_arr,
922 &null_debug_form,
923 aout_stdmac,
924 aout_init,
925 aout_set_info,
926 aout_out,
927 aout_deflabel,
928 aout_section_names,
929 aout_segbase,
930 aout_directive,
931 aout_filename,
932 aout_cleanup
935 #endif
937 #ifdef OF_AOUTB
939 struct ofmt of_aoutb = {
940 "NetBSD/FreeBSD a.out object files",
941 "aoutb",
942 NULL,
943 null_debug_arr,
944 &null_debug_form,
945 aout_stdmac,
946 aoutb_init,
947 aout_set_info,
948 aout_out,
949 aout_deflabel,
950 aout_section_names,
951 aout_segbase,
952 aout_directive,
953 aout_filename,
954 aout_cleanup
957 #endif