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