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