BR 2826669: update licensing information in README
[nasm.git] / output / outaout.c
blob4d38cdfc56f84026edfe9c44183f822e0fe6f53a
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
34 /*
35 * outaout.c output routines for the Netwide Assembler to produce
36 * Linux a.out object files
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "output/outform.h"
53 #include "output/outlib.h"
55 #if defined OF_AOUT || defined OF_AOUTB
57 #define RELTYPE_ABSOLUTE 0x00
58 #define RELTYPE_RELATIVE 0x01
59 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
60 #define RELTYPE_GOTOFF 0x10
61 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
62 #define RELTYPE_PLT 0x21
63 #define RELTYPE_SYMFLAG 0x08
65 struct Reloc {
66 struct Reloc *next;
67 int32_t address; /* relative to _start_ of section */
68 int32_t symbol; /* symbol number or -ve section id */
69 int bytes; /* 2 or 4 */
70 int reltype; /* see above */
73 struct Symbol {
74 int32_t strpos; /* string table position of name */
75 int type; /* symbol type - see flags below */
76 int32_t value; /* address, or COMMON variable size */
77 int32_t size; /* size for data or function exports */
78 int32_t segment; /* back-reference used by gsym_reloc */
79 struct Symbol *next; /* list of globals in each section */
80 struct Symbol *nextfwd; /* list of unresolved-size symbols */
81 char *name; /* for unresolved-size symbols */
82 int32_t symnum; /* index into symbol table */
86 * Section IDs - used in Reloc.symbol when negative, and in
87 * Symbol.type when positive.
89 #define SECT_ABS 2 /* absolute value */
90 #define SECT_TEXT 4 /* text section */
91 #define SECT_DATA 6 /* data section */
92 #define SECT_BSS 8 /* bss section */
93 #define SECT_MASK 0xE /* mask out any of the above */
96 * More flags used in Symbol.type.
98 #define SYM_GLOBAL 1 /* it's a global symbol */
99 #define SYM_DATA 0x100 /* used for shared libs */
100 #define SYM_FUNCTION 0x200 /* used for shared libs */
101 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
104 * Bit more explanation of symbol types: SECT_xxx denotes a local
105 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
106 * this module. Just SYM_GLOBAL, with zero value, denotes an
107 * external symbol referenced in this module. And just SYM_GLOBAL,
108 * but with a non-zero value, declares a C `common' variable, of
109 * size `value'.
112 struct Section {
113 struct SAA *data;
114 uint32_t len, size, nrelocs;
115 int32_t index;
116 struct Reloc *head, **tail;
117 struct Symbol *gsyms, *asym;
120 static struct Section stext, sdata, sbss;
122 static struct SAA *syms;
123 static uint32_t nsyms;
125 static struct RAA *bsym;
127 static struct SAA *strs;
128 static uint32_t strslen;
130 static struct Symbol *fwds;
132 static FILE *aoutfp;
133 static efunc error;
134 static evalfunc evaluate;
136 static int bsd;
137 static int is_pic;
139 static void aout_write(void);
140 static void aout_write_relocs(struct Reloc *);
141 static void aout_write_syms(void);
142 static void aout_sect_write(struct Section *, const uint8_t *,
143 uint32_t);
144 static void aout_pad_sections(void);
145 static void aout_fixup_relocs(struct Section *);
148 * Special section numbers which are used to define special
149 * symbols, which can be used with WRT to provide PIC relocation
150 * types.
152 static int32_t aout_gotpc_sect, aout_gotoff_sect;
153 static int32_t aout_got_sect, aout_plt_sect;
154 static int32_t aout_sym_sect;
156 static void aoutg_init(FILE * fp, efunc errfunc, ldfunc ldef,
157 evalfunc eval)
159 aoutfp = fp;
160 error = errfunc;
161 evaluate = eval;
162 (void)ldef; /* placate optimisers */
163 stext.data = saa_init(1L);
164 stext.head = NULL;
165 stext.tail = &stext.head;
166 sdata.data = saa_init(1L);
167 sdata.head = NULL;
168 sdata.tail = &sdata.head;
169 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
170 stext.nrelocs = sdata.nrelocs = 0;
171 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
172 stext.index = seg_alloc();
173 sdata.index = seg_alloc();
174 sbss.index = seg_alloc();
175 stext.asym = sdata.asym = sbss.asym = NULL;
176 syms = saa_init((int32_t)sizeof(struct Symbol));
177 nsyms = 0;
178 bsym = raa_init();
179 strs = saa_init(1L);
180 strslen = 0;
181 fwds = NULL;
184 #ifdef OF_AOUT
186 static void aout_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
188 bsd = false;
189 aoutg_init(fp, errfunc, ldef, eval);
191 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
192 aout_plt_sect = aout_sym_sect = NO_SEG;
195 #endif
197 #ifdef OF_AOUTB
199 extern struct ofmt of_aoutb;
201 static void aoutb_init(FILE * fp, efunc errfunc, ldfunc ldef,
202 evalfunc eval)
204 bsd = true;
205 aoutg_init(fp, errfunc, ldef, eval);
207 is_pic = 0x00; /* may become 0x40 */
209 aout_gotpc_sect = seg_alloc();
210 ldef("..gotpc", aout_gotpc_sect + 1, 0L, NULL, false, false, &of_aoutb,
211 error);
212 aout_gotoff_sect = seg_alloc();
213 ldef("..gotoff", aout_gotoff_sect + 1, 0L, NULL, false, false,
214 &of_aoutb, error);
215 aout_got_sect = seg_alloc();
216 ldef("..got", aout_got_sect + 1, 0L, NULL, false, false, &of_aoutb,
217 error);
218 aout_plt_sect = seg_alloc();
219 ldef("..plt", aout_plt_sect + 1, 0L, NULL, false, false, &of_aoutb,
220 error);
221 aout_sym_sect = seg_alloc();
222 ldef("..sym", aout_sym_sect + 1, 0L, NULL, false, false, &of_aoutb,
223 error);
226 #endif
228 static void aout_cleanup(int debuginfo)
230 struct Reloc *r;
232 (void)debuginfo;
234 aout_pad_sections();
235 aout_fixup_relocs(&stext);
236 aout_fixup_relocs(&sdata);
237 aout_write();
238 fclose(aoutfp);
239 saa_free(stext.data);
240 while (stext.head) {
241 r = stext.head;
242 stext.head = stext.head->next;
243 nasm_free(r);
245 saa_free(sdata.data);
246 while (sdata.head) {
247 r = sdata.head;
248 sdata.head = sdata.head->next;
249 nasm_free(r);
251 saa_free(syms);
252 raa_free(bsym);
253 saa_free(strs);
256 static int32_t aout_section_names(char *name, int pass, int *bits)
259 (void)pass;
262 * Default to 32 bits.
264 if (!name)
265 *bits = 32;
267 if (!name)
268 return stext.index;
270 if (!strcmp(name, ".text"))
271 return stext.index;
272 else if (!strcmp(name, ".data"))
273 return sdata.index;
274 else if (!strcmp(name, ".bss"))
275 return sbss.index;
276 else
277 return NO_SEG;
280 static void aout_deflabel(char *name, int32_t segment, int64_t offset,
281 int is_global, char *special)
283 int pos = strslen + 4;
284 struct Symbol *sym;
285 int special_used = false;
287 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
289 * This is a NASM special symbol. We never allow it into
290 * the a.out symbol table, even if it's a valid one. If it
291 * _isn't_ a valid one, we should barf immediately.
293 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
294 strcmp(name, "..got") && strcmp(name, "..plt") &&
295 strcmp(name, "..sym"))
296 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
297 return;
300 if (is_global == 3) {
301 struct Symbol **s;
303 * Fix up a forward-reference symbol size from the first
304 * pass.
306 for (s = &fwds; *s; s = &(*s)->nextfwd)
307 if (!strcmp((*s)->name, name)) {
308 struct tokenval tokval;
309 expr *e;
310 char *p = special;
312 while (*p && !nasm_isspace(*p))
313 p++;
314 while (*p && nasm_isspace(*p))
315 p++;
316 stdscan_reset();
317 stdscan_bufptr = p;
318 tokval.t_type = TOKEN_INVALID;
319 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
320 if (e) {
321 if (!is_simple(e))
322 error(ERR_NONFATAL, "cannot use relocatable"
323 " expression as symbol size");
324 else
325 (*s)->size = reloc_value(e);
329 * Remove it from the list of unresolved sizes.
331 nasm_free((*s)->name);
332 *s = (*s)->nextfwd;
333 return;
335 return; /* it wasn't an important one */
338 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
339 strslen += 1 + strlen(name);
341 sym = saa_wstruct(syms);
343 sym->strpos = pos;
344 sym->type = is_global ? SYM_GLOBAL : 0;
345 sym->segment = segment;
346 if (segment == NO_SEG)
347 sym->type |= SECT_ABS;
348 else if (segment == stext.index) {
349 sym->type |= SECT_TEXT;
350 if (is_global) {
351 sym->next = stext.gsyms;
352 stext.gsyms = sym;
353 } else if (!stext.asym)
354 stext.asym = sym;
355 } else if (segment == sdata.index) {
356 sym->type |= SECT_DATA;
357 if (is_global) {
358 sym->next = sdata.gsyms;
359 sdata.gsyms = sym;
360 } else if (!sdata.asym)
361 sdata.asym = sym;
362 } else if (segment == sbss.index) {
363 sym->type |= SECT_BSS;
364 if (is_global) {
365 sym->next = sbss.gsyms;
366 sbss.gsyms = sym;
367 } else if (!sbss.asym)
368 sbss.asym = sym;
369 } else
370 sym->type = SYM_GLOBAL;
371 if (is_global == 2)
372 sym->value = offset;
373 else
374 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
376 if (is_global && sym->type != SYM_GLOBAL) {
378 * Global symbol exported _from_ this module. We must check
379 * the special text for type information.
382 if (special) {
383 int n = strcspn(special, " ");
385 if (!nasm_strnicmp(special, "function", n))
386 sym->type |= SYM_FUNCTION;
387 else if (!nasm_strnicmp(special, "data", n) ||
388 !nasm_strnicmp(special, "object", n))
389 sym->type |= SYM_DATA;
390 else
391 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
392 n, special);
393 if (special[n]) {
394 struct tokenval tokval;
395 expr *e;
396 int fwd = false;
397 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
399 if (!bsd) {
400 error(ERR_NONFATAL, "Linux a.out does not support"
401 " symbol size information");
402 } else {
403 while (special[n] && nasm_isspace(special[n]))
404 n++;
406 * We have a size expression; attempt to
407 * evaluate it.
409 sym->type |= SYM_WITH_SIZE;
410 stdscan_reset();
411 stdscan_bufptr = special + n;
412 tokval.t_type = TOKEN_INVALID;
413 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
414 NULL);
415 if (fwd) {
416 sym->nextfwd = fwds;
417 fwds = sym;
418 sym->name = nasm_strdup(name);
419 } else if (e) {
420 if (!is_simple(e))
421 error(ERR_NONFATAL, "cannot use relocatable"
422 " expression as symbol size");
423 else
424 sym->size = reloc_value(e);
427 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
429 special_used = true;
434 * define the references from external-symbol segment numbers
435 * to these symbol records.
437 if (segment != NO_SEG && segment != stext.index &&
438 segment != sdata.index && segment != sbss.index)
439 bsym = raa_write(bsym, segment, nsyms);
440 sym->symnum = nsyms;
442 nsyms++;
443 if (sym->type & SYM_WITH_SIZE)
444 nsyms++; /* and another for the size */
446 if (special && !special_used)
447 error(ERR_NONFATAL, "no special symbol features supported here");
450 static void aout_add_reloc(struct Section *sect, int32_t segment,
451 int reltype, int bytes)
453 struct Reloc *r;
455 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
456 sect->tail = &r->next;
457 r->next = NULL;
459 r->address = sect->len;
460 r->symbol = (segment == NO_SEG ? -SECT_ABS :
461 segment == stext.index ? -SECT_TEXT :
462 segment == sdata.index ? -SECT_DATA :
463 segment == sbss.index ? -SECT_BSS :
464 raa_read(bsym, segment));
465 r->reltype = reltype;
466 if (r->symbol >= 0)
467 r->reltype |= RELTYPE_SYMFLAG;
468 r->bytes = bytes;
470 sect->nrelocs++;
474 * This routine deals with ..got and ..sym relocations: the more
475 * complicated kinds. In shared-library writing, some relocations
476 * with respect to global symbols must refer to the precise symbol
477 * rather than referring to an offset from the base of the section
478 * _containing_ the symbol. Such relocations call to this routine,
479 * which searches the symbol list for the symbol in question.
481 * RELTYPE_GOT references require the _exact_ symbol address to be
482 * used; RELTYPE_ABSOLUTE references can be at an offset from the
483 * symbol. The boolean argument `exact' tells us this.
485 * Return value is the adjusted value of `addr', having become an
486 * offset from the symbol rather than the section. Should always be
487 * zero when returning from an exact call.
489 * Limitation: if you define two symbols at the same place,
490 * confusion will occur.
492 * Inefficiency: we search, currently, using a linked list which
493 * isn't even necessarily sorted.
495 static int32_t aout_add_gsym_reloc(struct Section *sect,
496 int32_t segment, int32_t offset,
497 int type, int bytes, int exact)
499 struct Symbol *sym, *sm, *shead;
500 struct Reloc *r;
503 * First look up the segment to find whether it's text, data,
504 * bss or an external symbol.
506 shead = NULL;
507 if (segment == stext.index)
508 shead = stext.gsyms;
509 else if (segment == sdata.index)
510 shead = sdata.gsyms;
511 else if (segment == sbss.index)
512 shead = sbss.gsyms;
513 if (!shead) {
514 if (exact && offset != 0)
515 error(ERR_NONFATAL, "unable to find a suitable global symbol"
516 " for this reference");
517 else
518 aout_add_reloc(sect, segment, type, bytes);
519 return offset;
522 if (exact) {
524 * Find a symbol pointing _exactly_ at this one.
526 for (sym = shead; sym; sym = sym->next)
527 if (sym->value == offset)
528 break;
529 } else {
531 * Find the nearest symbol below this one.
533 sym = NULL;
534 for (sm = shead; sm; sm = sm->next)
535 if (sm->value <= offset && (!sym || sm->value > sym->value))
536 sym = sm;
538 if (!sym && exact) {
539 error(ERR_NONFATAL, "unable to find a suitable global symbol"
540 " for this reference");
541 return 0;
544 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
545 sect->tail = &r->next;
546 r->next = NULL;
548 r->address = sect->len;
549 r->symbol = sym->symnum;
550 r->reltype = type | RELTYPE_SYMFLAG;
551 r->bytes = bytes;
553 sect->nrelocs++;
555 return offset - sym->value;
559 * This routine deals with ..gotoff relocations. These _must_ refer
560 * to a symbol, due to a perversity of *BSD's PIC implementation,
561 * and it must be a non-global one as well; so we store `asym', the
562 * first nonglobal symbol defined in each section, and always work
563 * from that. Relocation type is always RELTYPE_GOTOFF.
565 * Return value is the adjusted value of `addr', having become an
566 * offset from the `asym' symbol rather than the section.
568 static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
569 int32_t offset, int bytes)
571 struct Reloc *r;
572 struct Symbol *asym;
575 * First look up the segment to find whether it's text, data,
576 * bss or an external symbol.
578 asym = NULL;
579 if (segment == stext.index)
580 asym = stext.asym;
581 else if (segment == sdata.index)
582 asym = sdata.asym;
583 else if (segment == sbss.index)
584 asym = sbss.asym;
585 if (!asym)
586 error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
587 " symbol in the section");
589 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
590 sect->tail = &r->next;
591 r->next = NULL;
593 r->address = sect->len;
594 r->symbol = asym->symnum;
595 r->reltype = RELTYPE_GOTOFF;
596 r->bytes = bytes;
598 sect->nrelocs++;
600 return offset - asym->value;
603 static void aout_out(int32_t segto, const void *data,
604 enum out_type type, uint64_t size,
605 int32_t segment, int32_t wrt)
607 struct Section *s;
608 int32_t addr;
609 uint8_t mydata[4], *p;
612 * handle absolute-assembly (structure definitions)
614 if (segto == NO_SEG) {
615 if (type != OUT_RESERVE)
616 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
617 " space");
618 return;
621 if (segto == stext.index)
622 s = &stext;
623 else if (segto == sdata.index)
624 s = &sdata;
625 else if (segto == sbss.index)
626 s = NULL;
627 else {
628 error(ERR_WARNING, "attempt to assemble code in"
629 " segment %d: defaulting to `.text'", segto);
630 s = &stext;
633 if (!s && type != OUT_RESERVE) {
634 error(ERR_WARNING, "attempt to initialize memory in the"
635 " BSS section: ignored");
636 sbss.len += realsize(type, size);
637 return;
640 if (type == OUT_RESERVE) {
641 if (s) {
642 error(ERR_WARNING, "uninitialized space declared in"
643 " %s section: zeroing",
644 (segto == stext.index ? "code" : "data"));
645 aout_sect_write(s, NULL, size);
646 } else
647 sbss.len += size;
648 } else if (type == OUT_RAWDATA) {
649 if (segment != NO_SEG)
650 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
651 aout_sect_write(s, data, size);
652 } else if (type == OUT_ADDRESS) {
653 addr = *(int64_t *)data;
654 if (segment != NO_SEG) {
655 if (segment % 2) {
656 error(ERR_NONFATAL, "a.out format does not support"
657 " segment base references");
658 } else {
659 if (wrt == NO_SEG) {
660 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE,
661 size);
662 } else if (!bsd) {
663 error(ERR_NONFATAL,
664 "Linux a.out format does not support"
665 " any use of WRT");
666 wrt = NO_SEG; /* we can at least _try_ to continue */
667 } else if (wrt == aout_gotpc_sect + 1) {
668 is_pic = 0x40;
669 aout_add_reloc(s, segment, RELTYPE_GOTPC, size);
670 } else if (wrt == aout_gotoff_sect + 1) {
671 is_pic = 0x40;
672 addr = aout_add_gotoff_reloc(s, segment,
673 addr, size);
674 } else if (wrt == aout_got_sect + 1) {
675 is_pic = 0x40;
676 addr =
677 aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
678 size, true);
679 } else if (wrt == aout_sym_sect + 1) {
680 addr = aout_add_gsym_reloc(s, segment, addr,
681 RELTYPE_ABSOLUTE, size,
682 false);
683 } else if (wrt == aout_plt_sect + 1) {
684 is_pic = 0x40;
685 error(ERR_NONFATAL,
686 "a.out format cannot produce non-PC-"
687 "relative PLT references");
688 } else {
689 error(ERR_NONFATAL,
690 "a.out format does not support this"
691 " use of WRT");
692 wrt = NO_SEG; /* we can at least _try_ to continue */
696 p = mydata;
697 if (size == 2)
698 WRITESHORT(p, addr);
699 else
700 WRITELONG(p, addr);
701 aout_sect_write(s, mydata, size);
702 } else if (type == OUT_REL2ADR) {
703 if (segment == segto)
704 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
705 if (segment != NO_SEG && segment % 2) {
706 error(ERR_NONFATAL, "a.out format does not support"
707 " segment base references");
708 } else {
709 if (wrt == NO_SEG) {
710 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
711 } else if (!bsd) {
712 error(ERR_NONFATAL, "Linux a.out format does not support"
713 " any use of WRT");
714 wrt = NO_SEG; /* we can at least _try_ to continue */
715 } else if (wrt == aout_plt_sect + 1) {
716 is_pic = 0x40;
717 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
718 } else if (wrt == aout_gotpc_sect + 1 ||
719 wrt == aout_gotoff_sect + 1 ||
720 wrt == aout_got_sect + 1) {
721 error(ERR_NONFATAL, "a.out format cannot produce PC-"
722 "relative GOT references");
723 } else {
724 error(ERR_NONFATAL, "a.out format does not support this"
725 " use of WRT");
726 wrt = NO_SEG; /* we can at least _try_ to continue */
729 p = mydata;
730 WRITESHORT(p, *(int64_t *)data - (size + s->len));
731 aout_sect_write(s, mydata, 2L);
732 } else if (type == OUT_REL4ADR) {
733 if (segment == segto)
734 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
735 if (segment != NO_SEG && segment % 2) {
736 error(ERR_NONFATAL, "a.out format does not support"
737 " segment base references");
738 } else {
739 if (wrt == NO_SEG) {
740 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
741 } else if (!bsd) {
742 error(ERR_NONFATAL, "Linux a.out format does not support"
743 " any use of WRT");
744 wrt = NO_SEG; /* we can at least _try_ to continue */
745 } else if (wrt == aout_plt_sect + 1) {
746 is_pic = 0x40;
747 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
748 } else if (wrt == aout_gotpc_sect + 1 ||
749 wrt == aout_gotoff_sect + 1 ||
750 wrt == aout_got_sect + 1) {
751 error(ERR_NONFATAL, "a.out format cannot produce PC-"
752 "relative GOT references");
753 } else {
754 error(ERR_NONFATAL, "a.out format does not support this"
755 " use of WRT");
756 wrt = NO_SEG; /* we can at least _try_ to continue */
759 p = mydata;
760 WRITELONG(p, *(int64_t *)data - (size + s->len));
761 aout_sect_write(s, mydata, 4L);
765 static void aout_pad_sections(void)
767 static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
769 * Pad each of the text and data sections with NOPs until their
770 * length is a multiple of four. (NOP == 0x90.) Also increase
771 * the length of the BSS section similarly.
773 aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
774 aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
775 sbss.len = (sbss.len + 3) & ~3;
779 * a.out files have the curious property that all references to
780 * things in the data or bss sections are done by addresses which
781 * are actually relative to the start of the _text_ section, in the
782 * _file_. (No relation to what happens after linking. No idea why
783 * this should be so. It's very strange.) So we have to go through
784 * the relocation table, _after_ the final size of each section is
785 * known, and fix up the relocations pointed to.
787 static void aout_fixup_relocs(struct Section *sect)
789 struct Reloc *r;
791 saa_rewind(sect->data);
792 for (r = sect->head; r; r = r->next) {
793 uint8_t *p, *q, blk[4];
794 int32_t l;
796 saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
797 p = q = blk;
798 l = *p++;
799 if (r->bytes > 1) {
800 l += ((int32_t)*p++) << 8;
801 if (r->bytes == 4) {
802 l += ((int32_t)*p++) << 16;
803 l += ((int32_t)*p++) << 24;
806 if (r->symbol == -SECT_DATA)
807 l += stext.len;
808 else if (r->symbol == -SECT_BSS)
809 l += stext.len + sdata.len;
810 if (r->bytes == 4)
811 WRITELONG(q, l);
812 else if (r->bytes == 2)
813 WRITESHORT(q, l);
814 else
815 *q++ = l & 0xFF;
816 saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
820 static void aout_write(void)
823 * Emit the a.out header.
825 /* OMAGIC, M_386 or MID_I386, no flags */
826 fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
827 fwriteint32_t(stext.len, aoutfp);
828 fwriteint32_t(sdata.len, aoutfp);
829 fwriteint32_t(sbss.len, aoutfp);
830 fwriteint32_t(nsyms * 12, aoutfp); /* length of symbol table */
831 fwriteint32_t(0L, aoutfp); /* object files have no entry point */
832 fwriteint32_t(stext.nrelocs * 8, aoutfp); /* size of text relocs */
833 fwriteint32_t(sdata.nrelocs * 8, aoutfp); /* size of data relocs */
836 * Write out the code section and the data section.
838 saa_fpwrite(stext.data, aoutfp);
839 saa_fpwrite(sdata.data, aoutfp);
842 * Write out the relocations.
844 aout_write_relocs(stext.head);
845 aout_write_relocs(sdata.head);
848 * Write the symbol table.
850 aout_write_syms();
853 * And the string table.
855 fwriteint32_t(strslen + 4, aoutfp); /* length includes length count */
856 saa_fpwrite(strs, aoutfp);
859 static void aout_write_relocs(struct Reloc *r)
861 while (r) {
862 uint32_t word2;
864 fwriteint32_t(r->address, aoutfp);
866 if (r->symbol >= 0)
867 word2 = r->symbol;
868 else
869 word2 = -r->symbol;
870 word2 |= r->reltype << 24;
871 word2 |= (r->bytes == 1 ? 0 :
872 r->bytes == 2 ? 0x2000000L : 0x4000000L);
873 fwriteint32_t(word2, aoutfp);
875 r = r->next;
879 static void aout_write_syms(void)
881 uint32_t i;
883 saa_rewind(syms);
884 for (i = 0; i < nsyms; i++) {
885 struct Symbol *sym = saa_rstruct(syms);
886 fwriteint32_t(sym->strpos, aoutfp);
887 fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, aoutfp);
889 * Fix up the symbol value now we know the final section
890 * sizes.
892 if ((sym->type & SECT_MASK) == SECT_DATA)
893 sym->value += stext.len;
894 if ((sym->type & SECT_MASK) == SECT_BSS)
895 sym->value += stext.len + sdata.len;
896 fwriteint32_t(sym->value, aoutfp);
898 * Output a size record if necessary.
900 if (sym->type & SYM_WITH_SIZE) {
901 fwriteint32_t(sym->strpos, aoutfp);
902 fwriteint32_t(0x0DL, aoutfp); /* special value: means size */
903 fwriteint32_t(sym->size, aoutfp);
904 i++; /* use up another of `nsyms' */
909 static void aout_sect_write(struct Section *sect,
910 const uint8_t *data, uint32_t len)
912 saa_wbytes(sect->data, data, len);
913 sect->len += len;
916 static int32_t aout_segbase(int32_t segment)
918 return segment;
921 static int aout_directive(char *directive, char *value, int pass)
923 (void)directive;
924 (void)value;
925 (void)pass;
926 return 0;
929 static void aout_filename(char *inname, char *outname, efunc error)
931 standard_extension(inname, outname, ".o", error);
934 extern macros_t aout_stdmac[];
936 static int aout_set_info(enum geninfo type, char **val)
938 (void)type;
939 (void)val;
940 return 0;
942 #endif /* OF_AOUT || OF_AOUTB */
944 #ifdef OF_AOUT
946 struct ofmt of_aout = {
947 "Linux a.out object files",
948 "aout",
950 null_debug_arr,
951 &null_debug_form,
952 aout_stdmac,
953 aout_init,
954 aout_set_info,
955 aout_out,
956 aout_deflabel,
957 aout_section_names,
958 aout_segbase,
959 aout_directive,
960 aout_filename,
961 aout_cleanup
964 #endif
966 #ifdef OF_AOUTB
968 struct ofmt of_aoutb = {
969 "NetBSD/FreeBSD a.out object files",
970 "aoutb",
972 null_debug_arr,
973 &null_debug_form,
974 aout_stdmac,
975 aoutb_init,
976 aout_set_info,
977 aout_out,
978 aout_deflabel,
979 aout_section_names,
980 aout_segbase,
981 aout_directive,
982 aout_filename,
983 aout_cleanup
986 #endif