Updated Turkish translation.
[external/binutils.git] / bfd / elf32-sh64.c
1 /* Hitachi SH64-specific support for 32-bit ELF
2    Copyright (C) 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 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 *,
43                                               asection *));
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 *,
55            asection *));
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));
62
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
71
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
80
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
86
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
91
92 #ifndef ELF_ARCH
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
100
101 #define elf_symbol_leading_char '_'
102 #endif /* ELF_ARCH */
103
104 #define GOT_BIAS (-((long)-32768))
105 #define INCLUDE_SHMEDIA
106 #include "elf32-sh.c"
107
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.  */
110
111 boolean
112 sh64_elf_fake_sections (output_bfd, elf_section_hdr, asect)
113      bfd *output_bfd ATTRIBUTE_UNUSED;
114      Elf_Internal_Shdr *elf_section_hdr;
115      asection *asect;
116 {
117   if (sh64_elf_section_data (asect) != NULL)
118     elf_section_hdr->sh_flags
119       |= sh64_elf_section_data (asect)->contents_flags;
120
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;
127
128   return true;
129 }
130
131 static boolean
132 sh64_elf_set_mach_from_flags (abfd)
133      bfd *abfd;
134 {
135   flagword flags = elf_elfheader (abfd)->e_flags;
136   asection *cranges;
137
138   switch (flags & EF_SH_MACH_MASK)
139     {
140     case EF_SH5:
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);
144       break;
145
146     default:
147       bfd_set_error (bfd_error_wrong_format);
148       return false;
149     }
150
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);
157   if (cranges != NULL
158       && ! bfd_set_section_flags (abfd, cranges,
159                                   bfd_get_section_flags (abfd, cranges)
160                                   | SEC_DEBUGGING))
161     return false;
162
163   return true;
164 }
165
166 static boolean
167 sh64_elf_copy_private_data (ibfd, obfd)
168      bfd * ibfd;
169      bfd * obfd;
170 {
171   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
172       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
173     return true;
174
175   BFD_ASSERT (!elf_flags_init (obfd)
176               || (elf_elfheader (obfd)->e_flags
177                   == elf_elfheader (ibfd)->e_flags));
178
179   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
180   return true;
181 }
182
183 static boolean
184 sh64_elf_merge_private_data (ibfd, obfd)
185      bfd *ibfd;
186      bfd *obfd;
187 {
188   flagword old_flags, new_flags;
189
190   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
191     return false;
192
193   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
194       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
195     return true;
196
197   if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd))
198     {
199       const char *msg;
200
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");
207       else
208         msg = _("%s: object size does not match that of target %s");
209
210       (*_bfd_error_handler) (msg, bfd_get_filename (ibfd),
211                              bfd_get_filename (obfd));
212       bfd_set_error (bfd_error_wrong_format);
213       return false;
214     }
215
216   old_flags = elf_elfheader (obfd)->e_flags;
217   new_flags = elf_elfheader (ibfd)->e_flags;
218   if (! elf_flags_init (obfd))
219     {
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;
223     }
224   /* We don't allow linking in non-SH64 code.  */
225   else if ((new_flags & EF_SH_MACH_MASK) != EF_SH5)
226     {
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);
231       return false;
232     }
233
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);
238 }
239
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.
242
243    We only recognize SHT_SH5_CR_SORTED, on the .cranges section.  */
244
245 boolean
246 sh64_backend_section_from_shdr (abfd, hdr, name)
247      bfd *abfd;
248      Elf_Internal_Shdr *hdr;
249      const char *name;
250 {
251   flagword flags = 0;
252
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)
257     {
258     case SHT_SH5_CR_SORTED:
259       if (strcmp (name, SH64_CRANGES_SECTION_NAME) != 0)
260         return false;
261
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;
268       break;
269
270     default:
271       return false;
272     }
273
274   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
275     return false;
276
277   if (flags
278       && ! bfd_set_section_flags (abfd, hdr->bfd_section,
279                                   bfd_get_section_flags (abfd,
280                                                          hdr->bfd_section)
281                                   | flags))
282     return false;
283
284   return true;
285 }
286
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.  */
291
292 boolean
293 sh64_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
294      bfd *ibfd;
295      asection *isec;
296      bfd *obfd;
297      asection *osec;
298 {
299   struct sh64_section_data *sh64_sec_data;
300
301   if (ibfd->xvec->flavour != bfd_target_elf_flavour
302       || obfd->xvec->flavour != bfd_target_elf_flavour)
303     return true;
304
305   if (! _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec))
306     return false;
307
308   sh64_sec_data = sh64_elf_section_data (isec);
309   if (sh64_sec_data == NULL)
310     {
311       sh64_sec_data = bfd_zmalloc (sizeof (struct sh64_section_data));
312
313       if (sh64_sec_data == NULL)
314         return false;
315
316       sh64_sec_data->contents_flags
317         = (elf_section_data (isec)->this_hdr.sh_flags
318            & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
319
320       sh64_elf_section_data (osec) = sh64_sec_data;
321     }
322
323   return true;
324 }
325
326 /* Function to keep SH64 specific file flags.  */
327
328 static boolean
329 sh64_elf_set_private_flags (abfd, flags)
330      bfd *    abfd;
331      flagword flags;
332 {
333   BFD_ASSERT (! elf_flags_init (abfd)
334               || elf_elfheader (abfd)->e_flags == flags);
335
336   elf_elfheader (abfd)->e_flags = flags;
337   elf_flags_init (abfd) = true;
338   return sh64_elf_set_mach_from_flags (abfd);
339 }
340
341 /* Called when writing out an object file to decide the type of a symbol.  */
342
343 static int
344 sh64_elf_get_symbol_type (elf_sym, type)
345      Elf_Internal_Sym * elf_sym;
346      int type;
347 {
348   if (ELF_ST_TYPE (elf_sym->st_info) == STT_DATALABEL)
349     return STT_DATALABEL;
350
351   return type;
352 }
353
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.
358
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
370    (not so good).  */
371
372 static boolean
373 sh64_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
374      bfd *abfd;
375      struct bfd_link_info *info;
376      const Elf_Internal_Sym *sym;
377      const char **namep;
378      flagword *flagsp ATTRIBUTE_UNUSED;
379      asection **secp;
380      bfd_vma *valp;
381 {
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)
385     {
386       struct elf_link_hash_entry *h;
387
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.  */
391       flagword flags
392         = info->relocateable || info->emitrelocations
393         ? BSF_GLOBAL : BSF_GLOBAL | BSF_INDIRECT;
394
395       char *dl_name
396         = bfd_malloc (strlen (*namep) + sizeof (DATALABEL_SUFFIX));
397       struct elf_link_hash_entry ** sym_hash = elf_sym_hashes (abfd);
398
399       BFD_ASSERT (sym_hash != NULL);
400
401       /* Allocation may fail.  */
402       if (dl_name == NULL)
403         return false;
404
405       strcpy (dl_name, *namep);
406       strcat (dl_name, DATALABEL_SUFFIX);
407
408       h = (struct elf_link_hash_entry *)
409         bfd_link_hash_lookup (info->hash, dl_name, false, false, false);
410
411       if (h == NULL)
412         {
413           /* No previous datalabel symbol.  Make one.  */
414           if (! _bfd_generic_link_add_one_symbol (info, abfd, dl_name,
415                                                   flags, *secp, *valp,
416                                                   *namep, false,
417                                                   get_elf_backend_data (abfd)->collect,
418                                                   (struct bfd_link_hash_entry **) &h))
419             {
420               free (dl_name);
421               return false;
422             }
423
424           h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
425           h->type = STT_DATALABEL;
426         }
427       else
428         /* If a new symbol was created, it holds the allocated name.
429            Otherwise, we don't need it anymore and should deallocate it.  */
430         free (dl_name);
431
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))
437         {
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);
443           return false;
444         }
445
446       /* Now find the hash-table slot for this entry and fill it in.  */
447       while (*sym_hash != NULL)
448         sym_hash++;
449       *sym_hash = h;
450
451       /* Signal to caller to skip this symbol - we've handled it.  */
452       *namep = NULL;
453     }
454
455   return true;
456 }
457
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.
462
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
468    DataLabel symbol.  */
469
470 boolean
471 sh64_elf_link_output_symbol_hook (abfd, info, cname, sym, input_sec)
472      bfd *abfd ATTRIBUTE_UNUSED;
473      struct bfd_link_info *info;
474      const char *cname;
475      Elf_Internal_Sym *sym;
476      asection *input_sec ATTRIBUTE_UNUSED;
477 {
478   char *name = (char *) cname;
479
480   if (info->relocateable || info->emitrelocations)
481     {
482       if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL)
483         name[strlen (name) - strlen (DATALABEL_SUFFIX)] = 0;
484     }
485
486   return true;
487 }
488
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.  */
492
493 static boolean
494 shmedia_prepare_reloc (info, abfd, input_section,
495                        contents, rel, relocation)
496      struct bfd_link_info *info;
497      bfd *abfd;
498      asection *input_section;
499      bfd_byte *contents;
500      const Elf_Internal_Rela *rel;
501      bfd_vma *relocation;
502 {
503   bfd_vma disp, dropped;
504
505   switch (ELF32_R_TYPE (rel->r_info))
506     {
507     case R_SH_PT_16:
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.  */
514       {
515         char *msg = NULL;
516         bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
517
518         if (insn & (1 << 10))
519           {
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)");
527
528             if (msg != NULL
529                 && ! ((*info->callbacks->reloc_dangerous)
530                       (info, msg, abfd, input_section,
531                        rel->r_offset)))
532               return false;
533           }
534         else
535           {
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)
542               {
543                 (*_bfd_error_handler)
544                   (_("%s: GAS error: unexpected PTB insn with R_SH_PT_16"),
545                    bfd_get_filename (input_section->owner));
546                 return false;
547               }
548
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);
553           }
554       }
555
556     case R_SH_SHMEDIA_CODE:
557     case R_SH_DIR5U:
558     case R_SH_DIR6S:
559     case R_SH_DIR6U:
560     case R_SH_DIR10S:
561     case R_SH_DIR10SW:
562     case R_SH_DIR10SL:
563     case R_SH_DIR10SQ:
564     case R_SH_IMMS16:
565     case R_SH_IMMU16:
566     case R_SH_IMM_LOW16:
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:
572     case R_SH_IMM_HI16:
573     case R_SH_IMM_HI16_PCREL:
574     case R_SH_64:
575     case R_SH_64_PCREL:
576       break;
577
578     default:
579       return false;
580     }
581
582   disp = (*relocation & 0xf);
583   dropped = 0;
584   switch (ELF32_R_TYPE (rel->r_info))
585     {
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;
589     }
590   if (dropped != 0)
591     {
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);
596       return false;
597     }
598
599   return true;
600 }
601
602 /* Helper function to locate the section holding a certain address.  This
603    is called via bfd_map_over_sections.  */
604
605 static void
606 sh64_find_section_for_address (abfd, section, data)
607      bfd *abfd ATTRIBUTE_UNUSED;
608      asection *section;
609      PTR data;
610 {
611   bfd_vma vma;
612   bfd_size_type size;
613   struct sh64_find_section_vma_data *fsec_datap
614     = (struct sh64_find_section_vma_data *) data;
615
616   /* Return if already found.  */
617   if (fsec_datap->section)
618     return;
619
620   /* If this section isn't part of the addressable contents, skip it.  */
621   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
622     return;
623
624   vma = bfd_get_section_vma (abfd, section);
625   if (fsec_datap->addr < vma)
626     return;
627
628   /* FIXME: section->reloc_done isn't set properly; a generic buglet
629      preventing us from using bfd_get_section_size_after_reloc.  */
630   size
631     = section->_cooked_size ? section->_cooked_size : section->_raw_size;
632
633   if (fsec_datap->addr >= vma + size)
634     return;
635
636   fsec_datap->section = section;
637 }
638
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).  */
643
644 static void
645 sh64_elf_final_write_processing (abfd, linker)
646      bfd *   abfd;
647      boolean linker ATTRIBUTE_UNUSED;
648 {
649   bfd_vma ld_generated_cranges_size;
650   asection *cranges
651     = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
652
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.  */
657   if (linker
658       && cranges != NULL
659       && elf_elfheader (abfd)->e_type != ET_EXEC
660       && (ld_generated_cranges_size
661           = sh64_elf_section_data (cranges)->cranges_growth) != 0)
662     {
663       bfd_vma incoming_cranges_size
664         = ((cranges->_cooked_size != 0
665             ? cranges->_cooked_size : cranges->_raw_size)
666            - ld_generated_cranges_size);
667
668       if (! bfd_set_section_contents (abfd, cranges,
669                                       cranges->contents
670                                       + incoming_cranges_size,
671                                       cranges->output_offset
672                                       + incoming_cranges_size,
673                                       ld_generated_cranges_size))
674         {
675           bfd_set_error (bfd_error_file_truncated);
676           (*_bfd_error_handler)
677             (_("%s: could not write out added .cranges entries"),
678              bfd_get_filename (abfd));
679         }
680     }
681
682   /* Only set entry address bit 0 and sort .cranges when linking to an
683      executable; never with objcopy or strip.  */
684   if (linker && elf_elfheader (abfd)->e_type == ET_EXEC)
685     {
686       struct sh64_find_section_vma_data fsec_data;
687       sh64_elf_crange dummy;
688
689       /* For a final link, set the low bit of the entry address to
690          reflect whether or not it is a SHmedia address.
691          FIXME: Perhaps we shouldn't do this if the entry address was
692          supplied numerically, but we currently lack the infrastructure to
693          recognize that: The entry symbol, and info whether it is numeric
694          or a symbol name is kept private in the linker.  */
695       fsec_data.addr = elf_elfheader (abfd)->e_entry;
696       fsec_data.section = NULL;
697
698       bfd_map_over_sections (abfd, sh64_find_section_for_address,
699                              (PTR) &fsec_data);
700       if (fsec_data.section
701           && (sh64_get_contents_type (fsec_data.section,
702                                       elf_elfheader (abfd)->e_entry,
703                                       &dummy) == CRT_SH5_ISA32))
704         elf_elfheader (abfd)->e_entry |= 1;
705
706       /* If we have a .cranges section, sort the entries.  */
707       if (cranges != NULL)
708         {
709           bfd_size_type cranges_size
710             = (cranges->_cooked_size != 0
711                ? cranges->_cooked_size : cranges->_raw_size);
712
713           /* We know we always have these in memory at this time.  */
714           BFD_ASSERT (cranges->contents != NULL);
715
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
718              it here.  */
719           if (elf_section_data (cranges)->this_hdr.sh_type
720               != SHT_SH5_CR_SORTED)
721             {
722               qsort (cranges->contents, cranges_size / SH64_CRANGE_SIZE,
723                      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
728                 = SHT_SH5_CR_SORTED;
729             }
730
731           /* We need to write it out in whole as sorted.  */
732           if (! bfd_set_section_contents (abfd, cranges,
733                                           cranges->contents,
734                                           cranges->output_offset,
735                                           cranges_size))
736             {
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));
741             }
742         }
743     }
744 }