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