PR binutils/22875: FRV/ELF: Prevent an out-of-bounds howto table access
[deliverable/binutils-gdb.git] / bfd / elf32-iq2000.c
CommitLineData
a75473eb 1/* IQ2000-specific support for 32-bit ELF.
219d1afa 2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
a75473eb 3
58852c27 4 This file is part of BFD, the Binary File Descriptor library.
a75473eb 5
58852c27
NC
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
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
58852c27 9 (at your option) any later version.
a75473eb 10
58852c27
NC
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.
a75473eb 15
58852c27
NC
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
3e110533 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
a75473eb 19
a75473eb 20#include "sysdep.h"
3db64b00 21#include "bfd.h"
a75473eb
SC
22#include "libbfd.h"
23#include "elf-bfd.h"
24#include "elf/iq2000.h"
25
26/* Forward declarations. */
27
58852c27 28static bfd_reloc_status_type iq2000_elf_howto_hi16_reloc (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
a75473eb
SC
29
30\f
31static reloc_howto_type iq2000_elf_howto_table [] =
32{
33 /* This reloc does nothing. */
34
35 HOWTO (R_IQ2000_NONE, /* type */
36 0, /* rightshift */
6346d5ca
AM
37 3, /* size (0 = byte, 1 = short, 2 = long) */
38 0, /* bitsize */
a75473eb
SC
39 FALSE, /* pc_relative */
40 0, /* bitpos */
6346d5ca 41 complain_overflow_dont, /* complain_on_overflow */
a75473eb
SC
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. */
205static 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. */
221static 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\f
a75473eb 237static bfd_reloc_status_type
58852c27
NC
238iq2000_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)
a75473eb
SC
245{
246 bfd_reloc_status_type ret;
247 bfd_vma relocation;
248
58852c27
NC
249 /* If we're relocating and this an external symbol,
250 we don't want to change anything. */
a75473eb
SC
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
58852c27 268 /* If %lo will have sign-extension, compensate by add 0x10000 to hi portion. */
a75473eb
SC
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
58852c27 276 /* Put it back the way it was. */
a75473eb
SC
277 if (relocation & 0x8000)
278 reloc_entry->addend -= 0x10000;
279
280 return ret;
281}
282
283static bfd_reloc_status_type
58852c27
NC
284iq2000_elf_relocate_hi16 (bfd *input_bfd,
285 Elf_Internal_Rela *relhi,
286 bfd_byte *contents,
287 bfd_vma value)
a75473eb
SC
288{
289 bfd_vma insn;
290
291 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
68ffbac6 292
a75473eb 293 value += relhi->r_addend;
58852c27 294 value &= 0x7fffffff; /* Mask off top-bit which is Harvard mask bit. */
a75473eb 295
58852c27
NC
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. */
a75473eb
SC
298 if (value & 0x8000)
299 value += 0x10000;
300
68ffbac6 301 value >>= 16;
a75473eb
SC
302 insn = ((insn & ~0xFFFF) | value);
303
304 bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
305 return bfd_reloc_ok;
306}
307
f15557e4
DD
308static bfd_reloc_status_type
309iq2000_elf_relocate_offset16 (bfd *input_bfd,
310 Elf_Internal_Rela *rel,
311 bfd_byte *contents,
312 bfd_vma value,
313 bfd_vma location)
314{
315 bfd_vma insn;
316 bfd_vma jtarget;
317
318 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
319
320 value += rel->r_addend;
321
322 if (value & 3)
323 return bfd_reloc_dangerous;
324
325 jtarget = (value & 0x3fffc) | (location & 0xf0000000L);
326
327 if (jtarget != value)
328 return bfd_reloc_overflow;
329
330 insn = (insn & ~0xFFFF) | ((value >> 2) & 0xFFFF);
331
332 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
333 return bfd_reloc_ok;
334}
335
6c408f53
NC
336/* Map BFD reloc types to IQ2000 ELF reloc types. */
337
a75473eb 338static reloc_howto_type *
de52dba4
AM
339iq2000_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
340 bfd_reloc_code_real_type code)
a75473eb
SC
341{
342 /* Note that the iq2000_elf_howto_table is indxed by the R_
343 constants. Thus, the order that the howto records appear in the
344 table *must* match the order of the relocation types defined in
345 include/elf/iq2000.h. */
346
347 switch (code)
348 {
349 case BFD_RELOC_NONE:
350 return &iq2000_elf_howto_table[ (int) R_IQ2000_NONE];
351 case BFD_RELOC_16:
352 return &iq2000_elf_howto_table[ (int) R_IQ2000_16];
353 case BFD_RELOC_32:
354 return &iq2000_elf_howto_table[ (int) R_IQ2000_32];
355 case BFD_RELOC_MIPS_JMP:
356 return &iq2000_elf_howto_table[ (int) R_IQ2000_26];
357 case BFD_RELOC_IQ2000_OFFSET_16:
358 return &iq2000_elf_howto_table[ (int) R_IQ2000_OFFSET_16];
359 case BFD_RELOC_IQ2000_OFFSET_21:
360 return &iq2000_elf_howto_table[ (int) R_IQ2000_OFFSET_21];
361 case BFD_RELOC_16_PCREL_S2:
362 return &iq2000_elf_howto_table[ (int) R_IQ2000_PC16];
363 case BFD_RELOC_HI16:
364 return &iq2000_elf_howto_table[ (int) R_IQ2000_HI16];
365 case BFD_RELOC_IQ2000_UHI16:
366 return &iq2000_elf_howto_table[ (int) R_IQ2000_UHI16];
367 case BFD_RELOC_LO16:
368 return &iq2000_elf_howto_table[ (int) R_IQ2000_LO16];
369 case BFD_RELOC_VTABLE_INHERIT:
370 return &iq2000_elf_vtinherit_howto;
371 case BFD_RELOC_VTABLE_ENTRY:
372 return &iq2000_elf_vtentry_howto;
373 default:
374 /* Pacify gcc -Wall. */
375 return NULL;
376 }
377 return NULL;
378}
379
157090f7
AM
380static reloc_howto_type *
381iq2000_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
382{
383 unsigned int i;
384
385 for (i = 0;
386 i < (sizeof (iq2000_elf_howto_table)
387 / sizeof (iq2000_elf_howto_table[0]));
388 i++)
389 if (iq2000_elf_howto_table[i].name != NULL
390 && strcasecmp (iq2000_elf_howto_table[i].name, r_name) == 0)
391 return &iq2000_elf_howto_table[i];
392
393 if (strcasecmp (iq2000_elf_vtinherit_howto.name, r_name) == 0)
394 return &iq2000_elf_vtinherit_howto;
395 if (strcasecmp (iq2000_elf_vtentry_howto.name, r_name) == 0)
396 return &iq2000_elf_vtentry_howto;
397
398 return NULL;
399}
a75473eb
SC
400\f
401/* Perform a single relocation. By default we use the standard BFD
402 routines. */
403
404static bfd_reloc_status_type
58852c27
NC
405iq2000_final_link_relocate (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)
a75473eb
SC
411{
412 return _bfd_final_link_relocate (howto, input_bfd, input_section,
413 contents, rel->r_offset,
414 relocation, rel->r_addend);
415}
416\f
417/* Set the howto pointer for a IQ2000 ELF reloc. */
418
f3185997 419static bfd_boolean
58852c27
NC
420iq2000_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
421 arelent * cache_ptr,
422 Elf_Internal_Rela * dst)
a75473eb
SC
423{
424 unsigned int r_type;
425
426 r_type = ELF32_R_TYPE (dst->r_info);
427 switch (r_type)
428 {
429 case R_IQ2000_GNU_VTINHERIT:
430 cache_ptr->howto = & iq2000_elf_vtinherit_howto;
431 break;
432
433 case R_IQ2000_GNU_VTENTRY:
434 cache_ptr->howto = & iq2000_elf_vtentry_howto;
435 break;
436
437 default:
5860e3f8
NC
438 if (r_type >= (unsigned int) R_IQ2000_max)
439 {
695344c0 440 /* xgettext:c-format */
0aa13fee
AM
441 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
442 abfd, r_type);
f3185997
NC
443 bfd_set_error (bfd_error_bad_value);
444 return FALSE;
5860e3f8 445 }
a75473eb
SC
446 cache_ptr->howto = & iq2000_elf_howto_table [r_type];
447 break;
448 }
f3185997 449 return TRUE;
a75473eb
SC
450}
451
452/* Look through the relocs for a section during the first phase.
453 Since we don't do .gots or .plts, we just need to consider the
454 virtual table relocs for gc. */
68ffbac6 455
a75473eb 456static bfd_boolean
58852c27
NC
457iq2000_elf_check_relocs (bfd *abfd,
458 struct bfd_link_info *info,
459 asection *sec,
460 const Elf_Internal_Rela *relocs)
a75473eb
SC
461{
462 Elf_Internal_Shdr *symtab_hdr;
5582a088 463 struct elf_link_hash_entry **sym_hashes;
60d8b524
AM
464 const Elf_Internal_Rela *rel;
465 const Elf_Internal_Rela *rel_end;
466 bfd_boolean changed = FALSE;
68ffbac6 467
0e1862bb 468 if (bfd_link_relocatable (info))
a75473eb 469 return TRUE;
68ffbac6 470
a75473eb
SC
471 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
472 sym_hashes = elf_sym_hashes (abfd);
68ffbac6 473
a75473eb
SC
474 rel_end = relocs + sec->reloc_count;
475 for (rel = relocs; rel < rel_end; rel++)
476 {
477 struct elf_link_hash_entry *h;
478 unsigned long r_symndx;
68ffbac6 479
a75473eb
SC
480 r_symndx = ELF32_R_SYM (rel->r_info);
481 if (r_symndx < symtab_hdr->sh_info)
482 h = NULL;
483 else
973a3492
L
484 {
485 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
486 while (h->root.type == bfd_link_hash_indirect
487 || h->root.type == bfd_link_hash_warning)
488 h = (struct elf_link_hash_entry *) h->root.u.i.link;
489 }
68ffbac6 490
a75473eb
SC
491 switch (ELF32_R_TYPE (rel->r_info))
492 {
58852c27
NC
493 /* This relocation describes the C++ object vtable
494 hierarchy. Reconstruct it for later use during GC. */
a75473eb 495 case R_IQ2000_GNU_VTINHERIT:
c152c796 496 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
a75473eb
SC
497 return FALSE;
498 break;
68ffbac6 499
58852c27
NC
500 /* This relocation describes which C++ vtable entries
501 are actually used. Record for later use during GC. */
a75473eb 502 case R_IQ2000_GNU_VTENTRY:
d17e0c6e
JB
503 BFD_ASSERT (h != NULL);
504 if (h != NULL
505 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
a75473eb
SC
506 return FALSE;
507 break;
508
509 case R_IQ2000_32:
58852c27 510 /* For debug section, change to special harvard-aware relocations. */
0112cd26
NC
511 if (CONST_STRNEQ (sec->name, ".debug")
512 || CONST_STRNEQ (sec->name, ".stab")
513 || CONST_STRNEQ (sec->name, ".eh_frame"))
60d8b524
AM
514 {
515 ((Elf_Internal_Rela *) rel)->r_info
516 = ELF32_R_INFO (ELF32_R_SYM (rel->r_info), R_IQ2000_32_DEBUG);
517 changed = TRUE;
518 }
a75473eb
SC
519 break;
520 }
521 }
60d8b524
AM
522
523 if (changed)
524 /* Note that we've changed relocs, otherwise if !info->keep_memory
525 we'll free the relocs and lose our changes. */
d5cb4bb8 526 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
60d8b524 527
a75473eb
SC
528 return TRUE;
529}
530
531\f
532/* Relocate a IQ2000 ELF section.
533 There is some attempt to make this function usable for many architectures,
534 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
535 if only to serve as a learning tool.
536
537 The RELOCATE_SECTION function is called by the new ELF backend linker
538 to handle the relocations for a section.
539
540 The relocs are always passed as Rela structures; if the section
541 actually uses Rel structures, the r_addend field will always be
542 zero.
543
544 This function is responsible for adjusting the section contents as
1049f94e 545 necessary, and (if using Rela relocs and generating a relocatable
a75473eb
SC
546 output file) adjusting the reloc addend as necessary.
547
548 This function does not have to worry about setting the reloc
549 address or the reloc symbol index.
550
551 LOCAL_SYMS is a pointer to the swapped in local symbols.
552
553 LOCAL_SECTIONS is an array giving the section in the input file
554 corresponding to the st_shndx field of each local symbol.
555
556 The global hash table entry for the global symbols can be found
557 via elf_sym_hashes (input_bfd).
558
1049f94e 559 When generating relocatable output, this function must handle
a75473eb
SC
560 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
561 going to be the section symbol corresponding to the output
562 section, which means that the addend must be adjusted
563 accordingly. */
564
565static bfd_boolean
58852c27
NC
566iq2000_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
567 struct bfd_link_info * info,
568 bfd * input_bfd,
569 asection * input_section,
570 bfd_byte * contents,
571 Elf_Internal_Rela * relocs,
572 Elf_Internal_Sym * local_syms,
573 asection ** local_sections)
a75473eb
SC
574{
575 Elf_Internal_Shdr * symtab_hdr;
576 struct elf_link_hash_entry ** sym_hashes;
577 Elf_Internal_Rela * rel;
578 Elf_Internal_Rela * relend;
579
580 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
581 sym_hashes = elf_sym_hashes (input_bfd);
582 relend = relocs + input_section->reloc_count;
583
584 for (rel = relocs; rel < relend; rel ++)
585 {
586 reloc_howto_type * howto;
587 unsigned long r_symndx;
588 Elf_Internal_Sym * sym;
589 asection * sec;
590 struct elf_link_hash_entry * h;
591 bfd_vma relocation;
592 bfd_reloc_status_type r;
593 const char * name = NULL;
594 int r_type;
68ffbac6 595
a75473eb 596 r_type = ELF32_R_TYPE (rel->r_info);
68ffbac6 597
a75473eb
SC
598 if ( r_type == R_IQ2000_GNU_VTINHERIT
599 || r_type == R_IQ2000_GNU_VTENTRY)
600 continue;
68ffbac6 601
a75473eb
SC
602 r_symndx = ELF32_R_SYM (rel->r_info);
603
a75473eb
SC
604 howto = iq2000_elf_howto_table + ELF32_R_TYPE (rel->r_info);
605 h = NULL;
606 sym = NULL;
607 sec = NULL;
68ffbac6 608
a75473eb
SC
609 if (r_symndx < symtab_hdr->sh_info)
610 {
f15557e4
DD
611 asection *osec;
612
a75473eb 613 sym = local_syms + r_symndx;
f15557e4
DD
614 osec = sec = local_sections [r_symndx];
615 if ((sec->flags & SEC_MERGE)
616 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
617 /* This relocation is relative to a section symbol that is
618 going to be merged. Change it so that it is relative
619 to the merged section symbol. */
620 rel->r_addend = _bfd_elf_rel_local_sym (output_bfd, sym, &sec,
621 rel->r_addend);
622
a75473eb
SC
623 relocation = (sec->output_section->vma
624 + sec->output_offset
625 + sym->st_value);
68ffbac6 626
a75473eb
SC
627 name = bfd_elf_string_from_elf_section
628 (input_bfd, symtab_hdr->sh_link, sym->st_name);
f15557e4 629 name = (name == NULL) ? bfd_section_name (input_bfd, osec) : name;
a75473eb
SC
630 }
631 else
632 {
560e09e9 633 bfd_boolean unresolved_reloc;
62d887d4 634 bfd_boolean warned, ignored;
560e09e9 635
b2a8e766
AM
636 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
637 r_symndx, symtab_hdr, sym_hashes,
638 h, sec, relocation,
62d887d4 639 unresolved_reloc, warned, ignored);
a75473eb
SC
640
641 name = h->root.root.string;
a75473eb
SC
642 }
643
dbaa2011 644 if (sec != NULL && discarded_section (sec))
e4067dbb 645 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 646 rel, 1, relend, howto, 0, contents);
ab96bf03 647
0e1862bb 648 if (bfd_link_relocatable (info))
ab96bf03
AM
649 continue;
650
a75473eb
SC
651 switch (r_type)
652 {
653 case R_IQ2000_HI16:
654 r = iq2000_elf_relocate_hi16 (input_bfd, rel, contents, relocation);
655 break;
656
f15557e4
DD
657 case R_IQ2000_OFFSET_16:
658 r = iq2000_elf_relocate_offset16 (input_bfd, rel, contents, relocation,
659 input_section->output_section->vma
660 + input_section->output_offset
661 + rel->r_offset);
662 break;
663
a75473eb
SC
664 case R_IQ2000_PC16:
665 rel->r_addend -= 4;
666 /* Fall through. */
667
668 default:
669 r = iq2000_final_link_relocate (howto, input_bfd, input_section,
670 contents, rel, relocation);
671 break;
672 }
673
674 if (r != bfd_reloc_ok)
675 {
676 const char * msg = (const char *) NULL;
677
678 switch (r)
679 {
680 case bfd_reloc_overflow:
1a72702b 681 (*info->callbacks->reloc_overflow)
dfeffb9f
L
682 (info, (h ? &h->root : NULL), name, howto->name,
683 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
a75473eb 684 break;
68ffbac6 685
a75473eb 686 case bfd_reloc_undefined:
1a72702b 687 (*info->callbacks->undefined_symbol)
a75473eb
SC
688 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
689 break;
68ffbac6 690
a75473eb
SC
691 case bfd_reloc_outofrange:
692 msg = _("internal error: out of range error");
693 break;
694
695 case bfd_reloc_notsupported:
696 msg = _("internal error: unsupported relocation error");
697 break;
698
699 case bfd_reloc_dangerous:
700 msg = _("internal error: dangerous relocation");
701 break;
702
703 default:
704 msg = _("internal error: unknown error");
705 break;
706 }
707
708 if (msg)
1a72702b
AM
709 (*info->callbacks->warning) (info, msg, name, input_bfd,
710 input_section, rel->r_offset);
a75473eb
SC
711 }
712 }
713
714 return TRUE;
715}
716\f
717
a75473eb
SC
718/* Return the section that should be marked against GC for a given
719 relocation. */
720
721static asection *
07adf181
AM
722iq2000_elf_gc_mark_hook (asection *sec,
723 struct bfd_link_info *info,
724 Elf_Internal_Rela *rel,
725 struct elf_link_hash_entry *h,
726 Elf_Internal_Sym *sym)
a75473eb 727{
07adf181
AM
728 if (h != NULL)
729 switch (ELF32_R_TYPE (rel->r_info))
730 {
731 case R_IQ2000_GNU_VTINHERIT:
732 case R_IQ2000_GNU_VTENTRY:
733 return NULL;
734 }
735
736 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
a75473eb
SC
737}
738
739\f
740/* Return the MACH for an e_flags value. */
741
742static int
58852c27 743elf32_iq2000_machine (bfd *abfd)
a75473eb
SC
744{
745 switch (elf_elfheader (abfd)->e_flags & EF_IQ2000_CPU_MASK)
746 {
58852c27
NC
747 case EF_IQ2000_CPU_IQ10:
748 return bfd_mach_iq10;
a75473eb 749
58852c27
NC
750 case EF_IQ2000_CPU_IQ2000:
751 default:
752 return bfd_mach_iq2000;
753 }
a75473eb
SC
754}
755
756\f
757/* Function to set the ELF flag bits. */
758
759static bfd_boolean
58852c27 760iq2000_elf_set_private_flags (bfd *abfd, flagword flags)
a75473eb
SC
761{
762 elf_elfheader (abfd)->e_flags = flags;
763 elf_flags_init (abfd) = TRUE;
764 return TRUE;
765}
766
58852c27
NC
767/* Merge backend specific data from an object
768 file to the output object file when linking. */
a75473eb
SC
769
770static bfd_boolean
50e03d47 771iq2000_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
a75473eb 772{
50e03d47 773 bfd *obfd = info->output_bfd;
a75473eb
SC
774 flagword old_flags, old_partial;
775 flagword new_flags, new_partial;
776 bfd_boolean error = FALSE;
777 char new_opt[80];
778 char old_opt[80];
779
780 new_opt[0] = old_opt[0] = '\0';
781 new_flags = elf_elfheader (ibfd)->e_flags;
782 old_flags = elf_elfheader (obfd)->e_flags;
783
a75473eb
SC
784 if (!elf_flags_init (obfd))
785 {
786 /* First call, no flags set. */
787 elf_flags_init (obfd) = TRUE;
788 elf_elfheader (obfd)->e_flags = new_flags;
789 }
790
58852c27 791 else if (new_flags != old_flags)
a75473eb 792 {
58852c27
NC
793 /* Warn if different cpu is used, but allow a
794 specific cpu to override the generic cpu. */
a75473eb
SC
795 new_partial = (new_flags & EF_IQ2000_CPU_MASK);
796 old_partial = (old_flags & EF_IQ2000_CPU_MASK);
a75473eb 797
58852c27 798 if (new_partial != old_partial)
a75473eb
SC
799 {
800 switch (new_partial)
801 {
58852c27
NC
802 case EF_IQ2000_CPU_IQ10:
803 strcat (new_opt, " -m10");
804 break;
805
806 default:
807 case EF_IQ2000_CPU_IQ2000:
808 strcat (new_opt, " -m2000");
809 break;
a75473eb
SC
810 }
811
812 switch (old_partial)
813 {
58852c27
NC
814 case EF_IQ2000_CPU_IQ10:
815 strcat (old_opt, " -m10");
816 break;
817
818 default:
819 case EF_IQ2000_CPU_IQ2000:
820 strcat (old_opt, " -m2000");
821 break;
a75473eb
SC
822 }
823 }
68ffbac6 824
a75473eb
SC
825 /* Print out any mismatches from above. */
826 if (new_opt[0])
827 {
828 error = TRUE;
58852c27 829 _bfd_error_handler
695344c0 830 /* xgettext:c-format */
871b3ab2 831 (_("%pB: compiled with %s and linked with modules compiled with %s"),
dae82561 832 ibfd, new_opt, old_opt);
a75473eb
SC
833 }
834
835 new_flags &= ~ EF_IQ2000_ALL_FLAGS;
836 old_flags &= ~ EF_IQ2000_ALL_FLAGS;
837
838 /* Warn about any other mismatches. */
839 if (new_flags != old_flags)
840 {
841 error = TRUE;
58852c27
NC
842
843 _bfd_error_handler
695344c0 844 /* xgettext:c-format */
871b3ab2 845 (_("%pB: uses different e_flags (%#x) fields than previous modules (%#x)"),
d42c267e 846 ibfd, new_flags, old_flags);
a75473eb
SC
847 }
848 }
849
850 if (error)
851 bfd_set_error (bfd_error_bad_value);
852
853 return !error;
854}
855
856\f
857static bfd_boolean
58852c27 858iq2000_elf_print_private_bfd_data (bfd *abfd, void * ptr)
a75473eb
SC
859{
860 FILE *file = (FILE *) ptr;
861 flagword flags;
862
863 BFD_ASSERT (abfd != NULL && ptr != NULL);
864
865 /* Print normal ELF private data. */
866 _bfd_elf_print_private_bfd_data (abfd, ptr);
867
868 flags = elf_elfheader (abfd)->e_flags;
0af1713e 869 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
a75473eb
SC
870
871 switch (flags & EF_IQ2000_CPU_MASK)
872 {
58852c27
NC
873 case EF_IQ2000_CPU_IQ10:
874 fprintf (file, " -m10");
875 break;
876 case EF_IQ2000_CPU_IQ2000:
877 fprintf (file, " -m2000");
878 break;
879 default:
880 break;
a75473eb
SC
881 }
882
883 fputc ('\n', file);
60d8b524 884 return TRUE;
a75473eb
SC
885}
886
887static
888bfd_boolean
58852c27 889iq2000_elf_object_p (bfd *abfd)
a75473eb 890{
a75473eb
SC
891 bfd_default_set_arch_mach (abfd, bfd_arch_iq2000,
892 elf32_iq2000_machine (abfd));
893 return TRUE;
894}
895
896\f
897#define ELF_ARCH bfd_arch_iq2000
898#define ELF_MACHINE_CODE EM_IQ2000
899#define ELF_MAXPAGESIZE 0x1000
900
6d00b590 901#define TARGET_BIG_SYM iq2000_elf32_vec
a75473eb
SC
902#define TARGET_BIG_NAME "elf32-iq2000"
903
904#define elf_info_to_howto_rel NULL
905#define elf_info_to_howto iq2000_info_to_howto_rela
906#define elf_backend_relocate_section iq2000_elf_relocate_section
907#define elf_backend_gc_mark_hook iq2000_elf_gc_mark_hook
a75473eb
SC
908#define elf_backend_check_relocs iq2000_elf_check_relocs
909#define elf_backend_object_p iq2000_elf_object_p
910#define elf_backend_rela_normal 1
911
912#define elf_backend_can_gc_sections 1
913
914#define bfd_elf32_bfd_reloc_type_lookup iq2000_reloc_type_lookup
157090f7 915#define bfd_elf32_bfd_reloc_name_lookup iq2000_reloc_name_lookup
a75473eb 916#define bfd_elf32_bfd_set_private_flags iq2000_elf_set_private_flags
a75473eb
SC
917#define bfd_elf32_bfd_merge_private_bfd_data iq2000_elf_merge_private_bfd_data
918#define bfd_elf32_bfd_print_private_bfd_data iq2000_elf_print_private_bfd_data
919
920#include "elf32-target.h"
This page took 0.783317 seconds and 4 git commands to generate.