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 | |
57 | PARAMS ((bfd *, Elf_Internal_Shdr *, char *)); | |
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 | ||
108 | /* The type sh64_elf_crange is defined in elf/sh.h which is included in | |
109 | elf32-sh.c, hence these prototypes located after including it. */ | |
110 | static int crange_qsort_cmpb PARAMS ((const void *, const void *)); | |
111 | static int crange_qsort_cmpl PARAMS ((const void *, const void *)); | |
112 | static int crange_bsearch_cmpb PARAMS ((const void *, const void *)); | |
113 | static int crange_bsearch_cmpl PARAMS ((const void *, const void *)); | |
114 | static boolean sh64_address_in_cranges | |
115 | PARAMS ((asection *cranges, bfd_vma, sh64_elf_crange *)); | |
116 | ||
117 | /* Set the SHF_SH5_ISA32 flag for ISA SHmedia code sections, and pass | |
118 | through SHT_SH5_CR_SORTED on a sorted .cranges section. */ | |
119 | ||
120 | boolean | |
121 | sh64_elf_fake_sections (output_bfd, elf_section_hdr, asect) | |
122 | bfd *output_bfd ATTRIBUTE_UNUSED; | |
123 | Elf_Internal_Shdr *elf_section_hdr; | |
124 | asection *asect; | |
125 | { | |
126 | if (sh64_elf_section_data (asect) != NULL) | |
127 | elf_section_hdr->sh_flags | |
128 | |= sh64_elf_section_data (asect)->contents_flags; | |
129 | ||
130 | /* If this section has the SEC_SORT_ENTRIES flag set, it is a sorted | |
131 | .cranges section passing through objcopy. */ | |
132 | if ((bfd_get_section_flags (output_bfd, asect) & SEC_SORT_ENTRIES) != 0 | |
133 | && strcmp (bfd_get_section_name (output_bfd, asect), | |
134 | SH64_CRANGES_SECTION_NAME) == 0) | |
135 | elf_section_hdr->sh_type = SHT_SH5_CR_SORTED; | |
136 | ||
137 | return true; | |
138 | } | |
139 | ||
140 | static boolean | |
141 | sh64_elf_set_mach_from_flags (abfd) | |
142 | bfd *abfd; | |
143 | { | |
144 | flagword flags = elf_elfheader (abfd)->e_flags; | |
145 | asection *cranges; | |
146 | ||
147 | switch (flags & EF_SH_MACH_MASK) | |
148 | { | |
149 | case EF_SH5: | |
150 | /* These are fit to execute on SH5. Just one but keep the switch | |
151 | construct to make additions easy. */ | |
152 | bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh5); | |
153 | break; | |
154 | ||
155 | default: | |
156 | bfd_set_error (bfd_error_wrong_format); | |
157 | return false; | |
158 | } | |
159 | ||
160 | /* We also need to set SEC_DEBUGGING on an incoming .cranges section. | |
161 | We could have used elf_backend_section_flags if it had given us the | |
162 | section name; the bfd_section member in the header argument is not | |
163 | set at the point of the call. FIXME: Find out whether that is by | |
164 | undocumented design or a bug. */ | |
165 | cranges = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME); | |
166 | if (cranges != NULL | |
167 | && ! bfd_set_section_flags (abfd, cranges, | |
168 | bfd_get_section_flags (abfd, cranges) | |
169 | | SEC_DEBUGGING)) | |
170 | return false; | |
171 | ||
172 | return true; | |
173 | } | |
174 | ||
175 | static boolean | |
176 | sh64_elf_copy_private_data (ibfd, obfd) | |
177 | bfd * ibfd; | |
178 | bfd * obfd; | |
179 | { | |
180 | if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
181 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
182 | return true; | |
183 | ||
184 | BFD_ASSERT (!elf_flags_init (obfd) | |
185 | || (elf_elfheader (obfd)->e_flags | |
186 | == elf_elfheader (ibfd)->e_flags)); | |
187 | ||
188 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
189 | return true; | |
190 | } | |
191 | ||
192 | static boolean | |
193 | sh64_elf_merge_private_data (ibfd, obfd) | |
194 | bfd *ibfd; | |
195 | bfd *obfd; | |
196 | { | |
197 | flagword old_flags, new_flags; | |
198 | ||
199 | if (_bfd_generic_verify_endian_match (ibfd, obfd) == false) | |
200 | return false; | |
201 | ||
202 | if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
203 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
204 | return true; | |
205 | ||
206 | if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd)) | |
207 | { | |
208 | const char *msg; | |
209 | ||
210 | if (bfd_get_arch_size (ibfd) == 32 | |
211 | && bfd_get_arch_size (obfd) == 64) | |
212 | msg = _("%s: compiled as 32-bit object and %s is 64-bit"); | |
213 | else if (bfd_get_arch_size (ibfd) == 64 | |
214 | && bfd_get_arch_size (obfd) == 32) | |
215 | msg = _("%s: compiled as 64-bit object and %s is 32-bit"); | |
216 | else | |
217 | msg = _("%s: object size does not match that of target %s"); | |
218 | ||
219 | (*_bfd_error_handler) (msg, bfd_get_filename (ibfd), | |
220 | bfd_get_filename (obfd)); | |
221 | bfd_set_error (bfd_error_wrong_format); | |
222 | return false; | |
223 | } | |
224 | ||
225 | old_flags = elf_elfheader (obfd)->e_flags; | |
226 | new_flags = elf_elfheader (ibfd)->e_flags; | |
227 | if (! elf_flags_init (obfd)) | |
228 | { | |
229 | /* This happens when ld starts out with a 'blank' output file. */ | |
230 | elf_flags_init (obfd) = true; | |
231 | elf_elfheader (obfd)->e_flags = old_flags = new_flags; | |
232 | } | |
233 | /* We don't allow linking in non-SH64 code. */ | |
234 | else if ((new_flags & EF_SH_MACH_MASK) != EF_SH5) | |
235 | { | |
236 | (*_bfd_error_handler) | |
237 | ("%s: uses non-SH64 instructions while previous modules use SH64 instructions", | |
238 | bfd_get_filename (ibfd)); | |
239 | bfd_set_error (bfd_error_bad_value); | |
240 | return false; | |
241 | } | |
242 | ||
243 | /* I can't think of anything sane other than old_flags being EF_SH5 and | |
244 | that we need to preserve that. */ | |
245 | elf_elfheader (obfd)->e_flags = old_flags; | |
246 | return sh64_elf_set_mach_from_flags (obfd); | |
247 | } | |
248 | ||
249 | /* Handle a SH64-specific section when reading an object file. This | |
250 | is called when elfcode.h finds a section with an unknown type. | |
251 | ||
252 | We only recognize SHT_SH5_CR_SORTED, on the .cranges section. */ | |
253 | ||
254 | boolean | |
255 | sh64_backend_section_from_shdr (abfd, hdr, name) | |
256 | bfd *abfd; | |
257 | Elf_Internal_Shdr *hdr; | |
258 | char *name; | |
259 | { | |
260 | flagword flags = 0; | |
261 | ||
262 | /* We do like MIPS with a bit switch for recognized types, and returning | |
263 | false for a recognized section type with an unexpected name. Right | |
264 | now we only have one recognized type, but that might change. */ | |
265 | switch (hdr->sh_type) | |
266 | { | |
267 | case SHT_SH5_CR_SORTED: | |
268 | if (strcmp (name, SH64_CRANGES_SECTION_NAME) != 0) | |
269 | return false; | |
270 | ||
271 | /* We set the SEC_SORT_ENTRIES flag so it can be passed on to | |
272 | sh64_elf_fake_sections, keeping SHT_SH5_CR_SORTED if this object | |
273 | passes through objcopy. Perhaps it is brittle; the flag can | |
274 | suddenly be used by other BFD parts, but it seems not really used | |
275 | anywhere at the moment. */ | |
276 | flags = SEC_DEBUGGING | SEC_SORT_ENTRIES; | |
277 | break; | |
278 | ||
279 | default: | |
280 | return false; | |
281 | } | |
282 | ||
283 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
284 | return false; | |
285 | ||
286 | if (flags | |
287 | && ! bfd_set_section_flags (abfd, hdr->bfd_section, | |
288 | bfd_get_section_flags (abfd, | |
289 | hdr->bfd_section) | |
290 | | flags)) | |
291 | return false; | |
292 | ||
293 | return true; | |
294 | } | |
295 | ||
296 | /* In contrast to sh64_backend_section_from_shdr, this is called for all | |
297 | sections, but only when copying sections, not when linking or | |
298 | assembling. We need to set up the sh64_elf_section_data (asection *) | |
299 | structure for the SH64 ELF section flags to be copied correctly. */ | |
300 | ||
301 | boolean | |
302 | sh64_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec) | |
303 | bfd *ibfd; | |
304 | asection *isec; | |
305 | bfd *obfd; | |
306 | asection *osec; | |
307 | { | |
308 | struct sh64_section_data *sh64_sec_data; | |
309 | ||
310 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
311 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
312 | return true; | |
313 | ||
314 | if (! _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)) | |
315 | return false; | |
316 | ||
317 | sh64_sec_data = sh64_elf_section_data (isec); | |
318 | if (sh64_sec_data == NULL) | |
319 | { | |
320 | sh64_sec_data = bfd_zmalloc (sizeof (struct sh64_section_data)); | |
321 | ||
322 | if (sh64_sec_data == NULL) | |
323 | return false; | |
324 | ||
325 | sh64_sec_data->contents_flags | |
326 | = (elf_section_data (isec)->this_hdr.sh_flags | |
327 | & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)); | |
328 | ||
329 | sh64_elf_section_data (osec) = sh64_sec_data; | |
330 | } | |
331 | ||
332 | return true; | |
333 | } | |
334 | ||
335 | /* Function to keep SH64 specific file flags. */ | |
336 | ||
337 | static boolean | |
338 | sh64_elf_set_private_flags (abfd, flags) | |
339 | bfd * abfd; | |
340 | flagword flags; | |
341 | { | |
342 | BFD_ASSERT (! elf_flags_init (abfd) | |
343 | || elf_elfheader (abfd)->e_flags == flags); | |
344 | ||
345 | elf_elfheader (abfd)->e_flags = flags; | |
346 | elf_flags_init (abfd) = true; | |
347 | return sh64_elf_set_mach_from_flags (abfd); | |
348 | } | |
349 | ||
350 | /* Called when writing out an object file to decide the type of a symbol. */ | |
351 | ||
352 | static int | |
353 | sh64_elf_get_symbol_type (elf_sym, type) | |
354 | Elf_Internal_Sym * elf_sym; | |
355 | int type; | |
356 | { | |
357 | if (ELF_ST_TYPE (elf_sym->st_info) == STT_DATALABEL) | |
358 | return STT_DATALABEL; | |
359 | ||
360 | return type; | |
361 | } | |
362 | ||
363 | /* Hook called by the linker routine which adds symbols from an object | |
364 | file. We must make indirect symbols for undefined symbols marked with | |
365 | STT_DATALABEL, so relocations passing them will pick up that attribute | |
366 | and neutralize STO_SH5_ISA32 found on the symbol definition. | |
367 | ||
368 | There is a problem, though: We want to fill in the hash-table entry for | |
369 | this symbol and signal to the caller that no further processing is | |
370 | needed. But we don't have the index for this hash-table entry. We | |
371 | rely here on that the current entry is the first hash-entry with NULL, | |
372 | which seems brittle. Also, iterating over the hash-table to find that | |
373 | entry is a linear operation on the number of symbols in this input | |
374 | file, and this function should take constant time, so that's not good | |
375 | too. Only comfort is that DataLabel references should only be found in | |
376 | hand-written assembly code and thus be rare. FIXME: Talk maintainers | |
377 | into adding an option to elf_add_symbol_hook (preferably) for the index | |
378 | or the hash entry, alternatively adding the index to Elf_Internal_Sym | |
379 | (not so good). */ | |
380 | ||
381 | static boolean | |
382 | sh64_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp) | |
383 | bfd *abfd; | |
384 | struct bfd_link_info *info; | |
385 | const Elf_Internal_Sym *sym; | |
386 | const char **namep; | |
387 | flagword *flagsp ATTRIBUTE_UNUSED; | |
388 | asection **secp; | |
389 | bfd_vma *valp; | |
390 | { | |
391 | /* We want to do this for relocatable as well as final linking. */ | |
392 | if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL) | |
393 | { | |
394 | struct elf_link_hash_entry *h; | |
395 | ||
396 | /* For relocateable links, we register the DataLabel sym in its own | |
397 | right, and tweak the name when it's output. Otherwise, we make | |
398 | an indirect symbol of it. */ | |
399 | flagword flags | |
400 | = info->relocateable || info->emitrelocations | |
401 | ? BSF_GLOBAL : BSF_GLOBAL | BSF_INDIRECT; | |
402 | ||
403 | char *dl_name | |
404 | = bfd_malloc (strlen (*namep) + sizeof (DATALABEL_SUFFIX)); | |
405 | struct elf_link_hash_entry ** sym_hash = elf_sym_hashes (abfd); | |
406 | ||
407 | BFD_ASSERT (sym_hash != NULL); | |
408 | ||
409 | /* Allocation may fail. */ | |
410 | if (dl_name == NULL) | |
411 | return false; | |
412 | ||
413 | strcpy (dl_name, *namep); | |
414 | strcat (dl_name, DATALABEL_SUFFIX); | |
415 | ||
416 | h = (struct elf_link_hash_entry *) | |
417 | bfd_link_hash_lookup (info->hash, dl_name, false, false, false); | |
418 | ||
419 | if (h == NULL) | |
420 | { | |
421 | /* No previous datalabel symbol. Make one. */ | |
422 | if (! _bfd_generic_link_add_one_symbol (info, abfd, dl_name, | |
423 | flags, *secp, *valp, | |
424 | *namep, false, | |
425 | get_elf_backend_data (abfd)->collect, | |
426 | (struct bfd_link_hash_entry **) &h)) | |
427 | { | |
428 | free (dl_name); | |
429 | return false; | |
430 | } | |
431 | ||
432 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; | |
433 | h->type = STT_DATALABEL; | |
434 | } | |
435 | else | |
436 | /* If a new symbol was created, it holds the allocated name. | |
437 | Otherwise, we don't need it anymore and should deallocate it. */ | |
438 | free (dl_name); | |
439 | ||
440 | if (h->type != STT_DATALABEL | |
441 | || ((info->relocateable || info->emitrelocations) | |
442 | && h->root.type != bfd_link_hash_undefined) | |
443 | || (! info->relocateable && !info->emitrelocations | |
444 | && h->root.type != bfd_link_hash_indirect)) | |
445 | { | |
446 | /* Make sure we don't get confused on invalid input. */ | |
447 | (*_bfd_error_handler) | |
448 | (_("%s: encountered datalabel symbol in input"), | |
449 | bfd_get_filename (abfd)); | |
450 | bfd_set_error (bfd_error_bad_value); | |
451 | return false; | |
452 | } | |
453 | ||
454 | /* Now find the hash-table slot for this entry and fill it in. */ | |
455 | while (*sym_hash != NULL) | |
456 | sym_hash++; | |
457 | *sym_hash = h; | |
458 | ||
459 | /* Signal to caller to skip this symbol - we've handled it. */ | |
460 | *namep = NULL; | |
461 | } | |
462 | ||
463 | return true; | |
464 | } | |
465 | ||
466 | /* This hook function is called before the linker writes out a global | |
467 | symbol. For relocatable links, DataLabel symbols will be present in | |
468 | linker output. We cut off the special suffix on those symbols, so the | |
469 | right name appears in the output. | |
470 | ||
471 | When linking and emitting relocations, there can appear global symbols | |
472 | that are not referenced by relocs, but rather only implicitly through | |
473 | DataLabel references, a relation that is not visible to the linker. | |
474 | Since no stripping of global symbols in done when doing such linking, | |
475 | we don't need to look up and make sure to emit the main symbol for each | |
476 | DataLabel symbol. */ | |
477 | ||
478 | boolean | |
479 | sh64_elf_link_output_symbol_hook (abfd, info, cname, sym, input_sec) | |
480 | bfd *abfd ATTRIBUTE_UNUSED; | |
481 | struct bfd_link_info *info; | |
482 | const char *cname; | |
483 | Elf_Internal_Sym *sym; | |
484 | asection *input_sec ATTRIBUTE_UNUSED; | |
485 | { | |
486 | char *name = (char *) cname; | |
487 | ||
488 | if (info->relocateable || info->emitrelocations) | |
489 | { | |
490 | if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL) | |
491 | name[strlen (name) - strlen (DATALABEL_SUFFIX)] = 0; | |
492 | } | |
493 | ||
494 | return true; | |
495 | } | |
496 | ||
497 | /* Check a SH64-specific reloc and put the value to relocate to into | |
498 | RELOCATION, ready to pass to _bfd_final_link_relocate. Return FALSE if | |
499 | bad value, TRUE if ok. */ | |
500 | ||
501 | static boolean | |
502 | shmedia_prepare_reloc (info, abfd, input_section, | |
503 | contents, rel, relocation) | |
504 | struct bfd_link_info *info; | |
505 | bfd *abfd; | |
506 | asection *input_section; | |
507 | bfd_byte *contents; | |
508 | const Elf_Internal_Rela *rel; | |
509 | bfd_vma *relocation; | |
510 | { | |
511 | bfd_vma disp, dropped; | |
512 | ||
513 | switch (ELF32_R_TYPE (rel->r_info)) | |
514 | { | |
515 | case R_SH_PT_16: | |
516 | /* Check the lowest bit of the destination field. If it is 1, we | |
517 | check the ISA type of the destination (i.e. the low bit of the | |
518 | "relocation" value, and emit an error if the instruction does not | |
519 | match). If it is 0, we change a PTA to PTB. There should never | |
520 | be a PTB that should change to a PTA; that indicates a toolchain | |
521 | error; a mismatch with GAS. */ | |
522 | { | |
523 | char *msg = NULL; | |
524 | bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset); | |
525 | ||
526 | if (insn & (1 << 10)) | |
527 | { | |
528 | /* Check matching insn and ISA (address of target). */ | |
529 | if ((insn & SHMEDIA_PTB_BIT) != 0 | |
530 | && ((*relocation + rel->r_addend) & 1) != 0) | |
531 | msg = _("PTB mismatch: a SHmedia address (bit 0 == 1)"); | |
532 | else if ((insn & SHMEDIA_PTB_BIT) == 0 | |
533 | && ((*relocation + rel->r_addend) & 1) == 0) | |
534 | msg = _("PTA mismatch: a SHcompact address (bit 0 == 0)"); | |
535 | ||
536 | if (msg != NULL | |
537 | && ! ((*info->callbacks->reloc_dangerous) | |
538 | (info, msg, abfd, input_section, | |
539 | rel->r_offset))) | |
540 | return false; | |
541 | } | |
542 | else | |
543 | { | |
544 | /* We shouldn't get here with a PTB insn and a R_SH_PT_16. It | |
545 | means GAS output does not match expectations; a PTA or PTB | |
546 | expressed as such (or a PT found at assembly to be PTB) | |
547 | would match the test above, and PT expansion with an | |
548 | unknown destination (or when relaxing) will get us here. */ | |
549 | if ((insn & SHMEDIA_PTB_BIT) != 0) | |
550 | { | |
551 | (*_bfd_error_handler) | |
552 | (_("%s: GAS error: unexpected PTB insn with R_SH_PT_16"), | |
553 | bfd_get_filename (input_section->owner)); | |
554 | return false; | |
555 | } | |
556 | ||
557 | /* Change the PTA to a PTB, if destination indicates so. */ | |
558 | if (((*relocation + rel->r_addend) & 1) == 0) | |
559 | bfd_put_32 (abfd, insn | SHMEDIA_PTB_BIT, | |
560 | contents + rel->r_offset); | |
561 | } | |
562 | } | |
563 | ||
564 | case R_SH_SHMEDIA_CODE: | |
565 | case R_SH_DIR5U: | |
566 | case R_SH_DIR6S: | |
567 | case R_SH_DIR6U: | |
568 | case R_SH_DIR10S: | |
569 | case R_SH_DIR10SW: | |
570 | case R_SH_DIR10SL: | |
571 | case R_SH_DIR10SQ: | |
572 | case R_SH_IMMS16: | |
573 | case R_SH_IMMU16: | |
574 | case R_SH_IMM_LOW16: | |
575 | case R_SH_IMM_LOW16_PCREL: | |
576 | case R_SH_IMM_MEDLOW16: | |
577 | case R_SH_IMM_MEDLOW16_PCREL: | |
578 | case R_SH_IMM_MEDHI16: | |
579 | case R_SH_IMM_MEDHI16_PCREL: | |
580 | case R_SH_IMM_HI16: | |
581 | case R_SH_IMM_HI16_PCREL: | |
582 | case R_SH_64: | |
583 | case R_SH_64_PCREL: | |
584 | break; | |
585 | ||
586 | default: | |
587 | return false; | |
588 | } | |
589 | ||
590 | disp = (*relocation & 0xf); | |
591 | dropped = 0; | |
592 | switch (ELF32_R_TYPE (rel->r_info)) | |
593 | { | |
594 | case R_SH_DIR10SW: dropped = disp & 1; break; | |
595 | case R_SH_DIR10SL: dropped = disp & 3; break; | |
596 | case R_SH_DIR10SQ: dropped = disp & 7; break; | |
597 | } | |
598 | if (dropped != 0) | |
599 | { | |
600 | (*_bfd_error_handler) | |
601 | (_("%s: error: unaligned relocation type %d at %08x reloc %08x\n"), | |
602 | bfd_get_filename (input_section->owner), ELF32_R_TYPE (rel->r_info), | |
603 | (unsigned)rel->r_offset, (unsigned)relocation); | |
604 | return false; | |
605 | } | |
606 | ||
607 | return true; | |
608 | } | |
609 | ||
610 | /* Helper function to locate the section holding a certain address. This | |
611 | is called via bfd_map_over_sections. */ | |
612 | ||
613 | static void | |
614 | sh64_find_section_for_address (abfd, section, data) | |
615 | bfd *abfd ATTRIBUTE_UNUSED; | |
616 | asection *section; | |
617 | PTR data; | |
618 | { | |
619 | bfd_vma vma; | |
620 | bfd_size_type size; | |
621 | struct sh64_find_section_vma_data *fsec_datap | |
622 | = (struct sh64_find_section_vma_data *) data; | |
623 | ||
624 | /* Return if already found. */ | |
625 | if (fsec_datap->section) | |
626 | return; | |
627 | ||
628 | /* If this section isn't part of the addressable contents, skip it. */ | |
629 | if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0) | |
630 | return; | |
631 | ||
632 | vma = bfd_get_section_vma (abfd, section); | |
633 | if (fsec_datap->addr < vma) | |
634 | return; | |
635 | ||
636 | /* FIXME: section->reloc_done isn't set properly; a generic buglet | |
637 | preventing us from using bfd_get_section_size_after_reloc. */ | |
638 | size | |
639 | = section->_cooked_size ? section->_cooked_size : section->_raw_size; | |
640 | ||
641 | if (fsec_datap->addr >= vma + size) | |
642 | return; | |
643 | ||
644 | fsec_datap->section = section; | |
645 | } | |
646 | ||
647 | /* Make sure to write out the generated entries in the .cranges section | |
648 | when doing partial linking, and set bit 0 on the entry address if it | |
649 | points to SHmedia code and write sorted .cranges entries when writing | |
650 | executables (final linking and objcopy). */ | |
651 | ||
652 | static void | |
653 | sh64_elf_final_write_processing (abfd, linker) | |
654 | bfd * abfd; | |
655 | boolean linker ATTRIBUTE_UNUSED; | |
656 | { | |
657 | bfd_vma ld_generated_cranges_size; | |
658 | asection *cranges | |
659 | = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME); | |
660 | ||
661 | /* If no new .cranges were added, the generic ELF linker parts will | |
662 | write it all out. If not, we need to write them out when doing | |
663 | partial linking. For a final link, we will sort them and write them | |
664 | all out further below. */ | |
665 | if (linker | |
666 | && cranges != NULL | |
667 | && elf_elfheader (abfd)->e_type != ET_EXEC | |
668 | && (ld_generated_cranges_size | |
669 | = sh64_elf_section_data (cranges)->cranges_growth) != 0) | |
670 | { | |
671 | bfd_vma incoming_cranges_size | |
672 | = ((cranges->_cooked_size != 0 | |
673 | ? cranges->_cooked_size : cranges->_raw_size) | |
674 | - ld_generated_cranges_size); | |
675 | ||
676 | if (! bfd_set_section_contents (abfd, cranges, | |
677 | cranges->contents | |
678 | + incoming_cranges_size, | |
679 | cranges->output_offset | |
680 | + incoming_cranges_size, | |
681 | ld_generated_cranges_size)) | |
682 | { | |
683 | bfd_set_error (bfd_error_file_truncated); | |
684 | (*_bfd_error_handler) | |
685 | (_("%s: could not write out added .cranges entries"), | |
686 | bfd_get_filename (abfd)); | |
687 | } | |
688 | } | |
689 | ||
690 | /* Only set entry address bit 0 and sort .cranges when linking to an | |
691 | executable; never with objcopy or strip. */ | |
692 | if (linker && elf_elfheader (abfd)->e_type == ET_EXEC) | |
693 | { | |
694 | struct sh64_find_section_vma_data fsec_data; | |
695 | sh64_elf_crange dummy; | |
696 | ||
697 | /* For a final link, set the low bit of the entry address to | |
698 | reflect whether or not it is a SHmedia address. | |
699 | FIXME: Perhaps we shouldn't do this if the entry address was | |
700 | supplied numerically, but we currently lack the infrastructure to | |
701 | recognize that: The entry symbol, and info whether it is numeric | |
702 | or a symbol name is kept private in the linker. */ | |
703 | fsec_data.addr = elf_elfheader (abfd)->e_entry; | |
704 | fsec_data.section = NULL; | |
705 | ||
706 | bfd_map_over_sections (abfd, sh64_find_section_for_address, | |
707 | (PTR) &fsec_data); | |
708 | if (fsec_data.section | |
709 | && (sh64_get_contents_type (fsec_data.section, | |
710 | elf_elfheader (abfd)->e_entry, | |
711 | &dummy) == CRT_SH5_ISA32)) | |
712 | elf_elfheader (abfd)->e_entry |= 1; | |
713 | ||
714 | /* If we have a .cranges section, sort the entries. */ | |
715 | if (cranges != NULL) | |
716 | { | |
717 | bfd_size_type cranges_size | |
718 | = (cranges->_cooked_size != 0 | |
719 | ? cranges->_cooked_size : cranges->_raw_size); | |
720 | ||
721 | /* We know we always have these in memory at this time. */ | |
722 | BFD_ASSERT (cranges->contents != NULL); | |
723 | ||
724 | /* The .cranges may already have been sorted in the process of | |
725 | finding out the ISA-type of the entry address. If not, we do | |
726 | it here. */ | |
727 | if (elf_section_data (cranges)->this_hdr.sh_type | |
728 | != SHT_SH5_CR_SORTED) | |
729 | { | |
730 | qsort (cranges->contents, cranges_size / SH64_CRANGE_SIZE, | |
731 | SH64_CRANGE_SIZE, | |
732 | bfd_big_endian (cranges->owner) | |
733 | ? crange_qsort_cmpb : crange_qsort_cmpl); | |
734 | elf_section_data (cranges)->this_hdr.sh_type | |
735 | = SHT_SH5_CR_SORTED; | |
736 | } | |
737 | ||
738 | /* We need to write it out in whole as sorted. */ | |
739 | if (! bfd_set_section_contents (abfd, cranges, | |
740 | cranges->contents, | |
741 | cranges->output_offset, | |
742 | cranges_size)) | |
743 | { | |
744 | bfd_set_error (bfd_error_file_truncated); | |
745 | (*_bfd_error_handler) | |
746 | (_("%s: could not write out sorted .cranges entries"), | |
747 | bfd_get_filename (abfd)); | |
748 | } | |
749 | } | |
750 | } | |
751 | } | |
752 | ||
753 | /* Ordering functions of a crange, for the qsort and bsearch calls and for | |
754 | different endianness. */ | |
755 | ||
756 | static int | |
757 | crange_qsort_cmpb (p1, p2) | |
758 | const PTR p1; | |
759 | const PTR p2; | |
760 | { | |
761 | bfd_vma a1 = bfd_getb32 (p1); | |
762 | bfd_vma a2 = bfd_getb32 (p2); | |
763 | ||
764 | /* Preserve order if there's ambiguous contents. */ | |
765 | if (a1 == a2) | |
766 | return (char *) p1 - (char *) p2; | |
767 | ||
768 | return a1 - a2; | |
769 | } | |
770 | ||
771 | static int | |
772 | crange_qsort_cmpl (p1, p2) | |
773 | const PTR p1; | |
774 | const PTR p2; | |
775 | { | |
776 | bfd_vma a1 = (bfd_vma) bfd_getl32 (p1); | |
777 | bfd_vma a2 = (bfd_vma) bfd_getl32 (p2); | |
778 | ||
779 | /* Preserve order if there's ambiguous contents. */ | |
780 | if (a1 == a2) | |
781 | return (char *) p1 - (char *) p2; | |
782 | ||
783 | return a1 - a2; | |
784 | } | |
785 | ||
786 | static int | |
787 | crange_bsearch_cmpb (p1, p2) | |
788 | const PTR p1; | |
789 | const PTR p2; | |
790 | { | |
791 | bfd_vma a1 = *(bfd_vma *) p1; | |
792 | bfd_vma a2 = (bfd_vma) bfd_getb32 (p2); | |
793 | bfd_size_type size | |
794 | = (bfd_size_type) bfd_getb32 (SH64_CRANGE_CR_SIZE_OFFSET + (char *) p2); | |
795 | ||
796 | if (a1 >= a2 + size) | |
797 | return 1; | |
798 | if (a1 < a2) | |
799 | return -1; | |
800 | return 0; | |
801 | } | |
802 | ||
803 | static int | |
804 | crange_bsearch_cmpl (p1, p2) | |
805 | const PTR p1; | |
806 | const PTR p2; | |
807 | { | |
808 | bfd_vma a1 = *(bfd_vma *) p1; | |
809 | bfd_vma a2 = (bfd_vma) bfd_getl32 (p2); | |
810 | bfd_size_type size | |
811 | = (bfd_size_type) bfd_getl32 (SH64_CRANGE_CR_SIZE_OFFSET + (char *) p2); | |
812 | ||
813 | if (a1 >= a2 + size) | |
814 | return 1; | |
815 | if (a1 < a2) | |
816 | return -1; | |
817 | return 0; | |
818 | } | |
819 | ||
820 | /* Check whether a specific address is specified within a .cranges | |
821 | section. Return FALSE if not found, and TRUE if found, and the region | |
822 | filled into RANGEP if non-NULL. */ | |
823 | ||
824 | static boolean | |
825 | sh64_address_in_cranges (cranges, addr, rangep) | |
826 | asection *cranges; | |
827 | bfd_vma addr; | |
828 | sh64_elf_crange *rangep; | |
829 | { | |
830 | bfd_byte *cranges_contents; | |
831 | bfd_byte *found_rangep; | |
832 | bfd_size_type cranges_size = bfd_section_size (cranges->owner, cranges); | |
833 | ||
834 | /* If the size is not a multiple of the cranges entry size, then | |
835 | something is badly wrong. */ | |
836 | if ((cranges_size % SH64_CRANGE_SIZE) != 0) | |
837 | return false; | |
838 | ||
839 | /* If this section has relocations, then we can't do anything sane. */ | |
840 | if (bfd_get_section_flags (cranges->owner, cranges) & SEC_RELOC) | |
841 | return false; | |
842 | ||
843 | /* Has some kind soul (or previous call) left processed, sorted contents | |
844 | for us? */ | |
845 | if ((bfd_get_section_flags (cranges->owner, cranges) & SEC_IN_MEMORY) | |
846 | && elf_section_data (cranges)->this_hdr.sh_type == SHT_SH5_CR_SORTED) | |
847 | cranges_contents = cranges->contents; | |
848 | else | |
849 | { | |
850 | cranges_contents | |
851 | = bfd_malloc (cranges->_cooked_size == 0 | |
852 | ? cranges->_cooked_size : cranges->_raw_size); | |
853 | if (cranges_contents == NULL) | |
854 | return false; | |
855 | ||
856 | if (! bfd_get_section_contents (cranges->owner, cranges, | |
857 | cranges_contents, (file_ptr) 0, | |
858 | cranges_size)) | |
859 | goto error_return; | |
860 | ||
861 | /* Is it sorted? */ | |
862 | if (elf_section_data (cranges)->this_hdr.sh_type | |
863 | != SHT_SH5_CR_SORTED) | |
864 | /* Nope. Lets sort it. */ | |
865 | qsort (cranges_contents, cranges_size / SH64_CRANGE_SIZE, | |
866 | SH64_CRANGE_SIZE, | |
867 | bfd_big_endian (cranges->owner) | |
868 | ? crange_qsort_cmpb : crange_qsort_cmpl); | |
869 | ||
870 | /* Let's keep it around. */ | |
871 | cranges->contents = cranges_contents; | |
872 | bfd_set_section_flags (cranges->owner, cranges, | |
873 | bfd_get_section_flags (cranges->owner, cranges) | |
874 | | SEC_IN_MEMORY); | |
875 | ||
876 | /* It's sorted now. */ | |
877 | elf_section_data (cranges)->this_hdr.sh_type = SHT_SH5_CR_SORTED; | |
878 | } | |
879 | ||
880 | /* Try and find a matching range. */ | |
881 | found_rangep | |
882 | = bsearch (&addr, cranges_contents, cranges_size / SH64_CRANGE_SIZE, | |
883 | SH64_CRANGE_SIZE, | |
884 | bfd_big_endian (cranges->owner) | |
885 | ? crange_bsearch_cmpb : crange_bsearch_cmpl); | |
886 | ||
887 | /* Fill in a few return values if we found a matching range. */ | |
888 | if (found_rangep) | |
889 | { | |
890 | enum sh64_elf_cr_type cr_type | |
891 | = bfd_get_16 (cranges->owner, | |
892 | SH64_CRANGE_CR_TYPE_OFFSET + found_rangep); | |
893 | bfd_vma cr_addr | |
894 | = bfd_get_32 (cranges->owner, | |
895 | SH64_CRANGE_CR_ADDR_OFFSET | |
896 | + (char *) found_rangep); | |
897 | bfd_size_type cr_size | |
898 | = bfd_get_32 (cranges->owner, | |
899 | SH64_CRANGE_CR_SIZE_OFFSET | |
900 | + (char *) found_rangep); | |
901 | ||
902 | rangep->cr_addr = cr_addr; | |
903 | rangep->cr_size = cr_size; | |
904 | rangep->cr_type = cr_type; | |
905 | ||
906 | return true; | |
907 | } | |
908 | ||
909 | /* There is a .cranges section, but it does not have a descriptor | |
910 | matching this address. */ | |
911 | return false; | |
912 | ||
913 | error_return: | |
914 | free (cranges_contents); | |
915 | return false; | |
916 | } | |
917 | ||
918 | /* Determine what ADDR points to in SEC, and fill in a range descriptor in | |
919 | *RANGEP if it's non-NULL. */ | |
920 | ||
921 | enum sh64_elf_cr_type | |
922 | sh64_get_contents_type (sec, addr, rangep) | |
923 | asection *sec; | |
924 | bfd_vma addr; | |
925 | sh64_elf_crange *rangep; | |
926 | { | |
927 | asection *cranges; | |
928 | ||
929 | /* Fill in the range with the boundaries of the section as a default. */ | |
930 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
931 | && elf_elfheader (sec->owner)->e_type == ET_EXEC) | |
932 | { | |
933 | rangep->cr_addr = bfd_get_section_vma (sec->owner, sec); | |
934 | rangep->cr_size = bfd_section_size (sec->owner, sec); | |
935 | rangep->cr_type = CRT_NONE; | |
936 | } | |
937 | else | |
938 | return false; | |
939 | ||
940 | /* If none of the pertinent bits are set, then it's a SHcompact (or at | |
941 | least not SHmedia). */ | |
942 | if ((elf_section_data (sec)->this_hdr.sh_flags | |
943 | & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) == 0) | |
944 | { | |
945 | enum sh64_elf_cr_type cr_type | |
946 | = ((bfd_get_section_flags (sec->owner, sec) & SEC_CODE) != 0 | |
947 | ? CRT_SH5_ISA16 : CRT_DATA); | |
948 | rangep->cr_type = cr_type; | |
949 | return cr_type; | |
950 | } | |
951 | ||
952 | /* If only the SHF_SH5_ISA32 bit is set, then we have SHmedia. */ | |
953 | if ((elf_section_data (sec)->this_hdr.sh_flags | |
954 | & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) == SHF_SH5_ISA32) | |
955 | { | |
956 | rangep->cr_type = CRT_SH5_ISA32; | |
957 | return CRT_SH5_ISA32; | |
958 | } | |
959 | ||
960 | /* Otherwise, we have to look up the .cranges section. */ | |
961 | cranges = bfd_get_section_by_name (sec->owner, SH64_CRANGES_SECTION_NAME); | |
962 | ||
963 | if (cranges == NULL) | |
964 | /* A mixed section but there's no .cranges section. This is probably | |
965 | bad input; it does not comply to specs. */ | |
966 | return CRT_NONE; | |
967 | ||
968 | /* If this call fails, we will still have CRT_NONE in rangep->cr_type | |
969 | and that will be suitable to return. */ | |
970 | sh64_address_in_cranges (cranges, addr, rangep); | |
971 | ||
972 | return rangep->cr_type; | |
973 | } | |
974 | ||
975 | /* This is a simpler exported interface for the benefit of gdb et al. */ | |
976 | ||
977 | boolean | |
978 | sh64_address_is_shmedia (sec, addr) | |
979 | asection *sec; | |
980 | bfd_vma addr; | |
981 | { | |
982 | sh64_elf_crange dummy; | |
983 | return sh64_get_contents_type (sec, addr, &dummy) == CRT_SH5_ISA32; | |
984 | } |