bfd/
[deliverable/binutils-gdb.git] / bfd / elf32-d10v.c
CommitLineData
252b5132 1/* D10V-specific support for 32-bit ELF
ab96bf03
AM
2 Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by Martin Hunt (hunt@cygnus.com).
5
47b0e7ad 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
47b0e7ad
NC
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
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or
47b0e7ad 11 (at your option) any later version.
252b5132 12
47b0e7ad
NC
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.
252b5132 17
47b0e7ad
NC
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. */
252b5132 22
252b5132 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
252b5132
RH
25#include "libbfd.h"
26#include "elf-bfd.h"
1b452ec6 27#include "elf/d10v.h"
252b5132 28
917583ad 29/* Use REL instead of RELA to save space. */
acf8aed4 30#define USE_REL 1
252b5132 31
252b5132 32static reloc_howto_type elf_d10v_howto_table[] =
47b0e7ad
NC
33{
34 /* This reloc does nothing. */
35 HOWTO (R_D10V_NONE, /* Type. */
36 0, /* Rightshift. */
37 2, /* Size (0 = byte, 1 = short, 2 = long). */
38 32, /* Bitsize. */
39 FALSE, /* PC_relative. */
40 0, /* Bitpos. */
41 complain_overflow_dont,/* Complain_on_overflow. */
42 bfd_elf_generic_reloc, /* Special_function. */
43 "R_D10V_NONE", /* Name. */
44 FALSE, /* Partial_inplace. */
45 0, /* Src_mask. */
46 0, /* Dst_mask. */
47 FALSE), /* PCrel_offset. */
48
49 /* An PC Relative 10-bit relocation, shifted by 2, right container. */
50 HOWTO (R_D10V_10_PCREL_R, /* Type. */
51 2, /* Rightshift. */
52 2, /* Size (0 = byte, 1 = short, 2 = long). */
a7985d73 53 8, /* Bitsize. */
47b0e7ad
NC
54 TRUE, /* PC_relative. */
55 0, /* Bitpos. */
a7985d73 56 complain_overflow_signed, /* Complain_on_overflow. */
47b0e7ad
NC
57 bfd_elf_generic_reloc, /* Special_function. */
58 "R_D10V_10_PCREL_R", /* Name. */
59 FALSE, /* Partial_inplace. */
60 0xff, /* Src_mask. */
61 0xff, /* Dst_mask. */
62 TRUE), /* PCrel_offset. */
63
64 /* An PC Relative 10-bit relocation, shifted by 2, left container. */
65 HOWTO (R_D10V_10_PCREL_L, /* Type. */
66 2, /* Rightshift. */
67 2, /* Size (0 = byte, 1 = short, 2 = long). */
a7985d73 68 8, /* Bitsize. */
47b0e7ad
NC
69 TRUE, /* PC_relative. */
70 15, /* Bitpos. */
a7985d73 71 complain_overflow_signed, /* Complain_on_overflow. */
47b0e7ad
NC
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. */
78
79 /* A 16 bit absolute relocation. */
80 HOWTO (R_D10V_16, /* Type. */
81 0, /* Rightshift. */
82 1, /* Size (0 = byte, 1 = short, 2 = long). */
83 16, /* Bitsize. */
84 FALSE, /* PC_relative. */
85 0, /* Bitpos. */
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. */
93
94 /* An 18 bit absolute relocation, right shifted 2. */
95 HOWTO (R_D10V_18, /* Type. */
96 2, /* Rightshift. */
97 1, /* Size (0 = byte, 1 = short, 2 = long). */
98 16, /* Bitsize. */
99 FALSE, /* PC_relative. */
100 0, /* Bitpos. */
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. */
108
109 /* A relative 18 bit relocation, right shifted by 2. */
110 HOWTO (R_D10V_18_PCREL, /* Type. */
111 2, /* Rightshift. */
112 2, /* Size (0 = byte, 1 = short, 2 = long). */
a7985d73 113 16, /* Bitsize. */
47b0e7ad
NC
114 TRUE, /* PC_relative. */
115 0, /* Bitpos. */
a7985d73 116 complain_overflow_signed, /* Complain_on_overflow. */
47b0e7ad
NC
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. */
123
124 /* A 32 bit absolute relocation. */
125 HOWTO (R_D10V_32, /* Type. */
126 0, /* Rightshift. */
127 2, /* Size (0 = byte, 1 = short, 2 = long). */
128 32, /* Bitsize. */
129 FALSE, /* PC_relative. */
130 0, /* Bitpos. */
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. */
138
139 /* GNU extension to record C++ vtable hierarchy. */
140 HOWTO (R_D10V_GNU_VTINHERIT, /* Type. */
141 0, /* Rightshift. */
142 2, /* Size (0 = byte, 1 = short, 2 = long). */
143 0, /* Bitsize. */
144 FALSE, /* PC_relative. */
145 0, /* Bitpos. */
146 complain_overflow_dont,/* Complain_on_overflow. */
147 NULL, /* Special_function. */
148 "R_D10V_GNU_VTINHERIT",/* Name. */
149 FALSE, /* Partial_inplace. */
150 0, /* Src_mask. */
151 0, /* Dst_mask. */
152 FALSE), /* PCrel_offset. */
153
154 /* GNU extension to record C++ vtable member usage. */
155 HOWTO (R_D10V_GNU_VTENTRY, /* Type. */
156 0, /* Rightshift. */
157 2, /* Size (0 = byte, 1 = short, 2 = long). */
158 0, /* Bitsize. */
159 FALSE, /* PC_relative. */
160 0, /* Bitpos. */
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. */
165 0, /* Src_mask. */
166 0, /* Dst_mask. */
167 FALSE), /* PCrel_offset. */
168};
252b5132
RH
169
170/* Map BFD reloc types to D10V ELF reloc types. */
171
172struct d10v_reloc_map
47b0e7ad
NC
173{
174 bfd_reloc_code_real_type bfd_reloc_val;
175 unsigned char elf_reloc_val;
176};
917583ad
NC
177
178static const struct d10v_reloc_map d10v_reloc_map[] =
47b0e7ad
NC
179{
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 },
189};
252b5132
RH
190
191static reloc_howto_type *
47b0e7ad
NC
192bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
193 bfd_reloc_code_real_type code)
252b5132
RH
194{
195 unsigned int i;
196
197 for (i = 0;
198 i < sizeof (d10v_reloc_map) / sizeof (struct d10v_reloc_map);
199 i++)
47b0e7ad
NC
200 if (d10v_reloc_map[i].bfd_reloc_val == code)
201 return &elf_d10v_howto_table[d10v_reloc_map[i].elf_reloc_val];
252b5132
RH
202
203 return NULL;
204}
205
157090f7
AM
206static reloc_howto_type *
207bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
208 const char *r_name)
209{
210 unsigned int i;
211
212 for (i = 0;
213 i < sizeof (elf_d10v_howto_table) / sizeof (elf_d10v_howto_table[0]);
214 i++)
215 if (elf_d10v_howto_table[i].name != NULL
216 && strcasecmp (elf_d10v_howto_table[i].name, r_name) == 0)
217 return &elf_d10v_howto_table[i];
218
219 return NULL;
220}
221
252b5132
RH
222/* Set the howto pointer for an D10V ELF reloc. */
223
224static void
47b0e7ad
NC
225d10v_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
226 arelent *cache_ptr,
227 Elf_Internal_Rela *dst)
252b5132
RH
228{
229 unsigned int r_type;
230
231 r_type = ELF32_R_TYPE (dst->r_info);
232 BFD_ASSERT (r_type < (unsigned int) R_D10V_max);
233 cache_ptr->howto = &elf_d10v_howto_table[r_type];
234}
235
236static asection *
47b0e7ad 237elf32_d10v_gc_mark_hook (asection *sec,
07adf181 238 struct bfd_link_info *info,
47b0e7ad
NC
239 Elf_Internal_Rela *rel,
240 struct elf_link_hash_entry *h,
241 Elf_Internal_Sym *sym)
252b5132
RH
242{
243 if (h != NULL)
07adf181 244 switch (ELF32_R_TYPE (rel->r_info))
252b5132
RH
245 {
246 case R_D10V_GNU_VTINHERIT:
247 case R_D10V_GNU_VTENTRY:
07adf181
AM
248 return NULL;
249 }
1e2f5b6e 250
07adf181 251 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
252b5132
RH
252}
253
254/* Look through the relocs for a section during the first phase.
255 Since we don't do .gots or .plts, we just need to consider the
256 virtual table relocs for gc. */
a7c10850 257
b34976b6 258static bfd_boolean
47b0e7ad
NC
259elf32_d10v_check_relocs (bfd *abfd,
260 struct bfd_link_info *info,
261 asection *sec,
262 const Elf_Internal_Rela *relocs)
252b5132
RH
263{
264 Elf_Internal_Shdr *symtab_hdr;
265 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
266 const Elf_Internal_Rela *rel;
267 const Elf_Internal_Rela *rel_end;
a7c10850 268
1049f94e 269 if (info->relocatable)
b34976b6 270 return TRUE;
a7c10850 271
252b5132
RH
272 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
273 sym_hashes = elf_sym_hashes (abfd);
a7c10850 274 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
252b5132
RH
275 if (!elf_bad_symtab (abfd))
276 sym_hashes_end -= symtab_hdr->sh_info;
a7c10850 277
252b5132
RH
278 rel_end = relocs + sec->reloc_count;
279 for (rel = relocs; rel < rel_end; rel++)
280 {
281 struct elf_link_hash_entry *h;
282 unsigned long r_symndx;
a7c10850 283
252b5132
RH
284 r_symndx = ELF32_R_SYM (rel->r_info);
285 if (r_symndx < symtab_hdr->sh_info)
286 h = NULL;
287 else
973a3492
L
288 {
289 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
290 while (h->root.type == bfd_link_hash_indirect
291 || h->root.type == bfd_link_hash_warning)
292 h = (struct elf_link_hash_entry *) h->root.u.i.link;
293 }
a7c10850 294
252b5132
RH
295 switch (ELF32_R_TYPE (rel->r_info))
296 {
297 /* This relocation describes the C++ object vtable hierarchy.
298 Reconstruct it for later use during GC. */
299 case R_D10V_GNU_VTINHERIT:
c152c796 300 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 301 return FALSE;
252b5132 302 break;
a7c10850 303
252b5132
RH
304 /* This relocation describes which C++ vtable entries are actually
305 used. Record for later use during GC. */
306 case R_D10V_GNU_VTENTRY:
d17e0c6e
JB
307 BFD_ASSERT (h != NULL);
308 if (h != NULL
309 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 310 return FALSE;
252b5132
RH
311 break;
312 }
313 }
a7c10850 314
b34976b6 315 return TRUE;
252b5132
RH
316}
317
a2b0fe9d 318static bfd_vma
47b0e7ad
NC
319extract_rel_addend (bfd *abfd,
320 bfd_byte *where,
321 reloc_howto_type *howto)
a2b0fe9d
AM
322{
323 bfd_vma insn, val;
324
325 switch (howto->size)
326 {
327 case 0:
328 insn = bfd_get_8 (abfd, where);
329 break;
330 case 1:
331 insn = bfd_get_16 (abfd, where);
332 break;
333 case 2:
334 insn = bfd_get_32 (abfd, where);
335 break;
336 default:
337 abort ();
338 }
339
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)
344 {
345 bfd_vma sign;
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;
349 }
350 return val;
351}
352
353static void
47b0e7ad
NC
354insert_rel_addend (bfd *abfd,
355 bfd_byte *where,
356 reloc_howto_type *howto,
357 bfd_vma addend)
a2b0fe9d
AM
358{
359 bfd_vma insn;
360
361 addend = (addend >> howto->rightshift << howto->bitpos) & howto->dst_mask;
362 insn = ~howto->dst_mask;
363 switch (howto->size)
364 {
365 case 0:
366 insn &= bfd_get_8 (abfd, where);
367 insn |= addend;
368 bfd_put_8 (abfd, insn, where);
369 break;
370 case 1:
371 insn &= bfd_get_16 (abfd, where);
372 insn |= addend;
373 bfd_put_16 (abfd, insn, where);
374 break;
375 case 2:
376 insn &= bfd_get_32 (abfd, where);
377 insn |= addend;
378 bfd_put_32 (abfd, insn, where);
379 break;
380 default:
381 abort ();
382 }
383}
384
252b5132 385/* Relocate a D10V ELF section. */
47b0e7ad 386
b34976b6 387static bfd_boolean
47b0e7ad
NC
388elf32_d10v_relocate_section (bfd *output_bfd,
389 struct bfd_link_info *info,
390 bfd *input_bfd,
391 asection *input_section,
392 bfd_byte *contents,
393 Elf_Internal_Rela *relocs,
394 Elf_Internal_Sym *local_syms,
395 asection **local_sections)
252b5132
RH
396{
397 Elf_Internal_Shdr *symtab_hdr;
398 struct elf_link_hash_entry **sym_hashes;
399 Elf_Internal_Rela *rel, *relend;
400 const char *name;
401
402 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
403 sym_hashes = elf_sym_hashes (input_bfd);
404
405 rel = relocs;
406 relend = relocs + input_section->reloc_count;
407 for (; rel < relend; rel++)
408 {
409 int r_type;
410 reloc_howto_type *howto;
411 unsigned long r_symndx;
412 Elf_Internal_Sym *sym;
413 asection *sec;
414 struct elf_link_hash_entry *h;
415 bfd_vma relocation;
416 bfd_reloc_status_type r;
417
418 r_symndx = ELF32_R_SYM (rel->r_info);
419 r_type = ELF32_R_TYPE (rel->r_info);
420
421 if (r_type == R_D10V_GNU_VTENTRY
47b0e7ad 422 || r_type == R_D10V_GNU_VTINHERIT)
252b5132
RH
423 continue;
424
425 howto = elf_d10v_howto_table + r_type;
252b5132
RH
426 h = NULL;
427 sym = NULL;
428 sec = NULL;
429 if (r_symndx < symtab_hdr->sh_info)
430 {
431 sym = local_syms + r_symndx;
432 sec = local_sections[r_symndx];
a2b0fe9d
AM
433 relocation = (sec->output_section->vma
434 + sec->output_offset
435 + sym->st_value);
ab96bf03
AM
436 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
437 && ((sec->flags & SEC_MERGE) != 0
438 || (info->relocatable
439 && sec->output_offset != 0)))
a2b0fe9d 440 {
a2b0fe9d
AM
441 bfd_vma addend;
442 bfd_byte *where = contents + rel->r_offset;
443
444 addend = extract_rel_addend (input_bfd, where, howto);
ab96bf03
AM
445
446 if (info->relocatable)
447 addend += sec->output_offset;
448 else
449 {
450 asection *msec = sec;
451 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
452 addend);
453 addend -= relocation;
454 addend += msec->output_section->vma + msec->output_offset;
455 }
a2b0fe9d
AM
456 insert_rel_addend (input_bfd, where, howto, addend);
457 }
252b5132
RH
458 }
459 else
460 {
59c2e50f
L
461 bfd_boolean unresolved_reloc, warned;
462
b2a8e766
AM
463 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
464 r_symndx, symtab_hdr, sym_hashes,
465 h, sec, relocation,
466 unresolved_reloc, warned);
252b5132
RH
467 }
468
ab96bf03 469 if (sec != NULL && elf_discarded_section (sec))
b1e24c02 470 {
ab96bf03
AM
471 /* For relocs against symbols from removed linkonce sections,
472 or sections discarded by a linker script, we just want the
473 section contents zeroed. Avoid any special processing. */
b1e24c02 474 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
ab96bf03
AM
475 rel->r_info = 0;
476 rel->r_addend = 0;
b1e24c02
DJ
477 continue;
478 }
479
ab96bf03
AM
480 if (info->relocatable)
481 continue;
482
252b5132
RH
483 if (h != NULL)
484 name = h->root.root.string;
485 else
486 {
487 name = (bfd_elf_string_from_elf_section
488 (input_bfd, symtab_hdr->sh_link, sym->st_name));
489 if (name == NULL || *name == '\0')
490 name = bfd_section_name (input_bfd, sec);
491 }
a7c10850 492
252b5132
RH
493 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
494 contents, rel->r_offset,
a2b0fe9d 495 relocation, (bfd_vma) 0);
252b5132
RH
496
497 if (r != bfd_reloc_ok)
498 {
499 const char * msg = (const char *) 0;
500
501 switch (r)
502 {
503 case bfd_reloc_overflow:
504 if (!((*info->callbacks->reloc_overflow)
47b0e7ad 505 (info, (h ? &h->root : NULL), name, howto->name,
dfeffb9f
L
506 (bfd_vma) 0, input_bfd, input_section,
507 rel->r_offset)))
b34976b6 508 return FALSE;
252b5132
RH
509 break;
510
511 case bfd_reloc_undefined:
512 if (!((*info->callbacks->undefined_symbol)
513 (info, name, input_bfd, input_section,
b34976b6
AM
514 rel->r_offset, TRUE)))
515 return FALSE;
252b5132
RH
516 break;
517
518 case bfd_reloc_outofrange:
517662d4 519 msg = _("internal error: out of range error");
252b5132
RH
520 goto common_error;
521
522 case bfd_reloc_notsupported:
517662d4 523 msg = _("internal error: unsupported relocation error");
252b5132
RH
524 goto common_error;
525
526 case bfd_reloc_dangerous:
517662d4 527 msg = _("internal error: dangerous error");
252b5132
RH
528 goto common_error;
529
530 default:
517662d4 531 msg = _("internal error: unknown error");
252b5132
RH
532 /* fall through */
533
534 common_error:
535 if (!((*info->callbacks->warning)
536 (info, msg, name, input_bfd, input_section,
537 rel->r_offset)))
b34976b6 538 return FALSE;
252b5132
RH
539 break;
540 }
541 }
542 }
543
b34976b6 544 return TRUE;
252b5132
RH
545}
546#define ELF_ARCH bfd_arch_d10v
aa4f99bb
AO
547#define ELF_MACHINE_CODE EM_D10V
548#define ELF_MACHINE_ALT1 EM_CYGNUS_D10V
252b5132
RH
549#define ELF_MAXPAGESIZE 0x1000
550
551#define TARGET_BIG_SYM bfd_elf32_d10v_vec
552#define TARGET_BIG_NAME "elf32-d10v"
553
554#define elf_info_to_howto 0
555#define elf_info_to_howto_rel d10v_info_to_howto_rel
556#define elf_backend_object_p 0
557#define elf_backend_final_write_processing 0
558#define elf_backend_gc_mark_hook elf32_d10v_gc_mark_hook
252b5132
RH
559#define elf_backend_check_relocs elf32_d10v_check_relocs
560#define elf_backend_relocate_section elf32_d10v_relocate_section
561#define elf_backend_can_gc_sections 1
562
563#include "elf32-target.h"
This page took 0.405292 seconds and 4 git commands to generate.