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