xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elf32-visium.c (revision eceb233b9bd0dfebb902ed73b531ae6964fa3f9b)
1 /* Visium-specific support for 32-bit ELF.
2 
3    Copyright (C) 2003-2018 Free Software Foundation, Inc.
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 3 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., 51 Franklin Street - Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21 
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/visium.h"
28 #include "libiberty.h"
29 
30 static bfd_reloc_status_type visium_elf_howto_parity_reloc
31   (bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **);
32 
33 static reloc_howto_type visium_elf_howto_table[] = {
34   /* This reloc does nothing.  */
35   HOWTO (R_VISIUM_NONE,		/* type */
36 	 0,			/* rightshift */
37 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
38 	 0,			/* bitsize */
39 	 FALSE,			/* pc_relative */
40 	 0,			/* bitpos */
41 	 complain_overflow_dont,	/* complain_on_overflow */
42 	 bfd_elf_generic_reloc,	/* special_function */
43 	 "R_VISIUM_NONE",	/* name */
44 	 FALSE,			/* partial_inplace */
45 	 0,			/* src_mask */
46 	 0,			/* dst_mask */
47 	 FALSE),		/* pcrel_offset */
48 
49   /* A 8 bit absolute relocation.  */
50   HOWTO (R_VISIUM_8,		/* type */
51 	 0,			/* rightshift */
52 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
53 	 8,			/* bitsize */
54 	 FALSE,			/* pc_relative */
55 	 0,			/* bitpos */
56 	 complain_overflow_bitfield,	/* complain_on_overflow */
57 	 bfd_elf_generic_reloc,	/* special_function */
58 	 "R_VISIUM_8",		/* name */
59 	 FALSE,			/* partial_inplace */
60 	 0x00,			/* src_mask */
61 	 0xff,			/* dst_mask */
62 	 FALSE),		/* pcrel_offset */
63 
64   /* A 16 bit absolute relocation.  */
65   HOWTO (R_VISIUM_16,		/* type */
66 	 0,			/* rightshift */
67 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
68 	 16,			/* bitsize */
69 	 FALSE,			/* pc_relative */
70 	 0,			/* bitpos */
71 	 complain_overflow_bitfield,	/* complain_on_overflow */
72 	 bfd_elf_generic_reloc,	/* special_function */
73 	 "R_VISIUM_16",		/* name */
74 	 FALSE,			/* partial_inplace */
75 	 0x0000,		/* src_mask */
76 	 0xffff,		/* dst_mask */
77 	 FALSE),		/* pcrel_offset */
78 
79   /* A 32 bit absolute relocation.  */
80   HOWTO (R_VISIUM_32,		/* type */
81 	 0,			/* rightshift */
82 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
83 	 32,			/* bitsize */
84 	 FALSE,			/* pc_relative */
85 	 0,			/* bitpos */
86 	 complain_overflow_bitfield,	/* complain_on_overflow */
87 	 bfd_elf_generic_reloc,	/* special_function */
88 	 "R_VISIUM_32",		/* name */
89 	 FALSE,			/* partial_inplace */
90 	 0x00000000,		/* src_mask */
91 	 0xffffffff,		/* dst_mask */
92 	 FALSE),		/* pcrel_offset */
93 
94 
95   /* A 8 bit PC relative relocation.  */
96   HOWTO (R_VISIUM_8_PCREL,		/* type */
97 	 0,			/* rightshift */
98 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
99 	 8,			/* bitsize */
100 	 TRUE,			/* pc_relative */
101 	 0,			/* bitpos */
102 	 complain_overflow_bitfield,	/* complain_on_overflow */
103 	 bfd_elf_generic_reloc,	/* special_function */
104 	 "R_VISIUM_8_PCREL",	/* name */
105 	 FALSE,			/* partial_inplace */
106 	 0x00,			/* src_mask */
107 	 0xff,			/* dst_mask */
108 	 TRUE),			/* pcrel_offset */
109 
110   /* A 16 bit PC relative relocation.  */
111   HOWTO (R_VISIUM_16_PCREL,	/* type */
112 	 0,			/* rightshift */
113 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
114 	 16,			/* bitsize */
115 	 TRUE,			/* pc_relative */
116 	 0,			/* bitpos */
117 	 complain_overflow_bitfield,	/* complain_on_overflow */
118 	 bfd_elf_generic_reloc,	/* special_function */
119 	 "R_VISIUM_16_PCREL",	/* name */
120 	 FALSE,			/* partial inplace */
121 	 0x0000,		/* src_mask */
122 	 0xffff,		/* dst_mask */
123 	 TRUE),			/* pcrel_offset */
124 
125   /* A 32-bit PC relative relocation.  */
126   HOWTO (R_VISIUM_32_PCREL,	/* type */
127 	 0,			/* rightshift */
128 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
129 	 32,			/* bitsize */
130 	 TRUE,			/* pc_relative */
131 	 0,			/* bitpos */
132 	 complain_overflow_bitfield,	/* complain_on_overflow */
133 	 bfd_elf_generic_reloc,	/* special_function */
134 	 "R_VISIUM_32_PCREL",	/* name */
135 	 FALSE,			/* partial_inplace */
136 	 0,			/* src_mask */
137 	 0xffffffff,		/* dst_mask */
138 	 TRUE),			/* pcrel_offset */
139 
140   /* A 16-bit PC word relative offset, relative to start of instruction
141      and always in the second half of the instruction.  */
142   HOWTO (R_VISIUM_PC16,		/* type */
143 	 2,			/* rightshift */
144 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
145 	 16,			/* bitsize */
146 	 TRUE,			/* pc_relative */
147 	 0,			/* bitpos */
148 	 complain_overflow_signed,	/* complain_on_overflow */
149 	 visium_elf_howto_parity_reloc,	/* special_function */
150 	 "R_VISIUM_PC16",	/* name */
151 	 FALSE,			/* partial_inplace */
152 	 0x00000000,		/* src_mask */
153 	 0x0000ffff,		/* dst_mask */
154 	 TRUE),			/* pcrel_offset */
155 
156   /* The high 16 bits of symbol value.  */
157   HOWTO (R_VISIUM_HI16,		/* type */
158 	 16,			/* rightshift */
159 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
160 	 16,			/* bitsize */
161 	 FALSE,			/* pc_relative */
162 	 0,			/* bitpos */
163 	 complain_overflow_dont,	/* complain_on_overflow */
164 	 visium_elf_howto_parity_reloc,	/* special_function */
165 	 "R_VISIUM_HI16",	/* name */
166 	 FALSE,			/* partial_inplace */
167 	 0x00000000,		/* src_mask */
168 	 0x0000ffff,		/* dst_mask */
169 	 FALSE),		/* pcrel_offset */
170 
171   /* The low 16 bits of symbol value.  */
172   HOWTO (R_VISIUM_LO16,		/* type */
173 	 0,			/* rightshift */
174 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
175 	 16,			/* bitsize */
176 	 FALSE,			/* pc_relative */
177 	 0,			/* bitpos */
178 	 complain_overflow_dont,	/* complain_on_overflow */
179 	 visium_elf_howto_parity_reloc,	/* special_function */
180 	 "R_VISIUM_LO16",	/* name */
181 	 FALSE,			/* partial_inplace */
182 	 0x00000000,		/* src_mask */
183 	 0x0000ffff,		/* dst_mask */
184 	 FALSE),		/* pcrel_offset */
185 
186   /* A 16 bit immediate value.  */
187   HOWTO (R_VISIUM_IM16,		/* type */
188 	 0,			/* rightshift */
189 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
190 	 16,			/* bitsize */
191 	 FALSE,			/* pc_relative */
192 	 0,			/* bitpos */
193 	 complain_overflow_unsigned,	/* complain_on_overflow */
194 	 visium_elf_howto_parity_reloc,	/* special_function */
195 	 "R_VISIUM_IM16",	/* name */
196 	 FALSE,			/* partial_inplace */
197 	 0x0000000,		/* src_mask */
198 	 0x000ffff,		/* dst_mask */
199 	 FALSE),		/* pcrel_offset */
200 
201   /* The high 16 bits of symbol value, pc relative.  */
202   HOWTO (R_VISIUM_HI16_PCREL,	/* type */
203 	 16,			/* rightshift */
204 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
205 	 16,			/* bitsize */
206 	 TRUE,			/* pc_relative */
207 	 0,			/* bitpos */
208 	 complain_overflow_dont,	/* complain_on_overflow */
209 	 visium_elf_howto_parity_reloc,	/* special_function */
210 	 "R_VISIUM_HI16_PCREL",	/* name */
211 	 FALSE,			/* partial_inplace */
212 	 0x00000000,		/* src_mask */
213 	 0x0000ffff,		/* dst_mask */
214 	 TRUE),			/* pcrel_offset */
215 
216   /* The low 16 bits of symbol value, pc relative.  */
217   HOWTO (R_VISIUM_LO16_PCREL,	/* type */
218 	 0,			/* rightshift */
219 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
220 	 16,			/* bitsize */
221 	 TRUE,			/* pc_relative */
222 	 0,			/* bitpos */
223 	 complain_overflow_dont,	/* complain_on_overflow */
224 	 visium_elf_howto_parity_reloc,	/* special_function */
225 	 "R_VISIUM_LO16_PCREL",	/* name */
226 	 FALSE,			/* partial_inplace */
227 	 0x00000000,		/* src_mask */
228 	 0x0000ffff,		/* dst_mask */
229 	 TRUE),			/* pcrel_offset */
230 
231   /* A 16 bit immediate value, pc relative.  */
232   HOWTO (R_VISIUM_IM16_PCREL,	/* type */
233 	 0,			/* rightshift */
234 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
235 	 16,			/* bitsize */
236 	 TRUE,			/* pc_relative */
237 	 0,			/* bitpos */
238 	 complain_overflow_unsigned,	/* complain_on_overflow */
239 	 visium_elf_howto_parity_reloc,	/* special_function */
240 	 "R_VISIUM_IM16_PCREL",	/* name */
241 	 FALSE,			/* partial_inplace */
242 	 0x0000000,		/* src_mask */
243 	 0x000ffff,		/* dst_mask */
244 	 TRUE),			/* pcrel_offset */
245 
246 };
247 
248 /* GNU extension to record C++ vtable hierarchy.  */
249 static reloc_howto_type visium_elf_vtinherit_howto =
250   HOWTO (R_VISIUM_GNU_VTINHERIT,      /* type */
251 	 0,			   /* rightshift */
252 	 2,			   /* size (0 = byte, 1 = short, 2 = long) */
253 	 0,			   /* bitsize */
254 	 FALSE,			   /* pc_relative */
255 	 0,			   /* bitpos */
256 	 complain_overflow_dont,   /* complain_on_overflow */
257 	 NULL,			   /* special_function */
258 	 "R_VISIUM_GNU_VTINHERIT", /* name */
259 	 FALSE,			   /* partial_inplace */
260 	 0,			   /* src_mask */
261 	 0,			   /* dst_mask */
262 	 FALSE);		   /* pcrel_offset */
263 
264 /* GNU extension to record C++ vtable member usage.  */
265 static reloc_howto_type visium_elf_vtentry_howto =
266   HOWTO (R_VISIUM_GNU_VTENTRY,	   /* type */
267 	 0,			   /* rightshift */
268 	 2,			   /* size (0 = byte, 1 = short, 2 = long) */
269 	 0,			   /* bitsize */
270 	 FALSE,			   /* pc_relative */
271 	 0,			   /* bitpos */
272 	 complain_overflow_dont,   /* complain_on_overflow */
273 	 NULL,			   /* special_function */
274 	 "R_VISIUM_GNU_VTENTRY",   /* name */
275 	 FALSE,			   /* partial_inplace */
276 	 0,			   /* src_mask */
277 	 0,			   /* dst_mask */
278 	 FALSE);		   /* pcrel_offset */
279 
280 /* Return the parity bit for INSN shifted to its final position.  */
281 
282 static bfd_vma
283 visium_parity_bit (bfd_vma insn)
284 {
285   bfd_vma p = 0;
286   int i;
287 
288   for (i = 0; i < 31; i++)
289     {
290       p ^= (insn & 1);
291       insn >>= 1;
292     }
293 
294   return p << 31;
295 }
296 
297 /* This "special function" will only be used when the input and
298    output files have different formats ie. when generating S-records
299    directly using "--oformat srec". Otherwise we use
300    _bfd_final_link_relocate which uses a howto structure, but does
301    not use the special_function field.
302 
303    It sets instruction parity to even.  This cannot be done by a howto.  */
304 
305 static bfd_reloc_status_type
306 visium_elf_howto_parity_reloc (bfd * input_bfd, arelent *reloc_entry,
307 			       asymbol *symbol, PTR data,
308 			       asection *input_section, bfd *output_bfd,
309 			       char **error_message ATTRIBUTE_UNUSED)
310 {
311   bfd_reloc_status_type ret;
312   bfd_vma relocation;
313   bfd_byte *inplace_address;
314   bfd_vma insn;
315   const bfd_vma signmask = 0xffff8000;
316 
317   /* This part is from bfd_elf_generic_reloc.
318      If we're relocating, and this an external symbol, we don't want
319      to change anything.  */
320   if (output_bfd != (bfd *) NULL && (symbol->flags & BSF_SECTION_SYM) == 0)
321     {
322       reloc_entry->address += input_section->output_offset;
323       return bfd_reloc_ok;
324     }
325 
326   /* Now do the reloc in the usual way.  */
327 
328   /* Sanity check the address (offset in section).  */
329   if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
330     return bfd_reloc_outofrange;
331 
332   ret = bfd_reloc_ok;
333   if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
334     ret = bfd_reloc_undefined;
335 
336   if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
337     relocation = 0;
338   else
339     relocation = symbol->value;
340 
341   /* Only do this for a final link.  */
342   if (output_bfd == (bfd *) NULL)
343     {
344       relocation += symbol->section->output_section->vma;
345       relocation += symbol->section->output_offset;
346     }
347 
348   relocation += reloc_entry->addend;
349   inplace_address = (bfd_byte *) data + reloc_entry->address;
350   insn = bfd_get_32 (input_bfd, inplace_address);
351 
352   if (reloc_entry->howto->pc_relative)
353     {
354       relocation -= input_section->output_section->vma
355 	+ input_section->output_offset;
356       relocation -= reloc_entry->address;
357     }
358 
359   switch (reloc_entry->howto->type)
360     {
361     case R_VISIUM_PC16:
362       relocation >>= 2;
363       if (ret == bfd_reloc_ok && (relocation & signmask) != 0
364 	  && (relocation & signmask) != signmask)
365 	ret = bfd_reloc_overflow;
366       relocation &= 0xffff;
367       break;
368     case R_VISIUM_HI16:
369     case R_VISIUM_HI16_PCREL:
370       relocation = (relocation >> 16) & 0xffff;
371       break;
372     case R_VISIUM_LO16:
373     case R_VISIUM_LO16_PCREL:
374       relocation &= 0xffff;
375       break;
376     case R_VISIUM_IM16:
377     case R_VISIUM_IM16_PCREL:
378       if (ret == bfd_reloc_ok && (relocation & 0xffff0000) != 0)
379 	ret = bfd_reloc_overflow;
380       relocation &= 0xffff;
381       break;
382     }
383   insn = (insn & 0x7fff0000) | relocation;
384   insn |= visium_parity_bit (insn);
385   bfd_put_32 (input_bfd, insn, inplace_address);
386 
387   if (output_bfd != (bfd *) NULL)
388     reloc_entry->address += input_section->output_offset;
389 
390   return ret;
391 }
392 
393 static reloc_howto_type *
394 visium_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
395 			  bfd_reloc_code_real_type code)
396 {
397   /* Note that the visium_elf_howto_table is indexed by the R_
398      constants. Thus, the order that the howto records appear in the
399      table *must* match the order of the relocation types defined in
400      include/elf/visium.h.  */
401   switch (code)
402     {
403     case BFD_RELOC_NONE:
404       return &visium_elf_howto_table[(int) R_VISIUM_NONE];
405     case BFD_RELOC_8:
406       return &visium_elf_howto_table[(int) R_VISIUM_8];
407     case BFD_RELOC_16:
408       return &visium_elf_howto_table[(int) R_VISIUM_16];
409     case BFD_RELOC_32:
410       return &visium_elf_howto_table[(int) R_VISIUM_32];
411     case BFD_RELOC_8_PCREL:
412       return &visium_elf_howto_table[(int) R_VISIUM_8_PCREL];
413     case BFD_RELOC_16_PCREL:
414       return &visium_elf_howto_table[(int) R_VISIUM_16_PCREL];
415     case BFD_RELOC_32_PCREL:
416       return &visium_elf_howto_table[(int) R_VISIUM_32_PCREL];
417     case BFD_RELOC_VISIUM_REL16:
418       return &visium_elf_howto_table[(int) R_VISIUM_PC16];
419     case BFD_RELOC_VISIUM_HI16:
420       return &visium_elf_howto_table[(int) R_VISIUM_HI16];
421     case BFD_RELOC_VISIUM_LO16:
422       return &visium_elf_howto_table[(int) R_VISIUM_LO16];
423     case BFD_RELOC_VISIUM_IM16:
424       return &visium_elf_howto_table[(int) R_VISIUM_IM16];
425     case BFD_RELOC_VISIUM_HI16_PCREL:
426       return &visium_elf_howto_table[(int) R_VISIUM_HI16_PCREL];
427     case BFD_RELOC_VISIUM_LO16_PCREL:
428       return &visium_elf_howto_table[(int) R_VISIUM_LO16_PCREL];
429     case BFD_RELOC_VISIUM_IM16_PCREL:
430       return &visium_elf_howto_table[(int) R_VISIUM_IM16_PCREL];
431     case BFD_RELOC_VTABLE_INHERIT:
432       return &visium_elf_vtinherit_howto;
433     case BFD_RELOC_VTABLE_ENTRY:
434       return &visium_elf_vtentry_howto;
435     default:
436       return NULL;
437     }
438 }
439 
440 static reloc_howto_type *
441 visium_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
442 {
443   unsigned int i;
444 
445   for (i = 0;
446        i < (sizeof (visium_elf_howto_table)
447 	    / sizeof (visium_elf_howto_table[0])); i++)
448     if (visium_elf_howto_table[i].name != NULL
449 	&& strcasecmp (visium_elf_howto_table[i].name, r_name) == 0)
450       return &visium_elf_howto_table[i];
451 
452   if (strcasecmp (visium_elf_vtinherit_howto.name, r_name) == 0)
453     return &visium_elf_vtinherit_howto;
454   if (strcasecmp (visium_elf_vtentry_howto.name, r_name) == 0)
455     return &visium_elf_vtentry_howto;
456 
457   return NULL;
458 }
459 
460 /* Set the howto pointer for a VISIUM ELF reloc.  */
461 
462 static bfd_boolean
463 visium_info_to_howto_rela (bfd *abfd, arelent *cache_ptr,
464 			   Elf_Internal_Rela *dst)
465 {
466   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
467 
468   switch (r_type)
469     {
470     case R_VISIUM_GNU_VTINHERIT:
471       cache_ptr->howto = &visium_elf_vtinherit_howto;
472       break;
473 
474     case R_VISIUM_GNU_VTENTRY:
475       cache_ptr->howto = &visium_elf_vtentry_howto;
476       break;
477 
478     default:
479       if (r_type >= ARRAY_SIZE (visium_elf_howto_table))
480 	{
481 	  /* xgettext:c-format */
482 	  _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
483 			      abfd, r_type);
484 	  bfd_set_error (bfd_error_bad_value);
485 	  return FALSE;
486 	}
487       cache_ptr->howto = &visium_elf_howto_table[r_type];
488       break;
489     }
490   return TRUE;
491 }
492 
493 /* Look through the relocs for a section during the first phase.
494    Since we don't do .gots or .plts, we just need to consider the
495    virtual table relocs for gc.  */
496 
497 static bfd_boolean
498 visium_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
499 			 asection *sec, const Elf_Internal_Rela *relocs)
500 {
501   Elf_Internal_Shdr *symtab_hdr;
502   struct elf_link_hash_entry **sym_hashes;
503   const Elf_Internal_Rela *rel;
504   const Elf_Internal_Rela *rel_end;
505 
506   if (bfd_link_relocatable (info))
507     return TRUE;
508 
509   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
510   sym_hashes = elf_sym_hashes (abfd);
511 
512   rel_end = relocs + sec->reloc_count;
513   for (rel = relocs; rel < rel_end; rel++)
514     {
515       struct elf_link_hash_entry *h;
516       unsigned long r_symndx;
517 
518       r_symndx = ELF32_R_SYM (rel->r_info);
519       if (r_symndx < symtab_hdr->sh_info)
520 	h = NULL;
521       else
522 	{
523 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
524 	  while (h->root.type == bfd_link_hash_indirect
525 		 || h->root.type == bfd_link_hash_warning)
526 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
527 	}
528 
529       switch (ELF32_R_TYPE (rel->r_info))
530 	{
531 	  /* This relocation describes the C++ object vtable hierarchy.
532 	     Reconstruct it for later use during GC.  */
533 	case R_VISIUM_GNU_VTINHERIT:
534 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
535 	    return FALSE;
536 	  break;
537 
538 	  /* This relocation describes which C++ vtable entries are actually
539 	     used.  Record for later use during GC.  */
540 	case R_VISIUM_GNU_VTENTRY:
541 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
542 	    return FALSE;
543 	  break;
544 	}
545     }
546 
547   return TRUE;
548 }
549 
550 /* Relocate a VISIUM ELF section.  */
551 
552 static bfd_boolean
553 visium_elf_relocate_section (bfd *output_bfd,
554 			     struct bfd_link_info *info, bfd *input_bfd,
555 			     asection *input_section, bfd_byte *contents,
556 			     Elf_Internal_Rela *relocs,
557 			     Elf_Internal_Sym *local_syms,
558 			     asection **local_sections)
559 {
560   Elf_Internal_Shdr *symtab_hdr;
561   struct elf_link_hash_entry **sym_hashes;
562   Elf_Internal_Rela *rel;
563   Elf_Internal_Rela *relend;
564 
565   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
566   sym_hashes = elf_sym_hashes (input_bfd);
567   relend = relocs + input_section->reloc_count;
568 
569   for (rel = relocs; rel < relend; rel++)
570     {
571       reloc_howto_type *howto;
572       unsigned long r_symndx;
573       Elf_Internal_Sym *sym;
574       asection *sec;
575       struct elf_link_hash_entry *h;
576       bfd_vma relocation;
577       bfd_reloc_status_type r;
578       const char *name = NULL;
579       int r_type;
580       bfd_vma insn;
581 
582       r_type = ELF32_R_TYPE (rel->r_info);
583 
584       if (r_type == R_VISIUM_GNU_VTINHERIT || r_type == R_VISIUM_GNU_VTENTRY)
585 	continue;
586 
587       r_symndx = ELF32_R_SYM (rel->r_info);
588 
589       howto = visium_elf_howto_table + ELF32_R_TYPE (rel->r_info);
590       h = NULL;
591       sym = NULL;
592       sec = NULL;
593 
594       if (r_symndx < symtab_hdr->sh_info)
595 	{
596 	  /* This is a local symbol.  */
597 	  sym = local_syms + r_symndx;
598 	  sec = local_sections[r_symndx];
599 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
600 
601 	  name = bfd_elf_string_from_elf_section
602 	    (input_bfd, symtab_hdr->sh_link, sym->st_name);
603 	  name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
604 	}
605       else
606 	{
607 	  bfd_boolean unresolved_reloc;
608 	  bfd_boolean warned, ignored;
609 
610 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
611 				   r_symndx, symtab_hdr, sym_hashes,
612 				   h, sec, relocation,
613 				   unresolved_reloc, warned, ignored);
614 
615 	  name = h->root.root.string;
616 	}
617 
618       if (sec != NULL && discarded_section (sec))
619 	{
620 	  /* For relocs against symbols from removed linkonce sections,
621 	     or sections discarded by a linker script, we just want the
622 	     section contents zeroed.  Avoid any special processing.  */
623 	  _bfd_clear_contents (howto, input_bfd, input_section,
624 			       contents + rel->r_offset);
625 
626 	  rel->r_info = 0;
627 	  rel->r_addend = 0;
628 	  continue;
629 	}
630 
631       if (bfd_link_relocatable (info))
632 	continue;
633 
634       switch (r_type)
635 	{
636 	case R_VISIUM_PC16:
637 	case R_VISIUM_HI16:
638 	case R_VISIUM_LO16:
639 	case R_VISIUM_IM16:
640 	case R_VISIUM_HI16_PCREL:
641 	case R_VISIUM_LO16_PCREL:
642 	case R_VISIUM_IM16_PCREL:
643 	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
644 					contents, rel->r_offset,
645 					relocation, rel->r_addend);
646 
647 	  /* For instruction relocations, the parity needs correcting.  */
648 	  if (r == bfd_reloc_ok)
649 	    {
650 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
651 	      insn = (insn & 0x7fffffff) | visium_parity_bit (insn);
652 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
653 	    }
654 	  break;
655 
656 	default:
657 	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
658 					contents, rel->r_offset,
659 					relocation, rel->r_addend);
660 	  break;
661 	}
662 
663       if (r != bfd_reloc_ok)
664 	{
665 	  const char *msg = (const char *) NULL;
666 
667 	  switch (r)
668 	    {
669 	    case bfd_reloc_overflow:
670 	      (*info->callbacks->reloc_overflow)
671 		(info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
672 		 input_bfd, input_section, rel->r_offset);
673 	      break;
674 
675 	    case bfd_reloc_undefined:
676 	      (*info->callbacks->undefined_symbol)
677 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
678 	      break;
679 
680 	    case bfd_reloc_outofrange:
681 	      msg = _("internal error: out of range error");
682 	      break;
683 
684 	    case bfd_reloc_notsupported:
685 	      msg = _("internal error: unsupported relocation error");
686 	      break;
687 
688 	    case bfd_reloc_dangerous:
689 	      msg = _("internal error: dangerous relocation");
690 	      break;
691 
692 	    default:
693 	      msg = _("internal error: unknown error");
694 	      break;
695 	    }
696 
697 	  if (msg)
698 	    (*info->callbacks->warning) (info, msg, name, input_bfd,
699 					 input_section, rel->r_offset);
700 	}
701     }
702 
703   return TRUE;
704 }
705 
706 /* This function is called during section gc to discover the section a
707    to which a particular relocation refers.  Return the section that
708    should be marked against GC for a given relocation.  */
709 
710 static asection *
711 visium_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
712 			 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
713 			 Elf_Internal_Sym *sym)
714 {
715   if (h != NULL)
716     switch (ELF32_R_TYPE (rel->r_info))
717       {
718       case R_VISIUM_GNU_VTINHERIT:
719       case R_VISIUM_GNU_VTENTRY:
720 	return NULL;
721       }
722 
723   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
724 }
725 
726 static void
727 visium_elf_post_process_headers (bfd *abfd,
728 				 struct bfd_link_info *info ATTRIBUTE_UNUSED)
729 {
730   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
731   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_STANDALONE;
732   i_ehdrp->e_ident[EI_ABIVERSION] = 1;
733 }
734 
735 /* Function to set the ELF flag bits.  */
736 
737 static bfd_boolean
738 visium_elf_set_private_flags (bfd *abfd, flagword flags)
739 {
740   elf_elfheader (abfd)->e_flags = flags;
741   elf_flags_init (abfd) = TRUE;
742   return TRUE;
743 }
744 
745 /* Copy backend specific data from one object module to another.  */
746 
747 static bfd_boolean
748 visium_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
749 {
750   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
751       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
752     return TRUE;
753 
754   BFD_ASSERT (!elf_flags_init (obfd)
755 	      || elf_elfheader (obfd)->e_flags ==
756 	      elf_elfheader (ibfd)->e_flags);
757 
758   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
759   elf_flags_init (obfd) = TRUE;
760 
761   /* Copy object attributes.  */
762   _bfd_elf_copy_obj_attributes (ibfd, obfd);
763 
764   return TRUE;
765 }
766 
767 /* Merge backend specific data from an object
768    file to the output object file when linking.  */
769 
770 static bfd_boolean
771 visium_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
772 {
773   bfd *obfd = info->output_bfd;
774   flagword old_flags;
775   flagword new_flags;
776   flagword mismatch;
777   const char *opt_arch = NULL;
778   const char *new_opt_with = NULL;
779   const char *old_opt_with = NULL;
780   const char *with = "with";
781   const char *without = "without";
782   const char *mcm = "mcm";
783   const char *mcm24 = "mcm24";
784   const char *gr6 = "gr6";
785 
786   new_flags = elf_elfheader (ibfd)->e_flags;
787   old_flags = elf_elfheader (obfd)->e_flags;
788 
789   if (!elf_flags_init (obfd))
790     {
791       /* First call, no flags set.  */
792       elf_flags_init (obfd) = TRUE;
793       elf_elfheader (obfd)->e_flags = new_flags;
794     }
795   else
796     {
797       mismatch = (new_flags ^ old_flags)
798 	& (EF_VISIUM_ARCH_MCM | EF_VISIUM_ARCH_MCM24 | EF_VISIUM_ARCH_GR6);
799       if (mismatch & EF_VISIUM_ARCH_GR6)
800 	{
801 	  opt_arch = gr6;
802 	  new_opt_with = new_flags & EF_VISIUM_ARCH_GR6 ? with : without;
803 	  old_opt_with = old_flags & EF_VISIUM_ARCH_GR6 ? with : without;
804 	}
805       else if (mismatch & EF_VISIUM_ARCH_MCM)
806 	{
807 	  opt_arch = mcm;
808 	  new_opt_with = new_flags & EF_VISIUM_ARCH_MCM ? with : without;
809 	  old_opt_with = old_flags & EF_VISIUM_ARCH_MCM ? with : without;
810 	}
811       else if (mismatch & EF_VISIUM_ARCH_MCM24)
812 	{
813 	  opt_arch = mcm24;
814 	  new_opt_with = new_flags & EF_VISIUM_ARCH_MCM24 ? with : without;
815 	  old_opt_with = old_flags & EF_VISIUM_ARCH_MCM24 ? with : without;
816 	}
817 
818       if (mismatch)
819 	_bfd_error_handler
820 	  /* xgettext:c-format */
821 	  (_("%pB: compiled %s -mtune=%s and linked with modules"
822 	     " compiled %s -mtune=%s"),
823 	   ibfd, new_opt_with, opt_arch, old_opt_with, opt_arch);
824     }
825 
826   return TRUE;
827 }
828 
829 static bfd_boolean
830 visium_elf_print_private_bfd_data (bfd *abfd, void *ptr)
831 {
832   FILE *file = (FILE *) ptr;
833   flagword flags;
834 
835   BFD_ASSERT (abfd != NULL && ptr != NULL);
836 
837   /* Print normal ELF private data.  */
838   _bfd_elf_print_private_bfd_data (abfd, ptr);
839 
840   flags = elf_elfheader (abfd)->e_flags;
841   fprintf (file, _("private flags = 0x%lx:"), (long) flags);
842 
843   if (flags & EF_VISIUM_ARCH_GR6)
844     fprintf (file, " -mtune=gr6");
845   else if (flags & EF_VISIUM_ARCH_MCM)
846     fprintf (file, " -mtune=mcm");
847   else if (flags & EF_VISIUM_ARCH_MCM24)
848     fprintf (file, " -mtune=mcm24");
849 
850   fputc ('\n', file);
851   return TRUE;
852 }
853 
854 #define ELF_ARCH		bfd_arch_visium
855 #define ELF_MACHINE_CODE	EM_VISIUM
856 #define ELF_MAXPAGESIZE		1
857 
858 #define TARGET_BIG_SYM		visium_elf32_vec
859 #define TARGET_BIG_NAME		"elf32-visium"
860 
861 #define elf_info_to_howto_rel			NULL
862 #define elf_info_to_howto			visium_info_to_howto_rela
863 #define elf_backend_relocate_section		visium_elf_relocate_section
864 #define elf_backend_gc_mark_hook		visium_elf_gc_mark_hook
865 #define elf_backend_check_relocs		visium_elf_check_relocs
866 #define elf_backend_rela_normal			1
867 
868 #define elf_backend_can_gc_sections		1
869 
870 #define bfd_elf32_bfd_reloc_type_lookup		visium_reloc_type_lookup
871 #define bfd_elf32_bfd_reloc_name_lookup		visium_reloc_name_lookup
872 
873 #define bfd_elf32_bfd_set_private_flags		visium_elf_set_private_flags
874 #define bfd_elf32_bfd_copy_private_bfd_data	visium_elf_copy_private_bfd_data
875 #define bfd_elf32_bfd_merge_private_bfd_data	visium_elf_merge_private_bfd_data
876 #define bfd_elf32_bfd_print_private_bfd_data	visium_elf_print_private_bfd_data
877 #define elf_backend_post_process_headers	visium_elf_post_process_headers
878 
879 #include "elf32-target.h"
880