Added Dev-Cpp Makefile
[nasm/sigaren-mirror.git] / output / outaout.c
blobe25076750802140687bf8986517008a132975c08
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 "outform.h"
20 #if defined OF_AOUT || defined OF_AOUTB
22 #define RELTYPE_ABSOLUTE 0x00
23 #define RELTYPE_RELATIVE 0x01
24 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
25 #define RELTYPE_GOTOFF 0x10
26 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
27 #define RELTYPE_PLT 0x21
28 #define RELTYPE_SYMFLAG 0x08
30 struct Reloc {
31 struct Reloc *next;
32 int32_t address; /* relative to _start_ of section */
33 int32_t symbol; /* symbol number or -ve section id */
34 int bytes; /* 2 or 4 */
35 int reltype; /* see above */
38 struct Symbol {
39 int32_t strpos; /* string table position of name */
40 int type; /* symbol type - see flags below */
41 int32_t value; /* address, or COMMON variable size */
42 int32_t size; /* size for data or function exports */
43 int32_t segment; /* back-reference used by gsym_reloc */
44 struct Symbol *next; /* list of globals in each section */
45 struct Symbol *nextfwd; /* list of unresolved-size symbols */
46 int8_t *name; /* for unresolved-size symbols */
47 int32_t symnum; /* index into symbol table */
51 * Section IDs - used in Reloc.symbol when negative, and in
52 * Symbol.type when positive.
54 #define SECT_ABS 2 /* absolute value */
55 #define SECT_TEXT 4 /* text section */
56 #define SECT_DATA 6 /* data section */
57 #define SECT_BSS 8 /* bss section */
58 #define SECT_MASK 0xE /* mask out any of the above */
61 * More flags used in Symbol.type.
63 #define SYM_GLOBAL 1 /* it's a global symbol */
64 #define SYM_DATA 0x100 /* used for shared libs */
65 #define SYM_FUNCTION 0x200 /* used for shared libs */
66 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
69 * Bit more explanation of symbol types: SECT_xxx denotes a local
70 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
71 * this module. Just SYM_GLOBAL, with zero value, denotes an
72 * external symbol referenced in this module. And just SYM_GLOBAL,
73 * but with a non-zero value, declares a C `common' variable, of
74 * size `value'.
77 struct Section {
78 struct SAA *data;
79 uint32_t len, size, nrelocs;
80 int32_t index;
81 struct Reloc *head, **tail;
82 struct Symbol *gsyms, *asym;
85 static struct Section stext, sdata, sbss;
87 static struct SAA *syms;
88 static uint32_t nsyms;
90 static struct RAA *bsym;
92 static struct SAA *strs;
93 static uint32_t strslen;
95 static struct Symbol *fwds;
97 static FILE *aoutfp;
98 static efunc error;
99 static evalfunc evaluate;
101 static int bsd;
102 static int is_pic;
104 static void aout_write(void);
105 static void aout_write_relocs(struct Reloc *);
106 static void aout_write_syms(void);
107 static void aout_sect_write(struct Section *, const uint8_t *,
108 uint32_t);
109 static void aout_pad_sections(void);
110 static void aout_fixup_relocs(struct Section *);
113 * Special section numbers which are used to define special
114 * symbols, which can be used with WRT to provide PIC relocation
115 * types.
117 static int32_t aout_gotpc_sect, aout_gotoff_sect;
118 static int32_t aout_got_sect, aout_plt_sect;
119 static int32_t aout_sym_sect;
121 static void aoutg_init(FILE * fp, efunc errfunc, ldfunc ldef,
122 evalfunc eval)
124 aoutfp = fp;
125 error = errfunc;
126 evaluate = eval;
127 (void)ldef; /* placate optimisers */
128 stext.data = saa_init(1L);
129 stext.head = NULL;
130 stext.tail = &stext.head;
131 sdata.data = saa_init(1L);
132 sdata.head = NULL;
133 sdata.tail = &sdata.head;
134 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
135 stext.nrelocs = sdata.nrelocs = 0;
136 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
137 stext.index = seg_alloc();
138 sdata.index = seg_alloc();
139 sbss.index = seg_alloc();
140 stext.asym = sdata.asym = sbss.asym = NULL;
141 syms = saa_init((int32_t)sizeof(struct Symbol));
142 nsyms = 0;
143 bsym = raa_init();
144 strs = saa_init(1L);
145 strslen = 0;
146 fwds = NULL;
149 #ifdef OF_AOUT
151 static void aout_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
153 bsd = FALSE;
154 aoutg_init(fp, errfunc, ldef, eval);
156 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
157 aout_plt_sect = aout_sym_sect = NO_SEG;
160 #endif
162 #ifdef OF_AOUTB
164 extern struct ofmt of_aoutb;
166 static void aoutb_init(FILE * fp, efunc errfunc, ldfunc ldef,
167 evalfunc eval)
169 bsd = TRUE;
170 aoutg_init(fp, errfunc, ldef, eval);
172 is_pic = 0x00; /* may become 0x40 */
174 aout_gotpc_sect = seg_alloc();
175 ldef("..gotpc", aout_gotpc_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
176 error);
177 aout_gotoff_sect = seg_alloc();
178 ldef("..gotoff", aout_gotoff_sect + 1, 0L, NULL, FALSE, FALSE,
179 &of_aoutb, error);
180 aout_got_sect = seg_alloc();
181 ldef("..got", aout_got_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
182 error);
183 aout_plt_sect = seg_alloc();
184 ldef("..plt", aout_plt_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
185 error);
186 aout_sym_sect = seg_alloc();
187 ldef("..sym", aout_sym_sect + 1, 0L, NULL, FALSE, FALSE, &of_aoutb,
188 error);
191 #endif
193 static void aout_cleanup(int debuginfo)
195 struct Reloc *r;
197 (void)debuginfo;
199 aout_pad_sections();
200 aout_fixup_relocs(&stext);
201 aout_fixup_relocs(&sdata);
202 aout_write();
203 fclose(aoutfp);
204 saa_free(stext.data);
205 while (stext.head) {
206 r = stext.head;
207 stext.head = stext.head->next;
208 nasm_free(r);
210 saa_free(sdata.data);
211 while (sdata.head) {
212 r = sdata.head;
213 sdata.head = sdata.head->next;
214 nasm_free(r);
216 saa_free(syms);
217 raa_free(bsym);
218 saa_free(strs);
221 static int32_t aout_section_names(int8_t *name, int pass, int *bits)
224 * Default to 32 bits.
226 if (!name)
227 *bits = 32;
229 if (!name)
230 return stext.index;
232 if (!strcmp(name, ".text"))
233 return stext.index;
234 else if (!strcmp(name, ".data"))
235 return sdata.index;
236 else if (!strcmp(name, ".bss"))
237 return sbss.index;
238 else
239 return NO_SEG;
242 static void aout_deflabel(int8_t *name, int32_t segment, int32_t offset,
243 int is_global, int8_t *special)
245 int pos = strslen + 4;
246 struct Symbol *sym;
247 int special_used = FALSE;
249 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
251 * This is a NASM special symbol. We never allow it into
252 * the a.out symbol table, even if it's a valid one. If it
253 * _isn't_ a valid one, we should barf immediately.
255 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
256 strcmp(name, "..got") && strcmp(name, "..plt") &&
257 strcmp(name, "..sym"))
258 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
259 return;
262 if (is_global == 3) {
263 struct Symbol **s;
265 * Fix up a forward-reference symbol size from the first
266 * pass.
268 for (s = &fwds; *s; s = &(*s)->nextfwd)
269 if (!strcmp((*s)->name, name)) {
270 struct tokenval tokval;
271 expr *e;
272 int8_t *p = special;
274 while (*p && !isspace(*p))
275 p++;
276 while (*p && isspace(*p))
277 p++;
278 stdscan_reset();
279 stdscan_bufptr = p;
280 tokval.t_type = TOKEN_INVALID;
281 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
282 if (e) {
283 if (!is_simple(e))
284 error(ERR_NONFATAL, "cannot use relocatable"
285 " expression as symbol size");
286 else
287 (*s)->size = reloc_value(e);
291 * Remove it from the list of unresolved sizes.
293 nasm_free((*s)->name);
294 *s = (*s)->nextfwd;
295 return;
297 return; /* it wasn't an important one */
300 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
301 strslen += 1 + strlen(name);
303 sym = saa_wstruct(syms);
305 sym->strpos = pos;
306 sym->type = is_global ? SYM_GLOBAL : 0;
307 sym->segment = segment;
308 if (segment == NO_SEG)
309 sym->type |= SECT_ABS;
310 else if (segment == stext.index) {
311 sym->type |= SECT_TEXT;
312 if (is_global) {
313 sym->next = stext.gsyms;
314 stext.gsyms = sym;
315 } else if (!stext.asym)
316 stext.asym = sym;
317 } else if (segment == sdata.index) {
318 sym->type |= SECT_DATA;
319 if (is_global) {
320 sym->next = sdata.gsyms;
321 sdata.gsyms = sym;
322 } else if (!sdata.asym)
323 sdata.asym = sym;
324 } else if (segment == sbss.index) {
325 sym->type |= SECT_BSS;
326 if (is_global) {
327 sym->next = sbss.gsyms;
328 sbss.gsyms = sym;
329 } else if (!sbss.asym)
330 sbss.asym = sym;
331 } else
332 sym->type = SYM_GLOBAL;
333 if (is_global == 2)
334 sym->value = offset;
335 else
336 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
338 if (is_global && sym->type != SYM_GLOBAL) {
340 * Global symbol exported _from_ this module. We must check
341 * the special text for type information.
344 if (special) {
345 int n = strcspn(special, " ");
347 if (!nasm_strnicmp(special, "function", n))
348 sym->type |= SYM_FUNCTION;
349 else if (!nasm_strnicmp(special, "data", n) ||
350 !nasm_strnicmp(special, "object", n))
351 sym->type |= SYM_DATA;
352 else
353 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
354 n, special);
355 if (special[n]) {
356 struct tokenval tokval;
357 expr *e;
358 int fwd = FALSE;
359 int8_t *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
361 if (!bsd) {
362 error(ERR_NONFATAL, "Linux a.out does not support"
363 " symbol size information");
364 } else {
365 while (special[n] && isspace(special[n]))
366 n++;
368 * We have a size expression; attempt to
369 * evaluate it.
371 sym->type |= SYM_WITH_SIZE;
372 stdscan_reset();
373 stdscan_bufptr = special + n;
374 tokval.t_type = TOKEN_INVALID;
375 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
376 NULL);
377 if (fwd) {
378 sym->nextfwd = fwds;
379 fwds = sym;
380 sym->name = nasm_strdup(name);
381 } else if (e) {
382 if (!is_simple(e))
383 error(ERR_NONFATAL, "cannot use relocatable"
384 " expression as symbol size");
385 else
386 sym->size = reloc_value(e);
389 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
391 special_used = TRUE;
396 * define the references from external-symbol segment numbers
397 * to these symbol records.
399 if (segment != NO_SEG && segment != stext.index &&
400 segment != sdata.index && segment != sbss.index)
401 bsym = raa_write(bsym, segment, nsyms);
402 sym->symnum = nsyms;
404 nsyms++;
405 if (sym->type & SYM_WITH_SIZE)
406 nsyms++; /* and another for the size */
408 if (special && !special_used)
409 error(ERR_NONFATAL, "no special symbol features supported here");
412 static void aout_add_reloc(struct Section *sect, int32_t segment,
413 int reltype, int bytes)
415 struct Reloc *r;
417 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
418 sect->tail = &r->next;
419 r->next = NULL;
421 r->address = sect->len;
422 r->symbol = (segment == NO_SEG ? -SECT_ABS :
423 segment == stext.index ? -SECT_TEXT :
424 segment == sdata.index ? -SECT_DATA :
425 segment == sbss.index ? -SECT_BSS :
426 raa_read(bsym, segment));
427 r->reltype = reltype;
428 if (r->symbol >= 0)
429 r->reltype |= RELTYPE_SYMFLAG;
430 r->bytes = bytes;
432 sect->nrelocs++;
436 * This routine deals with ..got and ..sym relocations: the more
437 * complicated kinds. In shared-library writing, some relocations
438 * with respect to global symbols must refer to the precise symbol
439 * rather than referring to an offset from the base of the section
440 * _containing_ the symbol. Such relocations call to this routine,
441 * which searches the symbol list for the symbol in question.
443 * RELTYPE_GOT references require the _exact_ symbol address to be
444 * used; RELTYPE_ABSOLUTE references can be at an offset from the
445 * symbol. The boolean argument `exact' tells us this.
447 * Return value is the adjusted value of `addr', having become an
448 * offset from the symbol rather than the section. Should always be
449 * zero when returning from an exact call.
451 * Limitation: if you define two symbols at the same place,
452 * confusion will occur.
454 * Inefficiency: we search, currently, using a linked list which
455 * isn't even necessarily sorted.
457 static int32_t aout_add_gsym_reloc(struct Section *sect,
458 int32_t segment, int32_t offset,
459 int type, int bytes, int exact)
461 struct Symbol *sym, *sm, *shead;
462 struct Reloc *r;
465 * First look up the segment to find whether it's text, data,
466 * bss or an external symbol.
468 shead = NULL;
469 if (segment == stext.index)
470 shead = stext.gsyms;
471 else if (segment == sdata.index)
472 shead = sdata.gsyms;
473 else if (segment == sbss.index)
474 shead = sbss.gsyms;
475 if (!shead) {
476 if (exact && offset != 0)
477 error(ERR_NONFATAL, "unable to find a suitable global symbol"
478 " for this reference");
479 else
480 aout_add_reloc(sect, segment, type, bytes);
481 return offset;
484 if (exact) {
486 * Find a symbol pointing _exactly_ at this one.
488 for (sym = shead; sym; sym = sym->next)
489 if (sym->value == offset)
490 break;
491 } else {
493 * Find the nearest symbol below this one.
495 sym = NULL;
496 for (sm = shead; sm; sm = sm->next)
497 if (sm->value <= offset && (!sym || sm->value > sym->value))
498 sym = sm;
500 if (!sym && exact) {
501 error(ERR_NONFATAL, "unable to find a suitable global symbol"
502 " for this reference");
503 return 0;
506 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
507 sect->tail = &r->next;
508 r->next = NULL;
510 r->address = sect->len;
511 r->symbol = sym->symnum;
512 r->reltype = type | RELTYPE_SYMFLAG;
513 r->bytes = bytes;
515 sect->nrelocs++;
517 return offset - sym->value;
521 * This routine deals with ..gotoff relocations. These _must_ refer
522 * to a symbol, due to a perversity of *BSD's PIC implementation,
523 * and it must be a non-global one as well; so we store `asym', the
524 * first nonglobal symbol defined in each section, and always work
525 * from that. Relocation type is always RELTYPE_GOTOFF.
527 * Return value is the adjusted value of `addr', having become an
528 * offset from the `asym' symbol rather than the section.
530 static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
531 int32_t offset, int bytes)
533 struct Reloc *r;
534 struct Symbol *asym;
537 * First look up the segment to find whether it's text, data,
538 * bss or an external symbol.
540 asym = NULL;
541 if (segment == stext.index)
542 asym = stext.asym;
543 else if (segment == sdata.index)
544 asym = sdata.asym;
545 else if (segment == sbss.index)
546 asym = sbss.asym;
547 if (!asym)
548 error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
549 " symbol in the section");
551 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
552 sect->tail = &r->next;
553 r->next = NULL;
555 r->address = sect->len;
556 r->symbol = asym->symnum;
557 r->reltype = RELTYPE_GOTOFF;
558 r->bytes = bytes;
560 sect->nrelocs++;
562 return offset - asym->value;
565 static void aout_out(int32_t segto, const void *data, uint32_t type,
566 int32_t segment, int32_t wrt)
568 struct Section *s;
569 int32_t realbytes = type & OUT_SIZMASK;
570 int32_t addr;
571 uint8_t mydata[4], *p;
573 type &= OUT_TYPMASK;
576 * handle absolute-assembly (structure definitions)
578 if (segto == NO_SEG) {
579 if (type != OUT_RESERVE)
580 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
581 " space");
582 return;
585 if (segto == stext.index)
586 s = &stext;
587 else if (segto == sdata.index)
588 s = &sdata;
589 else if (segto == sbss.index)
590 s = NULL;
591 else {
592 error(ERR_WARNING, "attempt to assemble code in"
593 " segment %d: defaulting to `.text'", segto);
594 s = &stext;
597 if (!s && type != OUT_RESERVE) {
598 error(ERR_WARNING, "attempt to initialize memory in the"
599 " BSS section: ignored");
600 if (type == OUT_REL2ADR)
601 realbytes = 2;
602 else if (type == OUT_REL4ADR)
603 realbytes = 4;
604 sbss.len += realbytes;
605 return;
608 if (type == OUT_RESERVE) {
609 if (s) {
610 error(ERR_WARNING, "uninitialized space declared in"
611 " %s section: zeroing",
612 (segto == stext.index ? "code" : "data"));
613 aout_sect_write(s, NULL, realbytes);
614 } else
615 sbss.len += realbytes;
616 } else if (type == OUT_RAWDATA) {
617 if (segment != NO_SEG)
618 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
619 aout_sect_write(s, data, realbytes);
620 } else if (type == OUT_ADDRESS) {
621 addr = *(int32_t *)data;
622 if (segment != NO_SEG) {
623 if (segment % 2) {
624 error(ERR_NONFATAL, "a.out format does not support"
625 " segment base references");
626 } else {
627 if (wrt == NO_SEG) {
628 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE,
629 realbytes);
630 } else if (!bsd) {
631 error(ERR_NONFATAL,
632 "Linux a.out format does not support"
633 " any use of WRT");
634 wrt = NO_SEG; /* we can at least _try_ to continue */
635 } else if (wrt == aout_gotpc_sect + 1) {
636 is_pic = 0x40;
637 aout_add_reloc(s, segment, RELTYPE_GOTPC, realbytes);
638 } else if (wrt == aout_gotoff_sect + 1) {
639 is_pic = 0x40;
640 addr = aout_add_gotoff_reloc(s, segment,
641 addr, realbytes);
642 } else if (wrt == aout_got_sect + 1) {
643 is_pic = 0x40;
644 addr =
645 aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
646 realbytes, TRUE);
647 } else if (wrt == aout_sym_sect + 1) {
648 addr = aout_add_gsym_reloc(s, segment, addr,
649 RELTYPE_ABSOLUTE, realbytes,
650 FALSE);
651 } else if (wrt == aout_plt_sect + 1) {
652 is_pic = 0x40;
653 error(ERR_NONFATAL,
654 "a.out format cannot produce non-PC-"
655 "relative PLT references");
656 } else {
657 error(ERR_NONFATAL,
658 "a.out format does not support this"
659 " use of WRT");
660 wrt = NO_SEG; /* we can at least _try_ to continue */
664 p = mydata;
665 if (realbytes == 2)
666 WRITESHORT(p, addr);
667 else
668 WRITELONG(p, addr);
669 aout_sect_write(s, mydata, realbytes);
670 } else if (type == OUT_REL2ADR) {
671 if (segment == segto)
672 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
673 if (segment != NO_SEG && segment % 2) {
674 error(ERR_NONFATAL, "a.out format does not support"
675 " segment base references");
676 } else {
677 if (wrt == NO_SEG) {
678 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
679 } else if (!bsd) {
680 error(ERR_NONFATAL, "Linux a.out format does not support"
681 " any use of WRT");
682 wrt = NO_SEG; /* we can at least _try_ to continue */
683 } else if (wrt == aout_plt_sect + 1) {
684 is_pic = 0x40;
685 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
686 } else if (wrt == aout_gotpc_sect + 1 ||
687 wrt == aout_gotoff_sect + 1 ||
688 wrt == aout_got_sect + 1) {
689 error(ERR_NONFATAL, "a.out format cannot produce PC-"
690 "relative GOT references");
691 } else {
692 error(ERR_NONFATAL, "a.out format does not support this"
693 " use of WRT");
694 wrt = NO_SEG; /* we can at least _try_ to continue */
697 p = mydata;
698 WRITESHORT(p, *(int32_t *)data - (realbytes + s->len));
699 aout_sect_write(s, mydata, 2L);
700 } else if (type == OUT_REL4ADR) {
701 if (segment == segto)
702 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
703 if (segment != NO_SEG && segment % 2) {
704 error(ERR_NONFATAL, "a.out format does not support"
705 " segment base references");
706 } else {
707 if (wrt == NO_SEG) {
708 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
709 } else if (!bsd) {
710 error(ERR_NONFATAL, "Linux a.out format does not support"
711 " any use of WRT");
712 wrt = NO_SEG; /* we can at least _try_ to continue */
713 } else if (wrt == aout_plt_sect + 1) {
714 is_pic = 0x40;
715 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
716 } else if (wrt == aout_gotpc_sect + 1 ||
717 wrt == aout_gotoff_sect + 1 ||
718 wrt == aout_got_sect + 1) {
719 error(ERR_NONFATAL, "a.out format cannot produce PC-"
720 "relative GOT references");
721 } else {
722 error(ERR_NONFATAL, "a.out format does not support this"
723 " use of WRT");
724 wrt = NO_SEG; /* we can at least _try_ to continue */
727 p = mydata;
728 WRITELONG(p, *(int32_t *)data - (realbytes + s->len));
729 aout_sect_write(s, mydata, 4L);
733 static void aout_pad_sections(void)
735 static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
737 * Pad each of the text and data sections with NOPs until their
738 * length is a multiple of four. (NOP == 0x90.) Also increase
739 * the length of the BSS section similarly.
741 aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
742 aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
743 sbss.len = (sbss.len + 3) & ~3;
747 * a.out files have the curious property that all references to
748 * things in the data or bss sections are done by addresses which
749 * are actually relative to the start of the _text_ section, in the
750 * _file_. (No relation to what happens after linking. No idea why
751 * this should be so. It's very strange.) So we have to go through
752 * the relocation table, _after_ the final size of each section is
753 * known, and fix up the relocations pointed to.
755 static void aout_fixup_relocs(struct Section *sect)
757 struct Reloc *r;
759 saa_rewind(sect->data);
760 for (r = sect->head; r; r = r->next) {
761 uint8_t *p, *q, blk[4];
762 int32_t l;
764 saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
765 p = q = blk;
766 l = *p++;
767 if (r->bytes > 1) {
768 l += ((int32_t)*p++) << 8;
769 if (r->bytes == 4) {
770 l += ((int32_t)*p++) << 16;
771 l += ((int32_t)*p++) << 24;
774 if (r->symbol == -SECT_DATA)
775 l += stext.len;
776 else if (r->symbol == -SECT_BSS)
777 l += stext.len + sdata.len;
778 if (r->bytes == 4)
779 WRITELONG(q, l);
780 else if (r->bytes == 2)
781 WRITESHORT(q, l);
782 else
783 *q++ = l & 0xFF;
784 saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
788 static void aout_write(void)
791 * Emit the a.out header.
793 /* OMAGIC, M_386 or MID_I386, no flags */
794 fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
795 fwriteint32_t(stext.len, aoutfp);
796 fwriteint32_t(sdata.len, aoutfp);
797 fwriteint32_t(sbss.len, aoutfp);
798 fwriteint32_t(nsyms * 12, aoutfp); /* length of symbol table */
799 fwriteint32_t(0L, aoutfp); /* object files have no entry point */
800 fwriteint32_t(stext.nrelocs * 8, aoutfp); /* size of text relocs */
801 fwriteint32_t(sdata.nrelocs * 8, aoutfp); /* size of data relocs */
804 * Write out the code section and the data section.
806 saa_fpwrite(stext.data, aoutfp);
807 saa_fpwrite(sdata.data, aoutfp);
810 * Write out the relocations.
812 aout_write_relocs(stext.head);
813 aout_write_relocs(sdata.head);
816 * Write the symbol table.
818 aout_write_syms();
821 * And the string table.
823 fwriteint32_t(strslen + 4, aoutfp); /* length includes length count */
824 saa_fpwrite(strs, aoutfp);
827 static void aout_write_relocs(struct Reloc *r)
829 while (r) {
830 uint32_t word2;
832 fwriteint32_t(r->address, aoutfp);
834 if (r->symbol >= 0)
835 word2 = r->symbol;
836 else
837 word2 = -r->symbol;
838 word2 |= r->reltype << 24;
839 word2 |= (r->bytes == 1 ? 0 :
840 r->bytes == 2 ? 0x2000000L : 0x4000000L);
841 fwriteint32_t(word2, aoutfp);
843 r = r->next;
847 static void aout_write_syms(void)
849 uint32_t i;
851 saa_rewind(syms);
852 for (i = 0; i < nsyms; i++) {
853 struct Symbol *sym = saa_rstruct(syms);
854 fwriteint32_t(sym->strpos, aoutfp);
855 fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, aoutfp);
857 * Fix up the symbol value now we know the final section
858 * sizes.
860 if ((sym->type & SECT_MASK) == SECT_DATA)
861 sym->value += stext.len;
862 if ((sym->type & SECT_MASK) == SECT_BSS)
863 sym->value += stext.len + sdata.len;
864 fwriteint32_t(sym->value, aoutfp);
866 * Output a size record if necessary.
868 if (sym->type & SYM_WITH_SIZE) {
869 fwriteint32_t(sym->strpos, aoutfp);
870 fwriteint32_t(0x0DL, aoutfp); /* special value: means size */
871 fwriteint32_t(sym->size, aoutfp);
872 i++; /* use up another of `nsyms' */
877 static void aout_sect_write(struct Section *sect,
878 const uint8_t *data, uint32_t len)
880 saa_wbytes(sect->data, data, len);
881 sect->len += len;
884 static int32_t aout_segbase(int32_t segment)
886 return segment;
889 static int aout_directive(int8_t *directive, int8_t *value, int pass)
891 return 0;
894 static void aout_filename(int8_t *inname, int8_t *outname, efunc error)
896 standard_extension(inname, outname, ".o", error);
899 static const int8_t *aout_stdmac[] = {
900 "%define __SECT__ [section .text]",
901 "%macro __NASM_CDecl__ 1",
902 "%endmacro",
903 NULL
906 static int aout_set_info(enum geninfo type, int8_t **val)
908 return 0;
910 #endif /* OF_AOUT || OF_AOUTB */
912 #ifdef OF_AOUT
914 struct ofmt of_aout = {
915 "Linux a.out object files",
916 "aout",
917 NULL,
918 null_debug_arr,
919 &null_debug_form,
920 aout_stdmac,
921 aout_init,
922 aout_set_info,
923 aout_out,
924 aout_deflabel,
925 aout_section_names,
926 aout_segbase,
927 aout_directive,
928 aout_filename,
929 aout_cleanup
932 #endif
934 #ifdef OF_AOUTB
936 struct ofmt of_aoutb = {
937 "NetBSD/FreeBSD a.out object files",
938 "aoutb",
939 NULL,
940 null_debug_arr,
941 &null_debug_form,
942 aout_stdmac,
943 aoutb_init,
944 aout_set_info,
945 aout_out,
946 aout_deflabel,
947 aout_section_names,
948 aout_segbase,
949 aout_directive,
950 aout_filename,
951 aout_cleanup
954 #endif