output: aout -- Use nasm_x_space helpers
[nasm.git] / output / outaout.c
blob575089c6fa8dfaa8b2e313500e5b763a84236228
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2013 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 "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
56 #if defined OF_AOUT || defined OF_AOUTB
58 #define RELTYPE_ABSOLUTE 0x00
59 #define RELTYPE_RELATIVE 0x01
60 #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
61 #define RELTYPE_GOTOFF 0x10
62 #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
63 #define RELTYPE_PLT 0x21
64 #define RELTYPE_SYMFLAG 0x08
66 struct Reloc {
67 struct Reloc *next;
68 int32_t address; /* relative to _start_ of section */
69 int32_t symbol; /* symbol number or -ve section id */
70 int bytes; /* 2 or 4 */
71 int reltype; /* see above */
74 struct Symbol {
75 int32_t strpos; /* string table position of name */
76 int type; /* symbol type - see flags below */
77 int32_t value; /* address, or COMMON variable size */
78 int32_t size; /* size for data or function exports */
79 int32_t segment; /* back-reference used by gsym_reloc */
80 struct Symbol *next; /* list of globals in each section */
81 struct Symbol *nextfwd; /* list of unresolved-size symbols */
82 char *name; /* for unresolved-size symbols */
83 int32_t symnum; /* index into symbol table */
87 * Section IDs - used in Reloc.symbol when negative, and in
88 * Symbol.type when positive.
90 #define SECT_ABS 2 /* absolute value */
91 #define SECT_TEXT 4 /* text section */
92 #define SECT_DATA 6 /* data section */
93 #define SECT_BSS 8 /* bss section */
94 #define SECT_MASK 0xE /* mask out any of the above */
97 * More flags used in Symbol.type.
99 #define SYM_GLOBAL 1 /* it's a global symbol */
100 #define SYM_DATA 0x100 /* used for shared libs */
101 #define SYM_FUNCTION 0x200 /* used for shared libs */
102 #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
105 * Bit more explanation of symbol types: SECT_xxx denotes a local
106 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
107 * this module. Just SYM_GLOBAL, with zero value, denotes an
108 * external symbol referenced in this module. And just SYM_GLOBAL,
109 * but with a non-zero value, declares a C `common' variable, of
110 * size `value'.
113 struct Section {
114 struct SAA *data;
115 uint32_t len, size, nrelocs;
116 int32_t index;
117 struct Reloc *head, **tail;
118 struct Symbol *gsyms, *asym;
121 static struct Section stext, sdata, sbss;
123 static struct SAA *syms;
124 static uint32_t nsyms;
126 static struct RAA *bsym;
128 static struct SAA *strs;
129 static uint32_t strslen;
131 static struct Symbol *fwds;
133 static int bsd;
134 static int is_pic;
136 static void aout_write(void);
137 static void aout_write_relocs(struct Reloc *);
138 static void aout_write_syms(void);
139 static void aout_sect_write(struct Section *, const uint8_t *,
140 uint32_t);
141 static void aout_pad_sections(void);
142 static void aout_fixup_relocs(struct Section *);
145 * Special section numbers which are used to define special
146 * symbols, which can be used with WRT to provide PIC relocation
147 * types.
149 static int32_t aout_gotpc_sect, aout_gotoff_sect;
150 static int32_t aout_got_sect, aout_plt_sect;
151 static int32_t aout_sym_sect;
153 static void aoutg_init(void)
155 stext.data = saa_init(1L);
156 stext.head = NULL;
157 stext.tail = &stext.head;
158 sdata.data = saa_init(1L);
159 sdata.head = NULL;
160 sdata.tail = &sdata.head;
161 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
162 stext.nrelocs = sdata.nrelocs = 0;
163 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
164 stext.index = seg_alloc();
165 sdata.index = seg_alloc();
166 sbss.index = seg_alloc();
167 stext.asym = sdata.asym = sbss.asym = NULL;
168 syms = saa_init((int32_t)sizeof(struct Symbol));
169 nsyms = 0;
170 bsym = raa_init();
171 strs = saa_init(1L);
172 strslen = 0;
173 fwds = NULL;
176 #ifdef OF_AOUT
178 static void aout_init(void)
180 bsd = false;
181 aoutg_init();
183 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
184 aout_plt_sect = aout_sym_sect = NO_SEG;
187 #endif
189 #ifdef OF_AOUTB
191 extern struct ofmt of_aoutb;
193 static void aoutb_init(void)
195 bsd = true;
196 aoutg_init();
198 is_pic = 0x00; /* may become 0x40 */
200 aout_gotpc_sect = seg_alloc();
201 define_label("..gotpc", aout_gotpc_sect + 1, 0L, NULL, false, false);
202 aout_gotoff_sect = seg_alloc();
203 define_label("..gotoff", aout_gotoff_sect + 1, 0L, NULL, false, false);
204 aout_got_sect = seg_alloc();
205 define_label("..got", aout_got_sect + 1, 0L, NULL, false, false);
206 aout_plt_sect = seg_alloc();
207 define_label("..plt", aout_plt_sect + 1, 0L, NULL, false, false);
208 aout_sym_sect = seg_alloc();
209 define_label("..sym", aout_sym_sect + 1, 0L, NULL, false, false);
212 #endif
214 static void aout_cleanup(int debuginfo)
216 struct Reloc *r;
218 (void)debuginfo;
220 aout_pad_sections();
221 aout_fixup_relocs(&stext);
222 aout_fixup_relocs(&sdata);
223 aout_write();
224 saa_free(stext.data);
225 while (stext.head) {
226 r = stext.head;
227 stext.head = stext.head->next;
228 nasm_free(r);
230 saa_free(sdata.data);
231 while (sdata.head) {
232 r = sdata.head;
233 sdata.head = sdata.head->next;
234 nasm_free(r);
236 saa_free(syms);
237 raa_free(bsym);
238 saa_free(strs);
241 static int32_t aout_section_names(char *name, int pass, int *bits)
244 (void)pass;
247 * Default to 32 bits.
249 if (!name)
250 *bits = 32;
252 if (!name)
253 return stext.index;
255 if (!strcmp(name, ".text"))
256 return stext.index;
257 else if (!strcmp(name, ".data"))
258 return sdata.index;
259 else if (!strcmp(name, ".bss"))
260 return sbss.index;
261 else
262 return NO_SEG;
265 static void aout_deflabel(char *name, int32_t segment, int64_t offset,
266 int is_global, char *special)
268 int pos = strslen + 4;
269 struct Symbol *sym;
270 int special_used = false;
272 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
274 * This is a NASM special symbol. We never allow it into
275 * the a.out symbol table, even if it's a valid one. If it
276 * _isn't_ a valid one, we should barf immediately.
278 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
279 strcmp(name, "..got") && strcmp(name, "..plt") &&
280 strcmp(name, "..sym"))
281 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
282 return;
285 if (is_global == 3) {
286 struct Symbol **s;
288 * Fix up a forward-reference symbol size from the first
289 * pass.
291 for (s = &fwds; *s; s = &(*s)->nextfwd)
292 if (!strcmp((*s)->name, name)) {
293 struct tokenval tokval;
294 expr *e;
295 char *p = special;
297 p = nasm_skip_spaces(nasm_skip_word(p));
298 stdscan_reset();
299 stdscan_set(p);
300 tokval.t_type = TOKEN_INVALID;
301 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
302 if (e) {
303 if (!is_simple(e))
304 nasm_error(ERR_NONFATAL, "cannot use relocatable"
305 " expression as symbol size");
306 else
307 (*s)->size = reloc_value(e);
311 * Remove it from the list of unresolved sizes.
313 nasm_free((*s)->name);
314 *s = (*s)->nextfwd;
315 return;
317 return; /* it wasn't an important one */
320 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
321 strslen += 1 + strlen(name);
323 sym = saa_wstruct(syms);
325 sym->strpos = pos;
326 sym->type = is_global ? SYM_GLOBAL : 0;
327 sym->segment = segment;
328 if (segment == NO_SEG)
329 sym->type |= SECT_ABS;
330 else if (segment == stext.index) {
331 sym->type |= SECT_TEXT;
332 if (is_global) {
333 sym->next = stext.gsyms;
334 stext.gsyms = sym;
335 } else if (!stext.asym)
336 stext.asym = sym;
337 } else if (segment == sdata.index) {
338 sym->type |= SECT_DATA;
339 if (is_global) {
340 sym->next = sdata.gsyms;
341 sdata.gsyms = sym;
342 } else if (!sdata.asym)
343 sdata.asym = sym;
344 } else if (segment == sbss.index) {
345 sym->type |= SECT_BSS;
346 if (is_global) {
347 sym->next = sbss.gsyms;
348 sbss.gsyms = sym;
349 } else if (!sbss.asym)
350 sbss.asym = sym;
351 } else
352 sym->type = SYM_GLOBAL;
353 if (is_global == 2)
354 sym->value = offset;
355 else
356 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
358 if (is_global && sym->type != SYM_GLOBAL) {
360 * Global symbol exported _from_ this module. We must check
361 * the special text for type information.
364 if (special) {
365 int n = strcspn(special, " ");
367 if (!nasm_strnicmp(special, "function", n))
368 sym->type |= SYM_FUNCTION;
369 else if (!nasm_strnicmp(special, "data", n) ||
370 !nasm_strnicmp(special, "object", n))
371 sym->type |= SYM_DATA;
372 else
373 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
374 n, special);
375 if (special[n]) {
376 struct tokenval tokval;
377 expr *e;
378 int fwd = false;
379 char *saveme = stdscan_get();
381 if (!bsd) {
382 nasm_error(ERR_NONFATAL, "Linux a.out does not support"
383 " symbol size information");
384 } else {
385 while (special[n] && nasm_isspace(special[n]))
386 n++;
388 * We have a size expression; attempt to
389 * evaluate it.
391 sym->type |= SYM_WITH_SIZE;
392 stdscan_reset();
393 stdscan_set(special + n);
394 tokval.t_type = TOKEN_INVALID;
395 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
396 NULL);
397 if (fwd) {
398 sym->nextfwd = fwds;
399 fwds = sym;
400 sym->name = nasm_strdup(name);
401 } else if (e) {
402 if (!is_simple(e))
403 nasm_error(ERR_NONFATAL, "cannot use relocatable"
404 " expression as symbol size");
405 else
406 sym->size = reloc_value(e);
409 stdscan_set(saveme);
411 special_used = true;
416 * define the references from external-symbol segment numbers
417 * to these symbol records.
419 if (segment != NO_SEG && segment != stext.index &&
420 segment != sdata.index && segment != sbss.index)
421 bsym = raa_write(bsym, segment, nsyms);
422 sym->symnum = nsyms;
424 nsyms++;
425 if (sym->type & SYM_WITH_SIZE)
426 nsyms++; /* and another for the size */
428 if (special && !special_used)
429 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
432 static void aout_add_reloc(struct Section *sect, int32_t segment,
433 int reltype, int bytes)
435 struct Reloc *r;
437 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
438 sect->tail = &r->next;
439 r->next = NULL;
441 r->address = sect->len;
442 r->symbol = (segment == NO_SEG ? -SECT_ABS :
443 segment == stext.index ? -SECT_TEXT :
444 segment == sdata.index ? -SECT_DATA :
445 segment == sbss.index ? -SECT_BSS :
446 raa_read(bsym, segment));
447 r->reltype = reltype;
448 if (r->symbol >= 0)
449 r->reltype |= RELTYPE_SYMFLAG;
450 r->bytes = bytes;
452 sect->nrelocs++;
456 * This routine deals with ..got and ..sym relocations: the more
457 * complicated kinds. In shared-library writing, some relocations
458 * with respect to global symbols must refer to the precise symbol
459 * rather than referring to an offset from the base of the section
460 * _containing_ the symbol. Such relocations call to this routine,
461 * which searches the symbol list for the symbol in question.
463 * RELTYPE_GOT references require the _exact_ symbol address to be
464 * used; RELTYPE_ABSOLUTE references can be at an offset from the
465 * symbol. The boolean argument `exact' tells us this.
467 * Return value is the adjusted value of `addr', having become an
468 * offset from the symbol rather than the section. Should always be
469 * zero when returning from an exact call.
471 * Limitation: if you define two symbols at the same place,
472 * confusion will occur.
474 * Inefficiency: we search, currently, using a linked list which
475 * isn't even necessarily sorted.
477 static int32_t aout_add_gsym_reloc(struct Section *sect,
478 int32_t segment, int32_t offset,
479 int type, int bytes, int exact)
481 struct Symbol *sym, *sm, *shead;
482 struct Reloc *r;
485 * First look up the segment to find whether it's text, data,
486 * bss or an external symbol.
488 shead = NULL;
489 if (segment == stext.index)
490 shead = stext.gsyms;
491 else if (segment == sdata.index)
492 shead = sdata.gsyms;
493 else if (segment == sbss.index)
494 shead = sbss.gsyms;
495 if (!shead) {
496 if (exact && offset != 0)
497 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
498 " for this reference");
499 else
500 aout_add_reloc(sect, segment, type, bytes);
501 return offset;
504 if (exact) {
506 * Find a symbol pointing _exactly_ at this one.
508 list_for_each(sym, shead)
509 if (sym->value == offset)
510 break;
511 } else {
513 * Find the nearest symbol below this one.
515 sym = NULL;
516 list_for_each(sm, shead)
517 if (sm->value <= offset && (!sym || sm->value > sym->value))
518 sym = sm;
520 if (!sym && exact) {
521 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
522 " for this reference");
523 return 0;
526 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
527 sect->tail = &r->next;
528 r->next = NULL;
530 r->address = sect->len;
531 r->symbol = sym->symnum;
532 r->reltype = type | RELTYPE_SYMFLAG;
533 r->bytes = bytes;
535 sect->nrelocs++;
537 return offset - sym->value;
541 * This routine deals with ..gotoff relocations. These _must_ refer
542 * to a symbol, due to a perversity of *BSD's PIC implementation,
543 * and it must be a non-global one as well; so we store `asym', the
544 * first nonglobal symbol defined in each section, and always work
545 * from that. Relocation type is always RELTYPE_GOTOFF.
547 * Return value is the adjusted value of `addr', having become an
548 * offset from the `asym' symbol rather than the section.
550 static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
551 int32_t offset, int bytes)
553 struct Reloc *r;
554 struct Symbol *asym;
557 * First look up the segment to find whether it's text, data,
558 * bss or an external symbol.
560 asym = NULL;
561 if (segment == stext.index)
562 asym = stext.asym;
563 else if (segment == sdata.index)
564 asym = sdata.asym;
565 else if (segment == sbss.index)
566 asym = sbss.asym;
567 if (!asym)
568 nasm_error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
569 " symbol in the section");
571 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
572 sect->tail = &r->next;
573 r->next = NULL;
575 r->address = sect->len;
576 r->symbol = asym->symnum;
577 r->reltype = RELTYPE_GOTOFF;
578 r->bytes = bytes;
580 sect->nrelocs++;
582 return offset - asym->value;
585 static void aout_out(int32_t segto, const void *data,
586 enum out_type type, uint64_t size,
587 int32_t segment, int32_t wrt)
589 struct Section *s;
590 int32_t addr;
591 uint8_t mydata[4], *p;
594 * handle absolute-assembly (structure definitions)
596 if (segto == NO_SEG) {
597 if (type != OUT_RESERVE)
598 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
599 " space");
600 return;
603 if (segto == stext.index)
604 s = &stext;
605 else if (segto == sdata.index)
606 s = &sdata;
607 else if (segto == sbss.index)
608 s = NULL;
609 else {
610 nasm_error(ERR_WARNING, "attempt to assemble code in"
611 " segment %d: defaulting to `.text'", segto);
612 s = &stext;
615 if (!s && type != OUT_RESERVE) {
616 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
617 " BSS section: ignored");
618 sbss.len += realsize(type, size);
619 return;
622 memset(mydata, 0, sizeof(mydata));
624 if (type == OUT_RESERVE) {
625 if (s) {
626 nasm_error(ERR_WARNING, "uninitialized space declared in"
627 " %s section: zeroing",
628 (segto == stext.index ? "code" : "data"));
629 aout_sect_write(s, NULL, size);
630 } else
631 sbss.len += size;
632 } else if (type == OUT_RAWDATA) {
633 if (segment != NO_SEG)
634 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
635 aout_sect_write(s, data, size);
636 } else if (type == OUT_ADDRESS) {
637 int asize = abs(size);
638 addr = *(int64_t *)data;
639 if (segment != NO_SEG) {
640 if (segment % 2) {
641 nasm_error(ERR_NONFATAL, "a.out format does not support"
642 " segment base references");
643 } else {
644 if (wrt == NO_SEG) {
645 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE, asize);
646 } else if (!bsd) {
647 nasm_error(ERR_NONFATAL,
648 "Linux a.out format does not support"
649 " any use of WRT");
650 wrt = NO_SEG; /* we can at least _try_ to continue */
651 } else if (wrt == aout_gotpc_sect + 1) {
652 is_pic = 0x40;
653 aout_add_reloc(s, segment, RELTYPE_GOTPC, asize);
654 } else if (wrt == aout_gotoff_sect + 1) {
655 is_pic = 0x40;
656 addr = aout_add_gotoff_reloc(s, segment, addr, asize);
657 } else if (wrt == aout_got_sect + 1) {
658 is_pic = 0x40;
659 addr = aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
660 asize, true);
661 } else if (wrt == aout_sym_sect + 1) {
662 addr = aout_add_gsym_reloc(s, segment, addr,
663 RELTYPE_ABSOLUTE, asize,
664 false);
665 } else if (wrt == aout_plt_sect + 1) {
666 is_pic = 0x40;
667 nasm_error(ERR_NONFATAL,
668 "a.out format cannot produce non-PC-"
669 "relative PLT references");
670 } else {
671 nasm_error(ERR_NONFATAL,
672 "a.out format does not support this"
673 " use of WRT");
674 wrt = NO_SEG; /* we can at least _try_ to continue */
678 p = mydata;
679 if (asize == 2)
680 WRITESHORT(p, addr);
681 else
682 WRITELONG(p, addr);
683 aout_sect_write(s, mydata, asize);
684 } else if (type == OUT_REL2ADR) {
685 if (segment == segto)
686 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
687 if (segment != NO_SEG && segment % 2) {
688 nasm_error(ERR_NONFATAL, "a.out format does not support"
689 " segment base references");
690 } else {
691 if (wrt == NO_SEG) {
692 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
693 } else if (!bsd) {
694 nasm_error(ERR_NONFATAL, "Linux a.out format does not support"
695 " any use of WRT");
696 wrt = NO_SEG; /* we can at least _try_ to continue */
697 } else if (wrt == aout_plt_sect + 1) {
698 is_pic = 0x40;
699 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
700 } else if (wrt == aout_gotpc_sect + 1 ||
701 wrt == aout_gotoff_sect + 1 ||
702 wrt == aout_got_sect + 1) {
703 nasm_error(ERR_NONFATAL, "a.out format cannot produce PC-"
704 "relative GOT references");
705 } else {
706 nasm_error(ERR_NONFATAL, "a.out format does not support this"
707 " use of WRT");
708 wrt = NO_SEG; /* we can at least _try_ to continue */
711 p = mydata;
712 WRITESHORT(p, *(int64_t *)data - (size + s->len));
713 aout_sect_write(s, mydata, 2L);
714 } else if (type == OUT_REL4ADR) {
715 if (segment == segto)
716 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
717 if (segment != NO_SEG && segment % 2) {
718 nasm_error(ERR_NONFATAL, "a.out format does not support"
719 " segment base references");
720 } else {
721 if (wrt == NO_SEG) {
722 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
723 } else if (!bsd) {
724 nasm_error(ERR_NONFATAL, "Linux a.out format does not support"
725 " any use of WRT");
726 wrt = NO_SEG; /* we can at least _try_ to continue */
727 } else if (wrt == aout_plt_sect + 1) {
728 is_pic = 0x40;
729 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
730 } else if (wrt == aout_gotpc_sect + 1 ||
731 wrt == aout_gotoff_sect + 1 ||
732 wrt == aout_got_sect + 1) {
733 nasm_error(ERR_NONFATAL, "a.out format cannot produce PC-"
734 "relative GOT references");
735 } else {
736 nasm_error(ERR_NONFATAL, "a.out format does not support this"
737 " use of WRT");
738 wrt = NO_SEG; /* we can at least _try_ to continue */
741 p = mydata;
742 WRITELONG(p, *(int64_t *)data - (size + s->len));
743 aout_sect_write(s, mydata, 4L);
747 static void aout_pad_sections(void)
749 static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
751 * Pad each of the text and data sections with NOPs until their
752 * length is a multiple of four. (NOP == 0x90.) Also increase
753 * the length of the BSS section similarly.
755 aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
756 aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
757 sbss.len = ALIGN(sbss.len, 4);
761 * a.out files have the curious property that all references to
762 * things in the data or bss sections are done by addresses which
763 * are actually relative to the start of the _text_ section, in the
764 * _file_. (No relation to what happens after linking. No idea why
765 * this should be so. It's very strange.) So we have to go through
766 * the relocation table, _after_ the final size of each section is
767 * known, and fix up the relocations pointed to.
769 static void aout_fixup_relocs(struct Section *sect)
771 struct Reloc *r;
773 saa_rewind(sect->data);
774 list_for_each(r, sect->head) {
775 uint8_t *p, *q, blk[4];
776 int32_t l;
778 saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
779 p = q = blk;
780 l = *p++;
781 if (r->bytes > 1) {
782 l += ((int32_t)*p++) << 8;
783 if (r->bytes == 4) {
784 l += ((int32_t)*p++) << 16;
785 l += ((int32_t)*p++) << 24;
788 if (r->symbol == -SECT_DATA)
789 l += stext.len;
790 else if (r->symbol == -SECT_BSS)
791 l += stext.len + sdata.len;
792 if (r->bytes == 4)
793 WRITELONG(q, l);
794 else if (r->bytes == 2)
795 WRITESHORT(q, l);
796 else
797 *q++ = l & 0xFF;
798 saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
802 static void aout_write(void)
805 * Emit the a.out header.
807 /* OMAGIC, M_386 or MID_I386, no flags */
808 fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, ofile);
809 fwriteint32_t(stext.len, ofile);
810 fwriteint32_t(sdata.len, ofile);
811 fwriteint32_t(sbss.len, ofile);
812 fwriteint32_t(nsyms * 12, ofile); /* length of symbol table */
813 fwriteint32_t(0L, ofile); /* object files have no entry point */
814 fwriteint32_t(stext.nrelocs * 8, ofile); /* size of text relocs */
815 fwriteint32_t(sdata.nrelocs * 8, ofile); /* size of data relocs */
818 * Write out the code section and the data section.
820 saa_fpwrite(stext.data, ofile);
821 saa_fpwrite(sdata.data, ofile);
824 * Write out the relocations.
826 aout_write_relocs(stext.head);
827 aout_write_relocs(sdata.head);
830 * Write the symbol table.
832 aout_write_syms();
835 * And the string table.
837 fwriteint32_t(strslen + 4, ofile); /* length includes length count */
838 saa_fpwrite(strs, ofile);
841 static void aout_write_relocs(struct Reloc *r)
843 list_for_each(r, r) {
844 uint32_t word2;
846 fwriteint32_t(r->address, ofile);
848 if (r->symbol >= 0)
849 word2 = r->symbol;
850 else
851 word2 = -r->symbol;
852 word2 |= r->reltype << 24;
853 word2 |= (r->bytes == 1 ? 0 :
854 r->bytes == 2 ? 0x2000000L : 0x4000000L);
855 fwriteint32_t(word2, ofile);
859 static void aout_write_syms(void)
861 uint32_t i;
863 saa_rewind(syms);
864 for (i = 0; i < nsyms; i++) {
865 struct Symbol *sym = saa_rstruct(syms);
866 fwriteint32_t(sym->strpos, ofile);
867 fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, ofile);
869 * Fix up the symbol value now we know the final section
870 * sizes.
872 if ((sym->type & SECT_MASK) == SECT_DATA)
873 sym->value += stext.len;
874 if ((sym->type & SECT_MASK) == SECT_BSS)
875 sym->value += stext.len + sdata.len;
876 fwriteint32_t(sym->value, ofile);
878 * Output a size record if necessary.
880 if (sym->type & SYM_WITH_SIZE) {
881 fwriteint32_t(sym->strpos, ofile);
882 fwriteint32_t(0x0DL, ofile); /* special value: means size */
883 fwriteint32_t(sym->size, ofile);
884 i++; /* use up another of `nsyms' */
889 static void aout_sect_write(struct Section *sect,
890 const uint8_t *data, uint32_t len)
892 saa_wbytes(sect->data, data, len);
893 sect->len += len;
896 static int32_t aout_segbase(int32_t segment)
898 return segment;
901 static void aout_filename(char *inname, char *outname)
903 standard_extension(inname, outname, ".o");
906 extern macros_t aout_stdmac[];
908 #endif /* OF_AOUT || OF_AOUTB */
910 #ifdef OF_AOUT
912 struct ofmt of_aout = {
913 "Linux a.out object files",
914 "aout",
916 null_debug_arr,
917 &null_debug_form,
918 aout_stdmac,
919 aout_init,
920 null_setinfo,
921 aout_out,
922 aout_deflabel,
923 aout_section_names,
924 null_sectalign,
925 aout_segbase,
926 null_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",
939 null_debug_arr,
940 &null_debug_form,
941 aout_stdmac,
942 aoutb_init,
943 null_setinfo,
944 aout_out,
945 aout_deflabel,
946 aout_section_names,
947 null_sectalign,
948 aout_segbase,
949 null_directive,
950 aout_filename,
951 aout_cleanup
954 #endif