1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 #include "elf-vxworks.h"
30 #include "libiberty.h"
31 #include "../opcodes/sh-opc.h"
33 static bfd_reloc_status_type sh_elf_reloc
34 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
35 static bfd_reloc_status_type sh_elf_ignore_reloc
36 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
37 static bfd_boolean sh_elf_relax_delete_bytes
38 (bfd *, asection *, bfd_vma, int);
39 static bfd_boolean sh_elf_align_loads
40 (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
41 static bfd_boolean sh_elf_swap_insns
42 (bfd *, asection *, void *, bfd_byte *, bfd_vma);
43 static int sh_elf_optimized_tls_reloc
44 (struct bfd_link_info *, int, int);
45 static bfd_vma dtpoff_base
46 (struct bfd_link_info *);
48 (struct bfd_link_info *, bfd_vma);
50 /* The name of the dynamic interpreter. This is put in the .interp
53 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
55 /* FDPIC binaries have a default 128K stack. */
56 #define DEFAULT_STACK_SIZE 0x20000
58 #define MINUS_ONE ((bfd_vma) 0 - 1)
60 /* Decide whether a reference to a symbol can be resolved locally or
61 not. If the symbol is protected, we want the local address, but
62 its function descriptor must be assigned by the dynamic linker. */
63 #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
64 (SYMBOL_REFERENCES_LOCAL (INFO, H) \
65 || ! elf_hash_table (INFO)->dynamic_sections_created)
67 #define SH_PARTIAL32 TRUE
68 #define SH_SRC_MASK32 0xffffffff
69 #define SH_ELF_RELOC sh_elf_reloc
70 static reloc_howto_type sh_elf_howto_table[] =
72 #include "elf32-sh-relocs.h"
75 #define SH_PARTIAL32 FALSE
76 #define SH_SRC_MASK32 0
77 #define SH_ELF_RELOC bfd_elf_generic_reloc
78 static reloc_howto_type sh_vxworks_howto_table[] =
80 #include "elf32-sh-relocs.h"
83 /* Return true if OUTPUT_BFD is a VxWorks object. */
86 vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
88 #if !defined SH_TARGET_ALREADY_DEFINED
89 extern const bfd_target sh_elf32_vxworks_le_vec;
90 extern const bfd_target sh_elf32_vxworks_vec;
92 return (abfd->xvec == &sh_elf32_vxworks_le_vec
93 || abfd->xvec == &sh_elf32_vxworks_vec);
99 /* Return true if OUTPUT_BFD is an FDPIC object. */
102 fdpic_object_p (bfd *abfd ATTRIBUTE_UNUSED)
104 #if !defined SH_TARGET_ALREADY_DEFINED
105 extern const bfd_target sh_elf32_fdpic_le_vec;
106 extern const bfd_target sh_elf32_fdpic_be_vec;
108 return (abfd->xvec == &sh_elf32_fdpic_le_vec
109 || abfd->xvec == &sh_elf32_fdpic_be_vec);
115 /* Return the howto table for ABFD. */
117 static reloc_howto_type *
118 get_howto_table (bfd *abfd)
120 if (vxworks_object_p (abfd))
121 return sh_vxworks_howto_table;
122 return sh_elf_howto_table;
125 static bfd_reloc_status_type
126 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
127 asection *input_section, bfd_byte *contents,
128 bfd_vma addr, asection *symbol_section,
129 bfd_vma start, bfd_vma end)
131 static bfd_vma last_addr;
132 static asection *last_symbol_section;
133 bfd_byte *start_ptr, *ptr, *last_ptr;
138 /* Sanity check the address. */
139 if (addr > bfd_get_section_limit (input_bfd, input_section))
140 return bfd_reloc_outofrange;
142 /* We require the start and end relocations to be processed consecutively -
143 although we allow then to be processed forwards or backwards. */
147 last_symbol_section = symbol_section;
150 if (last_addr != addr)
154 if (! symbol_section || last_symbol_section != symbol_section || end < start)
155 return bfd_reloc_outofrange;
157 /* Get the symbol_section contents. */
158 if (symbol_section != input_section)
160 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
161 contents = elf_section_data (symbol_section)->this_hdr.contents;
164 if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
167 if (contents != NULL)
169 return bfd_reloc_outofrange;
173 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
174 start_ptr = contents + start;
175 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
177 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
180 diff = (last_ptr - ptr) >> 1;
181 cum_diff += diff & 1;
184 /* Calculate the start / end values to load into rs / re minus four -
185 so that will cancel out the four we would otherwise have to add to
186 addr to get the value to subtract in order to get relative addressing. */
190 end = (ptr + cum_diff * 2) - contents;
194 bfd_vma start0 = start - 4;
196 while (start0 && IS_PPI (contents + start0))
198 start0 = start - 2 - ((start - start0) & 2);
199 start = start0 - cum_diff - 2;
204 && elf_section_data (symbol_section)->this_hdr.contents != contents)
207 insn = bfd_get_16 (input_bfd, contents + addr);
209 x = (insn & 0x200 ? end : start) - addr;
210 if (input_section != symbol_section)
211 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
212 - (input_section->output_section->vma
213 + input_section->output_offset));
215 if (x < -128 || x > 127)
216 return bfd_reloc_overflow;
218 x = (insn & ~0xff) | (x & 0xff);
219 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
224 /* This function is used for normal relocs. This used to be like the COFF
225 function, and is almost certainly incorrect for other ELF targets. */
227 static bfd_reloc_status_type
228 sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
229 void *data, asection *input_section, bfd *output_bfd,
230 char **error_message ATTRIBUTE_UNUSED)
234 enum elf_sh_reloc_type r_type;
235 bfd_vma addr = reloc_entry->address;
236 bfd_byte *hit_data = addr + (bfd_byte *) data;
238 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
240 if (output_bfd != NULL)
242 /* Partial linking--do nothing. */
243 reloc_entry->address += input_section->output_offset;
247 /* Almost all relocs have to do with relaxing. If any work must be
248 done for them, it has been done in sh_relax_section. */
249 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
252 if (symbol_in != NULL
253 && bfd_is_und_section (symbol_in->section))
254 return bfd_reloc_undefined;
256 /* PR 17512: file: 9891ca98. */
257 if (addr * bfd_octets_per_byte (abfd) + bfd_get_reloc_size (reloc_entry->howto)
258 > bfd_get_section_limit_octets (abfd, input_section))
259 return bfd_reloc_outofrange;
261 if (bfd_is_com_section (symbol_in->section))
264 sym_value = (symbol_in->value +
265 symbol_in->section->output_section->vma +
266 symbol_in->section->output_offset);
271 insn = bfd_get_32 (abfd, hit_data);
272 insn += sym_value + reloc_entry->addend;
273 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
276 insn = bfd_get_16 (abfd, hit_data);
277 sym_value += reloc_entry->addend;
278 sym_value -= (input_section->output_section->vma
279 + input_section->output_offset
282 sym_value += (insn & 0xfff) << 1;
285 insn = (insn & 0xf000) | (sym_value & 0xfff);
286 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
287 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
288 return bfd_reloc_overflow;
298 /* This function is used for relocs which are only used for relaxing,
299 which the linker should otherwise ignore. */
301 static bfd_reloc_status_type
302 sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
303 asymbol *symbol ATTRIBUTE_UNUSED,
304 void *data ATTRIBUTE_UNUSED, asection *input_section,
306 char **error_message ATTRIBUTE_UNUSED)
308 if (output_bfd != NULL)
309 reloc_entry->address += input_section->output_offset;
313 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
317 bfd_reloc_code_real_type bfd_reloc_val;
318 unsigned char elf_reloc_val;
321 /* An array mapping BFD reloc codes to SH ELF relocs. */
323 static const struct elf_reloc_map sh_reloc_map[] =
325 { BFD_RELOC_NONE, R_SH_NONE },
326 { BFD_RELOC_32, R_SH_DIR32 },
327 { BFD_RELOC_16, R_SH_DIR16 },
328 { BFD_RELOC_8, R_SH_DIR8 },
329 { BFD_RELOC_CTOR, R_SH_DIR32 },
330 { BFD_RELOC_32_PCREL, R_SH_REL32 },
331 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
332 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
333 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
334 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
335 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
336 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
337 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
338 { BFD_RELOC_SH_USES, R_SH_USES },
339 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
340 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
341 { BFD_RELOC_SH_CODE, R_SH_CODE },
342 { BFD_RELOC_SH_DATA, R_SH_DATA },
343 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
344 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
345 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
346 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
347 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
348 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
349 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
350 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
351 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
352 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
353 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
354 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
355 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
356 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
357 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
358 { BFD_RELOC_SH_COPY, R_SH_COPY },
359 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
360 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
361 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
362 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
363 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
364 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
365 { BFD_RELOC_SH_GOT20, R_SH_GOT20 },
366 { BFD_RELOC_SH_GOTOFF20, R_SH_GOTOFF20 },
367 { BFD_RELOC_SH_GOTFUNCDESC, R_SH_GOTFUNCDESC },
368 { BFD_RELOC_SH_GOTFUNCDESC20, R_SH_GOTFUNCDESC20 },
369 { BFD_RELOC_SH_GOTOFFFUNCDESC, R_SH_GOTOFFFUNCDESC },
370 { BFD_RELOC_SH_GOTOFFFUNCDESC20, R_SH_GOTOFFFUNCDESC20 },
371 { BFD_RELOC_SH_FUNCDESC, R_SH_FUNCDESC },
374 /* Given a BFD reloc code, return the howto structure for the
375 corresponding SH ELF reloc. */
377 static reloc_howto_type *
378 sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
382 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
384 if (sh_reloc_map[i].bfd_reloc_val == code)
385 return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
391 static reloc_howto_type *
392 sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
396 if (vxworks_object_p (abfd))
399 i < (sizeof (sh_vxworks_howto_table)
400 / sizeof (sh_vxworks_howto_table[0]));
402 if (sh_vxworks_howto_table[i].name != NULL
403 && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
404 return &sh_vxworks_howto_table[i];
409 i < (sizeof (sh_elf_howto_table)
410 / sizeof (sh_elf_howto_table[0]));
412 if (sh_elf_howto_table[i].name != NULL
413 && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
414 return &sh_elf_howto_table[i];
420 /* Given an ELF reloc, fill in the howto field of a relent. */
423 sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
427 r = ELF32_R_TYPE (dst->r_info);
430 || (r >= R_SH_FIRST_INVALID_RELOC && r <= R_SH_LAST_INVALID_RELOC)
431 || (r >= R_SH_FIRST_INVALID_RELOC_2 && r <= R_SH_LAST_INVALID_RELOC_2)
432 || (r >= R_SH_FIRST_INVALID_RELOC_3 && r <= R_SH_LAST_INVALID_RELOC_3)
433 || (r >= R_SH_FIRST_INVALID_RELOC_4 && r <= R_SH_LAST_INVALID_RELOC_4)
434 || (r >= R_SH_FIRST_INVALID_RELOC_5 && r <= R_SH_LAST_INVALID_RELOC_5)
435 || (r >= R_SH_FIRST_INVALID_RELOC_6 && r <= R_SH_LAST_INVALID_RELOC_6))
437 /* xgettext:c-format */
438 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
440 bfd_set_error (bfd_error_bad_value);
444 cache_ptr->howto = get_howto_table (abfd) + r;
448 /* This function handles relaxing for SH ELF. See the corresponding
449 function in coff-sh.c for a description of what this does. FIXME:
450 There is a lot of duplication here between this code and the COFF
451 specific code. The format of relocs and symbols is wound deeply
452 into this code, but it would still be better if the duplication
453 could be eliminated somehow. Note in particular that although both
454 functions use symbols like R_SH_CODE, those symbols have different
455 values; in coff-sh.c they come from include/coff/sh.h, whereas here
456 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
459 sh_elf_relax_section (bfd *abfd, asection *sec,
460 struct bfd_link_info *link_info, bfd_boolean *again)
462 Elf_Internal_Shdr *symtab_hdr;
463 Elf_Internal_Rela *internal_relocs;
464 bfd_boolean have_code;
465 Elf_Internal_Rela *irel, *irelend;
466 bfd_byte *contents = NULL;
467 Elf_Internal_Sym *isymbuf = NULL;
471 if (bfd_link_relocatable (link_info)
472 || (sec->flags & SEC_RELOC) == 0
473 || sec->reloc_count == 0)
476 symtab_hdr = &elf_symtab_hdr (abfd);
478 internal_relocs = (_bfd_elf_link_read_relocs
479 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
480 link_info->keep_memory));
481 if (internal_relocs == NULL)
486 irelend = internal_relocs + sec->reloc_count;
487 for (irel = internal_relocs; irel < irelend; irel++)
489 bfd_vma laddr, paddr, symval;
491 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
494 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
497 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
500 /* Get the section contents. */
501 if (contents == NULL)
503 if (elf_section_data (sec)->this_hdr.contents != NULL)
504 contents = elf_section_data (sec)->this_hdr.contents;
507 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
512 /* The r_addend field of the R_SH_USES reloc will point us to
513 the register load. The 4 is because the r_addend field is
514 computed as though it were a jump offset, which are based
515 from 4 bytes after the jump instruction. */
516 laddr = irel->r_offset + 4 + irel->r_addend;
517 if (laddr >= sec->size)
519 /* xgettext:c-format */
521 (_("%pB: %#" PRIx64 ": warning: bad R_SH_USES offset"),
522 abfd, (uint64_t) irel->r_offset);
525 insn = bfd_get_16 (abfd, contents + laddr);
527 /* If the instruction is not mov.l NN,rN, we don't know what to
529 if ((insn & 0xf000) != 0xd000)
532 /* xgettext:c-format */
533 (_("%pB: %#" PRIx64 ": warning: "
534 "R_SH_USES points to unrecognized insn 0x%x"),
535 abfd, (uint64_t) irel->r_offset, insn);
539 /* Get the address from which the register is being loaded. The
540 displacement in the mov.l instruction is quadrupled. It is a
541 displacement from four bytes after the movl instruction, but,
542 before adding in the PC address, two least significant bits
543 of the PC are cleared. We assume that the section is aligned
544 on a four byte boundary. */
547 paddr += (laddr + 4) &~ (bfd_vma) 3;
548 if (paddr >= sec->size)
551 /* xgettext:c-format */
552 (_("%pB: %#" PRIx64 ": warning: bad R_SH_USES load offset"),
553 abfd, (uint64_t) irel->r_offset);
557 /* Get the reloc for the address from which the register is
558 being loaded. This reloc will tell us which function is
559 actually being called. */
560 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
561 if (irelfn->r_offset == paddr
562 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
564 if (irelfn >= irelend)
567 /* xgettext:c-format */
568 (_("%pB: %#" PRIx64 ": warning: could not find expected reloc"),
569 abfd, (uint64_t) paddr);
573 /* Read this BFD's symbols if we haven't done so already. */
574 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
576 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
578 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
579 symtab_hdr->sh_info, 0,
585 /* Get the value of the symbol referred to by the reloc. */
586 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
588 /* A local symbol. */
589 Elf_Internal_Sym *isym;
591 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
593 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
596 /* xgettext:c-format */
597 (_("%pB: %#" PRIx64 ": warning: symbol in unexpected section"),
598 abfd, (uint64_t) paddr);
602 symval = (isym->st_value
603 + sec->output_section->vma
604 + sec->output_offset);
609 struct elf_link_hash_entry *h;
611 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
612 h = elf_sym_hashes (abfd)[indx];
613 BFD_ASSERT (h != NULL);
614 if (h->root.type != bfd_link_hash_defined
615 && h->root.type != bfd_link_hash_defweak)
617 /* This appears to be a reference to an undefined
618 symbol. Just ignore it--it will be caught by the
619 regular reloc processing. */
623 symval = (h->root.u.def.value
624 + h->root.u.def.section->output_section->vma
625 + h->root.u.def.section->output_offset);
628 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
629 symval += bfd_get_32 (abfd, contents + paddr);
631 symval += irelfn->r_addend;
633 /* See if this function call can be shortened. */
636 + sec->output_section->vma
639 /* A branch to an address beyond ours might be increased by an
640 .align that doesn't move when bytes behind us are deleted.
641 So, we add some slop in this calculation to allow for
643 if (foff < -0x1000 || foff >= 0x1000 - 8)
645 /* After all that work, we can't shorten this function call. */
649 /* Shorten the function call. */
651 /* For simplicity of coding, we are going to modify the section
652 contents, the section relocs, and the BFD symbol table. We
653 must tell the rest of the code not to free up this
654 information. It would be possible to instead create a table
655 of changes which have to be made, as is done in coff-mips.c;
656 that would be more work, but would require less memory when
657 the linker is run. */
659 elf_section_data (sec)->relocs = internal_relocs;
660 elf_section_data (sec)->this_hdr.contents = contents;
661 symtab_hdr->contents = (unsigned char *) isymbuf;
663 /* Replace the jmp/jsr with a bra/bsr. */
665 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
666 replace the jmp/jsr with a bra/bsr. */
667 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
668 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
669 here, but that only checks if the symbol is an external symbol,
670 not if the symbol is in a different section. Besides, we need
671 a consistent meaning for the relocation, so we just assume here that
672 the value of the symbol is not available. */
674 /* We can't fully resolve this yet, because the external
675 symbol value may be changed by future relaxing. We let
676 the final link phase handle it. */
677 if (bfd_get_16 (abfd, contents + irel->r_offset) & 0x0020)
678 bfd_put_16 (abfd, (bfd_vma) 0xa000, contents + irel->r_offset);
680 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
684 /* When we calculated the symbol "value" we had an offset in the
685 DIR32's word in memory (we read and add it above). However,
686 the jsr we create does NOT have this offset encoded, so we
687 have to add it to the addend to preserve it. */
688 irel->r_addend += bfd_get_32 (abfd, contents + paddr);
690 /* See if there is another R_SH_USES reloc referring to the same
692 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
693 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
694 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
696 if (irelscan < irelend)
698 /* Some other function call depends upon this register load,
699 and we have not yet converted that function call.
700 Indeed, we may never be able to convert it. There is
701 nothing else we can do at this point. */
705 /* Look for a R_SH_COUNT reloc on the location where the
706 function address is stored. Do this before deleting any
707 bytes, to avoid confusion about the address. */
708 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
709 if (irelcount->r_offset == paddr
710 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
713 /* Delete the register load. */
714 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
717 /* That will change things, so, just in case it permits some
718 other function call to come within range, we should relax
719 again. Note that this is not required, and it may be slow. */
722 /* Now check whether we got a COUNT reloc. */
723 if (irelcount >= irelend)
726 /* xgettext:c-format */
727 (_("%pB: %#" PRIx64 ": warning: "
728 "could not find expected COUNT reloc"),
729 abfd, (uint64_t) paddr);
733 /* The number of uses is stored in the r_addend field. We've
735 if (irelcount->r_addend == 0)
737 /* xgettext:c-format */
738 _bfd_error_handler (_("%pB: %#" PRIx64 ": warning: bad count"),
739 abfd, (uint64_t) paddr);
743 --irelcount->r_addend;
745 /* If there are no more uses, we can delete the address. Reload
746 the address from irelfn, in case it was changed by the
747 previous call to sh_elf_relax_delete_bytes. */
748 if (irelcount->r_addend == 0)
750 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
754 /* We've done all we can with that function call. */
757 /* Look for load and store instructions that we can align on four
759 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
764 /* Get the section contents. */
765 if (contents == NULL)
767 if (elf_section_data (sec)->this_hdr.contents != NULL)
768 contents = elf_section_data (sec)->this_hdr.contents;
771 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
776 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
782 elf_section_data (sec)->relocs = internal_relocs;
783 elf_section_data (sec)->this_hdr.contents = contents;
784 symtab_hdr->contents = (unsigned char *) isymbuf;
789 && symtab_hdr->contents != (unsigned char *) isymbuf)
791 if (! link_info->keep_memory)
795 /* Cache the symbols for elf_link_input_bfd. */
796 symtab_hdr->contents = (unsigned char *) isymbuf;
801 && elf_section_data (sec)->this_hdr.contents != contents)
803 if (! link_info->keep_memory)
807 /* Cache the section contents for elf_link_input_bfd. */
808 elf_section_data (sec)->this_hdr.contents = contents;
812 if (internal_relocs != NULL
813 && elf_section_data (sec)->relocs != internal_relocs)
814 free (internal_relocs);
820 && symtab_hdr->contents != (unsigned char *) isymbuf)
823 && elf_section_data (sec)->this_hdr.contents != contents)
825 if (internal_relocs != NULL
826 && elf_section_data (sec)->relocs != internal_relocs)
827 free (internal_relocs);
832 /* Delete some bytes from a section while relaxing. FIXME: There is a
833 lot of duplication between this function and sh_relax_delete_bytes
837 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
840 Elf_Internal_Shdr *symtab_hdr;
841 unsigned int sec_shndx;
843 Elf_Internal_Rela *irel, *irelend;
844 Elf_Internal_Rela *irelalign;
846 Elf_Internal_Sym *isymbuf, *isym, *isymend;
847 struct elf_link_hash_entry **sym_hashes;
848 struct elf_link_hash_entry **end_hashes;
849 unsigned int symcount;
852 symtab_hdr = &elf_symtab_hdr (abfd);
853 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
855 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
857 contents = elf_section_data (sec)->this_hdr.contents;
859 /* The deletion must stop at the next ALIGN reloc for an alignment
860 power larger than the number of bytes we are deleting. */
865 irel = elf_section_data (sec)->relocs;
866 irelend = irel + sec->reloc_count;
867 for (; irel < irelend; irel++)
869 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
870 && irel->r_offset > addr
871 && count < (1 << irel->r_addend))
874 toaddr = irel->r_offset;
879 /* Actually delete the bytes. */
880 memmove (contents + addr, contents + addr + count,
881 (size_t) (toaddr - addr - count));
882 if (irelalign == NULL)
888 #define NOP_OPCODE (0x0009)
890 BFD_ASSERT ((count & 1) == 0);
891 for (i = 0; i < count; i += 2)
892 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
895 /* Adjust all the relocs. */
896 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
898 bfd_vma nraddr, stop;
901 int off, adjust, oinsn;
902 bfd_signed_vma voff = 0;
903 bfd_boolean overflow;
905 /* Get the new reloc address. */
906 nraddr = irel->r_offset;
907 if ((irel->r_offset > addr
908 && irel->r_offset < toaddr)
909 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
910 && irel->r_offset == toaddr))
913 /* See if this reloc was for the bytes we have deleted, in which
914 case we no longer care about it. Don't delete relocs which
915 represent addresses, though. */
916 if (irel->r_offset >= addr
917 && irel->r_offset < addr + count
918 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
919 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
920 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
921 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
922 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
925 /* If this is a PC relative reloc, see if the range it covers
926 includes the bytes we have deleted. */
927 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
936 start = irel->r_offset;
937 insn = bfd_get_16 (abfd, contents + nraddr);
941 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
948 /* If this reloc is against a symbol defined in this
949 section, and the symbol will not be adjusted below, we
950 must check the addend to see it will put the value in
951 range to be adjusted, and hence must be changed. */
952 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
954 isym = isymbuf + ELF32_R_SYM (irel->r_info);
955 if (isym->st_shndx == sec_shndx
956 && (isym->st_value <= addr
957 || isym->st_value >= toaddr))
961 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
963 val = bfd_get_32 (abfd, contents + nraddr);
964 val += isym->st_value;
965 if (val > addr && val < toaddr)
966 bfd_put_32 (abfd, val - count, contents + nraddr);
970 val = isym->st_value + irel->r_addend;
971 if (val > addr && val < toaddr)
972 irel->r_addend -= count;
983 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
990 /* This has been made by previous relaxation. Since the
991 relocation will be against an external symbol, the
992 final relocation will just do the right thing. */
999 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1001 /* The addend will be against the section symbol, thus
1002 for adjusting the addend, the relevant start is the
1003 start of the section.
1004 N.B. If we want to abandon in-place changes here and
1005 test directly using symbol + addend, we have to take into
1006 account that the addend has already been adjusted by -4. */
1007 if (stop > addr && stop < toaddr)
1008 irel->r_addend -= count;
1014 stop = start + 4 + off * 2;
1019 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
1025 /* These relocs types represent
1027 The r_addend field holds the difference between the reloc
1028 address and L1. That is the start of the reloc, and
1029 adding in the contents gives us the top. We must adjust
1030 both the r_offset field and the section contents.
1031 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1032 and the elf bfd r_offset is called r_vaddr. */
1034 stop = irel->r_offset;
1035 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1039 && (stop <= addr || stop >= toaddr))
1040 irel->r_addend += count;
1041 else if (stop > addr
1043 && (start <= addr || start >= toaddr))
1044 irel->r_addend -= count;
1046 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1047 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1048 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1049 voff = bfd_get_8 (abfd, contents + nraddr);
1051 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1052 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1057 start = irel->r_offset;
1058 stop = (bfd_vma) ((bfd_signed_vma) start
1059 + (long) irel->r_addend
1066 && (stop <= addr || stop >= toaddr))
1068 else if (stop > addr
1070 && (start <= addr || start >= toaddr))
1079 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1088 if ((oinsn & 0xff00) != (insn & 0xff00))
1090 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1095 if ((oinsn & 0xf000) != (insn & 0xf000))
1097 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1101 BFD_ASSERT (adjust == count || count >= 4);
1106 if ((irel->r_offset & 3) == 0)
1109 if ((oinsn & 0xff00) != (insn & 0xff00))
1111 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1116 if (voff < 0 || voff >= 0xff)
1118 bfd_put_8 (abfd, voff, contents + nraddr);
1123 if (voff < - 0x8000 || voff >= 0x8000)
1125 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1130 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1134 irel->r_addend += adjust;
1141 /* xgettext:c-format */
1142 (_("%pB: %#" PRIx64 ": fatal: reloc overflow while relaxing"),
1143 abfd, (uint64_t) irel->r_offset);
1144 bfd_set_error (bfd_error_bad_value);
1149 irel->r_offset = nraddr;
1152 /* Look through all the other sections. If there contain any IMM32
1153 relocs against internal symbols which we are not going to adjust
1154 below, we may need to adjust the addends. */
1155 for (o = abfd->sections; o != NULL; o = o->next)
1157 Elf_Internal_Rela *internal_relocs;
1158 Elf_Internal_Rela *irelscan, *irelscanend;
1159 bfd_byte *ocontents;
1162 || (o->flags & SEC_RELOC) == 0
1163 || o->reloc_count == 0)
1166 /* We always cache the relocs. Perhaps, if info->keep_memory is
1167 FALSE, we should free them, if we are permitted to, when we
1168 leave sh_coff_relax_section. */
1169 internal_relocs = (_bfd_elf_link_read_relocs
1170 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
1171 if (internal_relocs == NULL)
1175 irelscanend = internal_relocs + o->reloc_count;
1176 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1178 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1179 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1181 bfd_vma start, stop;
1182 bfd_signed_vma voff;
1184 if (ocontents == NULL)
1186 if (elf_section_data (o)->this_hdr.contents != NULL)
1187 ocontents = elf_section_data (o)->this_hdr.contents;
1190 /* We always cache the section contents.
1191 Perhaps, if info->keep_memory is FALSE, we
1192 should free them, if we are permitted to,
1193 when we leave sh_coff_relax_section. */
1194 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1196 if (ocontents != NULL)
1201 elf_section_data (o)->this_hdr.contents = ocontents;
1205 stop = irelscan->r_offset;
1207 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1209 /* STOP is in a different section, so it won't change. */
1210 if (start > addr && start < toaddr)
1211 irelscan->r_addend += count;
1213 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1214 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1218 && (stop <= addr || stop >= toaddr))
1219 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
1220 ocontents + irelscan->r_offset);
1221 else if (stop > addr
1223 && (start <= addr || start >= toaddr))
1224 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
1225 ocontents + irelscan->r_offset);
1228 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1231 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1235 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1236 if (isym->st_shndx == sec_shndx
1237 && (isym->st_value <= addr
1238 || isym->st_value >= toaddr))
1242 if (ocontents == NULL)
1244 if (elf_section_data (o)->this_hdr.contents != NULL)
1245 ocontents = elf_section_data (o)->this_hdr.contents;
1248 /* We always cache the section contents.
1249 Perhaps, if info->keep_memory is FALSE, we
1250 should free them, if we are permitted to,
1251 when we leave sh_coff_relax_section. */
1252 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1254 if (ocontents != NULL)
1259 elf_section_data (o)->this_hdr.contents = ocontents;
1263 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1264 val += isym->st_value;
1265 if (val > addr && val < toaddr)
1266 bfd_put_32 (abfd, val - count,
1267 ocontents + irelscan->r_offset);
1272 /* Adjust the local symbols defined in this section. */
1273 isymend = isymbuf + symtab_hdr->sh_info;
1274 for (isym = isymbuf; isym < isymend; isym++)
1276 if (isym->st_shndx == sec_shndx
1277 && isym->st_value > addr
1278 && isym->st_value < toaddr)
1279 isym->st_value -= count;
1282 /* Now adjust the global symbols defined in this section. */
1283 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1284 - symtab_hdr->sh_info);
1285 sym_hashes = elf_sym_hashes (abfd);
1286 end_hashes = sym_hashes + symcount;
1287 for (; sym_hashes < end_hashes; sym_hashes++)
1289 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1290 if ((sym_hash->root.type == bfd_link_hash_defined
1291 || sym_hash->root.type == bfd_link_hash_defweak)
1292 && sym_hash->root.u.def.section == sec
1293 && sym_hash->root.u.def.value > addr
1294 && sym_hash->root.u.def.value < toaddr)
1296 sym_hash->root.u.def.value -= count;
1300 /* See if we can move the ALIGN reloc forward. We have adjusted
1301 r_offset for it already. */
1302 if (irelalign != NULL)
1304 bfd_vma alignto, alignaddr;
1306 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1307 alignaddr = BFD_ALIGN (irelalign->r_offset,
1308 1 << irelalign->r_addend);
1309 if (alignto != alignaddr)
1311 /* Tail recursion. */
1312 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1313 (int) (alignto - alignaddr));
1320 /* Look for loads and stores which we can align to four byte
1321 boundaries. This is like sh_align_loads in coff-sh.c. */
1324 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1325 Elf_Internal_Rela *internal_relocs,
1326 bfd_byte *contents ATTRIBUTE_UNUSED,
1327 bfd_boolean *pswapped)
1329 Elf_Internal_Rela *irel, *irelend;
1330 bfd_vma *labels = NULL;
1331 bfd_vma *label, *label_end;
1336 irelend = internal_relocs + sec->reloc_count;
1338 /* Get all the addresses with labels on them. */
1339 amt = sec->reloc_count;
1340 amt *= sizeof (bfd_vma);
1341 labels = (bfd_vma *) bfd_malloc (amt);
1345 for (irel = internal_relocs; irel < irelend; irel++)
1347 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1349 *label_end = irel->r_offset;
1354 /* Note that the assembler currently always outputs relocs in
1355 address order. If that ever changes, this code will need to sort
1356 the label values and the relocs. */
1360 for (irel = internal_relocs; irel < irelend; irel++)
1362 bfd_vma start, stop;
1364 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1367 start = irel->r_offset;
1369 for (irel++; irel < irelend; irel++)
1370 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1373 stop = irel->r_offset;
1377 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1378 internal_relocs, &label,
1379 label_end, start, stop, pswapped))
1393 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1396 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1397 bfd_byte *contents, bfd_vma addr)
1399 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1400 unsigned short i1, i2;
1401 Elf_Internal_Rela *irel, *irelend;
1403 /* Swap the instructions themselves. */
1404 i1 = bfd_get_16 (abfd, contents + addr);
1405 i2 = bfd_get_16 (abfd, contents + addr + 2);
1406 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1407 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
1409 /* Adjust all reloc addresses. */
1410 irelend = internal_relocs + sec->reloc_count;
1411 for (irel = internal_relocs; irel < irelend; irel++)
1413 enum elf_sh_reloc_type type;
1416 /* There are a few special types of relocs that we don't want to
1417 adjust. These relocs do not apply to the instruction itself,
1418 but are only associated with the address. */
1419 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1420 if (type == R_SH_ALIGN
1421 || type == R_SH_CODE
1422 || type == R_SH_DATA
1423 || type == R_SH_LABEL)
1426 /* If an R_SH_USES reloc points to one of the addresses being
1427 swapped, we must adjust it. It would be incorrect to do this
1428 for a jump, though, since we want to execute both
1429 instructions after the jump. (We have avoided swapping
1430 around a label, so the jump will not wind up executing an
1431 instruction it shouldn't). */
1432 if (type == R_SH_USES)
1436 off = irel->r_offset + 4 + irel->r_addend;
1438 irel->r_offset += 2;
1439 else if (off == addr + 2)
1440 irel->r_offset -= 2;
1443 if (irel->r_offset == addr)
1445 irel->r_offset += 2;
1448 else if (irel->r_offset == addr + 2)
1450 irel->r_offset -= 2;
1459 unsigned short insn, oinsn;
1460 bfd_boolean overflow;
1462 loc = contents + irel->r_offset;
1471 insn = bfd_get_16 (abfd, loc);
1474 if ((oinsn & 0xff00) != (insn & 0xff00))
1476 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1480 insn = bfd_get_16 (abfd, loc);
1483 if ((oinsn & 0xf000) != (insn & 0xf000))
1485 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1489 /* This reloc ignores the least significant 3 bits of
1490 the program counter before adding in the offset.
1491 This means that if ADDR is at an even address, the
1492 swap will not affect the offset. If ADDR is an at an
1493 odd address, then the instruction will be crossing a
1494 four byte boundary, and must be adjusted. */
1495 if ((addr & 3) != 0)
1497 insn = bfd_get_16 (abfd, loc);
1500 if ((oinsn & 0xff00) != (insn & 0xff00))
1502 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1511 /* xgettext:c-format */
1512 (_("%pB: %#" PRIx64 ": fatal: reloc overflow while relaxing"),
1513 abfd, (uint64_t) irel->r_offset);
1514 bfd_set_error (bfd_error_bad_value);
1523 /* Describes one of the various PLT styles. */
1525 struct elf_sh_plt_info
1527 /* The template for the first PLT entry, or NULL if there is no special
1529 const bfd_byte *plt0_entry;
1531 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1532 bfd_vma plt0_entry_size;
1534 /* Index I is the offset into PLT0_ENTRY of a pointer to
1535 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1536 if there is no such pointer. */
1537 bfd_vma plt0_got_fields[3];
1539 /* The template for a symbol's PLT entry. */
1540 const bfd_byte *symbol_entry;
1542 /* The size of SYMBOL_ENTRY in bytes. */
1543 bfd_vma symbol_entry_size;
1545 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1546 on all targets. The comments by each member indicate the value
1547 that the field must hold. */
1549 bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1550 bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1551 bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1552 bfd_boolean got20; /* TRUE if got_entry points to a movi20
1553 instruction (instead of a constant pool
1557 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1558 bfd_vma symbol_resolve_offset;
1560 /* A different PLT layout which can be used for the first
1561 MAX_SHORT_PLT entries. It must share the same plt0. NULL in
1563 const struct elf_sh_plt_info *short_plt;
1566 /* The size in bytes of an entry in the procedure linkage table. */
1568 #define ELF_PLT_ENTRY_SIZE 28
1570 /* First entry in an absolute procedure linkage table look like this. */
1572 /* Note - this code has been "optimised" not to use r2. r2 is used by
1573 GCC to return the address of large structures, so it should not be
1574 corrupted here. This does mean however, that this PLT does not conform
1575 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1576 and r2 contains the GOT id. This version stores the GOT id in r0 and
1577 ignores the type. Loaders can easily detect this difference however,
1578 since the type will always be 0 or 8, and the GOT ids will always be
1579 greater than or equal to 12. */
1580 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1582 0xd0, 0x05, /* mov.l 2f,r0 */
1583 0x60, 0x02, /* mov.l @r0,r0 */
1584 0x2f, 0x06, /* mov.l r0,@-r15 */
1585 0xd0, 0x03, /* mov.l 1f,r0 */
1586 0x60, 0x02, /* mov.l @r0,r0 */
1587 0x40, 0x2b, /* jmp @r0 */
1588 0x60, 0xf6, /* mov.l @r15+,r0 */
1589 0x00, 0x09, /* nop */
1590 0x00, 0x09, /* nop */
1591 0x00, 0x09, /* nop */
1592 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1593 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1596 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1598 0x05, 0xd0, /* mov.l 2f,r0 */
1599 0x02, 0x60, /* mov.l @r0,r0 */
1600 0x06, 0x2f, /* mov.l r0,@-r15 */
1601 0x03, 0xd0, /* mov.l 1f,r0 */
1602 0x02, 0x60, /* mov.l @r0,r0 */
1603 0x2b, 0x40, /* jmp @r0 */
1604 0xf6, 0x60, /* mov.l @r15+,r0 */
1605 0x09, 0x00, /* nop */
1606 0x09, 0x00, /* nop */
1607 0x09, 0x00, /* nop */
1608 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1609 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1612 /* Sebsequent entries in an absolute procedure linkage table look like
1615 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1617 0xd0, 0x04, /* mov.l 1f,r0 */
1618 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1619 0xd1, 0x02, /* mov.l 0f,r1 */
1620 0x40, 0x2b, /* jmp @r0 */
1621 0x60, 0x13, /* mov r1,r0 */
1622 0xd1, 0x03, /* mov.l 2f,r1 */
1623 0x40, 0x2b, /* jmp @r0 */
1624 0x00, 0x09, /* nop */
1625 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1626 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1627 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1630 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1632 0x04, 0xd0, /* mov.l 1f,r0 */
1633 0x02, 0x60, /* mov.l @r0,r0 */
1634 0x02, 0xd1, /* mov.l 0f,r1 */
1635 0x2b, 0x40, /* jmp @r0 */
1636 0x13, 0x60, /* mov r1,r0 */
1637 0x03, 0xd1, /* mov.l 2f,r1 */
1638 0x2b, 0x40, /* jmp @r0 */
1639 0x09, 0x00, /* nop */
1640 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1641 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1642 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1645 /* Entries in a PIC procedure linkage table look like this. */
1647 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1649 0xd0, 0x04, /* mov.l 1f,r0 */
1650 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1651 0x40, 0x2b, /* jmp @r0 */
1652 0x00, 0x09, /* nop */
1653 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1654 0xd1, 0x03, /* mov.l 2f,r1 */
1655 0x40, 0x2b, /* jmp @r0 */
1656 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1657 0x00, 0x09, /* nop */
1658 0x00, 0x09, /* nop */
1659 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1660 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1663 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1665 0x04, 0xd0, /* mov.l 1f,r0 */
1666 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1667 0x2b, 0x40, /* jmp @r0 */
1668 0x09, 0x00, /* nop */
1669 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1670 0x03, 0xd1, /* mov.l 2f,r1 */
1671 0x2b, 0x40, /* jmp @r0 */
1672 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1673 0x09, 0x00, /* nop */
1674 0x09, 0x00, /* nop */
1675 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1676 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1679 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1682 /* Big-endian non-PIC. */
1683 elf_sh_plt0_entry_be,
1685 { MINUS_ONE, 24, 20 },
1686 elf_sh_plt_entry_be,
1688 { 20, 16, 24, FALSE },
1693 /* Little-endian non-PIC. */
1694 elf_sh_plt0_entry_le,
1696 { MINUS_ONE, 24, 20 },
1697 elf_sh_plt_entry_le,
1699 { 20, 16, 24, FALSE },
1706 /* Big-endian PIC. */
1707 elf_sh_plt0_entry_be,
1709 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1710 elf_sh_pic_plt_entry_be,
1712 { 20, MINUS_ONE, 24, FALSE },
1717 /* Little-endian PIC. */
1718 elf_sh_plt0_entry_le,
1720 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1721 elf_sh_pic_plt_entry_le,
1723 { 20, MINUS_ONE, 24, FALSE },
1730 #define VXWORKS_PLT_HEADER_SIZE 12
1731 #define VXWORKS_PLT_ENTRY_SIZE 24
1733 static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
1735 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1736 0x61, 0x12, /* mov.l @r1,r1 */
1737 0x41, 0x2b, /* jmp @r1 */
1738 0x00, 0x09, /* nop */
1739 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1742 static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
1744 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1745 0x12, 0x61, /* mov.l @r1,r1 */
1746 0x2b, 0x41, /* jmp @r1 */
1747 0x09, 0x00, /* nop */
1748 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1751 static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1753 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1754 0x60, 0x02, /* mov.l @r0,r0 */
1755 0x40, 0x2b, /* jmp @r0 */
1756 0x00, 0x09, /* nop */
1757 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1758 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1759 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1760 0x00, 0x09, /* nop */
1761 0x00, 0x09, /* nop */
1762 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1765 static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1767 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1768 0x02, 0x60, /* mov.l @r0,r0 */
1769 0x2b, 0x40, /* jmp @r0 */
1770 0x09, 0x00, /* nop */
1771 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1772 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1773 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1774 0x09, 0x00, /* nop */
1775 0x09, 0x00, /* nop */
1776 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1779 static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1781 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1782 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1783 0x40, 0x2b, /* jmp @r0 */
1784 0x00, 0x09, /* nop */
1785 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1786 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1787 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1788 0x41, 0x2b, /* jmp @r1 */
1789 0x00, 0x09, /* nop */
1790 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1793 static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1795 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1796 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1797 0x2b, 0x40, /* jmp @r0 */
1798 0x09, 0x00, /* nop */
1799 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1800 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1801 0xc2, 0x51, /* mov.l @(8,r12),r1 */
1802 0x2b, 0x41, /* jmp @r1 */
1803 0x09, 0x00, /* nop */
1804 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1807 static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
1810 /* Big-endian non-PIC. */
1811 vxworks_sh_plt0_entry_be,
1812 VXWORKS_PLT_HEADER_SIZE,
1813 { MINUS_ONE, MINUS_ONE, 8 },
1814 vxworks_sh_plt_entry_be,
1815 VXWORKS_PLT_ENTRY_SIZE,
1816 { 8, 14, 20, FALSE },
1821 /* Little-endian non-PIC. */
1822 vxworks_sh_plt0_entry_le,
1823 VXWORKS_PLT_HEADER_SIZE,
1824 { MINUS_ONE, MINUS_ONE, 8 },
1825 vxworks_sh_plt_entry_le,
1826 VXWORKS_PLT_ENTRY_SIZE,
1827 { 8, 14, 20, FALSE },
1834 /* Big-endian PIC. */
1837 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1838 vxworks_sh_pic_plt_entry_be,
1839 VXWORKS_PLT_ENTRY_SIZE,
1840 { 8, MINUS_ONE, 20, FALSE },
1845 /* Little-endian PIC. */
1848 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1849 vxworks_sh_pic_plt_entry_le,
1850 VXWORKS_PLT_ENTRY_SIZE,
1851 { 8, MINUS_ONE, 20, FALSE },
1858 /* FDPIC PLT entries. Two unimplemented optimizations for lazy
1859 binding are to omit the lazy binding stub when linking with -z now
1860 and to move lazy binding stubs into a separate region for better
1863 #define FDPIC_PLT_ENTRY_SIZE 28
1864 #define FDPIC_PLT_LAZY_OFFSET 20
1866 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
1867 duplicated if it is out of range, or which can be inlined. So
1868 right now it is always inlined, which wastes a word per stub. It
1869 might be easier to handle the duplication if we put the lazy
1870 stubs separately. */
1872 static const bfd_byte fdpic_sh_plt_entry_be[FDPIC_PLT_ENTRY_SIZE] =
1874 0xd0, 0x02, /* mov.l @(12,pc),r0 */
1875 0x01, 0xce, /* mov.l @(r0,r12),r1 */
1876 0x70, 0x04, /* add #4, r0 */
1877 0x41, 0x2b, /* jmp @r1 */
1878 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1879 0x00, 0x09, /* nop */
1880 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1881 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1882 0x60, 0xc2, /* mov.l @r12,r0 */
1883 0x40, 0x2b, /* jmp @r0 */
1884 0x53, 0xc1, /* mov.l @(4,r12),r3 */
1885 0x00, 0x09, /* nop */
1888 static const bfd_byte fdpic_sh_plt_entry_le[FDPIC_PLT_ENTRY_SIZE] =
1890 0x02, 0xd0, /* mov.l @(12,pc),r0 */
1891 0xce, 0x01, /* mov.l @(r0,r12),r1 */
1892 0x04, 0x70, /* add #4, r0 */
1893 0x2b, 0x41, /* jmp @r1 */
1894 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1895 0x09, 0x00, /* nop */
1896 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1897 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1898 0xc2, 0x60, /* mov.l @r12,r0 */
1899 0x2b, 0x40, /* jmp @r0 */
1900 0xc1, 0x53, /* mov.l @(4,r12),r3 */
1901 0x09, 0x00, /* nop */
1904 static const struct elf_sh_plt_info fdpic_sh_plts[2] = {
1906 /* Big-endian PIC. */
1909 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1910 fdpic_sh_plt_entry_be,
1911 FDPIC_PLT_ENTRY_SIZE,
1912 { 12, MINUS_ONE, 16, FALSE },
1913 FDPIC_PLT_LAZY_OFFSET,
1917 /* Little-endian PIC. */
1920 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1921 fdpic_sh_plt_entry_le,
1922 FDPIC_PLT_ENTRY_SIZE,
1923 { 12, MINUS_ONE, 16, FALSE },
1924 FDPIC_PLT_LAZY_OFFSET,
1929 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
1930 entries for the first 64K slots. We use the normal FDPIC PLT entry
1931 past that point; we could also use movi20s, which might be faster,
1932 but would not be any smaller. */
1934 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
1935 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
1937 static const bfd_byte fdpic_sh2a_plt_entry_be[FDPIC_SH2A_PLT_ENTRY_SIZE] =
1939 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1940 0x01, 0xce, /* mov.l @(r0,r12),r1 */
1941 0x70, 0x04, /* add #4, r0 */
1942 0x41, 0x2b, /* jmp @r1 */
1943 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1944 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1945 0x60, 0xc2, /* mov.l @r12,r0 */
1946 0x40, 0x2b, /* jmp @r0 */
1947 0x53, 0xc1, /* mov.l @(4,r12),r3 */
1948 0x00, 0x09, /* nop */
1951 static const bfd_byte fdpic_sh2a_plt_entry_le[FDPIC_SH2A_PLT_ENTRY_SIZE] =
1953 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1954 0xce, 0x01, /* mov.l @(r0,r12),r1 */
1955 0x04, 0x70, /* add #4, r0 */
1956 0x2b, 0x41, /* jmp @r1 */
1957 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1958 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1959 0xc2, 0x60, /* mov.l @r12,r0 */
1960 0x2b, 0x40, /* jmp @r0 */
1961 0xc1, 0x53, /* mov.l @(4,r12),r3 */
1962 0x09, 0x00, /* nop */
1965 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be = {
1966 /* Big-endian FDPIC, max index 64K. */
1969 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1970 fdpic_sh2a_plt_entry_be,
1971 FDPIC_SH2A_PLT_ENTRY_SIZE,
1972 { 0, MINUS_ONE, 12, TRUE },
1973 FDPIC_SH2A_PLT_LAZY_OFFSET,
1977 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le = {
1978 /* Little-endian FDPIC, max index 64K. */
1981 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1982 fdpic_sh2a_plt_entry_le,
1983 FDPIC_SH2A_PLT_ENTRY_SIZE,
1984 { 0, MINUS_ONE, 12, TRUE },
1985 FDPIC_SH2A_PLT_LAZY_OFFSET,
1989 static const struct elf_sh_plt_info fdpic_sh2a_plts[2] = {
1991 /* Big-endian PIC. */
1994 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1995 fdpic_sh_plt_entry_be,
1996 FDPIC_PLT_ENTRY_SIZE,
1997 { 12, MINUS_ONE, 16, FALSE },
1998 FDPIC_PLT_LAZY_OFFSET,
1999 &fdpic_sh2a_short_plt_be
2002 /* Little-endian PIC. */
2005 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2006 fdpic_sh_plt_entry_le,
2007 FDPIC_PLT_ENTRY_SIZE,
2008 { 12, MINUS_ONE, 16, FALSE },
2009 FDPIC_PLT_LAZY_OFFSET,
2010 &fdpic_sh2a_short_plt_le
2014 /* Return the type of PLT associated with ABFD. PIC_P is true if
2015 the object is position-independent. */
2017 static const struct elf_sh_plt_info *
2018 get_plt_info (bfd *abfd, bfd_boolean pic_p)
2020 if (fdpic_object_p (abfd))
2022 /* If any input file requires SH2A we can use a shorter PLT
2024 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd)) & arch_sh2a_base)
2025 return &fdpic_sh2a_plts[!bfd_big_endian (abfd)];
2027 return &fdpic_sh_plts[!bfd_big_endian (abfd)];
2029 if (vxworks_object_p (abfd))
2030 return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2031 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2034 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2035 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2039 install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
2040 unsigned long value, bfd_byte *addr)
2042 bfd_put_32 (output_bfd, value, addr);
2045 /* The number of PLT entries which can use a shorter PLT, if any.
2046 Currently always 64K, since only SH-2A FDPIC uses this; a
2047 20-bit movi20 can address that many function descriptors below
2048 _GLOBAL_OFFSET_TABLE_. */
2049 #define MAX_SHORT_PLT 65536
2051 /* Return the index of the PLT entry at byte offset OFFSET. */
2054 get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2056 bfd_vma plt_index = 0;
2058 offset -= info->plt0_entry_size;
2059 if (info->short_plt != NULL)
2061 if (offset > MAX_SHORT_PLT * info->short_plt->symbol_entry_size)
2063 plt_index = MAX_SHORT_PLT;
2064 offset -= MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2067 info = info->short_plt;
2069 return plt_index + offset / info->symbol_entry_size;
2072 /* Do the inverse operation. */
2075 get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma plt_index)
2079 if (info->short_plt != NULL)
2081 if (plt_index > MAX_SHORT_PLT)
2083 offset = MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2084 plt_index -= MAX_SHORT_PLT;
2087 info = info->short_plt;
2089 return (offset + info->plt0_entry_size
2090 + (plt_index * info->symbol_entry_size));
2095 bfd_signed_vma refcount;
2099 /* sh ELF linker hash entry. */
2101 struct elf_sh_link_hash_entry
2103 struct elf_link_hash_entry root;
2105 /* Track dynamic relocs copied for this symbol. */
2106 struct elf_dyn_relocs *dyn_relocs;
2108 bfd_signed_vma gotplt_refcount;
2110 /* A local function descriptor, for FDPIC. The refcount counts
2111 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2112 relocations; the PLT and GOT entry are accounted
2113 for separately. After adjust_dynamic_symbol, the offset is
2114 MINUS_ONE if there is no local descriptor (dynamic linker
2115 managed and no PLT entry, or undefined weak non-dynamic).
2116 During check_relocs we do not yet know whether the local
2117 descriptor will be canonical. */
2118 union gotref funcdesc;
2120 /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2121 and thus require fixups or relocations. */
2122 bfd_signed_vma abs_funcdesc_refcount;
2125 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE, GOT_FUNCDESC
2129 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2131 struct sh_elf_obj_tdata
2133 struct elf_obj_tdata root;
2135 /* got_type for each local got entry. */
2136 char *local_got_type;
2138 /* Function descriptor refcount and offset for each local symbol. */
2139 union gotref *local_funcdesc;
2142 #define sh_elf_tdata(abfd) \
2143 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2145 #define sh_elf_local_got_type(abfd) \
2146 (sh_elf_tdata (abfd)->local_got_type)
2148 #define sh_elf_local_funcdesc(abfd) \
2149 (sh_elf_tdata (abfd)->local_funcdesc)
2151 #define is_sh_elf(bfd) \
2152 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2153 && elf_tdata (bfd) != NULL \
2154 && elf_object_id (bfd) == SH_ELF_DATA)
2156 /* Override the generic function because we need to store sh_elf_obj_tdata
2157 as the specific tdata. */
2160 sh_elf_mkobject (bfd *abfd)
2162 return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata),
2166 /* sh ELF linker hash table. */
2168 struct elf_sh_link_hash_table
2170 struct elf_link_hash_table root;
2172 /* Short-cuts to get to dynamic linker sections. */
2175 asection *sfuncdesc;
2176 asection *srelfuncdesc;
2179 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2182 /* Small local sym cache. */
2183 struct sym_cache sym_cache;
2185 /* A counter or offset to track a TLS got entry. */
2188 bfd_signed_vma refcount;
2192 /* The type of PLT to use. */
2193 const struct elf_sh_plt_info *plt_info;
2195 /* True if the target system is VxWorks. */
2196 bfd_boolean vxworks_p;
2198 /* True if the target system uses FDPIC. */
2199 bfd_boolean fdpic_p;
2202 /* Traverse an sh ELF linker hash table. */
2204 #define sh_elf_link_hash_traverse(table, func, info) \
2205 (elf_link_hash_traverse \
2207 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2210 /* Get the sh ELF linker hash table from a link_info structure. */
2212 #define sh_elf_hash_table(p) \
2213 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2214 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2216 /* Create an entry in an sh ELF linker hash table. */
2218 static struct bfd_hash_entry *
2219 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2220 struct bfd_hash_table *table,
2223 struct elf_sh_link_hash_entry *ret =
2224 (struct elf_sh_link_hash_entry *) entry;
2226 /* Allocate the structure if it has not already been allocated by a
2228 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2229 ret = ((struct elf_sh_link_hash_entry *)
2230 bfd_hash_allocate (table,
2231 sizeof (struct elf_sh_link_hash_entry)));
2232 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2233 return (struct bfd_hash_entry *) ret;
2235 /* Call the allocation method of the superclass. */
2236 ret = ((struct elf_sh_link_hash_entry *)
2237 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2239 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2241 ret->dyn_relocs = NULL;
2242 ret->gotplt_refcount = 0;
2243 ret->funcdesc.refcount = 0;
2244 ret->abs_funcdesc_refcount = 0;
2245 ret->got_type = GOT_UNKNOWN;
2248 return (struct bfd_hash_entry *) ret;
2251 /* Create an sh ELF linker hash table. */
2253 static struct bfd_link_hash_table *
2254 sh_elf_link_hash_table_create (bfd *abfd)
2256 struct elf_sh_link_hash_table *ret;
2257 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
2259 ret = (struct elf_sh_link_hash_table *) bfd_zmalloc (amt);
2260 if (ret == (struct elf_sh_link_hash_table *) NULL)
2263 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2264 sh_elf_link_hash_newfunc,
2265 sizeof (struct elf_sh_link_hash_entry),
2272 ret->vxworks_p = vxworks_object_p (abfd);
2273 ret->fdpic_p = fdpic_object_p (abfd);
2275 return &ret->root.root;
2279 sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2280 struct bfd_link_info *info, asection *p)
2282 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
2284 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2288 /* We need dynamic symbols for every section, since segments can
2289 relocate independently. */
2290 switch (elf_section_data (p)->this_hdr.sh_type)
2294 /* If sh_type is yet undecided, assume it could be
2295 SHT_PROGBITS/SHT_NOBITS. */
2299 /* There shouldn't be section relative relocations
2300 against any other section. */
2306 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2307 shortcuts to them in our hash table. */
2310 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2312 struct elf_sh_link_hash_table *htab;
2314 if (! _bfd_elf_create_got_section (dynobj, info))
2317 htab = sh_elf_hash_table (info);
2321 htab->sfuncdesc = bfd_make_section_anyway_with_flags (dynobj, ".got.funcdesc",
2322 (SEC_ALLOC | SEC_LOAD
2325 | SEC_LINKER_CREATED));
2326 if (htab->sfuncdesc == NULL
2327 || ! bfd_set_section_alignment (dynobj, htab->sfuncdesc, 2))
2330 htab->srelfuncdesc = bfd_make_section_anyway_with_flags (dynobj,
2331 ".rela.got.funcdesc",
2332 (SEC_ALLOC | SEC_LOAD
2335 | SEC_LINKER_CREATED
2337 if (htab->srelfuncdesc == NULL
2338 || ! bfd_set_section_alignment (dynobj, htab->srelfuncdesc, 2))
2341 /* Also create .rofixup. */
2342 htab->srofixup = bfd_make_section_anyway_with_flags (dynobj, ".rofixup",
2343 (SEC_ALLOC | SEC_LOAD
2346 | SEC_LINKER_CREATED
2348 if (htab->srofixup == NULL
2349 || ! bfd_set_section_alignment (dynobj, htab->srofixup, 2))
2355 /* Create dynamic sections when linking against a dynamic object. */
2358 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2360 struct elf_sh_link_hash_table *htab;
2361 flagword flags, pltflags;
2363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2366 switch (bed->s->arch_size)
2377 bfd_set_error (bfd_error_bad_value);
2381 htab = sh_elf_hash_table (info);
2385 if (htab->root.dynamic_sections_created)
2388 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2389 .rel[a].bss sections. */
2391 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2392 | SEC_LINKER_CREATED);
2395 pltflags |= SEC_CODE;
2396 if (bed->plt_not_loaded)
2397 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2398 if (bed->plt_readonly)
2399 pltflags |= SEC_READONLY;
2401 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
2402 htab->root.splt = s;
2404 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2407 if (bed->want_plt_sym)
2409 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2411 struct elf_link_hash_entry *h;
2412 struct bfd_link_hash_entry *bh = NULL;
2414 if (! (_bfd_generic_link_add_one_symbol
2415 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2416 (bfd_vma) 0, (const char *) NULL, FALSE,
2417 get_elf_backend_data (abfd)->collect, &bh)))
2420 h = (struct elf_link_hash_entry *) bh;
2422 h->type = STT_OBJECT;
2423 htab->root.hplt = h;
2425 if (bfd_link_pic (info)
2426 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2430 s = bfd_make_section_anyway_with_flags (abfd,
2431 bed->default_use_rela_p
2432 ? ".rela.plt" : ".rel.plt",
2433 flags | SEC_READONLY);
2434 htab->root.srelplt = s;
2436 || ! bfd_set_section_alignment (abfd, s, ptralign))
2439 if (htab->root.sgot == NULL
2440 && !create_got_section (abfd, info))
2443 if (bed->want_dynbss)
2445 /* The .dynbss section is a place to put symbols which are defined
2446 by dynamic objects, are referenced by regular objects, and are
2447 not functions. We must allocate space for them in the process
2448 image and use a R_*_COPY reloc to tell the dynamic linker to
2449 initialize them at run time. The linker script puts the .dynbss
2450 section into the .bss section of the final image. */
2451 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2452 SEC_ALLOC | SEC_LINKER_CREATED);
2457 /* The .rel[a].bss section holds copy relocs. This section is not
2458 normally needed. We need to create it here, though, so that the
2459 linker will map it to an output section. We can't just create it
2460 only if we need it, because we will not know whether we need it
2461 until we have seen all the input files, and the first time the
2462 main linker code calls BFD after examining all the input files
2463 (size_dynamic_sections) the input sections have already been
2464 mapped to the output sections. If the section turns out not to
2465 be needed, we can discard it later. We will never need this
2466 section when generating a shared object, since they do not use
2468 if (! bfd_link_pic (info))
2470 s = bfd_make_section_anyway_with_flags (abfd,
2471 (bed->default_use_rela_p
2472 ? ".rela.bss" : ".rel.bss"),
2473 flags | SEC_READONLY);
2476 || ! bfd_set_section_alignment (abfd, s, ptralign))
2481 if (htab->vxworks_p)
2483 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2490 /* Find dynamic relocs for H that apply to read-only sections. */
2493 readonly_dynrelocs (struct elf_link_hash_entry *h)
2495 struct elf_dyn_relocs *p;
2497 for (p = sh_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2499 asection *s = p->sec->output_section;
2501 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2507 /* Adjust a symbol defined by a dynamic object and referenced by a
2508 regular object. The current definition is in some section of the
2509 dynamic object, but we're not including those sections. We have to
2510 change the definition to something the rest of the link can
2514 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2515 struct elf_link_hash_entry *h)
2517 struct elf_sh_link_hash_table *htab;
2520 htab = sh_elf_hash_table (info);
2524 /* Make sure we know what is going on here. */
2525 BFD_ASSERT (htab->root.dynobj != NULL
2530 && !h->def_regular)));
2532 /* If this is a function, put it in the procedure linkage table. We
2533 will fill in the contents of the procedure linkage table later,
2534 when we know the address of the .got section. */
2535 if (h->type == STT_FUNC
2538 if (h->plt.refcount <= 0
2539 || SYMBOL_CALLS_LOCAL (info, h)
2540 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2541 && h->root.type == bfd_link_hash_undefweak))
2543 /* This case can occur if we saw a PLT reloc in an input
2544 file, but the symbol was never referred to by a dynamic
2545 object. In such a case, we don't actually need to build
2546 a procedure linkage table, and we can just do a REL32
2548 h->plt.offset = (bfd_vma) -1;
2555 h->plt.offset = (bfd_vma) -1;
2557 /* If this is a weak symbol, and there is a real definition, the
2558 processor independent code will have arranged for us to see the
2559 real definition first, and we can just use the same value. */
2560 if (h->is_weakalias)
2562 struct elf_link_hash_entry *def = weakdef (h);
2563 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2564 h->root.u.def.section = def->root.u.def.section;
2565 h->root.u.def.value = def->root.u.def.value;
2566 if (info->nocopyreloc)
2567 h->non_got_ref = def->non_got_ref;
2571 /* This is a reference to a symbol defined by a dynamic object which
2572 is not a function. */
2574 /* If we are creating a shared library, we must presume that the
2575 only references to the symbol are via the global offset table.
2576 For such cases we need not do anything here; the relocations will
2577 be handled correctly by relocate_section. */
2578 if (bfd_link_pic (info))
2581 /* If there are no references to this symbol that do not use the
2582 GOT, we don't need to generate a copy reloc. */
2583 if (!h->non_got_ref)
2586 /* If -z nocopyreloc was given, we won't generate them either. */
2587 if (0 && info->nocopyreloc)
2593 /* If we don't find any dynamic relocs in read-only sections, then
2594 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2595 if (0 && !readonly_dynrelocs (h))
2601 /* We must allocate the symbol in our .dynbss section, which will
2602 become part of the .bss section of the executable. There will be
2603 an entry for this symbol in the .dynsym section. The dynamic
2604 object will contain position independent code, so all references
2605 from the dynamic object to this symbol will go through the global
2606 offset table. The dynamic linker will use the .dynsym entry to
2607 determine the address it must put in the global offset table, so
2608 both the dynamic object and the regular object will refer to the
2609 same memory location for the variable. */
2612 BFD_ASSERT (s != NULL);
2614 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2615 copy the initial value out of the dynamic object and into the
2616 runtime process image. We need to remember the offset into the
2617 .rela.bss section we are going to use. */
2618 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2622 srel = htab->srelbss;
2623 BFD_ASSERT (srel != NULL);
2624 srel->size += sizeof (Elf32_External_Rela);
2628 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2631 /* Allocate space in .plt, .got and associated reloc sections for
2635 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2637 struct bfd_link_info *info;
2638 struct elf_sh_link_hash_table *htab;
2639 struct elf_sh_link_hash_entry *eh;
2640 struct elf_dyn_relocs *p;
2642 if (h->root.type == bfd_link_hash_indirect)
2645 info = (struct bfd_link_info *) inf;
2646 htab = sh_elf_hash_table (info);
2650 eh = (struct elf_sh_link_hash_entry *) h;
2651 if ((h->got.refcount > 0
2653 && eh->gotplt_refcount > 0)
2655 /* The symbol has been forced local, or we have some direct got refs,
2656 so treat all the gotplt refs as got refs. */
2657 h->got.refcount += eh->gotplt_refcount;
2658 if (h->plt.refcount >= eh->gotplt_refcount)
2659 h->plt.refcount -= eh->gotplt_refcount;
2662 if (htab->root.dynamic_sections_created
2663 && h->plt.refcount > 0
2664 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2665 || h->root.type != bfd_link_hash_undefweak))
2667 /* Make sure this symbol is output as a dynamic symbol.
2668 Undefined weak syms won't yet be marked as dynamic. */
2669 if (h->dynindx == -1
2670 && !h->forced_local)
2672 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2676 if (bfd_link_pic (info)
2677 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2679 asection *s = htab->root.splt;
2680 const struct elf_sh_plt_info *plt_info;
2682 /* If this is the first .plt entry, make room for the special
2685 s->size += htab->plt_info->plt0_entry_size;
2687 h->plt.offset = s->size;
2689 /* If this symbol is not defined in a regular file, and we are
2690 not generating a shared library, then set the symbol to this
2691 location in the .plt. This is required to make function
2692 pointers compare as equal between the normal executable and
2693 the shared library. Skip this for FDPIC, since the
2694 function's address will be the address of the canonical
2695 function descriptor. */
2696 if (!htab->fdpic_p && !bfd_link_pic (info) && !h->def_regular)
2698 h->root.u.def.section = s;
2699 h->root.u.def.value = h->plt.offset;
2702 /* Make room for this entry. */
2703 plt_info = htab->plt_info;
2704 if (plt_info->short_plt != NULL
2705 && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT))
2706 plt_info = plt_info->short_plt;
2707 s->size += plt_info->symbol_entry_size;
2709 /* We also need to make an entry in the .got.plt section, which
2710 will be placed in the .got section by the linker script. */
2712 htab->root.sgotplt->size += 4;
2714 htab->root.sgotplt->size += 8;
2716 /* We also need to make an entry in the .rel.plt section. */
2717 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
2719 if (htab->vxworks_p && !bfd_link_pic (info))
2721 /* VxWorks executables have a second set of relocations
2722 for each PLT entry. They go in a separate relocation
2723 section, which is processed by the kernel loader. */
2725 /* There is a relocation for the initial PLT entry:
2726 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2727 if (h->plt.offset == htab->plt_info->plt0_entry_size)
2728 htab->srelplt2->size += sizeof (Elf32_External_Rela);
2730 /* There are two extra relocations for each subsequent
2731 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2732 and an R_SH_DIR32 relocation for the PLT entry. */
2733 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
2738 h->plt.offset = (bfd_vma) -1;
2744 h->plt.offset = (bfd_vma) -1;
2748 if (h->got.refcount > 0)
2752 enum got_type got_type = sh_elf_hash_entry (h)->got_type;
2754 /* Make sure this symbol is output as a dynamic symbol.
2755 Undefined weak syms won't yet be marked as dynamic. */
2756 if (h->dynindx == -1
2757 && !h->forced_local)
2759 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2763 s = htab->root.sgot;
2764 h->got.offset = s->size;
2766 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2767 if (got_type == GOT_TLS_GD)
2769 dyn = htab->root.dynamic_sections_created;
2772 /* No dynamic relocations required. */
2773 if (htab->fdpic_p && !bfd_link_pic (info)
2774 && h->root.type != bfd_link_hash_undefweak
2775 && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC))
2776 htab->srofixup->size += 4;
2778 /* No dynamic relocations required when IE->LE conversion happens. */
2779 else if (got_type == GOT_TLS_IE
2781 && !bfd_link_pic (info))
2783 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
2784 R_SH_TLS_GD needs one if local symbol and two if global. */
2785 else if ((got_type == GOT_TLS_GD && h->dynindx == -1)
2786 || got_type == GOT_TLS_IE)
2787 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
2788 else if (got_type == GOT_TLS_GD)
2789 htab->root.srelgot->size += 2 * sizeof (Elf32_External_Rela);
2790 else if (got_type == GOT_FUNCDESC)
2792 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
2793 htab->srofixup->size += 4;
2795 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
2797 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2798 || h->root.type != bfd_link_hash_undefweak)
2799 && (bfd_link_pic (info)
2800 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2801 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
2802 else if (htab->fdpic_p
2803 && !bfd_link_pic (info)
2804 && got_type == GOT_NORMAL
2805 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2806 || h->root.type != bfd_link_hash_undefweak))
2807 htab->srofixup->size += 4;
2810 h->got.offset = (bfd_vma) -1;
2812 /* Allocate space for any dynamic relocations to function
2813 descriptors, canonical or otherwise. We need to relocate the
2814 reference unless it resolves to zero, which only happens for
2815 undefined weak symbols (either non-default visibility, or when
2816 static linking). Any GOT slot is accounted for elsewhere. */
2817 if (eh->abs_funcdesc_refcount > 0
2818 && (h->root.type != bfd_link_hash_undefweak
2819 || (htab->root.dynamic_sections_created
2820 && ! SYMBOL_CALLS_LOCAL (info, h))))
2822 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
2823 htab->srofixup->size += eh->abs_funcdesc_refcount * 4;
2825 htab->root.srelgot->size
2826 += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela);
2829 /* We must allocate a function descriptor if there are references to
2830 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
2831 the dynamic linker isn't going to allocate it. None of this
2832 applies if we already created one in .got.plt, but if the
2833 canonical function descriptor can be in this object, there
2834 won't be a PLT entry at all. */
2835 if ((eh->funcdesc.refcount > 0
2836 || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC))
2837 && h->root.type != bfd_link_hash_undefweak
2838 && SYMBOL_FUNCDESC_LOCAL (info, h))
2840 /* Make room for this function descriptor. */
2841 eh->funcdesc.offset = htab->sfuncdesc->size;
2842 htab->sfuncdesc->size += 8;
2844 /* We will need a relocation or two fixups to initialize the
2845 function descriptor, so allocate those too. */
2846 if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
2847 htab->srofixup->size += 8;
2849 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
2852 if (eh->dyn_relocs == NULL)
2855 /* In the shared -Bsymbolic case, discard space allocated for
2856 dynamic pc-relative relocs against symbols which turn out to be
2857 defined in regular objects. For the normal shared case, discard
2858 space for pc-relative relocs that have become local due to symbol
2859 visibility changes. */
2861 if (bfd_link_pic (info))
2863 if (SYMBOL_CALLS_LOCAL (info, h))
2865 struct elf_dyn_relocs **pp;
2867 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2869 p->count -= p->pc_count;
2878 if (htab->vxworks_p)
2880 struct elf_dyn_relocs **pp;
2882 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2884 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2891 /* Also discard relocs on undefined weak syms with non-default
2893 if (eh->dyn_relocs != NULL
2894 && h->root.type == bfd_link_hash_undefweak)
2896 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2897 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
2898 eh->dyn_relocs = NULL;
2900 /* Make sure undefined weak symbols are output as a dynamic
2902 else if (h->dynindx == -1
2903 && !h->forced_local)
2905 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2912 /* For the non-shared case, discard space for relocs against
2913 symbols which turn out to need copy relocs or are not
2919 || (htab->root.dynamic_sections_created
2920 && (h->root.type == bfd_link_hash_undefweak
2921 || h->root.type == bfd_link_hash_undefined))))
2923 /* Make sure this symbol is output as a dynamic symbol.
2924 Undefined weak syms won't yet be marked as dynamic. */
2925 if (h->dynindx == -1
2926 && !h->forced_local)
2928 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2932 /* If that succeeded, we know we'll be keeping all the
2934 if (h->dynindx != -1)
2938 eh->dyn_relocs = NULL;
2943 /* Finally, allocate space. */
2944 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2946 asection *sreloc = elf_section_data (p->sec)->sreloc;
2947 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2949 /* If we need relocations, we do not need fixups. */
2950 if (htab->fdpic_p && !bfd_link_pic (info))
2951 htab->srofixup->size -= 4 * (p->count - p->pc_count);
2957 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2958 read-only sections. */
2961 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
2965 if (h->root.type == bfd_link_hash_indirect)
2968 sec = readonly_dynrelocs (h);
2971 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
2973 info->flags |= DF_TEXTREL;
2974 info->callbacks->minfo
2975 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
2976 sec->owner, h->root.root.string, sec);
2978 /* Not an error, just cut short the traversal. */
2984 /* This function is called after all the input files have been read,
2985 and the input sections have been assigned to output sections.
2986 It's a convenient place to determine the PLT style. */
2989 sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
2991 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd,
2992 bfd_link_pic (info));
2994 if (sh_elf_hash_table (info)->fdpic_p && !bfd_link_relocatable (info)
2995 && !bfd_elf_stack_segment_size (output_bfd, info,
2996 "__stacksize", DEFAULT_STACK_SIZE))
3001 /* Set the sizes of the dynamic sections. */
3004 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3005 struct bfd_link_info *info)
3007 struct elf_sh_link_hash_table *htab;
3013 htab = sh_elf_hash_table (info);
3017 dynobj = htab->root.dynobj;
3018 BFD_ASSERT (dynobj != NULL);
3020 if (htab->root.dynamic_sections_created)
3022 /* Set the contents of the .interp section to the interpreter. */
3023 if (bfd_link_executable (info) && !info->nointerp)
3025 s = bfd_get_linker_section (dynobj, ".interp");
3026 BFD_ASSERT (s != NULL);
3027 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3028 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3032 /* Set up .got offsets for local syms, and space for local dynamic
3034 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3036 bfd_signed_vma *local_got;
3037 bfd_signed_vma *end_local_got;
3038 union gotref *local_funcdesc, *end_local_funcdesc;
3039 char *local_got_type;
3040 bfd_size_type locsymcount;
3041 Elf_Internal_Shdr *symtab_hdr;
3044 if (! is_sh_elf (ibfd))
3047 for (s = ibfd->sections; s != NULL; s = s->next)
3049 struct elf_dyn_relocs *p;
3051 for (p = ((struct elf_dyn_relocs *)
3052 elf_section_data (s)->local_dynrel);
3056 if (! bfd_is_abs_section (p->sec)
3057 && bfd_is_abs_section (p->sec->output_section))
3059 /* Input section has been discarded, either because
3060 it is a copy of a linkonce section or due to
3061 linker script /DISCARD/, so we'll be discarding
3064 else if (htab->vxworks_p
3065 && strcmp (p->sec->output_section->name,
3068 /* Relocations in vxworks .tls_vars sections are
3069 handled specially by the loader. */
3071 else if (p->count != 0)
3073 srel = elf_section_data (p->sec)->sreloc;
3074 srel->size += p->count * sizeof (Elf32_External_Rela);
3075 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3077 info->flags |= DF_TEXTREL;
3078 info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
3079 p->sec->owner, p->sec);
3082 /* If we need relocations, we do not need fixups. */
3083 if (htab->fdpic_p && !bfd_link_pic (info))
3084 htab->srofixup->size -= 4 * (p->count - p->pc_count);
3089 symtab_hdr = &elf_symtab_hdr (ibfd);
3090 locsymcount = symtab_hdr->sh_info;
3091 s = htab->root.sgot;
3092 srel = htab->root.srelgot;
3094 local_got = elf_local_got_refcounts (ibfd);
3097 end_local_got = local_got + locsymcount;
3098 local_got_type = sh_elf_local_got_type (ibfd);
3099 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3100 for (; local_got < end_local_got; ++local_got)
3104 *local_got = s->size;
3106 if (*local_got_type == GOT_TLS_GD)
3108 if (bfd_link_pic (info))
3109 srel->size += sizeof (Elf32_External_Rela);
3111 htab->srofixup->size += 4;
3113 if (*local_got_type == GOT_FUNCDESC)
3115 if (local_funcdesc == NULL)
3119 size = locsymcount * sizeof (union gotref);
3120 local_funcdesc = (union gotref *) bfd_zalloc (ibfd,
3122 if (local_funcdesc == NULL)
3124 sh_elf_local_funcdesc (ibfd) = local_funcdesc;
3125 local_funcdesc += (local_got
3126 - elf_local_got_refcounts (ibfd));
3128 local_funcdesc->refcount++;
3133 *local_got = (bfd_vma) -1;
3138 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3141 end_local_funcdesc = local_funcdesc + locsymcount;
3143 for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc)
3145 if (local_funcdesc->refcount > 0)
3147 local_funcdesc->offset = htab->sfuncdesc->size;
3148 htab->sfuncdesc->size += 8;
3149 if (!bfd_link_pic (info))
3150 htab->srofixup->size += 8;
3152 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3155 local_funcdesc->offset = MINUS_ONE;
3161 if (htab->tls_ldm_got.refcount > 0)
3163 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3165 htab->tls_ldm_got.offset = htab->root.sgot->size;
3166 htab->root.sgot->size += 8;
3167 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3170 htab->tls_ldm_got.offset = -1;
3172 /* Only the reserved entries should be present. For FDPIC, they go at
3173 the end of .got.plt. */
3176 BFD_ASSERT (htab->root.sgotplt && htab->root.sgotplt->size == 12);
3177 htab->root.sgotplt->size = 0;
3180 /* Allocate global sym .plt and .got entries, and space for global
3181 sym dynamic relocs. */
3182 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
3184 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3185 end of the FDPIC .got.plt. */
3188 htab->root.hgot->root.u.def.value = htab->root.sgotplt->size;
3189 htab->root.sgotplt->size += 12;
3192 /* At the very end of the .rofixup section is a pointer to the GOT. */
3193 if (htab->fdpic_p && htab->srofixup != NULL)
3194 htab->srofixup->size += 4;
3196 /* We now have determined the sizes of the various dynamic sections.
3197 Allocate memory for them. */
3199 for (s = dynobj->sections; s != NULL; s = s->next)
3201 if ((s->flags & SEC_LINKER_CREATED) == 0)
3204 if (s == htab->root.splt
3205 || s == htab->root.sgot
3206 || s == htab->root.sgotplt
3207 || s == htab->sfuncdesc
3208 || s == htab->srofixup
3209 || s == htab->sdynbss)
3211 /* Strip this section if we don't need it; see the
3214 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3216 if (s->size != 0 && s != htab->root.srelplt && s != htab->srelplt2)
3219 /* We use the reloc_count field as a counter if we need
3220 to copy relocs into the output file. */
3225 /* It's not one of our sections, so don't allocate space. */
3231 /* If we don't need this section, strip it from the
3232 output file. This is mostly to handle .rela.bss and
3233 .rela.plt. We must create both sections in
3234 create_dynamic_sections, because they must be created
3235 before the linker maps input sections to output
3236 sections. The linker does that before
3237 adjust_dynamic_symbol is called, and it is that
3238 function which decides whether anything needs to go
3239 into these sections. */
3241 s->flags |= SEC_EXCLUDE;
3245 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3248 /* Allocate memory for the section contents. We use bfd_zalloc
3249 here in case unused entries are not reclaimed before the
3250 section's contents are written out. This should not happen,
3251 but this way if it does, we get a R_SH_NONE reloc instead
3253 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3254 if (s->contents == NULL)
3258 if (htab->root.dynamic_sections_created)
3260 /* Add some entries to the .dynamic section. We fill in the
3261 values later, in sh_elf_finish_dynamic_sections, but we
3262 must add the entries now so that we get the correct size for
3263 the .dynamic section. The DT_DEBUG entry is filled in by the
3264 dynamic linker and used by the debugger. */
3265 #define add_dynamic_entry(TAG, VAL) \
3266 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3268 if (bfd_link_executable (info))
3270 if (! add_dynamic_entry (DT_DEBUG, 0))
3274 if (htab->root.splt->size != 0)
3276 if (! add_dynamic_entry (DT_PLTGOT, 0)
3277 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
3278 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
3279 || ! add_dynamic_entry (DT_JMPREL, 0))
3282 else if ((elf_elfheader (output_bfd)->e_flags & EF_SH_FDPIC))
3284 if (! add_dynamic_entry (DT_PLTGOT, 0))
3290 if (! add_dynamic_entry (DT_RELA, 0)
3291 || ! add_dynamic_entry (DT_RELASZ, 0)
3292 || ! add_dynamic_entry (DT_RELAENT,
3293 sizeof (Elf32_External_Rela)))
3296 /* If any dynamic relocs apply to a read-only section,
3297 then we need a DT_TEXTREL entry. */
3298 if ((info->flags & DF_TEXTREL) == 0)
3299 elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
3301 if ((info->flags & DF_TEXTREL) != 0)
3303 if (! add_dynamic_entry (DT_TEXTREL, 0))
3308 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3311 #undef add_dynamic_entry
3316 /* Add a dynamic relocation to the SRELOC section. */
3318 inline static bfd_vma
3319 sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
3320 int reloc_type, long dynindx, bfd_vma addend)
3322 Elf_Internal_Rela outrel;
3323 bfd_vma reloc_offset;
3325 outrel.r_offset = offset;
3326 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
3327 outrel.r_addend = addend;
3329 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela);
3330 BFD_ASSERT (reloc_offset < sreloc->size);
3331 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3332 sreloc->contents + reloc_offset);
3333 sreloc->reloc_count++;
3335 return reloc_offset;
3338 /* Add an FDPIC read-only fixup. */
3341 sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3343 bfd_vma fixup_offset;
3345 fixup_offset = srofixup->reloc_count++ * 4;
3346 BFD_ASSERT (fixup_offset < srofixup->size);
3347 bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3350 /* Return the offset of the generated .got section from the
3351 _GLOBAL_OFFSET_TABLE_ symbol. */
3353 static bfd_signed_vma
3354 sh_elf_got_offset (struct elf_sh_link_hash_table *htab)
3356 return (htab->root.sgot->output_offset - htab->root.sgotplt->output_offset
3357 - htab->root.hgot->root.u.def.value);
3360 /* Find the segment number in which OSEC, and output section, is
3364 sh_elf_osec_to_segment (bfd *output_bfd, asection *osec)
3366 Elf_Internal_Phdr *p = NULL;
3368 if (output_bfd->xvec->flavour == bfd_target_elf_flavour
3369 /* PR ld/17110: Do not look for output segments in an input bfd. */
3370 && output_bfd->direction != read_direction)
3371 p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
3373 /* FIXME: Nothing ever says what this index is relative to. The kernel
3374 supplies data in terms of the number of load segments but this is
3375 a phdr index and the first phdr may not be a load segment. */
3376 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
3380 sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec)
3382 unsigned seg = sh_elf_osec_to_segment (output_bfd, osec);
3384 return (seg != (unsigned) -1
3385 && ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W));
3388 /* Generate the initial contents of a local function descriptor, along
3389 with any relocations or fixups required. */
3391 sh_elf_initialize_funcdesc (bfd *output_bfd,
3392 struct bfd_link_info *info,
3393 struct elf_link_hash_entry *h,
3398 struct elf_sh_link_hash_table *htab;
3402 htab = sh_elf_hash_table (info);
3404 /* FIXME: The ABI says that the offset to the function goes in the
3405 descriptor, along with the segment index. We're RELA, so it could
3406 go in the reloc instead... */
3408 if (h != NULL && SYMBOL_CALLS_LOCAL (info, h))
3410 section = h->root.u.def.section;
3411 value = h->root.u.def.value;
3414 if (h == NULL || SYMBOL_CALLS_LOCAL (info, h))
3416 dynindx = elf_section_data (section->output_section)->dynindx;
3417 addr = value + section->output_offset;
3418 seg = sh_elf_osec_to_segment (output_bfd, section->output_section);
3422 BFD_ASSERT (h->dynindx != -1);
3423 dynindx = h->dynindx;
3427 if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
3429 if (h == NULL || h->root.type != bfd_link_hash_undefweak)
3431 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3433 + htab->sfuncdesc->output_section->vma
3434 + htab->sfuncdesc->output_offset);
3435 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3437 + htab->sfuncdesc->output_section->vma
3438 + htab->sfuncdesc->output_offset);
3441 /* There are no dynamic relocations so fill in the final
3442 address and gp value (barring fixups). */
3443 addr += section->output_section->vma;
3444 seg = htab->root.hgot->root.u.def.value
3445 + htab->root.hgot->root.u.def.section->output_section->vma
3446 + htab->root.hgot->root.u.def.section->output_offset;
3449 sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc,
3451 + htab->sfuncdesc->output_section->vma
3452 + htab->sfuncdesc->output_offset,
3453 R_SH_FUNCDESC_VALUE, dynindx, 0);
3455 bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset);
3456 bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4);
3461 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3462 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3465 static bfd_reloc_status_type
3466 install_movi20_field (bfd *output_bfd, unsigned long relocation,
3467 bfd *input_bfd, asection *input_section,
3468 bfd_byte *contents, bfd_vma offset)
3470 unsigned long cur_val;
3472 bfd_reloc_status_type r;
3474 if (offset > bfd_get_section_limit (input_bfd, input_section))
3475 return bfd_reloc_outofrange;
3477 r = bfd_check_overflow (complain_overflow_signed, 20, 0,
3478 bfd_arch_bits_per_address (input_bfd), relocation);
3479 if (r != bfd_reloc_ok)
3482 addr = contents + offset;
3483 cur_val = bfd_get_16 (output_bfd, addr);
3484 bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr);
3485 bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2);
3487 return bfd_reloc_ok;
3490 /* Relocate an SH ELF section. */
3493 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3494 bfd *input_bfd, asection *input_section,
3495 bfd_byte *contents, Elf_Internal_Rela *relocs,
3496 Elf_Internal_Sym *local_syms,
3497 asection **local_sections)
3499 struct elf_sh_link_hash_table *htab;
3500 Elf_Internal_Shdr *symtab_hdr;
3501 struct elf_link_hash_entry **sym_hashes;
3502 Elf_Internal_Rela *rel, *relend;
3503 bfd_vma *local_got_offsets;
3504 asection *sgot = NULL;
3505 asection *sgotplt = NULL;
3506 asection *splt = NULL;
3507 asection *sreloc = NULL;
3508 asection *srelgot = NULL;
3509 bfd_boolean is_vxworks_tls;
3510 unsigned isec_segment, got_segment, plt_segment, check_segment[2];
3511 bfd_boolean fdpic_p = FALSE;
3513 BFD_ASSERT (is_sh_elf (input_bfd));
3515 htab = sh_elf_hash_table (info);
3518 sgot = htab->root.sgot;
3519 sgotplt = htab->root.sgotplt;
3520 srelgot = htab->root.srelgot;
3521 splt = htab->root.splt;
3522 fdpic_p = htab->fdpic_p;
3524 symtab_hdr = &elf_symtab_hdr (input_bfd);
3525 sym_hashes = elf_sym_hashes (input_bfd);
3526 local_got_offsets = elf_local_got_offsets (input_bfd);
3528 isec_segment = sh_elf_osec_to_segment (output_bfd,
3529 input_section->output_section);
3530 if (fdpic_p && sgot)
3531 got_segment = sh_elf_osec_to_segment (output_bfd,
3532 sgot->output_section);
3535 if (fdpic_p && splt)
3536 plt_segment = sh_elf_osec_to_segment (output_bfd,
3537 splt->output_section);
3541 /* We have to handle relocations in vxworks .tls_vars sections
3542 specially, because the dynamic loader is 'weird'. */
3543 is_vxworks_tls = (htab && htab->vxworks_p && bfd_link_pic (info)
3544 && !strcmp (input_section->output_section->name,
3548 relend = relocs + input_section->reloc_count;
3549 for (; rel < relend; rel++)
3552 reloc_howto_type *howto;
3553 unsigned long r_symndx;
3554 Elf_Internal_Sym *sym;
3556 struct elf_link_hash_entry *h;
3558 bfd_vma addend = (bfd_vma) 0;
3559 bfd_reloc_status_type r;
3560 int seen_stt_datalabel = 0;
3562 enum got_type got_type;
3563 const char *symname = NULL;
3564 bfd_boolean resolved_to_zero;
3566 r_symndx = ELF32_R_SYM (rel->r_info);
3568 r_type = ELF32_R_TYPE (rel->r_info);
3570 /* Many of the relocs are only used for relaxing, and are
3571 handled entirely by the relaxation code. */
3572 if (r_type >= (int) R_SH_GNU_VTINHERIT
3573 && r_type <= (int) R_SH_LABEL)
3575 if (r_type == (int) R_SH_NONE)
3579 || r_type >= R_SH_max
3580 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
3581 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
3582 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3583 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2)
3584 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3585 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3586 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3587 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3588 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3589 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
3590 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_6
3591 && r_type <= (int) R_SH_LAST_INVALID_RELOC_6))
3593 bfd_set_error (bfd_error_bad_value);
3597 howto = get_howto_table (output_bfd) + r_type;
3599 /* For relocs that aren't partial_inplace, we get the addend from
3601 if (! howto->partial_inplace)
3602 addend = rel->r_addend;
3604 resolved_to_zero = FALSE;
3608 check_segment[0] = -1;
3609 check_segment[1] = -1;
3610 if (r_symndx < symtab_hdr->sh_info)
3612 sym = local_syms + r_symndx;
3613 sec = local_sections[r_symndx];
3615 symname = bfd_elf_string_from_elf_section
3616 (input_bfd, symtab_hdr->sh_link, sym->st_name);
3617 if (symname == NULL || *symname == '\0')
3618 symname = bfd_section_name (input_bfd, sec);
3620 relocation = (sec->output_section->vma
3621 + sec->output_offset
3623 /* A local symbol never has STO_SH5_ISA32, so we don't need
3624 datalabel processing here. Make sure this does not change
3626 if ((sym->st_other & STO_SH5_ISA32) != 0)
3627 (*info->callbacks->reloc_dangerous)
3629 _("unexpected STO_SH5_ISA32 on local symbol is not handled"),
3630 input_bfd, input_section, rel->r_offset);
3632 if (sec != NULL && discarded_section (sec))
3633 /* Handled below. */
3635 else if (bfd_link_relocatable (info))
3637 /* This is a relocatable link. We don't have to change
3638 anything, unless the reloc is against a section symbol,
3639 in which case we have to adjust according to where the
3640 section symbol winds up in the output section. */
3641 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3643 if (! howto->partial_inplace)
3645 /* For relocations with the addend in the
3646 relocation, we need just to update the addend.
3647 All real relocs are of type partial_inplace; this
3648 code is mostly for completeness. */
3649 rel->r_addend += sec->output_offset;
3654 /* Relocs of type partial_inplace need to pick up the
3655 contents in the contents and add the offset resulting
3656 from the changed location of the section symbol.
3657 Using _bfd_final_link_relocate (e.g. goto
3658 final_link_relocate) here would be wrong, because
3659 relocations marked pc_relative would get the current
3660 location subtracted, and we must only do that at the
3662 r = _bfd_relocate_contents (howto, input_bfd,
3665 contents + rel->r_offset);
3666 goto relocation_done;
3671 else if (! howto->partial_inplace)
3673 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3674 addend = rel->r_addend;
3676 else if ((sec->flags & SEC_MERGE)
3677 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3681 if (howto->rightshift || howto->src_mask != 0xffffffff)
3684 /* xgettext:c-format */
3685 (_("%pB(%pA+%#" PRIx64 "): "
3686 "%s relocation against SEC_MERGE section"),
3687 input_bfd, input_section,
3688 (uint64_t) rel->r_offset, howto->name);
3692 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
3695 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
3697 addend += msec->output_section->vma + msec->output_offset;
3698 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
3704 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3707 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3708 symname = h->root.root.string;
3709 while (h->root.type == bfd_link_hash_indirect
3710 || h->root.type == bfd_link_hash_warning)
3711 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3712 if (h->root.type == bfd_link_hash_defined
3713 || h->root.type == bfd_link_hash_defweak)
3717 dyn = htab ? htab->root.dynamic_sections_created : FALSE;
3718 sec = h->root.u.def.section;
3719 /* In these cases, we don't need the relocation value.
3720 We check specially because in some obscure cases
3721 sec->output_section will be NULL. */
3722 if (r_type == R_SH_GOTPC
3723 || r_type == R_SH_GOTPC_LOW16
3724 || r_type == R_SH_GOTPC_MEDLOW16
3725 || r_type == R_SH_GOTPC_MEDHI16
3726 || r_type == R_SH_GOTPC_HI16
3727 || ((r_type == R_SH_PLT32
3728 || r_type == R_SH_PLT_LOW16
3729 || r_type == R_SH_PLT_MEDLOW16
3730 || r_type == R_SH_PLT_MEDHI16
3731 || r_type == R_SH_PLT_HI16)
3732 && h->plt.offset != (bfd_vma) -1)
3733 || ((r_type == R_SH_GOT32
3734 || r_type == R_SH_GOT20
3735 || r_type == R_SH_GOTFUNCDESC
3736 || r_type == R_SH_GOTFUNCDESC20
3737 || r_type == R_SH_GOTOFFFUNCDESC
3738 || r_type == R_SH_GOTOFFFUNCDESC20
3739 || r_type == R_SH_FUNCDESC
3740 || r_type == R_SH_GOT_LOW16
3741 || r_type == R_SH_GOT_MEDLOW16
3742 || r_type == R_SH_GOT_MEDHI16
3743 || r_type == R_SH_GOT_HI16)
3744 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
3745 bfd_link_pic (info),
3747 && (! bfd_link_pic (info)
3748 || (! info->symbolic && h->dynindx != -1)
3749 || !h->def_regular))
3750 /* The cases above are those in which relocation is
3751 overwritten in the switch block below. The cases
3752 below are those in which we must defer relocation
3753 to run-time, because we can't resolve absolute
3754 addresses when creating a shared library. */
3755 || (bfd_link_pic (info)
3756 && ((! info->symbolic && h->dynindx != -1)
3758 && ((r_type == R_SH_DIR32
3759 && !h->forced_local)
3760 || (r_type == R_SH_REL32
3761 && !SYMBOL_CALLS_LOCAL (info, h)))
3762 && ((input_section->flags & SEC_ALLOC) != 0
3763 /* DWARF will emit R_SH_DIR32 relocations in its
3764 sections against symbols defined externally
3765 in shared libraries. We can't do anything
3767 || ((input_section->flags & SEC_DEBUGGING) != 0
3768 && h->def_dynamic)))
3769 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3770 sections because such sections are not SEC_ALLOC and
3771 thus ld.so will not process them. */
3772 || (sec->output_section == NULL
3773 && ((input_section->flags & SEC_DEBUGGING) != 0
3775 || (sec->output_section == NULL
3776 && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE
3777 || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD)))
3779 else if (sec->output_section != NULL)
3780 relocation = ((h->root.u.def.value
3781 + sec->output_section->vma
3782 + sec->output_offset)
3783 /* A STO_SH5_ISA32 causes a "bitor 1" to the
3784 symbol value, unless we've seen
3785 STT_DATALABEL on the way to it. */
3786 | ((h->other & STO_SH5_ISA32) != 0
3787 && ! seen_stt_datalabel));
3788 else if (!bfd_link_relocatable (info)
3789 && (_bfd_elf_section_offset (output_bfd, info,
3795 /* xgettext:c-format */
3796 (_("%pB(%pA+%#" PRIx64 "): "
3797 "unresolvable %s relocation against symbol `%s'"),
3800 (uint64_t) rel->r_offset,
3802 h->root.root.string);
3806 else if (h->root.type == bfd_link_hash_undefweak)
3807 resolved_to_zero = UNDEFWEAK_NO_DYNAMIC_RELOC (info, h);
3808 else if (info->unresolved_syms_in_objects == RM_IGNORE
3809 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3811 else if (!bfd_link_relocatable (info))
3812 (*info->callbacks->undefined_symbol)
3813 (info, h->root.root.string, input_bfd,
3814 input_section, rel->r_offset,
3815 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
3816 || ELF_ST_VISIBILITY (h->other)));
3819 if (sec != NULL && discarded_section (sec))
3820 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3821 rel, 1, relend, howto, 0, contents);
3823 if (bfd_link_relocatable (info))
3826 /* Check for inter-segment relocations in FDPIC files. Most
3827 relocations connect the relocation site to the location of
3828 the target symbol, but there are some exceptions below. */
3829 check_segment[0] = isec_segment;
3831 check_segment[1] = sh_elf_osec_to_segment (output_bfd,
3832 sec->output_section);
3834 check_segment[1] = -1;
3836 switch ((int) r_type)
3838 final_link_relocate:
3839 /* COFF relocs don't use the addend. The addend is used for
3840 R_SH_DIR32 to be compatible with other compilers. */
3841 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3842 contents, rel->r_offset,
3843 relocation, addend);
3847 goto final_link_relocate;
3852 /* If the reloc is against the start of this section, then
3853 the assembler has already taken care of it and the reloc
3854 is here only to assist in relaxing. If the reloc is not
3855 against the start of this section, then it's against an
3856 external symbol and we must deal with it ourselves. */
3857 if (input_section->output_section->vma + input_section->output_offset
3860 int disp = (relocation
3861 - input_section->output_section->vma
3862 - input_section->output_offset
3868 case R_SH_DIR8WPZ: mask = 1; break;
3869 case R_SH_DIR8WPL: mask = 3; break;
3870 default: mask = 0; break;
3875 /* xgettext:c-format */
3876 (_("%pB: %#" PRIx64 ": fatal: "
3877 "unaligned branch target for relax-support relocation"),
3878 input_section->owner,
3879 (uint64_t) rel->r_offset);
3880 bfd_set_error (bfd_error_bad_value);
3884 goto final_link_relocate;
3890 bfd_set_error (bfd_error_bad_value);
3898 goto final_link_relocate;
3905 /* xgettext:c-format */
3906 (_("%pB: %#" PRIx64 ": fatal: "
3907 "unaligned %s relocation %#" PRIx64),
3908 input_section->owner, (uint64_t) rel->r_offset,
3909 howto->name, (uint64_t) relocation);
3910 bfd_set_error (bfd_error_bad_value);
3913 goto final_link_relocate;
3921 /* xgettext:c-format */
3922 (_("%pB: %#" PRIx64 ": fatal: "
3923 "unaligned %s relocation %#" PRIx64 ""),
3924 input_section->owner,
3925 (uint64_t) rel->r_offset, howto->name,
3926 (uint64_t) relocation);
3927 bfd_set_error (bfd_error_bad_value);
3930 goto final_link_relocate;
3933 if ((signed int)relocation < -32
3934 || (signed int)relocation > 32)
3937 /* xgettext:c-format */
3938 (_("%pB: %#" PRIx64 ": fatal: R_SH_PSHA relocation %" PRId64
3939 " not in range -32..32"),
3940 input_section->owner,
3941 (uint64_t) rel->r_offset,
3942 (int64_t) relocation);
3943 bfd_set_error (bfd_error_bad_value);
3946 goto final_link_relocate;
3949 if ((signed int)relocation < -16
3950 || (signed int)relocation > 16)
3953 /* xgettext:c-format */
3954 (_("%pB: %#" PRIx64 ": fatal: R_SH_PSHL relocation %" PRId64
3955 " not in range -32..32"),
3956 input_section->owner,
3957 (uint64_t) rel->r_offset,
3958 (int64_t) relocation);
3959 bfd_set_error (bfd_error_bad_value);
3962 goto final_link_relocate;
3966 if (bfd_link_pic (info)
3968 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3969 && !resolved_to_zero)
3970 || h->root.type != bfd_link_hash_undefweak)
3971 && r_symndx != STN_UNDEF
3972 && (input_section->flags & SEC_ALLOC) != 0
3974 && (r_type == R_SH_DIR32
3975 || !SYMBOL_CALLS_LOCAL (info, h)))
3977 Elf_Internal_Rela outrel;
3979 bfd_boolean skip, relocate;
3981 /* When generating a shared object, these relocations
3982 are copied into the output file to be resolved at run
3987 sreloc = _bfd_elf_get_dynamic_reloc_section
3988 (input_bfd, input_section, /*rela?*/ TRUE);
3997 _bfd_elf_section_offset (output_bfd, info, input_section,
3999 if (outrel.r_offset == (bfd_vma) -1)
4001 else if (outrel.r_offset == (bfd_vma) -2)
4002 skip = TRUE, relocate = TRUE;
4003 outrel.r_offset += (input_section->output_section->vma
4004 + input_section->output_offset);
4007 memset (&outrel, 0, sizeof outrel);
4008 else if (r_type == R_SH_REL32)
4010 BFD_ASSERT (h != NULL && h->dynindx != -1);
4011 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4013 = (howto->partial_inplace
4014 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4019 || ((info->symbolic || h->dynindx == -1)
4020 && h->def_regular)))
4024 BFD_ASSERT (sec != NULL);
4025 BFD_ASSERT (sec->output_section != NULL);
4026 dynindx = elf_section_data (sec->output_section)->dynindx;
4027 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4028 outrel.r_addend = relocation;
4030 += (howto->partial_inplace
4031 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4033 outrel.r_addend -= sec->output_section->vma;
4037 /* h->dynindx may be -1 if this symbol was marked to
4040 || ((info->symbolic || h->dynindx == -1)
4043 relocate = howto->partial_inplace;
4044 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4048 BFD_ASSERT (h->dynindx != -1);
4049 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4051 outrel.r_addend = relocation;
4053 += (howto->partial_inplace
4054 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4058 loc = sreloc->contents;
4059 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4060 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4062 check_segment[0] = check_segment[1] = -1;
4064 /* If this reloc is against an external symbol, we do
4065 not want to fiddle with the addend. Otherwise, we
4066 need to include the symbol value so that it becomes
4067 an addend for the dynamic reloc. */
4071 else if (fdpic_p && !bfd_link_pic (info)
4072 && r_type == R_SH_DIR32
4073 && (input_section->flags & SEC_ALLOC) != 0)
4079 if (sh_elf_osec_readonly_p (output_bfd,
4080 input_section->output_section))
4083 /* xgettext:c-format */
4084 (_("%pB(%pA+%#" PRIx64 "): "
4085 "cannot emit fixup to `%s' in read-only section"),
4088 (uint64_t) rel->r_offset,
4093 offset = _bfd_elf_section_offset (output_bfd, info,
4094 input_section, rel->r_offset);
4095 if (offset != (bfd_vma)-1)
4096 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4097 input_section->output_section->vma
4098 + input_section->output_offset
4101 check_segment[0] = check_segment[1] = -1;
4103 /* We don't want warnings for non-NULL tests on undefined weak
4105 else if (r_type == R_SH_REL32
4107 && h->root.type == bfd_link_hash_undefweak)
4108 check_segment[0] = check_segment[1] = -1;
4109 goto final_link_relocate;
4112 /* Relocation is to the entry for this symbol in the
4113 procedure linkage table. */
4117 || ! bfd_link_pic (info)
4120 || h->plt.offset == (bfd_vma) -1
4121 || h->got.offset != (bfd_vma) -1)
4124 /* Relocation is to the entry for this symbol in the global
4125 offset table extension for the procedure linkage table. */
4128 BFD_ASSERT (sgotplt != NULL);
4129 relocation = (sgotplt->output_offset
4130 + (get_plt_index (htab->plt_info, h->plt.offset)
4134 relocation -= GOT_BIAS;
4137 goto final_link_relocate;
4142 /* Relocation is to the entry for this symbol in the global
4146 BFD_ASSERT (sgot != NULL);
4147 check_segment[0] = check_segment[1] = -1;
4153 off = h->got.offset;
4154 BFD_ASSERT (off != (bfd_vma) -1);
4156 dyn = htab->root.dynamic_sections_created;
4157 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4158 bfd_link_pic (info),
4160 || (bfd_link_pic (info)
4161 && SYMBOL_REFERENCES_LOCAL (info, h))
4162 || ((ELF_ST_VISIBILITY (h->other)
4163 || resolved_to_zero)
4164 && h->root.type == bfd_link_hash_undefweak))
4166 /* This is actually a static link, or it is a
4167 -Bsymbolic link and the symbol is defined
4168 locally, or the symbol was forced to be local
4169 because of a version file. We must initialize
4170 this entry in the global offset table. Since the
4171 offset must always be a multiple of 4, we use the
4172 least significant bit to record whether we have
4173 initialized it already.
4175 When doing a dynamic link, we create a .rela.got
4176 relocation entry to initialize the value. This
4177 is done in the finish_dynamic_symbol routine. */
4182 bfd_put_32 (output_bfd, relocation,
4183 sgot->contents + off);
4186 /* If we initialize the GOT entry here with a valid
4187 symbol address, also add a fixup. */
4188 if (fdpic_p && !bfd_link_pic (info)
4189 && sh_elf_hash_entry (h)->got_type == GOT_NORMAL
4190 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4191 || h->root.type != bfd_link_hash_undefweak))
4192 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4193 sgot->output_section->vma
4194 + sgot->output_offset
4199 relocation = sh_elf_got_offset (htab) + off;
4203 BFD_ASSERT (local_got_offsets != NULL
4204 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4206 off = local_got_offsets[r_symndx];
4208 /* The offset must always be a multiple of 4. We use
4209 the least significant bit to record whether we have
4210 already generated the necessary reloc. */
4215 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4217 if (bfd_link_pic (info))
4219 Elf_Internal_Rela outrel;
4222 outrel.r_offset = (sgot->output_section->vma
4223 + sgot->output_offset
4228 = elf_section_data (sec->output_section)->dynindx;
4229 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4230 outrel.r_addend = relocation;
4231 outrel.r_addend -= sec->output_section->vma;
4235 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4236 outrel.r_addend = relocation;
4238 loc = srelgot->contents;
4239 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4240 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4243 && (sh_elf_local_got_type (input_bfd) [r_symndx]
4245 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4246 sgot->output_section->vma
4247 + sgot->output_offset
4250 local_got_offsets[r_symndx] |= 1;
4253 relocation = sh_elf_got_offset (htab) + off;
4257 relocation -= GOT_BIAS;
4260 if (r_type == R_SH_GOT20)
4262 r = install_movi20_field (output_bfd, relocation + addend,
4263 input_bfd, input_section, contents,
4268 goto final_link_relocate;
4272 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4273 we place at the start of the .got.plt section. This is the same
4274 as the start of the output .got section, unless there are function
4275 descriptors in front of it. */
4277 BFD_ASSERT (sgotplt != NULL);
4278 check_segment[0] = got_segment;
4279 relocation -= sgotplt->output_section->vma + sgotplt->output_offset
4280 + htab->root.hgot->root.u.def.value;
4283 relocation -= GOT_BIAS;
4286 addend = rel->r_addend;
4288 if (r_type == R_SH_GOTOFF20)
4290 r = install_movi20_field (output_bfd, relocation + addend,
4291 input_bfd, input_section, contents,
4296 goto final_link_relocate;
4299 /* Use global offset table as symbol value. */
4301 BFD_ASSERT (sgotplt != NULL);
4302 relocation = sgotplt->output_section->vma + sgotplt->output_offset;
4305 relocation += GOT_BIAS;
4308 addend = rel->r_addend;
4310 goto final_link_relocate;
4313 /* Relocation is to the entry for this symbol in the
4314 procedure linkage table. */
4316 /* Resolve a PLT reloc against a local symbol directly,
4317 without using the procedure linkage table. */
4319 goto final_link_relocate;
4321 /* We don't want to warn on calls to undefined weak symbols,
4322 as calls to them must be protected by non-NULL tests
4323 anyway, and unprotected calls would invoke undefined
4325 if (h->root.type == bfd_link_hash_undefweak)
4326 check_segment[0] = check_segment[1] = -1;
4328 if (h->forced_local)
4329 goto final_link_relocate;
4331 if (h->plt.offset == (bfd_vma) -1)
4333 /* We didn't make a PLT entry for this symbol. This
4334 happens when statically linking PIC code, or when
4335 using -Bsymbolic. */
4336 goto final_link_relocate;
4339 BFD_ASSERT (splt != NULL);
4340 check_segment[1] = plt_segment;
4341 relocation = (splt->output_section->vma
4342 + splt->output_offset
4345 addend = rel->r_addend;
4347 goto final_link_relocate;
4349 /* Relocation is to the canonical function descriptor for this
4350 symbol, possibly via the GOT. Initialize the GOT
4351 entry and function descriptor if necessary. */
4352 case R_SH_GOTFUNCDESC:
4353 case R_SH_GOTFUNCDESC20:
4357 asection *reloc_section;
4358 bfd_vma reloc_offset;
4359 int reloc_type = R_SH_FUNCDESC;
4363 check_segment[0] = check_segment[1] = -1;
4365 /* FIXME: See what FRV does for global symbols in the
4366 executable, with --export-dynamic. Do they need ld.so
4367 to allocate official descriptors? See what this code
4373 if (r_type == R_SH_FUNCDESC)
4375 reloc_section = input_section;
4376 reloc_offset = rel->r_offset;
4380 reloc_section = sgot;
4383 reloc_offset = h->got.offset;
4386 BFD_ASSERT (local_got_offsets != NULL);
4387 reloc_offset = local_got_offsets[r_symndx];
4389 BFD_ASSERT (reloc_offset != MINUS_ONE);
4391 if (reloc_offset & 1)
4394 goto funcdesc_done_got;
4398 if (h && h->root.type == bfd_link_hash_undefweak
4399 && (SYMBOL_CALLS_LOCAL (info, h)
4400 || !htab->root.dynamic_sections_created))
4401 /* Undefined weak symbol which will not be dynamically
4402 resolved later; leave it at zero. */
4403 goto funcdesc_leave_zero;
4404 else if (SYMBOL_CALLS_LOCAL (info, h)
4405 && ! SYMBOL_FUNCDESC_LOCAL (info, h))
4407 /* If the symbol needs a non-local function descriptor
4408 but binds locally (i.e., its visibility is
4409 protected), emit a dynamic relocation decayed to
4410 section+offset. This is an optimization; the dynamic
4411 linker would resolve our function descriptor request
4412 to our copy of the function anyway. */
4413 dynindx = elf_section_data (h->root.u.def.section
4414 ->output_section)->dynindx;
4415 relocation += h->root.u.def.section->output_offset
4416 + h->root.u.def.value;
4418 else if (! SYMBOL_FUNCDESC_LOCAL (info, h))
4420 /* If the symbol is dynamic and there will be dynamic
4421 symbol resolution because we are or are linked with a
4422 shared library, emit a FUNCDESC relocation such that
4423 the dynamic linker will allocate the function
4425 BFD_ASSERT (h->dynindx != -1);
4426 dynindx = h->dynindx;
4432 /* Otherwise, we know we have a private function
4433 descriptor, so reference it directly. */
4434 reloc_type = R_SH_DIR32;
4435 dynindx = elf_section_data (htab->sfuncdesc
4436 ->output_section)->dynindx;
4440 offset = sh_elf_hash_entry (h)->funcdesc.offset;
4441 BFD_ASSERT (offset != MINUS_ONE);
4442 if ((offset & 1) == 0)
4444 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4447 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4452 union gotref *local_funcdesc;
4454 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4455 offset = local_funcdesc[r_symndx].offset;
4456 BFD_ASSERT (offset != MINUS_ONE);
4457 if ((offset & 1) == 0)
4459 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4463 local_funcdesc[r_symndx].offset |= 1;
4467 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4470 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
4474 if (sh_elf_osec_readonly_p (output_bfd,
4475 reloc_section->output_section))
4478 /* xgettext:c-format */
4479 (_("%pB(%pA+%#" PRIx64 "): "
4480 "cannot emit fixup to `%s' in read-only section"),
4483 (uint64_t) rel->r_offset,
4488 offset = _bfd_elf_section_offset (output_bfd, info,
4489 reloc_section, reloc_offset);
4491 if (offset != (bfd_vma)-1)
4492 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4494 + reloc_section->output_section->vma
4495 + reloc_section->output_offset);
4497 else if ((reloc_section->output_section->flags
4498 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
4502 if (sh_elf_osec_readonly_p (output_bfd,
4503 reloc_section->output_section))
4505 info->callbacks->warning
4507 _("cannot emit dynamic relocations in read-only section"),
4508 symname, input_bfd, reloc_section, reloc_offset);
4512 offset = _bfd_elf_section_offset (output_bfd, info,
4513 reloc_section, reloc_offset);
4515 if (offset != (bfd_vma)-1)
4516 sh_elf_add_dyn_reloc (output_bfd, srelgot,
4518 + reloc_section->output_section->vma
4519 + reloc_section->output_offset,
4520 reloc_type, dynindx, relocation);
4522 if (r_type == R_SH_FUNCDESC)
4530 goto funcdesc_leave_zero;
4534 if (SYMBOL_FUNCDESC_LOCAL (info, h))
4535 relocation += htab->sfuncdesc->output_section->vma;
4536 funcdesc_leave_zero:
4537 if (r_type != R_SH_FUNCDESC)
4539 bfd_put_32 (output_bfd, relocation,
4540 reloc_section->contents + reloc_offset);
4544 local_got_offsets[r_symndx] |= 1;
4548 relocation = sh_elf_got_offset (htab) + reloc_offset;
4550 relocation -= GOT_BIAS;
4553 if (r_type == R_SH_GOTFUNCDESC20)
4555 r = install_movi20_field (output_bfd, relocation + addend,
4556 input_bfd, input_section, contents,
4561 goto final_link_relocate;
4565 case R_SH_GOTOFFFUNCDESC:
4566 case R_SH_GOTOFFFUNCDESC20:
4567 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4568 executable and --export-dynamic. If such symbols get
4569 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
4573 check_segment[0] = check_segment[1] = -1;
4575 addend = rel->r_addend;
4577 if (h && (h->root.type == bfd_link_hash_undefweak
4578 || !SYMBOL_FUNCDESC_LOCAL (info, h)))
4581 /* xgettext:c-format */
4582 (_("%pB(%pA+%#" PRIx64 "): "
4583 "%s relocation against external symbol \"%s\""),
4584 input_bfd, input_section, (uint64_t) rel->r_offset,
4585 howto->name, h->root.root.string);
4592 /* Otherwise, we know we have a private function
4593 descriptor, so reference it directly. */
4596 offset = sh_elf_hash_entry (h)->funcdesc.offset;
4597 BFD_ASSERT (offset != MINUS_ONE);
4598 if ((offset & 1) == 0)
4600 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4603 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4608 union gotref *local_funcdesc;
4610 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4611 offset = local_funcdesc[r_symndx].offset;
4612 BFD_ASSERT (offset != MINUS_ONE);
4613 if ((offset & 1) == 0)
4615 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4619 local_funcdesc[r_symndx].offset |= 1;
4623 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4626 relocation -= (htab->root.hgot->root.u.def.value
4627 + sgotplt->output_offset);
4629 relocation -= GOT_BIAS;
4632 if (r_type == R_SH_GOTOFFFUNCDESC20)
4634 r = install_movi20_field (output_bfd, relocation + addend,
4635 input_bfd, input_section, contents,
4640 goto final_link_relocate;
4642 case R_SH_LOOP_START:
4644 static bfd_vma start, end;
4646 start = (relocation + rel->r_addend
4647 - (sec->output_section->vma + sec->output_offset));
4648 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
4649 rel->r_offset, sec, start, end);
4653 end = (relocation + rel->r_addend
4654 - (sec->output_section->vma + sec->output_offset));
4655 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
4656 rel->r_offset, sec, start, end);
4660 case R_SH_TLS_GD_32:
4661 case R_SH_TLS_IE_32:
4663 check_segment[0] = check_segment[1] = -1;
4664 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
4665 got_type = GOT_UNKNOWN;
4666 if (h == NULL && local_got_offsets)
4667 got_type = sh_elf_local_got_type (input_bfd) [r_symndx];
4670 got_type = sh_elf_hash_entry (h)->got_type;
4671 if (! bfd_link_pic (info)
4672 && (h->dynindx == -1
4674 r_type = R_SH_TLS_LE_32;
4677 if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE)
4678 r_type = R_SH_TLS_IE_32;
4680 if (r_type == R_SH_TLS_LE_32)
4683 unsigned short insn;
4685 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
4687 /* GD->LE transition:
4688 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4689 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4690 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4692 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
4694 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
4696 offset = rel->r_offset;
4700 /* xgettext:c-format */
4701 (_("%pB(%pA): offset in relocation for GD->LE translation is too small: %#" PRIx64),
4702 input_bfd, input_section, (uint64_t) offset);
4706 /* Size of GD instructions is 16 or 18. */
4708 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4709 if ((insn & 0xff00) == 0xc700)
4711 BFD_ASSERT (offset >= 2);
4713 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4716 if ((insn & 0xff00) != 0xd400)
4718 /* xgettext:c-format */ /* The backslash is to prevent bogus trigraph detection. */
4719 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd4?\?)"),
4720 input_bfd, input_section, (uint64_t) offset, (int) insn);
4722 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4724 if ((insn & 0xff00) != 0xc700)
4726 /* xgettext:c-format */
4727 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xc7?\?)"),
4728 input_bfd, input_section, (uint64_t) offset, (int) insn);
4730 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4731 if ((insn & 0xff00) != 0xd100)
4733 /* xgettext:c-format */
4734 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd1?\?)"),
4735 input_bfd, input_section, (uint64_t) offset, (int) insn);
4737 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4740 /* xgettext:c-format */
4741 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x310c)"),
4742 input_bfd, input_section, (uint64_t) offset, (int) insn);
4744 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4747 /* xgettext:c-format */
4748 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x410b)"),
4749 input_bfd, input_section, (uint64_t) offset, (int) insn);
4751 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4754 /* xgettext:c-format */
4755 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x34cc)"),
4756 input_bfd, input_section, (uint64_t) offset, (int) insn);
4758 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
4759 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
4760 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
4761 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4762 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4768 /* IE->LE transition:
4785 offset = rel->r_offset;
4789 /* xgettext:c-format */
4790 (_("%pB(%pA): offset in relocation for IE->LE translation is too small: %#" PRIx64),
4791 input_bfd, input_section, (uint64_t) offset);
4795 /* Size of IE instructions is 10 or 12. */
4797 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4798 if ((insn & 0xf0ff) == 0x0012)
4800 BFD_ASSERT (offset >= 2);
4802 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4805 if ((insn & 0xff00) != 0xd000)
4807 /* xgettext:c-format */
4808 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd0??: mov.l)"),
4809 input_bfd, input_section, (uint64_t) offset, (int) insn);
4811 target = insn & 0x00ff;
4813 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4814 if ((insn & 0xf0ff) != 0x0012)
4816 /* xgettext:c-format */
4817 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x0?12: stc)"),
4818 input_bfd, input_section, (uint64_t) (offset + 2), (int) insn);
4820 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4821 if ((insn & 0xf0ff) != 0x00ce)
4823 /* xgettext:c-format */
4824 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x0?ce: mov.l)"),
4825 input_bfd, input_section, (uint64_t) (offset + 4), (int) insn);
4827 insn = 0xd000 | (insn & 0x0f00) | target;
4828 bfd_put_16 (output_bfd, insn, contents + offset + 0);
4829 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
4832 bfd_put_32 (output_bfd, tpoff (info, relocation),
4833 contents + rel->r_offset);
4837 if (sgot == NULL || sgotplt == NULL)
4841 off = h->got.offset;
4844 if (local_got_offsets == NULL)
4847 off = local_got_offsets[r_symndx];
4850 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4851 if (r_type == R_SH_TLS_IE_32
4852 && ! htab->root.dynamic_sections_created)
4855 bfd_put_32 (output_bfd, tpoff (info, relocation),
4856 sgot->contents + off);
4857 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
4858 contents + rel->r_offset);
4866 Elf_Internal_Rela outrel;
4870 outrel.r_offset = (sgot->output_section->vma
4871 + sgot->output_offset + off);
4873 if (h == NULL || h->dynindx == -1)
4878 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
4880 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
4881 outrel.r_addend = relocation - dtpoff_base (info);
4883 outrel.r_addend = 0;
4884 outrel.r_info = ELF32_R_INFO (indx, dr_type);
4885 loc = srelgot->contents;
4886 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4887 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4889 if (r_type == R_SH_TLS_GD_32)
4893 bfd_put_32 (output_bfd,
4894 relocation - dtpoff_base (info),
4895 sgot->contents + off + 4);
4899 outrel.r_info = ELF32_R_INFO (indx,
4901 outrel.r_offset += 4;
4902 outrel.r_addend = 0;
4903 srelgot->reloc_count++;
4904 loc += sizeof (Elf32_External_Rela);
4905 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4912 local_got_offsets[r_symndx] |= 1;
4915 if (off >= (bfd_vma) -2)
4918 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
4919 relocation = sh_elf_got_offset (htab) + off;
4923 unsigned short insn;
4925 /* GD->IE transition:
4926 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4927 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4928 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4930 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4931 nop; nop; bra 3f; nop; .align 2;
4932 1: .long x@TPOFF; 2:...; 3:. */
4934 offset = rel->r_offset;
4938 /* xgettext:c-format */
4939 (_("%pB(%pA): offset in relocation for GD->IE translation is too small: %#" PRIx64),
4940 input_bfd, input_section, (uint64_t) offset);
4944 /* Size of GD instructions is 16 or 18. */
4946 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4947 if ((insn & 0xff00) == 0xc700)
4949 BFD_ASSERT (offset >= 2);
4951 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4954 BFD_ASSERT ((insn & 0xff00) == 0xd400);
4956 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4957 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
4959 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4960 BFD_ASSERT ((insn & 0xff00) == 0xc700);
4961 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4962 BFD_ASSERT ((insn & 0xff00) == 0xd100);
4963 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4964 BFD_ASSERT (insn == 0x310c);
4965 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4966 BFD_ASSERT (insn == 0x410b);
4967 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4968 BFD_ASSERT (insn == 0x34cc);
4970 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
4971 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
4972 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
4973 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4974 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4976 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
4977 contents + rel->r_offset);
4982 addend = rel->r_addend;
4984 goto final_link_relocate;
4986 case R_SH_TLS_LD_32:
4988 check_segment[0] = check_segment[1] = -1;
4989 if (! bfd_link_pic (info))
4992 unsigned short insn;
4994 /* LD->LE transition:
4995 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4996 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4997 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
4999 stc gbr,r0; nop; nop; nop;
5000 nop; nop; bra 3f; ...; 3:. */
5002 offset = rel->r_offset;
5006 /* xgettext:c-format */
5007 (_("%pB(%pA): offset in relocation for LD->LE translation is too small: %#" PRIx64),
5008 input_bfd, input_section, (uint64_t) offset);
5012 /* Size of LD instructions is 16 or 18. */
5014 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5015 if ((insn & 0xff00) == 0xc700)
5017 BFD_ASSERT (offset >= 2);
5019 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5022 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5023 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5024 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5025 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5026 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5027 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5028 BFD_ASSERT (insn == 0x310c);
5029 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5030 BFD_ASSERT (insn == 0x410b);
5031 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5032 BFD_ASSERT (insn == 0x34cc);
5034 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5035 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5036 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5037 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5038 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5039 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5044 if (sgot == NULL || sgotplt == NULL)
5047 off = htab->tls_ldm_got.offset;
5052 Elf_Internal_Rela outrel;
5055 outrel.r_offset = (sgot->output_section->vma
5056 + sgot->output_offset + off);
5057 outrel.r_addend = 0;
5058 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
5059 loc = srelgot->contents;
5060 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5061 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5062 htab->tls_ldm_got.offset |= 1;
5065 relocation = sh_elf_got_offset (htab) + off;
5066 addend = rel->r_addend;
5068 goto final_link_relocate;
5070 case R_SH_TLS_LDO_32:
5071 check_segment[0] = check_segment[1] = -1;
5072 if (! bfd_link_pic (info))
5073 relocation = tpoff (info, relocation);
5075 relocation -= dtpoff_base (info);
5077 addend = rel->r_addend;
5078 goto final_link_relocate;
5080 case R_SH_TLS_LE_32:
5083 Elf_Internal_Rela outrel;
5086 check_segment[0] = check_segment[1] = -1;
5088 if (!bfd_link_dll (info))
5090 relocation = tpoff (info, relocation);
5091 addend = rel->r_addend;
5092 goto final_link_relocate;
5097 sreloc = _bfd_elf_get_dynamic_reloc_section
5098 (input_bfd, input_section, /*rela?*/ TRUE);
5103 if (h == NULL || h->dynindx == -1)
5108 outrel.r_offset = (input_section->output_section->vma
5109 + input_section->output_offset
5111 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5113 outrel.r_addend = relocation - dtpoff_base (info);
5115 outrel.r_addend = 0;
5117 loc = sreloc->contents;
5118 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5119 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5125 if (fdpic_p && check_segment[0] != (unsigned) -1
5126 && check_segment[0] != check_segment[1])
5128 /* We don't want duplicate errors for undefined symbols. */
5129 if (!h || h->root.type != bfd_link_hash_undefined)
5131 if (bfd_link_pic (info))
5133 info->callbacks->einfo
5134 /* xgettext:c-format */
5135 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5136 input_bfd, input_section, rel->r_offset, symname);
5140 info->callbacks->einfo
5141 /* xgettext:c-format */
5142 (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5143 input_bfd, input_section, rel->r_offset, symname);
5146 elf_elfheader (output_bfd)->e_flags |= EF_SH_PIC;
5149 if (r != bfd_reloc_ok)
5154 case bfd_reloc_outofrange:
5156 case bfd_reloc_overflow:
5164 name = (bfd_elf_string_from_elf_section
5165 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5169 name = bfd_section_name (input_bfd, sec);
5171 (*info->callbacks->reloc_overflow)
5172 (info, (h ? &h->root : NULL), name, howto->name,
5173 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5183 /* This is a version of bfd_generic_get_relocated_section_contents
5184 which uses sh_elf_relocate_section. */
5187 sh_elf_get_relocated_section_contents (bfd *output_bfd,
5188 struct bfd_link_info *link_info,
5189 struct bfd_link_order *link_order,
5191 bfd_boolean relocatable,
5194 Elf_Internal_Shdr *symtab_hdr;
5195 asection *input_section = link_order->u.indirect.section;
5196 bfd *input_bfd = input_section->owner;
5197 asection **sections = NULL;
5198 Elf_Internal_Rela *internal_relocs = NULL;
5199 Elf_Internal_Sym *isymbuf = NULL;
5201 /* We only need to handle the case of relaxing, or of having a
5202 particular set of section contents, specially. */
5204 || elf_section_data (input_section)->this_hdr.contents == NULL)
5205 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5210 symtab_hdr = &elf_symtab_hdr (input_bfd);
5212 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5213 (size_t) input_section->size);
5215 if ((input_section->flags & SEC_RELOC) != 0
5216 && input_section->reloc_count > 0)
5219 Elf_Internal_Sym *isym, *isymend;
5222 internal_relocs = (_bfd_elf_link_read_relocs
5223 (input_bfd, input_section, NULL,
5224 (Elf_Internal_Rela *) NULL, FALSE));
5225 if (internal_relocs == NULL)
5228 if (symtab_hdr->sh_info != 0)
5230 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5231 if (isymbuf == NULL)
5232 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5233 symtab_hdr->sh_info, 0,
5235 if (isymbuf == NULL)
5239 amt = symtab_hdr->sh_info;
5240 amt *= sizeof (asection *);
5241 sections = (asection **) bfd_malloc (amt);
5242 if (sections == NULL && amt != 0)
5245 isymend = isymbuf + symtab_hdr->sh_info;
5246 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5250 if (isym->st_shndx == SHN_UNDEF)
5251 isec = bfd_und_section_ptr;
5252 else if (isym->st_shndx == SHN_ABS)
5253 isec = bfd_abs_section_ptr;
5254 else if (isym->st_shndx == SHN_COMMON)
5255 isec = bfd_com_section_ptr;
5257 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5262 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5263 input_section, data, internal_relocs,
5267 if (sections != NULL)
5270 && symtab_hdr->contents != (unsigned char *) isymbuf)
5272 if (elf_section_data (input_section)->relocs != internal_relocs)
5273 free (internal_relocs);
5279 if (sections != NULL)
5282 && symtab_hdr->contents != (unsigned char *) isymbuf)
5284 if (internal_relocs != NULL
5285 && elf_section_data (input_section)->relocs != internal_relocs)
5286 free (internal_relocs);
5290 /* Return the base VMA address which should be subtracted from real addresses
5291 when resolving @dtpoff relocation.
5292 This is PT_TLS segment p_vaddr. */
5295 dtpoff_base (struct bfd_link_info *info)
5297 /* If tls_sec is NULL, we should have signalled an error already. */
5298 if (elf_hash_table (info)->tls_sec == NULL)
5300 return elf_hash_table (info)->tls_sec->vma;
5303 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5306 tpoff (struct bfd_link_info *info, bfd_vma address)
5308 /* If tls_sec is NULL, we should have signalled an error already. */
5309 if (elf_hash_table (info)->tls_sec == NULL)
5311 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5312 structure which has 2 pointer fields. */
5313 return (address - elf_hash_table (info)->tls_sec->vma
5314 + align_power ((bfd_vma) 8,
5315 elf_hash_table (info)->tls_sec->alignment_power));
5319 sh_elf_gc_mark_hook (asection *sec,
5320 struct bfd_link_info *info,
5321 Elf_Internal_Rela *rel,
5322 struct elf_link_hash_entry *h,
5323 Elf_Internal_Sym *sym)
5326 switch (ELF32_R_TYPE (rel->r_info))
5328 case R_SH_GNU_VTINHERIT:
5329 case R_SH_GNU_VTENTRY:
5333 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5336 /* Copy the extra info we tack onto an elf_link_hash_entry. */
5339 sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
5340 struct elf_link_hash_entry *dir,
5341 struct elf_link_hash_entry *ind)
5343 struct elf_sh_link_hash_entry *edir, *eind;
5345 edir = (struct elf_sh_link_hash_entry *) dir;
5346 eind = (struct elf_sh_link_hash_entry *) ind;
5348 if (eind->dyn_relocs != NULL)
5350 if (edir->dyn_relocs != NULL)
5352 struct elf_dyn_relocs **pp;
5353 struct elf_dyn_relocs *p;
5355 /* Add reloc counts against the indirect sym to the direct sym
5356 list. Merge any entries against the same section. */
5357 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5359 struct elf_dyn_relocs *q;
5361 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5362 if (q->sec == p->sec)
5364 q->pc_count += p->pc_count;
5365 q->count += p->count;
5372 *pp = edir->dyn_relocs;
5375 edir->dyn_relocs = eind->dyn_relocs;
5376 eind->dyn_relocs = NULL;
5378 edir->gotplt_refcount = eind->gotplt_refcount;
5379 eind->gotplt_refcount = 0;
5380 edir->funcdesc.refcount += eind->funcdesc.refcount;
5381 eind->funcdesc.refcount = 0;
5382 edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount;
5383 eind->abs_funcdesc_refcount = 0;
5385 if (ind->root.type == bfd_link_hash_indirect
5386 && dir->got.refcount <= 0)
5388 edir->got_type = eind->got_type;
5389 eind->got_type = GOT_UNKNOWN;
5392 if (ind->root.type != bfd_link_hash_indirect
5393 && dir->dynamic_adjusted)
5395 /* If called to transfer flags for a weakdef during processing
5396 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5397 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
5398 if (dir->versioned != versioned_hidden)
5399 dir->ref_dynamic |= ind->ref_dynamic;
5400 dir->ref_regular |= ind->ref_regular;
5401 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
5402 dir->needs_plt |= ind->needs_plt;
5405 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
5409 sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
5412 if (bfd_link_pic (info))
5417 case R_SH_TLS_GD_32:
5418 case R_SH_TLS_IE_32:
5420 return R_SH_TLS_LE_32;
5421 return R_SH_TLS_IE_32;
5422 case R_SH_TLS_LD_32:
5423 return R_SH_TLS_LE_32;
5429 /* Look through the relocs for a section during the first phase.
5430 Since we don't do .gots or .plts, we just need to consider the
5431 virtual table relocs for gc. */
5434 sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
5435 const Elf_Internal_Rela *relocs)
5437 Elf_Internal_Shdr *symtab_hdr;
5438 struct elf_link_hash_entry **sym_hashes;
5439 struct elf_sh_link_hash_table *htab;
5440 const Elf_Internal_Rela *rel;
5441 const Elf_Internal_Rela *rel_end;
5443 unsigned int r_type;
5444 enum got_type got_type, old_got_type;
5448 if (bfd_link_relocatable (info))
5451 /* Don't do anything special with non-loaded, non-alloced sections.
5452 In particular, any relocs in such sections should not affect GOT
5453 and PLT reference counting (ie. we don't allow them to create GOT
5454 or PLT entries), there's no possibility or desire to optimize TLS
5455 relocs, and there's not much point in propagating relocs to shared
5456 libs that the dynamic linker won't relocate. */
5457 if ((sec->flags & SEC_ALLOC) == 0)
5460 BFD_ASSERT (is_sh_elf (abfd));
5462 symtab_hdr = &elf_symtab_hdr (abfd);
5463 sym_hashes = elf_sym_hashes (abfd);
5465 htab = sh_elf_hash_table (info);
5469 rel_end = relocs + sec->reloc_count;
5470 for (rel = relocs; rel < rel_end; rel++)
5472 struct elf_link_hash_entry *h;
5473 unsigned long r_symndx;
5475 r_symndx = ELF32_R_SYM (rel->r_info);
5476 r_type = ELF32_R_TYPE (rel->r_info);
5478 if (r_symndx < symtab_hdr->sh_info)
5482 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5483 while (h->root.type == bfd_link_hash_indirect
5484 || h->root.type == bfd_link_hash_warning)
5485 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5488 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5489 if (! bfd_link_pic (info)
5490 && r_type == R_SH_TLS_IE_32
5492 && h->root.type != bfd_link_hash_undefined
5493 && h->root.type != bfd_link_hash_undefweak
5494 && (h->dynindx == -1
5496 r_type = R_SH_TLS_LE_32;
5501 case R_SH_GOTOFFFUNCDESC:
5502 case R_SH_GOTOFFFUNCDESC20:
5504 case R_SH_GOTFUNCDESC:
5505 case R_SH_GOTFUNCDESC20:
5508 if (h->dynindx == -1)
5509 switch (ELF_ST_VISIBILITY (h->other))
5515 bfd_elf_link_record_dynamic_symbol (info, h);
5522 /* Some relocs require a global offset table. */
5523 if (htab->root.sgot == NULL)
5528 /* This may require an rofixup. */
5538 case R_SH_GOTFUNCDESC:
5539 case R_SH_GOTFUNCDESC20:
5540 case R_SH_GOTOFFFUNCDESC:
5541 case R_SH_GOTOFFFUNCDESC20:
5543 case R_SH_TLS_GD_32:
5544 case R_SH_TLS_LD_32:
5545 case R_SH_TLS_IE_32:
5546 if (htab->root.dynobj == NULL)
5547 htab->root.dynobj = abfd;
5548 if (!create_got_section (htab->root.dynobj, info))
5559 /* This relocation describes the C++ object vtable hierarchy.
5560 Reconstruct it for later use during GC. */
5561 case R_SH_GNU_VTINHERIT:
5562 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5566 /* This relocation describes which C++ vtable entries are actually
5567 used. Record for later use during GC. */
5568 case R_SH_GNU_VTENTRY:
5569 BFD_ASSERT (h != NULL);
5571 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5575 case R_SH_TLS_IE_32:
5576 if (bfd_link_pic (info))
5577 info->flags |= DF_STATIC_TLS;
5581 case R_SH_TLS_GD_32:
5584 case R_SH_GOTFUNCDESC:
5585 case R_SH_GOTFUNCDESC20:
5589 got_type = GOT_NORMAL;
5591 case R_SH_TLS_GD_32:
5592 got_type = GOT_TLS_GD;
5594 case R_SH_TLS_IE_32:
5595 got_type = GOT_TLS_IE;
5597 case R_SH_GOTFUNCDESC:
5598 case R_SH_GOTFUNCDESC20:
5599 got_type = GOT_FUNCDESC;
5605 h->got.refcount += 1;
5606 old_got_type = sh_elf_hash_entry (h)->got_type;
5610 bfd_signed_vma *local_got_refcounts;
5612 /* This is a global offset table entry for a local
5614 local_got_refcounts = elf_local_got_refcounts (abfd);
5615 if (local_got_refcounts == NULL)
5619 size = symtab_hdr->sh_info;
5620 size *= sizeof (bfd_signed_vma);
5621 size += symtab_hdr->sh_info;
5622 local_got_refcounts = ((bfd_signed_vma *)
5623 bfd_zalloc (abfd, size));
5624 if (local_got_refcounts == NULL)
5626 elf_local_got_refcounts (abfd) = local_got_refcounts;
5627 sh_elf_local_got_type (abfd)
5628 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
5630 local_got_refcounts[r_symndx] += 1;
5631 old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
5634 /* If a TLS symbol is accessed using IE at least once,
5635 there is no point to use dynamic model for it. */
5636 if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
5637 && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
5639 if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
5640 got_type = GOT_TLS_IE;
5643 if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
5644 && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
5646 /* xgettext:c-format */
5647 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5648 abfd, h->root.root.string);
5649 else if (old_got_type == GOT_FUNCDESC
5650 || got_type == GOT_FUNCDESC)
5652 /* xgettext:c-format */
5653 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5654 abfd, h->root.root.string);
5657 /* xgettext:c-format */
5658 (_("%pB: `%s' accessed both as normal and thread local symbol"),
5659 abfd, h->root.root.string);
5664 if (old_got_type != got_type)
5667 sh_elf_hash_entry (h)->got_type = got_type;
5669 sh_elf_local_got_type (abfd) [r_symndx] = got_type;
5674 case R_SH_TLS_LD_32:
5675 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
5679 case R_SH_GOTOFFFUNCDESC:
5680 case R_SH_GOTOFFFUNCDESC20:
5684 (_("%pB: Function descriptor relocation with non-zero addend"),
5691 union gotref *local_funcdesc;
5693 /* We need a function descriptor for a local symbol. */
5694 local_funcdesc = sh_elf_local_funcdesc (abfd);
5695 if (local_funcdesc == NULL)
5699 size = symtab_hdr->sh_info * sizeof (union gotref);
5700 local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
5701 if (local_funcdesc == NULL)
5703 sh_elf_local_funcdesc (abfd) = local_funcdesc;
5705 local_funcdesc[r_symndx].refcount += 1;
5707 if (r_type == R_SH_FUNCDESC)
5709 if (!bfd_link_pic (info))
5710 htab->srofixup->size += 4;
5712 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5717 sh_elf_hash_entry (h)->funcdesc.refcount++;
5718 if (r_type == R_SH_FUNCDESC)
5719 sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
5721 /* If there is a function descriptor reference, then
5722 there should not be any non-FDPIC references. */
5723 old_got_type = sh_elf_hash_entry (h)->got_type;
5724 if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
5726 if (old_got_type == GOT_NORMAL)
5728 /* xgettext:c-format */
5729 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5730 abfd, h->root.root.string);
5733 /* xgettext:c-format */
5734 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5735 abfd, h->root.root.string);
5741 /* If this is a local symbol, we resolve it directly without
5742 creating a procedure linkage table entry. */
5746 || ! bfd_link_pic (info)
5748 || h->dynindx == -1)
5752 h->plt.refcount += 1;
5753 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
5758 /* This symbol requires a procedure linkage table entry. We
5759 actually build the entry in adjust_dynamic_symbol,
5760 because this might be a case of linking PIC code which is
5761 never referenced by a dynamic object, in which case we
5762 don't need to generate a procedure linkage table entry
5765 /* If this is a local symbol, we resolve it directly without
5766 creating a procedure linkage table entry. */
5770 if (h->forced_local)
5774 h->plt.refcount += 1;
5779 if (h != NULL && ! bfd_link_pic (info))
5782 h->plt.refcount += 1;
5785 /* If we are creating a shared library, and this is a reloc
5786 against a global symbol, or a non PC relative reloc
5787 against a local symbol, then we need to copy the reloc
5788 into the shared library. However, if we are linking with
5789 -Bsymbolic, we do not need to copy a reloc against a
5790 global symbol which is defined in an object we are
5791 including in the link (i.e., DEF_REGULAR is set). At
5792 this point we have not seen all the input files, so it is
5793 possible that DEF_REGULAR is not set now but will be set
5794 later (it is never cleared). We account for that
5795 possibility below by storing information in the
5796 dyn_relocs field of the hash table entry. A similar
5797 situation occurs when creating shared libraries and symbol
5798 visibility changes render the symbol local.
5800 If on the other hand, we are creating an executable, we
5801 may need to keep relocations for symbols satisfied by a
5802 dynamic library if we manage to avoid copy relocs for the
5804 if ((bfd_link_pic (info)
5805 && (sec->flags & SEC_ALLOC) != 0
5806 && (r_type != R_SH_REL32
5808 && (! info->symbolic
5809 || h->root.type == bfd_link_hash_defweak
5810 || !h->def_regular))))
5811 || (! bfd_link_pic (info)
5812 && (sec->flags & SEC_ALLOC) != 0
5814 && (h->root.type == bfd_link_hash_defweak
5815 || !h->def_regular)))
5817 struct elf_dyn_relocs *p;
5818 struct elf_dyn_relocs **head;
5820 if (htab->root.dynobj == NULL)
5821 htab->root.dynobj = abfd;
5823 /* When creating a shared object, we must copy these
5824 reloc types into the output file. We create a reloc
5825 section in dynobj and make room for this reloc. */
5828 sreloc = _bfd_elf_make_dynamic_reloc_section
5829 (sec, htab->root.dynobj, 2, abfd, /*rela?*/ TRUE);
5835 /* If this is a global symbol, we count the number of
5836 relocations we need for this symbol. */
5838 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
5841 /* Track dynamic relocs needed for local syms too. */
5844 Elf_Internal_Sym *isym;
5846 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5851 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5855 vpp = &elf_section_data (s)->local_dynrel;
5856 head = (struct elf_dyn_relocs **) vpp;
5860 if (p == NULL || p->sec != sec)
5862 bfd_size_type amt = sizeof (*p);
5863 p = bfd_alloc (htab->root.dynobj, amt);
5874 if (r_type == R_SH_REL32)
5878 /* Allocate the fixup regardless of whether we need a relocation.
5879 If we end up generating the relocation, we'll unallocate the
5881 if (htab->fdpic_p && !bfd_link_pic (info)
5882 && r_type == R_SH_DIR32
5883 && (sec->flags & SEC_ALLOC) != 0)
5884 htab->srofixup->size += 4;
5887 case R_SH_TLS_LE_32:
5888 if (bfd_link_dll (info))
5891 (_("%pB: TLS local exec code cannot be linked into shared objects"),
5898 case R_SH_TLS_LDO_32:
5899 /* Nothing to do. */
5910 #ifndef sh_elf_set_mach_from_flags
5911 static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
5914 sh_elf_set_mach_from_flags (bfd *abfd)
5916 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
5918 if (flags >= ARRAY_SIZE (sh_ef_bfd_table))
5921 if (sh_ef_bfd_table[flags] == 0)
5924 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
5930 /* Reverse table lookup for sh_ef_bfd_table[].
5931 Given a bfd MACH value from archures.c
5932 return the equivalent ELF flags from the table.
5933 Return -1 if no match is found. */
5936 sh_elf_get_flags_from_mach (unsigned long mach)
5938 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
5941 if (sh_ef_bfd_table[i] == mach)
5944 /* shouldn't get here */
5949 #endif /* not sh_elf_set_mach_from_flags */
5951 #ifndef sh_elf_copy_private_data
5952 /* Copy backend specific data from one object module to another */
5955 sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
5957 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
5960 if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
5963 return sh_elf_set_mach_from_flags (obfd);
5965 #endif /* not sh_elf_copy_private_data */
5967 #ifndef sh_elf_merge_private_data
5969 /* This function returns the ELF architecture number that
5970 corresponds to the given arch_sh* flags. */
5973 sh_find_elf_flags (unsigned int arch_set)
5975 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
5976 unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
5978 return sh_elf_get_flags_from_mach (bfd_mach);
5981 /* Merge the architecture type of two BFD files, such that the
5982 resultant architecture supports all the features required
5983 by the two input BFDs.
5984 If the input BFDs are multually incompatible - i.e. one uses
5985 DSP while the other uses FPU - or there is no known architecture
5986 that fits the requirements then an error is emitted. */
5989 sh_merge_bfd_arch (bfd *ibfd, struct bfd_link_info *info)
5991 bfd *obfd = info->output_bfd;
5992 unsigned int old_arch, new_arch, merged_arch;
5994 if (! _bfd_generic_verify_endian_match (ibfd, info))
5997 old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
5998 new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
6000 merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
6002 if (!SH_VALID_CO_ARCH_SET (merged_arch))
6005 /* xgettext:c-format */
6006 (_("%pB: uses %s instructions while previous modules "
6007 "use %s instructions"),
6009 SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
6010 SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
6011 bfd_set_error (bfd_error_bad_value);
6014 else if (!SH_VALID_ARCH_SET (merged_arch))
6017 /* xgettext:c-format */
6018 (_("internal error: merge of architecture '%s' with "
6019 "architecture '%s' produced unknown architecture"),
6020 bfd_printable_name (obfd),
6021 bfd_printable_name (ibfd));
6022 bfd_set_error (bfd_error_bad_value);
6026 bfd_default_set_arch_mach (obfd, bfd_arch_sh,
6027 sh_get_bfd_mach_from_arch_set (merged_arch));
6032 /* This routine initialises the elf flags when required and
6033 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
6036 sh_elf_merge_private_data (bfd *ibfd, struct bfd_link_info *info)
6038 bfd *obfd = info->output_bfd;
6040 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6043 if (! elf_flags_init (obfd))
6045 /* This happens when ld starts out with a 'blank' output file. */
6046 elf_flags_init (obfd) = TRUE;
6047 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6048 sh_elf_set_mach_from_flags (obfd);
6049 if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC)
6050 elf_elfheader (obfd)->e_flags &= ~EF_SH_PIC;
6053 if (! sh_merge_bfd_arch (ibfd, info))
6055 _bfd_error_handler (_("%pB: uses instructions which are incompatible "
6056 "with instructions used in previous modules"),
6058 bfd_set_error (bfd_error_bad_value);
6062 elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK;
6063 elf_elfheader (obfd)->e_flags |=
6064 sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
6066 if (fdpic_object_p (ibfd) != fdpic_object_p (obfd))
6068 _bfd_error_handler (_("%pB: attempt to mix FDPIC and non-FDPIC objects"),
6070 bfd_set_error (bfd_error_bad_value);
6076 #endif /* not sh_elf_merge_private_data */
6078 /* Override the generic function because we need to store sh_elf_obj_tdata
6079 as the specific tdata. We set also the machine architecture from flags
6083 sh_elf_object_p (bfd *abfd)
6085 if (! sh_elf_set_mach_from_flags (abfd))
6088 return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0)
6089 == fdpic_object_p (abfd));
6092 /* Finish up dynamic symbol handling. We set the contents of various
6093 dynamic sections here. */
6096 sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6097 struct elf_link_hash_entry *h,
6098 Elf_Internal_Sym *sym)
6100 struct elf_sh_link_hash_table *htab;
6102 htab = sh_elf_hash_table (info);
6106 if (h->plt.offset != (bfd_vma) -1)
6114 Elf_Internal_Rela rel;
6116 const struct elf_sh_plt_info *plt_info;
6118 /* This symbol has an entry in the procedure linkage table. Set
6121 BFD_ASSERT (h->dynindx != -1);
6123 splt = htab->root.splt;
6124 sgotplt = htab->root.sgotplt;
6125 srelplt = htab->root.srelplt;
6126 BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
6128 /* Get the index in the procedure linkage table which
6129 corresponds to this symbol. This is the index of this symbol
6130 in all the symbols for which we are making plt entries. The
6131 first entry in the procedure linkage table is reserved. */
6132 plt_index = get_plt_index (htab->plt_info, h->plt.offset);
6134 plt_info = htab->plt_info;
6135 if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT)
6136 plt_info = plt_info->short_plt;
6138 /* Get the offset into the .got table of the entry that
6139 corresponds to this function. */
6141 /* The offset must be relative to the GOT symbol, twelve bytes
6142 before the end of .got.plt. Each descriptor is eight
6144 got_offset = plt_index * 8 + 12 - sgotplt->size;
6146 /* Each .got entry is 4 bytes. The first three are
6148 got_offset = (plt_index + 3) * 4;
6151 if (bfd_link_pic (info))
6152 got_offset -= GOT_BIAS;
6155 /* Fill in the entry in the procedure linkage table. */
6156 memcpy (splt->contents + h->plt.offset,
6157 plt_info->symbol_entry,
6158 plt_info->symbol_entry_size);
6160 if (bfd_link_pic (info) || htab->fdpic_p)
6162 if (plt_info->symbol_fields.got20)
6164 bfd_reloc_status_type r;
6165 r = install_movi20_field (output_bfd, got_offset,
6166 splt->owner, splt, splt->contents,
6168 + plt_info->symbol_fields.got_entry);
6169 BFD_ASSERT (r == bfd_reloc_ok);
6172 install_plt_field (output_bfd, FALSE, got_offset,
6175 + plt_info->symbol_fields.got_entry));
6179 BFD_ASSERT (!plt_info->symbol_fields.got20);
6181 install_plt_field (output_bfd, FALSE,
6182 (sgotplt->output_section->vma
6183 + sgotplt->output_offset
6187 + plt_info->symbol_fields.got_entry));
6188 if (htab->vxworks_p)
6190 unsigned int reachable_plts, plts_per_4k;
6193 /* Divide the PLT into groups. The first group contains
6194 REACHABLE_PLTS entries and the other groups contain
6195 PLTS_PER_4K entries. Entries in the first group can
6196 branch directly to .plt; those in later groups branch
6197 to the last element of the previous group. */
6198 /* ??? It would be better to create multiple copies of
6199 the common resolver stub. */
6200 reachable_plts = ((4096
6201 - plt_info->plt0_entry_size
6202 - (plt_info->symbol_fields.plt + 4))
6203 / plt_info->symbol_entry_size) + 1;
6204 plts_per_4k = (4096 / plt_info->symbol_entry_size);
6205 if (plt_index < reachable_plts)
6206 distance = -(h->plt.offset
6207 + plt_info->symbol_fields.plt);
6209 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
6210 * plt_info->symbol_entry_size);
6212 /* Install the 'bra' with this offset. */
6213 bfd_put_16 (output_bfd,
6214 0xa000 | (0x0fff & ((distance - 4) / 2)),
6217 + plt_info->symbol_fields.plt));
6220 install_plt_field (output_bfd, TRUE,
6221 splt->output_section->vma + splt->output_offset,
6224 + plt_info->symbol_fields.plt));
6227 /* Make got_offset relative to the start of .got.plt. */
6229 if (bfd_link_pic (info))
6230 got_offset += GOT_BIAS;
6233 got_offset = plt_index * 8;
6235 if (plt_info->symbol_fields.reloc_offset != MINUS_ONE)
6236 install_plt_field (output_bfd, FALSE,
6237 plt_index * sizeof (Elf32_External_Rela),
6240 + plt_info->symbol_fields.reloc_offset));
6242 /* Fill in the entry in the global offset table. */
6243 bfd_put_32 (output_bfd,
6244 (splt->output_section->vma
6245 + splt->output_offset
6247 + plt_info->symbol_resolve_offset),
6248 sgotplt->contents + got_offset);
6250 bfd_put_32 (output_bfd,
6251 sh_elf_osec_to_segment (output_bfd, splt->output_section),
6252 sgotplt->contents + got_offset + 4);
6254 /* Fill in the entry in the .rela.plt section. */
6255 rel.r_offset = (sgotplt->output_section->vma
6256 + sgotplt->output_offset
6259 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE);
6261 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6264 rel.r_addend = GOT_BIAS;
6266 loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
6267 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6269 if (htab->vxworks_p && !bfd_link_pic (info))
6271 /* Create the .rela.plt.unloaded relocations for this PLT entry.
6272 Begin by pointing LOC to the first such relocation. */
6273 loc = (htab->srelplt2->contents
6274 + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
6276 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6277 for the PLT entry's pointer to the .got.plt entry. */
6278 rel.r_offset = (splt->output_section->vma
6279 + splt->output_offset
6281 + plt_info->symbol_fields.got_entry);
6282 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6283 rel.r_addend = got_offset;
6284 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6285 loc += sizeof (Elf32_External_Rela);
6287 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6288 the .got.plt entry, which initially points to .plt. */
6289 rel.r_offset = (sgotplt->output_section->vma
6290 + sgotplt->output_offset
6292 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
6294 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6297 if (!h->def_regular)
6299 /* Mark the symbol as undefined, rather than as defined in
6300 the .plt section. Leave the value alone. */
6301 sym->st_shndx = SHN_UNDEF;
6305 if (h->got.offset != (bfd_vma) -1
6306 && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD
6307 && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE
6308 && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC)
6312 Elf_Internal_Rela rel;
6315 /* This symbol has an entry in the global offset table. Set it
6318 sgot = htab->root.sgot;
6319 srelgot = htab->root.srelgot;
6320 BFD_ASSERT (sgot != NULL && srelgot != NULL);
6322 rel.r_offset = (sgot->output_section->vma
6323 + sgot->output_offset
6324 + (h->got.offset &~ (bfd_vma) 1));
6326 /* If this is a static link, or it is a -Bsymbolic link and the
6327 symbol is defined locally or was forced to be local because
6328 of a version file, we just want to emit a RELATIVE reloc.
6329 The entry in the global offset table will already have been
6330 initialized in the relocate_section function. */
6331 if (bfd_link_pic (info)
6332 && SYMBOL_REFERENCES_LOCAL (info, h))
6336 asection *sec = h->root.u.def.section;
6338 = elf_section_data (sec->output_section)->dynindx;
6340 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6341 rel.r_addend = (h->root.u.def.value
6342 + h->root.u.def.section->output_offset);
6346 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6347 rel.r_addend = (h->root.u.def.value
6348 + h->root.u.def.section->output_section->vma
6349 + h->root.u.def.section->output_offset);
6354 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6355 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6359 loc = srelgot->contents;
6360 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6361 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6367 Elf_Internal_Rela rel;
6370 /* This symbol needs a copy reloc. Set it up. */
6372 BFD_ASSERT (h->dynindx != -1
6373 && (h->root.type == bfd_link_hash_defined
6374 || h->root.type == bfd_link_hash_defweak));
6376 s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
6377 BFD_ASSERT (s != NULL);
6379 rel.r_offset = (h->root.u.def.value
6380 + h->root.u.def.section->output_section->vma
6381 + h->root.u.def.section->output_offset);
6382 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
6384 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6385 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6388 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
6389 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6391 if (h == htab->root.hdynamic
6392 || (!htab->vxworks_p && h == htab->root.hgot))
6393 sym->st_shndx = SHN_ABS;
6398 /* Finish up the dynamic sections. */
6401 sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6403 struct elf_sh_link_hash_table *htab;
6407 htab = sh_elf_hash_table (info);
6411 sgotplt = htab->root.sgotplt;
6412 sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
6414 if (htab->root.dynamic_sections_created)
6417 Elf32_External_Dyn *dyncon, *dynconend;
6419 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6421 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6422 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6423 for (; dyncon < dynconend; dyncon++)
6425 Elf_Internal_Dyn dyn;
6428 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
6434 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
6435 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6439 BFD_ASSERT (htab->root.hgot != NULL);
6440 s = htab->root.hgot->root.u.def.section;
6441 dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value
6442 + s->output_section->vma + s->output_offset;
6443 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6447 s = htab->root.srelplt->output_section;
6448 BFD_ASSERT (s != NULL);
6449 dyn.d_un.d_ptr = s->vma;
6450 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6454 s = htab->root.srelplt->output_section;
6455 BFD_ASSERT (s != NULL);
6456 dyn.d_un.d_val = s->size;
6457 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6462 /* Fill in the first entry in the procedure linkage table. */
6463 splt = htab->root.splt;
6464 if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
6468 memcpy (splt->contents,
6469 htab->plt_info->plt0_entry,
6470 htab->plt_info->plt0_entry_size);
6471 for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
6472 if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
6473 install_plt_field (output_bfd, FALSE,
6474 (sgotplt->output_section->vma
6475 + sgotplt->output_offset
6478 + htab->plt_info->plt0_got_fields[i]));
6480 if (htab->vxworks_p)
6482 /* Finalize the .rela.plt.unloaded contents. */
6483 Elf_Internal_Rela rel;
6486 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
6487 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
6488 loc = htab->srelplt2->contents;
6489 rel.r_offset = (splt->output_section->vma
6490 + splt->output_offset
6491 + htab->plt_info->plt0_got_fields[2]);
6492 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6494 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6495 loc += sizeof (Elf32_External_Rela);
6497 /* Fix up the remaining .rela.plt.unloaded relocations.
6498 They may have the wrong symbol index for _G_O_T_ or
6499 _P_L_T_ depending on the order in which symbols were
6501 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
6503 /* The PLT entry's pointer to the .got.plt slot. */
6504 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
6505 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
6507 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6508 loc += sizeof (Elf32_External_Rela);
6510 /* The .got.plt slot's pointer to .plt. */
6511 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
6512 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
6514 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6515 loc += sizeof (Elf32_External_Rela);
6519 /* UnixWare sets the entsize of .plt to 4, although that doesn't
6520 really seem like the right value. */
6521 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
6525 /* Fill in the first three entries in the global offset table. */
6526 if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p)
6529 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6531 bfd_put_32 (output_bfd,
6532 sdyn->output_section->vma + sdyn->output_offset,
6534 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6535 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6538 if (sgotplt && sgotplt->size > 0)
6539 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6541 /* At the very end of the .rofixup section is a pointer to the GOT. */
6542 if (htab->fdpic_p && htab->srofixup != NULL)
6544 struct elf_link_hash_entry *hgot = htab->root.hgot;
6545 bfd_vma got_value = hgot->root.u.def.value
6546 + hgot->root.u.def.section->output_section->vma
6547 + hgot->root.u.def.section->output_offset;
6549 sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
6551 /* Make sure we allocated and generated the same number of fixups. */
6552 BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
6555 if (htab->srelfuncdesc)
6556 BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela)
6557 == htab->srelfuncdesc->size);
6559 if (htab->root.srelgot)
6560 BFD_ASSERT (htab->root.srelgot->reloc_count * sizeof (Elf32_External_Rela)
6561 == htab->root.srelgot->size);
6566 static enum elf_reloc_type_class
6567 sh_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
6568 const asection *rel_sec ATTRIBUTE_UNUSED,
6569 const Elf_Internal_Rela *rela)
6571 switch ((int) ELF32_R_TYPE (rela->r_info))
6574 return reloc_class_relative;
6576 return reloc_class_plt;
6578 return reloc_class_copy;
6580 return reloc_class_normal;
6584 #if !defined SH_TARGET_ALREADY_DEFINED
6585 /* Support for Linux core dump NOTE sections. */
6588 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6593 switch (note->descsz)
6598 case 168: /* Linux/SH */
6600 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
6603 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
6612 /* Make a ".reg/999" section. */
6613 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6614 size, note->descpos + offset);
6618 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6620 switch (note->descsz)
6625 case 124: /* Linux/SH elf_prpsinfo */
6626 elf_tdata (abfd)->core->program
6627 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
6628 elf_tdata (abfd)->core->command
6629 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
6632 /* Note that for some reason, a spurious space is tacked
6633 onto the end of the args in some (at least one anyway)
6634 implementations, so strip it off if it exists. */
6637 char *command = elf_tdata (abfd)->core->command;
6638 int n = strlen (command);
6640 if (0 < n && command[n - 1] == ' ')
6641 command[n - 1] = '\0';
6646 #endif /* not SH_TARGET_ALREADY_DEFINED */
6649 /* Return address for Ith PLT stub in section PLT, for relocation REL
6650 or (bfd_vma) -1 if it should not be included. */
6653 sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
6654 const arelent *rel ATTRIBUTE_UNUSED)
6656 const struct elf_sh_plt_info *plt_info;
6658 plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
6659 return plt->vma + get_plt_offset (plt_info, i);
6662 /* Decide whether to attempt to turn absptr or lsda encodings in
6663 shared libraries into pcrel within the given input section. */
6666 sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
6667 struct bfd_link_info *info,
6668 asection *eh_frame_section ATTRIBUTE_UNUSED)
6670 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
6672 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
6679 /* Adjust the contents of an eh_frame_hdr section before they're output. */
6682 sh_elf_encode_eh_address (bfd *abfd,
6683 struct bfd_link_info *info,
6684 asection *osec, bfd_vma offset,
6685 asection *loc_sec, bfd_vma loc_offset,
6688 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
6689 struct elf_link_hash_entry *h;
6692 return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
6693 loc_offset, encoded);
6695 h = htab->root.hgot;
6696 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
6698 if (! h || (sh_elf_osec_to_segment (abfd, osec)
6699 == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
6700 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
6701 loc_sec, loc_offset, encoded);
6703 BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
6704 == (sh_elf_osec_to_segment
6705 (abfd, h->root.u.def.section->output_section)));
6707 *encoded = osec->vma + offset
6708 - (h->root.u.def.value
6709 + h->root.u.def.section->output_section->vma
6710 + h->root.u.def.section->output_offset);
6712 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
6715 #if !defined SH_TARGET_ALREADY_DEFINED
6716 #define TARGET_BIG_SYM sh_elf32_vec
6717 #define TARGET_BIG_NAME "elf32-sh"
6718 #define TARGET_LITTLE_SYM sh_elf32_le_vec
6719 #define TARGET_LITTLE_NAME "elf32-shl"
6722 #define ELF_ARCH bfd_arch_sh
6723 #define ELF_TARGET_ID SH_ELF_DATA
6724 #define ELF_MACHINE_CODE EM_SH
6725 #ifdef __QNXTARGET__
6726 #define ELF_MAXPAGESIZE 0x1000
6728 #define ELF_MAXPAGESIZE 0x80
6731 #define elf_symbol_leading_char '_'
6733 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6734 #define bfd_elf32_bfd_reloc_name_lookup \
6735 sh_elf_reloc_name_lookup
6736 #define elf_info_to_howto sh_elf_info_to_howto
6737 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
6738 #define elf_backend_relocate_section sh_elf_relocate_section
6739 #define bfd_elf32_bfd_get_relocated_section_contents \
6740 sh_elf_get_relocated_section_contents
6741 #define bfd_elf32_mkobject sh_elf_mkobject
6742 #define elf_backend_object_p sh_elf_object_p
6743 #define bfd_elf32_bfd_copy_private_bfd_data \
6744 sh_elf_copy_private_data
6745 #define bfd_elf32_bfd_merge_private_bfd_data \
6746 sh_elf_merge_private_data
6748 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
6749 #define elf_backend_check_relocs sh_elf_check_relocs
6750 #define elf_backend_copy_indirect_symbol \
6751 sh_elf_copy_indirect_symbol
6752 #define elf_backend_create_dynamic_sections \
6753 sh_elf_create_dynamic_sections
6754 #define bfd_elf32_bfd_link_hash_table_create \
6755 sh_elf_link_hash_table_create
6756 #define elf_backend_adjust_dynamic_symbol \
6757 sh_elf_adjust_dynamic_symbol
6758 #define elf_backend_always_size_sections \
6759 sh_elf_always_size_sections
6760 #define elf_backend_size_dynamic_sections \
6761 sh_elf_size_dynamic_sections
6762 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
6763 #define elf_backend_finish_dynamic_symbol \
6764 sh_elf_finish_dynamic_symbol
6765 #define elf_backend_finish_dynamic_sections \
6766 sh_elf_finish_dynamic_sections
6767 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
6768 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
6769 #define elf_backend_can_make_relative_eh_frame \
6770 sh_elf_use_relative_eh_frame
6771 #define elf_backend_can_make_lsda_relative_eh_frame \
6772 sh_elf_use_relative_eh_frame
6773 #define elf_backend_encode_eh_address \
6774 sh_elf_encode_eh_address
6776 #define elf_backend_stack_align 8
6777 #define elf_backend_can_gc_sections 1
6778 #define elf_backend_can_refcount 1
6779 #define elf_backend_want_got_plt 1
6780 #define elf_backend_plt_readonly 1
6781 #define elf_backend_want_plt_sym 0
6782 #define elf_backend_got_header_size 12
6783 #define elf_backend_dtrel_excludes_plt 1
6785 #define elf_backend_linux_prpsinfo32_ugid16 TRUE
6787 #if !defined SH_TARGET_ALREADY_DEFINED
6789 #include "elf32-target.h"
6791 /* NetBSD support. */
6792 #undef TARGET_BIG_SYM
6793 #define TARGET_BIG_SYM sh_elf32_nbsd_vec
6794 #undef TARGET_BIG_NAME
6795 #define TARGET_BIG_NAME "elf32-sh-nbsd"
6796 #undef TARGET_LITTLE_SYM
6797 #define TARGET_LITTLE_SYM sh_elf32_nbsd_le_vec
6798 #undef TARGET_LITTLE_NAME
6799 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6800 #undef ELF_MAXPAGESIZE
6801 #define ELF_MAXPAGESIZE 0x10000
6802 #undef ELF_COMMONPAGESIZE
6803 #undef elf_symbol_leading_char
6804 #define elf_symbol_leading_char 0
6806 #define elf32_bed elf32_sh_nbsd_bed
6808 #include "elf32-target.h"
6811 /* Linux support. */
6812 #undef TARGET_BIG_SYM
6813 #define TARGET_BIG_SYM sh_elf32_linux_be_vec
6814 #undef TARGET_BIG_NAME
6815 #define TARGET_BIG_NAME "elf32-shbig-linux"
6816 #undef TARGET_LITTLE_SYM
6817 #define TARGET_LITTLE_SYM sh_elf32_linux_vec
6818 #undef TARGET_LITTLE_NAME
6819 #define TARGET_LITTLE_NAME "elf32-sh-linux"
6820 #undef ELF_COMMONPAGESIZE
6821 #define ELF_COMMONPAGESIZE 0x1000
6823 #undef elf_backend_grok_prstatus
6824 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6825 #undef elf_backend_grok_psinfo
6826 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
6828 #define elf32_bed elf32_sh_lin_bed
6830 #include "elf32-target.h"
6833 /* FDPIC support. */
6834 #undef TARGET_BIG_SYM
6835 #define TARGET_BIG_SYM sh_elf32_fdpic_be_vec
6836 #undef TARGET_BIG_NAME
6837 #define TARGET_BIG_NAME "elf32-shbig-fdpic"
6838 #undef TARGET_LITTLE_SYM
6839 #define TARGET_LITTLE_SYM sh_elf32_fdpic_le_vec
6840 #undef TARGET_LITTLE_NAME
6841 #define TARGET_LITTLE_NAME "elf32-sh-fdpic"
6844 #define elf32_bed elf32_sh_fd_bed
6846 #include "elf32-target.h"
6848 #undef elf_backend_modify_program_headers
6850 /* VxWorks support. */
6851 #undef TARGET_BIG_SYM
6852 #define TARGET_BIG_SYM sh_elf32_vxworks_vec
6853 #undef TARGET_BIG_NAME
6854 #define TARGET_BIG_NAME "elf32-sh-vxworks"
6855 #undef TARGET_LITTLE_SYM
6856 #define TARGET_LITTLE_SYM sh_elf32_vxworks_le_vec
6857 #undef TARGET_LITTLE_NAME
6858 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6860 #define elf32_bed elf32_sh_vxworks_bed
6862 #undef elf_backend_want_plt_sym
6863 #define elf_backend_want_plt_sym 1
6864 #undef elf_symbol_leading_char
6865 #define elf_symbol_leading_char '_'
6866 #define elf_backend_want_got_underscore 1
6867 #undef elf_backend_grok_prstatus
6868 #undef elf_backend_grok_psinfo
6869 #undef elf_backend_add_symbol_hook
6870 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6871 #undef elf_backend_link_output_symbol_hook
6872 #define elf_backend_link_output_symbol_hook \
6873 elf_vxworks_link_output_symbol_hook
6874 #undef elf_backend_emit_relocs
6875 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6876 #undef elf_backend_final_write_processing
6877 #define elf_backend_final_write_processing \
6878 elf_vxworks_final_write_processing
6879 #undef ELF_MAXPAGESIZE
6880 #define ELF_MAXPAGESIZE 0x1000
6881 #undef ELF_COMMONPAGESIZE
6883 #include "elf32-target.h"
6885 #endif /* not SH_TARGET_ALREADY_DEFINED */