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