ELF/BFD: Limit `_bfd_elf_link_renumber_dynsyms' call in section GC
[deliverable/binutils-gdb.git] / bfd / aout-adobe.c
CommitLineData
252b5132 1/* BFD back-end for a.out.adobe binaries.
2571583a 2 Copyright (C) 1990-2017 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:
4eca0228 124 _bfd_error_handler
695344c0 125 /* xgettext:c-format */
d003868e
AM
126 (_("%B: Unknown section type in a.out.adobe file: %x\n"),
127 abfd, ext->e_type[0]);
336eced2
KH
128 goto no_more_sections;
129 }
130
131 /* First one is called ".text" or whatever; subsequent ones are
132 ".text1", ".text2", ... */
336eced2 133 bfd_set_error (bfd_error_no_error);
117ed4f8 134 sect = bfd_make_section_with_flags (abfd, section_name, flags);
336eced2 135 trynum = 0;
1725a96e 136
336eced2
KH
137 while (!sect)
138 {
139 if (bfd_get_error () != bfd_error_no_error)
140 /* Some other error -- slide into the sunset. */
7920ce38 141 return NULL;
336eced2 142 sprintf (try_again, "%s%d", section_name, ++trynum);
117ed4f8 143 sect = bfd_make_section_with_flags (abfd, try_again, flags);
336eced2
KH
144 }
145
146 /* Fix the name, if it is a sprintf'd name. */
147 if (sect->name == try_again)
148 {
dc810e39 149 amt = strlen (sect->name);
7920ce38 150 newname = bfd_zalloc (abfd, amt);
336eced2 151 if (newname == NULL)
7920ce38 152 return NULL;
336eced2
KH
153 strcpy (newname, sect->name);
154 sect->name = newname;
155 }
156
336eced2 157 /* Assumed big-endian. */
eea6121a
AM
158 sect->size = ((ext->e_size[0] << 8)
159 | ext->e_size[1] << 8
160 | ext->e_size[2]);
dc810e39
AM
161 sect->vma = H_GET_32 (abfd, ext->e_virtbase);
162 sect->filepos = H_GET_32 (abfd, ext->e_filebase);
336eced2
KH
163 /* FIXME XXX alignment? */
164
165 /* Set relocation information for first section of each type. */
166 if (trynum == 0)
167 switch (ext->e_type[0])
168 {
169 case N_TEXT:
bbb1afc8 170 sect->rel_filepos = N_TRELOFF (execp);
336eced2
KH
171 sect->reloc_count = execp->a_trsize;
172 break;
173
174 case N_DATA:
bbb1afc8 175 sect->rel_filepos = N_DRELOFF (execp);
336eced2
KH
176 sect->reloc_count = execp->a_drsize;
177 break;
1725a96e
NC
178
179 default:
180 break;
336eced2 181 }
252b5132 182 }
336eced2 183 no_more_sections:
252b5132 184
336eced2
KH
185 adata (abfd).reloc_entry_size = sizeof (struct reloc_std_external);
186 adata (abfd).symbol_entry_size = sizeof (struct external_nlist);
187 adata (abfd).page_size = 1; /* Not applicable. */
188 adata (abfd).segment_size = 1; /* Not applicable. */
189 adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
252b5132
RH
190
191 return abfd->xvec;
192}
193
7920ce38
NC
194static const bfd_target *
195aout_adobe_object_p (bfd *abfd)
196{
197 struct internal_exec anexec;
198 struct external_exec exec_bytes;
199 char *targ;
200 bfd_size_type amt = EXEC_BYTES_SIZE;
201
202 if (bfd_bread (& exec_bytes, amt, abfd) != amt)
203 {
204 if (bfd_get_error () != bfd_error_system_call)
205 bfd_set_error (bfd_error_wrong_format);
206 return NULL;
207 }
208
209 anexec.a_info = H_GET_32 (abfd, exec_bytes.e_info);
210
211 /* Normally we just compare for the magic number.
212 However, a bunch of Adobe tools aren't fixed up yet; they generate
213 files using ZMAGIC(!).
214 If the environment variable GNUTARGET is set to "a.out.adobe", we will
215 take just about any a.out file as an Adobe a.out file. FIXME! */
216
bbb1afc8 217 if (N_BADMAG (&anexec))
7920ce38
NC
218 {
219 targ = getenv ("GNUTARGET");
6d00b590 220 if (targ && !strcmp (targ, aout_adobe_vec.name))
7920ce38
NC
221 /* Just continue anyway, if specifically set to this format. */
222 ;
223 else
224 {
225 bfd_set_error (bfd_error_wrong_format);
226 return NULL;
227 }
228 }
229
230 aout_adobe_swap_exec_header_in (abfd, &exec_bytes, &anexec);
231 return aout_32_some_aout_object_p (abfd, &anexec, aout_adobe_callback);
232}
233
1725a96e 234struct bout_data_struct
7920ce38
NC
235{
236 struct aoutdata a;
237 struct internal_exec e;
238};
252b5132 239
b34976b6 240static bfd_boolean
7920ce38 241aout_adobe_mkobject (bfd *abfd)
252b5132
RH
242{
243 struct bout_data_struct *rawptr;
dc810e39 244 bfd_size_type amt = sizeof (struct bout_data_struct);
252b5132 245
7920ce38 246 rawptr = bfd_zalloc (abfd, amt);
252b5132 247 if (rawptr == NULL)
b34976b6 248 return FALSE;
252b5132
RH
249
250 abfd->tdata.bout_data = rawptr;
251 exec_hdr (abfd) = &rawptr->e;
252
336eced2
KH
253 adata (abfd).reloc_entry_size = sizeof (struct reloc_std_external);
254 adata (abfd).symbol_entry_size = sizeof (struct external_nlist);
255 adata (abfd).page_size = 1; /* Not applicable. */
256 adata (abfd).segment_size = 1; /* Not applicable. */
257 adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
252b5132 258
b34976b6 259 return TRUE;
252b5132
RH
260}
261
7920ce38
NC
262static void
263aout_adobe_write_section (bfd *abfd ATTRIBUTE_UNUSED,
264 sec_ptr sect ATTRIBUTE_UNUSED)
265{
266 /* FIXME XXX. */
267}
268
b34976b6 269static bfd_boolean
7920ce38 270aout_adobe_write_object_contents (bfd *abfd)
252b5132
RH
271{
272 struct external_exec swapped_hdr;
336eced2 273 static struct external_segdesc sentinel[1]; /* Initialized to zero. */
252b5132 274 asection *sect;
dc810e39 275 bfd_size_type amt;
252b5132
RH
276
277 exec_hdr (abfd)->a_info = ZMAGIC;
278
c4dfa77f 279 /* Calculate text size as total of text sections, etc. */
252b5132
RH
280 exec_hdr (abfd)->a_text = 0;
281 exec_hdr (abfd)->a_data = 0;
282 exec_hdr (abfd)->a_bss = 0;
283 exec_hdr (abfd)->a_trsize = 0;
284 exec_hdr (abfd)->a_drsize = 0;
285
336eced2
KH
286 for (sect = abfd->sections; sect; sect = sect->next)
287 {
288 if (sect->flags & SEC_CODE)
289 {
eea6121a 290 exec_hdr (abfd)->a_text += sect->size;
336eced2
KH
291 exec_hdr (abfd)->a_trsize += sect->reloc_count *
292 sizeof (struct reloc_std_external);
293 }
294 else if (sect->flags & SEC_DATA)
295 {
eea6121a 296 exec_hdr (abfd)->a_data += sect->size;
336eced2
KH
297 exec_hdr (abfd)->a_drsize += sect->reloc_count *
298 sizeof (struct reloc_std_external);
299 }
300 else if (sect->flags & SEC_ALLOC && !(sect->flags & SEC_LOAD))
7920ce38 301 exec_hdr (abfd)->a_bss += sect->size;
252b5132 302 }
252b5132
RH
303
304 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd)
336eced2 305 * sizeof (struct external_nlist);
252b5132
RH
306 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
307
308 aout_adobe_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
309
dc810e39 310 amt = EXEC_BYTES_SIZE;
252b5132 311 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
7920ce38 312 || bfd_bwrite (& swapped_hdr, amt, abfd) != amt)
b34976b6 313 return FALSE;
252b5132
RH
314
315 /* Now write out the section information. Text first, data next, rest
316 afterward. */
336eced2 317 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
318 if (sect->flags & SEC_CODE)
319 aout_adobe_write_section (abfd, sect);
320
336eced2 321 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
322 if (sect->flags & SEC_DATA)
323 aout_adobe_write_section (abfd, sect);
324
336eced2 325 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
326 if (!(sect->flags & (SEC_CODE | SEC_DATA)))
327 aout_adobe_write_section (abfd, sect);
252b5132
RH
328
329 /* Write final `sentinel` section header (with type of 0). */
dc810e39 330 amt = sizeof (*sentinel);
7920ce38 331 if (bfd_bwrite (sentinel, amt, abfd) != amt)
b34976b6 332 return FALSE;
252b5132 333
336eced2 334 /* Now write out reloc info, followed by syms and strings. */
c4dfa77f 335 if (bfd_get_symcount (abfd) != 0)
252b5132 336 {
bbb1afc8 337 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (exec_hdr (abfd))), SEEK_SET)
252b5132 338 != 0)
b34976b6 339 return FALSE;
252b5132
RH
340
341 if (! aout_32_write_syms (abfd))
b34976b6 342 return FALSE;
252b5132 343
bbb1afc8 344 if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (exec_hdr (abfd))), SEEK_SET)
252b5132 345 != 0)
b34976b6 346 return FALSE;
252b5132 347
336eced2 348 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
349 if (sect->flags & SEC_CODE)
350 if (!aout_32_squirt_out_relocs (abfd, sect))
b34976b6 351 return FALSE;
252b5132 352
bbb1afc8 353 if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (exec_hdr (abfd))), SEEK_SET)
252b5132 354 != 0)
b34976b6 355 return FALSE;
252b5132 356
336eced2 357 for (sect = abfd->sections; sect; sect = sect->next)
1725a96e
NC
358 if (sect->flags & SEC_DATA)
359 if (!aout_32_squirt_out_relocs (abfd, sect))
b34976b6 360 return FALSE;
252b5132 361 }
1725a96e 362
b34976b6 363 return TRUE;
252b5132 364}
252b5132 365\f
b34976b6 366static bfd_boolean
7920ce38
NC
367aout_adobe_set_section_contents (bfd *abfd,
368 asection *section,
369 const void * location,
370 file_ptr offset,
371 bfd_size_type count)
252b5132
RH
372{
373 file_ptr section_start;
374 sec_ptr sect;
375
336eced2 376 /* Set by bfd.c handler. */
82e51918 377 if (! abfd->output_has_begun)
336eced2
KH
378 {
379 /* Assign file offsets to sections. Text sections are first, and
380 are contiguous. Then data sections. Everything else at the end. */
bbb1afc8 381 section_start = N_TXTOFF (0);
336eced2
KH
382
383 for (sect = abfd->sections; sect; sect = sect->next)
384 {
385 if (sect->flags & SEC_CODE)
386 {
387 sect->filepos = section_start;
388 /* FIXME: Round to alignment. */
eea6121a 389 section_start += sect->size;
336eced2
KH
390 }
391 }
392
393 for (sect = abfd->sections; sect; sect = sect->next)
394 {
395 if (sect->flags & SEC_DATA)
396 {
397 sect->filepos = section_start;
398 /* FIXME: Round to alignment. */
eea6121a 399 section_start += sect->size;
336eced2
KH
400 }
401 }
402
403 for (sect = abfd->sections; sect; sect = sect->next)
404 {
405 if (sect->flags & SEC_HAS_CONTENTS &&
406 !(sect->flags & (SEC_CODE | SEC_DATA)))
407 {
408 sect->filepos = section_start;
409 /* FIXME: Round to alignment. */
eea6121a 410 section_start += sect->size;
336eced2
KH
411 }
412 }
252b5132 413 }
252b5132 414
336eced2
KH
415 /* Regardless, once we know what we're doing, we might as well get
416 going. */
252b5132 417 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
b34976b6 418 return FALSE;
252b5132 419
dc810e39 420 if (count == 0)
b34976b6 421 return TRUE;
1725a96e 422
7920ce38 423 return bfd_bwrite (location, count, abfd) == count;
252b5132
RH
424}
425
b34976b6 426static bfd_boolean
7920ce38
NC
427aout_adobe_set_arch_mach (bfd *abfd,
428 enum bfd_architecture arch,
429 unsigned long machine)
252b5132
RH
430{
431 if (! bfd_default_set_arch_mach (abfd, arch, machine))
b34976b6 432 return FALSE;
252b5132
RH
433
434 if (arch == bfd_arch_unknown
435 || arch == bfd_arch_m68k)
b34976b6 436 return TRUE;
252b5132 437
b34976b6 438 return FALSE;
252b5132
RH
439}
440
c4dfa77f 441static int
7920ce38 442aout_adobe_sizeof_headers (bfd *ignore_abfd ATTRIBUTE_UNUSED,
a6b96beb 443 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132 444{
beb0d161 445 return sizeof (struct internal_exec);
252b5132
RH
446}
447
252b5132
RH
448/* Build the transfer vector for Adobe A.Out files. */
449
9c461f7d 450#define aout_32_find_line _bfd_nosymbols_find_line
60bb06bc 451#define aout_32_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
9c461f7d
AM
452#define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
453#define aout_32_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
454#define aout_32_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
7920ce38
NC
455#define aout_32_close_and_cleanup aout_32_bfd_free_cached_info
456#define aout_32_set_arch_mach aout_adobe_set_arch_mach
457#define aout_32_set_section_contents aout_adobe_set_section_contents
458#define aout_32_sizeof_headers aout_adobe_sizeof_headers
459#define aout_32_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
460#define aout_32_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
461#define aout_32_bfd_relax_section bfd_generic_relax_section
462#define aout_32_bfd_gc_sections bfd_generic_gc_sections
ae17ab41 463#define aout_32_bfd_lookup_section_flags bfd_generic_lookup_section_flags
7920ce38
NC
464#define aout_32_bfd_merge_sections bfd_generic_merge_sections
465#define aout_32_bfd_is_group_section bfd_generic_is_group_section
466#define aout_32_bfd_discard_group bfd_generic_discard_group
467#define aout_32_section_already_linked _bfd_generic_section_already_linked
3023e3f6 468#define aout_32_bfd_define_common_symbol bfd_generic_define_common_symbol
7920ce38 469#define aout_32_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
7920ce38
NC
470#define aout_32_bfd_link_add_symbols _bfd_generic_link_add_symbols
471#define aout_32_bfd_link_just_syms _bfd_generic_link_just_syms
1338dd10
PB
472#define aout_32_bfd_copy_link_hash_symbol_type \
473 _bfd_generic_copy_link_hash_symbol_type
7920ce38
NC
474#define aout_32_bfd_final_link _bfd_generic_final_link
475#define aout_32_bfd_link_split_section _bfd_generic_link_split_section
4f3b23b3 476#define aout_32_bfd_link_check_relocs _bfd_generic_link_check_relocs
252b5132 477
6d00b590 478const bfd_target aout_adobe_vec =
7920ce38
NC
479{
480 "a.out.adobe", /* Name. */
481 bfd_target_aout_flavour,
482 BFD_ENDIAN_BIG, /* Data byte order is unknown (big assumed). */
483 BFD_ENDIAN_BIG, /* Header byte order is big. */
484 (HAS_RELOC | EXEC_P | /* Object flags. */
485 HAS_LINENO | HAS_DEBUG |
486 HAS_SYMS | HAS_LOCALS | WP_TEXT ),
487 /* section flags */
488 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_DATA | SEC_RELOC),
489 '_', /* Symbol leading char. */
490 ' ', /* AR_pad_char. */
491 16, /* AR_max_namelen. */
0aabe54e 492 0, /* match priority. */
7920ce38
NC
493
494 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
495 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
496 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
497 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
498 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
499 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
500
501 {_bfd_dummy_target, aout_adobe_object_p, /* bfd_check_format. */
502 bfd_generic_archive_p, _bfd_dummy_target},
503 {bfd_false, aout_adobe_mkobject, /* bfd_set_format. */
504 _bfd_generic_mkarchive, bfd_false},
505 {bfd_false, aout_adobe_write_object_contents,/* bfd_write_contents. */
506 _bfd_write_archive_contents, bfd_false},
507
508 BFD_JUMP_TABLE_GENERIC (aout_32),
509 BFD_JUMP_TABLE_COPY (_bfd_generic),
510 BFD_JUMP_TABLE_CORE (_bfd_nocore),
511 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
512 BFD_JUMP_TABLE_SYMBOLS (aout_32),
513 BFD_JUMP_TABLE_RELOCS (aout_32),
514 BFD_JUMP_TABLE_WRITE (aout_32),
515 BFD_JUMP_TABLE_LINK (aout_32),
516 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
517
518 NULL,
519
520 NULL
521};
This page took 0.737227 seconds and 4 git commands to generate.