1 /* Hitachi SH specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Contributed by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 static bfd_reloc_status_type sh_elf_reloc
30 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
31 static bfd_reloc_status_type sh_elf_ignore_reloc
32 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
33 static reloc_howto_type *sh_elf_reloc_type_lookup
34 PARAMS ((bfd *, bfd_reloc_code_real_type));
35 static void sh_elf_info_to_howto
36 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
37 static boolean sh_elf_set_private_flags
38 PARAMS ((bfd *, flagword));
39 static boolean sh_elf_copy_private_data
40 PARAMS ((bfd *, bfd *));
41 static boolean sh_elf_merge_private_data
42 PARAMS ((bfd *, bfd *));
43 static boolean sh_elf_set_mach_from_flags
45 static boolean sh_elf_relax_section
46 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
47 static boolean sh_elf_relax_delete_bytes
48 PARAMS ((bfd *, asection *, bfd_vma, int));
49 static boolean sh_elf_align_loads
50 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, boolean *));
51 static boolean sh_elf_swap_insns
52 PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
53 static boolean sh_elf_relocate_section
54 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
55 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
56 static bfd_byte *sh_elf_get_relocated_section_contents
57 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
58 bfd_byte *, boolean, asymbol **));
59 static boolean sh_elf_check_relocs
60 PARAMS ((bfd *, struct bfd_link_info *, asection *,
61 const Elf_Internal_Rela *));
62 static struct bfd_hash_entry *sh_elf_link_hash_newfunc
63 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
64 static struct bfd_link_hash_table *sh_elf_link_hash_table_create
66 static boolean sh_elf_adjust_dynamic_symbol
67 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
68 static boolean sh_elf_size_dynamic_sections
69 PARAMS ((bfd *, struct bfd_link_info *));
70 static boolean sh_elf_finish_dynamic_symbol
71 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
73 static boolean sh_elf_finish_dynamic_sections
74 PARAMS ((bfd *, struct bfd_link_info *));
75 static bfd_reloc_status_type sh_elf_reloc_loop
76 PARAMS ((int, bfd *, asection *, bfd_byte *, bfd_vma, asection *,
78 static boolean sh_elf_create_dynamic_sections
79 PARAMS ((bfd *, struct bfd_link_info *));
80 static asection * sh_elf_gc_mark_hook
81 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
82 struct elf_link_hash_entry *, Elf_Internal_Sym *));
83 static boolean sh_elf_gc_sweep_hook
84 PARAMS ((bfd *, struct bfd_link_info *, asection *,
85 const Elf_Internal_Rela *));
86 static enum elf_reloc_type_class sh_elf_reloc_type_class
87 PARAMS ((const Elf_Internal_Rela *));
89 /* The name of the dynamic interpreter. This is put in the .interp
92 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
94 static reloc_howto_type sh_elf_howto_table[] =
97 HOWTO (R_SH_NONE, /* type */
99 0, /* size (0 = byte, 1 = short, 2 = long) */
101 false, /* pc_relative */
103 complain_overflow_dont, /* complain_on_overflow */
104 sh_elf_ignore_reloc, /* special_function */
105 "R_SH_NONE", /* name */
106 false, /* partial_inplace */
109 false), /* pcrel_offset */
111 /* 32 bit absolute relocation. Setting partial_inplace to true and
112 src_mask to a non-zero value is similar to the COFF toolchain. */
113 HOWTO (R_SH_DIR32, /* type */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
117 false, /* pc_relative */
119 complain_overflow_bitfield, /* complain_on_overflow */
120 sh_elf_reloc, /* special_function */
121 "R_SH_DIR32", /* name */
122 true, /* partial_inplace */
123 0xffffffff, /* src_mask */
124 0xffffffff, /* dst_mask */
125 false), /* pcrel_offset */
127 /* 32 bit PC relative relocation. */
128 HOWTO (R_SH_REL32, /* type */
130 2, /* size (0 = byte, 1 = short, 2 = long) */
132 true, /* pc_relative */
134 complain_overflow_signed, /* complain_on_overflow */
135 sh_elf_ignore_reloc, /* special_function */
136 "R_SH_REL32", /* name */
137 true, /* partial_inplace */
138 0xffffffff, /* src_mask */
139 0xffffffff, /* dst_mask */
140 true), /* pcrel_offset */
142 /* 8 bit PC relative branch divided by 2. */
143 HOWTO (R_SH_DIR8WPN, /* type */
145 1, /* size (0 = byte, 1 = short, 2 = long) */
147 true, /* pc_relative */
149 complain_overflow_signed, /* complain_on_overflow */
150 sh_elf_ignore_reloc, /* special_function */
151 "R_SH_DIR8WPN", /* name */
152 true, /* partial_inplace */
155 true), /* pcrel_offset */
157 /* 12 bit PC relative branch divided by 2. */
158 HOWTO (R_SH_IND12W, /* type */
160 1, /* size (0 = byte, 1 = short, 2 = long) */
162 true, /* pc_relative */
164 complain_overflow_signed, /* complain_on_overflow */
165 sh_elf_reloc, /* special_function */
166 "R_SH_IND12W", /* name */
167 true, /* partial_inplace */
168 0xfff, /* src_mask */
169 0xfff, /* dst_mask */
170 true), /* pcrel_offset */
172 /* 8 bit unsigned PC relative divided by 4. */
173 HOWTO (R_SH_DIR8WPL, /* type */
175 1, /* size (0 = byte, 1 = short, 2 = long) */
177 true, /* pc_relative */
179 complain_overflow_unsigned, /* complain_on_overflow */
180 sh_elf_ignore_reloc, /* special_function */
181 "R_SH_DIR8WPL", /* name */
182 true, /* partial_inplace */
185 true), /* pcrel_offset */
187 /* 8 bit unsigned PC relative divided by 2. */
188 HOWTO (R_SH_DIR8WPZ, /* type */
190 1, /* size (0 = byte, 1 = short, 2 = long) */
192 true, /* pc_relative */
194 complain_overflow_unsigned, /* complain_on_overflow */
195 sh_elf_ignore_reloc, /* special_function */
196 "R_SH_DIR8WPZ", /* name */
197 true, /* partial_inplace */
200 true), /* pcrel_offset */
202 /* 8 bit GBR relative. FIXME: This only makes sense if we have some
203 special symbol for the GBR relative area, and that is not
205 HOWTO (R_SH_DIR8BP, /* type */
207 1, /* size (0 = byte, 1 = short, 2 = long) */
209 false, /* pc_relative */
211 complain_overflow_unsigned, /* complain_on_overflow */
212 sh_elf_ignore_reloc, /* special_function */
213 "R_SH_DIR8BP", /* name */
214 false, /* partial_inplace */
217 true), /* pcrel_offset */
219 /* 8 bit GBR relative divided by 2. FIXME: This only makes sense if
220 we have some special symbol for the GBR relative area, and that
221 is not implemented. */
222 HOWTO (R_SH_DIR8W, /* type */
224 1, /* size (0 = byte, 1 = short, 2 = long) */
226 false, /* pc_relative */
228 complain_overflow_unsigned, /* complain_on_overflow */
229 sh_elf_ignore_reloc, /* special_function */
230 "R_SH_DIR8W", /* name */
231 false, /* partial_inplace */
234 true), /* pcrel_offset */
236 /* 8 bit GBR relative divided by 4. FIXME: This only makes sense if
237 we have some special symbol for the GBR relative area, and that
238 is not implemented. */
239 HOWTO (R_SH_DIR8L, /* type */
241 1, /* size (0 = byte, 1 = short, 2 = long) */
243 false, /* pc_relative */
245 complain_overflow_unsigned, /* complain_on_overflow */
246 sh_elf_ignore_reloc, /* special_function */
247 "R_SH_DIR8L", /* name */
248 false, /* partial_inplace */
251 true), /* pcrel_offset */
269 /* The remaining relocs are a GNU extension used for relaxing. The
270 final pass of the linker never needs to do anything with any of
271 these relocs. Any required operations are handled by the
274 /* A 16 bit switch table entry. This is generated for an expression
275 such as ``.word L1 - L2''. The offset holds the difference
276 between the reloc address and L2. */
277 HOWTO (R_SH_SWITCH16, /* type */
279 1, /* size (0 = byte, 1 = short, 2 = long) */
281 false, /* pc_relative */
283 complain_overflow_unsigned, /* complain_on_overflow */
284 sh_elf_ignore_reloc, /* special_function */
285 "R_SH_SWITCH16", /* name */
286 false, /* partial_inplace */
289 true), /* pcrel_offset */
291 /* A 32 bit switch table entry. This is generated for an expression
292 such as ``.long L1 - L2''. The offset holds the difference
293 between the reloc address and L2. */
294 HOWTO (R_SH_SWITCH32, /* type */
296 2, /* size (0 = byte, 1 = short, 2 = long) */
298 false, /* pc_relative */
300 complain_overflow_unsigned, /* complain_on_overflow */
301 sh_elf_ignore_reloc, /* special_function */
302 "R_SH_SWITCH32", /* name */
303 false, /* partial_inplace */
306 true), /* pcrel_offset */
308 /* Indicates a .uses pseudo-op. The compiler will generate .uses
309 pseudo-ops when it finds a function call which can be relaxed.
310 The offset field holds the PC relative offset to the instruction
311 which loads the register used in the function call. */
312 HOWTO (R_SH_USES, /* type */
314 1, /* size (0 = byte, 1 = short, 2 = long) */
316 false, /* pc_relative */
318 complain_overflow_unsigned, /* complain_on_overflow */
319 sh_elf_ignore_reloc, /* special_function */
320 "R_SH_USES", /* name */
321 false, /* partial_inplace */
324 true), /* pcrel_offset */
326 /* The assembler will generate this reloc for addresses referred to
327 by the register loads associated with USES relocs. The offset
328 field holds the number of times the address is referenced in the
330 HOWTO (R_SH_COUNT, /* type */
332 1, /* size (0 = byte, 1 = short, 2 = long) */
334 false, /* pc_relative */
336 complain_overflow_unsigned, /* complain_on_overflow */
337 sh_elf_ignore_reloc, /* special_function */
338 "R_SH_COUNT", /* name */
339 false, /* partial_inplace */
342 true), /* pcrel_offset */
344 /* Indicates an alignment statement. The offset field is the power
345 of 2 to which subsequent portions of the object file must be
347 HOWTO (R_SH_ALIGN, /* type */
349 1, /* size (0 = byte, 1 = short, 2 = long) */
351 false, /* pc_relative */
353 complain_overflow_unsigned, /* complain_on_overflow */
354 sh_elf_ignore_reloc, /* special_function */
355 "R_SH_ALIGN", /* name */
356 false, /* partial_inplace */
359 true), /* pcrel_offset */
361 /* The assembler will generate this reloc before a block of
362 instructions. A section should be processed as assumining it
363 contains data, unless this reloc is seen. */
364 HOWTO (R_SH_CODE, /* type */
366 1, /* size (0 = byte, 1 = short, 2 = long) */
368 false, /* pc_relative */
370 complain_overflow_unsigned, /* complain_on_overflow */
371 sh_elf_ignore_reloc, /* special_function */
372 "R_SH_CODE", /* name */
373 false, /* partial_inplace */
376 true), /* pcrel_offset */
378 /* The assembler will generate this reloc after a block of
379 instructions when it sees data that is not instructions. */
380 HOWTO (R_SH_DATA, /* type */
382 1, /* size (0 = byte, 1 = short, 2 = long) */
384 false, /* pc_relative */
386 complain_overflow_unsigned, /* complain_on_overflow */
387 sh_elf_ignore_reloc, /* special_function */
388 "R_SH_DATA", /* name */
389 false, /* partial_inplace */
392 true), /* pcrel_offset */
394 /* The assembler generates this reloc for each label within a block
395 of instructions. This permits the linker to avoid swapping
396 instructions which are the targets of branches. */
397 HOWTO (R_SH_LABEL, /* type */
399 1, /* size (0 = byte, 1 = short, 2 = long) */
401 false, /* pc_relative */
403 complain_overflow_unsigned, /* complain_on_overflow */
404 sh_elf_ignore_reloc, /* special_function */
405 "R_SH_LABEL", /* name */
406 false, /* partial_inplace */
409 true), /* pcrel_offset */
411 /* An 8 bit switch table entry. This is generated for an expression
412 such as ``.word L1 - L2''. The offset holds the difference
413 between the reloc address and L2. */
414 HOWTO (R_SH_SWITCH8, /* type */
416 0, /* size (0 = byte, 1 = short, 2 = long) */
418 false, /* pc_relative */
420 complain_overflow_unsigned, /* complain_on_overflow */
421 sh_elf_ignore_reloc, /* special_function */
422 "R_SH_SWITCH8", /* name */
423 false, /* partial_inplace */
426 true), /* pcrel_offset */
428 /* GNU extension to record C++ vtable hierarchy */
429 HOWTO (R_SH_GNU_VTINHERIT, /* type */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
433 false, /* pc_relative */
435 complain_overflow_dont, /* complain_on_overflow */
436 NULL, /* special_function */
437 "R_SH_GNU_VTINHERIT", /* name */
438 false, /* partial_inplace */
441 false), /* pcrel_offset */
443 /* GNU extension to record C++ vtable member usage */
444 HOWTO (R_SH_GNU_VTENTRY, /* type */
446 2, /* size (0 = byte, 1 = short, 2 = long) */
448 false, /* pc_relative */
450 complain_overflow_dont, /* complain_on_overflow */
451 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
452 "R_SH_GNU_VTENTRY", /* name */
453 false, /* partial_inplace */
456 false), /* pcrel_offset */
458 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
459 HOWTO (R_SH_LOOP_START, /* type */
461 1, /* size (0 = byte, 1 = short, 2 = long) */
463 false, /* pc_relative */
465 complain_overflow_signed, /* complain_on_overflow */
466 sh_elf_ignore_reloc, /* special_function */
467 "R_SH_LOOP_START", /* name */
468 true, /* partial_inplace */
471 true), /* pcrel_offset */
473 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
474 HOWTO (R_SH_LOOP_END, /* type */
476 1, /* size (0 = byte, 1 = short, 2 = long) */
478 false, /* pc_relative */
480 complain_overflow_signed, /* complain_on_overflow */
481 sh_elf_ignore_reloc, /* special_function */
482 "R_SH_LOOP_END", /* name */
483 true, /* partial_inplace */
486 true), /* pcrel_offset */
611 HOWTO (R_SH_GOT32, /* type */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
615 false, /* pc_relative */
617 complain_overflow_bitfield, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* */
619 "R_SH_GOT32", /* name */
620 true, /* partial_inplace */
621 0xffffffff, /* src_mask */
622 0xffffffff, /* dst_mask */
623 false), /* pcrel_offset */
625 HOWTO (R_SH_PLT32, /* type */
627 2, /* size (0 = byte, 1 = short, 2 = long) */
629 true, /* pc_relative */
631 complain_overflow_bitfield, /* complain_on_overflow */
632 bfd_elf_generic_reloc, /* */
633 "R_SH_PLT32", /* name */
634 true, /* partial_inplace */
635 0xffffffff, /* src_mask */
636 0xffffffff, /* dst_mask */
637 true), /* pcrel_offset */
639 HOWTO (R_SH_COPY, /* type */
641 2, /* size (0 = byte, 1 = short, 2 = long) */
643 false, /* pc_relative */
645 complain_overflow_bitfield, /* complain_on_overflow */
646 bfd_elf_generic_reloc, /* */
647 "R_SH_COPY", /* name */
648 true, /* partial_inplace */
649 0xffffffff, /* src_mask */
650 0xffffffff, /* dst_mask */
651 false), /* pcrel_offset */
653 HOWTO (R_SH_GLOB_DAT, /* type */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
657 false, /* pc_relative */
659 complain_overflow_bitfield, /* complain_on_overflow */
660 bfd_elf_generic_reloc, /* */
661 "R_SH_GLOB_DAT", /* name */
662 true, /* partial_inplace */
663 0xffffffff, /* src_mask */
664 0xffffffff, /* dst_mask */
665 false), /* pcrel_offset */
667 HOWTO (R_SH_JMP_SLOT, /* type */
669 2, /* size (0 = byte, 1 = short, 2 = long) */
671 false, /* pc_relative */
673 complain_overflow_bitfield, /* complain_on_overflow */
674 bfd_elf_generic_reloc, /* */
675 "R_SH_JMP_SLOT", /* name */
676 true, /* partial_inplace */
677 0xffffffff, /* src_mask */
678 0xffffffff, /* dst_mask */
679 false), /* pcrel_offset */
681 HOWTO (R_SH_RELATIVE, /* type */
683 2, /* size (0 = byte, 1 = short, 2 = long) */
685 false, /* pc_relative */
687 complain_overflow_bitfield, /* complain_on_overflow */
688 bfd_elf_generic_reloc, /* */
689 "R_SH_RELATIVE", /* name */
690 true, /* partial_inplace */
691 0xffffffff, /* src_mask */
692 0xffffffff, /* dst_mask */
693 false), /* pcrel_offset */
695 HOWTO (R_SH_GOTOFF, /* type */
697 2, /* size (0 = byte, 1 = short, 2 = long) */
699 false, /* pc_relative */
701 complain_overflow_bitfield, /* complain_on_overflow */
702 bfd_elf_generic_reloc, /* */
703 "R_SH_GOTOFF", /* name */
704 true, /* partial_inplace */
705 0xffffffff, /* src_mask */
706 0xffffffff, /* dst_mask */
707 false), /* pcrel_offset */
709 HOWTO (R_SH_GOTPC, /* type */
711 2, /* size (0 = byte, 1 = short, 2 = long) */
713 true, /* pc_relative */
715 complain_overflow_bitfield, /* complain_on_overflow */
716 bfd_elf_generic_reloc, /* */
717 "R_SH_GOTPC", /* name */
718 true, /* partial_inplace */
719 0xffffffff, /* src_mask */
720 0xffffffff, /* dst_mask */
721 true), /* pcrel_offset */
725 static bfd_reloc_status_type
726 sh_elf_reloc_loop (r_type, input_bfd, input_section, contents, addr,
727 symbol_section, start, end)
728 int r_type ATTRIBUTE_UNUSED;
730 asection *input_section;
733 asection *symbol_section;
736 static bfd_vma last_addr;
737 static asection *last_symbol_section;
738 bfd_byte *free_contents = NULL;
739 bfd_byte *start_ptr, *ptr, *last_ptr;
744 /* Sanity check the address. */
745 if (addr > input_section->_raw_size)
746 return bfd_reloc_outofrange;
748 /* We require the start and end relocations to be processed consecutively -
749 although we allow then to be processed forwards or backwards. */
753 last_symbol_section = symbol_section;
756 if (last_addr != addr)
760 if (! symbol_section || last_symbol_section != symbol_section || end < start)
761 return bfd_reloc_outofrange;
763 /* Get the symbol_section contents. */
764 if (symbol_section != input_section)
766 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
767 contents = elf_section_data (symbol_section)->this_hdr.contents;
770 contents = (bfd_byte *) bfd_malloc (symbol_section->_raw_size);
771 if (contents == NULL)
772 return bfd_reloc_outofrange;
773 free_contents = contents;
774 if (! bfd_get_section_contents (input_bfd, symbol_section, contents,
776 symbol_section->_raw_size))
779 return bfd_reloc_outofrange;
783 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
784 start_ptr = contents + start;
785 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
787 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
790 diff = (last_ptr - ptr) >> 1;
791 cum_diff += diff & 1;
794 /* Calculate the start / end values to load into rs / re minus four -
795 so that will cancel out the four we would otherwise have to add to
796 addr to get the value to subtract in order to get relative addressing. */
800 end = (ptr + cum_diff * 2) - contents;
804 bfd_vma start0 = start - 4;
806 while (start0 && IS_PPI (contents + start0))
808 start0 = start - 2 - ((start - start0) & 2);
809 start = start0 - cum_diff - 2;
814 free (free_contents);
816 insn = bfd_get_16 (input_bfd, contents + addr);
818 x = (insn & 0x200 ? end : start) - addr;
819 if (input_section != symbol_section)
820 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
821 - (input_section->output_section->vma
822 + input_section->output_offset));
824 if (x < -128 || x > 127)
825 return bfd_reloc_overflow;
827 x = (insn & ~0xff) | (x & 0xff);
828 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
833 /* This function is used for normal relocs. This used to be like the COFF
834 function, and is almost certainly incorrect for other ELF targets. */
836 static bfd_reloc_status_type
837 sh_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
840 arelent *reloc_entry;
843 asection *input_section;
845 char **error_message ATTRIBUTE_UNUSED;
849 enum elf_sh_reloc_type r_type;
850 bfd_vma addr = reloc_entry->address;
851 bfd_byte *hit_data = addr + (bfd_byte *) data;
853 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
855 if (output_bfd != NULL)
857 /* Partial linking--do nothing. */
858 reloc_entry->address += input_section->output_offset;
862 /* Almost all relocs have to do with relaxing. If any work must be
863 done for them, it has been done in sh_relax_section. */
864 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
867 if (symbol_in != NULL
868 && bfd_is_und_section (symbol_in->section))
869 return bfd_reloc_undefined;
871 if (bfd_is_com_section (symbol_in->section))
874 sym_value = (symbol_in->value +
875 symbol_in->section->output_section->vma +
876 symbol_in->section->output_offset);
881 insn = bfd_get_32 (abfd, hit_data);
882 insn += sym_value + reloc_entry->addend;
883 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
886 insn = bfd_get_16 (abfd, hit_data);
887 sym_value += reloc_entry->addend;
888 sym_value -= (input_section->output_section->vma
889 + input_section->output_offset
892 sym_value += (insn & 0xfff) << 1;
895 insn = (insn & 0xf000) | (sym_value & 0xfff);
896 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
897 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
898 return bfd_reloc_overflow;
908 /* This function is used for relocs which are only used for relaxing,
909 which the linker should otherwise ignore. */
911 static bfd_reloc_status_type
912 sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
913 output_bfd, error_message)
914 bfd *abfd ATTRIBUTE_UNUSED;
915 arelent *reloc_entry;
916 asymbol *symbol ATTRIBUTE_UNUSED;
917 PTR data ATTRIBUTE_UNUSED;
918 asection *input_section;
920 char **error_message ATTRIBUTE_UNUSED;
922 if (output_bfd != NULL)
923 reloc_entry->address += input_section->output_offset;
927 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
931 bfd_reloc_code_real_type bfd_reloc_val;
932 unsigned char elf_reloc_val;
935 /* An array mapping BFD reloc codes to SH ELF relocs. */
937 static const struct elf_reloc_map sh_reloc_map[] =
939 { BFD_RELOC_NONE, R_SH_NONE },
940 { BFD_RELOC_32, R_SH_DIR32 },
941 { BFD_RELOC_CTOR, R_SH_DIR32 },
942 { BFD_RELOC_32_PCREL, R_SH_REL32 },
943 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
944 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
945 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
946 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
947 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
948 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
949 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
950 { BFD_RELOC_SH_USES, R_SH_USES },
951 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
952 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
953 { BFD_RELOC_SH_CODE, R_SH_CODE },
954 { BFD_RELOC_SH_DATA, R_SH_DATA },
955 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
956 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
957 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
958 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
959 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
960 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
961 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
962 { BFD_RELOC_SH_COPY, R_SH_COPY },
963 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
964 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
965 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
966 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
967 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
970 /* Given a BFD reloc code, return the howto structure for the
971 corresponding SH ELf reloc. */
973 static reloc_howto_type *
974 sh_elf_reloc_type_lookup (abfd, code)
975 bfd *abfd ATTRIBUTE_UNUSED;
976 bfd_reloc_code_real_type code;
980 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
982 if (sh_reloc_map[i].bfd_reloc_val == code)
983 return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
989 /* Given an ELF reloc, fill in the howto field of a relent. */
992 sh_elf_info_to_howto (abfd, cache_ptr, dst)
993 bfd *abfd ATTRIBUTE_UNUSED;
995 Elf_Internal_Rela *dst;
999 r = ELF32_R_TYPE (dst->r_info);
1001 BFD_ASSERT (r < (unsigned int) R_SH_max);
1002 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
1003 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
1005 cache_ptr->howto = &sh_elf_howto_table[r];
1008 /* This function handles relaxing for SH ELF. See the corresponding
1009 function in coff-sh.c for a description of what this does. FIXME:
1010 There is a lot of duplication here between this code and the COFF
1011 specific code. The format of relocs and symbols is wound deeply
1012 into this code, but it would still be better if the duplication
1013 could be eliminated somehow. Note in particular that although both
1014 functions use symbols like R_SH_CODE, those symbols have different
1015 values; in coff-sh.c they come from include/coff/sh.h, whereas here
1016 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
1019 sh_elf_relax_section (abfd, sec, link_info, again)
1022 struct bfd_link_info *link_info;
1025 Elf_Internal_Shdr *symtab_hdr;
1026 Elf_Internal_Rela *internal_relocs;
1027 Elf_Internal_Rela *free_relocs = NULL;
1029 Elf_Internal_Rela *irel, *irelend;
1030 bfd_byte *contents = NULL;
1031 bfd_byte *free_contents = NULL;
1032 Elf32_External_Sym *extsyms = NULL;
1033 Elf32_External_Sym *free_extsyms = NULL;
1037 if (link_info->relocateable
1038 || (sec->flags & SEC_RELOC) == 0
1039 || sec->reloc_count == 0)
1042 /* If this is the first time we have been called for this section,
1043 initialize the cooked size. */
1044 if (sec->_cooked_size == 0)
1045 sec->_cooked_size = sec->_raw_size;
1047 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1049 internal_relocs = (_bfd_elf32_link_read_relocs
1050 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1051 link_info->keep_memory));
1052 if (internal_relocs == NULL)
1054 if (! link_info->keep_memory)
1055 free_relocs = internal_relocs;
1059 irelend = internal_relocs + sec->reloc_count;
1060 for (irel = internal_relocs; irel < irelend; irel++)
1062 bfd_vma laddr, paddr, symval;
1063 unsigned short insn;
1064 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
1065 bfd_signed_vma foff;
1067 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
1070 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
1073 /* Get the section contents. */
1074 if (contents == NULL)
1076 if (elf_section_data (sec)->this_hdr.contents != NULL)
1077 contents = elf_section_data (sec)->this_hdr.contents;
1080 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1081 if (contents == NULL)
1083 free_contents = contents;
1085 if (! bfd_get_section_contents (abfd, sec, contents,
1086 (file_ptr) 0, sec->_raw_size))
1091 /* The r_addend field of the R_SH_USES reloc will point us to
1092 the register load. The 4 is because the r_addend field is
1093 computed as though it were a jump offset, which are based
1094 from 4 bytes after the jump instruction. */
1095 laddr = irel->r_offset + 4 + irel->r_addend;
1096 if (laddr >= sec->_raw_size)
1098 (*_bfd_error_handler) (_("%s: 0x%lx: warning: bad R_SH_USES offset"),
1099 bfd_archive_filename (abfd),
1100 (unsigned long) irel->r_offset);
1103 insn = bfd_get_16 (abfd, contents + laddr);
1105 /* If the instruction is not mov.l NN,rN, we don't know what to
1107 if ((insn & 0xf000) != 0xd000)
1109 ((*_bfd_error_handler)
1110 (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
1111 bfd_archive_filename (abfd), (unsigned long) irel->r_offset, insn));
1115 /* Get the address from which the register is being loaded. The
1116 displacement in the mov.l instruction is quadrupled. It is a
1117 displacement from four bytes after the movl instruction, but,
1118 before adding in the PC address, two least significant bits
1119 of the PC are cleared. We assume that the section is aligned
1120 on a four byte boundary. */
1121 paddr = insn & 0xff;
1123 paddr += (laddr + 4) &~ (bfd_vma) 3;
1124 if (paddr >= sec->_raw_size)
1126 ((*_bfd_error_handler)
1127 (_("%s: 0x%lx: warning: bad R_SH_USES load offset"),
1128 bfd_archive_filename (abfd), (unsigned long) irel->r_offset));
1132 /* Get the reloc for the address from which the register is
1133 being loaded. This reloc will tell us which function is
1134 actually being called. */
1135 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
1136 if (irelfn->r_offset == paddr
1137 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
1139 if (irelfn >= irelend)
1141 ((*_bfd_error_handler)
1142 (_("%s: 0x%lx: warning: could not find expected reloc"),
1143 bfd_archive_filename (abfd), (unsigned long) paddr));
1147 /* Read this BFD's symbols if we haven't done so already. */
1148 if (extsyms == NULL)
1150 if (symtab_hdr->contents != NULL)
1151 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1154 extsyms = (Elf32_External_Sym *) bfd_malloc (symtab_hdr->sh_size);
1155 if (extsyms == NULL)
1157 free_extsyms = extsyms;
1158 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1159 || (bfd_bread (extsyms, symtab_hdr->sh_size, abfd)
1160 != symtab_hdr->sh_size))
1165 /* Get the value of the symbol referred to by the reloc. */
1166 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
1168 Elf_Internal_Sym isym;
1170 /* A local symbol. */
1171 bfd_elf32_swap_symbol_in (abfd,
1172 extsyms + ELF32_R_SYM (irelfn->r_info),
1175 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
1177 ((*_bfd_error_handler)
1178 (_("%s: 0x%lx: warning: symbol in unexpected section"),
1179 bfd_archive_filename (abfd), (unsigned long) paddr));
1183 symval = (isym.st_value
1184 + sec->output_section->vma
1185 + sec->output_offset);
1190 struct elf_link_hash_entry *h;
1192 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
1193 h = elf_sym_hashes (abfd)[indx];
1194 BFD_ASSERT (h != NULL);
1195 if (h->root.type != bfd_link_hash_defined
1196 && h->root.type != bfd_link_hash_defweak)
1198 /* This appears to be a reference to an undefined
1199 symbol. Just ignore it--it will be caught by the
1200 regular reloc processing. */
1204 symval = (h->root.u.def.value
1205 + h->root.u.def.section->output_section->vma
1206 + h->root.u.def.section->output_offset);
1209 symval += bfd_get_32 (abfd, contents + paddr);
1211 /* See if this function call can be shortened. */
1214 + sec->output_section->vma
1215 + sec->output_offset
1217 if (foff < -0x1000 || foff >= 0x1000)
1219 /* After all that work, we can't shorten this function call. */
1223 /* Shorten the function call. */
1225 /* For simplicity of coding, we are going to modify the section
1226 contents, the section relocs, and the BFD symbol table. We
1227 must tell the rest of the code not to free up this
1228 information. It would be possible to instead create a table
1229 of changes which have to be made, as is done in coff-mips.c;
1230 that would be more work, but would require less memory when
1231 the linker is run. */
1233 elf_section_data (sec)->relocs = internal_relocs;
1236 elf_section_data (sec)->this_hdr.contents = contents;
1237 free_contents = NULL;
1239 symtab_hdr->contents = (bfd_byte *) extsyms;
1240 free_extsyms = NULL;
1242 /* Replace the jsr with a bsr. */
1244 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
1245 replace the jsr with a bsr. */
1246 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
1247 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
1249 /* If this needs to be changed because of future relaxing,
1250 it will be handled here like other internal IND12W
1253 (bfd_vma) 0xb000 | ((foff >> 1) & 0xfff),
1254 contents + irel->r_offset);
1258 /* We can't fully resolve this yet, because the external
1259 symbol value may be changed by future relaxing. We let
1260 the final link phase handle it. */
1261 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
1264 /* See if there is another R_SH_USES reloc referring to the same
1266 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
1267 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
1268 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
1270 if (irelscan < irelend)
1272 /* Some other function call depends upon this register load,
1273 and we have not yet converted that function call.
1274 Indeed, we may never be able to convert it. There is
1275 nothing else we can do at this point. */
1279 /* Look for a R_SH_COUNT reloc on the location where the
1280 function address is stored. Do this before deleting any
1281 bytes, to avoid confusion about the address. */
1282 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
1283 if (irelcount->r_offset == paddr
1284 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
1287 /* Delete the register load. */
1288 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
1291 /* That will change things, so, just in case it permits some
1292 other function call to come within range, we should relax
1293 again. Note that this is not required, and it may be slow. */
1296 /* Now check whether we got a COUNT reloc. */
1297 if (irelcount >= irelend)
1299 ((*_bfd_error_handler)
1300 (_("%s: 0x%lx: warning: could not find expected COUNT reloc"),
1301 bfd_archive_filename (abfd), (unsigned long) paddr));
1305 /* The number of uses is stored in the r_addend field. We've
1306 just deleted one. */
1307 if (irelcount->r_addend == 0)
1309 ((*_bfd_error_handler) (_("%s: 0x%lx: warning: bad count"),
1310 bfd_archive_filename (abfd),
1311 (unsigned long) paddr));
1315 --irelcount->r_addend;
1317 /* If there are no more uses, we can delete the address. Reload
1318 the address from irelfn, in case it was changed by the
1319 previous call to sh_elf_relax_delete_bytes. */
1320 if (irelcount->r_addend == 0)
1322 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
1326 /* We've done all we can with that function call. */
1329 /* Look for load and store instructions that we can align on four
1335 /* Get the section contents. */
1336 if (contents == NULL)
1338 if (elf_section_data (sec)->this_hdr.contents != NULL)
1339 contents = elf_section_data (sec)->this_hdr.contents;
1342 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1343 if (contents == NULL)
1345 free_contents = contents;
1347 if (! bfd_get_section_contents (abfd, sec, contents,
1348 (file_ptr) 0, sec->_raw_size))
1353 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
1359 elf_section_data (sec)->relocs = internal_relocs;
1362 elf_section_data (sec)->this_hdr.contents = contents;
1363 free_contents = NULL;
1365 symtab_hdr->contents = (bfd_byte *) extsyms;
1366 free_extsyms = NULL;
1370 if (free_relocs != NULL)
1376 if (free_contents != NULL)
1378 if (! link_info->keep_memory)
1379 free (free_contents);
1382 /* Cache the section contents for elf_link_input_bfd. */
1383 elf_section_data (sec)->this_hdr.contents = contents;
1385 free_contents = NULL;
1388 if (free_extsyms != NULL)
1390 if (! link_info->keep_memory)
1391 free (free_extsyms);
1394 /* Cache the symbols for elf_link_input_bfd. */
1395 symtab_hdr->contents = (unsigned char *) extsyms;
1397 free_extsyms = NULL;
1403 if (free_relocs != NULL)
1405 if (free_contents != NULL)
1406 free (free_contents);
1407 if (free_extsyms != NULL)
1408 free (free_extsyms);
1412 /* Delete some bytes from a section while relaxing. FIXME: There is a
1413 lot of duplication between this function and sh_relax_delete_bytes
1417 sh_elf_relax_delete_bytes (abfd, sec, addr, count)
1423 Elf_Internal_Shdr *symtab_hdr;
1424 Elf32_External_Sym *extsyms;
1427 Elf_Internal_Rela *irel, *irelend;
1428 Elf_Internal_Rela *irelalign;
1430 Elf32_External_Sym *esym, *esymend;
1431 struct elf_link_hash_entry *sym_hash;
1434 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1435 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1437 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1439 contents = elf_section_data (sec)->this_hdr.contents;
1441 /* The deletion must stop at the next ALIGN reloc for an aligment
1442 power larger than the number of bytes we are deleting. */
1445 toaddr = sec->_cooked_size;
1447 irel = elf_section_data (sec)->relocs;
1448 irelend = irel + sec->reloc_count;
1449 for (; irel < irelend; irel++)
1451 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1452 && irel->r_offset > addr
1453 && count < (1 << irel->r_addend))
1456 toaddr = irel->r_offset;
1461 /* Actually delete the bytes. */
1462 memmove (contents + addr, contents + addr + count,
1463 (size_t) (toaddr - addr - count));
1464 if (irelalign == NULL)
1465 sec->_cooked_size -= count;
1470 #define NOP_OPCODE (0x0009)
1472 BFD_ASSERT ((count & 1) == 0);
1473 for (i = 0; i < count; i += 2)
1474 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
1477 /* Adjust all the relocs. */
1478 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1480 bfd_vma nraddr, stop;
1483 Elf_Internal_Sym sym;
1484 int off, adjust, oinsn;
1485 bfd_signed_vma voff = 0;
1488 /* Get the new reloc address. */
1489 nraddr = irel->r_offset;
1490 if ((irel->r_offset > addr
1491 && irel->r_offset < toaddr)
1492 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1493 && irel->r_offset == toaddr))
1496 /* See if this reloc was for the bytes we have deleted, in which
1497 case we no longer care about it. Don't delete relocs which
1498 represent addresses, though. */
1499 if (irel->r_offset >= addr
1500 && irel->r_offset < addr + count
1501 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
1502 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
1503 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
1504 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
1505 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1508 /* If this is a PC relative reloc, see if the range it covers
1509 includes the bytes we have deleted. */
1510 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1519 start = irel->r_offset;
1520 insn = bfd_get_16 (abfd, contents + nraddr);
1524 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1527 start = stop = addr;
1531 /* If this reloc is against a symbol defined in this
1532 section, and the symbol will not be adjusted below, we
1533 must check the addend to see it will put the value in
1534 range to be adjusted, and hence must be changed. */
1535 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1537 bfd_elf32_swap_symbol_in (abfd,
1538 extsyms + ELF32_R_SYM (irel->r_info),
1540 if (sym.st_shndx == shndx
1541 && (sym.st_value <= addr
1542 || sym.st_value >= toaddr))
1546 val = bfd_get_32 (abfd, contents + nraddr);
1547 val += sym.st_value;
1548 if (val > addr && val < toaddr)
1549 bfd_put_32 (abfd, val - count, contents + nraddr);
1552 start = stop = addr;
1559 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1563 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
1564 start = stop = addr;
1570 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1576 stop = start + 4 + off * 2;
1581 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
1587 /* These relocs types represent
1589 The r_addend field holds the difference between the reloc
1590 address and L1. That is the start of the reloc, and
1591 adding in the contents gives us the top. We must adjust
1592 both the r_offset field and the section contents.
1593 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1594 and the elf bfd r_offset is called r_vaddr. */
1596 stop = irel->r_offset;
1597 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1601 && (stop <= addr || stop >= toaddr))
1602 irel->r_addend += count;
1603 else if (stop > addr
1605 && (start <= addr || start >= toaddr))
1606 irel->r_addend -= count;
1608 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1609 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1610 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1611 voff = bfd_get_8 (abfd, contents + nraddr);
1613 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1614 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1619 start = irel->r_offset;
1620 stop = (bfd_vma) ((bfd_signed_vma) start
1621 + (long) irel->r_addend
1628 && (stop <= addr || stop >= toaddr))
1630 else if (stop > addr
1632 && (start <= addr || start >= toaddr))
1641 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1650 if ((oinsn & 0xff00) != (insn & 0xff00))
1652 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1657 if ((oinsn & 0xf000) != (insn & 0xf000))
1659 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1663 BFD_ASSERT (adjust == count || count >= 4);
1668 if ((irel->r_offset & 3) == 0)
1671 if ((oinsn & 0xff00) != (insn & 0xff00))
1673 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1678 if (voff < 0 || voff >= 0xff)
1680 bfd_put_8 (abfd, voff, contents + nraddr);
1685 if (voff < - 0x8000 || voff >= 0x8000)
1687 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1692 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1696 irel->r_addend += adjust;
1702 ((*_bfd_error_handler)
1703 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1704 bfd_archive_filename (abfd), (unsigned long) irel->r_offset));
1705 bfd_set_error (bfd_error_bad_value);
1710 irel->r_offset = nraddr;
1713 /* Look through all the other sections. If there contain any IMM32
1714 relocs against internal symbols which we are not going to adjust
1715 below, we may need to adjust the addends. */
1716 for (o = abfd->sections; o != NULL; o = o->next)
1718 Elf_Internal_Rela *internal_relocs;
1719 Elf_Internal_Rela *irelscan, *irelscanend;
1720 bfd_byte *ocontents;
1723 || (o->flags & SEC_RELOC) == 0
1724 || o->reloc_count == 0)
1727 /* We always cache the relocs. Perhaps, if info->keep_memory is
1728 false, we should free them, if we are permitted to, when we
1729 leave sh_coff_relax_section. */
1730 internal_relocs = (_bfd_elf32_link_read_relocs
1731 (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1733 if (internal_relocs == NULL)
1737 irelscanend = internal_relocs + o->reloc_count;
1738 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1740 Elf_Internal_Sym sym;
1742 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1743 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1745 bfd_vma start, stop;
1746 bfd_signed_vma voff;
1748 if (ocontents == NULL)
1750 if (elf_section_data (o)->this_hdr.contents != NULL)
1751 ocontents = elf_section_data (o)->this_hdr.contents;
1754 /* We always cache the section contents.
1755 Perhaps, if info->keep_memory is false, we
1756 should free them, if we are permitted to,
1757 when we leave sh_coff_relax_section. */
1758 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1759 if (ocontents == NULL)
1761 if (! bfd_get_section_contents (abfd, o, ocontents,
1765 elf_section_data (o)->this_hdr.contents = ocontents;
1769 stop = irelscan->r_offset;
1771 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1773 /* STOP is in a different section, so it won't change. */
1774 if (start > addr && start < toaddr)
1775 irelscan->r_addend += count;
1777 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1778 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1782 && (stop <= addr || stop >= toaddr))
1783 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
1784 ocontents + irelscan->r_offset);
1785 else if (stop > addr
1787 && (start <= addr || start >= toaddr))
1788 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
1789 ocontents + irelscan->r_offset);
1792 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1795 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1798 bfd_elf32_swap_symbol_in (abfd,
1799 extsyms + ELF32_R_SYM (irelscan->r_info),
1802 if (sym.st_shndx == shndx
1803 && (sym.st_value <= addr
1804 || sym.st_value >= toaddr))
1808 if (ocontents == NULL)
1810 if (elf_section_data (o)->this_hdr.contents != NULL)
1811 ocontents = elf_section_data (o)->this_hdr.contents;
1814 /* We always cache the section contents.
1815 Perhaps, if info->keep_memory is false, we
1816 should free them, if we are permitted to,
1817 when we leave sh_coff_relax_section. */
1818 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1819 if (ocontents == NULL)
1821 if (! bfd_get_section_contents (abfd, o, ocontents,
1825 elf_section_data (o)->this_hdr.contents = ocontents;
1829 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1830 val += sym.st_value;
1831 if (val > addr && val < toaddr)
1832 bfd_put_32 (abfd, val - count,
1833 ocontents + irelscan->r_offset);
1838 /* Adjust the local symbols defined in this section. */
1840 esymend = esym + symtab_hdr->sh_info;
1841 for (; esym < esymend; esym++)
1843 Elf_Internal_Sym isym;
1845 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1847 if (isym.st_shndx == shndx
1848 && isym.st_value > addr
1849 && isym.st_value < toaddr)
1851 isym.st_value -= count;
1852 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1856 /* Now adjust the global symbols defined in this section. */
1857 esym = extsyms + symtab_hdr->sh_info;
1858 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1859 for (index = 0; esym < esymend; esym++, index++)
1861 Elf_Internal_Sym isym;
1863 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1864 sym_hash = elf_sym_hashes (abfd)[index];
1865 if (isym.st_shndx == shndx
1866 && ((sym_hash)->root.type == bfd_link_hash_defined
1867 || (sym_hash)->root.type == bfd_link_hash_defweak)
1868 && (sym_hash)->root.u.def.section == sec
1869 && (sym_hash)->root.u.def.value > addr
1870 && (sym_hash)->root.u.def.value < toaddr)
1872 (sym_hash)->root.u.def.value -= count;
1876 /* See if we can move the ALIGN reloc forward. We have adjusted
1877 r_offset for it already. */
1878 if (irelalign != NULL)
1880 bfd_vma alignto, alignaddr;
1882 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1883 alignaddr = BFD_ALIGN (irelalign->r_offset,
1884 1 << irelalign->r_addend);
1885 if (alignto != alignaddr)
1887 /* Tail recursion. */
1888 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1889 (int) (alignto - alignaddr));
1896 /* Look for loads and stores which we can align to four byte
1897 boundaries. This is like sh_align_loads in coff-sh.c. */
1900 sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped)
1903 Elf_Internal_Rela *internal_relocs;
1907 Elf_Internal_Rela *irel, *irelend;
1908 bfd_vma *labels = NULL;
1909 bfd_vma *label, *label_end;
1914 irelend = internal_relocs + sec->reloc_count;
1916 /* Get all the addresses with labels on them. */
1917 amt = sec->reloc_count;
1918 amt *= sizeof (bfd_vma);
1919 labels = (bfd_vma *) bfd_malloc (amt);
1923 for (irel = internal_relocs; irel < irelend; irel++)
1925 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1927 *label_end = irel->r_offset;
1932 /* Note that the assembler currently always outputs relocs in
1933 address order. If that ever changes, this code will need to sort
1934 the label values and the relocs. */
1938 for (irel = internal_relocs; irel < irelend; irel++)
1940 bfd_vma start, stop;
1942 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1945 start = irel->r_offset;
1947 for (irel++; irel < irelend; irel++)
1948 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1951 stop = irel->r_offset;
1953 stop = sec->_cooked_size;
1955 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1956 (PTR) internal_relocs, &label,
1957 label_end, start, stop, pswapped))
1971 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1974 sh_elf_swap_insns (abfd, sec, relocs, contents, addr)
1981 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1982 unsigned short i1, i2;
1983 Elf_Internal_Rela *irel, *irelend;
1985 /* Swap the instructions themselves. */
1986 i1 = bfd_get_16 (abfd, contents + addr);
1987 i2 = bfd_get_16 (abfd, contents + addr + 2);
1988 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1989 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
1991 /* Adjust all reloc addresses. */
1992 irelend = internal_relocs + sec->reloc_count;
1993 for (irel = internal_relocs; irel < irelend; irel++)
1995 enum elf_sh_reloc_type type;
1998 /* There are a few special types of relocs that we don't want to
1999 adjust. These relocs do not apply to the instruction itself,
2000 but are only associated with the address. */
2001 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
2002 if (type == R_SH_ALIGN
2003 || type == R_SH_CODE
2004 || type == R_SH_DATA
2005 || type == R_SH_LABEL)
2008 /* If an R_SH_USES reloc points to one of the addresses being
2009 swapped, we must adjust it. It would be incorrect to do this
2010 for a jump, though, since we want to execute both
2011 instructions after the jump. (We have avoided swapping
2012 around a label, so the jump will not wind up executing an
2013 instruction it shouldn't). */
2014 if (type == R_SH_USES)
2018 off = irel->r_offset + 4 + irel->r_addend;
2020 irel->r_offset += 2;
2021 else if (off == addr + 2)
2022 irel->r_offset -= 2;
2025 if (irel->r_offset == addr)
2027 irel->r_offset += 2;
2030 else if (irel->r_offset == addr + 2)
2032 irel->r_offset -= 2;
2041 unsigned short insn, oinsn;
2044 loc = contents + irel->r_offset;
2053 insn = bfd_get_16 (abfd, loc);
2056 if ((oinsn & 0xff00) != (insn & 0xff00))
2058 bfd_put_16 (abfd, (bfd_vma) insn, loc);
2062 insn = bfd_get_16 (abfd, loc);
2065 if ((oinsn & 0xf000) != (insn & 0xf000))
2067 bfd_put_16 (abfd, (bfd_vma) insn, loc);
2071 /* This reloc ignores the least significant 3 bits of
2072 the program counter before adding in the offset.
2073 This means that if ADDR is at an even address, the
2074 swap will not affect the offset. If ADDR is an at an
2075 odd address, then the instruction will be crossing a
2076 four byte boundary, and must be adjusted. */
2077 if ((addr & 3) != 0)
2079 insn = bfd_get_16 (abfd, loc);
2082 if ((oinsn & 0xff00) != (insn & 0xff00))
2084 bfd_put_16 (abfd, (bfd_vma) insn, loc);
2092 ((*_bfd_error_handler)
2093 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
2094 bfd_archive_filename (abfd), (unsigned long) irel->r_offset));
2095 bfd_set_error (bfd_error_bad_value);
2104 /* The size in bytes of an entry in the procedure linkage table. */
2106 #define PLT_ENTRY_SIZE 28
2108 /* First entry in an absolute procedure linkage table look like this. */
2111 /* Note - this code has been "optimised" not to use r2. r2 is used by
2112 GCC to return the address of large strutcures, so it should not be
2113 corrupted here. This does mean however, that this PLT does not conform
2114 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
2115 and r2 contains the GOT id. This version stores the GOT id in r0 and
2116 ignores the type. Loaders can easily detect this difference however,
2117 since the type will always be 0 or 8, and the GOT ids will always be
2118 greater than or equal to 12. */
2119 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
2121 0xd0, 0x05, /* mov.l 2f,r0 */
2122 0x60, 0x02, /* mov.l @r0,r0 */
2123 0x2f, 0x06, /* mov.l r0,@-r15 */
2124 0xd0, 0x03, /* mov.l 1f,r0 */
2125 0x60, 0x02, /* mov.l @r0,r0 */
2126 0x40, 0x2b, /* jmp @r0 */
2127 0x60, 0xf6, /* mov.l @r15+,r0 */
2128 0x00, 0x09, /* nop */
2129 0x00, 0x09, /* nop */
2130 0x00, 0x09, /* nop */
2131 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2132 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2135 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
2137 0x05, 0xd0, /* mov.l 2f,r0 */
2138 0x02, 0x60, /* mov.l @r0,r0 */
2139 0x06, 0x2f, /* mov.l r0,@-r15 */
2140 0x03, 0xd0, /* mov.l 1f,r0 */
2141 0x02, 0x60, /* mov.l @r0,r0 */
2142 0x2b, 0x40, /* jmp @r0 */
2143 0xf6, 0x60, /* mov.l @r15+,r0 */
2144 0x09, 0x00, /* nop */
2145 0x09, 0x00, /* nop */
2146 0x09, 0x00, /* nop */
2147 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2148 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2151 /* Sebsequent entries in an absolute procedure linkage table look like
2154 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
2156 0xd0, 0x04, /* mov.l 1f,r0 */
2157 0x60, 0x02, /* mov.l @r0,r0 */
2158 0xd1, 0x02, /* mov.l 0f,r1 */
2159 0x40, 0x2b, /* jmp @r0 */
2160 0x60, 0x13, /* mov r1,r0 */
2161 0xd1, 0x03, /* mov.l 2f,r1 */
2162 0x40, 0x2b, /* jmp @r0 */
2163 0x00, 0x09, /* nop */
2164 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
2165 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2166 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2169 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
2171 0x04, 0xd0, /* mov.l 1f,r0 */
2172 0x02, 0x60, /* mov.l @r0,r0 */
2173 0x02, 0xd1, /* mov.l 0f,r1 */
2174 0x2b, 0x40, /* jmp @r0 */
2175 0x13, 0x60, /* mov r1,r0 */
2176 0x03, 0xd1, /* mov.l 2f,r1 */
2177 0x2b, 0x40, /* jmp @r0 */
2178 0x09, 0x00, /* nop */
2179 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
2180 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2181 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2184 /* Entries in a PIC procedure linkage table look like this. */
2186 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
2188 0xd0, 0x04, /* mov.l 1f,r0 */
2189 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2190 0x40, 0x2b, /* jmp @r0 */
2191 0x00, 0x09, /* nop */
2192 0x50, 0xc2, /* mov.l @(8,r12),r0 */
2193 0xd1, 0x03, /* mov.l 2f,r1 */
2194 0x40, 0x2b, /* jmp @r0 */
2195 0x50, 0xc1, /* mov.l @(4,r12),r0 */
2196 0x00, 0x09, /* nop */
2197 0x00, 0x09, /* nop */
2198 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2199 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2202 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
2204 0x04, 0xd0, /* mov.l 1f,r0 */
2205 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2206 0x2b, 0x40, /* jmp @r0 */
2207 0x09, 0x00, /* nop */
2208 0xc2, 0x50, /* mov.l @(8,r12),r0 */
2209 0x03, 0xd1, /* mov.l 2f,r1 */
2210 0x2b, 0x40, /* jmp @r0 */
2211 0xc1, 0x50, /* mov.l @(4,r12),r0 */
2212 0x09, 0x00, /* nop */
2213 0x09, 0x00, /* nop */
2214 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2215 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2218 #else /* These are the old style PLT entries. */
2219 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
2221 0xd0, 0x04, /* mov.l 1f,r0 */
2222 0xd2, 0x05, /* mov.l 2f,r2 */
2223 0x60, 0x02, /* mov.l @r0,r0 */
2224 0x62, 0x22, /* mov.l @r2,r2 */
2225 0x40, 0x2b, /* jmp @r0 */
2226 0xe0, 0x00, /* mov #0,r0 */
2227 0x00, 0x09, /* nop */
2228 0x00, 0x09, /* nop */
2229 0x00, 0x09, /* nop */
2230 0x00, 0x09, /* nop */
2231 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2232 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2235 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
2237 0x04, 0xd0, /* mov.l 1f,r0 */
2238 0x05, 0xd2, /* mov.l 2f,r2 */
2239 0x02, 0x60, /* mov.l @r0,r0 */
2240 0x22, 0x62, /* mov.l @r2,r2 */
2241 0x2b, 0x40, /* jmp @r0 */
2242 0x00, 0xe0, /* mov #0,r0 */
2243 0x09, 0x00, /* nop */
2244 0x09, 0x00, /* nop */
2245 0x09, 0x00, /* nop */
2246 0x09, 0x00, /* nop */
2247 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2248 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2251 /* Sebsequent entries in an absolute procedure linkage table look like
2254 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
2256 0xd0, 0x04, /* mov.l 1f,r0 */
2257 0x60, 0x02, /* mov.l @r0,r0 */
2258 0xd2, 0x02, /* mov.l 0f,r2 */
2259 0x40, 0x2b, /* jmp @r0 */
2260 0x60, 0x23, /* mov r2,r0 */
2261 0xd1, 0x03, /* mov.l 2f,r1 */
2262 0x40, 0x2b, /* jmp @r0 */
2263 0x00, 0x09, /* nop */
2264 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
2265 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2266 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2269 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
2271 0x04, 0xd0, /* mov.l 1f,r0 */
2272 0x02, 0x60, /* mov.l @r0,r0 */
2273 0x02, 0xd2, /* mov.l 0f,r2 */
2274 0x2b, 0x40, /* jmp @r0 */
2275 0x23, 0x60, /* mov r2,r0 */
2276 0x03, 0xd1, /* mov.l 2f,r1 */
2277 0x2b, 0x40, /* jmp @r0 */
2278 0x09, 0x00, /* nop */
2279 0, 0, 0, 0, /* 0: replaced with address of .PLT. */
2280 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2281 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2284 /* Entries in a PIC procedure linkage table look like this. */
2286 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
2288 0xd0, 0x04, /* mov.l 1f,r0 */
2289 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2290 0x40, 0x2b, /* jmp @r0 */
2291 0x00, 0x09, /* nop */
2292 0x50, 0xc2, /* 0: mov.l @(8,r12),r0 */
2293 0x52, 0xc1, /* 1: mov.l @(4,r12),r2 */
2294 0xd1, 0x02, /* mov.l 2f,r1 */
2295 0x40, 0x2b, /* jmp @r0 */
2296 0xe0, 0x00, /* mov #0,r0 ! shows the type of PLT. */
2297 0x00, 0x09, /* nop */
2298 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2299 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2302 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
2304 0x04, 0xd0, /* mov.l 1f,r0 */
2305 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2306 0x2b, 0x40, /* jmp @r0 */
2307 0x09, 0x00, /* nop */
2308 0xc2, 0x50, /* 0: mov.l @(8,r12),r0 */
2309 0xc1, 0x52, /* 1: mov.l @(4,r12),r2 */
2310 0x02, 0xd1, /* mov.l 2f,r1 */
2311 0x2b, 0x40, /* jmp @r0 */
2312 0x00, 0xe0, /* mov #0,r0 ! shows the type of PLT. */
2313 0x09, 0x00, /* nop */
2314 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2315 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2317 #endif /* old style PLT entries. */
2319 static const bfd_byte *elf_sh_plt0_entry;
2320 static const bfd_byte *elf_sh_plt_entry;
2321 static const bfd_byte *elf_sh_pic_plt_entry;
2323 /* Return size of a PLT entry. */
2324 #define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
2326 /* Return offset of the PLT0 address in an absolute PLT entry. */
2327 #define elf_sh_plt_plt0_offset(info) 16
2329 /* Return offset of the linker in PLT0 entry. */
2330 #define elf_sh_plt0_linker_offset(info) 20
2332 /* Return offset of the GOT id in PLT0 entry. */
2333 #define elf_sh_plt0_gotid_offset(info) 24
2335 /* Return offset of the tempoline in PLT entry */
2336 #define elf_sh_plt_temp_offset(info) 8
2338 /* Return offset of the symbol in PLT entry. */
2339 #define elf_sh_plt_symbol_offset(info) 20
2341 /* Return offset of the relocation in PLT entry. */
2342 #define elf_sh_plt_reloc_offset(info) 24
2344 /* The sh linker needs to keep track of the number of relocs that it
2345 decides to copy in check_relocs for each symbol. This is so that
2346 it can discard PC relative relocs if it doesn't need them when
2347 linking with -Bsymbolic. We store the information in a field
2348 extending the regular ELF linker hash table. */
2350 /* This structure keeps track of the number of PC relative relocs we
2351 have copied for a given symbol. */
2353 struct elf_sh_pcrel_relocs_copied
2356 struct elf_sh_pcrel_relocs_copied *next;
2357 /* A section in dynobj. */
2359 /* Number of relocs copied in this section. */
2360 bfd_size_type count;
2363 /* sh ELF linker hash entry. */
2365 struct elf_sh_link_hash_entry
2367 struct elf_link_hash_entry root;
2369 /* Number of PC relative relocs copied for this symbol. */
2370 struct elf_sh_pcrel_relocs_copied *pcrel_relocs_copied;
2373 /* sh ELF linker hash table. */
2375 struct elf_sh_link_hash_table
2377 struct elf_link_hash_table root;
2380 /* Declare this now that the above structures are defined. */
2382 static boolean sh_elf_discard_copies
2383 PARAMS ((struct elf_sh_link_hash_entry *, PTR));
2385 /* Traverse an sh ELF linker hash table. */
2387 #define sh_elf_link_hash_traverse(table, func, info) \
2388 (elf_link_hash_traverse \
2390 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2393 /* Get the sh ELF linker hash table from a link_info structure. */
2395 #define sh_elf_hash_table(p) \
2396 ((struct elf_sh_link_hash_table *) ((p)->hash))
2398 /* Create an entry in an sh ELF linker hash table. */
2400 static struct bfd_hash_entry *
2401 sh_elf_link_hash_newfunc (entry, table, string)
2402 struct bfd_hash_entry *entry;
2403 struct bfd_hash_table *table;
2406 struct elf_sh_link_hash_entry *ret =
2407 (struct elf_sh_link_hash_entry *) entry;
2409 /* Allocate the structure if it has not already been allocated by a
2411 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2412 ret = ((struct elf_sh_link_hash_entry *)
2413 bfd_hash_allocate (table,
2414 sizeof (struct elf_sh_link_hash_entry)));
2415 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2416 return (struct bfd_hash_entry *) ret;
2418 /* Call the allocation method of the superclass. */
2419 ret = ((struct elf_sh_link_hash_entry *)
2420 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2422 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2424 ret->pcrel_relocs_copied = NULL;
2427 return (struct bfd_hash_entry *) ret;
2430 /* Create an sh ELF linker hash table. */
2432 static struct bfd_link_hash_table *
2433 sh_elf_link_hash_table_create (abfd)
2436 struct elf_sh_link_hash_table *ret;
2437 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
2439 ret = (struct elf_sh_link_hash_table *) bfd_alloc (abfd, amt);
2440 if (ret == (struct elf_sh_link_hash_table *) NULL)
2443 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2444 sh_elf_link_hash_newfunc))
2446 bfd_release (abfd, ret);
2450 return &ret->root.root;
2453 /* Create dynamic sections when linking against a dynamic object. */
2456 sh_elf_create_dynamic_sections (abfd, info)
2458 struct bfd_link_info *info;
2460 flagword flags, pltflags;
2461 register asection *s;
2462 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2465 switch (bed->s->arch_size)
2476 bfd_set_error (bfd_error_bad_value);
2480 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2481 .rel[a].bss sections. */
2483 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2484 | SEC_LINKER_CREATED);
2487 pltflags |= SEC_CODE;
2488 if (bed->plt_not_loaded)
2489 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2490 if (bed->plt_readonly)
2491 pltflags |= SEC_READONLY;
2493 s = bfd_make_section (abfd, ".plt");
2495 || ! bfd_set_section_flags (abfd, s, pltflags)
2496 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2499 if (bed->want_plt_sym)
2501 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2503 struct elf_link_hash_entry *h = NULL;
2504 if (! (_bfd_generic_link_add_one_symbol
2505 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2506 (bfd_vma) 0, (const char *) NULL, false,
2507 get_elf_backend_data (abfd)->collect,
2508 (struct bfd_link_hash_entry **) &h)))
2510 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2511 h->type = STT_OBJECT;
2514 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2518 s = bfd_make_section (abfd,
2519 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
2521 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2522 || ! bfd_set_section_alignment (abfd, s, ptralign))
2525 if (! _bfd_elf_create_got_section (abfd, info))
2529 const char *secname;
2534 for (sec = abfd->sections; sec; sec = sec->next)
2536 secflags = bfd_get_section_flags (abfd, sec);
2537 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
2538 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
2540 secname = bfd_get_section_name (abfd, sec);
2541 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
2542 strcpy (relname, ".rela");
2543 strcat (relname, secname);
2544 s = bfd_make_section (abfd, relname);
2546 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2547 || ! bfd_set_section_alignment (abfd, s, ptralign))
2552 if (bed->want_dynbss)
2554 /* The .dynbss section is a place to put symbols which are defined
2555 by dynamic objects, are referenced by regular objects, and are
2556 not functions. We must allocate space for them in the process
2557 image and use a R_*_COPY reloc to tell the dynamic linker to
2558 initialize them at run time. The linker script puts the .dynbss
2559 section into the .bss section of the final image. */
2560 s = bfd_make_section (abfd, ".dynbss");
2562 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2565 /* The .rel[a].bss section holds copy relocs. This section is not
2566 normally needed. We need to create it here, though, so that the
2567 linker will map it to an output section. We can't just create it
2568 only if we need it, because we will not know whether we need it
2569 until we have seen all the input files, and the first time the
2570 main linker code calls BFD after examining all the input files
2571 (size_dynamic_sections) the input sections have already been
2572 mapped to the output sections. If the section turns out not to
2573 be needed, we can discard it later. We will never need this
2574 section when generating a shared object, since they do not use
2578 s = bfd_make_section (abfd,
2579 (bed->default_use_rela_p
2580 ? ".rela.bss" : ".rel.bss"));
2582 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2583 || ! bfd_set_section_alignment (abfd, s, ptralign))
2591 /* Adjust a symbol defined by a dynamic object and referenced by a
2592 regular object. The current definition is in some section of the
2593 dynamic object, but we're not including those sections. We have to
2594 change the definition to something the rest of the link can
2598 sh_elf_adjust_dynamic_symbol (info, h)
2599 struct bfd_link_info *info;
2600 struct elf_link_hash_entry *h;
2604 unsigned int power_of_two;
2606 dynobj = elf_hash_table (info)->dynobj;
2608 /* Make sure we know what is going on here. */
2609 BFD_ASSERT (dynobj != NULL
2610 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2611 || h->weakdef != NULL
2612 || ((h->elf_link_hash_flags
2613 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2614 && (h->elf_link_hash_flags
2615 & ELF_LINK_HASH_REF_REGULAR) != 0
2616 && (h->elf_link_hash_flags
2617 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2619 /* If this is a function, put it in the procedure linkage table. We
2620 will fill in the contents of the procedure linkage table later,
2621 when we know the address of the .got section. */
2622 if (h->type == STT_FUNC
2623 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2626 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2627 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
2629 /* This case can occur if we saw a PLT reloc in an input
2630 file, but the symbol was never referred to by a dynamic
2631 object. In such a case, we don't actually need to build
2632 a procedure linkage table, and we can just do a REL32
2634 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2638 /* Make sure this symbol is output as a dynamic symbol. */
2639 if (h->dynindx == -1)
2641 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2645 s = bfd_get_section_by_name (dynobj, ".plt");
2646 BFD_ASSERT (s != NULL);
2648 /* If this is the first .plt entry, make room for the special
2650 if (s->_raw_size == 0)
2651 s->_raw_size += PLT_ENTRY_SIZE;
2653 /* If this symbol is not defined in a regular file, and we are
2654 not generating a shared library, then set the symbol to this
2655 location in the .plt. This is required to make function
2656 pointers compare as equal between the normal executable and
2657 the shared library. */
2659 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2661 h->root.u.def.section = s;
2662 h->root.u.def.value = s->_raw_size;
2665 h->plt.offset = s->_raw_size;
2667 /* Make room for this entry. */
2668 s->_raw_size += elf_sh_sizeof_plt (info);
2670 /* We also need to make an entry in the .got.plt section, which
2671 will be placed in the .got section by the linker script. */
2673 s = bfd_get_section_by_name (dynobj, ".got.plt");
2674 BFD_ASSERT (s != NULL);
2677 /* We also need to make an entry in the .rela.plt section. */
2679 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2680 BFD_ASSERT (s != NULL);
2681 s->_raw_size += sizeof (Elf32_External_Rela);
2686 /* If this is a weak symbol, and there is a real definition, the
2687 processor independent code will have arranged for us to see the
2688 real definition first, and we can just use the same value. */
2689 if (h->weakdef != NULL)
2691 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2692 || h->weakdef->root.type == bfd_link_hash_defweak);
2693 h->root.u.def.section = h->weakdef->root.u.def.section;
2694 h->root.u.def.value = h->weakdef->root.u.def.value;
2698 /* This is a reference to a symbol defined by a dynamic object which
2699 is not a function. */
2701 /* If we are creating a shared library, we must presume that the
2702 only references to the symbol are via the global offset table.
2703 For such cases we need not do anything here; the relocations will
2704 be handled correctly by relocate_section. */
2708 /* If there are no references to this symbol that do not use the
2709 GOT, we don't need to generate a copy reloc. */
2710 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2713 /* We must allocate the symbol in our .dynbss section, which will
2714 become part of the .bss section of the executable. There will be
2715 an entry for this symbol in the .dynsym section. The dynamic
2716 object will contain position independent code, so all references
2717 from the dynamic object to this symbol will go through the global
2718 offset table. The dynamic linker will use the .dynsym entry to
2719 determine the address it must put in the global offset table, so
2720 both the dynamic object and the regular object will refer to the
2721 same memory location for the variable. */
2723 s = bfd_get_section_by_name (dynobj, ".dynbss");
2724 BFD_ASSERT (s != NULL);
2726 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2727 copy the initial value out of the dynamic object and into the
2728 runtime process image. We need to remember the offset into the
2729 .rela.bss section we are going to use. */
2730 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2734 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2735 BFD_ASSERT (srel != NULL);
2736 srel->_raw_size += sizeof (Elf32_External_Rela);
2737 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2740 /* We need to figure out the alignment required for this symbol. I
2741 have no idea how ELF linkers handle this. */
2742 power_of_two = bfd_log2 (h->size);
2743 if (power_of_two > 3)
2746 /* Apply the required alignment. */
2747 s->_raw_size = BFD_ALIGN (s->_raw_size,
2748 (bfd_size_type) (1 << power_of_two));
2749 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2751 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2755 /* Define the symbol as being at this point in the section. */
2756 h->root.u.def.section = s;
2757 h->root.u.def.value = s->_raw_size;
2759 /* Increment the section size to make room for the symbol. */
2760 s->_raw_size += h->size;
2765 /* Set the sizes of the dynamic sections. */
2768 sh_elf_size_dynamic_sections (output_bfd, info)
2769 bfd *output_bfd ATTRIBUTE_UNUSED;
2770 struct bfd_link_info *info;
2777 dynobj = elf_hash_table (info)->dynobj;
2778 BFD_ASSERT (dynobj != NULL);
2780 if (elf_hash_table (info)->dynamic_sections_created)
2782 /* Set the contents of the .interp section to the interpreter. */
2785 s = bfd_get_section_by_name (dynobj, ".interp");
2786 BFD_ASSERT (s != NULL);
2787 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2788 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2793 /* We may have created entries in the .rela.got section.
2794 However, if we are not creating the dynamic sections, we will
2795 not actually use these entries. Reset the size of .rela.got,
2796 which will cause it to get stripped from the output file
2798 s = bfd_get_section_by_name (dynobj, ".rela.got");
2803 /* If this is a -Bsymbolic shared link, then we need to discard all
2804 PC relative relocs against symbols defined in a regular object.
2805 We allocated space for them in the check_relocs routine, but we
2806 will not fill them in in the relocate_section routine. */
2807 if (info->shared && info->symbolic)
2808 sh_elf_link_hash_traverse (sh_elf_hash_table (info),
2809 sh_elf_discard_copies,
2812 /* The check_relocs and adjust_dynamic_symbol entry points have
2813 determined the sizes of the various dynamic sections. Allocate
2817 for (s = dynobj->sections; s != NULL; s = s->next)
2822 if ((s->flags & SEC_LINKER_CREATED) == 0)
2825 /* It's OK to base decisions on the section name, because none
2826 of the dynobj section names depend upon the input files. */
2827 name = bfd_get_section_name (dynobj, s);
2831 if (strcmp (name, ".plt") == 0)
2833 if (s->_raw_size == 0)
2835 /* Strip this section if we don't need it; see the
2841 /* Remember whether there is a PLT. */
2845 else if (strncmp (name, ".rela", 5) == 0)
2847 if (s->_raw_size == 0)
2849 /* If we don't need this section, strip it from the
2850 output file. This is mostly to handle .rela.bss and
2851 .rela.plt. We must create both sections in
2852 create_dynamic_sections, because they must be created
2853 before the linker maps input sections to output
2854 sections. The linker does that before
2855 adjust_dynamic_symbol is called, and it is that
2856 function which decides whether anything needs to go
2857 into these sections. */
2862 /* Remember whether there are any reloc sections other
2864 if (strcmp (name, ".rela.plt") != 0)
2867 /* We use the reloc_count field as a counter if we need
2868 to copy relocs into the output file. */
2872 else if (strncmp (name, ".got", 4) != 0)
2874 /* It's not one of our sections, so don't allocate space. */
2880 _bfd_strip_section_from_output (info, s);
2884 /* Allocate memory for the section contents. */
2885 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
2886 if (s->contents == NULL && s->_raw_size != 0)
2890 if (elf_hash_table (info)->dynamic_sections_created)
2892 /* Add some entries to the .dynamic section. We fill in the
2893 values later, in sh_elf_finish_dynamic_sections, but we
2894 must add the entries now so that we get the correct size for
2895 the .dynamic section. The DT_DEBUG entry is filled in by the
2896 dynamic linker and used by the debugger. */
2897 #define add_dynamic_entry(TAG, VAL) \
2898 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2902 if (!add_dynamic_entry (DT_DEBUG, 0))
2908 if (!add_dynamic_entry (DT_PLTGOT, 0)
2909 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2910 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2911 || !add_dynamic_entry (DT_JMPREL, 0))
2917 if (!add_dynamic_entry (DT_RELA, 0)
2918 || !add_dynamic_entry (DT_RELASZ, 0)
2919 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2923 if ((info->flags & DF_TEXTREL) != 0)
2925 if (!add_dynamic_entry (DT_TEXTREL, 0))
2929 #undef add_dynamic_entry
2934 /* This function is called via sh_elf_link_hash_traverse if we are
2935 creating a shared object with -Bsymbolic. It discards the space
2936 allocated to copy PC relative relocs against symbols which are
2937 defined in regular objects. We allocated space for them in the
2938 check_relocs routine, but we won't fill them in in the
2939 relocate_section routine. */
2942 sh_elf_discard_copies (h, ignore)
2943 struct elf_sh_link_hash_entry *h;
2944 PTR ignore ATTRIBUTE_UNUSED;
2946 struct elf_sh_pcrel_relocs_copied *s;
2948 /* We only discard relocs for symbols defined in a regular object. */
2949 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2952 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2953 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2958 /* Relocate an SH ELF section. */
2961 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2962 contents, relocs, local_syms, local_sections)
2964 struct bfd_link_info *info;
2966 asection *input_section;
2968 Elf_Internal_Rela *relocs;
2969 Elf_Internal_Sym *local_syms;
2970 asection **local_sections;
2972 Elf_Internal_Shdr *symtab_hdr;
2973 struct elf_link_hash_entry **sym_hashes;
2974 Elf_Internal_Rela *rel, *relend;
2976 bfd_vma *local_got_offsets;
2981 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2982 sym_hashes = elf_sym_hashes (input_bfd);
2983 dynobj = elf_hash_table (info)->dynobj;
2984 local_got_offsets = elf_local_got_offsets (input_bfd);
2991 relend = relocs + input_section->reloc_count;
2992 for (; rel < relend; rel++)
2995 reloc_howto_type *howto;
2996 unsigned long r_symndx;
2997 Elf_Internal_Sym *sym;
2999 struct elf_link_hash_entry *h;
3001 bfd_vma addend = (bfd_vma) 0;
3002 bfd_reloc_status_type r;
3004 r_symndx = ELF32_R_SYM (rel->r_info);
3006 r_type = ELF32_R_TYPE (rel->r_info);
3008 /* Many of the relocs are only used for relaxing, and are
3009 handled entirely by the relaxation code. */
3010 if (r_type > (int) R_SH_LAST_INVALID_RELOC
3011 && r_type < (int) R_SH_LOOP_START)
3013 if (r_type == (int) R_SH_NONE)
3017 || r_type >= R_SH_max
3018 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
3019 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
3020 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3021 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
3023 bfd_set_error (bfd_error_bad_value);
3027 howto = sh_elf_howto_table + r_type;
3029 /* For relocs that aren't partial_inplace, we get the addend from
3031 if (! howto->partial_inplace)
3032 addend = rel->r_addend;
3037 if (r_symndx < symtab_hdr->sh_info)
3039 sym = local_syms + r_symndx;
3040 sec = local_sections[r_symndx];
3041 relocation = (sec->output_section->vma
3042 + sec->output_offset
3044 if (info->relocateable)
3046 /* This is a relocateable link. We don't have to change
3047 anything, unless the reloc is against a section symbol,
3048 in which case we have to adjust according to where the
3049 section symbol winds up in the output section. */
3050 sym = local_syms + r_symndx;
3051 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3053 if (! howto->partial_inplace)
3055 /* For relocations with the addend in the
3056 relocation, we need just to update the addend.
3057 All real relocs are of type partial_inplace; this
3058 code is mostly for completeness. */
3059 rel->r_addend += sec->output_offset + sym->st_value;
3064 /* Relocs of type partial_inplace need to pick up the
3065 contents in the contents and add the offset resulting
3066 from the changed location of the section symbol.
3067 Using _bfd_final_link_relocate (e.g. goto
3068 final_link_relocate) here would be wrong, because
3069 relocations marked pc_relative would get the current
3070 location subtracted, and we must only do that at the
3072 r = _bfd_relocate_contents (howto, input_bfd,
3075 contents + rel->r_offset);
3076 goto relocation_done;
3081 else if (! howto->partial_inplace)
3083 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
3084 addend = rel->r_addend;
3086 else if ((sec->flags & SEC_MERGE)
3087 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3091 if (howto->rightshift || howto->src_mask != 0xffffffff)
3093 (*_bfd_error_handler)
3094 (_("%s(%s+0x%lx): %s relocation against SEC_MERGE section"),
3095 bfd_archive_filename (input_bfd),
3096 bfd_get_section_name (input_bfd, input_section),
3097 (long) rel->r_offset, howto->name);
3101 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
3104 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
3106 addend += msec->output_section->vma + msec->output_offset;
3107 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
3113 /* Section symbol are never (?) placed in the hash table, so
3114 we can just ignore hash relocations when creating a
3115 relocateable object file. */
3116 if (info->relocateable)
3119 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3120 while (h->root.type == bfd_link_hash_indirect
3121 || h->root.type == bfd_link_hash_warning)
3122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3123 if (h->root.type == bfd_link_hash_defined
3124 || h->root.type == bfd_link_hash_defweak)
3126 sec = h->root.u.def.section;
3127 /* In these cases, we don't need the relocation value.
3128 We check specially because in some obscure cases
3129 sec->output_section will be NULL. */
3130 if (r_type == R_SH_GOTPC
3131 || (r_type == R_SH_PLT32
3132 && h->plt.offset != (bfd_vma) -1)
3133 || (r_type == R_SH_GOT32
3134 && elf_hash_table (info)->dynamic_sections_created
3136 || (! info->symbolic && h->dynindx != -1)
3137 || (h->elf_link_hash_flags
3138 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3139 /* The cases above are those in which relocation is
3140 overwritten in the switch block below. The cases
3141 below are those in which we must defer relocation
3142 to run-time, because we can't resolve absolute
3143 addresses when creating a shared library. */
3145 && ((! info->symbolic && h->dynindx != -1)
3146 || (h->elf_link_hash_flags
3147 & ELF_LINK_HASH_DEF_REGULAR) == 0)
3148 && ((r_type == R_SH_DIR32
3149 && !(ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3150 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN))
3151 || r_type == R_SH_REL32)
3152 && ((input_section->flags & SEC_ALLOC) != 0
3153 /* DWARF will emit R_SH_DIR32 relocations in its
3154 sections against symbols defined externally
3155 in shared libraries. We can't do anything
3157 || (input_section->flags & SEC_DEBUGGING) != 0)))
3159 else if (sec->output_section == NULL)
3161 (*_bfd_error_handler)
3162 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
3163 bfd_archive_filename (input_bfd), h->root.root.string,
3164 bfd_get_section_name (input_bfd, input_section));
3168 relocation = (h->root.u.def.value
3169 + sec->output_section->vma
3170 + sec->output_offset);
3172 else if (h->root.type == bfd_link_hash_undefweak)
3174 else if (info->shared
3175 && (!info->symbolic || info->allow_shlib_undefined)
3176 && !info->no_undefined)
3180 if (! ((*info->callbacks->undefined_symbol)
3181 (info, h->root.root.string, input_bfd,
3182 input_section, rel->r_offset, true)))
3188 switch ((int) r_type)
3190 final_link_relocate:
3191 /* COFF relocs don't use the addend. The addend is used for
3192 R_SH_DIR32 to be compatible with other compilers. */
3193 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3194 contents, rel->r_offset,
3195 relocation, addend);
3200 goto final_link_relocate;
3205 /* If the reloc is against the start of this section, then
3206 the assembler has already taken care of it and the reloc
3207 is here only to assist in relaxing. If the reloc is not
3208 against the start of this section, then it's against an
3209 external symbol and we must deal with it ourselves. */
3210 if (input_section->output_section->vma + input_section->output_offset
3213 int disp = (relocation
3214 - input_section->output_section->vma
3215 - input_section->output_offset
3221 case R_SH_DIR8WPZ: mask = 1; break;
3222 case R_SH_DIR8WPL: mask = 3; break;
3223 default: mask = 0; break;
3227 ((*_bfd_error_handler)
3228 (_("%s: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
3229 bfd_archive_filename (input_section->owner),
3230 (unsigned long) rel->r_offset));
3231 bfd_set_error (bfd_error_bad_value);
3235 goto final_link_relocate;
3241 bfd_set_error (bfd_error_bad_value);
3248 && (input_section->flags & SEC_ALLOC) != 0
3249 && (r_type != R_SH_REL32
3252 && (! info->symbolic
3253 || (h->elf_link_hash_flags
3254 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3256 Elf_Internal_Rela outrel;
3257 boolean skip, relocate;
3259 /* When generating a shared object, these relocations
3260 are copied into the output file to be resolved at run
3267 name = (bfd_elf_string_from_elf_section
3269 elf_elfheader (input_bfd)->e_shstrndx,
3270 elf_section_data (input_section)->rel_hdr.sh_name));
3274 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3275 && strcmp (bfd_get_section_name (input_bfd,
3279 sreloc = bfd_get_section_by_name (dynobj, name);
3280 BFD_ASSERT (sreloc != NULL);
3286 _bfd_elf_section_offset (output_bfd, info, input_section,
3288 if (outrel.r_offset == (bfd_vma) -1)
3290 outrel.r_offset += (input_section->output_section->vma
3291 + input_section->output_offset);
3295 memset (&outrel, 0, sizeof outrel);
3298 else if (r_type == R_SH_REL32)
3300 BFD_ASSERT (h != NULL && h->dynindx != -1);
3302 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
3304 = bfd_get_32 (input_bfd, contents + rel->r_offset);
3308 /* h->dynindx may be -1 if this symbol was marked to
3311 || ((info->symbolic || h->dynindx == -1)
3312 && (h->elf_link_hash_flags
3313 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3316 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
3318 = relocation + bfd_get_32 (input_bfd,
3319 contents + rel->r_offset);
3323 BFD_ASSERT (h->dynindx != -1);
3325 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
3327 = relocation + bfd_get_32 (input_bfd,
3328 contents + rel->r_offset);
3332 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3333 (((Elf32_External_Rela *)
3335 + sreloc->reloc_count));
3336 ++sreloc->reloc_count;
3338 /* If this reloc is against an external symbol, we do
3339 not want to fiddle with the addend. Otherwise, we
3340 need to include the symbol value so that it becomes
3341 an addend for the dynamic reloc. */
3345 goto final_link_relocate;
3348 /* Relocation is to the entry for this symbol in the global
3352 sgot = bfd_get_section_by_name (dynobj, ".got");
3353 BFD_ASSERT (sgot != NULL);
3360 off = h->got.offset;
3361 BFD_ASSERT (off != (bfd_vma) -1);
3363 if (! elf_hash_table (info)->dynamic_sections_created
3365 && (info->symbolic || h->dynindx == -1
3366 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3367 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3368 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
3370 /* This is actually a static link, or it is a
3371 -Bsymbolic link and the symbol is defined
3372 locally, or the symbol was forced to be local
3373 because of a version file. We must initialize
3374 this entry in the global offset table. Since the
3375 offset must always be a multiple of 4, we use the
3376 least significant bit to record whether we have
3377 initialized it already.
3379 When doing a dynamic link, we create a .rela.got
3380 relocation entry to initialize the value. This
3381 is done in the finish_dynamic_symbol routine. */
3386 bfd_put_32 (output_bfd, relocation,
3387 sgot->contents + off);
3392 relocation = sgot->output_offset + off;
3398 BFD_ASSERT (local_got_offsets != NULL
3399 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3401 off = local_got_offsets[r_symndx];
3403 /* The offset must always be a multiple of 4. We use
3404 the least significant bit to record whether we have
3405 already generated the necessary reloc. */
3410 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3415 Elf_Internal_Rela outrel;
3417 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3418 BFD_ASSERT (srelgot != NULL);
3420 outrel.r_offset = (sgot->output_section->vma
3421 + sgot->output_offset
3423 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
3424 outrel.r_addend = relocation;
3425 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3426 (((Elf32_External_Rela *)
3428 + srelgot->reloc_count));
3429 ++srelgot->reloc_count;
3432 local_got_offsets[r_symndx] |= 1;
3435 relocation = sgot->output_offset + off;
3438 goto final_link_relocate;
3441 /* Relocation is relative to the start of the global offset
3446 sgot = bfd_get_section_by_name (dynobj, ".got");
3447 BFD_ASSERT (sgot != NULL);
3450 /* Note that sgot->output_offset is not involved in this
3451 calculation. We always want the start of .got. If we
3452 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3453 permitted by the ABI, we might have to change this
3455 relocation -= sgot->output_section->vma;
3457 goto final_link_relocate;
3460 /* Use global offset table as symbol value. */
3464 sgot = bfd_get_section_by_name (dynobj, ".got");
3465 BFD_ASSERT (sgot != NULL);
3468 relocation = sgot->output_section->vma;
3470 goto final_link_relocate;
3473 /* Relocation is to the entry for this symbol in the
3474 procedure linkage table. */
3476 /* Resolve a PLT reloc against a local symbol directly,
3477 without using the procedure linkage table. */
3479 goto final_link_relocate;
3481 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3482 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3483 goto final_link_relocate;
3485 if (h->plt.offset == (bfd_vma) -1)
3487 /* We didn't make a PLT entry for this symbol. This
3488 happens when statically linking PIC code, or when
3489 using -Bsymbolic. */
3490 goto final_link_relocate;
3495 splt = bfd_get_section_by_name (dynobj, ".plt");
3496 BFD_ASSERT (splt != NULL);
3499 relocation = (splt->output_section->vma
3500 + splt->output_offset
3503 goto final_link_relocate;
3505 case R_SH_LOOP_START:
3507 static bfd_vma start, end;
3509 start = (relocation + rel->r_addend
3510 - (sec->output_section->vma + sec->output_offset));
3511 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3512 rel->r_offset, sec, start, end);
3516 end = (relocation + rel->r_addend
3517 - (sec->output_section->vma + sec->output_offset));
3518 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3519 rel->r_offset, sec, start, end);
3525 if (r != bfd_reloc_ok)
3530 case bfd_reloc_outofrange:
3532 case bfd_reloc_overflow:
3537 name = h->root.root.string;
3540 name = (bfd_elf_string_from_elf_section
3541 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3545 name = bfd_section_name (input_bfd, sec);
3547 if (! ((*info->callbacks->reloc_overflow)
3548 (info, name, howto->name, (bfd_vma) 0,
3549 input_bfd, input_section, rel->r_offset)))
3560 /* This is a version of bfd_generic_get_relocated_section_contents
3561 which uses sh_elf_relocate_section. */
3564 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3565 data, relocateable, symbols)
3567 struct bfd_link_info *link_info;
3568 struct bfd_link_order *link_order;
3570 boolean relocateable;
3573 Elf_Internal_Shdr *symtab_hdr;
3574 asection *input_section = link_order->u.indirect.section;
3575 bfd *input_bfd = input_section->owner;
3576 asection **sections = NULL;
3577 Elf_Internal_Rela *internal_relocs = NULL;
3578 Elf32_External_Sym *external_syms = NULL;
3579 Elf_Internal_Sym *internal_syms = NULL;
3581 /* We only need to handle the case of relaxing, or of having a
3582 particular set of section contents, specially. */
3584 || elf_section_data (input_section)->this_hdr.contents == NULL)
3585 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3590 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3592 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3593 (size_t) input_section->_raw_size);
3595 if ((input_section->flags & SEC_RELOC) != 0
3596 && input_section->reloc_count > 0)
3598 Elf_Internal_Sym *isymp;
3600 Elf32_External_Sym *esym, *esymend;
3603 if (symtab_hdr->contents != NULL)
3604 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
3607 size = symtab_hdr->sh_info;
3608 size *= sizeof (Elf32_External_Sym);
3609 external_syms = (Elf32_External_Sym *) bfd_malloc (size);
3610 if (external_syms == NULL && size != 0)
3612 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
3613 || bfd_bread (external_syms, size, input_bfd) != size)
3617 internal_relocs = (_bfd_elf32_link_read_relocs
3618 (input_bfd, input_section, (PTR) NULL,
3619 (Elf_Internal_Rela *) NULL, false));
3620 if (internal_relocs == NULL)
3623 size = symtab_hdr->sh_info;
3624 size *= sizeof (Elf_Internal_Sym);
3625 internal_syms = (Elf_Internal_Sym *) bfd_malloc (size);
3626 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
3629 size = symtab_hdr->sh_info;
3630 size *= sizeof (asection *);
3631 sections = (asection **) bfd_malloc (size);
3632 if (sections == NULL && symtab_hdr->sh_info > 0)
3635 isymp = internal_syms;
3637 esym = external_syms;
3638 esymend = esym + symtab_hdr->sh_info;
3639 for (; esym < esymend; ++esym, ++isymp, ++secpp)
3643 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
3645 if (isymp->st_shndx == SHN_UNDEF)
3646 isec = bfd_und_section_ptr;
3647 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
3648 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
3649 else if (isymp->st_shndx == SHN_ABS)
3650 isec = bfd_abs_section_ptr;
3651 else if (isymp->st_shndx == SHN_COMMON)
3652 isec = bfd_com_section_ptr;
3662 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
3663 input_section, data, internal_relocs,
3664 internal_syms, sections))
3667 if (sections != NULL)
3670 if (internal_syms != NULL)
3671 free (internal_syms);
3672 internal_syms = NULL;
3673 if (external_syms != NULL && symtab_hdr->contents == NULL)
3674 free (external_syms);
3675 external_syms = NULL;
3676 if (internal_relocs != elf_section_data (input_section)->relocs)
3677 free (internal_relocs);
3678 internal_relocs = NULL;
3684 if (internal_relocs != NULL
3685 && internal_relocs != elf_section_data (input_section)->relocs)
3686 free (internal_relocs);
3687 if (external_syms != NULL && symtab_hdr->contents == NULL)
3688 free (external_syms);
3689 if (internal_syms != NULL)
3690 free (internal_syms);
3691 if (sections != NULL)
3697 sh_elf_gc_mark_hook (abfd, info, rel, h, sym)
3699 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3700 Elf_Internal_Rela *rel;
3701 struct elf_link_hash_entry *h;
3702 Elf_Internal_Sym *sym;
3706 switch (ELF32_R_TYPE (rel->r_info))
3708 case R_SH_GNU_VTINHERIT:
3709 case R_SH_GNU_VTENTRY:
3713 switch (h->root.type)
3715 case bfd_link_hash_defined:
3716 case bfd_link_hash_defweak:
3717 return h->root.u.def.section;
3719 case bfd_link_hash_common:
3720 return h->root.u.c.p->section;
3729 if (!(elf_bad_symtab (abfd)
3730 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
3731 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
3732 && sym->st_shndx != SHN_COMMON))
3733 return bfd_section_from_elf_index (abfd, sym->st_shndx);
3738 /* Update the got entry reference counts for the section being removed. */
3741 sh_elf_gc_sweep_hook (abfd, info, sec, relocs)
3742 bfd *abfd ATTRIBUTE_UNUSED;
3743 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3744 asection *sec ATTRIBUTE_UNUSED;
3745 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
3747 /* We use got and plt entries for sh, but it would seem that the
3748 existing SH code does no sort of reference counting or whatnot on
3749 its GOT and PLT entries, so it is not possible to garbage collect
3750 them at this time. */
3754 /* Look through the relocs for a section during the first phase.
3755 Since we don't do .gots or .plts, we just need to consider the
3756 virtual table relocs for gc. */
3759 sh_elf_check_relocs (abfd, info, sec, relocs)
3761 struct bfd_link_info *info;
3763 const Elf_Internal_Rela *relocs;
3765 Elf_Internal_Shdr *symtab_hdr;
3766 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3767 const Elf_Internal_Rela *rel;
3768 const Elf_Internal_Rela *rel_end;
3770 bfd_vma *local_got_offsets;
3779 if (info->relocateable)
3782 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3783 sym_hashes = elf_sym_hashes (abfd);
3784 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
3785 if (!elf_bad_symtab (abfd))
3786 sym_hashes_end -= symtab_hdr->sh_info;
3788 dynobj = elf_hash_table (info)->dynobj;
3789 local_got_offsets = elf_local_got_offsets (abfd);
3791 rel_end = relocs + sec->reloc_count;
3792 for (rel = relocs; rel < rel_end; rel++)
3794 struct elf_link_hash_entry *h;
3795 unsigned long r_symndx;
3797 r_symndx = ELF32_R_SYM (rel->r_info);
3798 if (r_symndx < symtab_hdr->sh_info)
3801 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3803 /* Some relocs require a global offset table. */
3806 switch (ELF32_R_TYPE (rel->r_info))
3811 elf_hash_table (info)->dynobj = dynobj = abfd;
3812 if (! _bfd_elf_create_got_section (dynobj, info))
3821 switch (ELF32_R_TYPE (rel->r_info))
3823 /* This relocation describes the C++ object vtable hierarchy.
3824 Reconstruct it for later use during GC. */
3825 case R_SH_GNU_VTINHERIT:
3826 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3830 /* This relocation describes which C++ vtable entries are actually
3831 used. Record for later use during GC. */
3832 case R_SH_GNU_VTENTRY:
3833 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3838 /* This symbol requires a global offset table entry. */
3842 sgot = bfd_get_section_by_name (dynobj, ".got");
3843 BFD_ASSERT (sgot != NULL);
3847 && (h != NULL || info->shared))
3849 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3850 if (srelgot == NULL)
3852 srelgot = bfd_make_section (dynobj, ".rela.got");
3854 || ! bfd_set_section_flags (dynobj, srelgot,
3859 | SEC_LINKER_CREATED
3861 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
3868 if (h->got.offset != (bfd_vma) -1)
3870 /* We have already allocated space in the .got. */
3873 h->got.offset = sgot->_raw_size;
3875 /* Make sure this symbol is output as a dynamic symbol. */
3876 if (h->dynindx == -1)
3878 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3882 srelgot->_raw_size += sizeof (Elf32_External_Rela);
3886 /* This is a global offset table entry for a local
3888 if (local_got_offsets == NULL)
3891 register unsigned int i;
3893 size = symtab_hdr->sh_info;
3894 size *= sizeof (bfd_vma);
3895 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
3896 if (local_got_offsets == NULL)
3898 elf_local_got_offsets (abfd) = local_got_offsets;
3899 for (i = 0; i < symtab_hdr->sh_info; i++)
3900 local_got_offsets[i] = (bfd_vma) -1;
3902 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
3904 /* We have already allocated space in the .got. */
3907 local_got_offsets[r_symndx] = sgot->_raw_size;
3911 /* If we are generating a shared object, we need to
3912 output a R_SH_RELATIVE reloc so that the dynamic
3913 linker can adjust this GOT entry. */
3914 srelgot->_raw_size += sizeof (Elf32_External_Rela);
3918 sgot->_raw_size += 4;
3923 /* This symbol requires a procedure linkage table entry. We
3924 actually build the entry in adjust_dynamic_symbol,
3925 because this might be a case of linking PIC code which is
3926 never referenced by a dynamic object, in which case we
3927 don't need to generate a procedure linkage table entry
3930 /* If this is a local symbol, we resolve it directly without
3931 creating a procedure linkage table entry. */
3935 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3936 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3939 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3946 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3948 /* If we are creating a shared library, and this is a reloc
3949 against a global symbol, or a non PC relative reloc
3950 against a local symbol, then we need to copy the reloc
3951 into the shared library. However, if we are linking with
3952 -Bsymbolic, we do not need to copy a reloc against a
3953 global symbol which is defined in an object we are
3954 including in the link (i.e., DEF_REGULAR is set). At
3955 this point we have not seen all the input files, so it is
3956 possible that DEF_REGULAR is not set now but will be set
3957 later (it is never cleared). We account for that
3958 possibility below by storing information in the
3959 pcrel_relocs_copied field of the hash table entry. */
3961 && (sec->flags & SEC_ALLOC) != 0
3962 && (ELF32_R_TYPE (rel->r_info) != R_SH_REL32
3964 && (! info->symbolic
3965 || (h->elf_link_hash_flags
3966 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3968 /* When creating a shared object, we must copy these
3969 reloc types into the output file. We create a reloc
3970 section in dynobj and make room for this reloc. */
3975 name = (bfd_elf_string_from_elf_section
3977 elf_elfheader (abfd)->e_shstrndx,
3978 elf_section_data (sec)->rel_hdr.sh_name));
3982 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3983 && strcmp (bfd_get_section_name (abfd, sec),
3986 sreloc = bfd_get_section_by_name (dynobj, name);
3991 sreloc = bfd_make_section (dynobj, name);
3992 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3993 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3994 if ((sec->flags & SEC_ALLOC) != 0)
3995 flags |= SEC_ALLOC | SEC_LOAD;
3997 || ! bfd_set_section_flags (dynobj, sreloc, flags)
3998 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4001 if (sec->flags & SEC_READONLY)
4002 info->flags |= DF_TEXTREL;
4005 sreloc->_raw_size += sizeof (Elf32_External_Rela);
4007 /* If we are linking with -Bsymbolic, and this is a
4008 global symbol, we count the number of PC relative
4009 relocations we have entered for this symbol, so that
4010 we can discard them again if the symbol is later
4011 defined by a regular object. Note that this function
4012 is only called if we are using an elf_sh linker
4013 hash table, which means that h is really a pointer to
4014 an elf_sh_link_hash_entry. */
4015 if (h != NULL && info->symbolic
4016 && ELF32_R_TYPE (rel->r_info) == R_SH_REL32)
4018 struct elf_sh_link_hash_entry *eh;
4019 struct elf_sh_pcrel_relocs_copied *p;
4021 eh = (struct elf_sh_link_hash_entry *) h;
4023 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
4024 if (p->section == sreloc)
4029 p = ((struct elf_sh_pcrel_relocs_copied *)
4030 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
4033 p->next = eh->pcrel_relocs_copied;
4034 eh->pcrel_relocs_copied = p;
4035 p->section = sreloc;
4051 sh_elf_set_mach_from_flags (abfd)
4054 flagword flags = elf_elfheader (abfd)->e_flags;
4056 switch (flags & EF_SH_MACH_MASK)
4059 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
4062 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
4065 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
4068 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
4071 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
4074 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
4078 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
4086 /* Function to keep SH specific file flags. */
4089 sh_elf_set_private_flags (abfd, flags)
4093 BFD_ASSERT (! elf_flags_init (abfd)
4094 || elf_elfheader (abfd)->e_flags == flags);
4096 elf_elfheader (abfd)->e_flags = flags;
4097 elf_flags_init (abfd) = true;
4098 return sh_elf_set_mach_from_flags (abfd);
4101 /* Copy backend specific data from one object module to another */
4104 sh_elf_copy_private_data (ibfd, obfd)
4108 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4109 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4112 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
4115 /* This routine checks for linking big and little endian objects
4116 together, and for linking sh-dsp with sh3e / sh4 objects. */
4119 sh_elf_merge_private_data (ibfd, obfd)
4123 flagword old_flags, new_flags;
4125 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
4128 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4129 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4132 if (! elf_flags_init (obfd))
4134 /* This happens when ld starts out with a 'blank' output file. */
4135 elf_flags_init (obfd) = true;
4136 elf_elfheader (obfd)->e_flags = EF_SH1;
4138 old_flags = elf_elfheader (obfd)->e_flags;
4139 new_flags = elf_elfheader (ibfd)->e_flags;
4140 if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
4141 || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
4143 (*_bfd_error_handler)
4144 ("%s: uses %s instructions while previous modules use %s instructions",
4145 bfd_archive_filename (ibfd),
4146 EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
4147 EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
4148 bfd_set_error (bfd_error_bad_value);
4151 elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
4153 return sh_elf_set_mach_from_flags (obfd);
4156 /* Finish up dynamic symbol handling. We set the contents of various
4157 dynamic sections here. */
4160 sh_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4162 struct bfd_link_info *info;
4163 struct elf_link_hash_entry *h;
4164 Elf_Internal_Sym *sym;
4168 dynobj = elf_hash_table (info)->dynobj;
4170 if (h->plt.offset != (bfd_vma) -1)
4178 Elf_Internal_Rela rel;
4180 /* This symbol has an entry in the procedure linkage table. Set
4183 BFD_ASSERT (h->dynindx != -1);
4185 splt = bfd_get_section_by_name (dynobj, ".plt");
4186 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4187 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4188 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4190 /* Get the index in the procedure linkage table which
4191 corresponds to this symbol. This is the index of this symbol
4192 in all the symbols for which we are making plt entries. The
4193 first entry in the procedure linkage table is reserved. */
4194 plt_index = h->plt.offset / elf_sh_sizeof_plt (info) - 1;
4196 /* Get the offset into the .got table of the entry that
4197 corresponds to this function. Each .got entry is 4 bytes.
4198 The first three are reserved. */
4199 got_offset = (plt_index + 3) * 4;
4201 /* Fill in the entry in the procedure linkage table. */
4204 if (elf_sh_plt_entry == NULL)
4206 elf_sh_plt_entry = (bfd_big_endian (output_bfd) ?
4207 elf_sh_plt_entry_be : elf_sh_plt_entry_le);
4209 memcpy (splt->contents + h->plt.offset, elf_sh_plt_entry,
4210 elf_sh_sizeof_plt (info));
4211 bfd_put_32 (output_bfd,
4212 (sgot->output_section->vma
4213 + sgot->output_offset
4215 (splt->contents + h->plt.offset
4216 + elf_sh_plt_symbol_offset (info)));
4218 bfd_put_32 (output_bfd,
4219 (splt->output_section->vma + splt->output_offset),
4220 (splt->contents + h->plt.offset
4221 + elf_sh_plt_plt0_offset (info)));
4225 if (elf_sh_pic_plt_entry == NULL)
4227 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
4228 elf_sh_pic_plt_entry_be :
4229 elf_sh_pic_plt_entry_le);
4231 memcpy (splt->contents + h->plt.offset, elf_sh_pic_plt_entry,
4232 elf_sh_sizeof_plt (info));
4233 bfd_put_32 (output_bfd, got_offset,
4234 (splt->contents + h->plt.offset
4235 + elf_sh_plt_symbol_offset (info)));
4238 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4239 (splt->contents + h->plt.offset
4240 + elf_sh_plt_reloc_offset (info)));
4242 /* Fill in the entry in the global offset table. */
4243 bfd_put_32 (output_bfd,
4244 (splt->output_section->vma
4245 + splt->output_offset
4247 + elf_sh_plt_temp_offset (info)),
4248 sgot->contents + got_offset);
4250 /* Fill in the entry in the .rela.plt section. */
4251 rel.r_offset = (sgot->output_section->vma
4252 + sgot->output_offset
4254 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
4256 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4257 ((Elf32_External_Rela *) srel->contents
4260 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4262 /* Mark the symbol as undefined, rather than as defined in
4263 the .plt section. Leave the value alone. */
4264 sym->st_shndx = SHN_UNDEF;
4268 if (h->got.offset != (bfd_vma) -1)
4272 Elf_Internal_Rela rel;
4274 /* This symbol has an entry in the global offset table. Set it
4277 sgot = bfd_get_section_by_name (dynobj, ".got");
4278 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4279 BFD_ASSERT (sgot != NULL && srel != NULL);
4281 rel.r_offset = (sgot->output_section->vma
4282 + sgot->output_offset
4283 + (h->got.offset &~ (bfd_vma) 1));
4285 /* If this is a -Bsymbolic link, and the symbol is defined
4286 locally, we just want to emit a RELATIVE reloc. Likewise if
4287 the symbol was forced to be local because of a version file.
4288 The entry in the global offset table will already have been
4289 initialized in the relocate_section function. */
4291 && (info->symbolic || h->dynindx == -1)
4292 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
4294 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4295 rel.r_addend = (h->root.u.def.value
4296 + h->root.u.def.section->output_section->vma
4297 + h->root.u.def.section->output_offset);
4301 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4302 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
4306 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4307 ((Elf32_External_Rela *) srel->contents
4308 + srel->reloc_count));
4309 ++srel->reloc_count;
4312 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4315 Elf_Internal_Rela rel;
4317 /* This symbol needs a copy reloc. Set it up. */
4319 BFD_ASSERT (h->dynindx != -1
4320 && (h->root.type == bfd_link_hash_defined
4321 || h->root.type == bfd_link_hash_defweak));
4323 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4325 BFD_ASSERT (s != NULL);
4327 rel.r_offset = (h->root.u.def.value
4328 + h->root.u.def.section->output_section->vma
4329 + h->root.u.def.section->output_offset);
4330 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
4332 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4333 ((Elf32_External_Rela *) s->contents
4338 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4339 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4340 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4341 sym->st_shndx = SHN_ABS;
4346 /* Finish up the dynamic sections. */
4349 sh_elf_finish_dynamic_sections (output_bfd, info)
4351 struct bfd_link_info *info;
4357 dynobj = elf_hash_table (info)->dynobj;
4359 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4360 BFD_ASSERT (sgot != NULL);
4361 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4363 if (elf_hash_table (info)->dynamic_sections_created)
4366 Elf32_External_Dyn *dyncon, *dynconend;
4368 BFD_ASSERT (sdyn != NULL);
4370 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4371 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4372 for (; dyncon < dynconend; dyncon++)
4374 Elf_Internal_Dyn dyn;
4378 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4392 s = bfd_get_section_by_name (output_bfd, name);
4393 BFD_ASSERT (s != NULL);
4394 dyn.d_un.d_ptr = s->vma;
4395 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4399 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4400 BFD_ASSERT (s != NULL);
4401 if (s->_cooked_size != 0)
4402 dyn.d_un.d_val = s->_cooked_size;
4404 dyn.d_un.d_val = s->_raw_size;
4405 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4409 /* My reading of the SVR4 ABI indicates that the
4410 procedure linkage table relocs (DT_JMPREL) should be
4411 included in the overall relocs (DT_RELA). This is
4412 what Solaris does. However, UnixWare can not handle
4413 that case. Therefore, we override the DT_RELASZ entry
4414 here to make it not include the JMPREL relocs. Since
4415 the linker script arranges for .rela.plt to follow all
4416 other relocation sections, we don't have to worry
4417 about changing the DT_RELA entry. */
4418 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4421 if (s->_cooked_size != 0)
4422 dyn.d_un.d_val -= s->_cooked_size;
4424 dyn.d_un.d_val -= s->_raw_size;
4426 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4431 /* Fill in the first entry in the procedure linkage table. */
4432 splt = bfd_get_section_by_name (dynobj, ".plt");
4433 if (splt && splt->_raw_size > 0)
4437 if (elf_sh_pic_plt_entry == NULL)
4439 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
4440 elf_sh_pic_plt_entry_be :
4441 elf_sh_pic_plt_entry_le);
4443 memcpy (splt->contents, elf_sh_pic_plt_entry,
4444 elf_sh_sizeof_plt (info));
4448 if (elf_sh_plt0_entry == NULL)
4450 elf_sh_plt0_entry = (bfd_big_endian (output_bfd) ?
4451 elf_sh_plt0_entry_be :
4452 elf_sh_plt0_entry_le);
4454 memcpy (splt->contents, elf_sh_plt0_entry, PLT_ENTRY_SIZE);
4455 bfd_put_32 (output_bfd,
4456 sgot->output_section->vma + sgot->output_offset + 4,
4457 splt->contents + elf_sh_plt0_gotid_offset (info));
4458 bfd_put_32 (output_bfd,
4459 sgot->output_section->vma + sgot->output_offset + 8,
4460 splt->contents + elf_sh_plt0_linker_offset (info));
4463 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4464 really seem like the right value. */
4465 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4469 /* Fill in the first three entries in the global offset table. */
4470 if (sgot->_raw_size > 0)
4473 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4475 bfd_put_32 (output_bfd,
4476 sdyn->output_section->vma + sdyn->output_offset,
4478 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4479 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4482 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4487 static enum elf_reloc_type_class
4488 sh_elf_reloc_type_class (rela)
4489 const Elf_Internal_Rela *rela;
4491 switch ((int) ELF32_R_TYPE (rela->r_info))
4494 return reloc_class_relative;
4496 return reloc_class_plt;
4498 return reloc_class_copy;
4500 return reloc_class_normal;
4505 #define TARGET_BIG_SYM bfd_elf32_sh_vec
4506 #define TARGET_BIG_NAME "elf32-sh"
4507 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
4508 #define TARGET_LITTLE_NAME "elf32-shl"
4509 #define ELF_ARCH bfd_arch_sh
4510 #define ELF_MACHINE_CODE EM_SH
4511 #define ELF_MAXPAGESIZE 128
4513 #define elf_symbol_leading_char '_'
4514 #endif /* ELF_ARCH */
4516 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
4517 #define elf_info_to_howto sh_elf_info_to_howto
4518 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
4519 #define elf_backend_relocate_section sh_elf_relocate_section
4520 #define bfd_elf32_bfd_get_relocated_section_contents \
4521 sh_elf_get_relocated_section_contents
4522 #define elf_backend_object_p sh_elf_set_mach_from_flags
4523 #define bfd_elf32_bfd_set_private_bfd_flags \
4524 sh_elf_set_private_flags
4525 #define bfd_elf32_bfd_copy_private_bfd_data \
4526 sh_elf_copy_private_data
4527 #define bfd_elf32_bfd_merge_private_bfd_data \
4528 sh_elf_merge_private_data
4530 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
4531 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
4532 #define elf_backend_check_relocs sh_elf_check_relocs
4534 #define elf_backend_can_gc_sections 1
4535 #define elf_backend_create_dynamic_sections \
4536 sh_elf_create_dynamic_sections
4537 #define bfd_elf32_bfd_link_hash_table_create \
4538 sh_elf_link_hash_table_create
4539 #define elf_backend_adjust_dynamic_symbol \
4540 sh_elf_adjust_dynamic_symbol
4541 #define elf_backend_size_dynamic_sections \
4542 sh_elf_size_dynamic_sections
4543 #define elf_backend_finish_dynamic_symbol \
4544 sh_elf_finish_dynamic_symbol
4545 #define elf_backend_finish_dynamic_sections \
4546 sh_elf_finish_dynamic_sections
4547 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
4549 #define elf_backend_want_got_plt 1
4550 #define elf_backend_plt_readonly 1
4551 #define elf_backend_want_plt_sym 0
4552 #define elf_backend_got_header_size 12
4553 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
4554 #include "elf32-target.h"