xref: /openbsd-src/gnu/usr.bin/binutils-2.17/bfd/coff-tic30.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* BFD back-end for TMS320C30 coff binaries.
2*3d8817e4Smiod    Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3*3d8817e4Smiod    Contributed by Steven Haworth (steve@pm.cse.rmit.edu.au)
4*3d8817e4Smiod 
5*3d8817e4Smiod    This file is part of BFD, the Binary File Descriptor library.
6*3d8817e4Smiod 
7*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
8*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
10*3d8817e4Smiod    (at your option) any later version.
11*3d8817e4Smiod 
12*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
13*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*3d8817e4Smiod    GNU General Public License for more details.
16*3d8817e4Smiod 
17*3d8817e4Smiod    You should have received a copy of the GNU General Public License
18*3d8817e4Smiod    along with this program; if not, write to the Free Software
19*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20*3d8817e4Smiod    02110-1301, USA.  */
21*3d8817e4Smiod 
22*3d8817e4Smiod #include "bfd.h"
23*3d8817e4Smiod #include "sysdep.h"
24*3d8817e4Smiod #include "libbfd.h"
25*3d8817e4Smiod #include "bfdlink.h"
26*3d8817e4Smiod #include "coff/tic30.h"
27*3d8817e4Smiod #include "coff/internal.h"
28*3d8817e4Smiod #include "libcoff.h"
29*3d8817e4Smiod 
30*3d8817e4Smiod static int  coff_tic30_select_reloc PARAMS ((reloc_howto_type *));
31*3d8817e4Smiod static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
32*3d8817e4Smiod static void reloc_processing PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
33*3d8817e4Smiod 
34*3d8817e4Smiod reloc_howto_type * tic30_coff_reloc_type_lookup PARAMS ((bfd *, bfd_reloc_code_real_type));
35*3d8817e4Smiod 
36*3d8817e4Smiod #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
37*3d8817e4Smiod 
38*3d8817e4Smiod reloc_howto_type tic30_coff_howto_table[] =
39*3d8817e4Smiod   {
40*3d8817e4Smiod     HOWTO (R_TIC30_ABS16, 2, 1, 16, FALSE, 0, 0, NULL,
41*3d8817e4Smiod 	   "16", FALSE, 0x0000FFFF, 0x0000FFFF, FALSE),
42*3d8817e4Smiod     HOWTO (R_TIC30_ABS24, 2, 2, 24, FALSE, 8, complain_overflow_bitfield, NULL,
43*3d8817e4Smiod 	   "24", FALSE, 0xFFFFFF00, 0xFFFFFF00, FALSE),
44*3d8817e4Smiod     HOWTO (R_TIC30_LDP, 18, 0, 24, FALSE, 0, complain_overflow_bitfield, NULL,
45*3d8817e4Smiod 	   "LDP", FALSE, 0x00FF0000, 0x000000FF, FALSE),
46*3d8817e4Smiod     HOWTO (R_TIC30_ABS32, 2, 2, 32, FALSE, 0, complain_overflow_bitfield, NULL,
47*3d8817e4Smiod 	   "32", FALSE, 0xFFFFFFFF, 0xFFFFFFFF, FALSE),
48*3d8817e4Smiod     HOWTO (R_TIC30_PC16, 2, 1, 16, TRUE, 0, complain_overflow_signed, NULL,
49*3d8817e4Smiod 	   "PCREL", FALSE, 0x0000FFFF, 0x0000FFFF, FALSE),
50*3d8817e4Smiod     EMPTY_HOWTO (-1)
51*3d8817e4Smiod   };
52*3d8817e4Smiod 
53*3d8817e4Smiod #ifndef coff_bfd_reloc_type_lookup
54*3d8817e4Smiod #define coff_bfd_reloc_type_lookup tic30_coff_reloc_type_lookup
55*3d8817e4Smiod 
56*3d8817e4Smiod /* For the case statement use the code values used in tc_gen_reloc to
57*3d8817e4Smiod    map to the howto table entries that match those in both the aout
58*3d8817e4Smiod    and coff implementations.  */
59*3d8817e4Smiod 
60*3d8817e4Smiod reloc_howto_type *
tic30_coff_reloc_type_lookup(abfd,code)61*3d8817e4Smiod tic30_coff_reloc_type_lookup (abfd, code)
62*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
63*3d8817e4Smiod      bfd_reloc_code_real_type code;
64*3d8817e4Smiod {
65*3d8817e4Smiod   switch (code)
66*3d8817e4Smiod     {
67*3d8817e4Smiod     case BFD_RELOC_8:
68*3d8817e4Smiod     case BFD_RELOC_TIC30_LDP:
69*3d8817e4Smiod       return &tic30_coff_howto_table[2];
70*3d8817e4Smiod     case BFD_RELOC_16:
71*3d8817e4Smiod       return &tic30_coff_howto_table[0];
72*3d8817e4Smiod     case BFD_RELOC_24:
73*3d8817e4Smiod       return &tic30_coff_howto_table[1];
74*3d8817e4Smiod     case BFD_RELOC_16_PCREL:
75*3d8817e4Smiod       return &tic30_coff_howto_table[4];
76*3d8817e4Smiod     case BFD_RELOC_32:
77*3d8817e4Smiod       return &tic30_coff_howto_table[3];
78*3d8817e4Smiod     default:
79*3d8817e4Smiod       return (reloc_howto_type *) NULL;
80*3d8817e4Smiod     }
81*3d8817e4Smiod }
82*3d8817e4Smiod 
83*3d8817e4Smiod #endif
84*3d8817e4Smiod 
85*3d8817e4Smiod /* Turn a howto into a reloc number.  */
86*3d8817e4Smiod 
87*3d8817e4Smiod static int
coff_tic30_select_reloc(howto)88*3d8817e4Smiod coff_tic30_select_reloc (howto)
89*3d8817e4Smiod      reloc_howto_type *howto;
90*3d8817e4Smiod {
91*3d8817e4Smiod   return howto->type;
92*3d8817e4Smiod }
93*3d8817e4Smiod 
94*3d8817e4Smiod #define SELECT_RELOC(x,howto) x.r_type = coff_tic30_select_reloc(howto)
95*3d8817e4Smiod 
96*3d8817e4Smiod #define BADMAG(x) TIC30BADMAG(x)
97*3d8817e4Smiod #define TIC30 1			/* Customize coffcode.h */
98*3d8817e4Smiod #define __A_MAGIC_SET__
99*3d8817e4Smiod 
100*3d8817e4Smiod /* Code to swap in the reloc */
101*3d8817e4Smiod #define SWAP_IN_RELOC_OFFSET  H_GET_32
102*3d8817e4Smiod #define SWAP_OUT_RELOC_OFFSET H_PUT_32
103*3d8817e4Smiod #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) dst->r_stuff[0] = 'S'; \
104*3d8817e4Smiod dst->r_stuff[1] = 'C';
105*3d8817e4Smiod 
106*3d8817e4Smiod /* Code to turn a r_type into a howto ptr, uses the above howto table.  */
107*3d8817e4Smiod 
108*3d8817e4Smiod static void
rtype2howto(internal,dst)109*3d8817e4Smiod rtype2howto (internal, dst)
110*3d8817e4Smiod      arelent *internal;
111*3d8817e4Smiod      struct internal_reloc *dst;
112*3d8817e4Smiod {
113*3d8817e4Smiod   switch (dst->r_type)
114*3d8817e4Smiod     {
115*3d8817e4Smiod     case R_TIC30_ABS16:
116*3d8817e4Smiod       internal->howto = &tic30_coff_howto_table[0];
117*3d8817e4Smiod       break;
118*3d8817e4Smiod     case R_TIC30_ABS24:
119*3d8817e4Smiod       internal->howto = &tic30_coff_howto_table[1];
120*3d8817e4Smiod       break;
121*3d8817e4Smiod     case R_TIC30_ABS32:
122*3d8817e4Smiod       internal->howto = &tic30_coff_howto_table[3];
123*3d8817e4Smiod       break;
124*3d8817e4Smiod     case R_TIC30_LDP:
125*3d8817e4Smiod       internal->howto = &tic30_coff_howto_table[2];
126*3d8817e4Smiod       break;
127*3d8817e4Smiod     case R_TIC30_PC16:
128*3d8817e4Smiod       internal->howto = &tic30_coff_howto_table[4];
129*3d8817e4Smiod       break;
130*3d8817e4Smiod     default:
131*3d8817e4Smiod       abort ();
132*3d8817e4Smiod       break;
133*3d8817e4Smiod     }
134*3d8817e4Smiod }
135*3d8817e4Smiod 
136*3d8817e4Smiod #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
137*3d8817e4Smiod 
138*3d8817e4Smiod /* Perform any necessary magic to the addend in a reloc entry */
139*3d8817e4Smiod 
140*3d8817e4Smiod #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
141*3d8817e4Smiod  cache_ptr->addend =  ext_reloc.r_offset;
142*3d8817e4Smiod 
143*3d8817e4Smiod #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
144*3d8817e4Smiod  reloc_processing(relent, reloc, symbols, abfd, section)
145*3d8817e4Smiod 
146*3d8817e4Smiod static void
reloc_processing(relent,reloc,symbols,abfd,section)147*3d8817e4Smiod reloc_processing (relent, reloc, symbols, abfd, section)
148*3d8817e4Smiod      arelent *relent;
149*3d8817e4Smiod      struct internal_reloc *reloc;
150*3d8817e4Smiod      asymbol **symbols;
151*3d8817e4Smiod      bfd *abfd;
152*3d8817e4Smiod      asection *section;
153*3d8817e4Smiod {
154*3d8817e4Smiod   relent->address = reloc->r_vaddr;
155*3d8817e4Smiod   rtype2howto (relent, reloc);
156*3d8817e4Smiod 
157*3d8817e4Smiod   if (reloc->r_symndx > 0)
158*3d8817e4Smiod     relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
159*3d8817e4Smiod   else
160*3d8817e4Smiod     relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
161*3d8817e4Smiod 
162*3d8817e4Smiod   relent->addend = reloc->r_offset;
163*3d8817e4Smiod   relent->address -= section->vma;
164*3d8817e4Smiod }
165*3d8817e4Smiod 
166*3d8817e4Smiod #include "coffcode.h"
167*3d8817e4Smiod 
168*3d8817e4Smiod const bfd_target tic30_coff_vec =
169*3d8817e4Smiod {
170*3d8817e4Smiod   "coff-tic30",			/* name */
171*3d8817e4Smiod   bfd_target_coff_flavour,
172*3d8817e4Smiod   BFD_ENDIAN_BIG,		/* data byte order is big */
173*3d8817e4Smiod   BFD_ENDIAN_LITTLE,		/* header byte order is little */
174*3d8817e4Smiod 
175*3d8817e4Smiod   (HAS_RELOC | EXEC_P |		/* object flags */
176*3d8817e4Smiod    HAS_LINENO | HAS_DEBUG |
177*3d8817e4Smiod    HAS_SYMS | HAS_LOCALS | WP_TEXT),
178*3d8817e4Smiod 
179*3d8817e4Smiod   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
180*3d8817e4Smiod   '_',				/* leading symbol underscore */
181*3d8817e4Smiod   '/',				/* ar_pad_char */
182*3d8817e4Smiod   15,				/* ar_max_namelen */
183*3d8817e4Smiod   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
184*3d8817e4Smiod   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
185*3d8817e4Smiod   bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* data */
186*3d8817e4Smiod   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
187*3d8817e4Smiod   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
188*3d8817e4Smiod   bfd_getl16, bfd_getl_signed_16, bfd_putl16,	/* hdrs */
189*3d8817e4Smiod 
190*3d8817e4Smiod   {_bfd_dummy_target, coff_object_p,	/* bfd_check_format */
191*3d8817e4Smiod    bfd_generic_archive_p, _bfd_dummy_target},
192*3d8817e4Smiod   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,	/* bfd_set_format */
193*3d8817e4Smiod    bfd_false},
194*3d8817e4Smiod   {bfd_false, coff_write_object_contents,	/* bfd_write_contents */
195*3d8817e4Smiod    _bfd_write_archive_contents, bfd_false},
196*3d8817e4Smiod 
197*3d8817e4Smiod   BFD_JUMP_TABLE_GENERIC (coff),
198*3d8817e4Smiod   BFD_JUMP_TABLE_COPY (coff),
199*3d8817e4Smiod   BFD_JUMP_TABLE_CORE (_bfd_nocore),
200*3d8817e4Smiod   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
201*3d8817e4Smiod   BFD_JUMP_TABLE_SYMBOLS (coff),
202*3d8817e4Smiod   BFD_JUMP_TABLE_RELOCS (coff),
203*3d8817e4Smiod   BFD_JUMP_TABLE_WRITE (coff),
204*3d8817e4Smiod   BFD_JUMP_TABLE_LINK (coff),
205*3d8817e4Smiod   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
206*3d8817e4Smiod 
207*3d8817e4Smiod   NULL,
208*3d8817e4Smiod 
209*3d8817e4Smiod   COFF_SWAP_TABLE
210*3d8817e4Smiod };
211