s/boolean/bfd_boolean/ s/true/TRUE/ s/false/FALSE/. Simplify
[platform/upstream/binutils.git] / bfd / elf32-sh64.c
1 /* Hitachi SH64-specific support for 32-bit ELF
2    Copyright 2000, 2001, 2002 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
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
30    means.  */
31 #define DATALABEL_SUFFIX " DL"
32
33 /* Used to hold data for function called through bfd_map_over_sections.  */
34 struct sh64_find_section_vma_data
35  {
36    asection *section;
37    bfd_vma addr;
38  };
39
40 static bfd_boolean sh64_elf_copy_private_data
41   PARAMS ((bfd *, bfd *));
42 static bfd_boolean sh64_elf_merge_private_data
43   PARAMS ((bfd *, bfd *));
44 static bfd_boolean sh64_elf_fake_sections
45   PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
46 static bfd_boolean sh64_elf_set_private_flags
47   PARAMS ((bfd *, flagword));
48 static bfd_boolean sh64_elf_set_mach_from_flags
49   PARAMS ((bfd *));
50 static bfd_boolean shmedia_prepare_reloc
51   PARAMS ((struct bfd_link_info *, bfd *, asection *,
52            bfd_byte *, const Elf_Internal_Rela *, bfd_vma *));
53 static int sh64_elf_get_symbol_type
54   PARAMS ((Elf_Internal_Sym *, int));
55 static bfd_boolean sh64_elf_add_symbol_hook
56   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
57            const char **, flagword *, asection **, bfd_vma *));
58 static bfd_boolean sh64_elf_link_output_symbol_hook
59   PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
60            asection *));
61 static bfd_boolean sh64_backend_section_from_shdr
62   PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
63 static void sh64_elf_final_write_processing
64   PARAMS ((bfd *, bfd_boolean));
65 static bfd_boolean sh64_bfd_elf_copy_private_section_data
66   PARAMS ((bfd *, asection *, bfd *, asection *));
67 static void sh64_find_section_for_address
68   PARAMS ((bfd *, asection *, PTR));
69
70 /* Let elf32-sh.c handle the "bfd_" definitions, so we only have to
71    intrude with an #ifndef around the function definition.  */
72 #define sh_elf_copy_private_data                sh64_elf_copy_private_data
73 #define sh_elf_merge_private_data               sh64_elf_merge_private_data
74 #define sh_elf_set_private_flags                sh64_elf_set_private_flags
75 /* Typo in elf32-sh.c (and unlinear name).  */
76 #define bfd_elf32_bfd_set_private_flags         sh64_elf_set_private_flags
77 #define sh_elf_set_mach_from_flags              sh64_elf_set_mach_from_flags
78
79 #define elf_backend_sign_extend_vma             1
80 #define elf_backend_fake_sections               sh64_elf_fake_sections
81 #define elf_backend_get_symbol_type             sh64_elf_get_symbol_type
82 #define elf_backend_add_symbol_hook             sh64_elf_add_symbol_hook
83 #define elf_backend_link_output_symbol_hook \
84         sh64_elf_link_output_symbol_hook
85 #define elf_backend_final_write_processing      sh64_elf_final_write_processing
86 #define elf_backend_section_from_shdr           sh64_backend_section_from_shdr
87
88 /* For objcopy, we need to set up sh64_elf_section_data (asection *) from
89    incoming section flags.  This is otherwise done in sh64elf.em when
90    linking or tc-sh64.c when assembling.  */
91 #define bfd_elf32_bfd_copy_private_section_data \
92         sh64_bfd_elf_copy_private_section_data
93
94 /* This COFF-only function (only compiled with COFF support, making
95    ELF-only chains problematic) returns TRUE early for SH4, so let's just
96    define it TRUE here.  */
97 #define _bfd_sh_align_load_span(a,b,c,d,e,f,g,h,i,j) TRUE
98
99 #define GOT_BIAS (-((long)-32768))
100 #define INCLUDE_SHMEDIA
101 #include "elf32-sh.c"
102
103 /* Set the SHF_SH5_ISA32 flag for ISA SHmedia code sections, and pass
104    through SHT_SH5_CR_SORTED on a sorted .cranges section.  */
105
106 bfd_boolean
107 sh64_elf_fake_sections (output_bfd, elf_section_hdr, asect)
108      bfd *output_bfd ATTRIBUTE_UNUSED;
109      Elf_Internal_Shdr *elf_section_hdr;
110      asection *asect;
111 {
112   if (sh64_elf_section_data (asect) != NULL)
113     elf_section_hdr->sh_flags
114       |= sh64_elf_section_data (asect)->contents_flags;
115
116   /* If this section has the SEC_SORT_ENTRIES flag set, it is a sorted
117      .cranges section passing through objcopy.  */
118   if ((bfd_get_section_flags (output_bfd, asect) & SEC_SORT_ENTRIES) != 0
119       && strcmp (bfd_get_section_name (output_bfd, asect),
120                  SH64_CRANGES_SECTION_NAME) == 0)
121     elf_section_hdr->sh_type = SHT_SH5_CR_SORTED;
122
123   return TRUE;
124 }
125
126 static bfd_boolean
127 sh64_elf_set_mach_from_flags (abfd)
128      bfd *abfd;
129 {
130   flagword flags = elf_elfheader (abfd)->e_flags;
131   asection *cranges;
132
133   switch (flags & EF_SH_MACH_MASK)
134     {
135     case EF_SH5:
136       /* These are fit to execute on SH5.  Just one but keep the switch
137          construct to make additions easy.  */
138       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh5);
139       break;
140
141     default:
142       bfd_set_error (bfd_error_wrong_format);
143       return FALSE;
144     }
145
146   /* We also need to set SEC_DEBUGGING on an incoming .cranges section.
147      We could have used elf_backend_section_flags if it had given us the
148      section name; the bfd_section member in the header argument is not
149      set at the point of the call.  FIXME: Find out whether that is by
150      undocumented design or a bug.  */
151   cranges = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
152   if (cranges != NULL
153       && ! bfd_set_section_flags (abfd, cranges,
154                                   bfd_get_section_flags (abfd, cranges)
155                                   | SEC_DEBUGGING))
156     return FALSE;
157
158   return TRUE;
159 }
160
161 static bfd_boolean
162 sh64_elf_copy_private_data (ibfd, obfd)
163      bfd * ibfd;
164      bfd * obfd;
165 {
166   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
167       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
168     return TRUE;
169
170   BFD_ASSERT (!elf_flags_init (obfd)
171               || (elf_elfheader (obfd)->e_flags
172                   == elf_elfheader (ibfd)->e_flags));
173
174   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
175   return TRUE;
176 }
177
178 static bfd_boolean
179 sh64_elf_merge_private_data (ibfd, obfd)
180      bfd *ibfd;
181      bfd *obfd;
182 {
183   flagword old_flags, new_flags;
184
185   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
186     return FALSE;
187
188   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
189       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
190     return TRUE;
191
192   if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd))
193     {
194       const char *msg;
195
196       if (bfd_get_arch_size (ibfd) == 32
197           && bfd_get_arch_size (obfd) == 64)
198         msg = _("%s: compiled as 32-bit object and %s is 64-bit");
199       else if (bfd_get_arch_size (ibfd) == 64
200                && bfd_get_arch_size (obfd) == 32)
201         msg = _("%s: compiled as 64-bit object and %s is 32-bit");
202       else
203         msg = _("%s: object size does not match that of target %s");
204
205       (*_bfd_error_handler) (msg, bfd_get_filename (ibfd),
206                              bfd_get_filename (obfd));
207       bfd_set_error (bfd_error_wrong_format);
208       return FALSE;
209     }
210
211   old_flags = elf_elfheader (obfd)->e_flags;
212   new_flags = elf_elfheader (ibfd)->e_flags;
213   if (! elf_flags_init (obfd))
214     {
215       /* This happens when ld starts out with a 'blank' output file.  */
216       elf_flags_init (obfd) = TRUE;
217       elf_elfheader (obfd)->e_flags = old_flags = new_flags;
218     }
219   /* We don't allow linking in non-SH64 code.  */
220   else if ((new_flags & EF_SH_MACH_MASK) != EF_SH5)
221     {
222       (*_bfd_error_handler)
223         ("%s: uses non-SH64 instructions while previous modules use SH64 instructions",
224          bfd_get_filename (ibfd));
225       bfd_set_error (bfd_error_bad_value);
226       return FALSE;
227     }
228
229   /* I can't think of anything sane other than old_flags being EF_SH5 and
230      that we need to preserve that.  */
231   elf_elfheader (obfd)->e_flags = old_flags;
232   return sh64_elf_set_mach_from_flags (obfd);
233 }
234
235 /* Handle a SH64-specific section when reading an object file.  This
236    is called when elfcode.h finds a section with an unknown type.
237
238    We only recognize SHT_SH5_CR_SORTED, on the .cranges section.  */
239
240 bfd_boolean
241 sh64_backend_section_from_shdr (abfd, hdr, name)
242      bfd *abfd;
243      Elf_Internal_Shdr *hdr;
244      const char *name;
245 {
246   flagword flags = 0;
247
248   /* We do like MIPS with a bit switch for recognized types, and returning
249      FALSE for a recognized section type with an unexpected name.  Right
250      now we only have one recognized type, but that might change.  */
251   switch (hdr->sh_type)
252     {
253     case SHT_SH5_CR_SORTED:
254       if (strcmp (name, SH64_CRANGES_SECTION_NAME) != 0)
255         return FALSE;
256
257       /* We set the SEC_SORT_ENTRIES flag so it can be passed on to
258          sh64_elf_fake_sections, keeping SHT_SH5_CR_SORTED if this object
259          passes through objcopy.  Perhaps it is brittle; the flag can
260          suddenly be used by other BFD parts, but it seems not really used
261          anywhere at the moment.  */
262       flags = SEC_DEBUGGING | SEC_SORT_ENTRIES;
263       break;
264
265     default:
266       return FALSE;
267     }
268
269   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
270     return FALSE;
271
272   if (flags
273       && ! bfd_set_section_flags (abfd, hdr->bfd_section,
274                                   bfd_get_section_flags (abfd,
275                                                          hdr->bfd_section)
276                                   | flags))
277     return FALSE;
278
279   return TRUE;
280 }
281
282 /* In contrast to sh64_backend_section_from_shdr, this is called for all
283    sections, but only when copying sections, not when linking or
284    assembling.  We need to set up the sh64_elf_section_data (asection *)
285    structure for the SH64 ELF section flags to be copied correctly.  */
286
287 bfd_boolean
288 sh64_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
289      bfd *ibfd;
290      asection *isec;
291      bfd *obfd;
292      asection *osec;
293 {
294   struct sh64_section_data *sh64_sec_data;
295
296   if (ibfd->xvec->flavour != bfd_target_elf_flavour
297       || obfd->xvec->flavour != bfd_target_elf_flavour)
298     return TRUE;
299
300   if (! _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec))
301     return FALSE;
302
303   sh64_sec_data = sh64_elf_section_data (isec);
304   if (sh64_sec_data == NULL)
305     {
306       sh64_sec_data = bfd_zmalloc (sizeof (struct sh64_section_data));
307
308       if (sh64_sec_data == NULL)
309         return FALSE;
310
311       sh64_sec_data->contents_flags
312         = (elf_section_data (isec)->this_hdr.sh_flags
313            & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
314
315       sh64_elf_section_data (osec) = sh64_sec_data;
316     }
317
318   return TRUE;
319 }
320
321 /* Function to keep SH64 specific file flags.  */
322
323 static bfd_boolean
324 sh64_elf_set_private_flags (abfd, flags)
325      bfd *    abfd;
326      flagword flags;
327 {
328   BFD_ASSERT (! elf_flags_init (abfd)
329               || elf_elfheader (abfd)->e_flags == flags);
330
331   elf_elfheader (abfd)->e_flags = flags;
332   elf_flags_init (abfd) = TRUE;
333   return sh64_elf_set_mach_from_flags (abfd);
334 }
335
336 /* Called when writing out an object file to decide the type of a symbol.  */
337
338 static int
339 sh64_elf_get_symbol_type (elf_sym, type)
340      Elf_Internal_Sym * elf_sym;
341      int type;
342 {
343   if (ELF_ST_TYPE (elf_sym->st_info) == STT_DATALABEL)
344     return STT_DATALABEL;
345
346   return type;
347 }
348
349 /* Hook called by the linker routine which adds symbols from an object
350    file.  We must make indirect symbols for undefined symbols marked with
351    STT_DATALABEL, so relocations passing them will pick up that attribute
352    and neutralize STO_SH5_ISA32 found on the symbol definition.
353
354    There is a problem, though: We want to fill in the hash-table entry for
355    this symbol and signal to the caller that no further processing is
356    needed.  But we don't have the index for this hash-table entry.  We
357    rely here on that the current entry is the first hash-entry with NULL,
358    which seems brittle.  Also, iterating over the hash-table to find that
359    entry is a linear operation on the number of symbols in this input
360    file, and this function should take constant time, so that's not good
361    too.  Only comfort is that DataLabel references should only be found in
362    hand-written assembly code and thus be rare.  FIXME: Talk maintainers
363    into adding an option to elf_add_symbol_hook (preferably) for the index
364    or the hash entry, alternatively adding the index to Elf_Internal_Sym
365    (not so good).  */
366
367 static bfd_boolean
368 sh64_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
369      bfd *abfd;
370      struct bfd_link_info *info;
371      const Elf_Internal_Sym *sym;
372      const char **namep;
373      flagword *flagsp ATTRIBUTE_UNUSED;
374      asection **secp;
375      bfd_vma *valp;
376 {
377   /* We want to do this for relocatable as well as final linking.  */
378   if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL
379       && info->hash->creator->flavour == bfd_target_elf_flavour)
380     {
381       struct elf_link_hash_entry *h;
382
383       /* For relocateable links, we register the DataLabel sym in its own
384          right, and tweak the name when it's output.  Otherwise, we make
385          an indirect symbol of it.  */
386       flagword flags
387         = info->relocateable || info->emitrelocations
388         ? BSF_GLOBAL : BSF_GLOBAL | BSF_INDIRECT;
389
390       char *dl_name
391         = bfd_malloc (strlen (*namep) + sizeof (DATALABEL_SUFFIX));
392       struct elf_link_hash_entry ** sym_hash = elf_sym_hashes (abfd);
393
394       BFD_ASSERT (sym_hash != NULL);
395
396       /* Allocation may fail.  */
397       if (dl_name == NULL)
398         return FALSE;
399
400       strcpy (dl_name, *namep);
401       strcat (dl_name, DATALABEL_SUFFIX);
402
403       h = (struct elf_link_hash_entry *)
404         bfd_link_hash_lookup (info->hash, dl_name, FALSE, FALSE, FALSE);
405
406       if (h == NULL)
407         {
408           /* No previous datalabel symbol.  Make one.  */
409           struct bfd_link_hash_entry *bh = NULL;
410           struct elf_backend_data *bed = get_elf_backend_data (abfd);
411
412           if (! _bfd_generic_link_add_one_symbol (info, abfd, dl_name,
413                                                   flags, *secp, *valp,
414                                                   *namep, FALSE,
415                                                   bed->collect, &bh))
416             {
417               free (dl_name);
418               return FALSE;
419             }
420
421           h = (struct elf_link_hash_entry *) bh;
422           h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
423           h->type = STT_DATALABEL;
424         }
425       else
426         /* If a new symbol was created, it holds the allocated name.
427            Otherwise, we don't need it anymore and should deallocate it.  */
428         free (dl_name);
429
430       if (h->type != STT_DATALABEL
431           || ((info->relocateable || info->emitrelocations)
432               && h->root.type != bfd_link_hash_undefined)
433           || (! info->relocateable && !info->emitrelocations
434               && h->root.type != bfd_link_hash_indirect))
435         {
436           /* Make sure we don't get confused on invalid input.  */
437           (*_bfd_error_handler)
438             (_("%s: encountered datalabel symbol in input"),
439              bfd_get_filename (abfd));
440           bfd_set_error (bfd_error_bad_value);
441           return FALSE;
442         }
443
444       /* Now find the hash-table slot for this entry and fill it in.  */
445       while (*sym_hash != NULL)
446         sym_hash++;
447       *sym_hash = h;
448
449       /* Signal to caller to skip this symbol - we've handled it.  */
450       *namep = NULL;
451     }
452
453   return TRUE;
454 }
455
456 /* This hook function is called before the linker writes out a global
457    symbol.  For relocatable links, DataLabel symbols will be present in
458    linker output.  We cut off the special suffix on those symbols, so the
459    right name appears in the output.
460
461    When linking and emitting relocations, there can appear global symbols
462    that are not referenced by relocs, but rather only implicitly through
463    DataLabel references, a relation that is not visible to the linker.
464    Since no stripping of global symbols in done when doing such linking,
465    we don't need to look up and make sure to emit the main symbol for each
466    DataLabel symbol.  */
467
468 bfd_boolean
469 sh64_elf_link_output_symbol_hook (abfd, info, cname, sym, input_sec)
470      bfd *abfd ATTRIBUTE_UNUSED;
471      struct bfd_link_info *info;
472      const char *cname;
473      Elf_Internal_Sym *sym;
474      asection *input_sec ATTRIBUTE_UNUSED;
475 {
476   char *name = (char *) cname;
477
478   if (info->relocateable || info->emitrelocations)
479     {
480       if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL)
481         name[strlen (name) - strlen (DATALABEL_SUFFIX)] = 0;
482     }
483
484   return TRUE;
485 }
486
487 /* Check a SH64-specific reloc and put the value to relocate to into
488    RELOCATION, ready to pass to _bfd_final_link_relocate.  Return FALSE if
489    bad value, TRUE if ok.  */
490
491 static bfd_boolean
492 shmedia_prepare_reloc (info, abfd, input_section,
493                        contents, rel, relocation)
494      struct bfd_link_info *info;
495      bfd *abfd;
496      asection *input_section;
497      bfd_byte *contents;
498      const Elf_Internal_Rela *rel;
499      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 (abfd, section, data)
605      bfd *abfd ATTRIBUTE_UNUSED;
606      asection *section;
607      PTR data;
608 {
609   bfd_vma vma;
610   bfd_size_type size;
611   struct sh64_find_section_vma_data *fsec_datap
612     = (struct sh64_find_section_vma_data *) data;
613
614   /* Return if already found.  */
615   if (fsec_datap->section)
616     return;
617
618   /* If this section isn't part of the addressable contents, skip it.  */
619   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
620     return;
621
622   vma = bfd_get_section_vma (abfd, section);
623   if (fsec_datap->addr < vma)
624     return;
625
626   /* FIXME: section->reloc_done isn't set properly; a generic buglet
627      preventing us from using bfd_get_section_size_after_reloc.  */
628   size
629     = section->_cooked_size ? section->_cooked_size : section->_raw_size;
630
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
642 static void
643 sh64_elf_final_write_processing (abfd, linker)
644      bfd *abfd;
645      bfd_boolean linker ATTRIBUTE_UNUSED;
646 {
647   bfd_vma ld_generated_cranges_size;
648   asection *cranges
649     = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
650
651   /* If no new .cranges were added, the generic ELF linker parts will
652      write it all out.  If not, we need to write them out when doing
653      partial linking.  For a final link, we will sort them and write them
654      all out further below.  */
655   if (linker
656       && cranges != NULL
657       && elf_elfheader (abfd)->e_type != ET_EXEC
658       && (ld_generated_cranges_size
659           = sh64_elf_section_data (cranges)->cranges_growth) != 0)
660     {
661       bfd_vma incoming_cranges_size
662         = ((cranges->_cooked_size != 0
663             ? cranges->_cooked_size : cranges->_raw_size)
664            - ld_generated_cranges_size);
665
666       if (! bfd_set_section_contents (abfd, cranges,
667                                       cranges->contents
668                                       + incoming_cranges_size,
669                                       cranges->output_offset
670                                       + incoming_cranges_size,
671                                       ld_generated_cranges_size))
672         {
673           bfd_set_error (bfd_error_file_truncated);
674           (*_bfd_error_handler)
675             (_("%s: could not write out added .cranges entries"),
676              bfd_get_filename (abfd));
677         }
678     }
679
680   /* Only set entry address bit 0 and sort .cranges when linking to an
681      executable; never with objcopy or strip.  */
682   if (linker && elf_elfheader (abfd)->e_type == ET_EXEC)
683     {
684       struct sh64_find_section_vma_data fsec_data;
685       sh64_elf_crange dummy;
686
687       /* For a final link, set the low bit of the entry address to
688          reflect whether or not it is a SHmedia address.
689          FIXME: Perhaps we shouldn't do this if the entry address was
690          supplied numerically, but we currently lack the infrastructure to
691          recognize that: The entry symbol, and info whether it is numeric
692          or a symbol name is kept private in the linker.  */
693       fsec_data.addr = elf_elfheader (abfd)->e_entry;
694       fsec_data.section = NULL;
695
696       bfd_map_over_sections (abfd, sh64_find_section_for_address,
697                              (PTR) &fsec_data);
698       if (fsec_data.section
699           && (sh64_get_contents_type (fsec_data.section,
700                                       elf_elfheader (abfd)->e_entry,
701                                       &dummy) == CRT_SH5_ISA32))
702         elf_elfheader (abfd)->e_entry |= 1;
703
704       /* If we have a .cranges section, sort the entries.  */
705       if (cranges != NULL)
706         {
707           bfd_size_type cranges_size
708             = (cranges->_cooked_size != 0
709                ? cranges->_cooked_size : cranges->_raw_size);
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)
723                      ? _bfd_sh64_crange_qsort_cmpb
724                      : _bfd_sh64_crange_qsort_cmpl);
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);
736               (*_bfd_error_handler)
737                 (_("%s: could not write out sorted .cranges entries"),
738                  bfd_get_filename (abfd));
739             }
740         }
741     }
742 }
743
744 #undef  TARGET_BIG_SYM
745 #define TARGET_BIG_SYM          bfd_elf32_sh64_vec
746 #undef  TARGET_BIG_NAME
747 #define TARGET_BIG_NAME         "elf32-sh64"
748 #undef  TARGET_LITTLE_SYM
749 #define TARGET_LITTLE_SYM       bfd_elf32_sh64l_vec
750 #undef  TARGET_LITTLE_NAME
751 #define TARGET_LITTLE_NAME      "elf32-sh64l"
752
753 #include "elf32-target.h"
754
755 /* NetBSD support.  */
756 #undef  TARGET_BIG_SYM
757 #define TARGET_BIG_SYM          bfd_elf32_sh64nbsd_vec
758 #undef  TARGET_BIG_NAME
759 #define TARGET_BIG_NAME         "elf32-sh64-nbsd"
760 #undef  TARGET_LITTLE_SYM
761 #define TARGET_LITTLE_SYM       bfd_elf32_sh64lnbsd_vec
762 #undef  TARGET_LITTLE_NAME
763 #define TARGET_LITTLE_NAME      "elf32-sh64l-nbsd"
764 #undef  ELF_MAXPAGESIZE
765 #define ELF_MAXPAGESIZE         0x10000
766 #undef  elf_symbol_leading_char
767 #define elf_symbol_leading_char 0
768
769 #define elf32_bed               elf32_sh64_nbsd_bed
770
771 #include "elf32-target.h"
772
773 #undef  elf32_bed
774
775 /* Linux support.  */
776 #undef  TARGET_BIG_SYM
777 #define TARGET_BIG_SYM          bfd_elf32_sh64blin_vec
778 #undef  TARGET_BIG_NAME
779 #define TARGET_BIG_NAME         "elf32-sh64big-linux"
780 #undef  TARGET_LITTLE_SYM
781 #define TARGET_LITTLE_SYM       bfd_elf32_sh64lin_vec
782 #undef  TARGET_LITTLE_NAME
783 #define TARGET_LITTLE_NAME      "elf32-sh64-linux"
784
785 #define elf32_bed               elf32_sh64_lin_bed
786
787 #include "elf32-target.h"
788
789 #undef  elf32_bed