Fix C++ build for Cygwin
[deliverable/binutils-gdb.git] / bfd / aout-adobe.c
CommitLineData
252b5132 1/* BFD back-end for a.out.adobe binaries.
6f2750fe 2 Copyright (C) 1990-2016 Free Software Foundation, Inc.
252b5132
RH
3 Written by Cygnus Support. Based on bout.c.
4
7920ce38 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
7920ce38
NC
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
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
7920ce38 10 (at your option) any later version.
252b5132 11
7920ce38
NC
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.
252b5132 16
7920ce38
NC
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
3e110533 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132 23#include "libbfd.h"
252b5132 24#include "aout/adobe.h"
252b5132 25#include "aout/stab_gnu.h"
1725a96e 26#include "libaout.h" /* BFD a.out internal data structures. */
252b5132 27
336eced2 28/* Forward decl. */
6d00b590 29extern const bfd_target aout_adobe_vec;
252b5132 30
252b5132
RH
31/* Swaps the information in an executable header taken from a raw byte
32 stream memory image, into the internal exec_header structure. */
33
7920ce38
NC
34static void
35aout_adobe_swap_exec_header_in (bfd *abfd,
36 struct external_exec *bytes,
37 struct internal_exec *execp)
252b5132 38{
252b5132 39 /* Now fill in fields in the execp, from the bytes in the raw data. */
dc810e39 40 execp->a_info = H_GET_32 (abfd, bytes->e_info);
252b5132
RH
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
7920ce38
NC
53static void
54aout_adobe_swap_exec_header_out (bfd *abfd,
55 struct internal_exec *execp,
56 struct external_exec *bytes)
252b5132 57{
336eced2
KH
58 /* Now fill in fields in the raw data, from the fields in the exec
59 struct. */
dc810e39 60 H_PUT_32 (abfd, execp->a_info , bytes->e_info);
252b5132
RH
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
252b5132
RH
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
73static const bfd_target *
7920ce38 74aout_adobe_callback (bfd *abfd)
252b5132
RH
75{
76 struct internal_exec *execp = exec_hdr (abfd);
77 asection *sect;
78 struct external_segdesc ext[1];
79 char *section_name;
1725a96e 80 char try_again[30]; /* Name and number. */
252b5132
RH
81 char *newname;
82 int trynum;
83 flagword flags;
84
85 /* Architecture and machine type -- unknown in this format. */
dc810e39 86 bfd_set_arch_mach (abfd, bfd_arch_unknown, 0L);
252b5132
RH
87
88 /* The positions of the string table and symbol table. */
bbb1afc8
AM
89 obj_str_filepos (abfd) = N_STROFF (execp);
90 obj_sym_filepos (abfd) = N_SYMOFF (execp);
252b5132
RH
91
92 /* Suck up the section information from the file, one section at a time. */
336eced2
KH
93 for (;;)
94 {
dc810e39 95 bfd_size_type amt = sizeof (*ext);
7920ce38 96 if (bfd_bread ( ext, amt, abfd) != amt)
336eced2
KH
97 {
98 if (bfd_get_error () != bfd_error_system_call)
99 bfd_set_error (bfd_error_wrong_format);
1725a96e 100
7920ce38 101 return NULL;
336eced2
KH
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)
d003868e
AM
125 (_("%B: Unknown section type in a.out.adobe file: %x\n"),
126 abfd, ext->e_type[0]);
336eced2
KH
127 goto no_more_sections;
128 }
129
130 /* First one is called ".text" or whatever; subsequent ones are
131 ".text1", ".text2", ... */
336eced2 132 bfd_set_error (bfd_error_no_error);
117ed4f8 133 sect = bfd_make_section_with_flags (abfd, section_name, flags);
336eced2 134 trynum = 0;
1725a96e 135
336eced2
KH
136 while (!sect)
137 {
138 if (bfd_get_error () != bfd_error_no_error)
139 /* Some other error -- slide into the sunset. */
7920ce38 140 return NULL;
336eced2 141 sprintf (try_again, "%s%d", section_name, ++trynum);
117ed4f8 142 sect = bfd_make_section_with_flags (abfd, try_again, flags);
336eced2
KH
143 }
144
145 /* Fix the name, if it is a sprintf'd name. */
146 if (sect->name == try_again)
147 {
dc810e39 148 amt = strlen (sect->name);
7920ce38 149 newname = bfd_zalloc (abfd, amt);
336eced2 150 if (newname == NULL)
7920ce38 151 return NULL;
336eced2
KH
152 strcpy (newname, sect->name);
153 sect->name = newname;
154 }
155
336eced2 156 /* Assumed big-endian. */
eea6121a
AM
157 sect->size = ((ext->e_size[0] << 8)
158 | ext->e_size[1] << 8
159 | ext->e_size[2]);
dc810e39
AM
160 sect->vma = H_GET_32 (abfd, ext->e_virtbase);
161 sect->filepos = H_GET_32 (abfd, ext->e_filebase);
336eced2
KH
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:
bbb1afc8 169 sect->rel_filepos = N_TRELOFF (execp);
336eced2
KH
170 sect->reloc_count = execp->a_trsize;
171 break;
172
173 case N_DATA:
bbb1afc8 174 sect->rel_filepos = N_DRELOFF (execp);
336eced2
KH
175 sect->reloc_count = execp->a_drsize;
176 break;
1725a96e
NC
177
178 default:
179 break;
336eced2 180 }
252b5132 181 }
336eced2 182 no_more_sections:
252b5132 183
336eced2
KH
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;
252b5132
RH
189
190 return abfd->xvec;
191}
192
7920ce38
NC
193static const bfd_target *
194aout_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
bbb1afc8 216 if (N_BADMAG (&anexec))
7920ce38
NC
217 {
218 targ = getenv ("GNUTARGET");
6d00b590 219 if (targ && !strcmp (targ, aout_adobe_vec.name))
7920ce38
NC
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
1725a96e 233struct bout_data_struct
7920ce38
NC
234{
235 struct aoutdata a;
236 struct internal_exec e;
237};
252b5132 238
b34976b6 239static bfd_boolean
7920ce38 240aout_adobe_mkobject (bfd *abfd)
252b5132
RH
241{
242 struct bout_data_struct *rawptr;
dc810e39 243 bfd_size_type amt = sizeof (struct bout_data_struct);
252b5132 244
7920ce38 245 rawptr = bfd_zalloc (abfd, amt);
252b5132 246 if (rawptr == NULL)
b34976b6 247 return FALSE;
252b5132
RH
248
249 abfd->tdata.bout_data = rawptr;
250 exec_hdr (abfd) = &rawptr->e;
251
336eced2
KH
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;
252b5132 257
b34976b6 258 return TRUE;
252b5132
RH
259}
260
7920ce38
NC
261static void
262aout_adobe_write_section (bfd *abfd ATTRIBUTE_UNUSED,
263 sec_ptr sect ATTRIBUTE_UNUSED)
264{
265 /* FIXME XXX. */
266}
267
b34976b6 268static bfd_boolean
7920ce38 269aout_adobe_write_object_contents (bfd *abfd)
252b5132
RH
270{
271 struct external_exec swapped_hdr;
336eced2 272 static struct external_segdesc sentinel[1]; /* Initialized to zero. */
252b5132 273 asection *sect;
dc810e39 274 bfd_size_type amt;
252b5132
RH
275
276 exec_hdr (abfd)->a_info = ZMAGIC;
277
c4dfa77f 278 /* Calculate text size as total of text sections, etc. */
252b5132
RH
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
336eced2
KH
285 for (sect = abfd->sections; sect; sect = sect->next)
286 {
287 if (sect->flags & SEC_CODE)
288 {
eea6121a 289 exec_hdr (abfd)->a_text += sect->size;
336eced2
KH
290 exec_hdr (abfd)->a_trsize += sect->reloc_count *
291 sizeof (struct reloc_std_external);
292 }
293 else if (sect->flags & SEC_DATA)
294 {
eea6121a 295 exec_hdr (abfd)->a_data += sect->size;
336eced2
KH
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))
7920ce38 300 exec_hdr (abfd)->a_bss += sect->size;
252b5132 301 }
252b5132
RH
302
303 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd)
336eced2 304 * sizeof (struct external_nlist);
252b5132
RH
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
dc810e39 309 amt = EXEC_BYTES_SIZE;
252b5132 310 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
7920ce38 311 || bfd_bwrite (& swapped_hdr, amt, abfd) != amt)
b34976b6 312 return FALSE;
252b5132
RH
313
314 /* Now write out the section information. Text first, data next, rest
315 afterward. */
336eced2 316 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
317 if (sect->flags & SEC_CODE)
318 aout_adobe_write_section (abfd, sect);
319
336eced2 320 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
321 if (sect->flags & SEC_DATA)
322 aout_adobe_write_section (abfd, sect);
323
336eced2 324 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
325 if (!(sect->flags & (SEC_CODE | SEC_DATA)))
326 aout_adobe_write_section (abfd, sect);
252b5132
RH
327
328 /* Write final `sentinel` section header (with type of 0). */
dc810e39 329 amt = sizeof (*sentinel);
7920ce38 330 if (bfd_bwrite (sentinel, amt, abfd) != amt)
b34976b6 331 return FALSE;
252b5132 332
336eced2 333 /* Now write out reloc info, followed by syms and strings. */
c4dfa77f 334 if (bfd_get_symcount (abfd) != 0)
252b5132 335 {
bbb1afc8 336 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (exec_hdr (abfd))), SEEK_SET)
252b5132 337 != 0)
b34976b6 338 return FALSE;
252b5132
RH
339
340 if (! aout_32_write_syms (abfd))
b34976b6 341 return FALSE;
252b5132 342
bbb1afc8 343 if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (exec_hdr (abfd))), SEEK_SET)
252b5132 344 != 0)
b34976b6 345 return FALSE;
252b5132 346
336eced2 347 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
348 if (sect->flags & SEC_CODE)
349 if (!aout_32_squirt_out_relocs (abfd, sect))
b34976b6 350 return FALSE;
252b5132 351
bbb1afc8 352 if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (exec_hdr (abfd))), SEEK_SET)
252b5132 353 != 0)
b34976b6 354 return FALSE;
252b5132 355
336eced2 356 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
357 if (sect->flags & SEC_DATA)
358 if (!aout_32_squirt_out_relocs (abfd, sect))
b34976b6 359 return FALSE;
252b5132 360 }
1725a96e 361
b34976b6 362 return TRUE;
252b5132 363}
252b5132 364\f
b34976b6 365static bfd_boolean
7920ce38
NC
366aout_adobe_set_section_contents (bfd *abfd,
367 asection *section,
368 const void * location,
369 file_ptr offset,
370 bfd_size_type count)
252b5132
RH
371{
372 file_ptr section_start;
373 sec_ptr sect;
374
336eced2 375 /* Set by bfd.c handler. */
82e51918 376 if (! abfd->output_has_begun)
336eced2
KH
377 {
378 /* Assign file offsets to sections. Text sections are first, and
379 are contiguous. Then data sections. Everything else at the end. */
bbb1afc8 380 section_start = N_TXTOFF (0);
336eced2
KH
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. */
eea6121a 388 section_start += sect->size;
336eced2
KH
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. */
eea6121a 398 section_start += sect->size;
336eced2
KH
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. */
eea6121a 409 section_start += sect->size;
336eced2
KH
410 }
411 }
252b5132 412 }
252b5132 413
336eced2
KH
414 /* Regardless, once we know what we're doing, we might as well get
415 going. */
252b5132 416 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
b34976b6 417 return FALSE;
252b5132 418
dc810e39 419 if (count == 0)
b34976b6 420 return TRUE;
1725a96e 421
7920ce38 422 return bfd_bwrite (location, count, abfd) == count;
252b5132
RH
423}
424
b34976b6 425static bfd_boolean
7920ce38
NC
426aout_adobe_set_arch_mach (bfd *abfd,
427 enum bfd_architecture arch,
428 unsigned long machine)
252b5132
RH
429{
430 if (! bfd_default_set_arch_mach (abfd, arch, machine))
b34976b6 431 return FALSE;
252b5132
RH
432
433 if (arch == bfd_arch_unknown
434 || arch == bfd_arch_m68k)
b34976b6 435 return TRUE;
252b5132 436
b34976b6 437 return FALSE;
252b5132
RH
438}
439
c4dfa77f 440static int
7920ce38 441aout_adobe_sizeof_headers (bfd *ignore_abfd ATTRIBUTE_UNUSED,
a6b96beb 442 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 443{
beb0d161 444 return sizeof (struct internal_exec);
252b5132
RH
445}
446
252b5132
RH
447/* Build the transfer vector for Adobe A.Out files. */
448
9c461f7d 449#define aout_32_find_line _bfd_nosymbols_find_line
60bb06bc 450#define aout_32_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
9c461f7d
AM
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
7920ce38
NC
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
ae17ab41 462#define aout_32_bfd_lookup_section_flags bfd_generic_lookup_section_flags
7920ce38
NC
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
3023e3f6 467#define aout_32_bfd_define_common_symbol bfd_generic_define_common_symbol
7920ce38 468#define aout_32_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
7920ce38
NC
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
1338dd10
PB
471#define aout_32_bfd_copy_link_hash_symbol_type \
472 _bfd_generic_copy_link_hash_symbol_type
7920ce38
NC
473#define aout_32_bfd_final_link _bfd_generic_final_link
474#define aout_32_bfd_link_split_section _bfd_generic_link_split_section
4f3b23b3 475#define aout_32_bfd_link_check_relocs _bfd_generic_link_check_relocs
252b5132 476
6d00b590 477const bfd_target aout_adobe_vec =
7920ce38
NC
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. */
0aabe54e 491 0, /* match priority. */
7920ce38
NC
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};
This page took 0.714504 seconds and 4 git commands to generate.