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
*));
87 /* The name of the dynamic interpreter. This is put in the .interp
90 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
92 static reloc_howto_type sh_elf_howto_table
[] =
95 HOWTO (R_SH_NONE
, /* type */
97 0, /* size (0 = byte, 1 = short, 2 = long) */
99 false, /* pc_relative */
101 complain_overflow_dont
, /* complain_on_overflow */
102 sh_elf_ignore_reloc
, /* special_function */
103 "R_SH_NONE", /* name */
104 false, /* partial_inplace */
107 false), /* pcrel_offset */
109 /* 32 bit absolute relocation. Setting partial_inplace to true and
110 src_mask to a non-zero value is similar to the COFF toolchain. */
111 HOWTO (R_SH_DIR32
, /* type */
113 2, /* size (0 = byte, 1 = short, 2 = long) */
115 false, /* pc_relative */
117 complain_overflow_bitfield
, /* complain_on_overflow */
118 sh_elf_reloc
, /* special_function */
119 "R_SH_DIR32", /* name */
120 true, /* partial_inplace */
121 0xffffffff, /* src_mask */
122 0xffffffff, /* dst_mask */
123 false), /* pcrel_offset */
125 /* 32 bit PC relative relocation. */
126 HOWTO (R_SH_REL32
, /* type */
128 2, /* size (0 = byte, 1 = short, 2 = long) */
130 true, /* pc_relative */
132 complain_overflow_signed
, /* complain_on_overflow */
133 sh_elf_ignore_reloc
, /* special_function */
134 "R_SH_REL32", /* name */
135 false, /* partial_inplace */
137 0xffffffff, /* dst_mask */
138 true), /* pcrel_offset */
140 /* 8 bit PC relative branch divided by 2. */
141 HOWTO (R_SH_DIR8WPN
, /* type */
143 1, /* size (0 = byte, 1 = short, 2 = long) */
145 true, /* pc_relative */
147 complain_overflow_signed
, /* complain_on_overflow */
148 sh_elf_ignore_reloc
, /* special_function */
149 "R_SH_DIR8WPN", /* name */
150 true, /* partial_inplace */
153 true), /* pcrel_offset */
155 /* 12 bit PC relative branch divided by 2. */
156 HOWTO (R_SH_IND12W
, /* type */
158 1, /* size (0 = byte, 1 = short, 2 = long) */
160 true, /* pc_relative */
162 complain_overflow_signed
, /* complain_on_overflow */
163 sh_elf_reloc
, /* special_function */
164 "R_SH_IND12W", /* name */
165 true, /* partial_inplace */
166 0xfff, /* src_mask */
167 0xfff, /* dst_mask */
168 true), /* pcrel_offset */
170 /* 8 bit unsigned PC relative divided by 4. */
171 HOWTO (R_SH_DIR8WPL
, /* type */
173 1, /* size (0 = byte, 1 = short, 2 = long) */
175 true, /* pc_relative */
177 complain_overflow_unsigned
, /* complain_on_overflow */
178 sh_elf_ignore_reloc
, /* special_function */
179 "R_SH_DIR8WPL", /* name */
180 true, /* partial_inplace */
183 true), /* pcrel_offset */
185 /* 8 bit unsigned PC relative divided by 2. */
186 HOWTO (R_SH_DIR8WPZ
, /* type */
188 1, /* size (0 = byte, 1 = short, 2 = long) */
190 true, /* pc_relative */
192 complain_overflow_unsigned
, /* complain_on_overflow */
193 sh_elf_ignore_reloc
, /* special_function */
194 "R_SH_DIR8WPZ", /* name */
195 true, /* partial_inplace */
198 true), /* pcrel_offset */
200 /* 8 bit GBR relative. FIXME: This only makes sense if we have some
201 special symbol for the GBR relative area, and that is not
203 HOWTO (R_SH_DIR8BP
, /* type */
205 1, /* size (0 = byte, 1 = short, 2 = long) */
207 false, /* pc_relative */
209 complain_overflow_unsigned
, /* complain_on_overflow */
210 sh_elf_ignore_reloc
, /* special_function */
211 "R_SH_DIR8BP", /* name */
212 false, /* partial_inplace */
215 true), /* pcrel_offset */
217 /* 8 bit GBR relative divided by 2. FIXME: This only makes sense if
218 we have some special symbol for the GBR relative area, and that
219 is not implemented. */
220 HOWTO (R_SH_DIR8W
, /* type */
222 1, /* size (0 = byte, 1 = short, 2 = long) */
224 false, /* pc_relative */
226 complain_overflow_unsigned
, /* complain_on_overflow */
227 sh_elf_ignore_reloc
, /* special_function */
228 "R_SH_DIR8W", /* name */
229 false, /* partial_inplace */
232 true), /* pcrel_offset */
234 /* 8 bit GBR relative divided by 4. FIXME: This only makes sense if
235 we have some special symbol for the GBR relative area, and that
236 is not implemented. */
237 HOWTO (R_SH_DIR8L
, /* type */
239 1, /* size (0 = byte, 1 = short, 2 = long) */
241 false, /* pc_relative */
243 complain_overflow_unsigned
, /* complain_on_overflow */
244 sh_elf_ignore_reloc
, /* special_function */
245 "R_SH_DIR8L", /* name */
246 false, /* partial_inplace */
249 true), /* pcrel_offset */
267 /* The remaining relocs are a GNU extension used for relaxing. The
268 final pass of the linker never needs to do anything with any of
269 these relocs. Any required operations are handled by the
272 /* A 16 bit switch table entry. This is generated for an expression
273 such as ``.word L1 - L2''. The offset holds the difference
274 between the reloc address and L2. */
275 HOWTO (R_SH_SWITCH16
, /* type */
277 1, /* size (0 = byte, 1 = short, 2 = long) */
279 false, /* pc_relative */
281 complain_overflow_unsigned
, /* complain_on_overflow */
282 sh_elf_ignore_reloc
, /* special_function */
283 "R_SH_SWITCH16", /* name */
284 false, /* partial_inplace */
287 true), /* pcrel_offset */
289 /* A 32 bit switch table entry. This is generated for an expression
290 such as ``.long L1 - L2''. The offset holds the difference
291 between the reloc address and L2. */
292 HOWTO (R_SH_SWITCH32
, /* type */
294 2, /* size (0 = byte, 1 = short, 2 = long) */
296 false, /* pc_relative */
298 complain_overflow_unsigned
, /* complain_on_overflow */
299 sh_elf_ignore_reloc
, /* special_function */
300 "R_SH_SWITCH32", /* name */
301 false, /* partial_inplace */
304 true), /* pcrel_offset */
306 /* Indicates a .uses pseudo-op. The compiler will generate .uses
307 pseudo-ops when it finds a function call which can be relaxed.
308 The offset field holds the PC relative offset to the instruction
309 which loads the register used in the function call. */
310 HOWTO (R_SH_USES
, /* type */
312 1, /* size (0 = byte, 1 = short, 2 = long) */
314 false, /* pc_relative */
316 complain_overflow_unsigned
, /* complain_on_overflow */
317 sh_elf_ignore_reloc
, /* special_function */
318 "R_SH_USES", /* name */
319 false, /* partial_inplace */
322 true), /* pcrel_offset */
324 /* The assembler will generate this reloc for addresses referred to
325 by the register loads associated with USES relocs. The offset
326 field holds the number of times the address is referenced in the
328 HOWTO (R_SH_COUNT
, /* type */
330 1, /* size (0 = byte, 1 = short, 2 = long) */
332 false, /* pc_relative */
334 complain_overflow_unsigned
, /* complain_on_overflow */
335 sh_elf_ignore_reloc
, /* special_function */
336 "R_SH_COUNT", /* name */
337 false, /* partial_inplace */
340 true), /* pcrel_offset */
342 /* Indicates an alignment statement. The offset field is the power
343 of 2 to which subsequent portions of the object file must be
345 HOWTO (R_SH_ALIGN
, /* type */
347 1, /* size (0 = byte, 1 = short, 2 = long) */
349 false, /* pc_relative */
351 complain_overflow_unsigned
, /* complain_on_overflow */
352 sh_elf_ignore_reloc
, /* special_function */
353 "R_SH_ALIGN", /* name */
354 false, /* partial_inplace */
357 true), /* pcrel_offset */
359 /* The assembler will generate this reloc before a block of
360 instructions. A section should be processed as assumining it
361 contains data, unless this reloc is seen. */
362 HOWTO (R_SH_CODE
, /* type */
364 1, /* size (0 = byte, 1 = short, 2 = long) */
366 false, /* pc_relative */
368 complain_overflow_unsigned
, /* complain_on_overflow */
369 sh_elf_ignore_reloc
, /* special_function */
370 "R_SH_CODE", /* name */
371 false, /* partial_inplace */
374 true), /* pcrel_offset */
376 /* The assembler will generate this reloc after a block of
377 instructions when it sees data that is not instructions. */
378 HOWTO (R_SH_DATA
, /* type */
380 1, /* size (0 = byte, 1 = short, 2 = long) */
382 false, /* pc_relative */
384 complain_overflow_unsigned
, /* complain_on_overflow */
385 sh_elf_ignore_reloc
, /* special_function */
386 "R_SH_DATA", /* name */
387 false, /* partial_inplace */
390 true), /* pcrel_offset */
392 /* The assembler generates this reloc for each label within a block
393 of instructions. This permits the linker to avoid swapping
394 instructions which are the targets of branches. */
395 HOWTO (R_SH_LABEL
, /* type */
397 1, /* size (0 = byte, 1 = short, 2 = long) */
399 false, /* pc_relative */
401 complain_overflow_unsigned
, /* complain_on_overflow */
402 sh_elf_ignore_reloc
, /* special_function */
403 "R_SH_LABEL", /* name */
404 false, /* partial_inplace */
407 true), /* pcrel_offset */
409 /* An 8 bit switch table entry. This is generated for an expression
410 such as ``.word L1 - L2''. The offset holds the difference
411 between the reloc address and L2. */
412 HOWTO (R_SH_SWITCH8
, /* type */
414 0, /* size (0 = byte, 1 = short, 2 = long) */
416 false, /* pc_relative */
418 complain_overflow_unsigned
, /* complain_on_overflow */
419 sh_elf_ignore_reloc
, /* special_function */
420 "R_SH_SWITCH8", /* name */
421 false, /* partial_inplace */
424 true), /* pcrel_offset */
426 /* GNU extension to record C++ vtable hierarchy */
427 HOWTO (R_SH_GNU_VTINHERIT
, /* type */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
431 false, /* pc_relative */
433 complain_overflow_dont
, /* complain_on_overflow */
434 NULL
, /* special_function */
435 "R_SH_GNU_VTINHERIT", /* name */
436 false, /* partial_inplace */
439 false), /* pcrel_offset */
441 /* GNU extension to record C++ vtable member usage */
442 HOWTO (R_SH_GNU_VTENTRY
, /* type */
444 2, /* size (0 = byte, 1 = short, 2 = long) */
446 false, /* pc_relative */
448 complain_overflow_dont
, /* complain_on_overflow */
449 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
450 "R_SH_GNU_VTENTRY", /* name */
451 false, /* partial_inplace */
454 false), /* pcrel_offset */
456 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
457 HOWTO (R_SH_LOOP_START
, /* type */
459 1, /* size (0 = byte, 1 = short, 2 = long) */
461 false, /* pc_relative */
463 complain_overflow_signed
, /* complain_on_overflow */
464 sh_elf_ignore_reloc
, /* special_function */
465 "R_SH_LOOP_START", /* name */
466 true, /* partial_inplace */
469 true), /* pcrel_offset */
471 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
472 HOWTO (R_SH_LOOP_END
, /* type */
474 1, /* size (0 = byte, 1 = short, 2 = long) */
476 false, /* pc_relative */
478 complain_overflow_signed
, /* complain_on_overflow */
479 sh_elf_ignore_reloc
, /* special_function */
480 "R_SH_LOOP_END", /* name */
481 true, /* partial_inplace */
484 true), /* pcrel_offset */
609 HOWTO (R_SH_GOT32
, /* type */
611 2, /* size (0 = byte, 1 = short, 2 = long) */
613 false, /* pc_relative */
615 complain_overflow_bitfield
, /* complain_on_overflow */
616 bfd_elf_generic_reloc
, /* */
617 "R_SH_GOT32", /* name */
618 true, /* partial_inplace */
619 0xffffffff, /* src_mask */
620 0xffffffff, /* dst_mask */
621 false), /* pcrel_offset */
623 HOWTO (R_SH_PLT32
, /* type */
625 2, /* size (0 = byte, 1 = short, 2 = long) */
627 true, /* pc_relative */
629 complain_overflow_bitfield
, /* complain_on_overflow */
630 bfd_elf_generic_reloc
, /* */
631 "R_SH_PLT32", /* name */
632 true, /* partial_inplace */
633 0xffffffff, /* src_mask */
634 0xffffffff, /* dst_mask */
635 true), /* pcrel_offset */
637 HOWTO (R_SH_COPY
, /* type */
639 2, /* size (0 = byte, 1 = short, 2 = long) */
641 false, /* pc_relative */
643 complain_overflow_bitfield
, /* complain_on_overflow */
644 bfd_elf_generic_reloc
, /* */
645 "R_SH_COPY", /* name */
646 true, /* partial_inplace */
647 0xffffffff, /* src_mask */
648 0xffffffff, /* dst_mask */
649 false), /* pcrel_offset */
651 HOWTO (R_SH_GLOB_DAT
, /* type */
653 2, /* size (0 = byte, 1 = short, 2 = long) */
655 false, /* pc_relative */
657 complain_overflow_bitfield
, /* complain_on_overflow */
658 bfd_elf_generic_reloc
, /* */
659 "R_SH_GLOB_DAT", /* name */
660 true, /* partial_inplace */
661 0xffffffff, /* src_mask */
662 0xffffffff, /* dst_mask */
663 false), /* pcrel_offset */
665 HOWTO (R_SH_JMP_SLOT
, /* type */
667 2, /* size (0 = byte, 1 = short, 2 = long) */
669 false, /* pc_relative */
671 complain_overflow_bitfield
, /* complain_on_overflow */
672 bfd_elf_generic_reloc
, /* */
673 "R_SH_JMP_SLOT", /* name */
674 true, /* partial_inplace */
675 0xffffffff, /* src_mask */
676 0xffffffff, /* dst_mask */
677 false), /* pcrel_offset */
679 HOWTO (R_SH_RELATIVE
, /* type */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
683 false, /* pc_relative */
685 complain_overflow_bitfield
, /* complain_on_overflow */
686 bfd_elf_generic_reloc
, /* */
687 "R_SH_RELATIVE", /* name */
688 true, /* partial_inplace */
689 0xffffffff, /* src_mask */
690 0xffffffff, /* dst_mask */
691 false), /* pcrel_offset */
693 HOWTO (R_SH_GOTOFF
, /* type */
695 2, /* size (0 = byte, 1 = short, 2 = long) */
697 false, /* pc_relative */
699 complain_overflow_bitfield
, /* complain_on_overflow */
700 bfd_elf_generic_reloc
, /* */
701 "R_SH_GOTOFF", /* name */
702 true, /* partial_inplace */
703 0xffffffff, /* src_mask */
704 0xffffffff, /* dst_mask */
705 false), /* pcrel_offset */
707 HOWTO (R_SH_GOTPC
, /* type */
709 2, /* size (0 = byte, 1 = short, 2 = long) */
711 true, /* pc_relative */
713 complain_overflow_bitfield
, /* complain_on_overflow */
714 bfd_elf_generic_reloc
, /* */
715 "R_SH_GOTPC", /* name */
716 true, /* partial_inplace */
717 0xffffffff, /* src_mask */
718 0xffffffff, /* dst_mask */
719 true), /* pcrel_offset */
723 static bfd_reloc_status_type
724 sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
, addr
,
725 symbol_section
, start
, end
)
726 int r_type ATTRIBUTE_UNUSED
;
728 asection
*input_section
;
731 asection
*symbol_section
;
734 static bfd_vma last_addr
;
735 static asection
*last_symbol_section
;
736 bfd_byte
*free_contents
= NULL
;
737 bfd_byte
*start_ptr
, *ptr
, *last_ptr
;
742 /* Sanity check the address. */
743 if (addr
> input_section
->_raw_size
)
744 return bfd_reloc_outofrange
;
746 /* We require the start and end relocations to be processed consecutively -
747 although we allow then to be processed forwards or backwards. */
751 last_symbol_section
= symbol_section
;
754 if (last_addr
!= addr
)
758 if (! symbol_section
|| last_symbol_section
!= symbol_section
|| end
< start
)
759 return bfd_reloc_outofrange
;
761 /* Get the symbol_section contents. */
762 if (symbol_section
!= input_section
)
764 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= NULL
)
765 contents
= elf_section_data (symbol_section
)->this_hdr
.contents
;
768 free_contents
= contents
769 = (bfd_byte
*) bfd_malloc (symbol_section
->_raw_size
);
770 if (contents
== NULL
)
771 return bfd_reloc_outofrange
;
772 if (! bfd_get_section_contents (input_bfd
, symbol_section
, contents
,
774 symbol_section
->_raw_size
))
777 return bfd_reloc_outofrange
;
781 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
782 start_ptr
= contents
+ start
;
783 for (cum_diff
= -6, ptr
= contents
+ end
; cum_diff
< 0 && ptr
> start_ptr
;)
785 for (last_ptr
= ptr
, ptr
-= 4; ptr
>= start_ptr
&& IS_PPI (ptr
);)
788 diff
= (last_ptr
- ptr
) >> 1;
789 cum_diff
+= diff
& 1;
792 /* Calculate the start / end values to load into rs / re minus four -
793 so that will cancel out the four we would otherwise have to add to
794 addr to get the value to subtract in order to get relative addressing. */
798 end
= (ptr
+ cum_diff
* 2) - contents
;
802 bfd_vma start0
= start
- 4;
804 while (start0
&& IS_PPI (contents
+ start0
))
806 start0
= start
- 2 - ((start
- start0
) & 2);
807 start
= start0
- cum_diff
- 2;
812 free (free_contents
);
814 insn
= bfd_get_16 (input_bfd
, contents
+ addr
);
816 x
= (insn
& 0x200 ? end
: start
) - addr
;
817 if (input_section
!= symbol_section
)
818 x
+= ((symbol_section
->output_section
->vma
+ symbol_section
->output_offset
)
819 - (input_section
->output_section
->vma
820 + input_section
->output_offset
));
822 if (x
< -128 || x
> 127)
823 return bfd_reloc_overflow
;
825 x
= (insn
& ~0xff) | (x
& 0xff);
826 bfd_put_16 (input_bfd
, x
, contents
+ addr
);
831 /* This function is used for normal relocs. This used to be like the COFF
832 function, and is almost certainly incorrect for other ELF targets. */
834 static bfd_reloc_status_type
835 sh_elf_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
838 arelent
*reloc_entry
;
841 asection
*input_section
;
843 char **error_message ATTRIBUTE_UNUSED
;
847 enum elf_sh_reloc_type r_type
;
848 bfd_vma addr
= reloc_entry
->address
;
849 bfd_byte
*hit_data
= addr
+ (bfd_byte
*) data
;
851 r_type
= (enum elf_sh_reloc_type
) reloc_entry
->howto
->type
;
853 if (output_bfd
!= NULL
)
855 /* Partial linking--do nothing. */
856 reloc_entry
->address
+= input_section
->output_offset
;
860 /* Almost all relocs have to do with relaxing. If any work must be
861 done for them, it has been done in sh_relax_section. */
862 if (r_type
== R_SH_IND12W
&& (symbol_in
->flags
& BSF_LOCAL
) != 0)
865 if (symbol_in
!= NULL
866 && bfd_is_und_section (symbol_in
->section
))
867 return bfd_reloc_undefined
;
869 if (bfd_is_com_section (symbol_in
->section
))
872 sym_value
= (symbol_in
->value
+
873 symbol_in
->section
->output_section
->vma
+
874 symbol_in
->section
->output_offset
);
879 insn
= bfd_get_32 (abfd
, hit_data
);
880 insn
+= sym_value
+ reloc_entry
->addend
;
881 bfd_put_32 (abfd
, insn
, hit_data
);
884 insn
= bfd_get_16 (abfd
, hit_data
);
885 sym_value
+= reloc_entry
->addend
;
886 sym_value
-= (input_section
->output_section
->vma
887 + input_section
->output_offset
890 sym_value
+= (insn
& 0xfff) << 1;
893 insn
= (insn
& 0xf000) | (sym_value
& 0xfff);
894 bfd_put_16 (abfd
, insn
, hit_data
);
895 if (sym_value
< (bfd_vma
) -0x1000 || sym_value
>= 0x1000)
896 return bfd_reloc_overflow
;
906 /* This function is used for relocs which are only used for relaxing,
907 which the linker should otherwise ignore. */
909 static bfd_reloc_status_type
910 sh_elf_ignore_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
,
911 output_bfd
, error_message
)
912 bfd
*abfd ATTRIBUTE_UNUSED
;
913 arelent
*reloc_entry
;
914 asymbol
*symbol ATTRIBUTE_UNUSED
;
915 PTR data ATTRIBUTE_UNUSED
;
916 asection
*input_section
;
918 char **error_message ATTRIBUTE_UNUSED
;
920 if (output_bfd
!= NULL
)
921 reloc_entry
->address
+= input_section
->output_offset
;
925 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
929 bfd_reloc_code_real_type bfd_reloc_val
;
930 unsigned char elf_reloc_val
;
933 /* An array mapping BFD reloc codes to SH ELF relocs. */
935 static const struct elf_reloc_map sh_reloc_map
[] =
937 { BFD_RELOC_NONE
, R_SH_NONE
},
938 { BFD_RELOC_32
, R_SH_DIR32
},
939 { BFD_RELOC_CTOR
, R_SH_DIR32
},
940 { BFD_RELOC_32_PCREL
, R_SH_REL32
},
941 { BFD_RELOC_SH_PCDISP8BY2
, R_SH_DIR8WPN
},
942 { BFD_RELOC_SH_PCDISP12BY2
, R_SH_IND12W
},
943 { BFD_RELOC_SH_PCRELIMM8BY2
, R_SH_DIR8WPZ
},
944 { BFD_RELOC_SH_PCRELIMM8BY4
, R_SH_DIR8WPL
},
945 { BFD_RELOC_8_PCREL
, R_SH_SWITCH8
},
946 { BFD_RELOC_SH_SWITCH16
, R_SH_SWITCH16
},
947 { BFD_RELOC_SH_SWITCH32
, R_SH_SWITCH32
},
948 { BFD_RELOC_SH_USES
, R_SH_USES
},
949 { BFD_RELOC_SH_COUNT
, R_SH_COUNT
},
950 { BFD_RELOC_SH_ALIGN
, R_SH_ALIGN
},
951 { BFD_RELOC_SH_CODE
, R_SH_CODE
},
952 { BFD_RELOC_SH_DATA
, R_SH_DATA
},
953 { BFD_RELOC_SH_LABEL
, R_SH_LABEL
},
954 { BFD_RELOC_VTABLE_INHERIT
, R_SH_GNU_VTINHERIT
},
955 { BFD_RELOC_VTABLE_ENTRY
, R_SH_GNU_VTENTRY
},
956 { BFD_RELOC_SH_LOOP_START
, R_SH_LOOP_START
},
957 { BFD_RELOC_SH_LOOP_END
, R_SH_LOOP_END
},
958 { BFD_RELOC_32_GOT_PCREL
, R_SH_GOT32
},
959 { BFD_RELOC_32_PLT_PCREL
, R_SH_PLT32
},
960 { BFD_RELOC_SH_COPY
, R_SH_COPY
},
961 { BFD_RELOC_SH_GLOB_DAT
, R_SH_GLOB_DAT
},
962 { BFD_RELOC_SH_JMP_SLOT
, R_SH_JMP_SLOT
},
963 { BFD_RELOC_SH_RELATIVE
, R_SH_RELATIVE
},
964 { BFD_RELOC_32_GOTOFF
, R_SH_GOTOFF
},
965 { BFD_RELOC_SH_GOTPC
, R_SH_GOTPC
},
968 /* Given a BFD reloc code, return the howto structure for the
969 corresponding SH ELf reloc. */
971 static reloc_howto_type
*
972 sh_elf_reloc_type_lookup (abfd
, code
)
973 bfd
*abfd ATTRIBUTE_UNUSED
;
974 bfd_reloc_code_real_type code
;
978 for (i
= 0; i
< sizeof (sh_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
980 if (sh_reloc_map
[i
].bfd_reloc_val
== code
)
981 return &sh_elf_howto_table
[(int) sh_reloc_map
[i
].elf_reloc_val
];
987 /* Given an ELF reloc, fill in the howto field of a relent. */
990 sh_elf_info_to_howto (abfd
, cache_ptr
, dst
)
991 bfd
*abfd ATTRIBUTE_UNUSED
;
993 Elf_Internal_Rela
*dst
;
997 r
= ELF32_R_TYPE (dst
->r_info
);
999 BFD_ASSERT (r
< (unsigned int) R_SH_max
);
1000 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC
|| r
> R_SH_LAST_INVALID_RELOC
);
1001 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_2
|| r
> R_SH_LAST_INVALID_RELOC_2
);
1003 cache_ptr
->howto
= &sh_elf_howto_table
[r
];
1006 /* This function handles relaxing for SH ELF. See the corresponding
1007 function in coff-sh.c for a description of what this does. FIXME:
1008 There is a lot of duplication here between this code and the COFF
1009 specific code. The format of relocs and symbols is wound deeply
1010 into this code, but it would still be better if the duplication
1011 could be eliminated somehow. Note in particular that although both
1012 functions use symbols like R_SH_CODE, those symbols have different
1013 values; in coff-sh.c they come from include/coff/sh.h, whereas here
1014 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
1017 sh_elf_relax_section (abfd
, sec
, link_info
, again
)
1020 struct bfd_link_info
*link_info
;
1023 Elf_Internal_Shdr
*symtab_hdr
;
1024 Elf_Internal_Rela
*internal_relocs
;
1025 Elf_Internal_Rela
*free_relocs
= NULL
;
1027 Elf_Internal_Rela
*irel
, *irelend
;
1028 bfd_byte
*contents
= NULL
;
1029 bfd_byte
*free_contents
= NULL
;
1030 Elf32_External_Sym
*extsyms
= NULL
;
1031 Elf32_External_Sym
*free_extsyms
= NULL
;
1035 if (link_info
->relocateable
1036 || (sec
->flags
& SEC_RELOC
) == 0
1037 || sec
->reloc_count
== 0)
1040 /* If this is the first time we have been called for this section,
1041 initialize the cooked size. */
1042 if (sec
->_cooked_size
== 0)
1043 sec
->_cooked_size
= sec
->_raw_size
;
1045 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1047 internal_relocs
= (_bfd_elf32_link_read_relocs
1048 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1049 link_info
->keep_memory
));
1050 if (internal_relocs
== NULL
)
1052 if (! link_info
->keep_memory
)
1053 free_relocs
= internal_relocs
;
1057 irelend
= internal_relocs
+ sec
->reloc_count
;
1058 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1060 bfd_vma laddr
, paddr
, symval
;
1061 unsigned short insn
;
1062 Elf_Internal_Rela
*irelfn
, *irelscan
, *irelcount
;
1063 bfd_signed_vma foff
;
1065 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_CODE
)
1068 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_USES
)
1071 /* Get the section contents. */
1072 if (contents
== NULL
)
1074 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1075 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1078 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
1079 if (contents
== NULL
)
1081 free_contents
= contents
;
1083 if (! bfd_get_section_contents (abfd
, sec
, contents
,
1084 (file_ptr
) 0, sec
->_raw_size
))
1089 /* The r_addend field of the R_SH_USES reloc will point us to
1090 the register load. The 4 is because the r_addend field is
1091 computed as though it were a jump offset, which are based
1092 from 4 bytes after the jump instruction. */
1093 laddr
= irel
->r_offset
+ 4 + irel
->r_addend
;
1094 if (laddr
>= sec
->_raw_size
)
1096 (*_bfd_error_handler
) (_("%s: 0x%lx: warning: bad R_SH_USES offset"),
1097 bfd_get_filename (abfd
),
1098 (unsigned long) irel
->r_offset
);
1101 insn
= bfd_get_16 (abfd
, contents
+ laddr
);
1103 /* If the instruction is not mov.l NN,rN, we don't know what to
1105 if ((insn
& 0xf000) != 0xd000)
1107 ((*_bfd_error_handler
)
1108 (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
1109 bfd_get_filename (abfd
), (unsigned long) irel
->r_offset
, insn
));
1113 /* Get the address from which the register is being loaded. The
1114 displacement in the mov.l instruction is quadrupled. It is a
1115 displacement from four bytes after the movl instruction, but,
1116 before adding in the PC address, two least significant bits
1117 of the PC are cleared. We assume that the section is aligned
1118 on a four byte boundary. */
1119 paddr
= insn
& 0xff;
1121 paddr
+= (laddr
+ 4) & ~3;
1122 if (paddr
>= sec
->_raw_size
)
1124 ((*_bfd_error_handler
)
1125 (_("%s: 0x%lx: warning: bad R_SH_USES load offset"),
1126 bfd_get_filename (abfd
), (unsigned long) irel
->r_offset
));
1130 /* Get the reloc for the address from which the register is
1131 being loaded. This reloc will tell us which function is
1132 actually being called. */
1133 for (irelfn
= internal_relocs
; irelfn
< irelend
; irelfn
++)
1134 if (irelfn
->r_offset
== paddr
1135 && ELF32_R_TYPE (irelfn
->r_info
) == (int) R_SH_DIR32
)
1137 if (irelfn
>= irelend
)
1139 ((*_bfd_error_handler
)
1140 (_("%s: 0x%lx: warning: could not find expected reloc"),
1141 bfd_get_filename (abfd
), (unsigned long) paddr
));
1145 /* Read this BFD's symbols if we haven't done so already. */
1146 if (extsyms
== NULL
)
1148 if (symtab_hdr
->contents
!= NULL
)
1149 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1152 extsyms
= ((Elf32_External_Sym
*)
1153 bfd_malloc (symtab_hdr
->sh_size
));
1154 if (extsyms
== NULL
)
1156 free_extsyms
= extsyms
;
1157 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1158 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, abfd
)
1159 != symtab_hdr
->sh_size
))
1164 /* Get the value of the symbol referred to by the reloc. */
1165 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
1167 Elf_Internal_Sym isym
;
1169 /* A local symbol. */
1170 bfd_elf32_swap_symbol_in (abfd
,
1171 extsyms
+ ELF32_R_SYM (irelfn
->r_info
),
1174 if (isym
.st_shndx
!= _bfd_elf_section_from_bfd_section (abfd
, sec
))
1176 ((*_bfd_error_handler
)
1177 (_("%s: 0x%lx: warning: symbol in unexpected section"),
1178 bfd_get_filename (abfd
), (unsigned long) paddr
));
1182 symval
= (isym
.st_value
1183 + sec
->output_section
->vma
1184 + sec
->output_offset
);
1189 struct elf_link_hash_entry
*h
;
1191 indx
= ELF32_R_SYM (irelfn
->r_info
) - symtab_hdr
->sh_info
;
1192 h
= elf_sym_hashes (abfd
)[indx
];
1193 BFD_ASSERT (h
!= NULL
);
1194 if (h
->root
.type
!= bfd_link_hash_defined
1195 && h
->root
.type
!= bfd_link_hash_defweak
)
1197 /* This appears to be a reference to an undefined
1198 symbol. Just ignore it--it will be caught by the
1199 regular reloc processing. */
1203 symval
= (h
->root
.u
.def
.value
1204 + h
->root
.u
.def
.section
->output_section
->vma
1205 + h
->root
.u
.def
.section
->output_offset
);
1208 symval
+= bfd_get_32 (abfd
, contents
+ paddr
);
1210 /* See if this function call can be shortened. */
1213 + sec
->output_section
->vma
1214 + sec
->output_offset
1216 if (foff
< -0x1000 || foff
>= 0x1000)
1218 /* After all that work, we can't shorten this function call. */
1222 /* Shorten the function call. */
1224 /* For simplicity of coding, we are going to modify the section
1225 contents, the section relocs, and the BFD symbol table. We
1226 must tell the rest of the code not to free up this
1227 information. It would be possible to instead create a table
1228 of changes which have to be made, as is done in coff-mips.c;
1229 that would be more work, but would require less memory when
1230 the linker is run. */
1232 elf_section_data (sec
)->relocs
= internal_relocs
;
1235 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1236 free_contents
= NULL
;
1238 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1239 free_extsyms
= NULL
;
1241 /* Replace the jsr with a bsr. */
1243 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
1244 replace the jsr with a bsr. */
1245 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irelfn
->r_info
), R_SH_IND12W
);
1246 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
1248 /* If this needs to be changed because of future relaxing,
1249 it will be handled here like other internal IND12W
1252 0xb000 | ((foff
>> 1) & 0xfff),
1253 contents
+ irel
->r_offset
);
1257 /* We can't fully resolve this yet, because the external
1258 symbol value may be changed by future relaxing. We let
1259 the final link phase handle it. */
1260 bfd_put_16 (abfd
, 0xb000, contents
+ irel
->r_offset
);
1263 /* See if there is another R_SH_USES reloc referring to the same
1265 for (irelscan
= internal_relocs
; irelscan
< irelend
; irelscan
++)
1266 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_USES
1267 && laddr
== irelscan
->r_offset
+ 4 + irelscan
->r_addend
)
1269 if (irelscan
< irelend
)
1271 /* Some other function call depends upon this register load,
1272 and we have not yet converted that function call.
1273 Indeed, we may never be able to convert it. There is
1274 nothing else we can do at this point. */
1278 /* Look for a R_SH_COUNT reloc on the location where the
1279 function address is stored. Do this before deleting any
1280 bytes, to avoid confusion about the address. */
1281 for (irelcount
= internal_relocs
; irelcount
< irelend
; irelcount
++)
1282 if (irelcount
->r_offset
== paddr
1283 && ELF32_R_TYPE (irelcount
->r_info
) == (int) R_SH_COUNT
)
1286 /* Delete the register load. */
1287 if (! sh_elf_relax_delete_bytes (abfd
, sec
, laddr
, 2))
1290 /* That will change things, so, just in case it permits some
1291 other function call to come within range, we should relax
1292 again. Note that this is not required, and it may be slow. */
1295 /* Now check whether we got a COUNT reloc. */
1296 if (irelcount
>= irelend
)
1298 ((*_bfd_error_handler
)
1299 (_("%s: 0x%lx: warning: could not find expected COUNT reloc"),
1300 bfd_get_filename (abfd
), (unsigned long) paddr
));
1304 /* The number of uses is stored in the r_addend field. We've
1305 just deleted one. */
1306 if (irelcount
->r_addend
== 0)
1308 ((*_bfd_error_handler
) (_("%s: 0x%lx: warning: bad count"),
1309 bfd_get_filename (abfd
),
1310 (unsigned long) paddr
));
1314 --irelcount
->r_addend
;
1316 /* If there are no more uses, we can delete the address. Reload
1317 the address from irelfn, in case it was changed by the
1318 previous call to sh_elf_relax_delete_bytes. */
1319 if (irelcount
->r_addend
== 0)
1321 if (! sh_elf_relax_delete_bytes (abfd
, sec
, irelfn
->r_offset
, 4))
1325 /* We've done all we can with that function call. */
1328 /* Look for load and store instructions that we can align on four
1334 /* Get the section contents. */
1335 if (contents
== NULL
)
1337 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1338 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1341 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
1342 if (contents
== NULL
)
1344 free_contents
= contents
;
1346 if (! bfd_get_section_contents (abfd
, sec
, contents
,
1347 (file_ptr
) 0, sec
->_raw_size
))
1352 if (! sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
,
1358 elf_section_data (sec
)->relocs
= internal_relocs
;
1361 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1362 free_contents
= NULL
;
1364 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1365 free_extsyms
= NULL
;
1369 if (free_relocs
!= NULL
)
1375 if (free_contents
!= NULL
)
1377 if (! link_info
->keep_memory
)
1378 free (free_contents
);
1381 /* Cache the section contents for elf_link_input_bfd. */
1382 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1384 free_contents
= NULL
;
1387 if (free_extsyms
!= NULL
)
1389 if (! link_info
->keep_memory
)
1390 free (free_extsyms
);
1393 /* Cache the symbols for elf_link_input_bfd. */
1394 symtab_hdr
->contents
= extsyms
;
1396 free_extsyms
= NULL
;
1402 if (free_relocs
!= NULL
)
1404 if (free_contents
!= NULL
)
1405 free (free_contents
);
1406 if (free_extsyms
!= NULL
)
1407 free (free_extsyms
);
1411 /* Delete some bytes from a section while relaxing. FIXME: There is a
1412 lot of duplication between this function and sh_relax_delete_bytes
1416 sh_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1422 Elf_Internal_Shdr
*symtab_hdr
;
1423 Elf32_External_Sym
*extsyms
;
1426 Elf_Internal_Rela
*irel
, *irelend
;
1427 Elf_Internal_Rela
*irelalign
;
1429 Elf32_External_Sym
*esym
, *esymend
;
1430 struct elf_link_hash_entry
*sym_hash
;
1433 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1434 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1436 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1438 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1440 /* The deletion must stop at the next ALIGN reloc for an aligment
1441 power larger than the number of bytes we are deleting. */
1444 toaddr
= sec
->_cooked_size
;
1446 irel
= elf_section_data (sec
)->relocs
;
1447 irelend
= irel
+ sec
->reloc_count
;
1448 for (; irel
< irelend
; irel
++)
1450 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
1451 && irel
->r_offset
> addr
1452 && count
< (1 << irel
->r_addend
))
1455 toaddr
= irel
->r_offset
;
1460 /* Actually delete the bytes. */
1461 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
1462 if (irelalign
== NULL
)
1463 sec
->_cooked_size
-= count
;
1468 #define NOP_OPCODE (0x0009)
1470 BFD_ASSERT ((count
& 1) == 0);
1471 for (i
= 0; i
< count
; i
+= 2)
1472 bfd_put_16 (abfd
, NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
1475 /* Adjust all the relocs. */
1476 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1478 bfd_vma nraddr
, stop
;
1481 Elf_Internal_Sym sym
;
1482 int off
, adjust
, oinsn
;
1483 bfd_signed_vma voff
= 0;
1486 /* Get the new reloc address. */
1487 nraddr
= irel
->r_offset
;
1488 if ((irel
->r_offset
> addr
1489 && irel
->r_offset
< toaddr
)
1490 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
1491 && irel
->r_offset
== toaddr
))
1494 /* See if this reloc was for the bytes we have deleted, in which
1495 case we no longer care about it. Don't delete relocs which
1496 represent addresses, though. */
1497 if (irel
->r_offset
>= addr
1498 && irel
->r_offset
< addr
+ count
1499 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_ALIGN
1500 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
1501 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_DATA
1502 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_LABEL
)
1503 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1506 /* If this is a PC relative reloc, see if the range it covers
1507 includes the bytes we have deleted. */
1508 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1517 start
= irel
->r_offset
;
1518 insn
= bfd_get_16 (abfd
, contents
+ nraddr
);
1522 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1525 start
= stop
= addr
;
1529 /* If this reloc is against a symbol defined in this
1530 section, and the symbol will not be adjusted below, we
1531 must check the addend to see it will put the value in
1532 range to be adjusted, and hence must be changed. */
1533 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1535 bfd_elf32_swap_symbol_in (abfd
,
1536 extsyms
+ ELF32_R_SYM (irel
->r_info
),
1538 if (sym
.st_shndx
== shndx
1539 && (sym
.st_value
<= addr
1540 || sym
.st_value
>= toaddr
))
1544 val
= bfd_get_32 (abfd
, contents
+ nraddr
);
1545 val
+= sym
.st_value
;
1546 if (val
> addr
&& val
< toaddr
)
1547 bfd_put_32 (abfd
, val
- count
, contents
+ nraddr
);
1550 start
= stop
= addr
;
1557 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
1561 if (ELF32_R_SYM (irel
->r_info
) >= symtab_hdr
->sh_info
)
1562 start
= stop
= addr
;
1568 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
1574 stop
= start
+ 4 + off
* 2;
1579 stop
= (start
& ~(bfd_vma
) 3) + 4 + off
* 4;
1585 /* These relocs types represent
1587 The r_addend field holds the difference between the reloc
1588 address and L1. That is the start of the reloc, and
1589 adding in the contents gives us the top. We must adjust
1590 both the r_offset field and the section contents.
1591 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1592 and the elf bfd r_offset is called r_vaddr. */
1594 stop
= irel
->r_offset
;
1595 start
= (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irel
->r_addend
);
1599 && (stop
<= addr
|| stop
>= toaddr
))
1600 irel
->r_addend
+= count
;
1601 else if (stop
> addr
1603 && (start
<= addr
|| start
>= toaddr
))
1604 irel
->r_addend
-= count
;
1606 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH16
)
1607 voff
= bfd_get_signed_16 (abfd
, contents
+ nraddr
);
1608 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH8
)
1609 voff
= bfd_get_8 (abfd
, contents
+ nraddr
);
1611 voff
= bfd_get_signed_32 (abfd
, contents
+ nraddr
);
1612 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1617 start
= irel
->r_offset
;
1618 stop
= (bfd_vma
) ((bfd_signed_vma
) start
1619 + (long) irel
->r_addend
1626 && (stop
<= addr
|| stop
>= toaddr
))
1628 else if (stop
> addr
1630 && (start
<= addr
|| start
>= toaddr
))
1639 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1648 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1650 bfd_put_16 (abfd
, insn
, contents
+ nraddr
);
1655 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1657 bfd_put_16 (abfd
, insn
, contents
+ nraddr
);
1661 BFD_ASSERT (adjust
== count
|| count
>= 4);
1666 if ((irel
->r_offset
& 3) == 0)
1669 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1671 bfd_put_16 (abfd
, insn
, contents
+ nraddr
);
1676 if (voff
< 0 || voff
>= 0xff)
1678 bfd_put_8 (abfd
, voff
, contents
+ nraddr
);
1683 if (voff
< - 0x8000 || voff
>= 0x8000)
1685 bfd_put_signed_16 (abfd
, voff
, contents
+ nraddr
);
1690 bfd_put_signed_32 (abfd
, voff
, contents
+ nraddr
);
1694 irel
->r_addend
+= adjust
;
1700 ((*_bfd_error_handler
)
1701 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1702 bfd_get_filename (abfd
), (unsigned long) irel
->r_offset
));
1703 bfd_set_error (bfd_error_bad_value
);
1708 irel
->r_offset
= nraddr
;
1711 /* Look through all the other sections. If there contain any IMM32
1712 relocs against internal symbols which we are not going to adjust
1713 below, we may need to adjust the addends. */
1714 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1716 Elf_Internal_Rela
*internal_relocs
;
1717 Elf_Internal_Rela
*irelscan
, *irelscanend
;
1718 bfd_byte
*ocontents
;
1721 || (o
->flags
& SEC_RELOC
) == 0
1722 || o
->reloc_count
== 0)
1725 /* We always cache the relocs. Perhaps, if info->keep_memory is
1726 false, we should free them, if we are permitted to, when we
1727 leave sh_coff_relax_section. */
1728 internal_relocs
= (_bfd_elf32_link_read_relocs
1729 (abfd
, o
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1731 if (internal_relocs
== NULL
)
1735 irelscanend
= internal_relocs
+ o
->reloc_count
;
1736 for (irelscan
= internal_relocs
; irelscan
< irelscanend
; irelscan
++)
1738 Elf_Internal_Sym sym
;
1740 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1741 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_SWITCH32
)
1743 bfd_vma start
, stop
;
1744 bfd_signed_vma voff
;
1746 if (ocontents
== NULL
)
1748 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1749 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1752 /* We always cache the section contents.
1753 Perhaps, if info->keep_memory is false, we
1754 should free them, if we are permitted to,
1755 when we leave sh_coff_relax_section. */
1756 ocontents
= (bfd_byte
*) bfd_malloc (o
->_raw_size
);
1757 if (ocontents
== NULL
)
1759 if (! bfd_get_section_contents (abfd
, o
, ocontents
,
1763 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1767 stop
= irelscan
->r_offset
;
1769 = (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irelscan
->r_addend
);
1771 /* STOP is in a different section, so it won't change. */
1772 if (start
> addr
&& start
< toaddr
)
1773 irelscan
->r_addend
+= count
;
1775 voff
= bfd_get_signed_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1776 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1780 && (stop
<= addr
|| stop
>= toaddr
))
1781 bfd_put_signed_32 (abfd
, voff
+ count
,
1782 ocontents
+ irelscan
->r_offset
);
1783 else if (stop
> addr
1785 && (start
<= addr
|| start
>= toaddr
))
1786 bfd_put_signed_32 (abfd
, voff
- count
,
1787 ocontents
+ irelscan
->r_offset
);
1790 if (ELF32_R_TYPE (irelscan
->r_info
) != (int) R_SH_DIR32
)
1793 if (ELF32_R_SYM (irelscan
->r_info
) >= symtab_hdr
->sh_info
)
1796 bfd_elf32_swap_symbol_in (abfd
,
1797 extsyms
+ ELF32_R_SYM (irelscan
->r_info
),
1800 if (sym
.st_shndx
== shndx
1801 && (sym
.st_value
<= addr
1802 || sym
.st_value
>= toaddr
))
1806 if (ocontents
== NULL
)
1808 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1809 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1812 /* We always cache the section contents.
1813 Perhaps, if info->keep_memory is false, we
1814 should free them, if we are permitted to,
1815 when we leave sh_coff_relax_section. */
1816 ocontents
= (bfd_byte
*) bfd_malloc (o
->_raw_size
);
1817 if (ocontents
== NULL
)
1819 if (! bfd_get_section_contents (abfd
, o
, ocontents
,
1823 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1827 val
= bfd_get_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1828 val
+= sym
.st_value
;
1829 if (val
> addr
&& val
< toaddr
)
1830 bfd_put_32 (abfd
, val
- count
,
1831 ocontents
+ irelscan
->r_offset
);
1836 /* Adjust the local symbols defined in this section. */
1838 esymend
= esym
+ symtab_hdr
->sh_info
;
1839 for (; esym
< esymend
; esym
++)
1841 Elf_Internal_Sym isym
;
1843 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1845 if (isym
.st_shndx
== shndx
1846 && isym
.st_value
> addr
1847 && isym
.st_value
< toaddr
)
1849 isym
.st_value
-= count
;
1850 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
1854 /* Now adjust the global symbols defined in this section. */
1855 esym
= extsyms
+ symtab_hdr
->sh_info
;
1856 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
1857 for (index
= 0; esym
< esymend
; esym
++, index
++)
1859 Elf_Internal_Sym isym
;
1861 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1862 sym_hash
= elf_sym_hashes (abfd
)[index
];
1863 if (isym
.st_shndx
== shndx
1864 && ((sym_hash
)->root
.type
== bfd_link_hash_defined
1865 || (sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1866 && (sym_hash
)->root
.u
.def
.section
== sec
1867 && (sym_hash
)->root
.u
.def
.value
> addr
1868 && (sym_hash
)->root
.u
.def
.value
< toaddr
)
1870 (sym_hash
)->root
.u
.def
.value
-= count
;
1874 /* See if we can move the ALIGN reloc forward. We have adjusted
1875 r_offset for it already. */
1876 if (irelalign
!= NULL
)
1878 bfd_vma alignto
, alignaddr
;
1880 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1881 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1882 1 << irelalign
->r_addend
);
1883 if (alignto
!= alignaddr
)
1885 /* Tail recursion. */
1886 return sh_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1887 alignto
- alignaddr
);
1894 /* Look for loads and stores which we can align to four byte
1895 boundaries. This is like sh_align_loads in coff-sh.c. */
1898 sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
, pswapped
)
1901 Elf_Internal_Rela
*internal_relocs
;
1905 Elf_Internal_Rela
*irel
, *irelend
;
1906 bfd_vma
*labels
= NULL
;
1907 bfd_vma
*label
, *label_end
;
1911 irelend
= internal_relocs
+ sec
->reloc_count
;
1913 /* Get all the addresses with labels on them. */
1914 labels
= (bfd_vma
*) bfd_malloc (sec
->reloc_count
* sizeof (bfd_vma
));
1918 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1920 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_LABEL
)
1922 *label_end
= irel
->r_offset
;
1927 /* Note that the assembler currently always outputs relocs in
1928 address order. If that ever changes, this code will need to sort
1929 the label values and the relocs. */
1933 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1935 bfd_vma start
, stop
;
1937 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
)
1940 start
= irel
->r_offset
;
1942 for (irel
++; irel
< irelend
; irel
++)
1943 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_DATA
)
1946 stop
= irel
->r_offset
;
1948 stop
= sec
->_cooked_size
;
1950 if (! _bfd_sh_align_load_span (abfd
, sec
, contents
, sh_elf_swap_insns
,
1951 (PTR
) internal_relocs
, &label
,
1952 label_end
, start
, stop
, pswapped
))
1966 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1969 sh_elf_swap_insns (abfd
, sec
, relocs
, contents
, addr
)
1976 Elf_Internal_Rela
*internal_relocs
= (Elf_Internal_Rela
*) relocs
;
1977 unsigned short i1
, i2
;
1978 Elf_Internal_Rela
*irel
, *irelend
;
1980 /* Swap the instructions themselves. */
1981 i1
= bfd_get_16 (abfd
, contents
+ addr
);
1982 i2
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1983 bfd_put_16 (abfd
, i2
, contents
+ addr
);
1984 bfd_put_16 (abfd
, i1
, contents
+ addr
+ 2);
1986 /* Adjust all reloc addresses. */
1987 irelend
= internal_relocs
+ sec
->reloc_count
;
1988 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1990 enum elf_sh_reloc_type type
;
1993 /* There are a few special types of relocs that we don't want to
1994 adjust. These relocs do not apply to the instruction itself,
1995 but are only associated with the address. */
1996 type
= (enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
);
1997 if (type
== R_SH_ALIGN
1998 || type
== R_SH_CODE
1999 || type
== R_SH_DATA
2000 || type
== R_SH_LABEL
)
2003 /* If an R_SH_USES reloc points to one of the addresses being
2004 swapped, we must adjust it. It would be incorrect to do this
2005 for a jump, though, since we want to execute both
2006 instructions after the jump. (We have avoided swapping
2007 around a label, so the jump will not wind up executing an
2008 instruction it shouldn't). */
2009 if (type
== R_SH_USES
)
2013 off
= irel
->r_offset
+ 4 + irel
->r_addend
;
2015 irel
->r_offset
+= 2;
2016 else if (off
== addr
+ 2)
2017 irel
->r_offset
-= 2;
2020 if (irel
->r_offset
== addr
)
2022 irel
->r_offset
+= 2;
2025 else if (irel
->r_offset
== addr
+ 2)
2027 irel
->r_offset
-= 2;
2036 unsigned short insn
, oinsn
;
2039 loc
= contents
+ irel
->r_offset
;
2048 insn
= bfd_get_16 (abfd
, loc
);
2051 if ((oinsn
& 0xff00) != (insn
& 0xff00))
2053 bfd_put_16 (abfd
, insn
, loc
);
2057 insn
= bfd_get_16 (abfd
, loc
);
2060 if ((oinsn
& 0xf000) != (insn
& 0xf000))
2062 bfd_put_16 (abfd
, insn
, loc
);
2066 /* This reloc ignores the least significant 3 bits of
2067 the program counter before adding in the offset.
2068 This means that if ADDR is at an even address, the
2069 swap will not affect the offset. If ADDR is an at an
2070 odd address, then the instruction will be crossing a
2071 four byte boundary, and must be adjusted. */
2072 if ((addr
& 3) != 0)
2074 insn
= bfd_get_16 (abfd
, loc
);
2077 if ((oinsn
& 0xff00) != (insn
& 0xff00))
2079 bfd_put_16 (abfd
, insn
, loc
);
2087 ((*_bfd_error_handler
)
2088 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
2089 bfd_get_filename (abfd
), (unsigned long) irel
->r_offset
));
2090 bfd_set_error (bfd_error_bad_value
);
2099 /* The size in bytes of an entry in the procedure linkage table. */
2101 #define PLT_ENTRY_SIZE 28
2103 /* First entry in an absolute procedure linkage table look like this. */
2106 /* Note - this code has been "optimised" not to use r2. r2 is used by
2107 GCC to return the address of large strutcures, so it should not be
2108 corrupted here. This does mean however, that this PLT does not conform
2109 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
2110 and r2 contains the GOT id. This version stores the GOT id in r0 and
2111 ignores the type. Loaders can easily detect this difference however,
2112 since the type will always be 0 or 8, and the GOT ids will always be
2113 greater than or equal to 12. */
2114 static const bfd_byte elf_sh_plt0_entry_be
[PLT_ENTRY_SIZE
] =
2116 0xd0, 0x05, /* mov.l 2f,r0 */
2117 0x60, 0x02, /* mov.l @r0,r0 */
2118 0x2f, 0x06, /* mov.l r0,@-r15 */
2119 0xd0, 0x03, /* mov.l 1f,r0 */
2120 0x60, 0x02, /* mov.l @r0,r0 */
2121 0x40, 0x2b, /* jmp @r0 */
2122 0x60, 0xf6, /* mov.l @r15+,r0 */
2123 0x00, 0x09, /* nop */
2124 0x00, 0x09, /* nop */
2125 0x00, 0x09, /* nop */
2126 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2127 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2130 static const bfd_byte elf_sh_plt0_entry_le
[PLT_ENTRY_SIZE
] =
2132 0x05, 0xd0, /* mov.l 2f,r0 */
2133 0x02, 0x60, /* mov.l @r0,r0 */
2134 0x06, 0x2f, /* mov.l r0,@-r15 */
2135 0x03, 0xd0, /* mov.l 1f,r0 */
2136 0x02, 0x60, /* mov.l @r0,r0 */
2137 0x2b, 0x40, /* jmp @r0 */
2138 0xf6, 0x60, /* mov.l @r15+,r0 */
2139 0x09, 0x00, /* nop */
2140 0x09, 0x00, /* nop */
2141 0x09, 0x00, /* nop */
2142 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2143 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2146 /* Sebsequent entries in an absolute procedure linkage table look like
2149 static const bfd_byte elf_sh_plt_entry_be
[PLT_ENTRY_SIZE
] =
2151 0xd0, 0x04, /* mov.l 1f,r0 */
2152 0x60, 0x02, /* mov.l @r0,r0 */
2153 0xd1, 0x02, /* mov.l 0f,r1 */
2154 0x40, 0x2b, /* jmp @r0 */
2155 0x60, 0x13, /* mov r1,r0 */
2156 0xd1, 0x03, /* mov.l 2f,r1 */
2157 0x40, 0x2b, /* jmp @r0 */
2158 0x00, 0x09, /* nop */
2159 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
2160 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2161 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2164 static const bfd_byte elf_sh_plt_entry_le
[PLT_ENTRY_SIZE
] =
2166 0x04, 0xd0, /* mov.l 1f,r0 */
2167 0x02, 0x60, /* mov.l @r0,r0 */
2168 0x02, 0xd1, /* mov.l 0f,r1 */
2169 0x2b, 0x40, /* jmp @r0 */
2170 0x13, 0x60, /* mov r1,r0 */
2171 0x03, 0xd1, /* mov.l 2f,r1 */
2172 0x2b, 0x40, /* jmp @r0 */
2173 0x09, 0x00, /* nop */
2174 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
2175 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2176 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2179 /* Entries in a PIC procedure linkage table look like this. */
2181 static const bfd_byte elf_sh_pic_plt_entry_be
[PLT_ENTRY_SIZE
] =
2183 0xd0, 0x04, /* mov.l 1f,r0 */
2184 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2185 0x40, 0x2b, /* jmp @r0 */
2186 0x00, 0x09, /* nop */
2187 0x50, 0xc2, /* mov.l @(8,r12),r0 */
2188 0xd1, 0x03, /* mov.l 2f,r1 */
2189 0x40, 0x2b, /* jmp @r0 */
2190 0x50, 0xc1, /* mov.l @(4,r12),r0 */
2191 0x00, 0x09, /* nop */
2192 0x00, 0x09, /* nop */
2193 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2194 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2197 static const bfd_byte elf_sh_pic_plt_entry_le
[PLT_ENTRY_SIZE
] =
2199 0x04, 0xd0, /* mov.l 1f,r0 */
2200 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2201 0x2b, 0x40, /* jmp @r0 */
2202 0x09, 0x00, /* nop */
2203 0xc2, 0x50, /* mov.l @(8,r12),r0 */
2204 0x03, 0xd1, /* mov.l 2f,r1 */
2205 0x2b, 0x40, /* jmp @r0 */
2206 0xc1, 0x50, /* mov.l @(4,r12),r0 */
2207 0x09, 0x00, /* nop */
2208 0x09, 0x00, /* nop */
2209 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2210 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2213 #else /* These are the old style PLT entries. */
2214 static const bfd_byte elf_sh_plt0_entry_be
[PLT_ENTRY_SIZE
] =
2216 0xd0, 0x04, /* mov.l 1f,r0 */
2217 0xd2, 0x05, /* mov.l 2f,r2 */
2218 0x60, 0x02, /* mov.l @r0,r0 */
2219 0x62, 0x22, /* mov.l @r2,r2 */
2220 0x40, 0x2b, /* jmp @r0 */
2221 0xe0, 0x00, /* mov #0,r0 */
2222 0x00, 0x09, /* nop */
2223 0x00, 0x09, /* nop */
2224 0x00, 0x09, /* nop */
2225 0x00, 0x09, /* nop */
2226 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2227 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2230 static const bfd_byte elf_sh_plt0_entry_le
[PLT_ENTRY_SIZE
] =
2232 0x04, 0xd0, /* mov.l 1f,r0 */
2233 0x05, 0xd2, /* mov.l 2f,r2 */
2234 0x02, 0x60, /* mov.l @r0,r0 */
2235 0x22, 0x62, /* mov.l @r2,r2 */
2236 0x2b, 0x40, /* jmp @r0 */
2237 0x00, 0xe0, /* mov #0,r0 */
2238 0x09, 0x00, /* nop */
2239 0x09, 0x00, /* nop */
2240 0x09, 0x00, /* nop */
2241 0x09, 0x00, /* nop */
2242 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2243 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2246 /* Sebsequent entries in an absolute procedure linkage table look like
2249 static const bfd_byte elf_sh_plt_entry_be
[PLT_ENTRY_SIZE
] =
2251 0xd0, 0x04, /* mov.l 1f,r0 */
2252 0x60, 0x02, /* mov.l @r0,r0 */
2253 0xd2, 0x02, /* mov.l 0f,r2 */
2254 0x40, 0x2b, /* jmp @r0 */
2255 0x60, 0x23, /* mov r2,r0 */
2256 0xd1, 0x03, /* mov.l 2f,r1 */
2257 0x40, 0x2b, /* jmp @r0 */
2258 0x00, 0x09, /* nop */
2259 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
2260 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2261 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2264 static const bfd_byte elf_sh_plt_entry_le
[PLT_ENTRY_SIZE
] =
2266 0x04, 0xd0, /* mov.l 1f,r0 */
2267 0x02, 0x60, /* mov.l @r0,r0 */
2268 0x02, 0xd2, /* mov.l 0f,r2 */
2269 0x2b, 0x40, /* jmp @r0 */
2270 0x23, 0x60, /* mov r2,r0 */
2271 0x03, 0xd1, /* mov.l 2f,r1 */
2272 0x2b, 0x40, /* jmp @r0 */
2273 0x09, 0x00, /* nop */
2274 0, 0, 0, 0, /* 0: replaced with address of .PLT. */
2275 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2276 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2279 /* Entries in a PIC procedure linkage table look like this. */
2281 static const bfd_byte elf_sh_pic_plt_entry_be
[PLT_ENTRY_SIZE
] =
2283 0xd0, 0x04, /* mov.l 1f,r0 */
2284 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2285 0x40, 0x2b, /* jmp @r0 */
2286 0x00, 0x09, /* nop */
2287 0x50, 0xc2, /* 0: mov.l @(8,r12),r0 */
2288 0x52, 0xc1, /* 1: mov.l @(4,r12),r2 */
2289 0xd1, 0x02, /* mov.l 2f,r1 */
2290 0x40, 0x2b, /* jmp @r0 */
2291 0xe0, 0x00, /* mov #0,r0 ! shows the type of PLT. */
2292 0x00, 0x09, /* nop */
2293 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2294 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2297 static const bfd_byte elf_sh_pic_plt_entry_le
[PLT_ENTRY_SIZE
] =
2299 0x04, 0xd0, /* mov.l 1f,r0 */
2300 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2301 0x2b, 0x40, /* jmp @r0 */
2302 0x09, 0x00, /* nop */
2303 0xc2, 0x50, /* 0: mov.l @(8,r12),r0 */
2304 0xc1, 0x52, /* 1: mov.l @(4,r12),r2 */
2305 0x02, 0xd1, /* mov.l 2f,r1 */
2306 0x2b, 0x40, /* jmp @r0 */
2307 0x00, 0xe0, /* mov #0,r0 ! shows the type of PLT. */
2308 0x09, 0x00, /* nop */
2309 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2310 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2312 #endif /* old style PLT entries. */
2314 static const bfd_byte
*elf_sh_plt0_entry
;
2315 static const bfd_byte
*elf_sh_plt_entry
;
2316 static const bfd_byte
*elf_sh_pic_plt_entry
;
2318 /* Return size of a PLT entry. */
2319 #define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
2321 /* Return offset of the PLT0 address in an absolute PLT entry. */
2322 #define elf_sh_plt_plt0_offset(info) 16
2324 /* Return offset of the linker in PLT0 entry. */
2325 #define elf_sh_plt0_linker_offset(info) 20
2327 /* Return offset of the GOT id in PLT0 entry. */
2328 #define elf_sh_plt0_gotid_offset(info) 24
2330 /* Return offset of the tempoline in PLT entry */
2331 #define elf_sh_plt_temp_offset(info) 8
2333 /* Return offset of the symbol in PLT entry. */
2334 #define elf_sh_plt_symbol_offset(info) 20
2336 /* Return offset of the relocation in PLT entry. */
2337 #define elf_sh_plt_reloc_offset(info) 24
2339 /* The sh linker needs to keep track of the number of relocs that it
2340 decides to copy in check_relocs for each symbol. This is so that
2341 it can discard PC relative relocs if it doesn't need them when
2342 linking with -Bsymbolic. We store the information in a field
2343 extending the regular ELF linker hash table. */
2345 /* This structure keeps track of the number of PC relative relocs we
2346 have copied for a given symbol. */
2348 struct elf_sh_pcrel_relocs_copied
2351 struct elf_sh_pcrel_relocs_copied
*next
;
2352 /* A section in dynobj. */
2354 /* Number of relocs copied in this section. */
2355 bfd_size_type count
;
2358 /* sh ELF linker hash entry. */
2360 struct elf_sh_link_hash_entry
2362 struct elf_link_hash_entry root
;
2364 /* Number of PC relative relocs copied for this symbol. */
2365 struct elf_sh_pcrel_relocs_copied
*pcrel_relocs_copied
;
2368 /* sh ELF linker hash table. */
2370 struct elf_sh_link_hash_table
2372 struct elf_link_hash_table root
;
2375 /* Declare this now that the above structures are defined. */
2377 static boolean sh_elf_discard_copies
2378 PARAMS ((struct elf_sh_link_hash_entry
*, PTR
));
2380 /* Traverse an sh ELF linker hash table. */
2382 #define sh_elf_link_hash_traverse(table, func, info) \
2383 (elf_link_hash_traverse \
2385 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2388 /* Get the sh ELF linker hash table from a link_info structure. */
2390 #define sh_elf_hash_table(p) \
2391 ((struct elf_sh_link_hash_table *) ((p)->hash))
2393 /* Create an entry in an sh ELF linker hash table. */
2395 static struct bfd_hash_entry
*
2396 sh_elf_link_hash_newfunc (entry
, table
, string
)
2397 struct bfd_hash_entry
*entry
;
2398 struct bfd_hash_table
*table
;
2401 struct elf_sh_link_hash_entry
*ret
=
2402 (struct elf_sh_link_hash_entry
*) entry
;
2404 /* Allocate the structure if it has not already been allocated by a
2406 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2407 ret
= ((struct elf_sh_link_hash_entry
*)
2408 bfd_hash_allocate (table
,
2409 sizeof (struct elf_sh_link_hash_entry
)));
2410 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2411 return (struct bfd_hash_entry
*) ret
;
2413 /* Call the allocation method of the superclass. */
2414 ret
= ((struct elf_sh_link_hash_entry
*)
2415 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2417 if (ret
!= (struct elf_sh_link_hash_entry
*) NULL
)
2419 ret
->pcrel_relocs_copied
= NULL
;
2422 return (struct bfd_hash_entry
*) ret
;
2425 /* Create an sh ELF linker hash table. */
2427 static struct bfd_link_hash_table
*
2428 sh_elf_link_hash_table_create (abfd
)
2431 struct elf_sh_link_hash_table
*ret
;
2433 ret
= ((struct elf_sh_link_hash_table
*)
2434 bfd_alloc (abfd
, sizeof (struct elf_sh_link_hash_table
)));
2435 if (ret
== (struct elf_sh_link_hash_table
*) NULL
)
2438 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2439 sh_elf_link_hash_newfunc
))
2441 bfd_release (abfd
, ret
);
2445 return &ret
->root
.root
;
2448 /* Create dynamic sections when linking against a dynamic object. */
2451 sh_elf_create_dynamic_sections (abfd
, info
)
2453 struct bfd_link_info
*info
;
2455 flagword flags
, pltflags
;
2456 register asection
*s
;
2457 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2460 switch (bed
->s
->arch_size
)
2471 bfd_set_error (bfd_error_bad_value
);
2475 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2476 .rel[a].bss sections. */
2478 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2479 | SEC_LINKER_CREATED
);
2482 pltflags
|= SEC_CODE
;
2483 if (bed
->plt_not_loaded
)
2484 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
2485 if (bed
->plt_readonly
)
2486 pltflags
|= SEC_READONLY
;
2488 s
= bfd_make_section (abfd
, ".plt");
2490 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
2491 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
2494 if (bed
->want_plt_sym
)
2496 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2498 struct elf_link_hash_entry
*h
= NULL
;
2499 if (! (_bfd_generic_link_add_one_symbol
2500 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
2501 (bfd_vma
) 0, (const char *) NULL
, false,
2502 get_elf_backend_data (abfd
)->collect
,
2503 (struct bfd_link_hash_entry
**) &h
)))
2505 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2506 h
->type
= STT_OBJECT
;
2509 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2513 s
= bfd_make_section (abfd
,
2514 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
2516 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2517 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2520 if (! _bfd_elf_create_got_section (abfd
, info
))
2524 const char *secname
;
2529 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2531 secflags
= bfd_get_section_flags (abfd
, sec
);
2532 if ((secflags
& (SEC_DATA
| SEC_LINKER_CREATED
))
2533 || ((secflags
& SEC_HAS_CONTENTS
) != SEC_HAS_CONTENTS
))
2535 secname
= bfd_get_section_name (abfd
, sec
);
2536 relname
= (char *) bfd_malloc (strlen (secname
) + 6);
2537 strcpy (relname
, ".rela");
2538 strcat (relname
, secname
);
2539 s
= bfd_make_section (abfd
, relname
);
2541 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2542 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2547 if (bed
->want_dynbss
)
2549 /* The .dynbss section is a place to put symbols which are defined
2550 by dynamic objects, are referenced by regular objects, and are
2551 not functions. We must allocate space for them in the process
2552 image and use a R_*_COPY reloc to tell the dynamic linker to
2553 initialize them at run time. The linker script puts the .dynbss
2554 section into the .bss section of the final image. */
2555 s
= bfd_make_section (abfd
, ".dynbss");
2557 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
2560 /* The .rel[a].bss section holds copy relocs. This section is not
2561 normally needed. We need to create it here, though, so that the
2562 linker will map it to an output section. We can't just create it
2563 only if we need it, because we will not know whether we need it
2564 until we have seen all the input files, and the first time the
2565 main linker code calls BFD after examining all the input files
2566 (size_dynamic_sections) the input sections have already been
2567 mapped to the output sections. If the section turns out not to
2568 be needed, we can discard it later. We will never need this
2569 section when generating a shared object, since they do not use
2573 s
= bfd_make_section (abfd
,
2574 (bed
->default_use_rela_p
2575 ? ".rela.bss" : ".rel.bss"));
2577 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2578 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2586 /* Adjust a symbol defined by a dynamic object and referenced by a
2587 regular object. The current definition is in some section of the
2588 dynamic object, but we're not including those sections. We have to
2589 change the definition to something the rest of the link can
2593 sh_elf_adjust_dynamic_symbol (info
, h
)
2594 struct bfd_link_info
*info
;
2595 struct elf_link_hash_entry
*h
;
2599 unsigned int power_of_two
;
2601 dynobj
= elf_hash_table (info
)->dynobj
;
2603 /* Make sure we know what is going on here. */
2604 BFD_ASSERT (dynobj
!= NULL
2605 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
2606 || h
->weakdef
!= NULL
2607 || ((h
->elf_link_hash_flags
2608 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2609 && (h
->elf_link_hash_flags
2610 & ELF_LINK_HASH_REF_REGULAR
) != 0
2611 && (h
->elf_link_hash_flags
2612 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
2614 /* If this is a function, put it in the procedure linkage table. We
2615 will fill in the contents of the procedure linkage table later,
2616 when we know the address of the .got section. */
2617 if (h
->type
== STT_FUNC
2618 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
2621 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2622 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
2624 /* This case can occur if we saw a PLT reloc in an input
2625 file, but the symbol was never referred to by a dynamic
2626 object. In such a case, we don't actually need to build
2627 a procedure linkage table, and we can just do a REL32
2629 BFD_ASSERT ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0);
2633 /* Make sure this symbol is output as a dynamic symbol. */
2634 if (h
->dynindx
== -1)
2636 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
2640 s
= bfd_get_section_by_name (dynobj
, ".plt");
2641 BFD_ASSERT (s
!= NULL
);
2643 /* If this is the first .plt entry, make room for the special
2645 if (s
->_raw_size
== 0)
2646 s
->_raw_size
+= PLT_ENTRY_SIZE
;
2648 /* If this symbol is not defined in a regular file, and we are
2649 not generating a shared library, then set the symbol to this
2650 location in the .plt. This is required to make function
2651 pointers compare as equal between the normal executable and
2652 the shared library. */
2654 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2656 h
->root
.u
.def
.section
= s
;
2657 h
->root
.u
.def
.value
= s
->_raw_size
;
2660 h
->plt
.offset
= s
->_raw_size
;
2662 /* Make room for this entry. */
2663 s
->_raw_size
+= elf_sh_sizeof_plt (info
);
2665 /* We also need to make an entry in the .got.plt section, which
2666 will be placed in the .got section by the linker script. */
2668 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
2669 BFD_ASSERT (s
!= NULL
);
2672 /* We also need to make an entry in the .rela.plt section. */
2674 s
= bfd_get_section_by_name (dynobj
, ".rela.plt");
2675 BFD_ASSERT (s
!= NULL
);
2676 s
->_raw_size
+= sizeof (Elf32_External_Rela
);
2681 /* If this is a weak symbol, and there is a real definition, the
2682 processor independent code will have arranged for us to see the
2683 real definition first, and we can just use the same value. */
2684 if (h
->weakdef
!= NULL
)
2686 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
2687 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
2688 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
2689 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
2693 /* This is a reference to a symbol defined by a dynamic object which
2694 is not a function. */
2696 /* If we are creating a shared library, we must presume that the
2697 only references to the symbol are via the global offset table.
2698 For such cases we need not do anything here; the relocations will
2699 be handled correctly by relocate_section. */
2703 /* If there are no references to this symbol that do not use the
2704 GOT, we don't need to generate a copy reloc. */
2705 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0)
2708 /* We must allocate the symbol in our .dynbss section, which will
2709 become part of the .bss section of the executable. There will be
2710 an entry for this symbol in the .dynsym section. The dynamic
2711 object will contain position independent code, so all references
2712 from the dynamic object to this symbol will go through the global
2713 offset table. The dynamic linker will use the .dynsym entry to
2714 determine the address it must put in the global offset table, so
2715 both the dynamic object and the regular object will refer to the
2716 same memory location for the variable. */
2718 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
2719 BFD_ASSERT (s
!= NULL
);
2721 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2722 copy the initial value out of the dynamic object and into the
2723 runtime process image. We need to remember the offset into the
2724 .rela.bss section we are going to use. */
2725 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
2729 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
2730 BFD_ASSERT (srel
!= NULL
);
2731 srel
->_raw_size
+= sizeof (Elf32_External_Rela
);
2732 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
2735 /* We need to figure out the alignment required for this symbol. I
2736 have no idea how ELF linkers handle this. */
2737 power_of_two
= bfd_log2 (h
->size
);
2738 if (power_of_two
> 3)
2741 /* Apply the required alignment. */
2742 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
2743 (bfd_size_type
) (1 << power_of_two
));
2744 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
2746 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
2750 /* Define the symbol as being at this point in the section. */
2751 h
->root
.u
.def
.section
= s
;
2752 h
->root
.u
.def
.value
= s
->_raw_size
;
2754 /* Increment the section size to make room for the symbol. */
2755 s
->_raw_size
+= h
->size
;
2760 /* Set the sizes of the dynamic sections. */
2763 sh_elf_size_dynamic_sections (output_bfd
, info
)
2765 struct bfd_link_info
*info
;
2773 dynobj
= elf_hash_table (info
)->dynobj
;
2774 BFD_ASSERT (dynobj
!= NULL
);
2776 if (elf_hash_table (info
)->dynamic_sections_created
)
2778 /* Set the contents of the .interp section to the interpreter. */
2781 s
= bfd_get_section_by_name (dynobj
, ".interp");
2782 BFD_ASSERT (s
!= NULL
);
2783 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
2784 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2789 /* We may have created entries in the .rela.got section.
2790 However, if we are not creating the dynamic sections, we will
2791 not actually use these entries. Reset the size of .rela.got,
2792 which will cause it to get stripped from the output file
2794 s
= bfd_get_section_by_name (dynobj
, ".rela.got");
2799 /* If this is a -Bsymbolic shared link, then we need to discard all
2800 PC relative relocs against symbols defined in a regular object.
2801 We allocated space for them in the check_relocs routine, but we
2802 will not fill them in in the relocate_section routine. */
2803 if (info
->shared
&& info
->symbolic
)
2804 sh_elf_link_hash_traverse (sh_elf_hash_table (info
),
2805 sh_elf_discard_copies
,
2808 /* The check_relocs and adjust_dynamic_symbol entry points have
2809 determined the sizes of the various dynamic sections. Allocate
2814 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2819 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2822 /* It's OK to base decisions on the section name, because none
2823 of the dynobj section names depend upon the input files. */
2824 name
= bfd_get_section_name (dynobj
, s
);
2828 if (strcmp (name
, ".plt") == 0)
2830 if (s
->_raw_size
== 0)
2832 /* Strip this section if we don't need it; see the
2838 /* Remember whether there is a PLT. */
2842 else if (strncmp (name
, ".rela", 5) == 0)
2844 if (s
->_raw_size
== 0)
2846 /* If we don't need this section, strip it from the
2847 output file. This is mostly to handle .rela.bss and
2848 .rela.plt. We must create both sections in
2849 create_dynamic_sections, because they must be created
2850 before the linker maps input sections to output
2851 sections. The linker does that before
2852 adjust_dynamic_symbol is called, and it is that
2853 function which decides whether anything needs to go
2854 into these sections. */
2861 /* Remember whether there are any reloc sections other
2863 if (strcmp (name
, ".rela.plt") != 0)
2865 const char *outname
;
2869 /* If this relocation section applies to a read only
2870 section, then we probably need a DT_TEXTREL
2871 entry. The entries in the .rela.plt section
2872 really apply to the .got section, which we
2873 created ourselves and so know is not readonly. */
2874 outname
= bfd_get_section_name (output_bfd
,
2876 target
= bfd_get_section_by_name (output_bfd
, outname
+ 5);
2878 && (target
->flags
& SEC_READONLY
) != 0
2879 && (target
->flags
& SEC_ALLOC
) != 0)
2883 /* We use the reloc_count field as a counter if we need
2884 to copy relocs into the output file. */
2888 else if (strncmp (name
, ".got", 4) != 0)
2890 /* It's not one of our sections, so don't allocate space. */
2896 _bfd_strip_section_from_output (info
, s
);
2900 /* Allocate memory for the section contents. */
2901 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
2902 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2906 if (elf_hash_table (info
)->dynamic_sections_created
)
2908 /* Add some entries to the .dynamic section. We fill in the
2909 values later, in sh_elf_finish_dynamic_sections, but we
2910 must add the entries now so that we get the correct size for
2911 the .dynamic section. The DT_DEBUG entry is filled in by the
2912 dynamic linker and used by the debugger. */
2915 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
2921 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0)
2922 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
2923 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_RELA
)
2924 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0))
2930 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELA
, 0)
2931 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELASZ
, 0)
2932 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELAENT
,
2933 sizeof (Elf32_External_Rela
)))
2939 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
2947 /* This function is called via sh_elf_link_hash_traverse if we are
2948 creating a shared object with -Bsymbolic. It discards the space
2949 allocated to copy PC relative relocs against symbols which are
2950 defined in regular objects. We allocated space for them in the
2951 check_relocs routine, but we won't fill them in in the
2952 relocate_section routine. */
2955 sh_elf_discard_copies (h
, ignore
)
2956 struct elf_sh_link_hash_entry
*h
;
2957 PTR ignore ATTRIBUTE_UNUSED
;
2959 struct elf_sh_pcrel_relocs_copied
*s
;
2961 /* We only discard relocs for symbols defined in a regular object. */
2962 if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2965 for (s
= h
->pcrel_relocs_copied
; s
!= NULL
; s
= s
->next
)
2966 s
->section
->_raw_size
-= s
->count
* sizeof (Elf32_External_Rela
);
2971 /* Relocate an SH ELF section. */
2974 sh_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
2975 contents
, relocs
, local_syms
, local_sections
)
2976 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2977 struct bfd_link_info
*info
;
2979 asection
*input_section
;
2981 Elf_Internal_Rela
*relocs
;
2982 Elf_Internal_Sym
*local_syms
;
2983 asection
**local_sections
;
2985 Elf_Internal_Shdr
*symtab_hdr
;
2986 struct elf_link_hash_entry
**sym_hashes
;
2987 Elf_Internal_Rela
*rel
, *relend
;
2989 bfd_vma
*local_got_offsets
;
2994 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2995 sym_hashes
= elf_sym_hashes (input_bfd
);
2996 dynobj
= elf_hash_table (info
)->dynobj
;
2997 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3004 relend
= relocs
+ input_section
->reloc_count
;
3005 for (; rel
< relend
; rel
++)
3008 reloc_howto_type
*howto
;
3009 unsigned long r_symndx
;
3010 Elf_Internal_Sym
*sym
;
3012 struct elf_link_hash_entry
*h
;
3014 bfd_vma addend
= (bfd_vma
) 0;
3015 bfd_reloc_status_type r
;
3017 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3019 r_type
= ELF32_R_TYPE (rel
->r_info
);
3021 /* Many of the relocs are only used for relaxing, and are
3022 handled entirely by the relaxation code. */
3023 if (r_type
> (int) R_SH_LAST_INVALID_RELOC
3024 && r_type
< (int) R_SH_LOOP_START
)
3026 if (r_type
== (int) R_SH_NONE
)
3030 || r_type
>= R_SH_max
3031 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC
3032 && r_type
<= (int) R_SH_LAST_INVALID_RELOC
)
3033 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC_2
3034 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_2
))
3036 bfd_set_error (bfd_error_bad_value
);
3040 howto
= sh_elf_howto_table
+ r_type
;
3042 /* This is a final link. */
3046 if (r_symndx
< symtab_hdr
->sh_info
)
3048 sym
= local_syms
+ r_symndx
;
3049 sec
= local_sections
[r_symndx
];
3050 relocation
= (sec
->output_section
->vma
3051 + sec
->output_offset
3054 if (info
->relocateable
)
3056 /* This is a relocateable link. We don't have to change
3057 anything, unless the reloc is against a section symbol,
3058 in which case we have to adjust according to where the
3059 section symbol winds up in the output section. */
3060 sym
= local_syms
+ r_symndx
;
3061 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3062 goto final_link_relocate
;
3069 /* Section symbol are never (?) placed in the hash table, so
3070 we can just ignore hash relocations when creating a
3071 relocateable object file. */
3072 if (info
->relocateable
)
3075 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3076 while (h
->root
.type
== bfd_link_hash_indirect
3077 || h
->root
.type
== bfd_link_hash_warning
)
3078 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3079 if (h
->root
.type
== bfd_link_hash_defined
3080 || h
->root
.type
== bfd_link_hash_defweak
)
3082 sec
= h
->root
.u
.def
.section
;
3083 /* In these cases, we don't need the relocation value.
3084 We check specially because in some obscure cases
3085 sec->output_section will be NULL. */
3086 if (r_type
== R_SH_GOTPC
3087 || (r_type
== R_SH_PLT32
3088 && h
->plt
.offset
!= (bfd_vma
) -1)
3089 || (r_type
== R_SH_GOT32
3090 && elf_hash_table (info
)->dynamic_sections_created
3092 || (! info
->symbolic
&& h
->dynindx
!= -1)
3093 || (h
->elf_link_hash_flags
3094 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
3095 /* The cases above are those in which relocation is
3096 overwritten in the switch block below. The cases
3097 below are those in which we must defer relocation
3098 to run-time, because we can't resolve absolute
3099 addresses when creating a shared library. */
3101 && ((! info
->symbolic
&& h
->dynindx
!= -1)
3102 || (h
->elf_link_hash_flags
3103 & ELF_LINK_HASH_DEF_REGULAR
) == 0)
3104 && ((r_type
== R_SH_DIR32
3105 && !(ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3106 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
))
3107 || r_type
== R_SH_REL32
)
3108 && ((input_section
->flags
& SEC_ALLOC
) != 0
3109 /* DWARF will emit R_SH_DIR32 relocations in its
3110 sections against symbols defined externally
3111 in shared libraries. We can't do anything
3113 || (input_section
->flags
& SEC_DEBUGGING
) != 0)))
3115 else if (sec
->output_section
== NULL
)
3117 (*_bfd_error_handler
)
3118 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
3119 bfd_get_filename (input_bfd
), h
->root
.root
.string
,
3120 bfd_get_section_name (input_bfd
, input_section
));
3124 relocation
= (h
->root
.u
.def
.value
3125 + sec
->output_section
->vma
3126 + sec
->output_offset
);
3128 else if (h
->root
.type
== bfd_link_hash_undefweak
)
3130 else if (info
->shared
&& !info
->symbolic
&& !info
->no_undefined
)
3134 if (! ((*info
->callbacks
->undefined_symbol
)
3135 (info
, h
->root
.root
.string
, input_bfd
,
3136 input_section
, rel
->r_offset
, true)))
3142 switch ((int) r_type
)
3144 final_link_relocate
:
3145 /* COFF relocs don't use the addend. The addend is used for
3146 R_SH_DIR32 to be compatible with other compilers. */
3147 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3148 contents
, rel
->r_offset
,
3149 relocation
, addend
);
3154 goto final_link_relocate
;
3159 /* If the reloc is against the start of this section, then
3160 the assembler has already taken care of it and the reloc
3161 is here only to assist in relaxing. If the reloc is not
3162 against the start of this section, then it's against an
3163 external symbol and we must deal with it ourselves. */
3164 if (input_section
->output_section
->vma
+ input_section
->output_offset
3167 int disp
= (relocation
3168 - input_section
->output_section
->vma
3169 - input_section
->output_offset
3175 case R_SH_DIR8WPZ
: mask
= 1; break;
3176 case R_SH_DIR8WPL
: mask
= 3; break;
3177 default: mask
= 0; break;
3181 ((*_bfd_error_handler
)
3182 (_("%s: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
3183 bfd_get_filename (input_section
->owner
),
3184 (unsigned long) rel
->r_offset
));
3185 bfd_set_error (bfd_error_bad_value
);
3189 goto final_link_relocate
;
3195 bfd_set_error (bfd_error_bad_value
);
3201 && (input_section
->flags
& SEC_ALLOC
) != 0
3202 && (r_type
!= R_SH_REL32
3205 && (! info
->symbolic
3206 || (h
->elf_link_hash_flags
3207 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
3209 Elf_Internal_Rela outrel
;
3210 boolean skip
, relocate
;
3212 /* When generating a shared object, these relocations
3213 are copied into the output file to be resolved at run
3220 name
= (bfd_elf_string_from_elf_section
3222 elf_elfheader (input_bfd
)->e_shstrndx
,
3223 elf_section_data (input_section
)->rel_hdr
.sh_name
));
3227 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
3228 && strcmp (bfd_get_section_name (input_bfd
,
3232 sreloc
= bfd_get_section_by_name (dynobj
, name
);
3233 BFD_ASSERT (sreloc
!= NULL
);
3238 if (elf_section_data (input_section
)->stab_info
== NULL
)
3239 outrel
.r_offset
= rel
->r_offset
;
3244 off
= (_bfd_stab_section_offset
3245 (output_bfd
, &elf_hash_table (info
)->stab_info
,
3247 &elf_section_data (input_section
)->stab_info
,
3249 if (off
== (bfd_vma
) -1)
3251 outrel
.r_offset
= off
;
3254 outrel
.r_offset
+= (input_section
->output_section
->vma
3255 + input_section
->output_offset
);
3259 memset (&outrel
, 0, sizeof outrel
);
3262 else if (r_type
== R_SH_REL32
)
3264 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
3266 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_REL32
);
3267 outrel
.r_addend
= rel
->r_addend
;
3271 /* h->dynindx may be -1 if this symbol was marked to
3274 || ((info
->symbolic
|| h
->dynindx
== -1)
3275 && (h
->elf_link_hash_flags
3276 & ELF_LINK_HASH_DEF_REGULAR
) != 0))
3279 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3280 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3284 BFD_ASSERT (h
->dynindx
!= -1);
3286 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_DIR32
);
3287 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3291 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3292 (((Elf32_External_Rela
*)
3294 + sreloc
->reloc_count
));
3295 ++sreloc
->reloc_count
;
3297 /* If this reloc is against an external symbol, we do
3298 not want to fiddle with the addend. Otherwise, we
3299 need to include the symbol value so that it becomes
3300 an addend for the dynamic reloc. */
3304 else if (r_type
== R_SH_DIR32
)
3305 addend
= rel
->r_addend
;
3306 goto final_link_relocate
;
3309 /* Relocation is to the entry for this symbol in the global
3313 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3314 BFD_ASSERT (sgot
!= NULL
);
3321 off
= h
->got
.offset
;
3322 BFD_ASSERT (off
!= (bfd_vma
) -1);
3324 if (! elf_hash_table (info
)->dynamic_sections_created
3326 && (info
->symbolic
|| h
->dynindx
== -1
3327 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3328 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3329 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
3331 /* This is actually a static link, or it is a
3332 -Bsymbolic link and the symbol is defined
3333 locally, or the symbol was forced to be local
3334 because of a version file. We must initialize
3335 this entry in the global offset table. Since the
3336 offset must always be a multiple of 4, we use the
3337 least significant bit to record whether we have
3338 initialized it already.
3340 When doing a dynamic link, we create a .rela.got
3341 relocation entry to initialize the value. This
3342 is done in the finish_dynamic_symbol routine. */
3347 bfd_put_32 (output_bfd
, relocation
,
3348 sgot
->contents
+ off
);
3353 relocation
= sgot
->output_offset
+ off
;
3359 BFD_ASSERT (local_got_offsets
!= NULL
3360 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
3362 off
= local_got_offsets
[r_symndx
];
3364 /* The offset must always be a multiple of 4. We use
3365 the least significant bit to record whether we have
3366 already generated the necessary reloc. */
3371 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
3376 Elf_Internal_Rela outrel
;
3378 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
3379 BFD_ASSERT (srelgot
!= NULL
);
3381 outrel
.r_offset
= (sgot
->output_section
->vma
3382 + sgot
->output_offset
3384 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3385 outrel
.r_addend
= relocation
;
3386 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3387 (((Elf32_External_Rela
*)
3389 + srelgot
->reloc_count
));
3390 ++srelgot
->reloc_count
;
3393 local_got_offsets
[r_symndx
] |= 1;
3396 relocation
= sgot
->output_offset
+ off
;
3399 goto final_link_relocate
;
3402 /* Relocation is relative to the start of the global offset
3407 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3408 BFD_ASSERT (sgot
!= NULL
);
3411 /* Note that sgot->output_offset is not involved in this
3412 calculation. We always want the start of .got. If we
3413 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3414 permitted by the ABI, we might have to change this
3416 relocation
-= sgot
->output_section
->vma
;
3418 goto final_link_relocate
;
3421 /* Use global offset table as symbol value. */
3425 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3426 BFD_ASSERT (sgot
!= NULL
);
3429 relocation
= sgot
->output_section
->vma
;
3431 goto final_link_relocate
;
3434 /* Relocation is to the entry for this symbol in the
3435 procedure linkage table. */
3437 /* Resolve a PLT reloc against a local symbol directly,
3438 without using the procedure linkage table. */
3440 goto final_link_relocate
;
3442 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3443 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3444 goto final_link_relocate
;
3446 if (h
->plt
.offset
== (bfd_vma
) -1)
3448 /* We didn't make a PLT entry for this symbol. This
3449 happens when statically linking PIC code, or when
3450 using -Bsymbolic. */
3451 goto final_link_relocate
;
3456 splt
= bfd_get_section_by_name (dynobj
, ".plt");
3457 BFD_ASSERT (splt
!= NULL
);
3460 relocation
= (splt
->output_section
->vma
3461 + splt
->output_offset
3464 goto final_link_relocate
;
3466 case R_SH_LOOP_START
:
3468 static bfd_vma start
, end
;
3470 start
= (relocation
+ rel
->r_addend
3471 - (sec
->output_section
->vma
+ sec
->output_offset
));
3472 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
3473 rel
->r_offset
, sec
, start
, end
);
3477 end
= (relocation
+ rel
->r_addend
3478 - (sec
->output_section
->vma
+ sec
->output_offset
));
3479 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
3480 rel
->r_offset
, sec
, start
, end
);
3485 if (r
!= bfd_reloc_ok
)
3490 case bfd_reloc_outofrange
:
3492 case bfd_reloc_overflow
:
3497 name
= h
->root
.root
.string
;
3500 name
= (bfd_elf_string_from_elf_section
3501 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
3505 name
= bfd_section_name (input_bfd
, sec
);
3507 if (! ((*info
->callbacks
->reloc_overflow
)
3508 (info
, name
, howto
->name
, (bfd_vma
) 0,
3509 input_bfd
, input_section
, rel
->r_offset
)))
3520 /* This is a version of bfd_generic_get_relocated_section_contents
3521 which uses sh_elf_relocate_section. */
3524 sh_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
3525 data
, relocateable
, symbols
)
3527 struct bfd_link_info
*link_info
;
3528 struct bfd_link_order
*link_order
;
3530 boolean relocateable
;
3533 Elf_Internal_Shdr
*symtab_hdr
;
3534 asection
*input_section
= link_order
->u
.indirect
.section
;
3535 bfd
*input_bfd
= input_section
->owner
;
3536 asection
**sections
= NULL
;
3537 Elf_Internal_Rela
*internal_relocs
= NULL
;
3538 Elf32_External_Sym
*external_syms
= NULL
;
3539 Elf_Internal_Sym
*internal_syms
= NULL
;
3541 /* We only need to handle the case of relaxing, or of having a
3542 particular set of section contents, specially. */
3544 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
3545 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
3550 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3552 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
3553 input_section
->_raw_size
);
3555 if ((input_section
->flags
& SEC_RELOC
) != 0
3556 && input_section
->reloc_count
> 0)
3558 Elf_Internal_Sym
*isymp
;
3560 Elf32_External_Sym
*esym
, *esymend
;
3562 if (symtab_hdr
->contents
!= NULL
)
3563 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
3566 external_syms
= ((Elf32_External_Sym
*)
3567 bfd_malloc (symtab_hdr
->sh_info
3568 * sizeof (Elf32_External_Sym
)));
3569 if (external_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
3571 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
3572 || (bfd_read (external_syms
, sizeof (Elf32_External_Sym
),
3573 symtab_hdr
->sh_info
, input_bfd
)
3574 != (symtab_hdr
->sh_info
* sizeof (Elf32_External_Sym
))))
3578 internal_relocs
= (_bfd_elf32_link_read_relocs
3579 (input_bfd
, input_section
, (PTR
) NULL
,
3580 (Elf_Internal_Rela
*) NULL
, false));
3581 if (internal_relocs
== NULL
)
3584 internal_syms
= ((Elf_Internal_Sym
*)
3585 bfd_malloc (symtab_hdr
->sh_info
3586 * sizeof (Elf_Internal_Sym
)));
3587 if (internal_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
3590 sections
= (asection
**) bfd_malloc (symtab_hdr
->sh_info
3591 * sizeof (asection
*));
3592 if (sections
== NULL
&& symtab_hdr
->sh_info
> 0)
3595 isymp
= internal_syms
;
3597 esym
= external_syms
;
3598 esymend
= esym
+ symtab_hdr
->sh_info
;
3599 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
3603 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
3605 if (isymp
->st_shndx
== SHN_UNDEF
)
3606 isec
= bfd_und_section_ptr
;
3607 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
3608 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
3609 else if (isymp
->st_shndx
== SHN_ABS
)
3610 isec
= bfd_abs_section_ptr
;
3611 else if (isymp
->st_shndx
== SHN_COMMON
)
3612 isec
= bfd_com_section_ptr
;
3622 if (! sh_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
3623 input_section
, data
, internal_relocs
,
3624 internal_syms
, sections
))
3627 if (sections
!= NULL
)
3630 if (internal_syms
!= NULL
)
3631 free (internal_syms
);
3632 internal_syms
= NULL
;
3633 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
3634 free (external_syms
);
3635 external_syms
= NULL
;
3636 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
3637 free (internal_relocs
);
3638 internal_relocs
= NULL
;
3644 if (internal_relocs
!= NULL
3645 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
3646 free (internal_relocs
);
3647 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
3648 free (external_syms
);
3649 if (internal_syms
!= NULL
)
3650 free (internal_syms
);
3651 if (sections
!= NULL
)
3657 sh_elf_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
3659 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
3660 Elf_Internal_Rela
*rel
;
3661 struct elf_link_hash_entry
*h
;
3662 Elf_Internal_Sym
*sym
;
3666 switch (ELF32_R_TYPE (rel
->r_info
))
3668 case R_SH_GNU_VTINHERIT
:
3669 case R_SH_GNU_VTENTRY
:
3673 switch (h
->root
.type
)
3675 case bfd_link_hash_defined
:
3676 case bfd_link_hash_defweak
:
3677 return h
->root
.u
.def
.section
;
3679 case bfd_link_hash_common
:
3680 return h
->root
.u
.c
.p
->section
;
3689 if (!(elf_bad_symtab (abfd
)
3690 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
3691 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
3692 && sym
->st_shndx
!= SHN_COMMON
))
3693 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
3698 /* Update the got entry reference counts for the section being removed. */
3701 sh_elf_gc_sweep_hook (abfd
, info
, sec
, relocs
)
3702 bfd
*abfd ATTRIBUTE_UNUSED
;
3703 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
3704 asection
*sec ATTRIBUTE_UNUSED
;
3705 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
;
3707 /* We use got and plt entries for sh, but it would seem that the
3708 existing SH code does no sort of reference counting or whatnot on
3709 its GOT and PLT entries, so it is not possible to garbage collect
3710 them at this time. */
3714 /* Look through the relocs for a section during the first phase.
3715 Since we don't do .gots or .plts, we just need to consider the
3716 virtual table relocs for gc. */
3719 sh_elf_check_relocs (abfd
, info
, sec
, relocs
)
3721 struct bfd_link_info
*info
;
3723 const Elf_Internal_Rela
*relocs
;
3725 Elf_Internal_Shdr
*symtab_hdr
;
3726 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
3727 const Elf_Internal_Rela
*rel
;
3728 const Elf_Internal_Rela
*rel_end
;
3730 bfd_vma
*local_got_offsets
;
3739 if (info
->relocateable
)
3742 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3743 sym_hashes
= elf_sym_hashes (abfd
);
3744 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
3745 if (!elf_bad_symtab (abfd
))
3746 sym_hashes_end
-= symtab_hdr
->sh_info
;
3748 dynobj
= elf_hash_table (info
)->dynobj
;
3749 local_got_offsets
= elf_local_got_offsets (abfd
);
3751 rel_end
= relocs
+ sec
->reloc_count
;
3752 for (rel
= relocs
; rel
< rel_end
; rel
++)
3754 struct elf_link_hash_entry
*h
;
3755 unsigned long r_symndx
;
3757 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3758 if (r_symndx
< symtab_hdr
->sh_info
)
3761 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3763 /* Some relocs require a global offset table. */
3766 switch (ELF32_R_TYPE (rel
->r_info
))
3771 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
3772 if (! _bfd_elf_create_got_section (dynobj
, info
))
3781 switch (ELF32_R_TYPE (rel
->r_info
))
3783 /* This relocation describes the C++ object vtable hierarchy.
3784 Reconstruct it for later use during GC. */
3785 case R_SH_GNU_VTINHERIT
:
3786 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
3790 /* This relocation describes which C++ vtable entries are actually
3791 used. Record for later use during GC. */
3792 case R_SH_GNU_VTENTRY
:
3793 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
3798 /* This symbol requires a global offset table entry. */
3802 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3803 BFD_ASSERT (sgot
!= NULL
);
3807 && (h
!= NULL
|| info
->shared
))
3809 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
3810 if (srelgot
== NULL
)
3812 srelgot
= bfd_make_section (dynobj
, ".rela.got");
3814 || ! bfd_set_section_flags (dynobj
, srelgot
,
3819 | SEC_LINKER_CREATED
3821 || ! bfd_set_section_alignment (dynobj
, srelgot
, 2))
3828 if (h
->got
.offset
!= (bfd_vma
) -1)
3830 /* We have already allocated space in the .got. */
3833 h
->got
.offset
= sgot
->_raw_size
;
3835 /* Make sure this symbol is output as a dynamic symbol. */
3836 if (h
->dynindx
== -1)
3838 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
3842 srelgot
->_raw_size
+= sizeof (Elf32_External_Rela
);
3846 /* This is a global offset table entry for a local
3848 if (local_got_offsets
== NULL
)
3851 register unsigned int i
;
3853 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
3854 local_got_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
3855 if (local_got_offsets
== NULL
)
3857 elf_local_got_offsets (abfd
) = local_got_offsets
;
3858 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3859 local_got_offsets
[i
] = (bfd_vma
) -1;
3861 if (local_got_offsets
[r_symndx
] != (bfd_vma
) -1)
3863 /* We have already allocated space in the .got. */
3866 local_got_offsets
[r_symndx
] = sgot
->_raw_size
;
3870 /* If we are generating a shared object, we need to
3871 output a R_SH_RELATIVE reloc so that the dynamic
3872 linker can adjust this GOT entry. */
3873 srelgot
->_raw_size
+= sizeof (Elf32_External_Rela
);
3877 sgot
->_raw_size
+= 4;
3882 /* This symbol requires a procedure linkage table entry. We
3883 actually build the entry in adjust_dynamic_symbol,
3884 because this might be a case of linking PIC code which is
3885 never referenced by a dynamic object, in which case we
3886 don't need to generate a procedure linkage table entry
3889 /* If this is a local symbol, we resolve it directly without
3890 creating a procedure linkage table entry. */
3894 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3895 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3898 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
3905 h
->elf_link_hash_flags
|= ELF_LINK_NON_GOT_REF
;
3907 /* If we are creating a shared library, and this is a reloc
3908 against a global symbol, or a non PC relative reloc
3909 against a local symbol, then we need to copy the reloc
3910 into the shared library. However, if we are linking with
3911 -Bsymbolic, we do not need to copy a reloc against a
3912 global symbol which is defined in an object we are
3913 including in the link (i.e., DEF_REGULAR is set). At
3914 this point we have not seen all the input files, so it is
3915 possible that DEF_REGULAR is not set now but will be set
3916 later (it is never cleared). We account for that
3917 possibility below by storing information in the
3918 pcrel_relocs_copied field of the hash table entry. */
3920 && (sec
->flags
& SEC_ALLOC
) != 0
3921 && (ELF32_R_TYPE (rel
->r_info
) != R_SH_REL32
3923 && (! info
->symbolic
3924 || (h
->elf_link_hash_flags
3925 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
3927 /* When creating a shared object, we must copy these
3928 reloc types into the output file. We create a reloc
3929 section in dynobj and make room for this reloc. */
3934 name
= (bfd_elf_string_from_elf_section
3936 elf_elfheader (abfd
)->e_shstrndx
,
3937 elf_section_data (sec
)->rel_hdr
.sh_name
));
3941 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
3942 && strcmp (bfd_get_section_name (abfd
, sec
),
3945 sreloc
= bfd_get_section_by_name (dynobj
, name
);
3950 sreloc
= bfd_make_section (dynobj
, name
);
3951 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
3952 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
3953 if ((sec
->flags
& SEC_ALLOC
) != 0)
3954 flags
|= SEC_ALLOC
| SEC_LOAD
;
3956 || ! bfd_set_section_flags (dynobj
, sreloc
, flags
)
3957 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
3962 sreloc
->_raw_size
+= sizeof (Elf32_External_Rela
);
3964 /* If we are linking with -Bsymbolic, and this is a
3965 global symbol, we count the number of PC relative
3966 relocations we have entered for this symbol, so that
3967 we can discard them again if the symbol is later
3968 defined by a regular object. Note that this function
3969 is only called if we are using an elf_sh linker
3970 hash table, which means that h is really a pointer to
3971 an elf_sh_link_hash_entry. */
3972 if (h
!= NULL
&& info
->symbolic
3973 && ELF32_R_TYPE (rel
->r_info
) == R_SH_REL32
)
3975 struct elf_sh_link_hash_entry
*eh
;
3976 struct elf_sh_pcrel_relocs_copied
*p
;
3978 eh
= (struct elf_sh_link_hash_entry
*) h
;
3980 for (p
= eh
->pcrel_relocs_copied
; p
!= NULL
; p
= p
->next
)
3981 if (p
->section
== sreloc
)
3986 p
= ((struct elf_sh_pcrel_relocs_copied
*)
3987 bfd_alloc (dynobj
, sizeof *p
));
3990 p
->next
= eh
->pcrel_relocs_copied
;
3991 eh
->pcrel_relocs_copied
= p
;
3992 p
->section
= sreloc
;
4008 sh_elf_set_mach_from_flags (abfd
)
4011 flagword flags
= elf_elfheader (abfd
)->e_flags
;
4013 switch (flags
& EF_SH_MACH_MASK
)
4016 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh
);
4019 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh2
);
4022 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh_dsp
);
4025 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh3
);
4028 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh3_dsp
);
4031 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh3e
);
4035 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh4
);
4043 /* Function to keep SH specific file flags. */
4046 sh_elf_set_private_flags (abfd
, flags
)
4050 BFD_ASSERT (! elf_flags_init (abfd
)
4051 || elf_elfheader (abfd
)->e_flags
== flags
);
4053 elf_elfheader (abfd
)->e_flags
= flags
;
4054 elf_flags_init (abfd
) = true;
4055 return sh_elf_set_mach_from_flags (abfd
);
4058 /* Copy backend specific data from one object module to another */
4061 sh_elf_copy_private_data (ibfd
, obfd
)
4065 if ( bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4066 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4069 return sh_elf_set_private_flags (obfd
, elf_elfheader (ibfd
)->e_flags
);
4072 /* This routine checks for linking big and little endian objects
4073 together, and for linking sh-dsp with sh3e / sh4 objects. */
4076 sh_elf_merge_private_data (ibfd
, obfd
)
4080 flagword old_flags
, new_flags
;
4082 if (_bfd_generic_verify_endian_match (ibfd
, obfd
) == false)
4085 if ( bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4086 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4089 if (! elf_flags_init (obfd
))
4091 /* This happens when ld starts out with a 'blank' output file. */
4092 elf_flags_init (obfd
) = true;
4093 elf_elfheader (obfd
)->e_flags
= EF_SH1
;
4095 old_flags
= elf_elfheader (obfd
)->e_flags
;
4096 new_flags
= elf_elfheader (ibfd
)->e_flags
;
4097 if ((EF_SH_HAS_DSP (old_flags
) && EF_SH_HAS_FP (new_flags
))
4098 || (EF_SH_HAS_DSP (new_flags
) && EF_SH_HAS_FP (old_flags
)))
4100 (*_bfd_error_handler
)
4101 ("%s: uses %s instructions while previous modules use %s instructions",
4102 bfd_get_filename (ibfd
),
4103 EF_SH_HAS_DSP (new_flags
) ? "dsp" : "floating point",
4104 EF_SH_HAS_DSP (new_flags
) ? "floating point" : "dsp");
4105 bfd_set_error (bfd_error_bad_value
);
4108 elf_elfheader (obfd
)->e_flags
= EF_SH_MERGE_MACH (old_flags
, new_flags
);
4110 return sh_elf_set_mach_from_flags (obfd
);
4113 /* Finish up dynamic symbol handling. We set the contents of various
4114 dynamic sections here. */
4117 sh_elf_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
4119 struct bfd_link_info
*info
;
4120 struct elf_link_hash_entry
*h
;
4121 Elf_Internal_Sym
*sym
;
4125 dynobj
= elf_hash_table (info
)->dynobj
;
4127 if (h
->plt
.offset
!= (bfd_vma
) -1)
4135 Elf_Internal_Rela rel
;
4137 /* This symbol has an entry in the procedure linkage table. Set
4140 BFD_ASSERT (h
->dynindx
!= -1);
4142 splt
= bfd_get_section_by_name (dynobj
, ".plt");
4143 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
4144 srel
= bfd_get_section_by_name (dynobj
, ".rela.plt");
4145 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
4147 /* Get the index in the procedure linkage table which
4148 corresponds to this symbol. This is the index of this symbol
4149 in all the symbols for which we are making plt entries. The
4150 first entry in the procedure linkage table is reserved. */
4151 plt_index
= h
->plt
.offset
/ elf_sh_sizeof_plt (info
) - 1;
4153 /* Get the offset into the .got table of the entry that
4154 corresponds to this function. Each .got entry is 4 bytes.
4155 The first three are reserved. */
4156 got_offset
= (plt_index
+ 3) * 4;
4158 /* Fill in the entry in the procedure linkage table. */
4161 if (elf_sh_plt_entry
== NULL
)
4163 elf_sh_plt_entry
= (bfd_big_endian (output_bfd
) ?
4164 elf_sh_plt_entry_be
: elf_sh_plt_entry_le
);
4166 memcpy (splt
->contents
+ h
->plt
.offset
, elf_sh_plt_entry
,
4167 elf_sh_sizeof_plt (info
));
4168 bfd_put_32 (output_bfd
,
4169 (sgot
->output_section
->vma
4170 + sgot
->output_offset
4172 (splt
->contents
+ h
->plt
.offset
4173 + elf_sh_plt_symbol_offset (info
)));
4175 bfd_put_32 (output_bfd
,
4176 (splt
->output_section
->vma
+ splt
->output_offset
),
4177 (splt
->contents
+ h
->plt
.offset
4178 + elf_sh_plt_plt0_offset (info
)));
4182 if (elf_sh_pic_plt_entry
== NULL
)
4184 elf_sh_pic_plt_entry
= (bfd_big_endian (output_bfd
) ?
4185 elf_sh_pic_plt_entry_be
:
4186 elf_sh_pic_plt_entry_le
);
4188 memcpy (splt
->contents
+ h
->plt
.offset
, elf_sh_pic_plt_entry
,
4189 elf_sh_sizeof_plt (info
));
4190 bfd_put_32 (output_bfd
, got_offset
,
4191 (splt
->contents
+ h
->plt
.offset
4192 + elf_sh_plt_symbol_offset (info
)));
4195 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rela
),
4196 (splt
->contents
+ h
->plt
.offset
4197 + elf_sh_plt_reloc_offset (info
)));
4199 /* Fill in the entry in the global offset table. */
4200 bfd_put_32 (output_bfd
,
4201 (splt
->output_section
->vma
4202 + splt
->output_offset
4204 + elf_sh_plt_temp_offset (info
)),
4205 sgot
->contents
+ got_offset
);
4207 /* Fill in the entry in the .rela.plt section. */
4208 rel
.r_offset
= (sgot
->output_section
->vma
4209 + sgot
->output_offset
4211 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_JMP_SLOT
);
4213 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4214 ((Elf32_External_Rela
*) srel
->contents
4217 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4219 /* Mark the symbol as undefined, rather than as defined in
4220 the .plt section. Leave the value alone. */
4221 sym
->st_shndx
= SHN_UNDEF
;
4225 if (h
->got
.offset
!= (bfd_vma
) -1)
4229 Elf_Internal_Rela rel
;
4231 /* This symbol has an entry in the global offset table. Set it
4234 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4235 srel
= bfd_get_section_by_name (dynobj
, ".rela.got");
4236 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
4238 rel
.r_offset
= (sgot
->output_section
->vma
4239 + sgot
->output_offset
4240 + (h
->got
.offset
&~ 1));
4242 /* If this is a -Bsymbolic link, and the symbol is defined
4243 locally, we just want to emit a RELATIVE reloc. Likewise if
4244 the symbol was forced to be local because of a version file.
4245 The entry in the global offset table will already have been
4246 initialized in the relocate_section function. */
4248 && (info
->symbolic
|| h
->dynindx
== -1)
4249 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
))
4251 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4252 rel
.r_addend
= (h
->root
.u
.def
.value
4253 + h
->root
.u
.def
.section
->output_section
->vma
4254 + h
->root
.u
.def
.section
->output_offset
);
4258 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
4259 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
4263 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4264 ((Elf32_External_Rela
*) srel
->contents
4265 + srel
->reloc_count
));
4266 ++srel
->reloc_count
;
4269 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
4272 Elf_Internal_Rela rel
;
4274 /* This symbol needs a copy reloc. Set it up. */
4276 BFD_ASSERT (h
->dynindx
!= -1
4277 && (h
->root
.type
== bfd_link_hash_defined
4278 || h
->root
.type
== bfd_link_hash_defweak
));
4280 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
4282 BFD_ASSERT (s
!= NULL
);
4284 rel
.r_offset
= (h
->root
.u
.def
.value
4285 + h
->root
.u
.def
.section
->output_section
->vma
4286 + h
->root
.u
.def
.section
->output_offset
);
4287 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_COPY
);
4289 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4290 ((Elf32_External_Rela
*) s
->contents
4295 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4296 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
4297 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
4298 sym
->st_shndx
= SHN_ABS
;
4303 /* Finish up the dynamic sections. */
4306 sh_elf_finish_dynamic_sections (output_bfd
, info
)
4308 struct bfd_link_info
*info
;
4314 dynobj
= elf_hash_table (info
)->dynobj
;
4316 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
4317 BFD_ASSERT (sgot
!= NULL
);
4318 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
4320 if (elf_hash_table (info
)->dynamic_sections_created
)
4323 Elf32_External_Dyn
*dyncon
, *dynconend
;
4325 BFD_ASSERT (sdyn
!= NULL
);
4327 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
4328 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
4329 for (; dyncon
< dynconend
; dyncon
++)
4331 Elf_Internal_Dyn dyn
;
4335 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4349 s
= bfd_get_section_by_name (output_bfd
, name
);
4350 BFD_ASSERT (s
!= NULL
);
4351 dyn
.d_un
.d_ptr
= s
->vma
;
4352 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4356 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4357 BFD_ASSERT (s
!= NULL
);
4358 if (s
->_cooked_size
!= 0)
4359 dyn
.d_un
.d_val
= s
->_cooked_size
;
4361 dyn
.d_un
.d_val
= s
->_raw_size
;
4362 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4366 /* My reading of the SVR4 ABI indicates that the
4367 procedure linkage table relocs (DT_JMPREL) should be
4368 included in the overall relocs (DT_RELA). This is
4369 what Solaris does. However, UnixWare can not handle
4370 that case. Therefore, we override the DT_RELASZ entry
4371 here to make it not include the JMPREL relocs. Since
4372 the linker script arranges for .rela.plt to follow all
4373 other relocation sections, we don't have to worry
4374 about changing the DT_RELA entry. */
4375 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4378 if (s
->_cooked_size
!= 0)
4379 dyn
.d_un
.d_val
-= s
->_cooked_size
;
4381 dyn
.d_un
.d_val
-= s
->_raw_size
;
4383 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4388 /* Fill in the first entry in the procedure linkage table. */
4389 splt
= bfd_get_section_by_name (dynobj
, ".plt");
4390 if (splt
&& splt
->_raw_size
> 0)
4394 if (elf_sh_pic_plt_entry
== NULL
)
4396 elf_sh_pic_plt_entry
= (bfd_big_endian (output_bfd
) ?
4397 elf_sh_pic_plt_entry_be
:
4398 elf_sh_pic_plt_entry_le
);
4400 memcpy (splt
->contents
, elf_sh_pic_plt_entry
,
4401 elf_sh_sizeof_plt (info
));
4405 if (elf_sh_plt0_entry
== NULL
)
4407 elf_sh_plt0_entry
= (bfd_big_endian (output_bfd
) ?
4408 elf_sh_plt0_entry_be
:
4409 elf_sh_plt0_entry_le
);
4411 memcpy (splt
->contents
, elf_sh_plt0_entry
, PLT_ENTRY_SIZE
);
4412 bfd_put_32 (output_bfd
,
4413 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
4414 splt
->contents
+ elf_sh_plt0_gotid_offset (info
));
4415 bfd_put_32 (output_bfd
,
4416 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
4417 splt
->contents
+ elf_sh_plt0_linker_offset (info
));
4420 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4421 really seem like the right value. */
4422 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
4426 /* Fill in the first three entries in the global offset table. */
4427 if (sgot
->_raw_size
> 0)
4430 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
4432 bfd_put_32 (output_bfd
,
4433 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4435 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
4436 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
4439 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
4445 #define TARGET_BIG_SYM bfd_elf32_sh_vec
4446 #define TARGET_BIG_NAME "elf32-sh"
4447 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
4448 #define TARGET_LITTLE_NAME "elf32-shl"
4449 #define ELF_ARCH bfd_arch_sh
4450 #define ELF_MACHINE_CODE EM_SH
4451 #define ELF_MAXPAGESIZE 128
4453 #define elf_symbol_leading_char '_'
4454 #endif /* ELF_ARCH */
4456 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
4457 #define elf_info_to_howto sh_elf_info_to_howto
4458 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
4459 #define elf_backend_relocate_section sh_elf_relocate_section
4460 #define bfd_elf32_bfd_get_relocated_section_contents \
4461 sh_elf_get_relocated_section_contents
4462 #define elf_backend_object_p sh_elf_set_mach_from_flags
4463 #define bfd_elf32_bfd_set_private_bfd_flags \
4464 sh_elf_set_private_flags
4465 #define bfd_elf32_bfd_copy_private_bfd_data \
4466 sh_elf_copy_private_data
4467 #define bfd_elf32_bfd_merge_private_bfd_data \
4468 sh_elf_merge_private_data
4470 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
4471 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
4472 #define elf_backend_check_relocs sh_elf_check_relocs
4474 #define elf_backend_can_gc_sections 1
4475 #define elf_backend_create_dynamic_sections \
4476 sh_elf_create_dynamic_sections
4477 #define bfd_elf32_bfd_link_hash_table_create \
4478 sh_elf_link_hash_table_create
4479 #define elf_backend_adjust_dynamic_symbol \
4480 sh_elf_adjust_dynamic_symbol
4481 #define elf_backend_size_dynamic_sections \
4482 sh_elf_size_dynamic_sections
4483 #define elf_backend_finish_dynamic_symbol \
4484 sh_elf_finish_dynamic_symbol
4485 #define elf_backend_finish_dynamic_sections \
4486 sh_elf_finish_dynamic_sections
4488 #define elf_backend_want_got_plt 1
4489 #define elf_backend_plt_readonly 1
4490 #define elf_backend_want_plt_sym 0
4491 #define elf_backend_got_header_size 12
4492 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
4493 #include "elf32-target.h"