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