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