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