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