1 /* IQ2000-specific support for 32-bit ELF.
2    Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 
4    This file is part of BFD, the Binary File Descriptor library.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
19 
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24 #include "elf/iq2000.h"
25 
26 /* Forward declarations.  */
27 
28 static bfd_reloc_status_type iq2000_elf_howto_hi16_reloc (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
29 
30 
31 static reloc_howto_type iq2000_elf_howto_table [] =
32 {
33   /* This reloc does nothing.  */
34 
35   HOWTO (R_IQ2000_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_bitfield, /* complain_on_overflow */
42 	 bfd_elf_generic_reloc,	     /* special_function */
43 	 "R_IQ2000_NONE",	     /* name */
44 	 FALSE,			     /* partial_inplace */
45 	 0,			     /* src_mask */
46 	 0,			     /* dst_mask */
47 	 FALSE),		     /* pcrel_offset */
48 
49   /* A 16 bit absolute relocation.  */
50   HOWTO (R_IQ2000_16,		     /* type */
51 	 0,			     /* rightshift */
52 	 1,			     /* size (0 = byte, 1 = short, 2 = long) */
53 	 16,			     /* bitsize */
54 	 FALSE,			     /* pc_relative */
55 	 0,			     /* bitpos */
56 	 complain_overflow_bitfield, /* complain_on_overflow */
57 	 bfd_elf_generic_reloc,	     /* special_function */
58 	 "R_IQ2000_16",		     /* name */
59 	 FALSE,			     /* partial_inplace */
60 	 0x0000,		     /* src_mask */
61 	 0xffff,		     /* dst_mask */
62 	 FALSE),		     /* pcrel_offset */
63 
64   /* A 32 bit absolute relocation.  */
65   HOWTO (R_IQ2000_32,		     /* type */
66 	 0,			     /* rightshift */
67 	 2,			     /* size (0 = byte, 1 = short, 2 = long) */
68 	 31,			     /* bitsize */
69 	 FALSE,			     /* pc_relative */
70 	 0,			     /* bitpos */
71 	 complain_overflow_bitfield, /* complain_on_overflow */
72 	 bfd_elf_generic_reloc,	     /* special_function */
73 	 "R_IQ2000_32",		     /* name */
74 	 FALSE,			     /* partial_inplace */
75 	 0x00000000,		     /* src_mask */
76 	 0x7fffffff,		     /* dst_mask */
77 	 FALSE),		     /* pcrel_offset */
78 
79   /* 26 bit branch address.  */
80   HOWTO (R_IQ2000_26,		/* type */
81 	 2,			/* rightshift */
82 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
83 	 26,			/* bitsize */
84 	 FALSE,			/* pc_relative */
85 	 0,			/* bitpos */
86 	 complain_overflow_dont, /* complain_on_overflow */
87 				/* This needs complex overflow
88 				   detection, because the upper four
89 				   bits must match the PC.  */
90 	 bfd_elf_generic_reloc,	/* special_function */
91 	 "R_IQ2000_26",		/* name */
92 	 FALSE,			/* partial_inplace */
93 	 0x00000000,		/* src_mask */
94 	 0x03ffffff,		/* dst_mask */
95 	 FALSE),		/* pcrel_offset */
96 
97   /* 16 bit PC relative reference.  */
98   HOWTO (R_IQ2000_PC16,		/* type */
99 	 2,			/* rightshift */
100 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
101 	 16,			/* bitsize */
102 	 TRUE,			/* pc_relative */
103 	 0,			/* bitpos */
104 	 complain_overflow_signed, /* complain_on_overflow */
105 	 bfd_elf_generic_reloc,	/* special_function */
106 	 "R_IQ2000_PC16",	/* name */
107 	 FALSE,			/* partial_inplace */
108 	 0x0000,		/* src_mask */
109 	 0xffff,		/* dst_mask */
110 	 TRUE),			/* pcrel_offset */
111 
112   /* high 16 bits of symbol value.  */
113   HOWTO (R_IQ2000_HI16,		/* type */
114 	 16,			/* rightshift */
115 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
116 	 15,			/* bitsize */
117 	 FALSE,			/* pc_relative */
118 	 0,			/* bitpos */
119 	 complain_overflow_dont, /* complain_on_overflow */
120 	 iq2000_elf_howto_hi16_reloc,	/* special_function */
121 	 "R_IQ2000_HI16",	/* name */
122 	 FALSE,			/* partial_inplace */
123 	 0x0000,		/* src_mask */
124 	 0x7fff,		/* dst_mask */
125 	 FALSE),		/* pcrel_offset */
126 
127   /* Low 16 bits of symbol value.  */
128   HOWTO (R_IQ2000_LO16,		/* type */
129 	 0,			/* rightshift */
130 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
131 	 16,			/* bitsize */
132 	 FALSE,			/* pc_relative */
133 	 0,			/* bitpos */
134 	 complain_overflow_dont, /* complain_on_overflow */
135 	 bfd_elf_generic_reloc,	/* special_function */
136 	 "R_IQ2000_LO16",	/* name */
137 	 FALSE,			/* partial_inplace */
138 	 0x0000,		/* src_mask */
139 	 0xffff,		/* dst_mask */
140 	 FALSE),		/* pcrel_offset */
141 
142   /* 16-bit jump offset.  */
143   HOWTO (R_IQ2000_OFFSET_16,	/* type */
144 	 2,			/* rightshift */
145 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
146 	 16,			/* bitsize */
147 	 FALSE,			/* pc_relative */
148 	 0,			/* bitpos */
149 	 complain_overflow_dont, /* complain_on_overflow */
150 	 bfd_elf_generic_reloc,	/* special_function */
151 	 "R_IQ2000_OFFSET_16",	/* name */
152 	 FALSE,			/* partial_inplace */
153 	 0x0000,		/* src_mask */
154 	 0xffff,		/* dst_mask */
155 	 FALSE),		/* pcrel_offset */
156 
157   /* 21-bit jump offset.  */
158   HOWTO (R_IQ2000_OFFSET_21,	/* type */
159 	 2,			/* rightshift */
160 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
161 	 21,			/* bitsize */
162 	 FALSE,			/* pc_relative */
163 	 0,			/* bitpos */
164 	 complain_overflow_dont, /* complain_on_overflow */
165 	 bfd_elf_generic_reloc,	/* special_function */
166 	 "R_IQ2000_OFFSET_21",	/* name */
167 	 FALSE,			/* partial_inplace */
168 	 0x000000,		/* src_mask */
169 	 0x1fffff,		/* dst_mask */
170 	 FALSE),		/* pcrel_offset */
171 
172   /* unsigned high 16 bits of value.  */
173   HOWTO (R_IQ2000_OFFSET_21,	/* type */
174 	 16,			/* rightshift */
175 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
176 	 16,			/* bitsize */
177 	 FALSE,			/* pc_relative */
178 	 0,			/* bitpos */
179 	 complain_overflow_dont, /* complain_on_overflow */
180 	 bfd_elf_generic_reloc,	/* special_function */
181 	 "R_IQ2000_UHI16",	/* name */
182 	 FALSE,			/* partial_inplace */
183 	 0x0000,		/* src_mask */
184 	 0x7fff,		/* dst_mask */
185 	 FALSE),		/* pcrel_offset */
186 
187   /* A 32 bit absolute debug relocation.  */
188   HOWTO (R_IQ2000_32_DEBUG,	     /* type */
189 	 0,			     /* rightshift */
190 	 2,			     /* size (0 = byte, 1 = short, 2 = long) */
191 	 32,			     /* bitsize */
192 	 FALSE,			     /* pc_relative */
193 	 0,			     /* bitpos */
194 	 complain_overflow_bitfield, /* complain_on_overflow */
195 	 bfd_elf_generic_reloc,	     /* special_function */
196 	 "R_IQ2000_32",		     /* name */
197 	 FALSE,			     /* partial_inplace */
198 	 0x00000000,		     /* src_mask */
199 	 0xffffffff,		     /* dst_mask */
200 	 FALSE),		     /* pcrel_offset */
201 
202 };
203 
204 /* GNU extension to record C++ vtable hierarchy.  */
205 static reloc_howto_type iq2000_elf_vtinherit_howto =
206   HOWTO (R_IQ2000_GNU_VTINHERIT,    /* type */
207 	 0,			   /* rightshift */
208 	 2,			   /* size (0 = byte, 1 = short, 2 = long) */
209 	 0,			   /* bitsize */
210 	 FALSE,			   /* pc_relative */
211 	 0,			   /* bitpos */
212 	 complain_overflow_dont,   /* complain_on_overflow */
213 	 NULL,			   /* special_function */
214 	 "R_IQ2000_GNU_VTINHERIT",  /* name */
215 	 FALSE,			   /* partial_inplace */
216 	 0,			   /* src_mask */
217 	 0,			   /* dst_mask */
218 	 FALSE);		   /* pcrel_offset */
219 
220 /* GNU extension to record C++ vtable member usage.  */
221 static reloc_howto_type iq2000_elf_vtentry_howto =
222   HOWTO (R_IQ2000_GNU_VTENTRY,	   /* type */
223 	 0,			   /* rightshift */
224 	 2,			   /* size (0 = byte, 1 = short, 2 = long) */
225 	 0,			   /* bitsize */
226 	 FALSE,			   /* pc_relative */
227 	 0,			   /* bitpos */
228 	 complain_overflow_dont,   /* complain_on_overflow */
229 	 NULL,			   /* special_function */
230 	 "R_IQ2000_GNU_VTENTRY",    /* name */
231 	 FALSE,			   /* partial_inplace */
232 	 0,			   /* src_mask */
233 	 0,			   /* dst_mask */
234 	 FALSE);		   /* pcrel_offset */
235 
236 
237 static bfd_reloc_status_type
iq2000_elf_howto_hi16_reloc(bfd * abfd ATTRIBUTE_UNUSED,arelent * reloc_entry,asymbol * symbol,void * data,asection * input_section,bfd * output_bfd,char ** error_message ATTRIBUTE_UNUSED)238 iq2000_elf_howto_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
239 			     arelent *reloc_entry,
240 			     asymbol *symbol,
241 			     void * data,
242 			     asection *input_section,
243 			     bfd *output_bfd,
244 			     char **error_message ATTRIBUTE_UNUSED)
245 {
246   bfd_reloc_status_type ret;
247   bfd_vma relocation;
248 
249   /* If we're relocating and this an external symbol,
250      we don't want to change anything.  */
251   if (output_bfd != (bfd *) NULL
252       && (symbol->flags & BSF_SECTION_SYM) == 0
253       && reloc_entry->addend == 0)
254     {
255       reloc_entry->address += input_section->output_offset;
256       return bfd_reloc_ok;
257     }
258 
259   if (bfd_is_com_section (symbol->section))
260     relocation = 0;
261   else
262     relocation = symbol->value;
263 
264   relocation += symbol->section->output_section->vma;
265   relocation += symbol->section->output_offset;
266   relocation += reloc_entry->addend;
267 
268   /* If %lo will have sign-extension, compensate by add 0x10000 to hi portion.  */
269   if (relocation & 0x8000)
270     reloc_entry->addend += 0x10000;
271 
272   /* Now do the reloc in the usual way.	 */
273   ret = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
274 				input_section, output_bfd, error_message);
275 
276   /* Put it back the way it was.  */
277   if (relocation & 0x8000)
278     reloc_entry->addend -= 0x10000;
279 
280   return ret;
281 }
282 
283 static bfd_reloc_status_type
iq2000_elf_relocate_hi16(bfd * input_bfd,Elf_Internal_Rela * relhi,bfd_byte * contents,bfd_vma value)284 iq2000_elf_relocate_hi16 (bfd *input_bfd,
285 			  Elf_Internal_Rela *relhi,
286 			  bfd_byte *contents,
287 			  bfd_vma value)
288 {
289   bfd_vma insn;
290 
291   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
292 
293   value += relhi->r_addend;
294   value &= 0x7fffffff; /* Mask off top-bit which is Harvard mask bit.  */
295 
296   /* If top-bit of %lo value is on, this means that %lo will
297      sign-propagate and so we compensate by adding 1 to %hi value.  */
298   if (value & 0x8000)
299     value += 0x10000;
300 
301   value >>= 16;
302   insn = ((insn & ~0xFFFF) | value);
303 
304   bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
305   return bfd_reloc_ok;
306 }
307 
308 /* Map BFD reloc types to IQ2000 ELF reloc types.  */
309 
310 static reloc_howto_type *
iq2000_reloc_type_lookup(bfd * abfd ATTRIBUTE_UNUSED,bfd_reloc_code_real_type code)311 iq2000_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
312 			  bfd_reloc_code_real_type code)
313 {
314   /* Note that the iq2000_elf_howto_table is indxed by the R_
315      constants.	 Thus, the order that the howto records appear in the
316      table *must* match the order of the relocation types defined in
317      include/elf/iq2000.h.  */
318 
319   switch (code)
320     {
321     case BFD_RELOC_NONE:
322       return &iq2000_elf_howto_table[ (int) R_IQ2000_NONE];
323     case BFD_RELOC_16:
324       return &iq2000_elf_howto_table[ (int) R_IQ2000_16];
325     case BFD_RELOC_32:
326       return &iq2000_elf_howto_table[ (int) R_IQ2000_32];
327     case BFD_RELOC_MIPS_JMP:
328       return &iq2000_elf_howto_table[ (int) R_IQ2000_26];
329     case BFD_RELOC_IQ2000_OFFSET_16:
330       return &iq2000_elf_howto_table[ (int) R_IQ2000_OFFSET_16];
331     case BFD_RELOC_IQ2000_OFFSET_21:
332       return &iq2000_elf_howto_table[ (int) R_IQ2000_OFFSET_21];
333     case BFD_RELOC_16_PCREL_S2:
334       return &iq2000_elf_howto_table[ (int) R_IQ2000_PC16];
335     case BFD_RELOC_HI16:
336       return &iq2000_elf_howto_table[ (int) R_IQ2000_HI16];
337     case BFD_RELOC_IQ2000_UHI16:
338       return &iq2000_elf_howto_table[ (int) R_IQ2000_UHI16];
339     case BFD_RELOC_LO16:
340       return &iq2000_elf_howto_table[ (int) R_IQ2000_LO16];
341     case BFD_RELOC_VTABLE_INHERIT:
342       return &iq2000_elf_vtinherit_howto;
343     case BFD_RELOC_VTABLE_ENTRY:
344       return &iq2000_elf_vtentry_howto;
345     default:
346       /* Pacify gcc -Wall.  */
347       return NULL;
348     }
349   return NULL;
350 }
351 
352 
353 /* Perform a single relocation.	 By default we use the standard BFD
354    routines.  */
355 
356 static bfd_reloc_status_type
iq2000_final_link_relocate(reloc_howto_type * howto,bfd * input_bfd,asection * input_section,bfd_byte * contents,Elf_Internal_Rela * rel,bfd_vma relocation)357 iq2000_final_link_relocate (reloc_howto_type *	howto,
358 			    bfd *		input_bfd,
359 			    asection *		input_section,
360 			    bfd_byte *		contents,
361 			    Elf_Internal_Rela *	rel,
362 			    bfd_vma		relocation)
363 {
364   return _bfd_final_link_relocate (howto, input_bfd, input_section,
365 				   contents, rel->r_offset,
366 				   relocation, rel->r_addend);
367 }
368 
369 /* Set the howto pointer for a IQ2000 ELF reloc.  */
370 
371 static void
iq2000_info_to_howto_rela(bfd * abfd ATTRIBUTE_UNUSED,arelent * cache_ptr,Elf_Internal_Rela * dst)372 iq2000_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
373 			   arelent * cache_ptr,
374 			   Elf_Internal_Rela * dst)
375 {
376   unsigned int r_type;
377 
378   r_type = ELF32_R_TYPE (dst->r_info);
379   switch (r_type)
380     {
381     case R_IQ2000_GNU_VTINHERIT:
382       cache_ptr->howto = & iq2000_elf_vtinherit_howto;
383       break;
384 
385     case R_IQ2000_GNU_VTENTRY:
386       cache_ptr->howto = & iq2000_elf_vtentry_howto;
387       break;
388 
389     default:
390       cache_ptr->howto = & iq2000_elf_howto_table [r_type];
391       break;
392     }
393 }
394 
395 /* Look through the relocs for a section during the first phase.
396    Since we don't do .gots or .plts, we just need to consider the
397    virtual table relocs for gc.	 */
398 
399 static bfd_boolean
iq2000_elf_check_relocs(bfd * abfd,struct bfd_link_info * info,asection * sec,const Elf_Internal_Rela * relocs)400 iq2000_elf_check_relocs (bfd *abfd,
401 			 struct bfd_link_info *info,
402 			 asection *sec,
403 			 const Elf_Internal_Rela *relocs)
404 {
405   Elf_Internal_Shdr *symtab_hdr;
406   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
407   const Elf_Internal_Rela *rel;
408   const Elf_Internal_Rela *rel_end;
409   bfd_boolean changed = FALSE;
410 
411   if (info->relocatable)
412     return TRUE;
413 
414   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
415   sym_hashes = elf_sym_hashes (abfd);
416   sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
417   if (!elf_bad_symtab (abfd))
418     sym_hashes_end -= symtab_hdr->sh_info;
419 
420   rel_end = relocs + sec->reloc_count;
421   for (rel = relocs; rel < rel_end; rel++)
422     {
423       struct elf_link_hash_entry *h;
424       unsigned long r_symndx;
425 
426       r_symndx = ELF32_R_SYM (rel->r_info);
427       if (r_symndx < symtab_hdr->sh_info)
428 	h = NULL;
429       else
430 	{
431 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
432 	  while (h->root.type == bfd_link_hash_indirect
433 		 || h->root.type == bfd_link_hash_warning)
434 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
435 	}
436 
437       switch (ELF32_R_TYPE (rel->r_info))
438 	{
439 	  /* This relocation describes the C++ object vtable
440 	     hierarchy.  Reconstruct it for later use during GC.  */
441 	case R_IQ2000_GNU_VTINHERIT:
442 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
443 	    return FALSE;
444 	  break;
445 
446 	  /* This relocation describes which C++ vtable entries
447 	     are actually used.  Record for later use during GC.  */
448 	case R_IQ2000_GNU_VTENTRY:
449 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
450 	    return FALSE;
451 	  break;
452 
453 	case R_IQ2000_32:
454 	  /* For debug section, change to special harvard-aware relocations.  */
455 	  if (memcmp (sec->name, ".debug", 6) == 0
456 	      || memcmp (sec->name, ".stab", 5) == 0
457 	      || memcmp (sec->name, ".eh_frame", 9) == 0)
458 	    {
459 	      ((Elf_Internal_Rela *) rel)->r_info
460 		= ELF32_R_INFO (ELF32_R_SYM (rel->r_info), R_IQ2000_32_DEBUG);
461 	      changed = TRUE;
462 	    }
463 	  break;
464 	}
465     }
466 
467   if (changed)
468     /* Note that we've changed relocs, otherwise if !info->keep_memory
469        we'll free the relocs and lose our changes.  */
470     elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
471 
472   return TRUE;
473 }
474 
475 
476 /* Relocate a IQ2000 ELF section.
477    There is some attempt to make this function usable for many architectures,
478    both USE_REL and USE_RELA ['twould be nice if such a critter existed],
479    if only to serve as a learning tool.
480 
481    The RELOCATE_SECTION function is called by the new ELF backend linker
482    to handle the relocations for a section.
483 
484    The relocs are always passed as Rela structures; if the section
485    actually uses Rel structures, the r_addend field will always be
486    zero.
487 
488    This function is responsible for adjusting the section contents as
489    necessary, and (if using Rela relocs and generating a relocatable
490    output file) adjusting the reloc addend as necessary.
491 
492    This function does not have to worry about setting the reloc
493    address or the reloc symbol index.
494 
495    LOCAL_SYMS is a pointer to the swapped in local symbols.
496 
497    LOCAL_SECTIONS is an array giving the section in the input file
498    corresponding to the st_shndx field of each local symbol.
499 
500    The global hash table entry for the global symbols can be found
501    via elf_sym_hashes (input_bfd).
502 
503    When generating relocatable output, this function must handle
504    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
505    going to be the section symbol corresponding to the output
506    section, which means that the addend must be adjusted
507    accordingly.	 */
508 
509 static bfd_boolean
iq2000_elf_relocate_section(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info,bfd * input_bfd,asection * input_section,bfd_byte * contents,Elf_Internal_Rela * relocs,Elf_Internal_Sym * local_syms,asection ** local_sections)510 iq2000_elf_relocate_section (bfd *		     output_bfd ATTRIBUTE_UNUSED,
511 			     struct bfd_link_info *  info,
512 			     bfd *		     input_bfd,
513 			     asection *		     input_section,
514 			     bfd_byte *		     contents,
515 			     Elf_Internal_Rela *     relocs,
516 			     Elf_Internal_Sym *	     local_syms,
517 			     asection **	     local_sections)
518 {
519   Elf_Internal_Shdr *		symtab_hdr;
520   struct elf_link_hash_entry ** sym_hashes;
521   Elf_Internal_Rela *		rel;
522   Elf_Internal_Rela *		relend;
523 
524   if (info->relocatable)
525     return TRUE;
526 
527   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
528   sym_hashes = elf_sym_hashes (input_bfd);
529   relend     = relocs + input_section->reloc_count;
530 
531   for (rel = relocs; rel < relend; rel ++)
532     {
533       reloc_howto_type *	   howto;
534       unsigned long		   r_symndx;
535       Elf_Internal_Sym *	   sym;
536       asection *		   sec;
537       struct elf_link_hash_entry * h;
538       bfd_vma			   relocation;
539       bfd_reloc_status_type	   r;
540       const char *		   name = NULL;
541       int			   r_type;
542 
543       r_type = ELF32_R_TYPE (rel->r_info);
544 
545       if (   r_type == R_IQ2000_GNU_VTINHERIT
546 	  || r_type == R_IQ2000_GNU_VTENTRY)
547 	continue;
548 
549       r_symndx = ELF32_R_SYM (rel->r_info);
550 
551       /* This is a final link.	*/
552       howto  = iq2000_elf_howto_table + ELF32_R_TYPE (rel->r_info);
553       h	     = NULL;
554       sym    = NULL;
555       sec    = NULL;
556 
557       if (r_symndx < symtab_hdr->sh_info)
558 	{
559 	  sym = local_syms + r_symndx;
560 	  sec = local_sections [r_symndx];
561 	  relocation = (sec->output_section->vma
562 			+ sec->output_offset
563 			+ sym->st_value);
564 
565 	  name = bfd_elf_string_from_elf_section
566 	    (input_bfd, symtab_hdr->sh_link, sym->st_name);
567 	  name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
568 	}
569       else
570 	{
571 	  bfd_boolean unresolved_reloc;
572 	  bfd_boolean warned;
573 
574 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
575 				   r_symndx, symtab_hdr, sym_hashes,
576 				   h, sec, relocation,
577 				   unresolved_reloc, warned);
578 
579 	  name = h->root.root.string;
580 	}
581 
582       switch (r_type)
583 	{
584 	case R_IQ2000_HI16:
585 	  r = iq2000_elf_relocate_hi16 (input_bfd, rel, contents, relocation);
586 	  break;
587 
588 	case R_IQ2000_PC16:
589 	  rel->r_addend -= 4;
590 	  /* Fall through.  */
591 
592 	default:
593 	  r = iq2000_final_link_relocate (howto, input_bfd, input_section,
594 					 contents, rel, relocation);
595 	  break;
596 	}
597 
598       if (r != bfd_reloc_ok)
599 	{
600 	  const char * msg = (const char *) NULL;
601 
602 	  switch (r)
603 	    {
604 	    case bfd_reloc_overflow:
605 	      r = info->callbacks->reloc_overflow
606 		(info, (h ? &h->root : NULL), name, howto->name,
607 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
608 	      break;
609 
610 	    case bfd_reloc_undefined:
611 	      r = info->callbacks->undefined_symbol
612 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
613 	      break;
614 
615 	    case bfd_reloc_outofrange:
616 	      msg = _("internal error: out of range error");
617 	      break;
618 
619 	    case bfd_reloc_notsupported:
620 	      msg = _("internal error: unsupported relocation error");
621 	      break;
622 
623 	    case bfd_reloc_dangerous:
624 	      msg = _("internal error: dangerous relocation");
625 	      break;
626 
627 	    default:
628 	      msg = _("internal error: unknown error");
629 	      break;
630 	    }
631 
632 	  if (msg)
633 	    r = info->callbacks->warning
634 	      (info, msg, name, input_bfd, input_section, rel->r_offset);
635 
636 	  if (! r)
637 	    return FALSE;
638 	}
639     }
640 
641   return TRUE;
642 }
643 
644 
645 /* Update the got entry reference counts for the section being
646    removed.  */
647 
648 static bfd_boolean
iq2000_elf_gc_sweep_hook(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED,const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)649 iq2000_elf_gc_sweep_hook (bfd *		            abfd ATTRIBUTE_UNUSED,
650 			  struct bfd_link_info *    info ATTRIBUTE_UNUSED,
651 			  asection *		    sec ATTRIBUTE_UNUSED,
652 			  const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)
653 {
654   return TRUE;
655 }
656 
657 /* Return the section that should be marked against GC for a given
658    relocation.	*/
659 
660 static asection *
iq2000_elf_gc_mark_hook(asection * sec,struct bfd_link_info * info ATTRIBUTE_UNUSED,Elf_Internal_Rela * rel,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)661 iq2000_elf_gc_mark_hook (asection *		      sec,
662 			 struct bfd_link_info *	      info ATTRIBUTE_UNUSED,
663 			 Elf_Internal_Rela *	      rel,
664 			 struct elf_link_hash_entry * h,
665 			 Elf_Internal_Sym *	      sym)
666 {
667   if (h == NULL)
668     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
669 
670   switch (ELF32_R_TYPE (rel->r_info))
671     {
672     case R_IQ2000_GNU_VTINHERIT:
673     case R_IQ2000_GNU_VTENTRY:
674       break;
675 
676     default:
677       switch (h->root.type)
678 	{
679 	case bfd_link_hash_defined:
680 	case bfd_link_hash_defweak:
681 	  return h->root.u.def.section;
682 
683 	case bfd_link_hash_common:
684 	  return h->root.u.c.p->section;
685 
686 	default:
687 	  break;
688 	}
689     }
690 
691   return NULL;
692 }
693 
694 
695 /* Return the MACH for an e_flags value.  */
696 
697 static int
elf32_iq2000_machine(bfd * abfd)698 elf32_iq2000_machine (bfd *abfd)
699 {
700   switch (elf_elfheader (abfd)->e_flags & EF_IQ2000_CPU_MASK)
701     {
702     case EF_IQ2000_CPU_IQ10:
703       return bfd_mach_iq10;
704 
705     case EF_IQ2000_CPU_IQ2000:
706     default:
707       return bfd_mach_iq2000;
708     }
709 }
710 
711 
712 /* Function to set the ELF flag bits.  */
713 
714 static bfd_boolean
iq2000_elf_set_private_flags(bfd * abfd,flagword flags)715 iq2000_elf_set_private_flags (bfd *abfd, flagword flags)
716 {
717   elf_elfheader (abfd)->e_flags = flags;
718   elf_flags_init (abfd) = TRUE;
719   return TRUE;
720 }
721 
722 /* Copy backend specific data from one object module to another.  */
723 
724 static bfd_boolean
iq2000_elf_copy_private_bfd_data(bfd * ibfd,bfd * obfd)725 iq2000_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
726 {
727   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
728       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
729     return TRUE;
730 
731   BFD_ASSERT (!elf_flags_init (obfd)
732 	      || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
733 
734   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
735   elf_flags_init (obfd) = TRUE;
736   return TRUE;
737 }
738 
739 /* Merge backend specific data from an object
740    file to the output object file when linking.  */
741 
742 static bfd_boolean
iq2000_elf_merge_private_bfd_data(bfd * ibfd,bfd * obfd)743 iq2000_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
744 {
745   flagword old_flags, old_partial;
746   flagword new_flags, new_partial;
747   bfd_boolean error = FALSE;
748   char new_opt[80];
749   char old_opt[80];
750 
751   new_opt[0] = old_opt[0] = '\0';
752   new_flags = elf_elfheader (ibfd)->e_flags;
753   old_flags = elf_elfheader (obfd)->e_flags;
754 
755   if (!elf_flags_init (obfd))
756     {
757       /* First call, no flags set.  */
758       elf_flags_init (obfd) = TRUE;
759       elf_elfheader (obfd)->e_flags = new_flags;
760     }
761 
762   else if (new_flags != old_flags)
763     {
764       /* Warn if different cpu is used, but allow a
765 	 specific cpu to override the generic cpu.  */
766       new_partial = (new_flags & EF_IQ2000_CPU_MASK);
767       old_partial = (old_flags & EF_IQ2000_CPU_MASK);
768 
769       if (new_partial != old_partial)
770 	{
771 	  switch (new_partial)
772 	    {
773 	    case EF_IQ2000_CPU_IQ10:
774 	      strcat (new_opt, " -m10");
775 	      break;
776 
777 	    default:
778 	    case EF_IQ2000_CPU_IQ2000:
779 	      strcat (new_opt, " -m2000");
780 	      break;
781 	    }
782 
783 	  switch (old_partial)
784 	    {
785 	    case EF_IQ2000_CPU_IQ10:
786 	      strcat (old_opt, " -m10");
787 	      break;
788 
789 	    default:
790 	    case EF_IQ2000_CPU_IQ2000:
791 	      strcat (old_opt, " -m2000");
792 	      break;
793 	    }
794 	}
795 
796       /* Print out any mismatches from above.  */
797       if (new_opt[0])
798 	{
799 	  error = TRUE;
800 	  _bfd_error_handler
801 	    (_("%s: compiled with %s and linked with modules compiled with %s"),
802 	     bfd_get_filename (ibfd), new_opt, old_opt);
803 	}
804 
805       new_flags &= ~ EF_IQ2000_ALL_FLAGS;
806       old_flags &= ~ EF_IQ2000_ALL_FLAGS;
807 
808       /* Warn about any other mismatches.  */
809       if (new_flags != old_flags)
810 	{
811 	  error = TRUE;
812 
813 	  _bfd_error_handler
814 	    (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
815 	     bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
816 	}
817     }
818 
819   if (error)
820     bfd_set_error (bfd_error_bad_value);
821 
822   return !error;
823 }
824 
825 
826 static bfd_boolean
iq2000_elf_print_private_bfd_data(bfd * abfd,void * ptr)827 iq2000_elf_print_private_bfd_data (bfd *abfd, void * ptr)
828 {
829   FILE *file = (FILE *) ptr;
830   flagword flags;
831 
832   BFD_ASSERT (abfd != NULL && ptr != NULL);
833 
834   /* Print normal ELF private data.  */
835   _bfd_elf_print_private_bfd_data (abfd, ptr);
836 
837   flags = elf_elfheader (abfd)->e_flags;
838   fprintf (file, _("private flags = 0x%lx:"), (long)flags);
839 
840   switch (flags & EF_IQ2000_CPU_MASK)
841     {
842     case EF_IQ2000_CPU_IQ10:
843       fprintf (file, " -m10");
844       break;
845     case EF_IQ2000_CPU_IQ2000:
846       fprintf (file, " -m2000");
847       break;
848     default:
849       break;
850     }
851 
852   fputc ('\n', file);
853   return TRUE;
854 }
855 
856 static
857 bfd_boolean
iq2000_elf_object_p(bfd * abfd)858 iq2000_elf_object_p (bfd *abfd)
859 {
860   bfd_default_set_arch_mach (abfd, bfd_arch_iq2000,
861 			     elf32_iq2000_machine (abfd));
862   return TRUE;
863 }
864 
865 
866 #define ELF_ARCH		bfd_arch_iq2000
867 #define ELF_MACHINE_CODE	EM_IQ2000
868 #define ELF_MAXPAGESIZE		0x1000
869 
870 #define TARGET_BIG_SYM		bfd_elf32_iq2000_vec
871 #define TARGET_BIG_NAME		"elf32-iq2000"
872 
873 #define elf_info_to_howto_rel			NULL
874 #define elf_info_to_howto			iq2000_info_to_howto_rela
875 #define elf_backend_relocate_section		iq2000_elf_relocate_section
876 #define elf_backend_gc_mark_hook		iq2000_elf_gc_mark_hook
877 #define elf_backend_gc_sweep_hook		iq2000_elf_gc_sweep_hook
878 #define elf_backend_check_relocs		iq2000_elf_check_relocs
879 #define elf_backend_object_p			iq2000_elf_object_p
880 #define elf_backend_rela_normal			1
881 
882 #define elf_backend_can_gc_sections		1
883 
884 #define bfd_elf32_bfd_reloc_type_lookup		iq2000_reloc_type_lookup
885 #define bfd_elf32_bfd_set_private_flags		iq2000_elf_set_private_flags
886 #define bfd_elf32_bfd_copy_private_bfd_data	iq2000_elf_copy_private_bfd_data
887 #define bfd_elf32_bfd_merge_private_bfd_data	iq2000_elf_merge_private_bfd_data
888 #define bfd_elf32_bfd_print_private_bfd_data	iq2000_elf_print_private_bfd_data
889 
890 #include "elf32-target.h"
891