1 /* D10V-specific support for 32-bit ELF
2 Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Contributed by Martin Hunt (hunt@cygnus.com).
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
29 /* Use REL instead of RELA to save space. */
32 static reloc_howto_type elf_d10v_howto_table
[] =
34 /* This reloc does nothing. */
35 HOWTO (R_D10V_NONE
, /* Type. */
37 2, /* Size (0 = byte, 1 = short, 2 = long). */
39 FALSE
, /* PC_relative. */
41 complain_overflow_dont
,/* Complain_on_overflow. */
42 bfd_elf_generic_reloc
, /* Special_function. */
43 "R_D10V_NONE", /* Name. */
44 FALSE
, /* Partial_inplace. */
47 FALSE
), /* PCrel_offset. */
49 /* An PC Relative 10-bit relocation, shifted by 2, right container. */
50 HOWTO (R_D10V_10_PCREL_R
, /* Type. */
52 2, /* Size (0 = byte, 1 = short, 2 = long). */
54 TRUE
, /* PC_relative. */
56 complain_overflow_signed
, /* Complain_on_overflow. */
57 bfd_elf_generic_reloc
, /* Special_function. */
58 "R_D10V_10_PCREL_R", /* Name. */
59 FALSE
, /* Partial_inplace. */
62 TRUE
), /* PCrel_offset. */
64 /* An PC Relative 10-bit relocation, shifted by 2, left container. */
65 HOWTO (R_D10V_10_PCREL_L
, /* Type. */
67 2, /* Size (0 = byte, 1 = short, 2 = long). */
69 TRUE
, /* PC_relative. */
71 complain_overflow_signed
, /* Complain_on_overflow. */
72 bfd_elf_generic_reloc
, /* Special_function. */
73 "R_D10V_10_PCREL_L", /* Name. */
74 FALSE
, /* Partial_inplace. */
75 0x07f8000, /* Src_mask. */
76 0x07f8000, /* Dst_mask. */
77 TRUE
), /* PCrel_offset. */
79 /* A 16 bit absolute relocation. */
80 HOWTO (R_D10V_16
, /* Type. */
82 1, /* Size (0 = byte, 1 = short, 2 = long). */
84 FALSE
, /* PC_relative. */
86 complain_overflow_dont
,/* Complain_on_overflow. */
87 bfd_elf_generic_reloc
, /* Special_function. */
88 "R_D10V_16", /* Name. */
89 FALSE
, /* Partial_inplace. */
90 0xffff, /* Src_mask. */
91 0xffff, /* Dst_mask. */
92 FALSE
), /* PCrel_offset. */
94 /* An 18 bit absolute relocation, right shifted 2. */
95 HOWTO (R_D10V_18
, /* Type. */
97 1, /* Size (0 = byte, 1 = short, 2 = long). */
99 FALSE
, /* PC_relative. */
101 complain_overflow_dont
, /* Complain_on_overflow. */
102 bfd_elf_generic_reloc
, /* Special_function. */
103 "R_D10V_18", /* Name. */
104 FALSE
, /* Partial_inplace. */
105 0xffff, /* Src_mask. */
106 0xffff, /* Dst_mask. */
107 FALSE
), /* PCrel_offset. */
109 /* A relative 18 bit relocation, right shifted by 2. */
110 HOWTO (R_D10V_18_PCREL
, /* Type. */
112 2, /* Size (0 = byte, 1 = short, 2 = long). */
114 TRUE
, /* PC_relative. */
116 complain_overflow_signed
, /* Complain_on_overflow. */
117 bfd_elf_generic_reloc
, /* Special_function. */
118 "R_D10V_18_PCREL", /* Name. */
119 FALSE
, /* Partial_inplace. */
120 0xffff, /* Src_mask. */
121 0xffff, /* Dst_mask. */
122 TRUE
), /* PCrel_offset. */
124 /* A 32 bit absolute relocation. */
125 HOWTO (R_D10V_32
, /* Type. */
127 2, /* Size (0 = byte, 1 = short, 2 = long). */
129 FALSE
, /* PC_relative. */
131 complain_overflow_dont
,/* Complain_on_overflow. */
132 bfd_elf_generic_reloc
, /* Special_function. */
133 "R_D10V_32", /* Name. */
134 FALSE
, /* Partial_inplace. */
135 0xffffffff, /* Src_mask. */
136 0xffffffff, /* Dst_mask. */
137 FALSE
), /* PCrel_offset. */
139 /* GNU extension to record C++ vtable hierarchy. */
140 HOWTO (R_D10V_GNU_VTINHERIT
, /* Type. */
142 2, /* Size (0 = byte, 1 = short, 2 = long). */
144 FALSE
, /* PC_relative. */
146 complain_overflow_dont
,/* Complain_on_overflow. */
147 NULL
, /* Special_function. */
148 "R_D10V_GNU_VTINHERIT",/* Name. */
149 FALSE
, /* Partial_inplace. */
152 FALSE
), /* PCrel_offset. */
154 /* GNU extension to record C++ vtable member usage. */
155 HOWTO (R_D10V_GNU_VTENTRY
, /* Type. */
157 2, /* Size (0 = byte, 1 = short, 2 = long). */
159 FALSE
, /* PC_relative. */
161 complain_overflow_dont
,/* Complain_on_overflow. */
162 _bfd_elf_rel_vtable_reloc_fn
, /* Special_function. */
163 "R_D10V_GNU_VTENTRY", /* Name. */
164 FALSE
, /* Partial_inplace. */
167 FALSE
), /* PCrel_offset. */
170 /* Map BFD reloc types to D10V ELF reloc types. */
172 struct d10v_reloc_map
174 bfd_reloc_code_real_type bfd_reloc_val
;
175 unsigned char elf_reloc_val
;
178 static const struct d10v_reloc_map d10v_reloc_map
[] =
180 { BFD_RELOC_NONE
, R_D10V_NONE
, },
181 { BFD_RELOC_D10V_10_PCREL_R
, R_D10V_10_PCREL_R
},
182 { BFD_RELOC_D10V_10_PCREL_L
, R_D10V_10_PCREL_L
},
183 { BFD_RELOC_16
, R_D10V_16
},
184 { BFD_RELOC_D10V_18
, R_D10V_18
},
185 { BFD_RELOC_D10V_18_PCREL
, R_D10V_18_PCREL
},
186 { BFD_RELOC_32
, R_D10V_32
},
187 { BFD_RELOC_VTABLE_INHERIT
, R_D10V_GNU_VTINHERIT
},
188 { BFD_RELOC_VTABLE_ENTRY
, R_D10V_GNU_VTENTRY
},
191 static reloc_howto_type
*
192 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
193 bfd_reloc_code_real_type code
)
198 i
< sizeof (d10v_reloc_map
) / sizeof (struct d10v_reloc_map
);
200 if (d10v_reloc_map
[i
].bfd_reloc_val
== code
)
201 return &elf_d10v_howto_table
[d10v_reloc_map
[i
].elf_reloc_val
];
206 /* Set the howto pointer for an D10V ELF reloc. */
209 d10v_info_to_howto_rel (bfd
*abfd ATTRIBUTE_UNUSED
,
211 Elf_Internal_Rela
*dst
)
215 r_type
= ELF32_R_TYPE (dst
->r_info
);
216 BFD_ASSERT (r_type
< (unsigned int) R_D10V_max
);
217 cache_ptr
->howto
= &elf_d10v_howto_table
[r_type
];
221 elf32_d10v_gc_mark_hook (asection
*sec
,
222 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
223 Elf_Internal_Rela
*rel
,
224 struct elf_link_hash_entry
*h
,
225 Elf_Internal_Sym
*sym
)
229 switch (ELF32_R_TYPE (rel
->r_info
))
231 case R_D10V_GNU_VTINHERIT
:
232 case R_D10V_GNU_VTENTRY
:
236 switch (h
->root
.type
)
238 case bfd_link_hash_defined
:
239 case bfd_link_hash_defweak
:
240 return h
->root
.u
.def
.section
;
242 case bfd_link_hash_common
:
243 return h
->root
.u
.c
.p
->section
;
251 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
257 elf32_d10v_gc_sweep_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
258 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
259 asection
*sec ATTRIBUTE_UNUSED
,
260 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
)
262 /* We don't use got and plt entries for d10v. */
266 /* Look through the relocs for a section during the first phase.
267 Since we don't do .gots or .plts, we just need to consider the
268 virtual table relocs for gc. */
271 elf32_d10v_check_relocs (bfd
*abfd
,
272 struct bfd_link_info
*info
,
274 const Elf_Internal_Rela
*relocs
)
276 Elf_Internal_Shdr
*symtab_hdr
;
277 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
278 const Elf_Internal_Rela
*rel
;
279 const Elf_Internal_Rela
*rel_end
;
281 if (info
->relocatable
)
284 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
285 sym_hashes
= elf_sym_hashes (abfd
);
286 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
287 if (!elf_bad_symtab (abfd
))
288 sym_hashes_end
-= symtab_hdr
->sh_info
;
290 rel_end
= relocs
+ sec
->reloc_count
;
291 for (rel
= relocs
; rel
< rel_end
; rel
++)
293 struct elf_link_hash_entry
*h
;
294 unsigned long r_symndx
;
296 r_symndx
= ELF32_R_SYM (rel
->r_info
);
297 if (r_symndx
< symtab_hdr
->sh_info
)
301 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
302 while (h
->root
.type
== bfd_link_hash_indirect
303 || h
->root
.type
== bfd_link_hash_warning
)
304 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
307 switch (ELF32_R_TYPE (rel
->r_info
))
309 /* This relocation describes the C++ object vtable hierarchy.
310 Reconstruct it for later use during GC. */
311 case R_D10V_GNU_VTINHERIT
:
312 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
316 /* This relocation describes which C++ vtable entries are actually
317 used. Record for later use during GC. */
318 case R_D10V_GNU_VTENTRY
:
319 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_offset
))
329 extract_rel_addend (bfd
*abfd
,
331 reloc_howto_type
*howto
)
338 insn
= bfd_get_8 (abfd
, where
);
341 insn
= bfd_get_16 (abfd
, where
);
344 insn
= bfd_get_32 (abfd
, where
);
350 val
= (insn
& howto
->dst_mask
) >> howto
->bitpos
<< howto
->rightshift
;
351 /* We should really be testing for signed addends here, but we don't
352 have that info directly in the howto. */
353 if (howto
->pc_relative
)
356 sign
= howto
->dst_mask
& (~howto
->dst_mask
>> 1 | ~(-(bfd_vma
) 1 >> 1));
357 sign
= sign
>> howto
->bitpos
<< howto
->rightshift
;
358 val
= (val
^ sign
) - sign
;
364 insert_rel_addend (bfd
*abfd
,
366 reloc_howto_type
*howto
,
371 addend
= (addend
>> howto
->rightshift
<< howto
->bitpos
) & howto
->dst_mask
;
372 insn
= ~howto
->dst_mask
;
376 insn
&= bfd_get_8 (abfd
, where
);
378 bfd_put_8 (abfd
, insn
, where
);
381 insn
&= bfd_get_16 (abfd
, where
);
383 bfd_put_16 (abfd
, insn
, where
);
386 insn
&= bfd_get_32 (abfd
, where
);
388 bfd_put_32 (abfd
, insn
, where
);
395 /* Relocate a D10V ELF section. */
398 elf32_d10v_relocate_section (bfd
*output_bfd
,
399 struct bfd_link_info
*info
,
401 asection
*input_section
,
403 Elf_Internal_Rela
*relocs
,
404 Elf_Internal_Sym
*local_syms
,
405 asection
**local_sections
)
407 Elf_Internal_Shdr
*symtab_hdr
;
408 struct elf_link_hash_entry
**sym_hashes
;
409 Elf_Internal_Rela
*rel
, *relend
;
412 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
413 sym_hashes
= elf_sym_hashes (input_bfd
);
416 relend
= relocs
+ input_section
->reloc_count
;
417 for (; rel
< relend
; rel
++)
420 reloc_howto_type
*howto
;
421 unsigned long r_symndx
;
422 Elf_Internal_Sym
*sym
;
424 struct elf_link_hash_entry
*h
;
426 bfd_reloc_status_type r
;
428 r_symndx
= ELF32_R_SYM (rel
->r_info
);
429 r_type
= ELF32_R_TYPE (rel
->r_info
);
431 if (r_type
== R_D10V_GNU_VTENTRY
432 || r_type
== R_D10V_GNU_VTINHERIT
)
435 howto
= elf_d10v_howto_table
+ r_type
;
437 if (info
->relocatable
)
442 /* This is a relocatable link. We don't have to change
443 anything, unless the reloc is against a section symbol,
444 in which case we have to adjust according to where the
445 section symbol winds up in the output section. */
446 if (r_symndx
>= symtab_hdr
->sh_info
)
449 sym
= local_syms
+ r_symndx
;
450 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
453 sec
= local_sections
[r_symndx
];
454 val
= sec
->output_offset
;
458 where
= contents
+ rel
->r_offset
;
459 val
+= extract_rel_addend (input_bfd
, where
, howto
);
460 insert_rel_addend (input_bfd
, where
, howto
, val
);
464 /* This is a final link. */
468 if (r_symndx
< symtab_hdr
->sh_info
)
470 sym
= local_syms
+ r_symndx
;
471 sec
= local_sections
[r_symndx
];
472 relocation
= (sec
->output_section
->vma
475 if ((sec
->flags
& SEC_MERGE
)
476 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
480 bfd_byte
*where
= contents
+ rel
->r_offset
;
482 addend
= extract_rel_addend (input_bfd
, where
, howto
);
484 addend
= _bfd_elf_rel_local_sym (output_bfd
, sym
, &msec
, addend
);
485 addend
-= relocation
;
486 addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
487 insert_rel_addend (input_bfd
, where
, howto
, addend
);
492 bfd_boolean unresolved_reloc
, warned
;
494 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
495 r_symndx
, symtab_hdr
, sym_hashes
,
497 unresolved_reloc
, warned
);
501 name
= h
->root
.root
.string
;
504 name
= (bfd_elf_string_from_elf_section
505 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
506 if (name
== NULL
|| *name
== '\0')
507 name
= bfd_section_name (input_bfd
, sec
);
510 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
511 contents
, rel
->r_offset
,
512 relocation
, (bfd_vma
) 0);
514 if (r
!= bfd_reloc_ok
)
516 const char * msg
= (const char *) 0;
520 case bfd_reloc_overflow
:
521 if (!((*info
->callbacks
->reloc_overflow
)
522 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
523 (bfd_vma
) 0, input_bfd
, input_section
,
528 case bfd_reloc_undefined
:
529 if (!((*info
->callbacks
->undefined_symbol
)
530 (info
, name
, input_bfd
, input_section
,
531 rel
->r_offset
, TRUE
)))
535 case bfd_reloc_outofrange
:
536 msg
= _("internal error: out of range error");
539 case bfd_reloc_notsupported
:
540 msg
= _("internal error: unsupported relocation error");
543 case bfd_reloc_dangerous
:
544 msg
= _("internal error: dangerous error");
548 msg
= _("internal error: unknown error");
552 if (!((*info
->callbacks
->warning
)
553 (info
, msg
, name
, input_bfd
, input_section
,
563 #define ELF_ARCH bfd_arch_d10v
564 #define ELF_MACHINE_CODE EM_D10V
565 #define ELF_MACHINE_ALT1 EM_CYGNUS_D10V
566 #define ELF_MAXPAGESIZE 0x1000
568 #define TARGET_BIG_SYM bfd_elf32_d10v_vec
569 #define TARGET_BIG_NAME "elf32-d10v"
571 #define elf_info_to_howto 0
572 #define elf_info_to_howto_rel d10v_info_to_howto_rel
573 #define elf_backend_object_p 0
574 #define elf_backend_final_write_processing 0
575 #define elf_backend_gc_mark_hook elf32_d10v_gc_mark_hook
576 #define elf_backend_gc_sweep_hook elf32_d10v_gc_sweep_hook
577 #define elf_backend_check_relocs elf32_d10v_check_relocs
578 #define elf_backend_relocate_section elf32_d10v_relocate_section
579 #define elf_backend_can_gc_sections 1
581 #include "elf32-target.h"