1 /* BFD back-end for a.out.adobe binaries.
2 Copyright (C) 1990-2016 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 3 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "libbfd.h"
24 #include "aout/adobe.h"
25 #include "aout/stab_gnu.h"
26 #include "libaout.h" /* BFD a.out internal data structures. */
27
28 /* Forward decl. */
29 extern const bfd_target aout_adobe_vec;
30
31 /* Swaps the information in an executable header taken from a raw byte
32 stream memory image, into the internal exec_header structure. */
33
34 static void
aout_adobe_swap_exec_header_in(bfd * abfd,struct external_exec * bytes,struct internal_exec * execp)35 aout_adobe_swap_exec_header_in (bfd *abfd,
36 struct external_exec *bytes,
37 struct internal_exec *execp)
38 {
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);
48 }
49
50 /* Swaps the information in an internal exec header structure into the
51 supplied buffer ready for writing to disk. */
52
53 static void
aout_adobe_swap_exec_header_out(bfd * abfd,struct internal_exec * execp,struct external_exec * bytes)54 aout_adobe_swap_exec_header_out (bfd *abfd,
55 struct internal_exec *execp,
56 struct external_exec *bytes)
57 {
58 /* Now fill in fields in the raw data, from the fields in the exec
59 struct. */
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);
68 }
69
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. */
72
73 static const bfd_target *
aout_adobe_callback(bfd * abfd)74 aout_adobe_callback (bfd *abfd)
75 {
76 struct internal_exec *execp = exec_hdr (abfd);
77 asection *sect;
78 struct external_segdesc ext[1];
79 char *section_name;
80 char try_again[30]; /* Name and number. */
81 char *newname;
82 int trynum;
83 flagword flags;
84
85 /* Architecture and machine type -- unknown in this format. */
86 bfd_set_arch_mach (abfd, bfd_arch_unknown, 0L);
87
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);
91
92 /* Suck up the section information from the file, one section at a time. */
93 for (;;)
94 {
95 bfd_size_type amt = sizeof (*ext);
96 if (bfd_bread ( ext, amt, abfd) != amt)
97 {
98 if (bfd_get_error () != bfd_error_system_call)
99 bfd_set_error (bfd_error_wrong_format);
100
101 return NULL;
102 }
103 switch (ext->e_type[0])
104 {
105 case N_TEXT:
106 section_name = ".text";
107 flags = SEC_CODE | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
108 break;
109
110 case N_DATA:
111 section_name = ".data";
112 flags = SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
113 break;
114
115 case N_BSS:
116 section_name = ".bss";
117 flags = SEC_DATA | SEC_HAS_CONTENTS;
118 break;
119
120 case 0:
121 goto no_more_sections;
122
123 default:
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;
128 }
129
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);
134 trynum = 0;
135
136 while (!sect)
137 {
138 if (bfd_get_error () != bfd_error_no_error)
139 /* Some other error -- slide into the sunset. */
140 return NULL;
141 sprintf (try_again, "%s%d", section_name, ++trynum);
142 sect = bfd_make_section_with_flags (abfd, try_again, flags);
143 }
144
145 /* Fix the name, if it is a sprintf'd name. */
146 if (sect->name == try_again)
147 {
148 amt = strlen (sect->name);
149 newname = bfd_zalloc (abfd, amt);
150 if (newname == NULL)
151 return NULL;
152 strcpy (newname, sect->name);
153 sect->name = newname;
154 }
155
156 /* Assumed big-endian. */
157 sect->size = ((ext->e_size[0] << 8)
158 | ext->e_size[1] << 8
159 | ext->e_size[2]);
160 sect->vma = H_GET_32 (abfd, ext->e_virtbase);
161 sect->filepos = H_GET_32 (abfd, ext->e_filebase);
162 /* FIXME XXX alignment? */
163
164 /* Set relocation information for first section of each type. */
165 if (trynum == 0)
166 switch (ext->e_type[0])
167 {
168 case N_TEXT:
169 sect->rel_filepos = N_TRELOFF (execp);
170 sect->reloc_count = execp->a_trsize;
171 break;
172
173 case N_DATA:
174 sect->rel_filepos = N_DRELOFF (execp);
175 sect->reloc_count = execp->a_drsize;
176 break;
177
178 default:
179 break;
180 }
181 }
182 no_more_sections:
183
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;
189
190 return abfd->xvec;
191 }
192
193 static const bfd_target *
aout_adobe_object_p(bfd * abfd)194 aout_adobe_object_p (bfd *abfd)
195 {
196 struct internal_exec anexec;
197 struct external_exec exec_bytes;
198 char *targ;
199 bfd_size_type amt = EXEC_BYTES_SIZE;
200
201 if (bfd_bread (& exec_bytes, amt, abfd) != amt)
202 {
203 if (bfd_get_error () != bfd_error_system_call)
204 bfd_set_error (bfd_error_wrong_format);
205 return NULL;
206 }
207
208 anexec.a_info = H_GET_32 (abfd, exec_bytes.e_info);
209
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! */
215
216 if (N_BADMAG (&anexec))
217 {
218 targ = getenv ("GNUTARGET");
219 if (targ && !strcmp (targ, aout_adobe_vec.name))
220 /* Just continue anyway, if specifically set to this format. */
221 ;
222 else
223 {
224 bfd_set_error (bfd_error_wrong_format);
225 return NULL;
226 }
227 }
228
229 aout_adobe_swap_exec_header_in (abfd, &exec_bytes, &anexec);
230 return aout_32_some_aout_object_p (abfd, &anexec, aout_adobe_callback);
231 }
232
233 struct bout_data_struct
234 {
235 struct aoutdata a;
236 struct internal_exec e;
237 };
238
239 static bfd_boolean
aout_adobe_mkobject(bfd * abfd)240 aout_adobe_mkobject (bfd *abfd)
241 {
242 struct bout_data_struct *rawptr;
243 bfd_size_type amt = sizeof (struct bout_data_struct);
244
245 rawptr = bfd_zalloc (abfd, amt);
246 if (rawptr == NULL)
247 return FALSE;
248
249 abfd->tdata.bout_data = rawptr;
250 exec_hdr (abfd) = &rawptr->e;
251
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;
257
258 return TRUE;
259 }
260
261 static void
aout_adobe_write_section(bfd * abfd ATTRIBUTE_UNUSED,sec_ptr sect ATTRIBUTE_UNUSED)262 aout_adobe_write_section (bfd *abfd ATTRIBUTE_UNUSED,
263 sec_ptr sect ATTRIBUTE_UNUSED)
264 {
265 /* FIXME XXX. */
266 }
267
268 static bfd_boolean
aout_adobe_write_object_contents(bfd * abfd)269 aout_adobe_write_object_contents (bfd *abfd)
270 {
271 struct external_exec swapped_hdr;
272 static struct external_segdesc sentinel[1]; /* Initialized to zero. */
273 asection *sect;
274 bfd_size_type amt;
275
276 exec_hdr (abfd)->a_info = ZMAGIC;
277
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;
284
285 for (sect = abfd->sections; sect; sect = sect->next)
286 {
287 if (sect->flags & SEC_CODE)
288 {
289 exec_hdr (abfd)->a_text += sect->size;
290 exec_hdr (abfd)->a_trsize += sect->reloc_count *
291 sizeof (struct reloc_std_external);
292 }
293 else if (sect->flags & SEC_DATA)
294 {
295 exec_hdr (abfd)->a_data += sect->size;
296 exec_hdr (abfd)->a_drsize += sect->reloc_count *
297 sizeof (struct reloc_std_external);
298 }
299 else if (sect->flags & SEC_ALLOC && !(sect->flags & SEC_LOAD))
300 exec_hdr (abfd)->a_bss += sect->size;
301 }
302
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);
306
307 aout_adobe_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
308
309 amt = EXEC_BYTES_SIZE;
310 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
311 || bfd_bwrite (& swapped_hdr, amt, abfd) != amt)
312 return FALSE;
313
314 /* Now write out the section information. Text first, data next, rest
315 afterward. */
316 for (sect = abfd->sections; sect; sect = sect->next)
317 if (sect->flags & SEC_CODE)
318 aout_adobe_write_section (abfd, sect);
319
320 for (sect = abfd->sections; sect; sect = sect->next)
321 if (sect->flags & SEC_DATA)
322 aout_adobe_write_section (abfd, sect);
323
324 for (sect = abfd->sections; sect; sect = sect->next)
325 if (!(sect->flags & (SEC_CODE | SEC_DATA)))
326 aout_adobe_write_section (abfd, sect);
327
328 /* Write final `sentinel` section header (with type of 0). */
329 amt = sizeof (*sentinel);
330 if (bfd_bwrite (sentinel, amt, abfd) != amt)
331 return FALSE;
332
333 /* Now write out reloc info, followed by syms and strings. */
334 if (bfd_get_symcount (abfd) != 0)
335 {
336 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (exec_hdr (abfd))), SEEK_SET)
337 != 0)
338 return FALSE;
339
340 if (! aout_32_write_syms (abfd))
341 return FALSE;
342
343 if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (exec_hdr (abfd))), SEEK_SET)
344 != 0)
345 return FALSE;
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 if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (exec_hdr (abfd))), SEEK_SET)
353 != 0)
354 return FALSE;
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 return TRUE;
363 }
364
365 static bfd_boolean
aout_adobe_set_section_contents(bfd * abfd,asection * section,const void * location,file_ptr offset,bfd_size_type count)366 aout_adobe_set_section_contents (bfd *abfd,
367 asection *section,
368 const void * location,
369 file_ptr offset,
370 bfd_size_type count)
371 {
372 file_ptr section_start;
373 sec_ptr sect;
374
375 /* Set by bfd.c handler. */
376 if (! abfd->output_has_begun)
377 {
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 (0);
381
382 for (sect = abfd->sections; sect; sect = sect->next)
383 {
384 if (sect->flags & SEC_CODE)
385 {
386 sect->filepos = section_start;
387 /* FIXME: Round to alignment. */
388 section_start += sect->size;
389 }
390 }
391
392 for (sect = abfd->sections; sect; sect = sect->next)
393 {
394 if (sect->flags & SEC_DATA)
395 {
396 sect->filepos = section_start;
397 /* FIXME: Round to alignment. */
398 section_start += sect->size;
399 }
400 }
401
402 for (sect = abfd->sections; sect; sect = sect->next)
403 {
404 if (sect->flags & SEC_HAS_CONTENTS &&
405 !(sect->flags & (SEC_CODE | SEC_DATA)))
406 {
407 sect->filepos = section_start;
408 /* FIXME: Round to alignment. */
409 section_start += sect->size;
410 }
411 }
412 }
413
414 /* Regardless, once we know what we're doing, we might as well get
415 going. */
416 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
417 return FALSE;
418
419 if (count == 0)
420 return TRUE;
421
422 return bfd_bwrite (location, count, abfd) == count;
423 }
424
425 static bfd_boolean
aout_adobe_set_arch_mach(bfd * abfd,enum bfd_architecture arch,unsigned long machine)426 aout_adobe_set_arch_mach (bfd *abfd,
427 enum bfd_architecture arch,
428 unsigned long machine)
429 {
430 if (! bfd_default_set_arch_mach (abfd, arch, machine))
431 return FALSE;
432
433 if (arch == bfd_arch_unknown
434 || arch == bfd_arch_m68k)
435 return TRUE;
436
437 return FALSE;
438 }
439
440 static int
aout_adobe_sizeof_headers(bfd * ignore_abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED)441 aout_adobe_sizeof_headers (bfd *ignore_abfd ATTRIBUTE_UNUSED,
442 struct bfd_link_info *info ATTRIBUTE_UNUSED)
443 {
444 return sizeof (struct internal_exec);
445 }
446
447 /* Build the transfer vector for Adobe A.Out files. */
448
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
475 #define aout_32_bfd_link_check_relocs _bfd_generic_link_check_relocs
476
477 const bfd_target aout_adobe_vec =
478 {
479 "a.out.adobe", /* Name. */
480 bfd_target_aout_flavour,
481 BFD_ENDIAN_BIG, /* Data byte order is unknown (big assumed). */
482 BFD_ENDIAN_BIG, /* Header byte order is big. */
483 (HAS_RELOC | EXEC_P | /* Object flags. */
484 HAS_LINENO | HAS_DEBUG |
485 HAS_SYMS | HAS_LOCALS | WP_TEXT ),
486 /* section flags */
487 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_DATA | SEC_RELOC),
488 '_', /* Symbol leading char. */
489 ' ', /* AR_pad_char. */
490 16, /* AR_max_namelen. */
491 0, /* match priority. */
492
493 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
494 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
495 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
496 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
497 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
498 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
499
500 {_bfd_dummy_target, aout_adobe_object_p, /* bfd_check_format. */
501 bfd_generic_archive_p, _bfd_dummy_target},
502 {bfd_false, aout_adobe_mkobject, /* bfd_set_format. */
503 _bfd_generic_mkarchive, bfd_false},
504 {bfd_false, aout_adobe_write_object_contents,/* bfd_write_contents. */
505 _bfd_write_archive_contents, bfd_false},
506
507 BFD_JUMP_TABLE_GENERIC (aout_32),
508 BFD_JUMP_TABLE_COPY (_bfd_generic),
509 BFD_JUMP_TABLE_CORE (_bfd_nocore),
510 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
511 BFD_JUMP_TABLE_SYMBOLS (aout_32),
512 BFD_JUMP_TABLE_RELOCS (aout_32),
513 BFD_JUMP_TABLE_WRITE (aout_32),
514 BFD_JUMP_TABLE_LINK (aout_32),
515 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
516
517 NULL,
518
519 NULL
520 };
521