]>
Commit | Line | Data |
---|---|---|
294eaca4 SC |
1 | /* BFD back-end for a.out.adobe binaries. |
2 | Copyright 1990, 1991, 1992 Free Software Foundation, Inc. | |
3 | Written by Cygnus Support. Based on bout.c. | |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program 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 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program 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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "libbfd.h" | |
24 | ||
25 | #include "aout/adobe.h" | |
26 | ||
27 | #include "aout/stab_gnu.h" | |
28 | #include "libaout.h" /* BFD a.out internal data structures */ | |
29 | ||
30 | extern bfd_target a_out_adobe_vec; /* Forward decl */ | |
31 | ||
32 | PROTO (static bfd_target *, aout_adobe_callback, (bfd *)); | |
33 | ||
34 | PROTO (boolean, aout_32_slurp_symbol_table, (bfd *abfd)); | |
35 | PROTO (void , aout_32_write_syms, ()); | |
36 | PROTO (static void, aout_adobe_write_section, (bfd *abfd, sec_ptr sect)); | |
37 | ||
38 | /* Swaps the information in an executable header taken from a raw byte | |
39 | stream memory image, into the internal exec_header structure. */ | |
40 | ||
41 | PROTO(void, aout_adobe_swap_exec_header_in, | |
42 | (bfd *abfd, | |
43 | struct external_exec *raw_bytes, | |
44 | struct internal_exec *execp)); | |
45 | ||
46 | void | |
47 | DEFUN(aout_adobe_swap_exec_header_in,(abfd, raw_bytes, execp), | |
48 | bfd *abfd AND | |
49 | struct external_exec *raw_bytes AND | |
50 | struct internal_exec *execp) | |
51 | { | |
52 | struct external_exec *bytes = (struct external_exec *)raw_bytes; | |
53 | ||
54 | /* Now fill in fields in the execp, from the bytes in the raw data. */ | |
55 | execp->a_info = bfd_h_get_32 (abfd, bytes->e_info); | |
56 | execp->a_text = GET_WORD (abfd, bytes->e_text); | |
57 | execp->a_data = GET_WORD (abfd, bytes->e_data); | |
58 | execp->a_bss = GET_WORD (abfd, bytes->e_bss); | |
59 | execp->a_syms = GET_WORD (abfd, bytes->e_syms); | |
60 | execp->a_entry = GET_WORD (abfd, bytes->e_entry); | |
61 | execp->a_trsize = GET_WORD (abfd, bytes->e_trsize); | |
62 | execp->a_drsize = GET_WORD (abfd, bytes->e_drsize); | |
63 | } | |
64 | ||
65 | /* Swaps the information in an internal exec header structure into the | |
66 | supplied buffer ready for writing to disk. */ | |
67 | ||
68 | PROTO(void, aout_adobe_swap_exec_header_out, | |
69 | (bfd *abfd, | |
70 | struct internal_exec *execp, | |
71 | struct external_exec *raw_bytes)); | |
72 | void | |
73 | DEFUN(aout_adobe_swap_exec_header_out,(abfd, execp, raw_bytes), | |
74 | bfd *abfd AND | |
75 | struct internal_exec *execp AND | |
76 | struct external_exec *raw_bytes) | |
77 | { | |
78 | struct external_exec *bytes = (struct external_exec *)raw_bytes; | |
79 | ||
80 | /* Now fill in fields in the raw data, from the fields in the exec struct. */ | |
81 | bfd_h_put_32 (abfd, execp->a_info , bytes->e_info); | |
82 | PUT_WORD (abfd, execp->a_text , bytes->e_text); | |
83 | PUT_WORD (abfd, execp->a_data , bytes->e_data); | |
84 | PUT_WORD (abfd, execp->a_bss , bytes->e_bss); | |
85 | PUT_WORD (abfd, execp->a_syms , bytes->e_syms); | |
86 | PUT_WORD (abfd, execp->a_entry , bytes->e_entry); | |
87 | PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize); | |
88 | PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize); | |
89 | } | |
90 | ||
91 | ||
92 | static bfd_target * | |
93 | aout_adobe_object_p (abfd) | |
94 | bfd *abfd; | |
95 | { | |
96 | struct internal_exec anexec; | |
97 | struct external_exec exec_bytes; | |
98 | char *targ; | |
99 | ||
100 | if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd) | |
101 | != EXEC_BYTES_SIZE) { | |
102 | bfd_error = wrong_format; | |
103 | return 0; | |
104 | } | |
105 | ||
106 | anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info); | |
107 | ||
108 | /* Normally we just compare for the magic number. | |
109 | However, a bunch of Adobe tools aren't fixed up yet; they generate | |
110 | files using ZMAGIC(!). | |
111 | If the environment variable GNUTARGET is set to "a.out.adobe", we will | |
112 | take just about any a.out file as an Adobe a.out file. FIXME! */ | |
113 | ||
114 | if (N_BADMAG (anexec)) { | |
115 | targ = getenv ("GNUTARGET"); | |
116 | if (targ && strcmp (targ, a_out_adobe_vec.name)) | |
117 | ; /* Just continue anyway, if specifically set to this format */ | |
118 | else | |
119 | { | |
120 | bfd_error = wrong_format; | |
121 | return 0; | |
122 | } | |
123 | } | |
124 | ||
125 | aout_adobe_swap_exec_header_in (abfd, &exec_bytes, &anexec); | |
126 | return aout_32_some_aout_object_p (abfd, &anexec, aout_adobe_callback); | |
127 | } | |
128 | ||
129 | ||
130 | /* Finish up the opening of a b.out file for reading. Fill in all the | |
131 | fields that are not handled by common code. */ | |
132 | ||
133 | static bfd_target * | |
134 | aout_adobe_callback (abfd) | |
135 | bfd *abfd; | |
136 | { | |
137 | struct internal_exec *execp = exec_hdr (abfd); | |
138 | unsigned long bss_start; | |
139 | asection *sect; | |
140 | struct external_segdesc ext[1]; | |
141 | char *section_name; | |
142 | char try_again[30]; /* name and number */ | |
143 | char *newname; | |
144 | int trynum; | |
145 | flagword flags; | |
146 | ||
147 | /* Architecture and machine type -- unknown in this format. */ | |
148 | bfd_set_arch_mach(abfd, bfd_arch_unknown, 0); | |
149 | ||
150 | /* The positions of the string table and symbol table. */ | |
151 | obj_str_filepos (abfd) = N_STROFF (*execp); | |
152 | obj_sym_filepos (abfd) = N_SYMOFF (*execp); | |
153 | ||
154 | /* Suck up the section information from the file, one section at a time. */ | |
155 | ||
156 | for (;;) { | |
157 | if (bfd_read ((PTR) ext, 1, sizeof (*ext), abfd) != sizeof (*ext)) { | |
158 | bfd_error = wrong_format; | |
159 | return 0; | |
160 | } | |
161 | switch (ext->e_type[0]) { | |
162 | case N_TEXT: | |
163 | section_name = ".text"; | |
164 | flags = SEC_CODE | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS; | |
165 | break; | |
166 | ||
167 | case N_DATA: | |
168 | section_name = ".data"; | |
169 | flags = SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS; | |
170 | break; | |
171 | ||
172 | case N_BSS: | |
173 | section_name = ".bss"; | |
174 | flags = SEC_DATA | SEC_HAS_CONTENTS; | |
175 | break; | |
176 | ||
177 | case 0: | |
178 | goto no_more_sections; | |
179 | ||
180 | default: | |
181 | fprintf (stderr, "Unknown section type in a.out.adobe file: %x\n", | |
182 | ext->e_type); | |
183 | goto no_more_sections; | |
184 | } | |
185 | ||
186 | /* First one is called ".text" or whatever; subsequent ones are | |
187 | ".text1", ".text2", ... */ | |
188 | ||
189 | bfd_error = no_error; | |
190 | sect = bfd_make_section (abfd, section_name); | |
191 | trynum = 0; | |
192 | while (!sect) { | |
193 | if (bfd_error != no_error) | |
194 | return 0; /* Some other error -- slide into the sunset */ | |
195 | sprintf (try_again, "%s%d", section_name, ++trynum); | |
196 | sect = bfd_make_section (abfd, try_again); | |
197 | } | |
198 | ||
199 | /* Fix the name, if it is a sprintf'd name. */ | |
200 | if (sect->name == try_again) { | |
201 | newname = (char *) bfd_zalloc(abfd, strlen (sect->name)); | |
202 | if (newname == NULL) { | |
203 | bfd_error = no_memory; | |
204 | return 0; | |
205 | } | |
206 | strcpy (newname, sect->name); | |
207 | sect->name = newname; | |
208 | } | |
209 | ||
210 | /* Now set the section's attributes. */ | |
211 | bfd_set_section_flags (abfd, sect, flags); | |
212 | sect->_raw_size = ((ext->e_size[0] << 8) /* Assumed big-endian */ | |
213 | | ext->e_size[1] << 8) | |
214 | | ext->e_size[2]; | |
215 | sect->_cooked_size = sect->_raw_size; | |
216 | sect->vma = bfd_h_get_32 (abfd, ext->e_virtbase); | |
217 | sect->filepos = bfd_h_get_32 (abfd, ext->e_filebase); | |
218 | /* FIXME XXX alignment? */ | |
219 | ||
220 | /* Set relocation information for first section of each type. */ | |
221 | if (trynum == 0) switch (ext->e_type[0]) { | |
222 | case N_TEXT: | |
223 | sect->rel_filepos = N_TRELOFF (*execp); | |
224 | sect->reloc_count = execp->a_trsize; | |
225 | break; | |
226 | ||
227 | case N_DATA: | |
228 | sect->rel_filepos = N_DRELOFF (*execp); | |
229 | sect->reloc_count = execp->a_drsize; | |
230 | break; | |
231 | } | |
232 | } | |
233 | no_more_sections: | |
234 | ||
235 | adata(abfd).reloc_entry_size = sizeof (struct reloc_std_external); | |
236 | adata(abfd).symbol_entry_size = sizeof (struct external_nlist); | |
237 | adata(abfd).page_size = 1; /* Not applicable. */ | |
238 | adata(abfd).segment_size = 1; /* Not applicable. */ | |
239 | adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE; | |
240 | ||
241 | return abfd->xvec; | |
242 | } | |
243 | ||
244 | struct bout_data_struct { | |
245 | struct aoutdata a; | |
246 | struct internal_exec e; | |
247 | }; | |
248 | ||
249 | static boolean | |
250 | aout_adobe_mkobject (abfd) | |
251 | bfd *abfd; | |
252 | { | |
253 | struct bout_data_struct *rawptr; | |
254 | ||
255 | rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct)); | |
256 | if (rawptr == NULL) { | |
257 | bfd_error = no_memory; | |
258 | return false; | |
259 | } | |
260 | ||
261 | abfd->tdata.bout_data = rawptr; | |
262 | exec_hdr (abfd) = &rawptr->e; | |
263 | ||
264 | adata(abfd).reloc_entry_size = sizeof (struct reloc_std_external); | |
265 | adata(abfd).symbol_entry_size = sizeof (struct external_nlist); | |
266 | adata(abfd).page_size = 1; /* Not applicable. */ | |
267 | adata(abfd).segment_size = 1; /* Not applicable. */ | |
268 | adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE; | |
269 | ||
270 | return true; | |
271 | } | |
272 | ||
273 | ||
274 | static boolean | |
275 | aout_adobe_write_object_contents (abfd) | |
276 | bfd *abfd; | |
277 | { | |
278 | struct external_exec swapped_hdr; | |
279 | static struct external_segdesc sentinel[1] = {0}; | |
280 | asection *sect; | |
281 | ||
282 | exec_hdr (abfd)->a_info = ZMAGIC; | |
283 | ||
284 | /* Calculate text size as total of text sections, etc. */ | |
285 | ||
286 | exec_hdr (abfd)->a_text = 0; | |
287 | exec_hdr (abfd)->a_data = 0; | |
288 | exec_hdr (abfd)->a_bss = 0; | |
289 | exec_hdr (abfd)->a_trsize = 0; | |
290 | exec_hdr (abfd)->a_drsize = 0; | |
291 | ||
292 | for (sect = abfd->sections; sect; sect = sect->next) { | |
293 | if (sect->flags & SEC_CODE) { | |
294 | exec_hdr (abfd)->a_text += sect->_raw_size; | |
295 | exec_hdr (abfd)->a_trsize += sect->reloc_count * | |
296 | sizeof (struct reloc_std_external); | |
297 | } else if (sect->flags & SEC_DATA) { | |
298 | exec_hdr (abfd)->a_data += sect->_raw_size; | |
299 | exec_hdr (abfd)->a_drsize += sect->reloc_count * | |
300 | sizeof (struct reloc_std_external); | |
301 | } else if (sect->flags & SEC_ALLOC && !(sect->flags & SEC_LOAD)) { | |
302 | exec_hdr (abfd)->a_bss += sect->_raw_size; | |
303 | } | |
304 | } | |
305 | ||
306 | exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) | |
307 | * sizeof (struct external_nlist); | |
308 | exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd); | |
309 | ||
310 | aout_adobe_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr); | |
311 | ||
312 | bfd_seek (abfd, 0L, SEEK_SET); | |
313 | bfd_write ((PTR) &swapped_hdr, 1, EXEC_BYTES_SIZE, abfd); | |
314 | ||
315 | /* Now write out the section information. Text first, data next, rest | |
316 | afterward. */ | |
317 | ||
318 | for (sect = abfd->sections; sect; sect = sect->next) { | |
319 | if (sect->flags & SEC_CODE) { | |
320 | aout_adobe_write_section (abfd, sect); | |
321 | } | |
322 | } | |
323 | for (sect = abfd->sections; sect; sect = sect->next) { | |
324 | if (sect->flags & SEC_DATA) { | |
325 | aout_adobe_write_section (abfd, sect); | |
326 | } | |
327 | } | |
328 | for (sect = abfd->sections; sect; sect = sect->next) { | |
329 | if (!(sect->flags & (SEC_CODE|SEC_DATA))) { | |
330 | aout_adobe_write_section (abfd, sect); | |
331 | } | |
332 | } | |
333 | ||
334 | /* Write final `sentinel` section header (with type of 0). */ | |
335 | bfd_write ((PTR) sentinel, 1, sizeof (*sentinel), abfd); | |
336 | ||
337 | /* Now write out reloc info, followed by syms and strings */ | |
338 | if (bfd_get_symcount (abfd) != 0) | |
339 | { | |
340 | bfd_seek (abfd, | |
341 | (long)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET); | |
342 | ||
343 | aout_32_write_syms (abfd); | |
344 | ||
345 | bfd_seek (abfd, (long)(N_TRELOFF(*exec_hdr(abfd))), SEEK_SET); | |
346 | ||
347 | for (sect = abfd->sections; sect; sect = sect->next) { | |
348 | if (sect->flags & SEC_CODE) { | |
349 | if (!aout_32_squirt_out_relocs (abfd, sect)) | |
350 | return false; | |
351 | } | |
352 | } | |
353 | ||
354 | bfd_seek (abfd, (long)(N_DRELOFF(*exec_hdr(abfd))), SEEK_SET); | |
355 | ||
356 | for (sect = abfd->sections; sect; sect = sect->next) { | |
357 | if (sect->flags & SEC_DATA) { | |
358 | if (!aout_32_squirt_out_relocs (abfd, sect)) | |
359 | return false; | |
360 | } | |
361 | } | |
362 | } | |
363 | return true; | |
364 | } | |
365 | ||
366 | static void | |
367 | aout_adobe_write_section (abfd, sect) | |
368 | bfd *abfd; | |
369 | sec_ptr sect; | |
370 | { | |
371 | /* FIXME XXX */ | |
372 | } | |
373 | \f | |
374 | static boolean | |
375 | aout_adobe_set_section_contents (abfd, section, location, offset, count) | |
376 | bfd *abfd; | |
377 | sec_ptr section; | |
378 | unsigned char *location; | |
379 | file_ptr offset; | |
380 | int count; | |
381 | { | |
382 | file_ptr section_start; | |
383 | sec_ptr sect; | |
384 | ||
385 | if (abfd->output_has_begun == false) { /* set by bfd.c handler */ | |
386 | ||
387 | /* Assign file offsets to sections. Text sections are first, and | |
388 | are contiguous. Then data sections. Everything else at the end. */ | |
389 | ||
390 | section_start = N_TXTOFF (ignore<-->me); | |
391 | ||
392 | for (sect = abfd->sections; sect; sect = sect->next) { | |
393 | if (sect->flags & SEC_CODE) { | |
394 | sect->filepos = section_start; | |
395 | /* FIXME: Round to alignment */ | |
396 | section_start += sect->_raw_size; | |
397 | } | |
398 | } | |
399 | ||
400 | for (sect = abfd->sections; sect; sect = sect->next) { | |
401 | if (sect->flags & SEC_DATA) { | |
402 | sect->filepos = section_start; | |
403 | /* FIXME: Round to alignment */ | |
404 | section_start += sect->_raw_size; | |
405 | } | |
406 | } | |
407 | ||
408 | for (sect = abfd->sections; sect; sect = sect->next) { | |
409 | if (sect->flags & SEC_HAS_CONTENTS && | |
410 | !(sect->flags & (SEC_CODE|SEC_DATA))) { | |
411 | sect->filepos = section_start; | |
412 | /* FIXME: Round to alignment */ | |
413 | section_start += sect->_raw_size; | |
414 | } | |
415 | } | |
416 | } | |
417 | ||
418 | /* regardless, once we know what we're doing, we might as well get going */ | |
419 | bfd_seek (abfd, section->filepos + offset, SEEK_SET); | |
420 | ||
421 | if (count != 0) { | |
422 | return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false; | |
423 | } | |
424 | return true; | |
425 | } | |
426 | ||
427 | static boolean | |
428 | aout_adobe_set_arch_mach (abfd, arch, machine) | |
429 | bfd *abfd; | |
430 | enum bfd_architecture arch; | |
431 | unsigned long machine; | |
432 | { | |
433 | bfd_default_set_arch_mach(abfd, arch, machine); | |
434 | ||
435 | if (arch == bfd_arch_unknown) /* Unknown machine arch is OK */ | |
436 | return true; | |
437 | ||
438 | return false; | |
439 | } | |
440 | ||
441 | static int | |
442 | DEFUN(aout_adobe_sizeof_headers,(ignore_abfd, ignore), | |
443 | bfd *ignore_abfd AND | |
444 | boolean ignore) | |
445 | { | |
446 | return sizeof(struct internal_exec); | |
447 | } | |
448 | ||
449 | ||
450 | ||
451 | ||
452 | /* Build the transfer vector for Adobe A.Out files. */ | |
453 | ||
454 | /* We don't have core files. */ | |
455 | #define aout_32_core_file_failing_command _bfd_dummy_core_file_failing_command | |
456 | #define aout_32_core_file_failing_signal _bfd_dummy_core_file_failing_signal | |
457 | #define aout_32_core_file_matches_executable_p \ | |
458 | _bfd_dummy_core_file_matches_executable_p | |
459 | ||
460 | /* We use BSD-Unix generic archive files. */ | |
461 | #define aout_32_openr_next_archived_file bfd_generic_openr_next_archived_file | |
462 | #define aout_32_generic_stat_arch_elt bfd_generic_stat_arch_elt | |
463 | #define aout_32_slurp_armap bfd_slurp_bsd_armap | |
464 | #define aout_32_slurp_extended_name_table bfd_true | |
465 | #define aout_32_write_armap bsd_write_armap | |
466 | #define aout_32_truncate_arname bfd_bsd_truncate_arname | |
467 | ||
468 | /* We override these routines from the usual a.out file routines. */ | |
469 | #define aout_32_set_section_contents aout_adobe_set_section_contents | |
470 | #define aout_32_set_arch_mach aout_adobe_set_arch_mach | |
471 | #define aout_32_sizeof_headers aout_adobe_sizeof_headers | |
472 | ||
473 | #define aout_32_bfd_debug_info_start bfd_void | |
474 | #define aout_32_bfd_debug_info_end bfd_void | |
475 | #define aout_32_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void | |
476 | ||
477 | #define aout_32_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents | |
478 | #define aout_32_bfd_relax_section bfd_generic_relax_section | |
479 | ||
480 | bfd_target a_out_adobe_vec = | |
481 | { | |
482 | "a.out.adobe", /* name */ | |
483 | bfd_target_aout_flavour, | |
484 | true, /* data byte order is unknown (big assumed) */ | |
485 | true, /* hdr byte order is big */ | |
486 | (HAS_RELOC | EXEC_P | /* object flags */ | |
487 | HAS_LINENO | HAS_DEBUG | | |
488 | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT ), | |
489 | /* section flags */ | |
490 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_DATA | SEC_RELOC), | |
491 | '_', /* symbol leading char */ | |
492 | ' ', /* ar_pad_char */ | |
493 | 16, /* ar_max_namelen */ | |
494 | 2, /* minumum alignment power */ | |
495 | ||
496 | _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */ | |
497 | _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */ | |
498 | {_bfd_dummy_target, aout_adobe_object_p, /* bfd_check_format */ | |
499 | bfd_generic_archive_p, _bfd_dummy_target}, | |
500 | {bfd_false, aout_adobe_mkobject, /* bfd_set_format */ | |
501 | _bfd_generic_mkarchive, bfd_false}, | |
502 | {bfd_false, aout_adobe_write_object_contents, /* bfd_write_contents */ | |
503 | _bfd_write_archive_contents, bfd_false}, | |
504 | ||
505 | JUMP_TABLE(aout_32) | |
506 | }; | |
507 |