* elf32-d10v.c (elf_d10v_howto_table): Adjust bit-size of
[external/binutils.git] / bfd / elf32-d10v.c
1 /* D10V-specific support for 32-bit ELF
2    Copyright 1996, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3    Contributed by Martin Hunt (hunt@cygnus.com).
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/d10v.h"
26
27 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
28   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
29 static void d10v_info_to_howto_rel
30   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
31 static asection * elf32_d10v_gc_mark_hook
32   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
33            struct elf_link_hash_entry *, Elf_Internal_Sym *));
34 static boolean elf32_d10v_gc_sweep_hook
35   PARAMS ((bfd *, struct bfd_link_info *, asection *,
36            const Elf_Internal_Rela *));
37 static boolean elf32_d10v_check_relocs
38   PARAMS ((bfd *, struct bfd_link_info *, asection *,
39            const Elf_Internal_Rela *));
40 static boolean elf32_d10v_relocate_section
41   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
42            bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *,
43            asection **));
44
45 /* Use REL instead of RELA to save space.  */
46 #define USE_REL
47
48 static reloc_howto_type elf_d10v_howto_table[] =
49   {
50     /* This reloc does nothing.  */
51     HOWTO (R_D10V_NONE,         /* type */
52            0,                   /* rightshift */
53            2,                   /* size (0 = byte, 1 = short, 2 = long) */
54            32,                  /* bitsize */
55            false,               /* pc_relative */
56            0,                   /* bitpos */
57            complain_overflow_dont, /* complain_on_overflow */
58            bfd_elf_generic_reloc, /* special_function */
59            "R_D10V_NONE",       /* name */
60            false,               /* partial_inplace */
61            0,                   /* src_mask */
62            0,                   /* dst_mask */
63            false),              /* pcrel_offset */
64
65     /* An PC Relative 10-bit relocation, shifted by 2  */
66     /* right container */
67     HOWTO (R_D10V_10_PCREL_R,   /* type */
68            2,                   /* rightshift */
69            2,                   /* size (0 = byte, 1 = short, 2 = long) */
70            8,                   /* bitsize */
71            true,                /* pc_relative */
72            0,                   /* bitpos */
73            complain_overflow_signed, /* complain_on_overflow */
74            bfd_elf_generic_reloc, /* special_function */
75            "R_D10V_10_PCREL_R", /* name */
76            false,               /* partial_inplace */
77            0xff,                /* src_mask */
78            0xff,                /* dst_mask */
79            true),               /* pcrel_offset */
80
81     /* An PC Relative 10-bit relocation, shifted by 2  */
82     /* left container */
83     HOWTO (R_D10V_10_PCREL_L,   /* type */
84            2,                   /* rightshift */
85            2,                   /* size (0 = byte, 1 = short, 2 = long) */
86            8,                   /* bitsize */
87            true,                /* pc_relative */
88            15,                  /* bitpos */
89            complain_overflow_signed, /* complain_on_overflow */
90            bfd_elf_generic_reloc, /* special_function */
91            "R_D10V_10_PCREL_L", /* name */
92            false,               /* partial_inplace */
93            0x07f8000,           /* src_mask */
94            0x07f8000,           /* dst_mask */
95            true),               /* pcrel_offset */
96
97     /* A 16 bit absolute relocation */
98     HOWTO (R_D10V_16,           /* type */
99            0,                   /* rightshift */
100            1,                   /* size (0 = byte, 1 = short, 2 = long) */
101            16,                  /* bitsize */
102            false,               /* pc_relative */
103            0,                   /* bitpos */
104            complain_overflow_dont, /* complain_on_overflow */
105            bfd_elf_generic_reloc, /* special_function */
106            "R_D10V_16",         /* name */
107            false,               /* partial_inplace */
108            0xffff,              /* src_mask */
109            0xffff,              /* dst_mask */
110            false),              /* pcrel_offset */
111
112     /* An 18 bit absolute relocation, right shifted 2 */
113     HOWTO (R_D10V_18,           /* type */
114            2,                   /* rightshift */
115            1,                   /* size (0 = byte, 1 = short, 2 = long) */
116            16,                  /* bitsize */
117            false,               /* pc_relative */
118            0,                   /* bitpos */
119            complain_overflow_dont, /* complain_on_overflow */
120            bfd_elf_generic_reloc, /* special_function */
121            "R_D10V_18",         /* name */
122            false,               /* partial_inplace */
123            0xffff,              /* src_mask */
124            0xffff,              /* dst_mask */
125            false),              /* pcrel_offset */
126
127     /* A relative 18 bit relocation, right shifted by 2  */
128     HOWTO (R_D10V_18_PCREL,     /* type */
129            2,                   /* rightshift */
130            2,                   /* size (0 = byte, 1 = short, 2 = long) */
131            16,                  /* bitsize */
132            true,                /* pc_relative */
133            0,                   /* bitpos */
134            complain_overflow_signed, /* complain_on_overflow */
135            bfd_elf_generic_reloc, /* special_function */
136            "R_D10V_18_PCREL",   /* name */
137            false,               /* partial_inplace */
138            0xffff,              /* src_mask */
139            0xffff,              /* dst_mask */
140            true),                       /* pcrel_offset */
141
142     /* A 32 bit absolute relocation */
143     HOWTO (R_D10V_32,           /* type */
144            0,                   /* rightshift */
145            2,                   /* size (0 = byte, 1 = short, 2 = long) */
146            32,                  /* bitsize */
147            false,               /* pc_relative */
148            0,                   /* bitpos */
149            complain_overflow_dont, /* complain_on_overflow */
150            bfd_elf_generic_reloc, /* special_function */
151            "R_D10V_32",         /* name */
152            false,               /* partial_inplace */
153            0xffffffff,          /* src_mask */
154            0xffffffff,          /* dst_mask */
155            false),              /* pcrel_offset */
156
157     /* GNU extension to record C++ vtable hierarchy */
158     HOWTO (R_D10V_GNU_VTINHERIT, /* type */
159            0,                     /* rightshift */
160            2,                     /* size (0 = byte, 1 = short, 2 = long) */
161            0,                     /* bitsize */
162            false,                 /* pc_relative */
163            0,                     /* bitpos */
164            complain_overflow_dont, /* complain_on_overflow */
165            NULL,                  /* special_function */
166            "R_D10V_GNU_VTINHERIT", /* name */
167            false,                 /* partial_inplace */
168            0,                     /* src_mask */
169            0,                     /* dst_mask */
170            false),                /* pcrel_offset */
171
172     /* GNU extension to record C++ vtable member usage */
173     HOWTO (R_D10V_GNU_VTENTRY,     /* type */
174            0,                     /* rightshift */
175            2,                     /* size (0 = byte, 1 = short, 2 = long) */
176            0,                     /* bitsize */
177            false,                 /* pc_relative */
178            0,                     /* bitpos */
179            complain_overflow_dont, /* complain_on_overflow */
180            _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
181            "R_D10V_GNU_VTENTRY",   /* name */
182            false,                 /* partial_inplace */
183            0,                     /* src_mask */
184            0,                     /* dst_mask */
185            false),                /* pcrel_offset */
186   };
187
188 /* Map BFD reloc types to D10V ELF reloc types.  */
189
190 struct d10v_reloc_map
191   {
192     bfd_reloc_code_real_type bfd_reloc_val;
193     unsigned char elf_reloc_val;
194   };
195
196 static const struct d10v_reloc_map d10v_reloc_map[] =
197   {
198     { BFD_RELOC_NONE, R_D10V_NONE, },
199     { BFD_RELOC_D10V_10_PCREL_R, R_D10V_10_PCREL_R },
200     { BFD_RELOC_D10V_10_PCREL_L, R_D10V_10_PCREL_L },
201     { BFD_RELOC_16, R_D10V_16 },
202     { BFD_RELOC_D10V_18, R_D10V_18 },
203     { BFD_RELOC_D10V_18_PCREL, R_D10V_18_PCREL },
204     { BFD_RELOC_32, R_D10V_32 },
205     { BFD_RELOC_VTABLE_INHERIT, R_D10V_GNU_VTINHERIT },
206     { BFD_RELOC_VTABLE_ENTRY, R_D10V_GNU_VTENTRY },
207   };
208
209 static reloc_howto_type *
210 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
211      bfd *abfd ATTRIBUTE_UNUSED;
212      bfd_reloc_code_real_type code;
213 {
214   unsigned int i;
215
216   for (i = 0;
217        i < sizeof (d10v_reloc_map) / sizeof (struct d10v_reloc_map);
218        i++)
219     {
220       if (d10v_reloc_map[i].bfd_reloc_val == code)
221         return &elf_d10v_howto_table[d10v_reloc_map[i].elf_reloc_val];
222     }
223
224   return NULL;
225 }
226
227 /* Set the howto pointer for an D10V ELF reloc.  */
228
229 static void
230 d10v_info_to_howto_rel (abfd, cache_ptr, dst)
231      bfd *abfd ATTRIBUTE_UNUSED;
232      arelent *cache_ptr;
233      Elf32_Internal_Rel *dst;
234 {
235   unsigned int r_type;
236
237   r_type = ELF32_R_TYPE (dst->r_info);
238   BFD_ASSERT (r_type < (unsigned int) R_D10V_max);
239   cache_ptr->howto = &elf_d10v_howto_table[r_type];
240 }
241
242 static asection *
243 elf32_d10v_gc_mark_hook (abfd, info, rel, h, sym)
244        bfd *abfd;
245        struct bfd_link_info *info ATTRIBUTE_UNUSED;
246        Elf_Internal_Rela *rel;
247        struct elf_link_hash_entry *h;
248        Elf_Internal_Sym *sym;
249 {
250   if (h != NULL)
251     {
252       switch (ELF32_R_TYPE (rel->r_info))
253       {
254       case R_D10V_GNU_VTINHERIT:
255       case R_D10V_GNU_VTENTRY:
256         break;
257
258       default:
259         switch (h->root.type)
260           {
261           case bfd_link_hash_defined:
262           case bfd_link_hash_defweak:
263             return h->root.u.def.section;
264
265           case bfd_link_hash_common:
266             return h->root.u.c.p->section;
267
268           default:
269             break;
270           }
271        }
272      }
273    else
274      {
275        if (!(elf_bad_symtab (abfd)
276            && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
277          && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
278                 && sym->st_shndx != SHN_COMMON))
279           {
280             return bfd_section_from_elf_index (abfd, sym->st_shndx);
281           }
282       }
283   return NULL;
284 }
285
286 static boolean
287 elf32_d10v_gc_sweep_hook (abfd, info, sec, relocs)
288      bfd *abfd ATTRIBUTE_UNUSED;
289      struct bfd_link_info *info ATTRIBUTE_UNUSED;
290      asection *sec ATTRIBUTE_UNUSED;
291      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
292 {
293   /* we don't use got and plt entries for d10v */
294   return true;
295 }
296
297 /* Look through the relocs for a section during the first phase.
298    Since we don't do .gots or .plts, we just need to consider the
299    virtual table relocs for gc.  */
300
301 static boolean
302 elf32_d10v_check_relocs (abfd, info, sec, relocs)
303      bfd *abfd;
304      struct bfd_link_info *info;
305      asection *sec;
306      const Elf_Internal_Rela *relocs;
307 {
308   Elf_Internal_Shdr *symtab_hdr;
309   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
310   const Elf_Internal_Rela *rel;
311   const Elf_Internal_Rela *rel_end;
312
313   if (info->relocateable)
314     return true;
315
316   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
317   sym_hashes = elf_sym_hashes (abfd);
318   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
319   if (!elf_bad_symtab (abfd))
320     sym_hashes_end -= symtab_hdr->sh_info;
321
322   rel_end = relocs + sec->reloc_count;
323   for (rel = relocs; rel < rel_end; rel++)
324     {
325       struct elf_link_hash_entry *h;
326       unsigned long r_symndx;
327
328       r_symndx = ELF32_R_SYM (rel->r_info);
329       if (r_symndx < symtab_hdr->sh_info)
330         h = NULL;
331       else
332         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
333
334       switch (ELF32_R_TYPE (rel->r_info))
335         {
336         /* This relocation describes the C++ object vtable hierarchy.
337            Reconstruct it for later use during GC.  */
338         case R_D10V_GNU_VTINHERIT:
339           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
340             return false;
341           break;
342
343         /* This relocation describes which C++ vtable entries are actually
344            used.  Record for later use during GC.  */
345         case R_D10V_GNU_VTENTRY:
346           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
347             return false;
348           break;
349         }
350     }
351
352   return true;
353 }
354
355 /* Relocate a D10V ELF section.  */
356 static boolean
357 elf32_d10v_relocate_section (output_bfd, info, input_bfd, input_section,
358                             contents, relocs, local_syms, local_sections)
359      bfd *output_bfd;
360      struct bfd_link_info *info;
361      bfd *input_bfd;
362      asection *input_section;
363      bfd_byte *contents;
364      Elf_Internal_Rela *relocs;
365      Elf_Internal_Sym *local_syms;
366      asection **local_sections;
367 {
368   Elf_Internal_Shdr *symtab_hdr;
369   struct elf_link_hash_entry **sym_hashes;
370   Elf_Internal_Rela *rel, *relend;
371   const char *name;
372
373   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
374   sym_hashes = elf_sym_hashes (input_bfd);
375
376   rel = relocs;
377   relend = relocs + input_section->reloc_count;
378   for (; rel < relend; rel++)
379     {
380       int r_type;
381       reloc_howto_type *howto;
382       unsigned long r_symndx;
383       Elf_Internal_Sym *sym;
384       asection *sec;
385       struct elf_link_hash_entry *h;
386       bfd_vma relocation;
387       bfd_reloc_status_type r;
388
389       r_symndx = ELF32_R_SYM (rel->r_info);
390       r_type = ELF32_R_TYPE (rel->r_info);
391
392       if (r_type == R_D10V_GNU_VTENTRY
393           || r_type == R_D10V_GNU_VTINHERIT )
394         continue;
395
396       howto = elf_d10v_howto_table + r_type;
397
398       if (info->relocateable)
399         {
400           /* This is a relocateable link.  We don't have to change
401              anything, unless the reloc is against a section symbol,
402              in which case we have to adjust according to where the
403              section symbol winds up in the output section.  */
404           if (r_symndx < symtab_hdr->sh_info)
405             {
406               sym = local_syms + r_symndx;
407               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
408                 {
409                   sec = local_sections[r_symndx];
410                   rel->r_addend += sec->output_offset + sym->st_value;
411                 }
412             }
413
414           continue;
415         }
416
417       /* This is a final link.  */
418       h = NULL;
419       sym = NULL;
420       sec = NULL;
421       if (r_symndx < symtab_hdr->sh_info)
422         {
423           sym = local_syms + r_symndx;
424           sec = local_sections[r_symndx];
425           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
426         }
427       else
428         {
429           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
430           while (h->root.type == bfd_link_hash_indirect
431                  || h->root.type == bfd_link_hash_warning)
432             h = (struct elf_link_hash_entry *) h->root.u.i.link;
433           if (h->root.type == bfd_link_hash_defined
434               || h->root.type == bfd_link_hash_defweak)
435             {
436               sec = h->root.u.def.section;
437               relocation = (h->root.u.def.value
438                             + sec->output_section->vma
439                             + sec->output_offset);
440             }
441           else if (h->root.type == bfd_link_hash_undefweak)
442             relocation = 0;
443           else
444             {
445               if (!((*info->callbacks->undefined_symbol)
446                     (info, h->root.root.string, input_bfd,
447                      input_section, rel->r_offset, true)))
448                 return false;
449               relocation = 0;
450             }
451         }
452
453       if (h != NULL)
454         name = h->root.root.string;
455       else
456         {
457           name = (bfd_elf_string_from_elf_section
458                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
459           if (name == NULL || *name == '\0')
460             name = bfd_section_name (input_bfd, sec);
461         }
462
463       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
464                                     contents, rel->r_offset,
465                                     relocation, rel->r_addend);
466
467       if (r != bfd_reloc_ok)
468         {
469           const char * msg = (const char *) 0;
470
471           switch (r)
472             {
473             case bfd_reloc_overflow:
474               if (!((*info->callbacks->reloc_overflow)
475                     (info, name, howto->name, (bfd_vma) 0,
476                      input_bfd, input_section, rel->r_offset)))
477                 return false;
478               break;
479
480             case bfd_reloc_undefined:
481               if (!((*info->callbacks->undefined_symbol)
482                     (info, name, input_bfd, input_section,
483                      rel->r_offset, true)))
484                 return false;
485               break;
486
487             case bfd_reloc_outofrange:
488               msg = _ ("internal error: out of range error");
489               goto common_error;
490
491             case bfd_reloc_notsupported:
492               msg = _ ("internal error: unsupported relocation error");
493               goto common_error;
494
495             case bfd_reloc_dangerous:
496               msg = _ ("internal error: dangerous error");
497               goto common_error;
498
499             default:
500               msg = _ ("internal error: unknown error");
501               /* fall through */
502
503             common_error:
504               if (!((*info->callbacks->warning)
505                     (info, msg, name, input_bfd, input_section,
506                      rel->r_offset)))
507                 return false;
508               break;
509             }
510         }
511     }
512
513   return true;
514 }
515 #define ELF_ARCH                bfd_arch_d10v
516 #define ELF_MACHINE_CODE        EM_D10V
517 #define ELF_MACHINE_ALT1        EM_CYGNUS_D10V
518 #define ELF_MAXPAGESIZE         0x1000
519
520 #define TARGET_BIG_SYM          bfd_elf32_d10v_vec
521 #define TARGET_BIG_NAME         "elf32-d10v"
522
523 #define elf_info_to_howto                    0
524 #define elf_info_to_howto_rel                d10v_info_to_howto_rel
525 #define elf_backend_object_p                 0
526 #define elf_backend_final_write_processing   0
527 #define elf_backend_gc_mark_hook             elf32_d10v_gc_mark_hook
528 #define elf_backend_gc_sweep_hook            elf32_d10v_gc_sweep_hook
529 #define elf_backend_check_relocs             elf32_d10v_check_relocs
530 #define elf_backend_relocate_section         elf32_d10v_relocate_section
531 #define elf_backend_can_gc_sections          1
532
533 #include "elf32-target.h"