Provide 64-bit support for ORG directive
[nasm/avx512.git] / output / outaout.c
blobd5358b4cd7e302d2542d357ccc6e281310220ccd
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>
14 #include <inttypes.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "stdscan.h"
19 #include "outform.h"
21 #if defined OF_AOUT || defined OF_AOUTB
23 #define RELTYPE_ABSOLUTE 0x00
24 #define RELTYPE_RELATIVE 0x01
25 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
26 #define RELTYPE_GOTOFF 0x10
27 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
28 #define RELTYPE_PLT 0x21
29 #define RELTYPE_SYMFLAG 0x08
31 struct Reloc {
32 struct Reloc *next;
33 int32_t address; /* relative to _start_ of section */
34 int32_t symbol; /* symbol number or -ve section id */
35 int bytes; /* 2 or 4 */
36 int reltype; /* see above */
39 struct Symbol {
40 int32_t strpos; /* string table position of name */
41 int type; /* symbol type - see flags below */
42 int32_t value; /* address, or COMMON variable size */
43 int32_t size; /* size for data or function exports */
44 int32_t segment; /* back-reference used by gsym_reloc */
45 struct Symbol *next; /* list of globals in each section */
46 struct Symbol *nextfwd; /* list of unresolved-size symbols */
47 char *name; /* for unresolved-size symbols */
48 int32_t symnum; /* index into symbol table */
52 * Section IDs - used in Reloc.symbol when negative, and in
53 * Symbol.type when positive.
55 #define SECT_ABS 2 /* absolute value */
56 #define SECT_TEXT 4 /* text section */
57 #define SECT_DATA 6 /* data section */
58 #define SECT_BSS 8 /* bss section */
59 #define SECT_MASK 0xE /* mask out any of the above */
62 * More flags used in Symbol.type.
64 #define SYM_GLOBAL 1 /* it's a global symbol */
65 #define SYM_DATA 0x100 /* used for shared libs */
66 #define SYM_FUNCTION 0x200 /* used for shared libs */
67 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
70 * Bit more explanation of symbol types: SECT_xxx denotes a local
71 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
72 * this module. Just SYM_GLOBAL, with zero value, denotes an
73 * external symbol referenced in this module. And just SYM_GLOBAL,
74 * but with a non-zero value, declares a C `common' variable, of
75 * size `value'.
78 struct Section {
79 struct SAA *data;
80 uint32_t len, size, nrelocs;
81 int32_t index;
82 struct Reloc *head, **tail;
83 struct Symbol *gsyms, *asym;
86 static struct Section stext, sdata, sbss;
88 static struct SAA *syms;
89 static uint32_t nsyms;
91 static struct RAA *bsym;
93 static struct SAA *strs;
94 static uint32_t strslen;
96 static struct Symbol *fwds;
98 static FILE *aoutfp;
99 static efunc error;
100 static evalfunc evaluate;
102 static int bsd;
103 static int is_pic;
105 static void aout_write(void);
106 static void aout_write_relocs(struct Reloc *);
107 static void aout_write_syms(void);
108 static void aout_sect_write(struct Section *, const uint8_t *,
109 uint32_t);
110 static void aout_pad_sections(void);
111 static void aout_fixup_relocs(struct Section *);
114 * Special section numbers which are used to define special
115 * symbols, which can be used with WRT to provide PIC relocation
116 * types.
118 static int32_t aout_gotpc_sect, aout_gotoff_sect;
119 static int32_t aout_got_sect, aout_plt_sect;
120 static int32_t aout_sym_sect;
122 static void aoutg_init(FILE * fp, efunc errfunc, ldfunc ldef,
123 evalfunc eval)
125 aoutfp = fp;
126 error = errfunc;
127 evaluate = eval;
128 (void)ldef; /* placate optimisers */
129 stext.data = saa_init(1L);
130 stext.head = NULL;
131 stext.tail = &stext.head;
132 sdata.data = saa_init(1L);
133 sdata.head = NULL;
134 sdata.tail = &sdata.head;
135 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
136 stext.nrelocs = sdata.nrelocs = 0;
137 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
138 stext.index = seg_alloc();
139 sdata.index = seg_alloc();
140 sbss.index = seg_alloc();
141 stext.asym = sdata.asym = sbss.asym = NULL;
142 syms = saa_init((int32_t)sizeof(struct Symbol));
143 nsyms = 0;
144 bsym = raa_init();
145 strs = saa_init(1L);
146 strslen = 0;
147 fwds = NULL;
150 #ifdef OF_AOUT
152 static void aout_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
154 bsd = FALSE;
155 aoutg_init(fp, errfunc, ldef, eval);
157 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
158 aout_plt_sect = aout_sym_sect = NO_SEG;
161 #endif
163 #ifdef OF_AOUTB
165 extern struct ofmt of_aoutb;
167 static void aoutb_init(FILE * fp, efunc errfunc, ldfunc ldef,
168 evalfunc eval)
170 bsd = TRUE;
171 aoutg_init(fp, errfunc, ldef, eval);
173 is_pic = 0x00; /* may become 0x40 */
175 aout_gotpc_sect = seg_alloc();
176 ldef("..gotpc", aout_gotpc_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
177 error);
178 aout_gotoff_sect = seg_alloc();
179 ldef("..gotoff", aout_gotoff_sect + 1, 0L, NULL, FALSE, FALSE,
180 &of_aoutb, error);
181 aout_got_sect = seg_alloc();
182 ldef("..got", aout_got_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
183 error);
184 aout_plt_sect = seg_alloc();
185 ldef("..plt", aout_plt_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
186 error);
187 aout_sym_sect = seg_alloc();
188 ldef("..sym", aout_sym_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
189 error);
192 #endif
194 static void aout_cleanup(int debuginfo)
196 struct Reloc *r;
198 (void)debuginfo;
200 aout_pad_sections();
201 aout_fixup_relocs(&stext);
202 aout_fixup_relocs(&sdata);
203 aout_write();
204 fclose(aoutfp);
205 saa_free(stext.data);
206 while (stext.head) {
207 r = stext.head;
208 stext.head = stext.head->next;
209 nasm_free(r);
211 saa_free(sdata.data);
212 while (sdata.head) {
213 r = sdata.head;
214 sdata.head = sdata.head->next;
215 nasm_free(r);
217 saa_free(syms);
218 raa_free(bsym);
219 saa_free(strs);
222 static int32_t aout_section_names(char *name, int pass, int *bits)
225 (void)pass;
228 * Default to 32 bits.
230 if (!name)
231 *bits = 32;
233 if (!name)
234 return stext.index;
236 if (!strcmp(name, ".text"))
237 return stext.index;
238 else if (!strcmp(name, ".data"))
239 return sdata.index;
240 else if (!strcmp(name, ".bss"))
241 return sbss.index;
242 else
243 return NO_SEG;
246 static void aout_deflabel(char *name, int32_t segment, int32_t offset,
247 int is_global, char *special)
249 int pos = strslen + 4;
250 struct Symbol *sym;
251 int special_used = FALSE;
253 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
255 * This is a NASM special symbol. We never allow it into
256 * the a.out symbol table, even if it's a valid one. If it
257 * _isn't_ a valid one, we should barf immediately.
259 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
260 strcmp(name, "..got") && strcmp(name, "..plt") &&
261 strcmp(name, "..sym"))
262 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
263 return;
266 if (is_global == 3) {
267 struct Symbol **s;
269 * Fix up a forward-reference symbol size from the first
270 * pass.
272 for (s = &fwds; *s; s = &(*s)->nextfwd)
273 if (!strcmp((*s)->name, name)) {
274 struct tokenval tokval;
275 expr *e;
276 char *p = special;
278 while (*p && !isspace(*p))
279 p++;
280 while (*p && isspace(*p))
281 p++;
282 stdscan_reset();
283 stdscan_bufptr = p;
284 tokval.t_type = TOKEN_INVALID;
285 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
286 if (e) {
287 if (!is_simple(e))
288 error(ERR_NONFATAL, "cannot use relocatable"
289 " expression as symbol size");
290 else
291 (*s)->size = reloc_value(e);
295 * Remove it from the list of unresolved sizes.
297 nasm_free((*s)->name);
298 *s = (*s)->nextfwd;
299 return;
301 return; /* it wasn't an important one */
304 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
305 strslen += 1 + strlen(name);
307 sym = saa_wstruct(syms);
309 sym->strpos = pos;
310 sym->type = is_global ? SYM_GLOBAL : 0;
311 sym->segment = segment;
312 if (segment == NO_SEG)
313 sym->type |= SECT_ABS;
314 else if (segment == stext.index) {
315 sym->type |= SECT_TEXT;
316 if (is_global) {
317 sym->next = stext.gsyms;
318 stext.gsyms = sym;
319 } else if (!stext.asym)
320 stext.asym = sym;
321 } else if (segment == sdata.index) {
322 sym->type |= SECT_DATA;
323 if (is_global) {
324 sym->next = sdata.gsyms;
325 sdata.gsyms = sym;
326 } else if (!sdata.asym)
327 sdata.asym = sym;
328 } else if (segment == sbss.index) {
329 sym->type |= SECT_BSS;
330 if (is_global) {
331 sym->next = sbss.gsyms;
332 sbss.gsyms = sym;
333 } else if (!sbss.asym)
334 sbss.asym = sym;
335 } else
336 sym->type = SYM_GLOBAL;
337 if (is_global == 2)
338 sym->value = offset;
339 else
340 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
342 if (is_global && sym->type != SYM_GLOBAL) {
344 * Global symbol exported _from_ this module. We must check
345 * the special text for type information.
348 if (special) {
349 int n = strcspn(special, " ");
351 if (!nasm_strnicmp(special, "function", n))
352 sym->type |= SYM_FUNCTION;
353 else if (!nasm_strnicmp(special, "data", n) ||
354 !nasm_strnicmp(special, "object", n))
355 sym->type |= SYM_DATA;
356 else
357 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
358 n, special);
359 if (special[n]) {
360 struct tokenval tokval;
361 expr *e;
362 int fwd = FALSE;
363 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
365 if (!bsd) {
366 error(ERR_NONFATAL, "Linux a.out does not support"
367 " symbol size information");
368 } else {
369 while (special[n] && isspace(special[n]))
370 n++;
372 * We have a size expression; attempt to
373 * evaluate it.
375 sym->type |= SYM_WITH_SIZE;
376 stdscan_reset();
377 stdscan_bufptr = special + n;
378 tokval.t_type = TOKEN_INVALID;
379 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
380 NULL);
381 if (fwd) {
382 sym->nextfwd = fwds;
383 fwds = sym;
384 sym->name = nasm_strdup(name);
385 } else if (e) {
386 if (!is_simple(e))
387 error(ERR_NONFATAL, "cannot use relocatable"
388 " expression as symbol size");
389 else
390 sym->size = reloc_value(e);
393 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
395 special_used = TRUE;
400 * define the references from external-symbol segment numbers
401 * to these symbol records.
403 if (segment != NO_SEG && segment != stext.index &&
404 segment != sdata.index && segment != sbss.index)
405 bsym = raa_write(bsym, segment, nsyms);
406 sym->symnum = nsyms;
408 nsyms++;
409 if (sym->type & SYM_WITH_SIZE)
410 nsyms++; /* and another for the size */
412 if (special && !special_used)
413 error(ERR_NONFATAL, "no special symbol features supported here");
416 static void aout_add_reloc(struct Section *sect, int32_t segment,
417 int reltype, int bytes)
419 struct Reloc *r;
421 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
422 sect->tail = &r->next;
423 r->next = NULL;
425 r->address = sect->len;
426 r->symbol = (segment == NO_SEG ? -SECT_ABS :
427 segment == stext.index ? -SECT_TEXT :
428 segment == sdata.index ? -SECT_DATA :
429 segment == sbss.index ? -SECT_BSS :
430 raa_read(bsym, segment));
431 r->reltype = reltype;
432 if (r->symbol >= 0)
433 r->reltype |= RELTYPE_SYMFLAG;
434 r->bytes = bytes;
436 sect->nrelocs++;
440 * This routine deals with ..got and ..sym relocations: the more
441 * complicated kinds. In shared-library writing, some relocations
442 * with respect to global symbols must refer to the precise symbol
443 * rather than referring to an offset from the base of the section
444 * _containing_ the symbol. Such relocations call to this routine,
445 * which searches the symbol list for the symbol in question.
447 * RELTYPE_GOT references require the _exact_ symbol address to be
448 * used; RELTYPE_ABSOLUTE references can be at an offset from the
449 * symbol. The boolean argument `exact' tells us this.
451 * Return value is the adjusted value of `addr', having become an
452 * offset from the symbol rather than the section. Should always be
453 * zero when returning from an exact call.
455 * Limitation: if you define two symbols at the same place,
456 * confusion will occur.
458 * Inefficiency: we search, currently, using a linked list which
459 * isn't even necessarily sorted.
461 static int32_t aout_add_gsym_reloc(struct Section *sect,
462 int32_t segment, int32_t offset,
463 int type, int bytes, int exact)
465 struct Symbol *sym, *sm, *shead;
466 struct Reloc *r;
469 * First look up the segment to find whether it's text, data,
470 * bss or an external symbol.
472 shead = NULL;
473 if (segment == stext.index)
474 shead = stext.gsyms;
475 else if (segment == sdata.index)
476 shead = sdata.gsyms;
477 else if (segment == sbss.index)
478 shead = sbss.gsyms;
479 if (!shead) {
480 if (exact && offset != 0)
481 error(ERR_NONFATAL, "unable to find a suitable global symbol"
482 " for this reference");
483 else
484 aout_add_reloc(sect, segment, type, bytes);
485 return offset;
488 if (exact) {
490 * Find a symbol pointing _exactly_ at this one.
492 for (sym = shead; sym; sym = sym->next)
493 if (sym->value == offset)
494 break;
495 } else {
497 * Find the nearest symbol below this one.
499 sym = NULL;
500 for (sm = shead; sm; sm = sm->next)
501 if (sm->value <= offset && (!sym || sm->value > sym->value))
502 sym = sm;
504 if (!sym && exact) {
505 error(ERR_NONFATAL, "unable to find a suitable global symbol"
506 " for this reference");
507 return 0;
510 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
511 sect->tail = &r->next;
512 r->next = NULL;
514 r->address = sect->len;
515 r->symbol = sym->symnum;
516 r->reltype = type | RELTYPE_SYMFLAG;
517 r->bytes = bytes;
519 sect->nrelocs++;
521 return offset - sym->value;
525 * This routine deals with ..gotoff relocations. These _must_ refer
526 * to a symbol, due to a perversity of *BSD's PIC implementation,
527 * and it must be a non-global one as well; so we store `asym', the
528 * first nonglobal symbol defined in each section, and always work
529 * from that. Relocation type is always RELTYPE_GOTOFF.
531 * Return value is the adjusted value of `addr', having become an
532 * offset from the `asym' symbol rather than the section.
534 static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
535 int32_t offset, int bytes)
537 struct Reloc *r;
538 struct Symbol *asym;
541 * First look up the segment to find whether it's text, data,
542 * bss or an external symbol.
544 asym = NULL;
545 if (segment == stext.index)
546 asym = stext.asym;
547 else if (segment == sdata.index)
548 asym = sdata.asym;
549 else if (segment == sbss.index)
550 asym = sbss.asym;
551 if (!asym)
552 error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
553 " symbol in the section");
555 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
556 sect->tail = &r->next;
557 r->next = NULL;
559 r->address = sect->len;
560 r->symbol = asym->symnum;
561 r->reltype = RELTYPE_GOTOFF;
562 r->bytes = bytes;
564 sect->nrelocs++;
566 return offset - asym->value;
569 static void aout_out(int32_t segto, const void *data, uint32_t type,
570 int32_t segment, int32_t wrt)
572 struct Section *s;
573 int32_t realbytes = type & OUT_SIZMASK;
574 int32_t addr;
575 uint8_t mydata[4], *p;
577 type &= OUT_TYPMASK;
580 * handle absolute-assembly (structure definitions)
582 if (segto == NO_SEG) {
583 if (type != OUT_RESERVE)
584 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
585 " space");
586 return;
589 if (segto == stext.index)
590 s = &stext;
591 else if (segto == sdata.index)
592 s = &sdata;
593 else if (segto == sbss.index)
594 s = NULL;
595 else {
596 error(ERR_WARNING, "attempt to assemble code in"
597 " segment %d: defaulting to `.text'", segto);
598 s = &stext;
601 if (!s && type != OUT_RESERVE) {
602 error(ERR_WARNING, "attempt to initialize memory in the"
603 " BSS section: ignored");
604 if (type == OUT_REL2ADR)
605 realbytes = 2;
606 else if (type == OUT_REL4ADR)
607 realbytes = 4;
608 sbss.len += realbytes;
609 return;
612 if (type == OUT_RESERVE) {
613 if (s) {
614 error(ERR_WARNING, "uninitialized space declared in"
615 " %s section: zeroing",
616 (segto == stext.index ? "code" : "data"));
617 aout_sect_write(s, NULL, realbytes);
618 } else
619 sbss.len += realbytes;
620 } else if (type == OUT_RAWDATA) {
621 if (segment != NO_SEG)
622 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
623 aout_sect_write(s, data, realbytes);
624 } else if (type == OUT_ADDRESS) {
625 addr = *(int32_t *)data;
626 if (segment != NO_SEG) {
627 if (segment % 2) {
628 error(ERR_NONFATAL, "a.out format does not support"
629 " segment base references");
630 } else {
631 if (wrt == NO_SEG) {
632 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE,
633 realbytes);
634 } else if (!bsd) {
635 error(ERR_NONFATAL,
636 "Linux a.out format does not support"
637 " any use of WRT");
638 wrt = NO_SEG; /* we can at least _try_ to continue */
639 } else if (wrt == aout_gotpc_sect + 1) {
640 is_pic = 0x40;
641 aout_add_reloc(s, segment, RELTYPE_GOTPC, realbytes);
642 } else if (wrt == aout_gotoff_sect + 1) {
643 is_pic = 0x40;
644 addr = aout_add_gotoff_reloc(s, segment,
645 addr, realbytes);
646 } else if (wrt == aout_got_sect + 1) {
647 is_pic = 0x40;
648 addr =
649 aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
650 realbytes, TRUE);
651 } else if (wrt == aout_sym_sect + 1) {
652 addr = aout_add_gsym_reloc(s, segment, addr,
653 RELTYPE_ABSOLUTE, realbytes,
654 FALSE);
655 } else if (wrt == aout_plt_sect + 1) {
656 is_pic = 0x40;
657 error(ERR_NONFATAL,
658 "a.out format cannot produce non-PC-"
659 "relative PLT references");
660 } else {
661 error(ERR_NONFATAL,
662 "a.out format does not support this"
663 " use of WRT");
664 wrt = NO_SEG; /* we can at least _try_ to continue */
668 p = mydata;
669 if (realbytes == 2)
670 WRITESHORT(p, addr);
671 else
672 WRITELONG(p, addr);
673 aout_sect_write(s, mydata, realbytes);
674 } else if (type == OUT_REL2ADR) {
675 if (segment == segto)
676 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
677 if (segment != NO_SEG && segment % 2) {
678 error(ERR_NONFATAL, "a.out format does not support"
679 " segment base references");
680 } else {
681 if (wrt == NO_SEG) {
682 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
683 } else if (!bsd) {
684 error(ERR_NONFATAL, "Linux a.out format does not support"
685 " any use of WRT");
686 wrt = NO_SEG; /* we can at least _try_ to continue */
687 } else if (wrt == aout_plt_sect + 1) {
688 is_pic = 0x40;
689 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
690 } else if (wrt == aout_gotpc_sect + 1 ||
691 wrt == aout_gotoff_sect + 1 ||
692 wrt == aout_got_sect + 1) {
693 error(ERR_NONFATAL, "a.out format cannot produce PC-"
694 "relative GOT references");
695 } else {
696 error(ERR_NONFATAL, "a.out format does not support this"
697 " use of WRT");
698 wrt = NO_SEG; /* we can at least _try_ to continue */
701 p = mydata;
702 WRITESHORT(p, *(int32_t *)data - (realbytes + s->len));
703 aout_sect_write(s, mydata, 2L);
704 } else if (type == OUT_REL4ADR) {
705 if (segment == segto)
706 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
707 if (segment != NO_SEG && segment % 2) {
708 error(ERR_NONFATAL, "a.out format does not support"
709 " segment base references");
710 } else {
711 if (wrt == NO_SEG) {
712 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
713 } else if (!bsd) {
714 error(ERR_NONFATAL, "Linux a.out format does not support"
715 " any use of WRT");
716 wrt = NO_SEG; /* we can at least _try_ to continue */
717 } else if (wrt == aout_plt_sect + 1) {
718 is_pic = 0x40;
719 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
720 } else if (wrt == aout_gotpc_sect + 1 ||
721 wrt == aout_gotoff_sect + 1 ||
722 wrt == aout_got_sect + 1) {
723 error(ERR_NONFATAL, "a.out format cannot produce PC-"
724 "relative GOT references");
725 } else {
726 error(ERR_NONFATAL, "a.out format does not support this"
727 " use of WRT");
728 wrt = NO_SEG; /* we can at least _try_ to continue */
731 p = mydata;
732 WRITELONG(p, *(int32_t *)data - (realbytes + s->len));
733 aout_sect_write(s, mydata, 4L);
737 static void aout_pad_sections(void)
739 static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
741 * Pad each of the text and data sections with NOPs until their
742 * length is a multiple of four. (NOP == 0x90.) Also increase
743 * the length of the BSS section similarly.
745 aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
746 aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
747 sbss.len = (sbss.len + 3) & ~3;
751 * a.out files have the curious property that all references to
752 * things in the data or bss sections are done by addresses which
753 * are actually relative to the start of the _text_ section, in the
754 * _file_. (No relation to what happens after linking. No idea why
755 * this should be so. It's very strange.) So we have to go through
756 * the relocation table, _after_ the final size of each section is
757 * known, and fix up the relocations pointed to.
759 static void aout_fixup_relocs(struct Section *sect)
761 struct Reloc *r;
763 saa_rewind(sect->data);
764 for (r = sect->head; r; r = r->next) {
765 uint8_t *p, *q, blk[4];
766 int32_t l;
768 saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
769 p = q = blk;
770 l = *p++;
771 if (r->bytes > 1) {
772 l += ((int32_t)*p++) << 8;
773 if (r->bytes == 4) {
774 l += ((int32_t)*p++) << 16;
775 l += ((int32_t)*p++) << 24;
778 if (r->symbol == -SECT_DATA)
779 l += stext.len;
780 else if (r->symbol == -SECT_BSS)
781 l += stext.len + sdata.len;
782 if (r->bytes == 4)
783 WRITELONG(q, l);
784 else if (r->bytes == 2)
785 WRITESHORT(q, l);
786 else
787 *q++ = l & 0xFF;
788 saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
792 static void aout_write(void)
795 * Emit the a.out header.
797 /* OMAGIC, M_386 or MID_I386, no flags */
798 fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
799 fwriteint32_t(stext.len, aoutfp);
800 fwriteint32_t(sdata.len, aoutfp);
801 fwriteint32_t(sbss.len, aoutfp);
802 fwriteint32_t(nsyms * 12, aoutfp); /* length of symbol table */
803 fwriteint32_t(0L, aoutfp); /* object files have no entry point */
804 fwriteint32_t(stext.nrelocs * 8, aoutfp); /* size of text relocs */
805 fwriteint32_t(sdata.nrelocs * 8, aoutfp); /* size of data relocs */
808 * Write out the code section and the data section.
810 saa_fpwrite(stext.data, aoutfp);
811 saa_fpwrite(sdata.data, aoutfp);
814 * Write out the relocations.
816 aout_write_relocs(stext.head);
817 aout_write_relocs(sdata.head);
820 * Write the symbol table.
822 aout_write_syms();
825 * And the string table.
827 fwriteint32_t(strslen + 4, aoutfp); /* length includes length count */
828 saa_fpwrite(strs, aoutfp);
831 static void aout_write_relocs(struct Reloc *r)
833 while (r) {
834 uint32_t word2;
836 fwriteint32_t(r->address, aoutfp);
838 if (r->symbol >= 0)
839 word2 = r->symbol;
840 else
841 word2 = -r->symbol;
842 word2 |= r->reltype << 24;
843 word2 |= (r->bytes == 1 ? 0 :
844 r->bytes == 2 ? 0x2000000L : 0x4000000L);
845 fwriteint32_t(word2, aoutfp);
847 r = r->next;
851 static void aout_write_syms(void)
853 uint32_t i;
855 saa_rewind(syms);
856 for (i = 0; i < nsyms; i++) {
857 struct Symbol *sym = saa_rstruct(syms);
858 fwriteint32_t(sym->strpos, aoutfp);
859 fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, aoutfp);
861 * Fix up the symbol value now we know the final section
862 * sizes.
864 if ((sym->type & SECT_MASK) == SECT_DATA)
865 sym->value += stext.len;
866 if ((sym->type & SECT_MASK) == SECT_BSS)
867 sym->value += stext.len + sdata.len;
868 fwriteint32_t(sym->value, aoutfp);
870 * Output a size record if necessary.
872 if (sym->type & SYM_WITH_SIZE) {
873 fwriteint32_t(sym->strpos, aoutfp);
874 fwriteint32_t(0x0DL, aoutfp); /* special value: means size */
875 fwriteint32_t(sym->size, aoutfp);
876 i++; /* use up another of `nsyms' */
881 static void aout_sect_write(struct Section *sect,
882 const uint8_t *data, uint32_t len)
884 saa_wbytes(sect->data, data, len);
885 sect->len += len;
888 static int32_t aout_segbase(int32_t segment)
890 return segment;
893 static int aout_directive(char *directive, char *value, int pass)
895 (void)directive;
896 (void)value;
897 (void)pass;
898 return 0;
901 static void aout_filename(char *inname, char *outname, efunc error)
903 standard_extension(inname, outname, ".o", error);
906 static const char *aout_stdmac[] = {
907 "%define __SECT__ [section .text]",
908 "%macro __NASM_CDecl__ 1",
909 "%endmacro",
910 NULL
913 static int aout_set_info(enum geninfo type, char **val)
915 (void)type;
916 (void)val;
917 return 0;
919 #endif /* OF_AOUT || OF_AOUTB */
921 #ifdef OF_AOUT
923 struct ofmt of_aout = {
924 "Linux a.out object files",
925 "aout",
926 NULL,
927 null_debug_arr,
928 &null_debug_form,
929 aout_stdmac,
930 aout_init,
931 aout_set_info,
932 aout_out,
933 aout_deflabel,
934 aout_section_names,
935 aout_segbase,
936 aout_directive,
937 aout_filename,
938 aout_cleanup
941 #endif
943 #ifdef OF_AOUTB
945 struct ofmt of_aoutb = {
946 "NetBSD/FreeBSD a.out object files",
947 "aoutb",
948 NULL,
949 null_debug_arr,
950 &null_debug_form,
951 aout_stdmac,
952 aoutb_init,
953 aout_set_info,
954 aout_out,
955 aout_deflabel,
956 aout_section_names,
957 aout_segbase,
958 aout_directive,
959 aout_filename,
960 aout_cleanup
963 #endif