Commit | Line | Data |
---|---|---|
2c3f93e5 JG |
1 | /* BFD back-end for a.out files encapsulated with COFF headers. |
2 | Copyright (C) 1990-1991 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | /* THIS MODULE IS NOT FINISHED. IT PROBABLY DOESN'T EVEN COMPILE. */ | |
21 | ||
22 | #if 0 | |
23 | #define PAGE_SIZE 4096 | |
24 | #define SEGMENT_SIZE PAGE_SIZE | |
25 | #define TEXT_START_ADDR 0 | |
26 | #define ARCH 32 | |
27 | #define BYTES_IN_WORD 4 | |
28 | #endif | |
29 | ||
30 | #include "bfd.h" | |
31 | #include <sysdep.h> | |
32 | #include "libbfd.h" | |
33 | #include "aout64.h" | |
34 | ||
35 | /**From: bothner@cs.wisc.edu***********************************************/ | |
36 | #undef N_TXTOFF | |
37 | #define N_TXTOFF(x) ( (N_MAGIC((x)) == ZMAGIC) ? PAGE_SIZE : EXEC_BYTES_SIZE) | |
38 | /**************************************************************************/ | |
39 | ||
c3eb25fc SC |
40 | #include "aout/stab_gnu.h" |
41 | #include "aout/ar.h" | |
2c3f93e5 JG |
42 | #include "libaout.h" /* BFD a.out internal data structures */ |
43 | ||
44 | bfd_target *encap_callback (); | |
45 | ||
46 | bfd_target * | |
47 | DEFUN(encap_object_p,(abfd), | |
48 | bfd *abfd) | |
49 | { | |
50 | unsigned char magicbuf[4]; /* Raw bytes of magic number from file */ | |
51 | unsigned long magic; /* Swapped magic number */ | |
52 | short coff_magic; | |
53 | struct external_exec exec_bytes; | |
54 | struct internal_exec exec; | |
55 | ||
56 | bfd_error = system_call_error; | |
57 | ||
58 | if (bfd_read ((PTR)magicbuf, 1, sizeof (magicbuf), abfd) != | |
59 | sizeof (magicbuf)) | |
60 | return 0; | |
61 | ||
62 | coff_magic = bfd_h_get_16 (abfd, magicbuf); | |
63 | if (coff_magic != COFF_MAGIC) | |
64 | return 0; /* Not an encap coff file */ | |
65 | ||
66 | __header_offset_temp==COFF_MAGIC ? sizeof(struct coffheader) : 0) | |
67 | (fseek ((f), HEADER_OFFSET((f)), 1)) | |
68 | ||
69 | magic = bfd_h_get_32 (abfd, magicbuf); | |
70 | ||
71 | if (N_BADMAG (*((struct internal_exec *) &magic))) return 0; | |
72 | ||
73 | struct external_exec exec_bytes; | |
74 | if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd) | |
75 | != EXEC_BYTES_SIZE) { | |
76 | bfd_error = wrong_format; | |
77 | return 0; | |
78 | } | |
79 | NAME(aout,swap_exec_header_in)(abfd, &exec_bytes, &exec); | |
80 | ||
81 | return aout_32_some_aout_object_p (abfd, &exec, encap_callback); | |
82 | } | |
83 | ||
84 | /* Finish up the reading of a encapsulated-coff a.out file header */ | |
85 | bfd_target * | |
86 | DEFUN(encap_callback,(abfd), | |
87 | bfd *abfd) | |
88 | { | |
89 | struct internal_exec *execp = exec_hdr (abfd); | |
90 | ||
91 | WORK_OUT_FILE_POSITIONS(abfd, execp) ; | |
92 | ||
93 | /* If we have a coff header, it can give us better values for | |
94 | text_start and exec_data_start. This is particularly useful | |
95 | for remote debugging of embedded systems. */ | |
96 | if (N_FLAGS(exec_aouthdr) & N_FLAGS_COFF_ENCAPSULATE) | |
97 | { | |
98 | struct coffheader ch; | |
99 | int val; | |
100 | val = lseek (execchan, -(sizeof (AOUTHDR) + sizeof (ch)), 1); | |
101 | if (val == -1) | |
102 | perror_with_name (filename); | |
103 | val = myread (execchan, &ch, sizeof (ch)); | |
104 | if (val < 0) | |
105 | perror_with_name (filename); | |
106 | text_start = ch.text_start; | |
107 | exec_data_start = ch.data_start; | |
108 | } else | |
109 | { | |
110 | text_start = | |
111 | IS_OBJECT_FILE (exec_aouthdr) ? 0 : N_TXTADDR (exec_aouthdr); | |
112 | exec_data_start = IS_OBJECT_FILE (exec_aouthdr) | |
113 | ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr); | |
114 | } | |
115 | ||
116 | /* Determine the architecture and machine type of the object file. */ | |
117 | bfd_default_set_arch_mach(abfd, bfd_arch_m68k, 0); /* FIXME */ | |
118 | ||
119 | return abfd->xvec; | |
120 | } | |
121 | ||
122 | /* Write an object file in Encapsulated COFF format. | |
123 | Section contents have already been written. We write the | |
124 | file header, symbols, and relocation. */ | |
125 | ||
126 | boolean | |
127 | DEFUN(encap_write_object_contents,(abfd), | |
128 | bfd *abfd) | |
129 | { | |
130 | bfd_size_type data_pad = 0; | |
131 | struct external_exec exec_bytes; | |
132 | struct internal_exec *execp = exec_hdr (abfd); | |
133 | ||
134 | /****** FIXME: Fragments from the old GNU LD program for dealing with | |
135 | encap coff. */ | |
136 | struct coffheader coffheader; | |
137 | int need_coff_header; | |
138 | ||
139 | /* Determine whether to count the header as part of | |
140 | the text size, and initialize the text size accordingly. | |
141 | This depends on the kind of system and on the output format selected. */ | |
142 | ||
143 | N_SET_MAGIC (outheader, magic); | |
144 | #ifdef INITIALIZE_HEADER | |
145 | INITIALIZE_HEADER; | |
146 | #endif | |
147 | ||
148 | text_size = sizeof (struct exec); | |
149 | #ifdef COFF_ENCAPSULATE | |
150 | if (relocatable_output == 0 && file_table[0].just_syms_flag == 0) | |
151 | { | |
152 | need_coff_header = 1; | |
153 | /* set this flag now, since it will change the values of N_TXTOFF, etc */ | |
154 | N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE); | |
155 | text_size += sizeof (struct coffheader); | |
156 | } | |
157 | #endif | |
158 | ||
159 | #ifdef COFF_ENCAPSULATE | |
160 | if (need_coff_header) | |
161 | { | |
162 | /* We are encapsulating BSD format within COFF format. */ | |
163 | struct coffscn *tp, *dp, *bp; | |
164 | ||
165 | tp = &coffheader.scns[0]; | |
166 | dp = &coffheader.scns[1]; | |
167 | bp = &coffheader.scns[2]; | |
168 | ||
169 | strcpy (tp->s_name, ".text"); | |
170 | tp->s_paddr = text_start; | |
171 | tp->s_vaddr = text_start; | |
172 | tp->s_size = text_size; | |
173 | tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec); | |
174 | tp->s_relptr = 0; | |
175 | tp->s_lnnoptr = 0; | |
176 | tp->s_nreloc = 0; | |
177 | tp->s_nlnno = 0; | |
178 | tp->s_flags = 0x20; | |
179 | strcpy (dp->s_name, ".data"); | |
180 | dp->s_paddr = data_start; | |
181 | dp->s_vaddr = data_start; | |
182 | dp->s_size = data_size; | |
183 | dp->s_scnptr = tp->s_scnptr + tp->s_size; | |
184 | dp->s_relptr = 0; | |
185 | dp->s_lnnoptr = 0; | |
186 | dp->s_nreloc = 0; | |
187 | dp->s_nlnno = 0; | |
188 | dp->s_flags = 0x40; | |
189 | strcpy (bp->s_name, ".bss"); | |
190 | bp->s_paddr = dp->s_vaddr + dp->s_size; | |
191 | bp->s_vaddr = bp->s_paddr; | |
192 | bp->s_size = bss_size; | |
193 | bp->s_scnptr = 0; | |
194 | bp->s_relptr = 0; | |
195 | bp->s_lnnoptr = 0; | |
196 | bp->s_nreloc = 0; | |
197 | bp->s_nlnno = 0; | |
198 | bp->s_flags = 0x80; | |
199 | ||
200 | coffheader.f_magic = COFF_MAGIC; | |
201 | coffheader.f_nscns = 3; | |
202 | /* store an unlikely time so programs can | |
203 | * tell that there is a bsd header | |
204 | */ | |
205 | coffheader.f_timdat = 1; | |
206 | coffheader.f_symptr = 0; | |
207 | coffheader.f_nsyms = 0; | |
208 | coffheader.f_opthdr = 28; | |
209 | coffheader.f_flags = 0x103; | |
210 | /* aouthdr */ | |
211 | coffheader.magic = ZMAGIC; | |
212 | coffheader.vstamp = 0; | |
213 | coffheader.tsize = tp->s_size; | |
214 | coffheader.dsize = dp->s_size; | |
215 | coffheader.bsize = bp->s_size; | |
216 | coffheader.entry = outheader.a_entry; | |
217 | coffheader.text_start = tp->s_vaddr; | |
218 | coffheader.data_start = dp->s_vaddr; | |
219 | } | |
220 | #endif | |
221 | ||
222 | #ifdef COFF_ENCAPSULATE | |
223 | if (need_coff_header) | |
224 | mywrite (&coffheader, sizeof coffheader, 1, outdesc); | |
225 | #endif | |
226 | ||
227 | #ifndef COFF_ENCAPSULATE | |
228 | padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc); | |
229 | #endif | |
230 | ||
231 | text_size -= N_TXTOFF (outheader); | |
232 | WRITE_HEADERS(abfd, execp); | |
233 | return true; | |
234 | } | |
235 | \f | |
236 | /* Transfer vectors for Encapsulated-(aout-in)-coff */ | |
237 | ||
238 | /* We use BFD generic archive files. */ | |
239 | #define newsos_openr_next_archived_file bfd_generic_openr_next_archived_file | |
240 | #define newsos_generic_stat_arch_elt bfd_generic_stat_arch_elt | |
241 | #define newsos_slurp_armap bfd_slurp_bsd_armap | |
242 | #define newsos_slurp_extended_name_table bfd_true | |
243 | #define newsos_write_armap bsd_write_armap | |
244 | #define newsos_truncate_arname bfd_bsd_truncate_arname | |
245 | ||
246 | /* We don't support core files yet. FIXME. */ | |
247 | #define newsos_core_file_failing_command _bfd_dummy_core_file_failing_command | |
248 | #define newsos_core_file_failing_signal _bfd_dummy_core_file_failing_signal | |
249 | #define newsos_core_file_matches_executable_p \ | |
250 | _bfd_dummy_core_file_matches_executable_p | |
251 | #define newsos_core_file_p _bfd_dummy_target | |
252 | ||
253 | #define newsos_bfd_debug_info_start bfd_void | |
254 | #define newsos_bfd_debug_info_end bfd_void | |
255 | #define newsos_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void | |
256 | ||
257 | #define newsos_mkobject aout_32_mkobject | |
258 | #define newsos_close_and_cleanup aout_32_close_and_cleanup | |
259 | #define newsos_set_section_contents aout_32_set_section_contents | |
260 | #define newsos_get_section_contents aout_32_get_section_contents | |
261 | #define newsos_new_section_hook aout_32_new_section_hook | |
262 | #define newsos_get_symtab_upper_bound aout_32_get_symtab_upper_bound | |
263 | #define newsos_get_symtab aout_32_get_symtab | |
264 | #define newsos_get_reloc_upper_bound aout_32_get_reloc_upper_bound | |
265 | #define newsos_canonicalize_reloc aout_32_canonicalize_reloc | |
266 | #define newsos_make_empty_symbol aout_32_make_empty_symbol | |
267 | #define newsos_print_symbol aout_32_print_symbol | |
268 | #define newsos_get_lineno aout_32_get_lineno | |
269 | #define newsos_set_arch_mach aout_32_set_arch_mach | |
270 | #define newsos_find_nearest_line aout_32_find_nearest_line | |
271 | #define newsos_sizeof_headers aout_32_sizeof_headers | |
272 | ||
273 | ||
274 | /* We define our own versions of these routines. */ | |
275 | ||
276 | ||
277 | bfd_target encap_big_vec = /* "Encapsulated coff": a.out with COFF header prefix */ | |
278 | { | |
279 | "a.out-encap", /* name */ | |
280 | bfd_target_aout_flavour, | |
281 | true, /* target byte order */ | |
282 | true, /* target headers byte order */ | |
283 | (HAS_RELOC | EXEC_P | /* object flags */ | |
284 | HAS_LINENO | HAS_DEBUG | | |
285 | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED), | |
286 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ | |
287 | ' ', /* ar_pad_char */ | |
288 | 16, /* ar_max_namelen */ | |
289 | 1, /* minimum alignment */ | |
290 | _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */ | |
291 | _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */ | |
292 | ||
293 | {_bfd_dummy_target, newsos3_object_p, /* bfd_check_format */ | |
294 | bfd_generic_archive_p, newsos_core_file_p}, | |
295 | {bfd_false, newsos_mkobject, /* bfd_set_format */ | |
296 | _bfd_generic_mkarchive, bfd_false}, | |
297 | {bfd_false, newsos3_write_object_contents, /* bfd_write_contents */ | |
298 | _bfd_write_archive_contents, bfd_false}, | |
299 | ||
300 | JUMP_TABLE(newsos) | |
301 | }; | |
302 | ||
303 | /* Little-endian "encapsulated coff": a.out with COFF header prefix */ | |
304 | bfd_target encap_little_vec = | |
305 | { | |
306 | "a.out-encap-little", /* name */ | |
307 | bfd_target_aout_flavour, | |
308 | false, /* target byte order */ | |
309 | false, /* target headers byte order */ | |
310 | (HAS_RELOC | EXEC_P | /* object flags */ | |
311 | HAS_LINENO | HAS_DEBUG | | |
312 | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED), | |
313 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ | |
314 | ' ', /* ar_pad_char */ | |
315 | 16, /* ar_max_namelen */ | |
316 | 1, /* minimum alignment */ | |
317 | _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */ | |
318 | _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putb16, /* hdrs */ | |
319 | ||
320 | {_bfd_dummy_target, newsos3_object_p, /* bfd_check_format */ | |
321 | bfd_generic_archive_p, newsos_core_file_p}, | |
322 | {bfd_false, newsos_mkobject, /* bfd_set_format */ | |
323 | _bfd_generic_mkarchive, bfd_false}, | |
324 | {bfd_false, newsos3_write_object_contents, /* bfd_write_contents */ | |
325 | _bfd_write_archive_contents, bfd_false}, | |
326 | ||
327 | JUMP_TABLE(newsos) | |
328 | }; |