Makefiles: run "make alldeps"
[nasm.git] / output / outaout.c
blobb353600ea88b0adc1893abccf85b081e3bb6b785
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(void)
216 struct Reloc *r;
218 aout_pad_sections();
219 aout_fixup_relocs(&stext);
220 aout_fixup_relocs(&sdata);
221 aout_write();
222 saa_free(stext.data);
223 while (stext.head) {
224 r = stext.head;
225 stext.head = stext.head->next;
226 nasm_free(r);
228 saa_free(sdata.data);
229 while (sdata.head) {
230 r = sdata.head;
231 sdata.head = sdata.head->next;
232 nasm_free(r);
234 saa_free(syms);
235 raa_free(bsym);
236 saa_free(strs);
239 static int32_t aout_section_names(char *name, int pass, int *bits)
242 (void)pass;
245 * Default to 32 bits.
247 if (!name)
248 *bits = 32;
250 if (!name)
251 return stext.index;
253 if (!strcmp(name, ".text"))
254 return stext.index;
255 else if (!strcmp(name, ".data"))
256 return sdata.index;
257 else if (!strcmp(name, ".bss"))
258 return sbss.index;
259 else
260 return NO_SEG;
263 static void aout_deflabel(char *name, int32_t segment, int64_t offset,
264 int is_global, char *special)
266 int pos = strslen + 4;
267 struct Symbol *sym;
268 int special_used = false;
270 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
272 * This is a NASM special symbol. We never allow it into
273 * the a.out symbol table, even if it's a valid one. If it
274 * _isn't_ a valid one, we should barf immediately.
276 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
277 strcmp(name, "..got") && strcmp(name, "..plt") &&
278 strcmp(name, "..sym"))
279 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
280 return;
283 if (is_global == 3) {
284 struct Symbol **s;
286 * Fix up a forward-reference symbol size from the first
287 * pass.
289 for (s = &fwds; *s; s = &(*s)->nextfwd)
290 if (!strcmp((*s)->name, name)) {
291 struct tokenval tokval;
292 expr *e;
293 char *p = special;
295 p = nasm_skip_spaces(nasm_skip_word(p));
296 stdscan_reset();
297 stdscan_set(p);
298 tokval.t_type = TOKEN_INVALID;
299 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
300 if (e) {
301 if (!is_simple(e))
302 nasm_error(ERR_NONFATAL, "cannot use relocatable"
303 " expression as symbol size");
304 else
305 (*s)->size = reloc_value(e);
309 * Remove it from the list of unresolved sizes.
311 nasm_free((*s)->name);
312 *s = (*s)->nextfwd;
313 return;
315 return; /* it wasn't an important one */
318 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
319 strslen += 1 + strlen(name);
321 sym = saa_wstruct(syms);
323 sym->strpos = pos;
324 sym->type = is_global ? SYM_GLOBAL : 0;
325 sym->segment = segment;
326 if (segment == NO_SEG)
327 sym->type |= SECT_ABS;
328 else if (segment == stext.index) {
329 sym->type |= SECT_TEXT;
330 if (is_global) {
331 sym->next = stext.gsyms;
332 stext.gsyms = sym;
333 } else if (!stext.asym)
334 stext.asym = sym;
335 } else if (segment == sdata.index) {
336 sym->type |= SECT_DATA;
337 if (is_global) {
338 sym->next = sdata.gsyms;
339 sdata.gsyms = sym;
340 } else if (!sdata.asym)
341 sdata.asym = sym;
342 } else if (segment == sbss.index) {
343 sym->type |= SECT_BSS;
344 if (is_global) {
345 sym->next = sbss.gsyms;
346 sbss.gsyms = sym;
347 } else if (!sbss.asym)
348 sbss.asym = sym;
349 } else
350 sym->type = SYM_GLOBAL;
351 if (is_global == 2)
352 sym->value = offset;
353 else
354 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
356 if (is_global && sym->type != SYM_GLOBAL) {
358 * Global symbol exported _from_ this module. We must check
359 * the special text for type information.
362 if (special) {
363 int n = strcspn(special, " ");
365 if (!nasm_strnicmp(special, "function", n))
366 sym->type |= SYM_FUNCTION;
367 else if (!nasm_strnicmp(special, "data", n) ||
368 !nasm_strnicmp(special, "object", n))
369 sym->type |= SYM_DATA;
370 else
371 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
372 n, special);
373 if (special[n]) {
374 struct tokenval tokval;
375 expr *e;
376 int fwd = false;
377 char *saveme = stdscan_get();
379 if (!bsd) {
380 nasm_error(ERR_NONFATAL, "Linux a.out does not support"
381 " symbol size information");
382 } else {
383 while (special[n] && nasm_isspace(special[n]))
384 n++;
386 * We have a size expression; attempt to
387 * evaluate it.
389 sym->type |= SYM_WITH_SIZE;
390 stdscan_reset();
391 stdscan_set(special + n);
392 tokval.t_type = TOKEN_INVALID;
393 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
394 if (fwd) {
395 sym->nextfwd = fwds;
396 fwds = sym;
397 sym->name = nasm_strdup(name);
398 } else if (e) {
399 if (!is_simple(e))
400 nasm_error(ERR_NONFATAL, "cannot use relocatable"
401 " expression as symbol size");
402 else
403 sym->size = reloc_value(e);
406 stdscan_set(saveme);
408 special_used = true;
413 * define the references from external-symbol segment numbers
414 * to these symbol records.
416 if (segment != NO_SEG && segment != stext.index &&
417 segment != sdata.index && segment != sbss.index)
418 bsym = raa_write(bsym, segment, nsyms);
419 sym->symnum = nsyms;
421 nsyms++;
422 if (sym->type & SYM_WITH_SIZE)
423 nsyms++; /* and another for the size */
425 if (special && !special_used)
426 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
429 static void aout_add_reloc(struct Section *sect, int32_t segment,
430 int reltype, int bytes)
432 struct Reloc *r;
434 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
435 sect->tail = &r->next;
436 r->next = NULL;
438 r->address = sect->len;
439 r->symbol = (segment == NO_SEG ? -SECT_ABS :
440 segment == stext.index ? -SECT_TEXT :
441 segment == sdata.index ? -SECT_DATA :
442 segment == sbss.index ? -SECT_BSS :
443 raa_read(bsym, segment));
444 r->reltype = reltype;
445 if (r->symbol >= 0)
446 r->reltype |= RELTYPE_SYMFLAG;
447 r->bytes = bytes;
449 sect->nrelocs++;
453 * This routine deals with ..got and ..sym relocations: the more
454 * complicated kinds. In shared-library writing, some relocations
455 * with respect to global symbols must refer to the precise symbol
456 * rather than referring to an offset from the base of the section
457 * _containing_ the symbol. Such relocations call to this routine,
458 * which searches the symbol list for the symbol in question.
460 * RELTYPE_GOT references require the _exact_ symbol address to be
461 * used; RELTYPE_ABSOLUTE references can be at an offset from the
462 * symbol. The boolean argument `exact' tells us this.
464 * Return value is the adjusted value of `addr', having become an
465 * offset from the symbol rather than the section. Should always be
466 * zero when returning from an exact call.
468 * Limitation: if you define two symbols at the same place,
469 * confusion will occur.
471 * Inefficiency: we search, currently, using a linked list which
472 * isn't even necessarily sorted.
474 static int32_t aout_add_gsym_reloc(struct Section *sect,
475 int32_t segment, int32_t offset,
476 int type, int bytes, int exact)
478 struct Symbol *sym, *sm, *shead;
479 struct Reloc *r;
482 * First look up the segment to find whether it's text, data,
483 * bss or an external symbol.
485 shead = NULL;
486 if (segment == stext.index)
487 shead = stext.gsyms;
488 else if (segment == sdata.index)
489 shead = sdata.gsyms;
490 else if (segment == sbss.index)
491 shead = sbss.gsyms;
492 if (!shead) {
493 if (exact && offset != 0)
494 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
495 " for this reference");
496 else
497 aout_add_reloc(sect, segment, type, bytes);
498 return offset;
501 if (exact) {
503 * Find a symbol pointing _exactly_ at this one.
505 list_for_each(sym, shead)
506 if (sym->value == offset)
507 break;
508 } else {
510 * Find the nearest symbol below this one.
512 sym = NULL;
513 list_for_each(sm, shead)
514 if (sm->value <= offset && (!sym || sm->value > sym->value))
515 sym = sm;
517 if (!sym && exact) {
518 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
519 " for this reference");
520 return 0;
523 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
524 sect->tail = &r->next;
525 r->next = NULL;
527 r->address = sect->len;
528 r->symbol = sym->symnum;
529 r->reltype = type | RELTYPE_SYMFLAG;
530 r->bytes = bytes;
532 sect->nrelocs++;
534 return offset - sym->value;
538 * This routine deals with ..gotoff relocations. These _must_ refer
539 * to a symbol, due to a perversity of *BSD's PIC implementation,
540 * and it must be a non-global one as well; so we store `asym', the
541 * first nonglobal symbol defined in each section, and always work
542 * from that. Relocation type is always RELTYPE_GOTOFF.
544 * Return value is the adjusted value of `addr', having become an
545 * offset from the `asym' symbol rather than the section.
547 static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
548 int32_t offset, int bytes)
550 struct Reloc *r;
551 struct Symbol *asym;
554 * First look up the segment to find whether it's text, data,
555 * bss or an external symbol.
557 asym = NULL;
558 if (segment == stext.index)
559 asym = stext.asym;
560 else if (segment == sdata.index)
561 asym = sdata.asym;
562 else if (segment == sbss.index)
563 asym = sbss.asym;
564 if (!asym)
565 nasm_error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
566 " symbol in the section");
568 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
569 sect->tail = &r->next;
570 r->next = NULL;
572 r->address = sect->len;
573 r->symbol = asym->symnum;
574 r->reltype = RELTYPE_GOTOFF;
575 r->bytes = bytes;
577 sect->nrelocs++;
579 return offset - asym->value;
582 static void aout_out(int32_t segto, const void *data,
583 enum out_type type, uint64_t size,
584 int32_t segment, int32_t wrt)
586 struct Section *s;
587 int32_t addr;
588 uint8_t mydata[4], *p;
591 * handle absolute-assembly (structure definitions)
593 if (segto == NO_SEG) {
594 if (type != OUT_RESERVE)
595 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
596 " space");
597 return;
600 if (segto == stext.index)
601 s = &stext;
602 else if (segto == sdata.index)
603 s = &sdata;
604 else if (segto == sbss.index)
605 s = NULL;
606 else {
607 nasm_error(ERR_WARNING, "attempt to assemble code in"
608 " segment %d: defaulting to `.text'", segto);
609 s = &stext;
612 if (!s && type != OUT_RESERVE) {
613 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
614 " BSS section: ignored");
615 sbss.len += realsize(type, size);
616 return;
619 memset(mydata, 0, sizeof(mydata));
621 if (type == OUT_RESERVE) {
622 if (s) {
623 nasm_error(ERR_WARNING, "uninitialized space declared in"
624 " %s section: zeroing",
625 (segto == stext.index ? "code" : "data"));
626 aout_sect_write(s, NULL, size);
627 } else
628 sbss.len += size;
629 } else if (type == OUT_RAWDATA) {
630 if (segment != NO_SEG)
631 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
632 aout_sect_write(s, data, size);
633 } else if (type == OUT_ADDRESS) {
634 int asize = abs((int)size);
635 addr = *(int64_t *)data;
636 if (segment != NO_SEG) {
637 if (segment % 2) {
638 nasm_error(ERR_NONFATAL, "a.out format does not support"
639 " segment base references");
640 } else {
641 if (wrt == NO_SEG) {
642 aout_add_reloc(s, segment, RELTYPE_ABSOLUTE, asize);
643 } else if (!bsd) {
644 nasm_error(ERR_NONFATAL,
645 "Linux a.out format does not support"
646 " any use of WRT");
647 wrt = NO_SEG; /* we can at least _try_ to continue */
648 } else if (wrt == aout_gotpc_sect + 1) {
649 is_pic = 0x40;
650 aout_add_reloc(s, segment, RELTYPE_GOTPC, asize);
651 } else if (wrt == aout_gotoff_sect + 1) {
652 is_pic = 0x40;
653 addr = aout_add_gotoff_reloc(s, segment, addr, asize);
654 } else if (wrt == aout_got_sect + 1) {
655 is_pic = 0x40;
656 addr = aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
657 asize, true);
658 } else if (wrt == aout_sym_sect + 1) {
659 addr = aout_add_gsym_reloc(s, segment, addr,
660 RELTYPE_ABSOLUTE, asize,
661 false);
662 } else if (wrt == aout_plt_sect + 1) {
663 is_pic = 0x40;
664 nasm_error(ERR_NONFATAL,
665 "a.out format cannot produce non-PC-"
666 "relative PLT references");
667 } else {
668 nasm_error(ERR_NONFATAL,
669 "a.out format does not support this"
670 " use of WRT");
671 wrt = NO_SEG; /* we can at least _try_ to continue */
675 p = mydata;
676 if (asize == 2)
677 WRITESHORT(p, addr);
678 else
679 WRITELONG(p, addr);
680 aout_sect_write(s, mydata, asize);
681 } else if (type == OUT_REL2ADR) {
682 if (segment == segto)
683 nasm_panic(0, "intra-segment OUT_REL2ADR");
684 if (segment != NO_SEG && segment % 2) {
685 nasm_error(ERR_NONFATAL, "a.out format does not support"
686 " segment base references");
687 } else {
688 if (wrt == NO_SEG) {
689 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
690 } else if (!bsd) {
691 nasm_error(ERR_NONFATAL, "Linux a.out format does not support"
692 " any use of WRT");
693 wrt = NO_SEG; /* we can at least _try_ to continue */
694 } else if (wrt == aout_plt_sect + 1) {
695 is_pic = 0x40;
696 aout_add_reloc(s, segment, RELTYPE_PLT, 2);
697 } else if (wrt == aout_gotpc_sect + 1 ||
698 wrt == aout_gotoff_sect + 1 ||
699 wrt == aout_got_sect + 1) {
700 nasm_error(ERR_NONFATAL, "a.out format cannot produce PC-"
701 "relative GOT references");
702 } else {
703 nasm_error(ERR_NONFATAL, "a.out format does not support this"
704 " use of WRT");
705 wrt = NO_SEG; /* we can at least _try_ to continue */
708 p = mydata;
709 WRITESHORT(p, *(int64_t *)data - (size + s->len));
710 aout_sect_write(s, mydata, 2L);
711 } else if (type == OUT_REL4ADR) {
712 if (segment == segto)
713 nasm_panic(0, "intra-segment OUT_REL4ADR");
714 if (segment != NO_SEG && segment % 2) {
715 nasm_error(ERR_NONFATAL, "a.out format does not support"
716 " segment base references");
717 } else {
718 if (wrt == NO_SEG) {
719 aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
720 } else if (!bsd) {
721 nasm_error(ERR_NONFATAL, "Linux a.out format does not support"
722 " any use of WRT");
723 wrt = NO_SEG; /* we can at least _try_ to continue */
724 } else if (wrt == aout_plt_sect + 1) {
725 is_pic = 0x40;
726 aout_add_reloc(s, segment, RELTYPE_PLT, 4);
727 } else if (wrt == aout_gotpc_sect + 1 ||
728 wrt == aout_gotoff_sect + 1 ||
729 wrt == aout_got_sect + 1) {
730 nasm_error(ERR_NONFATAL, "a.out format cannot produce PC-"
731 "relative GOT references");
732 } else {
733 nasm_error(ERR_NONFATAL, "a.out format does not support this"
734 " use of WRT");
735 wrt = NO_SEG; /* we can at least _try_ to continue */
738 p = mydata;
739 WRITELONG(p, *(int64_t *)data - (size + s->len));
740 aout_sect_write(s, mydata, 4L);
744 static void aout_pad_sections(void)
746 static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
748 * Pad each of the text and data sections with NOPs until their
749 * length is a multiple of four. (NOP == 0x90.) Also increase
750 * the length of the BSS section similarly.
752 aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
753 aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
754 sbss.len = ALIGN(sbss.len, 4);
758 * a.out files have the curious property that all references to
759 * things in the data or bss sections are done by addresses which
760 * are actually relative to the start of the _text_ section, in the
761 * _file_. (No relation to what happens after linking. No idea why
762 * this should be so. It's very strange.) So we have to go through
763 * the relocation table, _after_ the final size of each section is
764 * known, and fix up the relocations pointed to.
766 static void aout_fixup_relocs(struct Section *sect)
768 struct Reloc *r;
770 saa_rewind(sect->data);
771 list_for_each(r, sect->head) {
772 uint8_t *p, *q, blk[4];
773 int32_t l;
775 saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
776 p = q = blk;
777 l = *p++;
778 if (r->bytes > 1) {
779 l += ((int32_t)*p++) << 8;
780 if (r->bytes == 4) {
781 l += ((int32_t)*p++) << 16;
782 l += ((int32_t)*p++) << 24;
785 if (r->symbol == -SECT_DATA)
786 l += stext.len;
787 else if (r->symbol == -SECT_BSS)
788 l += stext.len + sdata.len;
789 if (r->bytes == 4)
790 WRITELONG(q, l);
791 else if (r->bytes == 2)
792 WRITESHORT(q, l);
793 else
794 *q++ = l & 0xFF;
795 saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
799 static void aout_write(void)
802 * Emit the a.out header.
804 /* OMAGIC, M_386 or MID_I386, no flags */
805 fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, ofile);
806 fwriteint32_t(stext.len, ofile);
807 fwriteint32_t(sdata.len, ofile);
808 fwriteint32_t(sbss.len, ofile);
809 fwriteint32_t(nsyms * 12, ofile); /* length of symbol table */
810 fwriteint32_t(0L, ofile); /* object files have no entry point */
811 fwriteint32_t(stext.nrelocs * 8, ofile); /* size of text relocs */
812 fwriteint32_t(sdata.nrelocs * 8, ofile); /* size of data relocs */
815 * Write out the code section and the data section.
817 saa_fpwrite(stext.data, ofile);
818 saa_fpwrite(sdata.data, ofile);
821 * Write out the relocations.
823 aout_write_relocs(stext.head);
824 aout_write_relocs(sdata.head);
827 * Write the symbol table.
829 aout_write_syms();
832 * And the string table.
834 fwriteint32_t(strslen + 4, ofile); /* length includes length count */
835 saa_fpwrite(strs, ofile);
838 static void aout_write_relocs(struct Reloc *r)
840 list_for_each(r, r) {
841 uint32_t word2;
843 fwriteint32_t(r->address, ofile);
845 if (r->symbol >= 0)
846 word2 = r->symbol;
847 else
848 word2 = -r->symbol;
849 word2 |= r->reltype << 24;
850 word2 |= (r->bytes == 1 ? 0 :
851 r->bytes == 2 ? 0x2000000L : 0x4000000L);
852 fwriteint32_t(word2, ofile);
856 static void aout_write_syms(void)
858 uint32_t i;
860 saa_rewind(syms);
861 for (i = 0; i < nsyms; i++) {
862 struct Symbol *sym = saa_rstruct(syms);
863 fwriteint32_t(sym->strpos, ofile);
864 fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, ofile);
866 * Fix up the symbol value now we know the final section
867 * sizes.
869 if ((sym->type & SECT_MASK) == SECT_DATA)
870 sym->value += stext.len;
871 if ((sym->type & SECT_MASK) == SECT_BSS)
872 sym->value += stext.len + sdata.len;
873 fwriteint32_t(sym->value, ofile);
875 * Output a size record if necessary.
877 if (sym->type & SYM_WITH_SIZE) {
878 fwriteint32_t(sym->strpos, ofile);
879 fwriteint32_t(0x0DL, ofile); /* special value: means size */
880 fwriteint32_t(sym->size, ofile);
881 i++; /* use up another of `nsyms' */
886 static void aout_sect_write(struct Section *sect,
887 const uint8_t *data, uint32_t len)
889 saa_wbytes(sect->data, data, len);
890 sect->len += len;
893 static int32_t aout_segbase(int32_t segment)
895 return segment;
898 static void aout_filename(char *inname, char *outname)
900 standard_extension(inname, outname, ".o");
903 extern macros_t aout_stdmac[];
905 #endif /* OF_AOUT || OF_AOUTB */
907 #ifdef OF_AOUT
909 struct ofmt of_aout = {
910 "Linux a.out object files",
911 "aout",
914 null_debug_arr,
915 &null_debug_form,
916 aout_stdmac,
917 aout_init,
918 null_setinfo,
919 aout_out,
920 aout_deflabel,
921 aout_section_names,
922 null_sectalign,
923 aout_segbase,
924 null_directive,
925 aout_filename,
926 aout_cleanup
929 #endif
931 #ifdef OF_AOUTB
933 struct ofmt of_aoutb = {
934 "NetBSD/FreeBSD a.out object files",
935 "aoutb",
938 null_debug_arr,
939 &null_debug_form,
940 aout_stdmac,
941 aoutb_init,
942 null_setinfo,
943 aout_out,
944 aout_deflabel,
945 aout_section_names,
946 null_sectalign,
947 aout_segbase,
948 null_directive,
949 aout_filename,
950 aout_cleanup
953 #endif