Touches most files in bfd/, so likely will be blamed for everything..
[platform/upstream/binutils.git] / bfd / aout-tic30.c
1 /* BFD back-end for TMS320C30 a.out binaries.
2    Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3    Contributed by Steven Haworth (steve@pm.cse.rmit.edu.au)
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 2 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., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #define TARGET_IS_BIG_ENDIAN_P
23 #define N_HEADER_IN_TEXT(x) 1
24 #define BYTES_IN_WORD 4
25 #define TEXT_START_ADDR 1024
26 #define TARGET_PAGE_SIZE 128
27 #define SEGMENT_SIZE TARGET_PAGE_SIZE
28 #define DEFAULT_ARCH bfd_arch_tic30
29 #define ARCH_SIZE 32
30
31 #define MY(OP) CAT(tic30_aout_,OP)
32 #define TARGETNAME "a.out-tic30"
33 #define NAME(x,y) CAT3(tic30_aout,_32_,y)
34
35 #include "bfd.h"
36 #include "sysdep.h"
37 #include "libaout.h"
38
39 #include "aout/aout64.h"
40 #include "aout/stab_gnu.h"
41 #include "aout/ar.h"
42
43 static bfd_reloc_status_type tic30_aout_fix_16
44   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
45 static bfd_reloc_status_type tic30_aout_fix_32
46   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
47 static bfd_reloc_status_type tic30_aout_fix_pcrel_16
48   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
49 static reloc_howto_type *tic30_aout_reloc_howto
50   PARAMS ((bfd *, struct reloc_std_external *, int *, int *, int *));
51 static bfd_reloc_status_type tic30_aout_relocate_contents
52   PARAMS ((reloc_howto_type *, bfd *, bfd_vma, bfd_byte *));
53 static bfd_reloc_status_type tic30_aout_final_link_relocate
54   PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *, bfd_vma,
55            bfd_vma, bfd_vma));
56 static const bfd_target *tic30_aout_object_p PARAMS ((bfd *));
57 static boolean tic30_aout_write_object_contents PARAMS ((bfd *));
58 static boolean tic30_aout_set_sizes PARAMS ((bfd *));
59 static const bfd_target * tic30_aout_callback PARAMS ((bfd *));
60 static boolean MY_bfd_copy_private_section_data
61   PARAMS ((bfd *, asection *, bfd *, asection *));
62 static boolean MY_bfd_final_link PARAMS ((bfd *, struct bfd_link_info *));
63 reloc_howto_type * tic30_aout_reloc_type_lookup
64   PARAMS ((bfd *, bfd_reloc_code_real_type));
65 enum machine_type tic30_aout_machine_type
66   PARAMS ((enum bfd_architecture, unsigned long, boolean *));
67 boolean tic30_aout_set_arch_mach
68   PARAMS ((bfd *, enum bfd_architecture, unsigned long));
69
70 #define MY_reloc_howto(BFD, REL, IN, EX, PC) \
71   tic30_aout_reloc_howto(BFD, REL, &IN, &EX, &PC)
72 #define MY_final_link_relocate tic30_aout_final_link_relocate
73 #define MY_object_p tic30_aout_object_p
74 #define MY_mkobject NAME(aout,mkobject)
75 #define MY_write_object_contents tic30_aout_write_object_contents
76 #define MY_set_sizes tic30_aout_set_sizes
77
78 #ifndef MY_exec_hdr_flags
79 #define MY_exec_hdr_flags 1
80 #endif
81
82 #ifndef MY_backend_data
83
84 #ifndef MY_zmagic_contiguous
85 #define MY_zmagic_contiguous 0
86 #endif
87 #ifndef MY_text_includes_header
88 #define MY_text_includes_header 0
89 #endif
90 #ifndef MY_entry_is_text_address
91 #define MY_entry_is_text_address 0
92 #endif
93 #ifndef MY_exec_header_not_counted
94 #define MY_exec_header_not_counted 1
95 #endif
96 #ifndef MY_add_dynamic_symbols
97 #define MY_add_dynamic_symbols 0
98 #endif
99 #ifndef MY_add_one_symbol
100 #define MY_add_one_symbol 0
101 #endif
102 #ifndef MY_link_dynamic_object
103 #define MY_link_dynamic_object 0
104 #endif
105 #ifndef MY_write_dynamic_symbol
106 #define MY_write_dynamic_symbol 0
107 #endif
108 #ifndef MY_check_dynamic_reloc
109 #define MY_check_dynamic_reloc 0
110 #endif
111 #ifndef MY_finish_dynamic_link
112 #define MY_finish_dynamic_link 0
113 #endif
114
115 static const struct aout_backend_data tic30_aout_backend_data =
116 {
117   MY_zmagic_contiguous,
118   MY_text_includes_header,
119   MY_entry_is_text_address,
120   MY_exec_hdr_flags,
121   0,                            /* text vma? */
122   MY_set_sizes,
123   MY_exec_header_not_counted,
124   MY_add_dynamic_symbols,
125   MY_add_one_symbol,
126   MY_link_dynamic_object,
127   MY_write_dynamic_symbol,
128   MY_check_dynamic_reloc,
129   MY_finish_dynamic_link
130 };
131 #define MY_backend_data &tic30_aout_backend_data
132 #endif
133
134 /* FIXME: This is wrong.  aoutx.h should really only be included by
135    aout32.c.  */
136
137 #include "aoutx.h"
138
139 /* This table lists the relocation types for the TMS320C30.  There are
140    only a few relocations required, and all must be divided by 4 (>>
141    2) to get the 32-bit addresses in the format the TMS320C30 likes
142    it.  */
143 reloc_howto_type tic30_aout_howto_table[] =
144   {
145     EMPTY_HOWTO (-1),
146     HOWTO (1, 2, 1, 16, false, 0, 0, tic30_aout_fix_16,
147            "16", false, 0x0000FFFF, 0x0000FFFF, false),
148     HOWTO (2, 2, 2, 24, false, 0, complain_overflow_bitfield, NULL,
149            "24", false, 0x00FFFFFF, 0x00FFFFFF, false),
150     HOWTO (3, 18, 3, 24, false, 0, complain_overflow_bitfield, NULL,
151            "LDP", false, 0x00FF0000, 0x000000FF, false),
152     HOWTO (4, 2, 4, 32, false, 0, complain_overflow_bitfield, tic30_aout_fix_32,
153            "32", false, 0xFFFFFFFF, 0xFFFFFFFF, false),
154     HOWTO (5, 2, 1, 16, true, 0, complain_overflow_signed,
155            tic30_aout_fix_pcrel_16, "PCREL", true, 0x0000FFFF, 0x0000FFFF, true),
156     EMPTY_HOWTO (-1),
157     EMPTY_HOWTO (-1),
158     EMPTY_HOWTO (-1),
159     EMPTY_HOWTO (-1),
160     EMPTY_HOWTO (-1)
161   };
162
163 extern reloc_howto_type *NAME (aout, reloc_type_lookup)
164      PARAMS ((bfd *, bfd_reloc_code_real_type));
165
166 reloc_howto_type *
167 tic30_aout_reloc_type_lookup (abfd, code)
168      bfd *abfd ATTRIBUTE_UNUSED;
169      bfd_reloc_code_real_type code;
170 {
171   switch (code)
172     {
173     case BFD_RELOC_8:
174     case BFD_RELOC_TIC30_LDP:
175       return &tic30_aout_howto_table[3];
176     case BFD_RELOC_16:
177       return &tic30_aout_howto_table[1];
178     case BFD_RELOC_24:
179       return &tic30_aout_howto_table[2];
180     case BFD_RELOC_16_PCREL:
181       return &tic30_aout_howto_table[5];
182     case BFD_RELOC_32:
183       return &tic30_aout_howto_table[4];
184     default:
185       return (reloc_howto_type *) NULL;
186     }
187 }
188
189 static reloc_howto_type *
190 tic30_aout_reloc_howto (abfd, relocs, r_index, r_extern, r_pcrel)
191      bfd *abfd;
192      struct reloc_std_external *relocs;
193      int *r_index;
194      int *r_extern;
195      int *r_pcrel;
196 {
197   unsigned int r_length;
198   unsigned int r_pcrel_done;
199   int index;
200
201   *r_pcrel = 0;
202   if (bfd_header_big_endian (abfd))
203     {
204       *r_index = ((relocs->r_index[0] << 16) | (relocs->r_index[1] << 8) | relocs->r_index[2]);
205       *r_extern = (0 != (relocs->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
206       r_pcrel_done = (0 != (relocs->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
207       r_length = ((relocs->r_type[0] & RELOC_STD_BITS_LENGTH_BIG) >> RELOC_STD_BITS_LENGTH_SH_BIG);
208     }
209   else
210     {
211       *r_index = ((relocs->r_index[2] << 16) | (relocs->r_index[1] << 8) | relocs->r_index[0]);
212       *r_extern = (0 != (relocs->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
213       r_pcrel_done = (0 != (relocs->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
214       r_length = ((relocs->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE) >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
215     }
216   index = r_length + 4 * r_pcrel_done;
217   return tic30_aout_howto_table + index;
218 }
219
220 /* This function is used as a callback for 16-bit relocs.  This is
221    required for relocations between segments.  A line in aoutx.h
222    requires that any relocations for the data section should point to
223    the end of the aligned text section, plus an offset.  By default,
224    this does not happen, therefore this function takes care of
225    that.  */
226
227 static bfd_reloc_status_type
228 tic30_aout_fix_16 (abfd, reloc_entry, symbol, data, input_section, output_bfd, error_message)
229      bfd *abfd;
230      arelent *reloc_entry;
231      asymbol *symbol;
232      PTR data;
233      asection *input_section ATTRIBUTE_UNUSED;
234      bfd *output_bfd;
235      char **error_message ATTRIBUTE_UNUSED;
236 {
237   bfd_vma relocation;
238
239   /* Make sure that the symbol's section is defined.  */
240   if (symbol->section == &bfd_und_section && (symbol->flags & BSF_WEAK) == 0)
241     return output_bfd ? bfd_reloc_ok : bfd_reloc_undefined;
242   /* Get the size of the input section and turn it into the TMS320C30
243      32-bit address format.  */
244   relocation = (symbol->section->vma >> 2);
245   relocation += bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
246   bfd_put_16 (abfd, relocation, (bfd_byte *) data + reloc_entry->address);
247   return bfd_reloc_ok;
248 }
249
250 /* This function does the same thing as tic30_aout_fix_16 except for 32
251    bit relocations.  */
252
253 static bfd_reloc_status_type
254 tic30_aout_fix_32 (abfd, reloc_entry, symbol, data, input_section,
255                    output_bfd, error_message)
256      bfd *abfd;
257      arelent *reloc_entry;
258      asymbol *symbol;
259      PTR data;
260      asection *input_section ATTRIBUTE_UNUSED;
261      bfd *output_bfd;
262      char **error_message ATTRIBUTE_UNUSED;
263 {
264   bfd_vma relocation;
265
266   /* Make sure that the symbol's section is defined.  */
267   if (symbol->section == &bfd_und_section && (symbol->flags & BSF_WEAK) == 0)
268     return output_bfd ? bfd_reloc_ok : bfd_reloc_undefined;
269   /* Get the size of the input section and turn it into the TMS320C30
270      32-bit address format.  */
271   relocation = (symbol->section->vma >> 2);
272   relocation += bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
273   bfd_put_32 (abfd, relocation, (bfd_byte *) data + reloc_entry->address);
274   return bfd_reloc_ok;
275 }
276
277 /* This function is used to work out pc-relative offsets for the
278    TMS320C30.  The data already placed by md_pcrel_from within gas is
279    useless for a relocation, so we just get the offset value and place
280    a version of this within the object code.
281    tic30_aout_final_link_relocate will then calculate the required
282    relocation to add on to the value in the object code.  */
283
284 static bfd_reloc_status_type
285 tic30_aout_fix_pcrel_16 (abfd, reloc_entry, symbol, data, input_section,
286                          output_bfd, error_message)
287      bfd *abfd;
288      arelent *reloc_entry;
289      asymbol *symbol ATTRIBUTE_UNUSED;
290      PTR data;
291      asection *input_section ATTRIBUTE_UNUSED;
292      bfd *output_bfd ATTRIBUTE_UNUSED;
293      char **error_message ATTRIBUTE_UNUSED;
294 {
295   bfd_vma relocation = 1;
296   bfd_byte offset_data = bfd_get_8 (abfd, (bfd_byte *) data + reloc_entry->address - 1);
297
298   /* The byte before the location of the fix contains bits 23-16 of
299      the pcrel instruction.  Bit 21 is set for a delayed instruction
300      which requires on offset of 3 instead of 1.  */
301   if (offset_data & 0x20)
302     relocation -= 3;
303   else
304     relocation -= 1;
305   bfd_put_16 (abfd, relocation, (bfd_byte *) data + reloc_entry->address);
306   return bfd_reloc_ok;
307 }
308
309 /* These macros will get 24-bit values from the bfd definition.
310    Big-endian only.  */
311 #define bfd_getb_24(BFD,ADDR)                   \
312  (bfd_get_8 (BFD, ADDR    ) << 16) |            \
313  (bfd_get_8 (BFD, ADDR + 1) <<  8) |            \
314  (bfd_get_8 (BFD, ADDR + 2)      )
315
316 #define bfd_putb_24(BFD,DATA,ADDR)                              \
317  bfd_put_8 (BFD, (bfd_byte) ((DATA >> 16) & 0xFF), ADDR    );   \
318  bfd_put_8 (BFD, (bfd_byte) ((DATA >>  8) & 0xFF), ADDR + 1);   \
319  bfd_put_8 (BFD, (bfd_byte) ( DATA        & 0xFF), ADDR + 2)
320
321 /* Set parameters about this a.out file that are machine-dependent.
322    This routine is called from some_aout_object_p just before it returns.  */
323
324 static const bfd_target *
325 tic30_aout_callback (abfd)
326      bfd *abfd;
327 {
328   struct internal_exec *execp = exec_hdr (abfd);
329   unsigned int arch_align_power;
330   unsigned long arch_align;
331
332   /* Calculate the file positions of the parts of a newly read aout header.  */
333   obj_textsec (abfd)->_raw_size = N_TXTSIZE (*execp);
334
335   /* The virtual memory addresses of the sections.  */
336   obj_textsec (abfd)->vma = N_TXTADDR (*execp);
337   obj_datasec (abfd)->vma = N_DATADDR (*execp);
338   obj_bsssec (abfd)->vma = N_BSSADDR (*execp);
339
340   obj_textsec (abfd)->lma = obj_textsec (abfd)->vma;
341   obj_datasec (abfd)->lma = obj_datasec (abfd)->vma;
342   obj_bsssec (abfd)->lma = obj_bsssec (abfd)->vma;
343
344   /* The file offsets of the sections.  */
345   obj_textsec (abfd)->filepos = N_TXTOFF (*execp);
346   obj_datasec (abfd)->filepos = N_DATOFF (*execp);
347
348   /* The file offsets of the relocation info.  */
349   obj_textsec (abfd)->rel_filepos = N_TRELOFF (*execp);
350   obj_datasec (abfd)->rel_filepos = N_DRELOFF (*execp);
351
352   /* The file offsets of the string table and symbol table.  */
353   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
354   obj_str_filepos (abfd) = N_STROFF (*execp);
355
356   /* Determine the architecture and machine type of the object file.  */
357 #ifdef SET_ARCH_MACH
358   SET_ARCH_MACH (abfd, *execp);
359 #else
360   bfd_default_set_arch_mach (abfd, DEFAULT_ARCH, 0L);
361 #endif
362
363   /* Now that we know the architecture, set the alignments of the
364      sections.  This is normally done by NAME(aout,new_section_hook),
365      but when the initial sections were created the architecture had
366      not yet been set.  However, for backward compatibility, we don't
367      set the alignment power any higher than as required by the size
368      of the section.  */
369   arch_align_power = bfd_get_arch_info (abfd)->section_align_power;
370   arch_align = 1 << arch_align_power;
371   if ((BFD_ALIGN (obj_textsec (abfd)->_raw_size, arch_align)
372        == obj_textsec (abfd)->_raw_size)
373       && (BFD_ALIGN (obj_datasec (abfd)->_raw_size, arch_align)
374           == obj_datasec (abfd)->_raw_size)
375       && (BFD_ALIGN (obj_bsssec (abfd)->_raw_size, arch_align)
376           == obj_bsssec (abfd)->_raw_size))
377     {
378       obj_textsec (abfd)->alignment_power = arch_align_power;
379       obj_datasec (abfd)->alignment_power = arch_align_power;
380       obj_bsssec (abfd)->alignment_power = arch_align_power;
381     }
382   return abfd->xvec;
383 }
384
385 static bfd_reloc_status_type
386 tic30_aout_final_link_relocate (howto, input_bfd, input_section, contents,
387                                 address, value, addend)
388      reloc_howto_type *howto;
389      bfd *input_bfd;
390      asection *input_section;
391      bfd_byte *contents;
392      bfd_vma address;
393      bfd_vma value;
394      bfd_vma addend;
395 {
396   bfd_vma relocation;
397
398   if (address > input_section->_raw_size)
399     return bfd_reloc_outofrange;
400
401   relocation = value + addend;
402   if (howto->pc_relative)
403     {
404       relocation -= (input_section->output_section->vma + input_section->output_offset);
405       if (howto->pcrel_offset)
406         relocation -= address;
407     }
408   return tic30_aout_relocate_contents (howto, input_bfd, relocation,
409                                        contents + address);
410 }
411
412 bfd_reloc_status_type
413 tic30_aout_relocate_contents (howto, input_bfd, relocation, location)
414      reloc_howto_type *howto;
415      bfd *input_bfd;
416      bfd_vma relocation;
417      bfd_byte *location;
418 {
419   bfd_vma x;
420   boolean overflow;
421
422   if (howto->size < 0)
423     relocation = -relocation;
424
425   switch (howto->size)
426     {
427     default:
428     case 0:
429       abort ();
430       break;
431     case 1:
432       x = bfd_get_16 (input_bfd, location);
433       break;
434     case 2:
435       x = bfd_getb_24 (input_bfd, location);
436       break;
437     case 3:
438       x = bfd_get_8 (input_bfd, location);
439       break;
440     case 4:
441       x = bfd_get_32 (input_bfd, location);
442       break;
443     }
444
445   overflow = false;
446
447   if (howto->complain_on_overflow != complain_overflow_dont)
448     {
449       bfd_vma check;
450       bfd_signed_vma signed_check;
451       bfd_vma add;
452       bfd_signed_vma signed_add;
453
454       if (howto->rightshift == 0)
455         {
456           check = relocation;
457           signed_check = (bfd_signed_vma) relocation;
458         }
459       else
460         {
461           check = relocation >> howto->rightshift;
462           if ((bfd_signed_vma) relocation >= 0)
463             signed_check = check;
464           else
465             signed_check = (check | ((bfd_vma) - 1 & ~((bfd_vma) - 1 >> howto->rightshift)));
466         }
467       add = x & howto->src_mask;
468       signed_add = add;
469       if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
470         signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
471       if (howto->bitpos == 0)
472         {
473           check += add;
474           signed_check += signed_add;
475         }
476       else
477         {
478           check += add >> howto->bitpos;
479           if (signed_add >= 0)
480             signed_check += add >> howto->bitpos;
481           else
482             signed_check += ((add >> howto->bitpos) | ((bfd_vma) - 1 & ~((bfd_vma) - 1 >> howto->bitpos)));
483         }
484       switch (howto->complain_on_overflow)
485         {
486         case complain_overflow_signed:
487           {
488             bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
489             bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
490             if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
491               overflow = true;
492           }
493           break;
494         case complain_overflow_unsigned:
495           {
496             bfd_vma reloc_unsigned_max = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
497             if (check > reloc_unsigned_max)
498               overflow = true;
499           }
500           break;
501         case complain_overflow_bitfield:
502           {
503             bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
504             if ((check & ~reloc_bits) != 0
505                 && (((bfd_vma) signed_check & ~reloc_bits)
506                     != ((bfd_vma) -1 & ~reloc_bits)))
507               overflow = true;
508           }
509           break;
510         default:
511           abort ();
512         }
513     }
514   relocation >>= (bfd_vma) howto->rightshift;
515   relocation <<= (bfd_vma) howto->bitpos;
516   x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask));
517   switch (howto->size)
518     {
519     default:
520     case 0:
521       abort ();
522       break;
523     case 1:
524       bfd_put_16 (input_bfd, x, location);
525       break;
526     case 2:
527       bfd_putb_24 (input_bfd, x, location);
528       break;
529     case 3:
530       bfd_put_8 (input_bfd, x, location);
531       break;
532     case 4:
533       bfd_put_32 (input_bfd, x, location);
534       break;
535     }
536   return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
537 }
538
539 /* Finish up the reading of an a.out file header.  */
540
541 static const bfd_target *
542 tic30_aout_object_p (abfd)
543      bfd *abfd;
544 {
545   struct external_exec exec_bytes;      /* Raw exec header from file.  */
546   struct internal_exec exec;    /* Cleaned-up exec header.  */
547   const bfd_target *target;
548   bfd_size_type amt = EXEC_BYTES_SIZE;
549
550   if (bfd_bread ((PTR) &exec_bytes, amt, abfd) != amt)
551     {
552       if (bfd_get_error () != bfd_error_system_call)
553         bfd_set_error (bfd_error_wrong_format);
554       return 0;
555     }
556
557 #ifdef SWAP_MAGIC
558   exec.a_info = SWAP_MAGIC (exec_bytes.e_info);
559 #else
560   exec.a_info = H_GET_32 (abfd, exec_bytes.e_info);
561 #endif /* SWAP_MAGIC */
562
563   if (N_BADMAG (exec))
564     return 0;
565 #ifdef MACHTYPE_OK
566   if (!(MACHTYPE_OK (N_MACHTYPE (exec))))
567     return 0;
568 #endif
569
570   NAME (aout, swap_exec_header_in) (abfd, &exec_bytes, &exec);
571
572 #ifdef SWAP_MAGIC
573   /* swap_exec_header_in read in a_info with the wrong byte order */
574   exec.a_info = SWAP_MAGIC (exec_bytes.e_info);
575 #endif /* SWAP_MAGIC */
576
577   target = NAME (aout, some_aout_object_p) (abfd, &exec, tic30_aout_callback);
578
579 #ifdef ENTRY_CAN_BE_ZERO
580   /* The NEWSOS3 entry-point is/was 0, which (amongst other lossage)
581      means that it isn't obvious if EXEC_P should be set.
582      All of the following must be true for an executable:
583      There must be no relocations, the bfd can be neither an
584      archive nor an archive element, and the file must be executable.  */
585
586   if (exec.a_trsize + exec.a_drsize == 0
587       && bfd_get_format (abfd) == bfd_object && abfd->my_archive == NULL)
588     {
589       struct stat buf;
590 #ifndef S_IXUSR
591 #define S_IXUSR 0100            /* Execute by owner.  */
592 #endif
593       if (stat (abfd->filename, &buf) == 0 && (buf.st_mode & S_IXUSR))
594         abfd->flags |= EXEC_P;
595     }
596 #endif /* ENTRY_CAN_BE_ZERO */
597
598   return target;
599 }
600
601 /* Copy private section data.  This actually does nothing with the
602    sections.  It copies the subformat field.  We copy it here, because
603    we need to know whether this is a QMAGIC file before we set the
604    section contents, and copy_private_bfd_data is not called until
605    after the section contents have been set.  */
606
607 static boolean
608 MY_bfd_copy_private_section_data (ibfd, isec, obfd, osec)
609      bfd *ibfd;
610      asection *isec ATTRIBUTE_UNUSED;
611      bfd *obfd;
612      asection *osec ATTRIBUTE_UNUSED;
613 {
614   if (bfd_get_flavour (obfd) == bfd_target_aout_flavour)
615     obj_aout_subformat (obfd) = obj_aout_subformat (ibfd);
616   return true;
617 }
618
619 /* Write an object file.
620    Section contents have already been written.  We write the
621    file header, symbols, and relocation.  */
622
623 static boolean
624 tic30_aout_write_object_contents (abfd)
625      bfd *abfd;
626 {
627   struct external_exec exec_bytes;
628   struct internal_exec *execp = exec_hdr (abfd);
629
630   obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
631
632   {
633     bfd_size_type text_size;    /* Dummy vars.  */
634     file_ptr text_end;
635
636     if (adata (abfd).magic == undecided_magic)
637       NAME (aout, adjust_sizes_and_vmas) (abfd, &text_size, &text_end);
638
639     execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE;
640     execp->a_entry = bfd_get_start_address (abfd);
641
642     execp->a_trsize = ((obj_textsec (abfd)->reloc_count) * obj_reloc_entry_size (abfd));
643     execp->a_drsize = ((obj_datasec (abfd)->reloc_count) * obj_reloc_entry_size (abfd));
644     NAME (aout, swap_exec_header_out) (abfd, execp, &exec_bytes);
645
646     if (adata (abfd).exec_bytes_size > 0)
647       {
648         bfd_size_type amt;
649         if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
650           return false;
651         amt = adata (abfd).exec_bytes_size;
652         if (bfd_bwrite ((PTR) &exec_bytes, amt, abfd) != amt)
653           return false;
654       }
655
656     /* Now write out reloc info, followed by syms and strings.  */
657     if (bfd_get_outsymbols (abfd) != (asymbol **) NULL
658         && bfd_get_symcount (abfd) != 0)
659       {
660         if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*execp)), SEEK_SET) != 0)
661           return false;
662
663         if (!NAME (aout, write_syms) (abfd))
664           return false;
665       }
666
667     if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (*execp)), SEEK_SET) != 0)
668       return false;
669     if (!NAME (aout, squirt_out_relocs) (abfd, obj_textsec (abfd)))
670       return false;
671
672     if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (*execp)), SEEK_SET) != 0)
673       return false;
674     if (!NAME (aout, squirt_out_relocs) (abfd, obj_datasec (abfd)))
675       return false;
676   }
677
678   return true;
679 }
680
681 static boolean
682 tic30_aout_set_sizes (abfd)
683      bfd *abfd;
684 {
685   adata (abfd).page_size = TARGET_PAGE_SIZE;
686
687 #ifdef SEGMENT_SIZE
688   adata (abfd).segment_size = SEGMENT_SIZE;
689 #else
690   adata (abfd).segment_size = TARGET_PAGE_SIZE;
691 #endif
692
693 #ifdef ZMAGIC_DISK_BLOCK_SIZE
694   adata (abfd).zmagic_disk_block_size = ZMAGIC_DISK_BLOCK_SIZE;
695 #else
696   adata (abfd).zmagic_disk_block_size = TARGET_PAGE_SIZE;
697 #endif
698
699   adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
700
701   return true;
702 }
703
704 #ifndef MY_final_link_callback
705
706 /* Callback for the final_link routine to set the section offsets.  */
707
708 static void MY_final_link_callback
709   PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
710
711 static void
712 MY_final_link_callback (abfd, ptreloff, pdreloff, psymoff)
713      bfd *abfd;
714      file_ptr *ptreloff;
715      file_ptr *pdreloff;
716      file_ptr *psymoff;
717 {
718   struct internal_exec *execp = exec_hdr (abfd);
719
720   *ptreloff = obj_datasec (abfd)->filepos + execp->a_data;
721   *pdreloff = *ptreloff + execp->a_trsize;
722   *psymoff = *pdreloff + execp->a_drsize;;
723 }
724
725 #endif
726
727 #ifndef MY_bfd_final_link
728
729 /* Final link routine.  We need to use a call back to get the correct
730    offsets in the output file.  */
731
732 static boolean
733 MY_bfd_final_link (abfd, info)
734      bfd *abfd;
735      struct bfd_link_info *info;
736 {
737   struct internal_exec *execp = exec_hdr (abfd);
738   file_ptr pos;
739   bfd_vma vma = 0;
740   int pad;
741
742   /* Set the executable header size to 0, as we don't want one for an
743      output.  */
744   adata (abfd).exec_bytes_size = 0;
745   pos = adata (abfd).exec_bytes_size;
746   /* Text.  */
747   vma = info->create_object_symbols_section->vma;
748   pos += vma;
749   obj_textsec (abfd)->filepos = pos;
750   obj_textsec (abfd)->vma = vma;
751   obj_textsec (abfd)->user_set_vma = 1;
752   pos += obj_textsec (abfd)->_raw_size;
753   vma += obj_textsec (abfd)->_raw_size;
754
755   /* Data.  */
756   if (abfd->flags & D_PAGED)
757     {
758       if (info->create_object_symbols_section->next->vma > 0)
759         obj_datasec (abfd)->vma = info->create_object_symbols_section->next->vma;
760       else
761         obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
762     }
763   else
764     {
765       obj_datasec (abfd)->vma = BFD_ALIGN (vma, 4);
766     }
767
768   if (obj_datasec (abfd)->vma < vma)
769     obj_datasec (abfd)->vma = BFD_ALIGN (vma, 4);
770
771   obj_datasec (abfd)->user_set_vma = 1;
772   vma = obj_datasec (abfd)->vma;
773   obj_datasec (abfd)->filepos = vma + adata (abfd).exec_bytes_size;
774   execp->a_text = vma - obj_textsec (abfd)->vma;
775   obj_textsec (abfd)->_raw_size = execp->a_text;
776
777   /* Since BSS follows data immediately, see if it needs alignment.  */
778   vma += obj_datasec (abfd)->_raw_size;
779   pad = align_power (vma, obj_bsssec (abfd)->alignment_power) - vma;
780   obj_datasec (abfd)->_raw_size += pad;
781   pos += obj_datasec (abfd)->_raw_size;
782   execp->a_data = obj_datasec (abfd)->_raw_size;
783
784   /* BSS.  */
785   obj_bsssec (abfd)->vma = vma;
786   obj_bsssec (abfd)->user_set_vma = 1;
787
788   /* We are fully resized, so don't readjust in final_link.  */
789   adata (abfd).magic = z_magic;
790
791   return NAME (aout, final_link) (abfd, info, MY_final_link_callback);
792 }
793
794 #endif
795
796 enum machine_type
797 tic30_aout_machine_type (arch, machine, unknown)
798      enum bfd_architecture arch;
799      unsigned long machine ATTRIBUTE_UNUSED;
800      boolean *unknown;
801 {
802   enum machine_type arch_flags;
803
804   arch_flags = M_UNKNOWN;
805   *unknown = true;
806
807   switch (arch)
808     {
809     case bfd_arch_tic30:
810       *unknown = false;
811       break;
812     default:
813       arch_flags = M_UNKNOWN;
814     }
815   if (arch_flags != M_UNKNOWN)
816     *unknown = false;
817   return arch_flags;
818 }
819
820 boolean
821 tic30_aout_set_arch_mach (abfd, arch, machine)
822      bfd *abfd;
823      enum bfd_architecture arch;
824      unsigned long machine;
825 {
826   if (!bfd_default_set_arch_mach (abfd, arch, machine))
827     return false;
828   if (arch != bfd_arch_unknown)
829     {
830       boolean unknown;
831       tic30_aout_machine_type (arch, machine, &unknown);
832       if (unknown)
833         return false;
834     }
835   obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
836   return (*aout_backend_info (abfd)->set_sizes) (abfd);
837 }
838
839 /* We assume BFD generic archive files.  */
840 #ifndef MY_openr_next_archived_file
841 #define MY_openr_next_archived_file     bfd_generic_openr_next_archived_file
842 #endif
843 #ifndef MY_get_elt_at_index
844 #define MY_get_elt_at_index             _bfd_generic_get_elt_at_index
845 #endif
846 #ifndef MY_generic_stat_arch_elt
847 #define MY_generic_stat_arch_elt        bfd_generic_stat_arch_elt
848 #endif
849 #ifndef MY_slurp_armap
850 #define MY_slurp_armap                  bfd_slurp_bsd_armap
851 #endif
852 #ifndef MY_slurp_extended_name_table
853 #define MY_slurp_extended_name_table    _bfd_slurp_extended_name_table
854 #endif
855 #ifndef MY_construct_extended_name_table
856 #define MY_construct_extended_name_table \
857   _bfd_archive_bsd_construct_extended_name_table
858 #endif
859 #ifndef MY_write_armap
860 #define MY_write_armap          bsd_write_armap
861 #endif
862 #ifndef MY_read_ar_hdr
863 #define MY_read_ar_hdr          _bfd_generic_read_ar_hdr
864 #endif
865 #ifndef MY_truncate_arname
866 #define MY_truncate_arname              bfd_bsd_truncate_arname
867 #endif
868 #ifndef MY_update_armap_timestamp
869 #define MY_update_armap_timestamp _bfd_archive_bsd_update_armap_timestamp
870 #endif
871
872 /* No core file defined here -- configure in trad-core.c separately.  */
873 #ifndef MY_core_file_failing_command
874 #define MY_core_file_failing_command _bfd_nocore_core_file_failing_command
875 #endif
876 #ifndef MY_core_file_failing_signal
877 #define MY_core_file_failing_signal     _bfd_nocore_core_file_failing_signal
878 #endif
879 #ifndef MY_core_file_matches_executable_p
880 #define MY_core_file_matches_executable_p       \
881                                 _bfd_nocore_core_file_matches_executable_p
882 #endif
883 #ifndef MY_core_file_p
884 #define MY_core_file_p          _bfd_dummy_target
885 #endif
886
887 #ifndef MY_bfd_debug_info_start
888 #define MY_bfd_debug_info_start         bfd_void
889 #endif
890 #ifndef MY_bfd_debug_info_end
891 #define MY_bfd_debug_info_end           bfd_void
892 #endif
893 #ifndef MY_bfd_debug_info_accumulate
894 #define MY_bfd_debug_info_accumulate    \
895                         (void (*) PARAMS ((bfd*, struct sec *))) bfd_void
896 #endif
897
898 #ifndef MY_core_file_failing_command
899 #define MY_core_file_failing_command NAME(aout,core_file_failing_command)
900 #endif
901 #ifndef MY_core_file_failing_signal
902 #define MY_core_file_failing_signal NAME(aout,core_file_failing_signal)
903 #endif
904 #ifndef MY_core_file_matches_executable_p
905 #define MY_core_file_matches_executable_p NAME(aout,core_file_matches_executable_p)
906 #endif
907 #ifndef MY_set_section_contents
908 #define MY_set_section_contents NAME(aout,set_section_contents)
909 #endif
910 #ifndef MY_get_section_contents
911 #define MY_get_section_contents aout_32_get_section_contents
912 #endif
913 #ifndef MY_get_section_contents_in_window
914 #define MY_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
915 #endif
916 #ifndef MY_new_section_hook
917 #define MY_new_section_hook NAME(aout,new_section_hook)
918 #endif
919 #ifndef MY_get_symtab_upper_bound
920 #define MY_get_symtab_upper_bound NAME(aout,get_symtab_upper_bound)
921 #endif
922 #ifndef MY_get_symtab
923 #define MY_get_symtab NAME(aout,get_symtab)
924 #endif
925 #ifndef MY_get_reloc_upper_bound
926 #define MY_get_reloc_upper_bound NAME(aout,get_reloc_upper_bound)
927 #endif
928 #ifndef MY_canonicalize_reloc
929 #define MY_canonicalize_reloc NAME(aout,canonicalize_reloc)
930 #endif
931 #ifndef MY_make_empty_symbol
932 #define MY_make_empty_symbol NAME(aout,make_empty_symbol)
933 #endif
934 #ifndef MY_print_symbol
935 #define MY_print_symbol NAME(aout,print_symbol)
936 #endif
937 #ifndef MY_get_symbol_info
938 #define MY_get_symbol_info NAME(aout,get_symbol_info)
939 #endif
940 #ifndef MY_get_lineno
941 #define MY_get_lineno NAME(aout,get_lineno)
942 #endif
943 #ifndef MY_set_arch_mach
944 #define MY_set_arch_mach tic30_aout_set_arch_mach
945 #endif
946 #ifndef MY_find_nearest_line
947 #define MY_find_nearest_line NAME(aout,find_nearest_line)
948 #endif
949 #ifndef MY_sizeof_headers
950 #define MY_sizeof_headers NAME(aout,sizeof_headers)
951 #endif
952 #ifndef MY_bfd_get_relocated_section_contents
953 #define MY_bfd_get_relocated_section_contents \
954                         bfd_generic_get_relocated_section_contents
955 #endif
956 #ifndef MY_bfd_relax_section
957 #define MY_bfd_relax_section bfd_generic_relax_section
958 #endif
959 #ifndef MY_bfd_gc_sections
960 #define MY_bfd_gc_sections bfd_generic_gc_sections
961 #endif
962 #ifndef MY_bfd_merge_sections
963 #define MY_bfd_merge_sections bfd_generic_merge_sections
964 #endif
965 #ifndef MY_bfd_reloc_type_lookup
966 #define MY_bfd_reloc_type_lookup tic30_aout_reloc_type_lookup
967 #endif
968 #ifndef MY_bfd_make_debug_symbol
969 #define MY_bfd_make_debug_symbol 0
970 #endif
971 #ifndef MY_read_minisymbols
972 #define MY_read_minisymbols NAME(aout,read_minisymbols)
973 #endif
974 #ifndef MY_minisymbol_to_symbol
975 #define MY_minisymbol_to_symbol NAME(aout,minisymbol_to_symbol)
976 #endif
977 #ifndef MY_bfd_link_hash_table_create
978 #define MY_bfd_link_hash_table_create NAME(aout,link_hash_table_create)
979 #endif
980 #ifndef MY_bfd_link_add_symbols
981 #define MY_bfd_link_add_symbols NAME(aout,link_add_symbols)
982 #endif
983 #ifndef MY_bfd_link_split_section
984 #define MY_bfd_link_split_section  _bfd_generic_link_split_section
985 #endif
986
987 #ifndef MY_bfd_copy_private_bfd_data
988 #define MY_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
989 #endif
990
991 #ifndef MY_bfd_merge_private_bfd_data
992 #define MY_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
993 #endif
994
995 #ifndef MY_bfd_copy_private_symbol_data
996 #define MY_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
997 #endif
998
999 #ifndef MY_bfd_print_private_bfd_data
1000 #define MY_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
1001 #endif
1002
1003 #ifndef MY_bfd_set_private_flags
1004 #define MY_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
1005 #endif
1006
1007 #ifndef MY_bfd_is_local_label_name
1008 #define MY_bfd_is_local_label_name bfd_generic_is_local_label_name
1009 #endif
1010
1011 #ifndef MY_bfd_free_cached_info
1012 #define MY_bfd_free_cached_info NAME(aout,bfd_free_cached_info)
1013 #endif
1014
1015 #ifndef MY_close_and_cleanup
1016 #define MY_close_and_cleanup MY_bfd_free_cached_info
1017 #endif
1018
1019 #ifndef MY_get_dynamic_symtab_upper_bound
1020 #define MY_get_dynamic_symtab_upper_bound \
1021   _bfd_nodynamic_get_dynamic_symtab_upper_bound
1022 #endif
1023 #ifndef MY_canonicalize_dynamic_symtab
1024 #define MY_canonicalize_dynamic_symtab \
1025   _bfd_nodynamic_canonicalize_dynamic_symtab
1026 #endif
1027 #ifndef MY_get_dynamic_reloc_upper_bound
1028 #define MY_get_dynamic_reloc_upper_bound \
1029   _bfd_nodynamic_get_dynamic_reloc_upper_bound
1030 #endif
1031 #ifndef MY_canonicalize_dynamic_reloc
1032 #define MY_canonicalize_dynamic_reloc \
1033   _bfd_nodynamic_canonicalize_dynamic_reloc
1034 #endif
1035
1036 /* Aout symbols normally have leading underscores */
1037 #ifndef MY_symbol_leading_char
1038 #define MY_symbol_leading_char '_'
1039 #endif
1040
1041 /* Aout archives normally use spaces for padding */
1042 #ifndef AR_PAD_CHAR
1043 #define AR_PAD_CHAR ' '
1044 #endif
1045
1046 #ifndef MY_BFD_TARGET
1047 const bfd_target tic30_aout_vec =
1048 {
1049   TARGETNAME,                   /* name */
1050   bfd_target_aout_flavour,
1051   BFD_ENDIAN_BIG,               /* target byte order (big) */
1052   BFD_ENDIAN_BIG,               /* target headers byte order (big) */
1053   (HAS_RELOC |                  /* object flags */
1054    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1055   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),        /* section flags */
1056   MY_symbol_leading_char,
1057   AR_PAD_CHAR,                  /* ar_pad_char */
1058   15,                           /* ar_max_namelen */
1059   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1060   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1061   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
1062   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1063   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1064   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
1065   {_bfd_dummy_target, MY_object_p,      /* bfd_check_format */
1066    bfd_generic_archive_p, MY_core_file_p},
1067   {bfd_false, MY_mkobject,      /* bfd_set_format */
1068    _bfd_generic_mkarchive, bfd_false},
1069   {bfd_false, MY_write_object_contents,         /* bfd_write_contents */
1070    _bfd_write_archive_contents, bfd_false},
1071
1072   BFD_JUMP_TABLE_GENERIC (MY),
1073   BFD_JUMP_TABLE_COPY (MY),
1074   BFD_JUMP_TABLE_CORE (MY),
1075   BFD_JUMP_TABLE_ARCHIVE (MY),
1076   BFD_JUMP_TABLE_SYMBOLS (MY),
1077   BFD_JUMP_TABLE_RELOCS (MY),
1078   BFD_JUMP_TABLE_WRITE (MY),
1079   BFD_JUMP_TABLE_LINK (MY),
1080   BFD_JUMP_TABLE_DYNAMIC (MY),
1081
1082   NULL,
1083
1084   (PTR) MY_backend_data
1085 };
1086 #endif /* MY_BFD_TARGET */