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