1 /* ----------------------------------------------------------------------- *
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
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 * ----------------------------------------------------------------------- */
35 * outaout.c output routines for the Netwide Assembler to produce
36 * Linux a.out object files
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
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 */
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
114 uint32_t len
, size
, nrelocs
;
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
;
134 static evalfunc evaluate
;
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 *,
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
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
,
162 (void)ldef
; /* placate optimisers */
163 stext
.data
= saa_init(1L);
165 stext
.tail
= &stext
.head
;
166 sdata
.data
= saa_init(1L);
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
));
186 static void aout_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
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
;
199 extern struct ofmt of_aoutb
;
201 static void aoutb_init(FILE * fp
, efunc errfunc
, ldfunc ldef
,
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
,
212 aout_gotoff_sect
= seg_alloc();
213 ldef("..gotoff", aout_gotoff_sect
+ 1, 0L, NULL
, false, false,
215 aout_got_sect
= seg_alloc();
216 ldef("..got", aout_got_sect
+ 1, 0L, NULL
, false, false, &of_aoutb
,
218 aout_plt_sect
= seg_alloc();
219 ldef("..plt", aout_plt_sect
+ 1, 0L, NULL
, false, false, &of_aoutb
,
221 aout_sym_sect
= seg_alloc();
222 ldef("..sym", aout_sym_sect
+ 1, 0L, NULL
, false, false, &of_aoutb
,
228 static void aout_cleanup(int debuginfo
)
235 aout_fixup_relocs(&stext
);
236 aout_fixup_relocs(&sdata
);
239 saa_free(stext
.data
);
242 stext
.head
= stext
.head
->next
;
245 saa_free(sdata
.data
);
248 sdata
.head
= sdata
.head
->next
;
256 static int32_t aout_section_names(char *name
, int pass
, int *bits
)
262 * Default to 32 bits.
270 if (!strcmp(name
, ".text"))
272 else if (!strcmp(name
, ".data"))
274 else if (!strcmp(name
, ".bss"))
280 static void aout_deflabel(char *name
, int32_t segment
, int64_t offset
,
281 int is_global
, char *special
)
283 int pos
= strslen
+ 4;
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
);
300 if (is_global
== 3) {
303 * Fix up a forward-reference symbol size from the first
306 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
307 if (!strcmp((*s
)->name
, name
)) {
308 struct tokenval tokval
;
312 while (*p
&& !nasm_isspace(*p
))
314 while (*p
&& nasm_isspace(*p
))
318 tokval
.t_type
= TOKEN_INVALID
;
319 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
322 error(ERR_NONFATAL
, "cannot use relocatable"
323 " expression as symbol size");
325 (*s
)->size
= reloc_value(e
);
329 * Remove it from the list of unresolved sizes.
331 nasm_free((*s
)->name
);
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
);
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
;
351 sym
->next
= stext
.gsyms
;
353 } else if (!stext
.asym
)
355 } else if (segment
== sdata
.index
) {
356 sym
->type
|= SECT_DATA
;
358 sym
->next
= sdata
.gsyms
;
360 } else if (!sdata
.asym
)
362 } else if (segment
== sbss
.index
) {
363 sym
->type
|= SECT_BSS
;
365 sym
->next
= sbss
.gsyms
;
367 } else if (!sbss
.asym
)
370 sym
->type
= SYM_GLOBAL
;
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.
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
;
391 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
394 struct tokenval tokval
;
397 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
400 error(ERR_NONFATAL
, "Linux a.out does not support"
401 " symbol size information");
403 while (special
[n
] && nasm_isspace(special
[n
]))
406 * We have a size expression; attempt to
409 sym
->type
|= SYM_WITH_SIZE
;
411 stdscan_bufptr
= special
+ n
;
412 tokval
.t_type
= TOKEN_INVALID
;
413 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
418 sym
->name
= nasm_strdup(name
);
421 error(ERR_NONFATAL
, "cannot use relocatable"
422 " expression as symbol size");
424 sym
->size
= reloc_value(e
);
427 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
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
);
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
)
455 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
456 sect
->tail
= &r
->next
;
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
;
467 r
->reltype
|= RELTYPE_SYMFLAG
;
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
;
503 * First look up the segment to find whether it's text, data,
504 * bss or an external symbol.
507 if (segment
== stext
.index
)
509 else if (segment
== sdata
.index
)
511 else if (segment
== sbss
.index
)
514 if (exact
&& offset
!= 0)
515 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
516 " for this reference");
518 aout_add_reloc(sect
, segment
, type
, bytes
);
524 * Find a symbol pointing _exactly_ at this one.
526 for (sym
= shead
; sym
; sym
= sym
->next
)
527 if (sym
->value
== offset
)
531 * Find the nearest symbol below this one.
534 for (sm
= shead
; sm
; sm
= sm
->next
)
535 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
539 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
540 " for this reference");
544 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
545 sect
->tail
= &r
->next
;
548 r
->address
= sect
->len
;
549 r
->symbol
= sym
->symnum
;
550 r
->reltype
= type
| RELTYPE_SYMFLAG
;
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
)
575 * First look up the segment to find whether it's text, data,
576 * bss or an external symbol.
579 if (segment
== stext
.index
)
581 else if (segment
== sdata
.index
)
583 else if (segment
== sbss
.index
)
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
;
593 r
->address
= sect
->len
;
594 r
->symbol
= asym
->symnum
;
595 r
->reltype
= RELTYPE_GOTOFF
;
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
)
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]"
621 if (segto
== stext
.index
)
623 else if (segto
== sdata
.index
)
625 else if (segto
== sbss
.index
)
628 error(ERR_WARNING
, "attempt to assemble code in"
629 " segment %d: defaulting to `.text'", segto
);
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
);
640 if (type
== OUT_RESERVE
) {
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
);
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
) {
656 error(ERR_NONFATAL
, "a.out format does not support"
657 " segment base references");
660 aout_add_reloc(s
, segment
, RELTYPE_ABSOLUTE
,
664 "Linux a.out format does not support"
666 wrt
= NO_SEG
; /* we can at least _try_ to continue */
667 } else if (wrt
== aout_gotpc_sect
+ 1) {
669 aout_add_reloc(s
, segment
, RELTYPE_GOTPC
, size
);
670 } else if (wrt
== aout_gotoff_sect
+ 1) {
672 addr
= aout_add_gotoff_reloc(s
, segment
,
674 } else if (wrt
== aout_got_sect
+ 1) {
677 aout_add_gsym_reloc(s
, segment
, addr
, RELTYPE_GOT
,
679 } else if (wrt
== aout_sym_sect
+ 1) {
680 addr
= aout_add_gsym_reloc(s
, segment
, addr
,
681 RELTYPE_ABSOLUTE
, size
,
683 } else if (wrt
== aout_plt_sect
+ 1) {
686 "a.out format cannot produce non-PC-"
687 "relative PLT references");
690 "a.out format does not support this"
692 wrt
= NO_SEG
; /* we can at least _try_ to continue */
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");
710 aout_add_reloc(s
, segment
, RELTYPE_RELATIVE
, 2);
712 error(ERR_NONFATAL
, "Linux a.out format does not support"
714 wrt
= NO_SEG
; /* we can at least _try_ to continue */
715 } else if (wrt
== aout_plt_sect
+ 1) {
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");
724 error(ERR_NONFATAL
, "a.out format does not support this"
726 wrt
= NO_SEG
; /* we can at least _try_ to continue */
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");
740 aout_add_reloc(s
, segment
, RELTYPE_RELATIVE
, 4);
742 error(ERR_NONFATAL
, "Linux a.out format does not support"
744 wrt
= NO_SEG
; /* we can at least _try_ to continue */
745 } else if (wrt
== aout_plt_sect
+ 1) {
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");
754 error(ERR_NONFATAL
, "a.out format does not support this"
756 wrt
= NO_SEG
; /* we can at least _try_ to continue */
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
)
791 saa_rewind(sect
->data
);
792 for (r
= sect
->head
; r
; r
= r
->next
) {
793 uint8_t *p
, *q
, blk
[4];
796 saa_fread(sect
->data
, r
->address
, blk
, (int32_t)r
->bytes
);
800 l
+= ((int32_t)*p
++) << 8;
802 l
+= ((int32_t)*p
++) << 16;
803 l
+= ((int32_t)*p
++) << 24;
806 if (r
->symbol
== -SECT_DATA
)
808 else if (r
->symbol
== -SECT_BSS
)
809 l
+= stext
.len
+ sdata
.len
;
812 else if (r
->bytes
== 2)
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.
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
)
864 fwriteint32_t(r
->address
, aoutfp
);
870 word2
|= r
->reltype
<< 24;
871 word2
|= (r
->bytes
== 1 ? 0 :
872 r
->bytes
== 2 ? 0x2000000L
: 0x4000000L
);
873 fwriteint32_t(word2
, aoutfp
);
879 static void aout_write_syms(void)
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
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
);
916 static int32_t aout_segbase(int32_t segment
)
921 static int aout_directive(char *directive
, char *value
, int pass
)
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
)
942 #endif /* OF_AOUT || OF_AOUTB */
946 struct ofmt of_aout
= {
947 "Linux a.out object files",
968 struct ofmt of_aoutb
= {
969 "NetBSD/FreeBSD a.out object files",