1 /* D30V-specific support for 32-bit ELF
2 Copyright (C) 1997-2023 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
28 #define MAX32 ((bfd_signed_vma) 0x7fffffff)
29 #define MIN32 (- MAX32 - 1)
31 static bfd_reloc_status_type
32 bfd_elf_d30v_reloc (bfd
*abfd
,
36 asection
*input_section
,
40 bfd_signed_vma relocation
;
41 bfd_vma in1
, in2
, num
;
43 bfd_reloc_status_type r
;
44 asection
*reloc_target_output_section
;
45 bfd_size_type addr
= reloc_entry
->address
;
46 bfd_reloc_status_type flag
= bfd_reloc_ok
;
47 bfd_vma output_base
= 0;
48 reloc_howto_type
*howto
= reloc_entry
->howto
;
49 int make_absolute
= 0;
51 if (output_bfd
!= NULL
)
53 /* Partial linking -- do nothing. */
54 reloc_entry
->address
+= input_section
->output_offset
;
58 r
= bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
59 input_section
, output_bfd
, error_message
);
60 if (r
!= bfd_reloc_continue
)
63 /* A hacked-up version of bfd_perform_reloc() follows. */
64 if (bfd_is_und_section (symbol
->section
)
65 && (symbol
->flags
& BSF_WEAK
) == 0
66 && output_bfd
== NULL
)
67 flag
= bfd_reloc_undefined
;
69 /* Is the address of the relocation really within the section? */
70 if (reloc_entry
->address
> bfd_get_section_limit (abfd
, input_section
))
71 return bfd_reloc_outofrange
;
73 /* Work out which section the relocation is targeted at and the
74 initial relocation command value. */
76 /* Get symbol value. (Common symbols are special.) */
77 if (bfd_is_com_section (symbol
->section
))
80 relocation
= symbol
->value
;
82 reloc_target_output_section
= symbol
->section
->output_section
;
84 /* Convert input-section-relative symbol value to absolute. */
85 output_base
= reloc_target_output_section
->vma
;
86 relocation
+= output_base
+ symbol
->section
->output_offset
;
88 /* Add in supplied addend. */
89 relocation
+= reloc_entry
->addend
;
91 /* Here the variable relocation holds the final address of the
92 symbol we are relocating against, plus any addend. */
93 if (howto
->pc_relative
)
95 tmp_addr
= input_section
->output_section
->vma
96 + input_section
->output_offset
97 + reloc_entry
->address
;
98 relocation
-= tmp_addr
;
101 in1
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ addr
);
102 in2
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ addr
+ 4);
104 /* Extract the addend. */
105 num
= ((in2
& 0x3FFFF)
106 | ((in2
& 0xFF00000) >> 2)
107 | ((in1
& 0x3F) << 26));
113 if (howto
->pc_relative
&& howto
->bitsize
== 32)
115 /* The D30V has a PC that doesn't wrap and PC-relative jumps are
116 signed, so a PC-relative jump can't be more than +/- 2^31 bytes.
117 If one exceeds this, change it to an absolute jump. */
118 if (relocation
> MAX32
|| relocation
< MIN32
)
120 relocation
= (relocation
+ tmp_addr
) & 0xffffffff;
125 in1
|= (relocation
>> 26) & 0x3F; /* Top 6 bits. */
126 in2
|= ((relocation
& 0x03FC0000) << 2); /* Next 8 bits. */
127 in2
|= relocation
& 0x0003FFFF; /* Bottom 18 bits. */
129 /* Change a PC-relative instruction to its
130 absolute equivalent with this simple hack. */
134 bfd_put_32 (abfd
, in1
, (bfd_byte
*) data
+ addr
);
135 bfd_put_32 (abfd
, in2
, (bfd_byte
*) data
+ addr
+ 4);
140 static bfd_reloc_status_type
141 bfd_elf_d30v_reloc_21 (bfd
*abfd
,
142 arelent
*reloc_entry
,
145 asection
*input_section
,
147 char **error_message
)
151 bfd_reloc_status_type r
;
152 asection
*reloc_target_output_section
;
153 bfd_size_type addr
= reloc_entry
->address
;
154 bfd_reloc_status_type flag
= bfd_reloc_ok
;
155 bfd_vma output_base
= 0;
156 reloc_howto_type
*howto
= reloc_entry
->howto
;
159 if (output_bfd
!= NULL
)
161 /* Partial linking -- do nothing. */
162 reloc_entry
->address
+= input_section
->output_offset
;
166 r
= bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
167 input_section
, output_bfd
, error_message
);
168 if (r
!= bfd_reloc_continue
)
171 /* A hacked-up version of bfd_perform_reloc() follows. */
172 if (bfd_is_und_section (symbol
->section
)
173 && (symbol
->flags
& BSF_WEAK
) == 0
174 && output_bfd
== NULL
)
175 flag
= bfd_reloc_undefined
;
177 /* Is the address of the relocation really within the section? */
178 if (reloc_entry
->address
> bfd_get_section_limit (abfd
, input_section
))
179 return bfd_reloc_outofrange
;
181 /* Work out which section the relocation is targeted at and the
182 initial relocation command value. */
184 /* Get symbol value. (Common symbols are special.) */
185 if (bfd_is_com_section (symbol
->section
))
188 relocation
= symbol
->value
;
190 reloc_target_output_section
= symbol
->section
->output_section
;
192 /* Convert input-section-relative symbol value to absolute. */
193 output_base
= reloc_target_output_section
->vma
;
194 relocation
+= output_base
+ symbol
->section
->output_offset
;
196 /* Add in supplied addend. */
197 relocation
+= reloc_entry
->addend
;
199 /* Here the variable relocation holds the final address of the
200 symbol we are relocating against, plus any addend. */
202 if (howto
->pc_relative
)
204 relocation
-= (input_section
->output_section
->vma
205 + input_section
->output_offset
);
206 if (howto
->pcrel_offset
)
207 relocation
-= reloc_entry
->address
;
210 in1
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ addr
);
212 mask
= (1 << howto
->bitsize
) - 1;
213 if (howto
->bitsize
== 6)
215 max
= (1 << (howto
->bitsize
+ 2)) - 1;
217 /* Extract the addend. */
218 num
= in1
& mask
; /* 18 bits. */
219 if (howto
->bitsize
== 6)
221 num
<<= 3; /* shift left 3. */
222 in1
&= ~mask
; /* Mask out addend. */
225 if (howto
->type
== R_D30V_21_PCREL_R
226 || howto
->type
== R_D30V_15_PCREL_R
227 || howto
->type
== R_D30V_9_PCREL_R
)
230 if ((int) relocation
< 0)
232 if (~ (int) relocation
> max
)
233 flag
= bfd_reloc_overflow
;
237 if ((int) relocation
> max
)
238 flag
= bfd_reloc_overflow
;
242 if (howto
->bitsize
== 6)
243 in1
|= ((relocation
& (mask
>> 12)) << 12);
245 in1
|= relocation
& mask
;
247 bfd_put_32 (abfd
, in1
, (bfd_byte
*) data
+ addr
);
252 static reloc_howto_type elf_d30v_howto_table
[] =
254 /* This reloc does nothing. */
255 HOWTO (R_D30V_NONE
, /* Type. */
259 false, /* PC_relative. */
261 complain_overflow_dont
, /* Complain_on_overflow. */
262 bfd_elf_generic_reloc
, /* Special_function. */
263 "R_D30V_NONE", /* Name. */
264 false, /* Partial_inplace. */
267 false), /* PCrel_offset. */
269 /* A 6 bit absolute relocation. */
270 HOWTO (R_D30V_6
, /* Type. */
274 false, /* PC_relative. */
276 complain_overflow_bitfield
, /* Complain_on_overflow. */
277 bfd_elf_generic_reloc
, /* Special_function. */
278 "R_D30V_6", /* Name. */
279 false, /* Partial_inplace. */
280 0x3f, /* Src_mask. */
281 0x3f, /* Dst_mask. */
282 false), /* PCrel_offset. */
284 /* A relative 9 bit relocation, right shifted by 3. */
285 HOWTO (R_D30V_9_PCREL
, /* Type. */
289 true, /* PC_relative. */
291 complain_overflow_signed
, /* Complain_on_overflow. */
292 bfd_elf_d30v_reloc_21
, /* Special_function. */
293 "R_D30V_9_PCREL", /* Name. */
294 false, /* Partial_inplace. */
295 0x3f, /* Src_mask. */
296 0x3f, /* Dst_mask. */
297 true), /* PCrel_offset. */
299 /* A relative 9 bit relocation, right shifted by 3. */
300 HOWTO (R_D30V_9_PCREL_R
, /* Type. */
304 true, /* PC_relative. */
306 complain_overflow_signed
, /* Complain_on_overflow. */
307 bfd_elf_d30v_reloc_21
, /* Special_function. */
308 "R_D30V_9_PCREL_R", /* Name. */
309 false, /* Partial_inplace. */
310 0x3f, /* Src_mask. */
311 0x3f, /* Dst_mask. */
312 true), /* PCrel_offset. */
314 /* An absolute 15 bit relocation, right shifted by 3. */
315 HOWTO (R_D30V_15
, /* Type. */
319 false, /* PC_relative. */
321 complain_overflow_signed
, /* Complain_on_overflow. */
322 bfd_elf_generic_reloc
, /* Special_function. */
323 "R_D30V_15", /* Name. */
324 false, /* Partial_inplace. */
325 0xfff, /* Src_mask. */
326 0xfff, /* Dst_mask. */
327 false), /* PCrel_offset. */
329 /* A relative 15 bit relocation, right shifted by 3. */
330 HOWTO (R_D30V_15_PCREL
, /* Type. */
334 true, /* PC_relative. */
336 complain_overflow_signed
, /* Complain_on_overflow. */
337 bfd_elf_d30v_reloc_21
, /* Special_function. */
338 "R_D30V_15_PCREL", /* Name. */
339 false, /* Partial_inplace. */
340 0xfff, /* Src_mask. */
341 0xfff, /* Dst_mask. */
342 true), /* PCrel_offset. */
344 /* A relative 15 bit relocation, right shifted by 3. */
345 HOWTO (R_D30V_15_PCREL_R
, /* Type. */
349 true, /* PC_relative. */
351 complain_overflow_signed
, /* Complain_on_overflow. */
352 bfd_elf_d30v_reloc_21
, /* Special_function. */
353 "R_D30V_15_PCREL_R", /* Name. */
354 false, /* Partial_inplace. */
355 0xfff, /* Src_mask. */
356 0xfff, /* Dst_mask. */
357 true), /* PCrel_offset. */
359 /* An absolute 21 bit relocation, right shifted by 3. */
360 HOWTO (R_D30V_21
, /* Type. */
364 false, /* PC_relative. */
366 complain_overflow_signed
, /* Complain_on_overflow. */
367 bfd_elf_generic_reloc
, /* Special_function. */
368 "R_D30V_21", /* Name. */
369 false, /* Partial_inplace. */
370 0x3ffff, /* Src_mask. */
371 0x3ffff, /* Dst_mask. */
372 false), /* PCrel_offset. */
374 /* A relative 21 bit relocation, right shifted by 3. */
375 HOWTO (R_D30V_21_PCREL
, /* Type. */
379 true, /* PC_relative. */
381 complain_overflow_signed
, /* Complain_on_overflow. */
382 bfd_elf_d30v_reloc_21
, /* Special_function. */
383 "R_D30V_21_PCREL", /* Name. */
384 false, /* Partial_inplace. */
385 0x3ffff, /* Src_mask. */
386 0x3ffff, /* Dst_mask. */
387 true), /* PCrel_offset. */
389 /* A relative 21 bit relocation, right shifted by 3, in the Right container. */
390 HOWTO (R_D30V_21_PCREL_R
, /* Type. */
394 true, /* PC_relative. */
396 complain_overflow_signed
, /* Complain_on_overflow. */
397 bfd_elf_d30v_reloc_21
, /* Special_function. */
398 "R_D30V_21_PCREL_R", /* Name. */
399 false, /* Partial_inplace. */
400 0x3ffff, /* Src_mask. */
401 0x3ffff, /* Dst_mask. */
402 true), /* PCrel_offset. */
404 /* A D30V 32 bit absolute relocation. */
405 HOWTO (R_D30V_32
, /* Type. */
409 false, /* PC_relative. */
411 complain_overflow_bitfield
, /* Complain_on_overflow. */
412 bfd_elf_d30v_reloc
, /* Special_function. */
413 "R_D30V_32", /* Name. */
414 false, /* Partial_inplace. */
415 0xffffffff, /* Src_mask. */
416 0xffffffff, /* Dst_mask. */
417 false), /* PCrel_offset. */
419 /* A relative 32 bit relocation. */
420 HOWTO (R_D30V_32_PCREL
, /* Type. */
424 true, /* PC_relative. */
426 complain_overflow_signed
, /* Complain_on_overflow. */
427 bfd_elf_d30v_reloc
, /* Special_function. */
428 "R_D30V_32_PCREL", /* Name. */
429 false, /* Partial_inplace. */
430 0xffffffff, /* Src_mask. */
431 0xffffffff, /* Dst_mask. */
432 true), /* PCrel_offset. */
434 /* A regular 32 bit absolute relocation. */
435 HOWTO (R_D30V_32_NORMAL
, /* Type. */
439 false, /* PC_relative. */
441 complain_overflow_bitfield
, /* Complain_on_overflow. */
442 bfd_elf_generic_reloc
, /* Special_function. */
443 "R_D30V_32_NORMAL", /* Name. */
444 false, /* Partial_inplace. */
445 0xffffffff, /* Src_mask. */
446 0xffffffff, /* Dst_mask. */
447 false), /* PCrel_offset. */
451 /* Map BFD reloc types to D30V ELF reloc types. */
453 struct d30v_reloc_map
455 bfd_reloc_code_real_type bfd_reloc_val
;
456 unsigned char elf_reloc_val
;
459 static const struct d30v_reloc_map d30v_reloc_map
[] =
461 { BFD_RELOC_NONE
, R_D30V_NONE
, },
462 { BFD_RELOC_D30V_6
, R_D30V_6
},
463 { BFD_RELOC_D30V_9_PCREL
, R_D30V_9_PCREL
},
464 { BFD_RELOC_D30V_9_PCREL_R
, R_D30V_9_PCREL_R
},
465 { BFD_RELOC_D30V_15
, R_D30V_15
},
466 { BFD_RELOC_D30V_15_PCREL
, R_D30V_15_PCREL
},
467 { BFD_RELOC_D30V_15_PCREL_R
, R_D30V_15_PCREL_R
},
468 { BFD_RELOC_D30V_21
, R_D30V_21
},
469 { BFD_RELOC_D30V_21_PCREL
, R_D30V_21_PCREL
},
470 { BFD_RELOC_D30V_21_PCREL_R
, R_D30V_21_PCREL_R
},
471 { BFD_RELOC_D30V_32
, R_D30V_32
},
472 { BFD_RELOC_D30V_32_PCREL
, R_D30V_32_PCREL
},
473 { BFD_RELOC_32
, R_D30V_32_NORMAL
},
476 static reloc_howto_type
*
477 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
478 bfd_reloc_code_real_type code
)
483 i
< sizeof (d30v_reloc_map
) / sizeof (struct d30v_reloc_map
);
486 if (d30v_reloc_map
[i
].bfd_reloc_val
== code
)
487 return &elf_d30v_howto_table
[d30v_reloc_map
[i
].elf_reloc_val
];
493 static reloc_howto_type
*
494 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
500 i
< sizeof (elf_d30v_howto_table
) / sizeof (elf_d30v_howto_table
[0]);
502 if (elf_d30v_howto_table
[i
].name
!= NULL
503 && strcasecmp (elf_d30v_howto_table
[i
].name
, r_name
) == 0)
504 return &elf_d30v_howto_table
[i
];
509 /* Set the howto pointer for an D30V ELF reloc (type REL). */
512 d30v_info_to_howto_rel (bfd
*abfd
,
514 Elf_Internal_Rela
*dst
)
518 r_type
= ELF32_R_TYPE (dst
->r_info
);
519 if (r_type
>= (unsigned int) R_D30V_max
)
521 /* xgettext:c-format */
522 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
524 bfd_set_error (bfd_error_bad_value
);
527 cache_ptr
->howto
= &elf_d30v_howto_table
[r_type
];
531 /* Set the howto pointer for an D30V ELF reloc (type RELA). */
534 d30v_info_to_howto_rela (bfd
*abfd
,
536 Elf_Internal_Rela
*dst
)
540 r_type
= ELF32_R_TYPE (dst
->r_info
);
541 if (r_type
>= (unsigned int) R_D30V_max
)
543 /* xgettext:c-format */
544 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
546 bfd_set_error (bfd_error_bad_value
);
549 cache_ptr
->howto
= &elf_d30v_howto_table
[r_type
];
553 #define ELF_ARCH bfd_arch_d30v
554 #define ELF_MACHINE_CODE EM_D30V
555 #define ELF_MACHINE_ALT1 EM_CYGNUS_D30V
556 #define ELF_MAXPAGESIZE 0x1000
558 #define TARGET_BIG_SYM d30v_elf32_vec
559 #define TARGET_BIG_NAME "elf32-d30v"
561 #define elf_info_to_howto d30v_info_to_howto_rela
562 #define elf_info_to_howto_rel d30v_info_to_howto_rel
563 #define elf_backend_object_p 0
565 #include "elf32-target.h"