2002-05-23 H.J. Lu <hjl@gnu.org>
[binutils.git] / bfd / elf32-sh64.c
blob5e7ccdd68223b57819623e164ba8a35592a5c9c1
1 /* Hitachi SH64-specific support for 32-bit ELF
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
20 #define SH64_ELF
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "elf-bfd.h"
25 #include "../opcodes/sh64-opc.h"
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"
33 /* Used to hold data for function called through bfd_map_over_sections. */
34 struct sh64_find_section_vma_data
36 asection *section;
37 bfd_vma addr;
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 *, 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));
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
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
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
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
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
101 #define elf_symbol_leading_char '_'
102 #endif /* ELF_ARCH */
104 #define GOT_BIAS (-((long)-32768))
105 #define INCLUDE_SHMEDIA
106 #include "elf32-sh.c"
108 /* The type sh64_elf_crange is defined in elf/sh.h which is included in
109 elf32-sh.c, hence these prototypes located after including it. */
110 static int crange_qsort_cmpb PARAMS ((const void *, const void *));
111 static int crange_qsort_cmpl PARAMS ((const void *, const void *));
112 static int crange_bsearch_cmpb PARAMS ((const void *, const void *));
113 static int crange_bsearch_cmpl PARAMS ((const void *, const void *));
114 static boolean sh64_address_in_cranges
115 PARAMS ((asection *cranges, bfd_vma, sh64_elf_crange *));
117 /* Set the SHF_SH5_ISA32 flag for ISA SHmedia code sections, and pass
118 through SHT_SH5_CR_SORTED on a sorted .cranges section. */
120 boolean
121 sh64_elf_fake_sections (output_bfd, elf_section_hdr, asect)
122 bfd *output_bfd ATTRIBUTE_UNUSED;
123 Elf_Internal_Shdr *elf_section_hdr;
124 asection *asect;
126 if (sh64_elf_section_data (asect) != NULL)
127 elf_section_hdr->sh_flags
128 |= sh64_elf_section_data (asect)->contents_flags;
130 /* If this section has the SEC_SORT_ENTRIES flag set, it is a sorted
131 .cranges section passing through objcopy. */
132 if ((bfd_get_section_flags (output_bfd, asect) & SEC_SORT_ENTRIES) != 0
133 && strcmp (bfd_get_section_name (output_bfd, asect),
134 SH64_CRANGES_SECTION_NAME) == 0)
135 elf_section_hdr->sh_type = SHT_SH5_CR_SORTED;
137 return true;
140 static boolean
141 sh64_elf_set_mach_from_flags (abfd)
142 bfd *abfd;
144 flagword flags = elf_elfheader (abfd)->e_flags;
145 asection *cranges;
147 switch (flags & EF_SH_MACH_MASK)
149 case EF_SH5:
150 /* These are fit to execute on SH5. Just one but keep the switch
151 construct to make additions easy. */
152 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh5);
153 break;
155 default:
156 bfd_set_error (bfd_error_wrong_format);
157 return false;
160 /* We also need to set SEC_DEBUGGING on an incoming .cranges section.
161 We could have used elf_backend_section_flags if it had given us the
162 section name; the bfd_section member in the header argument is not
163 set at the point of the call. FIXME: Find out whether that is by
164 undocumented design or a bug. */
165 cranges = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
166 if (cranges != NULL
167 && ! bfd_set_section_flags (abfd, cranges,
168 bfd_get_section_flags (abfd, cranges)
169 | SEC_DEBUGGING))
170 return false;
172 return true;
175 static boolean
176 sh64_elf_copy_private_data (ibfd, obfd)
177 bfd * ibfd;
178 bfd * obfd;
180 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
181 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
182 return true;
184 BFD_ASSERT (!elf_flags_init (obfd)
185 || (elf_elfheader (obfd)->e_flags
186 == elf_elfheader (ibfd)->e_flags));
188 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
189 return true;
192 static boolean
193 sh64_elf_merge_private_data (ibfd, obfd)
194 bfd *ibfd;
195 bfd *obfd;
197 flagword old_flags, new_flags;
199 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
200 return false;
202 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
203 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
204 return true;
206 if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd))
208 const char *msg;
210 if (bfd_get_arch_size (ibfd) == 32
211 && bfd_get_arch_size (obfd) == 64)
212 msg = _("%s: compiled as 32-bit object and %s is 64-bit");
213 else if (bfd_get_arch_size (ibfd) == 64
214 && bfd_get_arch_size (obfd) == 32)
215 msg = _("%s: compiled as 64-bit object and %s is 32-bit");
216 else
217 msg = _("%s: object size does not match that of target %s");
219 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd),
220 bfd_get_filename (obfd));
221 bfd_set_error (bfd_error_wrong_format);
222 return false;
225 old_flags = elf_elfheader (obfd)->e_flags;
226 new_flags = elf_elfheader (ibfd)->e_flags;
227 if (! elf_flags_init (obfd))
229 /* This happens when ld starts out with a 'blank' output file. */
230 elf_flags_init (obfd) = true;
231 elf_elfheader (obfd)->e_flags = old_flags = new_flags;
233 /* We don't allow linking in non-SH64 code. */
234 else if ((new_flags & EF_SH_MACH_MASK) != EF_SH5)
236 (*_bfd_error_handler)
237 ("%s: uses non-SH64 instructions while previous modules use SH64 instructions",
238 bfd_get_filename (ibfd));
239 bfd_set_error (bfd_error_bad_value);
240 return false;
243 /* I can't think of anything sane other than old_flags being EF_SH5 and
244 that we need to preserve that. */
245 elf_elfheader (obfd)->e_flags = old_flags;
246 return sh64_elf_set_mach_from_flags (obfd);
249 /* Handle a SH64-specific section when reading an object file. This
250 is called when elfcode.h finds a section with an unknown type.
252 We only recognize SHT_SH5_CR_SORTED, on the .cranges section. */
254 boolean
255 sh64_backend_section_from_shdr (abfd, hdr, name)
256 bfd *abfd;
257 Elf_Internal_Shdr *hdr;
258 char *name;
260 flagword flags = 0;
262 /* We do like MIPS with a bit switch for recognized types, and returning
263 false for a recognized section type with an unexpected name. Right
264 now we only have one recognized type, but that might change. */
265 switch (hdr->sh_type)
267 case SHT_SH5_CR_SORTED:
268 if (strcmp (name, SH64_CRANGES_SECTION_NAME) != 0)
269 return false;
271 /* We set the SEC_SORT_ENTRIES flag so it can be passed on to
272 sh64_elf_fake_sections, keeping SHT_SH5_CR_SORTED if this object
273 passes through objcopy. Perhaps it is brittle; the flag can
274 suddenly be used by other BFD parts, but it seems not really used
275 anywhere at the moment. */
276 flags = SEC_DEBUGGING | SEC_SORT_ENTRIES;
277 break;
279 default:
280 return false;
283 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
284 return false;
286 if (flags
287 && ! bfd_set_section_flags (abfd, hdr->bfd_section,
288 bfd_get_section_flags (abfd,
289 hdr->bfd_section)
290 | flags))
291 return false;
293 return true;
296 /* In contrast to sh64_backend_section_from_shdr, this is called for all
297 sections, but only when copying sections, not when linking or
298 assembling. We need to set up the sh64_elf_section_data (asection *)
299 structure for the SH64 ELF section flags to be copied correctly. */
301 boolean
302 sh64_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
303 bfd *ibfd;
304 asection *isec;
305 bfd *obfd;
306 asection *osec;
308 struct sh64_section_data *sh64_sec_data;
310 if (ibfd->xvec->flavour != bfd_target_elf_flavour
311 || obfd->xvec->flavour != bfd_target_elf_flavour)
312 return true;
314 if (! _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec))
315 return false;
317 sh64_sec_data = sh64_elf_section_data (isec);
318 if (sh64_sec_data == NULL)
320 sh64_sec_data = bfd_zmalloc (sizeof (struct sh64_section_data));
322 if (sh64_sec_data == NULL)
323 return false;
325 sh64_sec_data->contents_flags
326 = (elf_section_data (isec)->this_hdr.sh_flags
327 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
329 sh64_elf_section_data (osec) = sh64_sec_data;
332 return true;
335 /* Function to keep SH64 specific file flags. */
337 static boolean
338 sh64_elf_set_private_flags (abfd, flags)
339 bfd * abfd;
340 flagword flags;
342 BFD_ASSERT (! elf_flags_init (abfd)
343 || elf_elfheader (abfd)->e_flags == flags);
345 elf_elfheader (abfd)->e_flags = flags;
346 elf_flags_init (abfd) = true;
347 return sh64_elf_set_mach_from_flags (abfd);
350 /* Called when writing out an object file to decide the type of a symbol. */
352 static int
353 sh64_elf_get_symbol_type (elf_sym, type)
354 Elf_Internal_Sym * elf_sym;
355 int type;
357 if (ELF_ST_TYPE (elf_sym->st_info) == STT_DATALABEL)
358 return STT_DATALABEL;
360 return type;
363 /* Hook called by the linker routine which adds symbols from an object
364 file. We must make indirect symbols for undefined symbols marked with
365 STT_DATALABEL, so relocations passing them will pick up that attribute
366 and neutralize STO_SH5_ISA32 found on the symbol definition.
368 There is a problem, though: We want to fill in the hash-table entry for
369 this symbol and signal to the caller that no further processing is
370 needed. But we don't have the index for this hash-table entry. We
371 rely here on that the current entry is the first hash-entry with NULL,
372 which seems brittle. Also, iterating over the hash-table to find that
373 entry is a linear operation on the number of symbols in this input
374 file, and this function should take constant time, so that's not good
375 too. Only comfort is that DataLabel references should only be found in
376 hand-written assembly code and thus be rare. FIXME: Talk maintainers
377 into adding an option to elf_add_symbol_hook (preferably) for the index
378 or the hash entry, alternatively adding the index to Elf_Internal_Sym
379 (not so good). */
381 static boolean
382 sh64_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
383 bfd *abfd;
384 struct bfd_link_info *info;
385 const Elf_Internal_Sym *sym;
386 const char **namep;
387 flagword *flagsp ATTRIBUTE_UNUSED;
388 asection **secp;
389 bfd_vma *valp;
391 /* We want to do this for relocatable as well as final linking. */
392 if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL
393 && info->hash->creator->flavour == bfd_target_elf_flavour)
395 struct elf_link_hash_entry *h;
397 /* For relocateable links, we register the DataLabel sym in its own
398 right, and tweak the name when it's output. Otherwise, we make
399 an indirect symbol of it. */
400 flagword flags
401 = info->relocateable || info->emitrelocations
402 ? BSF_GLOBAL : BSF_GLOBAL | BSF_INDIRECT;
404 char *dl_name
405 = bfd_malloc (strlen (*namep) + sizeof (DATALABEL_SUFFIX));
406 struct elf_link_hash_entry ** sym_hash = elf_sym_hashes (abfd);
408 BFD_ASSERT (sym_hash != NULL);
410 /* Allocation may fail. */
411 if (dl_name == NULL)
412 return false;
414 strcpy (dl_name, *namep);
415 strcat (dl_name, DATALABEL_SUFFIX);
417 h = (struct elf_link_hash_entry *)
418 bfd_link_hash_lookup (info->hash, dl_name, false, false, false);
420 if (h == NULL)
422 /* No previous datalabel symbol. Make one. */
423 if (! _bfd_generic_link_add_one_symbol (info, abfd, dl_name,
424 flags, *secp, *valp,
425 *namep, false,
426 get_elf_backend_data (abfd)->collect,
427 (struct bfd_link_hash_entry **) &h))
429 free (dl_name);
430 return false;
433 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
434 h->type = STT_DATALABEL;
436 else
437 /* If a new symbol was created, it holds the allocated name.
438 Otherwise, we don't need it anymore and should deallocate it. */
439 free (dl_name);
441 if (h->type != STT_DATALABEL
442 || ((info->relocateable || info->emitrelocations)
443 && h->root.type != bfd_link_hash_undefined)
444 || (! info->relocateable && !info->emitrelocations
445 && h->root.type != bfd_link_hash_indirect))
447 /* Make sure we don't get confused on invalid input. */
448 (*_bfd_error_handler)
449 (_("%s: encountered datalabel symbol in input"),
450 bfd_get_filename (abfd));
451 bfd_set_error (bfd_error_bad_value);
452 return false;
455 /* Now find the hash-table slot for this entry and fill it in. */
456 while (*sym_hash != NULL)
457 sym_hash++;
458 *sym_hash = h;
460 /* Signal to caller to skip this symbol - we've handled it. */
461 *namep = NULL;
464 return true;
467 /* This hook function is called before the linker writes out a global
468 symbol. For relocatable links, DataLabel symbols will be present in
469 linker output. We cut off the special suffix on those symbols, so the
470 right name appears in the output.
472 When linking and emitting relocations, there can appear global symbols
473 that are not referenced by relocs, but rather only implicitly through
474 DataLabel references, a relation that is not visible to the linker.
475 Since no stripping of global symbols in done when doing such linking,
476 we don't need to look up and make sure to emit the main symbol for each
477 DataLabel symbol. */
479 boolean
480 sh64_elf_link_output_symbol_hook (abfd, info, cname, sym, input_sec)
481 bfd *abfd ATTRIBUTE_UNUSED;
482 struct bfd_link_info *info;
483 const char *cname;
484 Elf_Internal_Sym *sym;
485 asection *input_sec ATTRIBUTE_UNUSED;
487 char *name = (char *) cname;
489 if (info->relocateable || info->emitrelocations)
491 if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL)
492 name[strlen (name) - strlen (DATALABEL_SUFFIX)] = 0;
495 return true;
498 /* Check a SH64-specific reloc and put the value to relocate to into
499 RELOCATION, ready to pass to _bfd_final_link_relocate. Return FALSE if
500 bad value, TRUE if ok. */
502 static boolean
503 shmedia_prepare_reloc (info, abfd, input_section,
504 contents, rel, relocation)
505 struct bfd_link_info *info;
506 bfd *abfd;
507 asection *input_section;
508 bfd_byte *contents;
509 const Elf_Internal_Rela *rel;
510 bfd_vma *relocation;
512 bfd_vma disp, dropped;
514 switch (ELF32_R_TYPE (rel->r_info))
516 case R_SH_PT_16:
517 /* Check the lowest bit of the destination field. If it is 1, we
518 check the ISA type of the destination (i.e. the low bit of the
519 "relocation" value, and emit an error if the instruction does not
520 match). If it is 0, we change a PTA to PTB. There should never
521 be a PTB that should change to a PTA; that indicates a toolchain
522 error; a mismatch with GAS. */
524 char *msg = NULL;
525 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
527 if (insn & (1 << 10))
529 /* Check matching insn and ISA (address of target). */
530 if ((insn & SHMEDIA_PTB_BIT) != 0
531 && ((*relocation + rel->r_addend) & 1) != 0)
532 msg = _("PTB mismatch: a SHmedia address (bit 0 == 1)");
533 else if ((insn & SHMEDIA_PTB_BIT) == 0
534 && ((*relocation + rel->r_addend) & 1) == 0)
535 msg = _("PTA mismatch: a SHcompact address (bit 0 == 0)");
537 if (msg != NULL
538 && ! ((*info->callbacks->reloc_dangerous)
539 (info, msg, abfd, input_section,
540 rel->r_offset)))
541 return false;
543 else
545 /* We shouldn't get here with a PTB insn and a R_SH_PT_16. It
546 means GAS output does not match expectations; a PTA or PTB
547 expressed as such (or a PT found at assembly to be PTB)
548 would match the test above, and PT expansion with an
549 unknown destination (or when relaxing) will get us here. */
550 if ((insn & SHMEDIA_PTB_BIT) != 0)
552 (*_bfd_error_handler)
553 (_("%s: GAS error: unexpected PTB insn with R_SH_PT_16"),
554 bfd_get_filename (input_section->owner));
555 return false;
558 /* Change the PTA to a PTB, if destination indicates so. */
559 if (((*relocation + rel->r_addend) & 1) == 0)
560 bfd_put_32 (abfd, insn | SHMEDIA_PTB_BIT,
561 contents + rel->r_offset);
565 case R_SH_SHMEDIA_CODE:
566 case R_SH_DIR5U:
567 case R_SH_DIR6S:
568 case R_SH_DIR6U:
569 case R_SH_DIR10S:
570 case R_SH_DIR10SW:
571 case R_SH_DIR10SL:
572 case R_SH_DIR10SQ:
573 case R_SH_IMMS16:
574 case R_SH_IMMU16:
575 case R_SH_IMM_LOW16:
576 case R_SH_IMM_LOW16_PCREL:
577 case R_SH_IMM_MEDLOW16:
578 case R_SH_IMM_MEDLOW16_PCREL:
579 case R_SH_IMM_MEDHI16:
580 case R_SH_IMM_MEDHI16_PCREL:
581 case R_SH_IMM_HI16:
582 case R_SH_IMM_HI16_PCREL:
583 case R_SH_64:
584 case R_SH_64_PCREL:
585 break;
587 default:
588 return false;
591 disp = (*relocation & 0xf);
592 dropped = 0;
593 switch (ELF32_R_TYPE (rel->r_info))
595 case R_SH_DIR10SW: dropped = disp & 1; break;
596 case R_SH_DIR10SL: dropped = disp & 3; break;
597 case R_SH_DIR10SQ: dropped = disp & 7; break;
599 if (dropped != 0)
601 (*_bfd_error_handler)
602 (_("%s: error: unaligned relocation type %d at %08x reloc %08x\n"),
603 bfd_get_filename (input_section->owner), ELF32_R_TYPE (rel->r_info),
604 (unsigned)rel->r_offset, (unsigned)relocation);
605 return false;
608 return true;
611 /* Helper function to locate the section holding a certain address. This
612 is called via bfd_map_over_sections. */
614 static void
615 sh64_find_section_for_address (abfd, section, data)
616 bfd *abfd ATTRIBUTE_UNUSED;
617 asection *section;
618 PTR data;
620 bfd_vma vma;
621 bfd_size_type size;
622 struct sh64_find_section_vma_data *fsec_datap
623 = (struct sh64_find_section_vma_data *) data;
625 /* Return if already found. */
626 if (fsec_datap->section)
627 return;
629 /* If this section isn't part of the addressable contents, skip it. */
630 if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
631 return;
633 vma = bfd_get_section_vma (abfd, section);
634 if (fsec_datap->addr < vma)
635 return;
637 /* FIXME: section->reloc_done isn't set properly; a generic buglet
638 preventing us from using bfd_get_section_size_after_reloc. */
639 size
640 = section->_cooked_size ? section->_cooked_size : section->_raw_size;
642 if (fsec_datap->addr >= vma + size)
643 return;
645 fsec_datap->section = section;
648 /* Make sure to write out the generated entries in the .cranges section
649 when doing partial linking, and set bit 0 on the entry address if it
650 points to SHmedia code and write sorted .cranges entries when writing
651 executables (final linking and objcopy). */
653 static void
654 sh64_elf_final_write_processing (abfd, linker)
655 bfd * abfd;
656 boolean linker ATTRIBUTE_UNUSED;
658 bfd_vma ld_generated_cranges_size;
659 asection *cranges
660 = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
662 /* If no new .cranges were added, the generic ELF linker parts will
663 write it all out. If not, we need to write them out when doing
664 partial linking. For a final link, we will sort them and write them
665 all out further below. */
666 if (linker
667 && cranges != NULL
668 && elf_elfheader (abfd)->e_type != ET_EXEC
669 && (ld_generated_cranges_size
670 = sh64_elf_section_data (cranges)->cranges_growth) != 0)
672 bfd_vma incoming_cranges_size
673 = ((cranges->_cooked_size != 0
674 ? cranges->_cooked_size : cranges->_raw_size)
675 - ld_generated_cranges_size);
677 if (! bfd_set_section_contents (abfd, cranges,
678 cranges->contents
679 + incoming_cranges_size,
680 cranges->output_offset
681 + incoming_cranges_size,
682 ld_generated_cranges_size))
684 bfd_set_error (bfd_error_file_truncated);
685 (*_bfd_error_handler)
686 (_("%s: could not write out added .cranges entries"),
687 bfd_get_filename (abfd));
691 /* Only set entry address bit 0 and sort .cranges when linking to an
692 executable; never with objcopy or strip. */
693 if (linker && elf_elfheader (abfd)->e_type == ET_EXEC)
695 struct sh64_find_section_vma_data fsec_data;
696 sh64_elf_crange dummy;
698 /* For a final link, set the low bit of the entry address to
699 reflect whether or not it is a SHmedia address.
700 FIXME: Perhaps we shouldn't do this if the entry address was
701 supplied numerically, but we currently lack the infrastructure to
702 recognize that: The entry symbol, and info whether it is numeric
703 or a symbol name is kept private in the linker. */
704 fsec_data.addr = elf_elfheader (abfd)->e_entry;
705 fsec_data.section = NULL;
707 bfd_map_over_sections (abfd, sh64_find_section_for_address,
708 (PTR) &fsec_data);
709 if (fsec_data.section
710 && (sh64_get_contents_type (fsec_data.section,
711 elf_elfheader (abfd)->e_entry,
712 &dummy) == CRT_SH5_ISA32))
713 elf_elfheader (abfd)->e_entry |= 1;
715 /* If we have a .cranges section, sort the entries. */
716 if (cranges != NULL)
718 bfd_size_type cranges_size
719 = (cranges->_cooked_size != 0
720 ? cranges->_cooked_size : cranges->_raw_size);
722 /* We know we always have these in memory at this time. */
723 BFD_ASSERT (cranges->contents != NULL);
725 /* The .cranges may already have been sorted in the process of
726 finding out the ISA-type of the entry address. If not, we do
727 it here. */
728 if (elf_section_data (cranges)->this_hdr.sh_type
729 != SHT_SH5_CR_SORTED)
731 qsort (cranges->contents, cranges_size / SH64_CRANGE_SIZE,
732 SH64_CRANGE_SIZE,
733 bfd_big_endian (cranges->owner)
734 ? crange_qsort_cmpb : crange_qsort_cmpl);
735 elf_section_data (cranges)->this_hdr.sh_type
736 = SHT_SH5_CR_SORTED;
739 /* We need to write it out in whole as sorted. */
740 if (! bfd_set_section_contents (abfd, cranges,
741 cranges->contents,
742 cranges->output_offset,
743 cranges_size))
745 bfd_set_error (bfd_error_file_truncated);
746 (*_bfd_error_handler)
747 (_("%s: could not write out sorted .cranges entries"),
748 bfd_get_filename (abfd));
754 /* Ordering functions of a crange, for the qsort and bsearch calls and for
755 different endianness. */
757 static int
758 crange_qsort_cmpb (p1, p2)
759 const PTR p1;
760 const PTR p2;
762 bfd_vma a1 = bfd_getb32 (p1);
763 bfd_vma a2 = bfd_getb32 (p2);
765 /* Preserve order if there's ambiguous contents. */
766 if (a1 == a2)
767 return (char *) p1 - (char *) p2;
769 return a1 - a2;
772 static int
773 crange_qsort_cmpl (p1, p2)
774 const PTR p1;
775 const PTR p2;
777 bfd_vma a1 = (bfd_vma) bfd_getl32 (p1);
778 bfd_vma a2 = (bfd_vma) bfd_getl32 (p2);
780 /* Preserve order if there's ambiguous contents. */
781 if (a1 == a2)
782 return (char *) p1 - (char *) p2;
784 return a1 - a2;
787 static int
788 crange_bsearch_cmpb (p1, p2)
789 const PTR p1;
790 const PTR p2;
792 bfd_vma a1 = *(bfd_vma *) p1;
793 bfd_vma a2 = (bfd_vma) bfd_getb32 (p2);
794 bfd_size_type size
795 = (bfd_size_type) bfd_getb32 (SH64_CRANGE_CR_SIZE_OFFSET + (char *) p2);
797 if (a1 >= a2 + size)
798 return 1;
799 if (a1 < a2)
800 return -1;
801 return 0;
804 static int
805 crange_bsearch_cmpl (p1, p2)
806 const PTR p1;
807 const PTR p2;
809 bfd_vma a1 = *(bfd_vma *) p1;
810 bfd_vma a2 = (bfd_vma) bfd_getl32 (p2);
811 bfd_size_type size
812 = (bfd_size_type) bfd_getl32 (SH64_CRANGE_CR_SIZE_OFFSET + (char *) p2);
814 if (a1 >= a2 + size)
815 return 1;
816 if (a1 < a2)
817 return -1;
818 return 0;
821 /* Check whether a specific address is specified within a .cranges
822 section. Return FALSE if not found, and TRUE if found, and the region
823 filled into RANGEP if non-NULL. */
825 static boolean
826 sh64_address_in_cranges (cranges, addr, rangep)
827 asection *cranges;
828 bfd_vma addr;
829 sh64_elf_crange *rangep;
831 bfd_byte *cranges_contents;
832 bfd_byte *found_rangep;
833 bfd_size_type cranges_size = bfd_section_size (cranges->owner, cranges);
835 /* If the size is not a multiple of the cranges entry size, then
836 something is badly wrong. */
837 if ((cranges_size % SH64_CRANGE_SIZE) != 0)
838 return false;
840 /* If this section has relocations, then we can't do anything sane. */
841 if (bfd_get_section_flags (cranges->owner, cranges) & SEC_RELOC)
842 return false;
844 /* Has some kind soul (or previous call) left processed, sorted contents
845 for us? */
846 if ((bfd_get_section_flags (cranges->owner, cranges) & SEC_IN_MEMORY)
847 && elf_section_data (cranges)->this_hdr.sh_type == SHT_SH5_CR_SORTED)
848 cranges_contents = cranges->contents;
849 else
851 cranges_contents
852 = bfd_malloc (cranges->_cooked_size == 0
853 ? cranges->_cooked_size : cranges->_raw_size);
854 if (cranges_contents == NULL)
855 return false;
857 if (! bfd_get_section_contents (cranges->owner, cranges,
858 cranges_contents, (file_ptr) 0,
859 cranges_size))
860 goto error_return;
862 /* Is it sorted? */
863 if (elf_section_data (cranges)->this_hdr.sh_type
864 != SHT_SH5_CR_SORTED)
865 /* Nope. Lets sort it. */
866 qsort (cranges_contents, cranges_size / SH64_CRANGE_SIZE,
867 SH64_CRANGE_SIZE,
868 bfd_big_endian (cranges->owner)
869 ? crange_qsort_cmpb : crange_qsort_cmpl);
871 /* Let's keep it around. */
872 cranges->contents = cranges_contents;
873 bfd_set_section_flags (cranges->owner, cranges,
874 bfd_get_section_flags (cranges->owner, cranges)
875 | SEC_IN_MEMORY);
877 /* It's sorted now. */
878 elf_section_data (cranges)->this_hdr.sh_type = SHT_SH5_CR_SORTED;
881 /* Try and find a matching range. */
882 found_rangep
883 = bsearch (&addr, cranges_contents, cranges_size / SH64_CRANGE_SIZE,
884 SH64_CRANGE_SIZE,
885 bfd_big_endian (cranges->owner)
886 ? crange_bsearch_cmpb : crange_bsearch_cmpl);
888 /* Fill in a few return values if we found a matching range. */
889 if (found_rangep)
891 enum sh64_elf_cr_type cr_type
892 = bfd_get_16 (cranges->owner,
893 SH64_CRANGE_CR_TYPE_OFFSET + found_rangep);
894 bfd_vma cr_addr
895 = bfd_get_32 (cranges->owner,
896 SH64_CRANGE_CR_ADDR_OFFSET
897 + (char *) found_rangep);
898 bfd_size_type cr_size
899 = bfd_get_32 (cranges->owner,
900 SH64_CRANGE_CR_SIZE_OFFSET
901 + (char *) found_rangep);
903 rangep->cr_addr = cr_addr;
904 rangep->cr_size = cr_size;
905 rangep->cr_type = cr_type;
907 return true;
910 /* There is a .cranges section, but it does not have a descriptor
911 matching this address. */
912 return false;
914 error_return:
915 free (cranges_contents);
916 return false;
919 /* Determine what ADDR points to in SEC, and fill in a range descriptor in
920 *RANGEP if it's non-NULL. */
922 enum sh64_elf_cr_type
923 sh64_get_contents_type (sec, addr, rangep)
924 asection *sec;
925 bfd_vma addr;
926 sh64_elf_crange *rangep;
928 asection *cranges;
930 /* Fill in the range with the boundaries of the section as a default. */
931 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
932 && elf_elfheader (sec->owner)->e_type == ET_EXEC)
934 rangep->cr_addr = bfd_get_section_vma (sec->owner, sec);
935 rangep->cr_size = bfd_section_size (sec->owner, sec);
936 rangep->cr_type = CRT_NONE;
938 else
939 return false;
941 /* If none of the pertinent bits are set, then it's a SHcompact (or at
942 least not SHmedia). */
943 if ((elf_section_data (sec)->this_hdr.sh_flags
944 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) == 0)
946 enum sh64_elf_cr_type cr_type
947 = ((bfd_get_section_flags (sec->owner, sec) & SEC_CODE) != 0
948 ? CRT_SH5_ISA16 : CRT_DATA);
949 rangep->cr_type = cr_type;
950 return cr_type;
953 /* If only the SHF_SH5_ISA32 bit is set, then we have SHmedia. */
954 if ((elf_section_data (sec)->this_hdr.sh_flags
955 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) == SHF_SH5_ISA32)
957 rangep->cr_type = CRT_SH5_ISA32;
958 return CRT_SH5_ISA32;
961 /* Otherwise, we have to look up the .cranges section. */
962 cranges = bfd_get_section_by_name (sec->owner, SH64_CRANGES_SECTION_NAME);
964 if (cranges == NULL)
965 /* A mixed section but there's no .cranges section. This is probably
966 bad input; it does not comply to specs. */
967 return CRT_NONE;
969 /* If this call fails, we will still have CRT_NONE in rangep->cr_type
970 and that will be suitable to return. */
971 sh64_address_in_cranges (cranges, addr, rangep);
973 return rangep->cr_type;
976 /* This is a simpler exported interface for the benefit of gdb et al. */
978 boolean
979 sh64_address_is_shmedia (sec, addr)
980 asection *sec;
981 bfd_vma addr;
983 sh64_elf_crange dummy;
984 return sh64_get_contents_type (sec, addr, &dummy) == CRT_SH5_ISA32;