1 /* Hitachi SH specific support for 32-bit ELF
2 Copyright 1996, 97, 98, 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 static bfd_reloc_status_type sh_elf_reloc
29 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
30 static bfd_reloc_status_type sh_elf_ignore_reloc
31 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
32 static reloc_howto_type
*sh_elf_reloc_type_lookup
33 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
34 static void sh_elf_info_to_howto
35 PARAMS ((bfd
*, arelent
*, Elf_Internal_Rela
*));
36 static boolean sh_elf_set_private_flags
37 PARAMS ((bfd
*, flagword
));
38 static boolean sh_elf_copy_private_data
39 PARAMS ((bfd
*, bfd
*));
40 static boolean sh_elf_merge_private_data
41 PARAMS ((bfd
*, bfd
*));
42 static boolean sh_elf_set_mach_from_flags
44 static boolean sh_elf_relax_section
45 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, boolean
*));
46 static boolean sh_elf_relax_delete_bytes
47 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
48 static boolean sh_elf_align_loads
49 PARAMS ((bfd
*, asection
*, Elf_Internal_Rela
*, bfd_byte
*, boolean
*));
50 static boolean sh_elf_swap_insns
51 PARAMS ((bfd
*, asection
*, PTR
, bfd_byte
*, bfd_vma
));
52 static boolean sh_elf_relocate_section
53 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
54 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
55 static bfd_byte
*sh_elf_get_relocated_section_contents
56 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
57 bfd_byte
*, boolean
, asymbol
**));
58 static boolean sh_elf_check_relocs
59 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
60 const Elf_Internal_Rela
*));
61 static struct bfd_hash_entry
*sh_elf_link_hash_newfunc
62 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
63 static struct bfd_link_hash_table
*sh_elf_link_hash_table_create
65 static boolean sh_elf_adjust_dynamic_symbol
66 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
67 static boolean sh_elf_size_dynamic_sections
68 PARAMS ((bfd
*, struct bfd_link_info
*));
69 static boolean sh_elf_finish_dynamic_symbol
70 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
72 static boolean sh_elf_finish_dynamic_sections
73 PARAMS ((bfd
*, struct bfd_link_info
*));
75 /* The name of the dynamic interpreter. This is put in the .interp
78 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
80 static reloc_howto_type sh_elf_howto_table
[] = {
82 HOWTO (R_SH_NONE
, /* type */
84 0, /* size (0 = byte, 1 = short, 2 = long) */
86 false, /* pc_relative */
88 complain_overflow_dont
, /* complain_on_overflow */
89 sh_elf_ignore_reloc
, /* special_function */
90 "R_SH_NONE", /* name */
91 false, /* partial_inplace */
94 false), /* pcrel_offset */
96 /* 32 bit absolute relocation. Setting partial_inplace to true and
97 src_mask to a non-zero value is similar to the COFF toolchain. */
98 HOWTO (R_SH_DIR32
, /* type */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
102 false, /* pc_relative */
104 complain_overflow_bitfield
, /* complain_on_overflow */
105 sh_elf_reloc
, /* special_function */
106 "R_SH_DIR32", /* name */
107 true, /* partial_inplace */
108 0xffffffff, /* src_mask */
109 0xffffffff, /* dst_mask */
110 false), /* pcrel_offset */
112 /* 32 bit PC relative relocation. */
113 HOWTO (R_SH_REL32
, /* type */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
117 true, /* pc_relative */
119 complain_overflow_signed
, /* complain_on_overflow */
120 sh_elf_ignore_reloc
, /* special_function */
121 "R_SH_REL32", /* name */
122 false, /* partial_inplace */
124 0xffffffff, /* dst_mask */
125 true), /* pcrel_offset */
127 /* 8 bit PC relative branch divided by 2. */
128 HOWTO (R_SH_DIR8WPN
, /* type */
130 1, /* size (0 = byte, 1 = short, 2 = long) */
132 true, /* pc_relative */
134 complain_overflow_signed
, /* complain_on_overflow */
135 sh_elf_ignore_reloc
, /* special_function */
136 "R_SH_DIR8WPN", /* name */
137 true, /* partial_inplace */
140 true), /* pcrel_offset */
142 /* 12 bit PC relative branch divided by 2. */
143 HOWTO (R_SH_IND12W
, /* type */
145 1, /* size (0 = byte, 1 = short, 2 = long) */
147 true, /* pc_relative */
149 complain_overflow_signed
, /* complain_on_overflow */
150 sh_elf_reloc
, /* special_function */
151 "R_SH_IND12W", /* name */
152 true, /* partial_inplace */
153 0xfff, /* src_mask */
154 0xfff, /* dst_mask */
155 true), /* pcrel_offset */
157 /* 8 bit unsigned PC relative divided by 4. */
158 HOWTO (R_SH_DIR8WPL
, /* type */
160 1, /* size (0 = byte, 1 = short, 2 = long) */
162 true, /* pc_relative */
164 complain_overflow_unsigned
, /* complain_on_overflow */
165 sh_elf_ignore_reloc
, /* special_function */
166 "R_SH_DIR8WPL", /* name */
167 true, /* partial_inplace */
170 true), /* pcrel_offset */
172 /* 8 bit unsigned PC relative divided by 2. */
173 HOWTO (R_SH_DIR8WPZ
, /* type */
175 1, /* size (0 = byte, 1 = short, 2 = long) */
177 true, /* pc_relative */
179 complain_overflow_unsigned
, /* complain_on_overflow */
180 sh_elf_ignore_reloc
, /* special_function */
181 "R_SH_DIR8WPZ", /* name */
182 true, /* partial_inplace */
185 true), /* pcrel_offset */
187 /* 8 bit GBR relative. FIXME: This only makes sense if we have some
188 special symbol for the GBR relative area, and that is not
190 HOWTO (R_SH_DIR8BP
, /* type */
192 1, /* size (0 = byte, 1 = short, 2 = long) */
194 false, /* pc_relative */
196 complain_overflow_unsigned
, /* complain_on_overflow */
197 sh_elf_ignore_reloc
, /* special_function */
198 "R_SH_DIR8BP", /* name */
199 false, /* partial_inplace */
202 true), /* pcrel_offset */
204 /* 8 bit GBR relative divided by 2. FIXME: This only makes sense if
205 we have some special symbol for the GBR relative area, and that
206 is not implemented. */
207 HOWTO (R_SH_DIR8W
, /* type */
209 1, /* size (0 = byte, 1 = short, 2 = long) */
211 false, /* pc_relative */
213 complain_overflow_unsigned
, /* complain_on_overflow */
214 sh_elf_ignore_reloc
, /* special_function */
215 "R_SH_DIR8W", /* name */
216 false, /* partial_inplace */
219 true), /* pcrel_offset */
221 /* 8 bit GBR relative divided by 4. FIXME: This only makes sense if
222 we have some special symbol for the GBR relative area, and that
223 is not implemented. */
224 HOWTO (R_SH_DIR8L
, /* type */
226 1, /* size (0 = byte, 1 = short, 2 = long) */
228 false, /* pc_relative */
230 complain_overflow_unsigned
, /* complain_on_overflow */
231 sh_elf_ignore_reloc
, /* special_function */
232 "R_SH_DIR8L", /* name */
233 false, /* partial_inplace */
236 true), /* pcrel_offset */
254 /* The remaining relocs are a GNU extension used for relaxing. The
255 final pass of the linker never needs to do anything with any of
256 these relocs. Any required operations are handled by the
259 /* A 16 bit switch table entry. This is generated for an expression
260 such as ``.word L1 - L2''. The offset holds the difference
261 between the reloc address and L2. */
262 HOWTO (R_SH_SWITCH16
, /* type */
264 1, /* size (0 = byte, 1 = short, 2 = long) */
266 false, /* pc_relative */
268 complain_overflow_unsigned
, /* complain_on_overflow */
269 sh_elf_ignore_reloc
, /* special_function */
270 "R_SH_SWITCH16", /* name */
271 false, /* partial_inplace */
274 true), /* pcrel_offset */
276 /* A 32 bit switch table entry. This is generated for an expression
277 such as ``.long L1 - L2''. The offset holds the difference
278 between the reloc address and L2. */
279 HOWTO (R_SH_SWITCH32
, /* type */
281 2, /* size (0 = byte, 1 = short, 2 = long) */
283 false, /* pc_relative */
285 complain_overflow_unsigned
, /* complain_on_overflow */
286 sh_elf_ignore_reloc
, /* special_function */
287 "R_SH_SWITCH32", /* name */
288 false, /* partial_inplace */
291 true), /* pcrel_offset */
293 /* Indicates a .uses pseudo-op. The compiler will generate .uses
294 pseudo-ops when it finds a function call which can be relaxed.
295 The offset field holds the PC relative offset to the instruction
296 which loads the register used in the function call. */
297 HOWTO (R_SH_USES
, /* type */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
301 false, /* pc_relative */
303 complain_overflow_unsigned
, /* complain_on_overflow */
304 sh_elf_ignore_reloc
, /* special_function */
305 "R_SH_USES", /* name */
306 false, /* partial_inplace */
309 true), /* pcrel_offset */
311 /* The assembler will generate this reloc for addresses referred to
312 by the register loads associated with USES relocs. The offset
313 field holds the number of times the address is referenced in the
315 HOWTO (R_SH_COUNT
, /* type */
317 1, /* size (0 = byte, 1 = short, 2 = long) */
319 false, /* pc_relative */
321 complain_overflow_unsigned
, /* complain_on_overflow */
322 sh_elf_ignore_reloc
, /* special_function */
323 "R_SH_COUNT", /* name */
324 false, /* partial_inplace */
327 true), /* pcrel_offset */
329 /* Indicates an alignment statement. The offset field is the power
330 of 2 to which subsequent portions of the object file must be
332 HOWTO (R_SH_ALIGN
, /* type */
334 1, /* size (0 = byte, 1 = short, 2 = long) */
336 false, /* pc_relative */
338 complain_overflow_unsigned
, /* complain_on_overflow */
339 sh_elf_ignore_reloc
, /* special_function */
340 "R_SH_ALIGN", /* name */
341 false, /* partial_inplace */
344 true), /* pcrel_offset */
346 /* The assembler will generate this reloc before a block of
347 instructions. A section should be processed as assumining it
348 contains data, unless this reloc is seen. */
349 HOWTO (R_SH_CODE
, /* type */
351 1, /* size (0 = byte, 1 = short, 2 = long) */
353 false, /* pc_relative */
355 complain_overflow_unsigned
, /* complain_on_overflow */
356 sh_elf_ignore_reloc
, /* special_function */
357 "R_SH_CODE", /* name */
358 false, /* partial_inplace */
361 true), /* pcrel_offset */
363 /* The assembler will generate this reloc after a block of
364 instructions when it sees data that is not instructions. */
365 HOWTO (R_SH_DATA
, /* type */
367 1, /* size (0 = byte, 1 = short, 2 = long) */
369 false, /* pc_relative */
371 complain_overflow_unsigned
, /* complain_on_overflow */
372 sh_elf_ignore_reloc
, /* special_function */
373 "R_SH_DATA", /* name */
374 false, /* partial_inplace */
377 true), /* pcrel_offset */
379 /* The assembler generates this reloc for each label within a block
380 of instructions. This permits the linker to avoid swapping
381 instructions which are the targets of branches. */
382 HOWTO (R_SH_LABEL
, /* type */
384 1, /* size (0 = byte, 1 = short, 2 = long) */
386 false, /* pc_relative */
388 complain_overflow_unsigned
, /* complain_on_overflow */
389 sh_elf_ignore_reloc
, /* special_function */
390 "R_SH_LABEL", /* name */
391 false, /* partial_inplace */
394 true), /* pcrel_offset */
396 /* An 8 bit switch table entry. This is generated for an expression
397 such as ``.word L1 - L2''. The offset holds the difference
398 between the reloc address and L2. */
399 HOWTO (R_SH_SWITCH8
, /* type */
401 0, /* size (0 = byte, 1 = short, 2 = long) */
403 false, /* pc_relative */
405 complain_overflow_unsigned
, /* complain_on_overflow */
406 sh_elf_ignore_reloc
, /* special_function */
407 "R_SH_SWITCH8", /* name */
408 false, /* partial_inplace */
411 true), /* pcrel_offset */
413 /* GNU extension to record C++ vtable hierarchy */
414 HOWTO (R_SH_GNU_VTINHERIT
, /* type */
416 2, /* size (0 = byte, 1 = short, 2 = long) */
418 false, /* pc_relative */
420 complain_overflow_dont
, /* complain_on_overflow */
421 NULL
, /* special_function */
422 "R_SH_GNU_VTINHERIT", /* name */
423 false, /* partial_inplace */
426 false), /* pcrel_offset */
428 /* GNU extension to record C++ vtable member usage */
429 HOWTO (R_SH_GNU_VTENTRY
, /* type */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
433 false, /* pc_relative */
435 complain_overflow_dont
, /* complain_on_overflow */
436 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
437 "R_SH_GNU_VTENTRY", /* name */
438 false, /* partial_inplace */
441 false), /* pcrel_offset */
443 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
444 HOWTO (R_SH_LOOP_START
, /* type */
446 1, /* size (0 = byte, 1 = short, 2 = long) */
448 false, /* pc_relative */
450 complain_overflow_signed
, /* complain_on_overflow */
451 sh_elf_ignore_reloc
, /* special_function */
452 "R_SH_LOOP_START", /* name */
453 true, /* partial_inplace */
456 true), /* pcrel_offset */
458 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
459 HOWTO (R_SH_LOOP_END
, /* type */
461 1, /* size (0 = byte, 1 = short, 2 = long) */
463 false, /* pc_relative */
465 complain_overflow_signed
, /* complain_on_overflow */
466 sh_elf_ignore_reloc
, /* special_function */
467 "R_SH_LOOP_END", /* name */
468 true, /* partial_inplace */
471 true), /* pcrel_offset */
596 HOWTO (R_SH_GOT32
, /* type */
598 2, /* size (0 = byte, 1 = short, 2 = long) */
600 false, /* pc_relative */
602 complain_overflow_bitfield
, /* complain_on_overflow */
603 bfd_elf_generic_reloc
, /* */
604 "R_SH_GOT32", /* name */
605 true, /* partial_inplace */
606 0xffffffff, /* src_mask */
607 0xffffffff, /* dst_mask */
608 false), /* pcrel_offset */
610 HOWTO (R_SH_PLT32
, /* type */
612 2, /* size (0 = byte, 1 = short, 2 = long) */
614 true, /* pc_relative */
616 complain_overflow_bitfield
, /* complain_on_overflow */
617 bfd_elf_generic_reloc
, /* */
618 "R_SH_PLT32", /* name */
619 true, /* partial_inplace */
620 0xffffffff, /* src_mask */
621 0xffffffff, /* dst_mask */
622 true), /* pcrel_offset */
624 HOWTO (R_SH_COPY
, /* type */
626 2, /* size (0 = byte, 1 = short, 2 = long) */
628 false, /* pc_relative */
630 complain_overflow_bitfield
, /* complain_on_overflow */
631 bfd_elf_generic_reloc
, /* */
632 "R_SH_COPY", /* name */
633 true, /* partial_inplace */
634 0xffffffff, /* src_mask */
635 0xffffffff, /* dst_mask */
636 false), /* pcrel_offset */
638 HOWTO (R_SH_GLOB_DAT
, /* type */
640 2, /* size (0 = byte, 1 = short, 2 = long) */
642 false, /* pc_relative */
644 complain_overflow_bitfield
, /* complain_on_overflow */
645 bfd_elf_generic_reloc
, /* */
646 "R_SH_GLOB_DAT", /* name */
647 true, /* partial_inplace */
648 0xffffffff, /* src_mask */
649 0xffffffff, /* dst_mask */
650 false), /* pcrel_offset */
652 HOWTO (R_SH_JMP_SLOT
, /* type */
654 2, /* size (0 = byte, 1 = short, 2 = long) */
656 false, /* pc_relative */
658 complain_overflow_bitfield
, /* complain_on_overflow */
659 bfd_elf_generic_reloc
, /* */
660 "R_SH_JMP_SLOT", /* name */
661 true, /* partial_inplace */
662 0xffffffff, /* src_mask */
663 0xffffffff, /* dst_mask */
664 false), /* pcrel_offset */
666 HOWTO (R_SH_RELATIVE
, /* type */
668 2, /* size (0 = byte, 1 = short, 2 = long) */
670 false, /* pc_relative */
672 complain_overflow_bitfield
, /* complain_on_overflow */
673 bfd_elf_generic_reloc
, /* */
674 "R_SH_RELATIVE", /* name */
675 true, /* partial_inplace */
676 0xffffffff, /* src_mask */
677 0xffffffff, /* dst_mask */
678 false), /* pcrel_offset */
680 HOWTO (R_SH_GOTOFF
, /* type */
682 2, /* size (0 = byte, 1 = short, 2 = long) */
684 false, /* pc_relative */
686 complain_overflow_bitfield
, /* complain_on_overflow */
687 bfd_elf_generic_reloc
, /* */
688 "R_SH_GOTOFF", /* name */
689 true, /* partial_inplace */
690 0xffffffff, /* src_mask */
691 0xffffffff, /* dst_mask */
692 false), /* pcrel_offset */
694 HOWTO (R_SH_GOTPC
, /* type */
696 2, /* size (0 = byte, 1 = short, 2 = long) */
698 true, /* pc_relative */
700 complain_overflow_bitfield
, /* complain_on_overflow */
701 bfd_elf_generic_reloc
, /* */
702 "R_SH_GOTPC", /* name */
703 true, /* partial_inplace */
704 0xffffffff, /* src_mask */
705 0xffffffff, /* dst_mask */
706 true), /* pcrel_offset */
710 static bfd_reloc_status_type
711 sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
, addr
,
712 symbol_section
, start
, end
)
713 int r_type ATTRIBUTE_UNUSED
;
715 asection
*input_section
;
718 asection
*symbol_section
;
721 static bfd_vma last_addr
;
722 static asection
*last_symbol_section
;
723 bfd_byte
*free_contents
= NULL
;
724 bfd_byte
*start_ptr
, *ptr
, *last_ptr
;
729 /* Sanity check the address. */
730 if (addr
> input_section
->_raw_size
)
731 return bfd_reloc_outofrange
;
733 /* We require the start and end relocations to be processed consecutively -
734 although we allow then to be processed forwards or backwards. */
738 last_symbol_section
= symbol_section
;
741 if (last_addr
!= addr
)
745 if (! symbol_section
|| last_symbol_section
!= symbol_section
|| end
< start
)
746 return bfd_reloc_outofrange
;
748 /* Get the symbol_section contents. */
749 if (symbol_section
!= input_section
)
751 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= NULL
)
752 contents
= elf_section_data (symbol_section
)->this_hdr
.contents
;
755 free_contents
= contents
756 = (bfd_byte
*) bfd_malloc (symbol_section
->_raw_size
);
757 if (contents
== NULL
)
758 return bfd_reloc_outofrange
;
759 if (! bfd_get_section_contents (input_bfd
, symbol_section
, contents
,
761 symbol_section
->_raw_size
))
764 return bfd_reloc_outofrange
;
768 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
769 start_ptr
= contents
+ start
;
770 for (cum_diff
= -6, ptr
= contents
+ end
; cum_diff
< 0 && ptr
> start_ptr
;)
772 for (last_ptr
= ptr
, ptr
-= 4; ptr
>= start_ptr
&& IS_PPI (ptr
);)
775 diff
= (last_ptr
- ptr
) >> 1;
776 cum_diff
+= diff
& 1;
779 /* Calculate the start / end values to load into rs / re minus four -
780 so that will cancel out the four we would otherwise have to add to
781 addr to get the value to subtract in order to get relative addressing. */
785 end
= (ptr
+ cum_diff
* 2) - contents
;
789 bfd_vma start0
= start
- 4;
791 while (start0
&& IS_PPI (contents
+ start0
))
793 start0
= start
- 2 - ((start
- start0
) & 2);
794 start
= start0
- cum_diff
- 2;
799 free (free_contents
);
801 insn
= bfd_get_16 (input_bfd
, contents
+ addr
);
803 x
= (insn
& 0x200 ? end
: start
) - addr
;
804 if (input_section
!= symbol_section
)
805 x
+= ((symbol_section
->output_section
->vma
+ symbol_section
->output_offset
)
806 - (input_section
->output_section
->vma
807 + input_section
->output_offset
));
809 if (x
< -128 || x
> 127)
810 return bfd_reloc_overflow
;
812 x
= (insn
& ~0xff) | (x
& 0xff);
813 bfd_put_16 (input_bfd
, x
, contents
+ addr
);
818 /* This function is used for normal relocs. This used to be like the COFF
819 function, and is almost certainly incorrect for other ELF targets. */
821 static bfd_reloc_status_type
822 sh_elf_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
825 arelent
*reloc_entry
;
828 asection
*input_section
;
830 char **error_message ATTRIBUTE_UNUSED
;
834 enum elf_sh_reloc_type r_type
;
835 bfd_vma addr
= reloc_entry
->address
;
836 bfd_byte
*hit_data
= addr
+ (bfd_byte
*) data
;
838 r_type
= (enum elf_sh_reloc_type
) reloc_entry
->howto
->type
;
840 if (output_bfd
!= NULL
)
842 /* Partial linking--do nothing. */
843 reloc_entry
->address
+= input_section
->output_offset
;
847 /* Almost all relocs have to do with relaxing. If any work must be
848 done for them, it has been done in sh_relax_section. */
849 if (r_type
== R_SH_IND12W
&& (symbol_in
->flags
& BSF_LOCAL
) != 0)
852 if (symbol_in
!= NULL
853 && bfd_is_und_section (symbol_in
->section
))
854 return bfd_reloc_undefined
;
856 if (bfd_is_com_section (symbol_in
->section
))
859 sym_value
= (symbol_in
->value
+
860 symbol_in
->section
->output_section
->vma
+
861 symbol_in
->section
->output_offset
);
866 insn
= bfd_get_32 (abfd
, hit_data
);
867 insn
+= sym_value
+ reloc_entry
->addend
;
868 bfd_put_32 (abfd
, insn
, hit_data
);
871 insn
= bfd_get_16 (abfd
, hit_data
);
872 sym_value
+= reloc_entry
->addend
;
873 sym_value
-= (input_section
->output_section
->vma
874 + input_section
->output_offset
877 sym_value
+= (insn
& 0xfff) << 1;
880 insn
= (insn
& 0xf000) | (sym_value
& 0xfff);
881 bfd_put_16 (abfd
, insn
, hit_data
);
882 if (sym_value
< (bfd_vma
) -0x1000 || sym_value
>= 0x1000)
883 return bfd_reloc_overflow
;
893 /* This function is used for relocs which are only used for relaxing,
894 which the linker should otherwise ignore. */
896 static bfd_reloc_status_type
897 sh_elf_ignore_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
,
898 output_bfd
, error_message
)
899 bfd
*abfd ATTRIBUTE_UNUSED
;
900 arelent
*reloc_entry
;
901 asymbol
*symbol ATTRIBUTE_UNUSED
;
902 PTR data ATTRIBUTE_UNUSED
;
903 asection
*input_section
;
905 char **error_message ATTRIBUTE_UNUSED
;
907 if (output_bfd
!= NULL
)
908 reloc_entry
->address
+= input_section
->output_offset
;
912 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
914 struct elf_reloc_map
{
915 bfd_reloc_code_real_type bfd_reloc_val
;
916 unsigned char elf_reloc_val
;
919 /* An array mapping BFD reloc codes to SH ELF relocs. */
921 static const struct elf_reloc_map sh_reloc_map
[] = {
922 { BFD_RELOC_NONE
, R_SH_NONE
},
923 { BFD_RELOC_32
, R_SH_DIR32
},
924 { BFD_RELOC_CTOR
, R_SH_DIR32
},
925 { BFD_RELOC_32_PCREL
, R_SH_REL32
},
926 { BFD_RELOC_SH_PCDISP8BY2
, R_SH_DIR8WPN
},
927 { BFD_RELOC_SH_PCDISP12BY2
, R_SH_IND12W
},
928 { BFD_RELOC_SH_PCRELIMM8BY2
, R_SH_DIR8WPZ
},
929 { BFD_RELOC_SH_PCRELIMM8BY4
, R_SH_DIR8WPL
},
930 { BFD_RELOC_8_PCREL
, R_SH_SWITCH8
},
931 { BFD_RELOC_SH_SWITCH16
, R_SH_SWITCH16
},
932 { BFD_RELOC_SH_SWITCH32
, R_SH_SWITCH32
},
933 { BFD_RELOC_SH_USES
, R_SH_USES
},
934 { BFD_RELOC_SH_COUNT
, R_SH_COUNT
},
935 { BFD_RELOC_SH_ALIGN
, R_SH_ALIGN
},
936 { BFD_RELOC_SH_CODE
, R_SH_CODE
},
937 { BFD_RELOC_SH_DATA
, R_SH_DATA
},
938 { BFD_RELOC_SH_LABEL
, R_SH_LABEL
},
939 { BFD_RELOC_VTABLE_INHERIT
, R_SH_GNU_VTINHERIT
},
940 { BFD_RELOC_VTABLE_ENTRY
, R_SH_GNU_VTENTRY
},
941 { BFD_RELOC_SH_LOOP_START
, R_SH_LOOP_START
},
942 { BFD_RELOC_SH_LOOP_END
, R_SH_LOOP_END
},
943 { BFD_RELOC_32_GOT_PCREL
, R_SH_GOT32
},
944 { BFD_RELOC_32_PLT_PCREL
, R_SH_PLT32
},
945 { BFD_RELOC_SH_COPY
, R_SH_COPY
},
946 { BFD_RELOC_SH_GLOB_DAT
, R_SH_GLOB_DAT
},
947 { BFD_RELOC_SH_JMP_SLOT
, R_SH_JMP_SLOT
},
948 { BFD_RELOC_SH_RELATIVE
, R_SH_RELATIVE
},
949 { BFD_RELOC_32_GOTOFF
, R_SH_GOTOFF
},
950 { BFD_RELOC_SH_GOTPC
, R_SH_GOTPC
},
953 /* Given a BFD reloc code, return the howto structure for the
954 corresponding SH ELf reloc. */
956 static reloc_howto_type
*
957 sh_elf_reloc_type_lookup (abfd
, code
)
958 bfd
*abfd ATTRIBUTE_UNUSED
;
959 bfd_reloc_code_real_type code
;
963 for (i
= 0; i
< sizeof (sh_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
965 if (sh_reloc_map
[i
].bfd_reloc_val
== code
)
966 return &sh_elf_howto_table
[(int) sh_reloc_map
[i
].elf_reloc_val
];
972 /* Given an ELF reloc, fill in the howto field of a relent. */
975 sh_elf_info_to_howto (abfd
, cache_ptr
, dst
)
976 bfd
*abfd ATTRIBUTE_UNUSED
;
978 Elf_Internal_Rela
*dst
;
982 r
= ELF32_R_TYPE (dst
->r_info
);
984 BFD_ASSERT (r
< (unsigned int) R_SH_max
);
985 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC
|| r
> R_SH_LAST_INVALID_RELOC
);
986 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_2
|| r
> R_SH_LAST_INVALID_RELOC_2
);
988 cache_ptr
->howto
= &sh_elf_howto_table
[r
];
991 /* This function handles relaxing for SH ELF. See the corresponding
992 function in coff-sh.c for a description of what this does. FIXME:
993 There is a lot of duplication here between this code and the COFF
994 specific code. The format of relocs and symbols is wound deeply
995 into this code, but it would still be better if the duplication
996 could be eliminated somehow. Note in particular that although both
997 functions use symbols like R_SH_CODE, those symbols have different
998 values; in coff-sh.c they come from include/coff/sh.h, whereas here
999 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
1002 sh_elf_relax_section (abfd
, sec
, link_info
, again
)
1005 struct bfd_link_info
*link_info
;
1008 Elf_Internal_Shdr
*symtab_hdr
;
1009 Elf_Internal_Rela
*internal_relocs
;
1010 Elf_Internal_Rela
*free_relocs
= NULL
;
1012 Elf_Internal_Rela
*irel
, *irelend
;
1013 bfd_byte
*contents
= NULL
;
1014 bfd_byte
*free_contents
= NULL
;
1015 Elf32_External_Sym
*extsyms
= NULL
;
1016 Elf32_External_Sym
*free_extsyms
= NULL
;
1020 if (link_info
->relocateable
1021 || (sec
->flags
& SEC_RELOC
) == 0
1022 || sec
->reloc_count
== 0)
1025 /* If this is the first time we have been called for this section,
1026 initialize the cooked size. */
1027 if (sec
->_cooked_size
== 0)
1028 sec
->_cooked_size
= sec
->_raw_size
;
1030 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1032 internal_relocs
= (_bfd_elf32_link_read_relocs
1033 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1034 link_info
->keep_memory
));
1035 if (internal_relocs
== NULL
)
1037 if (! link_info
->keep_memory
)
1038 free_relocs
= internal_relocs
;
1042 irelend
= internal_relocs
+ sec
->reloc_count
;
1043 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1045 bfd_vma laddr
, paddr
, symval
;
1046 unsigned short insn
;
1047 Elf_Internal_Rela
*irelfn
, *irelscan
, *irelcount
;
1048 bfd_signed_vma foff
;
1050 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_CODE
)
1053 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_USES
)
1056 /* Get the section contents. */
1057 if (contents
== NULL
)
1059 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1060 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1063 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
1064 if (contents
== NULL
)
1066 free_contents
= contents
;
1068 if (! bfd_get_section_contents (abfd
, sec
, contents
,
1069 (file_ptr
) 0, sec
->_raw_size
))
1074 /* The r_addend field of the R_SH_USES reloc will point us to
1075 the register load. The 4 is because the r_addend field is
1076 computed as though it were a jump offset, which are based
1077 from 4 bytes after the jump instruction. */
1078 laddr
= irel
->r_offset
+ 4 + irel
->r_addend
;
1079 if (laddr
>= sec
->_raw_size
)
1081 (*_bfd_error_handler
) (_("%s: 0x%lx: warning: bad R_SH_USES offset"),
1082 bfd_get_filename (abfd
),
1083 (unsigned long) irel
->r_offset
);
1086 insn
= bfd_get_16 (abfd
, contents
+ laddr
);
1088 /* If the instruction is not mov.l NN,rN, we don't know what to
1090 if ((insn
& 0xf000) != 0xd000)
1092 ((*_bfd_error_handler
)
1093 (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
1094 bfd_get_filename (abfd
), (unsigned long) irel
->r_offset
, insn
));
1098 /* Get the address from which the register is being loaded. The
1099 displacement in the mov.l instruction is quadrupled. It is a
1100 displacement from four bytes after the movl instruction, but,
1101 before adding in the PC address, two least significant bits
1102 of the PC are cleared. We assume that the section is aligned
1103 on a four byte boundary. */
1104 paddr
= insn
& 0xff;
1106 paddr
+= (laddr
+ 4) & ~3;
1107 if (paddr
>= sec
->_raw_size
)
1109 ((*_bfd_error_handler
)
1110 (_("%s: 0x%lx: warning: bad R_SH_USES load offset"),
1111 bfd_get_filename (abfd
), (unsigned long) irel
->r_offset
));
1115 /* Get the reloc for the address from which the register is
1116 being loaded. This reloc will tell us which function is
1117 actually being called. */
1118 for (irelfn
= internal_relocs
; irelfn
< irelend
; irelfn
++)
1119 if (irelfn
->r_offset
== paddr
1120 && ELF32_R_TYPE (irelfn
->r_info
) == (int) R_SH_DIR32
)
1122 if (irelfn
>= irelend
)
1124 ((*_bfd_error_handler
)
1125 (_("%s: 0x%lx: warning: could not find expected reloc"),
1126 bfd_get_filename (abfd
), (unsigned long) paddr
));
1130 /* Read this BFD's symbols if we haven't done so already. */
1131 if (extsyms
== NULL
)
1133 if (symtab_hdr
->contents
!= NULL
)
1134 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1137 extsyms
= ((Elf32_External_Sym
*)
1138 bfd_malloc (symtab_hdr
->sh_size
));
1139 if (extsyms
== NULL
)
1141 free_extsyms
= extsyms
;
1142 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1143 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, abfd
)
1144 != symtab_hdr
->sh_size
))
1149 /* Get the value of the symbol referred to by the reloc. */
1150 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
1152 Elf_Internal_Sym isym
;
1154 /* A local symbol. */
1155 bfd_elf32_swap_symbol_in (abfd
,
1156 extsyms
+ ELF32_R_SYM (irelfn
->r_info
),
1159 if (isym
.st_shndx
!= _bfd_elf_section_from_bfd_section (abfd
, sec
))
1161 ((*_bfd_error_handler
)
1162 (_("%s: 0x%lx: warning: symbol in unexpected section"),
1163 bfd_get_filename (abfd
), (unsigned long) paddr
));
1167 symval
= (isym
.st_value
1168 + sec
->output_section
->vma
1169 + sec
->output_offset
);
1174 struct elf_link_hash_entry
*h
;
1176 indx
= ELF32_R_SYM (irelfn
->r_info
) - symtab_hdr
->sh_info
;
1177 h
= elf_sym_hashes (abfd
)[indx
];
1178 BFD_ASSERT (h
!= NULL
);
1179 if (h
->root
.type
!= bfd_link_hash_defined
1180 && h
->root
.type
!= bfd_link_hash_defweak
)
1182 /* This appears to be a reference to an undefined
1183 symbol. Just ignore it--it will be caught by the
1184 regular reloc processing. */
1188 symval
= (h
->root
.u
.def
.value
1189 + h
->root
.u
.def
.section
->output_section
->vma
1190 + h
->root
.u
.def
.section
->output_offset
);
1193 symval
+= bfd_get_32 (abfd
, contents
+ paddr
);
1195 /* See if this function call can be shortened. */
1198 + sec
->output_section
->vma
1199 + sec
->output_offset
1201 if (foff
< -0x1000 || foff
>= 0x1000)
1203 /* After all that work, we can't shorten this function call. */
1207 /* Shorten the function call. */
1209 /* For simplicity of coding, we are going to modify the section
1210 contents, the section relocs, and the BFD symbol table. We
1211 must tell the rest of the code not to free up this
1212 information. It would be possible to instead create a table
1213 of changes which have to be made, as is done in coff-mips.c;
1214 that would be more work, but would require less memory when
1215 the linker is run. */
1217 elf_section_data (sec
)->relocs
= internal_relocs
;
1220 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1221 free_contents
= NULL
;
1223 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1224 free_extsyms
= NULL
;
1226 /* Replace the jsr with a bsr. */
1228 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
1229 replace the jsr with a bsr. */
1230 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irelfn
->r_info
), R_SH_IND12W
);
1231 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
1233 /* If this needs to be changed because of future relaxing,
1234 it will be handled here like other internal IND12W
1237 0xb000 | ((foff
>> 1) & 0xfff),
1238 contents
+ irel
->r_offset
);
1242 /* We can't fully resolve this yet, because the external
1243 symbol value may be changed by future relaxing. We let
1244 the final link phase handle it. */
1245 bfd_put_16 (abfd
, 0xb000, contents
+ irel
->r_offset
);
1248 /* See if there is another R_SH_USES reloc referring to the same
1250 for (irelscan
= internal_relocs
; irelscan
< irelend
; irelscan
++)
1251 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_USES
1252 && laddr
== irelscan
->r_offset
+ 4 + irelscan
->r_addend
)
1254 if (irelscan
< irelend
)
1256 /* Some other function call depends upon this register load,
1257 and we have not yet converted that function call.
1258 Indeed, we may never be able to convert it. There is
1259 nothing else we can do at this point. */
1263 /* Look for a R_SH_COUNT reloc on the location where the
1264 function address is stored. Do this before deleting any
1265 bytes, to avoid confusion about the address. */
1266 for (irelcount
= internal_relocs
; irelcount
< irelend
; irelcount
++)
1267 if (irelcount
->r_offset
== paddr
1268 && ELF32_R_TYPE (irelcount
->r_info
) == (int) R_SH_COUNT
)
1271 /* Delete the register load. */
1272 if (! sh_elf_relax_delete_bytes (abfd
, sec
, laddr
, 2))
1275 /* That will change things, so, just in case it permits some
1276 other function call to come within range, we should relax
1277 again. Note that this is not required, and it may be slow. */
1280 /* Now check whether we got a COUNT reloc. */
1281 if (irelcount
>= irelend
)
1283 ((*_bfd_error_handler
)
1284 (_("%s: 0x%lx: warning: could not find expected COUNT reloc"),
1285 bfd_get_filename (abfd
), (unsigned long) paddr
));
1289 /* The number of uses is stored in the r_addend field. We've
1290 just deleted one. */
1291 if (irelcount
->r_addend
== 0)
1293 ((*_bfd_error_handler
) (_("%s: 0x%lx: warning: bad count"),
1294 bfd_get_filename (abfd
),
1295 (unsigned long) paddr
));
1299 --irelcount
->r_addend
;
1301 /* If there are no more uses, we can delete the address. Reload
1302 the address from irelfn, in case it was changed by the
1303 previous call to sh_elf_relax_delete_bytes. */
1304 if (irelcount
->r_addend
== 0)
1306 if (! sh_elf_relax_delete_bytes (abfd
, sec
, irelfn
->r_offset
, 4))
1310 /* We've done all we can with that function call. */
1313 /* Look for load and store instructions that we can align on four
1319 /* Get the section contents. */
1320 if (contents
== NULL
)
1322 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1323 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1326 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
1327 if (contents
== NULL
)
1329 free_contents
= contents
;
1331 if (! bfd_get_section_contents (abfd
, sec
, contents
,
1332 (file_ptr
) 0, sec
->_raw_size
))
1337 if (! sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
,
1343 elf_section_data (sec
)->relocs
= internal_relocs
;
1346 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1347 free_contents
= NULL
;
1349 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1350 free_extsyms
= NULL
;
1354 if (free_relocs
!= NULL
)
1360 if (free_contents
!= NULL
)
1362 if (! link_info
->keep_memory
)
1363 free (free_contents
);
1366 /* Cache the section contents for elf_link_input_bfd. */
1367 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1369 free_contents
= NULL
;
1372 if (free_extsyms
!= NULL
)
1374 if (! link_info
->keep_memory
)
1375 free (free_extsyms
);
1378 /* Cache the symbols for elf_link_input_bfd. */
1379 symtab_hdr
->contents
= extsyms
;
1381 free_extsyms
= NULL
;
1387 if (free_relocs
!= NULL
)
1389 if (free_contents
!= NULL
)
1390 free (free_contents
);
1391 if (free_extsyms
!= NULL
)
1392 free (free_extsyms
);
1396 /* Delete some bytes from a section while relaxing. FIXME: There is a
1397 lot of duplication between this function and sh_relax_delete_bytes
1401 sh_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1407 Elf_Internal_Shdr
*symtab_hdr
;
1408 Elf32_External_Sym
*extsyms
;
1411 Elf_Internal_Rela
*irel
, *irelend
;
1412 Elf_Internal_Rela
*irelalign
;
1414 Elf32_External_Sym
*esym
, *esymend
;
1415 struct elf_link_hash_entry
*sym_hash
;
1418 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1419 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1421 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1423 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1425 /* The deletion must stop at the next ALIGN reloc for an aligment
1426 power larger than the number of bytes we are deleting. */
1429 toaddr
= sec
->_cooked_size
;
1431 irel
= elf_section_data (sec
)->relocs
;
1432 irelend
= irel
+ sec
->reloc_count
;
1433 for (; irel
< irelend
; irel
++)
1435 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
1436 && irel
->r_offset
> addr
1437 && count
< (1 << irel
->r_addend
))
1440 toaddr
= irel
->r_offset
;
1445 /* Actually delete the bytes. */
1446 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
1447 if (irelalign
== NULL
)
1448 sec
->_cooked_size
-= count
;
1453 #define NOP_OPCODE (0x0009)
1455 BFD_ASSERT ((count
& 1) == 0);
1456 for (i
= 0; i
< count
; i
+= 2)
1457 bfd_put_16 (abfd
, NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
1460 /* Adjust all the relocs. */
1461 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1463 bfd_vma nraddr
, stop
;
1466 Elf_Internal_Sym sym
;
1467 int off
, adjust
, oinsn
;
1468 bfd_signed_vma voff
= 0;
1471 /* Get the new reloc address. */
1472 nraddr
= irel
->r_offset
;
1473 if ((irel
->r_offset
> addr
1474 && irel
->r_offset
< toaddr
)
1475 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
1476 && irel
->r_offset
== toaddr
))
1479 /* See if this reloc was for the bytes we have deleted, in which
1480 case we no longer care about it. Don't delete relocs which
1481 represent addresses, though. */
1482 if (irel
->r_offset
>= addr
1483 && irel
->r_offset
< addr
+ count
1484 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_ALIGN
1485 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
1486 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_DATA
1487 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_LABEL
)
1488 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1491 /* If this is a PC relative reloc, see if the range it covers
1492 includes the bytes we have deleted. */
1493 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1502 start
= irel
->r_offset
;
1503 insn
= bfd_get_16 (abfd
, contents
+ nraddr
);
1507 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1510 start
= stop
= addr
;
1514 /* If this reloc is against a symbol defined in this
1515 section, and the symbol will not be adjusted below, we
1516 must check the addend to see it will put the value in
1517 range to be adjusted, and hence must be changed. */
1518 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1520 bfd_elf32_swap_symbol_in (abfd
,
1521 extsyms
+ ELF32_R_SYM (irel
->r_info
),
1523 if (sym
.st_shndx
== shndx
1524 && (sym
.st_value
<= addr
1525 || sym
.st_value
>= toaddr
))
1529 val
= bfd_get_32 (abfd
, contents
+ nraddr
);
1530 val
+= sym
.st_value
;
1531 if (val
> addr
&& val
< toaddr
)
1532 bfd_put_32 (abfd
, val
- count
, contents
+ nraddr
);
1535 start
= stop
= addr
;
1542 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
1546 if (ELF32_R_SYM (irel
->r_info
) >= symtab_hdr
->sh_info
)
1547 start
= stop
= addr
;
1553 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
1559 stop
= start
+ 4 + off
* 2;
1564 stop
= (start
& ~(bfd_vma
) 3) + 4 + off
* 4;
1570 /* These relocs types represent
1572 The r_addend field holds the difference between the reloc
1573 address and L1. That is the start of the reloc, and
1574 adding in the contents gives us the top. We must adjust
1575 both the r_offset field and the section contents.
1576 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1577 and the elf bfd r_offset is called r_vaddr. */
1579 stop
= irel
->r_offset
;
1580 start
= (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irel
->r_addend
);
1584 && (stop
<= addr
|| stop
>= toaddr
))
1585 irel
->r_addend
+= count
;
1586 else if (stop
> addr
1588 && (start
<= addr
|| start
>= toaddr
))
1589 irel
->r_addend
-= count
;
1591 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH16
)
1592 voff
= bfd_get_signed_16 (abfd
, contents
+ nraddr
);
1593 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH8
)
1594 voff
= bfd_get_8 (abfd
, contents
+ nraddr
);
1596 voff
= bfd_get_signed_32 (abfd
, contents
+ nraddr
);
1597 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1602 start
= irel
->r_offset
;
1603 stop
= (bfd_vma
) ((bfd_signed_vma
) start
1604 + (long) irel
->r_addend
1611 && (stop
<= addr
|| stop
>= toaddr
))
1613 else if (stop
> addr
1615 && (start
<= addr
|| start
>= toaddr
))
1624 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1633 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1635 bfd_put_16 (abfd
, insn
, contents
+ nraddr
);
1640 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1642 bfd_put_16 (abfd
, insn
, contents
+ nraddr
);
1646 BFD_ASSERT (adjust
== count
|| count
>= 4);
1651 if ((irel
->r_offset
& 3) == 0)
1654 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1656 bfd_put_16 (abfd
, insn
, contents
+ nraddr
);
1661 if (voff
< 0 || voff
>= 0xff)
1663 bfd_put_8 (abfd
, voff
, contents
+ nraddr
);
1668 if (voff
< - 0x8000 || voff
>= 0x8000)
1670 bfd_put_signed_16 (abfd
, voff
, contents
+ nraddr
);
1675 bfd_put_signed_32 (abfd
, voff
, contents
+ nraddr
);
1679 irel
->r_addend
+= adjust
;
1685 ((*_bfd_error_handler
)
1686 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1687 bfd_get_filename (abfd
), (unsigned long) irel
->r_offset
));
1688 bfd_set_error (bfd_error_bad_value
);
1693 irel
->r_offset
= nraddr
;
1696 /* Look through all the other sections. If there contain any IMM32
1697 relocs against internal symbols which we are not going to adjust
1698 below, we may need to adjust the addends. */
1699 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1701 Elf_Internal_Rela
*internal_relocs
;
1702 Elf_Internal_Rela
*irelscan
, *irelscanend
;
1703 bfd_byte
*ocontents
;
1706 || (o
->flags
& SEC_RELOC
) == 0
1707 || o
->reloc_count
== 0)
1710 /* We always cache the relocs. Perhaps, if info->keep_memory is
1711 false, we should free them, if we are permitted to, when we
1712 leave sh_coff_relax_section. */
1713 internal_relocs
= (_bfd_elf32_link_read_relocs
1714 (abfd
, o
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1716 if (internal_relocs
== NULL
)
1720 irelscanend
= internal_relocs
+ o
->reloc_count
;
1721 for (irelscan
= internal_relocs
; irelscan
< irelscanend
; irelscan
++)
1723 Elf_Internal_Sym sym
;
1725 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1726 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_SWITCH32
)
1728 bfd_vma start
, stop
;
1729 bfd_signed_vma voff
;
1731 if (ocontents
== NULL
)
1733 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1734 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1737 /* We always cache the section contents.
1738 Perhaps, if info->keep_memory is false, we
1739 should free them, if we are permitted to,
1740 when we leave sh_coff_relax_section. */
1741 ocontents
= (bfd_byte
*) bfd_malloc (o
->_raw_size
);
1742 if (ocontents
== NULL
)
1744 if (! bfd_get_section_contents (abfd
, o
, ocontents
,
1748 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1752 stop
= irelscan
->r_offset
;
1754 = (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irelscan
->r_addend
);
1756 /* STOP is in a different section, so it won't change. */
1757 if (start
> addr
&& start
< toaddr
)
1758 irelscan
->r_addend
+= count
;
1760 voff
= bfd_get_signed_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1761 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1765 && (stop
<= addr
|| stop
>= toaddr
))
1766 bfd_put_signed_32 (abfd
, voff
+ count
,
1767 ocontents
+ irelscan
->r_offset
);
1768 else if (stop
> addr
1770 && (start
<= addr
|| start
>= toaddr
))
1771 bfd_put_signed_32 (abfd
, voff
- count
,
1772 ocontents
+ irelscan
->r_offset
);
1775 if (ELF32_R_TYPE (irelscan
->r_info
) != (int) R_SH_DIR32
)
1778 if (ELF32_R_SYM (irelscan
->r_info
) >= symtab_hdr
->sh_info
)
1781 bfd_elf32_swap_symbol_in (abfd
,
1782 extsyms
+ ELF32_R_SYM (irelscan
->r_info
),
1785 if (sym
.st_shndx
== shndx
1786 && (sym
.st_value
<= addr
1787 || sym
.st_value
>= toaddr
))
1791 if (ocontents
== NULL
)
1793 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1794 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1797 /* We always cache the section contents.
1798 Perhaps, if info->keep_memory is false, we
1799 should free them, if we are permitted to,
1800 when we leave sh_coff_relax_section. */
1801 ocontents
= (bfd_byte
*) bfd_malloc (o
->_raw_size
);
1802 if (ocontents
== NULL
)
1804 if (! bfd_get_section_contents (abfd
, o
, ocontents
,
1808 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1812 val
= bfd_get_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1813 val
+= sym
.st_value
;
1814 if (val
> addr
&& val
< toaddr
)
1815 bfd_put_32 (abfd
, val
- count
,
1816 ocontents
+ irelscan
->r_offset
);
1821 /* Adjust the local symbols defined in this section. */
1823 esymend
= esym
+ symtab_hdr
->sh_info
;
1824 for (; esym
< esymend
; esym
++)
1826 Elf_Internal_Sym isym
;
1828 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1830 if (isym
.st_shndx
== shndx
1831 && isym
.st_value
> addr
1832 && isym
.st_value
< toaddr
)
1834 isym
.st_value
-= count
;
1835 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
1839 /* Now adjust the global symbols defined in this section. */
1840 esym
= extsyms
+ symtab_hdr
->sh_info
;
1841 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
1842 for (index
= 0; esym
< esymend
; esym
++, index
++)
1844 Elf_Internal_Sym isym
;
1846 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1847 sym_hash
= elf_sym_hashes (abfd
)[index
];
1848 if (isym
.st_shndx
== shndx
1849 && ((sym_hash
)->root
.type
== bfd_link_hash_defined
1850 || (sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1851 && (sym_hash
)->root
.u
.def
.section
== sec
1852 && (sym_hash
)->root
.u
.def
.value
> addr
1853 && (sym_hash
)->root
.u
.def
.value
< toaddr
)
1855 (sym_hash
)->root
.u
.def
.value
-= count
;
1859 /* See if we can move the ALIGN reloc forward. We have adjusted
1860 r_offset for it already. */
1861 if (irelalign
!= NULL
)
1863 bfd_vma alignto
, alignaddr
;
1865 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1866 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1867 1 << irelalign
->r_addend
);
1868 if (alignto
!= alignaddr
)
1870 /* Tail recursion. */
1871 return sh_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1872 alignto
- alignaddr
);
1879 /* Look for loads and stores which we can align to four byte
1880 boundaries. This is like sh_align_loads in coff-sh.c. */
1883 sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
, pswapped
)
1886 Elf_Internal_Rela
*internal_relocs
;
1890 Elf_Internal_Rela
*irel
, *irelend
;
1891 bfd_vma
*labels
= NULL
;
1892 bfd_vma
*label
, *label_end
;
1896 irelend
= internal_relocs
+ sec
->reloc_count
;
1898 /* Get all the addresses with labels on them. */
1899 labels
= (bfd_vma
*) bfd_malloc (sec
->reloc_count
* sizeof (bfd_vma
));
1903 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1905 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_LABEL
)
1907 *label_end
= irel
->r_offset
;
1912 /* Note that the assembler currently always outputs relocs in
1913 address order. If that ever changes, this code will need to sort
1914 the label values and the relocs. */
1918 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1920 bfd_vma start
, stop
;
1922 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
)
1925 start
= irel
->r_offset
;
1927 for (irel
++; irel
< irelend
; irel
++)
1928 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_DATA
)
1931 stop
= irel
->r_offset
;
1933 stop
= sec
->_cooked_size
;
1935 if (! _bfd_sh_align_load_span (abfd
, sec
, contents
, sh_elf_swap_insns
,
1936 (PTR
) internal_relocs
, &label
,
1937 label_end
, start
, stop
, pswapped
))
1951 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1954 sh_elf_swap_insns (abfd
, sec
, relocs
, contents
, addr
)
1961 Elf_Internal_Rela
*internal_relocs
= (Elf_Internal_Rela
*) relocs
;
1962 unsigned short i1
, i2
;
1963 Elf_Internal_Rela
*irel
, *irelend
;
1965 /* Swap the instructions themselves. */
1966 i1
= bfd_get_16 (abfd
, contents
+ addr
);
1967 i2
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1968 bfd_put_16 (abfd
, i2
, contents
+ addr
);
1969 bfd_put_16 (abfd
, i1
, contents
+ addr
+ 2);
1971 /* Adjust all reloc addresses. */
1972 irelend
= internal_relocs
+ sec
->reloc_count
;
1973 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1975 enum elf_sh_reloc_type type
;
1978 /* There are a few special types of relocs that we don't want to
1979 adjust. These relocs do not apply to the instruction itself,
1980 but are only associated with the address. */
1981 type
= (enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
);
1982 if (type
== R_SH_ALIGN
1983 || type
== R_SH_CODE
1984 || type
== R_SH_DATA
1985 || type
== R_SH_LABEL
)
1988 /* If an R_SH_USES reloc points to one of the addresses being
1989 swapped, we must adjust it. It would be incorrect to do this
1990 for a jump, though, since we want to execute both
1991 instructions after the jump. (We have avoided swapping
1992 around a label, so the jump will not wind up executing an
1993 instruction it shouldn't). */
1994 if (type
== R_SH_USES
)
1998 off
= irel
->r_offset
+ 4 + irel
->r_addend
;
2000 irel
->r_offset
+= 2;
2001 else if (off
== addr
+ 2)
2002 irel
->r_offset
-= 2;
2005 if (irel
->r_offset
== addr
)
2007 irel
->r_offset
+= 2;
2010 else if (irel
->r_offset
== addr
+ 2)
2012 irel
->r_offset
-= 2;
2021 unsigned short insn
, oinsn
;
2024 loc
= contents
+ irel
->r_offset
;
2033 insn
= bfd_get_16 (abfd
, loc
);
2036 if ((oinsn
& 0xff00) != (insn
& 0xff00))
2038 bfd_put_16 (abfd
, insn
, loc
);
2042 insn
= bfd_get_16 (abfd
, loc
);
2045 if ((oinsn
& 0xf000) != (insn
& 0xf000))
2047 bfd_put_16 (abfd
, insn
, loc
);
2051 /* This reloc ignores the least significant 3 bits of
2052 the program counter before adding in the offset.
2053 This means that if ADDR is at an even address, the
2054 swap will not affect the offset. If ADDR is an at an
2055 odd address, then the instruction will be crossing a
2056 four byte boundary, and must be adjusted. */
2057 if ((addr
& 3) != 0)
2059 insn
= bfd_get_16 (abfd
, loc
);
2062 if ((oinsn
& 0xff00) != (insn
& 0xff00))
2064 bfd_put_16 (abfd
, insn
, loc
);
2072 ((*_bfd_error_handler
)
2073 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
2074 bfd_get_filename (abfd
), (unsigned long) irel
->r_offset
));
2075 bfd_set_error (bfd_error_bad_value
);
2084 /* The size in bytes of an entry in the procedure linkage table. */
2086 #define PLT_ENTRY_SIZE 28
2088 /* First entry in an absolute procedure linkage table look like this. */
2090 static const bfd_byte elf_sh_plt0_entry_be
[PLT_ENTRY_SIZE
] = {
2091 0xd0, 0x04, /* mov.l 1f,r0 */
2092 0xd2, 0x05, /* mov.l 2f,r2 */
2093 0x60, 0x02, /* mov.l @r0,r0 */
2094 0x62, 0x22, /* mov.l @r2,r2 */
2095 0x40, 0x2b, /* jmp @r0 */
2096 0xe0, 0x00, /* mov #0,r0 */
2097 0x00, 0x09, /* nop */
2098 0x00, 0x09, /* nop */
2099 0x00, 0x09, /* nop */
2100 0x00, 0x09, /* nop */
2101 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2102 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2105 static const bfd_byte elf_sh_plt0_entry_le
[PLT_ENTRY_SIZE
] = {
2106 0x04, 0xd0, /* mov.l 1f,r0 */
2107 0x05, 0xd2, /* mov.l 2f,r2 */
2108 0x02, 0x60, /* mov.l @r0,r0 */
2109 0x22, 0x62, /* mov.l @r2,r2 */
2110 0x2b, 0x40, /* jmp @r0 */
2111 0x00, 0xe0, /* mov #0,r0 */
2112 0x09, 0x00, /* nop */
2113 0x09, 0x00, /* nop */
2114 0x09, 0x00, /* nop */
2115 0x09, 0x00, /* nop */
2116 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2117 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2120 /* Sebsequent entries in an absolute procedure linkage table look like
2123 static const bfd_byte elf_sh_plt_entry_be
[PLT_ENTRY_SIZE
] = {
2124 0xd0, 0x04, /* mov.l 1f,r0 */
2125 0x60, 0x02, /* mov.l @r0,r0 */
2126 0xd2, 0x02, /* mov.l 0f,r2 */
2127 0x40, 0x2b, /* jmp @r0 */
2128 0x60, 0x23, /* mov r2,r0 */
2129 0xd1, 0x03, /* mov.l 2f,r1 */
2130 0x40, 0x2b, /* jmp @r0 */
2131 0x00, 0x09, /* nop */
2132 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
2133 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2134 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2137 static const bfd_byte elf_sh_plt_entry_le
[PLT_ENTRY_SIZE
] = {
2138 0x04, 0xd0, /* mov.l 1f,r0 */
2139 0x02, 0x60, /* mov.l @r0,r0 */
2140 0x02, 0xd2, /* mov.l 0f,r2 */
2141 0x2b, 0x40, /* jmp @r0 */
2142 0x23, 0x60, /* mov r2,r0 */
2143 0x03, 0xd1, /* mov.l 2f,r1 */
2144 0x2b, 0x40, /* jmp @r0 */
2145 0x09, 0x00, /* nop */
2146 0, 0, 0, 0, /* 0: replaced with address of .PLT. */
2147 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2148 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2151 /* Entries in a PIC procedure linkage table look like this. */
2153 static const bfd_byte elf_sh_pic_plt_entry_be
[PLT_ENTRY_SIZE
] = {
2154 0xd0, 0x04, /* mov.l 1f,r0 */
2155 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2156 0x40, 0x2b, /* jmp @r0 */
2157 0x00, 0x09, /* nop */
2158 0x50, 0xc2, /* 0: mov.l @(8,r12),r0 */
2159 0x52, 0xc1, /* 1: mov.l @(4,r12),r2 */
2160 0xd1, 0x02, /* mov.l 2f,r1 */
2161 0x40, 0x2b, /* jmp @r0 */
2162 0xe0, 0x00, /* mov #0,r0 ! shows the type of PLT. */
2163 0x00, 0x09, /* nop */
2164 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2165 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2168 static const bfd_byte elf_sh_pic_plt_entry_le
[PLT_ENTRY_SIZE
] = {
2169 0x04, 0xd0, /* mov.l 1f,r0 */
2170 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2171 0x2b, 0x40, /* jmp @r0 */
2172 0x09, 0x00, /* nop */
2173 0xc2, 0x50, /* 0: mov.l @(8,r12),r0 */
2174 0xc1, 0x52, /* 1: mov.l @(4,r12),r2 */
2175 0x02, 0xd1, /* mov.l 2f,r1 */
2176 0x2b, 0x40, /* jmp @r0 */
2177 0x00, 0xe0, /* mov #0,r0 ! shows the type of PLT. */
2178 0x09, 0x00, /* nop */
2179 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2180 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2183 static const bfd_byte
*elf_sh_plt0_entry
;
2184 static const bfd_byte
*elf_sh_plt_entry
;
2185 static const bfd_byte
*elf_sh_pic_plt_entry
;
2187 /* Return size of a PLT entry. */
2188 #define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
2190 /* Return offset of the PLT0 address in an absolute PLT entry. */
2191 #define elf_sh_plt_plt0_offset(info) 16
2193 /* Return offset of the linker in PLT0 entry. */
2194 #define elf_sh_plt0_linker_offset(info) 20
2196 /* Return offset of the GOT id in PLT0 entry. */
2197 #define elf_sh_plt0_gotid_offset(info) 24
2199 /* Return offset of the tempoline in PLT entry */
2200 #define elf_sh_plt_temp_offset(info) 8
2202 /* Return offset of the symbol in PLT entry. */
2203 #define elf_sh_plt_symbol_offset(info) 20
2205 /* Return offset of the relocation in PLT entry. */
2206 #define elf_sh_plt_reloc_offset(info) 24
2208 /* The sh linker needs to keep track of the number of relocs that it
2209 decides to copy in check_relocs for each symbol. This is so that
2210 it can discard PC relative relocs if it doesn't need them when
2211 linking with -Bsymbolic. We store the information in a field
2212 extending the regular ELF linker hash table. */
2214 /* This structure keeps track of the number of PC relative relocs we
2215 have copied for a given symbol. */
2217 struct elf_sh_pcrel_relocs_copied
{
2219 struct elf_sh_pcrel_relocs_copied
*next
;
2220 /* A section in dynobj. */
2222 /* Number of relocs copied in this section. */
2223 bfd_size_type count
;
2226 /* sh ELF linker hash entry. */
2228 struct elf_sh_link_hash_entry
{
2229 struct elf_link_hash_entry root
;
2231 /* Number of PC relative relocs copied for this symbol. */
2232 struct elf_sh_pcrel_relocs_copied
*pcrel_relocs_copied
;
2235 /* sh ELF linker hash table. */
2237 struct elf_sh_link_hash_table
{
2238 struct elf_link_hash_table root
;
2241 /* Declare this now that the above structures are defined. */
2243 static boolean sh_elf_discard_copies
2244 PARAMS ((struct elf_sh_link_hash_entry
*, PTR
));
2246 /* Traverse an sh ELF linker hash table. */
2248 #define sh_elf_link_hash_traverse(table, func, info) \
2249 (elf_link_hash_traverse \
2251 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2254 /* Get the sh ELF linker hash table from a link_info structure. */
2256 #define sh_elf_hash_table(p) \
2257 ((struct elf_sh_link_hash_table *) ((p)->hash))
2259 /* Create an entry in an sh ELF linker hash table. */
2261 static struct bfd_hash_entry
*
2262 sh_elf_link_hash_newfunc (entry
, table
, string
)
2263 struct bfd_hash_entry
*entry
;
2264 struct bfd_hash_table
*table
;
2267 struct elf_sh_link_hash_entry
*ret
=
2268 (struct elf_sh_link_hash_entry
*) entry
;
2270 /* Allocate the structure if it has not already been allocated by a
2272 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2273 ret
= ((struct elf_sh_link_hash_entry
*)
2274 bfd_hash_allocate (table
,
2275 sizeof (struct elf_sh_link_hash_entry
)));
2276 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2277 return (struct bfd_hash_entry
*) ret
;
2279 /* Call the allocation method of the superclass. */
2280 ret
= ((struct elf_sh_link_hash_entry
*)
2281 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2283 if (ret
!= (struct elf_sh_link_hash_entry
*) NULL
)
2285 ret
->pcrel_relocs_copied
= NULL
;
2288 return (struct bfd_hash_entry
*) ret
;
2291 /* Create an sh ELF linker hash table. */
2293 static struct bfd_link_hash_table
*
2294 sh_elf_link_hash_table_create (abfd
)
2297 struct elf_sh_link_hash_table
*ret
;
2299 ret
= ((struct elf_sh_link_hash_table
*)
2300 bfd_alloc (abfd
, sizeof (struct elf_sh_link_hash_table
)));
2301 if (ret
== (struct elf_sh_link_hash_table
*) NULL
)
2304 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2305 sh_elf_link_hash_newfunc
))
2307 bfd_release (abfd
, ret
);
2311 return &ret
->root
.root
;
2314 /* Create dynamic sections when linking against a dynamic object. */
2317 sh_elf_create_dynamic_sections (abfd
, info
)
2319 struct bfd_link_info
*info
;
2321 flagword flags
, pltflags
;
2322 register asection
*s
;
2323 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2326 switch (bed
->s
->arch_size
)
2337 bfd_set_error (bfd_error_bad_value
);
2341 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2342 .rel[a].bss sections. */
2344 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2345 | SEC_LINKER_CREATED
);
2348 pltflags
|= SEC_CODE
;
2349 if (bed
->plt_not_loaded
)
2350 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
2351 if (bed
->plt_readonly
)
2352 pltflags
|= SEC_READONLY
;
2354 s
= bfd_make_section (abfd
, ".plt");
2356 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
2357 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
2360 if (bed
->want_plt_sym
)
2362 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2364 struct elf_link_hash_entry
*h
= NULL
;
2365 if (! (_bfd_generic_link_add_one_symbol
2366 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
2367 (bfd_vma
) 0, (const char *) NULL
, false,
2368 get_elf_backend_data (abfd
)->collect
,
2369 (struct bfd_link_hash_entry
**) &h
)))
2371 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2372 h
->type
= STT_OBJECT
;
2375 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2379 s
= bfd_make_section (abfd
,
2380 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
2382 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2383 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2386 if (! _bfd_elf_create_got_section (abfd
, info
))
2390 const char *secname
;
2395 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2397 secflags
= bfd_get_section_flags (abfd
, sec
);
2398 if ((secflags
& (SEC_DATA
| SEC_LINKER_CREATED
))
2399 || ((secflags
& SEC_HAS_CONTENTS
) != SEC_HAS_CONTENTS
))
2401 secname
= bfd_get_section_name (abfd
, sec
);
2402 relname
= (char *) bfd_malloc (strlen (secname
) + 6);
2403 strcpy (relname
, ".rela");
2404 strcat (relname
, secname
);
2405 s
= bfd_make_section (abfd
, relname
);
2407 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2408 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2413 if (bed
->want_dynbss
)
2415 /* The .dynbss section is a place to put symbols which are defined
2416 by dynamic objects, are referenced by regular objects, and are
2417 not functions. We must allocate space for them in the process
2418 image and use a R_*_COPY reloc to tell the dynamic linker to
2419 initialize them at run time. The linker script puts the .dynbss
2420 section into the .bss section of the final image. */
2421 s
= bfd_make_section (abfd
, ".dynbss");
2423 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
2426 /* The .rel[a].bss section holds copy relocs. This section is not
2427 normally needed. We need to create it here, though, so that the
2428 linker will map it to an output section. We can't just create it
2429 only if we need it, because we will not know whether we need it
2430 until we have seen all the input files, and the first time the
2431 main linker code calls BFD after examining all the input files
2432 (size_dynamic_sections) the input sections have already been
2433 mapped to the output sections. If the section turns out not to
2434 be needed, we can discard it later. We will never need this
2435 section when generating a shared object, since they do not use
2439 s
= bfd_make_section (abfd
,
2440 (bed
->default_use_rela_p
2441 ? ".rela.bss" : ".rel.bss"));
2443 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2444 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2452 /* Adjust a symbol defined by a dynamic object and referenced by a
2453 regular object. The current definition is in some section of the
2454 dynamic object, but we're not including those sections. We have to
2455 change the definition to something the rest of the link can
2459 sh_elf_adjust_dynamic_symbol (info
, h
)
2460 struct bfd_link_info
*info
;
2461 struct elf_link_hash_entry
*h
;
2465 unsigned int power_of_two
;
2467 dynobj
= elf_hash_table (info
)->dynobj
;
2469 /* Make sure we know what is going on here. */
2470 BFD_ASSERT (dynobj
!= NULL
2471 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
2472 || h
->weakdef
!= NULL
2473 || ((h
->elf_link_hash_flags
2474 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2475 && (h
->elf_link_hash_flags
2476 & ELF_LINK_HASH_REF_REGULAR
) != 0
2477 && (h
->elf_link_hash_flags
2478 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
2480 /* If this is a function, put it in the procedure linkage table. We
2481 will fill in the contents of the procedure linkage table later,
2482 when we know the address of the .got section. */
2483 if (h
->type
== STT_FUNC
2484 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
2487 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2488 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
2490 /* This case can occur if we saw a PLT reloc in an input
2491 file, but the symbol was never referred to by a dynamic
2492 object. In such a case, we don't actually need to build
2493 a procedure linkage table, and we can just do a REL32
2495 BFD_ASSERT ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0);
2499 /* Make sure this symbol is output as a dynamic symbol. */
2500 if (h
->dynindx
== -1)
2502 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
2506 s
= bfd_get_section_by_name (dynobj
, ".plt");
2507 BFD_ASSERT (s
!= NULL
);
2509 /* If this is the first .plt entry, make room for the special
2511 if (s
->_raw_size
== 0)
2512 s
->_raw_size
+= PLT_ENTRY_SIZE
;
2514 /* If this symbol is not defined in a regular file, and we are
2515 not generating a shared library, then set the symbol to this
2516 location in the .plt. This is required to make function
2517 pointers compare as equal between the normal executable and
2518 the shared library. */
2520 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2522 h
->root
.u
.def
.section
= s
;
2523 h
->root
.u
.def
.value
= s
->_raw_size
;
2526 h
->plt
.offset
= s
->_raw_size
;
2528 /* Make room for this entry. */
2529 s
->_raw_size
+= elf_sh_sizeof_plt (info
);
2531 /* We also need to make an entry in the .got.plt section, which
2532 will be placed in the .got section by the linker script. */
2534 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
2535 BFD_ASSERT (s
!= NULL
);
2538 /* We also need to make an entry in the .rela.plt section. */
2540 s
= bfd_get_section_by_name (dynobj
, ".rela.plt");
2541 BFD_ASSERT (s
!= NULL
);
2542 s
->_raw_size
+= sizeof (Elf32_External_Rela
);
2547 /* If this is a weak symbol, and there is a real definition, the
2548 processor independent code will have arranged for us to see the
2549 real definition first, and we can just use the same value. */
2550 if (h
->weakdef
!= NULL
)
2552 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
2553 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
2554 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
2555 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
2559 /* This is a reference to a symbol defined by a dynamic object which
2560 is not a function. */
2562 /* If we are creating a shared library, we must presume that the
2563 only references to the symbol are via the global offset table.
2564 For such cases we need not do anything here; the relocations will
2565 be handled correctly by relocate_section. */
2569 /* If there are no references to this symbol that do not use the
2570 GOT, we don't need to generate a copy reloc. */
2571 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0)
2574 /* We must allocate the symbol in our .dynbss section, which will
2575 become part of the .bss section of the executable. There will be
2576 an entry for this symbol in the .dynsym section. The dynamic
2577 object will contain position independent code, so all references
2578 from the dynamic object to this symbol will go through the global
2579 offset table. The dynamic linker will use the .dynsym entry to
2580 determine the address it must put in the global offset table, so
2581 both the dynamic object and the regular object will refer to the
2582 same memory location for the variable. */
2584 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
2585 BFD_ASSERT (s
!= NULL
);
2587 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2588 copy the initial value out of the dynamic object and into the
2589 runtime process image. We need to remember the offset into the
2590 .rela.bss section we are going to use. */
2591 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
2595 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
2596 BFD_ASSERT (srel
!= NULL
);
2597 srel
->_raw_size
+= sizeof (Elf32_External_Rela
);
2598 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
2601 /* We need to figure out the alignment required for this symbol. I
2602 have no idea how ELF linkers handle this. */
2603 power_of_two
= bfd_log2 (h
->size
);
2604 if (power_of_two
> 3)
2607 /* Apply the required alignment. */
2608 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
2609 (bfd_size_type
) (1 << power_of_two
));
2610 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
2612 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
2616 /* Define the symbol as being at this point in the section. */
2617 h
->root
.u
.def
.section
= s
;
2618 h
->root
.u
.def
.value
= s
->_raw_size
;
2620 /* Increment the section size to make room for the symbol. */
2621 s
->_raw_size
+= h
->size
;
2626 /* Set the sizes of the dynamic sections. */
2629 sh_elf_size_dynamic_sections (output_bfd
, info
)
2631 struct bfd_link_info
*info
;
2639 dynobj
= elf_hash_table (info
)->dynobj
;
2640 BFD_ASSERT (dynobj
!= NULL
);
2642 if (elf_hash_table (info
)->dynamic_sections_created
)
2644 /* Set the contents of the .interp section to the interpreter. */
2647 s
= bfd_get_section_by_name (dynobj
, ".interp");
2648 BFD_ASSERT (s
!= NULL
);
2649 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
2650 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2655 /* We may have created entries in the .rela.got section.
2656 However, if we are not creating the dynamic sections, we will
2657 not actually use these entries. Reset the size of .rela.got,
2658 which will cause it to get stripped from the output file
2660 s
= bfd_get_section_by_name (dynobj
, ".rela.got");
2665 /* If this is a -Bsymbolic shared link, then we need to discard all
2666 PC relative relocs against symbols defined in a regular object.
2667 We allocated space for them in the check_relocs routine, but we
2668 will not fill them in in the relocate_section routine. */
2669 if (info
->shared
&& info
->symbolic
)
2670 sh_elf_link_hash_traverse (sh_elf_hash_table (info
),
2671 sh_elf_discard_copies
,
2674 /* The check_relocs and adjust_dynamic_symbol entry points have
2675 determined the sizes of the various dynamic sections. Allocate
2680 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2685 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2688 /* It's OK to base decisions on the section name, because none
2689 of the dynobj section names depend upon the input files. */
2690 name
= bfd_get_section_name (dynobj
, s
);
2694 if (strcmp (name
, ".plt") == 0)
2696 if (s
->_raw_size
== 0)
2698 /* Strip this section if we don't need it; see the
2704 /* Remember whether there is a PLT. */
2708 else if (strncmp (name
, ".rela", 5) == 0)
2710 if (s
->_raw_size
== 0)
2712 /* If we don't need this section, strip it from the
2713 output file. This is mostly to handle .rela.bss and
2714 .rela.plt. We must create both sections in
2715 create_dynamic_sections, because they must be created
2716 before the linker maps input sections to output
2717 sections. The linker does that before
2718 adjust_dynamic_symbol is called, and it is that
2719 function which decides whether anything needs to go
2720 into these sections. */
2727 /* Remember whether there are any reloc sections other
2729 if (strcmp (name
, ".rela.plt") != 0)
2731 const char *outname
;
2735 /* If this relocation section applies to a read only
2736 section, then we probably need a DT_TEXTREL
2737 entry. The entries in the .rela.plt section
2738 really apply to the .got section, which we
2739 created ourselves and so know is not readonly. */
2740 outname
= bfd_get_section_name (output_bfd
,
2742 target
= bfd_get_section_by_name (output_bfd
, outname
+ 5);
2744 && (target
->flags
& SEC_READONLY
) != 0
2745 && (target
->flags
& SEC_ALLOC
) != 0)
2749 /* We use the reloc_count field as a counter if we need
2750 to copy relocs into the output file. */
2754 else if (strncmp (name
, ".got", 4) != 0)
2756 /* It's not one of our sections, so don't allocate space. */
2762 _bfd_strip_section_from_output (info
, s
);
2766 /* Allocate memory for the section contents. */
2767 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
2768 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2772 if (elf_hash_table (info
)->dynamic_sections_created
)
2774 /* Add some entries to the .dynamic section. We fill in the
2775 values later, in sh_elf_finish_dynamic_sections, but we
2776 must add the entries now so that we get the correct size for
2777 the .dynamic section. The DT_DEBUG entry is filled in by the
2778 dynamic linker and used by the debugger. */
2781 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
2787 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0)
2788 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
2789 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_RELA
)
2790 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0))
2796 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELA
, 0)
2797 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELASZ
, 0)
2798 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELAENT
,
2799 sizeof (Elf32_External_Rela
)))
2805 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
2813 /* This function is called via sh_elf_link_hash_traverse if we are
2814 creating a shared object with -Bsymbolic. It discards the space
2815 allocated to copy PC relative relocs against symbols which are
2816 defined in regular objects. We allocated space for them in the
2817 check_relocs routine, but we won't fill them in in the
2818 relocate_section routine. */
2821 sh_elf_discard_copies (h
, ignore
)
2822 struct elf_sh_link_hash_entry
*h
;
2823 PTR ignore ATTRIBUTE_UNUSED
;
2825 struct elf_sh_pcrel_relocs_copied
*s
;
2827 /* We only discard relocs for symbols defined in a regular object. */
2828 if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2831 for (s
= h
->pcrel_relocs_copied
; s
!= NULL
; s
= s
->next
)
2832 s
->section
->_raw_size
-= s
->count
* sizeof (Elf32_External_Rela
);
2838 /* Relocate an SH ELF section. */
2841 sh_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
2842 contents
, relocs
, local_syms
, local_sections
)
2843 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2844 struct bfd_link_info
*info
;
2846 asection
*input_section
;
2848 Elf_Internal_Rela
*relocs
;
2849 Elf_Internal_Sym
*local_syms
;
2850 asection
**local_sections
;
2852 Elf_Internal_Shdr
*symtab_hdr
;
2853 struct elf_link_hash_entry
**sym_hashes
;
2854 Elf_Internal_Rela
*rel
, *relend
;
2856 bfd_vma
*local_got_offsets
;
2861 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2862 sym_hashes
= elf_sym_hashes (input_bfd
);
2863 dynobj
= elf_hash_table (info
)->dynobj
;
2864 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2871 relend
= relocs
+ input_section
->reloc_count
;
2872 for (; rel
< relend
; rel
++)
2875 reloc_howto_type
*howto
;
2876 unsigned long r_symndx
;
2877 Elf_Internal_Sym
*sym
;
2879 struct elf_link_hash_entry
*h
;
2881 bfd_vma addend
= (bfd_vma
) 0;
2882 bfd_reloc_status_type r
;
2884 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2886 r_type
= ELF32_R_TYPE (rel
->r_info
);
2888 /* Many of the relocs are only used for relaxing, and are
2889 handled entirely by the relaxation code. */
2890 if (r_type
> (int) R_SH_LAST_INVALID_RELOC
2891 && r_type
< (int) R_SH_LOOP_START
)
2893 if (r_type
== (int) R_SH_NONE
)
2897 || r_type
>= R_SH_max
2898 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC
2899 && r_type
<= (int) R_SH_LAST_INVALID_RELOC
)
2900 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC_2
2901 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_2
))
2903 bfd_set_error (bfd_error_bad_value
);
2907 howto
= sh_elf_howto_table
+ r_type
;
2909 /* This is a final link. */
2913 if (r_symndx
< symtab_hdr
->sh_info
)
2915 sym
= local_syms
+ r_symndx
;
2916 sec
= local_sections
[r_symndx
];
2917 relocation
= (sec
->output_section
->vma
2918 + sec
->output_offset
2921 if (info
->relocateable
)
2923 /* This is a relocateable link. We don't have to change
2924 anything, unless the reloc is against a section symbol,
2925 in which case we have to adjust according to where the
2926 section symbol winds up in the output section. */
2927 sym
= local_syms
+ r_symndx
;
2928 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
2929 goto final_link_relocate
;
2936 /* Section symbol are never (?) placed in the hash table, so
2937 we can just ignore hash relocations when creating a
2938 relocateable object file. */
2939 if (info
->relocateable
)
2942 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
2943 while (h
->root
.type
== bfd_link_hash_indirect
2944 || h
->root
.type
== bfd_link_hash_warning
)
2945 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2946 if (h
->root
.type
== bfd_link_hash_defined
2947 || h
->root
.type
== bfd_link_hash_defweak
)
2949 sec
= h
->root
.u
.def
.section
;
2950 /* In these cases, we don't need the relocation value.
2951 We check specially because in some obscure cases
2952 sec->output_section will be NULL. */
2953 if (r_type
== R_SH_GOTPC
2954 || (r_type
== R_SH_PLT32
2955 && h
->plt
.offset
!= (bfd_vma
) -1)
2956 || (r_type
== R_SH_GOT32
2957 && elf_hash_table (info
)->dynamic_sections_created
2959 || (! info
->symbolic
&& h
->dynindx
!= -1)
2960 || (h
->elf_link_hash_flags
2961 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
2962 /* The cases above are those in which relocation is
2963 overwritten in the switch block below. The cases
2964 below are those in which we must defer relocation
2965 to run-time, because we can't resolve absolute
2966 addresses when creating a shared library. */
2968 && ((! info
->symbolic
&& h
->dynindx
!= -1)
2969 || (h
->elf_link_hash_flags
2970 & ELF_LINK_HASH_DEF_REGULAR
) == 0)
2971 && ((r_type
== R_SH_DIR32
2972 && !(ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2973 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
))
2974 || r_type
== R_SH_REL32
)
2975 && ((input_section
->flags
& SEC_ALLOC
) != 0
2976 /* DWARF will emit R_SH_DIR32 relocations in its
2977 sections against symbols defined externally
2978 in shared libraries. We can't do anything
2980 || (input_section
->flags
& SEC_DEBUGGING
) != 0)))
2982 else if (sec
->output_section
== NULL
)
2984 (*_bfd_error_handler
)
2985 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2986 bfd_get_filename (input_bfd
), h
->root
.root
.string
,
2987 bfd_get_section_name (input_bfd
, input_section
));
2991 relocation
= (h
->root
.u
.def
.value
2992 + sec
->output_section
->vma
2993 + sec
->output_offset
);
2995 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2997 else if (info
->shared
&& !info
->symbolic
&& !info
->no_undefined
)
3001 if (! ((*info
->callbacks
->undefined_symbol
)
3002 (info
, h
->root
.root
.string
, input_bfd
,
3003 input_section
, rel
->r_offset
, true)))
3009 switch ((int) r_type
)
3011 final_link_relocate
:
3012 /* COFF relocs don't use the addend. The addend is used for
3013 R_SH_DIR32 to be compatible with other compilers. */
3014 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3015 contents
, rel
->r_offset
,
3016 relocation
, addend
);
3023 /* These should normally be handled by the assembler, but at
3024 least IND12W is generated by ourselves, so we must deal
3027 goto final_link_relocate
;
3030 bfd_set_error (bfd_error_bad_value
);
3036 && (input_section
->flags
& SEC_ALLOC
) != 0
3037 && (r_type
!= R_SH_REL32
3040 && (! info
->symbolic
3041 || (h
->elf_link_hash_flags
3042 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
3044 Elf_Internal_Rela outrel
;
3045 boolean skip
, relocate
;
3047 /* When generating a shared object, these relocations
3048 are copied into the output file to be resolved at run
3055 name
= (bfd_elf_string_from_elf_section
3057 elf_elfheader (input_bfd
)->e_shstrndx
,
3058 elf_section_data (input_section
)->rel_hdr
.sh_name
));
3062 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
3063 && strcmp (bfd_get_section_name (input_bfd
,
3067 sreloc
= bfd_get_section_by_name (dynobj
, name
);
3068 BFD_ASSERT (sreloc
!= NULL
);
3073 if (elf_section_data (input_section
)->stab_info
== NULL
)
3074 outrel
.r_offset
= rel
->r_offset
;
3079 off
= (_bfd_stab_section_offset
3080 (output_bfd
, &elf_hash_table (info
)->stab_info
,
3082 &elf_section_data (input_section
)->stab_info
,
3084 if (off
== (bfd_vma
) -1)
3086 outrel
.r_offset
= off
;
3089 outrel
.r_offset
+= (input_section
->output_section
->vma
3090 + input_section
->output_offset
);
3094 memset (&outrel
, 0, sizeof outrel
);
3097 else if (r_type
== R_SH_REL32
)
3099 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
3101 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_REL32
);
3102 outrel
.r_addend
= rel
->r_addend
;
3106 /* h->dynindx may be -1 if this symbol was marked to
3109 || ((info
->symbolic
|| h
->dynindx
== -1)
3110 && (h
->elf_link_hash_flags
3111 & ELF_LINK_HASH_DEF_REGULAR
) != 0))
3114 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3115 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3119 BFD_ASSERT (h
->dynindx
!= -1);
3121 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_DIR32
);
3122 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3126 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3127 (((Elf32_External_Rela
*)
3129 + sreloc
->reloc_count
));
3130 ++sreloc
->reloc_count
;
3132 /* If this reloc is against an external symbol, we do
3133 not want to fiddle with the addend. Otherwise, we
3134 need to include the symbol value so that it becomes
3135 an addend for the dynamic reloc. */
3139 else if (r_type
== R_SH_DIR32
)
3140 addend
= rel
->r_addend
;
3141 goto final_link_relocate
;
3144 /* Relocation is to the entry for this symbol in the global
3148 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3149 BFD_ASSERT (sgot
!= NULL
);
3156 off
= h
->got
.offset
;
3157 BFD_ASSERT (off
!= (bfd_vma
) -1);
3159 if (! elf_hash_table (info
)->dynamic_sections_created
3161 && (info
->symbolic
|| h
->dynindx
== -1
3162 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3163 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3164 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
3166 /* This is actually a static link, or it is a
3167 -Bsymbolic link and the symbol is defined
3168 locally, or the symbol was forced to be local
3169 because of a version file. We must initialize
3170 this entry in the global offset table. Since the
3171 offset must always be a multiple of 4, we use the
3172 least significant bit to record whether we have
3173 initialized it already.
3175 When doing a dynamic link, we create a .rela.got
3176 relocation entry to initialize the value. This
3177 is done in the finish_dynamic_symbol routine. */
3182 bfd_put_32 (output_bfd
, relocation
,
3183 sgot
->contents
+ off
);
3188 relocation
= sgot
->output_offset
+ off
;
3194 BFD_ASSERT (local_got_offsets
!= NULL
3195 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
3197 off
= local_got_offsets
[r_symndx
];
3199 /* The offset must always be a multiple of 4. We use
3200 the least significant bit to record whether we have
3201 already generated the necessary reloc. */
3206 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
3211 Elf_Internal_Rela outrel
;
3213 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
3214 BFD_ASSERT (srelgot
!= NULL
);
3216 outrel
.r_offset
= (sgot
->output_section
->vma
3217 + sgot
->output_offset
3219 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3220 outrel
.r_addend
= relocation
;
3221 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3222 (((Elf32_External_Rela
*)
3224 + srelgot
->reloc_count
));
3225 ++srelgot
->reloc_count
;
3228 local_got_offsets
[r_symndx
] |= 1;
3231 relocation
= sgot
->output_offset
+ off
;
3234 goto final_link_relocate
;
3237 /* Relocation is relative to the start of the global offset
3242 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3243 BFD_ASSERT (sgot
!= NULL
);
3246 /* Note that sgot->output_offset is not involved in this
3247 calculation. We always want the start of .got. If we
3248 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3249 permitted by the ABI, we might have to change this
3251 relocation
-= sgot
->output_section
->vma
;
3253 goto final_link_relocate
;
3256 /* Use global offset table as symbol value. */
3260 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3261 BFD_ASSERT (sgot
!= NULL
);
3264 relocation
= sgot
->output_section
->vma
;
3266 goto final_link_relocate
;
3269 /* Relocation is to the entry for this symbol in the
3270 procedure linkage table. */
3272 /* Resolve a PLT reloc against a local symbol directly,
3273 without using the procedure linkage table. */
3275 goto final_link_relocate
;
3277 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3278 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3279 goto final_link_relocate
;
3281 if (h
->plt
.offset
== (bfd_vma
) -1)
3283 /* We didn't make a PLT entry for this symbol. This
3284 happens when statically linking PIC code, or when
3285 using -Bsymbolic. */
3286 goto final_link_relocate
;
3291 splt
= bfd_get_section_by_name (dynobj
, ".plt");
3292 BFD_ASSERT (splt
!= NULL
);
3295 relocation
= (splt
->output_section
->vma
3296 + splt
->output_offset
3299 goto final_link_relocate
;
3301 case R_SH_LOOP_START
:
3303 static bfd_vma start
, end
;
3305 start
= (relocation
+ rel
->r_addend
3306 - (sec
->output_section
->vma
+ sec
->output_offset
));
3307 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
3308 rel
->r_offset
, sec
, start
, end
);
3312 end
= (relocation
+ rel
->r_addend
3313 - (sec
->output_section
->vma
+ sec
->output_offset
));
3314 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
3315 rel
->r_offset
, sec
, start
, end
);
3320 if (r
!= bfd_reloc_ok
)
3325 case bfd_reloc_outofrange
:
3327 case bfd_reloc_overflow
:
3332 name
= h
->root
.root
.string
;
3335 name
= (bfd_elf_string_from_elf_section
3336 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
3340 name
= bfd_section_name (input_bfd
, sec
);
3342 if (! ((*info
->callbacks
->reloc_overflow
)
3343 (info
, name
, howto
->name
, (bfd_vma
) 0,
3344 input_bfd
, input_section
, rel
->r_offset
)))
3355 /* This is a version of bfd_generic_get_relocated_section_contents
3356 which uses sh_elf_relocate_section. */
3359 sh_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
3360 data
, relocateable
, symbols
)
3362 struct bfd_link_info
*link_info
;
3363 struct bfd_link_order
*link_order
;
3365 boolean relocateable
;
3368 Elf_Internal_Shdr
*symtab_hdr
;
3369 asection
*input_section
= link_order
->u
.indirect
.section
;
3370 bfd
*input_bfd
= input_section
->owner
;
3371 asection
**sections
= NULL
;
3372 Elf_Internal_Rela
*internal_relocs
= NULL
;
3373 Elf32_External_Sym
*external_syms
= NULL
;
3374 Elf_Internal_Sym
*internal_syms
= NULL
;
3376 /* We only need to handle the case of relaxing, or of having a
3377 particular set of section contents, specially. */
3379 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
3380 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
3385 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3387 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
3388 input_section
->_raw_size
);
3390 if ((input_section
->flags
& SEC_RELOC
) != 0
3391 && input_section
->reloc_count
> 0)
3393 Elf_Internal_Sym
*isymp
;
3395 Elf32_External_Sym
*esym
, *esymend
;
3397 if (symtab_hdr
->contents
!= NULL
)
3398 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
3401 external_syms
= ((Elf32_External_Sym
*)
3402 bfd_malloc (symtab_hdr
->sh_info
3403 * sizeof (Elf32_External_Sym
)));
3404 if (external_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
3406 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
3407 || (bfd_read (external_syms
, sizeof (Elf32_External_Sym
),
3408 symtab_hdr
->sh_info
, input_bfd
)
3409 != (symtab_hdr
->sh_info
* sizeof (Elf32_External_Sym
))))
3413 internal_relocs
= (_bfd_elf32_link_read_relocs
3414 (input_bfd
, input_section
, (PTR
) NULL
,
3415 (Elf_Internal_Rela
*) NULL
, false));
3416 if (internal_relocs
== NULL
)
3419 internal_syms
= ((Elf_Internal_Sym
*)
3420 bfd_malloc (symtab_hdr
->sh_info
3421 * sizeof (Elf_Internal_Sym
)));
3422 if (internal_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
3425 sections
= (asection
**) bfd_malloc (symtab_hdr
->sh_info
3426 * sizeof (asection
*));
3427 if (sections
== NULL
&& symtab_hdr
->sh_info
> 0)
3430 isymp
= internal_syms
;
3432 esym
= external_syms
;
3433 esymend
= esym
+ symtab_hdr
->sh_info
;
3434 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
3438 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
3440 if (isymp
->st_shndx
== SHN_UNDEF
)
3441 isec
= bfd_und_section_ptr
;
3442 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
3443 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
3444 else if (isymp
->st_shndx
== SHN_ABS
)
3445 isec
= bfd_abs_section_ptr
;
3446 else if (isymp
->st_shndx
== SHN_COMMON
)
3447 isec
= bfd_com_section_ptr
;
3457 if (! sh_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
3458 input_section
, data
, internal_relocs
,
3459 internal_syms
, sections
))
3462 if (sections
!= NULL
)
3465 if (internal_syms
!= NULL
)
3466 free (internal_syms
);
3467 internal_syms
= NULL
;
3468 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
3469 free (external_syms
);
3470 external_syms
= NULL
;
3471 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
3472 free (internal_relocs
);
3473 internal_relocs
= NULL
;
3479 if (internal_relocs
!= NULL
3480 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
3481 free (internal_relocs
);
3482 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
3483 free (external_syms
);
3484 if (internal_syms
!= NULL
)
3485 free (internal_syms
);
3486 if (sections
!= NULL
)
3491 sh_elf_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
3493 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
3494 Elf_Internal_Rela
*rel
;
3495 struct elf_link_hash_entry
*h
;
3496 Elf_Internal_Sym
*sym
;
3500 switch (ELF32_R_TYPE (rel
->r_info
))
3502 case R_SH_GNU_VTINHERIT
:
3503 case R_SH_GNU_VTENTRY
:
3507 switch (h
->root
.type
)
3509 case bfd_link_hash_defined
:
3510 case bfd_link_hash_defweak
:
3511 return h
->root
.u
.def
.section
;
3513 case bfd_link_hash_common
:
3514 return h
->root
.u
.c
.p
->section
;
3523 if (!(elf_bad_symtab (abfd
)
3524 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
3525 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
3526 && sym
->st_shndx
!= SHN_COMMON
))
3528 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
3534 /* Update the got entry reference counts for the section being removed. */
3537 sh_elf_gc_sweep_hook (abfd
, info
, sec
, relocs
)
3538 bfd
*abfd ATTRIBUTE_UNUSED
;
3539 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
3540 asection
*sec ATTRIBUTE_UNUSED
;
3541 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
;
3543 /* We use got and plt entries for sh, but it would seem that the
3544 existing SH code does no sort of reference counting or whatnot on
3545 its GOT and PLT entries, so it is not possible to garbage collect
3546 them at this time. */
3550 /* Look through the relocs for a section during the first phase.
3551 Since we don't do .gots or .plts, we just need to consider the
3552 virtual table relocs for gc. */
3555 sh_elf_check_relocs (abfd
, info
, sec
, relocs
)
3557 struct bfd_link_info
*info
;
3559 const Elf_Internal_Rela
*relocs
;
3561 Elf_Internal_Shdr
*symtab_hdr
;
3562 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
3563 const Elf_Internal_Rela
*rel
;
3564 const Elf_Internal_Rela
*rel_end
;
3566 bfd_vma
*local_got_offsets
;
3575 if (info
->relocateable
)
3578 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3579 sym_hashes
= elf_sym_hashes (abfd
);
3580 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
3581 if (!elf_bad_symtab (abfd
))
3582 sym_hashes_end
-= symtab_hdr
->sh_info
;
3584 dynobj
= elf_hash_table (info
)->dynobj
;
3585 local_got_offsets
= elf_local_got_offsets (abfd
);
3587 rel_end
= relocs
+ sec
->reloc_count
;
3588 for (rel
= relocs
; rel
< rel_end
; rel
++)
3590 struct elf_link_hash_entry
*h
;
3591 unsigned long r_symndx
;
3593 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3594 if (r_symndx
< symtab_hdr
->sh_info
)
3597 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3599 /* Some relocs require a global offset table. */
3602 switch (ELF32_R_TYPE (rel
->r_info
))
3607 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
3608 if (! _bfd_elf_create_got_section (dynobj
, info
))
3617 switch (ELF32_R_TYPE (rel
->r_info
))
3619 /* This relocation describes the C++ object vtable hierarchy.
3620 Reconstruct it for later use during GC. */
3621 case R_SH_GNU_VTINHERIT
:
3622 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
3626 /* This relocation describes which C++ vtable entries are actually
3627 used. Record for later use during GC. */
3628 case R_SH_GNU_VTENTRY
:
3629 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
3634 /* This symbol requires a global offset table entry. */
3638 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3639 BFD_ASSERT (sgot
!= NULL
);
3643 && (h
!= NULL
|| info
->shared
))
3645 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
3646 if (srelgot
== NULL
)
3648 srelgot
= bfd_make_section (dynobj
, ".rela.got");
3650 || ! bfd_set_section_flags (dynobj
, srelgot
,
3655 | SEC_LINKER_CREATED
3657 || ! bfd_set_section_alignment (dynobj
, srelgot
, 2))
3664 if (h
->got
.offset
!= (bfd_vma
) -1)
3666 /* We have already allocated space in the .got. */
3669 h
->got
.offset
= sgot
->_raw_size
;
3671 /* Make sure this symbol is output as a dynamic symbol. */
3672 if (h
->dynindx
== -1)
3674 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
3678 srelgot
->_raw_size
+= sizeof (Elf32_External_Rela
);
3682 /* This is a global offset table entry for a local
3684 if (local_got_offsets
== NULL
)
3687 register unsigned int i
;
3689 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
3690 local_got_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
3691 if (local_got_offsets
== NULL
)
3693 elf_local_got_offsets (abfd
) = local_got_offsets
;
3694 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3695 local_got_offsets
[i
] = (bfd_vma
) -1;
3697 if (local_got_offsets
[r_symndx
] != (bfd_vma
) -1)
3699 /* We have already allocated space in the .got. */
3702 local_got_offsets
[r_symndx
] = sgot
->_raw_size
;
3706 /* If we are generating a shared object, we need to
3707 output a R_SH_RELATIVE reloc so that the dynamic
3708 linker can adjust this GOT entry. */
3709 srelgot
->_raw_size
+= sizeof (Elf32_External_Rela
);
3713 sgot
->_raw_size
+= 4;
3718 /* This symbol requires a procedure linkage table entry. We
3719 actually build the entry in adjust_dynamic_symbol,
3720 because this might be a case of linking PIC code which is
3721 never referenced by a dynamic object, in which case we
3722 don't need to generate a procedure linkage table entry
3725 /* If this is a local symbol, we resolve it directly without
3726 creating a procedure linkage table entry. */
3730 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3731 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3734 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
3741 h
->elf_link_hash_flags
|= ELF_LINK_NON_GOT_REF
;
3743 /* If we are creating a shared library, and this is a reloc
3744 against a global symbol, or a non PC relative reloc
3745 against a local symbol, then we need to copy the reloc
3746 into the shared library. However, if we are linking with
3747 -Bsymbolic, we do not need to copy a reloc against a
3748 global symbol which is defined in an object we are
3749 including in the link (i.e., DEF_REGULAR is set). At
3750 this point we have not seen all the input files, so it is
3751 possible that DEF_REGULAR is not set now but will be set
3752 later (it is never cleared). We account for that
3753 possibility below by storing information in the
3754 pcrel_relocs_copied field of the hash table entry. */
3756 && (sec
->flags
& SEC_ALLOC
) != 0
3757 && (ELF32_R_TYPE (rel
->r_info
) != R_SH_REL32
3759 && (! info
->symbolic
3760 || (h
->elf_link_hash_flags
3761 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
3763 /* When creating a shared object, we must copy these
3764 reloc types into the output file. We create a reloc
3765 section in dynobj and make room for this reloc. */
3770 name
= (bfd_elf_string_from_elf_section
3772 elf_elfheader (abfd
)->e_shstrndx
,
3773 elf_section_data (sec
)->rel_hdr
.sh_name
));
3777 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
3778 && strcmp (bfd_get_section_name (abfd
, sec
),
3781 sreloc
= bfd_get_section_by_name (dynobj
, name
);
3786 sreloc
= bfd_make_section (dynobj
, name
);
3787 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
3788 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
3789 if ((sec
->flags
& SEC_ALLOC
) != 0)
3790 flags
|= SEC_ALLOC
| SEC_LOAD
;
3792 || ! bfd_set_section_flags (dynobj
, sreloc
, flags
)
3793 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
3798 sreloc
->_raw_size
+= sizeof (Elf32_External_Rela
);
3800 /* If we are linking with -Bsymbolic, and this is a
3801 global symbol, we count the number of PC relative
3802 relocations we have entered for this symbol, so that
3803 we can discard them again if the symbol is later
3804 defined by a regular object. Note that this function
3805 is only called if we are using an elf_sh linker
3806 hash table, which means that h is really a pointer to
3807 an elf_sh_link_hash_entry. */
3808 if (h
!= NULL
&& info
->symbolic
3809 && ELF32_R_TYPE (rel
->r_info
) == R_SH_REL32
)
3811 struct elf_sh_link_hash_entry
*eh
;
3812 struct elf_sh_pcrel_relocs_copied
*p
;
3814 eh
= (struct elf_sh_link_hash_entry
*) h
;
3816 for (p
= eh
->pcrel_relocs_copied
; p
!= NULL
; p
= p
->next
)
3817 if (p
->section
== sreloc
)
3822 p
= ((struct elf_sh_pcrel_relocs_copied
*)
3823 bfd_alloc (dynobj
, sizeof *p
));
3826 p
->next
= eh
->pcrel_relocs_copied
;
3827 eh
->pcrel_relocs_copied
= p
;
3828 p
->section
= sreloc
;
3844 sh_elf_set_mach_from_flags (abfd
)
3847 flagword flags
= elf_elfheader (abfd
)->e_flags
;
3849 switch (flags
& EF_SH_MACH_MASK
)
3852 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh
);
3855 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh2
);
3858 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh_dsp
);
3861 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh3
);
3864 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh3_dsp
);
3867 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh3e
);
3871 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, bfd_mach_sh4
);
3879 /* Function to keep SH specific file flags. */
3882 sh_elf_set_private_flags (abfd
, flags
)
3886 BFD_ASSERT (! elf_flags_init (abfd
)
3887 || elf_elfheader (abfd
)->e_flags
== flags
);
3889 elf_elfheader (abfd
)->e_flags
= flags
;
3890 elf_flags_init (abfd
) = true;
3891 return sh_elf_set_mach_from_flags (abfd
);
3894 /* Copy backend specific data from one object module to another */
3897 sh_elf_copy_private_data (ibfd
, obfd
)
3901 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3902 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3905 return sh_elf_set_private_flags (obfd
, elf_elfheader (ibfd
)->e_flags
);
3908 /* This routine checks for linking big and little endian objects
3909 together, and for linking sh-dsp with sh3e / sh4 objects. */
3912 sh_elf_merge_private_data (ibfd
, obfd
)
3916 flagword old_flags
, new_flags
;
3918 if (_bfd_generic_verify_endian_match (ibfd
, obfd
) == false)
3921 if ( bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3922 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3925 if (! elf_flags_init (obfd
))
3927 /* This happens when ld starts out with a 'blank' output file. */
3928 elf_flags_init (obfd
) = true;
3929 elf_elfheader (obfd
)->e_flags
= EF_SH1
;
3931 old_flags
= elf_elfheader (obfd
)->e_flags
;
3932 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3933 if ((EF_SH_HAS_DSP (old_flags
) && EF_SH_HAS_FP (new_flags
))
3934 || (EF_SH_HAS_DSP (new_flags
) && EF_SH_HAS_FP (old_flags
)))
3936 (*_bfd_error_handler
)
3937 ("%s: uses %s instructions while previous modules use %s instructions",
3938 bfd_get_filename (ibfd
),
3939 EF_SH_HAS_DSP (new_flags
) ? "dsp" : "floating point",
3940 EF_SH_HAS_DSP (new_flags
) ? "floating point" : "dsp");
3941 bfd_set_error (bfd_error_bad_value
);
3944 elf_elfheader (obfd
)->e_flags
= EF_SH_MERGE_MACH (old_flags
, new_flags
);
3946 return sh_elf_set_mach_from_flags (obfd
);
3949 /* Finish up dynamic symbol handling. We set the contents of various
3950 dynamic sections here. */
3953 sh_elf_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
3955 struct bfd_link_info
*info
;
3956 struct elf_link_hash_entry
*h
;
3957 Elf_Internal_Sym
*sym
;
3961 dynobj
= elf_hash_table (info
)->dynobj
;
3963 if (h
->plt
.offset
!= (bfd_vma
) -1)
3971 Elf_Internal_Rela rel
;
3973 /* This symbol has an entry in the procedure linkage table. Set
3976 BFD_ASSERT (h
->dynindx
!= -1);
3978 splt
= bfd_get_section_by_name (dynobj
, ".plt");
3979 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
3980 srel
= bfd_get_section_by_name (dynobj
, ".rela.plt");
3981 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
3983 /* Get the index in the procedure linkage table which
3984 corresponds to this symbol. This is the index of this symbol
3985 in all the symbols for which we are making plt entries. The
3986 first entry in the procedure linkage table is reserved. */
3987 plt_index
= h
->plt
.offset
/ elf_sh_sizeof_plt (info
) - 1;
3989 /* Get the offset into the .got table of the entry that
3990 corresponds to this function. Each .got entry is 4 bytes.
3991 The first three are reserved. */
3992 got_offset
= (plt_index
+ 3) * 4;
3994 /* Fill in the entry in the procedure linkage table. */
3997 if (elf_sh_plt_entry
== NULL
)
3999 elf_sh_plt_entry
= (bfd_big_endian (output_bfd
) ?
4000 elf_sh_plt_entry_be
: elf_sh_plt_entry_le
);
4002 memcpy (splt
->contents
+ h
->plt
.offset
, elf_sh_plt_entry
,
4003 elf_sh_sizeof_plt (info
));
4004 bfd_put_32 (output_bfd
,
4005 (sgot
->output_section
->vma
4006 + sgot
->output_offset
4008 (splt
->contents
+ h
->plt
.offset
4009 + elf_sh_plt_symbol_offset (info
)));
4011 bfd_put_32 (output_bfd
,
4012 (splt
->output_section
->vma
+ splt
->output_offset
),
4013 (splt
->contents
+ h
->plt
.offset
4014 + elf_sh_plt_plt0_offset (info
)));
4018 if (elf_sh_pic_plt_entry
== NULL
)
4020 elf_sh_pic_plt_entry
= (bfd_big_endian (output_bfd
) ?
4021 elf_sh_pic_plt_entry_be
:
4022 elf_sh_pic_plt_entry_le
);
4024 memcpy (splt
->contents
+ h
->plt
.offset
, elf_sh_pic_plt_entry
,
4025 elf_sh_sizeof_plt (info
));
4026 bfd_put_32 (output_bfd
, got_offset
,
4027 (splt
->contents
+ h
->plt
.offset
4028 + elf_sh_plt_symbol_offset (info
)));
4031 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rela
),
4032 (splt
->contents
+ h
->plt
.offset
4033 + elf_sh_plt_reloc_offset (info
)));
4035 /* Fill in the entry in the global offset table. */
4036 bfd_put_32 (output_bfd
,
4037 (splt
->output_section
->vma
4038 + splt
->output_offset
4040 + elf_sh_plt_temp_offset (info
)),
4041 sgot
->contents
+ got_offset
);
4043 /* Fill in the entry in the .rela.plt section. */
4044 rel
.r_offset
= (sgot
->output_section
->vma
4045 + sgot
->output_offset
4047 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_JMP_SLOT
);
4049 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4050 ((Elf32_External_Rela
*) srel
->contents
4053 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4055 /* Mark the symbol as undefined, rather than as defined in
4056 the .plt section. Leave the value alone. */
4057 sym
->st_shndx
= SHN_UNDEF
;
4061 if (h
->got
.offset
!= (bfd_vma
) -1)
4065 Elf_Internal_Rela rel
;
4067 /* This symbol has an entry in the global offset table. Set it
4070 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4071 srel
= bfd_get_section_by_name (dynobj
, ".rela.got");
4072 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
4074 rel
.r_offset
= (sgot
->output_section
->vma
4075 + sgot
->output_offset
4076 + (h
->got
.offset
&~ 1));
4078 /* If this is a -Bsymbolic link, and the symbol is defined
4079 locally, we just want to emit a RELATIVE reloc. Likewise if
4080 the symbol was forced to be local because of a version file.
4081 The entry in the global offset table will already have been
4082 initialized in the relocate_section function. */
4084 && (info
->symbolic
|| h
->dynindx
== -1)
4085 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
))
4087 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4088 rel
.r_addend
= (h
->root
.u
.def
.value
4089 + h
->root
.u
.def
.section
->output_section
->vma
4090 + h
->root
.u
.def
.section
->output_offset
);
4094 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
4095 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
4099 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4100 ((Elf32_External_Rela
*) srel
->contents
4101 + srel
->reloc_count
));
4102 ++srel
->reloc_count
;
4105 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
4108 Elf_Internal_Rela rel
;
4110 /* This symbol needs a copy reloc. Set it up. */
4112 BFD_ASSERT (h
->dynindx
!= -1
4113 && (h
->root
.type
== bfd_link_hash_defined
4114 || h
->root
.type
== bfd_link_hash_defweak
));
4116 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
4118 BFD_ASSERT (s
!= NULL
);
4120 rel
.r_offset
= (h
->root
.u
.def
.value
4121 + h
->root
.u
.def
.section
->output_section
->vma
4122 + h
->root
.u
.def
.section
->output_offset
);
4123 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_COPY
);
4125 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4126 ((Elf32_External_Rela
*) s
->contents
4131 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4132 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
4133 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
4134 sym
->st_shndx
= SHN_ABS
;
4139 /* Finish up the dynamic sections. */
4142 sh_elf_finish_dynamic_sections (output_bfd
, info
)
4144 struct bfd_link_info
*info
;
4150 dynobj
= elf_hash_table (info
)->dynobj
;
4152 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
4153 BFD_ASSERT (sgot
!= NULL
);
4154 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
4156 if (elf_hash_table (info
)->dynamic_sections_created
)
4159 Elf32_External_Dyn
*dyncon
, *dynconend
;
4161 BFD_ASSERT (sdyn
!= NULL
);
4163 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
4164 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
4165 for (; dyncon
< dynconend
; dyncon
++)
4167 Elf_Internal_Dyn dyn
;
4171 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4185 s
= bfd_get_section_by_name (output_bfd
, name
);
4186 BFD_ASSERT (s
!= NULL
);
4187 dyn
.d_un
.d_ptr
= s
->vma
;
4188 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4192 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4193 BFD_ASSERT (s
!= NULL
);
4194 if (s
->_cooked_size
!= 0)
4195 dyn
.d_un
.d_val
= s
->_cooked_size
;
4197 dyn
.d_un
.d_val
= s
->_raw_size
;
4198 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4202 /* My reading of the SVR4 ABI indicates that the
4203 procedure linkage table relocs (DT_JMPREL) should be
4204 included in the overall relocs (DT_RELA). This is
4205 what Solaris does. However, UnixWare can not handle
4206 that case. Therefore, we override the DT_RELASZ entry
4207 here to make it not include the JMPREL relocs. Since
4208 the linker script arranges for .rela.plt to follow all
4209 other relocation sections, we don't have to worry
4210 about changing the DT_RELA entry. */
4211 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4214 if (s
->_cooked_size
!= 0)
4215 dyn
.d_un
.d_val
-= s
->_cooked_size
;
4217 dyn
.d_un
.d_val
-= s
->_raw_size
;
4219 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4224 /* Fill in the first entry in the procedure linkage table. */
4225 splt
= bfd_get_section_by_name (dynobj
, ".plt");
4226 if (splt
&& splt
->_raw_size
> 0)
4230 if (elf_sh_pic_plt_entry
== NULL
)
4232 elf_sh_pic_plt_entry
= (bfd_big_endian (output_bfd
) ?
4233 elf_sh_pic_plt_entry_be
:
4234 elf_sh_pic_plt_entry_le
);
4236 memcpy (splt
->contents
, elf_sh_pic_plt_entry
,
4237 elf_sh_sizeof_plt (info
));
4241 if (elf_sh_plt0_entry
== NULL
)
4243 elf_sh_plt0_entry
= (bfd_big_endian (output_bfd
) ?
4244 elf_sh_plt0_entry_be
:
4245 elf_sh_plt0_entry_le
);
4247 memcpy (splt
->contents
, elf_sh_plt0_entry
, PLT_ENTRY_SIZE
);
4248 bfd_put_32 (output_bfd
,
4249 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
4250 splt
->contents
+ elf_sh_plt0_gotid_offset (info
));
4251 bfd_put_32 (output_bfd
,
4252 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
4253 splt
->contents
+ elf_sh_plt0_linker_offset (info
));
4256 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4257 really seem like the right value. */
4258 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
4262 /* Fill in the first three entries in the global offset table. */
4263 if (sgot
->_raw_size
> 0)
4266 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
4268 bfd_put_32 (output_bfd
,
4269 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4271 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
4272 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
4275 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
4281 #define TARGET_BIG_SYM bfd_elf32_sh_vec
4282 #define TARGET_BIG_NAME "elf32-sh"
4283 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
4284 #define TARGET_LITTLE_NAME "elf32-shl"
4285 #define ELF_ARCH bfd_arch_sh
4286 #define ELF_MACHINE_CODE EM_SH
4287 #define ELF_MAXPAGESIZE 128
4289 #define elf_symbol_leading_char '_'
4290 #endif /* ELF_ARCH */
4292 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
4293 #define elf_info_to_howto sh_elf_info_to_howto
4294 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
4295 #define elf_backend_relocate_section sh_elf_relocate_section
4296 #define bfd_elf32_bfd_get_relocated_section_contents \
4297 sh_elf_get_relocated_section_contents
4298 #define elf_backend_object_p sh_elf_set_mach_from_flags
4299 #define bfd_elf32_bfd_set_private_bfd_flags \
4300 sh_elf_set_private_flags
4301 #define bfd_elf32_bfd_copy_private_bfd_data \
4302 sh_elf_copy_private_data
4303 #define bfd_elf32_bfd_merge_private_bfd_data \
4304 sh_elf_merge_private_data
4306 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
4307 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
4308 #define elf_backend_check_relocs sh_elf_check_relocs
4310 #define elf_backend_can_gc_sections 1
4311 #define elf_backend_create_dynamic_sections \
4312 sh_elf_create_dynamic_sections
4313 #define bfd_elf32_bfd_link_hash_table_create \
4314 sh_elf_link_hash_table_create
4315 #define elf_backend_adjust_dynamic_symbol \
4316 sh_elf_adjust_dynamic_symbol
4317 #define elf_backend_size_dynamic_sections \
4318 sh_elf_size_dynamic_sections
4319 #define elf_backend_finish_dynamic_symbol \
4320 sh_elf_finish_dynamic_symbol
4321 #define elf_backend_finish_dynamic_sections \
4322 sh_elf_finish_dynamic_sections
4324 #define elf_backend_want_got_plt 1
4325 #define elf_backend_plt_readonly 1
4326 #define elf_backend_want_plt_sym 0
4327 #define elf_backend_got_header_size 12
4328 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
4329 #include "elf32-target.h"