daily update
[deliverable/binutils-gdb.git] / bfd / elf32-sh64.c
1 /* Hitachi SH64-specific support for 32-bit ELF
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #define SH64_ELF
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "elf-bfd.h"
25 #include "../opcodes/sh64-opc.h"
26
27 /* Add a suffix for datalabel indirection symbols. It must not match any
28 other symbols; user symbols with or without version or other
29 decoration. It must only be used internally and not emitted by any
30 means. */
31 #define DATALABEL_SUFFIX " DL"
32
33 /* Used to hold data for function called through bfd_map_over_sections. */
34 struct sh64_find_section_vma_data
35 {
36 asection *section;
37 bfd_vma addr;
38 };
39
40 static boolean sh64_elf_copy_private_data PARAMS ((bfd *, bfd *));
41 static boolean sh64_elf_merge_private_data PARAMS ((bfd *, bfd *));
42 static boolean sh64_elf_fake_sections PARAMS ((bfd *, Elf_Internal_Shdr *,
43 asection *));
44 static boolean sh64_elf_set_private_flags PARAMS ((bfd *, flagword));
45 static boolean sh64_elf_set_mach_from_flags PARAMS ((bfd *));
46 static boolean shmedia_prepare_reloc
47 PARAMS ((struct bfd_link_info *, bfd *, asection *,
48 bfd_byte *, const Elf_Internal_Rela *, bfd_vma *));
49 static int sh64_elf_get_symbol_type PARAMS ((Elf_Internal_Sym *, int));
50 static boolean sh64_elf_add_symbol_hook
51 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
52 const char **, flagword *, asection **, bfd_vma *));
53 static boolean sh64_elf_link_output_symbol_hook
54 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
55 asection *));
56 static boolean sh64_backend_section_from_shdr
57 PARAMS ((bfd *, Elf_Internal_Shdr *, 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
62 /* Let elf32-sh.c handle the "bfd_" definitions, so we only have to
63 intrude with an #ifndef around the function definition. */
64 #define sh_elf_copy_private_data sh64_elf_copy_private_data
65 #define sh_elf_merge_private_data sh64_elf_merge_private_data
66 #define sh_elf_set_private_flags sh64_elf_set_private_flags
67 /* Typo in elf32-sh.c (and unlinear name). */
68 #define bfd_elf32_bfd_set_private_flags sh64_elf_set_private_flags
69 #define sh_elf_set_mach_from_flags sh64_elf_set_mach_from_flags
70
71 #define elf_backend_sign_extend_vma 1
72 #define elf_backend_fake_sections sh64_elf_fake_sections
73 #define elf_backend_get_symbol_type sh64_elf_get_symbol_type
74 #define elf_backend_add_symbol_hook sh64_elf_add_symbol_hook
75 #define elf_backend_link_output_symbol_hook \
76 sh64_elf_link_output_symbol_hook
77 #define elf_backend_final_write_processing sh64_elf_final_write_processing
78 #define elf_backend_section_from_shdr sh64_backend_section_from_shdr
79
80 /* For objcopy, we need to set up sh64_elf_section_data (asection *) from
81 incoming section flags. This is otherwise done in sh64elf.em when
82 linking or tc-sh64.c when assembling. */
83 #define bfd_elf32_bfd_copy_private_section_data \
84 sh64_bfd_elf_copy_private_section_data
85
86 /* This COFF-only function (only compiled with COFF support, making
87 ELF-only chains problematic) returns true early for SH4, so let's just
88 define it true here. */
89 #define _bfd_sh_align_load_span(a,b,c,d,e,f,g,h,i,j) true
90
91 #ifndef ELF_ARCH
92 #define TARGET_BIG_SYM bfd_elf32_sh64_vec
93 #define TARGET_BIG_NAME "elf32-sh64"
94 #define TARGET_LITTLE_SYM bfd_elf32_sh64l_vec
95 #define TARGET_LITTLE_NAME "elf32-sh64l"
96 #define ELF_ARCH bfd_arch_sh
97 #define ELF_MACHINE_CODE EM_SH
98 #define ELF_MAXPAGESIZE 128
99
100 #define elf_symbol_leading_char '_'
101 #endif /* ELF_ARCH */
102
103 #define GOT_BIAS (-((long)-32768))
104 #define INCLUDE_SHMEDIA
105 #include "elf32-sh.c"
106
107 /* The type sh64_elf_crange is defined in elf/sh.h which is included in
108 elf32-sh.c, hence these prototypes located after including it. */
109 static int crange_qsort_cmpb PARAMS ((const void *, const void *));
110 static int crange_qsort_cmpl PARAMS ((const void *, const void *));
111 static int crange_bsearch_cmpb PARAMS ((const void *, const void *));
112 static int crange_bsearch_cmpl PARAMS ((const void *, const void *));
113 static boolean sh64_address_in_cranges
114 PARAMS ((asection *cranges, bfd_vma, sh64_elf_crange *));
115
116 /* Set the SHF_SH5_ISA32 flag for ISA SHmedia code sections, and pass
117 through SHT_SH5_CR_SORTED on a sorted .cranges section. */
118
119 boolean
120 sh64_elf_fake_sections (output_bfd, elf_section_hdr, asect)
121 bfd *output_bfd ATTRIBUTE_UNUSED;
122 Elf_Internal_Shdr *elf_section_hdr;
123 asection *asect;
124 {
125 if (sh64_elf_section_data (asect) != NULL)
126 elf_section_hdr->sh_flags
127 |= sh64_elf_section_data (asect)->contents_flags;
128
129 /* If this section has the SEC_SORT_ENTRIES flag set, it is a sorted
130 .cranges section passing through objcopy. */
131 if ((bfd_get_section_flags (output_bfd, asect) & SEC_SORT_ENTRIES) != 0
132 && strcmp (bfd_get_section_name (output_bfd, asect),
133 SH64_CRANGES_SECTION_NAME) == 0)
134 elf_section_hdr->sh_type = SHT_SH5_CR_SORTED;
135
136 return true;
137 }
138
139 static boolean
140 sh64_elf_set_mach_from_flags (abfd)
141 bfd *abfd;
142 {
143 flagword flags = elf_elfheader (abfd)->e_flags;
144 asection *cranges;
145
146 switch (flags & EF_SH_MACH_MASK)
147 {
148 case EF_SH5:
149 /* These are fit to execute on SH5. Just one but keep the switch
150 construct to make additions easy. */
151 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh5);
152 break;
153
154 default:
155 bfd_set_error (bfd_error_wrong_format);
156 return false;
157 }
158
159 /* We also need to set SEC_DEBUGGING on an incoming .cranges section.
160 We could have used elf_backend_section_flags if it had given us the
161 section name; the bfd_section member in the header argument is not
162 set at the point of the call. FIXME: Find out whether that is by
163 undocumented design or a bug. */
164 cranges = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
165 if (cranges != NULL
166 && ! bfd_set_section_flags (abfd, cranges,
167 bfd_get_section_flags (abfd, cranges)
168 | SEC_DEBUGGING))
169 return false;
170
171 return true;
172 }
173
174 static boolean
175 sh64_elf_copy_private_data (ibfd, obfd)
176 bfd * ibfd;
177 bfd * obfd;
178 {
179 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
180 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
181 return true;
182
183 BFD_ASSERT (!elf_flags_init (obfd)
184 || (elf_elfheader (obfd)->e_flags
185 == elf_elfheader (ibfd)->e_flags));
186
187 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
188 return true;
189 }
190
191 static boolean
192 sh64_elf_merge_private_data (ibfd, obfd)
193 bfd *ibfd;
194 bfd *obfd;
195 {
196 flagword old_flags, new_flags;
197
198 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
199 return false;
200
201 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
202 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
203 return true;
204
205 if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd))
206 {
207 const char *msg;
208
209 if (bfd_get_arch_size (ibfd) == 32
210 && bfd_get_arch_size (obfd) == 64)
211 msg = _("%s: compiled as 32-bit object and %s is 64-bit");
212 else if (bfd_get_arch_size (ibfd) == 64
213 && bfd_get_arch_size (obfd) == 32)
214 msg = _("%s: compiled as 64-bit object and %s is 32-bit");
215 else
216 msg = _("%s: object size does not match that of target %s");
217
218 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd),
219 bfd_get_filename (obfd));
220 bfd_set_error (bfd_error_wrong_format);
221 return false;
222 }
223
224 old_flags = elf_elfheader (obfd)->e_flags;
225 new_flags = elf_elfheader (ibfd)->e_flags;
226 if (! elf_flags_init (obfd))
227 {
228 /* This happens when ld starts out with a 'blank' output file. */
229 elf_flags_init (obfd) = true;
230 elf_elfheader (obfd)->e_flags = old_flags = new_flags;
231 }
232 /* We don't allow linking in non-SH64 code. */
233 else if ((new_flags & EF_SH_MACH_MASK) != EF_SH5)
234 {
235 (*_bfd_error_handler)
236 ("%s: uses non-SH64 instructions while previous modules use SH64 instructions",
237 bfd_get_filename (ibfd));
238 bfd_set_error (bfd_error_bad_value);
239 return false;
240 }
241
242 /* I can't think of anything sane other than old_flags being EF_SH5 and
243 that we need to preserve that. */
244 elf_elfheader (obfd)->e_flags = old_flags;
245 return sh64_elf_set_mach_from_flags (obfd);
246 }
247
248 /* Handle a SH64-specific section when reading an object file. This
249 is called when elfcode.h finds a section with an unknown type.
250
251 We only recognize SHT_SH5_CR_SORTED, on the .cranges section. */
252
253 boolean
254 sh64_backend_section_from_shdr (abfd, hdr, name)
255 bfd *abfd;
256 Elf_Internal_Shdr *hdr;
257 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 boolean
301 sh64_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
302 bfd *ibfd;
303 asection *isec;
304 bfd *obfd;
305 asection *osec;
306 {
307 struct sh64_section_data *sh64_sec_data;
308
309 if (ibfd->xvec->flavour != bfd_target_elf_flavour
310 || obfd->xvec->flavour != bfd_target_elf_flavour)
311 return true;
312
313 if (! _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec))
314 return false;
315
316 sh64_sec_data = sh64_elf_section_data (isec);
317 if (sh64_sec_data == NULL)
318 {
319 sh64_sec_data = bfd_zmalloc (sizeof (struct sh64_section_data));
320
321 if (sh64_sec_data == NULL)
322 return false;
323
324 sh64_sec_data->contents_flags
325 = (elf_section_data (isec)->this_hdr.sh_flags
326 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
327
328 sh64_elf_section_data (osec) = sh64_sec_data;
329 }
330
331 return true;
332 }
333
334 /* Function to keep SH64 specific file flags. */
335
336 static boolean
337 sh64_elf_set_private_flags (abfd, flags)
338 bfd * abfd;
339 flagword flags;
340 {
341 BFD_ASSERT (! elf_flags_init (abfd)
342 || elf_elfheader (abfd)->e_flags == flags);
343
344 elf_elfheader (abfd)->e_flags = flags;
345 elf_flags_init (abfd) = true;
346 return sh64_elf_set_mach_from_flags (abfd);
347 }
348
349 /* Called when writing out an object file to decide the type of a symbol. */
350
351 static int
352 sh64_elf_get_symbol_type (elf_sym, type)
353 Elf_Internal_Sym * elf_sym;
354 int type;
355 {
356 if (ELF_ST_TYPE (elf_sym->st_info) == STT_DATALABEL)
357 return STT_DATALABEL;
358
359 return type;
360 }
361
362 /* Hook called by the linker routine which adds symbols from an object
363 file. We must make indirect symbols for undefined symbols marked with
364 STT_DATALABEL, so relocations passing them will pick up that attribute
365 and neutralize STO_SH5_ISA32 found on the symbol definition.
366
367 There is a problem, though: We want to fill in the hash-table entry for
368 this symbol and signal to the caller that no further processing is
369 needed. But we don't have the index for this hash-table entry. We
370 rely here on that the current entry is the first hash-entry with NULL,
371 which seems brittle. Also, iterating over the hash-table to find that
372 entry is a linear operation on the number of symbols in this input
373 file, and this function should take constant time, so that's not good
374 too. Only comfort is that DataLabel references should only be found in
375 hand-written assembly code and thus be rare. FIXME: Talk maintainers
376 into adding an option to elf_add_symbol_hook (preferably) for the index
377 or the hash entry, alternatively adding the index to Elf_Internal_Sym
378 (not so good). */
379
380 static boolean
381 sh64_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
382 bfd *abfd;
383 struct bfd_link_info *info;
384 const Elf_Internal_Sym *sym;
385 const char **namep;
386 flagword *flagsp ATTRIBUTE_UNUSED;
387 asection **secp;
388 bfd_vma *valp;
389 {
390 /* We want to do this for relocatable as well as final linking. */
391 if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL)
392 {
393 struct elf_link_hash_entry *h;
394
395 /* For relocateable links, we register the DataLabel sym in its own
396 right, and tweak the name when it's output. Otherwise, we make
397 an indirect symbol of it. */
398 flagword flags
399 = info->relocateable || info->emitrelocations
400 ? BSF_GLOBAL : BSF_GLOBAL | BSF_INDIRECT;
401
402 char *dl_name
403 = bfd_malloc (strlen (*namep) + sizeof (DATALABEL_SUFFIX));
404 struct elf_link_hash_entry ** sym_hash = elf_sym_hashes (abfd);
405
406 BFD_ASSERT (sym_hash != NULL);
407
408 /* Allocation may fail. */
409 if (dl_name == NULL)
410 return false;
411
412 strcpy (dl_name, *namep);
413 strcat (dl_name, DATALABEL_SUFFIX);
414
415 h = (struct elf_link_hash_entry *)
416 bfd_link_hash_lookup (info->hash, dl_name, false, false, false);
417
418 if (h == NULL)
419 {
420 /* No previous datalabel symbol. Make one. */
421 if (! _bfd_generic_link_add_one_symbol (info, abfd, dl_name,
422 flags, *secp, *valp,
423 *namep, false,
424 get_elf_backend_data (abfd)->collect,
425 (struct bfd_link_hash_entry **) &h))
426 {
427 free (dl_name);
428 return false;
429 }
430
431 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
432 h->type = STT_DATALABEL;
433 }
434 else
435 /* If a new symbol was created, it holds the allocated name.
436 Otherwise, we don't need it anymore and should deallocate it. */
437 free (dl_name);
438
439 if (h->type != STT_DATALABEL
440 || ((info->relocateable || info->emitrelocations)
441 && h->root.type != bfd_link_hash_undefined)
442 || (! info->relocateable && !info->emitrelocations
443 && h->root.type != bfd_link_hash_indirect))
444 {
445 /* Make sure we don't get confused on invalid input. */
446 (*_bfd_error_handler)
447 (_("%s: encountered datalabel symbol in input"),
448 bfd_get_filename (abfd));
449 bfd_set_error (bfd_error_bad_value);
450 return false;
451 }
452
453 /* Now find the hash-table slot for this entry and fill it in. */
454 while (*sym_hash != NULL)
455 sym_hash++;
456 *sym_hash = h;
457
458 /* Signal to caller to skip this symbol - we've handled it. */
459 *namep = NULL;
460 }
461
462 return true;
463 }
464
465 /* This hook function is called before the linker writes out a global
466 symbol. For relocatable links, DataLabel symbols will be present in
467 linker output. We cut off the special suffix on those symbols, so the
468 right name appears in the output.
469
470 When linking and emitting relocations, there can appear global symbols
471 that are not referenced by relocs, but rather only implicitly through
472 DataLabel references, a relation that is not visible to the linker.
473 Since no stripping of global symbols in done when doing such linking,
474 we don't need to look up and make sure to emit the main symbol for each
475 DataLabel symbol. */
476
477 boolean
478 sh64_elf_link_output_symbol_hook (abfd, info, cname, sym, input_sec)
479 bfd *abfd ATTRIBUTE_UNUSED;
480 struct bfd_link_info *info;
481 const char *cname;
482 Elf_Internal_Sym *sym;
483 asection *input_sec ATTRIBUTE_UNUSED;
484 {
485 char *name = (char *) cname;
486
487 if (info->relocateable || info->emitrelocations)
488 {
489 if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL)
490 name[strlen (name) - strlen (DATALABEL_SUFFIX)] = 0;
491 }
492
493 return true;
494 }
495
496 /* Check a SH64-specific reloc and put the value to relocate to into
497 RELOCATION, ready to pass to _bfd_final_link_relocate. Return FALSE if
498 bad value, TRUE if ok. */
499
500 static boolean
501 shmedia_prepare_reloc (info, abfd, input_section,
502 contents, rel, relocation)
503 struct bfd_link_info *info;
504 bfd *abfd;
505 asection *input_section;
506 bfd_byte *contents;
507 const Elf_Internal_Rela *rel;
508 bfd_vma *relocation;
509 {
510 bfd_vma disp, dropped;
511
512 switch (ELF32_R_TYPE (rel->r_info))
513 {
514 case R_SH_PT_16:
515 /* Check the lowest bit of the destination field. If it is 1, we
516 check the ISA type of the destination (i.e. the low bit of the
517 "relocation" value, and emit an error if the instruction does not
518 match). If it is 0, we change a PTA to PTB. There should never
519 be a PTB that should change to a PTA; that indicates a toolchain
520 error; a mismatch with GAS. */
521 {
522 char *msg = NULL;
523 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
524
525 if (insn & (1 << 10))
526 {
527 /* Check matching insn and ISA (address of target). */
528 if ((insn & SHMEDIA_PTB_BIT) != 0
529 && ((*relocation + rel->r_addend) & 1) != 0)
530 msg = _("PTB mismatch: a SHmedia address (bit 0 == 1)");
531 else if ((insn & SHMEDIA_PTB_BIT) == 0
532 && ((*relocation + rel->r_addend) & 1) == 0)
533 msg = _("PTA mismatch: a SHcompact address (bit 0 == 0)");
534
535 if (msg != NULL
536 && ! ((*info->callbacks->reloc_dangerous)
537 (info, msg, abfd, input_section,
538 rel->r_offset)))
539 return false;
540 }
541 else
542 {
543 /* We shouldn't get here with a PTB insn and a R_SH_PT_16. It
544 means GAS output does not match expectations; a PTA or PTB
545 expressed as such (or a PT found at assembly to be PTB)
546 would match the test above, and PT expansion with an
547 unknown destination (or when relaxing) will get us here. */
548 if ((insn & SHMEDIA_PTB_BIT) != 0)
549 {
550 (*_bfd_error_handler)
551 (_("%s: GAS error: unexpected PTB insn with R_SH_PT_16"),
552 bfd_get_filename (input_section->owner));
553 return false;
554 }
555
556 /* Change the PTA to a PTB, if destination indicates so. */
557 if (((*relocation + rel->r_addend) & 1) == 0)
558 bfd_put_32 (abfd, insn | SHMEDIA_PTB_BIT,
559 contents + rel->r_offset);
560 }
561 }
562
563 case R_SH_SHMEDIA_CODE:
564 case R_SH_DIR5U:
565 case R_SH_DIR6S:
566 case R_SH_DIR6U:
567 case R_SH_DIR10S:
568 case R_SH_DIR10SW:
569 case R_SH_DIR10SL:
570 case R_SH_DIR10SQ:
571 case R_SH_IMMS16:
572 case R_SH_IMMU16:
573 case R_SH_IMM_LOW16:
574 case R_SH_IMM_LOW16_PCREL:
575 case R_SH_IMM_MEDLOW16:
576 case R_SH_IMM_MEDLOW16_PCREL:
577 case R_SH_IMM_MEDHI16:
578 case R_SH_IMM_MEDHI16_PCREL:
579 case R_SH_IMM_HI16:
580 case R_SH_IMM_HI16_PCREL:
581 case R_SH_64:
582 case R_SH_64_PCREL:
583 break;
584
585 default:
586 return false;
587 }
588
589 disp = (*relocation & 0xf);
590 dropped = 0;
591 switch (ELF32_R_TYPE (rel->r_info))
592 {
593 case R_SH_DIR10SW: dropped = disp & 1; break;
594 case R_SH_DIR10SL: dropped = disp & 3; break;
595 case R_SH_DIR10SQ: dropped = disp & 7; break;
596 }
597 if (dropped != 0)
598 {
599 (*_bfd_error_handler)
600 (_("%s: error: unaligned relocation type %d at %08x reloc %08x\n"),
601 bfd_get_filename (input_section->owner), ELF32_R_TYPE (rel->r_info),
602 (unsigned)rel->r_offset, (unsigned)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 (abfd, section, data)
614 bfd *abfd ATTRIBUTE_UNUSED;
615 asection *section;
616 PTR data;
617 {
618 bfd_vma vma;
619 bfd_size_type size;
620 struct sh64_find_section_vma_data *fsec_datap
621 = (struct sh64_find_section_vma_data *) data;
622
623 /* Return if already found. */
624 if (fsec_datap->section)
625 return;
626
627 /* If this section isn't part of the addressable contents, skip it. */
628 if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
629 return;
630
631 vma = bfd_get_section_vma (abfd, section);
632 if (fsec_datap->addr < vma)
633 return;
634
635 /* FIXME: section->reloc_done isn't set properly; a generic buglet
636 preventing us from using bfd_get_section_size_after_reloc. */
637 size
638 = section->_cooked_size ? section->_cooked_size : section->_raw_size;
639
640 if (fsec_datap->addr >= vma + size)
641 return;
642
643 fsec_datap->section = section;
644 }
645
646 /* Make sure to write out the generated entries in the .cranges section
647 when doing partial linking, and set bit 0 on the entry address if it
648 points to SHmedia code and write sorted .cranges entries when writing
649 executables (final linking and objcopy). */
650
651 static void
652 sh64_elf_final_write_processing (abfd, linker)
653 bfd * abfd;
654 boolean linker ATTRIBUTE_UNUSED;
655 {
656 bfd_vma ld_generated_cranges_size;
657 asection *cranges
658 = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
659
660 /* If no new .cranges were added, the generic ELF linker parts will
661 write it all out. If not, we need to write them out when doing
662 partial linking. For a final link, we will sort them and write them
663 all out further below. */
664 if (linker
665 && cranges != NULL
666 && elf_elfheader (abfd)->e_type != ET_EXEC
667 && (ld_generated_cranges_size
668 = sh64_elf_section_data (cranges)->cranges_growth) != 0)
669 {
670 bfd_vma incoming_cranges_size
671 = ((cranges->_cooked_size != 0
672 ? cranges->_cooked_size : cranges->_raw_size)
673 - ld_generated_cranges_size);
674
675 if (! bfd_set_section_contents (abfd, cranges,
676 cranges->contents
677 + incoming_cranges_size,
678 cranges->output_offset
679 + incoming_cranges_size,
680 ld_generated_cranges_size))
681 {
682 bfd_set_error (bfd_error_file_truncated);
683 (*_bfd_error_handler)
684 (_("%s: could not write out added .cranges entries"),
685 bfd_get_filename (abfd));
686 }
687 }
688
689 /* Only set entry address bit 0 and sort .cranges when linking to an
690 executable; never with objcopy or strip. */
691 if (linker && elf_elfheader (abfd)->e_type == ET_EXEC)
692 {
693 struct sh64_find_section_vma_data fsec_data;
694 sh64_elf_crange dummy;
695
696 /* For a final link, set the low bit of the entry address to
697 reflect whether or not it is a SHmedia address.
698 FIXME: Perhaps we shouldn't do this if the entry address was
699 supplied numerically, but we currently lack the infrastructure to
700 recognize that: The entry symbol, and info whether it is numeric
701 or a symbol name is kept private in the linker. */
702 fsec_data.addr = elf_elfheader (abfd)->e_entry;
703 fsec_data.section = NULL;
704
705 bfd_map_over_sections (abfd, sh64_find_section_for_address,
706 (PTR) &fsec_data);
707 if (fsec_data.section
708 && (sh64_get_contents_type (fsec_data.section,
709 elf_elfheader (abfd)->e_entry,
710 &dummy) == CRT_SH5_ISA32))
711 elf_elfheader (abfd)->e_entry |= 1;
712
713 /* If we have a .cranges section, sort the entries. */
714 if (cranges != NULL)
715 {
716 bfd_size_type cranges_size
717 = (cranges->_cooked_size != 0
718 ? cranges->_cooked_size : cranges->_raw_size);
719
720 /* We know we always have these in memory at this time. */
721 BFD_ASSERT (cranges->contents != NULL);
722
723 /* The .cranges may already have been sorted in the process of
724 finding out the ISA-type of the entry address. If not, we do
725 it here. */
726 if (elf_section_data (cranges)->this_hdr.sh_type
727 != SHT_SH5_CR_SORTED)
728 {
729 qsort (cranges->contents, cranges_size / SH64_CRANGE_SIZE,
730 SH64_CRANGE_SIZE,
731 bfd_big_endian (cranges->owner)
732 ? crange_qsort_cmpb : crange_qsort_cmpl);
733 elf_section_data (cranges)->this_hdr.sh_type
734 = SHT_SH5_CR_SORTED;
735 }
736
737 /* We need to write it out in whole as sorted. */
738 if (! bfd_set_section_contents (abfd, cranges,
739 cranges->contents,
740 cranges->output_offset,
741 cranges_size))
742 {
743 bfd_set_error (bfd_error_file_truncated);
744 (*_bfd_error_handler)
745 (_("%s: could not write out sorted .cranges entries"),
746 bfd_get_filename (abfd));
747 }
748 }
749 }
750 }
751
752 /* Ordering functions of a crange, for the qsort and bsearch calls and for
753 different endianness. */
754
755 static int
756 crange_qsort_cmpb (p1, p2)
757 const PTR p1;
758 const PTR p2;
759 {
760 bfd_vma a1 = bfd_getb32 (p1);
761 bfd_vma a2 = bfd_getb32 (p2);
762
763 /* Preserve order if there's ambiguous contents. */
764 if (a1 == a2)
765 return (char *) p1 - (char *) p2;
766
767 return a1 - a2;
768 }
769
770 static int
771 crange_qsort_cmpl (p1, p2)
772 const PTR p1;
773 const PTR p2;
774 {
775 bfd_vma a1 = (bfd_vma) bfd_getl32 (p1);
776 bfd_vma a2 = (bfd_vma) bfd_getl32 (p2);
777
778 /* Preserve order if there's ambiguous contents. */
779 if (a1 == a2)
780 return (char *) p1 - (char *) p2;
781
782 return a1 - a2;
783 }
784
785 static int
786 crange_bsearch_cmpb (p1, p2)
787 const PTR p1;
788 const PTR p2;
789 {
790 bfd_vma a1 = *(bfd_vma *) p1;
791 bfd_vma a2 = (bfd_vma) bfd_getb32 (p2);
792 bfd_size_type size
793 = (bfd_size_type) bfd_getb32 (SH64_CRANGE_CR_SIZE_OFFSET + (char *) p2);
794
795 if (a1 >= a2 + size)
796 return 1;
797 if (a1 < a2)
798 return -1;
799 return 0;
800 }
801
802 static int
803 crange_bsearch_cmpl (p1, p2)
804 const PTR p1;
805 const PTR p2;
806 {
807 bfd_vma a1 = *(bfd_vma *) p1;
808 bfd_vma a2 = (bfd_vma) bfd_getl32 (p2);
809 bfd_size_type size
810 = (bfd_size_type) bfd_getl32 (SH64_CRANGE_CR_SIZE_OFFSET + (char *) p2);
811
812 if (a1 >= a2 + size)
813 return 1;
814 if (a1 < a2)
815 return -1;
816 return 0;
817 }
818
819 /* Check whether a specific address is specified within a .cranges
820 section. Return FALSE if not found, and TRUE if found, and the region
821 filled into RANGEP if non-NULL. */
822
823 static boolean
824 sh64_address_in_cranges (cranges, addr, rangep)
825 asection *cranges;
826 bfd_vma addr;
827 sh64_elf_crange *rangep;
828 {
829 bfd_byte *cranges_contents;
830 bfd_byte *found_rangep;
831 bfd_size_type cranges_size = bfd_section_size (cranges->owner, cranges);
832
833 /* If the size is not a multiple of the cranges entry size, then
834 something is badly wrong. */
835 if ((cranges_size % SH64_CRANGE_SIZE) != 0)
836 return false;
837
838 /* If this section has relocations, then we can't do anything sane. */
839 if (bfd_get_section_flags (cranges->owner, cranges) & SEC_RELOC)
840 return false;
841
842 /* Has some kind soul (or previous call) left processed, sorted contents
843 for us? */
844 if ((bfd_get_section_flags (cranges->owner, cranges) & SEC_IN_MEMORY)
845 && elf_section_data (cranges)->this_hdr.sh_type == SHT_SH5_CR_SORTED)
846 cranges_contents = cranges->contents;
847 else
848 {
849 cranges_contents
850 = bfd_malloc (cranges->_cooked_size == 0
851 ? cranges->_cooked_size : cranges->_raw_size);
852 if (cranges_contents == NULL)
853 return false;
854
855 if (! bfd_get_section_contents (cranges->owner, cranges,
856 cranges_contents, (file_ptr) 0,
857 cranges_size))
858 goto error_return;
859
860 /* Is it sorted? */
861 if (elf_section_data (cranges)->this_hdr.sh_type
862 != SHT_SH5_CR_SORTED)
863 /* Nope. Lets sort it. */
864 qsort (cranges_contents, cranges_size / SH64_CRANGE_SIZE,
865 SH64_CRANGE_SIZE,
866 bfd_big_endian (cranges->owner)
867 ? crange_qsort_cmpb : crange_qsort_cmpl);
868
869 /* Let's keep it around. */
870 cranges->contents = cranges_contents;
871 bfd_set_section_flags (cranges->owner, cranges,
872 bfd_get_section_flags (cranges->owner, cranges)
873 | SEC_IN_MEMORY);
874
875 /* It's sorted now. */
876 elf_section_data (cranges)->this_hdr.sh_type = SHT_SH5_CR_SORTED;
877 }
878
879 /* Try and find a matching range. */
880 found_rangep
881 = bsearch (&addr, cranges_contents, cranges_size / SH64_CRANGE_SIZE,
882 SH64_CRANGE_SIZE,
883 bfd_big_endian (cranges->owner)
884 ? crange_bsearch_cmpb : crange_bsearch_cmpl);
885
886 /* Fill in a few return values if we found a matching range. */
887 if (found_rangep)
888 {
889 enum sh64_elf_cr_type cr_type
890 = bfd_get_16 (cranges->owner,
891 SH64_CRANGE_CR_TYPE_OFFSET + found_rangep);
892 bfd_vma cr_addr
893 = bfd_get_32 (cranges->owner,
894 SH64_CRANGE_CR_ADDR_OFFSET
895 + (char *) found_rangep);
896 bfd_size_type cr_size
897 = bfd_get_32 (cranges->owner,
898 SH64_CRANGE_CR_SIZE_OFFSET
899 + (char *) found_rangep);
900
901 rangep->cr_addr = cr_addr;
902 rangep->cr_size = cr_size;
903 rangep->cr_type = cr_type;
904
905 return true;
906 }
907
908 /* There is a .cranges section, but it does not have a descriptor
909 matching this address. */
910 return false;
911
912 error_return:
913 free (cranges_contents);
914 return false;
915 }
916
917 /* Determine what ADDR points to in SEC, and fill in a range descriptor in
918 *RANGEP if it's non-NULL. */
919
920 enum sh64_elf_cr_type
921 sh64_get_contents_type (sec, addr, rangep)
922 asection *sec;
923 bfd_vma addr;
924 sh64_elf_crange *rangep;
925 {
926 asection *cranges;
927
928 /* Fill in the range with the boundaries of the section as a default. */
929 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
930 && elf_elfheader (sec->owner)->e_type == ET_EXEC)
931 {
932 rangep->cr_addr = bfd_get_section_vma (sec->owner, sec);
933 rangep->cr_size = bfd_section_size (sec->owner, sec);
934 rangep->cr_type = CRT_NONE;
935 }
936 else
937 return false;
938
939 /* If none of the pertinent bits are set, then it's a SHcompact (or at
940 least not SHmedia). */
941 if ((elf_section_data (sec)->this_hdr.sh_flags
942 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) == 0)
943 {
944 enum sh64_elf_cr_type cr_type
945 = ((bfd_get_section_flags (sec->owner, sec) & SEC_CODE) != 0
946 ? CRT_SH5_ISA16 : CRT_DATA);
947 rangep->cr_type = cr_type;
948 return cr_type;
949 }
950
951 /* If only the SHF_SH5_ISA32 bit is set, then we have SHmedia. */
952 if ((elf_section_data (sec)->this_hdr.sh_flags
953 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) == SHF_SH5_ISA32)
954 {
955 rangep->cr_type = CRT_SH5_ISA32;
956 return CRT_SH5_ISA32;
957 }
958
959 /* Otherwise, we have to look up the .cranges section. */
960 cranges = bfd_get_section_by_name (sec->owner, SH64_CRANGES_SECTION_NAME);
961
962 if (cranges == NULL)
963 /* A mixed section but there's no .cranges section. This is probably
964 bad input; it does not comply to specs. */
965 return CRT_NONE;
966
967 /* If this call fails, we will still have CRT_NONE in rangep->cr_type
968 and that will be suitable to return. */
969 sh64_address_in_cranges (cranges, addr, rangep);
970
971 return rangep->cr_type;
972 }
973
974 /* This is a simpler exported interface for the benefit of gdb et al. */
975
976 boolean
977 sh64_address_is_shmedia (sec, addr)
978 asection *sec;
979 bfd_vma addr;
980 {
981 sh64_elf_crange dummy;
982 return sh64_get_contents_type (sec, addr, &dummy) == CRT_SH5_ISA32;
983 }
This page took 0.051073 seconds and 4 git commands to generate.