1 /* BFD back-end for a.out.adobe binaries.
2 Copyright (C) 1990-2015 Free Software Foundation, Inc.
3 Written by Cygnus Support. Based on bout.c.
5 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
10 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include "aout/adobe.h"
25 #include "aout/stab_gnu.h"
26 #include "libaout.h" /* BFD a.out internal data structures. */
29 extern const bfd_target aout_adobe_vec;
31 /* Swaps the information in an executable header taken from a raw byte
32 stream memory image, into the internal exec_header structure. */
35 aout_adobe_swap_exec_header_in (bfd *abfd,
36 struct external_exec *bytes,
37 struct internal_exec *execp)
39 /* Now fill in fields in the execp, from the bytes in the raw data. */
40 execp->a_info = H_GET_32 (abfd, bytes->e_info);
41 execp->a_text = GET_WORD (abfd, bytes->e_text);
42 execp->a_data = GET_WORD (abfd, bytes->e_data);
43 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
44 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
45 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
46 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
47 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
50 /* Swaps the information in an internal exec header structure into the
51 supplied buffer ready for writing to disk. */
54 aout_adobe_swap_exec_header_out (bfd *abfd,
55 struct internal_exec *execp,
56 struct external_exec *bytes)
58 /* Now fill in fields in the raw data, from the fields in the exec
60 H_PUT_32 (abfd, execp->a_info , bytes->e_info);
61 PUT_WORD (abfd, execp->a_text , bytes->e_text);
62 PUT_WORD (abfd, execp->a_data , bytes->e_data);
63 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
64 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
65 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
66 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
67 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
70 /* Finish up the opening of a b.out file for reading. Fill in all the
71 fields that are not handled by common code. */
73 static const bfd_target *
74 aout_adobe_callback (bfd *abfd)
76 struct internal_exec *execp = exec_hdr (abfd);
78 struct external_segdesc ext[1];
80 char try_again[30]; /* Name and number. */
85 /* Architecture and machine type -- unknown in this format. */
86 bfd_set_arch_mach (abfd, bfd_arch_unknown, 0L);
88 /* The positions of the string table and symbol table. */
89 obj_str_filepos (abfd) = N_STROFF (*execp);
90 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
92 /* Suck up the section information from the file, one section at a time. */
95 bfd_size_type amt = sizeof (*ext);
96 if (bfd_bread ( ext, amt, abfd) != amt)
98 if (bfd_get_error () != bfd_error_system_call)
99 bfd_set_error (bfd_error_wrong_format);
103 switch (ext->e_type[0])
106 section_name = ".text";
107 flags = SEC_CODE | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
111 section_name = ".data";
112 flags = SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
116 section_name = ".bss";
117 flags = SEC_DATA | SEC_HAS_CONTENTS;
121 goto no_more_sections;
124 (*_bfd_error_handler)
125 (_("%B: Unknown section type in a.out.adobe file: %x\n"),
126 abfd, ext->e_type[0]);
127 goto no_more_sections;
130 /* First one is called ".text" or whatever; subsequent ones are
131 ".text1", ".text2", ... */
132 bfd_set_error (bfd_error_no_error);
133 sect = bfd_make_section_with_flags (abfd, section_name, flags);
138 if (bfd_get_error () != bfd_error_no_error)
139 /* Some other error -- slide into the sunset. */
141 sprintf (try_again, "%s%d", section_name, ++trynum);
142 sect = bfd_make_section_with_flags (abfd, try_again, flags);
145 /* Fix the name, if it is a sprintf'd name. */
146 if (sect->name == try_again)
148 amt = strlen (sect->name);
149 newname = bfd_zalloc (abfd, amt);
152 strcpy (newname, sect->name);
153 sect->name = newname;
156 /* Assumed big-endian. */
157 sect->size = ((ext->e_size[0] << 8)
158 | ext->e_size[1] << 8
160 sect->vma = H_GET_32 (abfd, ext->e_virtbase);
161 sect->filepos = H_GET_32 (abfd, ext->e_filebase);
162 /* FIXME XXX alignment? */
164 /* Set relocation information for first section of each type. */
166 switch (ext->e_type[0])
169 sect->rel_filepos = N_TRELOFF (*execp);
170 sect->reloc_count = execp->a_trsize;
174 sect->rel_filepos = N_DRELOFF (*execp);
175 sect->reloc_count = execp->a_drsize;
184 adata (abfd).reloc_entry_size = sizeof (struct reloc_std_external);
185 adata (abfd).symbol_entry_size = sizeof (struct external_nlist);
186 adata (abfd).page_size = 1; /* Not applicable. */
187 adata (abfd).segment_size = 1; /* Not applicable. */
188 adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
193 static const bfd_target *
194 aout_adobe_object_p (bfd *abfd)
196 struct internal_exec anexec;
197 struct external_exec exec_bytes;
199 bfd_size_type amt = EXEC_BYTES_SIZE;
201 if (bfd_bread (& exec_bytes, amt, abfd) != amt)
203 if (bfd_get_error () != bfd_error_system_call)
204 bfd_set_error (bfd_error_wrong_format);
208 anexec.a_info = H_GET_32 (abfd, exec_bytes.e_info);
210 /* Normally we just compare for the magic number.
211 However, a bunch of Adobe tools aren't fixed up yet; they generate
212 files using ZMAGIC(!).
213 If the environment variable GNUTARGET is set to "a.out.adobe", we will
214 take just about any a.out file as an Adobe a.out file. FIXME! */
216 if (N_BADMAG (anexec))
218 targ = getenv ("GNUTARGET");
219 if (targ && !strcmp (targ, aout_adobe_vec.name))
220 /* Just continue anyway, if specifically set to this format. */
224 bfd_set_error (bfd_error_wrong_format);
229 aout_adobe_swap_exec_header_in (abfd, &exec_bytes, &anexec);
230 return aout_32_some_aout_object_p (abfd, &anexec, aout_adobe_callback);
233 struct bout_data_struct
236 struct internal_exec e;
240 aout_adobe_mkobject (bfd *abfd)
242 struct bout_data_struct *rawptr;
243 bfd_size_type amt = sizeof (struct bout_data_struct);
245 rawptr = bfd_zalloc (abfd, amt);
249 abfd->tdata.bout_data = rawptr;
250 exec_hdr (abfd) = &rawptr->e;
252 adata (abfd).reloc_entry_size = sizeof (struct reloc_std_external);
253 adata (abfd).symbol_entry_size = sizeof (struct external_nlist);
254 adata (abfd).page_size = 1; /* Not applicable. */
255 adata (abfd).segment_size = 1; /* Not applicable. */
256 adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
262 aout_adobe_write_section (bfd *abfd ATTRIBUTE_UNUSED,
263 sec_ptr sect ATTRIBUTE_UNUSED)
269 aout_adobe_write_object_contents (bfd *abfd)
271 struct external_exec swapped_hdr;
272 static struct external_segdesc sentinel[1]; /* Initialized to zero. */
276 exec_hdr (abfd)->a_info = ZMAGIC;
278 /* Calculate text size as total of text sections, etc. */
279 exec_hdr (abfd)->a_text = 0;
280 exec_hdr (abfd)->a_data = 0;
281 exec_hdr (abfd)->a_bss = 0;
282 exec_hdr (abfd)->a_trsize = 0;
283 exec_hdr (abfd)->a_drsize = 0;
285 for (sect = abfd->sections; sect; sect = sect->next)
287 if (sect->flags & SEC_CODE)
289 exec_hdr (abfd)->a_text += sect->size;
290 exec_hdr (abfd)->a_trsize += sect->reloc_count *
291 sizeof (struct reloc_std_external);
293 else if (sect->flags & SEC_DATA)
295 exec_hdr (abfd)->a_data += sect->size;
296 exec_hdr (abfd)->a_drsize += sect->reloc_count *
297 sizeof (struct reloc_std_external);
299 else if (sect->flags & SEC_ALLOC && !(sect->flags & SEC_LOAD))
300 exec_hdr (abfd)->a_bss += sect->size;
303 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd)
304 * sizeof (struct external_nlist);
305 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
307 aout_adobe_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
309 amt = EXEC_BYTES_SIZE;
310 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
311 || bfd_bwrite (& swapped_hdr, amt, abfd) != amt)
314 /* Now write out the section information. Text first, data next, rest
316 for (sect = abfd->sections; sect; sect = sect->next)
317 if (sect->flags & SEC_CODE)
318 aout_adobe_write_section (abfd, sect);
320 for (sect = abfd->sections; sect; sect = sect->next)
321 if (sect->flags & SEC_DATA)
322 aout_adobe_write_section (abfd, sect);
324 for (sect = abfd->sections; sect; sect = sect->next)
325 if (!(sect->flags & (SEC_CODE | SEC_DATA)))
326 aout_adobe_write_section (abfd, sect);
328 /* Write final `sentinel` section header (with type of 0). */
329 amt = sizeof (*sentinel);
330 if (bfd_bwrite (sentinel, amt, abfd) != amt)
333 /* Now write out reloc info, followed by syms and strings. */
334 if (bfd_get_symcount (abfd) != 0)
336 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*exec_hdr (abfd))), SEEK_SET)
340 if (! aout_32_write_syms (abfd))
343 if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (*exec_hdr (abfd))), SEEK_SET)
347 for (sect = abfd->sections; sect; sect = sect->next)
348 if (sect->flags & SEC_CODE)
349 if (!aout_32_squirt_out_relocs (abfd, sect))
352 if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (*exec_hdr (abfd))), SEEK_SET)
356 for (sect = abfd->sections; sect; sect = sect->next)
357 if (sect->flags & SEC_DATA)
358 if (!aout_32_squirt_out_relocs (abfd, sect))
366 aout_adobe_set_section_contents (bfd *abfd,
368 const void * location,
372 file_ptr section_start;
375 /* Set by bfd.c handler. */
376 if (! abfd->output_has_begun)
378 /* Assign file offsets to sections. Text sections are first, and
379 are contiguous. Then data sections. Everything else at the end. */
380 section_start = N_TXTOFF (ignore<-->me);
382 for (sect = abfd->sections; sect; sect = sect->next)
384 if (sect->flags & SEC_CODE)
386 sect->filepos = section_start;
387 /* FIXME: Round to alignment. */
388 section_start += sect->size;
392 for (sect = abfd->sections; sect; sect = sect->next)
394 if (sect->flags & SEC_DATA)
396 sect->filepos = section_start;
397 /* FIXME: Round to alignment. */
398 section_start += sect->size;
402 for (sect = abfd->sections; sect; sect = sect->next)
404 if (sect->flags & SEC_HAS_CONTENTS &&
405 !(sect->flags & (SEC_CODE | SEC_DATA)))
407 sect->filepos = section_start;
408 /* FIXME: Round to alignment. */
409 section_start += sect->size;
414 /* Regardless, once we know what we're doing, we might as well get
416 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
422 return bfd_bwrite (location, count, abfd) == count;
426 aout_adobe_set_arch_mach (bfd *abfd,
427 enum bfd_architecture arch,
428 unsigned long machine)
430 if (! bfd_default_set_arch_mach (abfd, arch, machine))
433 if (arch == bfd_arch_unknown
434 || arch == bfd_arch_m68k)
441 aout_adobe_sizeof_headers (bfd *ignore_abfd ATTRIBUTE_UNUSED,
442 struct bfd_link_info *info ATTRIBUTE_UNUSED)
444 return sizeof (struct internal_exec);
447 /* Build the transfer vector for Adobe A.Out files. */
449 #define aout_32_find_line _bfd_nosymbols_find_line
450 #define aout_32_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
451 #define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
452 #define aout_32_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
453 #define aout_32_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
454 #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info
455 #define aout_32_set_arch_mach aout_adobe_set_arch_mach
456 #define aout_32_set_section_contents aout_adobe_set_section_contents
457 #define aout_32_sizeof_headers aout_adobe_sizeof_headers
458 #define aout_32_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
459 #define aout_32_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
460 #define aout_32_bfd_relax_section bfd_generic_relax_section
461 #define aout_32_bfd_gc_sections bfd_generic_gc_sections
462 #define aout_32_bfd_lookup_section_flags bfd_generic_lookup_section_flags
463 #define aout_32_bfd_merge_sections bfd_generic_merge_sections
464 #define aout_32_bfd_is_group_section bfd_generic_is_group_section
465 #define aout_32_bfd_discard_group bfd_generic_discard_group
466 #define aout_32_section_already_linked _bfd_generic_section_already_linked
467 #define aout_32_bfd_define_common_symbol bfd_generic_define_common_symbol
468 #define aout_32_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
469 #define aout_32_bfd_link_add_symbols _bfd_generic_link_add_symbols
470 #define aout_32_bfd_link_just_syms _bfd_generic_link_just_syms
471 #define aout_32_bfd_copy_link_hash_symbol_type \
472 _bfd_generic_copy_link_hash_symbol_type
473 #define aout_32_bfd_final_link _bfd_generic_final_link
474 #define aout_32_bfd_link_split_section _bfd_generic_link_split_section
476 const bfd_target aout_adobe_vec =
478 "a.out.adobe", /* Name. */
479 bfd_target_aout_flavour,
480 BFD_ENDIAN_BIG, /* Data byte order is unknown (big assumed). */
481 BFD_ENDIAN_BIG, /* Header byte order is big. */
482 (HAS_RELOC | EXEC_P | /* Object flags. */
483 HAS_LINENO | HAS_DEBUG |
484 HAS_SYMS | HAS_LOCALS | WP_TEXT ),
486 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_DATA | SEC_RELOC),
487 '_', /* Symbol leading char. */
488 ' ', /* AR_pad_char. */
489 16, /* AR_max_namelen. */
490 0, /* match priority. */
492 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
493 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
494 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
495 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
496 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
497 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
499 {_bfd_dummy_target, aout_adobe_object_p, /* bfd_check_format. */
500 bfd_generic_archive_p, _bfd_dummy_target},
501 {bfd_false, aout_adobe_mkobject, /* bfd_set_format. */
502 _bfd_generic_mkarchive, bfd_false},
503 {bfd_false, aout_adobe_write_object_contents,/* bfd_write_contents. */
504 _bfd_write_archive_contents, bfd_false},
506 BFD_JUMP_TABLE_GENERIC (aout_32),
507 BFD_JUMP_TABLE_COPY (_bfd_generic),
508 BFD_JUMP_TABLE_CORE (_bfd_nocore),
509 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
510 BFD_JUMP_TABLE_SYMBOLS (aout_32),
511 BFD_JUMP_TABLE_RELOCS (aout_32),
512 BFD_JUMP_TABLE_WRITE (aout_32),
513 BFD_JUMP_TABLE_LINK (aout_32),
514 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),