1 /* BFD backend for local host's a.out binaries */
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Diddler.
7 BFD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
12 BFD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with BFD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #include "liba.out.h" /* BFD a.out internal data structures */
29 #include "trad-core.h" /* Traditional Unix core files */
31 void (*bfd_error_trap
)();
33 static bfd_target
*host_aout_callback ();
39 host_aout_object_p (abfd
)
42 unsigned char magicbuf
[4]; /* Raw bytes of magic number from file */
43 unsigned long magic
; /* Swapped magic number */
45 bfd_error
= system_call_error
;
47 if (bfd_read ((PTR
)magicbuf
, 1, sizeof (magicbuf
), abfd
) !=
50 magic
= bfd_h_getlong (abfd
, magicbuf
);
52 if (N_BADMAG (*((struct exec
*) &magic
))) return 0;
54 return some_aout_object_p (abfd
, host_aout_callback
);
57 /* Set parameters about this a.out file that are machine-dependent.
58 This routine is called from some_aout_object_p just before it returns. */
61 host_aout_callback (abfd
)
64 struct exec
*execp
= exec_hdr (abfd
);
66 /* The virtual memory addresses of the sections */
67 obj_datasec (abfd
)->vma
= N_DATADDR(*execp
);
68 obj_bsssec (abfd
)->vma
= N_BSSADDR(*execp
);
69 obj_textsec (abfd
)->vma
= N_TXTADDR(*execp
);
71 /* The file offsets of the sections */
72 obj_textsec (abfd
)->filepos
= N_TXTOFF(*execp
);
73 obj_datasec (abfd
)->filepos
= N_DATOFF(*execp
);
75 /* The file offsets of the relocation info */
76 obj_textsec (abfd
)->rel_filepos
= N_TRELOFF(*execp
);
77 obj_datasec (abfd
)->rel_filepos
= N_DRELOFF(*execp
);
79 /* The file offsets of the string table and symbol table. */
80 obj_str_filepos (abfd
) = N_STROFF (*execp
);
81 obj_sym_filepos (abfd
) = N_SYMOFF (*execp
);
83 #ifdef HOST_MACHINE_ARCH
84 abfd
->obj_arch
= HOST_MACHINE_ARCH
;
86 #ifdef HOST_MACHINE_MACHINE
87 abfd
->obj_machine
= HOST_MACHINE_MACHINE
;
95 host_aout_mkobject (abfd
)
100 bfd_error
= system_call_error
;
102 /* Use an intermediate variable for clarity */
103 rawptr
= bfd_zalloc (abfd
, sizeof (struct aoutdata
) + sizeof (struct exec
));
105 if (rawptr
== NULL
) {
106 bfd_error
= no_memory
;
110 set_tdata (abfd
, (struct aoutdata
*) rawptr
);
111 exec_hdr (abfd
) = (struct exec
*) (rawptr
+ sizeof (struct aoutdata
));
113 /* For simplicity's sake we just make all the sections right here. */
115 obj_textsec (abfd
) = (asection
*)NULL
;
116 obj_datasec (abfd
) = (asection
*)NULL
;
117 obj_bsssec (abfd
) = (asection
*)NULL
;
118 bfd_make_section (abfd
, ".text");
119 bfd_make_section (abfd
, ".data");
120 bfd_make_section (abfd
, ".bss");
125 /* Write an object file in host a.out format.
126 Section contents have already been written. We write the
127 file header, symbols, and relocation. */
130 host_aout_write_object_contents (abfd
)
133 /* This works because we are on the host system */
134 #define EXEC_BYTES_SIZE sizeof (struct exec)
136 unsigned char exec_bytes
[EXEC_BYTES_SIZE
];
137 struct exec
*execp
= exec_hdr (abfd
);
139 execp
->a_text
= obj_textsec (abfd
)->size
;
142 N_SET_MAGIC (*execp
, OMAGIC
);
143 if (abfd
->flags
& D_PAGED
) {
144 /* This is not strictly true, but will probably do for the default
148 execp
->a_text
= obj_textsec (abfd
)->size
+ EXEC_BYTES_SIZE
;
149 N_SET_MAGIC (*execp
, ZMAGIC
);
150 } else if (abfd
->flags
& WP_TEXT
) {
151 N_SET_MAGIC (*execp
, NMAGIC
);
153 N_SET_FLAGS (*execp
, 0x1); /* copied from ld.c; who the hell knows? */
155 if (abfd
->flags
& D_PAGED
)
157 data_pad
= ((obj_datasec(abfd
)->size
+ PAGE_SIZE
-1)
158 & (- PAGE_SIZE
)) - obj_datasec(abfd
)->size
;
160 if (data_pad
> obj_bsssec(abfd
)->size
)
163 execp
->a_bss
= obj_bsssec(abfd
)->size
- data_pad
;
164 execp
->a_data
= obj_datasec(abfd
)->size
+ data_pad
;
168 execp
->a_data
= obj_datasec (abfd
)->size
;
169 execp
->a_bss
= obj_bsssec (abfd
)->size
;
172 execp
->a_syms
= bfd_get_symcount (abfd
) * sizeof (struct nlist
);
173 execp
->a_entry
= bfd_get_start_address (abfd
);
175 execp
->a_trsize
= ((obj_textsec (abfd
)->reloc_count
) *
176 obj_reloc_entry_size (abfd
));
178 execp
->a_drsize
= ((obj_datasec (abfd
)->reloc_count
) *
179 obj_reloc_entry_size (abfd
));
181 bfd_aout_swap_exec_header_out (abfd
, execp
, exec_bytes
);
183 bfd_seek (abfd
, 0L, false);
184 bfd_write ((PTR
) exec_bytes
, 1, EXEC_BYTES_SIZE
, abfd
);
186 /* Now write out reloc info, followed by syms and strings */
188 if (bfd_get_symcount (abfd
) != 0)
191 (long)(N_SYMOFF(*execp
)), false);
193 aout_write_syms (abfd
);
195 bfd_seek (abfd
, (long)(N_TRELOFF(*execp
)), false);
197 if (!aout_squirt_out_relocs (abfd
, obj_textsec (abfd
))) return false;
198 bfd_seek (abfd
, (long)(N_DRELOFF(*execp
)), false);
200 if (!aout_squirt_out_relocs (abfd
, obj_datasec (abfd
))) return false;
205 /* We use BFD generic archive files. */
206 #define aout_openr_next_archived_file bfd_generic_openr_next_archived_file
207 #define aout_generic_stat_arch_elt bfd_generic_stat_arch_elt
208 #define aout_slurp_armap bfd_slurp_bsd_armap
209 #define aout_slurp_extended_name_table bfd_true
210 #define aout_write_armap bsd_write_armap
211 #define aout_truncate_arname bfd_bsd_truncate_arname
213 /* We use traditional Unix core file format. */
214 #define aout_core_file_failing_command trad_unix_core_file_failing_command
215 #define aout_core_file_failing_signal trad_unix_core_file_failing_signal
216 #define aout_core_file_matches_executable_p \
217 trad_unix_core_file_matches_executable_p
219 /* We replace this function */
220 #define aout_write_object_contents host_aout_write_object_contents
222 bfd_target host_aout_big_vec
=
224 "a.out-host-big", /* name */
225 bfd_target_aout_flavour_enum
,
226 true, /* target byte order */
227 true, /* target headers byte order */
228 (HAS_RELOC
| EXEC_P
| /* object flags */
229 HAS_LINENO
| HAS_DEBUG
|
230 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
| D_PAGED
),
231 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
232 ' ', /* ar_pad_char */
233 16, /* ar_max_namelen */
234 _do_getblong
, _do_putblong
, _do_getbshort
, _do_putbshort
, /* data */
235 _do_getblong
, _do_putblong
, _do_getbshort
, _do_putbshort
, /* hdrs */
237 {_bfd_dummy_target
, host_aout_object_p
,
238 bfd_generic_archive_p
, trad_unix_core_file_p
},
239 {bfd_false
, host_aout_mkobject
,
240 _bfd_generic_mkarchive
, bfd_false
},
241 {bfd_false
, aout_write_object_contents
, /* bfd_write_contents */
242 _bfd_write_archive_contents
, bfd_false
},
247 bfd_target host_aout_little_vec
=
249 "a.out-host-little", /* name */
250 bfd_target_aout_flavour_enum
,
251 false, /* target byte order */
252 false, /* target headers byte order */
253 (HAS_RELOC
| EXEC_P
| /* object flags */
254 HAS_LINENO
| HAS_DEBUG
|
255 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
| D_PAGED
),
256 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
257 ' ', /* ar_pad_char */
258 16, /* ar_max_namelen */
259 _do_getllong
, _do_putllong
, _do_getlshort
, _do_putlshort
, /* data */
260 _do_getllong
, _do_putllong
, _do_getlshort
, _do_putlshort
, /* hdrs */
262 {_bfd_dummy_target
, host_aout_object_p
,
263 bfd_generic_archive_p
, trad_unix_core_file_p
},
264 {bfd_false
, host_aout_mkobject
,
265 _bfd_generic_mkarchive
, bfd_false
},
266 {bfd_false
, aout_write_object_contents
, /* bfd_write_contents */
267 _bfd_write_archive_contents
, bfd_false
},