1*3d8817e4Smiod /* D30V-specific support for 32-bit ELF
2*3d8817e4Smiod    Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3*3d8817e4Smiod    Free Software Foundation, Inc.
4*3d8817e4Smiod    Contributed by Martin Hunt (hunt@cygnus.com).
5*3d8817e4Smiod 
6*3d8817e4Smiod    This file is part of BFD, the Binary File Descriptor library.
7*3d8817e4Smiod 
8*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
9*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
10*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
11*3d8817e4Smiod    (at your option) any later version.
12*3d8817e4Smiod 
13*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
14*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*3d8817e4Smiod    GNU General Public License for more details.
17*3d8817e4Smiod 
18*3d8817e4Smiod    You should have received a copy of the GNU General Public License
19*3d8817e4Smiod    along with this program; if not, write to the Free Software
20*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*3d8817e4Smiod    MA 02110-1301, USA.  */
22*3d8817e4Smiod 
23*3d8817e4Smiod #include "bfd.h"
24*3d8817e4Smiod #include "sysdep.h"
25*3d8817e4Smiod #include "libbfd.h"
26*3d8817e4Smiod #include "elf-bfd.h"
27*3d8817e4Smiod #include "elf/d30v.h"
28*3d8817e4Smiod 
29*3d8817e4Smiod #define MAX32 ((bfd_signed_vma) 0x7fffffff)
30*3d8817e4Smiod #define MIN32 (- MAX32 - 1)
31*3d8817e4Smiod 
32*3d8817e4Smiod static bfd_reloc_status_type
bfd_elf_d30v_reloc(bfd * abfd,arelent * reloc_entry,asymbol * symbol,void * data,asection * input_section,bfd * output_bfd,char ** error_message)33*3d8817e4Smiod bfd_elf_d30v_reloc (bfd *abfd,
34*3d8817e4Smiod 		    arelent *reloc_entry,
35*3d8817e4Smiod 		    asymbol *symbol,
36*3d8817e4Smiod 		    void * data,
37*3d8817e4Smiod 		    asection *input_section,
38*3d8817e4Smiod 		    bfd *output_bfd,
39*3d8817e4Smiod 		    char **error_message)
40*3d8817e4Smiod {
41*3d8817e4Smiod   bfd_signed_vma relocation;
42*3d8817e4Smiod   bfd_vma in1, in2, num;
43*3d8817e4Smiod   bfd_vma tmp_addr = 0;
44*3d8817e4Smiod   bfd_reloc_status_type r;
45*3d8817e4Smiod   asection *reloc_target_output_section;
46*3d8817e4Smiod   bfd_size_type addr = reloc_entry->address;
47*3d8817e4Smiod   bfd_reloc_status_type flag = bfd_reloc_ok;
48*3d8817e4Smiod   bfd_vma output_base = 0;
49*3d8817e4Smiod   reloc_howto_type *howto = reloc_entry->howto;
50*3d8817e4Smiod   int make_absolute = 0;
51*3d8817e4Smiod 
52*3d8817e4Smiod   if (output_bfd != NULL)
53*3d8817e4Smiod     {
54*3d8817e4Smiod       /* Partial linking -- do nothing.  */
55*3d8817e4Smiod       reloc_entry->address += input_section->output_offset;
56*3d8817e4Smiod       return bfd_reloc_ok;
57*3d8817e4Smiod     }
58*3d8817e4Smiod 
59*3d8817e4Smiod   r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
60*3d8817e4Smiod                              input_section, output_bfd, error_message);
61*3d8817e4Smiod   if (r != bfd_reloc_continue)
62*3d8817e4Smiod     return r;
63*3d8817e4Smiod 
64*3d8817e4Smiod   /* A hacked-up version of bfd_perform_reloc() follows.  */
65*3d8817e4Smiod  if (bfd_is_und_section (symbol->section)
66*3d8817e4Smiod       && (symbol->flags & BSF_WEAK) == 0
67*3d8817e4Smiod       && output_bfd == NULL)
68*3d8817e4Smiod     flag = bfd_reloc_undefined;
69*3d8817e4Smiod 
70*3d8817e4Smiod   /* Is the address of the relocation really within the section?  */
71*3d8817e4Smiod   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
72*3d8817e4Smiod     return bfd_reloc_outofrange;
73*3d8817e4Smiod 
74*3d8817e4Smiod   /* Work out which section the relocation is targeted at and the
75*3d8817e4Smiod      initial relocation command value.  */
76*3d8817e4Smiod 
77*3d8817e4Smiod   /* Get symbol value.  (Common symbols are special.)  */
78*3d8817e4Smiod   if (bfd_is_com_section (symbol->section))
79*3d8817e4Smiod     relocation = 0;
80*3d8817e4Smiod   else
81*3d8817e4Smiod     relocation = symbol->value;
82*3d8817e4Smiod 
83*3d8817e4Smiod   reloc_target_output_section = symbol->section->output_section;
84*3d8817e4Smiod 
85*3d8817e4Smiod   /* Convert input-section-relative symbol value to absolute.  */
86*3d8817e4Smiod   output_base = reloc_target_output_section->vma;
87*3d8817e4Smiod   relocation += output_base + symbol->section->output_offset;
88*3d8817e4Smiod 
89*3d8817e4Smiod   /* Add in supplied addend.  */
90*3d8817e4Smiod   relocation += reloc_entry->addend;
91*3d8817e4Smiod 
92*3d8817e4Smiod   /* Here the variable relocation holds the final address of the
93*3d8817e4Smiod      symbol we are relocating against, plus any addend.  */
94*3d8817e4Smiod   if (howto->pc_relative)
95*3d8817e4Smiod     {
96*3d8817e4Smiod       tmp_addr = input_section->output_section->vma
97*3d8817e4Smiod 	+ input_section->output_offset
98*3d8817e4Smiod 	+ reloc_entry->address;
99*3d8817e4Smiod       relocation -= tmp_addr;
100*3d8817e4Smiod     }
101*3d8817e4Smiod 
102*3d8817e4Smiod   in1 = bfd_get_32 (abfd, (bfd_byte *) data + addr);
103*3d8817e4Smiod   in2 = bfd_get_32 (abfd, (bfd_byte *) data + addr + 4);
104*3d8817e4Smiod 
105*3d8817e4Smiod   /* Extract the addend.  */
106*3d8817e4Smiod   num = ((in2 & 0x3FFFF)
107*3d8817e4Smiod 	 | ((in2 & 0xFF00000) >> 2)
108*3d8817e4Smiod 	 | ((in1 & 0x3F) << 26));
109*3d8817e4Smiod   in1 &= 0xFFFFFFC0;
110*3d8817e4Smiod   in2 = 0x80000000;
111*3d8817e4Smiod 
112*3d8817e4Smiod   relocation += num;
113*3d8817e4Smiod 
114*3d8817e4Smiod   if (howto->pc_relative && howto->bitsize == 32)
115*3d8817e4Smiod     {
116*3d8817e4Smiod       /* The D30V has a PC that doesn't wrap and PC-relative jumps are
117*3d8817e4Smiod 	 signed, so a PC-relative jump can't be more than +/- 2^31 bytes.
118*3d8817e4Smiod 	 If one exceeds this, change it to an absolute jump.  */
119*3d8817e4Smiod       if (relocation > MAX32 || relocation < MIN32)
120*3d8817e4Smiod 	{
121*3d8817e4Smiod 	  relocation = (relocation + tmp_addr) & 0xffffffff;
122*3d8817e4Smiod 	  make_absolute = 1;
123*3d8817e4Smiod 	}
124*3d8817e4Smiod     }
125*3d8817e4Smiod 
126*3d8817e4Smiod   in1 |= (relocation >> 26) & 0x3F;		/* Top 6 bits.  */
127*3d8817e4Smiod   in2 |= ((relocation & 0x03FC0000) << 2);  	/* Next 8 bits.  */
128*3d8817e4Smiod   in2 |= relocation & 0x0003FFFF;		/* Bottom 18 bits.  */
129*3d8817e4Smiod 
130*3d8817e4Smiod   /* Change a PC-relative instruction to its
131*3d8817e4Smiod      absolute equivalent with this simple hack.  */
132*3d8817e4Smiod   if (make_absolute)
133*3d8817e4Smiod     in1 |= 0x00100000;
134*3d8817e4Smiod 
135*3d8817e4Smiod   bfd_put_32 (abfd, in1, (bfd_byte *) data + addr);
136*3d8817e4Smiod   bfd_put_32 (abfd, in2, (bfd_byte *) data + addr + 4);
137*3d8817e4Smiod 
138*3d8817e4Smiod   return flag;
139*3d8817e4Smiod }
140*3d8817e4Smiod 
141*3d8817e4Smiod static bfd_reloc_status_type
bfd_elf_d30v_reloc_21(bfd * abfd,arelent * reloc_entry,asymbol * symbol,void * data,asection * input_section,bfd * output_bfd,char ** error_message)142*3d8817e4Smiod bfd_elf_d30v_reloc_21 (bfd *abfd,
143*3d8817e4Smiod 		       arelent *reloc_entry,
144*3d8817e4Smiod 		       asymbol *symbol,
145*3d8817e4Smiod 		       void * data,
146*3d8817e4Smiod 		       asection *input_section,
147*3d8817e4Smiod 		       bfd *output_bfd,
148*3d8817e4Smiod 		       char **error_message)
149*3d8817e4Smiod {
150*3d8817e4Smiod   bfd_vma relocation;
151*3d8817e4Smiod   bfd_vma in1, num;
152*3d8817e4Smiod   bfd_reloc_status_type r;
153*3d8817e4Smiod   asection *reloc_target_output_section;
154*3d8817e4Smiod   bfd_size_type addr = reloc_entry->address;
155*3d8817e4Smiod   bfd_reloc_status_type flag = bfd_reloc_ok;
156*3d8817e4Smiod   bfd_vma output_base = 0;
157*3d8817e4Smiod   reloc_howto_type *howto = reloc_entry->howto;
158*3d8817e4Smiod   int mask, max;
159*3d8817e4Smiod 
160*3d8817e4Smiod   if (output_bfd != NULL)
161*3d8817e4Smiod     {
162*3d8817e4Smiod       /* Partial linking -- do nothing.  */
163*3d8817e4Smiod       reloc_entry->address += input_section->output_offset;
164*3d8817e4Smiod       return bfd_reloc_ok;
165*3d8817e4Smiod     }
166*3d8817e4Smiod 
167*3d8817e4Smiod   r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
168*3d8817e4Smiod                              input_section, output_bfd, error_message);
169*3d8817e4Smiod   if (r != bfd_reloc_continue)
170*3d8817e4Smiod     return r;
171*3d8817e4Smiod 
172*3d8817e4Smiod   /* A hacked-up version of bfd_perform_reloc() follows.  */
173*3d8817e4Smiod   if (bfd_is_und_section (symbol->section)
174*3d8817e4Smiod       && (symbol->flags & BSF_WEAK) == 0
175*3d8817e4Smiod       && output_bfd == NULL)
176*3d8817e4Smiod     flag = bfd_reloc_undefined;
177*3d8817e4Smiod 
178*3d8817e4Smiod   /* Is the address of the relocation really within the section?  */
179*3d8817e4Smiod   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
180*3d8817e4Smiod     return bfd_reloc_outofrange;
181*3d8817e4Smiod 
182*3d8817e4Smiod   /* Work out which section the relocation is targeted at and the
183*3d8817e4Smiod      initial relocation command value.  */
184*3d8817e4Smiod 
185*3d8817e4Smiod   /* Get symbol value.  (Common symbols are special.)  */
186*3d8817e4Smiod   if (bfd_is_com_section (symbol->section))
187*3d8817e4Smiod     relocation = 0;
188*3d8817e4Smiod   else
189*3d8817e4Smiod     relocation = symbol->value;
190*3d8817e4Smiod 
191*3d8817e4Smiod   reloc_target_output_section = symbol->section->output_section;
192*3d8817e4Smiod 
193*3d8817e4Smiod   /* Convert input-section-relative symbol value to absolute.  */
194*3d8817e4Smiod   output_base = reloc_target_output_section->vma;
195*3d8817e4Smiod   relocation += output_base + symbol->section->output_offset;
196*3d8817e4Smiod 
197*3d8817e4Smiod   /* Add in supplied addend.  */
198*3d8817e4Smiod   relocation += reloc_entry->addend;
199*3d8817e4Smiod 
200*3d8817e4Smiod   /* Here the variable relocation holds the final address of the
201*3d8817e4Smiod      symbol we are relocating against, plus any addend.  */
202*3d8817e4Smiod 
203*3d8817e4Smiod   if (howto->pc_relative)
204*3d8817e4Smiod     {
205*3d8817e4Smiod       relocation -= (input_section->output_section->vma
206*3d8817e4Smiod 		     + input_section->output_offset);
207*3d8817e4Smiod       if (howto->pcrel_offset)
208*3d8817e4Smiod 	relocation -= reloc_entry->address;
209*3d8817e4Smiod     }
210*3d8817e4Smiod 
211*3d8817e4Smiod   in1 = bfd_get_32 (abfd, (bfd_byte *) data + addr);
212*3d8817e4Smiod 
213*3d8817e4Smiod   mask =  (1 << howto->bitsize) - 1;
214*3d8817e4Smiod   if (howto->bitsize == 6)
215*3d8817e4Smiod     mask <<= 12;
216*3d8817e4Smiod   max = (1 << (howto->bitsize + 2)) - 1;
217*3d8817e4Smiod 
218*3d8817e4Smiod   /* Extract the addend.  */
219*3d8817e4Smiod   num = in1 & mask;  /* 18 bits.  */
220*3d8817e4Smiod   if (howto->bitsize == 6)
221*3d8817e4Smiod     num >>= 12;
222*3d8817e4Smiod   num <<= 3; /* shift left 3.  */
223*3d8817e4Smiod   in1 &= ~mask;  /* Mask out addend.  */
224*3d8817e4Smiod 
225*3d8817e4Smiod   relocation += num;
226*3d8817e4Smiod   if (howto->type == R_D30V_21_PCREL_R
227*3d8817e4Smiod       || howto->type == R_D30V_15_PCREL_R
228*3d8817e4Smiod       || howto->type == R_D30V_9_PCREL_R)
229*3d8817e4Smiod     relocation += 4;
230*3d8817e4Smiod 
231*3d8817e4Smiod   if ((int) relocation < 0)
232*3d8817e4Smiod     {
233*3d8817e4Smiod       if (~ (int) relocation > max)
234*3d8817e4Smiod 	flag = bfd_reloc_overflow;
235*3d8817e4Smiod     }
236*3d8817e4Smiod   else
237*3d8817e4Smiod     {
238*3d8817e4Smiod       if ((int) relocation > max)
239*3d8817e4Smiod 	flag = bfd_reloc_overflow;
240*3d8817e4Smiod     }
241*3d8817e4Smiod 
242*3d8817e4Smiod   relocation >>= 3;
243*3d8817e4Smiod   if (howto->bitsize == 6)
244*3d8817e4Smiod     in1 |= ((relocation & (mask >> 12)) << 12);
245*3d8817e4Smiod   else
246*3d8817e4Smiod     in1 |= relocation & mask;
247*3d8817e4Smiod 
248*3d8817e4Smiod   bfd_put_32 (abfd, in1, (bfd_byte *) data + addr);
249*3d8817e4Smiod 
250*3d8817e4Smiod   return flag;
251*3d8817e4Smiod }
252*3d8817e4Smiod 
253*3d8817e4Smiod static reloc_howto_type elf_d30v_howto_table[] =
254*3d8817e4Smiod {
255*3d8817e4Smiod   /* This reloc does nothing.  */
256*3d8817e4Smiod   HOWTO (R_D30V_NONE,		/* Type.  */
257*3d8817e4Smiod 	 0,			/* Rightshift.  */
258*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
259*3d8817e4Smiod 	 32,			/* Bitsize.  */
260*3d8817e4Smiod 	 FALSE,			/* PC_relative.  */
261*3d8817e4Smiod 	 0,			/* Bitpos.  */
262*3d8817e4Smiod 	 complain_overflow_bitfield, /* Complain_on_overflow.  */
263*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* Special_function.  */
264*3d8817e4Smiod 	 "R_D30V_NONE",		/* Name.  */
265*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
266*3d8817e4Smiod 	 0,			/* Src_mask.  */
267*3d8817e4Smiod 	 0,			/* Dst_mask.  */
268*3d8817e4Smiod 	 FALSE),		/* PCrel_offset.  */
269*3d8817e4Smiod 
270*3d8817e4Smiod   /* A 6 bit absolute relocation.  */
271*3d8817e4Smiod   HOWTO (R_D30V_6,		/* Type.  */
272*3d8817e4Smiod 	 0,			/* Rightshift.  */
273*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
274*3d8817e4Smiod 	 6,			/* Bitsize.  */
275*3d8817e4Smiod 	 FALSE,			/* PC_relative.  */
276*3d8817e4Smiod 	 0,			/* Bitpos.  */
277*3d8817e4Smiod 	 complain_overflow_bitfield, /* Complain_on_overflow.  */
278*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* Special_function.  */
279*3d8817e4Smiod 	 "R_D30V_6",		/* Name.  */
280*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
281*3d8817e4Smiod 	 0x3f,			/* Src_mask.  */
282*3d8817e4Smiod 	 0x3f,			/* Dst_mask.  */
283*3d8817e4Smiod 	 FALSE),		/* PCrel_offset.  */
284*3d8817e4Smiod 
285*3d8817e4Smiod   /* A relative 9 bit relocation, right shifted by 3.  */
286*3d8817e4Smiod   HOWTO (R_D30V_9_PCREL,	/* Type.  */
287*3d8817e4Smiod 	 3,			/* Rightshift.  */
288*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
289*3d8817e4Smiod 	 6,			/* Bitsize.  */
290*3d8817e4Smiod 	 TRUE,			/* PC_relative.  */
291*3d8817e4Smiod 	 0,			/* Bitpos.  */
292*3d8817e4Smiod 	 complain_overflow_signed, /* Complain_on_overflow.  */
293*3d8817e4Smiod 	 bfd_elf_d30v_reloc_21,	/* Special_function.  */
294*3d8817e4Smiod 	 "R_D30V_9_PCREL",	/* Name.  */
295*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
296*3d8817e4Smiod 	 0x3f,			/* Src_mask.  */
297*3d8817e4Smiod 	 0x3f,			/* Dst_mask.  */
298*3d8817e4Smiod 	 TRUE),			/* PCrel_offset.  */
299*3d8817e4Smiod 
300*3d8817e4Smiod   /* A relative 9 bit relocation, right shifted by 3.  */
301*3d8817e4Smiod   HOWTO (R_D30V_9_PCREL_R,	/* Type.  */
302*3d8817e4Smiod 	 3,			/* Rightshift.  */
303*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
304*3d8817e4Smiod 	 6,			/* Bitsize.  */
305*3d8817e4Smiod 	 TRUE,			/* PC_relative.  */
306*3d8817e4Smiod 	 0,			/* Bitpos.  */
307*3d8817e4Smiod 	 complain_overflow_signed, /* Complain_on_overflow.  */
308*3d8817e4Smiod 	 bfd_elf_d30v_reloc_21,	/* Special_function.  */
309*3d8817e4Smiod 	 "R_D30V_9_PCREL_R",	/* Name.  */
310*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
311*3d8817e4Smiod 	 0x3f,			/* Src_mask.  */
312*3d8817e4Smiod 	 0x3f,			/* Dst_mask.  */
313*3d8817e4Smiod 	 TRUE),			/* PCrel_offset.  */
314*3d8817e4Smiod 
315*3d8817e4Smiod   /* An absolute 15 bit relocation, right shifted by 3.  */
316*3d8817e4Smiod   HOWTO (R_D30V_15,		/* Type.  */
317*3d8817e4Smiod 	 3,			/* Rightshift.  */
318*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
319*3d8817e4Smiod 	 12,			/* Bitsize.  */
320*3d8817e4Smiod 	 FALSE,			/* PC_relative.  */
321*3d8817e4Smiod 	 0,			/* Bitpos.  */
322*3d8817e4Smiod 	 complain_overflow_signed, /* Complain_on_overflow.  */
323*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* Special_function.  */
324*3d8817e4Smiod 	 "R_D30V_15",		/* Name.  */
325*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
326*3d8817e4Smiod 	 0xfff,			/* Src_mask.  */
327*3d8817e4Smiod 	 0xfff,			/* Dst_mask.  */
328*3d8817e4Smiod 	 FALSE),		/* PCrel_offset.  */
329*3d8817e4Smiod 
330*3d8817e4Smiod   /* A relative 15 bit relocation, right shifted by 3.  */
331*3d8817e4Smiod   HOWTO (R_D30V_15_PCREL,	/* Type.  */
332*3d8817e4Smiod 	 3,			/* Rightshift.  */
333*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
334*3d8817e4Smiod 	 12,			/* Bitsize.  */
335*3d8817e4Smiod 	 TRUE,			/* PC_relative.  */
336*3d8817e4Smiod 	 0,			/* Bitpos.  */
337*3d8817e4Smiod 	 complain_overflow_signed, /* Complain_on_overflow.  */
338*3d8817e4Smiod 	 bfd_elf_d30v_reloc_21,	/* Special_function.  */
339*3d8817e4Smiod 	 "R_D30V_15_PCREL",	/* Name.  */
340*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
341*3d8817e4Smiod 	 0xfff,			/* Src_mask.  */
342*3d8817e4Smiod 	 0xfff,			/* Dst_mask.  */
343*3d8817e4Smiod 	 TRUE),			/* PCrel_offset.  */
344*3d8817e4Smiod 
345*3d8817e4Smiod   /* A relative 15 bit relocation, right shifted by 3.  */
346*3d8817e4Smiod   HOWTO (R_D30V_15_PCREL_R,	/* Type.  */
347*3d8817e4Smiod 	 3,			/* Rightshift.  */
348*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
349*3d8817e4Smiod 	 12,			/* Bitsize.  */
350*3d8817e4Smiod 	 TRUE,			/* PC_relative.  */
351*3d8817e4Smiod 	 0,			/* Bitpos.  */
352*3d8817e4Smiod 	 complain_overflow_signed, /* Complain_on_overflow.  */
353*3d8817e4Smiod 	 bfd_elf_d30v_reloc_21,	/* Special_function.  */
354*3d8817e4Smiod 	 "R_D30V_15_PCREL_R",	/* Name.  */
355*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
356*3d8817e4Smiod 	 0xfff,			/* Src_mask.  */
357*3d8817e4Smiod 	 0xfff,			/* Dst_mask.  */
358*3d8817e4Smiod 	 TRUE),			/* PCrel_offset.  */
359*3d8817e4Smiod 
360*3d8817e4Smiod   /* An absolute 21 bit relocation, right shifted by 3.  */
361*3d8817e4Smiod   HOWTO (R_D30V_21,		/* Type.  */
362*3d8817e4Smiod 	 3,			/* Rightshift.  */
363*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
364*3d8817e4Smiod 	 18,			/* Bitsize.  */
365*3d8817e4Smiod 	 FALSE,			/* PC_relative.  */
366*3d8817e4Smiod 	 0,			/* Bitpos.  */
367*3d8817e4Smiod 	 complain_overflow_signed, /* Complain_on_overflow.  */
368*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* Special_function.  */
369*3d8817e4Smiod 	 "R_D30V_21",		/* Name.  */
370*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
371*3d8817e4Smiod 	 0x3ffff,		/* Src_mask.  */
372*3d8817e4Smiod 	 0x3ffff,		/* Dst_mask.  */
373*3d8817e4Smiod 	 FALSE),		/* PCrel_offset.  */
374*3d8817e4Smiod 
375*3d8817e4Smiod   /* A relative 21 bit relocation, right shifted by 3.  */
376*3d8817e4Smiod   HOWTO (R_D30V_21_PCREL,	/* Type.  */
377*3d8817e4Smiod 	 3,			/* Rightshift.  */
378*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
379*3d8817e4Smiod 	 18,			/* Bitsize.  */
380*3d8817e4Smiod 	 TRUE,			/* PC_relative.  */
381*3d8817e4Smiod 	 0,			/* Bitpos.  */
382*3d8817e4Smiod 	 complain_overflow_signed, /* Complain_on_overflow.  */
383*3d8817e4Smiod 	 bfd_elf_d30v_reloc_21,	/* Special_function.  */
384*3d8817e4Smiod 	 "R_D30V_21_PCREL",	/* Name.  */
385*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
386*3d8817e4Smiod 	 0x3ffff,		/* Src_mask.  */
387*3d8817e4Smiod 	 0x3ffff,		/* Dst_mask.  */
388*3d8817e4Smiod 	 TRUE),			/* PCrel_offset.  */
389*3d8817e4Smiod 
390*3d8817e4Smiod   /* A relative 21 bit relocation, right shifted by 3, in the Right container.  */
391*3d8817e4Smiod   HOWTO (R_D30V_21_PCREL_R,	/* Type.  */
392*3d8817e4Smiod 	 3,			/* Rightshift.  */
393*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
394*3d8817e4Smiod 	 18,			/* Bitsize.  */
395*3d8817e4Smiod 	 TRUE,			/* PC_relative.  */
396*3d8817e4Smiod 	 0,			/* Bitpos.  */
397*3d8817e4Smiod 	 complain_overflow_signed, /* Complain_on_overflow.  */
398*3d8817e4Smiod 	 bfd_elf_d30v_reloc_21,	/* Special_function.  */
399*3d8817e4Smiod 	 "R_D30V_21_PCREL_R",	/* Name.  */
400*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
401*3d8817e4Smiod 	 0x3ffff,		/* Src_mask.  */
402*3d8817e4Smiod 	 0x3ffff,		/* Dst_mask.  */
403*3d8817e4Smiod 	 TRUE),			/* PCrel_offset.  */
404*3d8817e4Smiod 
405*3d8817e4Smiod   /* A D30V 32 bit absolute relocation.  */
406*3d8817e4Smiod   HOWTO (R_D30V_32,		/* Type.  */
407*3d8817e4Smiod 	 0,			/* Rightshift.  */
408*3d8817e4Smiod 	 4,			/* Size (0 = byte, 1 = short, 2 = long).  */
409*3d8817e4Smiod 	 32,			/* Bitsize.  */
410*3d8817e4Smiod 	 FALSE,			/* PC_relative.  */
411*3d8817e4Smiod 	 0,			/* Bitpos.  */
412*3d8817e4Smiod 	 complain_overflow_bitfield, /* Complain_on_overflow.  */
413*3d8817e4Smiod 	 bfd_elf_d30v_reloc,	/* Special_function.  */
414*3d8817e4Smiod 	 "R_D30V_32",		/* Name.  */
415*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
416*3d8817e4Smiod 	 0xffffffff,		/* Src_mask.  */
417*3d8817e4Smiod 	 0xffffffff,		/* Dst_mask.  */
418*3d8817e4Smiod 	 FALSE),		/* PCrel_offset.  */
419*3d8817e4Smiod 
420*3d8817e4Smiod   /* A relative 32 bit relocation.  */
421*3d8817e4Smiod   HOWTO (R_D30V_32_PCREL,	/* Type.  */
422*3d8817e4Smiod 	 0,			/* Rightshift.  */
423*3d8817e4Smiod 	 4,			/* Size (0 = byte, 1 = short, 2 = long).  */
424*3d8817e4Smiod 	 32,			/* Bitsize.  */
425*3d8817e4Smiod 	 TRUE,			/* PC_relative.  */
426*3d8817e4Smiod 	 0,			/* Bitpos.  */
427*3d8817e4Smiod 	 complain_overflow_signed, /* Complain_on_overflow.  */
428*3d8817e4Smiod 	 bfd_elf_d30v_reloc,	/* Special_function.  */
429*3d8817e4Smiod 	 "R_D30V_32_PCREL",	/* Name.  */
430*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
431*3d8817e4Smiod 	 0xffffffff,		/* Src_mask.  */
432*3d8817e4Smiod 	 0xffffffff,		/* Dst_mask.  */
433*3d8817e4Smiod 	 TRUE),			/* PCrel_offset.  */
434*3d8817e4Smiod 
435*3d8817e4Smiod   /* A regular 32 bit absolute relocation.  */
436*3d8817e4Smiod   HOWTO (R_D30V_32_NORMAL,	/* Type.  */
437*3d8817e4Smiod 	 0,			/* Rightshift.  */
438*3d8817e4Smiod 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
439*3d8817e4Smiod 	 32,			/* Bitsize.  */
440*3d8817e4Smiod 	 FALSE,			/* PC_relative.  */
441*3d8817e4Smiod 	 0,			/* Bitpos.  */
442*3d8817e4Smiod 	 complain_overflow_bitfield, /* Complain_on_overflow.  */
443*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* Special_function.  */
444*3d8817e4Smiod 	 "R_D30V_32_NORMAL",	/* Name.  */
445*3d8817e4Smiod 	 FALSE,			/* Partial_inplace.  */
446*3d8817e4Smiod 	 0xffffffff,		/* Src_mask.  */
447*3d8817e4Smiod 	 0xffffffff,		/* Dst_mask.  */
448*3d8817e4Smiod 	 FALSE),		/* PCrel_offset.  */
449*3d8817e4Smiod 
450*3d8817e4Smiod };
451*3d8817e4Smiod 
452*3d8817e4Smiod /* Map BFD reloc types to D30V ELF reloc types.  */
453*3d8817e4Smiod 
454*3d8817e4Smiod struct d30v_reloc_map
455*3d8817e4Smiod {
456*3d8817e4Smiod   bfd_reloc_code_real_type bfd_reloc_val;
457*3d8817e4Smiod   unsigned char elf_reloc_val;
458*3d8817e4Smiod };
459*3d8817e4Smiod 
460*3d8817e4Smiod static const struct d30v_reloc_map d30v_reloc_map[] =
461*3d8817e4Smiod {
462*3d8817e4Smiod   { BFD_RELOC_NONE, R_D30V_NONE, },
463*3d8817e4Smiod   { BFD_RELOC_D30V_6, R_D30V_6 },
464*3d8817e4Smiod   { BFD_RELOC_D30V_9_PCREL, R_D30V_9_PCREL },
465*3d8817e4Smiod   { BFD_RELOC_D30V_9_PCREL_R, R_D30V_9_PCREL_R },
466*3d8817e4Smiod   { BFD_RELOC_D30V_15, R_D30V_15 },
467*3d8817e4Smiod   { BFD_RELOC_D30V_15_PCREL, R_D30V_15_PCREL },
468*3d8817e4Smiod   { BFD_RELOC_D30V_15_PCREL_R, R_D30V_15_PCREL_R },
469*3d8817e4Smiod   { BFD_RELOC_D30V_21, R_D30V_21 },
470*3d8817e4Smiod   { BFD_RELOC_D30V_21_PCREL, R_D30V_21_PCREL },
471*3d8817e4Smiod   { BFD_RELOC_D30V_21_PCREL_R, R_D30V_21_PCREL_R },
472*3d8817e4Smiod   { BFD_RELOC_D30V_32, R_D30V_32 },
473*3d8817e4Smiod   { BFD_RELOC_D30V_32_PCREL, R_D30V_32_PCREL },
474*3d8817e4Smiod   { BFD_RELOC_32, R_D30V_32_NORMAL },
475*3d8817e4Smiod };
476*3d8817e4Smiod 
477*3d8817e4Smiod static reloc_howto_type *
bfd_elf32_bfd_reloc_type_lookup(bfd * abfd ATTRIBUTE_UNUSED,bfd_reloc_code_real_type code)478*3d8817e4Smiod bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
479*3d8817e4Smiod 				 bfd_reloc_code_real_type code)
480*3d8817e4Smiod {
481*3d8817e4Smiod   unsigned int i;
482*3d8817e4Smiod 
483*3d8817e4Smiod   for (i = 0;
484*3d8817e4Smiod        i < sizeof (d30v_reloc_map) / sizeof (struct d30v_reloc_map);
485*3d8817e4Smiod        i++)
486*3d8817e4Smiod     {
487*3d8817e4Smiod       if (d30v_reloc_map[i].bfd_reloc_val == code)
488*3d8817e4Smiod 	return &elf_d30v_howto_table[d30v_reloc_map[i].elf_reloc_val];
489*3d8817e4Smiod     }
490*3d8817e4Smiod 
491*3d8817e4Smiod   return NULL;
492*3d8817e4Smiod }
493*3d8817e4Smiod 
494*3d8817e4Smiod /* Set the howto pointer for an D30V ELF reloc (type REL).  */
495*3d8817e4Smiod 
496*3d8817e4Smiod static void
d30v_info_to_howto_rel(bfd * abfd ATTRIBUTE_UNUSED,arelent * cache_ptr,Elf_Internal_Rela * dst)497*3d8817e4Smiod d30v_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
498*3d8817e4Smiod 			arelent *cache_ptr,
499*3d8817e4Smiod 			Elf_Internal_Rela *dst)
500*3d8817e4Smiod {
501*3d8817e4Smiod   unsigned int r_type;
502*3d8817e4Smiod 
503*3d8817e4Smiod   r_type = ELF32_R_TYPE (dst->r_info);
504*3d8817e4Smiod   BFD_ASSERT (r_type < (unsigned int) R_D30V_max);
505*3d8817e4Smiod   cache_ptr->howto = &elf_d30v_howto_table[r_type];
506*3d8817e4Smiod }
507*3d8817e4Smiod 
508*3d8817e4Smiod /* Set the howto pointer for an D30V ELF reloc (type RELA).  */
509*3d8817e4Smiod 
510*3d8817e4Smiod static void
d30v_info_to_howto_rela(bfd * abfd ATTRIBUTE_UNUSED,arelent * cache_ptr,Elf_Internal_Rela * dst)511*3d8817e4Smiod d30v_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
512*3d8817e4Smiod 			 arelent *cache_ptr,
513*3d8817e4Smiod 			 Elf_Internal_Rela *dst)
514*3d8817e4Smiod {
515*3d8817e4Smiod   unsigned int r_type;
516*3d8817e4Smiod 
517*3d8817e4Smiod   r_type = ELF32_R_TYPE (dst->r_info);
518*3d8817e4Smiod   BFD_ASSERT (r_type < (unsigned int) R_D30V_max);
519*3d8817e4Smiod   cache_ptr->howto = &elf_d30v_howto_table[r_type];
520*3d8817e4Smiod }
521*3d8817e4Smiod 
522*3d8817e4Smiod #define ELF_ARCH		bfd_arch_d30v
523*3d8817e4Smiod #define ELF_MACHINE_CODE	EM_D30V
524*3d8817e4Smiod #define ELF_MACHINE_ALT1	EM_CYGNUS_D30V
525*3d8817e4Smiod #define ELF_MAXPAGESIZE		0x1000
526*3d8817e4Smiod 
527*3d8817e4Smiod #define TARGET_BIG_SYM          bfd_elf32_d30v_vec
528*3d8817e4Smiod #define TARGET_BIG_NAME		"elf32-d30v"
529*3d8817e4Smiod 
530*3d8817e4Smiod #define elf_info_to_howto	d30v_info_to_howto_rela
531*3d8817e4Smiod #define elf_info_to_howto_rel	d30v_info_to_howto_rel
532*3d8817e4Smiod #define elf_backend_object_p	0
533*3d8817e4Smiod #define elf_backend_final_write_processing	0
534*3d8817e4Smiod 
535*3d8817e4Smiod #include "elf32-target.h"
536