1 /* D30V-specific support for 32-bit ELF
2 Copyright (C) 1997, 1998, 1999 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. */
26 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
27 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
28 static void d30v_info_to_howto_rel
29 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
30 static void d30v_info_to_howto_rela
31 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
32 static bfd_reloc_status_type bfd_elf_d30v_reloc PARAMS ((
37 asection *input_section,
39 char **error_message));
40 static bfd_reloc_status_type bfd_elf_d30v_reloc_21 PARAMS ((
45 asection *input_section,
47 char **error_message));
67 static reloc_howto_type elf_d30v_howto_table[] =
69 /* This reloc does nothing. */
70 HOWTO (R_D30V_NONE, /* type */
72 2, /* size (0 = byte, 1 = short, 2 = long) */
74 false, /* pc_relative */
76 complain_overflow_bitfield, /* complain_on_overflow */
77 bfd_elf_generic_reloc, /* special_function */
78 "R_D30V_NONE", /* name */
79 false, /* partial_inplace */
82 false), /* pcrel_offset */
84 /* A 6 bit absolute relocation */
85 HOWTO (R_D30V_6, /* type */
87 2, /* size (0 = byte, 1 = short, 2 = long) */
89 false, /* pc_relative */
91 complain_overflow_bitfield, /* complain_on_overflow */
92 bfd_elf_generic_reloc, /* special_function */
93 "R_D30V_6", /* name */
94 false, /* partial_inplace */
97 false), /* pcrel_offset */
99 /* A relative 9 bit relocation, right shifted by 3 */
100 HOWTO (R_D30V_9_PCREL, /* type */
102 2, /* size (0 = byte, 1 = short, 2 = long) */
104 true, /* pc_relative */
106 complain_overflow_signed, /* complain_on_overflow */
107 bfd_elf_d30v_reloc_21, /* special_function */
108 "R_D30V_9_PCREL", /* name */
109 false, /* partial_inplace */
112 true), /* pcrel_offset */
114 /* A relative 9 bit relocation, right shifted by 3 */
115 HOWTO (R_D30V_9_PCREL_R, /* type */
117 2, /* size (0 = byte, 1 = short, 2 = long) */
119 true, /* pc_relative */
121 complain_overflow_signed, /* complain_on_overflow */
122 bfd_elf_d30v_reloc_21, /* special_function */
123 "R_D30V_9_PCREL_R", /* name */
124 false, /* partial_inplace */
127 true), /* pcrel_offset */
129 /* An absolute 15 bit relocation, right shifted by 3 */
130 HOWTO (R_D30V_15, /* type */
132 2, /* size (0 = byte, 1 = short, 2 = long) */
134 false, /* pc_relative */
136 complain_overflow_signed, /* complain_on_overflow */
137 bfd_elf_generic_reloc, /* special_function */
138 "R_D30V_15", /* name */
139 false, /* partial_inplace */
140 0xfff, /* src_mask */
141 0xfff, /* dst_mask */
142 false), /* pcrel_offset */
144 /* A relative 15 bit relocation, right shifted by 3 */
145 HOWTO (R_D30V_15_PCREL, /* type */
147 2, /* size (0 = byte, 1 = short, 2 = long) */
149 true, /* pc_relative */
151 complain_overflow_signed, /* complain_on_overflow */
152 bfd_elf_d30v_reloc_21, /* special_function */
153 "R_D30V_15_PCREL", /* name */
154 false, /* partial_inplace */
155 0xfff, /* src_mask */
156 0xfff, /* dst_mask */
157 true), /* pcrel_offset */
159 /* A relative 15 bit relocation, right shifted by 3 */
160 HOWTO (R_D30V_15_PCREL_R, /* type */
162 2, /* size (0 = byte, 1 = short, 2 = long) */
164 true, /* pc_relative */
166 complain_overflow_signed, /* complain_on_overflow */
167 bfd_elf_d30v_reloc_21, /* special_function */
168 "R_D30V_15_PCREL_R", /* name */
169 false, /* partial_inplace */
170 0xfff, /* src_mask */
171 0xfff, /* dst_mask */
172 true), /* pcrel_offset */
174 /* An absolute 21 bit relocation, right shifted by 3 */
175 HOWTO (R_D30V_21, /* type */
177 2, /* size (0 = byte, 1 = short, 2 = long) */
179 false, /* pc_relative */
181 complain_overflow_signed, /* complain_on_overflow */
182 bfd_elf_generic_reloc, /* special_function */
183 "R_D30V_21", /* name */
184 false, /* partial_inplace */
185 0x3ffff, /* src_mask */
186 0x3ffff, /* dst_mask */
187 false), /* pcrel_offset */
189 /* A relative 21 bit relocation, right shifted by 3 */
190 HOWTO (R_D30V_21_PCREL, /* type */
192 2, /* size (0 = byte, 1 = short, 2 = long) */
194 true, /* pc_relative */
196 complain_overflow_signed, /* complain_on_overflow */
197 bfd_elf_d30v_reloc_21, /* special_function */
198 "R_D30V_21_PCREL", /* name */
199 false, /* partial_inplace */
200 0x3ffff, /* src_mask */
201 0x3ffff, /* dst_mask */
202 true), /* pcrel_offset */
204 /* A relative 21 bit relocation, right shifted by 3, in the Right container */
205 HOWTO (R_D30V_21_PCREL_R, /* type */
207 2, /* size (0 = byte, 1 = short, 2 = long) */
209 true, /* pc_relative */
211 complain_overflow_signed, /* complain_on_overflow */
212 bfd_elf_d30v_reloc_21, /* special_function */
213 "R_D30V_21_PCREL_R", /* name */
214 false, /* partial_inplace */
215 0x3ffff, /* src_mask */
216 0x3ffff, /* dst_mask */
217 true), /* pcrel_offset */
219 /* A D30V 32 bit absolute relocation */
220 HOWTO (R_D30V_32, /* type */
222 4, /* size (0 = byte, 1 = short, 2 = long) */
224 false, /* pc_relative */
226 complain_overflow_bitfield, /* complain_on_overflow */
227 bfd_elf_d30v_reloc, /* special_function */
228 "R_D30V_32", /* name */
229 false, /* partial_inplace */
230 0xffffffff, /* src_mask */
231 0xffffffff, /* dst_mask */
232 false), /* pcrel_offset */
234 /* A relative 32 bit relocation */
235 HOWTO (R_D30V_32_PCREL, /* type */
237 4, /* size (0 = byte, 1 = short, 2 = long) */
239 true, /* pc_relative */
241 complain_overflow_signed, /* complain_on_overflow */
242 bfd_elf_d30v_reloc, /* special_function */
243 "R_D30V_32_PCREL", /* name */
244 false, /* partial_inplace */
245 0xffffffff, /* src_mask */
246 0xffffffff, /* dst_mask */
247 true), /* pcrel_offset */
249 /* A regular 32 bit absolute relocation */
250 HOWTO (R_D30V_32_NORMAL, /* type */
252 2, /* size (0 = byte, 1 = short, 2 = long) */
254 false, /* pc_relative */
256 complain_overflow_bitfield, /* complain_on_overflow */
257 bfd_elf_generic_reloc, /* special_function */
258 "R_D30V_32_NORMAL", /* name */
259 false, /* partial_inplace */
260 0xffffffff, /* src_mask */
261 0xffffffff, /* dst_mask */
262 false), /* pcrel_offset */
266 #define MIN32 (long long)0xffffffff80000000LL
267 #define MAX32 0x7fffffffLL
269 static bfd_reloc_status_type
270 bfd_elf_d30v_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd, error_message)
272 arelent *reloc_entry;
275 asection *input_section;
277 char **error_message;
279 long long relocation;
280 bfd_vma in1, in2, num;
281 bfd_vma tmp_addr = 0;
282 bfd_reloc_status_type r;
283 asection *reloc_target_output_section;
284 bfd_size_type addr = reloc_entry->address;
285 bfd_reloc_status_type flag = bfd_reloc_ok;
286 bfd_vma output_base = 0;
287 reloc_howto_type *howto = reloc_entry->howto;
288 int make_absolute = 0;
290 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
291 input_section, output_bfd, error_message);
292 if (r != bfd_reloc_continue)
295 /* a hacked-up version of bfd_perform_reloc() follows */
296 if (bfd_is_und_section (symbol->section)
297 && (symbol->flags & BSF_WEAK) == 0
298 && output_bfd == (bfd *) NULL)
299 flag = bfd_reloc_undefined;
301 /* Is the address of the relocation really within the section? */
302 if (reloc_entry->address > input_section->_cooked_size)
303 return bfd_reloc_outofrange;
305 /* Work out which section the relocation is targetted at and the
306 initial relocation command value. */
308 /* Get symbol value. (Common symbols are special.) */
309 if (bfd_is_com_section (symbol->section))
312 relocation = symbol->value;
314 reloc_target_output_section = symbol->section->output_section;
316 /* Convert input-section-relative symbol value to absolute. */
320 output_base = reloc_target_output_section->vma;
322 relocation += output_base + symbol->section->output_offset;
324 /* Add in supplied addend. */
325 relocation += reloc_entry->addend;
327 /* Here the variable relocation holds the final address of the
328 symbol we are relocating against, plus any addend. */
330 if (howto->pc_relative == true)
332 tmp_addr = input_section->output_section->vma + input_section->output_offset
333 + reloc_entry->address;
334 relocation -= tmp_addr;
337 if (output_bfd != (bfd *) NULL)
339 /* This is a partial relocation, and we want to apply the relocation
340 to the reloc entry rather than the raw data. Modify the reloc
341 inplace to reflect what we now know. */
342 reloc_entry->addend = relocation;
343 reloc_entry->address += input_section->output_offset;
347 reloc_entry->addend = 0;
349 in1 = bfd_get_32 (abfd, (bfd_byte *) data + addr);
350 in2 = bfd_get_32 (abfd, (bfd_byte *) data + addr + 4);
352 /* extract the addend */
353 num = ((in2 & 0x3FFFF)
354 | ((in2 & 0xFF00000) >> 2)
355 | ((in1 & 0x3F) << 26));
361 if (howto->pc_relative == true && howto->bitsize == 32)
363 /* the D30V has a PC that doesn't wrap and PC-relative jumps */
364 /* are signed, so a PC-relative jump can'tbe more than +/- 2^31 byrtes */
365 /* if one exceeds this, change it to an absolute jump */
366 if (relocation > MAX32)
368 relocation = (relocation + tmp_addr) & 0xffffffff;
371 else if (relocation < MIN32)
373 relocation = (relocation + tmp_addr) & 0xffffffff;
378 in1 |= (relocation >> 26) & 0x3F; /* top 6 bits */
379 in2 |= ((relocation & 0x03FC0000) << 2); /* next 8 bits */
380 in2 |= relocation & 0x0003FFFF; /* bottom 18 bits */
382 /* change a PC-relative instruction to its absolute equivalent */
383 /* with this simple hack */
387 bfd_put_32 (abfd, in1, (bfd_byte *) data + addr);
388 bfd_put_32 (abfd, in2, (bfd_byte *) data + addr + 4);
394 static bfd_reloc_status_type
395 bfd_elf_d30v_reloc_21 (abfd, reloc_entry, symbol, data, input_section, output_bfd, error_message)
397 arelent *reloc_entry;
400 asection *input_section;
402 char **error_message;
406 bfd_reloc_status_type r;
407 asection *reloc_target_output_section;
408 bfd_size_type addr = reloc_entry->address;
409 bfd_reloc_status_type flag = bfd_reloc_ok;
410 bfd_vma output_base = 0;
411 reloc_howto_type *howto = reloc_entry->howto;
414 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
415 input_section, output_bfd, error_message);
416 if (r != bfd_reloc_continue)
419 /* a hacked-up version of bfd_perform_reloc() follows */
420 if (bfd_is_und_section (symbol->section)
421 && (symbol->flags & BSF_WEAK) == 0
422 && output_bfd == (bfd *) NULL)
423 flag = bfd_reloc_undefined;
425 /* Is the address of the relocation really within the section? */
426 if (reloc_entry->address > input_section->_cooked_size)
427 return bfd_reloc_outofrange;
429 /* Work out which section the relocation is targetted at and the
430 initial relocation command value. */
432 /* Get symbol value. (Common symbols are special.) */
433 if (bfd_is_com_section (symbol->section))
436 relocation = symbol->value;
438 reloc_target_output_section = symbol->section->output_section;
440 /* Convert input-section-relative symbol value to absolute. */
444 output_base = reloc_target_output_section->vma;
446 relocation += output_base + symbol->section->output_offset;
448 /* Add in supplied addend. */
449 relocation += reloc_entry->addend;
451 /* Here the variable relocation holds the final address of the
452 symbol we are relocating against, plus any addend. */
454 if (howto->pc_relative == true)
456 relocation -= input_section->output_section->vma + input_section->output_offset;
457 if (howto->pcrel_offset == true)
458 relocation -= reloc_entry->address;
461 if (output_bfd != (bfd *) NULL)
463 /* This is a partial relocation, and we want to apply the relocation
464 to the reloc entry rather than the raw data. Modify the reloc
465 inplace to reflect what we now know. */
466 reloc_entry->addend = relocation;
467 reloc_entry->address += input_section->output_offset;
471 reloc_entry->addend = 0;
473 in1 = bfd_get_32 (abfd, (bfd_byte *) data + addr);
475 mask = (1 << howto->bitsize) - 1;
476 if (howto->bitsize == 6)
478 max = (1 << (howto->bitsize + 2)) - 1;
480 /* extract the addend */
481 num = in1 & mask; /* 18 bits */
482 if (howto->bitsize == 6)
484 num <<= 3; /* shift left 3 */
485 in1 &= ~mask; /* mask out addend */
488 if (howto->type == R_D30V_21_PCREL_R || howto->type == R_D30V_15_PCREL_R ||
489 howto->type == R_D30V_9_PCREL_R )
494 if ((int)relocation < 0 )
496 if (~(int)relocation > max)
497 flag = bfd_reloc_overflow;
501 if ((int)relocation > max)
502 flag = bfd_reloc_overflow;
505 if (howto->bitsize == 6)
506 in1 |= ((relocation & (mask >> 12)) << 12);
508 in1 |= relocation & mask;
510 bfd_put_32 (abfd, in1, (bfd_byte *) data + addr);
515 /* Map BFD reloc types to D30V ELF reloc types. */
517 struct d30v_reloc_map
519 bfd_reloc_code_real_type bfd_reloc_val;
520 unsigned char elf_reloc_val;
524 static const struct d30v_reloc_map d30v_reloc_map[] =
526 { BFD_RELOC_NONE, R_D30V_NONE, },
527 { BFD_RELOC_D30V_6, R_D30V_6 },
528 { BFD_RELOC_D30V_9_PCREL, R_D30V_9_PCREL },
529 { BFD_RELOC_D30V_9_PCREL_R, R_D30V_9_PCREL_R },
530 { BFD_RELOC_D30V_15, R_D30V_15 },
531 { BFD_RELOC_D30V_15_PCREL, R_D30V_15_PCREL },
532 { BFD_RELOC_D30V_15_PCREL_R, R_D30V_15_PCREL_R },
533 { BFD_RELOC_D30V_21, R_D30V_21 },
534 { BFD_RELOC_D30V_21_PCREL, R_D30V_21_PCREL },
535 { BFD_RELOC_D30V_21_PCREL_R, R_D30V_21_PCREL_R },
536 { BFD_RELOC_D30V_32, R_D30V_32 },
537 { BFD_RELOC_D30V_32_PCREL, R_D30V_32_PCREL },
538 { BFD_RELOC_32, R_D30V_32_NORMAL },
541 static reloc_howto_type *
542 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
543 bfd *abfd ATTRIBUTE_UNUSED;
544 bfd_reloc_code_real_type code;
549 i < sizeof (d30v_reloc_map) / sizeof (struct d30v_reloc_map);
552 if (d30v_reloc_map[i].bfd_reloc_val == code)
553 return &elf_d30v_howto_table[d30v_reloc_map[i].elf_reloc_val];
559 /* Set the howto pointer for an D30V ELF reloc (type REL). */
562 d30v_info_to_howto_rel (abfd, cache_ptr, dst)
563 bfd *abfd ATTRIBUTE_UNUSED;
565 Elf32_Internal_Rel *dst;
569 r_type = ELF32_R_TYPE (dst->r_info);
570 BFD_ASSERT (r_type < (unsigned int) R_D30V_max);
571 cache_ptr->howto = &elf_d30v_howto_table[r_type];
574 /* Set the howto pointer for an D30V ELF reloc (type RELA). */
577 d30v_info_to_howto_rela (abfd, cache_ptr, dst)
578 bfd *abfd ATTRIBUTE_UNUSED;
580 Elf32_Internal_Rela *dst;
584 r_type = ELF32_R_TYPE (dst->r_info);
585 BFD_ASSERT (r_type < (unsigned int) R_D30V_max);
586 cache_ptr->howto = &elf_d30v_howto_table[r_type];
589 #define ELF_ARCH bfd_arch_d30v
590 #define ELF_MACHINE_CODE EM_CYGNUS_D30V
591 #define ELF_MAXPAGESIZE 0x1000
593 #define TARGET_BIG_SYM bfd_elf32_d30v_vec
594 #define TARGET_BIG_NAME "elf32-d30v"
596 #define elf_info_to_howto d30v_info_to_howto_rela
597 #define elf_info_to_howto_rel d30v_info_to_howto_rel
598 #define elf_backend_object_p 0
599 #define elf_backend_final_write_processing 0
601 #include "elf32-target.h"