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