Apply Nindent to all .c and .h files
[nasm/avx512.git] / output / outaout.c
blobdabfe1f968e93100518b18af16af32e2da34393c
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 *, const unsigned char *,
107 unsigned long);
108 static void aout_pad_sections(void);
109 static void aout_fixup_relocs(struct Section *);
112 * Special section numbers which are used to define special
113 * symbols, which can be used with WRT to provide PIC relocation
114 * types.
116 static long aout_gotpc_sect, aout_gotoff_sect;
117 static long aout_got_sect, aout_plt_sect;
118 static long aout_sym_sect;
120 static void aoutg_init(FILE * fp, efunc errfunc, ldfunc ldef,
121 evalfunc eval)
123 aoutfp = fp;
124 error = errfunc;
125 evaluate = eval;
126 (void)ldef; /* placate optimisers */
127 stext.data = saa_init(1L);
128 stext.head = NULL;
129 stext.tail = &stext.head;
130 sdata.data = saa_init(1L);
131 sdata.head = NULL;
132 sdata.tail = &sdata.head;
133 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
134 stext.nrelocs = sdata.nrelocs = 0;
135 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
136 stext.index = seg_alloc();
137 sdata.index = seg_alloc();
138 sbss.index = seg_alloc();
139 stext.asym = sdata.asym = sbss.asym = NULL;
140 syms = saa_init((long)sizeof(struct Symbol));
141 nsyms = 0;
142 bsym = raa_init();
143 strs = saa_init(1L);
144 strslen = 0;
145 fwds = NULL;
148 #ifdef OF_AOUT
150 static void aout_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
152 bsd = FALSE;
153 aoutg_init(fp, errfunc, ldef, eval);
155 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
156 aout_plt_sect = aout_sym_sect = NO_SEG;
159 #endif
161 #ifdef OF_AOUTB
163 extern struct ofmt of_aoutb;
165 static void aoutb_init(FILE * fp, efunc errfunc, ldfunc ldef,
166 evalfunc eval)
168 bsd = TRUE;
169 aoutg_init(fp, errfunc, ldef, eval);
171 is_pic = 0x00; /* may become 0x40 */
173 aout_gotpc_sect = seg_alloc();
174 ldef("..gotpc", aout_gotpc_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
175 error);
176 aout_gotoff_sect = seg_alloc();
177 ldef("..gotoff", aout_gotoff_sect + 1, 0L, NULL, FALSE, FALSE,
178 &of_aoutb, error);
179 aout_got_sect = seg_alloc();
180 ldef("..got", aout_got_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
181 error);
182 aout_plt_sect = seg_alloc();
183 ldef("..plt", aout_plt_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
184 error);
185 aout_sym_sect = seg_alloc();
186 ldef("..sym", aout_sym_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
187 error);
190 #endif
192 static void aout_cleanup(int debuginfo)
194 struct Reloc *r;
196 (void)debuginfo;
198 aout_pad_sections();
199 aout_fixup_relocs(&stext);
200 aout_fixup_relocs(&sdata);
201 aout_write();
202 fclose(aoutfp);
203 saa_free(stext.data);
204 while (stext.head) {
205 r = stext.head;
206 stext.head = stext.head->next;
207 nasm_free(r);
209 saa_free(sdata.data);
210 while (sdata.head) {
211 r = sdata.head;
212 sdata.head = sdata.head->next;
213 nasm_free(r);
215 saa_free(syms);
216 raa_free(bsym);
217 saa_free(strs);
220 static long aout_section_names(char *name, int pass, int *bits)
223 * Default to 32 bits.
225 if (!name)
226 *bits = 32;
228 if (!name)
229 return stext.index;
231 if (!strcmp(name, ".text"))
232 return stext.index;
233 else if (!strcmp(name, ".data"))
234 return sdata.index;
235 else if (!strcmp(name, ".bss"))
236 return sbss.index;
237 else
238 return NO_SEG;
241 static void aout_deflabel(char *name, long segment, long offset,
242 int is_global, char *special)
244 int pos = strslen + 4;
245 struct Symbol *sym;
246 int special_used = FALSE;
248 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
250 * This is a NASM special symbol. We never allow it into
251 * the a.out symbol table, even if it's a valid one. If it
252 * _isn't_ a valid one, we should barf immediately.
254 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
255 strcmp(name, "..got") && strcmp(name, "..plt") &&
256 strcmp(name, "..sym"))
257 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
258 return;
261 if (is_global == 3) {
262 struct Symbol **s;
264 * Fix up a forward-reference symbol size from the first
265 * pass.
267 for (s = &fwds; *s; s = &(*s)->nextfwd)
268 if (!strcmp((*s)->name, name)) {
269 struct tokenval tokval;
270 expr *e;
271 char *p = special;
273 while (*p && !isspace(*p))
274 p++;
275 while (*p && isspace(*p))
276 p++;
277 stdscan_reset();
278 stdscan_bufptr = p;
279 tokval.t_type = TOKEN_INVALID;
280 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
281 if (e) {
282 if (!is_simple(e))
283 error(ERR_NONFATAL, "cannot use relocatable"
284 " expression as symbol size");
285 else
286 (*s)->size = reloc_value(e);
290 * Remove it from the list of unresolved sizes.
292 nasm_free((*s)->name);
293 *s = (*s)->nextfwd;
294 return;
296 return; /* it wasn't an important one */
299 saa_wbytes(strs, name, (long)(1 + strlen(name)));
300 strslen += 1 + strlen(name);
302 sym = saa_wstruct(syms);
304 sym->strpos = pos;
305 sym->type = is_global ? SYM_GLOBAL : 0;
306 sym->segment = segment;
307 if (segment == NO_SEG)
308 sym->type |= SECT_ABS;
309 else if (segment == stext.index) {
310 sym->type |= SECT_TEXT;
311 if (is_global) {
312 sym->next = stext.gsyms;
313 stext.gsyms = sym;
314 } else if (!stext.asym)
315 stext.asym = sym;
316 } else if (segment == sdata.index) {
317 sym->type |= SECT_DATA;
318 if (is_global) {
319 sym->next = sdata.gsyms;
320 sdata.gsyms = sym;
321 } else if (!sdata.asym)
322 sdata.asym = sym;
323 } else if (segment == sbss.index) {
324 sym->type |= SECT_BSS;
325 if (is_global) {
326 sym->next = sbss.gsyms;
327 sbss.gsyms = sym;
328 } else if (!sbss.asym)
329 sbss.asym = sym;
330 } else
331 sym->type = SYM_GLOBAL;
332 if (is_global == 2)
333 sym->value = offset;
334 else
335 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
337 if (is_global && sym->type != SYM_GLOBAL) {
339 * Global symbol exported _from_ this module. We must check
340 * the special text for type information.
343 if (special) {
344 int n = strcspn(special, " ");
346 if (!nasm_strnicmp(special, "function", n))
347 sym->type |= SYM_FUNCTION;
348 else if (!nasm_strnicmp(special, "data", n) ||
349 !nasm_strnicmp(special, "object", n))
350 sym->type |= SYM_DATA;
351 else
352 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
353 n, special);
354 if (special[n]) {
355 struct tokenval tokval;
356 expr *e;
357 int fwd = FALSE;
358 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
360 if (!bsd) {
361 error(ERR_NONFATAL, "Linux a.out does not support"
362 " symbol size information");
363 } else {
364 while (special[n] && isspace(special[n]))
365 n++;
367 * We have a size expression; attempt to
368 * evaluate it.
370 sym->type |= SYM_WITH_SIZE;
371 stdscan_reset();
372 stdscan_bufptr = special + n;
373 tokval.t_type = TOKEN_INVALID;
374 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
375 NULL);
376 if (fwd) {
377 sym->nextfwd = fwds;
378 fwds = sym;
379 sym->name = nasm_strdup(name);
380 } else if (e) {
381 if (!is_simple(e))
382 error(ERR_NONFATAL, "cannot use relocatable"
383 " expression as symbol size");
384 else
385 sym->size = reloc_value(e);
388 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
390 special_used = TRUE;
395 * define the references from external-symbol segment numbers
396 * to these symbol records.
398 if (segment != NO_SEG && segment != stext.index &&
399 segment != sdata.index && segment != sbss.index)
400 bsym = raa_write(bsym, segment, nsyms);
401 sym->symnum = nsyms;
403 nsyms++;
404 if (sym->type & SYM_WITH_SIZE)
405 nsyms++; /* and another for the size */
407 if (special && !special_used)
408 error(ERR_NONFATAL, "no special symbol features supported here");
411 static void aout_add_reloc(struct Section *sect, long segment,
412 int reltype, int bytes)
414 struct Reloc *r;
416 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
417 sect->tail = &r->next;
418 r->next = NULL;
420 r->address = sect->len;
421 r->symbol = (segment == NO_SEG ? -SECT_ABS :
422 segment == stext.index ? -SECT_TEXT :
423 segment == sdata.index ? -SECT_DATA :
424 segment == sbss.index ? -SECT_BSS :
425 raa_read(bsym, segment));
426 r->reltype = reltype;
427 if (r->symbol >= 0)
428 r->reltype |= RELTYPE_SYMFLAG;
429 r->bytes = bytes;
431 sect->nrelocs++;
435 * This routine deals with ..got and ..sym relocations: the more
436 * complicated kinds. In shared-library writing, some relocations
437 * with respect to global symbols must refer to the precise symbol
438 * rather than referring to an offset from the base of the section
439 * _containing_ the symbol. Such relocations call to this routine,
440 * which searches the symbol list for the symbol in question.
442 * RELTYPE_GOT references require the _exact_ symbol address to be
443 * used; RELTYPE_ABSOLUTE references can be at an offset from the
444 * symbol. The boolean argument `exact' tells us this.
446 * Return value is the adjusted value of `addr', having become an
447 * offset from the symbol rather than the section. Should always be
448 * zero when returning from an exact call.
450 * Limitation: if you define two symbols at the same place,
451 * confusion will occur.
453 * Inefficiency: we search, currently, using a linked list which
454 * isn't even necessarily sorted.
456 static long aout_add_gsym_reloc(struct Section *sect,
457 long segment, long offset,
458 int type, int bytes, int exact)
460 struct Symbol *sym, *sm, *shead;
461 struct Reloc *r;
464 * First look up the segment to find whether it's text, data,
465 * bss or an external symbol.
467 shead = NULL;
468 if (segment == stext.index)
469 shead = stext.gsyms;
470 else if (segment == sdata.index)
471 shead = sdata.gsyms;
472 else if (segment == sbss.index)
473 shead = sbss.gsyms;
474 if (!shead) {
475 if (exact && offset != 0)
476 error(ERR_NONFATAL, "unable to find a suitable global symbol"
477 " for this reference");
478 else
479 aout_add_reloc(sect, segment, type, bytes);
480 return offset;
483 if (exact) {
485 * Find a symbol pointing _exactly_ at this one.
487 for (sym = shead; sym; sym = sym->next)
488 if (sym->value == offset)
489 break;
490 } else {
492 * Find the nearest symbol below this one.
494 sym = NULL;
495 for (sm = shead; sm; sm = sm->next)
496 if (sm->value <= offset && (!sym || sm->value > sym->value))
497 sym = sm;
499 if (!sym && exact) {
500 error(ERR_NONFATAL, "unable to find a suitable global symbol"
501 " for this reference");
502 return 0;
505 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
506 sect->tail = &r->next;
507 r->next = NULL;
509 r->address = sect->len;
510 r->symbol = sym->symnum;
511 r->reltype = type | RELTYPE_SYMFLAG;
512 r->bytes = bytes;
514 sect->nrelocs++;
516 return offset - sym->value;
520 * This routine deals with ..gotoff relocations. These _must_ refer
521 * to a symbol, due to a perversity of *BSD's PIC implementation,
522 * and it must be a non-global one as well; so we store `asym', the
523 * first nonglobal symbol defined in each section, and always work
524 * from that. Relocation type is always RELTYPE_GOTOFF.
526 * Return value is the adjusted value of `addr', having become an
527 * offset from the `asym' symbol rather than the section.
529 static long aout_add_gotoff_reloc(struct Section *sect, long segment,
530 long offset, int bytes)
532 struct Reloc *r;
533 struct Symbol *asym;
536 * First look up the segment to find whether it's text, data,
537 * bss or an external symbol.
539 asym = NULL;
540 if (segment == stext.index)
541 asym = stext.asym;
542 else if (segment == sdata.index)
543 asym = sdata.asym;
544 else if (segment == sbss.index)
545 asym = sbss.asym;
546 if (!asym)
547 error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
548 " symbol in the section");
550 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
551 sect->tail = &r->next;
552 r->next = NULL;
554 r->address = sect->len;
555 r->symbol = asym->symnum;
556 r->reltype = RELTYPE_GOTOFF;
557 r->bytes = bytes;
559 sect->nrelocs++;
561 return offset - asym->value;
564 static void aout_out(long segto, const void *data, unsigned long type,
565 long segment, long wrt)
567 struct Section *s;
568 long realbytes = type & OUT_SIZMASK;
569 long addr;
570 unsigned char mydata[4], *p;
572 type &= OUT_TYPMASK;
575 * handle absolute-assembly (structure definitions)
577 if (segto == NO_SEG) {
578 if (type != OUT_RESERVE)
579 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
580 " space");
581 return;
584 if (segto == stext.index)
585 s = &stext;
586 else if (segto == sdata.index)
587 s = &sdata;
588 else if (segto == sbss.index)
589 s = NULL;
590 else {
591 error(ERR_WARNING, "attempt to assemble code in"
592 " segment %d: defaulting to `.text'", segto);
593 s = &stext;
596 if (!s && type != OUT_RESERVE) {
597 error(ERR_WARNING, "attempt to initialise memory in the"
598 " BSS section: ignored");
599 if (type == OUT_REL2ADR)
600 realbytes = 2;
601 else if (type == OUT_REL4ADR)
602 realbytes = 4;
603 sbss.len += realbytes;
604 return;
607 if (type == OUT_RESERVE) {
608 if (s) {
609 error(ERR_WARNING, "uninitialised space declared in"
610 " %s section: zeroing",
611 (segto == stext.index ? "code" : "data"));
612 aout_sect_write(s, NULL, realbytes);
613 } else
614 sbss.len += realbytes;
615 } else if (type == OUT_RAWDATA) {
616 if (segment != NO_SEG)
617 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
618 aout_sect_write(s, data, realbytes);
619 } else if (type == OUT_ADDRESS) {
620 addr = *(long *)data;
621 if (segment != NO_SEG) {
622 if (segment % 2) {
623 error(ERR_NONFATAL, "a.out format does not support"
624 " segment base references");
625 } else {
626 if (wrt == NO_SEG) {
627 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE,
628 realbytes);
629 } else if (!bsd) {
630 error(ERR_NONFATAL,
631 "Linux a.out format does not support"
632 " any use of WRT");
633 wrt = NO_SEG; /* we can at least _try_ to continue */
634 } else if (wrt == aout_gotpc_sect + 1) {
635 is_pic = 0x40;
636 aout_add_reloc(s, segment, RELTYPE_GOTPC, realbytes);
637 } else if (wrt == aout_gotoff_sect + 1) {
638 is_pic = 0x40;
639 addr = aout_add_gotoff_reloc(s, segment,
640 addr, realbytes);
641 } else if (wrt == aout_got_sect + 1) {
642 is_pic = 0x40;
643 addr =
644 aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
645 realbytes, TRUE);
646 } else if (wrt == aout_sym_sect + 1) {
647 addr = aout_add_gsym_reloc(s, segment, addr,
648 RELTYPE_ABSOLUTE, realbytes,
649 FALSE);
650 } else if (wrt == aout_plt_sect + 1) {
651 is_pic = 0x40;
652 error(ERR_NONFATAL,
653 "a.out format cannot produce non-PC-"
654 "relative PLT references");
655 } else {
656 error(ERR_NONFATAL,
657 "a.out format does not support this"
658 " use of WRT");
659 wrt = NO_SEG; /* we can at least _try_ to continue */
663 p = mydata;
664 if (realbytes == 2)
665 WRITESHORT(p, addr);
666 else
667 WRITELONG(p, addr);
668 aout_sect_write(s, mydata, realbytes);
669 } else if (type == OUT_REL2ADR) {
670 if (segment == segto)
671 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
672 if (segment != NO_SEG && segment % 2) {
673 error(ERR_NONFATAL, "a.out format does not support"
674 " segment base references");
675 } else {
676 if (wrt == NO_SEG) {
677 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
678 } else if (!bsd) {
679 error(ERR_NONFATAL, "Linux a.out format does not support"
680 " any use of WRT");
681 wrt = NO_SEG; /* we can at least _try_ to continue */
682 } else if (wrt == aout_plt_sect + 1) {
683 is_pic = 0x40;
684 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
685 } else if (wrt == aout_gotpc_sect + 1 ||
686 wrt == aout_gotoff_sect + 1 ||
687 wrt == aout_got_sect + 1) {
688 error(ERR_NONFATAL, "a.out format cannot produce PC-"
689 "relative GOT references");
690 } else {
691 error(ERR_NONFATAL, "a.out format does not support this"
692 " use of WRT");
693 wrt = NO_SEG; /* we can at least _try_ to continue */
696 p = mydata;
697 WRITESHORT(p, *(long *)data - (realbytes + s->len));
698 aout_sect_write(s, mydata, 2L);
699 } else if (type == OUT_REL4ADR) {
700 if (segment == segto)
701 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
702 if (segment != NO_SEG && segment % 2) {
703 error(ERR_NONFATAL, "a.out format does not support"
704 " segment base references");
705 } else {
706 if (wrt == NO_SEG) {
707 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
708 } else if (!bsd) {
709 error(ERR_NONFATAL, "Linux a.out format does not support"
710 " any use of WRT");
711 wrt = NO_SEG; /* we can at least _try_ to continue */
712 } else if (wrt == aout_plt_sect + 1) {
713 is_pic = 0x40;
714 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
715 } else if (wrt == aout_gotpc_sect + 1 ||
716 wrt == aout_gotoff_sect + 1 ||
717 wrt == aout_got_sect + 1) {
718 error(ERR_NONFATAL, "a.out format cannot produce PC-"
719 "relative GOT references");
720 } else {
721 error(ERR_NONFATAL, "a.out format does not support this"
722 " use of WRT");
723 wrt = NO_SEG; /* we can at least _try_ to continue */
726 p = mydata;
727 WRITELONG(p, *(long *)data - (realbytes + s->len));
728 aout_sect_write(s, mydata, 4L);
732 static void aout_pad_sections(void)
734 static unsigned char pad[] = { 0x90, 0x90, 0x90, 0x90 };
736 * Pad each of the text and data sections with NOPs until their
737 * length is a multiple of four. (NOP == 0x90.) Also increase
738 * the length of the BSS section similarly.
740 aout_sect_write(&stext, pad, (-(long)stext.len) & 3);
741 aout_sect_write(&sdata, pad, (-(long)sdata.len) & 3);
742 sbss.len = (sbss.len + 3) & ~3;
746 * a.out files have the curious property that all references to
747 * things in the data or bss sections are done by addresses which
748 * are actually relative to the start of the _text_ section, in the
749 * _file_. (No relation to what happens after linking. No idea why
750 * this should be so. It's very strange.) So we have to go through
751 * the relocation table, _after_ the final size of each section is
752 * known, and fix up the relocations pointed to.
754 static void aout_fixup_relocs(struct Section *sect)
756 struct Reloc *r;
758 saa_rewind(sect->data);
759 for (r = sect->head; r; r = r->next) {
760 unsigned char *p, *q, blk[4];
761 long l;
763 saa_fread(sect->data, r->address, blk, (long)r->bytes);
764 p = q = blk;
765 l = *p++;
766 if (r->bytes > 1) {
767 l += ((long)*p++) << 8;
768 if (r->bytes == 4) {
769 l += ((long)*p++) << 16;
770 l += ((long)*p++) << 24;
773 if (r->symbol == -SECT_DATA)
774 l += stext.len;
775 else if (r->symbol == -SECT_BSS)
776 l += stext.len + sdata.len;
777 if (r->bytes == 4)
778 WRITELONG(q, l);
779 else if (r->bytes == 2)
780 WRITESHORT(q, l);
781 else
782 *q++ = l & 0xFF;
783 saa_fwrite(sect->data, r->address, blk, (long)r->bytes);
787 static void aout_write(void)
790 * Emit the a.out header.
792 /* OMAGIC, M_386 or MID_I386, no flags */
793 fwritelong(bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
794 fwritelong(stext.len, aoutfp);
795 fwritelong(sdata.len, aoutfp);
796 fwritelong(sbss.len, aoutfp);
797 fwritelong(nsyms * 12, aoutfp); /* length of symbol table */
798 fwritelong(0L, aoutfp); /* object files have no entry point */
799 fwritelong(stext.nrelocs * 8, aoutfp); /* size of text relocs */
800 fwritelong(sdata.nrelocs * 8, aoutfp); /* size of data relocs */
803 * Write out the code section and the data section.
805 saa_fpwrite(stext.data, aoutfp);
806 saa_fpwrite(sdata.data, aoutfp);
809 * Write out the relocations.
811 aout_write_relocs(stext.head);
812 aout_write_relocs(sdata.head);
815 * Write the symbol table.
817 aout_write_syms();
820 * And the string table.
822 fwritelong(strslen + 4, aoutfp); /* length includes length count */
823 saa_fpwrite(strs, aoutfp);
826 static void aout_write_relocs(struct Reloc *r)
828 while (r) {
829 unsigned long word2;
831 fwritelong(r->address, aoutfp);
833 if (r->symbol >= 0)
834 word2 = r->symbol;
835 else
836 word2 = -r->symbol;
837 word2 |= r->reltype << 24;
838 word2 |= (r->bytes == 1 ? 0 :
839 r->bytes == 2 ? 0x2000000L : 0x4000000L);
840 fwritelong(word2, aoutfp);
842 r = r->next;
846 static void aout_write_syms(void)
848 unsigned long i;
850 saa_rewind(syms);
851 for (i = 0; i < nsyms; i++) {
852 struct Symbol *sym = saa_rstruct(syms);
853 fwritelong(sym->strpos, aoutfp);
854 fwritelong((long)sym->type & ~SYM_WITH_SIZE, aoutfp);
856 * Fix up the symbol value now we know the final section
857 * sizes.
859 if ((sym->type & SECT_MASK) == SECT_DATA)
860 sym->value += stext.len;
861 if ((sym->type & SECT_MASK) == SECT_BSS)
862 sym->value += stext.len + sdata.len;
863 fwritelong(sym->value, aoutfp);
865 * Output a size record if necessary.
867 if (sym->type & SYM_WITH_SIZE) {
868 fwritelong(sym->strpos, aoutfp);
869 fwritelong(0x0DL, aoutfp); /* special value: means size */
870 fwritelong(sym->size, aoutfp);
871 i++; /* use up another of `nsyms' */
876 static void aout_sect_write(struct Section *sect,
877 const unsigned char *data, unsigned long len)
879 saa_wbytes(sect->data, data, len);
880 sect->len += len;
883 static long aout_segbase(long segment)
885 return segment;
888 static int aout_directive(char *directive, char *value, int pass)
890 return 0;
893 static void aout_filename(char *inname, char *outname, efunc error)
895 standard_extension(inname, outname, ".o", error);
898 static const char *aout_stdmac[] = {
899 "%define __SECT__ [section .text]",
900 "%macro __NASM_CDecl__ 1",
901 "%endmacro",
902 NULL
905 static int aout_set_info(enum geninfo type, char **val)
907 return 0;
909 #endif /* OF_AOUT || OF_AOUTB */
911 #ifdef OF_AOUT
913 struct ofmt of_aout = {
914 "Linux a.out object files",
915 "aout",
916 NULL,
917 null_debug_arr,
918 &null_debug_form,
919 aout_stdmac,
920 aout_init,
921 aout_set_info,
922 aout_out,
923 aout_deflabel,
924 aout_section_names,
925 aout_segbase,
926 aout_directive,
927 aout_filename,
928 aout_cleanup
931 #endif
933 #ifdef OF_AOUTB
935 struct ofmt of_aoutb = {
936 "NetBSD/FreeBSD a.out object files",
937 "aoutb",
938 NULL,
939 null_debug_arr,
940 &null_debug_form,
941 aout_stdmac,
942 aoutb_init,
943 aout_set_info,
944 aout_out,
945 aout_deflabel,
946 aout_section_names,
947 aout_segbase,
948 aout_directive,
949 aout_filename,
950 aout_cleanup
953 #endif