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