Correct VLE 16D and SDAREL relocations
[external/binutils.git] / bfd / aout-adobe.c
1 /* BFD back-end for a.out.adobe binaries.
2    Copyright (C) 1990-2017 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
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
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 *
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             /* xgettext:c-format */
126             (_("%B: Unknown section type in a.out.adobe file: %x\n"),
127              abfd, ext->e_type[0]);
128           goto no_more_sections;
129         }
130
131       /* First one is called ".text" or whatever; subsequent ones are
132          ".text1", ".text2", ...  */
133       bfd_set_error (bfd_error_no_error);
134       sect = bfd_make_section_with_flags (abfd, section_name, flags);
135       trynum = 0;
136
137       while (!sect)
138         {
139           if (bfd_get_error () != bfd_error_no_error)
140             /* Some other error -- slide into the sunset.  */
141             return NULL;
142           sprintf (try_again, "%s%d", section_name, ++trynum);
143           sect = bfd_make_section_with_flags (abfd, try_again, flags);
144         }
145
146       /* Fix the name, if it is a sprintf'd name.  */
147       if (sect->name == try_again)
148         {
149           amt = strlen (sect->name);
150           newname = bfd_zalloc (abfd, amt);
151           if (newname == NULL)
152             return NULL;
153           strcpy (newname, sect->name);
154           sect->name = newname;
155         }
156
157       /* Assumed big-endian.  */
158       sect->size = ((ext->e_size[0] << 8)
159                     | ext->e_size[1] << 8
160                     | ext->e_size[2]);
161       sect->vma = H_GET_32 (abfd, ext->e_virtbase);
162       sect->filepos = H_GET_32 (abfd, ext->e_filebase);
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:
170             sect->rel_filepos = N_TRELOFF (execp);
171             sect->reloc_count = execp->a_trsize;
172             break;
173
174           case N_DATA:
175             sect->rel_filepos = N_DRELOFF (execp);
176             sect->reloc_count = execp->a_drsize;
177             break;
178
179           default:
180             break;
181           }
182     }
183  no_more_sections:
184
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;
190
191   return abfd->xvec;
192 }
193
194 static const bfd_target *
195 aout_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
217   if (N_BADMAG (&anexec))
218     {
219       targ = getenv ("GNUTARGET");
220       if (targ && !strcmp (targ, aout_adobe_vec.name))
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
234 struct bout_data_struct
235 {
236   struct aoutdata a;
237   struct internal_exec e;
238 };
239
240 static bfd_boolean
241 aout_adobe_mkobject (bfd *abfd)
242 {
243   struct bout_data_struct *rawptr;
244   bfd_size_type amt = sizeof (struct bout_data_struct);
245
246   rawptr = bfd_zalloc (abfd, amt);
247   if (rawptr == NULL)
248     return FALSE;
249
250   abfd->tdata.bout_data = rawptr;
251   exec_hdr (abfd) = &rawptr->e;
252
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;
258
259   return TRUE;
260 }
261
262 static void
263 aout_adobe_write_section (bfd *abfd ATTRIBUTE_UNUSED,
264                           sec_ptr sect ATTRIBUTE_UNUSED)
265 {
266   /* FIXME XXX.  */
267 }
268
269 static bfd_boolean
270 aout_adobe_write_object_contents (bfd *abfd)
271 {
272   struct external_exec swapped_hdr;
273   static struct external_segdesc sentinel[1];   /* Initialized to zero.  */
274   asection *sect;
275   bfd_size_type amt;
276
277   exec_hdr (abfd)->a_info = ZMAGIC;
278
279   /* Calculate text size as total of text sections, etc.  */
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
286   for (sect = abfd->sections; sect; sect = sect->next)
287     {
288       if (sect->flags & SEC_CODE)
289         {
290           exec_hdr (abfd)->a_text += sect->size;
291           exec_hdr (abfd)->a_trsize += sect->reloc_count *
292             sizeof (struct reloc_std_external);
293         }
294       else if (sect->flags & SEC_DATA)
295         {
296           exec_hdr (abfd)->a_data += sect->size;
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))
301         exec_hdr (abfd)->a_bss += sect->size;
302     }
303
304   exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd)
305     * sizeof (struct external_nlist);
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
310   amt = EXEC_BYTES_SIZE;
311   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
312       || bfd_bwrite (& swapped_hdr, amt, abfd) != amt)
313     return FALSE;
314
315   /* Now write out the section information.  Text first, data next, rest
316      afterward.  */
317   for (sect = abfd->sections; sect; sect = sect->next)
318     if (sect->flags & SEC_CODE)
319       aout_adobe_write_section (abfd, sect);
320
321   for (sect = abfd->sections; sect; sect = sect->next)
322     if (sect->flags & SEC_DATA)
323       aout_adobe_write_section (abfd, sect);
324
325   for (sect = abfd->sections; sect; sect = sect->next)
326     if (!(sect->flags & (SEC_CODE | SEC_DATA)))
327       aout_adobe_write_section (abfd, sect);
328
329   /* Write final `sentinel` section header (with type of 0).  */
330   amt = sizeof (*sentinel);
331   if (bfd_bwrite (sentinel, amt, abfd) != amt)
332     return FALSE;
333
334   /* Now write out reloc info, followed by syms and strings.  */
335   if (bfd_get_symcount (abfd) != 0)
336     {
337       if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (exec_hdr (abfd))), SEEK_SET)
338           != 0)
339         return FALSE;
340
341       if (! aout_32_write_syms (abfd))
342         return FALSE;
343
344       if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (exec_hdr (abfd))), SEEK_SET)
345           != 0)
346         return FALSE;
347
348       for (sect = abfd->sections; sect; sect = sect->next)
349         if (sect->flags & SEC_CODE)
350           if (!aout_32_squirt_out_relocs (abfd, sect))
351             return FALSE;
352
353       if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (exec_hdr (abfd))), SEEK_SET)
354           != 0)
355         return FALSE;
356
357       for (sect = abfd->sections; sect; sect = sect->next)
358         if (sect->flags & SEC_DATA)
359           if (!aout_32_squirt_out_relocs (abfd, sect))
360             return FALSE;
361     }
362
363   return TRUE;
364 }
365 \f
366 static bfd_boolean
367 aout_adobe_set_section_contents (bfd *abfd,
368                                  asection *section,
369                                  const void * location,
370                                  file_ptr offset,
371                                  bfd_size_type count)
372 {
373   file_ptr section_start;
374   sec_ptr sect;
375
376   /* Set by bfd.c handler.  */
377   if (! abfd->output_has_begun)
378     {
379       /* Assign file offsets to sections.  Text sections are first, and
380          are contiguous.  Then data sections.  Everything else at the end.  */
381       section_start = N_TXTOFF (0);
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.  */
389               section_start += sect->size;
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.  */
399               section_start += sect->size;
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.  */
410               section_start += sect->size;
411             }
412         }
413     }
414
415   /* Regardless, once we know what we're doing, we might as well get
416      going.  */
417   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
418     return FALSE;
419
420   if (count == 0)
421     return TRUE;
422
423   return bfd_bwrite (location, count, abfd) == count;
424 }
425
426 static bfd_boolean
427 aout_adobe_set_arch_mach (bfd *abfd,
428                           enum bfd_architecture arch,
429                           unsigned long machine)
430 {
431   if (! bfd_default_set_arch_mach (abfd, arch, machine))
432     return FALSE;
433
434   if (arch == bfd_arch_unknown
435       || arch == bfd_arch_m68k)
436     return TRUE;
437
438   return FALSE;
439 }
440
441 static int
442 aout_adobe_sizeof_headers (bfd *ignore_abfd ATTRIBUTE_UNUSED,
443                            struct bfd_link_info *info ATTRIBUTE_UNUSED)
444 {
445   return sizeof (struct internal_exec);
446 }
447
448 /* Build the transfer vector for Adobe A.Out files.  */
449
450 #define aout_32_find_line                           _bfd_nosymbols_find_line
451 #define aout_32_get_symbol_version_string           _bfd_nosymbols_get_symbol_version_string
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
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
463 #define aout_32_bfd_lookup_section_flags            bfd_generic_lookup_section_flags
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
468 #define aout_32_bfd_define_common_symbol            bfd_generic_define_common_symbol
469 #define aout_32_bfd_link_hash_table_create          _bfd_generic_link_hash_table_create
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
472 #define aout_32_bfd_copy_link_hash_symbol_type \
473   _bfd_generic_copy_link_hash_symbol_type
474 #define aout_32_bfd_final_link                      _bfd_generic_final_link
475 #define aout_32_bfd_link_split_section              _bfd_generic_link_split_section
476 #define aout_32_bfd_link_check_relocs               _bfd_generic_link_check_relocs
477
478 const bfd_target aout_adobe_vec =
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.  */
492   0,                            /* match priority.  */
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 };