1
/* Hitachi SH64-specific support for 32-bit ELF
2
Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4
This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
25
#include "../opcodes/sh64-opc.h"
27
/* Add a suffix for datalabel indirection symbols. It must not match any
28
other symbols; user symbols with or without version or other
29
decoration. It must only be used internally and not emitted by any
31
#define DATALABEL_SUFFIX " DL"
33
/* Used to hold data for function called through bfd_map_over_sections. */
34
struct sh64_find_section_vma_data
40
static boolean sh64_elf_copy_private_data PARAMS ((bfd *, bfd *));
41
static boolean sh64_elf_merge_private_data PARAMS ((bfd *, bfd *));
42
static boolean sh64_elf_fake_sections PARAMS ((bfd *, Elf_Internal_Shdr *,
44
static boolean sh64_elf_set_private_flags PARAMS ((bfd *, flagword));
45
static boolean sh64_elf_set_mach_from_flags PARAMS ((bfd *));
46
static boolean shmedia_prepare_reloc
47
PARAMS ((struct bfd_link_info *, bfd *, asection *,
48
bfd_byte *, const Elf_Internal_Rela *, bfd_vma *));
49
static int sh64_elf_get_symbol_type PARAMS ((Elf_Internal_Sym *, int));
50
static boolean sh64_elf_add_symbol_hook
51
PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
52
const char **, flagword *, asection **, bfd_vma *));
53
static boolean sh64_elf_link_output_symbol_hook
54
PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
56
static boolean sh64_backend_section_from_shdr
57
PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
58
static void sh64_elf_final_write_processing PARAMS ((bfd *, boolean));
59
static boolean sh64_bfd_elf_copy_private_section_data
60
PARAMS ((bfd *, asection *, bfd *, asection *));
61
static void sh64_find_section_for_address PARAMS ((bfd *, asection *, PTR));
63
/* Let elf32-sh.c handle the "bfd_" definitions, so we only have to
64
intrude with an #ifndef around the function definition. */
65
#define sh_elf_copy_private_data sh64_elf_copy_private_data
66
#define sh_elf_merge_private_data sh64_elf_merge_private_data
67
#define sh_elf_set_private_flags sh64_elf_set_private_flags
68
/* Typo in elf32-sh.c (and unlinear name). */
69
#define bfd_elf32_bfd_set_private_flags sh64_elf_set_private_flags
70
#define sh_elf_set_mach_from_flags sh64_elf_set_mach_from_flags
72
#define elf_backend_sign_extend_vma 1
73
#define elf_backend_fake_sections sh64_elf_fake_sections
74
#define elf_backend_get_symbol_type sh64_elf_get_symbol_type
75
#define elf_backend_add_symbol_hook sh64_elf_add_symbol_hook
76
#define elf_backend_link_output_symbol_hook \
77
sh64_elf_link_output_symbol_hook
78
#define elf_backend_final_write_processing sh64_elf_final_write_processing
79
#define elf_backend_section_from_shdr sh64_backend_section_from_shdr
81
/* For objcopy, we need to set up sh64_elf_section_data (asection *) from
82
incoming section flags. This is otherwise done in sh64elf.em when
83
linking or tc-sh64.c when assembling. */
84
#define bfd_elf32_bfd_copy_private_section_data \
85
sh64_bfd_elf_copy_private_section_data
87
/* This COFF-only function (only compiled with COFF support, making
88
ELF-only chains problematic) returns true early for SH4, so let's just
89
define it true here. */
90
#define _bfd_sh_align_load_span(a,b,c,d,e,f,g,h,i,j) true
93
#define TARGET_BIG_SYM bfd_elf32_sh64_vec
94
#define TARGET_BIG_NAME "elf32-sh64"
95
#define TARGET_LITTLE_SYM bfd_elf32_sh64l_vec
96
#define TARGET_LITTLE_NAME "elf32-sh64l"
97
#define ELF_ARCH bfd_arch_sh
98
#define ELF_MACHINE_CODE EM_SH
99
#define ELF_MAXPAGESIZE 128
101
#define elf_symbol_leading_char '_'
102
#endif /* ELF_ARCH */
104
#define GOT_BIAS (-((long)-32768))
105
#define INCLUDE_SHMEDIA
106
#include "elf32-sh.c"
108
/* Set the SHF_SH5_ISA32 flag for ISA SHmedia code sections, and pass
109
through SHT_SH5_CR_SORTED on a sorted .cranges section. */
112
sh64_elf_fake_sections (output_bfd, elf_section_hdr, asect)
113
bfd *output_bfd ATTRIBUTE_UNUSED;
114
Elf_Internal_Shdr *elf_section_hdr;
117
if (sh64_elf_section_data (asect) != NULL)
118
elf_section_hdr->sh_flags
119
|= sh64_elf_section_data (asect)->contents_flags;
121
/* If this section has the SEC_SORT_ENTRIES flag set, it is a sorted
122
.cranges section passing through objcopy. */
123
if ((bfd_get_section_flags (output_bfd, asect) & SEC_SORT_ENTRIES) != 0
124
&& strcmp (bfd_get_section_name (output_bfd, asect),
125
SH64_CRANGES_SECTION_NAME) == 0)
126
elf_section_hdr->sh_type = SHT_SH5_CR_SORTED;
132
sh64_elf_set_mach_from_flags (abfd)
135
flagword flags = elf_elfheader (abfd)->e_flags;
138
switch (flags & EF_SH_MACH_MASK)
141
/* These are fit to execute on SH5. Just one but keep the switch
142
construct to make additions easy. */
143
bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh5);
147
bfd_set_error (bfd_error_wrong_format);
151
/* We also need to set SEC_DEBUGGING on an incoming .cranges section.
152
We could have used elf_backend_section_flags if it had given us the
153
section name; the bfd_section member in the header argument is not
154
set at the point of the call. FIXME: Find out whether that is by
155
undocumented design or a bug. */
156
cranges = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
158
&& ! bfd_set_section_flags (abfd, cranges,
159
bfd_get_section_flags (abfd, cranges)
167
sh64_elf_copy_private_data (ibfd, obfd)
171
if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
172
|| bfd_get_flavour (obfd) != bfd_target_elf_flavour)
175
BFD_ASSERT (!elf_flags_init (obfd)
176
|| (elf_elfheader (obfd)->e_flags
177
== elf_elfheader (ibfd)->e_flags));
179
elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
184
sh64_elf_merge_private_data (ibfd, obfd)
188
flagword old_flags, new_flags;
190
if (! _bfd_generic_verify_endian_match (ibfd, obfd))
193
if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
194
|| bfd_get_flavour (obfd) != bfd_target_elf_flavour)
197
if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd))
201
if (bfd_get_arch_size (ibfd) == 32
202
&& bfd_get_arch_size (obfd) == 64)
203
msg = _("%s: compiled as 32-bit object and %s is 64-bit");
204
else if (bfd_get_arch_size (ibfd) == 64
205
&& bfd_get_arch_size (obfd) == 32)
206
msg = _("%s: compiled as 64-bit object and %s is 32-bit");
208
msg = _("%s: object size does not match that of target %s");
210
(*_bfd_error_handler) (msg, bfd_get_filename (ibfd),
211
bfd_get_filename (obfd));
212
bfd_set_error (bfd_error_wrong_format);
216
old_flags = elf_elfheader (obfd)->e_flags;
217
new_flags = elf_elfheader (ibfd)->e_flags;
218
if (! elf_flags_init (obfd))
220
/* This happens when ld starts out with a 'blank' output file. */
221
elf_flags_init (obfd) = true;
222
elf_elfheader (obfd)->e_flags = old_flags = new_flags;
224
/* We don't allow linking in non-SH64 code. */
225
else if ((new_flags & EF_SH_MACH_MASK) != EF_SH5)
227
(*_bfd_error_handler)
228
("%s: uses non-SH64 instructions while previous modules use SH64 instructions",
229
bfd_get_filename (ibfd));
230
bfd_set_error (bfd_error_bad_value);
234
/* I can't think of anything sane other than old_flags being EF_SH5 and
235
that we need to preserve that. */
236
elf_elfheader (obfd)->e_flags = old_flags;
237
return sh64_elf_set_mach_from_flags (obfd);
240
/* Handle a SH64-specific section when reading an object file. This
241
is called when elfcode.h finds a section with an unknown type.
243
We only recognize SHT_SH5_CR_SORTED, on the .cranges section. */
246
sh64_backend_section_from_shdr (abfd, hdr, name)
248
Elf_Internal_Shdr *hdr;
253
/* We do like MIPS with a bit switch for recognized types, and returning
254
false for a recognized section type with an unexpected name. Right
255
now we only have one recognized type, but that might change. */
256
switch (hdr->sh_type)
258
case SHT_SH5_CR_SORTED:
259
if (strcmp (name, SH64_CRANGES_SECTION_NAME) != 0)
262
/* We set the SEC_SORT_ENTRIES flag so it can be passed on to
263
sh64_elf_fake_sections, keeping SHT_SH5_CR_SORTED if this object
264
passes through objcopy. Perhaps it is brittle; the flag can
265
suddenly be used by other BFD parts, but it seems not really used
266
anywhere at the moment. */
267
flags = SEC_DEBUGGING | SEC_SORT_ENTRIES;
274
if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
278
&& ! bfd_set_section_flags (abfd, hdr->bfd_section,
279
bfd_get_section_flags (abfd,
287
/* In contrast to sh64_backend_section_from_shdr, this is called for all
288
sections, but only when copying sections, not when linking or
289
assembling. We need to set up the sh64_elf_section_data (asection *)
290
structure for the SH64 ELF section flags to be copied correctly. */
293
sh64_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
299
struct sh64_section_data *sh64_sec_data;
301
if (ibfd->xvec->flavour != bfd_target_elf_flavour
302
|| obfd->xvec->flavour != bfd_target_elf_flavour)
305
if (! _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec))
308
sh64_sec_data = sh64_elf_section_data (isec);
309
if (sh64_sec_data == NULL)
311
sh64_sec_data = bfd_zmalloc (sizeof (struct sh64_section_data));
313
if (sh64_sec_data == NULL)
316
sh64_sec_data->contents_flags
317
= (elf_section_data (isec)->this_hdr.sh_flags
318
& (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
320
sh64_elf_section_data (osec) = sh64_sec_data;
326
/* Function to keep SH64 specific file flags. */
329
sh64_elf_set_private_flags (abfd, flags)
333
BFD_ASSERT (! elf_flags_init (abfd)
334
|| elf_elfheader (abfd)->e_flags == flags);
336
elf_elfheader (abfd)->e_flags = flags;
337
elf_flags_init (abfd) = true;
338
return sh64_elf_set_mach_from_flags (abfd);
341
/* Called when writing out an object file to decide the type of a symbol. */
344
sh64_elf_get_symbol_type (elf_sym, type)
345
Elf_Internal_Sym * elf_sym;
348
if (ELF_ST_TYPE (elf_sym->st_info) == STT_DATALABEL)
349
return STT_DATALABEL;
354
/* Hook called by the linker routine which adds symbols from an object
355
file. We must make indirect symbols for undefined symbols marked with
356
STT_DATALABEL, so relocations passing them will pick up that attribute
357
and neutralize STO_SH5_ISA32 found on the symbol definition.
359
There is a problem, though: We want to fill in the hash-table entry for
360
this symbol and signal to the caller that no further processing is
361
needed. But we don't have the index for this hash-table entry. We
362
rely here on that the current entry is the first hash-entry with NULL,
363
which seems brittle. Also, iterating over the hash-table to find that
364
entry is a linear operation on the number of symbols in this input
365
file, and this function should take constant time, so that's not good
366
too. Only comfort is that DataLabel references should only be found in
367
hand-written assembly code and thus be rare. FIXME: Talk maintainers
368
into adding an option to elf_add_symbol_hook (preferably) for the index
369
or the hash entry, alternatively adding the index to Elf_Internal_Sym
373
sh64_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
375
struct bfd_link_info *info;
376
const Elf_Internal_Sym *sym;
378
flagword *flagsp ATTRIBUTE_UNUSED;
382
/* We want to do this for relocatable as well as final linking. */
383
if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL
384
&& info->hash->creator->flavour == bfd_target_elf_flavour)
386
struct elf_link_hash_entry *h;
388
/* For relocateable links, we register the DataLabel sym in its own
389
right, and tweak the name when it's output. Otherwise, we make
390
an indirect symbol of it. */
392
= info->relocateable || info->emitrelocations
393
? BSF_GLOBAL : BSF_GLOBAL | BSF_INDIRECT;
396
= bfd_malloc (strlen (*namep) + sizeof (DATALABEL_SUFFIX));
397
struct elf_link_hash_entry ** sym_hash = elf_sym_hashes (abfd);
399
BFD_ASSERT (sym_hash != NULL);
401
/* Allocation may fail. */
405
strcpy (dl_name, *namep);
406
strcat (dl_name, DATALABEL_SUFFIX);
408
h = (struct elf_link_hash_entry *)
409
bfd_link_hash_lookup (info->hash, dl_name, false, false, false);
413
/* No previous datalabel symbol. Make one. */
414
if (! _bfd_generic_link_add_one_symbol (info, abfd, dl_name,
417
get_elf_backend_data (abfd)->collect,
418
(struct bfd_link_hash_entry **) &h))
424
h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
425
h->type = STT_DATALABEL;
428
/* If a new symbol was created, it holds the allocated name.
429
Otherwise, we don't need it anymore and should deallocate it. */
432
if (h->type != STT_DATALABEL
433
|| ((info->relocateable || info->emitrelocations)
434
&& h->root.type != bfd_link_hash_undefined)
435
|| (! info->relocateable && !info->emitrelocations
436
&& h->root.type != bfd_link_hash_indirect))
438
/* Make sure we don't get confused on invalid input. */
439
(*_bfd_error_handler)
440
(_("%s: encountered datalabel symbol in input"),
441
bfd_get_filename (abfd));
442
bfd_set_error (bfd_error_bad_value);
446
/* Now find the hash-table slot for this entry and fill it in. */
447
while (*sym_hash != NULL)
451
/* Signal to caller to skip this symbol - we've handled it. */
458
/* This hook function is called before the linker writes out a global
459
symbol. For relocatable links, DataLabel symbols will be present in
460
linker output. We cut off the special suffix on those symbols, so the
461
right name appears in the output.
463
When linking and emitting relocations, there can appear global symbols
464
that are not referenced by relocs, but rather only implicitly through
465
DataLabel references, a relation that is not visible to the linker.
466
Since no stripping of global symbols in done when doing such linking,
467
we don't need to look up and make sure to emit the main symbol for each
471
sh64_elf_link_output_symbol_hook (abfd, info, cname, sym, input_sec)
472
bfd *abfd ATTRIBUTE_UNUSED;
473
struct bfd_link_info *info;
475
Elf_Internal_Sym *sym;
476
asection *input_sec ATTRIBUTE_UNUSED;
478
char *name = (char *) cname;
480
if (info->relocateable || info->emitrelocations)
482
if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL)
483
name[strlen (name) - strlen (DATALABEL_SUFFIX)] = 0;
489
/* Check a SH64-specific reloc and put the value to relocate to into
490
RELOCATION, ready to pass to _bfd_final_link_relocate. Return FALSE if
491
bad value, TRUE if ok. */
494
shmedia_prepare_reloc (info, abfd, input_section,
495
contents, rel, relocation)
496
struct bfd_link_info *info;
498
asection *input_section;
500
const Elf_Internal_Rela *rel;
503
bfd_vma disp, dropped;
505
switch (ELF32_R_TYPE (rel->r_info))
508
/* Check the lowest bit of the destination field. If it is 1, we
509
check the ISA type of the destination (i.e. the low bit of the
510
"relocation" value, and emit an error if the instruction does not
511
match). If it is 0, we change a PTA to PTB. There should never
512
be a PTB that should change to a PTA; that indicates a toolchain
513
error; a mismatch with GAS. */
516
bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
518
if (insn & (1 << 10))
520
/* Check matching insn and ISA (address of target). */
521
if ((insn & SHMEDIA_PTB_BIT) != 0
522
&& ((*relocation + rel->r_addend) & 1) != 0)
523
msg = _("PTB mismatch: a SHmedia address (bit 0 == 1)");
524
else if ((insn & SHMEDIA_PTB_BIT) == 0
525
&& ((*relocation + rel->r_addend) & 1) == 0)
526
msg = _("PTA mismatch: a SHcompact address (bit 0 == 0)");
529
&& ! ((*info->callbacks->reloc_dangerous)
530
(info, msg, abfd, input_section,
536
/* We shouldn't get here with a PTB insn and a R_SH_PT_16. It
537
means GAS output does not match expectations; a PTA or PTB
538
expressed as such (or a PT found at assembly to be PTB)
539
would match the test above, and PT expansion with an
540
unknown destination (or when relaxing) will get us here. */
541
if ((insn & SHMEDIA_PTB_BIT) != 0)
543
(*_bfd_error_handler)
544
(_("%s: GAS error: unexpected PTB insn with R_SH_PT_16"),
545
bfd_get_filename (input_section->owner));
549
/* Change the PTA to a PTB, if destination indicates so. */
550
if (((*relocation + rel->r_addend) & 1) == 0)
551
bfd_put_32 (abfd, insn | SHMEDIA_PTB_BIT,
552
contents + rel->r_offset);
556
case R_SH_SHMEDIA_CODE:
567
case R_SH_IMM_LOW16_PCREL:
568
case R_SH_IMM_MEDLOW16:
569
case R_SH_IMM_MEDLOW16_PCREL:
570
case R_SH_IMM_MEDHI16:
571
case R_SH_IMM_MEDHI16_PCREL:
573
case R_SH_IMM_HI16_PCREL:
582
disp = (*relocation & 0xf);
584
switch (ELF32_R_TYPE (rel->r_info))
586
case R_SH_DIR10SW: dropped = disp & 1; break;
587
case R_SH_DIR10SL: dropped = disp & 3; break;
588
case R_SH_DIR10SQ: dropped = disp & 7; break;
592
(*_bfd_error_handler)
593
(_("%s: error: unaligned relocation type %d at %08x reloc %08x\n"),
594
bfd_get_filename (input_section->owner), ELF32_R_TYPE (rel->r_info),
595
(unsigned)rel->r_offset, (unsigned)relocation);
602
/* Helper function to locate the section holding a certain address. This
603
is called via bfd_map_over_sections. */
606
sh64_find_section_for_address (abfd, section, data)
607
bfd *abfd ATTRIBUTE_UNUSED;
613
struct sh64_find_section_vma_data *fsec_datap
614
= (struct sh64_find_section_vma_data *) data;
616
/* Return if already found. */
617
if (fsec_datap->section)
620
/* If this section isn't part of the addressable contents, skip it. */
621
if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
624
vma = bfd_get_section_vma (abfd, section);
625
if (fsec_datap->addr < vma)
628
/* FIXME: section->reloc_done isn't set properly; a generic buglet
629
preventing us from using bfd_get_section_size_after_reloc. */
631
= section->_cooked_size ? section->_cooked_size : section->_raw_size;
633
if (fsec_datap->addr >= vma + size)
636
fsec_datap->section = section;
639
/* Make sure to write out the generated entries in the .cranges section
640
when doing partial linking, and set bit 0 on the entry address if it
641
points to SHmedia code and write sorted .cranges entries when writing
642
executables (final linking and objcopy). */
645
sh64_elf_final_write_processing (abfd, linker)
647
boolean linker ATTRIBUTE_UNUSED;
649
bfd_vma ld_generated_cranges_size;
651
= bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
653
/* If no new .cranges were added, the generic ELF linker parts will
654
write it all out. If not, we need to write them out when doing
655
partial linking. For a final link, we will sort them and write them
656
all out further below. */
659
&& elf_elfheader (abfd)->e_type != ET_EXEC
660
&& (ld_generated_cranges_size
661
= sh64_elf_section_data (cranges)->cranges_growth) != 0)
663
bfd_vma incoming_cranges_size
664
= ((cranges->_cooked_size != 0
665
? cranges->_cooked_size : cranges->_raw_size)
666
- ld_generated_cranges_size);
668
if (! bfd_set_section_contents (abfd, cranges,
670
+ incoming_cranges_size,
671
cranges->output_offset
672
+ incoming_cranges_size,
673
ld_generated_cranges_size))
675
bfd_set_error (bfd_error_file_truncated);
676
(*_bfd_error_handler)
677
(_("%s: could not write out added .cranges entries"),
678
bfd_get_filename (abfd));
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)
686
struct sh64_find_section_vma_data fsec_data;
687
sh64_elf_crange dummy;
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;
698
bfd_map_over_sections (abfd, sh64_find_section_for_address,
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;
706
/* If we have a .cranges section, sort the entries. */
709
bfd_size_type cranges_size
710
= (cranges->_cooked_size != 0
711
? cranges->_cooked_size : cranges->_raw_size);
713
/* We know we always have these in memory at this time. */
714
BFD_ASSERT (cranges->contents != NULL);
716
/* The .cranges may already have been sorted in the process of
717
finding out the ISA-type of the entry address. If not, we do
719
if (elf_section_data (cranges)->this_hdr.sh_type
720
!= SHT_SH5_CR_SORTED)
722
qsort (cranges->contents, cranges_size / SH64_CRANGE_SIZE,
724
bfd_big_endian (cranges->owner)
725
? _bfd_sh64_crange_qsort_cmpb
726
: _bfd_sh64_crange_qsort_cmpl);
727
elf_section_data (cranges)->this_hdr.sh_type
731
/* We need to write it out in whole as sorted. */
732
if (! bfd_set_section_contents (abfd, cranges,
734
cranges->output_offset,
737
bfd_set_error (bfd_error_file_truncated);
738
(*_bfd_error_handler)
739
(_("%s: could not write out sorted .cranges entries"),
740
bfd_get_filename (abfd));