1 /* D10V-specific support for 32-bit ELF
2 Copyright (C) 1996-2014 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 /* Use REL instead of RELA to save space. */
31 static reloc_howto_type elf_d10v_howto_table[] =
33 /* This reloc does nothing. */
34 HOWTO (R_D10V_NONE, /* Type. */
36 2, /* Size (0 = byte, 1 = short, 2 = long). */
38 FALSE, /* PC_relative. */
40 complain_overflow_dont,/* Complain_on_overflow. */
41 bfd_elf_generic_reloc, /* Special_function. */
42 "R_D10V_NONE", /* Name. */
43 FALSE, /* Partial_inplace. */
46 FALSE), /* PCrel_offset. */
48 /* An PC Relative 10-bit relocation, shifted by 2, right container. */
49 HOWTO (R_D10V_10_PCREL_R, /* Type. */
51 2, /* Size (0 = byte, 1 = short, 2 = long). */
53 TRUE, /* PC_relative. */
55 complain_overflow_signed, /* Complain_on_overflow. */
56 bfd_elf_generic_reloc, /* Special_function. */
57 "R_D10V_10_PCREL_R", /* Name. */
58 FALSE, /* Partial_inplace. */
61 TRUE), /* PCrel_offset. */
63 /* An PC Relative 10-bit relocation, shifted by 2, left container. */
64 HOWTO (R_D10V_10_PCREL_L, /* Type. */
66 2, /* Size (0 = byte, 1 = short, 2 = long). */
68 TRUE, /* PC_relative. */
70 complain_overflow_signed, /* Complain_on_overflow. */
71 bfd_elf_generic_reloc, /* Special_function. */
72 "R_D10V_10_PCREL_L", /* Name. */
73 FALSE, /* Partial_inplace. */
74 0x07f8000, /* Src_mask. */
75 0x07f8000, /* Dst_mask. */
76 TRUE), /* PCrel_offset. */
78 /* A 16 bit absolute relocation. */
79 HOWTO (R_D10V_16, /* Type. */
81 1, /* Size (0 = byte, 1 = short, 2 = long). */
83 FALSE, /* PC_relative. */
85 complain_overflow_dont,/* Complain_on_overflow. */
86 bfd_elf_generic_reloc, /* Special_function. */
87 "R_D10V_16", /* Name. */
88 FALSE, /* Partial_inplace. */
89 0xffff, /* Src_mask. */
90 0xffff, /* Dst_mask. */
91 FALSE), /* PCrel_offset. */
93 /* An 18 bit absolute relocation, right shifted 2. */
94 HOWTO (R_D10V_18, /* Type. */
96 1, /* Size (0 = byte, 1 = short, 2 = long). */
98 FALSE, /* PC_relative. */
100 complain_overflow_dont, /* Complain_on_overflow. */
101 bfd_elf_generic_reloc, /* Special_function. */
102 "R_D10V_18", /* Name. */
103 FALSE, /* Partial_inplace. */
104 0xffff, /* Src_mask. */
105 0xffff, /* Dst_mask. */
106 FALSE), /* PCrel_offset. */
108 /* A relative 18 bit relocation, right shifted by 2. */
109 HOWTO (R_D10V_18_PCREL, /* Type. */
111 2, /* Size (0 = byte, 1 = short, 2 = long). */
113 TRUE, /* PC_relative. */
115 complain_overflow_signed, /* Complain_on_overflow. */
116 bfd_elf_generic_reloc, /* Special_function. */
117 "R_D10V_18_PCREL", /* Name. */
118 FALSE, /* Partial_inplace. */
119 0xffff, /* Src_mask. */
120 0xffff, /* Dst_mask. */
121 TRUE), /* PCrel_offset. */
123 /* A 32 bit absolute relocation. */
124 HOWTO (R_D10V_32, /* Type. */
126 2, /* Size (0 = byte, 1 = short, 2 = long). */
128 FALSE, /* PC_relative. */
130 complain_overflow_dont,/* Complain_on_overflow. */
131 bfd_elf_generic_reloc, /* Special_function. */
132 "R_D10V_32", /* Name. */
133 FALSE, /* Partial_inplace. */
134 0xffffffff, /* Src_mask. */
135 0xffffffff, /* Dst_mask. */
136 FALSE), /* PCrel_offset. */
138 /* GNU extension to record C++ vtable hierarchy. */
139 HOWTO (R_D10V_GNU_VTINHERIT, /* Type. */
141 2, /* Size (0 = byte, 1 = short, 2 = long). */
143 FALSE, /* PC_relative. */
145 complain_overflow_dont,/* Complain_on_overflow. */
146 NULL, /* Special_function. */
147 "R_D10V_GNU_VTINHERIT",/* Name. */
148 FALSE, /* Partial_inplace. */
151 FALSE), /* PCrel_offset. */
153 /* GNU extension to record C++ vtable member usage. */
154 HOWTO (R_D10V_GNU_VTENTRY, /* Type. */
156 2, /* Size (0 = byte, 1 = short, 2 = long). */
158 FALSE, /* PC_relative. */
160 complain_overflow_dont,/* Complain_on_overflow. */
161 _bfd_elf_rel_vtable_reloc_fn, /* Special_function. */
162 "R_D10V_GNU_VTENTRY", /* Name. */
163 FALSE, /* Partial_inplace. */
166 FALSE), /* PCrel_offset. */
169 /* Map BFD reloc types to D10V ELF reloc types. */
171 struct d10v_reloc_map
173 bfd_reloc_code_real_type bfd_reloc_val;
174 unsigned char elf_reloc_val;
177 static const struct d10v_reloc_map d10v_reloc_map[] =
179 { BFD_RELOC_NONE, R_D10V_NONE, },
180 { BFD_RELOC_D10V_10_PCREL_R, R_D10V_10_PCREL_R },
181 { BFD_RELOC_D10V_10_PCREL_L, R_D10V_10_PCREL_L },
182 { BFD_RELOC_16, R_D10V_16 },
183 { BFD_RELOC_D10V_18, R_D10V_18 },
184 { BFD_RELOC_D10V_18_PCREL, R_D10V_18_PCREL },
185 { BFD_RELOC_32, R_D10V_32 },
186 { BFD_RELOC_VTABLE_INHERIT, R_D10V_GNU_VTINHERIT },
187 { BFD_RELOC_VTABLE_ENTRY, R_D10V_GNU_VTENTRY },
190 static reloc_howto_type *
191 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
192 bfd_reloc_code_real_type code)
197 i < sizeof (d10v_reloc_map) / sizeof (struct d10v_reloc_map);
199 if (d10v_reloc_map[i].bfd_reloc_val == code)
200 return &elf_d10v_howto_table[d10v_reloc_map[i].elf_reloc_val];
205 static reloc_howto_type *
206 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
212 i < sizeof (elf_d10v_howto_table) / sizeof (elf_d10v_howto_table[0]);
214 if (elf_d10v_howto_table[i].name != NULL
215 && strcasecmp (elf_d10v_howto_table[i].name, r_name) == 0)
216 return &elf_d10v_howto_table[i];
221 /* Set the howto pointer for an D10V ELF reloc. */
224 d10v_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
226 Elf_Internal_Rela *dst)
230 r_type = ELF32_R_TYPE (dst->r_info);
231 BFD_ASSERT (r_type < (unsigned int) R_D10V_max);
232 cache_ptr->howto = &elf_d10v_howto_table[r_type];
236 elf32_d10v_gc_mark_hook (asection *sec,
237 struct bfd_link_info *info,
238 Elf_Internal_Rela *rel,
239 struct elf_link_hash_entry *h,
240 Elf_Internal_Sym *sym)
243 switch (ELF32_R_TYPE (rel->r_info))
245 case R_D10V_GNU_VTINHERIT:
246 case R_D10V_GNU_VTENTRY:
250 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
253 /* Look through the relocs for a section during the first phase.
254 Since we don't do .gots or .plts, we just need to consider the
255 virtual table relocs for gc. */
258 elf32_d10v_check_relocs (bfd *abfd,
259 struct bfd_link_info *info,
261 const Elf_Internal_Rela *relocs)
263 Elf_Internal_Shdr *symtab_hdr;
264 struct elf_link_hash_entry **sym_hashes;
265 const Elf_Internal_Rela *rel;
266 const Elf_Internal_Rela *rel_end;
268 if (info->relocatable)
271 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
272 sym_hashes = elf_sym_hashes (abfd);
274 rel_end = relocs + sec->reloc_count;
275 for (rel = relocs; rel < rel_end; rel++)
277 struct elf_link_hash_entry *h;
278 unsigned long r_symndx;
280 r_symndx = ELF32_R_SYM (rel->r_info);
281 if (r_symndx < symtab_hdr->sh_info)
285 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
286 while (h->root.type == bfd_link_hash_indirect
287 || h->root.type == bfd_link_hash_warning)
288 h = (struct elf_link_hash_entry *) h->root.u.i.link;
290 /* PR15323, ref flags aren't set for references in the same
292 h->root.non_ir_ref = 1;
295 switch (ELF32_R_TYPE (rel->r_info))
297 /* This relocation describes the C++ object vtable hierarchy.
298 Reconstruct it for later use during GC. */
299 case R_D10V_GNU_VTINHERIT:
300 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
304 /* This relocation describes which C++ vtable entries are actually
305 used. Record for later use during GC. */
306 case R_D10V_GNU_VTENTRY:
307 BFD_ASSERT (h != NULL);
309 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
319 extract_rel_addend (bfd *abfd,
321 reloc_howto_type *howto)
328 insn = bfd_get_8 (abfd, where);
331 insn = bfd_get_16 (abfd, where);
334 insn = bfd_get_32 (abfd, where);
340 val = (insn & howto->dst_mask) >> howto->bitpos << howto->rightshift;
341 /* We should really be testing for signed addends here, but we don't
342 have that info directly in the howto. */
343 if (howto->pc_relative)
346 sign = howto->dst_mask & (~howto->dst_mask >> 1 | ~(-(bfd_vma) 1 >> 1));
347 sign = sign >> howto->bitpos << howto->rightshift;
348 val = (val ^ sign) - sign;
354 insert_rel_addend (bfd *abfd,
356 reloc_howto_type *howto,
361 addend = (addend >> howto->rightshift << howto->bitpos) & howto->dst_mask;
362 insn = ~howto->dst_mask;
366 insn &= bfd_get_8 (abfd, where);
368 bfd_put_8 (abfd, insn, where);
371 insn &= bfd_get_16 (abfd, where);
373 bfd_put_16 (abfd, insn, where);
376 insn &= bfd_get_32 (abfd, where);
378 bfd_put_32 (abfd, insn, where);
385 /* Relocate a D10V ELF section. */
388 elf32_d10v_relocate_section (bfd *output_bfd,
389 struct bfd_link_info *info,
391 asection *input_section,
393 Elf_Internal_Rela *relocs,
394 Elf_Internal_Sym *local_syms,
395 asection **local_sections)
397 Elf_Internal_Shdr *symtab_hdr;
398 struct elf_link_hash_entry **sym_hashes;
399 Elf_Internal_Rela *rel, *relend;
402 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
403 sym_hashes = elf_sym_hashes (input_bfd);
406 relend = relocs + input_section->reloc_count;
407 for (; rel < relend; rel++)
410 reloc_howto_type *howto;
411 unsigned long r_symndx;
412 Elf_Internal_Sym *sym;
414 struct elf_link_hash_entry *h;
416 bfd_reloc_status_type r;
418 r_symndx = ELF32_R_SYM (rel->r_info);
419 r_type = ELF32_R_TYPE (rel->r_info);
421 if (r_type == R_D10V_GNU_VTENTRY
422 || r_type == R_D10V_GNU_VTINHERIT)
425 howto = elf_d10v_howto_table + r_type;
429 if (r_symndx < symtab_hdr->sh_info)
431 sym = local_syms + r_symndx;
432 sec = local_sections[r_symndx];
433 relocation = (sec->output_section->vma
436 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
437 && ((sec->flags & SEC_MERGE) != 0
438 || (info->relocatable
439 && sec->output_offset != 0)))
442 bfd_byte *where = contents + rel->r_offset;
444 addend = extract_rel_addend (input_bfd, where, howto);
446 if (info->relocatable)
447 addend += sec->output_offset;
450 asection *msec = sec;
451 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
453 addend -= relocation;
454 addend += msec->output_section->vma + msec->output_offset;
456 insert_rel_addend (input_bfd, where, howto, addend);
461 bfd_boolean unresolved_reloc, warned, ignored;
463 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
464 r_symndx, symtab_hdr, sym_hashes,
466 unresolved_reloc, warned, ignored);
469 if (sec != NULL && discarded_section (sec))
470 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
471 rel, 1, relend, howto, 0, contents);
473 if (info->relocatable)
477 name = h->root.root.string;
480 name = (bfd_elf_string_from_elf_section
481 (input_bfd, symtab_hdr->sh_link, sym->st_name));
482 if (name == NULL || *name == '\0')
483 name = bfd_section_name (input_bfd, sec);
486 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
487 contents, rel->r_offset,
488 relocation, (bfd_vma) 0);
490 if (r != bfd_reloc_ok)
492 const char * msg = (const char *) 0;
496 case bfd_reloc_overflow:
497 if (!((*info->callbacks->reloc_overflow)
498 (info, (h ? &h->root : NULL), name, howto->name,
499 (bfd_vma) 0, input_bfd, input_section,
504 case bfd_reloc_undefined:
505 if (!((*info->callbacks->undefined_symbol)
506 (info, name, input_bfd, input_section,
507 rel->r_offset, TRUE)))
511 case bfd_reloc_outofrange:
512 msg = _("internal error: out of range error");
515 case bfd_reloc_notsupported:
516 msg = _("internal error: unsupported relocation error");
519 case bfd_reloc_dangerous:
520 msg = _("internal error: dangerous error");
524 msg = _("internal error: unknown error");
528 if (!((*info->callbacks->warning)
529 (info, msg, name, input_bfd, input_section,
539 #define ELF_ARCH bfd_arch_d10v
540 #define ELF_MACHINE_CODE EM_D10V
541 #define ELF_MACHINE_ALT1 EM_CYGNUS_D10V
542 #define ELF_MAXPAGESIZE 0x1000
544 #define TARGET_BIG_SYM d10v_elf32_vec
545 #define TARGET_BIG_NAME "elf32-d10v"
547 #define elf_info_to_howto 0
548 #define elf_info_to_howto_rel d10v_info_to_howto_rel
549 #define elf_backend_object_p 0
550 #define elf_backend_final_write_processing 0
551 #define elf_backend_gc_mark_hook elf32_d10v_gc_mark_hook
552 #define elf_backend_check_relocs elf32_d10v_check_relocs
553 #define elf_backend_relocate_section elf32_d10v_relocate_section
554 #define elf_backend_can_gc_sections 1
556 #include "elf32-target.h"