]>
Commit | Line | Data |
---|---|---|
a85d7ed0 | 1 | /* IBM S/390-specific support for 64-bit ELF |
e92d460e | 2 | Copyright 2000, 2001, 2002 Free Software Foundation, Inc. |
a85d7ed0 NC |
3 | Contributed Martin Schwidefsky (schwidefsky@de.ibm.com). |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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 | |
20 | 02111-1307, USA. */ | |
21 | ||
22 | #include "bfd.h" | |
23 | #include "sysdep.h" | |
24 | #include "bfdlink.h" | |
25 | #include "libbfd.h" | |
26 | #include "elf-bfd.h" | |
27 | ||
28 | static reloc_howto_type *elf_s390_reloc_type_lookup | |
29 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
30 | static void elf_s390_info_to_howto | |
31 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); | |
b34976b6 | 32 | static bfd_boolean elf_s390_is_local_label_name |
0451c93c MS |
33 | PARAMS ((bfd *, const char *)); |
34 | static struct bfd_hash_entry *link_hash_newfunc | |
a85d7ed0 NC |
35 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); |
36 | static struct bfd_link_hash_table *elf_s390_link_hash_table_create | |
37 | PARAMS ((bfd *)); | |
b34976b6 | 38 | static bfd_boolean create_got_section |
0451c93c | 39 | PARAMS((bfd *, struct bfd_link_info *)); |
b34976b6 | 40 | static bfd_boolean elf_s390_create_dynamic_sections |
0451c93c MS |
41 | PARAMS((bfd *, struct bfd_link_info *)); |
42 | static void elf_s390_copy_indirect_symbol | |
b48fa14c AM |
43 | PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *, |
44 | struct elf_link_hash_entry *)); | |
b34976b6 | 45 | static bfd_boolean elf_s390_check_relocs |
a85d7ed0 NC |
46 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
47 | const Elf_Internal_Rela *)); | |
99c79b2e | 48 | static asection *elf_s390_gc_mark_hook |
1e2f5b6e | 49 | PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *, |
99c79b2e | 50 | struct elf_link_hash_entry *, Elf_Internal_Sym *)); |
b34976b6 | 51 | static bfd_boolean elf_s390_gc_sweep_hook |
99c79b2e AJ |
52 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
53 | const Elf_Internal_Rela *)); | |
5236c819 MS |
54 | struct elf_s390_link_hash_entry; |
55 | static void elf_s390_adjust_gotplt | |
56 | PARAMS ((struct elf_s390_link_hash_entry *)); | |
b34976b6 | 57 | static bfd_boolean elf_s390_adjust_dynamic_symbol |
a85d7ed0 | 58 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); |
b34976b6 | 59 | static bfd_boolean allocate_dynrelocs |
0451c93c | 60 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
b34976b6 | 61 | static bfd_boolean readonly_dynrelocs |
0451c93c | 62 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
b34976b6 | 63 | static bfd_boolean elf_s390_size_dynamic_sections |
a85d7ed0 | 64 | PARAMS ((bfd *, struct bfd_link_info *)); |
b34976b6 | 65 | static bfd_boolean elf_s390_relocate_section |
a85d7ed0 NC |
66 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
67 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
b34976b6 | 68 | static bfd_boolean elf_s390_finish_dynamic_symbol |
a85d7ed0 NC |
69 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, |
70 | Elf_Internal_Sym *)); | |
0451c93c MS |
71 | static enum elf_reloc_type_class elf_s390_reloc_type_class |
72 | PARAMS ((const Elf_Internal_Rela *)); | |
b34976b6 | 73 | static bfd_boolean elf_s390_finish_dynamic_sections |
a85d7ed0 | 74 | PARAMS ((bfd *, struct bfd_link_info *)); |
5236c819 MS |
75 | static bfd_boolean elf_s390_object_p |
76 | PARAMS ((bfd *)); | |
a85d7ed0 | 77 | |
a85d7ed0 NC |
78 | #include "elf/s390.h" |
79 | ||
80 | /* In case we're on a 32-bit machine, construct a 64-bit "-1" value | |
81 | from smaller values. Start with zero, widen, *then* decrement. */ | |
82 | #define MINUS_ONE (((bfd_vma)0) - 1) | |
83 | ||
84 | /* The relocation "howto" table. */ | |
85 | static reloc_howto_type elf_howto_table[] = | |
86 | { | |
87 | HOWTO (R_390_NONE, /* type */ | |
88 | 0, /* rightshift */ | |
89 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
90 | 0, /* bitsize */ | |
b34976b6 | 91 | FALSE, /* pc_relative */ |
a85d7ed0 NC |
92 | 0, /* bitpos */ |
93 | complain_overflow_dont, /* complain_on_overflow */ | |
94 | bfd_elf_generic_reloc, /* special_function */ | |
95 | "R_390_NONE", /* name */ | |
b34976b6 | 96 | FALSE, /* partial_inplace */ |
a85d7ed0 NC |
97 | 0, /* src_mask */ |
98 | 0, /* dst_mask */ | |
b34976b6 AM |
99 | FALSE), /* pcrel_offset */ |
100 | ||
5236c819 MS |
101 | HOWTO(R_390_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, |
102 | bfd_elf_generic_reloc, "R_390_8", FALSE, 0,0x000000ff, FALSE), | |
103 | HOWTO(R_390_12, 0, 1, 12, FALSE, 0, complain_overflow_dont, | |
104 | bfd_elf_generic_reloc, "R_390_12", FALSE, 0,0x00000fff, FALSE), | |
105 | HOWTO(R_390_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, | |
106 | bfd_elf_generic_reloc, "R_390_16", FALSE, 0,0x0000ffff, FALSE), | |
107 | HOWTO(R_390_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
108 | bfd_elf_generic_reloc, "R_390_32", FALSE, 0,0xffffffff, FALSE), | |
109 | HOWTO(R_390_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
110 | bfd_elf_generic_reloc, "R_390_PC32", FALSE, 0,0xffffffff, TRUE), | |
111 | HOWTO(R_390_GOT12, 0, 1, 12, FALSE, 0, complain_overflow_dont, | |
112 | bfd_elf_generic_reloc, "R_390_GOT12", FALSE, 0,0x00000fff, FALSE), | |
113 | HOWTO(R_390_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
114 | bfd_elf_generic_reloc, "R_390_GOT32", FALSE, 0,0xffffffff, FALSE), | |
115 | HOWTO(R_390_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
116 | bfd_elf_generic_reloc, "R_390_PLT32", FALSE, 0,0xffffffff, TRUE), | |
117 | HOWTO(R_390_COPY, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
118 | bfd_elf_generic_reloc, "R_390_COPY", FALSE, 0,MINUS_ONE, FALSE), | |
119 | HOWTO(R_390_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
120 | bfd_elf_generic_reloc, "R_390_GLOB_DAT", FALSE, 0,MINUS_ONE, FALSE), | |
121 | HOWTO(R_390_JMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
122 | bfd_elf_generic_reloc, "R_390_JMP_SLOT", FALSE, 0,MINUS_ONE, FALSE), | |
123 | HOWTO(R_390_RELATIVE, 0, 4, 64, TRUE, 0, complain_overflow_bitfield, | |
124 | bfd_elf_generic_reloc, "R_390_RELATIVE", FALSE, 0,MINUS_ONE, FALSE), | |
125 | HOWTO(R_390_GOTOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
126 | bfd_elf_generic_reloc, "R_390_GOTOFF32", FALSE, 0,MINUS_ONE, FALSE), | |
127 | HOWTO(R_390_GOTPC, 0, 4, 64, TRUE, 0, complain_overflow_bitfield, | |
128 | bfd_elf_generic_reloc, "R_390_GOTPC", FALSE, 0,MINUS_ONE, TRUE), | |
129 | HOWTO(R_390_GOT16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, | |
130 | bfd_elf_generic_reloc, "R_390_GOT16", FALSE, 0,0x0000ffff, FALSE), | |
131 | HOWTO(R_390_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield, | |
132 | bfd_elf_generic_reloc, "R_390_PC16", FALSE, 0,0x0000ffff, TRUE), | |
133 | HOWTO(R_390_PC16DBL, 1, 1, 16, TRUE, 0, complain_overflow_bitfield, | |
134 | bfd_elf_generic_reloc, "R_390_PC16DBL", FALSE, 0,0x0000ffff, TRUE), | |
135 | HOWTO(R_390_PLT16DBL, 1, 1, 16, TRUE, 0, complain_overflow_bitfield, | |
136 | bfd_elf_generic_reloc, "R_390_PLT16DBL", FALSE, 0,0x0000ffff, TRUE), | |
137 | HOWTO(R_390_PC32DBL, 1, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
138 | bfd_elf_generic_reloc, "R_390_PC32DBL", FALSE, 0,0xffffffff, TRUE), | |
139 | HOWTO(R_390_PLT32DBL, 1, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
140 | bfd_elf_generic_reloc, "R_390_PLT32DBL", FALSE, 0,0xffffffff, TRUE), | |
141 | HOWTO(R_390_GOTPCDBL, 1, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
142 | bfd_elf_generic_reloc, "R_390_GOTPCDBL", FALSE, 0,MINUS_ONE, TRUE), | |
143 | HOWTO(R_390_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
144 | bfd_elf_generic_reloc, "R_390_64", FALSE, 0,MINUS_ONE, FALSE), | |
145 | HOWTO(R_390_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield, | |
146 | bfd_elf_generic_reloc, "R_390_PC64", FALSE, 0,MINUS_ONE, TRUE), | |
147 | HOWTO(R_390_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
148 | bfd_elf_generic_reloc, "R_390_GOT64", FALSE, 0,MINUS_ONE, FALSE), | |
149 | HOWTO(R_390_PLT64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield, | |
150 | bfd_elf_generic_reloc, "R_390_PLT64", FALSE, 0,MINUS_ONE, TRUE), | |
151 | HOWTO(R_390_GOTENT, 1, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
152 | bfd_elf_generic_reloc, "R_390_GOTENT", FALSE, 0,MINUS_ONE, TRUE), | |
153 | HOWTO(R_390_GOTOFF16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, | |
154 | bfd_elf_generic_reloc, "R_390_GOTOFF16", FALSE, 0,0x0000ffff, FALSE), | |
155 | HOWTO(R_390_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
156 | bfd_elf_generic_reloc, "R_390_GOTOFF64", FALSE, 0,MINUS_ONE, FALSE), | |
157 | HOWTO(R_390_GOTPLT12, 0, 1, 12, FALSE, 0, complain_overflow_dont, | |
158 | bfd_elf_generic_reloc, "R_390_GOTPLT12", FALSE, 0,0x00000fff, FALSE), | |
159 | HOWTO(R_390_GOTPLT16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, | |
160 | bfd_elf_generic_reloc, "R_390_GOTPLT16", FALSE, 0,0x0000ffff, FALSE), | |
161 | HOWTO(R_390_GOTPLT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
162 | bfd_elf_generic_reloc, "R_390_GOTPLT32", FALSE, 0,0xffffffff, FALSE), | |
163 | HOWTO(R_390_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
164 | bfd_elf_generic_reloc, "R_390_GOTPLT64", FALSE, 0,MINUS_ONE, FALSE), | |
165 | HOWTO(R_390_GOTPLTENT, 1, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
166 | bfd_elf_generic_reloc, "R_390_GOTPLTENT",FALSE, 0,MINUS_ONE, TRUE), | |
167 | HOWTO(R_390_PLTOFF16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, | |
168 | bfd_elf_generic_reloc, "R_390_PLTOFF16", FALSE, 0,0x0000ffff, FALSE), | |
169 | HOWTO(R_390_PLTOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
170 | bfd_elf_generic_reloc, "R_390_PLTOFF32", FALSE, 0,0xffffffff, FALSE), | |
171 | HOWTO(R_390_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
172 | bfd_elf_generic_reloc, "R_390_PLTOFF64", FALSE, 0,MINUS_ONE, FALSE), | |
a85d7ed0 NC |
173 | }; |
174 | ||
175 | /* GNU extension to record C++ vtable hierarchy. */ | |
176 | static reloc_howto_type elf64_s390_vtinherit_howto = | |
b34976b6 | 177 | HOWTO (R_390_GNU_VTINHERIT, 0,4,0,FALSE,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", FALSE,0, 0, FALSE); |
a85d7ed0 | 178 | static reloc_howto_type elf64_s390_vtentry_howto = |
b34976b6 | 179 | HOWTO (R_390_GNU_VTENTRY, 0,4,0,FALSE,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_390_GNU_VTENTRY", FALSE,0,0, FALSE); |
a85d7ed0 NC |
180 | |
181 | static reloc_howto_type * | |
182 | elf_s390_reloc_type_lookup (abfd, code) | |
183 | bfd *abfd ATTRIBUTE_UNUSED; | |
184 | bfd_reloc_code_real_type code; | |
185 | { | |
0451c93c MS |
186 | switch (code) |
187 | { | |
188 | case BFD_RELOC_NONE: | |
189 | return &elf_howto_table[(int) R_390_NONE]; | |
190 | case BFD_RELOC_8: | |
191 | return &elf_howto_table[(int) R_390_8]; | |
192 | case BFD_RELOC_390_12: | |
193 | return &elf_howto_table[(int) R_390_12]; | |
194 | case BFD_RELOC_16: | |
195 | return &elf_howto_table[(int) R_390_16]; | |
196 | case BFD_RELOC_32: | |
197 | return &elf_howto_table[(int) R_390_32]; | |
198 | case BFD_RELOC_CTOR: | |
199 | return &elf_howto_table[(int) R_390_32]; | |
200 | case BFD_RELOC_32_PCREL: | |
201 | return &elf_howto_table[(int) R_390_PC32]; | |
202 | case BFD_RELOC_390_GOT12: | |
203 | return &elf_howto_table[(int) R_390_GOT12]; | |
204 | case BFD_RELOC_32_GOT_PCREL: | |
205 | return &elf_howto_table[(int) R_390_GOT32]; | |
206 | case BFD_RELOC_390_PLT32: | |
207 | return &elf_howto_table[(int) R_390_PLT32]; | |
208 | case BFD_RELOC_390_COPY: | |
209 | return &elf_howto_table[(int) R_390_COPY]; | |
210 | case BFD_RELOC_390_GLOB_DAT: | |
211 | return &elf_howto_table[(int) R_390_GLOB_DAT]; | |
212 | case BFD_RELOC_390_JMP_SLOT: | |
213 | return &elf_howto_table[(int) R_390_JMP_SLOT]; | |
214 | case BFD_RELOC_390_RELATIVE: | |
215 | return &elf_howto_table[(int) R_390_RELATIVE]; | |
216 | case BFD_RELOC_32_GOTOFF: | |
5236c819 | 217 | return &elf_howto_table[(int) R_390_GOTOFF32]; |
0451c93c MS |
218 | case BFD_RELOC_390_GOTPC: |
219 | return &elf_howto_table[(int) R_390_GOTPC]; | |
220 | case BFD_RELOC_390_GOT16: | |
221 | return &elf_howto_table[(int) R_390_GOT16]; | |
222 | case BFD_RELOC_16_PCREL: | |
223 | return &elf_howto_table[(int) R_390_PC16]; | |
224 | case BFD_RELOC_390_PC16DBL: | |
225 | return &elf_howto_table[(int) R_390_PC16DBL]; | |
226 | case BFD_RELOC_390_PLT16DBL: | |
227 | return &elf_howto_table[(int) R_390_PLT16DBL]; | |
0451c93c MS |
228 | case BFD_RELOC_390_PC32DBL: |
229 | return &elf_howto_table[(int) R_390_PC32DBL]; | |
230 | case BFD_RELOC_390_PLT32DBL: | |
231 | return &elf_howto_table[(int) R_390_PLT32DBL]; | |
232 | case BFD_RELOC_390_GOTPCDBL: | |
233 | return &elf_howto_table[(int) R_390_GOTPCDBL]; | |
234 | case BFD_RELOC_64: | |
235 | return &elf_howto_table[(int) R_390_64]; | |
236 | case BFD_RELOC_64_PCREL: | |
237 | return &elf_howto_table[(int) R_390_PC64]; | |
238 | case BFD_RELOC_390_GOT64: | |
239 | return &elf_howto_table[(int) R_390_GOT64]; | |
240 | case BFD_RELOC_390_PLT64: | |
241 | return &elf_howto_table[(int) R_390_PLT64]; | |
242 | case BFD_RELOC_390_GOTENT: | |
243 | return &elf_howto_table[(int) R_390_GOTENT]; | |
5236c819 MS |
244 | case BFD_RELOC_16_GOTOFF: |
245 | return &elf_howto_table[(int) R_390_GOTOFF16]; | |
246 | case BFD_RELOC_390_GOTOFF64: | |
247 | return &elf_howto_table[(int) R_390_GOTOFF64]; | |
248 | case BFD_RELOC_390_GOTPLT12: | |
249 | return &elf_howto_table[(int) R_390_GOTPLT12]; | |
250 | case BFD_RELOC_390_GOTPLT16: | |
251 | return &elf_howto_table[(int) R_390_GOTPLT16]; | |
252 | case BFD_RELOC_390_GOTPLT32: | |
253 | return &elf_howto_table[(int) R_390_GOTPLT32]; | |
254 | case BFD_RELOC_390_GOTPLT64: | |
255 | return &elf_howto_table[(int) R_390_GOTPLT64]; | |
256 | case BFD_RELOC_390_GOTPLTENT: | |
257 | return &elf_howto_table[(int) R_390_GOTPLTENT]; | |
258 | case BFD_RELOC_390_PLTOFF16: | |
259 | return &elf_howto_table[(int) R_390_PLTOFF16]; | |
260 | case BFD_RELOC_390_PLTOFF32: | |
261 | return &elf_howto_table[(int) R_390_PLTOFF32]; | |
262 | case BFD_RELOC_390_PLTOFF64: | |
263 | return &elf_howto_table[(int) R_390_PLTOFF64]; | |
264 | case BFD_RELOC_VTABLE_INHERIT: | |
265 | return &elf64_s390_vtinherit_howto; | |
266 | case BFD_RELOC_VTABLE_ENTRY: | |
267 | return &elf64_s390_vtentry_howto; | |
0451c93c MS |
268 | default: |
269 | break; | |
270 | } | |
a85d7ed0 NC |
271 | return 0; |
272 | } | |
273 | ||
274 | /* We need to use ELF64_R_TYPE so we have our own copy of this function, | |
275 | and elf64-s390.c has its own copy. */ | |
276 | ||
277 | static void | |
278 | elf_s390_info_to_howto (abfd, cache_ptr, dst) | |
279 | bfd *abfd ATTRIBUTE_UNUSED; | |
280 | arelent *cache_ptr; | |
281 | Elf_Internal_Rela *dst; | |
282 | { | |
283 | switch (ELF64_R_TYPE(dst->r_info)) | |
284 | { | |
285 | case R_390_GNU_VTINHERIT: | |
286 | cache_ptr->howto = &elf64_s390_vtinherit_howto; | |
287 | break; | |
288 | ||
289 | case R_390_GNU_VTENTRY: | |
290 | cache_ptr->howto = &elf64_s390_vtentry_howto; | |
291 | break; | |
292 | ||
293 | default: | |
294 | BFD_ASSERT (ELF64_R_TYPE(dst->r_info) < (unsigned int) R_390_max); | |
295 | cache_ptr->howto = &elf_howto_table[ELF64_R_TYPE(dst->r_info)]; | |
99c79b2e | 296 | } |
a85d7ed0 NC |
297 | } |
298 | ||
b34976b6 | 299 | static bfd_boolean |
a85d7ed0 NC |
300 | elf_s390_is_local_label_name (abfd, name) |
301 | bfd *abfd; | |
302 | const char *name; | |
303 | { | |
304 | if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L')) | |
b34976b6 | 305 | return TRUE; |
a85d7ed0 NC |
306 | |
307 | return _bfd_elf_is_local_label_name (abfd, name); | |
308 | } | |
309 | ||
310 | /* Functions for the 390 ELF linker. */ | |
311 | ||
312 | /* The name of the dynamic interpreter. This is put in the .interp | |
313 | section. */ | |
314 | ||
315 | #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1" | |
316 | ||
a85d7ed0 NC |
317 | /* The size in bytes of the first entry in the procedure linkage table. */ |
318 | #define PLT_FIRST_ENTRY_SIZE 32 | |
319 | /* The size in bytes of an entry in the procedure linkage table. */ | |
99c79b2e | 320 | #define PLT_ENTRY_SIZE 32 |
a85d7ed0 NC |
321 | |
322 | #define GOT_ENTRY_SIZE 8 | |
323 | ||
324 | /* The first three entries in a procedure linkage table are reserved, | |
325 | and the initial contents are unimportant (we zero them out). | |
326 | Subsequent entries look like this. See the SVR4 ABI 386 | |
327 | supplement to see how this works. */ | |
328 | ||
329 | /* For the s390, simple addr offset can only be 0 - 4096. | |
330 | To use the full 16777216 TB address space, several instructions | |
331 | are needed to load an address in a register and execute | |
332 | a branch( or just saving the address) | |
333 | ||
99c79b2e | 334 | Furthermore, only r 0 and 1 are free to use!!! */ |
a85d7ed0 NC |
335 | |
336 | /* The first 3 words in the GOT are then reserved. | |
337 | Word 0 is the address of the dynamic table. | |
338 | Word 1 is a pointer to a structure describing the object | |
339 | Word 2 is used to point to the loader entry address. | |
340 | ||
341 | The code for PLT entries looks like this: | |
342 | ||
343 | The GOT holds the address in the PLT to be executed. | |
344 | The loader then gets: | |
345 | 24(15) = Pointer to the structure describing the object. | |
99c79b2e | 346 | 28(15) = Offset in symbol table |
a85d7ed0 NC |
347 | The loader must then find the module where the function is |
348 | and insert the address in the GOT. | |
349 | ||
350 | PLT1: LARL 1,<fn>@GOTENT # 6 bytes Load address of GOT entry in r1 | |
351 | LG 1,0(1) # 6 bytes Load address from GOT in r1 | |
352 | BCR 15,1 # 2 bytes Jump to address | |
353 | RET1: BASR 1,0 # 2 bytes Return from GOT 1st time | |
354 | LGF 1,12(1) # 6 bytes Load offset in symbl table in r1 | |
355 | BRCL 15,-x # 6 bytes Jump to start of PLT | |
356 | .long ? # 4 bytes offset into symbol table | |
357 | ||
358 | Total = 32 bytes per PLT entry | |
359 | Fixup at offset 2: relative address to GOT entry | |
360 | Fixup at offset 22: relative branch to PLT0 | |
361 | Fixup at offset 28: 32 bit offset into symbol table | |
362 | ||
363 | A 32 bit offset into the symbol table is enough. It allows for symbol | |
364 | tables up to a size of 2 gigabyte. A single dynamic object (the main | |
365 | program, any shared library) is limited to 4GB in size and I want to see | |
366 | the program that manages to have a symbol table of more than 2 GB with a | |
367 | total size of at max 4 GB. */ | |
368 | ||
dc810e39 AM |
369 | #define PLT_ENTRY_WORD0 (bfd_vma) 0xc0100000 |
370 | #define PLT_ENTRY_WORD1 (bfd_vma) 0x0000e310 | |
371 | #define PLT_ENTRY_WORD2 (bfd_vma) 0x10000004 | |
372 | #define PLT_ENTRY_WORD3 (bfd_vma) 0x07f10d10 | |
373 | #define PLT_ENTRY_WORD4 (bfd_vma) 0xe310100c | |
374 | #define PLT_ENTRY_WORD5 (bfd_vma) 0x0014c0f4 | |
375 | #define PLT_ENTRY_WORD6 (bfd_vma) 0x00000000 | |
376 | #define PLT_ENTRY_WORD7 (bfd_vma) 0x00000000 | |
a85d7ed0 NC |
377 | |
378 | /* The first PLT entry pushes the offset into the symbol table | |
379 | from R1 onto the stack at 8(15) and the loader object info | |
380 | at 12(15), loads the loader address in R1 and jumps to it. */ | |
381 | ||
382 | /* The first entry in the PLT: | |
383 | ||
384 | PLT0: | |
385 | STG 1,56(15) # r1 contains the offset into the symbol table | |
386 | LARL 1,_GLOBAL_OFFSET_TABLE # load address of global offset table | |
387 | MVC 48(8,15),8(1) # move loader ino (object struct address) to stack | |
388 | LG 1,16(1) # get entry address of loader | |
389 | BCR 15,1 # jump to loader | |
390 | ||
391 | Fixup at offset 8: relative address to start of GOT. */ | |
392 | ||
dc810e39 AM |
393 | #define PLT_FIRST_ENTRY_WORD0 (bfd_vma) 0xe310f038 |
394 | #define PLT_FIRST_ENTRY_WORD1 (bfd_vma) 0x0024c010 | |
395 | #define PLT_FIRST_ENTRY_WORD2 (bfd_vma) 0x00000000 | |
396 | #define PLT_FIRST_ENTRY_WORD3 (bfd_vma) 0xd207f030 | |
397 | #define PLT_FIRST_ENTRY_WORD4 (bfd_vma) 0x1008e310 | |
398 | #define PLT_FIRST_ENTRY_WORD5 (bfd_vma) 0x10100004 | |
399 | #define PLT_FIRST_ENTRY_WORD6 (bfd_vma) 0x07f10700 | |
400 | #define PLT_FIRST_ENTRY_WORD7 (bfd_vma) 0x07000700 | |
a85d7ed0 NC |
401 | |
402 | /* The s390 linker needs to keep track of the number of relocs that it | |
0451c93c MS |
403 | decides to copy as dynamic relocs in check_relocs for each symbol. |
404 | This is so that it can later discard them if they are found to be | |
405 | unnecessary. We store the information in a field extending the | |
406 | regular ELF linker hash table. */ | |
a85d7ed0 | 407 | |
0451c93c | 408 | struct elf_s390_dyn_relocs |
a85d7ed0 | 409 | { |
0451c93c MS |
410 | struct elf_s390_dyn_relocs *next; |
411 | ||
412 | /* The input section of the reloc. */ | |
413 | asection *sec; | |
414 | ||
415 | /* Total number of relocs copied for the input section. */ | |
a85d7ed0 | 416 | bfd_size_type count; |
0451c93c MS |
417 | |
418 | /* Number of pc-relative relocs copied for the input section. */ | |
419 | bfd_size_type pc_count; | |
a85d7ed0 NC |
420 | }; |
421 | ||
422 | /* s390 ELF linker hash entry. */ | |
423 | ||
424 | struct elf_s390_link_hash_entry | |
425 | { | |
0451c93c | 426 | struct elf_link_hash_entry elf; |
a85d7ed0 | 427 | |
0451c93c MS |
428 | /* Track dynamic relocs copied for this symbol. */ |
429 | struct elf_s390_dyn_relocs *dyn_relocs; | |
5236c819 MS |
430 | |
431 | /* Number of GOTPLT references for a function. */ | |
432 | bfd_signed_vma gotplt_refcount; | |
a85d7ed0 NC |
433 | }; |
434 | ||
435 | /* s390 ELF linker hash table. */ | |
436 | ||
437 | struct elf_s390_link_hash_table | |
438 | { | |
0451c93c | 439 | struct elf_link_hash_table elf; |
a85d7ed0 | 440 | |
0451c93c MS |
441 | /* Short-cuts to get to dynamic linker sections. */ |
442 | asection *sgot; | |
443 | asection *sgotplt; | |
444 | asection *srelgot; | |
445 | asection *splt; | |
446 | asection *srelplt; | |
447 | asection *sdynbss; | |
448 | asection *srelbss; | |
ec338859 AM |
449 | |
450 | /* Small local sym to section mapping cache. */ | |
451 | struct sym_sec_cache sym_sec; | |
0451c93c | 452 | }; |
a85d7ed0 NC |
453 | |
454 | /* Get the s390 ELF linker hash table from a link_info structure. */ | |
455 | ||
456 | #define elf_s390_hash_table(p) \ | |
457 | ((struct elf_s390_link_hash_table *) ((p)->hash)) | |
458 | ||
459 | /* Create an entry in an s390 ELF linker hash table. */ | |
460 | ||
461 | static struct bfd_hash_entry * | |
0451c93c | 462 | link_hash_newfunc (entry, table, string) |
a85d7ed0 NC |
463 | struct bfd_hash_entry *entry; |
464 | struct bfd_hash_table *table; | |
465 | const char *string; | |
466 | { | |
a85d7ed0 NC |
467 | /* Allocate the structure if it has not already been allocated by a |
468 | subclass. */ | |
0451c93c MS |
469 | if (entry == NULL) |
470 | { | |
471 | entry = bfd_hash_allocate (table, | |
472 | sizeof (struct elf_s390_link_hash_entry)); | |
473 | if (entry == NULL) | |
474 | return entry; | |
475 | } | |
a85d7ed0 NC |
476 | |
477 | /* Call the allocation method of the superclass. */ | |
0451c93c MS |
478 | entry = _bfd_elf_link_hash_newfunc (entry, table, string); |
479 | if (entry != NULL) | |
a85d7ed0 | 480 | { |
0451c93c MS |
481 | struct elf_s390_link_hash_entry *eh; |
482 | ||
483 | eh = (struct elf_s390_link_hash_entry *) entry; | |
484 | eh->dyn_relocs = NULL; | |
5236c819 | 485 | eh->gotplt_refcount = 0; |
a85d7ed0 NC |
486 | } |
487 | ||
0451c93c | 488 | return entry; |
a85d7ed0 NC |
489 | } |
490 | ||
491 | /* Create an s390 ELF linker hash table. */ | |
492 | ||
493 | static struct bfd_link_hash_table * | |
494 | elf_s390_link_hash_table_create (abfd) | |
495 | bfd *abfd; | |
496 | { | |
497 | struct elf_s390_link_hash_table *ret; | |
dc810e39 | 498 | bfd_size_type amt = sizeof (struct elf_s390_link_hash_table); |
a85d7ed0 | 499 | |
e2d34d7d | 500 | ret = (struct elf_s390_link_hash_table *) bfd_malloc (amt); |
0451c93c | 501 | if (ret == NULL) |
a85d7ed0 NC |
502 | return NULL; |
503 | ||
0451c93c | 504 | if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc)) |
a85d7ed0 | 505 | { |
e2d34d7d | 506 | free (ret); |
a85d7ed0 NC |
507 | return NULL; |
508 | } | |
509 | ||
0451c93c MS |
510 | ret->sgot = NULL; |
511 | ret->sgotplt = NULL; | |
512 | ret->srelgot = NULL; | |
513 | ret->splt = NULL; | |
514 | ret->srelplt = NULL; | |
515 | ret->sdynbss = NULL; | |
516 | ret->srelbss = NULL; | |
ec338859 | 517 | ret->sym_sec.abfd = NULL; |
0451c93c MS |
518 | |
519 | return &ret->elf.root; | |
520 | } | |
521 | ||
522 | /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up | |
523 | shortcuts to them in our hash table. */ | |
524 | ||
b34976b6 | 525 | static bfd_boolean |
0451c93c MS |
526 | create_got_section (dynobj, info) |
527 | bfd *dynobj; | |
528 | struct bfd_link_info *info; | |
529 | { | |
530 | struct elf_s390_link_hash_table *htab; | |
531 | ||
532 | if (! _bfd_elf_create_got_section (dynobj, info)) | |
b34976b6 | 533 | return FALSE; |
0451c93c MS |
534 | |
535 | htab = elf_s390_hash_table (info); | |
536 | htab->sgot = bfd_get_section_by_name (dynobj, ".got"); | |
537 | htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); | |
538 | if (!htab->sgot || !htab->sgotplt) | |
539 | abort (); | |
540 | ||
541 | htab->srelgot = bfd_make_section (dynobj, ".rela.got"); | |
542 | if (htab->srelgot == NULL | |
543 | || ! bfd_set_section_flags (dynobj, htab->srelgot, | |
544 | (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
545 | | SEC_IN_MEMORY | SEC_LINKER_CREATED | |
546 | | SEC_READONLY)) | |
99881371 | 547 | || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3)) |
b34976b6 AM |
548 | return FALSE; |
549 | return TRUE; | |
0451c93c MS |
550 | } |
551 | ||
552 | /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and | |
553 | .rela.bss sections in DYNOBJ, and set up shortcuts to them in our | |
554 | hash table. */ | |
555 | ||
b34976b6 | 556 | static bfd_boolean |
0451c93c MS |
557 | elf_s390_create_dynamic_sections (dynobj, info) |
558 | bfd *dynobj; | |
559 | struct bfd_link_info *info; | |
560 | { | |
561 | struct elf_s390_link_hash_table *htab; | |
562 | ||
563 | htab = elf_s390_hash_table (info); | |
564 | if (!htab->sgot && !create_got_section (dynobj, info)) | |
b34976b6 | 565 | return FALSE; |
0451c93c MS |
566 | |
567 | if (!_bfd_elf_create_dynamic_sections (dynobj, info)) | |
b34976b6 | 568 | return FALSE; |
0451c93c MS |
569 | |
570 | htab->splt = bfd_get_section_by_name (dynobj, ".plt"); | |
571 | htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
572 | htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss"); | |
573 | if (!info->shared) | |
574 | htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss"); | |
575 | ||
576 | if (!htab->splt || !htab->srelplt || !htab->sdynbss | |
577 | || (!info->shared && !htab->srelbss)) | |
578 | abort (); | |
579 | ||
b34976b6 | 580 | return TRUE; |
a85d7ed0 NC |
581 | } |
582 | ||
0451c93c MS |
583 | /* Copy the extra info we tack onto an elf_link_hash_entry. */ |
584 | ||
585 | static void | |
b48fa14c AM |
586 | elf_s390_copy_indirect_symbol (bed, dir, ind) |
587 | struct elf_backend_data *bed; | |
0451c93c MS |
588 | struct elf_link_hash_entry *dir, *ind; |
589 | { | |
590 | struct elf_s390_link_hash_entry *edir, *eind; | |
591 | ||
592 | edir = (struct elf_s390_link_hash_entry *) dir; | |
593 | eind = (struct elf_s390_link_hash_entry *) ind; | |
594 | ||
595 | if (eind->dyn_relocs != NULL) | |
596 | { | |
597 | if (edir->dyn_relocs != NULL) | |
598 | { | |
599 | struct elf_s390_dyn_relocs **pp; | |
600 | struct elf_s390_dyn_relocs *p; | |
601 | ||
602 | if (ind->root.type == bfd_link_hash_indirect) | |
603 | abort (); | |
604 | ||
605 | /* Add reloc counts against the weak sym to the strong sym | |
606 | list. Merge any entries against the same section. */ | |
607 | for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) | |
608 | { | |
609 | struct elf_s390_dyn_relocs *q; | |
610 | ||
611 | for (q = edir->dyn_relocs; q != NULL; q = q->next) | |
612 | if (q->sec == p->sec) | |
613 | { | |
614 | q->pc_count += p->pc_count; | |
615 | q->count += p->count; | |
616 | *pp = p->next; | |
617 | break; | |
618 | } | |
619 | if (q == NULL) | |
620 | pp = &p->next; | |
621 | } | |
622 | *pp = edir->dyn_relocs; | |
623 | } | |
624 | ||
625 | edir->dyn_relocs = eind->dyn_relocs; | |
626 | eind->dyn_relocs = NULL; | |
627 | } | |
628 | ||
b48fa14c | 629 | _bfd_elf_link_hash_copy_indirect (bed, dir, ind); |
0451c93c | 630 | } |
a85d7ed0 NC |
631 | |
632 | /* Look through the relocs for a section during the first phase, and | |
633 | allocate space in the global offset table or procedure linkage | |
634 | table. */ | |
635 | ||
b34976b6 | 636 | static bfd_boolean |
a85d7ed0 NC |
637 | elf_s390_check_relocs (abfd, info, sec, relocs) |
638 | bfd *abfd; | |
639 | struct bfd_link_info *info; | |
640 | asection *sec; | |
641 | const Elf_Internal_Rela *relocs; | |
642 | { | |
0451c93c | 643 | struct elf_s390_link_hash_table *htab; |
a85d7ed0 NC |
644 | Elf_Internal_Shdr *symtab_hdr; |
645 | struct elf_link_hash_entry **sym_hashes; | |
a85d7ed0 NC |
646 | const Elf_Internal_Rela *rel; |
647 | const Elf_Internal_Rela *rel_end; | |
a85d7ed0 | 648 | asection *sreloc; |
5236c819 | 649 | bfd_signed_vma *local_got_refcounts; |
a85d7ed0 NC |
650 | |
651 | if (info->relocateable) | |
b34976b6 | 652 | return TRUE; |
a85d7ed0 | 653 | |
0451c93c | 654 | htab = elf_s390_hash_table (info); |
a85d7ed0 NC |
655 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
656 | sym_hashes = elf_sym_hashes (abfd); | |
5236c819 | 657 | local_got_refcounts = elf_local_got_refcounts (abfd); |
a85d7ed0 | 658 | |
a85d7ed0 NC |
659 | sreloc = NULL; |
660 | ||
661 | rel_end = relocs + sec->reloc_count; | |
662 | for (rel = relocs; rel < rel_end; rel++) | |
663 | { | |
664 | unsigned long r_symndx; | |
665 | struct elf_link_hash_entry *h; | |
666 | ||
667 | r_symndx = ELF64_R_SYM (rel->r_info); | |
668 | ||
0451c93c MS |
669 | if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) |
670 | { | |
671 | (*_bfd_error_handler) (_("%s: bad symbol index: %d"), | |
672 | bfd_archive_filename (abfd), | |
673 | r_symndx); | |
b34976b6 | 674 | return FALSE; |
0451c93c MS |
675 | } |
676 | ||
a85d7ed0 NC |
677 | if (r_symndx < symtab_hdr->sh_info) |
678 | h = NULL; | |
679 | else | |
99c79b2e | 680 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
a85d7ed0 | 681 | |
5236c819 MS |
682 | /* Create got section and local_got_refcounts array if they |
683 | are needed. */ | |
a85d7ed0 NC |
684 | switch (ELF64_R_TYPE (rel->r_info)) |
685 | { | |
686 | case R_390_GOT12: | |
947216bf | 687 | case R_390_GOT16: |
a85d7ed0 NC |
688 | case R_390_GOT32: |
689 | case R_390_GOT64: | |
690 | case R_390_GOTENT: | |
5236c819 MS |
691 | case R_390_GOTPLT12: |
692 | case R_390_GOTPLT16: | |
693 | case R_390_GOTPLT32: | |
694 | case R_390_GOTPLT64: | |
695 | case R_390_GOTPLTENT: | |
696 | if (h == NULL | |
697 | && local_got_refcounts == NULL) | |
a85d7ed0 | 698 | { |
5236c819 | 699 | bfd_size_type size; |
0451c93c | 700 | |
5236c819 MS |
701 | size = symtab_hdr->sh_info; |
702 | size *= sizeof (bfd_signed_vma); | |
703 | local_got_refcounts = ((bfd_signed_vma *) | |
704 | bfd_zalloc (abfd, size)); | |
a85d7ed0 | 705 | if (local_got_refcounts == NULL) |
5236c819 MS |
706 | return FALSE; |
707 | elf_local_got_refcounts (abfd) = local_got_refcounts; | |
a85d7ed0 | 708 | } |
5236c819 MS |
709 | /* Fall through. */ |
710 | case R_390_GOTOFF16: | |
711 | case R_390_GOTOFF32: | |
712 | case R_390_GOTOFF64: | |
0451c93c MS |
713 | case R_390_GOTPC: |
714 | case R_390_GOTPCDBL: | |
715 | if (htab->sgot == NULL) | |
716 | { | |
717 | if (htab->elf.dynobj == NULL) | |
718 | htab->elf.dynobj = abfd; | |
719 | if (!create_got_section (htab->elf.dynobj, info)) | |
b34976b6 | 720 | return FALSE; |
0451c93c | 721 | } |
5236c819 MS |
722 | } |
723 | ||
724 | switch (ELF64_R_TYPE (rel->r_info)) | |
725 | { | |
726 | case R_390_GOT12: | |
727 | case R_390_GOT16: | |
728 | case R_390_GOT32: | |
729 | case R_390_GOT64: | |
730 | case R_390_GOTENT: | |
731 | /* This symbol requires a global offset table entry. */ | |
732 | if (h != NULL) | |
733 | { | |
734 | h->got.refcount += 1; | |
735 | } | |
736 | else | |
737 | { | |
738 | local_got_refcounts[r_symndx] += 1; | |
739 | } | |
740 | /* Fall through */ | |
741 | ||
742 | case R_390_GOTOFF16: | |
743 | case R_390_GOTOFF32: | |
744 | case R_390_GOTOFF64: | |
745 | case R_390_GOTPC: | |
746 | case R_390_GOTPCDBL: | |
747 | /* Got is created, nothing to be done. */ | |
0451c93c | 748 | break; |
ec338859 | 749 | |
947216bf | 750 | case R_390_PLT16DBL: |
a85d7ed0 NC |
751 | case R_390_PLT32: |
752 | case R_390_PLT32DBL: | |
753 | case R_390_PLT64: | |
5236c819 MS |
754 | case R_390_PLTOFF16: |
755 | case R_390_PLTOFF32: | |
756 | case R_390_PLTOFF64: | |
a85d7ed0 | 757 | /* This symbol requires a procedure linkage table entry. We |
947216bf AM |
758 | actually build the entry in adjust_dynamic_symbol, |
759 | because this might be a case of linking PIC code which is | |
760 | never referenced by a dynamic object, in which case we | |
761 | don't need to generate a procedure linkage table entry | |
762 | after all. */ | |
ec338859 | 763 | |
a85d7ed0 | 764 | /* If this is a local symbol, we resolve it directly without |
947216bf | 765 | creating a procedure linkage table entry. */ |
5236c819 MS |
766 | if (h != NULL) |
767 | { | |
768 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; | |
769 | h->plt.refcount += 1; | |
770 | } | |
771 | break; | |
ec338859 | 772 | |
5236c819 MS |
773 | case R_390_GOTPLT12: |
774 | case R_390_GOTPLT16: | |
775 | case R_390_GOTPLT32: | |
776 | case R_390_GOTPLT64: | |
777 | case R_390_GOTPLTENT: | |
778 | /* This symbol requires either a procedure linkage table entry | |
779 | or an entry in the local got. We actually build the entry | |
780 | in adjust_dynamic_symbol because whether this is really a | |
781 | global reference can change and with it the fact if we have | |
782 | to create a plt entry or a local got entry. To be able to | |
783 | make a once global symbol a local one we have to keep track | |
784 | of the number of gotplt references that exist for this | |
785 | symbol. */ | |
786 | if (h != NULL) | |
787 | { | |
788 | ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount++; | |
789 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; | |
790 | h->plt.refcount += 1; | |
791 | } | |
792 | else | |
793 | { | |
794 | local_got_refcounts[r_symndx] += 1; | |
795 | } | |
a85d7ed0 | 796 | break; |
ec338859 | 797 | |
947216bf AM |
798 | case R_390_8: |
799 | case R_390_16: | |
a85d7ed0 NC |
800 | case R_390_32: |
801 | case R_390_64: | |
947216bf AM |
802 | case R_390_PC16: |
803 | case R_390_PC16DBL: | |
a85d7ed0 NC |
804 | case R_390_PC32: |
805 | case R_390_PC32DBL: | |
806 | case R_390_PC64: | |
0451c93c MS |
807 | if (h != NULL && !info->shared) |
808 | { | |
809 | /* If this reloc is in a read-only section, we might | |
810 | need a copy reloc. We can't check reliably at this | |
811 | stage whether the section is read-only, as input | |
812 | sections have not yet been mapped to output sections. | |
813 | Tentatively set the flag for now, and correct in | |
814 | adjust_dynamic_symbol. */ | |
815 | h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF; | |
ec338859 | 816 | |
0451c93c MS |
817 | /* We may need a .plt entry if the function this reloc |
818 | refers to is in a shared lib. */ | |
819 | h->plt.refcount += 1; | |
820 | } | |
ec338859 | 821 | |
a85d7ed0 | 822 | /* If we are creating a shared library, and this is a reloc |
0451c93c MS |
823 | against a global symbol, or a non PC relative reloc |
824 | against a local symbol, then we need to copy the reloc | |
825 | into the shared library. However, if we are linking with | |
826 | -Bsymbolic, we do not need to copy a reloc against a | |
827 | global symbol which is defined in an object we are | |
828 | including in the link (i.e., DEF_REGULAR is set). At | |
829 | this point we have not seen all the input files, so it is | |
830 | possible that DEF_REGULAR is not set now but will be set | |
831 | later (it is never cleared). In case of a weak definition, | |
832 | DEF_REGULAR may be cleared later by a strong definition in | |
833 | a shared library. We account for that possibility below by | |
834 | storing information in the relocs_copied field of the hash | |
835 | table entry. A similar situation occurs when creating | |
836 | shared libraries and symbol visibility changes render the | |
837 | symbol local. | |
838 | ||
839 | If on the other hand, we are creating an executable, we | |
840 | may need to keep relocations for symbols satisfied by a | |
841 | dynamic library if we manage to avoid copy relocs for the | |
842 | symbol. */ | |
843 | if ((info->shared | |
844 | && (sec->flags & SEC_ALLOC) != 0 | |
845 | && ((ELF64_R_TYPE (rel->r_info) != R_390_PC16 | |
846 | && ELF64_R_TYPE (rel->r_info) != R_390_PC16DBL | |
847 | && ELF64_R_TYPE (rel->r_info) != R_390_PC32 | |
848 | && ELF64_R_TYPE (rel->r_info) != R_390_PC32DBL | |
849 | && ELF64_R_TYPE (rel->r_info) != R_390_PC64) | |
850 | || (h != NULL | |
851 | && (! info->symbolic | |
852 | || h->root.type == bfd_link_hash_defweak | |
853 | || (h->elf_link_hash_flags | |
854 | & ELF_LINK_HASH_DEF_REGULAR) == 0)))) | |
855 | || (!info->shared | |
856 | && (sec->flags & SEC_ALLOC) != 0 | |
857 | && h != NULL | |
858 | && (h->root.type == bfd_link_hash_defweak | |
859 | || (h->elf_link_hash_flags | |
860 | & ELF_LINK_HASH_DEF_REGULAR) == 0))) | |
a85d7ed0 | 861 | { |
ec338859 AM |
862 | struct elf_s390_dyn_relocs *p; |
863 | struct elf_s390_dyn_relocs **head; | |
864 | ||
0451c93c MS |
865 | /* We must copy these reloc types into the output file. |
866 | Create a reloc section in dynobj and make room for | |
867 | this reloc. */ | |
a85d7ed0 NC |
868 | if (sreloc == NULL) |
869 | { | |
870 | const char *name; | |
0451c93c | 871 | bfd *dynobj; |
ec338859 | 872 | |
a85d7ed0 NC |
873 | name = (bfd_elf_string_from_elf_section |
874 | (abfd, | |
875 | elf_elfheader (abfd)->e_shstrndx, | |
876 | elf_section_data (sec)->rel_hdr.sh_name)); | |
877 | if (name == NULL) | |
b34976b6 | 878 | return FALSE; |
ec338859 | 879 | |
0451c93c MS |
880 | if (strncmp (name, ".rela", 5) != 0 |
881 | || strcmp (bfd_get_section_name (abfd, sec), | |
882 | name + 5) != 0) | |
883 | { | |
884 | (*_bfd_error_handler) | |
885 | (_("%s: bad relocation section name `%s\'"), | |
886 | bfd_archive_filename (abfd), name); | |
887 | } | |
ec338859 | 888 | |
0451c93c MS |
889 | if (htab->elf.dynobj == NULL) |
890 | htab->elf.dynobj = abfd; | |
a85d7ed0 | 891 | |
0451c93c | 892 | dynobj = htab->elf.dynobj; |
a85d7ed0 NC |
893 | sreloc = bfd_get_section_by_name (dynobj, name); |
894 | if (sreloc == NULL) | |
895 | { | |
896 | flagword flags; | |
ec338859 | 897 | |
a85d7ed0 NC |
898 | sreloc = bfd_make_section (dynobj, name); |
899 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | |
900 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
901 | if ((sec->flags & SEC_ALLOC) != 0) | |
902 | flags |= SEC_ALLOC | SEC_LOAD; | |
903 | if (sreloc == NULL | |
904 | || ! bfd_set_section_flags (dynobj, sreloc, flags) | |
99881371 | 905 | || ! bfd_set_section_alignment (dynobj, sreloc, 3)) |
b34976b6 | 906 | return FALSE; |
a85d7ed0 | 907 | } |
0451c93c | 908 | elf_section_data (sec)->sreloc = sreloc; |
a85d7ed0 | 909 | } |
ec338859 | 910 | |
0451c93c MS |
911 | /* If this is a global symbol, we count the number of |
912 | relocations we need for this symbol. */ | |
913 | if (h != NULL) | |
a85d7ed0 | 914 | { |
ec338859 | 915 | head = &((struct elf_s390_link_hash_entry *) h)->dyn_relocs; |
0451c93c MS |
916 | } |
917 | else | |
918 | { | |
ec338859 AM |
919 | /* Track dynamic relocs needed for local syms too. |
920 | We really need local syms available to do this | |
921 | easily. Oh well. */ | |
922 | ||
923 | asection *s; | |
924 | s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, | |
925 | sec, r_symndx); | |
926 | if (s == NULL) | |
b34976b6 | 927 | return FALSE; |
ec338859 AM |
928 | |
929 | head = ((struct elf_s390_dyn_relocs **) | |
930 | &elf_section_data (s)->local_dynrel); | |
931 | } | |
932 | ||
933 | p = *head; | |
934 | if (p == NULL || p->sec != sec) | |
935 | { | |
936 | bfd_size_type amt = sizeof *p; | |
937 | p = ((struct elf_s390_dyn_relocs *) | |
938 | bfd_alloc (htab->elf.dynobj, amt)); | |
939 | if (p == NULL) | |
b34976b6 | 940 | return FALSE; |
ec338859 AM |
941 | p->next = *head; |
942 | *head = p; | |
943 | p->sec = sec; | |
944 | p->count = 0; | |
945 | p->pc_count = 0; | |
a85d7ed0 | 946 | } |
ec338859 AM |
947 | |
948 | p->count += 1; | |
949 | if (ELF64_R_TYPE (rel->r_info) == R_390_PC16 | |
950 | || ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL | |
951 | || ELF64_R_TYPE (rel->r_info) == R_390_PC32 | |
952 | || ELF64_R_TYPE (rel->r_info) == R_390_PC32DBL | |
953 | || ELF64_R_TYPE (rel->r_info) == R_390_PC64) | |
954 | p->pc_count += 1; | |
a85d7ed0 | 955 | } |
a85d7ed0 | 956 | break; |
ec338859 | 957 | |
a85d7ed0 NC |
958 | /* This relocation describes the C++ object vtable hierarchy. |
959 | Reconstruct it for later use during GC. */ | |
947216bf AM |
960 | case R_390_GNU_VTINHERIT: |
961 | if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) | |
b34976b6 | 962 | return FALSE; |
947216bf | 963 | break; |
ec338859 | 964 | |
a85d7ed0 NC |
965 | /* This relocation describes which C++ vtable entries are actually |
966 | used. Record for later use during GC. */ | |
947216bf AM |
967 | case R_390_GNU_VTENTRY: |
968 | if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend)) | |
b34976b6 | 969 | return FALSE; |
947216bf | 970 | break; |
ec338859 | 971 | |
a85d7ed0 NC |
972 | default: |
973 | break; | |
974 | } | |
975 | } | |
976 | ||
b34976b6 | 977 | return TRUE; |
a85d7ed0 NC |
978 | } |
979 | ||
980 | /* Return the section that should be marked against GC for a given | |
981 | relocation. */ | |
982 | ||
983 | static asection * | |
1e2f5b6e AM |
984 | elf_s390_gc_mark_hook (sec, info, rel, h, sym) |
985 | asection *sec; | |
a85d7ed0 NC |
986 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
987 | Elf_Internal_Rela *rel; | |
988 | struct elf_link_hash_entry *h; | |
989 | Elf_Internal_Sym *sym; | |
990 | { | |
991 | if (h != NULL) | |
992 | { | |
993 | switch (ELF64_R_TYPE (rel->r_info)) | |
994 | { | |
995 | case R_390_GNU_VTINHERIT: | |
996 | case R_390_GNU_VTENTRY: | |
997 | break; | |
998 | ||
999 | default: | |
1000 | switch (h->root.type) | |
1001 | { | |
1002 | case bfd_link_hash_defined: | |
1003 | case bfd_link_hash_defweak: | |
1004 | return h->root.u.def.section; | |
1005 | ||
1006 | case bfd_link_hash_common: | |
1007 | return h->root.u.c.p->section; | |
1008 | ||
1009 | default: | |
1010 | break; | |
1011 | } | |
1012 | } | |
1013 | } | |
1014 | else | |
1e2f5b6e | 1015 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); |
a85d7ed0 NC |
1016 | |
1017 | return NULL; | |
1018 | } | |
1019 | ||
1020 | /* Update the got entry reference counts for the section being removed. */ | |
1021 | ||
b34976b6 | 1022 | static bfd_boolean |
a85d7ed0 | 1023 | elf_s390_gc_sweep_hook (abfd, info, sec, relocs) |
0451c93c MS |
1024 | bfd *abfd; |
1025 | struct bfd_link_info *info; | |
1026 | asection *sec; | |
1027 | const Elf_Internal_Rela *relocs; | |
a85d7ed0 NC |
1028 | { |
1029 | Elf_Internal_Shdr *symtab_hdr; | |
1030 | struct elf_link_hash_entry **sym_hashes; | |
1031 | bfd_signed_vma *local_got_refcounts; | |
1032 | const Elf_Internal_Rela *rel, *relend; | |
1033 | unsigned long r_symndx; | |
1034 | struct elf_link_hash_entry *h; | |
a85d7ed0 | 1035 | |
ec338859 | 1036 | elf_section_data (sec)->local_dynrel = NULL; |
a85d7ed0 | 1037 | |
0451c93c MS |
1038 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1039 | sym_hashes = elf_sym_hashes (abfd); | |
1040 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
a85d7ed0 NC |
1041 | |
1042 | relend = relocs + sec->reloc_count; | |
1043 | for (rel = relocs; rel < relend; rel++) | |
5236c819 MS |
1044 | { |
1045 | r_symndx = ELF64_R_SYM (rel->r_info); | |
1046 | ||
1047 | if (r_symndx < symtab_hdr->sh_info) | |
1048 | h = NULL; | |
1049 | else | |
1050 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1051 | ||
1052 | switch (ELF64_R_TYPE (rel->r_info)) | |
1053 | { | |
1054 | case R_390_GOT12: | |
1055 | case R_390_GOT16: | |
1056 | case R_390_GOT32: | |
1057 | case R_390_GOT64: | |
1058 | case R_390_GOTOFF16: | |
1059 | case R_390_GOTOFF32: | |
1060 | case R_390_GOTOFF64: | |
1061 | case R_390_GOTPC: | |
1062 | case R_390_GOTPCDBL: | |
1063 | case R_390_GOTENT: | |
1064 | if (h != NULL) | |
1065 | { | |
1066 | if (h->got.refcount > 0) | |
1067 | h->got.refcount -= 1; | |
1068 | } | |
1069 | else if (local_got_refcounts != NULL) | |
1070 | { | |
1071 | if (local_got_refcounts[r_symndx] > 0) | |
1072 | local_got_refcounts[r_symndx] -= 1; | |
1073 | } | |
1074 | break; | |
1075 | ||
1076 | case R_390_PLT16DBL: | |
1077 | case R_390_PLT32: | |
1078 | case R_390_PLT32DBL: | |
1079 | case R_390_PLT64: | |
1080 | case R_390_PLTOFF16: | |
1081 | case R_390_PLTOFF32: | |
1082 | case R_390_PLTOFF64: | |
1083 | if (h != NULL) | |
1084 | { | |
1085 | if (h->plt.refcount > 0) | |
1086 | h->plt.refcount -= 1; | |
1087 | } | |
1088 | break; | |
0451c93c | 1089 | |
5236c819 MS |
1090 | case R_390_GOTPLT12: |
1091 | case R_390_GOTPLT16: | |
1092 | case R_390_GOTPLT32: | |
1093 | case R_390_GOTPLT64: | |
1094 | case R_390_GOTPLTENT: | |
1095 | if (h != NULL) | |
1096 | { | |
1097 | if (h->plt.refcount > 0) | |
0451c93c | 1098 | { |
5236c819 MS |
1099 | ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount--; |
1100 | h->plt.refcount -= 1; | |
0451c93c | 1101 | } |
5236c819 MS |
1102 | } |
1103 | else if (local_got_refcounts != NULL) | |
1104 | { | |
1105 | if (local_got_refcounts[r_symndx] > 0) | |
1106 | local_got_refcounts[r_symndx] -= 1; | |
1107 | } | |
1108 | break; | |
a85d7ed0 | 1109 | |
5236c819 MS |
1110 | case R_390_8: |
1111 | case R_390_12: | |
1112 | case R_390_16: | |
1113 | case R_390_32: | |
1114 | case R_390_64: | |
1115 | case R_390_PC16: | |
1116 | case R_390_PC16DBL: | |
1117 | case R_390_PC32: | |
1118 | case R_390_PC32DBL: | |
1119 | case R_390_PC64: | |
1120 | if (h != NULL) | |
1121 | { | |
1122 | struct elf_s390_link_hash_entry *eh; | |
1123 | struct elf_s390_dyn_relocs **pp; | |
1124 | struct elf_s390_dyn_relocs *p; | |
1125 | ||
1126 | if (!info->shared && h->plt.refcount > 0) | |
1127 | h->plt.refcount -= 1; | |
1128 | ||
1129 | eh = (struct elf_s390_link_hash_entry *) h; | |
1130 | ||
1131 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) | |
1132 | if (p->sec == sec) | |
1133 | { | |
1134 | if (ELF64_R_TYPE (rel->r_info) == R_390_PC16 | |
1135 | || ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL | |
1136 | || ELF64_R_TYPE (rel->r_info) == R_390_PC32 | |
1137 | || ELF64_R_TYPE (rel->r_info) == R_390_PC32DBL | |
1138 | || ELF64_R_TYPE (rel->r_info) == R_390_PC64) | |
1139 | p->pc_count -= 1; | |
1140 | p->count -= 1; | |
1141 | if (p->count == 0) | |
1142 | *pp = p->next; | |
1143 | break; | |
1144 | } | |
1145 | } | |
1146 | break; | |
a85d7ed0 | 1147 | |
5236c819 MS |
1148 | default: |
1149 | break; | |
1150 | } | |
1151 | } | |
a85d7ed0 | 1152 | |
b34976b6 | 1153 | return TRUE; |
a85d7ed0 NC |
1154 | } |
1155 | ||
5236c819 MS |
1156 | /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT |
1157 | entry but we found we will not create any. Called when we find we will | |
1158 | not have any PLT for this symbol, by for example | |
1159 | elf_s390_adjust_dynamic_symbol when we're doing a proper dynamic link, | |
1160 | or elf_s390_size_dynamic_sections if no dynamic sections will be | |
1161 | created (we're only linking static objects). */ | |
1162 | ||
1163 | static void | |
1164 | elf_s390_adjust_gotplt (h) | |
1165 | struct elf_s390_link_hash_entry *h; | |
1166 | { | |
1167 | if (h->elf.root.type == bfd_link_hash_warning) | |
1168 | h = (struct elf_s390_link_hash_entry *) h->elf.root.u.i.link; | |
1169 | ||
1170 | if (h->gotplt_refcount <= 0) | |
1171 | return; | |
1172 | ||
1173 | /* We simply add the number of gotplt references to the number | |
1174 | * of got references for this symbol. */ | |
1175 | h->elf.got.refcount += h->gotplt_refcount; | |
1176 | h->gotplt_refcount = -1; | |
1177 | } | |
1178 | ||
a85d7ed0 NC |
1179 | /* Adjust a symbol defined by a dynamic object and referenced by a |
1180 | regular object. The current definition is in some section of the | |
1181 | dynamic object, but we're not including those sections. We have to | |
1182 | change the definition to something the rest of the link can | |
1183 | understand. */ | |
1184 | ||
b34976b6 | 1185 | static bfd_boolean |
a85d7ed0 NC |
1186 | elf_s390_adjust_dynamic_symbol (info, h) |
1187 | struct bfd_link_info *info; | |
1188 | struct elf_link_hash_entry *h; | |
1189 | { | |
0451c93c MS |
1190 | struct elf_s390_link_hash_table *htab; |
1191 | struct elf_s390_link_hash_entry * eh; | |
1192 | struct elf_s390_dyn_relocs *p; | |
a85d7ed0 NC |
1193 | asection *s; |
1194 | unsigned int power_of_two; | |
1195 | ||
a85d7ed0 NC |
1196 | /* If this is a function, put it in the procedure linkage table. We |
1197 | will fill in the contents of the procedure linkage table later | |
cedb70c5 | 1198 | (although we could actually do it here). */ |
a85d7ed0 NC |
1199 | if (h->type == STT_FUNC |
1200 | || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) | |
1201 | { | |
0451c93c MS |
1202 | if (h->plt.refcount <= 0 |
1203 | || (! info->shared | |
1204 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
f9cd9119 MS |
1205 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0 |
1206 | && h->root.type != bfd_link_hash_undefweak | |
1207 | && h->root.type != bfd_link_hash_undefined)) | |
a85d7ed0 NC |
1208 | { |
1209 | /* This case can occur if we saw a PLT32 reloc in an input | |
947216bf AM |
1210 | file, but the symbol was never referred to by a dynamic |
1211 | object, or if all references were garbage collected. In | |
0451c93c MS |
1212 | such a case, we don't actually need to build a procedure |
1213 | linkage table, and we can just do a PC32 reloc instead. */ | |
a85d7ed0 NC |
1214 | h->plt.offset = (bfd_vma) -1; |
1215 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
5236c819 | 1216 | elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h); |
a85d7ed0 NC |
1217 | } |
1218 | ||
b34976b6 | 1219 | return TRUE; |
a85d7ed0 | 1220 | } |
bbd7ec4a | 1221 | else |
0451c93c MS |
1222 | /* It's possible that we incorrectly decided a .plt reloc was |
1223 | needed for an R_390_PC32 reloc to a non-function sym in | |
1224 | check_relocs. We can't decide accurately between function and | |
1225 | non-function syms in check-relocs; Objects loaded later in | |
1226 | the link may change h->type. So fix it now. */ | |
bbd7ec4a | 1227 | h->plt.offset = (bfd_vma) -1; |
a85d7ed0 NC |
1228 | |
1229 | /* If this is a weak symbol, and there is a real definition, the | |
1230 | processor independent code will have arranged for us to see the | |
1231 | real definition first, and we can just use the same value. */ | |
1232 | if (h->weakdef != NULL) | |
1233 | { | |
1234 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined | |
1235 | || h->weakdef->root.type == bfd_link_hash_defweak); | |
1236 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
1237 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
b34976b6 | 1238 | return TRUE; |
a85d7ed0 NC |
1239 | } |
1240 | ||
1241 | /* This is a reference to a symbol defined by a dynamic object which | |
1242 | is not a function. */ | |
1243 | ||
1244 | /* If we are creating a shared library, we must presume that the | |
1245 | only references to the symbol are via the global offset table. | |
1246 | For such cases we need not do anything here; the relocations will | |
1247 | be handled correctly by relocate_section. */ | |
1248 | if (info->shared) | |
b34976b6 | 1249 | return TRUE; |
a85d7ed0 NC |
1250 | |
1251 | /* If there are no references to this symbol that do not use the | |
1252 | GOT, we don't need to generate a copy reloc. */ | |
1253 | if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0) | |
b34976b6 | 1254 | return TRUE; |
a85d7ed0 | 1255 | |
0451c93c MS |
1256 | /* If -z nocopyreloc was given, we won't generate them either. */ |
1257 | if (info->nocopyreloc) | |
1258 | { | |
1259 | h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF; | |
b34976b6 | 1260 | return TRUE; |
0451c93c MS |
1261 | } |
1262 | ||
1263 | eh = (struct elf_s390_link_hash_entry *) h; | |
1264 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1265 | { | |
1266 | s = p->sec->output_section; | |
1267 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
1268 | break; | |
1269 | } | |
1270 | ||
1271 | /* If we didn't find any dynamic relocs in read-only sections, then | |
1272 | we'll be keeping the dynamic relocs and avoiding the copy reloc. */ | |
1273 | if (p == NULL) | |
1274 | { | |
1275 | h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF; | |
b34976b6 | 1276 | return TRUE; |
0451c93c MS |
1277 | } |
1278 | ||
a85d7ed0 NC |
1279 | /* We must allocate the symbol in our .dynbss section, which will |
1280 | become part of the .bss section of the executable. There will be | |
1281 | an entry for this symbol in the .dynsym section. The dynamic | |
1282 | object will contain position independent code, so all references | |
1283 | from the dynamic object to this symbol will go through the global | |
1284 | offset table. The dynamic linker will use the .dynsym entry to | |
1285 | determine the address it must put in the global offset table, so | |
1286 | both the dynamic object and the regular object will refer to the | |
1287 | same memory location for the variable. */ | |
1288 | ||
0451c93c | 1289 | htab = elf_s390_hash_table (info); |
a85d7ed0 | 1290 | |
0451c93c MS |
1291 | /* We must generate a R_390_COPY reloc to tell the dynamic linker to |
1292 | copy the initial value out of the dynamic object and into the | |
1293 | runtime process image. */ | |
a85d7ed0 NC |
1294 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) |
1295 | { | |
0451c93c | 1296 | htab->srelbss->_raw_size += sizeof (Elf64_External_Rela); |
a85d7ed0 NC |
1297 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; |
1298 | } | |
1299 | ||
1300 | /* We need to figure out the alignment required for this symbol. I | |
1301 | have no idea how ELF linkers handle this. */ | |
1302 | power_of_two = bfd_log2 (h->size); | |
1303 | if (power_of_two > 3) | |
1304 | power_of_two = 3; | |
1305 | ||
1306 | /* Apply the required alignment. */ | |
0451c93c MS |
1307 | s = htab->sdynbss; |
1308 | s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two)); | |
1309 | if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s)) | |
a85d7ed0 | 1310 | { |
0451c93c | 1311 | if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two)) |
b34976b6 | 1312 | return FALSE; |
a85d7ed0 NC |
1313 | } |
1314 | ||
1315 | /* Define the symbol as being at this point in the section. */ | |
1316 | h->root.u.def.section = s; | |
1317 | h->root.u.def.value = s->_raw_size; | |
1318 | ||
1319 | /* Increment the section size to make room for the symbol. */ | |
1320 | s->_raw_size += h->size; | |
1321 | ||
b34976b6 | 1322 | return TRUE; |
a85d7ed0 NC |
1323 | } |
1324 | ||
0451c93c MS |
1325 | /* This is the condition under which elf_s390_finish_dynamic_symbol |
1326 | will be called from elflink.h. If elflink.h doesn't call our | |
1327 | finish_dynamic_symbol routine, we'll need to do something about | |
1328 | initializing any .plt and .got entries in elf_s390_relocate_section. */ | |
1329 | #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \ | |
1330 | ((DYN) \ | |
1331 | && ((INFO)->shared \ | |
1332 | || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \ | |
1333 | && ((H)->dynindx != -1 \ | |
1334 | || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)) | |
1335 | ||
1336 | /* Allocate space in .plt, .got and associated reloc sections for | |
1337 | dynamic relocs. */ | |
1338 | ||
b34976b6 | 1339 | static bfd_boolean |
0451c93c MS |
1340 | allocate_dynrelocs (h, inf) |
1341 | struct elf_link_hash_entry *h; | |
1342 | PTR inf; | |
1343 | { | |
1344 | struct bfd_link_info *info; | |
1345 | struct elf_s390_link_hash_table *htab; | |
1346 | struct elf_s390_link_hash_entry *eh; | |
1347 | struct elf_s390_dyn_relocs *p; | |
1348 | ||
e92d460e | 1349 | if (h->root.type == bfd_link_hash_indirect) |
b34976b6 | 1350 | return TRUE; |
0451c93c | 1351 | |
e92d460e | 1352 | if (h->root.type == bfd_link_hash_warning) |
5236c819 MS |
1353 | /* When warning symbols are created, they **replace** the "real" |
1354 | entry in the hash table, thus we never get to see the real | |
1355 | symbol in a hash traversal. So look at it now. */ | |
e92d460e AM |
1356 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
1357 | ||
0451c93c MS |
1358 | info = (struct bfd_link_info *) inf; |
1359 | htab = elf_s390_hash_table (info); | |
1360 | ||
1361 | if (htab->elf.dynamic_sections_created | |
1362 | && h->plt.refcount > 0) | |
1363 | { | |
1364 | /* Make sure this symbol is output as a dynamic symbol. | |
1365 | Undefined weak syms won't yet be marked as dynamic. */ | |
1366 | if (h->dynindx == -1 | |
1367 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
1368 | { | |
1369 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
b34976b6 | 1370 | return FALSE; |
0451c93c MS |
1371 | } |
1372 | ||
1373 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h)) | |
1374 | { | |
1375 | asection *s = htab->splt; | |
1376 | ||
1377 | /* If this is the first .plt entry, make room for the special | |
1378 | first entry. */ | |
1379 | if (s->_raw_size == 0) | |
1380 | s->_raw_size += PLT_FIRST_ENTRY_SIZE; | |
1381 | ||
1382 | h->plt.offset = s->_raw_size; | |
1383 | ||
1384 | /* If this symbol is not defined in a regular file, and we are | |
1385 | not generating a shared library, then set the symbol to this | |
1386 | location in the .plt. This is required to make function | |
1387 | pointers compare as equal between the normal executable and | |
1388 | the shared library. */ | |
1389 | if (! info->shared | |
1390 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1391 | { | |
1392 | h->root.u.def.section = s; | |
1393 | h->root.u.def.value = h->plt.offset; | |
1394 | } | |
ec338859 | 1395 | |
0451c93c MS |
1396 | /* Make room for this entry. */ |
1397 | s->_raw_size += PLT_ENTRY_SIZE; | |
ec338859 | 1398 | |
0451c93c MS |
1399 | /* We also need to make an entry in the .got.plt section, which |
1400 | will be placed in the .got section by the linker script. */ | |
1401 | htab->sgotplt->_raw_size += GOT_ENTRY_SIZE; | |
1402 | ||
1403 | /* We also need to make an entry in the .rela.plt section. */ | |
1404 | htab->srelplt->_raw_size += sizeof (Elf64_External_Rela); | |
1405 | } | |
1406 | else | |
1407 | { | |
1408 | h->plt.offset = (bfd_vma) -1; | |
1409 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
5236c819 | 1410 | elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h); |
0451c93c MS |
1411 | } |
1412 | } | |
1413 | else | |
1414 | { | |
1415 | h->plt.offset = (bfd_vma) -1; | |
1416 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
5236c819 | 1417 | elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h); |
0451c93c MS |
1418 | } |
1419 | ||
1420 | if (h->got.refcount > 0) | |
1421 | { | |
1422 | asection *s; | |
b34976b6 | 1423 | bfd_boolean dyn; |
0451c93c MS |
1424 | |
1425 | /* Make sure this symbol is output as a dynamic symbol. | |
1426 | Undefined weak syms won't yet be marked as dynamic. */ | |
1427 | if (h->dynindx == -1 | |
1428 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
1429 | { | |
1430 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
b34976b6 | 1431 | return FALSE; |
0451c93c MS |
1432 | } |
1433 | ||
1434 | s = htab->sgot; | |
1435 | h->got.offset = s->_raw_size; | |
1436 | s->_raw_size += GOT_ENTRY_SIZE; | |
1437 | dyn = htab->elf.dynamic_sections_created; | |
1438 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)) | |
1439 | htab->srelgot->_raw_size += sizeof (Elf64_External_Rela); | |
1440 | } | |
1441 | else | |
1442 | h->got.offset = (bfd_vma) -1; | |
1443 | ||
1444 | eh = (struct elf_s390_link_hash_entry *) h; | |
1445 | if (eh->dyn_relocs == NULL) | |
b34976b6 | 1446 | return TRUE; |
0451c93c MS |
1447 | |
1448 | /* In the shared -Bsymbolic case, discard space allocated for | |
1449 | dynamic pc-relative relocs against symbols which turn out to be | |
1450 | defined in regular objects. For the normal shared case, discard | |
1451 | space for pc-relative relocs that have become local due to symbol | |
1452 | visibility changes. */ | |
1453 | ||
1454 | if (info->shared) | |
1455 | { | |
1456 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
1457 | && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0 | |
1458 | || info->symbolic)) | |
1459 | { | |
1460 | struct elf_s390_dyn_relocs **pp; | |
1461 | ||
1462 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) | |
1463 | { | |
1464 | p->count -= p->pc_count; | |
1465 | p->pc_count = 0; | |
1466 | if (p->count == 0) | |
1467 | *pp = p->next; | |
1468 | else | |
1469 | pp = &p->next; | |
1470 | } | |
1471 | } | |
1472 | } | |
1473 | else | |
1474 | { | |
1475 | /* For the non-shared case, discard space for relocs against | |
1476 | symbols which turn out to need copy relocs or are not | |
1477 | dynamic. */ | |
1478 | ||
1479 | if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0 | |
1480 | && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1481 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1482 | || (htab->elf.dynamic_sections_created | |
1483 | && (h->root.type == bfd_link_hash_undefweak | |
1484 | || h->root.type == bfd_link_hash_undefined)))) | |
1485 | { | |
1486 | /* Make sure this symbol is output as a dynamic symbol. | |
1487 | Undefined weak syms won't yet be marked as dynamic. */ | |
1488 | if (h->dynindx == -1 | |
1489 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
1490 | { | |
1491 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
b34976b6 | 1492 | return FALSE; |
0451c93c MS |
1493 | } |
1494 | ||
1495 | /* If that succeeded, we know we'll be keeping all the | |
1496 | relocs. */ | |
1497 | if (h->dynindx != -1) | |
1498 | goto keep; | |
1499 | } | |
1500 | ||
1501 | eh->dyn_relocs = NULL; | |
1502 | ||
ec338859 | 1503 | keep: ; |
0451c93c MS |
1504 | } |
1505 | ||
1506 | /* Finally, allocate space. */ | |
1507 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1508 | { | |
1509 | asection *sreloc = elf_section_data (p->sec)->sreloc; | |
1510 | sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela); | |
1511 | } | |
1512 | ||
b34976b6 | 1513 | return TRUE; |
0451c93c MS |
1514 | } |
1515 | ||
1516 | /* Find any dynamic relocs that apply to read-only sections. */ | |
1517 | ||
b34976b6 | 1518 | static bfd_boolean |
0451c93c MS |
1519 | readonly_dynrelocs (h, inf) |
1520 | struct elf_link_hash_entry *h; | |
1521 | PTR inf; | |
1522 | { | |
1523 | struct elf_s390_link_hash_entry *eh; | |
1524 | struct elf_s390_dyn_relocs *p; | |
1525 | ||
e92d460e AM |
1526 | if (h->root.type == bfd_link_hash_warning) |
1527 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1528 | ||
0451c93c MS |
1529 | eh = (struct elf_s390_link_hash_entry *) h; |
1530 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1531 | { | |
1532 | asection *s = p->sec->output_section; | |
1533 | ||
1534 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
1535 | { | |
1536 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
1537 | ||
1538 | info->flags |= DF_TEXTREL; | |
1539 | ||
1540 | /* Not an error, just cut short the traversal. */ | |
b34976b6 | 1541 | return FALSE; |
0451c93c MS |
1542 | } |
1543 | } | |
b34976b6 | 1544 | return TRUE; |
0451c93c MS |
1545 | } |
1546 | ||
a85d7ed0 NC |
1547 | /* Set the sizes of the dynamic sections. */ |
1548 | ||
b34976b6 | 1549 | static bfd_boolean |
a85d7ed0 | 1550 | elf_s390_size_dynamic_sections (output_bfd, info) |
29c2fb7c | 1551 | bfd *output_bfd ATTRIBUTE_UNUSED; |
a85d7ed0 NC |
1552 | struct bfd_link_info *info; |
1553 | { | |
0451c93c | 1554 | struct elf_s390_link_hash_table *htab; |
a85d7ed0 NC |
1555 | bfd *dynobj; |
1556 | asection *s; | |
b34976b6 | 1557 | bfd_boolean relocs; |
0451c93c | 1558 | bfd *ibfd; |
a85d7ed0 | 1559 | |
0451c93c MS |
1560 | htab = elf_s390_hash_table (info); |
1561 | dynobj = htab->elf.dynobj; | |
1562 | if (dynobj == NULL) | |
1563 | abort (); | |
a85d7ed0 | 1564 | |
0451c93c | 1565 | if (htab->elf.dynamic_sections_created) |
a85d7ed0 NC |
1566 | { |
1567 | /* Set the contents of the .interp section to the interpreter. */ | |
1568 | if (! info->shared) | |
1569 | { | |
1570 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
0451c93c MS |
1571 | if (s == NULL) |
1572 | abort (); | |
a85d7ed0 NC |
1573 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; |
1574 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
1575 | } | |
1576 | } | |
a85d7ed0 | 1577 | |
0451c93c MS |
1578 | /* Set up .got offsets for local syms, and space for local dynamic |
1579 | relocs. */ | |
1580 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
a85d7ed0 | 1581 | { |
0451c93c MS |
1582 | bfd_signed_vma *local_got; |
1583 | bfd_signed_vma *end_local_got; | |
1584 | bfd_size_type locsymcount; | |
1585 | Elf_Internal_Shdr *symtab_hdr; | |
1586 | asection *srela; | |
a85d7ed0 | 1587 | |
0451c93c | 1588 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
a85d7ed0 NC |
1589 | continue; |
1590 | ||
0451c93c | 1591 | for (s = ibfd->sections; s != NULL; s = s->next) |
a85d7ed0 | 1592 | { |
ec338859 | 1593 | struct elf_s390_dyn_relocs *p; |
0451c93c | 1594 | |
ec338859 AM |
1595 | for (p = *((struct elf_s390_dyn_relocs **) |
1596 | &elf_section_data (s)->local_dynrel); | |
1597 | p != NULL; | |
1598 | p = p->next) | |
a85d7ed0 | 1599 | { |
ec338859 AM |
1600 | if (!bfd_is_abs_section (p->sec) |
1601 | && bfd_is_abs_section (p->sec->output_section)) | |
1602 | { | |
1603 | /* Input section has been discarded, either because | |
1604 | it is a copy of a linkonce section or due to | |
1605 | linker script /DISCARD/, so we'll be discarding | |
1606 | the relocs too. */ | |
1607 | } | |
248866a8 | 1608 | else if (p->count != 0) |
ec338859 AM |
1609 | { |
1610 | srela = elf_section_data (p->sec)->sreloc; | |
1611 | srela->_raw_size += p->count * sizeof (Elf64_External_Rela); | |
248866a8 AM |
1612 | if ((p->sec->output_section->flags & SEC_READONLY) != 0) |
1613 | info->flags |= DF_TEXTREL; | |
ec338859 | 1614 | } |
a85d7ed0 NC |
1615 | } |
1616 | } | |
0451c93c MS |
1617 | |
1618 | local_got = elf_local_got_refcounts (ibfd); | |
1619 | if (!local_got) | |
1620 | continue; | |
1621 | ||
1622 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
1623 | locsymcount = symtab_hdr->sh_info; | |
1624 | end_local_got = local_got + locsymcount; | |
1625 | s = htab->sgot; | |
1626 | srela = htab->srelgot; | |
1627 | for (; local_got < end_local_got; ++local_got) | |
a85d7ed0 | 1628 | { |
0451c93c | 1629 | if (*local_got > 0) |
a85d7ed0 | 1630 | { |
0451c93c MS |
1631 | *local_got = s->_raw_size; |
1632 | s->_raw_size += GOT_ENTRY_SIZE; | |
1633 | if (info->shared) | |
1634 | srela->_raw_size += sizeof (Elf64_External_Rela); | |
a85d7ed0 NC |
1635 | } |
1636 | else | |
0451c93c | 1637 | *local_got = (bfd_vma) -1; |
a85d7ed0 | 1638 | } |
0451c93c MS |
1639 | } |
1640 | ||
1641 | /* Allocate global sym .plt and .got entries, and space for global | |
1642 | sym dynamic relocs. */ | |
1643 | elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info); | |
1644 | ||
1645 | /* We now have determined the sizes of the various dynamic sections. | |
1646 | Allocate memory for them. */ | |
b34976b6 | 1647 | relocs = FALSE; |
0451c93c MS |
1648 | for (s = dynobj->sections; s != NULL; s = s->next) |
1649 | { | |
1650 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
1651 | continue; | |
1652 | ||
1653 | if (s == htab->splt | |
1654 | || s == htab->sgot | |
1655 | || s == htab->sgotplt) | |
1656 | { | |
1657 | /* Strip this section if we don't need it; see the | |
1658 | comment below. */ | |
1659 | } | |
1660 | else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0) | |
1661 | { | |
1662 | if (s->_raw_size != 0 && s != htab->srelplt) | |
b34976b6 | 1663 | relocs = TRUE; |
ec338859 | 1664 | |
0451c93c MS |
1665 | /* We use the reloc_count field as a counter if we need |
1666 | to copy relocs into the output file. */ | |
1667 | s->reloc_count = 0; | |
1668 | } | |
1669 | else | |
a85d7ed0 NC |
1670 | { |
1671 | /* It's not one of our sections, so don't allocate space. */ | |
1672 | continue; | |
1673 | } | |
1674 | ||
0451c93c | 1675 | if (s->_raw_size == 0) |
a85d7ed0 | 1676 | { |
0451c93c MS |
1677 | /* If we don't need this section, strip it from the |
1678 | output file. This is to handle .rela.bss and | |
1679 | .rela.plt. We must create it in | |
1680 | create_dynamic_sections, because it must be created | |
1681 | before the linker maps input sections to output | |
1682 | sections. The linker does that before | |
1683 | adjust_dynamic_symbol is called, and it is that | |
1684 | function which decides whether anything needs to go | |
1685 | into these sections. */ | |
1686 | ||
a85d7ed0 NC |
1687 | _bfd_strip_section_from_output (info, s); |
1688 | continue; | |
1689 | } | |
1690 | ||
0451c93c MS |
1691 | /* Allocate memory for the section contents. We use bfd_zalloc |
1692 | here in case unused entries are not reclaimed before the | |
1693 | section's contents are written out. This should not happen, | |
1694 | but this way if it does, we get a R_390_NONE reloc instead | |
1695 | of garbage. */ | |
1696 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size); | |
1697 | if (s->contents == NULL) | |
b34976b6 | 1698 | return FALSE; |
a85d7ed0 NC |
1699 | } |
1700 | ||
0451c93c | 1701 | if (htab->elf.dynamic_sections_created) |
a85d7ed0 NC |
1702 | { |
1703 | /* Add some entries to the .dynamic section. We fill in the | |
1704 | values later, in elf_s390_finish_dynamic_sections, but we | |
1705 | must add the entries now so that we get the correct size for | |
1706 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
1707 | dynamic linker and used by the debugger. */ | |
dc810e39 AM |
1708 | #define add_dynamic_entry(TAG, VAL) \ |
1709 | bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL)) | |
1710 | ||
a85d7ed0 NC |
1711 | if (! info->shared) |
1712 | { | |
dc810e39 | 1713 | if (!add_dynamic_entry (DT_DEBUG, 0)) |
b34976b6 | 1714 | return FALSE; |
a85d7ed0 NC |
1715 | } |
1716 | ||
0451c93c | 1717 | if (htab->splt->_raw_size != 0) |
a85d7ed0 | 1718 | { |
dc810e39 AM |
1719 | if (!add_dynamic_entry (DT_PLTGOT, 0) |
1720 | || !add_dynamic_entry (DT_PLTRELSZ, 0) | |
1721 | || !add_dynamic_entry (DT_PLTREL, DT_RELA) | |
1722 | || !add_dynamic_entry (DT_JMPREL, 0)) | |
b34976b6 | 1723 | return FALSE; |
a85d7ed0 NC |
1724 | } |
1725 | ||
1726 | if (relocs) | |
947216bf AM |
1727 | { |
1728 | if (!add_dynamic_entry (DT_RELA, 0) | |
1729 | || !add_dynamic_entry (DT_RELASZ, 0) | |
1730 | || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela))) | |
b34976b6 | 1731 | return FALSE; |
ec338859 | 1732 | |
0451c93c MS |
1733 | /* If any dynamic relocs apply to a read-only section, |
1734 | then we need a DT_TEXTREL entry. */ | |
248866a8 AM |
1735 | if ((info->flags & DF_TEXTREL) == 0) |
1736 | elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, | |
1737 | (PTR) info); | |
ec338859 | 1738 | |
0451c93c MS |
1739 | if ((info->flags & DF_TEXTREL) != 0) |
1740 | { | |
1741 | if (!add_dynamic_entry (DT_TEXTREL, 0)) | |
b34976b6 | 1742 | return FALSE; |
0451c93c | 1743 | } |
a85d7ed0 NC |
1744 | } |
1745 | } | |
dc810e39 | 1746 | #undef add_dynamic_entry |
a85d7ed0 | 1747 | |
b34976b6 | 1748 | return TRUE; |
a85d7ed0 NC |
1749 | } |
1750 | ||
a85d7ed0 NC |
1751 | /* Relocate a 390 ELF section. */ |
1752 | ||
b34976b6 | 1753 | static bfd_boolean |
a85d7ed0 NC |
1754 | elf_s390_relocate_section (output_bfd, info, input_bfd, input_section, |
1755 | contents, relocs, local_syms, local_sections) | |
1756 | bfd *output_bfd; | |
1757 | struct bfd_link_info *info; | |
1758 | bfd *input_bfd; | |
1759 | asection *input_section; | |
1760 | bfd_byte *contents; | |
1761 | Elf_Internal_Rela *relocs; | |
1762 | Elf_Internal_Sym *local_syms; | |
1763 | asection **local_sections; | |
1764 | { | |
0451c93c | 1765 | struct elf_s390_link_hash_table *htab; |
a85d7ed0 NC |
1766 | Elf_Internal_Shdr *symtab_hdr; |
1767 | struct elf_link_hash_entry **sym_hashes; | |
1768 | bfd_vma *local_got_offsets; | |
a85d7ed0 NC |
1769 | Elf_Internal_Rela *rel; |
1770 | Elf_Internal_Rela *relend; | |
1771 | ||
b491616a | 1772 | if (info->relocateable) |
b34976b6 | 1773 | return TRUE; |
b491616a | 1774 | |
0451c93c | 1775 | htab = elf_s390_hash_table (info); |
a85d7ed0 NC |
1776 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
1777 | sym_hashes = elf_sym_hashes (input_bfd); | |
1778 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
1779 | ||
a85d7ed0 NC |
1780 | rel = relocs; |
1781 | relend = relocs + input_section->reloc_count; | |
1782 | for (; rel < relend; rel++) | |
1783 | { | |
5236c819 | 1784 | unsigned int r_type; |
a85d7ed0 NC |
1785 | reloc_howto_type *howto; |
1786 | unsigned long r_symndx; | |
1787 | struct elf_link_hash_entry *h; | |
1788 | Elf_Internal_Sym *sym; | |
1789 | asection *sec; | |
0451c93c | 1790 | bfd_vma off; |
a85d7ed0 | 1791 | bfd_vma relocation; |
b34976b6 | 1792 | bfd_boolean unresolved_reloc; |
a85d7ed0 NC |
1793 | bfd_reloc_status_type r; |
1794 | ||
1795 | r_type = ELF64_R_TYPE (rel->r_info); | |
0451c93c | 1796 | if (r_type == (int) R_390_GNU_VTINHERIT |
947216bf AM |
1797 | || r_type == (int) R_390_GNU_VTENTRY) |
1798 | continue; | |
5236c819 | 1799 | if (r_type >= (int) R_390_max) |
a85d7ed0 NC |
1800 | { |
1801 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 1802 | return FALSE; |
a85d7ed0 | 1803 | } |
a85d7ed0 | 1804 | |
b491616a | 1805 | howto = elf_howto_table + r_type; |
a85d7ed0 | 1806 | r_symndx = ELF64_R_SYM (rel->r_info); |
5236c819 MS |
1807 | |
1808 | /* This is a final link. */ | |
a85d7ed0 NC |
1809 | h = NULL; |
1810 | sym = NULL; | |
1811 | sec = NULL; | |
b34976b6 | 1812 | unresolved_reloc = FALSE; |
a85d7ed0 NC |
1813 | if (r_symndx < symtab_hdr->sh_info) |
1814 | { | |
1815 | sym = local_syms + r_symndx; | |
1816 | sec = local_sections[r_symndx]; | |
f8df10f4 | 1817 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel); |
a85d7ed0 NC |
1818 | } |
1819 | else | |
1820 | { | |
1821 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1822 | while (h->root.type == bfd_link_hash_indirect | |
1823 | || h->root.type == bfd_link_hash_warning) | |
1824 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
0451c93c | 1825 | |
a85d7ed0 NC |
1826 | if (h->root.type == bfd_link_hash_defined |
1827 | || h->root.type == bfd_link_hash_defweak) | |
1828 | { | |
1829 | sec = h->root.u.def.section; | |
27018c3f | 1830 | if (sec->output_section == NULL) |
a85d7ed0 | 1831 | { |
0451c93c MS |
1832 | /* Set a flag that will be cleared later if we find a |
1833 | relocation value for this symbol. output_section | |
1834 | is typically NULL for symbols satisfied by a shared | |
1835 | library. */ | |
b34976b6 | 1836 | unresolved_reloc = TRUE; |
a85d7ed0 NC |
1837 | relocation = 0; |
1838 | } | |
1839 | else | |
1840 | relocation = (h->root.u.def.value | |
1841 | + sec->output_section->vma | |
1842 | + sec->output_offset); | |
1843 | } | |
1844 | else if (h->root.type == bfd_link_hash_undefweak) | |
1845 | relocation = 0; | |
671bae9c NC |
1846 | else if (info->shared |
1847 | && (!info->symbolic || info->allow_shlib_undefined) | |
a85d7ed0 NC |
1848 | && !info->no_undefined |
1849 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
1850 | relocation = 0; | |
1851 | else | |
1852 | { | |
1853 | if (! ((*info->callbacks->undefined_symbol) | |
1854 | (info, h->root.root.string, input_bfd, | |
947216bf | 1855 | input_section, rel->r_offset, |
0451c93c MS |
1856 | (!info->shared || info->no_undefined |
1857 | || ELF_ST_VISIBILITY (h->other))))) | |
b34976b6 | 1858 | return FALSE; |
a85d7ed0 NC |
1859 | relocation = 0; |
1860 | } | |
1861 | } | |
1862 | ||
1863 | switch (r_type) | |
1864 | { | |
5236c819 MS |
1865 | case R_390_GOTPLT12: |
1866 | case R_390_GOTPLT16: | |
1867 | case R_390_GOTPLT32: | |
1868 | case R_390_GOTPLT64: | |
1869 | case R_390_GOTPLTENT: | |
1870 | /* There are three cases for a GOTPLT relocation. 1) The | |
1871 | relocation is against the jump slot entry of a plt that | |
1872 | will get emitted to the output file. 2) The relocation | |
1873 | is against the jump slot of a plt entry that has been | |
1874 | removed. elf_s390_adjust_gotplt has created a GOT entry | |
1875 | as replacement. 3) The relocation is against a local symbol. | |
1876 | Cases 2) and 3) are the same as the GOT relocation code | |
1877 | so we just have to test for case 1 and fall through for | |
1878 | the other two. */ | |
1879 | if (h != NULL && h->plt.offset != (bfd_vma) -1) | |
1880 | { | |
1881 | bfd_vma plt_index; | |
1882 | ||
1883 | /* Calc. index no. | |
1884 | Current offset - size first entry / entry size. */ | |
1885 | plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / | |
1886 | PLT_ENTRY_SIZE; | |
1887 | ||
1888 | /* Offset in GOT is PLT index plus GOT headers(3) times 4, | |
1889 | addr & GOT addr. */ | |
1890 | relocation = (plt_index + 3) * GOT_ENTRY_SIZE; | |
1891 | unresolved_reloc = FALSE; | |
1892 | ||
1893 | if (r_type == R_390_GOTPLTENT) | |
1894 | relocation += htab->sgot->output_section->vma; | |
1895 | break; | |
1896 | } | |
1897 | /* Fall through. */ | |
1898 | ||
947216bf AM |
1899 | case R_390_GOT12: |
1900 | case R_390_GOT16: | |
1901 | case R_390_GOT32: | |
1902 | case R_390_GOT64: | |
1903 | case R_390_GOTENT: | |
1904 | /* Relocation is to the entry for this symbol in the global | |
1905 | offset table. */ | |
0451c93c MS |
1906 | if (htab->sgot == NULL) |
1907 | abort (); | |
a85d7ed0 | 1908 | |
947216bf AM |
1909 | if (h != NULL) |
1910 | { | |
b34976b6 | 1911 | bfd_boolean dyn; |
a85d7ed0 | 1912 | |
947216bf | 1913 | off = h->got.offset; |
0451c93c | 1914 | dyn = htab->elf.dynamic_sections_created; |
947216bf AM |
1915 | if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h) |
1916 | || (info->shared | |
1917 | && (info->symbolic | |
0451c93c MS |
1918 | || h->dynindx == -1 |
1919 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)) | |
947216bf AM |
1920 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) |
1921 | { | |
1922 | /* This is actually a static link, or it is a | |
1923 | -Bsymbolic link and the symbol is defined | |
1924 | locally, or the symbol was forced to be local | |
1925 | because of a version file. We must initialize | |
1926 | this entry in the global offset table. Since the | |
1927 | offset must always be a multiple of 2, we use the | |
1928 | least significant bit to record whether we have | |
1929 | initialized it already. | |
1930 | ||
1931 | When doing a dynamic link, we create a .rel.got | |
1932 | relocation entry to initialize the value. This | |
1933 | is done in the finish_dynamic_symbol routine. */ | |
1934 | if ((off & 1) != 0) | |
1935 | off &= ~1; | |
1936 | else | |
1937 | { | |
a85d7ed0 | 1938 | bfd_put_64 (output_bfd, relocation, |
0451c93c | 1939 | htab->sgot->contents + off); |
947216bf AM |
1940 | h->got.offset |= 1; |
1941 | } | |
1942 | } | |
0451c93c | 1943 | else |
b34976b6 | 1944 | unresolved_reloc = FALSE; |
947216bf AM |
1945 | } |
1946 | else | |
1947 | { | |
0451c93c MS |
1948 | if (local_got_offsets == NULL) |
1949 | abort (); | |
a85d7ed0 | 1950 | |
947216bf | 1951 | off = local_got_offsets[r_symndx]; |
a85d7ed0 | 1952 | |
947216bf AM |
1953 | /* The offset must always be a multiple of 8. We use |
1954 | the least significant bit to record whether we have | |
1955 | already generated the necessary reloc. */ | |
1956 | if ((off & 1) != 0) | |
1957 | off &= ~1; | |
1958 | else | |
1959 | { | |
1960 | bfd_put_64 (output_bfd, relocation, | |
0451c93c | 1961 | htab->sgot->contents + off); |
a85d7ed0 | 1962 | |
947216bf AM |
1963 | if (info->shared) |
1964 | { | |
1965 | asection *s; | |
1966 | Elf_Internal_Rela outrel; | |
1967 | bfd_byte *loc; | |
a85d7ed0 | 1968 | |
947216bf AM |
1969 | s = htab->srelgot; |
1970 | if (s == NULL) | |
0451c93c | 1971 | abort (); |
a85d7ed0 | 1972 | |
947216bf AM |
1973 | outrel.r_offset = (htab->sgot->output_section->vma |
1974 | + htab->sgot->output_offset | |
1975 | + off); | |
1976 | outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE); | |
a85d7ed0 | 1977 | outrel.r_addend = relocation; |
947216bf AM |
1978 | loc = s->contents; |
1979 | loc += s->reloc_count++ * sizeof (Elf64_External_Rela); | |
1980 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); | |
1981 | } | |
a85d7ed0 | 1982 | |
947216bf AM |
1983 | local_got_offsets[r_symndx] |= 1; |
1984 | } | |
1985 | } | |
a85d7ed0 | 1986 | |
0451c93c MS |
1987 | if (off >= (bfd_vma) -2) |
1988 | abort (); | |
1989 | ||
1990 | relocation = htab->sgot->output_offset + off; | |
1991 | ||
a85d7ed0 NC |
1992 | /* |
1993 | * For @GOTENT the relocation is against the offset between | |
1994 | * the instruction and the symbols entry in the GOT and not | |
1995 | * between the start of the GOT and the symbols entry. We | |
1996 | * add the vma of the GOT to get the correct value. | |
1997 | */ | |
5236c819 MS |
1998 | if ( r_type == R_390_GOTENT |
1999 | || r_type == R_390_GOTPLTENT) | |
0451c93c | 2000 | relocation += htab->sgot->output_section->vma; |
a85d7ed0 | 2001 | |
947216bf | 2002 | break; |
99c79b2e | 2003 | |
5236c819 MS |
2004 | case R_390_GOTOFF16: |
2005 | case R_390_GOTOFF32: | |
2006 | case R_390_GOTOFF64: | |
947216bf AM |
2007 | /* Relocation is relative to the start of the global offset |
2008 | table. */ | |
a85d7ed0 | 2009 | |
947216bf AM |
2010 | /* Note that sgot->output_offset is not involved in this |
2011 | calculation. We always want the start of .got. If we | |
2012 | defined _GLOBAL_OFFSET_TABLE in a different way, as is | |
2013 | permitted by the ABI, we might have to change this | |
2014 | calculation. */ | |
2015 | relocation -= htab->sgot->output_section->vma; | |
947216bf | 2016 | break; |
a85d7ed0 | 2017 | |
947216bf | 2018 | case R_390_GOTPC: |
a85d7ed0 | 2019 | case R_390_GOTPCDBL: |
947216bf AM |
2020 | /* Use global offset table as symbol value. */ |
2021 | relocation = htab->sgot->output_section->vma; | |
b34976b6 | 2022 | unresolved_reloc = FALSE; |
947216bf | 2023 | break; |
a85d7ed0 | 2024 | |
947216bf AM |
2025 | case R_390_PLT16DBL: |
2026 | case R_390_PLT32: | |
2027 | case R_390_PLT32DBL: | |
2028 | case R_390_PLT64: | |
2029 | /* Relocation is to the entry for this symbol in the | |
2030 | procedure linkage table. */ | |
a85d7ed0 | 2031 | |
947216bf AM |
2032 | /* Resolve a PLT32 reloc against a local symbol directly, |
2033 | without using the procedure linkage table. */ | |
2034 | if (h == NULL) | |
2035 | break; | |
a85d7ed0 | 2036 | |
947216bf | 2037 | if (h->plt.offset == (bfd_vma) -1 |
0451c93c | 2038 | || htab->splt == NULL) |
947216bf AM |
2039 | { |
2040 | /* We didn't make a PLT entry for this symbol. This | |
2041 | happens when statically linking PIC code, or when | |
2042 | using -Bsymbolic. */ | |
2043 | break; | |
2044 | } | |
2045 | ||
2046 | relocation = (htab->splt->output_section->vma | |
2047 | + htab->splt->output_offset | |
2048 | + h->plt.offset); | |
b34976b6 | 2049 | unresolved_reloc = FALSE; |
5236c819 MS |
2050 | break; |
2051 | ||
2052 | case R_390_PLTOFF16: | |
2053 | case R_390_PLTOFF32: | |
2054 | case R_390_PLTOFF64: | |
2055 | /* Relocation is to the entry for this symbol in the | |
2056 | procedure linkage table relative to the start of the GOT. */ | |
2057 | ||
2058 | /* For local symbols or if we didn't make a PLT entry for | |
2059 | this symbol resolve the symbol directly. */ | |
2060 | if ( h == NULL | |
2061 | || h->plt.offset == (bfd_vma) -1 | |
2062 | || htab->splt == NULL) | |
2063 | { | |
2064 | relocation -= htab->sgot->output_section->vma; | |
2065 | break; | |
2066 | } | |
2067 | ||
2068 | relocation = (htab->splt->output_section->vma | |
2069 | + htab->splt->output_offset | |
2070 | + h->plt.offset | |
2071 | - htab->sgot->output_section->vma); | |
2072 | unresolved_reloc = FALSE; | |
947216bf AM |
2073 | break; |
2074 | ||
2075 | case R_390_8: | |
2076 | case R_390_16: | |
2077 | case R_390_32: | |
2078 | case R_390_64: | |
2079 | case R_390_PC16: | |
2080 | case R_390_PC16DBL: | |
2081 | case R_390_PC32: | |
a85d7ed0 | 2082 | case R_390_PC32DBL: |
947216bf | 2083 | case R_390_PC64: |
ec338859 AM |
2084 | /* r_symndx will be zero only for relocs against symbols |
2085 | from removed linkonce sections, or sections discarded by | |
2086 | a linker script. */ | |
947216bf AM |
2087 | if (r_symndx == 0 |
2088 | || (input_section->flags & SEC_ALLOC) == 0) | |
ec338859 AM |
2089 | break; |
2090 | ||
947216bf | 2091 | if ((info->shared |
0451c93c MS |
2092 | && ((r_type != R_390_PC16 |
2093 | && r_type != R_390_PC16DBL | |
2094 | && r_type != R_390_PC32 | |
2095 | && r_type != R_390_PC32DBL | |
2096 | && r_type != R_390_PC64) | |
2097 | || (h != NULL | |
2098 | && h->dynindx != -1 | |
2099 | && (! info->symbolic | |
2100 | || (h->elf_link_hash_flags | |
2101 | & ELF_LINK_HASH_DEF_REGULAR) == 0)))) | |
2102 | || (!info->shared | |
0451c93c MS |
2103 | && h != NULL |
2104 | && h->dynindx != -1 | |
2105 | && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0 | |
2106 | && (((h->elf_link_hash_flags | |
2107 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2108 | && (h->elf_link_hash_flags | |
2109 | & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2110 | || h->root.type == bfd_link_hash_undefweak | |
2111 | || h->root.type == bfd_link_hash_undefined))) | |
947216bf AM |
2112 | { |
2113 | Elf_Internal_Rela outrel; | |
b34976b6 | 2114 | bfd_boolean skip, relocate; |
0451c93c | 2115 | asection *sreloc; |
947216bf | 2116 | bfd_byte *loc; |
a85d7ed0 | 2117 | |
947216bf AM |
2118 | /* When generating a shared object, these relocations |
2119 | are copied into the output file to be resolved at run | |
2120 | time. */ | |
a85d7ed0 | 2121 | |
b34976b6 AM |
2122 | skip = FALSE; |
2123 | relocate = FALSE; | |
a85d7ed0 | 2124 | |
c629eae0 JJ |
2125 | outrel.r_offset = |
2126 | _bfd_elf_section_offset (output_bfd, info, input_section, | |
2127 | rel->r_offset); | |
2128 | if (outrel.r_offset == (bfd_vma) -1) | |
b34976b6 | 2129 | skip = TRUE; |
0bb2d96a | 2130 | else if (outrel.r_offset == (bfd_vma) -2) |
b34976b6 | 2131 | skip = TRUE, relocate = TRUE; |
a85d7ed0 | 2132 | |
947216bf AM |
2133 | outrel.r_offset += (input_section->output_section->vma |
2134 | + input_section->output_offset); | |
a85d7ed0 | 2135 | |
947216bf | 2136 | if (skip) |
0bb2d96a | 2137 | memset (&outrel, 0, sizeof outrel); |
947216bf | 2138 | else if (h != NULL |
0451c93c MS |
2139 | && h->dynindx != -1 |
2140 | && (r_type == R_390_PC16 | |
2141 | || r_type == R_390_PC16DBL | |
2142 | || r_type == R_390_PC32 | |
2143 | || r_type == R_390_PC32DBL | |
2144 | || r_type == R_390_PC64 | |
2145 | || !info->shared | |
2146 | || !info->symbolic | |
2147 | || (h->elf_link_hash_flags | |
2148 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
947216bf AM |
2149 | { |
2150 | outrel.r_info = ELF64_R_INFO (h->dynindx, r_type); | |
27018c3f | 2151 | outrel.r_addend = rel->r_addend; |
947216bf AM |
2152 | } |
2153 | else | |
2154 | { | |
0451c93c | 2155 | /* This symbol is local, or marked to become local. */ |
b34976b6 | 2156 | relocate = TRUE; |
0451c93c MS |
2157 | outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE); |
2158 | outrel.r_addend = relocation + rel->r_addend; | |
2159 | } | |
a85d7ed0 | 2160 | |
0451c93c MS |
2161 | sreloc = elf_section_data (input_section)->sreloc; |
2162 | if (sreloc == NULL) | |
2163 | abort (); | |
2164 | ||
947216bf AM |
2165 | loc = sreloc->contents; |
2166 | loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela); | |
2167 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); | |
a85d7ed0 | 2168 | |
947216bf AM |
2169 | /* If this reloc is against an external symbol, we do |
2170 | not want to fiddle with the addend. Otherwise, we | |
2171 | need to include the symbol value so that it becomes | |
2172 | an addend for the dynamic reloc. */ | |
2173 | if (! relocate) | |
2174 | continue; | |
2175 | } | |
a85d7ed0 | 2176 | |
947216bf | 2177 | break; |
a85d7ed0 | 2178 | |
947216bf AM |
2179 | default: |
2180 | break; | |
2181 | } | |
a85d7ed0 | 2182 | |
239e1f3a AM |
2183 | /* Dynamic relocs are not propagated for SEC_DEBUGGING sections |
2184 | because such sections are not SEC_ALLOC and thus ld.so will | |
2185 | not process them. */ | |
0451c93c | 2186 | if (unresolved_reloc |
239e1f3a | 2187 | && !((input_section->flags & SEC_DEBUGGING) != 0 |
0451c93c MS |
2188 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)) |
2189 | (*_bfd_error_handler) | |
2190 | (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"), | |
2191 | bfd_archive_filename (input_bfd), | |
2192 | bfd_get_section_name (input_bfd, input_section), | |
2193 | (long) rel->r_offset, | |
2194 | h->root.root.string); | |
2195 | ||
a85d7ed0 NC |
2196 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
2197 | contents, rel->r_offset, | |
2198 | relocation, rel->r_addend); | |
2199 | ||
2200 | if (r != bfd_reloc_ok) | |
2201 | { | |
0451c93c | 2202 | const char *name; |
ec338859 | 2203 | |
0451c93c MS |
2204 | if (h != NULL) |
2205 | name = h->root.root.string; | |
2206 | else | |
a85d7ed0 | 2207 | { |
0451c93c MS |
2208 | name = bfd_elf_string_from_elf_section (input_bfd, |
2209 | symtab_hdr->sh_link, | |
2210 | sym->st_name); | |
2211 | if (name == NULL) | |
b34976b6 | 2212 | return FALSE; |
0451c93c MS |
2213 | if (*name == '\0') |
2214 | name = bfd_section_name (input_bfd, sec); | |
2215 | } | |
ec338859 | 2216 | |
0451c93c MS |
2217 | if (r == bfd_reloc_overflow) |
2218 | { | |
ec338859 | 2219 | |
0451c93c MS |
2220 | if (! ((*info->callbacks->reloc_overflow) |
2221 | (info, name, howto->name, (bfd_vma) 0, | |
2222 | input_bfd, input_section, rel->r_offset))) | |
b34976b6 | 2223 | return FALSE; |
0451c93c MS |
2224 | } |
2225 | else | |
2226 | { | |
2227 | (*_bfd_error_handler) | |
2228 | (_("%s(%s+0x%lx): reloc against `%s': error %d"), | |
2229 | bfd_archive_filename (input_bfd), | |
2230 | bfd_get_section_name (input_bfd, input_section), | |
2231 | (long) rel->r_offset, name, (int) r); | |
b34976b6 | 2232 | return FALSE; |
a85d7ed0 NC |
2233 | } |
2234 | } | |
2235 | } | |
2236 | ||
b34976b6 | 2237 | return TRUE; |
a85d7ed0 NC |
2238 | } |
2239 | ||
2240 | /* Finish up dynamic symbol handling. We set the contents of various | |
2241 | dynamic sections here. */ | |
2242 | ||
b34976b6 | 2243 | static bfd_boolean |
a85d7ed0 NC |
2244 | elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym) |
2245 | bfd *output_bfd; | |
2246 | struct bfd_link_info *info; | |
2247 | struct elf_link_hash_entry *h; | |
2248 | Elf_Internal_Sym *sym; | |
2249 | { | |
0451c93c | 2250 | struct elf_s390_link_hash_table *htab; |
a85d7ed0 | 2251 | |
0451c93c | 2252 | htab = elf_s390_hash_table (info); |
a85d7ed0 NC |
2253 | |
2254 | if (h->plt.offset != (bfd_vma) -1) | |
2255 | { | |
a85d7ed0 | 2256 | bfd_vma plt_index; |
0451c93c MS |
2257 | bfd_vma got_offset; |
2258 | Elf_Internal_Rela rela; | |
947216bf | 2259 | bfd_byte *loc; |
a85d7ed0 NC |
2260 | |
2261 | /* This symbol has an entry in the procedure linkage table. Set | |
947216bf | 2262 | it up. */ |
a85d7ed0 | 2263 | |
0451c93c MS |
2264 | if (h->dynindx == -1 |
2265 | || htab->splt == NULL | |
2266 | || htab->sgotplt == NULL | |
2267 | || htab->srelplt == NULL) | |
2268 | abort (); | |
a85d7ed0 | 2269 | |
99c79b2e | 2270 | /* Calc. index no. |
947216bf | 2271 | Current offset - size first entry / entry size. */ |
a85d7ed0 NC |
2272 | plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE; |
2273 | ||
2274 | /* Offset in GOT is PLT index plus GOT headers(3) times 8, | |
947216bf | 2275 | addr & GOT addr. */ |
a85d7ed0 NC |
2276 | got_offset = (plt_index + 3) * GOT_ENTRY_SIZE; |
2277 | ||
2278 | /* Fill in the blueprint of a PLT. */ | |
0451c93c MS |
2279 | bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD0, |
2280 | htab->splt->contents + h->plt.offset); | |
2281 | bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD1, | |
2282 | htab->splt->contents + h->plt.offset + 4); | |
2283 | bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2, | |
2284 | htab->splt->contents + h->plt.offset + 8); | |
2285 | bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3, | |
2286 | htab->splt->contents + h->plt.offset + 12); | |
2287 | bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD4, | |
2288 | htab->splt->contents + h->plt.offset + 16); | |
2289 | bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD5, | |
2290 | htab->splt->contents + h->plt.offset + 20); | |
2291 | bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD6, | |
2292 | htab->splt->contents + h->plt.offset + 24); | |
2293 | bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD7, | |
2294 | htab->splt->contents + h->plt.offset + 28); | |
a85d7ed0 NC |
2295 | /* Fixup the relative address to the GOT entry */ |
2296 | bfd_put_32 (output_bfd, | |
0451c93c MS |
2297 | (htab->sgotplt->output_section->vma + |
2298 | htab->sgotplt->output_offset + got_offset | |
2299 | - (htab->splt->output_section->vma + h->plt.offset))/2, | |
2300 | htab->splt->contents + h->plt.offset + 2); | |
a85d7ed0 NC |
2301 | /* Fixup the relative branch to PLT 0 */ |
2302 | bfd_put_32 (output_bfd, - (PLT_FIRST_ENTRY_SIZE + | |
2303 | (PLT_ENTRY_SIZE * plt_index) + 22)/2, | |
0451c93c | 2304 | htab->splt->contents + h->plt.offset + 24); |
a85d7ed0 NC |
2305 | /* Fixup offset into symbol table */ |
2306 | bfd_put_32 (output_bfd, plt_index * sizeof (Elf64_External_Rela), | |
0451c93c | 2307 | htab->splt->contents + h->plt.offset + 28); |
a85d7ed0 NC |
2308 | |
2309 | /* Fill in the entry in the global offset table. | |
947216bf | 2310 | Points to instruction after GOT offset. */ |
a85d7ed0 | 2311 | bfd_put_64 (output_bfd, |
0451c93c MS |
2312 | (htab->splt->output_section->vma |
2313 | + htab->splt->output_offset | |
a85d7ed0 NC |
2314 | + h->plt.offset |
2315 | + 14), | |
0451c93c | 2316 | htab->sgotplt->contents + got_offset); |
a85d7ed0 | 2317 | |
0451c93c MS |
2318 | /* Fill in the entry in the .rela.plt section. */ |
2319 | rela.r_offset = (htab->sgotplt->output_section->vma | |
2320 | + htab->sgotplt->output_offset | |
2321 | + got_offset); | |
2322 | rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT); | |
2323 | rela.r_addend = 0; | |
947216bf | 2324 | loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela); |
0451c93c | 2325 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); |
a85d7ed0 NC |
2326 | |
2327 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2328 | { | |
2329 | /* Mark the symbol as undefined, rather than as defined in | |
0451c93c MS |
2330 | the .plt section. Leave the value alone. This is a clue |
2331 | for the dynamic linker, to make function pointer | |
2332 | comparisons work between an application and shared | |
2333 | library. */ | |
a85d7ed0 NC |
2334 | sym->st_shndx = SHN_UNDEF; |
2335 | } | |
2336 | } | |
2337 | ||
2338 | if (h->got.offset != (bfd_vma) -1) | |
2339 | { | |
a85d7ed0 | 2340 | Elf_Internal_Rela rela; |
947216bf | 2341 | bfd_byte *loc; |
a85d7ed0 NC |
2342 | |
2343 | /* This symbol has an entry in the global offset table. Set it | |
947216bf | 2344 | up. */ |
a85d7ed0 | 2345 | |
0451c93c MS |
2346 | if (htab->sgot == NULL || htab->srelgot == NULL) |
2347 | abort (); | |
a85d7ed0 | 2348 | |
0451c93c MS |
2349 | rela.r_offset = (htab->sgot->output_section->vma |
2350 | + htab->sgot->output_offset | |
dc810e39 | 2351 | + (h->got.offset &~ (bfd_vma) 1)); |
a85d7ed0 NC |
2352 | |
2353 | /* If this is a static link, or it is a -Bsymbolic link and the | |
2354 | symbol is defined locally or was forced to be local because | |
2355 | of a version file, we just want to emit a RELATIVE reloc. | |
2356 | The entry in the global offset table will already have been | |
2357 | initialized in the relocate_section function. */ | |
0451c93c MS |
2358 | if (info->shared |
2359 | && (info->symbolic | |
2360 | || h->dynindx == -1 | |
2361 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)) | |
2362 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)) | |
947216bf | 2363 | { |
0451c93c | 2364 | BFD_ASSERT((h->got.offset & 1) != 0); |
947216bf AM |
2365 | rela.r_info = ELF64_R_INFO (0, R_390_RELATIVE); |
2366 | rela.r_addend = (h->root.u.def.value | |
2367 | + h->root.u.def.section->output_section->vma | |
2368 | + h->root.u.def.section->output_offset); | |
2369 | } | |
a85d7ed0 NC |
2370 | else |
2371 | { | |
2372 | BFD_ASSERT((h->got.offset & 1) == 0); | |
0451c93c | 2373 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgot->contents + h->got.offset); |
a85d7ed0 | 2374 | rela.r_info = ELF64_R_INFO (h->dynindx, R_390_GLOB_DAT); |
947216bf AM |
2375 | rela.r_addend = 0; |
2376 | } | |
a85d7ed0 | 2377 | |
947216bf AM |
2378 | loc = htab->srelgot->contents; |
2379 | loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela); | |
0451c93c | 2380 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); |
a85d7ed0 NC |
2381 | } |
2382 | ||
2383 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) | |
2384 | { | |
a85d7ed0 | 2385 | Elf_Internal_Rela rela; |
947216bf | 2386 | bfd_byte *loc; |
a85d7ed0 NC |
2387 | |
2388 | /* This symbols needs a copy reloc. Set it up. */ | |
2389 | ||
0451c93c MS |
2390 | if (h->dynindx == -1 |
2391 | || (h->root.type != bfd_link_hash_defined | |
2392 | && h->root.type != bfd_link_hash_defweak) | |
2393 | || htab->srelbss == NULL) | |
2394 | abort (); | |
a85d7ed0 NC |
2395 | |
2396 | rela.r_offset = (h->root.u.def.value | |
2397 | + h->root.u.def.section->output_section->vma | |
2398 | + h->root.u.def.section->output_offset); | |
2399 | rela.r_info = ELF64_R_INFO (h->dynindx, R_390_COPY); | |
2400 | rela.r_addend = 0; | |
947216bf AM |
2401 | loc = htab->srelbss->contents; |
2402 | loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela); | |
0451c93c | 2403 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); |
a85d7ed0 NC |
2404 | } |
2405 | ||
2406 | /* Mark some specially defined symbols as absolute. */ | |
2407 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
2408 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0 | |
2409 | || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0) | |
2410 | sym->st_shndx = SHN_ABS; | |
2411 | ||
b34976b6 | 2412 | return TRUE; |
a85d7ed0 NC |
2413 | } |
2414 | ||
0451c93c MS |
2415 | /* Used to decide how to sort relocs in an optimal manner for the |
2416 | dynamic linker, before writing them out. */ | |
2417 | ||
2418 | static enum elf_reloc_type_class | |
2419 | elf_s390_reloc_type_class (rela) | |
2420 | const Elf_Internal_Rela *rela; | |
2421 | { | |
2422 | switch ((int) ELF64_R_TYPE (rela->r_info)) | |
2423 | { | |
2424 | case R_390_RELATIVE: | |
2425 | return reloc_class_relative; | |
2426 | case R_390_JMP_SLOT: | |
2427 | return reloc_class_plt; | |
2428 | case R_390_COPY: | |
2429 | return reloc_class_copy; | |
2430 | default: | |
2431 | return reloc_class_normal; | |
2432 | } | |
2433 | } | |
2434 | ||
a85d7ed0 NC |
2435 | /* Finish up the dynamic sections. */ |
2436 | ||
b34976b6 | 2437 | static bfd_boolean |
a85d7ed0 NC |
2438 | elf_s390_finish_dynamic_sections (output_bfd, info) |
2439 | bfd *output_bfd; | |
2440 | struct bfd_link_info *info; | |
2441 | { | |
0451c93c | 2442 | struct elf_s390_link_hash_table *htab; |
a85d7ed0 NC |
2443 | bfd *dynobj; |
2444 | asection *sdyn; | |
a85d7ed0 | 2445 | |
0451c93c MS |
2446 | htab = elf_s390_hash_table (info); |
2447 | dynobj = htab->elf.dynobj; | |
a85d7ed0 NC |
2448 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); |
2449 | ||
0451c93c | 2450 | if (htab->elf.dynamic_sections_created) |
a85d7ed0 | 2451 | { |
a85d7ed0 NC |
2452 | Elf64_External_Dyn *dyncon, *dynconend; |
2453 | ||
0451c93c MS |
2454 | if (sdyn == NULL || htab->sgot == NULL) |
2455 | abort (); | |
a85d7ed0 NC |
2456 | |
2457 | dyncon = (Elf64_External_Dyn *) sdyn->contents; | |
2458 | dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
2459 | for (; dyncon < dynconend; dyncon++) | |
2460 | { | |
2461 | Elf_Internal_Dyn dyn; | |
a85d7ed0 | 2462 | asection *s; |
ec338859 | 2463 | |
a85d7ed0 | 2464 | bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn); |
ec338859 | 2465 | |
a85d7ed0 NC |
2466 | switch (dyn.d_tag) |
2467 | { | |
2468 | default: | |
0451c93c | 2469 | continue; |
ec338859 | 2470 | |
a85d7ed0 | 2471 | case DT_PLTGOT: |
0451c93c MS |
2472 | dyn.d_un.d_ptr = htab->sgot->output_section->vma; |
2473 | break; | |
ec338859 | 2474 | |
a85d7ed0 | 2475 | case DT_JMPREL: |
0451c93c | 2476 | dyn.d_un.d_ptr = htab->srelplt->output_section->vma; |
a85d7ed0 | 2477 | break; |
ec338859 | 2478 | |
a85d7ed0 | 2479 | case DT_PLTRELSZ: |
0451c93c | 2480 | s = htab->srelplt->output_section; |
a85d7ed0 NC |
2481 | if (s->_cooked_size != 0) |
2482 | dyn.d_un.d_val = s->_cooked_size; | |
2483 | else | |
2484 | dyn.d_un.d_val = s->_raw_size; | |
a85d7ed0 | 2485 | break; |
ec338859 | 2486 | |
a85d7ed0 NC |
2487 | case DT_RELASZ: |
2488 | /* The procedure linkage table relocs (DT_JMPREL) should | |
2489 | not be included in the overall relocs (DT_RELA). | |
2490 | Therefore, we override the DT_RELASZ entry here to | |
2491 | make it not include the JMPREL relocs. Since the | |
2492 | linker script arranges for .rela.plt to follow all | |
2493 | other relocation sections, we don't have to worry | |
2494 | about changing the DT_RELA entry. */ | |
0451c93c MS |
2495 | s = htab->srelplt->output_section; |
2496 | if (s->_cooked_size != 0) | |
2497 | dyn.d_un.d_val -= s->_cooked_size; | |
2498 | else | |
2499 | dyn.d_un.d_val -= s->_raw_size; | |
a85d7ed0 NC |
2500 | break; |
2501 | } | |
0451c93c MS |
2502 | |
2503 | bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon); | |
a85d7ed0 NC |
2504 | } |
2505 | ||
2506 | /* Fill in the special first entry in the procedure linkage table. */ | |
0451c93c | 2507 | if (htab->splt && htab->splt->_raw_size > 0) |
a85d7ed0 NC |
2508 | { |
2509 | /* fill in blueprint for plt 0 entry */ | |
0451c93c MS |
2510 | bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD0, |
2511 | htab->splt->contents ); | |
2512 | bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD1, | |
2513 | htab->splt->contents +4 ); | |
2514 | bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD3, | |
2515 | htab->splt->contents +12 ); | |
2516 | bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD4, | |
2517 | htab->splt->contents +16 ); | |
2518 | bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD5, | |
2519 | htab->splt->contents +20 ); | |
2520 | bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD6, | |
2521 | htab->splt->contents + 24); | |
2522 | bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD7, | |
2523 | htab->splt->contents + 28 ); | |
a85d7ed0 NC |
2524 | /* Fixup relative address to start of GOT */ |
2525 | bfd_put_32 (output_bfd, | |
0451c93c MS |
2526 | (htab->sgotplt->output_section->vma + |
2527 | htab->sgotplt->output_offset | |
2528 | - htab->splt->output_section->vma - 6)/2, | |
2529 | htab->splt->contents + 8); | |
a85d7ed0 | 2530 | } |
0451c93c MS |
2531 | elf_section_data (htab->splt->output_section) |
2532 | ->this_hdr.sh_entsize = PLT_ENTRY_SIZE; | |
a85d7ed0 NC |
2533 | } |
2534 | ||
0451c93c | 2535 | if (htab->sgotplt) |
a85d7ed0 | 2536 | { |
0451c93c MS |
2537 | /* Fill in the first three entries in the global offset table. */ |
2538 | if (htab->sgotplt->_raw_size > 0) | |
2539 | { | |
2540 | bfd_put_64 (output_bfd, | |
2541 | (sdyn == NULL ? (bfd_vma) 0 | |
2542 | : sdyn->output_section->vma + sdyn->output_offset), | |
2543 | htab->sgotplt->contents); | |
2544 | /* One entry for shared object struct ptr. */ | |
2545 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8); | |
2546 | /* One entry for _dl_runtime_resolve. */ | |
2547 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 12); | |
2548 | } | |
a85d7ed0 | 2549 | |
0451c93c MS |
2550 | elf_section_data (htab->sgot->output_section) |
2551 | ->this_hdr.sh_entsize = 8; | |
2552 | } | |
b34976b6 | 2553 | return TRUE; |
a85d7ed0 NC |
2554 | } |
2555 | ||
b34976b6 | 2556 | static bfd_boolean |
a85d7ed0 NC |
2557 | elf_s390_object_p (abfd) |
2558 | bfd *abfd; | |
2559 | { | |
befc3abb | 2560 | return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_64); |
a85d7ed0 NC |
2561 | } |
2562 | ||
2563 | /* | |
2564 | * Why was the hash table entry size definition changed from | |
2565 | * ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and | |
2566 | * this is the only reason for the s390_elf64_size_info structure. | |
2567 | */ | |
2568 | ||
2569 | const struct elf_size_info s390_elf64_size_info = | |
2570 | { | |
2571 | sizeof (Elf64_External_Ehdr), | |
2572 | sizeof (Elf64_External_Phdr), | |
2573 | sizeof (Elf64_External_Shdr), | |
2574 | sizeof (Elf64_External_Rel), | |
2575 | sizeof (Elf64_External_Rela), | |
2576 | sizeof (Elf64_External_Sym), | |
2577 | sizeof (Elf64_External_Dyn), | |
2578 | sizeof (Elf_External_Note), | |
947216bf AM |
2579 | 8, /* hash-table entry size */ |
2580 | 1, /* internal relocations per external relocations */ | |
a85d7ed0 NC |
2581 | 64, /* arch_size */ |
2582 | 8, /* file_align */ | |
2583 | ELFCLASS64, EV_CURRENT, | |
2584 | bfd_elf64_write_out_phdrs, | |
2585 | bfd_elf64_write_shdrs_and_ehdr, | |
2586 | bfd_elf64_write_relocs, | |
73ff0d56 | 2587 | bfd_elf64_swap_symbol_in, |
a85d7ed0 NC |
2588 | bfd_elf64_swap_symbol_out, |
2589 | bfd_elf64_slurp_reloc_table, | |
2590 | bfd_elf64_slurp_symbol_table, | |
2591 | bfd_elf64_swap_dyn_in, | |
2592 | bfd_elf64_swap_dyn_out, | |
947216bf AM |
2593 | bfd_elf64_swap_reloc_in, |
2594 | bfd_elf64_swap_reloc_out, | |
2595 | bfd_elf64_swap_reloca_in, | |
2596 | bfd_elf64_swap_reloca_out | |
a85d7ed0 NC |
2597 | }; |
2598 | ||
2599 | #define TARGET_BIG_SYM bfd_elf64_s390_vec | |
2600 | #define TARGET_BIG_NAME "elf64-s390" | |
2601 | #define ELF_ARCH bfd_arch_s390 | |
2602 | #define ELF_MACHINE_CODE EM_S390 | |
2603 | #define ELF_MACHINE_ALT1 EM_S390_OLD | |
2604 | #define ELF_MAXPAGESIZE 0x1000 | |
2605 | ||
2606 | #define elf_backend_size_info s390_elf64_size_info | |
2607 | ||
2608 | #define elf_backend_can_gc_sections 1 | |
51b64d56 | 2609 | #define elf_backend_can_refcount 1 |
a85d7ed0 NC |
2610 | #define elf_backend_want_got_plt 1 |
2611 | #define elf_backend_plt_readonly 1 | |
2612 | #define elf_backend_want_plt_sym 0 | |
2613 | #define elf_backend_got_header_size 24 | |
2614 | #define elf_backend_plt_header_size PLT_ENTRY_SIZE | |
b491616a | 2615 | #define elf_backend_rela_normal 1 |
a85d7ed0 NC |
2616 | |
2617 | #define elf_info_to_howto elf_s390_info_to_howto | |
2618 | ||
a85d7ed0 NC |
2619 | #define bfd_elf64_bfd_is_local_label_name elf_s390_is_local_label_name |
2620 | #define bfd_elf64_bfd_link_hash_table_create elf_s390_link_hash_table_create | |
2621 | #define bfd_elf64_bfd_reloc_type_lookup elf_s390_reloc_type_lookup | |
2622 | ||
2623 | #define elf_backend_adjust_dynamic_symbol elf_s390_adjust_dynamic_symbol | |
2624 | #define elf_backend_check_relocs elf_s390_check_relocs | |
0451c93c MS |
2625 | #define elf_backend_copy_indirect_symbol elf_s390_copy_indirect_symbol |
2626 | #define elf_backend_create_dynamic_sections elf_s390_create_dynamic_sections | |
a85d7ed0 NC |
2627 | #define elf_backend_finish_dynamic_sections elf_s390_finish_dynamic_sections |
2628 | #define elf_backend_finish_dynamic_symbol elf_s390_finish_dynamic_symbol | |
2629 | #define elf_backend_gc_mark_hook elf_s390_gc_mark_hook | |
2630 | #define elf_backend_gc_sweep_hook elf_s390_gc_sweep_hook | |
0451c93c | 2631 | #define elf_backend_reloc_type_class elf_s390_reloc_type_class |
a85d7ed0 NC |
2632 | #define elf_backend_relocate_section elf_s390_relocate_section |
2633 | #define elf_backend_size_dynamic_sections elf_s390_size_dynamic_sections | |
29c2fb7c | 2634 | #define elf_backend_reloc_type_class elf_s390_reloc_type_class |
a85d7ed0 | 2635 | |
947216bf | 2636 | #define elf_backend_object_p elf_s390_object_p |
a85d7ed0 NC |
2637 | |
2638 | #include "elf64-target.h" |