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