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