1 /* D30V-specific support for 32-bit ELF
2 Copyright (C) 1997, 98, 99, 2000 Free Software Foundation, Inc.
3 Contributed by Martin Hunt (hunt@cygnus.com).
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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 02111-1307, USA. */
27 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
28 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
29 static void d30v_info_to_howto_rel
30 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rel
*));
31 static void d30v_info_to_howto_rela
32 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rela
*));
33 static bfd_reloc_status_type bfd_elf_d30v_reloc
PARAMS ((
38 asection
*input_section
,
40 char **error_message
));
41 static bfd_reloc_status_type bfd_elf_d30v_reloc_21
PARAMS ((
46 asection
*input_section
,
48 char **error_message
));
50 static reloc_howto_type elf_d30v_howto_table
[] =
52 /* This reloc does nothing. */
53 HOWTO (R_D30V_NONE
, /* type */
55 2, /* size (0 = byte, 1 = short, 2 = long) */
57 false, /* pc_relative */
59 complain_overflow_bitfield
, /* complain_on_overflow */
60 bfd_elf_generic_reloc
, /* special_function */
61 "R_D30V_NONE", /* name */
62 false, /* partial_inplace */
65 false), /* pcrel_offset */
67 /* A 6 bit absolute relocation */
68 HOWTO (R_D30V_6
, /* type */
70 2, /* size (0 = byte, 1 = short, 2 = long) */
72 false, /* pc_relative */
74 complain_overflow_bitfield
, /* complain_on_overflow */
75 bfd_elf_generic_reloc
, /* special_function */
76 "R_D30V_6", /* name */
77 false, /* partial_inplace */
80 false), /* pcrel_offset */
82 /* A relative 9 bit relocation, right shifted by 3 */
83 HOWTO (R_D30V_9_PCREL
, /* type */
85 2, /* size (0 = byte, 1 = short, 2 = long) */
87 true, /* pc_relative */
89 complain_overflow_signed
, /* complain_on_overflow */
90 bfd_elf_d30v_reloc_21
, /* special_function */
91 "R_D30V_9_PCREL", /* name */
92 false, /* partial_inplace */
95 true), /* pcrel_offset */
97 /* A relative 9 bit relocation, right shifted by 3 */
98 HOWTO (R_D30V_9_PCREL_R
, /* type */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
102 true, /* pc_relative */
104 complain_overflow_signed
, /* complain_on_overflow */
105 bfd_elf_d30v_reloc_21
, /* special_function */
106 "R_D30V_9_PCREL_R", /* name */
107 false, /* partial_inplace */
110 true), /* pcrel_offset */
112 /* An absolute 15 bit relocation, right shifted by 3 */
113 HOWTO (R_D30V_15
, /* type */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
117 false, /* pc_relative */
119 complain_overflow_signed
, /* complain_on_overflow */
120 bfd_elf_generic_reloc
, /* special_function */
121 "R_D30V_15", /* name */
122 false, /* partial_inplace */
123 0xfff, /* src_mask */
124 0xfff, /* dst_mask */
125 false), /* pcrel_offset */
127 /* A relative 15 bit relocation, right shifted by 3 */
128 HOWTO (R_D30V_15_PCREL
, /* type */
130 2, /* size (0 = byte, 1 = short, 2 = long) */
132 true, /* pc_relative */
134 complain_overflow_signed
, /* complain_on_overflow */
135 bfd_elf_d30v_reloc_21
, /* special_function */
136 "R_D30V_15_PCREL", /* name */
137 false, /* partial_inplace */
138 0xfff, /* src_mask */
139 0xfff, /* dst_mask */
140 true), /* pcrel_offset */
142 /* A relative 15 bit relocation, right shifted by 3 */
143 HOWTO (R_D30V_15_PCREL_R
, /* type */
145 2, /* size (0 = byte, 1 = short, 2 = long) */
147 true, /* pc_relative */
149 complain_overflow_signed
, /* complain_on_overflow */
150 bfd_elf_d30v_reloc_21
, /* special_function */
151 "R_D30V_15_PCREL_R", /* name */
152 false, /* partial_inplace */
153 0xfff, /* src_mask */
154 0xfff, /* dst_mask */
155 true), /* pcrel_offset */
157 /* An absolute 21 bit relocation, right shifted by 3 */
158 HOWTO (R_D30V_21
, /* type */
160 2, /* size (0 = byte, 1 = short, 2 = long) */
162 false, /* pc_relative */
164 complain_overflow_signed
, /* complain_on_overflow */
165 bfd_elf_generic_reloc
, /* special_function */
166 "R_D30V_21", /* name */
167 false, /* partial_inplace */
168 0x3ffff, /* src_mask */
169 0x3ffff, /* dst_mask */
170 false), /* pcrel_offset */
172 /* A relative 21 bit relocation, right shifted by 3 */
173 HOWTO (R_D30V_21_PCREL
, /* type */
175 2, /* size (0 = byte, 1 = short, 2 = long) */
177 true, /* pc_relative */
179 complain_overflow_signed
, /* complain_on_overflow */
180 bfd_elf_d30v_reloc_21
, /* special_function */
181 "R_D30V_21_PCREL", /* name */
182 false, /* partial_inplace */
183 0x3ffff, /* src_mask */
184 0x3ffff, /* dst_mask */
185 true), /* pcrel_offset */
187 /* A relative 21 bit relocation, right shifted by 3, in the Right container */
188 HOWTO (R_D30V_21_PCREL_R
, /* type */
190 2, /* size (0 = byte, 1 = short, 2 = long) */
192 true, /* pc_relative */
194 complain_overflow_signed
, /* complain_on_overflow */
195 bfd_elf_d30v_reloc_21
, /* special_function */
196 "R_D30V_21_PCREL_R", /* name */
197 false, /* partial_inplace */
198 0x3ffff, /* src_mask */
199 0x3ffff, /* dst_mask */
200 true), /* pcrel_offset */
202 /* A D30V 32 bit absolute relocation */
203 HOWTO (R_D30V_32
, /* type */
205 4, /* size (0 = byte, 1 = short, 2 = long) */
207 false, /* pc_relative */
209 complain_overflow_bitfield
, /* complain_on_overflow */
210 bfd_elf_d30v_reloc
, /* special_function */
211 "R_D30V_32", /* name */
212 false, /* partial_inplace */
213 0xffffffff, /* src_mask */
214 0xffffffff, /* dst_mask */
215 false), /* pcrel_offset */
217 /* A relative 32 bit relocation */
218 HOWTO (R_D30V_32_PCREL
, /* type */
220 4, /* size (0 = byte, 1 = short, 2 = long) */
222 true, /* pc_relative */
224 complain_overflow_signed
, /* complain_on_overflow */
225 bfd_elf_d30v_reloc
, /* special_function */
226 "R_D30V_32_PCREL", /* name */
227 false, /* partial_inplace */
228 0xffffffff, /* src_mask */
229 0xffffffff, /* dst_mask */
230 true), /* pcrel_offset */
232 /* A regular 32 bit absolute relocation */
233 HOWTO (R_D30V_32_NORMAL
, /* type */
235 2, /* size (0 = byte, 1 = short, 2 = long) */
237 false, /* pc_relative */
239 complain_overflow_bitfield
, /* complain_on_overflow */
240 bfd_elf_generic_reloc
, /* special_function */
241 "R_D30V_32_NORMAL", /* name */
242 false, /* partial_inplace */
243 0xffffffff, /* src_mask */
244 0xffffffff, /* dst_mask */
245 false), /* pcrel_offset */
249 #define MAX32 ((bfd_signed_vma) 0x7fffffff)
250 #define MIN32 (- MAX32 - 1)
252 static bfd_reloc_status_type
253 bfd_elf_d30v_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
, error_message
)
255 arelent
*reloc_entry
;
258 asection
*input_section
;
260 char **error_message
;
262 bfd_signed_vma relocation
;
263 bfd_vma in1
, in2
, num
;
264 bfd_vma tmp_addr
= 0;
265 bfd_reloc_status_type r
;
266 asection
*reloc_target_output_section
;
267 bfd_size_type addr
= reloc_entry
->address
;
268 bfd_reloc_status_type flag
= bfd_reloc_ok
;
269 bfd_vma output_base
= 0;
270 reloc_howto_type
*howto
= reloc_entry
->howto
;
271 int make_absolute
= 0;
273 if (output_bfd
!= (bfd
*) NULL
)
275 /* Partial linking -- do nothing. */
276 reloc_entry
->address
+= input_section
->output_offset
;
280 r
= bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
281 input_section
, output_bfd
, error_message
);
282 if (r
!= bfd_reloc_continue
)
285 /* a hacked-up version of bfd_perform_reloc() follows */
286 if (bfd_is_und_section (symbol
->section
)
287 && (symbol
->flags
& BSF_WEAK
) == 0
288 && output_bfd
== (bfd
*) NULL
)
289 flag
= bfd_reloc_undefined
;
291 /* Is the address of the relocation really within the section? */
292 if (reloc_entry
->address
> input_section
->_cooked_size
)
293 return bfd_reloc_outofrange
;
295 /* Work out which section the relocation is targetted at and the
296 initial relocation command value. */
298 /* Get symbol value. (Common symbols are special.) */
299 if (bfd_is_com_section (symbol
->section
))
302 relocation
= symbol
->value
;
304 reloc_target_output_section
= symbol
->section
->output_section
;
306 /* Convert input-section-relative symbol value to absolute. */
307 output_base
= reloc_target_output_section
->vma
;
308 relocation
+= output_base
+ symbol
->section
->output_offset
;
310 /* Add in supplied addend. */
311 relocation
+= reloc_entry
->addend
;
313 /* Here the variable relocation holds the final address of the
314 symbol we are relocating against, plus any addend. */
316 if (howto
->pc_relative
== true)
318 tmp_addr
= input_section
->output_section
->vma
+ input_section
->output_offset
319 + reloc_entry
->address
;
320 relocation
-= tmp_addr
;
323 in1
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ addr
);
324 in2
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ addr
+ 4);
326 /* extract the addend */
327 num
= ((in2
& 0x3FFFF)
328 | ((in2
& 0xFF00000) >> 2)
329 | ((in1
& 0x3F) << 26));
335 if (howto
->pc_relative
== true && howto
->bitsize
== 32)
337 /* The D30V has a PC that doesn't wrap and PC-relative jumps are
338 signed, so a PC-relative jump can't be more than +/- 2^31 bytes.
339 If one exceeds this, change it to an absolute jump. */
340 if (relocation
> MAX32
|| relocation
< MIN32
)
342 relocation
= (relocation
+ tmp_addr
) & 0xffffffff;
347 in1
|= (relocation
>> 26) & 0x3F; /* top 6 bits */
348 in2
|= ((relocation
& 0x03FC0000) << 2); /* next 8 bits */
349 in2
|= relocation
& 0x0003FFFF; /* bottom 18 bits */
351 /* change a PC-relative instruction to its absolute equivalent */
352 /* with this simple hack */
356 bfd_put_32 (abfd
, in1
, (bfd_byte
*) data
+ addr
);
357 bfd_put_32 (abfd
, in2
, (bfd_byte
*) data
+ addr
+ 4);
363 static bfd_reloc_status_type
364 bfd_elf_d30v_reloc_21 (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
, error_message
)
366 arelent
*reloc_entry
;
369 asection
*input_section
;
371 char **error_message
;
375 bfd_reloc_status_type r
;
376 asection
*reloc_target_output_section
;
377 bfd_size_type addr
= reloc_entry
->address
;
378 bfd_reloc_status_type flag
= bfd_reloc_ok
;
379 bfd_vma output_base
= 0;
380 reloc_howto_type
*howto
= reloc_entry
->howto
;
383 if (output_bfd
!= (bfd
*) NULL
)
385 /* Partial linking -- do nothing. */
386 reloc_entry
->address
+= input_section
->output_offset
;
390 r
= bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
391 input_section
, output_bfd
, error_message
);
392 if (r
!= bfd_reloc_continue
)
395 /* a hacked-up version of bfd_perform_reloc() follows */
396 if (bfd_is_und_section (symbol
->section
)
397 && (symbol
->flags
& BSF_WEAK
) == 0
398 && output_bfd
== (bfd
*) NULL
)
399 flag
= bfd_reloc_undefined
;
401 /* Is the address of the relocation really within the section? */
402 if (reloc_entry
->address
> input_section
->_cooked_size
)
403 return bfd_reloc_outofrange
;
405 /* Work out which section the relocation is targetted at and the
406 initial relocation command value. */
408 /* Get symbol value. (Common symbols are special.) */
409 if (bfd_is_com_section (symbol
->section
))
412 relocation
= symbol
->value
;
414 reloc_target_output_section
= symbol
->section
->output_section
;
416 /* Convert input-section-relative symbol value to absolute. */
417 output_base
= reloc_target_output_section
->vma
;
418 relocation
+= output_base
+ symbol
->section
->output_offset
;
420 /* Add in supplied addend. */
421 relocation
+= reloc_entry
->addend
;
423 /* Here the variable relocation holds the final address of the
424 symbol we are relocating against, plus any addend. */
426 if (howto
->pc_relative
== true)
428 relocation
-= (input_section
->output_section
->vma
429 + input_section
->output_offset
);
430 if (howto
->pcrel_offset
== true)
431 relocation
-= reloc_entry
->address
;
434 in1
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ addr
);
436 mask
= (1 << howto
->bitsize
) - 1;
437 if (howto
->bitsize
== 6)
439 max
= (1 << (howto
->bitsize
+ 2)) - 1;
441 /* extract the addend */
442 num
= in1
& mask
; /* 18 bits */
443 if (howto
->bitsize
== 6)
445 num
<<= 3; /* shift left 3 */
446 in1
&= ~mask
; /* mask out addend */
449 if (howto
->type
== R_D30V_21_PCREL_R
|| howto
->type
== R_D30V_15_PCREL_R
||
450 howto
->type
== R_D30V_9_PCREL_R
)
455 if ((int)relocation
< 0 )
457 if (~(int)relocation
> max
)
458 flag
= bfd_reloc_overflow
;
462 if ((int)relocation
> max
)
463 flag
= bfd_reloc_overflow
;
466 if (howto
->bitsize
== 6)
467 in1
|= ((relocation
& (mask
>> 12)) << 12);
469 in1
|= relocation
& mask
;
471 bfd_put_32 (abfd
, in1
, (bfd_byte
*) data
+ addr
);
476 /* Map BFD reloc types to D30V ELF reloc types. */
478 struct d30v_reloc_map
480 bfd_reloc_code_real_type bfd_reloc_val
;
481 unsigned char elf_reloc_val
;
485 static const struct d30v_reloc_map d30v_reloc_map
[] =
487 { BFD_RELOC_NONE
, R_D30V_NONE
, },
488 { BFD_RELOC_D30V_6
, R_D30V_6
},
489 { BFD_RELOC_D30V_9_PCREL
, R_D30V_9_PCREL
},
490 { BFD_RELOC_D30V_9_PCREL_R
, R_D30V_9_PCREL_R
},
491 { BFD_RELOC_D30V_15
, R_D30V_15
},
492 { BFD_RELOC_D30V_15_PCREL
, R_D30V_15_PCREL
},
493 { BFD_RELOC_D30V_15_PCREL_R
, R_D30V_15_PCREL_R
},
494 { BFD_RELOC_D30V_21
, R_D30V_21
},
495 { BFD_RELOC_D30V_21_PCREL
, R_D30V_21_PCREL
},
496 { BFD_RELOC_D30V_21_PCREL_R
, R_D30V_21_PCREL_R
},
497 { BFD_RELOC_D30V_32
, R_D30V_32
},
498 { BFD_RELOC_D30V_32_PCREL
, R_D30V_32_PCREL
},
499 { BFD_RELOC_32
, R_D30V_32_NORMAL
},
502 static reloc_howto_type
*
503 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
504 bfd
*abfd ATTRIBUTE_UNUSED
;
505 bfd_reloc_code_real_type code
;
510 i
< sizeof (d30v_reloc_map
) / sizeof (struct d30v_reloc_map
);
513 if (d30v_reloc_map
[i
].bfd_reloc_val
== code
)
514 return &elf_d30v_howto_table
[d30v_reloc_map
[i
].elf_reloc_val
];
520 /* Set the howto pointer for an D30V ELF reloc (type REL). */
523 d30v_info_to_howto_rel (abfd
, cache_ptr
, dst
)
524 bfd
*abfd ATTRIBUTE_UNUSED
;
526 Elf32_Internal_Rel
*dst
;
530 r_type
= ELF32_R_TYPE (dst
->r_info
);
531 BFD_ASSERT (r_type
< (unsigned int) R_D30V_max
);
532 cache_ptr
->howto
= &elf_d30v_howto_table
[r_type
];
535 /* Set the howto pointer for an D30V ELF reloc (type RELA). */
538 d30v_info_to_howto_rela (abfd
, cache_ptr
, dst
)
539 bfd
*abfd ATTRIBUTE_UNUSED
;
541 Elf32_Internal_Rela
*dst
;
545 r_type
= ELF32_R_TYPE (dst
->r_info
);
546 BFD_ASSERT (r_type
< (unsigned int) R_D30V_max
);
547 cache_ptr
->howto
= &elf_d30v_howto_table
[r_type
];
550 #define ELF_ARCH bfd_arch_d30v
551 #define ELF_MACHINE_CODE EM_CYGNUS_D30V
552 #define ELF_MAXPAGESIZE 0x1000
554 #define TARGET_BIG_SYM bfd_elf32_d30v_vec
555 #define TARGET_BIG_NAME "elf32-d30v"
557 #define elf_info_to_howto d30v_info_to_howto_rela
558 #define elf_info_to_howto_rel d30v_info_to_howto_rel
559 #define elf_backend_object_p 0
560 #define elf_backend_final_write_processing 0
562 #include "elf32-target.h"