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