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