]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-mcore.c
daily update
[thirdparty/binutils-gdb.git] / bfd / elf32-mcore.c
CommitLineData
59d23ada 1/* Motorola MCore specific support for 32-bit ELF
b491616a
AM
2 Copyright 1994, 1995, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
252b5132
RH
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* This file is based on a preliminary RCE ELF ABI. The
22 information may not match the final RCE ELF ABI. */
23
24#include "bfd.h"
25#include "sysdep.h"
26#include "bfdlink.h"
27#include "libbfd.h"
28#include "elf-bfd.h"
29#include "elf/mcore.h"
30#include <assert.h>
31
f0fe0e16 32/* RELA relocs are used here... */
252b5132
RH
33
34static void mcore_elf_howto_init
35 PARAMS ((void));
36static reloc_howto_type * mcore_elf_reloc_type_lookup
37 PARAMS ((bfd *, bfd_reloc_code_real_type));
38static void mcore_elf_info_to_howto
947216bf 39 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
b34976b6 40static bfd_boolean mcore_elf_set_private_flags
252b5132 41 PARAMS ((bfd *, flagword));
b34976b6 42static bfd_boolean mcore_elf_merge_private_bfd_data
252b5132
RH
43 PARAMS ((bfd *, bfd *));
44static bfd_reloc_status_type mcore_elf_unsupported_reloc
45 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
b34976b6 46static bfd_boolean mcore_elf_relocate_section
252b5132
RH
47 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
48 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
917583ad 49static asection * mcore_elf_gc_mark_hook
1e2f5b6e 50 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
917583ad 51 struct elf_link_hash_entry *, Elf_Internal_Sym *));
b34976b6 52static bfd_boolean mcore_elf_gc_sweep_hook
917583ad
NC
53 PARAMS ((bfd *, struct bfd_link_info *, asection *,
54 const Elf_Internal_Rela *));
b34976b6 55static bfd_boolean mcore_elf_check_relocs
917583ad
NC
56 PARAMS ((bfd *, struct bfd_link_info *, asection *,
57 const Elf_Internal_Rela *));
252b5132
RH
58
59static reloc_howto_type * mcore_elf_howto_table [(int) R_MCORE_max];
60
61static reloc_howto_type mcore_elf_howto_raw[] =
62{
63 /* This reloc does nothing. */
64 HOWTO (R_MCORE_NONE, /* type */
65 0, /* rightshift */
66 2, /* size (0 = byte, 1 = short, 2 = long) */
59d23ada 67 32, /* bitsize */
b34976b6 68 FALSE, /* pc_relative */
252b5132
RH
69 0, /* bitpos */
70 complain_overflow_bitfield, /* complain_on_overflow */
59d23ada 71 NULL, /* special_function */
252b5132 72 "R_MCORE_NONE", /* name */
b34976b6 73 FALSE, /* partial_inplace */
252b5132
RH
74 0, /* src_mask */
75 0, /* dst_mask */
b34976b6 76 FALSE), /* pcrel_offset */
252b5132
RH
77
78 /* A standard 32 bit relocation. */
79 HOWTO (R_MCORE_ADDR32, /* type */
80 0, /* rightshift */
81 2, /* size (0 = byte, 1 = short, 2 = long) */
82 32, /* bitsize */
b34976b6 83 FALSE, /* pc_relative */
252b5132
RH
84 0, /* bitpos */
85 complain_overflow_bitfield, /* complain_on_overflow */
86 bfd_elf_generic_reloc, /* special_function */
87 "ADDR32", /* name *//* For compatability with coff/pe port. */
b34976b6 88 FALSE, /* partial_inplace */
252b5132
RH
89 0x0, /* src_mask */
90 0xffffffff, /* dst_mask */
b34976b6 91 FALSE), /* pcrel_offset */
252b5132
RH
92
93 /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
c3668558 94 Should not appear in object files. */
252b5132
RH
95 HOWTO (R_MCORE_PCRELIMM8BY4, /* type */
96 2, /* rightshift */
97 1, /* size (0 = byte, 1 = short, 2 = long) */
98 8, /* bitsize */
b34976b6 99 TRUE, /* pc_relative */
252b5132
RH
100 0, /* bitpos */
101 complain_overflow_bitfield, /* complain_on_overflow */
102 mcore_elf_unsupported_reloc, /* special_function */
103 "R_MCORE_PCRELIMM8BY4",/* name */
b34976b6 104 FALSE, /* partial_inplace */
252b5132
RH
105 0, /* src_mask */
106 0, /* dst_mask */
b34976b6 107 TRUE), /* pcrel_offset */
252b5132 108
c3668558 109 /* bsr/bt/bf/br instructions; 11 bits + 1 zero bit
252b5132
RH
110 Span 2k instructions == 4k bytes.
111 Only useful pieces at the relocated address are the opcode (5 bits) */
112 HOWTO (R_MCORE_PCRELIMM11BY2,/* type */
113 1, /* rightshift */
114 1, /* size (0 = byte, 1 = short, 2 = long) */
115 11, /* bitsize */
b34976b6 116 TRUE, /* pc_relative */
252b5132
RH
117 0, /* bitpos */
118 complain_overflow_signed, /* complain_on_overflow */
119 bfd_elf_generic_reloc, /* special_function */
120 "R_MCORE_PCRELIMM11BY2",/* name */
b34976b6 121 FALSE, /* partial_inplace */
252b5132
RH
122 0x0, /* src_mask */
123 0x7ff, /* dst_mask */
b34976b6 124 TRUE), /* pcrel_offset */
252b5132 125
c3668558 126 /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported. */
252b5132
RH
127 HOWTO (R_MCORE_PCRELIMM4BY2, /* type */
128 1, /* rightshift */
129 1, /* size (0 = byte, 1 = short, 2 = long) */
130 4, /* bitsize */
b34976b6 131 TRUE, /* pc_relative */
252b5132
RH
132 0, /* bitpos */
133 complain_overflow_bitfield, /* complain_on_overflow */
134 mcore_elf_unsupported_reloc,/* special_function */
135 "R_MCORE_PCRELIMM4BY2",/* name */
b34976b6 136 FALSE, /* partial_inplace */
252b5132
RH
137 0, /* src_mask */
138 0, /* dst_mask */
b34976b6 139 TRUE), /* pcrel_offset */
252b5132 140
c3668558 141 /* 32-bit pc-relative. Eventually this will help support PIC code. */
252b5132
RH
142 HOWTO (R_MCORE_PCREL32, /* type */
143 0, /* rightshift */
144 2, /* size (0 = byte, 1 = short, 2 = long) */
145 32, /* bitsize */
b34976b6 146 TRUE, /* pc_relative */
252b5132
RH
147 0, /* bitpos */
148 complain_overflow_bitfield, /* complain_on_overflow */
149 bfd_elf_generic_reloc, /* special_function */
150 "R_MCORE_PCREL32", /* name */
b34976b6 151 FALSE, /* partial_inplace */
252b5132
RH
152 0x0, /* src_mask */
153 0xffffffff, /* dst_mask */
b34976b6 154 TRUE), /* pcrel_offset */
252b5132
RH
155
156 /* Like PCRELIMM11BY2, this relocation indicates that there is a
157 'jsri' at the specified address. There is a separate relocation
c3668558 158 entry for the literal pool entry that it references, but we
252b5132
RH
159 might be able to change the jsri to a bsr if the target turns out
160 to be close enough [even though we won't reclaim the literal pool
161 entry, we'll get some runtime efficiency back]. Note that this
c3668558 162 is a relocation that we are allowed to safely ignore. */
252b5132
RH
163 HOWTO (R_MCORE_PCRELJSR_IMM11BY2,/* type */
164 1, /* rightshift */
165 1, /* size (0 = byte, 1 = short, 2 = long) */
166 11, /* bitsize */
b34976b6 167 TRUE, /* pc_relative */
252b5132
RH
168 0, /* bitpos */
169 complain_overflow_signed, /* complain_on_overflow */
170 bfd_elf_generic_reloc, /* special_function */
171 "R_MCORE_PCRELJSR_IMM11BY2", /* name */
b34976b6 172 FALSE, /* partial_inplace */
252b5132
RH
173 0x0, /* src_mask */
174 0x7ff, /* dst_mask */
b34976b6 175 TRUE), /* pcrel_offset */
c3668558 176
252b5132
RH
177 /* GNU extension to record C++ vtable hierarchy */
178 HOWTO (R_MCORE_GNU_VTINHERIT, /* type */
179 0, /* rightshift */
180 2, /* size (0 = byte, 1 = short, 2 = long) */
181 0, /* bitsize */
b34976b6 182 FALSE, /* pc_relative */
252b5132
RH
183 0, /* bitpos */
184 complain_overflow_dont, /* complain_on_overflow */
185 NULL, /* special_function */
186 "R_MCORE_GNU_VTINHERIT", /* name */
b34976b6 187 FALSE, /* partial_inplace */
252b5132
RH
188 0, /* src_mask */
189 0, /* dst_mask */
b34976b6 190 FALSE), /* pcrel_offset */
252b5132
RH
191
192 /* GNU extension to record C++ vtable member usage */
193 HOWTO (R_MCORE_GNU_VTENTRY, /* type */
194 0, /* rightshift */
195 2, /* size (0 = byte, 1 = short, 2 = long) */
196 0, /* bitsize */
b34976b6 197 FALSE, /* pc_relative */
252b5132
RH
198 0, /* bitpos */
199 complain_overflow_dont,/* complain_on_overflow */
200 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
201 "R_MCORE_GNU_VTENTRY", /* name */
b34976b6 202 FALSE, /* partial_inplace */
252b5132
RH
203 0, /* src_mask */
204 0, /* dst_mask */
b34976b6 205 FALSE), /* pcrel_offset */
c3668558 206
36797d47
NC
207 HOWTO (R_MCORE_RELATIVE, /* type */
208 0, /* rightshift */
209 2, /* size (0 = byte, 1 = short, 2 = long) */
210 32, /* bitsize */
b34976b6 211 FALSE, /* pc_relative */
36797d47
NC
212 0, /* bitpos */
213 complain_overflow_signed, /* complain_on_overflow */
214 NULL, /* special_function */
215 "R_MCORE_RELATIVE", /* name */
b34976b6 216 TRUE, /* partial_inplace */
36797d47
NC
217 0xffffffff, /* src_mask */
218 0xffffffff, /* dst_mask */
b34976b6 219 FALSE) /* pcrel_offset */
252b5132
RH
220};
221
222#ifndef NUM_ELEM
223#define NUM_ELEM(a) (sizeof (a) / sizeof (a)[0])
224#endif
225\f
226/* Initialize the mcore_elf_howto_table, so that linear accesses can be done. */
227static void
228mcore_elf_howto_init ()
229{
230 unsigned int i;
231
232 for (i = NUM_ELEM (mcore_elf_howto_raw); i--;)
233 {
234 unsigned int type;
c3668558 235
252b5132 236 type = mcore_elf_howto_raw[i].type;
c3668558 237
252b5132 238 BFD_ASSERT (type < NUM_ELEM (mcore_elf_howto_table));
c3668558 239
252b5132
RH
240 mcore_elf_howto_table [type] = & mcore_elf_howto_raw [i];
241 }
242}
252b5132
RH
243\f
244static reloc_howto_type *
245mcore_elf_reloc_type_lookup (abfd, code)
5f771d47 246 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
247 bfd_reloc_code_real_type code;
248{
249 enum elf_mcore_reloc_type mcore_reloc = R_MCORE_NONE;
250
251 switch (code)
252 {
253 case BFD_RELOC_NONE: mcore_reloc = R_MCORE_NONE; break;
254 case BFD_RELOC_32: mcore_reloc = R_MCORE_ADDR32; break;
255 case BFD_RELOC_MCORE_PCREL_IMM8BY4: mcore_reloc = R_MCORE_PCRELIMM8BY4; break;
256 case BFD_RELOC_MCORE_PCREL_IMM11BY2: mcore_reloc = R_MCORE_PCRELIMM11BY2; break;
257 case BFD_RELOC_MCORE_PCREL_IMM4BY2: mcore_reloc = R_MCORE_PCRELIMM4BY2; break;
258 case BFD_RELOC_32_PCREL: mcore_reloc = R_MCORE_PCREL32; break;
259 case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2: mcore_reloc = R_MCORE_PCRELJSR_IMM11BY2; break;
260 case BFD_RELOC_VTABLE_INHERIT: mcore_reloc = R_MCORE_GNU_VTINHERIT; break;
261 case BFD_RELOC_VTABLE_ENTRY: mcore_reloc = R_MCORE_GNU_VTENTRY; break;
36797d47 262 case BFD_RELOC_RVA: mcore_reloc = R_MCORE_RELATIVE; break;
252b5132
RH
263 default:
264 return (reloc_howto_type *)NULL;
265 }
266
267 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4]) /* Initialize howto table if needed */
268 mcore_elf_howto_init ();
269
270 return mcore_elf_howto_table [(int) mcore_reloc];
271};
272
273/* Set the howto pointer for a RCE ELF reloc. */
274static void
275mcore_elf_info_to_howto (abfd, cache_ptr, dst)
5f771d47 276 bfd * abfd ATTRIBUTE_UNUSED;
252b5132 277 arelent * cache_ptr;
947216bf 278 Elf_Internal_Rela * dst;
252b5132
RH
279{
280 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4]) /* Initialize howto table if needed */
281 mcore_elf_howto_init ();
282
283 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_MCORE_max);
c3668558 284
252b5132
RH
285 cache_ptr->howto = mcore_elf_howto_table [ELF32_R_TYPE (dst->r_info)];
286}
287
c3668558 288/* Function to set whether a module needs the -mrelocatable bit set. */
b34976b6 289static bfd_boolean
252b5132
RH
290mcore_elf_set_private_flags (abfd, flags)
291 bfd * abfd;
292 flagword flags;
293{
294 BFD_ASSERT (! elf_flags_init (abfd)
295 || elf_elfheader (abfd)->e_flags == flags);
296
297 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
298 elf_flags_init (abfd) = TRUE;
299 return TRUE;
252b5132
RH
300}
301
252b5132
RH
302/* Merge backend specific data from an object file to the output
303 object file when linking. */
b34976b6 304static bfd_boolean
252b5132
RH
305mcore_elf_merge_private_bfd_data (ibfd, obfd)
306 bfd * ibfd;
307 bfd * obfd;
308{
309 flagword old_flags;
310 flagword new_flags;
252b5132
RH
311
312 /* Check if we have the same endianess */
82e51918 313 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
b34976b6 314 return FALSE;
252b5132
RH
315
316 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
317 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 318 return TRUE;
252b5132
RH
319
320 new_flags = elf_elfheader (ibfd)->e_flags;
321 old_flags = elf_elfheader (obfd)->e_flags;
c3668558 322
252b5132
RH
323 if (! elf_flags_init (obfd)) /* First call, no flags set */
324 {
b34976b6 325 elf_flags_init (obfd) = TRUE;
252b5132
RH
326 elf_elfheader (obfd)->e_flags = new_flags;
327 }
328 else if (new_flags == old_flags) /* Compatible flags are ok */
329 ;
5f771d47
ILT
330 else
331 {
332 /* FIXME */
333 }
334
b34976b6 335 return TRUE;
252b5132 336}
252b5132
RH
337\f
338/* Don't pretend we can deal with unsupported relocs. */
339
252b5132
RH
340static bfd_reloc_status_type
341mcore_elf_unsupported_reloc (abfd, reloc_entry, symbol, data, input_section,
342 output_bfd, error_message)
343 bfd * abfd;
344 arelent * reloc_entry;
5f771d47
ILT
345 asymbol * symbol ATTRIBUTE_UNUSED;
346 PTR data ATTRIBUTE_UNUSED;
347 asection * input_section ATTRIBUTE_UNUSED;
348 bfd * output_bfd ATTRIBUTE_UNUSED;
349 char ** error_message ATTRIBUTE_UNUSED;
252b5132
RH
350{
351 BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
c3668558 352
252b5132 353 _bfd_error_handler (_("%s: Relocation %s (%d) is not currently supported.\n"),
8f615d07 354 bfd_archive_filename (abfd),
252b5132
RH
355 reloc_entry->howto->name,
356 reloc_entry->howto->type);
357
358 return bfd_reloc_notsupported;
359}
252b5132
RH
360\f
361/* The RELOCATE_SECTION function is called by the ELF backend linker
362 to handle the relocations for a section.
363
364 The relocs are always passed as Rela structures; if the section
365 actually uses Rel structures, the r_addend field will always be
366 zero.
367
368 This function is responsible for adjust the section contents as
369 necessary, and (if using Rela relocs and generating a
370 relocateable output file) adjusting the reloc addend as
371 necessary.
372
373 This function does not have to worry about setting the reloc
374 address or the reloc symbol index.
375
376 LOCAL_SYMS is a pointer to the swapped in local symbols.
377
378 LOCAL_SECTIONS is an array giving the section in the input file
379 corresponding to the st_shndx field of each local symbol.
380
381 The global hash table entry for the global symbols can be found
382 via elf_sym_hashes (input_bfd).
383
384 When generating relocateable output, this function must handle
385 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
386 going to be the section symbol corresponding to the output
387 section, which means that the addend must be adjusted
388 accordingly. */
389
b34976b6 390static bfd_boolean
252b5132 391mcore_elf_relocate_section (output_bfd, info, input_bfd, input_section,
b34976b6 392 contents, relocs, local_syms, local_sections)
f8df10f4 393 bfd * output_bfd;
252b5132
RH
394 struct bfd_link_info * info;
395 bfd * input_bfd;
396 asection * input_section;
397 bfd_byte * contents;
398 Elf_Internal_Rela * relocs;
399 Elf_Internal_Sym * local_syms;
400 asection ** local_sections;
401{
b34976b6 402 Elf_Internal_Shdr * symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
252b5132 403 struct elf_link_hash_entry ** sym_hashes = elf_sym_hashes (input_bfd);
b34976b6
AM
404 Elf_Internal_Rela * rel = relocs;
405 Elf_Internal_Rela * relend = relocs + input_section->reloc_count;
406 bfd_boolean ret = TRUE;
252b5132
RH
407
408#ifdef DEBUG
409 fprintf (stderr,
410 "mcore_elf_relocate_section called for %s section %s, %ld relocations%s\n",
8f615d07 411 bfd_archive_filename (input_bfd),
252b5132
RH
412 bfd_section_name(input_bfd, input_section),
413 (long) input_section->reloc_count,
414 (info->relocateable) ? " (relocatable)" : "");
415#endif
416
b491616a 417 if (info->relocateable)
b34976b6 418 return TRUE;
b491616a 419
252b5132
RH
420 if (! mcore_elf_howto_table [R_MCORE_PCRELIMM8BY4]) /* Initialize howto table if needed */
421 mcore_elf_howto_init ();
422
423 for (; rel < relend; rel++)
424 {
425 enum elf_mcore_reloc_type r_type = (enum elf_mcore_reloc_type) ELF32_R_TYPE (rel->r_info);
426 bfd_vma offset = rel->r_offset;
427 bfd_vma addend = rel->r_addend;
428 bfd_reloc_status_type r = bfd_reloc_other;
429 asection * sec = (asection *) 0;
430 reloc_howto_type * howto;
431 bfd_vma relocation;
432 Elf_Internal_Sym * sym = (Elf_Internal_Sym *) 0;
433 unsigned long r_symndx;
434 struct elf_link_hash_entry * h = (struct elf_link_hash_entry *) 0;
86033394 435 unsigned short oldinst = 0;
c3668558 436
252b5132
RH
437 /* Unknown relocation handling */
438 if ((unsigned) r_type >= (unsigned) R_MCORE_max
439 || ! mcore_elf_howto_table [(int)r_type])
440 {
441 _bfd_error_handler (_("%s: Unknown relocation type %d\n"),
8f615d07 442 bfd_archive_filename (input_bfd),
252b5132
RH
443 (int) r_type);
444
445 bfd_set_error (bfd_error_bad_value);
b34976b6 446 ret = FALSE;
252b5132
RH
447 continue;
448 }
c3668558 449
252b5132
RH
450 howto = mcore_elf_howto_table [(int) r_type];
451 r_symndx = ELF32_R_SYM (rel->r_info);
c3668558 452
a022216b 453 /* Complain about known relocation that are not yet supported. */
252b5132
RH
454 if (howto->special_function == mcore_elf_unsupported_reloc)
455 {
456 _bfd_error_handler (_("%s: Relocation %s (%d) is not currently supported.\n"),
8f615d07 457 bfd_archive_filename (input_bfd),
252b5132
RH
458 howto->name,
459 (int)r_type);
460
461 bfd_set_error (bfd_error_bad_value);
b34976b6 462 ret = FALSE;
252b5132
RH
463 continue;
464 }
465
466 if (r_symndx < symtab_hdr->sh_info)
467 {
468 sym = local_syms + r_symndx;
469 sec = local_sections [r_symndx];
f8df10f4
JJ
470 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
471 addend = rel->r_addend;
252b5132
RH
472 }
473 else
474 {
475 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
476 if ( h->root.type == bfd_link_hash_defined
477 || h->root.type == bfd_link_hash_defweak)
478 {
479 sec = h->root.u.def.section;
480 relocation = (h->root.u.def.value
481 + sec->output_section->vma
482 + sec->output_offset);
483 }
484 else if (h->root.type == bfd_link_hash_undefweak)
485 relocation = 0;
3a27a730
L
486 else if (info->shared
487 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
252b5132
RH
488 relocation = 0;
489 else
490 {
491 if (! ((*info->callbacks->undefined_symbol)
492 (info, h->root.root.string, input_bfd,
b34976b6
AM
493 input_section, rel->r_offset, TRUE)))
494 return FALSE;
252b5132 495
b34976b6 496 ret = FALSE;
252b5132
RH
497 continue;
498 }
499 }
500
501 switch (r_type)
502 {
503 default:
252b5132
RH
504 break;
505
506 case R_MCORE_PCRELJSR_IMM11BY2:
507 oldinst = bfd_get_16 (input_bfd, contents + offset);
508#define MCORE_INST_BSR 0xF800
dc810e39 509 bfd_put_16 (input_bfd, (bfd_vma) MCORE_INST_BSR, contents + offset);
252b5132
RH
510 break;
511 }
512
252b5132
RH
513#ifdef DEBUG
514 fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
515 howto->name, r_type, r_symndx, (long) offset, (long) addend);
516#endif
517
518 r = _bfd_final_link_relocate
519 (howto, input_bfd, input_section, contents, offset, relocation, addend);
520
521 if (r != bfd_reloc_ok && r_type == R_MCORE_PCRELJSR_IMM11BY2)
522 {
523 /* Wasn't ok, back it out and give up. */
dc810e39 524 bfd_put_16 (input_bfd, (bfd_vma) oldinst, contents + offset);
252b5132
RH
525 r = bfd_reloc_ok;
526 }
c3668558 527
252b5132
RH
528 if (r != bfd_reloc_ok)
529 {
b34976b6 530 ret = FALSE;
c3668558 531
252b5132
RH
532 switch (r)
533 {
534 default:
535 break;
536
537 case bfd_reloc_overflow:
538 {
539 const char * name;
540
541 if (h != NULL)
542 name = h->root.root.string;
543 else
544 {
545 name = bfd_elf_string_from_elf_section
546 (input_bfd, symtab_hdr->sh_link, sym->st_name);
c3668558 547
252b5132
RH
548 if (name == NULL)
549 break;
550
551 if (* name == '\0')
552 name = bfd_section_name (input_bfd, sec);
553 }
554
555 (*info->callbacks->reloc_overflow)
556 (info, name, howto->name, (bfd_vma) 0, input_bfd, input_section,
557 offset);
558 }
559 break;
560 }
561 }
562 }
563
564#ifdef DEBUG
565 fprintf (stderr, "\n");
566#endif
567
568 return ret;
569}
570\f
571/* Return the section that should be marked against GC for a given
572 relocation. */
573
574static asection *
1e2f5b6e
AM
575mcore_elf_gc_mark_hook (sec, info, rel, h, sym)
576 asection * sec;
5f771d47 577 struct bfd_link_info * info ATTRIBUTE_UNUSED;
252b5132
RH
578 Elf_Internal_Rela * rel;
579 struct elf_link_hash_entry * h;
580 Elf_Internal_Sym * sym;
581{
582 if (h != NULL)
583 {
584 switch (ELF32_R_TYPE (rel->r_info))
585 {
586 case R_MCORE_GNU_VTINHERIT:
587 case R_MCORE_GNU_VTENTRY:
588 break;
589
590 default:
591 switch (h->root.type)
592 {
593 case bfd_link_hash_defined:
594 case bfd_link_hash_defweak:
595 return h->root.u.def.section;
c3668558 596
252b5132
RH
597 case bfd_link_hash_common:
598 return h->root.u.c.p->section;
e049a0de
ILT
599
600 default:
601 break;
252b5132
RH
602 }
603 }
604 }
605 else
1e2f5b6e 606 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
252b5132
RH
607
608 return NULL;
609}
610
611/* Update the got entry reference counts for the section being removed. */
612
b34976b6 613static bfd_boolean
252b5132 614mcore_elf_gc_sweep_hook (abfd, info, sec, relocs)
b34976b6
AM
615 bfd * abfd ATTRIBUTE_UNUSED;
616 struct bfd_link_info * info ATTRIBUTE_UNUSED;
617 asection * sec ATTRIBUTE_UNUSED;
5f771d47 618 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED;
252b5132 619{
b34976b6 620 return TRUE;
252b5132
RH
621}
622
623/* Look through the relocs for a section during the first phase.
624 Since we don't do .gots or .plts, we just need to consider the
625 virtual table relocs for gc. */
c3668558 626
b34976b6 627static bfd_boolean
252b5132
RH
628mcore_elf_check_relocs (abfd, info, sec, relocs)
629 bfd * abfd;
630 struct bfd_link_info * info;
631 asection * sec;
632 const Elf_Internal_Rela * relocs;
633{
b34976b6 634 Elf_Internal_Shdr * symtab_hdr;
252b5132
RH
635 struct elf_link_hash_entry ** sym_hashes;
636 struct elf_link_hash_entry ** sym_hashes_end;
b34976b6
AM
637 const Elf_Internal_Rela * rel;
638 const Elf_Internal_Rela * rel_end;
c3668558 639
252b5132 640 if (info->relocateable)
b34976b6 641 return TRUE;
c3668558 642
252b5132
RH
643 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
644 sym_hashes = elf_sym_hashes (abfd);
645 sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
646 if (!elf_bad_symtab (abfd))
647 sym_hashes_end -= symtab_hdr->sh_info;
c3668558 648
252b5132 649 rel_end = relocs + sec->reloc_count;
c3668558 650
252b5132
RH
651 for (rel = relocs; rel < rel_end; rel++)
652 {
653 struct elf_link_hash_entry * h;
654 unsigned long r_symndx;
c3668558 655
252b5132 656 r_symndx = ELF32_R_SYM (rel->r_info);
c3668558 657
252b5132
RH
658 if (r_symndx < symtab_hdr->sh_info)
659 h = NULL;
660 else
661 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
c3668558 662
252b5132
RH
663 switch (ELF32_R_TYPE (rel->r_info))
664 {
665 /* This relocation describes the C++ object vtable hierarchy.
666 Reconstruct it for later use during GC. */
667 case R_MCORE_GNU_VTINHERIT:
668 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 669 return FALSE;
252b5132 670 break;
c3668558 671
252b5132
RH
672 /* This relocation describes which C++ vtable entries are actually
673 used. Record for later use during GC. */
674 case R_MCORE_GNU_VTENTRY:
675 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 676 return FALSE;
252b5132
RH
677 break;
678 }
679 }
c3668558 680
b34976b6 681 return TRUE;
252b5132
RH
682}
683
684#define TARGET_BIG_SYM bfd_elf32_mcore_big_vec
685#define TARGET_BIG_NAME "elf32-mcore-big"
686#define TARGET_LITTLE_SYM bfd_elf32_mcore_little_vec
687#define TARGET_LITTLE_NAME "elf32-mcore-little"
688
689#define ELF_ARCH bfd_arch_mcore
690#define ELF_MACHINE_CODE EM_MCORE
691#define ELF_MAXPAGESIZE 0x1000 /* 4k, if we ever have 'em */
692#define elf_info_to_howto mcore_elf_info_to_howto
693#define elf_info_to_howto_rel NULL
694
252b5132
RH
695#define bfd_elf32_bfd_merge_private_bfd_data mcore_elf_merge_private_bfd_data
696#define bfd_elf32_bfd_set_private_flags mcore_elf_set_private_flags
697#define bfd_elf32_bfd_reloc_type_lookup mcore_elf_reloc_type_lookup
698#define elf_backend_relocate_section mcore_elf_relocate_section
699#define elf_backend_gc_mark_hook mcore_elf_gc_mark_hook
700#define elf_backend_gc_sweep_hook mcore_elf_gc_sweep_hook
701#define elf_backend_check_relocs mcore_elf_check_relocs
702
703#define elf_backend_can_gc_sections 1
b491616a 704#define elf_backend_rela_normal 1
252b5132
RH
705
706#include "elf32-target.h"