Commit | Line | Data |
---|---|---|
fbca6ad9 AO |
1 | /* Hitachi SH64-specific support for 32-bit ELF |
2 | Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
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 | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
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. | |
15 | ||
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 | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
19 | ||
20 | #define SH64_ELF | |
21 | ||
22 | #include "bfd.h" | |
23 | #include "sysdep.h" | |
24 | #include "elf-bfd.h" | |
25 | #include "../opcodes/sh64-opc.h" | |
26 | ||
27 | /* Add a suffix for datalabel indirection symbols. It must not match any | |
28 | other symbols; user symbols with or without version or other | |
29 | decoration. It must only be used internally and not emitted by any | |
30 | means. */ | |
31 | #define DATALABEL_SUFFIX " DL" | |
32 | ||
33 | /* Used to hold data for function called through bfd_map_over_sections. */ | |
34 | struct sh64_find_section_vma_data | |
35 | { | |
36 | asection *section; | |
37 | bfd_vma addr; | |
38 | }; | |
39 | ||
40 | static boolean sh64_elf_copy_private_data PARAMS ((bfd *, bfd *)); | |
41 | static boolean sh64_elf_merge_private_data PARAMS ((bfd *, bfd *)); | |
42 | static boolean sh64_elf_fake_sections PARAMS ((bfd *, Elf_Internal_Shdr *, | |
43 | asection *)); | |
44 | static boolean sh64_elf_set_private_flags PARAMS ((bfd *, flagword)); | |
45 | static boolean sh64_elf_set_mach_from_flags PARAMS ((bfd *)); | |
46 | static boolean shmedia_prepare_reloc | |
47 | PARAMS ((struct bfd_link_info *, bfd *, asection *, | |
48 | bfd_byte *, const Elf_Internal_Rela *, bfd_vma *)); | |
49 | static int sh64_elf_get_symbol_type PARAMS ((Elf_Internal_Sym *, int)); | |
50 | static boolean sh64_elf_add_symbol_hook | |
51 | PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *, | |
52 | const char **, flagword *, asection **, bfd_vma *)); | |
53 | static boolean sh64_elf_link_output_symbol_hook | |
54 | PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *, | |
55 | asection *)); | |
56 | static boolean sh64_backend_section_from_shdr | |
90937f86 | 57 | PARAMS ((bfd *, Elf_Internal_Shdr *, const char *)); |
fbca6ad9 AO |
58 | static void sh64_elf_final_write_processing PARAMS ((bfd *, boolean)); |
59 | static boolean sh64_bfd_elf_copy_private_section_data | |
60 | PARAMS ((bfd *, asection *, bfd *, asection *)); | |
518313c3 | 61 | static void sh64_find_section_for_address PARAMS ((bfd *, asection *, PTR)); |
fbca6ad9 AO |
62 | |
63 | /* Let elf32-sh.c handle the "bfd_" definitions, so we only have to | |
64 | intrude with an #ifndef around the function definition. */ | |
65 | #define sh_elf_copy_private_data sh64_elf_copy_private_data | |
66 | #define sh_elf_merge_private_data sh64_elf_merge_private_data | |
67 | #define sh_elf_set_private_flags sh64_elf_set_private_flags | |
68 | /* Typo in elf32-sh.c (and unlinear name). */ | |
69 | #define bfd_elf32_bfd_set_private_flags sh64_elf_set_private_flags | |
70 | #define sh_elf_set_mach_from_flags sh64_elf_set_mach_from_flags | |
71 | ||
72 | #define elf_backend_sign_extend_vma 1 | |
73 | #define elf_backend_fake_sections sh64_elf_fake_sections | |
74 | #define elf_backend_get_symbol_type sh64_elf_get_symbol_type | |
75 | #define elf_backend_add_symbol_hook sh64_elf_add_symbol_hook | |
76 | #define elf_backend_link_output_symbol_hook \ | |
77 | sh64_elf_link_output_symbol_hook | |
78 | #define elf_backend_final_write_processing sh64_elf_final_write_processing | |
79 | #define elf_backend_section_from_shdr sh64_backend_section_from_shdr | |
80 | ||
81 | /* For objcopy, we need to set up sh64_elf_section_data (asection *) from | |
82 | incoming section flags. This is otherwise done in sh64elf.em when | |
83 | linking or tc-sh64.c when assembling. */ | |
84 | #define bfd_elf32_bfd_copy_private_section_data \ | |
85 | sh64_bfd_elf_copy_private_section_data | |
86 | ||
87 | /* This COFF-only function (only compiled with COFF support, making | |
88 | ELF-only chains problematic) returns true early for SH4, so let's just | |
89 | define it true here. */ | |
90 | #define _bfd_sh_align_load_span(a,b,c,d,e,f,g,h,i,j) true | |
91 | ||
fbca6ad9 AO |
92 | #define GOT_BIAS (-((long)-32768)) |
93 | #define INCLUDE_SHMEDIA | |
94 | #include "elf32-sh.c" | |
95 | ||
fbca6ad9 AO |
96 | /* Set the SHF_SH5_ISA32 flag for ISA SHmedia code sections, and pass |
97 | through SHT_SH5_CR_SORTED on a sorted .cranges section. */ | |
98 | ||
99 | boolean | |
100 | sh64_elf_fake_sections (output_bfd, elf_section_hdr, asect) | |
101 | bfd *output_bfd ATTRIBUTE_UNUSED; | |
102 | Elf_Internal_Shdr *elf_section_hdr; | |
103 | asection *asect; | |
104 | { | |
105 | if (sh64_elf_section_data (asect) != NULL) | |
106 | elf_section_hdr->sh_flags | |
107 | |= sh64_elf_section_data (asect)->contents_flags; | |
108 | ||
109 | /* If this section has the SEC_SORT_ENTRIES flag set, it is a sorted | |
110 | .cranges section passing through objcopy. */ | |
111 | if ((bfd_get_section_flags (output_bfd, asect) & SEC_SORT_ENTRIES) != 0 | |
112 | && strcmp (bfd_get_section_name (output_bfd, asect), | |
113 | SH64_CRANGES_SECTION_NAME) == 0) | |
114 | elf_section_hdr->sh_type = SHT_SH5_CR_SORTED; | |
115 | ||
116 | return true; | |
117 | } | |
118 | ||
119 | static boolean | |
120 | sh64_elf_set_mach_from_flags (abfd) | |
121 | bfd *abfd; | |
122 | { | |
123 | flagword flags = elf_elfheader (abfd)->e_flags; | |
124 | asection *cranges; | |
125 | ||
126 | switch (flags & EF_SH_MACH_MASK) | |
127 | { | |
128 | case EF_SH5: | |
129 | /* These are fit to execute on SH5. Just one but keep the switch | |
130 | construct to make additions easy. */ | |
131 | bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh5); | |
132 | break; | |
133 | ||
134 | default: | |
135 | bfd_set_error (bfd_error_wrong_format); | |
136 | return false; | |
137 | } | |
138 | ||
139 | /* We also need to set SEC_DEBUGGING on an incoming .cranges section. | |
140 | We could have used elf_backend_section_flags if it had given us the | |
141 | section name; the bfd_section member in the header argument is not | |
142 | set at the point of the call. FIXME: Find out whether that is by | |
143 | undocumented design or a bug. */ | |
144 | cranges = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME); | |
145 | if (cranges != NULL | |
146 | && ! bfd_set_section_flags (abfd, cranges, | |
147 | bfd_get_section_flags (abfd, cranges) | |
148 | | SEC_DEBUGGING)) | |
149 | return false; | |
150 | ||
151 | return true; | |
152 | } | |
153 | ||
154 | static boolean | |
155 | sh64_elf_copy_private_data (ibfd, obfd) | |
156 | bfd * ibfd; | |
157 | bfd * obfd; | |
158 | { | |
159 | if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
160 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
161 | return true; | |
162 | ||
163 | BFD_ASSERT (!elf_flags_init (obfd) | |
164 | || (elf_elfheader (obfd)->e_flags | |
165 | == elf_elfheader (ibfd)->e_flags)); | |
166 | ||
167 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
168 | return true; | |
169 | } | |
170 | ||
171 | static boolean | |
172 | sh64_elf_merge_private_data (ibfd, obfd) | |
173 | bfd *ibfd; | |
174 | bfd *obfd; | |
175 | { | |
176 | flagword old_flags, new_flags; | |
177 | ||
82e51918 | 178 | if (! _bfd_generic_verify_endian_match (ibfd, obfd)) |
fbca6ad9 AO |
179 | return false; |
180 | ||
181 | if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
182 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
183 | return true; | |
184 | ||
185 | if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd)) | |
186 | { | |
187 | const char *msg; | |
188 | ||
189 | if (bfd_get_arch_size (ibfd) == 32 | |
190 | && bfd_get_arch_size (obfd) == 64) | |
191 | msg = _("%s: compiled as 32-bit object and %s is 64-bit"); | |
192 | else if (bfd_get_arch_size (ibfd) == 64 | |
193 | && bfd_get_arch_size (obfd) == 32) | |
194 | msg = _("%s: compiled as 64-bit object and %s is 32-bit"); | |
195 | else | |
196 | msg = _("%s: object size does not match that of target %s"); | |
197 | ||
198 | (*_bfd_error_handler) (msg, bfd_get_filename (ibfd), | |
199 | bfd_get_filename (obfd)); | |
200 | bfd_set_error (bfd_error_wrong_format); | |
201 | return false; | |
202 | } | |
203 | ||
204 | old_flags = elf_elfheader (obfd)->e_flags; | |
205 | new_flags = elf_elfheader (ibfd)->e_flags; | |
206 | if (! elf_flags_init (obfd)) | |
207 | { | |
208 | /* This happens when ld starts out with a 'blank' output file. */ | |
209 | elf_flags_init (obfd) = true; | |
210 | elf_elfheader (obfd)->e_flags = old_flags = new_flags; | |
211 | } | |
212 | /* We don't allow linking in non-SH64 code. */ | |
213 | else if ((new_flags & EF_SH_MACH_MASK) != EF_SH5) | |
214 | { | |
215 | (*_bfd_error_handler) | |
216 | ("%s: uses non-SH64 instructions while previous modules use SH64 instructions", | |
217 | bfd_get_filename (ibfd)); | |
218 | bfd_set_error (bfd_error_bad_value); | |
219 | return false; | |
220 | } | |
221 | ||
222 | /* I can't think of anything sane other than old_flags being EF_SH5 and | |
223 | that we need to preserve that. */ | |
224 | elf_elfheader (obfd)->e_flags = old_flags; | |
225 | return sh64_elf_set_mach_from_flags (obfd); | |
226 | } | |
227 | ||
228 | /* Handle a SH64-specific section when reading an object file. This | |
229 | is called when elfcode.h finds a section with an unknown type. | |
230 | ||
231 | We only recognize SHT_SH5_CR_SORTED, on the .cranges section. */ | |
232 | ||
233 | boolean | |
234 | sh64_backend_section_from_shdr (abfd, hdr, name) | |
235 | bfd *abfd; | |
236 | Elf_Internal_Shdr *hdr; | |
90937f86 | 237 | const char *name; |
fbca6ad9 AO |
238 | { |
239 | flagword flags = 0; | |
240 | ||
241 | /* We do like MIPS with a bit switch for recognized types, and returning | |
242 | false for a recognized section type with an unexpected name. Right | |
243 | now we only have one recognized type, but that might change. */ | |
244 | switch (hdr->sh_type) | |
245 | { | |
246 | case SHT_SH5_CR_SORTED: | |
247 | if (strcmp (name, SH64_CRANGES_SECTION_NAME) != 0) | |
248 | return false; | |
249 | ||
250 | /* We set the SEC_SORT_ENTRIES flag so it can be passed on to | |
251 | sh64_elf_fake_sections, keeping SHT_SH5_CR_SORTED if this object | |
252 | passes through objcopy. Perhaps it is brittle; the flag can | |
253 | suddenly be used by other BFD parts, but it seems not really used | |
254 | anywhere at the moment. */ | |
255 | flags = SEC_DEBUGGING | SEC_SORT_ENTRIES; | |
256 | break; | |
257 | ||
258 | default: | |
259 | return false; | |
260 | } | |
261 | ||
262 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
263 | return false; | |
264 | ||
265 | if (flags | |
266 | && ! bfd_set_section_flags (abfd, hdr->bfd_section, | |
267 | bfd_get_section_flags (abfd, | |
268 | hdr->bfd_section) | |
269 | | flags)) | |
270 | return false; | |
271 | ||
272 | return true; | |
273 | } | |
274 | ||
275 | /* In contrast to sh64_backend_section_from_shdr, this is called for all | |
276 | sections, but only when copying sections, not when linking or | |
277 | assembling. We need to set up the sh64_elf_section_data (asection *) | |
278 | structure for the SH64 ELF section flags to be copied correctly. */ | |
279 | ||
280 | boolean | |
281 | sh64_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec) | |
282 | bfd *ibfd; | |
283 | asection *isec; | |
284 | bfd *obfd; | |
285 | asection *osec; | |
286 | { | |
287 | struct sh64_section_data *sh64_sec_data; | |
288 | ||
289 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
290 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
291 | return true; | |
292 | ||
293 | if (! _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)) | |
294 | return false; | |
295 | ||
296 | sh64_sec_data = sh64_elf_section_data (isec); | |
297 | if (sh64_sec_data == NULL) | |
298 | { | |
299 | sh64_sec_data = bfd_zmalloc (sizeof (struct sh64_section_data)); | |
300 | ||
301 | if (sh64_sec_data == NULL) | |
302 | return false; | |
303 | ||
304 | sh64_sec_data->contents_flags | |
305 | = (elf_section_data (isec)->this_hdr.sh_flags | |
306 | & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)); | |
307 | ||
308 | sh64_elf_section_data (osec) = sh64_sec_data; | |
309 | } | |
310 | ||
311 | return true; | |
312 | } | |
313 | ||
314 | /* Function to keep SH64 specific file flags. */ | |
315 | ||
316 | static boolean | |
317 | sh64_elf_set_private_flags (abfd, flags) | |
318 | bfd * abfd; | |
319 | flagword flags; | |
320 | { | |
321 | BFD_ASSERT (! elf_flags_init (abfd) | |
322 | || elf_elfheader (abfd)->e_flags == flags); | |
323 | ||
324 | elf_elfheader (abfd)->e_flags = flags; | |
325 | elf_flags_init (abfd) = true; | |
326 | return sh64_elf_set_mach_from_flags (abfd); | |
327 | } | |
328 | ||
329 | /* Called when writing out an object file to decide the type of a symbol. */ | |
330 | ||
331 | static int | |
332 | sh64_elf_get_symbol_type (elf_sym, type) | |
333 | Elf_Internal_Sym * elf_sym; | |
334 | int type; | |
335 | { | |
336 | if (ELF_ST_TYPE (elf_sym->st_info) == STT_DATALABEL) | |
337 | return STT_DATALABEL; | |
338 | ||
339 | return type; | |
340 | } | |
341 | ||
342 | /* Hook called by the linker routine which adds symbols from an object | |
343 | file. We must make indirect symbols for undefined symbols marked with | |
344 | STT_DATALABEL, so relocations passing them will pick up that attribute | |
345 | and neutralize STO_SH5_ISA32 found on the symbol definition. | |
346 | ||
347 | There is a problem, though: We want to fill in the hash-table entry for | |
348 | this symbol and signal to the caller that no further processing is | |
349 | needed. But we don't have the index for this hash-table entry. We | |
350 | rely here on that the current entry is the first hash-entry with NULL, | |
351 | which seems brittle. Also, iterating over the hash-table to find that | |
352 | entry is a linear operation on the number of symbols in this input | |
353 | file, and this function should take constant time, so that's not good | |
354 | too. Only comfort is that DataLabel references should only be found in | |
355 | hand-written assembly code and thus be rare. FIXME: Talk maintainers | |
356 | into adding an option to elf_add_symbol_hook (preferably) for the index | |
357 | or the hash entry, alternatively adding the index to Elf_Internal_Sym | |
358 | (not so good). */ | |
359 | ||
360 | static boolean | |
361 | sh64_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp) | |
362 | bfd *abfd; | |
363 | struct bfd_link_info *info; | |
364 | const Elf_Internal_Sym *sym; | |
365 | const char **namep; | |
366 | flagword *flagsp ATTRIBUTE_UNUSED; | |
367 | asection **secp; | |
368 | bfd_vma *valp; | |
369 | { | |
370 | /* We want to do this for relocatable as well as final linking. */ | |
4ab82700 AM |
371 | if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL |
372 | && info->hash->creator->flavour == bfd_target_elf_flavour) | |
fbca6ad9 AO |
373 | { |
374 | struct elf_link_hash_entry *h; | |
375 | ||
376 | /* For relocateable links, we register the DataLabel sym in its own | |
377 | right, and tweak the name when it's output. Otherwise, we make | |
378 | an indirect symbol of it. */ | |
379 | flagword flags | |
380 | = info->relocateable || info->emitrelocations | |
381 | ? BSF_GLOBAL : BSF_GLOBAL | BSF_INDIRECT; | |
382 | ||
383 | char *dl_name | |
384 | = bfd_malloc (strlen (*namep) + sizeof (DATALABEL_SUFFIX)); | |
385 | struct elf_link_hash_entry ** sym_hash = elf_sym_hashes (abfd); | |
386 | ||
387 | BFD_ASSERT (sym_hash != NULL); | |
388 | ||
389 | /* Allocation may fail. */ | |
390 | if (dl_name == NULL) | |
391 | return false; | |
392 | ||
393 | strcpy (dl_name, *namep); | |
394 | strcat (dl_name, DATALABEL_SUFFIX); | |
395 | ||
396 | h = (struct elf_link_hash_entry *) | |
397 | bfd_link_hash_lookup (info->hash, dl_name, false, false, false); | |
398 | ||
399 | if (h == NULL) | |
400 | { | |
401 | /* No previous datalabel symbol. Make one. */ | |
14a793b2 AM |
402 | struct bfd_link_hash_entry *bh = NULL; |
403 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
404 | ||
fbca6ad9 AO |
405 | if (! _bfd_generic_link_add_one_symbol (info, abfd, dl_name, |
406 | flags, *secp, *valp, | |
407 | *namep, false, | |
14a793b2 | 408 | bed->collect, &bh)) |
fbca6ad9 AO |
409 | { |
410 | free (dl_name); | |
411 | return false; | |
412 | } | |
413 | ||
14a793b2 | 414 | h = (struct elf_link_hash_entry *) bh; |
fbca6ad9 AO |
415 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; |
416 | h->type = STT_DATALABEL; | |
417 | } | |
418 | else | |
419 | /* If a new symbol was created, it holds the allocated name. | |
420 | Otherwise, we don't need it anymore and should deallocate it. */ | |
421 | free (dl_name); | |
422 | ||
423 | if (h->type != STT_DATALABEL | |
424 | || ((info->relocateable || info->emitrelocations) | |
425 | && h->root.type != bfd_link_hash_undefined) | |
426 | || (! info->relocateable && !info->emitrelocations | |
427 | && h->root.type != bfd_link_hash_indirect)) | |
428 | { | |
429 | /* Make sure we don't get confused on invalid input. */ | |
430 | (*_bfd_error_handler) | |
431 | (_("%s: encountered datalabel symbol in input"), | |
432 | bfd_get_filename (abfd)); | |
433 | bfd_set_error (bfd_error_bad_value); | |
434 | return false; | |
435 | } | |
436 | ||
437 | /* Now find the hash-table slot for this entry and fill it in. */ | |
438 | while (*sym_hash != NULL) | |
439 | sym_hash++; | |
440 | *sym_hash = h; | |
441 | ||
442 | /* Signal to caller to skip this symbol - we've handled it. */ | |
443 | *namep = NULL; | |
444 | } | |
445 | ||
446 | return true; | |
447 | } | |
448 | ||
449 | /* This hook function is called before the linker writes out a global | |
450 | symbol. For relocatable links, DataLabel symbols will be present in | |
451 | linker output. We cut off the special suffix on those symbols, so the | |
452 | right name appears in the output. | |
453 | ||
454 | When linking and emitting relocations, there can appear global symbols | |
455 | that are not referenced by relocs, but rather only implicitly through | |
456 | DataLabel references, a relation that is not visible to the linker. | |
457 | Since no stripping of global symbols in done when doing such linking, | |
458 | we don't need to look up and make sure to emit the main symbol for each | |
459 | DataLabel symbol. */ | |
460 | ||
461 | boolean | |
462 | sh64_elf_link_output_symbol_hook (abfd, info, cname, sym, input_sec) | |
463 | bfd *abfd ATTRIBUTE_UNUSED; | |
464 | struct bfd_link_info *info; | |
465 | const char *cname; | |
466 | Elf_Internal_Sym *sym; | |
467 | asection *input_sec ATTRIBUTE_UNUSED; | |
468 | { | |
469 | char *name = (char *) cname; | |
470 | ||
471 | if (info->relocateable || info->emitrelocations) | |
472 | { | |
473 | if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL) | |
474 | name[strlen (name) - strlen (DATALABEL_SUFFIX)] = 0; | |
475 | } | |
476 | ||
477 | return true; | |
478 | } | |
479 | ||
480 | /* Check a SH64-specific reloc and put the value to relocate to into | |
481 | RELOCATION, ready to pass to _bfd_final_link_relocate. Return FALSE if | |
482 | bad value, TRUE if ok. */ | |
483 | ||
484 | static boolean | |
485 | shmedia_prepare_reloc (info, abfd, input_section, | |
486 | contents, rel, relocation) | |
487 | struct bfd_link_info *info; | |
488 | bfd *abfd; | |
489 | asection *input_section; | |
490 | bfd_byte *contents; | |
491 | const Elf_Internal_Rela *rel; | |
492 | bfd_vma *relocation; | |
493 | { | |
494 | bfd_vma disp, dropped; | |
495 | ||
496 | switch (ELF32_R_TYPE (rel->r_info)) | |
497 | { | |
498 | case R_SH_PT_16: | |
499 | /* Check the lowest bit of the destination field. If it is 1, we | |
500 | check the ISA type of the destination (i.e. the low bit of the | |
501 | "relocation" value, and emit an error if the instruction does not | |
502 | match). If it is 0, we change a PTA to PTB. There should never | |
503 | be a PTB that should change to a PTA; that indicates a toolchain | |
504 | error; a mismatch with GAS. */ | |
505 | { | |
506 | char *msg = NULL; | |
507 | bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset); | |
508 | ||
509 | if (insn & (1 << 10)) | |
510 | { | |
511 | /* Check matching insn and ISA (address of target). */ | |
512 | if ((insn & SHMEDIA_PTB_BIT) != 0 | |
513 | && ((*relocation + rel->r_addend) & 1) != 0) | |
514 | msg = _("PTB mismatch: a SHmedia address (bit 0 == 1)"); | |
515 | else if ((insn & SHMEDIA_PTB_BIT) == 0 | |
516 | && ((*relocation + rel->r_addend) & 1) == 0) | |
517 | msg = _("PTA mismatch: a SHcompact address (bit 0 == 0)"); | |
518 | ||
519 | if (msg != NULL | |
520 | && ! ((*info->callbacks->reloc_dangerous) | |
521 | (info, msg, abfd, input_section, | |
522 | rel->r_offset))) | |
523 | return false; | |
524 | } | |
525 | else | |
526 | { | |
527 | /* We shouldn't get here with a PTB insn and a R_SH_PT_16. It | |
528 | means GAS output does not match expectations; a PTA or PTB | |
529 | expressed as such (or a PT found at assembly to be PTB) | |
530 | would match the test above, and PT expansion with an | |
531 | unknown destination (or when relaxing) will get us here. */ | |
532 | if ((insn & SHMEDIA_PTB_BIT) != 0) | |
533 | { | |
534 | (*_bfd_error_handler) | |
535 | (_("%s: GAS error: unexpected PTB insn with R_SH_PT_16"), | |
536 | bfd_get_filename (input_section->owner)); | |
537 | return false; | |
538 | } | |
539 | ||
540 | /* Change the PTA to a PTB, if destination indicates so. */ | |
541 | if (((*relocation + rel->r_addend) & 1) == 0) | |
542 | bfd_put_32 (abfd, insn | SHMEDIA_PTB_BIT, | |
543 | contents + rel->r_offset); | |
544 | } | |
545 | } | |
546 | ||
547 | case R_SH_SHMEDIA_CODE: | |
548 | case R_SH_DIR5U: | |
549 | case R_SH_DIR6S: | |
550 | case R_SH_DIR6U: | |
551 | case R_SH_DIR10S: | |
552 | case R_SH_DIR10SW: | |
553 | case R_SH_DIR10SL: | |
554 | case R_SH_DIR10SQ: | |
555 | case R_SH_IMMS16: | |
556 | case R_SH_IMMU16: | |
557 | case R_SH_IMM_LOW16: | |
558 | case R_SH_IMM_LOW16_PCREL: | |
559 | case R_SH_IMM_MEDLOW16: | |
560 | case R_SH_IMM_MEDLOW16_PCREL: | |
561 | case R_SH_IMM_MEDHI16: | |
562 | case R_SH_IMM_MEDHI16_PCREL: | |
563 | case R_SH_IMM_HI16: | |
564 | case R_SH_IMM_HI16_PCREL: | |
565 | case R_SH_64: | |
566 | case R_SH_64_PCREL: | |
567 | break; | |
568 | ||
569 | default: | |
570 | return false; | |
571 | } | |
572 | ||
573 | disp = (*relocation & 0xf); | |
574 | dropped = 0; | |
575 | switch (ELF32_R_TYPE (rel->r_info)) | |
576 | { | |
577 | case R_SH_DIR10SW: dropped = disp & 1; break; | |
578 | case R_SH_DIR10SL: dropped = disp & 3; break; | |
579 | case R_SH_DIR10SQ: dropped = disp & 7; break; | |
580 | } | |
581 | if (dropped != 0) | |
582 | { | |
583 | (*_bfd_error_handler) | |
584 | (_("%s: error: unaligned relocation type %d at %08x reloc %08x\n"), | |
585 | bfd_get_filename (input_section->owner), ELF32_R_TYPE (rel->r_info), | |
586 | (unsigned)rel->r_offset, (unsigned)relocation); | |
587 | return false; | |
588 | } | |
589 | ||
590 | return true; | |
591 | } | |
592 | ||
593 | /* Helper function to locate the section holding a certain address. This | |
594 | is called via bfd_map_over_sections. */ | |
595 | ||
596 | static void | |
597 | sh64_find_section_for_address (abfd, section, data) | |
598 | bfd *abfd ATTRIBUTE_UNUSED; | |
599 | asection *section; | |
600 | PTR data; | |
601 | { | |
602 | bfd_vma vma; | |
603 | bfd_size_type size; | |
604 | struct sh64_find_section_vma_data *fsec_datap | |
605 | = (struct sh64_find_section_vma_data *) data; | |
606 | ||
607 | /* Return if already found. */ | |
608 | if (fsec_datap->section) | |
609 | return; | |
610 | ||
611 | /* If this section isn't part of the addressable contents, skip it. */ | |
612 | if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0) | |
613 | return; | |
614 | ||
615 | vma = bfd_get_section_vma (abfd, section); | |
616 | if (fsec_datap->addr < vma) | |
617 | return; | |
618 | ||
619 | /* FIXME: section->reloc_done isn't set properly; a generic buglet | |
620 | preventing us from using bfd_get_section_size_after_reloc. */ | |
621 | size | |
622 | = section->_cooked_size ? section->_cooked_size : section->_raw_size; | |
623 | ||
624 | if (fsec_datap->addr >= vma + size) | |
625 | return; | |
626 | ||
627 | fsec_datap->section = section; | |
628 | } | |
629 | ||
630 | /* Make sure to write out the generated entries in the .cranges section | |
631 | when doing partial linking, and set bit 0 on the entry address if it | |
632 | points to SHmedia code and write sorted .cranges entries when writing | |
633 | executables (final linking and objcopy). */ | |
634 | ||
635 | static void | |
636 | sh64_elf_final_write_processing (abfd, linker) | |
637 | bfd * abfd; | |
638 | boolean linker ATTRIBUTE_UNUSED; | |
639 | { | |
640 | bfd_vma ld_generated_cranges_size; | |
641 | asection *cranges | |
642 | = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME); | |
643 | ||
644 | /* If no new .cranges were added, the generic ELF linker parts will | |
645 | write it all out. If not, we need to write them out when doing | |
646 | partial linking. For a final link, we will sort them and write them | |
647 | all out further below. */ | |
648 | if (linker | |
649 | && cranges != NULL | |
650 | && elf_elfheader (abfd)->e_type != ET_EXEC | |
651 | && (ld_generated_cranges_size | |
652 | = sh64_elf_section_data (cranges)->cranges_growth) != 0) | |
653 | { | |
654 | bfd_vma incoming_cranges_size | |
655 | = ((cranges->_cooked_size != 0 | |
656 | ? cranges->_cooked_size : cranges->_raw_size) | |
657 | - ld_generated_cranges_size); | |
658 | ||
659 | if (! bfd_set_section_contents (abfd, cranges, | |
660 | cranges->contents | |
661 | + incoming_cranges_size, | |
662 | cranges->output_offset | |
663 | + incoming_cranges_size, | |
664 | ld_generated_cranges_size)) | |
665 | { | |
666 | bfd_set_error (bfd_error_file_truncated); | |
667 | (*_bfd_error_handler) | |
668 | (_("%s: could not write out added .cranges entries"), | |
669 | bfd_get_filename (abfd)); | |
670 | } | |
671 | } | |
672 | ||
673 | /* Only set entry address bit 0 and sort .cranges when linking to an | |
674 | executable; never with objcopy or strip. */ | |
675 | if (linker && elf_elfheader (abfd)->e_type == ET_EXEC) | |
676 | { | |
677 | struct sh64_find_section_vma_data fsec_data; | |
678 | sh64_elf_crange dummy; | |
679 | ||
680 | /* For a final link, set the low bit of the entry address to | |
681 | reflect whether or not it is a SHmedia address. | |
682 | FIXME: Perhaps we shouldn't do this if the entry address was | |
683 | supplied numerically, but we currently lack the infrastructure to | |
684 | recognize that: The entry symbol, and info whether it is numeric | |
685 | or a symbol name is kept private in the linker. */ | |
686 | fsec_data.addr = elf_elfheader (abfd)->e_entry; | |
687 | fsec_data.section = NULL; | |
688 | ||
689 | bfd_map_over_sections (abfd, sh64_find_section_for_address, | |
690 | (PTR) &fsec_data); | |
691 | if (fsec_data.section | |
692 | && (sh64_get_contents_type (fsec_data.section, | |
693 | elf_elfheader (abfd)->e_entry, | |
694 | &dummy) == CRT_SH5_ISA32)) | |
695 | elf_elfheader (abfd)->e_entry |= 1; | |
696 | ||
697 | /* If we have a .cranges section, sort the entries. */ | |
698 | if (cranges != NULL) | |
699 | { | |
700 | bfd_size_type cranges_size | |
701 | = (cranges->_cooked_size != 0 | |
702 | ? cranges->_cooked_size : cranges->_raw_size); | |
703 | ||
704 | /* We know we always have these in memory at this time. */ | |
705 | BFD_ASSERT (cranges->contents != NULL); | |
706 | ||
707 | /* The .cranges may already have been sorted in the process of | |
708 | finding out the ISA-type of the entry address. If not, we do | |
709 | it here. */ | |
710 | if (elf_section_data (cranges)->this_hdr.sh_type | |
711 | != SHT_SH5_CR_SORTED) | |
712 | { | |
713 | qsort (cranges->contents, cranges_size / SH64_CRANGE_SIZE, | |
714 | SH64_CRANGE_SIZE, | |
715 | bfd_big_endian (cranges->owner) | |
9cba27b2 JT |
716 | ? _bfd_sh64_crange_qsort_cmpb |
717 | : _bfd_sh64_crange_qsort_cmpl); | |
fbca6ad9 AO |
718 | elf_section_data (cranges)->this_hdr.sh_type |
719 | = SHT_SH5_CR_SORTED; | |
720 | } | |
721 | ||
722 | /* We need to write it out in whole as sorted. */ | |
723 | if (! bfd_set_section_contents (abfd, cranges, | |
724 | cranges->contents, | |
725 | cranges->output_offset, | |
726 | cranges_size)) | |
727 | { | |
728 | bfd_set_error (bfd_error_file_truncated); | |
729 | (*_bfd_error_handler) | |
730 | (_("%s: could not write out sorted .cranges entries"), | |
731 | bfd_get_filename (abfd)); | |
732 | } | |
733 | } | |
734 | } | |
735 | } | |
2bc3c89a AM |
736 | |
737 | #undef TARGET_BIG_SYM | |
738 | #define TARGET_BIG_SYM bfd_elf32_sh64_vec | |
739 | #undef TARGET_BIG_NAME | |
740 | #define TARGET_BIG_NAME "elf32-sh64" | |
741 | #undef TARGET_LITTLE_SYM | |
742 | #define TARGET_LITTLE_SYM bfd_elf32_sh64l_vec | |
743 | #undef TARGET_LITTLE_NAME | |
744 | #define TARGET_LITTLE_NAME "elf32-sh64l" | |
745 | ||
746 | #include "elf32-target.h" | |
747 | ||
748 | /* NetBSD support. */ | |
749 | #undef TARGET_BIG_SYM | |
750 | #define TARGET_BIG_SYM bfd_elf32_sh64nbsd_vec | |
751 | #undef TARGET_BIG_NAME | |
752 | #define TARGET_BIG_NAME "elf32-sh64-nbsd" | |
753 | #undef TARGET_LITTLE_SYM | |
754 | #define TARGET_LITTLE_SYM bfd_elf32_sh64lnbsd_vec | |
755 | #undef TARGET_LITTLE_NAME | |
756 | #define TARGET_LITTLE_NAME "elf32-sh64l-nbsd" | |
757 | #undef ELF_MAXPAGESIZE | |
758 | #define ELF_MAXPAGESIZE 0x10000 | |
759 | #undef elf_symbol_leading_char | |
760 | #define elf_symbol_leading_char 0 | |
761 | ||
762 | #define elf32_bed elf32_sh64_nbsd_bed | |
763 | ||
764 | #include "elf32-target.h" | |
765 | ||
766 | #undef elf32_bed | |
767 | ||
768 | /* Linux support. */ | |
769 | #undef TARGET_BIG_SYM | |
770 | #define TARGET_BIG_SYM bfd_elf32_sh64blin_vec | |
771 | #undef TARGET_BIG_NAME | |
772 | #define TARGET_BIG_NAME "elf32-sh64big-linux" | |
773 | #undef TARGET_LITTLE_SYM | |
774 | #define TARGET_LITTLE_SYM bfd_elf32_sh64lin_vec | |
775 | #undef TARGET_LITTLE_NAME | |
776 | #define TARGET_LITTLE_NAME "elf32-sh64-linux" | |
777 | ||
778 | #define elf32_bed elf32_sh64_lin_bed | |
779 | ||
780 | #include "elf32-target.h" | |
781 | ||
782 | #undef elf32_bed |