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