preproc: error on unterminated %[...]
[nasm.git] / output / outaout.c
blob48a2cffd6532c220620675102c03135418cc99e2
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"
25 #if defined OF_AOUT || defined OF_AOUTB
27 #define RELTYPE_ABSOLUTE 0x00
28 #define RELTYPE_RELATIVE 0x01
29 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
30 #define RELTYPE_GOTOFF 0x10
31 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
32 #define RELTYPE_PLT 0x21
33 #define RELTYPE_SYMFLAG 0x08
35 struct Reloc {
36 struct Reloc *next;
37 int32_t address; /* relative to _start_ of section */
38 int32_t symbol; /* symbol number or -ve section id */
39 int bytes; /* 2 or 4 */
40 int reltype; /* see above */
43 struct Symbol {
44 int32_t strpos; /* string table position of name */
45 int type; /* symbol type - see flags below */
46 int32_t value; /* address, or COMMON variable size */
47 int32_t size; /* size for data or function exports */
48 int32_t segment; /* back-reference used by gsym_reloc */
49 struct Symbol *next; /* list of globals in each section */
50 struct Symbol *nextfwd; /* list of unresolved-size symbols */
51 char *name; /* for unresolved-size symbols */
52 int32_t symnum; /* index into symbol table */
56 * Section IDs - used in Reloc.symbol when negative, and in
57 * Symbol.type when positive.
59 #define SECT_ABS 2 /* absolute value */
60 #define SECT_TEXT 4 /* text section */
61 #define SECT_DATA 6 /* data section */
62 #define SECT_BSS 8 /* bss section */
63 #define SECT_MASK 0xE /* mask out any of the above */
66 * More flags used in Symbol.type.
68 #define SYM_GLOBAL 1 /* it's a global symbol */
69 #define SYM_DATA 0x100 /* used for shared libs */
70 #define SYM_FUNCTION 0x200 /* used for shared libs */
71 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
74 * Bit more explanation of symbol types: SECT_xxx denotes a local
75 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
76 * this module. Just SYM_GLOBAL, with zero value, denotes an
77 * external symbol referenced in this module. And just SYM_GLOBAL,
78 * but with a non-zero value, declares a C `common' variable, of
79 * size `value'.
82 struct Section {
83 struct SAA *data;
84 uint32_t len, size, nrelocs;
85 int32_t index;
86 struct Reloc *head, **tail;
87 struct Symbol *gsyms, *asym;
90 static struct Section stext, sdata, sbss;
92 static struct SAA *syms;
93 static uint32_t nsyms;
95 static struct RAA *bsym;
97 static struct SAA *strs;
98 static uint32_t strslen;
100 static struct Symbol *fwds;
102 static FILE *aoutfp;
103 static efunc error;
104 static evalfunc evaluate;
106 static int bsd;
107 static int is_pic;
109 static void aout_write(void);
110 static void aout_write_relocs(struct Reloc *);
111 static void aout_write_syms(void);
112 static void aout_sect_write(struct Section *, const uint8_t *,
113 uint32_t);
114 static void aout_pad_sections(void);
115 static void aout_fixup_relocs(struct Section *);
118 * Special section numbers which are used to define special
119 * symbols, which can be used with WRT to provide PIC relocation
120 * types.
122 static int32_t aout_gotpc_sect, aout_gotoff_sect;
123 static int32_t aout_got_sect, aout_plt_sect;
124 static int32_t aout_sym_sect;
126 static void aoutg_init(FILE * fp, efunc errfunc, ldfunc ldef,
127 evalfunc eval)
129 aoutfp = fp;
130 error = errfunc;
131 evaluate = eval;
132 (void)ldef; /* placate optimisers */
133 stext.data = saa_init(1L);
134 stext.head = NULL;
135 stext.tail = &stext.head;
136 sdata.data = saa_init(1L);
137 sdata.head = NULL;
138 sdata.tail = &sdata.head;
139 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
140 stext.nrelocs = sdata.nrelocs = 0;
141 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
142 stext.index = seg_alloc();
143 sdata.index = seg_alloc();
144 sbss.index = seg_alloc();
145 stext.asym = sdata.asym = sbss.asym = NULL;
146 syms = saa_init((int32_t)sizeof(struct Symbol));
147 nsyms = 0;
148 bsym = raa_init();
149 strs = saa_init(1L);
150 strslen = 0;
151 fwds = NULL;
154 #ifdef OF_AOUT
156 static void aout_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
158 bsd = false;
159 aoutg_init(fp, errfunc, ldef, eval);
161 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
162 aout_plt_sect = aout_sym_sect = NO_SEG;
165 #endif
167 #ifdef OF_AOUTB
169 extern struct ofmt of_aoutb;
171 static void aoutb_init(FILE * fp, efunc errfunc, ldfunc ldef,
172 evalfunc eval)
174 bsd = true;
175 aoutg_init(fp, errfunc, ldef, eval);
177 is_pic = 0x00; /* may become 0x40 */
179 aout_gotpc_sect = seg_alloc();
180 ldef("..gotpc", aout_gotpc_sect + 1, 0L, NULL, false, false, &of_aoutb,
181 error);
182 aout_gotoff_sect = seg_alloc();
183 ldef("..gotoff", aout_gotoff_sect + 1, 0L, NULL, false, false,
184 &of_aoutb, error);
185 aout_got_sect = seg_alloc();
186 ldef("..got", aout_got_sect + 1, 0L, NULL, false, false, &of_aoutb,
187 error);
188 aout_plt_sect = seg_alloc();
189 ldef("..plt", aout_plt_sect + 1, 0L, NULL, false, false, &of_aoutb,
190 error);
191 aout_sym_sect = seg_alloc();
192 ldef("..sym", aout_sym_sect + 1, 0L, NULL, false, false, &of_aoutb,
193 error);
196 #endif
198 static void aout_cleanup(int debuginfo)
200 struct Reloc *r;
202 (void)debuginfo;
204 aout_pad_sections();
205 aout_fixup_relocs(&stext);
206 aout_fixup_relocs(&sdata);
207 aout_write();
208 fclose(aoutfp);
209 saa_free(stext.data);
210 while (stext.head) {
211 r = stext.head;
212 stext.head = stext.head->next;
213 nasm_free(r);
215 saa_free(sdata.data);
216 while (sdata.head) {
217 r = sdata.head;
218 sdata.head = sdata.head->next;
219 nasm_free(r);
221 saa_free(syms);
222 raa_free(bsym);
223 saa_free(strs);
226 static int32_t aout_section_names(char *name, int pass, int *bits)
229 (void)pass;
232 * Default to 32 bits.
234 if (!name)
235 *bits = 32;
237 if (!name)
238 return stext.index;
240 if (!strcmp(name, ".text"))
241 return stext.index;
242 else if (!strcmp(name, ".data"))
243 return sdata.index;
244 else if (!strcmp(name, ".bss"))
245 return sbss.index;
246 else
247 return NO_SEG;
250 static void aout_deflabel(char *name, int32_t segment, int64_t offset,
251 int is_global, char *special)
253 int pos = strslen + 4;
254 struct Symbol *sym;
255 int special_used = false;
257 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
259 * This is a NASM special symbol. We never allow it into
260 * the a.out symbol table, even if it's a valid one. If it
261 * _isn't_ a valid one, we should barf immediately.
263 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
264 strcmp(name, "..got") && strcmp(name, "..plt") &&
265 strcmp(name, "..sym"))
266 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
267 return;
270 if (is_global == 3) {
271 struct Symbol **s;
273 * Fix up a forward-reference symbol size from the first
274 * pass.
276 for (s = &fwds; *s; s = &(*s)->nextfwd)
277 if (!strcmp((*s)->name, name)) {
278 struct tokenval tokval;
279 expr *e;
280 char *p = special;
282 while (*p && !nasm_isspace(*p))
283 p++;
284 while (*p && nasm_isspace(*p))
285 p++;
286 stdscan_reset();
287 stdscan_bufptr = p;
288 tokval.t_type = TOKEN_INVALID;
289 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
290 if (e) {
291 if (!is_simple(e))
292 error(ERR_NONFATAL, "cannot use relocatable"
293 " expression as symbol size");
294 else
295 (*s)->size = reloc_value(e);
299 * Remove it from the list of unresolved sizes.
301 nasm_free((*s)->name);
302 *s = (*s)->nextfwd;
303 return;
305 return; /* it wasn't an important one */
308 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
309 strslen += 1 + strlen(name);
311 sym = saa_wstruct(syms);
313 sym->strpos = pos;
314 sym->type = is_global ? SYM_GLOBAL : 0;
315 sym->segment = segment;
316 if (segment == NO_SEG)
317 sym->type |= SECT_ABS;
318 else if (segment == stext.index) {
319 sym->type |= SECT_TEXT;
320 if (is_global) {
321 sym->next = stext.gsyms;
322 stext.gsyms = sym;
323 } else if (!stext.asym)
324 stext.asym = sym;
325 } else if (segment == sdata.index) {
326 sym->type |= SECT_DATA;
327 if (is_global) {
328 sym->next = sdata.gsyms;
329 sdata.gsyms = sym;
330 } else if (!sdata.asym)
331 sdata.asym = sym;
332 } else if (segment == sbss.index) {
333 sym->type |= SECT_BSS;
334 if (is_global) {
335 sym->next = sbss.gsyms;
336 sbss.gsyms = sym;
337 } else if (!sbss.asym)
338 sbss.asym = sym;
339 } else
340 sym->type = SYM_GLOBAL;
341 if (is_global == 2)
342 sym->value = offset;
343 else
344 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
346 if (is_global && sym->type != SYM_GLOBAL) {
348 * Global symbol exported _from_ this module. We must check
349 * the special text for type information.
352 if (special) {
353 int n = strcspn(special, " ");
355 if (!nasm_strnicmp(special, "function", n))
356 sym->type |= SYM_FUNCTION;
357 else if (!nasm_strnicmp(special, "data", n) ||
358 !nasm_strnicmp(special, "object", n))
359 sym->type |= SYM_DATA;
360 else
361 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
362 n, special);
363 if (special[n]) {
364 struct tokenval tokval;
365 expr *e;
366 int fwd = false;
367 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
369 if (!bsd) {
370 error(ERR_NONFATAL, "Linux a.out does not support"
371 " symbol size information");
372 } else {
373 while (special[n] && nasm_isspace(special[n]))
374 n++;
376 * We have a size expression; attempt to
377 * evaluate it.
379 sym->type |= SYM_WITH_SIZE;
380 stdscan_reset();
381 stdscan_bufptr = special + n;
382 tokval.t_type = TOKEN_INVALID;
383 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
384 NULL);
385 if (fwd) {
386 sym->nextfwd = fwds;
387 fwds = sym;
388 sym->name = nasm_strdup(name);
389 } else if (e) {
390 if (!is_simple(e))
391 error(ERR_NONFATAL, "cannot use relocatable"
392 " expression as symbol size");
393 else
394 sym->size = reloc_value(e);
397 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
399 special_used = true;
404 * define the references from external-symbol segment numbers
405 * to these symbol records.
407 if (segment != NO_SEG && segment != stext.index &&
408 segment != sdata.index && segment != sbss.index)
409 bsym = raa_write(bsym, segment, nsyms);
410 sym->symnum = nsyms;
412 nsyms++;
413 if (sym->type & SYM_WITH_SIZE)
414 nsyms++; /* and another for the size */
416 if (special && !special_used)
417 error(ERR_NONFATAL, "no special symbol features supported here");
420 static void aout_add_reloc(struct Section *sect, int32_t segment,
421 int reltype, int bytes)
423 struct Reloc *r;
425 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
426 sect->tail = &r->next;
427 r->next = NULL;
429 r->address = sect->len;
430 r->symbol = (segment == NO_SEG ? -SECT_ABS :
431 segment == stext.index ? -SECT_TEXT :
432 segment == sdata.index ? -SECT_DATA :
433 segment == sbss.index ? -SECT_BSS :
434 raa_read(bsym, segment));
435 r->reltype = reltype;
436 if (r->symbol >= 0)
437 r->reltype |= RELTYPE_SYMFLAG;
438 r->bytes = bytes;
440 sect->nrelocs++;
444 * This routine deals with ..got and ..sym relocations: the more
445 * complicated kinds. In shared-library writing, some relocations
446 * with respect to global symbols must refer to the precise symbol
447 * rather than referring to an offset from the base of the section
448 * _containing_ the symbol. Such relocations call to this routine,
449 * which searches the symbol list for the symbol in question.
451 * RELTYPE_GOT references require the _exact_ symbol address to be
452 * used; RELTYPE_ABSOLUTE references can be at an offset from the
453 * symbol. The boolean argument `exact' tells us this.
455 * Return value is the adjusted value of `addr', having become an
456 * offset from the symbol rather than the section. Should always be
457 * zero when returning from an exact call.
459 * Limitation: if you define two symbols at the same place,
460 * confusion will occur.
462 * Inefficiency: we search, currently, using a linked list which
463 * isn't even necessarily sorted.
465 static int32_t aout_add_gsym_reloc(struct Section *sect,
466 int32_t segment, int32_t offset,
467 int type, int bytes, int exact)
469 struct Symbol *sym, *sm, *shead;
470 struct Reloc *r;
473 * First look up the segment to find whether it's text, data,
474 * bss or an external symbol.
476 shead = NULL;
477 if (segment == stext.index)
478 shead = stext.gsyms;
479 else if (segment == sdata.index)
480 shead = sdata.gsyms;
481 else if (segment == sbss.index)
482 shead = sbss.gsyms;
483 if (!shead) {
484 if (exact && offset != 0)
485 error(ERR_NONFATAL, "unable to find a suitable global symbol"
486 " for this reference");
487 else
488 aout_add_reloc(sect, segment, type, bytes);
489 return offset;
492 if (exact) {
494 * Find a symbol pointing _exactly_ at this one.
496 for (sym = shead; sym; sym = sym->next)
497 if (sym->value == offset)
498 break;
499 } else {
501 * Find the nearest symbol below this one.
503 sym = NULL;
504 for (sm = shead; sm; sm = sm->next)
505 if (sm->value <= offset && (!sym || sm->value > sym->value))
506 sym = sm;
508 if (!sym && exact) {
509 error(ERR_NONFATAL, "unable to find a suitable global symbol"
510 " for this reference");
511 return 0;
514 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
515 sect->tail = &r->next;
516 r->next = NULL;
518 r->address = sect->len;
519 r->symbol = sym->symnum;
520 r->reltype = type | RELTYPE_SYMFLAG;
521 r->bytes = bytes;
523 sect->nrelocs++;
525 return offset - sym->value;
529 * This routine deals with ..gotoff relocations. These _must_ refer
530 * to a symbol, due to a perversity of *BSD's PIC implementation,
531 * and it must be a non-global one as well; so we store `asym', the
532 * first nonglobal symbol defined in each section, and always work
533 * from that. Relocation type is always RELTYPE_GOTOFF.
535 * Return value is the adjusted value of `addr', having become an
536 * offset from the `asym' symbol rather than the section.
538 static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
539 int32_t offset, int bytes)
541 struct Reloc *r;
542 struct Symbol *asym;
545 * First look up the segment to find whether it's text, data,
546 * bss or an external symbol.
548 asym = NULL;
549 if (segment == stext.index)
550 asym = stext.asym;
551 else if (segment == sdata.index)
552 asym = sdata.asym;
553 else if (segment == sbss.index)
554 asym = sbss.asym;
555 if (!asym)
556 error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
557 " symbol in the section");
559 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
560 sect->tail = &r->next;
561 r->next = NULL;
563 r->address = sect->len;
564 r->symbol = asym->symnum;
565 r->reltype = RELTYPE_GOTOFF;
566 r->bytes = bytes;
568 sect->nrelocs++;
570 return offset - asym->value;
573 static void aout_out(int32_t segto, const void *data,
574 enum out_type type, uint64_t size,
575 int32_t segment, int32_t wrt)
577 struct Section *s;
578 int32_t addr;
579 uint8_t mydata[4], *p;
582 * handle absolute-assembly (structure definitions)
584 if (segto == NO_SEG) {
585 if (type != OUT_RESERVE)
586 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
587 " space");
588 return;
591 if (segto == stext.index)
592 s = &stext;
593 else if (segto == sdata.index)
594 s = &sdata;
595 else if (segto == sbss.index)
596 s = NULL;
597 else {
598 error(ERR_WARNING, "attempt to assemble code in"
599 " segment %d: defaulting to `.text'", segto);
600 s = &stext;
603 if (!s && type != OUT_RESERVE) {
604 error(ERR_WARNING, "attempt to initialize memory in the"
605 " BSS section: ignored");
606 if (type == OUT_REL2ADR)
607 size = 2;
608 else if (type == OUT_REL4ADR)
609 size = 4;
610 sbss.len += size;
611 return;
614 if (type == OUT_RESERVE) {
615 if (s) {
616 error(ERR_WARNING, "uninitialized space declared in"
617 " %s section: zeroing",
618 (segto == stext.index ? "code" : "data"));
619 aout_sect_write(s, NULL, size);
620 } else
621 sbss.len += size;
622 } else if (type == OUT_RAWDATA) {
623 if (segment != NO_SEG)
624 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
625 aout_sect_write(s, data, size);
626 } else if (type == OUT_ADDRESS) {
627 addr = *(int64_t *)data;
628 if (segment != NO_SEG) {
629 if (segment % 2) {
630 error(ERR_NONFATAL, "a.out format does not support"
631 " segment base references");
632 } else {
633 if (wrt == NO_SEG) {
634 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE,
635 size);
636 } else if (!bsd) {
637 error(ERR_NONFATAL,
638 "Linux a.out format does not support"
639 " any use of WRT");
640 wrt = NO_SEG; /* we can at least _try_ to continue */
641 } else if (wrt == aout_gotpc_sect + 1) {
642 is_pic = 0x40;
643 aout_add_reloc(s, segment, RELTYPE_GOTPC, size);
644 } else if (wrt == aout_gotoff_sect + 1) {
645 is_pic = 0x40;
646 addr = aout_add_gotoff_reloc(s, segment,
647 addr, size);
648 } else if (wrt == aout_got_sect + 1) {
649 is_pic = 0x40;
650 addr =
651 aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
652 size, true);
653 } else if (wrt == aout_sym_sect + 1) {
654 addr = aout_add_gsym_reloc(s, segment, addr,
655 RELTYPE_ABSOLUTE, size,
656 false);
657 } else if (wrt == aout_plt_sect + 1) {
658 is_pic = 0x40;
659 error(ERR_NONFATAL,
660 "a.out format cannot produce non-PC-"
661 "relative PLT references");
662 } else {
663 error(ERR_NONFATAL,
664 "a.out format does not support this"
665 " use of WRT");
666 wrt = NO_SEG; /* we can at least _try_ to continue */
670 p = mydata;
671 if (size == 2)
672 WRITESHORT(p, addr);
673 else
674 WRITELONG(p, addr);
675 aout_sect_write(s, mydata, size);
676 } else if (type == OUT_REL2ADR) {
677 if (segment == segto)
678 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
679 if (segment != NO_SEG && segment % 2) {
680 error(ERR_NONFATAL, "a.out format does not support"
681 " segment base references");
682 } else {
683 if (wrt == NO_SEG) {
684 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
685 } else if (!bsd) {
686 error(ERR_NONFATAL, "Linux a.out format does not support"
687 " any use of WRT");
688 wrt = NO_SEG; /* we can at least _try_ to continue */
689 } else if (wrt == aout_plt_sect + 1) {
690 is_pic = 0x40;
691 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
692 } else if (wrt == aout_gotpc_sect + 1 ||
693 wrt == aout_gotoff_sect + 1 ||
694 wrt == aout_got_sect + 1) {
695 error(ERR_NONFATAL, "a.out format cannot produce PC-"
696 "relative GOT references");
697 } else {
698 error(ERR_NONFATAL, "a.out format does not support this"
699 " use of WRT");
700 wrt = NO_SEG; /* we can at least _try_ to continue */
703 p = mydata;
704 WRITESHORT(p, *(int64_t *)data - (size + s->len));
705 aout_sect_write(s, mydata, 2L);
706 } else if (type == OUT_REL4ADR) {
707 if (segment == segto)
708 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
709 if (segment != NO_SEG && segment % 2) {
710 error(ERR_NONFATAL, "a.out format does not support"
711 " segment base references");
712 } else {
713 if (wrt == NO_SEG) {
714 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
715 } else if (!bsd) {
716 error(ERR_NONFATAL, "Linux a.out format does not support"
717 " any use of WRT");
718 wrt = NO_SEG; /* we can at least _try_ to continue */
719 } else if (wrt == aout_plt_sect + 1) {
720 is_pic = 0x40;
721 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
722 } else if (wrt == aout_gotpc_sect + 1 ||
723 wrt == aout_gotoff_sect + 1 ||
724 wrt == aout_got_sect + 1) {
725 error(ERR_NONFATAL, "a.out format cannot produce PC-"
726 "relative GOT references");
727 } else {
728 error(ERR_NONFATAL, "a.out format does not support this"
729 " use of WRT");
730 wrt = NO_SEG; /* we can at least _try_ to continue */
733 p = mydata;
734 WRITELONG(p, *(int64_t *)data - (size + s->len));
735 aout_sect_write(s, mydata, 4L);
739 static void aout_pad_sections(void)
741 static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
743 * Pad each of the text and data sections with NOPs until their
744 * length is a multiple of four. (NOP == 0x90.) Also increase
745 * the length of the BSS section similarly.
747 aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
748 aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
749 sbss.len = (sbss.len + 3) & ~3;
753 * a.out files have the curious property that all references to
754 * things in the data or bss sections are done by addresses which
755 * are actually relative to the start of the _text_ section, in the
756 * _file_. (No relation to what happens after linking. No idea why
757 * this should be so. It's very strange.) So we have to go through
758 * the relocation table, _after_ the final size of each section is
759 * known, and fix up the relocations pointed to.
761 static void aout_fixup_relocs(struct Section *sect)
763 struct Reloc *r;
765 saa_rewind(sect->data);
766 for (r = sect->head; r; r = r->next) {
767 uint8_t *p, *q, blk[4];
768 int32_t l;
770 saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
771 p = q = blk;
772 l = *p++;
773 if (r->bytes > 1) {
774 l += ((int32_t)*p++) << 8;
775 if (r->bytes == 4) {
776 l += ((int32_t)*p++) << 16;
777 l += ((int32_t)*p++) << 24;
780 if (r->symbol == -SECT_DATA)
781 l += stext.len;
782 else if (r->symbol == -SECT_BSS)
783 l += stext.len + sdata.len;
784 if (r->bytes == 4)
785 WRITELONG(q, l);
786 else if (r->bytes == 2)
787 WRITESHORT(q, l);
788 else
789 *q++ = l & 0xFF;
790 saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
794 static void aout_write(void)
797 * Emit the a.out header.
799 /* OMAGIC, M_386 or MID_I386, no flags */
800 fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
801 fwriteint32_t(stext.len, aoutfp);
802 fwriteint32_t(sdata.len, aoutfp);
803 fwriteint32_t(sbss.len, aoutfp);
804 fwriteint32_t(nsyms * 12, aoutfp); /* length of symbol table */
805 fwriteint32_t(0L, aoutfp); /* object files have no entry point */
806 fwriteint32_t(stext.nrelocs * 8, aoutfp); /* size of text relocs */
807 fwriteint32_t(sdata.nrelocs * 8, aoutfp); /* size of data relocs */
810 * Write out the code section and the data section.
812 saa_fpwrite(stext.data, aoutfp);
813 saa_fpwrite(sdata.data, aoutfp);
816 * Write out the relocations.
818 aout_write_relocs(stext.head);
819 aout_write_relocs(sdata.head);
822 * Write the symbol table.
824 aout_write_syms();
827 * And the string table.
829 fwriteint32_t(strslen + 4, aoutfp); /* length includes length count */
830 saa_fpwrite(strs, aoutfp);
833 static void aout_write_relocs(struct Reloc *r)
835 while (r) {
836 uint32_t word2;
838 fwriteint32_t(r->address, aoutfp);
840 if (r->symbol >= 0)
841 word2 = r->symbol;
842 else
843 word2 = -r->symbol;
844 word2 |= r->reltype << 24;
845 word2 |= (r->bytes == 1 ? 0 :
846 r->bytes == 2 ? 0x2000000L : 0x4000000L);
847 fwriteint32_t(word2, aoutfp);
849 r = r->next;
853 static void aout_write_syms(void)
855 uint32_t i;
857 saa_rewind(syms);
858 for (i = 0; i < nsyms; i++) {
859 struct Symbol *sym = saa_rstruct(syms);
860 fwriteint32_t(sym->strpos, aoutfp);
861 fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, aoutfp);
863 * Fix up the symbol value now we know the final section
864 * sizes.
866 if ((sym->type & SECT_MASK) == SECT_DATA)
867 sym->value += stext.len;
868 if ((sym->type & SECT_MASK) == SECT_BSS)
869 sym->value += stext.len + sdata.len;
870 fwriteint32_t(sym->value, aoutfp);
872 * Output a size record if necessary.
874 if (sym->type & SYM_WITH_SIZE) {
875 fwriteint32_t(sym->strpos, aoutfp);
876 fwriteint32_t(0x0DL, aoutfp); /* special value: means size */
877 fwriteint32_t(sym->size, aoutfp);
878 i++; /* use up another of `nsyms' */
883 static void aout_sect_write(struct Section *sect,
884 const uint8_t *data, uint32_t len)
886 saa_wbytes(sect->data, data, len);
887 sect->len += len;
890 static int32_t aout_segbase(int32_t segment)
892 return segment;
895 static int aout_directive(char *directive, char *value, int pass)
897 (void)directive;
898 (void)value;
899 (void)pass;
900 return 0;
903 static void aout_filename(char *inname, char *outname, efunc error)
905 standard_extension(inname, outname, ".o", error);
908 extern macros_t aout_stdmac[];
910 static int aout_set_info(enum geninfo type, char **val)
912 (void)type;
913 (void)val;
914 return 0;
916 #endif /* OF_AOUT || OF_AOUTB */
918 #ifdef OF_AOUT
920 struct ofmt of_aout = {
921 "Linux a.out object files",
922 "aout",
923 NULL,
924 null_debug_arr,
925 &null_debug_form,
926 aout_stdmac,
927 aout_init,
928 aout_set_info,
929 aout_out,
930 aout_deflabel,
931 aout_section_names,
932 aout_segbase,
933 aout_directive,
934 aout_filename,
935 aout_cleanup
938 #endif
940 #ifdef OF_AOUTB
942 struct ofmt of_aoutb = {
943 "NetBSD/FreeBSD a.out object files",
944 "aoutb",
945 NULL,
946 null_debug_arr,
947 &null_debug_form,
948 aout_stdmac,
949 aoutb_init,
950 aout_set_info,
951 aout_out,
952 aout_deflabel,
953 aout_section_names,
954 aout_segbase,
955 aout_directive,
956 aout_filename,
957 aout_cleanup
960 #endif