1/* BFD back-end for TMS320C30 coff binaries.
2   Copyright 1998, 1999, 2000, 2001, 2002 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#include "bfd.h"
23#include "sysdep.h"
24#include "libbfd.h"
25#include "bfdlink.h"
26#include "coff/tic30.h"
27#include "coff/internal.h"
28#include "libcoff.h"
29
30static int  coff_tic30_select_reloc PARAMS ((reloc_howto_type *));
31static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
32static void reloc_processing PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
33
34reloc_howto_type * tic30_coff_reloc_type_lookup PARAMS ((bfd *, bfd_reloc_code_real_type));
35
36#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
37
38reloc_howto_type tic30_coff_howto_table[] =
39  {
40    HOWTO (R_TIC30_ABS16, 2, 1, 16, FALSE, 0, 0, NULL,
41	   "16", FALSE, 0x0000FFFF, 0x0000FFFF, FALSE),
42    HOWTO (R_TIC30_ABS24, 2, 2, 24, FALSE, 8, complain_overflow_bitfield, NULL,
43	   "24", FALSE, 0xFFFFFF00, 0xFFFFFF00, FALSE),
44    HOWTO (R_TIC30_LDP, 18, 0, 24, FALSE, 0, complain_overflow_bitfield, NULL,
45	   "LDP", FALSE, 0x00FF0000, 0x000000FF, FALSE),
46    HOWTO (R_TIC30_ABS32, 2, 2, 32, FALSE, 0, complain_overflow_bitfield, NULL,
47	   "32", FALSE, 0xFFFFFFFF, 0xFFFFFFFF, FALSE),
48    HOWTO (R_TIC30_PC16, 2, 1, 16, TRUE, 0, complain_overflow_signed, NULL,
49	   "PCREL", FALSE, 0x0000FFFF, 0x0000FFFF, FALSE),
50    EMPTY_HOWTO (-1)
51  };
52
53#ifndef coff_bfd_reloc_type_lookup
54#define coff_bfd_reloc_type_lookup tic30_coff_reloc_type_lookup
55
56/* For the case statement use the code values used in tc_gen_reloc to
57   map to the howto table entries that match those in both the aout
58   and coff implementations.  */
59
60reloc_howto_type *
61tic30_coff_reloc_type_lookup (abfd, code)
62     bfd *abfd ATTRIBUTE_UNUSED;
63     bfd_reloc_code_real_type code;
64{
65  switch (code)
66    {
67    case BFD_RELOC_8:
68    case BFD_RELOC_TIC30_LDP:
69      return &tic30_coff_howto_table[2];
70    case BFD_RELOC_16:
71      return &tic30_coff_howto_table[0];
72    case BFD_RELOC_24:
73      return &tic30_coff_howto_table[1];
74    case BFD_RELOC_16_PCREL:
75      return &tic30_coff_howto_table[4];
76    case BFD_RELOC_32:
77      return &tic30_coff_howto_table[3];
78    default:
79      return (reloc_howto_type *) NULL;
80    }
81}
82
83#endif
84
85/* Turn a howto into a reloc number.  */
86
87static int
88coff_tic30_select_reloc (howto)
89     reloc_howto_type *howto;
90{
91  return howto->type;
92}
93
94#define SELECT_RELOC(x,howto) x.r_type = coff_tic30_select_reloc(howto)
95
96#define BADMAG(x) TIC30BADMAG(x)
97#define TIC30 1			/* Customize coffcode.h */
98#define __A_MAGIC_SET__
99
100/* Code to swap in the reloc */
101#define SWAP_IN_RELOC_OFFSET  H_GET_32
102#define SWAP_OUT_RELOC_OFFSET H_PUT_32
103#define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) dst->r_stuff[0] = 'S'; \
104dst->r_stuff[1] = 'C';
105
106/* Code to turn a r_type into a howto ptr, uses the above howto table.  */
107
108static void
109rtype2howto (internal, dst)
110     arelent *internal;
111     struct internal_reloc *dst;
112{
113  switch (dst->r_type)
114    {
115    case R_TIC30_ABS16:
116      internal->howto = &tic30_coff_howto_table[0];
117      break;
118    case R_TIC30_ABS24:
119      internal->howto = &tic30_coff_howto_table[1];
120      break;
121    case R_TIC30_ABS32:
122      internal->howto = &tic30_coff_howto_table[3];
123      break;
124    case R_TIC30_LDP:
125      internal->howto = &tic30_coff_howto_table[2];
126      break;
127    case R_TIC30_PC16:
128      internal->howto = &tic30_coff_howto_table[4];
129      break;
130    default:
131      abort ();
132      break;
133    }
134}
135
136#define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
137
138/* Perform any necessary magic to the addend in a reloc entry */
139
140#define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
141 cache_ptr->addend =  ext_reloc.r_offset;
142
143#define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
144 reloc_processing(relent, reloc, symbols, abfd, section)
145
146static void
147reloc_processing (relent, reloc, symbols, abfd, section)
148     arelent *relent;
149     struct internal_reloc *reloc;
150     asymbol **symbols;
151     bfd *abfd;
152     asection *section;
153{
154  relent->address = reloc->r_vaddr;
155  rtype2howto (relent, reloc);
156
157  if (reloc->r_symndx > 0)
158    relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
159  else
160    relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
161
162  relent->addend = reloc->r_offset;
163  relent->address -= section->vma;
164}
165
166#include "coffcode.h"
167
168const bfd_target tic30_coff_vec =
169{
170  "coff-tic30",			/* name */
171  bfd_target_coff_flavour,
172  BFD_ENDIAN_BIG,		/* data byte order is big */
173  BFD_ENDIAN_LITTLE,		/* header byte order is little */
174
175  (HAS_RELOC | EXEC_P |		/* object flags */
176   HAS_LINENO | HAS_DEBUG |
177   HAS_SYMS | HAS_LOCALS | WP_TEXT),
178
179  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
180  '_',				/* leading symbol underscore */
181  '/',				/* ar_pad_char */
182  15,				/* ar_max_namelen */
183  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
184  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
185  bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* data */
186  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
187  bfd_getl32, bfd_getl_signed_32, bfd_putl32,
188  bfd_getl16, bfd_getl_signed_16, bfd_putl16,	/* hdrs */
189
190  {_bfd_dummy_target, coff_object_p,	/* bfd_check_format */
191   bfd_generic_archive_p, _bfd_dummy_target},
192  {bfd_false, coff_mkobject, _bfd_generic_mkarchive,	/* bfd_set_format */
193   bfd_false},
194  {bfd_false, coff_write_object_contents,	/* bfd_write_contents */
195   _bfd_write_archive_contents, bfd_false},
196
197  BFD_JUMP_TABLE_GENERIC (coff),
198  BFD_JUMP_TABLE_COPY (coff),
199  BFD_JUMP_TABLE_CORE (_bfd_nocore),
200  BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
201  BFD_JUMP_TABLE_SYMBOLS (coff),
202  BFD_JUMP_TABLE_RELOCS (coff),
203  BFD_JUMP_TABLE_WRITE (coff),
204  BFD_JUMP_TABLE_LINK (coff),
205  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
206
207  NULL,
208
209  COFF_SWAP_TABLE
210};
211