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