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