]>
Commit | Line | Data |
---|---|---|
32090b8e | 1 | /* SPARC-specific support for 32-bit ELF |
904ae083 | 2 | Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. |
32090b8e KR |
3 | |
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
943fbd5b | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
32090b8e KR |
19 | |
20 | #include "bfd.h" | |
21 | #include "sysdep.h" | |
013dec1a | 22 | #include "bfdlink.h" |
32090b8e | 23 | #include "libbfd.h" |
3b3f7625 | 24 | #include "elf-bfd.h" |
5bc513b4 | 25 | #include "elf/sparc.h" |
32090b8e | 26 | |
19bfbcbe | 27 | static reloc_howto_type *elf32_sparc_reloc_type_lookup |
013dec1a | 28 | PARAMS ((bfd *, bfd_reloc_code_real_type)); |
ff12f303 | 29 | static void elf32_sparc_info_to_howto |
013dec1a | 30 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); |
4fbc96ad KR |
31 | static boolean elf32_sparc_check_relocs |
32 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
33 | const Elf_Internal_Rela *)); | |
013dec1a ILT |
34 | static boolean elf32_sparc_adjust_dynamic_symbol |
35 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
14cac507 ILT |
36 | static boolean elf32_sparc_adjust_dynindx |
37 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
013dec1a ILT |
38 | static boolean elf32_sparc_size_dynamic_sections |
39 | PARAMS ((bfd *, struct bfd_link_info *)); | |
40 | static boolean elf32_sparc_relocate_section | |
41 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
12662be4 | 42 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); |
013dec1a ILT |
43 | static boolean elf32_sparc_finish_dynamic_symbol |
44 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, | |
45 | Elf_Internal_Sym *)); | |
46 | static boolean elf32_sparc_finish_dynamic_sections | |
47 | PARAMS ((bfd *, struct bfd_link_info *)); | |
5bc513b4 DE |
48 | static boolean elf32_sparc_merge_private_bfd_data PARAMS ((bfd *, bfd *)); |
49 | static boolean elf32_sparc_object_p | |
50 | PARAMS ((bfd *)); | |
51 | static void elf32_sparc_final_write_processing | |
52 | PARAMS ((bfd *, boolean)); | |
19bfbcbe ILT |
53 | \f |
54 | /* The howto table and associated functions. | |
55 | ??? elf64-sparc.c has its own copy for the moment to ease transition | |
56 | since some of the relocation values have changed. At some point we'll | |
57 | want elf64-sparc.c to switch over and use this table. | |
58 | ??? Do we want to recognize (or flag as errors) some of the 64 bit entries | |
59 | if the target is elf32-sparc. | |
60 | */ | |
61 | ||
62 | static bfd_reloc_status_type sparc_elf_notsupported_reloc | |
63 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
64 | static bfd_reloc_status_type sparc_elf_wdisp16_reloc | |
65 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
66 | ||
ff12f303 | 67 | reloc_howto_type _bfd_sparc_elf_howto_table[] = |
32090b8e | 68 | { |
19bfbcbe ILT |
69 | HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true), |
70 | HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", false,0,0x000000ff,true), | |
71 | HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", false,0,0x0000ffff,true), | |
72 | HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", false,0,0xffffffff,true), | |
73 | HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", false,0,0x000000ff,true), | |
74 | HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", false,0,0x0000ffff,true), | |
75 | HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", false,0,0x00ffffff,true), | |
76 | HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", false,0,0x3fffffff,true), | |
77 | HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", false,0,0x003fffff,true), | |
78 | HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", false,0,0x003fffff,true), | |
79 | HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", false,0,0x003fffff,true), | |
80 | HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", false,0,0x00001fff,true), | |
81 | HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", false,0,0x000003ff,true), | |
82 | HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", false,0,0x000003ff,true), | |
83 | HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", false,0,0x00001fff,true), | |
84 | HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", false,0,0x003fffff,true), | |
85 | HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", false,0,0x000003ff,true), | |
86 | HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", false,0,0x003fffff,true), | |
87 | HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", false,0,0x3fffffff,true), | |
88 | HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", false,0,0x00000000,true), | |
89 | HOWTO(R_SPARC_GLOB_DAT, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GLOB_DAT",false,0,0x00000000,true), | |
90 | HOWTO(R_SPARC_JMP_SLOT, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_JMP_SLOT",false,0,0x00000000,true), | |
91 | HOWTO(R_SPARC_RELATIVE, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",false,0,0x00000000,true), | |
92 | HOWTO(R_SPARC_UA32, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_UA32", false,0,0x00000000,true), | |
93 | HOWTO(R_SPARC_PLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PLT32", false,0,0x00000000,true), | |
94 | HOWTO(R_SPARC_HIPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HIPLT22", false,0,0x00000000,true), | |
95 | HOWTO(R_SPARC_LOPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LOPLT10", false,0,0x00000000,true), | |
96 | HOWTO(R_SPARC_PCPLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT32", false,0,0x00000000,true), | |
97 | HOWTO(R_SPARC_PCPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT22", false,0,0x00000000,true), | |
98 | HOWTO(R_SPARC_PCPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT10", false,0,0x00000000,true), | |
99 | HOWTO(R_SPARC_10, 0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", false,0,0x000003ff,true), | |
100 | HOWTO(R_SPARC_11, 0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", false,0,0x000007ff,true), | |
101 | /* ??? If we need to handle R_SPARC_64 then we need (figuratively) | |
102 | --enable-64-bit-bfd. That causes objdump to print address as 64 bits | |
103 | which we really don't want on an elf32-sparc system. There may be other | |
104 | consequences which we may not want (at least not until it's proven they're | |
105 | necessary) so for now these are only enabled ifdef BFD64. */ | |
106 | #ifdef BFD64 | |
107 | HOWTO(R_SPARC_64, 0,4,00,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_64", false,0,~ (bfd_vma) 0, true), | |
108 | /* ??? These don't make sense except in 64 bit systems so they're disabled | |
109 | ifndef BFD64 too (for now). */ | |
110 | HOWTO(R_SPARC_OLO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_OLO10", false,0,0x000003ff,true), | |
111 | HOWTO(R_SPARC_HH22, 42,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HH22", false,0,0x003fffff,true), | |
112 | HOWTO(R_SPARC_HM10, 32,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HM10", false,0,0x000003ff,true), | |
113 | HOWTO(R_SPARC_LM22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LM22", false,0,0x003fffff,true), | |
114 | HOWTO(R_SPARC_PC_HH22, 42,2,22,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HH22", false,0,0x003fffff,true), | |
115 | HOWTO(R_SPARC_PC_HM10, 32,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HM10", false,0,0x000003ff,true), | |
116 | HOWTO(R_SPARC_PC_LM22, 10,2,22,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LM22", false,0,0x003fffff,true), | |
117 | #else | |
118 | HOWTO(R_SPARC_64, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_64", false,0,0x00000000,true), | |
119 | HOWTO(R_SPARC_OLO10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_OLO10", false,0,0x00000000,true), | |
120 | HOWTO(R_SPARC_HH22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HH22", false,0,0x00000000,true), | |
121 | HOWTO(R_SPARC_HM10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HM10", false,0,0x00000000,true), | |
122 | HOWTO(R_SPARC_LM22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LM22", false,0,0x00000000,true), | |
123 | HOWTO(R_SPARC_PC_HH22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HH22", false,0,0x00000000,true), | |
124 | HOWTO(R_SPARC_PC_HM10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HM10", false,0,0x00000000,true), | |
125 | HOWTO(R_SPARC_PC_LM22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_LM22", false,0,0x00000000,true), | |
32090b8e | 126 | #endif |
19bfbcbe ILT |
127 | HOWTO(R_SPARC_WDISP16, 2,2,16,true, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true), |
128 | HOWTO(R_SPARC_WDISP19, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", false,0,0x0007ffff,true), | |
ff12f303 | 129 | HOWTO(R_SPARC_GLOB_JMP, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GLOB_JMP",false,0,0x00000000,true), |
19bfbcbe ILT |
130 | HOWTO(R_SPARC_7, 0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", false,0,0x0000007f,true), |
131 | HOWTO(R_SPARC_5, 0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", false,0,0x0000001f,true), | |
132 | HOWTO(R_SPARC_6, 0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", false,0,0x0000003f,true), | |
32090b8e KR |
133 | }; |
134 | ||
135 | struct elf_reloc_map { | |
136 | unsigned char bfd_reloc_val; | |
137 | unsigned char elf_reloc_val; | |
138 | }; | |
139 | ||
140 | static CONST struct elf_reloc_map sparc_reloc_map[] = | |
141 | { | |
142 | { BFD_RELOC_NONE, R_SPARC_NONE, }, | |
143 | { BFD_RELOC_16, R_SPARC_16, }, | |
144 | { BFD_RELOC_8, R_SPARC_8 }, | |
145 | { BFD_RELOC_8_PCREL, R_SPARC_DISP8 }, | |
19bfbcbe ILT |
146 | /* ??? This might cause us to need separate functions in elf{32,64}-sparc.c |
147 | (we could still have just one table), but is this reloc ever used? */ | |
32090b8e KR |
148 | { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits. */ |
149 | { BFD_RELOC_32, R_SPARC_32 }, | |
150 | { BFD_RELOC_32_PCREL, R_SPARC_DISP32 }, | |
151 | { BFD_RELOC_HI22, R_SPARC_HI22 }, | |
152 | { BFD_RELOC_LO10, R_SPARC_LO10, }, | |
153 | { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 }, | |
154 | { BFD_RELOC_SPARC22, R_SPARC_22 }, | |
155 | { BFD_RELOC_SPARC13, R_SPARC_13 }, | |
156 | { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 }, | |
157 | { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 }, | |
158 | { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 }, | |
159 | { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 }, | |
160 | { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 }, | |
161 | { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 }, | |
162 | { BFD_RELOC_SPARC_COPY, R_SPARC_COPY }, | |
163 | { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT }, | |
164 | { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT }, | |
165 | { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE }, | |
166 | { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 }, | |
19bfbcbe | 167 | /* ??? Doesn't dwarf use this? */ |
4fbc96ad | 168 | /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */ |
19bfbcbe ILT |
169 | {BFD_RELOC_SPARC_10, R_SPARC_10}, |
170 | {BFD_RELOC_SPARC_11, R_SPARC_11}, | |
171 | {BFD_RELOC_SPARC_64, R_SPARC_64}, | |
172 | {BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10}, | |
173 | {BFD_RELOC_SPARC_HH22, R_SPARC_HH22}, | |
174 | {BFD_RELOC_SPARC_HM10, R_SPARC_HM10}, | |
175 | {BFD_RELOC_SPARC_LM22, R_SPARC_LM22}, | |
176 | {BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22}, | |
177 | {BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10}, | |
178 | {BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22}, | |
179 | {BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16}, | |
180 | {BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19}, | |
181 | {BFD_RELOC_SPARC_GLOB_JMP, R_SPARC_GLOB_JMP}, | |
182 | {BFD_RELOC_SPARC_7, R_SPARC_7}, | |
183 | {BFD_RELOC_SPARC_5, R_SPARC_5}, | |
184 | {BFD_RELOC_SPARC_6, R_SPARC_6}, | |
32090b8e KR |
185 | }; |
186 | ||
f786a73f | 187 | static reloc_howto_type * |
19bfbcbe | 188 | elf32_sparc_reloc_type_lookup (abfd, code) |
013dec1a ILT |
189 | bfd *abfd; |
190 | bfd_reloc_code_real_type code; | |
32090b8e | 191 | { |
3b3f7625 | 192 | unsigned int i; |
32090b8e KR |
193 | for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++) |
194 | { | |
195 | if (sparc_reloc_map[i].bfd_reloc_val == code) | |
19bfbcbe | 196 | return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val]; |
32090b8e KR |
197 | } |
198 | return 0; | |
199 | } | |
200 | ||
19bfbcbe ILT |
201 | /* We need to use ELF32_R_TYPE so we have our own copy of this function, |
202 | and elf64-sparc.c has its own copy. */ | |
203 | ||
32090b8e | 204 | static void |
ff12f303 | 205 | elf32_sparc_info_to_howto (abfd, cache_ptr, dst) |
013dec1a ILT |
206 | bfd *abfd; |
207 | arelent *cache_ptr; | |
208 | Elf_Internal_Rela *dst; | |
32090b8e KR |
209 | { |
210 | BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max); | |
19bfbcbe | 211 | cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)]; |
32090b8e | 212 | } |
19bfbcbe ILT |
213 | \f |
214 | /* For unsupported relocs. */ | |
215 | ||
216 | static bfd_reloc_status_type | |
217 | sparc_elf_notsupported_reloc (abfd, | |
218 | reloc_entry, | |
219 | symbol, | |
220 | data, | |
221 | input_section, | |
222 | output_bfd, | |
223 | error_message) | |
224 | bfd *abfd; | |
225 | arelent *reloc_entry; | |
226 | asymbol *symbol; | |
227 | PTR data; | |
228 | asection *input_section; | |
229 | bfd *output_bfd; | |
230 | char **error_message; | |
231 | { | |
232 | return bfd_reloc_notsupported; | |
233 | } | |
234 | ||
235 | /* Handle the WDISP16 reloc. */ | |
236 | ||
237 | static bfd_reloc_status_type | |
238 | sparc_elf_wdisp16_reloc (abfd, | |
239 | reloc_entry, | |
240 | symbol, | |
241 | data, | |
242 | input_section, | |
243 | output_bfd, | |
244 | error_message) | |
245 | bfd *abfd; | |
246 | arelent *reloc_entry; | |
247 | asymbol *symbol; | |
248 | PTR data; | |
249 | asection *input_section; | |
250 | bfd *output_bfd; | |
251 | char **error_message; | |
252 | { | |
253 | bfd_vma relocation; | |
254 | bfd_vma x; | |
32090b8e | 255 | |
19bfbcbe ILT |
256 | if (output_bfd != (bfd *) NULL |
257 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
258 | && (! reloc_entry->howto->partial_inplace | |
259 | || reloc_entry->addend == 0)) | |
260 | { | |
261 | reloc_entry->address += input_section->output_offset; | |
262 | return bfd_reloc_ok; | |
263 | } | |
264 | ||
265 | if (output_bfd != NULL) | |
266 | return bfd_reloc_continue; | |
267 | ||
268 | if (reloc_entry->address > input_section->_cooked_size) | |
269 | return bfd_reloc_outofrange; | |
270 | ||
271 | relocation = (symbol->value | |
272 | + symbol->section->output_section->vma | |
273 | + symbol->section->output_offset); | |
274 | relocation += reloc_entry->addend; | |
275 | relocation -= (input_section->output_section->vma | |
276 | + input_section->output_offset); | |
277 | relocation -= reloc_entry->address; | |
278 | ||
279 | x = bfd_get_32 (abfd, (char *) data + reloc_entry->address); | |
280 | x |= ((((relocation >> 2) & 0xc000) << 6) | |
281 | | ((relocation >> 2) & 0x3fff)); | |
282 | bfd_put_32 (abfd, x, (char *) data + reloc_entry->address); | |
283 | ||
284 | if ((bfd_signed_vma) relocation < - 0x40000 | |
285 | || (bfd_signed_vma) relocation > 0x3ffff) | |
286 | return bfd_reloc_overflow; | |
287 | else | |
288 | return bfd_reloc_ok; | |
289 | } | |
013dec1a ILT |
290 | \f |
291 | /* Functions for the SPARC ELF linker. */ | |
292 | ||
293 | /* The name of the dynamic interpreter. This is put in the .interp | |
294 | section. */ | |
295 | ||
296 | #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1" | |
297 | ||
298 | /* The nop opcode we use. */ | |
299 | ||
300 | #define SPARC_NOP 0x01000000 | |
301 | ||
302 | /* The size in bytes of an entry in the procedure linkage table. */ | |
303 | ||
304 | #define PLT_ENTRY_SIZE 12 | |
305 | ||
306 | /* The first four entries in a procedure linkage table are reserved, | |
307 | and the initial contents are unimportant (we zero them out). | |
308 | Subsequent entries look like this. See the SVR4 ABI SPARC | |
309 | supplement to see how this works. */ | |
310 | ||
311 | /* sethi %hi(.-.plt0),%g1. We fill in the address later. */ | |
312 | #define PLT_ENTRY_WORD0 0x03000000 | |
313 | /* b,a .plt0. We fill in the offset later. */ | |
314 | #define PLT_ENTRY_WORD1 0x30800000 | |
315 | /* nop. */ | |
316 | #define PLT_ENTRY_WORD2 SPARC_NOP | |
317 | ||
4fbc96ad KR |
318 | /* Look through the relocs for a section during the first phase, and |
319 | allocate space in the global offset table or procedure linkage | |
320 | table. */ | |
321 | ||
322 | static boolean | |
323 | elf32_sparc_check_relocs (abfd, info, sec, relocs) | |
324 | bfd *abfd; | |
325 | struct bfd_link_info *info; | |
326 | asection *sec; | |
327 | const Elf_Internal_Rela *relocs; | |
328 | { | |
329 | bfd *dynobj; | |
330 | Elf_Internal_Shdr *symtab_hdr; | |
331 | struct elf_link_hash_entry **sym_hashes; | |
332 | bfd_vma *local_got_offsets; | |
333 | const Elf_Internal_Rela *rel; | |
334 | const Elf_Internal_Rela *rel_end; | |
335 | asection *sgot; | |
336 | asection *srelgot; | |
4fbc96ad KR |
337 | asection *sreloc; |
338 | ||
339 | if (info->relocateable) | |
340 | return true; | |
341 | ||
342 | dynobj = elf_hash_table (info)->dynobj; | |
343 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
344 | sym_hashes = elf_sym_hashes (abfd); | |
345 | local_got_offsets = elf_local_got_offsets (abfd); | |
346 | ||
347 | sgot = NULL; | |
348 | srelgot = NULL; | |
4fbc96ad KR |
349 | sreloc = NULL; |
350 | ||
351 | rel_end = relocs + sec->reloc_count; | |
352 | for (rel = relocs; rel < rel_end; rel++) | |
353 | { | |
3b3f7625 | 354 | unsigned long r_symndx; |
4fbc96ad KR |
355 | struct elf_link_hash_entry *h; |
356 | ||
357 | r_symndx = ELF32_R_SYM (rel->r_info); | |
358 | if (r_symndx < symtab_hdr->sh_info) | |
359 | h = NULL; | |
360 | else | |
361 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
362 | ||
4fbc96ad KR |
363 | switch (ELF32_R_TYPE (rel->r_info)) |
364 | { | |
365 | case R_SPARC_GOT10: | |
366 | case R_SPARC_GOT13: | |
367 | case R_SPARC_GOT22: | |
368 | /* This symbol requires a global offset table entry. */ | |
369 | ||
12662be4 ILT |
370 | if (dynobj == NULL) |
371 | { | |
372 | /* Create the .got section. */ | |
373 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
ede4eed4 | 374 | if (! _bfd_elf_create_got_section (dynobj, info)) |
12662be4 ILT |
375 | return false; |
376 | } | |
377 | ||
4fbc96ad KR |
378 | if (sgot == NULL) |
379 | { | |
380 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
12662be4 ILT |
381 | BFD_ASSERT (sgot != NULL); |
382 | } | |
383 | ||
384 | if (srelgot == NULL | |
385 | && (h != NULL || info->shared)) | |
386 | { | |
4fbc96ad KR |
387 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); |
388 | if (srelgot == NULL) | |
389 | { | |
390 | srelgot = bfd_make_section (dynobj, ".rela.got"); | |
391 | if (srelgot == NULL | |
392 | || ! bfd_set_section_flags (dynobj, srelgot, | |
393 | (SEC_ALLOC | |
394 | | SEC_LOAD | |
395 | | SEC_HAS_CONTENTS | |
396 | | SEC_IN_MEMORY | |
ff12f303 | 397 | | SEC_LINKER_CREATED |
12662be4 ILT |
398 | | SEC_READONLY)) |
399 | || ! bfd_set_section_alignment (dynobj, srelgot, 2)) | |
4fbc96ad KR |
400 | return false; |
401 | } | |
4fbc96ad KR |
402 | } |
403 | ||
404 | if (h != NULL) | |
405 | { | |
406 | if (h->got_offset != (bfd_vma) -1) | |
407 | { | |
408 | /* We have already allocated space in the .got. */ | |
409 | break; | |
410 | } | |
411 | h->got_offset = sgot->_raw_size; | |
12662be4 ILT |
412 | |
413 | /* Make sure this symbol is output as a dynamic symbol. */ | |
414 | if (h->dynindx == -1) | |
415 | { | |
416 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
417 | return false; | |
418 | } | |
419 | ||
420 | srelgot->_raw_size += sizeof (Elf32_External_Rela); | |
4fbc96ad KR |
421 | } |
422 | else | |
423 | { | |
424 | /* This is a global offset table entry for a local | |
425 | symbol. */ | |
426 | if (local_got_offsets == NULL) | |
427 | { | |
428 | size_t size; | |
3b3f7625 | 429 | register unsigned int i; |
4fbc96ad KR |
430 | |
431 | size = symtab_hdr->sh_info * sizeof (bfd_vma); | |
432 | local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size); | |
433 | if (local_got_offsets == NULL) | |
a9713b91 | 434 | return false; |
4fbc96ad KR |
435 | elf_local_got_offsets (abfd) = local_got_offsets; |
436 | for (i = 0; i < symtab_hdr->sh_info; i++) | |
437 | local_got_offsets[i] = (bfd_vma) -1; | |
438 | } | |
439 | if (local_got_offsets[r_symndx] != (bfd_vma) -1) | |
440 | { | |
441 | /* We have already allocated space in the .got. */ | |
442 | break; | |
443 | } | |
444 | local_got_offsets[r_symndx] = sgot->_raw_size; | |
12662be4 ILT |
445 | |
446 | if (info->shared) | |
447 | { | |
448 | /* If we are generating a shared object, we need to | |
449 | output a R_SPARC_RELATIVE reloc so that the | |
450 | dynamic linker can adjust this GOT entry. */ | |
451 | srelgot->_raw_size += sizeof (Elf32_External_Rela); | |
452 | } | |
4fbc96ad KR |
453 | } |
454 | ||
455 | sgot->_raw_size += 4; | |
4fbc96ad | 456 | |
19bfbcbe ILT |
457 | /* If the .got section is more than 0x1000 bytes, we add |
458 | 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13 | |
459 | bit relocations have a greater chance of working. */ | |
460 | if (sgot->_raw_size >= 0x1000 | |
461 | && elf_hash_table (info)->hgot->root.u.def.value == 0) | |
462 | elf_hash_table (info)->hgot->root.u.def.value = 0x1000; | |
463 | ||
4fbc96ad KR |
464 | break; |
465 | ||
466 | case R_SPARC_WPLT30: | |
12662be4 ILT |
467 | /* This symbol requires a procedure linkage table entry. We |
468 | actually build the entry in adjust_dynamic_symbol, | |
469 | because this might be a case of linking PIC code without | |
470 | linking in any dynamic objects, in which case we don't | |
471 | need to generate a procedure linkage table after all. */ | |
4fbc96ad KR |
472 | |
473 | if (h == NULL) | |
474 | { | |
475 | /* It does not make sense to have a procedure linkage | |
476 | table entry for a local symbol. */ | |
477 | bfd_set_error (bfd_error_bad_value); | |
478 | return false; | |
479 | } | |
480 | ||
12662be4 ILT |
481 | /* Make sure this symbol is output as a dynamic symbol. */ |
482 | if (h->dynindx == -1) | |
4fbc96ad | 483 | { |
12662be4 ILT |
484 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) |
485 | return false; | |
4fbc96ad KR |
486 | } |
487 | ||
12662be4 | 488 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; |
4fbc96ad KR |
489 | |
490 | break; | |
491 | ||
492 | case R_SPARC_PC10: | |
493 | case R_SPARC_PC22: | |
494 | if (h != NULL | |
495 | && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
496 | break; | |
497 | /* Fall through. */ | |
4fbc96ad KR |
498 | case R_SPARC_DISP8: |
499 | case R_SPARC_DISP16: | |
500 | case R_SPARC_DISP32: | |
501 | case R_SPARC_WDISP30: | |
502 | case R_SPARC_WDISP22: | |
19bfbcbe ILT |
503 | case R_SPARC_WDISP19: |
504 | case R_SPARC_WDISP16: | |
3b3f7625 ILT |
505 | if (h == NULL) |
506 | break; | |
507 | /* Fall through. */ | |
508 | case R_SPARC_8: | |
509 | case R_SPARC_16: | |
510 | case R_SPARC_32: | |
4fbc96ad KR |
511 | case R_SPARC_HI22: |
512 | case R_SPARC_22: | |
513 | case R_SPARC_13: | |
514 | case R_SPARC_LO10: | |
515 | case R_SPARC_UA32: | |
ff12f303 | 516 | if (info->shared) |
4fbc96ad KR |
517 | { |
518 | /* When creating a shared object, we must copy these | |
519 | relocs into the output file. We create a reloc | |
520 | section in dynobj and make room for the reloc. */ | |
521 | if (sreloc == NULL) | |
522 | { | |
523 | const char *name; | |
524 | ||
ede4eed4 | 525 | name = (bfd_elf_string_from_elf_section |
4fbc96ad KR |
526 | (abfd, |
527 | elf_elfheader (abfd)->e_shstrndx, | |
528 | elf_section_data (sec)->rel_hdr.sh_name)); | |
529 | if (name == NULL) | |
530 | return false; | |
531 | ||
532 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
533 | && strcmp (bfd_get_section_name (abfd, sec), | |
534 | name + 5) == 0); | |
535 | ||
536 | sreloc = bfd_get_section_by_name (dynobj, name); | |
537 | if (sreloc == NULL) | |
538 | { | |
ff12f303 ILT |
539 | flagword flags; |
540 | ||
4fbc96ad | 541 | sreloc = bfd_make_section (dynobj, name); |
ff12f303 ILT |
542 | flags = (SEC_HAS_CONTENTS | SEC_READONLY |
543 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
544 | if ((sec->flags & SEC_ALLOC) != 0) | |
545 | flags |= SEC_ALLOC | SEC_LOAD; | |
4fbc96ad | 546 | if (sreloc == NULL |
ff12f303 | 547 | || ! bfd_set_section_flags (dynobj, sreloc, flags) |
4fbc96ad KR |
548 | || ! bfd_set_section_alignment (dynobj, sreloc, 2)) |
549 | return false; | |
550 | } | |
551 | } | |
552 | ||
553 | sreloc->_raw_size += sizeof (Elf32_External_Rela); | |
554 | } | |
555 | ||
556 | break; | |
557 | ||
558 | default: | |
559 | break; | |
560 | } | |
561 | } | |
013dec1a ILT |
562 | |
563 | return true; | |
564 | } | |
565 | ||
566 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
567 | regular object. The current definition is in some section of the | |
568 | dynamic object, but we're not including those sections. We have to | |
569 | change the definition to something the rest of the link can | |
570 | understand. */ | |
571 | ||
572 | static boolean | |
573 | elf32_sparc_adjust_dynamic_symbol (info, h) | |
574 | struct bfd_link_info *info; | |
575 | struct elf_link_hash_entry *h; | |
576 | { | |
577 | bfd *dynobj; | |
578 | asection *s; | |
579 | unsigned int power_of_two; | |
013dec1a ILT |
580 | |
581 | dynobj = elf_hash_table (info)->dynobj; | |
582 | ||
583 | /* Make sure we know what is going on here. */ | |
3004a68c ILT |
584 | BFD_ASSERT (dynobj != NULL |
585 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) | |
3b3f7625 | 586 | || h->weakdef != NULL |
3004a68c ILT |
587 | || ((h->elf_link_hash_flags |
588 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
589 | && (h->elf_link_hash_flags | |
590 | & ELF_LINK_HASH_REF_REGULAR) != 0 | |
591 | && (h->elf_link_hash_flags | |
592 | & ELF_LINK_HASH_DEF_REGULAR) == 0))); | |
013dec1a ILT |
593 | |
594 | /* If this is a function, put it in the procedure linkage table. We | |
595 | will fill in the contents of the procedure linkage table later | |
596 | (although we could actually do it here). */ | |
12662be4 ILT |
597 | if (h->type == STT_FUNC |
598 | || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) | |
013dec1a | 599 | { |
12662be4 | 600 | if (! elf_hash_table (info)->dynamic_sections_created) |
013dec1a | 601 | { |
12662be4 ILT |
602 | /* This case can occur if we saw a WPLT30 reloc in an input |
603 | file, but none of the input files were dynamic objects. | |
604 | In such a case, we don't actually need to build a | |
605 | procedure linkage table, and we can just do a WDISP30 | |
606 | reloc instead. */ | |
607 | BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0); | |
608 | return true; | |
609 | } | |
4fbc96ad | 610 | |
12662be4 ILT |
611 | s = bfd_get_section_by_name (dynobj, ".plt"); |
612 | BFD_ASSERT (s != NULL); | |
013dec1a | 613 | |
12662be4 ILT |
614 | /* The first four entries in .plt are reserved. */ |
615 | if (s->_raw_size == 0) | |
616 | s->_raw_size = 4 * PLT_ENTRY_SIZE; | |
013dec1a | 617 | |
12662be4 ILT |
618 | /* The procedure linkage table has a maximum size. */ |
619 | if (s->_raw_size >= 0x400000) | |
620 | { | |
621 | bfd_set_error (bfd_error_bad_value); | |
622 | return false; | |
623 | } | |
013dec1a | 624 | |
9b09a015 ILT |
625 | /* If this symbol is not defined in a regular file, and we are |
626 | not generating a shared library, then set the symbol to this | |
627 | location in the .plt. This is required to make function | |
628 | pointers compare as equal between the normal executable and | |
629 | the shared library. */ | |
630 | if (! info->shared | |
631 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
14cac507 ILT |
632 | { |
633 | h->root.u.def.section = s; | |
634 | h->root.u.def.value = s->_raw_size; | |
635 | } | |
013dec1a | 636 | |
12662be4 | 637 | h->plt_offset = s->_raw_size; |
4fbc96ad | 638 | |
12662be4 ILT |
639 | /* Make room for this entry. */ |
640 | s->_raw_size += PLT_ENTRY_SIZE; | |
641 | ||
642 | /* We also need to make an entry in the .rela.plt section. */ | |
643 | ||
644 | s = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
645 | BFD_ASSERT (s != NULL); | |
646 | s->_raw_size += sizeof (Elf32_External_Rela); | |
013dec1a ILT |
647 | |
648 | return true; | |
649 | } | |
650 | ||
651 | /* If this is a weak symbol, and there is a real definition, the | |
652 | processor independent code will have arranged for us to see the | |
653 | real definition first, and we can just use the same value. */ | |
654 | if (h->weakdef != NULL) | |
655 | { | |
f786a73f ILT |
656 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined |
657 | || h->weakdef->root.type == bfd_link_hash_defweak); | |
013dec1a ILT |
658 | h->root.u.def.section = h->weakdef->root.u.def.section; |
659 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
013dec1a ILT |
660 | return true; |
661 | } | |
662 | ||
663 | /* This is a reference to a symbol defined by a dynamic object which | |
4fbc96ad KR |
664 | is not a function. */ |
665 | ||
666 | /* If we are creating a shared library, we must presume that the | |
667 | only references to the symbol are via the global offset table. | |
668 | For such cases we need not do anything here; the relocations will | |
669 | be handled correctly by relocate_section. */ | |
670 | if (info->shared) | |
671 | return true; | |
672 | ||
673 | /* We must allocate the symbol in our .dynbss section, which will | |
674 | become part of the .bss section of the executable. There will be | |
675 | an entry for this symbol in the .dynsym section. The dynamic | |
676 | object will contain position independent code, so all references | |
677 | from the dynamic object to this symbol will go through the global | |
678 | offset table. The dynamic linker will use the .dynsym entry to | |
679 | determine the address it must put in the global offset table, so | |
680 | both the dynamic object and the regular object will refer to the | |
681 | same memory location for the variable. */ | |
013dec1a ILT |
682 | |
683 | s = bfd_get_section_by_name (dynobj, ".dynbss"); | |
684 | BFD_ASSERT (s != NULL); | |
685 | ||
686 | /* If the symbol is currently defined in the .bss section of the | |
687 | dynamic object, then it is OK to simply initialize it to zero. | |
688 | If the symbol is in some other section, we must generate a | |
689 | R_SPARC_COPY reloc to tell the dynamic linker to copy the initial | |
690 | value out of the dynamic object and into the runtime process | |
691 | image. We need to remember the offset into the .rel.bss section | |
7c6da9ca | 692 | we are going to use. */ |
4fbc96ad | 693 | if ((h->root.u.def.section->flags & SEC_LOAD) != 0) |
013dec1a ILT |
694 | { |
695 | asection *srel; | |
696 | ||
697 | srel = bfd_get_section_by_name (dynobj, ".rela.bss"); | |
698 | BFD_ASSERT (srel != NULL); | |
013dec1a | 699 | srel->_raw_size += sizeof (Elf32_External_Rela); |
4fbc96ad | 700 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; |
013dec1a ILT |
701 | } |
702 | ||
703 | /* We need to figure out the alignment required for this symbol. I | |
704 | have no idea how ELF linkers handle this. */ | |
7c6da9ca ILT |
705 | power_of_two = bfd_log2 (h->size); |
706 | if (power_of_two > 3) | |
707 | power_of_two = 3; | |
013dec1a ILT |
708 | |
709 | /* Apply the required alignment. */ | |
7c6da9ca ILT |
710 | s->_raw_size = BFD_ALIGN (s->_raw_size, |
711 | (bfd_size_type) (1 << power_of_two)); | |
013dec1a ILT |
712 | if (power_of_two > bfd_get_section_alignment (dynobj, s)) |
713 | { | |
714 | if (! bfd_set_section_alignment (dynobj, s, power_of_two)) | |
715 | return false; | |
716 | } | |
717 | ||
718 | /* Define the symbol as being at this point in the section. */ | |
719 | h->root.u.def.section = s; | |
720 | h->root.u.def.value = s->_raw_size; | |
721 | ||
722 | /* Increment the section size to make room for the symbol. */ | |
723 | s->_raw_size += h->size; | |
724 | ||
725 | return true; | |
726 | } | |
727 | ||
013dec1a ILT |
728 | /* Set the sizes of the dynamic sections. */ |
729 | ||
730 | static boolean | |
731 | elf32_sparc_size_dynamic_sections (output_bfd, info) | |
732 | bfd *output_bfd; | |
733 | struct bfd_link_info *info; | |
734 | { | |
735 | bfd *dynobj; | |
736 | asection *s; | |
4fbc96ad | 737 | boolean reltext; |
f786a73f | 738 | boolean relplt; |
013dec1a ILT |
739 | |
740 | dynobj = elf_hash_table (info)->dynobj; | |
741 | BFD_ASSERT (dynobj != NULL); | |
742 | ||
12662be4 | 743 | if (elf_hash_table (info)->dynamic_sections_created) |
8af74670 | 744 | { |
12662be4 ILT |
745 | /* Set the contents of the .interp section to the interpreter. */ |
746 | if (! info->shared) | |
747 | { | |
748 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
749 | BFD_ASSERT (s != NULL); | |
750 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; | |
751 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
752 | } | |
753 | ||
754 | /* Make space for the trailing nop in .plt. */ | |
755 | s = bfd_get_section_by_name (dynobj, ".plt"); | |
8af74670 | 756 | BFD_ASSERT (s != NULL); |
12662be4 ILT |
757 | if (s->_raw_size > 0) |
758 | s->_raw_size += 4; | |
759 | } | |
760 | else | |
761 | { | |
762 | /* We may have created entries in the .rela.got section. | |
763 | However, if we are not creating the dynamic sections, we will | |
764 | not actually use these entries. Reset the size of .rela.got, | |
765 | which will cause it to get stripped from the output file | |
766 | below. */ | |
767 | s = bfd_get_section_by_name (dynobj, ".rela.got"); | |
768 | if (s != NULL) | |
769 | s->_raw_size = 0; | |
8af74670 | 770 | } |
013dec1a | 771 | |
4fbc96ad KR |
772 | /* The check_relocs and adjust_dynamic_symbol entry points have |
773 | determined the sizes of the various dynamic sections. Allocate | |
774 | memory for them. */ | |
775 | reltext = false; | |
f786a73f | 776 | relplt = false; |
4fbc96ad KR |
777 | for (s = dynobj->sections; s != NULL; s = s->next) |
778 | { | |
779 | const char *name; | |
12662be4 | 780 | boolean strip; |
4fbc96ad | 781 | |
ff12f303 | 782 | if ((s->flags & SEC_LINKER_CREATED) == 0) |
4fbc96ad KR |
783 | continue; |
784 | ||
785 | /* It's OK to base decisions on the section name, because none | |
786 | of the dynobj section names depend upon the input files. */ | |
787 | name = bfd_get_section_name (dynobj, s); | |
788 | ||
12662be4 ILT |
789 | strip = false; |
790 | ||
791 | if (strncmp (name, ".rela", 5) == 0) | |
4fbc96ad | 792 | { |
12662be4 ILT |
793 | if (s->_raw_size == 0) |
794 | { | |
795 | /* If we don't need this section, strip it from the | |
796 | output file. This is to handle .rela.bss and | |
797 | .rel.plt. We must create it in | |
798 | create_dynamic_sections, because it must be created | |
799 | before the linker maps input sections to output | |
800 | sections. The linker does that before | |
801 | adjust_dynamic_symbol is called, and it is that | |
802 | function which decides whether anything needs to go | |
803 | into these sections. */ | |
804 | strip = true; | |
805 | } | |
806 | else | |
807 | { | |
808 | asection *target; | |
809 | ||
810 | /* If this relocation section applies to a read only | |
811 | section, then we probably need a DT_TEXTREL entry. */ | |
812 | target = bfd_get_section_by_name (output_bfd, name + 5); | |
813 | if (target != NULL | |
814 | && (target->flags & SEC_READONLY) != 0) | |
815 | reltext = true; | |
816 | ||
f786a73f ILT |
817 | if (strcmp (name, ".rela.plt") == 0) |
818 | relplt = true; | |
819 | ||
12662be4 ILT |
820 | /* We use the reloc_count field as a counter if we need |
821 | to copy relocs into the output file. */ | |
822 | s->reloc_count = 0; | |
823 | } | |
4fbc96ad KR |
824 | } |
825 | else if (strcmp (name, ".plt") != 0 | |
826 | && strcmp (name, ".got") != 0) | |
827 | { | |
828 | /* It's not one of our sections, so don't allocate space. */ | |
829 | continue; | |
830 | } | |
12662be4 ILT |
831 | |
832 | if (strip) | |
833 | { | |
834 | asection **spp; | |
835 | ||
836 | for (spp = &s->output_section->owner->sections; | |
837 | *spp != s->output_section; | |
838 | spp = &(*spp)->next) | |
839 | ; | |
840 | *spp = s->output_section->next; | |
841 | --s->output_section->owner->section_count; | |
842 | ||
843 | continue; | |
844 | } | |
845 | ||
4fbc96ad KR |
846 | /* Allocate memory for the section contents. */ |
847 | s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size); | |
848 | if (s->contents == NULL && s->_raw_size != 0) | |
a9713b91 | 849 | return false; |
4fbc96ad | 850 | } |
013dec1a | 851 | |
12662be4 | 852 | if (elf_hash_table (info)->dynamic_sections_created) |
4fbc96ad | 853 | { |
12662be4 ILT |
854 | /* Add some entries to the .dynamic section. We fill in the |
855 | values later, in elf32_sparc_finish_dynamic_sections, but we | |
856 | must add the entries now so that we get the correct size for | |
857 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
858 | dynamic linker and used by the debugger. */ | |
859 | if (! info->shared) | |
860 | { | |
861 | if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0)) | |
862 | return false; | |
863 | } | |
013dec1a | 864 | |
f786a73f ILT |
865 | if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)) |
866 | return false; | |
867 | ||
868 | if (relplt) | |
869 | { | |
870 | if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0) | |
871 | || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA) | |
872 | || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0)) | |
873 | return false; | |
874 | } | |
875 | ||
876 | if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0) | |
12662be4 ILT |
877 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0) |
878 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT, | |
879 | sizeof (Elf32_External_Rela))) | |
4fbc96ad | 880 | return false; |
12662be4 ILT |
881 | |
882 | if (reltext) | |
883 | { | |
884 | if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0)) | |
885 | return false; | |
886 | } | |
4fbc96ad KR |
887 | } |
888 | ||
14cac507 ILT |
889 | /* If we are generating a shared library, we generate a section |
890 | symbol for each output section. These are local symbols, which | |
891 | means that they must come first in the dynamic symbol table. | |
892 | That means we must increment the dynamic symbol index of every | |
893 | other dynamic symbol. */ | |
894 | if (info->shared) | |
895 | { | |
896 | int c, i; | |
897 | ||
898 | c = bfd_count_sections (output_bfd); | |
899 | elf_link_hash_traverse (elf_hash_table (info), | |
900 | elf32_sparc_adjust_dynindx, | |
901 | (PTR) &c); | |
902 | elf_hash_table (info)->dynsymcount += c; | |
903 | ||
904 | for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++) | |
905 | { | |
906 | elf_section_data (s)->dynindx = i; | |
907 | /* These symbols will have no names, so we don't need to | |
908 | fiddle with dynstr_index. */ | |
909 | } | |
910 | } | |
911 | ||
912 | return true; | |
913 | } | |
914 | ||
915 | /* Increment the index of a dynamic symbol by a given amount. Called | |
916 | via elf_link_hash_traverse. */ | |
917 | ||
918 | static boolean | |
919 | elf32_sparc_adjust_dynindx (h, cparg) | |
920 | struct elf_link_hash_entry *h; | |
921 | PTR cparg; | |
922 | { | |
923 | int *cp = (int *) cparg; | |
924 | ||
925 | if (h->dynindx != -1) | |
926 | h->dynindx += *cp; | |
013dec1a ILT |
927 | return true; |
928 | } | |
929 | ||
930 | /* Relocate a SPARC ELF section. */ | |
931 | ||
932 | static boolean | |
933 | elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section, | |
12662be4 | 934 | contents, relocs, local_syms, local_sections) |
013dec1a ILT |
935 | bfd *output_bfd; |
936 | struct bfd_link_info *info; | |
937 | bfd *input_bfd; | |
938 | asection *input_section; | |
939 | bfd_byte *contents; | |
940 | Elf_Internal_Rela *relocs; | |
941 | Elf_Internal_Sym *local_syms; | |
942 | asection **local_sections; | |
943 | { | |
4fbc96ad | 944 | bfd *dynobj; |
013dec1a ILT |
945 | Elf_Internal_Shdr *symtab_hdr; |
946 | struct elf_link_hash_entry **sym_hashes; | |
4fbc96ad | 947 | bfd_vma *local_got_offsets; |
19bfbcbe | 948 | bfd_vma got_base; |
4fbc96ad KR |
949 | asection *sgot; |
950 | asection *splt; | |
951 | asection *sreloc; | |
013dec1a ILT |
952 | Elf_Internal_Rela *rel; |
953 | Elf_Internal_Rela *relend; | |
954 | ||
4fbc96ad | 955 | dynobj = elf_hash_table (info)->dynobj; |
013dec1a ILT |
956 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
957 | sym_hashes = elf_sym_hashes (input_bfd); | |
4fbc96ad KR |
958 | local_got_offsets = elf_local_got_offsets (input_bfd); |
959 | ||
19bfbcbe ILT |
960 | if (elf_hash_table (info)->hgot == NULL) |
961 | got_base = 0; | |
962 | else | |
963 | got_base = elf_hash_table (info)->hgot->root.u.def.value; | |
964 | ||
4fbc96ad KR |
965 | sgot = NULL; |
966 | splt = NULL; | |
967 | sreloc = NULL; | |
013dec1a ILT |
968 | |
969 | rel = relocs; | |
970 | relend = relocs + input_section->reloc_count; | |
971 | for (; rel < relend; rel++) | |
972 | { | |
973 | int r_type; | |
82b1edf7 | 974 | reloc_howto_type *howto; |
3b3f7625 | 975 | unsigned long r_symndx; |
013dec1a ILT |
976 | struct elf_link_hash_entry *h; |
977 | Elf_Internal_Sym *sym; | |
978 | asection *sec; | |
979 | bfd_vma relocation; | |
980 | bfd_reloc_status_type r; | |
981 | ||
982 | r_type = ELF32_R_TYPE (rel->r_info); | |
983 | if (r_type < 0 || r_type >= (int) R_SPARC_max) | |
984 | { | |
985 | bfd_set_error (bfd_error_bad_value); | |
986 | return false; | |
987 | } | |
19bfbcbe | 988 | howto = _bfd_sparc_elf_howto_table + r_type; |
013dec1a ILT |
989 | |
990 | r_symndx = ELF32_R_SYM (rel->r_info); | |
991 | ||
992 | if (info->relocateable) | |
993 | { | |
994 | /* This is a relocateable link. We don't have to change | |
995 | anything, unless the reloc is against a section symbol, | |
996 | in which case we have to adjust according to where the | |
997 | section symbol winds up in the output section. */ | |
998 | if (r_symndx < symtab_hdr->sh_info) | |
999 | { | |
1000 | sym = local_syms + r_symndx; | |
1001 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
1002 | { | |
1003 | sec = local_sections[r_symndx]; | |
1004 | rel->r_addend += sec->output_offset + sym->st_value; | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | continue; | |
1009 | } | |
1010 | ||
1011 | /* This is a final link. */ | |
1012 | h = NULL; | |
1013 | sym = NULL; | |
1014 | sec = NULL; | |
1015 | if (r_symndx < symtab_hdr->sh_info) | |
1016 | { | |
1017 | sym = local_syms + r_symndx; | |
1018 | sec = local_sections[r_symndx]; | |
1019 | relocation = (sec->output_section->vma | |
1020 | + sec->output_offset | |
1021 | + sym->st_value); | |
1022 | } | |
1023 | else | |
1024 | { | |
4fbc96ad | 1025 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
19bfbcbe ILT |
1026 | while (h->root.type == bfd_link_hash_indirect |
1027 | || h->root.type == bfd_link_hash_warning) | |
1028 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
f786a73f ILT |
1029 | if (h->root.type == bfd_link_hash_defined |
1030 | || h->root.type == bfd_link_hash_defweak) | |
013dec1a ILT |
1031 | { |
1032 | sec = h->root.u.def.section; | |
9b09a015 ILT |
1033 | if ((r_type == R_SPARC_WPLT30 |
1034 | && h->plt_offset != (bfd_vma) -1) | |
1035 | || ((r_type == R_SPARC_GOT10 | |
1036 | || r_type == R_SPARC_GOT13 | |
1037 | || r_type == R_SPARC_GOT22) | |
3b3f7625 ILT |
1038 | && elf_hash_table (info)->dynamic_sections_created |
1039 | && (! info->shared | |
1040 | || ! info->symbolic | |
1041 | || (h->elf_link_hash_flags | |
1042 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
9b09a015 | 1043 | || (info->shared |
53787b23 ILT |
1044 | && (! info->symbolic |
1045 | || (h->elf_link_hash_flags | |
1046 | & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
9b09a015 ILT |
1047 | && (r_type == R_SPARC_8 |
1048 | || r_type == R_SPARC_16 | |
1049 | || r_type == R_SPARC_32 | |
1050 | || r_type == R_SPARC_DISP8 | |
1051 | || r_type == R_SPARC_DISP16 | |
1052 | || r_type == R_SPARC_DISP32 | |
1053 | || r_type == R_SPARC_WDISP30 | |
1054 | || r_type == R_SPARC_WDISP22 | |
19bfbcbe ILT |
1055 | || r_type == R_SPARC_WDISP19 |
1056 | || r_type == R_SPARC_WDISP16 | |
9b09a015 ILT |
1057 | || r_type == R_SPARC_HI22 |
1058 | || r_type == R_SPARC_22 | |
1059 | || r_type == R_SPARC_13 | |
1060 | || r_type == R_SPARC_LO10 | |
1061 | || r_type == R_SPARC_UA32 | |
1062 | || ((r_type == R_SPARC_PC10 | |
1063 | || r_type == R_SPARC_PC22) | |
1064 | && strcmp (h->root.root.string, | |
1065 | "_GLOBAL_OFFSET_TABLE_") != 0)))) | |
1066 | { | |
1067 | /* In these cases, we don't need the relocation | |
1068 | value. We check specially because in some | |
1069 | obscure cases sec->output_section will be NULL. */ | |
1070 | relocation = 0; | |
1071 | } | |
1072 | else | |
1073 | relocation = (h->root.u.def.value | |
1074 | + sec->output_section->vma | |
1075 | + sec->output_offset); | |
013dec1a | 1076 | } |
f786a73f | 1077 | else if (h->root.type == bfd_link_hash_undefweak) |
013dec1a | 1078 | relocation = 0; |
3b3f7625 | 1079 | else if (info->shared && !info->symbolic) |
4fbc96ad | 1080 | relocation = 0; |
013dec1a ILT |
1081 | else |
1082 | { | |
1083 | if (! ((*info->callbacks->undefined_symbol) | |
1084 | (info, h->root.root.string, input_bfd, | |
1085 | input_section, rel->r_offset))) | |
1086 | return false; | |
1087 | relocation = 0; | |
1088 | } | |
1089 | } | |
1090 | ||
4fbc96ad KR |
1091 | switch (r_type) |
1092 | { | |
1093 | case R_SPARC_GOT10: | |
1094 | case R_SPARC_GOT13: | |
1095 | case R_SPARC_GOT22: | |
1096 | /* Relocation is to the entry for this symbol in the global | |
1097 | offset table. */ | |
1098 | if (sgot == NULL) | |
1099 | { | |
1100 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1101 | BFD_ASSERT (sgot != NULL); | |
1102 | } | |
1103 | ||
1104 | if (h != NULL) | |
1105 | { | |
12662be4 ILT |
1106 | bfd_vma off; |
1107 | ||
1108 | off = h->got_offset; | |
1109 | BFD_ASSERT (off != (bfd_vma) -1); | |
1110 | ||
3b3f7625 ILT |
1111 | if (! elf_hash_table (info)->dynamic_sections_created |
1112 | || (info->shared | |
1113 | && info->symbolic | |
1114 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
12662be4 | 1115 | { |
3b3f7625 ILT |
1116 | /* This is actually a static link, or it is a |
1117 | -Bsymbolic link and the symbol is defined | |
1118 | locally. We must initialize this entry in the | |
1119 | global offset table. Since the offset must | |
1120 | always be a multiple of 4, we use the least | |
1121 | significant bit to record whether we have | |
1122 | initialized it already. | |
12662be4 ILT |
1123 | |
1124 | When doing a dynamic link, we create a .rela.got | |
1125 | relocation entry to initialize the value. This | |
1126 | is done in the finish_dynamic_symbol routine. */ | |
1127 | if ((off & 1) != 0) | |
1128 | off &= ~1; | |
1129 | else | |
1130 | { | |
1131 | bfd_put_32 (output_bfd, relocation, | |
1132 | sgot->contents + off); | |
1133 | h->got_offset |= 1; | |
1134 | } | |
1135 | } | |
1136 | ||
19bfbcbe | 1137 | relocation = sgot->output_offset + off - got_base; |
4fbc96ad KR |
1138 | } |
1139 | else | |
1140 | { | |
1141 | bfd_vma off; | |
1142 | ||
1143 | BFD_ASSERT (local_got_offsets != NULL | |
1144 | && local_got_offsets[r_symndx] != (bfd_vma) -1); | |
1145 | ||
1146 | off = local_got_offsets[r_symndx]; | |
1147 | ||
1148 | /* The offset must always be a multiple of 4. We use | |
12662be4 ILT |
1149 | the least significant bit to record whether we have |
1150 | already processed this entry. */ | |
4fbc96ad KR |
1151 | if ((off & 1) != 0) |
1152 | off &= ~1; | |
1153 | else | |
1154 | { | |
4fbc96ad KR |
1155 | bfd_put_32 (output_bfd, relocation, sgot->contents + off); |
1156 | ||
12662be4 ILT |
1157 | if (info->shared) |
1158 | { | |
1159 | asection *srelgot; | |
1160 | Elf_Internal_Rela outrel; | |
1161 | ||
1162 | /* We need to generate a R_SPARC_RELATIVE reloc | |
1163 | for the dynamic linker. */ | |
1164 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1165 | BFD_ASSERT (srelgot != NULL); | |
1166 | ||
1167 | outrel.r_offset = (sgot->output_section->vma | |
1168 | + sgot->output_offset | |
1169 | + off); | |
1170 | outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE); | |
1171 | outrel.r_addend = 0; | |
1172 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
1173 | (((Elf32_External_Rela *) | |
1174 | srelgot->contents) | |
1175 | + srelgot->reloc_count)); | |
1176 | ++srelgot->reloc_count; | |
1177 | } | |
4fbc96ad KR |
1178 | |
1179 | local_got_offsets[r_symndx] |= 1; | |
1180 | } | |
1181 | ||
19bfbcbe | 1182 | relocation = sgot->output_offset + off - got_base; |
4fbc96ad KR |
1183 | } |
1184 | ||
1185 | break; | |
1186 | ||
1187 | case R_SPARC_WPLT30: | |
1188 | /* Relocation is to the entry for this symbol in the | |
1189 | procedure linkage table. */ | |
12662be4 ILT |
1190 | BFD_ASSERT (h != NULL); |
1191 | ||
1192 | if (h->plt_offset == (bfd_vma) -1) | |
1193 | { | |
1194 | /* We didn't make a PLT entry for this symbol. This | |
3b3f7625 ILT |
1195 | happens when statically linking PIC code, or when |
1196 | using -Bsymbolic. */ | |
12662be4 ILT |
1197 | break; |
1198 | } | |
1199 | ||
4fbc96ad KR |
1200 | if (splt == NULL) |
1201 | { | |
1202 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1203 | BFD_ASSERT (splt != NULL); | |
1204 | } | |
1205 | ||
4fbc96ad KR |
1206 | relocation = (splt->output_section->vma |
1207 | + splt->output_offset | |
1208 | + h->plt_offset); | |
1209 | break; | |
1210 | ||
1211 | case R_SPARC_PC10: | |
1212 | case R_SPARC_PC22: | |
1213 | if (h != NULL | |
1214 | && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
1215 | break; | |
1216 | /* Fall through. */ | |
4fbc96ad KR |
1217 | case R_SPARC_DISP8: |
1218 | case R_SPARC_DISP16: | |
1219 | case R_SPARC_DISP32: | |
1220 | case R_SPARC_WDISP30: | |
1221 | case R_SPARC_WDISP22: | |
19bfbcbe ILT |
1222 | case R_SPARC_WDISP19: |
1223 | case R_SPARC_WDISP16: | |
3b3f7625 ILT |
1224 | if (h == NULL) |
1225 | break; | |
1226 | /* Fall through. */ | |
1227 | case R_SPARC_8: | |
1228 | case R_SPARC_16: | |
1229 | case R_SPARC_32: | |
4fbc96ad KR |
1230 | case R_SPARC_HI22: |
1231 | case R_SPARC_22: | |
1232 | case R_SPARC_13: | |
1233 | case R_SPARC_LO10: | |
1234 | case R_SPARC_UA32: | |
ff12f303 | 1235 | if (info->shared) |
4fbc96ad KR |
1236 | { |
1237 | Elf_Internal_Rela outrel; | |
1238 | ||
1239 | /* When generating a shared object, these relocations | |
1240 | are copied into the output file to be resolved at run | |
1241 | time. */ | |
1242 | ||
1243 | if (sreloc == NULL) | |
1244 | { | |
1245 | const char *name; | |
1246 | ||
ede4eed4 | 1247 | name = (bfd_elf_string_from_elf_section |
4fbc96ad KR |
1248 | (input_bfd, |
1249 | elf_elfheader (input_bfd)->e_shstrndx, | |
1250 | elf_section_data (input_section)->rel_hdr.sh_name)); | |
1251 | if (name == NULL) | |
1252 | return false; | |
1253 | ||
1254 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
1255 | && strcmp (bfd_get_section_name (input_bfd, | |
1256 | input_section), | |
1257 | name + 5) == 0); | |
1258 | ||
1259 | sreloc = bfd_get_section_by_name (dynobj, name); | |
1260 | BFD_ASSERT (sreloc != NULL); | |
1261 | } | |
1262 | ||
1263 | outrel.r_offset = (rel->r_offset | |
1264 | + input_section->output_section->vma | |
1265 | + input_section->output_offset); | |
53787b23 ILT |
1266 | if (h != NULL |
1267 | && (! info->symbolic | |
1268 | || (h->elf_link_hash_flags | |
1269 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
4fbc96ad | 1270 | { |
12662be4 | 1271 | BFD_ASSERT (h->dynindx != -1); |
4fbc96ad | 1272 | outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); |
14cac507 | 1273 | outrel.r_addend = rel->r_addend; |
4fbc96ad KR |
1274 | } |
1275 | else | |
1276 | { | |
14cac507 | 1277 | if (r_type == R_SPARC_32) |
4fbc96ad | 1278 | { |
14cac507 ILT |
1279 | outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE); |
1280 | outrel.r_addend = relocation + rel->r_addend; | |
4fbc96ad KR |
1281 | } |
1282 | else | |
1283 | { | |
14cac507 ILT |
1284 | long indx; |
1285 | ||
53787b23 ILT |
1286 | if (h == NULL) |
1287 | sec = local_sections[r_symndx]; | |
1288 | else | |
1289 | { | |
1290 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
1291 | || (h->root.type | |
1292 | == bfd_link_hash_defweak)); | |
1293 | sec = h->root.u.def.section; | |
1294 | } | |
14cac507 ILT |
1295 | if (sec != NULL && bfd_is_abs_section (sec)) |
1296 | indx = 0; | |
1297 | else if (sec == NULL || sec->owner == NULL) | |
1298 | { | |
1299 | bfd_set_error (bfd_error_bad_value); | |
1300 | return false; | |
1301 | } | |
1302 | else | |
1303 | { | |
1304 | asection *osec; | |
1305 | ||
1306 | osec = sec->output_section; | |
1307 | indx = elf_section_data (osec)->dynindx; | |
1308 | if (indx == 0) | |
1309 | abort (); | |
1310 | } | |
1311 | ||
1312 | outrel.r_info = ELF32_R_INFO (indx, r_type); | |
1313 | outrel.r_addend = relocation + rel->r_addend; | |
4fbc96ad | 1314 | } |
4fbc96ad KR |
1315 | } |
1316 | ||
1317 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
1318 | (((Elf32_External_Rela *) | |
1319 | sreloc->contents) | |
1320 | + sreloc->reloc_count)); | |
1321 | ++sreloc->reloc_count; | |
1322 | ||
1323 | /* This reloc will be computed at runtime, so there's no | |
1324 | need to do anything now. */ | |
1325 | continue; | |
1326 | } | |
1327 | ||
1328 | default: | |
1329 | break; | |
ff12f303 | 1330 | } |
4fbc96ad | 1331 | |
19bfbcbe ILT |
1332 | if (r_type != R_SPARC_WDISP16) |
1333 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
1334 | contents, rel->r_offset, | |
1335 | relocation, rel->r_addend); | |
1336 | else | |
1337 | { | |
1338 | bfd_vma x; | |
1339 | ||
1340 | relocation += rel->r_addend; | |
1341 | relocation -= (input_section->output_section->vma | |
1342 | + input_section->output_offset); | |
1343 | relocation -= rel->r_offset; | |
1344 | ||
1345 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
1346 | x |= ((((relocation >> 2) & 0xc000) << 6) | |
1347 | | ((relocation >> 2) & 0x3fff)); | |
1348 | bfd_put_32 (input_bfd, x, contents + rel->r_offset); | |
1349 | ||
1350 | if ((bfd_signed_vma) relocation < - 0x40000 | |
1351 | || (bfd_signed_vma) relocation > 0x3ffff) | |
1352 | r = bfd_reloc_overflow; | |
1353 | else | |
1354 | r = bfd_reloc_ok; | |
1355 | } | |
013dec1a ILT |
1356 | |
1357 | if (r != bfd_reloc_ok) | |
1358 | { | |
1359 | switch (r) | |
1360 | { | |
1361 | default: | |
1362 | case bfd_reloc_outofrange: | |
1363 | abort (); | |
1364 | case bfd_reloc_overflow: | |
1365 | { | |
1366 | const char *name; | |
1367 | ||
1368 | if (h != NULL) | |
1369 | name = h->root.root.string; | |
1370 | else | |
1371 | { | |
ede4eed4 KR |
1372 | name = bfd_elf_string_from_elf_section (input_bfd, |
1373 | symtab_hdr->sh_link, | |
1374 | sym->st_name); | |
013dec1a ILT |
1375 | if (name == NULL) |
1376 | return false; | |
1377 | if (*name == '\0') | |
1378 | name = bfd_section_name (input_bfd, sec); | |
1379 | } | |
1380 | if (! ((*info->callbacks->reloc_overflow) | |
1381 | (info, name, howto->name, (bfd_vma) 0, | |
1382 | input_bfd, input_section, rel->r_offset))) | |
1383 | return false; | |
1384 | } | |
1385 | break; | |
1386 | } | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | return true; | |
1391 | } | |
1392 | ||
1393 | /* Finish up dynamic symbol handling. We set the contents of various | |
1394 | dynamic sections here. */ | |
1395 | ||
1396 | static boolean | |
1397 | elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym) | |
1398 | bfd *output_bfd; | |
1399 | struct bfd_link_info *info; | |
1400 | struct elf_link_hash_entry *h; | |
1401 | Elf_Internal_Sym *sym; | |
1402 | { | |
4fbc96ad | 1403 | bfd *dynobj; |
013dec1a | 1404 | |
4fbc96ad | 1405 | dynobj = elf_hash_table (info)->dynobj; |
013dec1a | 1406 | |
4fbc96ad | 1407 | if (h->plt_offset != (bfd_vma) -1) |
013dec1a ILT |
1408 | { |
1409 | asection *splt; | |
1410 | asection *srela; | |
1411 | Elf_Internal_Rela rela; | |
1412 | ||
4fbc96ad KR |
1413 | /* This symbol has an entry in the procedure linkage table. Set |
1414 | it up. */ | |
1415 | ||
1416 | BFD_ASSERT (h->dynindx != -1); | |
1417 | ||
1418 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1419 | srela = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
1420 | BFD_ASSERT (splt != NULL && srela != NULL); | |
013dec1a ILT |
1421 | |
1422 | /* Fill in the entry in the procedure linkage table. */ | |
1423 | bfd_put_32 (output_bfd, | |
4fbc96ad KR |
1424 | PLT_ENTRY_WORD0 + h->plt_offset, |
1425 | splt->contents + h->plt_offset); | |
013dec1a ILT |
1426 | bfd_put_32 (output_bfd, |
1427 | (PLT_ENTRY_WORD1 | |
4fbc96ad KR |
1428 | + (((- (h->plt_offset + 4)) >> 2) & 0x3fffff)), |
1429 | splt->contents + h->plt_offset + 4); | |
013dec1a | 1430 | bfd_put_32 (output_bfd, PLT_ENTRY_WORD2, |
4fbc96ad | 1431 | splt->contents + h->plt_offset + 8); |
013dec1a ILT |
1432 | |
1433 | /* Fill in the entry in the .rela.plt section. */ | |
1434 | rela.r_offset = (splt->output_section->vma | |
1435 | + splt->output_offset | |
4fbc96ad | 1436 | + h->plt_offset); |
013dec1a ILT |
1437 | rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT); |
1438 | rela.r_addend = 0; | |
1439 | bfd_elf32_swap_reloca_out (output_bfd, &rela, | |
1440 | ((Elf32_External_Rela *) srela->contents | |
4fbc96ad KR |
1441 | + h->plt_offset / PLT_ENTRY_SIZE - 4)); |
1442 | ||
1443 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1444 | { | |
1445 | /* Mark the symbol as undefined, rather than as defined in | |
1446 | the .plt section. Leave the value alone. */ | |
1447 | sym->st_shndx = SHN_UNDEF; | |
1448 | } | |
1449 | } | |
1450 | ||
1451 | if (h->got_offset != (bfd_vma) -1) | |
1452 | { | |
1453 | asection *sgot; | |
1454 | asection *srela; | |
1455 | Elf_Internal_Rela rela; | |
1456 | ||
1457 | /* This symbol has an entry in the global offset table. Set it | |
1458 | up. */ | |
013dec1a | 1459 | |
4fbc96ad KR |
1460 | BFD_ASSERT (h->dynindx != -1); |
1461 | ||
1462 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1463 | srela = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1464 | BFD_ASSERT (sgot != NULL && srela != NULL); | |
1465 | ||
4fbc96ad KR |
1466 | rela.r_offset = (sgot->output_section->vma |
1467 | + sgot->output_offset | |
3b3f7625 ILT |
1468 | + (h->got_offset &~ 1)); |
1469 | ||
1470 | /* If this is a -Bsymbolic link, and the symbol is defined | |
1471 | locally, we just want to emit a RELATIVE reloc. The entry in | |
1472 | the global offset table will already have been initialized in | |
1473 | the relocate_section function. */ | |
1474 | if (info->shared | |
1475 | && info->symbolic | |
1476 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)) | |
1477 | rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE); | |
1478 | else | |
1479 | { | |
1480 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset); | |
1481 | rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT); | |
1482 | } | |
1483 | ||
4fbc96ad KR |
1484 | rela.r_addend = 0; |
1485 | bfd_elf32_swap_reloca_out (output_bfd, &rela, | |
1486 | ((Elf32_External_Rela *) srela->contents | |
1487 | + srela->reloc_count)); | |
1488 | ++srela->reloc_count; | |
013dec1a | 1489 | } |
4fbc96ad KR |
1490 | |
1491 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) | |
013dec1a | 1492 | { |
4fbc96ad KR |
1493 | asection *s; |
1494 | Elf_Internal_Rela rela; | |
013dec1a | 1495 | |
4fbc96ad | 1496 | /* This symbols needs a copy reloc. Set it up. */ |
013dec1a | 1497 | |
4fbc96ad KR |
1498 | BFD_ASSERT (h->dynindx != -1); |
1499 | ||
1500 | s = bfd_get_section_by_name (h->root.u.def.section->owner, | |
1501 | ".rela.bss"); | |
1502 | BFD_ASSERT (s != NULL); | |
1503 | ||
1504 | rela.r_offset = (h->root.u.def.value | |
1505 | + h->root.u.def.section->output_section->vma | |
1506 | + h->root.u.def.section->output_offset); | |
1507 | rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY); | |
1508 | rela.r_addend = 0; | |
1509 | bfd_elf32_swap_reloca_out (output_bfd, &rela, | |
1510 | ((Elf32_External_Rela *) s->contents | |
1511 | + s->reloc_count)); | |
1512 | ++s->reloc_count; | |
013dec1a ILT |
1513 | } |
1514 | ||
4fbc96ad KR |
1515 | /* Mark some specially defined symbols as absolute. */ |
1516 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
1517 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0 | |
1518 | || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0) | |
1519 | sym->st_shndx = SHN_ABS; | |
1520 | ||
013dec1a ILT |
1521 | return true; |
1522 | } | |
1523 | ||
1524 | /* Finish up the dynamic sections. */ | |
1525 | ||
1526 | static boolean | |
1527 | elf32_sparc_finish_dynamic_sections (output_bfd, info) | |
1528 | bfd *output_bfd; | |
1529 | struct bfd_link_info *info; | |
1530 | { | |
4fbc96ad | 1531 | bfd *dynobj; |
013dec1a | 1532 | asection *sdyn; |
12662be4 | 1533 | asection *sgot; |
013dec1a | 1534 | |
4fbc96ad KR |
1535 | dynobj = elf_hash_table (info)->dynobj; |
1536 | ||
4fbc96ad | 1537 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); |
013dec1a | 1538 | |
12662be4 | 1539 | if (elf_hash_table (info)->dynamic_sections_created) |
013dec1a | 1540 | { |
12662be4 ILT |
1541 | asection *splt; |
1542 | Elf32_External_Dyn *dyncon, *dynconend; | |
013dec1a | 1543 | |
12662be4 ILT |
1544 | splt = bfd_get_section_by_name (dynobj, ".plt"); |
1545 | BFD_ASSERT (splt != NULL && sdyn != NULL); | |
013dec1a | 1546 | |
12662be4 ILT |
1547 | dyncon = (Elf32_External_Dyn *) sdyn->contents; |
1548 | dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
1549 | for (; dyncon < dynconend; dyncon++) | |
013dec1a | 1550 | { |
12662be4 ILT |
1551 | Elf_Internal_Dyn dyn; |
1552 | const char *name; | |
1553 | boolean size; | |
013dec1a | 1554 | |
12662be4 | 1555 | bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); |
013dec1a | 1556 | |
12662be4 ILT |
1557 | switch (dyn.d_tag) |
1558 | { | |
1559 | case DT_PLTGOT: name = ".plt"; size = false; break; | |
1560 | case DT_PLTRELSZ: name = ".rela.plt"; size = true; break; | |
1561 | case DT_JMPREL: name = ".rela.plt"; size = false; break; | |
1562 | default: name = NULL; size = false; break; | |
1563 | } | |
1564 | ||
1565 | if (name != NULL) | |
013dec1a | 1566 | { |
12662be4 ILT |
1567 | asection *s; |
1568 | ||
1569 | s = bfd_get_section_by_name (output_bfd, name); | |
14cac507 ILT |
1570 | if (s == NULL) |
1571 | dyn.d_un.d_val = 0; | |
013dec1a | 1572 | else |
12662be4 | 1573 | { |
14cac507 ILT |
1574 | if (! size) |
1575 | dyn.d_un.d_ptr = s->vma; | |
12662be4 | 1576 | else |
14cac507 ILT |
1577 | { |
1578 | if (s->_cooked_size != 0) | |
1579 | dyn.d_un.d_val = s->_cooked_size; | |
1580 | else | |
1581 | dyn.d_un.d_val = s->_raw_size; | |
1582 | } | |
12662be4 ILT |
1583 | } |
1584 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
013dec1a | 1585 | } |
013dec1a | 1586 | } |
013dec1a | 1587 | |
12662be4 ILT |
1588 | /* Clear the first four entries in the procedure linkage table, |
1589 | and put a nop in the last four bytes. */ | |
1590 | if (splt->_raw_size > 0) | |
1591 | { | |
1592 | memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE); | |
1593 | bfd_put_32 (output_bfd, SPARC_NOP, | |
1594 | splt->contents + splt->_raw_size - 4); | |
1595 | } | |
1596 | ||
1597 | elf_section_data (splt->output_section)->this_hdr.sh_entsize = | |
1598 | PLT_ENTRY_SIZE; | |
013dec1a ILT |
1599 | } |
1600 | ||
1601 | /* Set the first entry in the global offset table to the address of | |
1602 | the dynamic section. */ | |
12662be4 ILT |
1603 | sgot = bfd_get_section_by_name (dynobj, ".got"); |
1604 | BFD_ASSERT (sgot != NULL); | |
013dec1a | 1605 | if (sgot->_raw_size > 0) |
12662be4 ILT |
1606 | { |
1607 | if (sdyn == NULL) | |
1608 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents); | |
1609 | else | |
1610 | bfd_put_32 (output_bfd, | |
1611 | sdyn->output_section->vma + sdyn->output_offset, | |
1612 | sgot->contents); | |
1613 | } | |
013dec1a ILT |
1614 | |
1615 | elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4; | |
013dec1a | 1616 | |
14cac507 ILT |
1617 | if (info->shared) |
1618 | { | |
1619 | asection *sdynsym; | |
1620 | asection *s; | |
1621 | Elf_Internal_Sym sym; | |
1622 | ||
1623 | /* Set up the section symbols for the output sections. */ | |
1624 | ||
1625 | sdynsym = bfd_get_section_by_name (dynobj, ".dynsym"); | |
1626 | BFD_ASSERT (sdynsym != NULL); | |
1627 | ||
1628 | sym.st_size = 0; | |
1629 | sym.st_name = 0; | |
1630 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
1631 | sym.st_other = 0; | |
1632 | ||
1633 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
1634 | { | |
1635 | int indx; | |
1636 | ||
1637 | sym.st_value = s->vma; | |
1638 | ||
1639 | indx = elf_section_data (s)->this_idx; | |
1640 | BFD_ASSERT (indx > 0); | |
1641 | sym.st_shndx = indx; | |
1642 | ||
1643 | bfd_elf32_swap_symbol_out (output_bfd, &sym, | |
3b3f7625 ILT |
1644 | (PTR) (((Elf32_External_Sym *) |
1645 | sdynsym->contents) | |
1646 | + elf_section_data (s)->dynindx)); | |
14cac507 ILT |
1647 | } |
1648 | ||
1649 | /* Set the sh_info field of the output .dynsym section to the | |
1650 | index of the first global symbol. */ | |
1651 | elf_section_data (sdynsym->output_section)->this_hdr.sh_info = | |
1652 | bfd_count_sections (output_bfd) + 1; | |
1653 | } | |
1654 | ||
013dec1a ILT |
1655 | return true; |
1656 | } | |
5bc513b4 | 1657 | \f |
53787b23 | 1658 | /* Functions for dealing with the e_flags field. |
5bc513b4 | 1659 | |
53787b23 ILT |
1660 | We don't define set_private_flags or copy_private_bfd_data because |
1661 | the only currently defined values are based on the bfd mach number, | |
1662 | so we use the latter instead and defer setting e_flags until the | |
1663 | file is written out. */ | |
5bc513b4 DE |
1664 | |
1665 | /* Merge backend specific data from an object file to the output | |
1666 | object file when linking. */ | |
1667 | ||
1668 | static boolean | |
1669 | elf32_sparc_merge_private_bfd_data (ibfd, obfd) | |
1670 | bfd *ibfd; | |
1671 | bfd *obfd; | |
1672 | { | |
5bc513b4 DE |
1673 | boolean error; |
1674 | ||
ff12f303 ILT |
1675 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
1676 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
5bc513b4 DE |
1677 | return true; |
1678 | ||
1679 | error = false; | |
1680 | ||
53787b23 ILT |
1681 | #if 0 |
1682 | /* ??? The native linker doesn't do this so we can't (otherwise gcc would | |
1683 | have to know which linker is being used). Instead, the native linker | |
1684 | bumps up the architecture level when it has to. However, I still think | |
1685 | warnings like these are good, so it would be nice to have them turned on | |
1686 | by some option. */ | |
1687 | ||
5bc513b4 DE |
1688 | /* If the output machine is normal sparc, we can't allow v9 input files. */ |
1689 | if (bfd_get_mach (obfd) == bfd_mach_sparc | |
1690 | && (bfd_get_mach (ibfd) == bfd_mach_sparc_v8plus | |
1691 | || bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa)) | |
1692 | { | |
1693 | error = true; | |
1694 | (*_bfd_error_handler) | |
1695 | ("%s: compiled for a v8plus system and target is v8", | |
1696 | bfd_get_filename (ibfd)); | |
1697 | } | |
1698 | /* If the output machine is v9, we can't allow v9+vis input files. */ | |
1699 | if (bfd_get_mach (obfd) == bfd_mach_sparc_v8plus | |
1700 | && bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa) | |
1701 | { | |
1702 | error = true; | |
1703 | (*_bfd_error_handler) | |
1704 | ("%s: compiled for a v8plusa system and target is v8plus", | |
1705 | bfd_get_filename (ibfd)); | |
1706 | } | |
53787b23 ILT |
1707 | #else |
1708 | if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9) | |
5bc513b4 | 1709 | { |
53787b23 ILT |
1710 | error = true; |
1711 | (*_bfd_error_handler) | |
1712 | ("%s: compiled for a 64 bit system and target is 32 bit", | |
1713 | bfd_get_filename (ibfd)); | |
5bc513b4 | 1714 | } |
53787b23 ILT |
1715 | else if (bfd_get_mach (obfd) < bfd_get_mach (ibfd)) |
1716 | bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd)); | |
1717 | #endif | |
5bc513b4 DE |
1718 | |
1719 | if (error) | |
1720 | { | |
1721 | bfd_set_error (bfd_error_bad_value); | |
1722 | return false; | |
1723 | } | |
1724 | ||
1725 | return true; | |
1726 | } | |
1727 | \f | |
1728 | /* Set the right machine number. */ | |
013dec1a | 1729 | |
5bc513b4 DE |
1730 | static boolean |
1731 | elf32_sparc_object_p (abfd) | |
1732 | bfd *abfd; | |
1733 | { | |
1734 | if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS) | |
53787b23 ILT |
1735 | { |
1736 | if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1) | |
1737 | return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, | |
1738 | bfd_mach_sparc_v8plusa); | |
1739 | else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS) | |
1740 | return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, | |
1741 | bfd_mach_sparc_v8plus); | |
1742 | else | |
1743 | return false; | |
1744 | } | |
5bc513b4 DE |
1745 | else |
1746 | return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc); | |
1747 | } | |
1748 | ||
1749 | /* The final processing done just before writing out the object file. | |
1750 | We need to set the e_machine field appropriately. */ | |
1751 | ||
1752 | static void | |
1753 | elf32_sparc_final_write_processing (abfd, linker) | |
1754 | bfd *abfd; | |
1755 | boolean linker; | |
1756 | { | |
53787b23 | 1757 | switch (bfd_get_mach (abfd)) |
5bc513b4 | 1758 | { |
53787b23 ILT |
1759 | case bfd_mach_sparc : |
1760 | break; /* nothing to do */ | |
1761 | case bfd_mach_sparc_v8plus : | |
5bc513b4 | 1762 | elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS; |
5bc513b4 | 1763 | elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK; |
5bc513b4 | 1764 | elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS; |
53787b23 ILT |
1765 | break; |
1766 | case bfd_mach_sparc_v8plusa : | |
1767 | elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS; | |
1768 | elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK; | |
1769 | elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1; | |
1770 | break; | |
1771 | default : | |
1772 | abort (); | |
5bc513b4 DE |
1773 | } |
1774 | } | |
5bc513b4 | 1775 | \f |
32090b8e KR |
1776 | #define TARGET_BIG_SYM bfd_elf32_sparc_vec |
1777 | #define TARGET_BIG_NAME "elf32-sparc" | |
1778 | #define ELF_ARCH bfd_arch_sparc | |
013dec1a | 1779 | #define ELF_MACHINE_CODE EM_SPARC |
6b3eb07e | 1780 | #define ELF_MACHINE_ALT1 EM_SPARC32PLUS |
013dec1a | 1781 | #define ELF_MAXPAGESIZE 0x10000 |
19bfbcbe ILT |
1782 | |
1783 | #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup | |
ff12f303 | 1784 | #define elf_info_to_howto elf32_sparc_info_to_howto |
013dec1a | 1785 | #define elf_backend_create_dynamic_sections \ |
ede4eed4 | 1786 | _bfd_elf_create_dynamic_sections |
4fbc96ad | 1787 | #define elf_backend_check_relocs elf32_sparc_check_relocs |
013dec1a ILT |
1788 | #define elf_backend_adjust_dynamic_symbol \ |
1789 | elf32_sparc_adjust_dynamic_symbol | |
1790 | #define elf_backend_size_dynamic_sections \ | |
1791 | elf32_sparc_size_dynamic_sections | |
1792 | #define elf_backend_relocate_section elf32_sparc_relocate_section | |
1793 | #define elf_backend_finish_dynamic_symbol \ | |
1794 | elf32_sparc_finish_dynamic_symbol | |
1795 | #define elf_backend_finish_dynamic_sections \ | |
1796 | elf32_sparc_finish_dynamic_sections | |
5bc513b4 DE |
1797 | #define bfd_elf32_bfd_merge_private_bfd_data \ |
1798 | elf32_sparc_merge_private_bfd_data | |
1799 | #define elf_backend_object_p elf32_sparc_object_p | |
1800 | #define elf_backend_final_write_processing \ | |
1801 | elf32_sparc_final_write_processing | |
ede4eed4 | 1802 | #define elf_backend_want_got_plt 0 |
3b3f7625 | 1803 | #define elf_backend_plt_readonly 0 |
ede4eed4 | 1804 | #define elf_backend_want_plt_sym 1 |
32090b8e KR |
1805 | |
1806 | #include "elf32-target.h" |