elf64-s390.c revision 1.12 1 /* IBM S/390-specific support for 64-bit ELF
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed Martin Schwidefsky (schwidefsky (at) 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 3 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., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/s390.h"
28 #include "elf-s390.h"
29 #include <stdarg.h>
30
31 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
32 from smaller values. Start with zero, widen, *then* decrement. */
33 #define MINUS_ONE (((bfd_vma)0) - 1)
34
35 static bfd_reloc_status_type
36 s390_tls_reloc (bfd *, arelent *, asymbol *, void *,
37 asection *, bfd *, char **);
38 static bfd_reloc_status_type
39 s390_elf_ldisp_reloc (bfd *, arelent *, asymbol *, void *,
40 asection *, bfd *, char **);
41
42 /* The relocation "howto" table. */
43 static reloc_howto_type elf_howto_table[] =
44 {
45 HOWTO (R_390_NONE, /* type */
46 0, /* rightshift */
47 0, /* size */
48 0, /* bitsize */
49 false, /* pc_relative */
50 0, /* bitpos */
51 complain_overflow_dont, /* complain_on_overflow */
52 bfd_elf_generic_reloc, /* special_function */
53 "R_390_NONE", /* name */
54 false, /* partial_inplace */
55 0, /* src_mask */
56 0, /* dst_mask */
57 false), /* pcrel_offset */
58
59 HOWTO(R_390_8, 0, 1, 8, false, 0, complain_overflow_bitfield,
60 bfd_elf_generic_reloc, "R_390_8", false, 0,0x000000ff, false),
61 HOWTO(R_390_12, 0, 2, 12, false, 0, complain_overflow_dont,
62 bfd_elf_generic_reloc, "R_390_12", false, 0,0x00000fff, false),
63 HOWTO(R_390_16, 0, 2, 16, false, 0, complain_overflow_bitfield,
64 bfd_elf_generic_reloc, "R_390_16", false, 0,0x0000ffff, false),
65 HOWTO(R_390_32, 0, 4, 32, false, 0, complain_overflow_bitfield,
66 bfd_elf_generic_reloc, "R_390_32", false, 0,0xffffffff, false),
67 HOWTO(R_390_PC32, 0, 4, 32, true, 0, complain_overflow_bitfield,
68 bfd_elf_generic_reloc, "R_390_PC32", false, 0,0xffffffff, true),
69 HOWTO(R_390_GOT12, 0, 2, 12, false, 0, complain_overflow_bitfield,
70 bfd_elf_generic_reloc, "R_390_GOT12", false, 0,0x00000fff, false),
71 HOWTO(R_390_GOT32, 0, 4, 32, false, 0, complain_overflow_bitfield,
72 bfd_elf_generic_reloc, "R_390_GOT32", false, 0,0xffffffff, false),
73 HOWTO(R_390_PLT32, 0, 4, 32, true, 0, complain_overflow_bitfield,
74 bfd_elf_generic_reloc, "R_390_PLT32", false, 0,0xffffffff, true),
75 HOWTO(R_390_COPY, 0, 8, 64, false, 0, complain_overflow_bitfield,
76 bfd_elf_generic_reloc, "R_390_COPY", false, 0,MINUS_ONE, false),
77 HOWTO(R_390_GLOB_DAT, 0, 8, 64, false, 0, complain_overflow_bitfield,
78 bfd_elf_generic_reloc, "R_390_GLOB_DAT", false, 0,MINUS_ONE, false),
79 HOWTO(R_390_JMP_SLOT, 0, 8, 64, false, 0, complain_overflow_bitfield,
80 bfd_elf_generic_reloc, "R_390_JMP_SLOT", false, 0,MINUS_ONE, false),
81 HOWTO(R_390_RELATIVE, 0, 8, 64, true, 0, complain_overflow_bitfield,
82 bfd_elf_generic_reloc, "R_390_RELATIVE", false, 0,MINUS_ONE, false),
83 HOWTO(R_390_GOTOFF32, 0, 4, 32, false, 0, complain_overflow_bitfield,
84 bfd_elf_generic_reloc, "R_390_GOTOFF32", false, 0,MINUS_ONE, false),
85 HOWTO(R_390_GOTPC, 0, 8, 64, true, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_390_GOTPC", false, 0,MINUS_ONE, true),
87 HOWTO(R_390_GOT16, 0, 2, 16, false, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_390_GOT16", false, 0,0x0000ffff, false),
89 HOWTO(R_390_PC16, 0, 2, 16, true, 0, complain_overflow_bitfield,
90 bfd_elf_generic_reloc, "R_390_PC16", false, 0,0x0000ffff, true),
91 HOWTO(R_390_PC16DBL, 1, 2, 16, true, 0, complain_overflow_bitfield,
92 bfd_elf_generic_reloc, "R_390_PC16DBL", false, 0,0x0000ffff, true),
93 HOWTO(R_390_PLT16DBL, 1, 2, 16, true, 0, complain_overflow_bitfield,
94 bfd_elf_generic_reloc, "R_390_PLT16DBL", false, 0,0x0000ffff, true),
95 HOWTO(R_390_PC32DBL, 1, 4, 32, true, 0, complain_overflow_bitfield,
96 bfd_elf_generic_reloc, "R_390_PC32DBL", false, 0,0xffffffff, true),
97 HOWTO(R_390_PLT32DBL, 1, 4, 32, true, 0, complain_overflow_bitfield,
98 bfd_elf_generic_reloc, "R_390_PLT32DBL", false, 0,0xffffffff, true),
99 HOWTO(R_390_GOTPCDBL, 1, 4, 32, true, 0, complain_overflow_bitfield,
100 bfd_elf_generic_reloc, "R_390_GOTPCDBL", false, 0,MINUS_ONE, true),
101 HOWTO(R_390_64, 0, 8, 64, false, 0, complain_overflow_bitfield,
102 bfd_elf_generic_reloc, "R_390_64", false, 0,MINUS_ONE, false),
103 HOWTO(R_390_PC64, 0, 8, 64, true, 0, complain_overflow_bitfield,
104 bfd_elf_generic_reloc, "R_390_PC64", false, 0,MINUS_ONE, true),
105 HOWTO(R_390_GOT64, 0, 8, 64, false, 0, complain_overflow_bitfield,
106 bfd_elf_generic_reloc, "R_390_GOT64", false, 0,MINUS_ONE, false),
107 HOWTO(R_390_PLT64, 0, 8, 64, true, 0, complain_overflow_bitfield,
108 bfd_elf_generic_reloc, "R_390_PLT64", false, 0,MINUS_ONE, true),
109 HOWTO(R_390_GOTENT, 1, 4, 32, true, 0, complain_overflow_bitfield,
110 bfd_elf_generic_reloc, "R_390_GOTENT", false, 0,MINUS_ONE, true),
111 HOWTO(R_390_GOTOFF16, 0, 2, 16, false, 0, complain_overflow_bitfield,
112 bfd_elf_generic_reloc, "R_390_GOTOFF16", false, 0,0x0000ffff, false),
113 HOWTO(R_390_GOTOFF64, 0, 8, 64, false, 0, complain_overflow_bitfield,
114 bfd_elf_generic_reloc, "R_390_GOTOFF64", false, 0,MINUS_ONE, false),
115 HOWTO(R_390_GOTPLT12, 0, 2, 12, false, 0, complain_overflow_dont,
116 bfd_elf_generic_reloc, "R_390_GOTPLT12", false, 0,0x00000fff, false),
117 HOWTO(R_390_GOTPLT16, 0, 2, 16, false, 0, complain_overflow_bitfield,
118 bfd_elf_generic_reloc, "R_390_GOTPLT16", false, 0,0x0000ffff, false),
119 HOWTO(R_390_GOTPLT32, 0, 4, 32, false, 0, complain_overflow_bitfield,
120 bfd_elf_generic_reloc, "R_390_GOTPLT32", false, 0,0xffffffff, false),
121 HOWTO(R_390_GOTPLT64, 0, 8, 64, false, 0, complain_overflow_bitfield,
122 bfd_elf_generic_reloc, "R_390_GOTPLT64", false, 0,MINUS_ONE, false),
123 HOWTO(R_390_GOTPLTENT, 1, 4, 32, true, 0, complain_overflow_bitfield,
124 bfd_elf_generic_reloc, "R_390_GOTPLTENT",false, 0,MINUS_ONE, true),
125 HOWTO(R_390_PLTOFF16, 0, 2, 16, false, 0, complain_overflow_bitfield,
126 bfd_elf_generic_reloc, "R_390_PLTOFF16", false, 0,0x0000ffff, false),
127 HOWTO(R_390_PLTOFF32, 0, 4, 32, false, 0, complain_overflow_bitfield,
128 bfd_elf_generic_reloc, "R_390_PLTOFF32", false, 0,0xffffffff, false),
129 HOWTO(R_390_PLTOFF64, 0, 8, 64, false, 0, complain_overflow_bitfield,
130 bfd_elf_generic_reloc, "R_390_PLTOFF64", false, 0,MINUS_ONE, false),
131 HOWTO(R_390_TLS_LOAD, 0, 0, 0, false, 0, complain_overflow_dont,
132 s390_tls_reloc, "R_390_TLS_LOAD", false, 0, 0, false),
133 HOWTO(R_390_TLS_GDCALL, 0, 0, 0, false, 0, complain_overflow_dont,
134 s390_tls_reloc, "R_390_TLS_GDCALL", false, 0, 0, false),
135 HOWTO(R_390_TLS_LDCALL, 0, 0, 0, false, 0, complain_overflow_dont,
136 s390_tls_reloc, "R_390_TLS_LDCALL", false, 0, 0, false),
137 EMPTY_HOWTO (R_390_TLS_GD32), /* Empty entry for R_390_TLS_GD32. */
138 HOWTO(R_390_TLS_GD64, 0, 8, 64, false, 0, complain_overflow_bitfield,
139 bfd_elf_generic_reloc, "R_390_TLS_GD64", false, 0, MINUS_ONE, false),
140 HOWTO(R_390_TLS_GOTIE12, 0, 2, 12, false, 0, complain_overflow_dont,
141 bfd_elf_generic_reloc, "R_390_TLS_GOTIE12", false, 0, 0x00000fff, false),
142 EMPTY_HOWTO (R_390_TLS_GOTIE32), /* Empty entry for R_390_TLS_GOTIE32. */
143 HOWTO(R_390_TLS_GOTIE64, 0, 8, 64, false, 0, complain_overflow_bitfield,
144 bfd_elf_generic_reloc, "R_390_TLS_GOTIE64", false, 0, MINUS_ONE, false),
145 EMPTY_HOWTO (R_390_TLS_LDM32), /* Empty entry for R_390_TLS_LDM32. */
146 HOWTO(R_390_TLS_LDM64, 0, 8, 64, false, 0, complain_overflow_bitfield,
147 bfd_elf_generic_reloc, "R_390_TLS_LDM64", false, 0, MINUS_ONE, false),
148 EMPTY_HOWTO (R_390_TLS_IE32), /* Empty entry for R_390_TLS_IE32. */
149 HOWTO(R_390_TLS_IE64, 0, 8, 64, false, 0, complain_overflow_bitfield,
150 bfd_elf_generic_reloc, "R_390_TLS_IE64", false, 0, MINUS_ONE, false),
151 HOWTO(R_390_TLS_IEENT, 1, 4, 32, true, 0, complain_overflow_bitfield,
152 bfd_elf_generic_reloc, "R_390_TLS_IEENT", false, 0, MINUS_ONE, true),
153 EMPTY_HOWTO (R_390_TLS_LE32), /* Empty entry for R_390_TLS_LE32. */
154 HOWTO(R_390_TLS_LE64, 0, 4, 32, false, 0, complain_overflow_bitfield,
155 bfd_elf_generic_reloc, "R_390_TLS_LE64", false, 0, MINUS_ONE, false),
156 EMPTY_HOWTO (R_390_TLS_LDO32), /* Empty entry for R_390_TLS_LDO32. */
157 HOWTO(R_390_TLS_LDO64, 0, 8, 64, false, 0, complain_overflow_bitfield,
158 bfd_elf_generic_reloc, "R_390_TLS_LDO64", false, 0, MINUS_ONE, false),
159 HOWTO(R_390_TLS_DTPMOD, 0, 8, 64, false, 0, complain_overflow_bitfield,
160 bfd_elf_generic_reloc, "R_390_TLS_DTPMOD", false, 0, MINUS_ONE, false),
161 HOWTO(R_390_TLS_DTPOFF, 0, 8, 64, false, 0, complain_overflow_bitfield,
162 bfd_elf_generic_reloc, "R_390_TLS_DTPOFF", false, 0, MINUS_ONE, false),
163 HOWTO(R_390_TLS_TPOFF, 0, 8, 64, false, 0, complain_overflow_bitfield,
164 bfd_elf_generic_reloc, "R_390_TLS_TPOFF", false, 0, MINUS_ONE, false),
165 HOWTO(R_390_20, 0, 4, 20, false, 8, complain_overflow_dont,
166 s390_elf_ldisp_reloc, "R_390_20", false, 0,0x0fffff00, false),
167 HOWTO(R_390_GOT20, 0, 4, 20, false, 8, complain_overflow_dont,
168 s390_elf_ldisp_reloc, "R_390_GOT20", false, 0,0x0fffff00, false),
169 HOWTO(R_390_GOTPLT20, 0, 4, 20, false, 8, complain_overflow_dont,
170 s390_elf_ldisp_reloc, "R_390_GOTPLT20", false, 0,0x0fffff00, false),
171 HOWTO(R_390_TLS_GOTIE20, 0, 4, 20, false, 8, complain_overflow_dont,
172 s390_elf_ldisp_reloc, "R_390_TLS_GOTIE20", false, 0,0x0fffff00, false),
173 HOWTO(R_390_IRELATIVE, 0, 8, 64, false, 0, complain_overflow_bitfield,
174 bfd_elf_generic_reloc, "R_390_IRELATIVE", false, 0, MINUS_ONE, false),
175 HOWTO(R_390_PC12DBL, 1, 2, 12, true, 0, complain_overflow_bitfield,
176 bfd_elf_generic_reloc, "R_390_PC12DBL", false, 0,0x00000fff, true),
177 HOWTO(R_390_PLT12DBL, 1, 2, 12, true, 0, complain_overflow_bitfield,
178 bfd_elf_generic_reloc, "R_390_PLT12DBL", false, 0,0x00000fff, true),
179 HOWTO(R_390_PC24DBL, 1, 4, 24, true, 0, complain_overflow_bitfield,
180 bfd_elf_generic_reloc, "R_390_PC24DBL", false, 0,0x00ffffff, true),
181 HOWTO(R_390_PLT24DBL, 1, 4, 24, true, 0, complain_overflow_bitfield,
182 bfd_elf_generic_reloc, "R_390_PLT24DBL", false, 0,0x00ffffff, true),
183 };
184
185 /* GNU extension to record C++ vtable hierarchy. */
186 static reloc_howto_type elf64_s390_vtinherit_howto =
187 HOWTO (R_390_GNU_VTINHERIT, 0,8,0,false,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", false,0, 0, false);
188 static reloc_howto_type elf64_s390_vtentry_howto =
189 HOWTO (R_390_GNU_VTENTRY, 0,8,0,false,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_390_GNU_VTENTRY", false,0,0, false);
190
191 static reloc_howto_type *
192 elf_s390_reloc_type_lookup (bfd *abfd,
193 bfd_reloc_code_real_type code)
194 {
195 switch (code)
196 {
197 case BFD_RELOC_NONE:
198 return &elf_howto_table[(int) R_390_NONE];
199 case BFD_RELOC_8:
200 return &elf_howto_table[(int) R_390_8];
201 case BFD_RELOC_390_12:
202 return &elf_howto_table[(int) R_390_12];
203 case BFD_RELOC_16:
204 return &elf_howto_table[(int) R_390_16];
205 case BFD_RELOC_32:
206 return &elf_howto_table[(int) R_390_32];
207 case BFD_RELOC_CTOR:
208 return &elf_howto_table[(int) R_390_32];
209 case BFD_RELOC_32_PCREL:
210 return &elf_howto_table[(int) R_390_PC32];
211 case BFD_RELOC_390_GOT12:
212 return &elf_howto_table[(int) R_390_GOT12];
213 case BFD_RELOC_32_GOT_PCREL:
214 return &elf_howto_table[(int) R_390_GOT32];
215 case BFD_RELOC_390_PLT32:
216 return &elf_howto_table[(int) R_390_PLT32];
217 case BFD_RELOC_390_COPY:
218 return &elf_howto_table[(int) R_390_COPY];
219 case BFD_RELOC_390_GLOB_DAT:
220 return &elf_howto_table[(int) R_390_GLOB_DAT];
221 case BFD_RELOC_390_JMP_SLOT:
222 return &elf_howto_table[(int) R_390_JMP_SLOT];
223 case BFD_RELOC_390_RELATIVE:
224 return &elf_howto_table[(int) R_390_RELATIVE];
225 case BFD_RELOC_32_GOTOFF:
226 return &elf_howto_table[(int) R_390_GOTOFF32];
227 case BFD_RELOC_390_GOTPC:
228 return &elf_howto_table[(int) R_390_GOTPC];
229 case BFD_RELOC_390_GOT16:
230 return &elf_howto_table[(int) R_390_GOT16];
231 case BFD_RELOC_16_PCREL:
232 return &elf_howto_table[(int) R_390_PC16];
233 case BFD_RELOC_390_PC12DBL:
234 return &elf_howto_table[(int) R_390_PC12DBL];
235 case BFD_RELOC_390_PLT12DBL:
236 return &elf_howto_table[(int) R_390_PLT12DBL];
237 case BFD_RELOC_390_PC16DBL:
238 return &elf_howto_table[(int) R_390_PC16DBL];
239 case BFD_RELOC_390_PLT16DBL:
240 return &elf_howto_table[(int) R_390_PLT16DBL];
241 case BFD_RELOC_390_PC24DBL:
242 return &elf_howto_table[(int) R_390_PC24DBL];
243 case BFD_RELOC_390_PLT24DBL:
244 return &elf_howto_table[(int) R_390_PLT24DBL];
245 case BFD_RELOC_390_PC32DBL:
246 return &elf_howto_table[(int) R_390_PC32DBL];
247 case BFD_RELOC_390_PLT32DBL:
248 return &elf_howto_table[(int) R_390_PLT32DBL];
249 case BFD_RELOC_390_GOTPCDBL:
250 return &elf_howto_table[(int) R_390_GOTPCDBL];
251 case BFD_RELOC_64:
252 return &elf_howto_table[(int) R_390_64];
253 case BFD_RELOC_64_PCREL:
254 return &elf_howto_table[(int) R_390_PC64];
255 case BFD_RELOC_390_GOT64:
256 return &elf_howto_table[(int) R_390_GOT64];
257 case BFD_RELOC_390_PLT64:
258 return &elf_howto_table[(int) R_390_PLT64];
259 case BFD_RELOC_390_GOTENT:
260 return &elf_howto_table[(int) R_390_GOTENT];
261 case BFD_RELOC_16_GOTOFF:
262 return &elf_howto_table[(int) R_390_GOTOFF16];
263 case BFD_RELOC_390_GOTOFF64:
264 return &elf_howto_table[(int) R_390_GOTOFF64];
265 case BFD_RELOC_390_GOTPLT12:
266 return &elf_howto_table[(int) R_390_GOTPLT12];
267 case BFD_RELOC_390_GOTPLT16:
268 return &elf_howto_table[(int) R_390_GOTPLT16];
269 case BFD_RELOC_390_GOTPLT32:
270 return &elf_howto_table[(int) R_390_GOTPLT32];
271 case BFD_RELOC_390_GOTPLT64:
272 return &elf_howto_table[(int) R_390_GOTPLT64];
273 case BFD_RELOC_390_GOTPLTENT:
274 return &elf_howto_table[(int) R_390_GOTPLTENT];
275 case BFD_RELOC_390_PLTOFF16:
276 return &elf_howto_table[(int) R_390_PLTOFF16];
277 case BFD_RELOC_390_PLTOFF32:
278 return &elf_howto_table[(int) R_390_PLTOFF32];
279 case BFD_RELOC_390_PLTOFF64:
280 return &elf_howto_table[(int) R_390_PLTOFF64];
281 case BFD_RELOC_390_TLS_LOAD:
282 return &elf_howto_table[(int) R_390_TLS_LOAD];
283 case BFD_RELOC_390_TLS_GDCALL:
284 return &elf_howto_table[(int) R_390_TLS_GDCALL];
285 case BFD_RELOC_390_TLS_LDCALL:
286 return &elf_howto_table[(int) R_390_TLS_LDCALL];
287 case BFD_RELOC_390_TLS_GD64:
288 return &elf_howto_table[(int) R_390_TLS_GD64];
289 case BFD_RELOC_390_TLS_GOTIE12:
290 return &elf_howto_table[(int) R_390_TLS_GOTIE12];
291 case BFD_RELOC_390_TLS_GOTIE64:
292 return &elf_howto_table[(int) R_390_TLS_GOTIE64];
293 case BFD_RELOC_390_TLS_LDM64:
294 return &elf_howto_table[(int) R_390_TLS_LDM64];
295 case BFD_RELOC_390_TLS_IE64:
296 return &elf_howto_table[(int) R_390_TLS_IE64];
297 case BFD_RELOC_390_TLS_IEENT:
298 return &elf_howto_table[(int) R_390_TLS_IEENT];
299 case BFD_RELOC_390_TLS_LE64:
300 return &elf_howto_table[(int) R_390_TLS_LE64];
301 case BFD_RELOC_390_TLS_LDO64:
302 return &elf_howto_table[(int) R_390_TLS_LDO64];
303 case BFD_RELOC_390_TLS_DTPMOD:
304 return &elf_howto_table[(int) R_390_TLS_DTPMOD];
305 case BFD_RELOC_390_TLS_DTPOFF:
306 return &elf_howto_table[(int) R_390_TLS_DTPOFF];
307 case BFD_RELOC_390_TLS_TPOFF:
308 return &elf_howto_table[(int) R_390_TLS_TPOFF];
309 case BFD_RELOC_390_20:
310 return &elf_howto_table[(int) R_390_20];
311 case BFD_RELOC_390_GOT20:
312 return &elf_howto_table[(int) R_390_GOT20];
313 case BFD_RELOC_390_GOTPLT20:
314 return &elf_howto_table[(int) R_390_GOTPLT20];
315 case BFD_RELOC_390_TLS_GOTIE20:
316 return &elf_howto_table[(int) R_390_TLS_GOTIE20];
317 case BFD_RELOC_390_IRELATIVE:
318 return &elf_howto_table[(int) R_390_IRELATIVE];
319 case BFD_RELOC_VTABLE_INHERIT:
320 return &elf64_s390_vtinherit_howto;
321 case BFD_RELOC_VTABLE_ENTRY:
322 return &elf64_s390_vtentry_howto;
323 default:
324 break;
325 }
326
327 /* xgettext:c-format */
328 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
329 bfd_set_error (bfd_error_bad_value);
330 return NULL;
331 }
332
333 static reloc_howto_type *
334 elf_s390_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
335 const char *r_name)
336 {
337 unsigned int i;
338
339 for (i = 0;
340 i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]);
341 i++)
342 if (elf_howto_table[i].name != NULL
343 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
344 return &elf_howto_table[i];
345
346 if (strcasecmp (elf64_s390_vtinherit_howto.name, r_name) == 0)
347 return &elf64_s390_vtinherit_howto;
348 if (strcasecmp (elf64_s390_vtentry_howto.name, r_name) == 0)
349 return &elf64_s390_vtentry_howto;
350
351 return NULL;
352 }
353
354 /* We need to use ELF64_R_TYPE so we have our own copy of this function,
355 and elf64-s390.c has its own copy. */
356
357 static bool
358 elf_s390_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
359 arelent *cache_ptr,
360 Elf_Internal_Rela *dst)
361 {
362 unsigned int r_type = ELF64_R_TYPE(dst->r_info);
363
364 switch (r_type)
365 {
366 case R_390_GNU_VTINHERIT:
367 cache_ptr->howto = &elf64_s390_vtinherit_howto;
368 break;
369
370 case R_390_GNU_VTENTRY:
371 cache_ptr->howto = &elf64_s390_vtentry_howto;
372 break;
373
374 default:
375 if (r_type >= sizeof (elf_howto_table) / sizeof (elf_howto_table[0]))
376 {
377 /* xgettext:c-format */
378 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
379 abfd, r_type);
380 bfd_set_error (bfd_error_bad_value);
381 return false;
382 }
383 cache_ptr->howto = &elf_howto_table[r_type];
384 }
385 return true;
386 }
387
388 /* A relocation function which doesn't do anything. */
389 static bfd_reloc_status_type
390 s390_tls_reloc (bfd *abfd ATTRIBUTE_UNUSED,
391 arelent *reloc_entry,
392 asymbol *symbol ATTRIBUTE_UNUSED,
393 void * data ATTRIBUTE_UNUSED,
394 asection *input_section,
395 bfd *output_bfd,
396 char **error_message ATTRIBUTE_UNUSED)
397 {
398 if (output_bfd)
399 reloc_entry->address += input_section->output_offset;
400 return bfd_reloc_ok;
401 }
402
403 /* Handle the large displacement relocs. */
404 static bfd_reloc_status_type
405 s390_elf_ldisp_reloc (bfd *abfd,
406 arelent *reloc_entry,
407 asymbol *symbol,
408 void * data,
409 asection *input_section,
410 bfd *output_bfd,
411 char **error_message ATTRIBUTE_UNUSED)
412 {
413 reloc_howto_type *howto = reloc_entry->howto;
414 bfd_vma relocation;
415 bfd_vma insn;
416
417 if (output_bfd != (bfd *) NULL
418 && (symbol->flags & BSF_SECTION_SYM) == 0
419 && (! howto->partial_inplace
420 || reloc_entry->addend == 0))
421 {
422 reloc_entry->address += input_section->output_offset;
423 return bfd_reloc_ok;
424 }
425 if (output_bfd != NULL)
426 return bfd_reloc_continue;
427
428 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
429 return bfd_reloc_outofrange;
430
431 relocation = (symbol->value
432 + symbol->section->output_section->vma
433 + symbol->section->output_offset);
434 relocation += reloc_entry->addend;
435 if (howto->pc_relative)
436 {
437 relocation -= (input_section->output_section->vma
438 + input_section->output_offset);
439 relocation -= reloc_entry->address;
440 }
441
442 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
443 insn |= (relocation & 0xfff) << 16 | (relocation & 0xff000) >> 4;
444 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
445
446 if ((bfd_signed_vma) relocation < - 0x80000
447 || (bfd_signed_vma) relocation > 0x7ffff)
448 return bfd_reloc_overflow;
449 else
450 return bfd_reloc_ok;
451 }
452
453 static bool
454 elf_s390_is_local_label_name (bfd *abfd, const char *name)
455 {
456 if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L'))
457 return true;
458
459 return _bfd_elf_is_local_label_name (abfd, name);
460 }
461
462 /* Functions for the 390 ELF linker. */
463
464 /* The name of the dynamic interpreter. This is put in the .interp
465 section. */
466
467 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
468
469 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
470 copying dynamic variables from a shared lib into an app's dynbss
471 section, and instead use a dynamic relocation to point into the
472 shared lib. */
473 #define ELIMINATE_COPY_RELOCS 1
474
475 /* The size in bytes of the first entry in the procedure linkage table. */
476 #define PLT_FIRST_ENTRY_SIZE 32
477 /* The size in bytes of an entry in the procedure linkage table. */
478 #define PLT_ENTRY_SIZE 32
479
480 #define GOT_ENTRY_SIZE 8
481
482 #define RELA_ENTRY_SIZE sizeof (Elf64_External_Rela)
483
484 /* The first three entries in a global offset table are reserved,
485 and the initial contents are unimportant (we zero them out).
486 Subsequent entries look like this. See the SVR4 ABI 386
487 supplement to see how this works. */
488
489 /* For the s390, simple addr offset can only be 0 - 4096.
490 To use the full 16777216 TB address space, several instructions
491 are needed to load an address in a register and execute
492 a branch( or just saving the address)
493
494 Furthermore, only r 0 and 1 are free to use!!! */
495
496 /* The first 3 words in the GOT are then reserved.
497 Word 0 is the address of the dynamic table.
498 Word 1 is a pointer to a structure describing the object
499 Word 2 is used to point to the loader entry address.
500
501 The code for PLT entries looks like this:
502
503 The GOT holds the address in the PLT to be executed.
504 The loader then gets:
505 48(15) = Pointer to the structure describing the object.
506 56(15) = Offset in symbol table
507 The loader must then find the module where the function is
508 and insert the address in the GOT.
509
510 PLT1: LARL 1,<fn>@GOTENT # 6 bytes Load address of GOT entry in r1
511 LG 1,0(1) # 6 bytes Load address from GOT in r1
512 BCR 15,1 # 2 bytes Jump to address
513 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
514 LGF 1,12(1) # 6 bytes Load rela.plt offset into r1
515 BRCL 15,-x # 6 bytes Jump to first PLT entry
516 .long ? # 4 bytes offset into .rela.plt
517
518 Total = 32 bytes per PLT entry
519 Fixup at offset 2: relative address to GOT entry
520 Fixup at offset 22: relative branch to PLT0
521 Fixup at offset 28: 32 bit offset into .rela.plt
522
523 A 32 bit offset into the symbol table is enough. It allows for
524 .rela.plt sections up to a size of 2 gigabyte. A single dynamic
525 object (the main program, any shared library) is limited to 4GB in
526 size. Having a .rela.plt of 2GB would already make the .plt
527 section bigger than 8GB. */
528
529 static const bfd_byte elf_s390x_plt_entry[PLT_ENTRY_SIZE] =
530 {
531 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, /* larl %r1,. */
532 0xe3, 0x10, 0x10, 0x00, 0x00, 0x04, /* lg %r1,0(%r1) */
533 0x07, 0xf1, /* br %r1 */
534 0x0d, 0x10, /* basr %r1,%r0 */
535 0xe3, 0x10, 0x10, 0x0c, 0x00, 0x14, /* lgf %r1,12(%r1) */
536 0xc0, 0xf4, 0x00, 0x00, 0x00, 0x00, /* jg first plt */
537 0x00, 0x00, 0x00, 0x00 /* .long 0x00000000 */
538 };
539
540 /* The first PLT entry pushes the offset into the symbol table
541 from R1 onto the stack at 56(15) and the loader object info
542 at 48(15), loads the loader address in R1 and jumps to it. */
543
544 /* The first entry in the PLT:
545
546 PLT0:
547 STG 1,56(15) # r1 contains the offset into the symbol table
548 LARL 1,_GLOBAL_OFFSET_TABLE # load address of global offset table
549 MVC 48(8,15),8(1) # move loader ino (object struct address) to stack
550 LG 1,16(1) # get entry address of loader
551 BCR 15,1 # jump to loader
552
553 Fixup at offset 8: relative address to start of GOT. */
554
555 static const bfd_byte elf_s390x_first_plt_entry[PLT_FIRST_ENTRY_SIZE] =
556 {
557 0xe3, 0x10, 0xf0, 0x38, 0x00, 0x24, /* stg %r1,56(%r15) */
558 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, /* larl %r1,. */
559 0xd2, 0x07, 0xf0, 0x30, 0x10, 0x08, /* mvc 48(8,%r15),8(%r1) */
560 0xe3, 0x10, 0x10, 0x10, 0x00, 0x04, /* lg %r1,16(%r1) */
561 0x07, 0xf1, /* br %r1 */
562 0x07, 0x00, /* nopr %r0 */
563 0x07, 0x00, /* nopr %r0 */
564 0x07, 0x00 /* nopr %r0 */
565 };
566
567
568 /* s390 ELF linker hash entry. */
569
570 struct elf_s390_link_hash_entry
571 {
572 struct elf_link_hash_entry elf;
573
574 /* Number of GOTPLT references for a function. */
575 bfd_signed_vma gotplt_refcount;
576
577 #define GOT_UNKNOWN 0
578 #define GOT_NORMAL 1
579 #define GOT_TLS_GD 2
580 #define GOT_TLS_IE 3
581 #define GOT_TLS_IE_NLT 3
582 unsigned char tls_type;
583
584 /* For pointer equality reasons we might need to change the symbol
585 type from STT_GNU_IFUNC to STT_FUNC together with its value and
586 section entry. So after alloc_dynrelocs only these values should
587 be used. In order to check whether a symbol is IFUNC use
588 s390_is_ifunc_symbol_p. */
589 bfd_vma ifunc_resolver_address;
590 asection *ifunc_resolver_section;
591 };
592
593 #define elf_s390_hash_entry(ent) \
594 ((struct elf_s390_link_hash_entry *)(ent))
595
596 /* This structure represents an entry in the local PLT list needed for
597 local IFUNC symbols. */
598 struct plt_entry
599 {
600 /* The section of the local symbol.
601 Set in relocate_section and used in finish_dynamic_sections. */
602 asection *sec;
603
604 union
605 {
606 bfd_signed_vma refcount;
607 bfd_vma offset;
608 } plt;
609 };
610
611 /* NOTE: Keep this structure in sync with
612 the one declared in elf32-s390.c. */
613 struct elf_s390_obj_tdata
614 {
615 struct elf_obj_tdata root;
616
617 /* A local PLT is needed for ifunc symbols. */
618 struct plt_entry *local_plt;
619
620 /* TLS type for each local got entry. */
621 char *local_got_tls_type;
622 };
623
624 #define elf_s390_tdata(abfd) \
625 ((struct elf_s390_obj_tdata *) (abfd)->tdata.any)
626
627 #define elf_s390_local_plt(abfd) \
628 (elf_s390_tdata (abfd)->local_plt)
629
630 #define elf_s390_local_got_tls_type(abfd) \
631 (elf_s390_tdata (abfd)->local_got_tls_type)
632
633 #define is_s390_elf(bfd) \
634 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
635 && elf_tdata (bfd) != NULL \
636 && elf_object_id (bfd) == S390_ELF_DATA)
637
638 static bool
639 elf_s390_mkobject (bfd *abfd)
640 {
641 return bfd_elf_allocate_object (abfd, sizeof (struct elf_s390_obj_tdata));
642 }
643
644 static bool
645 elf_s390_object_p (bfd *abfd)
646 {
647 /* Set the right machine number for an s390 elf32 file. */
648 return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_64);
649 }
650
651 /* s390 ELF linker hash table. */
652
653 struct elf_s390_link_hash_table
654 {
655 struct elf_link_hash_table elf;
656
657 /* Short-cuts to get to dynamic linker sections. */
658 asection *irelifunc;
659
660 union {
661 bfd_signed_vma refcount;
662 bfd_vma offset;
663 } tls_ldm_got;
664
665 /* Options passed from the linker. */
666 struct s390_elf_params *params;
667 };
668
669 /* Get the s390 ELF linker hash table from a link_info structure. */
670
671 #define elf_s390_hash_table(p) \
672 ((is_elf_hash_table ((p)->hash) \
673 && elf_hash_table_id (elf_hash_table (p)) == S390_ELF_DATA) \
674 ? (struct elf_s390_link_hash_table *) (p)->hash : NULL)
675
676 #define ELF64 1
677 #include "elf-s390-common.c"
678
679 /* Create an entry in an s390 ELF linker hash table. */
680
681 static struct bfd_hash_entry *
682 link_hash_newfunc (struct bfd_hash_entry *entry,
683 struct bfd_hash_table *table,
684 const char *string)
685 {
686 /* Allocate the structure if it has not already been allocated by a
687 subclass. */
688 if (entry == NULL)
689 {
690 entry = bfd_hash_allocate (table,
691 sizeof (struct elf_s390_link_hash_entry));
692 if (entry == NULL)
693 return entry;
694 }
695
696 /* Call the allocation method of the superclass. */
697 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
698 if (entry != NULL)
699 {
700 struct elf_s390_link_hash_entry *eh;
701
702 eh = (struct elf_s390_link_hash_entry *) entry;
703 eh->gotplt_refcount = 0;
704 eh->tls_type = GOT_UNKNOWN;
705 eh->ifunc_resolver_address = 0;
706 eh->ifunc_resolver_section = NULL;
707 }
708
709 return entry;
710 }
711
712 /* Create an s390 ELF linker hash table. */
713
714 static struct bfd_link_hash_table *
715 elf_s390_link_hash_table_create (bfd *abfd)
716 {
717 struct elf_s390_link_hash_table *ret;
718 size_t amt = sizeof (struct elf_s390_link_hash_table);
719
720 ret = (struct elf_s390_link_hash_table *) bfd_zmalloc (amt);
721 if (ret == NULL)
722 return NULL;
723
724 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
725 sizeof (struct elf_s390_link_hash_entry)))
726 {
727 free (ret);
728 return NULL;
729 }
730
731 return &ret->elf.root;
732 }
733
734 /* Copy the extra info we tack onto an elf_link_hash_entry. */
735
736 static void
737 elf_s390_copy_indirect_symbol (struct bfd_link_info *info,
738 struct elf_link_hash_entry *dir,
739 struct elf_link_hash_entry *ind)
740 {
741 struct elf_s390_link_hash_entry *edir, *eind;
742
743 edir = (struct elf_s390_link_hash_entry *) dir;
744 eind = (struct elf_s390_link_hash_entry *) ind;
745
746 if (ind->root.type == bfd_link_hash_indirect
747 && dir->got.refcount <= 0)
748 {
749 edir->tls_type = eind->tls_type;
750 eind->tls_type = GOT_UNKNOWN;
751 }
752
753 if (ELIMINATE_COPY_RELOCS
754 && ind->root.type != bfd_link_hash_indirect
755 && dir->dynamic_adjusted)
756 {
757 /* If called to transfer flags for a weakdef during processing
758 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
759 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
760 if (dir->versioned != versioned_hidden)
761 dir->ref_dynamic |= ind->ref_dynamic;
762 dir->ref_regular |= ind->ref_regular;
763 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
764 dir->needs_plt |= ind->needs_plt;
765 }
766 else
767 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
768 }
769
770 static int
771 elf_s390_tls_transition (struct bfd_link_info *info,
772 int r_type,
773 int is_local)
774 {
775 if (bfd_link_dll (info))
776 return r_type;
777
778 switch (r_type)
779 {
780 case R_390_TLS_GD64:
781 case R_390_TLS_IE64:
782 if (is_local)
783 return R_390_TLS_LE64;
784 return R_390_TLS_IE64;
785 case R_390_TLS_GOTIE64:
786 if (is_local)
787 return R_390_TLS_LE64;
788 return R_390_TLS_GOTIE64;
789 case R_390_TLS_LDM64:
790 return R_390_TLS_LE64;
791 }
792
793 return r_type;
794 }
795
796 /* Look through the relocs for a section during the first phase, and
797 allocate space in the global offset table or procedure linkage
798 table. */
799
800 static bool
801 elf_s390_check_relocs (bfd *abfd,
802 struct bfd_link_info *info,
803 asection *sec,
804 const Elf_Internal_Rela *relocs)
805 {
806 struct elf_s390_link_hash_table *htab;
807 Elf_Internal_Shdr *symtab_hdr;
808 struct elf_link_hash_entry **sym_hashes;
809 const Elf_Internal_Rela *rel;
810 const Elf_Internal_Rela *rel_end;
811 asection *sreloc;
812 bfd_signed_vma *local_got_refcounts;
813 int tls_type, old_tls_type;
814
815 if (bfd_link_relocatable (info))
816 return true;
817
818 BFD_ASSERT (is_s390_elf (abfd));
819
820 htab = elf_s390_hash_table (info);
821 if (htab == NULL)
822 return false;
823
824 symtab_hdr = &elf_symtab_hdr (abfd);
825 sym_hashes = elf_sym_hashes (abfd);
826 local_got_refcounts = elf_local_got_refcounts (abfd);
827
828 sreloc = NULL;
829
830 rel_end = relocs + sec->reloc_count;
831 for (rel = relocs; rel < rel_end; rel++)
832 {
833 unsigned int r_type;
834 unsigned int r_symndx;
835 struct elf_link_hash_entry *h;
836 Elf_Internal_Sym *isym;
837
838 r_symndx = ELF64_R_SYM (rel->r_info);
839
840 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
841 {
842 /* xgettext:c-format */
843 _bfd_error_handler (_("%pB: bad symbol index: %d"),
844 abfd, r_symndx);
845 return false;
846 }
847
848 if (r_symndx < symtab_hdr->sh_info)
849 {
850 /* A local symbol. */
851 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
852 abfd, r_symndx);
853 if (isym == NULL)
854 return false;
855
856 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
857 {
858 struct plt_entry *plt;
859
860 if (htab->elf.dynobj == NULL)
861 htab->elf.dynobj = abfd;
862
863 if (!s390_elf_create_ifunc_sections (htab->elf.dynobj, info))
864 return false;
865
866 if (local_got_refcounts == NULL)
867 {
868 if (!elf_s390_allocate_local_syminfo (abfd, symtab_hdr))
869 return false;
870 local_got_refcounts = elf_local_got_refcounts (abfd);
871 }
872 plt = elf_s390_local_plt (abfd);
873 plt[r_symndx].plt.refcount++;
874 }
875 h = NULL;
876 }
877 else
878 {
879 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
880 while (h->root.type == bfd_link_hash_indirect
881 || h->root.type == bfd_link_hash_warning)
882 h = (struct elf_link_hash_entry *) h->root.u.i.link;
883 }
884
885 /* Create got section and local_got_refcounts array if they
886 are needed. */
887 r_type = elf_s390_tls_transition (info,
888 ELF64_R_TYPE (rel->r_info),
889 h == NULL);
890 switch (r_type)
891 {
892 case R_390_GOT12:
893 case R_390_GOT16:
894 case R_390_GOT20:
895 case R_390_GOT32:
896 case R_390_GOT64:
897 case R_390_GOTENT:
898 case R_390_GOTPLT12:
899 case R_390_GOTPLT16:
900 case R_390_GOTPLT20:
901 case R_390_GOTPLT32:
902 case R_390_GOTPLT64:
903 case R_390_GOTPLTENT:
904 case R_390_TLS_GD64:
905 case R_390_TLS_GOTIE12:
906 case R_390_TLS_GOTIE20:
907 case R_390_TLS_GOTIE64:
908 case R_390_TLS_IEENT:
909 case R_390_TLS_IE64:
910 case R_390_TLS_LDM64:
911 if (h == NULL
912 && local_got_refcounts == NULL)
913 {
914 if (!elf_s390_allocate_local_syminfo (abfd, symtab_hdr))
915 return false;
916 local_got_refcounts = elf_local_got_refcounts (abfd);
917 }
918
919 /* Fall through. */
920 case R_390_GOTOFF16:
921 case R_390_GOTOFF32:
922 case R_390_GOTOFF64:
923 case R_390_GOTPC:
924 case R_390_GOTPCDBL:
925 if (htab->elf.sgot == NULL)
926 {
927 if (htab->elf.dynobj == NULL)
928 htab->elf.dynobj = abfd;
929 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
930 return false;
931 }
932 }
933
934 if (h != NULL)
935 {
936 if (htab->elf.dynobj == NULL)
937 htab->elf.dynobj = abfd;
938 if (!s390_elf_create_ifunc_sections (htab->elf.dynobj, info))
939 return false;
940
941 /* Make sure an IFUNC symbol defined in a non-shared object
942 always gets a PLT slot. */
943 if (s390_is_ifunc_symbol_p (h) && h->def_regular)
944 {
945 /* The symbol is called by the dynamic loader in order
946 to resolve the relocation. So it is in fact also
947 referenced. */
948 h->ref_regular = 1;
949 h->needs_plt = 1;
950 }
951 }
952
953 switch (r_type)
954 {
955 case R_390_GOTPC:
956 case R_390_GOTPCDBL:
957 /* These relocs do not need a GOT slot. They just load the
958 GOT pointer itself or address something else relative to
959 the GOT. Since the GOT pointer has been set up above we
960 are done. */
961 break;
962 case R_390_GOTOFF16:
963 case R_390_GOTOFF32:
964 case R_390_GOTOFF64:
965 if (h == NULL || !s390_is_ifunc_symbol_p (h) || !h->def_regular)
966 break;
967 /* Fall through. */
968
969 case R_390_PLT12DBL:
970 case R_390_PLT16DBL:
971 case R_390_PLT24DBL:
972 case R_390_PLT32:
973 case R_390_PLT32DBL:
974 case R_390_PLT64:
975 case R_390_PLTOFF16:
976 case R_390_PLTOFF32:
977 case R_390_PLTOFF64:
978 /* This symbol requires a procedure linkage table entry. We
979 actually build the entry in adjust_dynamic_symbol,
980 because this might be a case of linking PIC code which is
981 never referenced by a dynamic object, in which case we
982 don't need to generate a procedure linkage table entry
983 after all. */
984
985 /* If this is a local symbol, we resolve it directly without
986 creating a procedure linkage table entry. */
987 if (h != NULL)
988 {
989 h->needs_plt = 1;
990 h->plt.refcount += 1;
991 }
992 break;
993
994 case R_390_GOTPLT12:
995 case R_390_GOTPLT16:
996 case R_390_GOTPLT20:
997 case R_390_GOTPLT32:
998 case R_390_GOTPLT64:
999 case R_390_GOTPLTENT:
1000 /* This symbol requires either a procedure linkage table entry
1001 or an entry in the local got. We actually build the entry
1002 in adjust_dynamic_symbol because whether this is really a
1003 global reference can change and with it the fact if we have
1004 to create a plt entry or a local got entry. To be able to
1005 make a once global symbol a local one we have to keep track
1006 of the number of gotplt references that exist for this
1007 symbol. */
1008 if (h != NULL)
1009 {
1010 ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount++;
1011 h->needs_plt = 1;
1012 h->plt.refcount += 1;
1013 }
1014 else
1015 local_got_refcounts[r_symndx] += 1;
1016 break;
1017
1018 case R_390_TLS_LDM64:
1019 htab->tls_ldm_got.refcount += 1;
1020 break;
1021
1022 case R_390_TLS_IE64:
1023 case R_390_TLS_GOTIE12:
1024 case R_390_TLS_GOTIE20:
1025 case R_390_TLS_GOTIE64:
1026 case R_390_TLS_IEENT:
1027 if (bfd_link_dll (info))
1028 info->flags |= DF_STATIC_TLS;
1029 /* Fall through */
1030
1031 case R_390_GOT12:
1032 case R_390_GOT16:
1033 case R_390_GOT20:
1034 case R_390_GOT32:
1035 case R_390_GOT64:
1036 case R_390_GOTENT:
1037 case R_390_TLS_GD64:
1038 /* This symbol requires a global offset table entry. */
1039 switch (r_type)
1040 {
1041 default:
1042 case R_390_GOT12:
1043 case R_390_GOT16:
1044 case R_390_GOT20:
1045 case R_390_GOT32:
1046 case R_390_GOTENT:
1047 tls_type = GOT_NORMAL;
1048 break;
1049 case R_390_TLS_GD64:
1050 tls_type = GOT_TLS_GD;
1051 break;
1052 case R_390_TLS_IE64:
1053 case R_390_TLS_GOTIE64:
1054 tls_type = GOT_TLS_IE;
1055 break;
1056 case R_390_TLS_GOTIE12:
1057 case R_390_TLS_GOTIE20:
1058 case R_390_TLS_IEENT:
1059 tls_type = GOT_TLS_IE_NLT;
1060 break;
1061 }
1062
1063 if (h != NULL)
1064 {
1065 h->got.refcount += 1;
1066 old_tls_type = elf_s390_hash_entry(h)->tls_type;
1067 }
1068 else
1069 {
1070 local_got_refcounts[r_symndx] += 1;
1071 old_tls_type = elf_s390_local_got_tls_type (abfd) [r_symndx];
1072 }
1073 /* If a TLS symbol is accessed using IE at least once,
1074 there is no point to use dynamic model for it. */
1075 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN)
1076 {
1077 if (old_tls_type == GOT_NORMAL || tls_type == GOT_NORMAL)
1078 {
1079 _bfd_error_handler
1080 /* xgettext:c-format */
1081 (_("%pB: `%s' accessed both as normal and thread local symbol"),
1082 abfd, h->root.root.string);
1083 return false;
1084 }
1085 if (old_tls_type > tls_type)
1086 tls_type = old_tls_type;
1087 }
1088
1089 if (old_tls_type != tls_type)
1090 {
1091 if (h != NULL)
1092 elf_s390_hash_entry (h)->tls_type = tls_type;
1093 else
1094 elf_s390_local_got_tls_type (abfd) [r_symndx] = tls_type;
1095 }
1096
1097 if (r_type != R_390_TLS_IE64)
1098 break;
1099 /* Fall through */
1100
1101 case R_390_TLS_LE64:
1102 /* For static linking and executables this reloc will be
1103 calculated at linktime otherwise a TLS_TPOFF runtime
1104 reloc will be generated. */
1105 if (r_type == R_390_TLS_LE64 && bfd_link_pie (info))
1106 break;
1107
1108 if (!bfd_link_dll (info))
1109 break;
1110 info->flags |= DF_STATIC_TLS;
1111 /* Fall through */
1112
1113 case R_390_8:
1114 case R_390_16:
1115 case R_390_32:
1116 case R_390_64:
1117 case R_390_PC12DBL:
1118 case R_390_PC16:
1119 case R_390_PC16DBL:
1120 case R_390_PC24DBL:
1121 case R_390_PC32:
1122 case R_390_PC32DBL:
1123 case R_390_PC64:
1124 if (h != NULL && bfd_link_executable (info))
1125 {
1126 /* If this reloc is in a read-only section, we might
1127 need a copy reloc. We can't check reliably at this
1128 stage whether the section is read-only, as input
1129 sections have not yet been mapped to output sections.
1130 Tentatively set the flag for now, and correct in
1131 adjust_dynamic_symbol. */
1132 h->non_got_ref = 1;
1133
1134 if (!bfd_link_pic (info))
1135 {
1136 /* We may need a .plt entry if the function this reloc
1137 refers to is in a shared lib. */
1138 h->plt.refcount += 1;
1139 }
1140 }
1141
1142 /* If we are creating a shared library, and this is a reloc
1143 against a global symbol, or a non PC relative reloc
1144 against a local symbol, then we need to copy the reloc
1145 into the shared library. However, if we are linking with
1146 -Bsymbolic, we do not need to copy a reloc against a
1147 global symbol which is defined in an object we are
1148 including in the link (i.e., DEF_REGULAR is set). At
1149 this point we have not seen all the input files, so it is
1150 possible that DEF_REGULAR is not set now but will be set
1151 later (it is never cleared). In case of a weak definition,
1152 DEF_REGULAR may be cleared later by a strong definition in
1153 a shared library. We account for that possibility below by
1154 storing information in the relocs_copied field of the hash
1155 table entry. A similar situation occurs when creating
1156 shared libraries and symbol visibility changes render the
1157 symbol local.
1158
1159 If on the other hand, we are creating an executable, we
1160 may need to keep relocations for symbols satisfied by a
1161 dynamic library if we manage to avoid copy relocs for the
1162 symbol. */
1163 if ((bfd_link_pic (info)
1164 && (sec->flags & SEC_ALLOC) != 0
1165 && ((ELF64_R_TYPE (rel->r_info) != R_390_PC16
1166 && ELF64_R_TYPE (rel->r_info) != R_390_PC12DBL
1167 && ELF64_R_TYPE (rel->r_info) != R_390_PC16DBL
1168 && ELF64_R_TYPE (rel->r_info) != R_390_PC24DBL
1169 && ELF64_R_TYPE (rel->r_info) != R_390_PC32
1170 && ELF64_R_TYPE (rel->r_info) != R_390_PC32DBL
1171 && ELF64_R_TYPE (rel->r_info) != R_390_PC64)
1172 || (h != NULL
1173 && (! SYMBOLIC_BIND (info, h)
1174 || h->root.type == bfd_link_hash_defweak
1175 || !h->def_regular))))
1176 || (ELIMINATE_COPY_RELOCS
1177 && !bfd_link_pic (info)
1178 && (sec->flags & SEC_ALLOC) != 0
1179 && h != NULL
1180 && (h->root.type == bfd_link_hash_defweak
1181 || !h->def_regular)))
1182 {
1183 struct elf_dyn_relocs *p;
1184 struct elf_dyn_relocs **head;
1185
1186 /* We must copy these reloc types into the output file.
1187 Create a reloc section in dynobj and make room for
1188 this reloc. */
1189 if (sreloc == NULL)
1190 {
1191 if (htab->elf.dynobj == NULL)
1192 htab->elf.dynobj = abfd;
1193
1194 sreloc = _bfd_elf_make_dynamic_reloc_section
1195 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
1196
1197 if (sreloc == NULL)
1198 return false;
1199 }
1200
1201 /* If this is a global symbol, we count the number of
1202 relocations we need for this symbol. */
1203 if (h != NULL)
1204 {
1205 head = &h->dyn_relocs;
1206 }
1207 else
1208 {
1209 /* Track dynamic relocs needed for local syms too.
1210 We really need local syms available to do this
1211 easily. Oh well. */
1212 asection *s;
1213 void *vpp;
1214
1215 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1216 abfd, r_symndx);
1217 if (isym == NULL)
1218 return false;
1219
1220 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1221 if (s == NULL)
1222 s = sec;
1223
1224 vpp = &elf_section_data (s)->local_dynrel;
1225 head = (struct elf_dyn_relocs **) vpp;
1226 }
1227
1228 p = *head;
1229 if (p == NULL || p->sec != sec)
1230 {
1231 size_t amt = sizeof *p;
1232 p = ((struct elf_dyn_relocs *)
1233 bfd_alloc (htab->elf.dynobj, amt));
1234 if (p == NULL)
1235 return false;
1236 p->next = *head;
1237 *head = p;
1238 p->sec = sec;
1239 p->count = 0;
1240 p->pc_count = 0;
1241 }
1242
1243 p->count += 1;
1244 if (ELF64_R_TYPE (rel->r_info) == R_390_PC16
1245 || ELF64_R_TYPE (rel->r_info) == R_390_PC12DBL
1246 || ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL
1247 || ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL
1248 || ELF64_R_TYPE (rel->r_info) == R_390_PC32
1249 || ELF64_R_TYPE (rel->r_info) == R_390_PC32DBL
1250 || ELF64_R_TYPE (rel->r_info) == R_390_PC64)
1251 p->pc_count += 1;
1252 }
1253 break;
1254
1255 /* This relocation describes the C++ object vtable hierarchy.
1256 Reconstruct it for later use during GC. */
1257 case R_390_GNU_VTINHERIT:
1258 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1259 return false;
1260 break;
1261
1262 /* This relocation describes which C++ vtable entries are actually
1263 used. Record for later use during GC. */
1264 case R_390_GNU_VTENTRY:
1265 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1266 return false;
1267 break;
1268
1269 default:
1270 break;
1271 }
1272 }
1273
1274 return true;
1275 }
1276
1277 /* Return the section that should be marked against GC for a given
1278 relocation. */
1279
1280 static asection *
1281 elf_s390_gc_mark_hook (asection *sec,
1282 struct bfd_link_info *info,
1283 Elf_Internal_Rela *rel,
1284 struct elf_link_hash_entry *h,
1285 Elf_Internal_Sym *sym)
1286 {
1287 if (h != NULL)
1288 switch (ELF64_R_TYPE (rel->r_info))
1289 {
1290 case R_390_GNU_VTINHERIT:
1291 case R_390_GNU_VTENTRY:
1292 return NULL;
1293 }
1294
1295 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1296 }
1297
1298 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1299 entry but we found we will not create any. Called when we find we will
1300 not have any PLT for this symbol, by for example
1301 elf_s390_adjust_dynamic_symbol when we're doing a proper dynamic link,
1302 or elf_s390_late_size_sections if no dynamic sections will be
1303 created (we're only linking static objects). */
1304
1305 static void
1306 elf_s390_adjust_gotplt (struct elf_s390_link_hash_entry *h)
1307 {
1308 if (h->elf.root.type == bfd_link_hash_warning)
1309 h = (struct elf_s390_link_hash_entry *) h->elf.root.u.i.link;
1310
1311 if (h->gotplt_refcount <= 0)
1312 return;
1313
1314 /* We simply add the number of gotplt references to the number
1315 * of got references for this symbol. */
1316 h->elf.got.refcount += h->gotplt_refcount;
1317 h->gotplt_refcount = -1;
1318 }
1319
1320 /* Adjust a symbol defined by a dynamic object and referenced by a
1321 regular object. The current definition is in some section of the
1322 dynamic object, but we're not including those sections. We have to
1323 change the definition to something the rest of the link can
1324 understand. */
1325
1326 static bool
1327 elf_s390_adjust_dynamic_symbol (struct bfd_link_info *info,
1328 struct elf_link_hash_entry *h)
1329 {
1330 struct elf_s390_link_hash_table *htab;
1331 asection *s, *srel;
1332
1333 /* STT_GNU_IFUNC symbol must go through PLT. */
1334 if (s390_is_ifunc_symbol_p (h))
1335 {
1336 /* All local STT_GNU_IFUNC references must be treated as local
1337 calls via local PLT. */
1338 if (h->ref_regular && SYMBOL_CALLS_LOCAL (info, h))
1339 {
1340 bfd_size_type pc_count = 0, count = 0;
1341 struct elf_dyn_relocs **pp;
1342 struct elf_dyn_relocs *p;
1343
1344 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
1345 {
1346 pc_count += p->pc_count;
1347 p->count -= p->pc_count;
1348 p->pc_count = 0;
1349 count += p->count;
1350 if (p->count == 0)
1351 *pp = p->next;
1352 else
1353 pp = &p->next;
1354 }
1355
1356 if (pc_count || count)
1357 {
1358 h->needs_plt = 1;
1359 h->non_got_ref = 1;
1360 if (h->plt.refcount <= 0)
1361 h->plt.refcount = 1;
1362 else
1363 h->plt.refcount += 1;
1364 }
1365 }
1366
1367 if (h->plt.refcount <= 0)
1368 {
1369 h->plt.offset = (bfd_vma) -1;
1370 h->needs_plt = 0;
1371 }
1372 return true;
1373 }
1374
1375 /* If this is a function, put it in the procedure linkage table. We
1376 will fill in the contents of the procedure linkage table later
1377 (although we could actually do it here). */
1378 if (h->type == STT_FUNC
1379 || h->needs_plt)
1380 {
1381 if (h->plt.refcount <= 0
1382 || SYMBOL_CALLS_LOCAL (info, h)
1383 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1384 {
1385 /* This case can occur if we saw a PLT32 reloc in an input
1386 file, but the symbol was never referred to by a dynamic
1387 object, or if all references were garbage collected. In
1388 such a case, we don't actually need to build a procedure
1389 linkage table, and we can just do a PC32 reloc instead. */
1390 h->plt.offset = (bfd_vma) -1;
1391 h->needs_plt = 0;
1392 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1393 }
1394
1395 return true;
1396 }
1397 else
1398 /* It's possible that we incorrectly decided a .plt reloc was
1399 needed for an R_390_PC32 reloc to a non-function sym in
1400 check_relocs. We can't decide accurately between function and
1401 non-function syms in check-relocs; Objects loaded later in
1402 the link may change h->type. So fix it now. */
1403 h->plt.offset = (bfd_vma) -1;
1404
1405 /* If this is a weak symbol, and there is a real definition, the
1406 processor independent code will have arranged for us to see the
1407 real definition first, and we can just use the same value. */
1408 if (h->is_weakalias)
1409 {
1410 struct elf_link_hash_entry *def = weakdef (h);
1411 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1412 h->root.u.def.section = def->root.u.def.section;
1413 h->root.u.def.value = def->root.u.def.value;
1414 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1415 h->non_got_ref = def->non_got_ref;
1416 return true;
1417 }
1418
1419 /* This is a reference to a symbol defined by a dynamic object which
1420 is not a function. */
1421
1422 /* If we are creating a shared library, we must presume that the
1423 only references to the symbol are via the global offset table.
1424 For such cases we need not do anything here; the relocations will
1425 be handled correctly by relocate_section. */
1426 if (bfd_link_pic (info))
1427 return true;
1428
1429 /* If there are no references to this symbol that do not use the
1430 GOT, we don't need to generate a copy reloc. */
1431 if (!h->non_got_ref)
1432 return true;
1433
1434 /* If -z nocopyreloc was given, we won't generate them either. */
1435 if (info->nocopyreloc)
1436 {
1437 h->non_got_ref = 0;
1438 return true;
1439 }
1440
1441 /* If we don't find any dynamic relocs in read-only sections, then
1442 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1443 if (ELIMINATE_COPY_RELOCS && !_bfd_elf_readonly_dynrelocs (h))
1444 {
1445 h->non_got_ref = 0;
1446 return true;
1447 }
1448
1449 /* We must allocate the symbol in our .dynbss section, which will
1450 become part of the .bss section of the executable. There will be
1451 an entry for this symbol in the .dynsym section. The dynamic
1452 object will contain position independent code, so all references
1453 from the dynamic object to this symbol will go through the global
1454 offset table. The dynamic linker will use the .dynsym entry to
1455 determine the address it must put in the global offset table, so
1456 both the dynamic object and the regular object will refer to the
1457 same memory location for the variable. */
1458
1459 htab = elf_s390_hash_table (info);
1460 if (htab == NULL)
1461 return false;
1462
1463 /* We must generate a R_390_COPY reloc to tell the dynamic linker to
1464 copy the initial value out of the dynamic object and into the
1465 runtime process image. */
1466 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
1467 {
1468 s = htab->elf.sdynrelro;
1469 srel = htab->elf.sreldynrelro;
1470 }
1471 else
1472 {
1473 s = htab->elf.sdynbss;
1474 srel = htab->elf.srelbss;
1475 }
1476 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
1477 {
1478 srel->size += sizeof (Elf64_External_Rela);
1479 h->needs_copy = 1;
1480 }
1481
1482 return _bfd_elf_adjust_dynamic_copy (info, h, s);
1483 }
1484
1485 /* Allocate space in .plt, .got and associated reloc sections for
1486 dynamic relocs. */
1487
1488 static bool
1489 allocate_dynrelocs (struct elf_link_hash_entry *h,
1490 void * inf)
1491 {
1492 struct bfd_link_info *info;
1493 struct elf_s390_link_hash_table *htab;
1494 struct elf_dyn_relocs *p;
1495
1496 if (h->root.type == bfd_link_hash_indirect)
1497 return true;
1498
1499 info = (struct bfd_link_info *) inf;
1500 htab = elf_s390_hash_table (info);
1501 if (htab == NULL)
1502 return false;
1503
1504 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1505 here if it is defined and referenced in a non-shared object. */
1506 if (s390_is_ifunc_symbol_p (h) && h->def_regular)
1507 return s390_elf_allocate_ifunc_dyn_relocs (info, h);
1508 else if (htab->elf.dynamic_sections_created
1509 && h->plt.refcount > 0)
1510 {
1511 /* Make sure this symbol is output as a dynamic symbol.
1512 Undefined weak syms won't yet be marked as dynamic. */
1513 if (h->dynindx == -1
1514 && !h->forced_local)
1515 {
1516 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1517 return false;
1518 }
1519
1520 if (bfd_link_pic (info)
1521 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1522 {
1523 asection *s = htab->elf.splt;
1524
1525 /* If this is the first .plt entry, make room for the special
1526 first entry. */
1527 if (s->size == 0)
1528 s->size += PLT_FIRST_ENTRY_SIZE;
1529
1530 h->plt.offset = s->size;
1531
1532 /* If this symbol is not defined in a regular file, and we are
1533 not generating a shared library, then set the symbol to this
1534 location in the .plt. This is required to make function
1535 pointers compare as equal between the normal executable and
1536 the shared library. */
1537 if (! bfd_link_pic (info)
1538 && !h->def_regular)
1539 {
1540 h->root.u.def.section = s;
1541 h->root.u.def.value = h->plt.offset;
1542 }
1543
1544 /* Make room for this entry. */
1545 s->size += PLT_ENTRY_SIZE;
1546
1547 /* We also need to make an entry in the .got.plt section. */
1548 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1549
1550 /* We also need to make an entry in the .rela.plt section. */
1551 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1552 }
1553 else
1554 {
1555 h->plt.offset = (bfd_vma) -1;
1556 h->needs_plt = 0;
1557 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1558 }
1559 }
1560 else
1561 {
1562 h->plt.offset = (bfd_vma) -1;
1563 h->needs_plt = 0;
1564 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1565 }
1566
1567 /* If R_390_TLS_{IE64,GOTIE64,GOTIE12,IEENT} symbol is now local to
1568 the binary, we can optimize a bit. IE64 and GOTIE64 get converted
1569 to R_390_TLS_LE64 requiring no TLS entry. For GOTIE12 and IEENT
1570 we can save the dynamic TLS relocation. */
1571 if (h->got.refcount > 0
1572 && !bfd_link_dll (info)
1573 && h->dynindx == -1
1574 && elf_s390_hash_entry(h)->tls_type >= GOT_TLS_IE)
1575 {
1576 if (elf_s390_hash_entry(h)->tls_type == GOT_TLS_IE_NLT)
1577 /* For the GOTIE access without a literal pool entry the offset has
1578 to be stored somewhere. The immediate value in the instruction
1579 is not bit enough so the value is stored in the got. */
1580 {
1581 h->got.offset = htab->elf.sgot->size;
1582 htab->elf.sgot->size += GOT_ENTRY_SIZE;
1583 }
1584 else
1585 h->got.offset = (bfd_vma) -1;
1586 }
1587 else if (h->got.refcount > 0)
1588 {
1589 asection *s;
1590 bool dyn;
1591 int tls_type = elf_s390_hash_entry(h)->tls_type;
1592
1593 /* Make sure this symbol is output as a dynamic symbol.
1594 Undefined weak syms won't yet be marked as dynamic. */
1595 if (h->dynindx == -1
1596 && !h->forced_local)
1597 {
1598 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1599 return false;
1600 }
1601
1602 s = htab->elf.sgot;
1603 h->got.offset = s->size;
1604 s->size += GOT_ENTRY_SIZE;
1605 /* R_390_TLS_GD64 needs 2 consecutive GOT slots. */
1606 if (tls_type == GOT_TLS_GD)
1607 s->size += GOT_ENTRY_SIZE;
1608 dyn = htab->elf.dynamic_sections_created;
1609 /* R_390_TLS_IE64 needs one dynamic relocation,
1610 R_390_TLS_GD64 needs one if local symbol and two if global. */
1611 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1612 || tls_type >= GOT_TLS_IE)
1613 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1614 else if (tls_type == GOT_TLS_GD)
1615 htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
1616 else if (!UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)
1617 && (bfd_link_pic (info)
1618 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1619 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1620 }
1621 else
1622 h->got.offset = (bfd_vma) -1;
1623
1624 if (h->dyn_relocs == NULL)
1625 return true;
1626
1627 /* In the shared -Bsymbolic case, discard space allocated for
1628 dynamic pc-relative relocs against symbols which turn out to be
1629 defined in regular objects. For the normal shared case, discard
1630 space for pc-relative relocs that have become local due to symbol
1631 visibility changes. */
1632
1633 if (bfd_link_pic (info))
1634 {
1635 if (SYMBOL_CALLS_LOCAL (info, h))
1636 {
1637 struct elf_dyn_relocs **pp;
1638
1639 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
1640 {
1641 p->count -= p->pc_count;
1642 p->pc_count = 0;
1643 if (p->count == 0)
1644 *pp = p->next;
1645 else
1646 pp = &p->next;
1647 }
1648 }
1649
1650 /* Also discard relocs on undefined weak syms with non-default
1651 visibility. */
1652 if (h->dyn_relocs != NULL
1653 && h->root.type == bfd_link_hash_undefweak)
1654 {
1655 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1656 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1657 h->dyn_relocs = NULL;
1658
1659 /* Make sure undefined weak symbols are output as a dynamic
1660 symbol in PIEs. */
1661 else if (h->dynindx == -1
1662 && !h->forced_local)
1663 {
1664 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1665 return false;
1666 }
1667 }
1668 }
1669 else if (ELIMINATE_COPY_RELOCS)
1670 {
1671 /* For the non-shared case, discard space for relocs against
1672 symbols which turn out to need copy relocs or are not
1673 dynamic. */
1674
1675 if (!h->non_got_ref
1676 && ((h->def_dynamic
1677 && !h->def_regular)
1678 || (htab->elf.dynamic_sections_created
1679 && (h->root.type == bfd_link_hash_undefweak
1680 || h->root.type == bfd_link_hash_undefined))))
1681 {
1682 /* Make sure this symbol is output as a dynamic symbol.
1683 Undefined weak syms won't yet be marked as dynamic. */
1684 if (h->dynindx == -1
1685 && !h->forced_local)
1686 {
1687 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1688 return false;
1689 }
1690
1691 /* If that succeeded, we know we'll be keeping all the
1692 relocs. */
1693 if (h->dynindx != -1)
1694 goto keep;
1695 }
1696
1697 h->dyn_relocs = NULL;
1698
1699 keep: ;
1700 }
1701
1702 /* Finally, allocate space. */
1703 for (p = h->dyn_relocs; p != NULL; p = p->next)
1704 {
1705 asection *sreloc = elf_section_data (p->sec)->sreloc;
1706 sreloc->size += p->count * sizeof (Elf64_External_Rela);
1707 }
1708
1709 return true;
1710 }
1711
1712 /* Set the sizes of the dynamic sections. */
1713
1714 static bool
1715 elf_s390_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1716 struct bfd_link_info *info)
1717 {
1718 struct elf_s390_link_hash_table *htab;
1719 bfd *dynobj;
1720 asection *s;
1721 bool relocs;
1722 bfd *ibfd;
1723
1724 htab = elf_s390_hash_table (info);
1725 if (htab == NULL)
1726 return false;
1727
1728 dynobj = htab->elf.dynobj;
1729 if (dynobj == NULL)
1730 return true;
1731
1732 if (htab->elf.dynamic_sections_created)
1733 {
1734 /* Set the contents of the .interp section to the interpreter. */
1735 if (bfd_link_executable (info) && !info->nointerp)
1736 {
1737 s = bfd_get_linker_section (dynobj, ".interp");
1738 if (s == NULL)
1739 abort ();
1740 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1741 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1742 }
1743 }
1744
1745 if (htab->elf.sgot && s390_gotplt_after_got_p (info))
1746 {
1747 /* _bfd_elf_create_got_section adds the got header size always
1748 to .got.plt but we need it in .got if this section comes
1749 first. */
1750 htab->elf.sgot->size += 3 * GOT_ENTRY_SIZE;
1751 htab->elf.sgotplt->size -= 3 * GOT_ENTRY_SIZE;
1752
1753 /* Make the _GLOBAL_OFFSET_TABLE_ symbol point to the .got
1754 instead of .got.plt. */
1755 htab->elf.hgot->root.u.def.section = htab->elf.sgot;
1756 htab->elf.hgot->root.u.def.value = 0;
1757 }
1758
1759 /* Set up .got offsets for local syms, and space for local dynamic
1760 relocs. */
1761 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1762 {
1763 bfd_signed_vma *local_got;
1764 bfd_signed_vma *end_local_got;
1765 char *local_tls_type;
1766 bfd_size_type locsymcount;
1767 Elf_Internal_Shdr *symtab_hdr;
1768 asection *srela;
1769 struct plt_entry *local_plt;
1770 unsigned int i;
1771
1772 if (! is_s390_elf (ibfd))
1773 continue;
1774
1775 for (s = ibfd->sections; s != NULL; s = s->next)
1776 {
1777 struct elf_dyn_relocs *p;
1778
1779 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
1780 {
1781 if (!bfd_is_abs_section (p->sec)
1782 && bfd_is_abs_section (p->sec->output_section))
1783 {
1784 /* Input section has been discarded, either because
1785 it is a copy of a linkonce section or due to
1786 linker script /DISCARD/, so we'll be discarding
1787 the relocs too. */
1788 }
1789 else if (p->count != 0)
1790 {
1791 srela = elf_section_data (p->sec)->sreloc;
1792 srela->size += p->count * sizeof (Elf64_External_Rela);
1793 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1794 info->flags |= DF_TEXTREL;
1795 }
1796 }
1797 }
1798
1799 local_got = elf_local_got_refcounts (ibfd);
1800 if (!local_got)
1801 continue;
1802
1803 symtab_hdr = &elf_symtab_hdr (ibfd);
1804 locsymcount = symtab_hdr->sh_info;
1805 end_local_got = local_got + locsymcount;
1806 local_tls_type = elf_s390_local_got_tls_type (ibfd);
1807 s = htab->elf.sgot;
1808 srela = htab->elf.srelgot;
1809 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1810 {
1811 if (*local_got > 0)
1812 {
1813 *local_got = s->size;
1814 s->size += GOT_ENTRY_SIZE;
1815 if (*local_tls_type == GOT_TLS_GD)
1816 s->size += GOT_ENTRY_SIZE;
1817 if (bfd_link_pic (info))
1818 srela->size += sizeof (Elf64_External_Rela);
1819 }
1820 else
1821 *local_got = (bfd_vma) -1;
1822 }
1823
1824 local_plt = elf_s390_local_plt (ibfd);
1825 for (i = 0; i < symtab_hdr->sh_info; i++)
1826 {
1827 if (local_plt[i].plt.refcount > 0)
1828 {
1829 local_plt[i].plt.offset = htab->elf.iplt->size;
1830 htab->elf.iplt->size += PLT_ENTRY_SIZE;
1831 htab->elf.igotplt->size += GOT_ENTRY_SIZE;
1832 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
1833 }
1834 else
1835 local_plt[i].plt.offset = (bfd_vma) -1;
1836 }
1837 }
1838
1839 if (htab->tls_ldm_got.refcount > 0)
1840 {
1841 /* Allocate 2 got entries and 1 dynamic reloc for R_390_TLS_LDM64
1842 relocs. */
1843 htab->tls_ldm_got.offset = htab->elf.sgot->size;
1844 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
1845 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1846 }
1847 else
1848 htab->tls_ldm_got.offset = -1;
1849
1850 /* Allocate global sym .plt and .got entries, and space for global
1851 sym dynamic relocs. */
1852 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
1853
1854 /* We now have determined the sizes of the various dynamic sections.
1855 Allocate memory for them. */
1856 relocs = false;
1857 for (s = dynobj->sections; s != NULL; s = s->next)
1858 {
1859 if ((s->flags & SEC_LINKER_CREATED) == 0)
1860 continue;
1861
1862 if (s == htab->elf.splt
1863 || s == htab->elf.sgot
1864 || s == htab->elf.sgotplt
1865 || s == htab->elf.sdynbss
1866 || s == htab->elf.sdynrelro
1867 || s == htab->elf.iplt
1868 || s == htab->elf.igotplt
1869 || s == htab->irelifunc)
1870 {
1871 /* Strip this section if we don't need it; see the
1872 comment below. */
1873 }
1874 else if (startswith (bfd_section_name (s), ".rela"))
1875 {
1876 if (s->size != 0 && s != htab->elf.srelplt)
1877 {
1878 relocs = true;
1879 if (s == htab->elf.irelplt)
1880 {
1881 /* In static-pie case, there are IRELATIVE-relocs in
1882 .rela.iplt (htab->irelplt), which will later be grouped
1883 to .rela.plt. On s390, the IRELATIVE relocations are
1884 always located in .rela.iplt - even for non-static case.
1885 Ensure that DT_JMPREL, DT_PLTRELA, DT_PLTRELASZ is added
1886 to the dynamic section even if htab->srelplt->size == 0.
1887 See _bfd_elf_add_dynamic_tags in bfd/elflink.c. */
1888 htab->elf.dt_jmprel_required = true;
1889 }
1890 }
1891
1892 /* We use the reloc_count field as a counter if we need
1893 to copy relocs into the output file. */
1894 s->reloc_count = 0;
1895 }
1896 else
1897 {
1898 /* It's not one of our sections, so don't allocate space. */
1899 continue;
1900 }
1901
1902 if (s->size == 0)
1903 {
1904 /* If we don't need this section, strip it from the
1905 output file. This is to handle .rela.bss and
1906 .rela.plt. We must create it in
1907 create_dynamic_sections, because it must be created
1908 before the linker maps input sections to output
1909 sections. The linker does that before
1910 adjust_dynamic_symbol is called, and it is that
1911 function which decides whether anything needs to go
1912 into these sections. */
1913
1914 s->flags |= SEC_EXCLUDE;
1915 continue;
1916 }
1917
1918 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1919 continue;
1920
1921 /* Allocate memory for the section contents. We use bfd_zalloc
1922 here in case unused entries are not reclaimed before the
1923 section's contents are written out. This should not happen,
1924 but this way if it does, we get a R_390_NONE reloc instead
1925 of garbage. */
1926 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1927 if (s->contents == NULL)
1928 return false;
1929 }
1930
1931 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
1932 }
1933
1934 /* Return the base VMA address which should be subtracted from real addresses
1935 when resolving @dtpoff relocation.
1936 This is PT_TLS segment p_vaddr. */
1937
1938 static bfd_vma
1939 dtpoff_base (struct bfd_link_info *info)
1940 {
1941 /* If tls_sec is NULL, we should have signalled an error already. */
1942 if (elf_hash_table (info)->tls_sec == NULL)
1943 return 0;
1944 return elf_hash_table (info)->tls_sec->vma;
1945 }
1946
1947 /* Return the relocation value for @tpoff relocation
1948 if STT_TLS virtual address is ADDRESS. */
1949
1950 static bfd_vma
1951 tpoff (struct bfd_link_info *info, bfd_vma address)
1952 {
1953 struct elf_link_hash_table *htab = elf_hash_table (info);
1954
1955 /* If tls_sec is NULL, we should have signalled an error already. */
1956 if (htab->tls_sec == NULL)
1957 return 0;
1958 return htab->tls_size + htab->tls_sec->vma - address;
1959 }
1960
1961 /* Complain if TLS instruction relocation is against an invalid
1962 instruction. */
1963
1964 static void
1965 invalid_tls_insn (bfd *input_bfd,
1966 asection *input_section,
1967 Elf_Internal_Rela *rel)
1968 {
1969 reloc_howto_type *howto;
1970
1971 howto = elf_howto_table + ELF64_R_TYPE (rel->r_info);
1972 _bfd_error_handler
1973 /* xgettext:c-format */
1974 (_("%pB(%pA+%#" PRIx64 "): invalid instruction for TLS relocation %s"),
1975 input_bfd,
1976 input_section,
1977 (uint64_t) rel->r_offset,
1978 howto->name);
1979 bfd_set_error (bfd_error_bad_value);
1980 }
1981
1982 /* Relocate a 390 ELF section. */
1983
1984 static int
1985 elf_s390_relocate_section (bfd *output_bfd,
1986 struct bfd_link_info *info,
1987 bfd *input_bfd,
1988 asection *input_section,
1989 bfd_byte *contents,
1990 Elf_Internal_Rela *relocs,
1991 Elf_Internal_Sym *local_syms,
1992 asection **local_sections)
1993 {
1994 struct elf_s390_link_hash_table *htab;
1995 Elf_Internal_Shdr *symtab_hdr;
1996 struct elf_link_hash_entry **sym_hashes;
1997 bfd_vma *local_got_offsets;
1998 Elf_Internal_Rela *rel;
1999 Elf_Internal_Rela *relend;
2000
2001 if (!is_s390_elf (input_bfd))
2002 {
2003 bfd_set_error (bfd_error_wrong_format);
2004 return false;
2005 }
2006
2007 htab = elf_s390_hash_table (info);
2008 if (htab == NULL)
2009 return false;
2010
2011 symtab_hdr = &elf_symtab_hdr (input_bfd);
2012 sym_hashes = elf_sym_hashes (input_bfd);
2013 local_got_offsets = elf_local_got_offsets (input_bfd);
2014
2015 rel = relocs;
2016 relend = relocs + input_section->reloc_count;
2017 for (; rel < relend; rel++)
2018 {
2019 unsigned int r_type;
2020 reloc_howto_type *howto;
2021 unsigned long r_symndx;
2022 struct elf_link_hash_entry *h;
2023 Elf_Internal_Sym *sym;
2024 asection *sec;
2025 bfd_vma off;
2026 bfd_vma relocation;
2027 bool unresolved_reloc;
2028 bfd_reloc_status_type r;
2029 int tls_type;
2030 bool resolved_to_zero;
2031
2032 r_type = ELF64_R_TYPE (rel->r_info);
2033 if (r_type == (int) R_390_GNU_VTINHERIT
2034 || r_type == (int) R_390_GNU_VTENTRY)
2035 continue;
2036 if (r_type >= (int) R_390_max)
2037 {
2038 bfd_set_error (bfd_error_bad_value);
2039 return false;
2040 }
2041
2042 howto = elf_howto_table + r_type;
2043 r_symndx = ELF64_R_SYM (rel->r_info);
2044
2045 h = NULL;
2046 sym = NULL;
2047 sec = NULL;
2048 unresolved_reloc = false;
2049 if (r_symndx < symtab_hdr->sh_info)
2050 {
2051 sym = local_syms + r_symndx;
2052 sec = local_sections[r_symndx];
2053
2054 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2055 {
2056 struct plt_entry *local_plt = elf_s390_local_plt (input_bfd);
2057 if (local_plt == NULL)
2058 return false;
2059
2060 /* Address of the PLT slot. */
2061 relocation = (htab->elf.iplt->output_section->vma
2062 + htab->elf.iplt->output_offset
2063 + local_plt[r_symndx].plt.offset);
2064
2065 switch (r_type)
2066 {
2067 case R_390_PLTOFF16:
2068 case R_390_PLTOFF32:
2069 case R_390_PLTOFF64:
2070 relocation -= s390_got_pointer (info);
2071 break;
2072 case R_390_GOTPLT12:
2073 case R_390_GOTPLT16:
2074 case R_390_GOTPLT20:
2075 case R_390_GOTPLT32:
2076 case R_390_GOTPLT64:
2077 case R_390_GOTPLTENT:
2078 case R_390_GOT12:
2079 case R_390_GOT16:
2080 case R_390_GOT20:
2081 case R_390_GOT32:
2082 case R_390_GOT64:
2083 case R_390_GOTENT:
2084 {
2085 /* Write the PLT slot address into the GOT slot. */
2086 bfd_put_64 (output_bfd, relocation,
2087 htab->elf.sgot->contents +
2088 local_got_offsets[r_symndx]);
2089 relocation = (local_got_offsets[r_symndx] +
2090 s390_got_offset (info));
2091
2092 if (r_type == R_390_GOTENT || r_type == R_390_GOTPLTENT)
2093 relocation += s390_got_pointer (info);
2094 break;
2095 }
2096 default:
2097 break;
2098 }
2099 /* The output section is needed later in
2100 finish_dynamic_section when creating the dynamic
2101 relocation. */
2102 local_plt[r_symndx].sec = sec;
2103 goto do_relocation;
2104 }
2105 else
2106 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2107 }
2108 else
2109 {
2110 bool warned ATTRIBUTE_UNUSED;
2111 bool ignored ATTRIBUTE_UNUSED;
2112
2113 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2114 r_symndx, symtab_hdr, sym_hashes,
2115 h, sec, relocation,
2116 unresolved_reloc, warned, ignored);
2117 }
2118
2119 if (sec != NULL && discarded_section (sec))
2120 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2121 rel, 1, relend, howto, 0, contents);
2122
2123 if (bfd_link_relocatable (info))
2124 continue;
2125
2126 resolved_to_zero = (h != NULL
2127 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
2128
2129 switch (r_type)
2130 {
2131 case R_390_GOTPLT12:
2132 case R_390_GOTPLT16:
2133 case R_390_GOTPLT20:
2134 case R_390_GOTPLT32:
2135 case R_390_GOTPLT64:
2136 case R_390_GOTPLTENT:
2137 /* There are three cases for a GOTPLT relocation. 1) The
2138 relocation is against the jump slot entry of a plt that
2139 will get emitted to the output file. 2) The relocation
2140 is against the jump slot of a plt entry that has been
2141 removed. elf_s390_adjust_gotplt has created a GOT entry
2142 as replacement. 3) The relocation is against a local symbol.
2143 Cases 2) and 3) are the same as the GOT relocation code
2144 so we just have to test for case 1 and fall through for
2145 the other two. */
2146 if (h != NULL && h->plt.offset != (bfd_vma) -1)
2147 {
2148 bfd_vma plt_index;
2149
2150 if (s390_is_ifunc_symbol_p (h))
2151 {
2152 /* Entry indices of .iplt and .igot.plt match
2153 1:1. No magic PLT first entry here. */
2154 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2155 relocation = (plt_index * GOT_ENTRY_SIZE
2156 + s390_gotplt_offset (info)
2157 + htab->elf.igotplt->output_offset);
2158 }
2159 else
2160 {
2161 plt_index = ((h->plt.offset - PLT_FIRST_ENTRY_SIZE)
2162 / PLT_ENTRY_SIZE);
2163
2164 relocation = (plt_index * GOT_ENTRY_SIZE
2165 + s390_gotplt_offset (info));
2166 }
2167 if (r_type == R_390_GOTPLTENT)
2168 relocation += s390_got_pointer (info);
2169 unresolved_reloc = false;
2170 break;
2171 }
2172 /* Fall through. */
2173
2174 case R_390_GOT12:
2175 case R_390_GOT16:
2176 case R_390_GOT20:
2177 case R_390_GOT32:
2178 case R_390_GOT64:
2179 case R_390_GOTENT:
2180 /* Relocation is to the entry for this symbol in the global
2181 offset table. */
2182 if (htab->elf.sgot == NULL)
2183 abort ();
2184
2185 if (h != NULL)
2186 {
2187 bool dyn;
2188
2189 off = h->got.offset;
2190 dyn = htab->elf.dynamic_sections_created;
2191
2192 if (s390_is_ifunc_symbol_p (h))
2193 {
2194 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
2195 if (off == (bfd_vma)-1)
2196 {
2197 /* No explicit GOT usage so redirect to the
2198 got.iplt slot. */
2199 relocation = (s390_gotplt_offset (info)
2200 + htab->elf.igotplt->output_offset
2201 + (h->plt.offset / PLT_ENTRY_SIZE
2202 * GOT_ENTRY_SIZE));
2203
2204 /* For @GOTENT the relocation is against the offset between
2205 the instruction and the symbols entry in the GOT and not
2206 between the start of the GOT and the symbols entry. We
2207 add the vma of the GOT to get the correct value. */
2208 if (r_type == R_390_GOTENT || r_type == R_390_GOTPLTENT)
2209 relocation += s390_got_pointer (info);
2210
2211 break;
2212 }
2213 else
2214 {
2215 /* Explicit GOT slots must contain the address
2216 of the PLT slot. This will be handled in
2217 finish_dynamic_symbol. */
2218 }
2219 }
2220 else if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2221 bfd_link_pic (info),
2222 h)
2223 || SYMBOL_REFERENCES_LOCAL (info, h)
2224 || resolved_to_zero)
2225 {
2226 Elf_Internal_Sym *isym;
2227 asection *sym_sec;
2228
2229 /* This is actually a static link, or it is a
2230 -Bsymbolic link and the symbol is defined
2231 locally, or the symbol was forced to be local
2232 because of a version file. We must initialize
2233 this entry in the global offset table. Since the
2234 offset must always be a multiple of 2, we use the
2235 least significant bit to record whether we have
2236 initialized it already.
2237
2238 When doing a dynamic link, we create a .rel.got
2239 relocation entry to initialize the value. This
2240 is done in the finish_dynamic_symbol routine. */
2241 if ((off & 1) != 0)
2242 off &= ~1;
2243 else
2244 {
2245 bfd_put_64 (output_bfd, relocation,
2246 htab->elf.sgot->contents + off);
2247 h->got.offset |= 1;
2248 }
2249
2250 /* When turning a GOT slot dereference into a direct
2251 reference using larl we have to make sure that
2252 the symbol is 1. properly aligned and 2. it is no
2253 ABS symbol or will become one. */
2254 if (h->def_regular
2255 && SYMBOL_REFERENCES_LOCAL (info, h)
2256 /* lgrl rx,sym@GOTENT -> larl rx, sym */
2257 && ((r_type == R_390_GOTENT
2258 && (bfd_get_16 (input_bfd,
2259 contents + rel->r_offset - 2)
2260 & 0xff0f) == 0xc408)
2261 /* lg rx, sym@GOT(r12) -> larl rx, sym */
2262 || (r_type == R_390_GOT20
2263 && (bfd_get_32 (input_bfd,
2264 contents + rel->r_offset - 2)
2265 & 0xff00f000) == 0xe300c000
2266 && bfd_get_8 (input_bfd,
2267 contents + rel->r_offset + 3) == 0x04))
2268 && (isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
2269 input_bfd, r_symndx))
2270 && isym->st_shndx != SHN_ABS
2271 && h != htab->elf.hdynamic
2272 && h != htab->elf.hgot
2273 && h != htab->elf.hplt
2274 && !(isym->st_value & 1)
2275 && (sym_sec = bfd_section_from_elf_index (input_bfd,
2276 isym->st_shndx))
2277 && sym_sec->alignment_power)
2278 {
2279 unsigned short new_insn =
2280 (0xc000 | (bfd_get_8 (input_bfd,
2281 contents + rel->r_offset - 1) & 0xf0));
2282 bfd_put_16 (output_bfd, new_insn,
2283 contents + rel->r_offset - 2);
2284 r_type = R_390_PC32DBL;
2285 rel->r_addend = 2;
2286 howto = elf_howto_table + r_type;
2287 relocation = h->root.u.def.value
2288 + h->root.u.def.section->output_section->vma
2289 + h->root.u.def.section->output_offset;
2290 goto do_relocation;
2291 }
2292 }
2293 else
2294 unresolved_reloc = false;
2295 }
2296 else
2297 {
2298 if (local_got_offsets == NULL)
2299 abort ();
2300
2301 off = local_got_offsets[r_symndx];
2302
2303 /* The offset must always be a multiple of 8. We use
2304 the least significant bit to record whether we have
2305 already generated the necessary reloc. */
2306 if ((off & 1) != 0)
2307 off &= ~1;
2308 else
2309 {
2310 bfd_put_64 (output_bfd, relocation,
2311 htab->elf.sgot->contents + off);
2312
2313 if (bfd_link_pic (info))
2314 {
2315 asection *s;
2316 Elf_Internal_Rela outrel;
2317 bfd_byte *loc;
2318
2319 s = htab->elf.srelgot;
2320 if (s == NULL)
2321 abort ();
2322
2323 outrel.r_offset = (htab->elf.sgot->output_section->vma
2324 + htab->elf.sgot->output_offset
2325 + off);
2326 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
2327 outrel.r_addend = relocation;
2328 loc = s->contents;
2329 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2330 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2331 }
2332
2333 local_got_offsets[r_symndx] |= 1;
2334 }
2335 }
2336
2337 if (off >= (bfd_vma) -2)
2338 abort ();
2339
2340 relocation = s390_got_offset (info) + off;
2341
2342 /* For @GOTENT the relocation is against the offset between
2343 the instruction and the symbols entry in the GOT and not
2344 between the start of the GOT and the symbols entry. We
2345 add the vma of the GOT to get the correct value. */
2346 if ( r_type == R_390_GOTENT
2347 || r_type == R_390_GOTPLTENT)
2348 relocation += s390_got_pointer (info);
2349
2350 break;
2351
2352 case R_390_GOTOFF16:
2353 case R_390_GOTOFF32:
2354 case R_390_GOTOFF64:
2355 /* Relocation is relative to the start of the global offset
2356 table. */
2357
2358 if (h != NULL
2359 && s390_is_ifunc_symbol_p (h)
2360 && h->def_regular
2361 && !bfd_link_executable (info))
2362 {
2363 relocation = (htab->elf.iplt->output_section->vma
2364 + htab->elf.iplt->output_offset
2365 + h->plt.offset
2366 - s390_got_pointer (info));
2367 goto do_relocation;
2368 }
2369
2370 relocation -= s390_got_pointer (info);
2371 break;
2372
2373 case R_390_GOTPC:
2374 case R_390_GOTPCDBL:
2375 /* Use global offset table as symbol value. */
2376 relocation = s390_got_pointer (info);
2377 unresolved_reloc = false;
2378 break;
2379
2380 case R_390_PLT12DBL:
2381 case R_390_PLT16DBL:
2382 case R_390_PLT24DBL:
2383 case R_390_PLT32:
2384 case R_390_PLT32DBL:
2385 case R_390_PLT64:
2386 /* Relocation is to the entry for this symbol in the
2387 procedure linkage table. */
2388
2389 /* Resolve a PLT32 reloc against a local symbol directly,
2390 without using the procedure linkage table. */
2391 if (h == NULL)
2392 break;
2393
2394 if (h->plt.offset == (bfd_vma) -1
2395 || (htab->elf.splt == NULL && !s390_is_ifunc_symbol_p (h)))
2396 {
2397 /* We didn't make a PLT entry for this symbol. This
2398 happens when statically linking PIC code, or when
2399 using -Bsymbolic. */
2400
2401 /* Replace relative long addressing instructions of weak
2402 symbols, which will definitely resolve to zero, with
2403 either a load address of 0 or a trapping insn.
2404 This prevents the PLT32DBL relocation from overflowing in
2405 case the binary will be loaded at 4GB or more. */
2406 if (h->root.type == bfd_link_hash_undefweak
2407 && !h->root.linker_def
2408 && (bfd_link_executable (info)
2409 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2410 && r_type == R_390_PLT32DBL
2411 && rel->r_offset >= 2)
2412 {
2413 void *insn_start = contents + rel->r_offset - 2;
2414 uint16_t op = bfd_get_16 (input_bfd, insn_start) & 0xff0f;
2415 uint8_t reg = bfd_get_8 (input_bfd, insn_start + 1) & 0xf0;
2416
2417 /* NOTE: The order of the if's is important! */
2418 /* Replace load address relative long (larl) with load
2419 address (lay) */
2420 if (op == 0xc000)
2421 {
2422 /* larl rX,<weak sym> -> lay rX,0(0) */
2423 bfd_put_16 (output_bfd, 0xe300 | reg, insn_start);
2424 bfd_put_32 (output_bfd, 0x71, insn_start + 2);
2425 continue;
2426 }
2427 /* Replace branch relative and save long (brasl) with a trap. */
2428 else if (op == 0xc005)
2429 {
2430 /* brasl rX,<weak sym> -> jg .+2 (6-byte trap) */
2431 bfd_put_16 (output_bfd, 0xc0f4, insn_start);
2432 bfd_put_32 (output_bfd, 0x1, insn_start + 2);
2433 continue;
2434 }
2435 }
2436
2437 break;
2438 }
2439 if (s390_is_ifunc_symbol_p (h))
2440 relocation = (htab->elf.iplt->output_section->vma
2441 + htab->elf.iplt->output_offset
2442 + h->plt.offset);
2443 else
2444 relocation = (htab->elf.splt->output_section->vma
2445 + htab->elf.splt->output_offset
2446 + h->plt.offset);
2447 unresolved_reloc = false;
2448 break;
2449
2450 case R_390_PLTOFF16:
2451 case R_390_PLTOFF32:
2452 case R_390_PLTOFF64:
2453 /* Relocation is to the entry for this symbol in the
2454 procedure linkage table relative to the start of the GOT. */
2455
2456 /* For local symbols or if we didn't make a PLT entry for
2457 this symbol resolve the symbol directly. */
2458 if (h == NULL
2459 || h->plt.offset == (bfd_vma) -1
2460 || (htab->elf.splt == NULL && !s390_is_ifunc_symbol_p (h)))
2461 {
2462 relocation -= s390_got_pointer (info);
2463 break;
2464 }
2465
2466 if (s390_is_ifunc_symbol_p (h))
2467 relocation = (htab->elf.iplt->output_section->vma
2468 + htab->elf.iplt->output_offset
2469 + h->plt.offset
2470 - s390_got_pointer (info));
2471 else
2472 relocation = (htab->elf.splt->output_section->vma
2473 + htab->elf.splt->output_offset
2474 + h->plt.offset
2475 - s390_got_pointer (info));
2476 unresolved_reloc = false;
2477 break;
2478
2479 case R_390_PC16:
2480 case R_390_PC12DBL:
2481 case R_390_PC16DBL:
2482 case R_390_PC24DBL:
2483 case R_390_PC32:
2484 case R_390_PC32DBL:
2485 case R_390_PC64:
2486 if (h != NULL
2487 && bfd_link_pie (info)
2488 && !h->def_regular)
2489 {
2490 _bfd_error_handler (_("%pB: `%s' non-PLT reloc for symbol defined "
2491 "in shared library and accessed "
2492 "from executable "
2493 "(rebuild file with -fPIC ?)"),
2494 input_bfd, h->root.root.string);
2495 bfd_set_error (bfd_error_bad_value);
2496 return false;
2497 }
2498 /* The target of these relocs are instruction operands
2499 residing in read-only sections. We cannot emit a runtime
2500 reloc for it. */
2501 if (h != NULL
2502 && s390_is_ifunc_symbol_p (h)
2503 && h->def_regular
2504 && bfd_link_pic (info))
2505 {
2506 relocation = (htab->elf.iplt->output_section->vma
2507 + htab->elf.iplt->output_offset
2508 + h->plt.offset);
2509 goto do_relocation;
2510 }
2511
2512 /* Replace relative long addressing instructions of weak
2513 symbols, which will definitely resolve to zero, with
2514 either a load address of 0, a NOP, or a trapping insn.
2515 This prevents the PC32DBL relocation from overflowing in
2516 case the binary will be loaded at 4GB or more. */
2517 if (h != NULL
2518 && h->root.type == bfd_link_hash_undefweak
2519 && !h->root.linker_def
2520 && (bfd_link_executable (info)
2521 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2522 && r_type == R_390_PC32DBL
2523 && rel->r_offset >= 2)
2524 {
2525 void *insn_start = contents + rel->r_offset - 2;
2526 uint16_t op = bfd_get_16 (input_bfd, insn_start) & 0xff0f;
2527 uint8_t reg = bfd_get_8 (input_bfd, insn_start + 1) & 0xf0;
2528
2529 /* NOTE: The order of the if's is important! */
2530 /* Replace load address relative long (larl) with load
2531 address (lay) */
2532 if (op == 0xc000)
2533 {
2534 /* larl rX,<weak sym> -> lay rX,0(0) */
2535 bfd_put_16 (output_bfd, 0xe300 | reg, insn_start);
2536 bfd_put_32 (output_bfd, 0x71, insn_start + 2);
2537 continue;
2538 }
2539 /* Replace prefetch data relative long (pfdrl) with a NOP */
2540 else if (op == 0xc602)
2541 {
2542 /* Emit a 6-byte NOP: jgnop . */
2543 bfd_put_16 (output_bfd, 0xc004, insn_start);
2544 bfd_put_32 (output_bfd, 0x0, insn_start + 2);
2545 continue;
2546 }
2547 /* Replace the following instructions with a trap:
2548 - branch relative and save long (brasl)
2549 - load (logical) relative long (lrl, lgrl, lgfrl, llgfrl)
2550 - load (logical) halfword relative long (lhrl, lghrl, llhrl, llghrl)
2551 - store relative long (strl, stgrl)
2552 - store halfword relative long (sthrl)
2553 - execute relative long (exrl)
2554 - compare (logical) relative long (crl, clrl, cgrl, clgrl, cgfrl, clgfrl)
2555 - compare (logical) halfword relative long (chrl, cghrl, clhrl, clghrl) */
2556 else if (op == 0xc005 || (op & 0xff00) == 0xc400
2557 || (op & 0xff00) == 0xc600)
2558 {
2559 /* Emit a 6-byte trap: jg .+2 */
2560 bfd_put_16 (output_bfd, 0xc0f4, insn_start);
2561 bfd_put_32 (output_bfd, 0x1, insn_start + 2);
2562 continue;
2563 }
2564 }
2565 /* Fall through. */
2566
2567 case R_390_8:
2568 case R_390_16:
2569 case R_390_32:
2570 case R_390_64:
2571
2572 if ((input_section->flags & SEC_ALLOC) == 0)
2573 break;
2574
2575 if (h != NULL
2576 && s390_is_ifunc_symbol_p (h)
2577 && h->def_regular)
2578 {
2579 if (!bfd_link_pic (info))
2580 {
2581 /* For a non-shared object the symbol will not
2582 change. Hence we can write the address of the
2583 target IPLT slot now. */
2584 relocation = (htab->elf.iplt->output_section->vma
2585 + htab->elf.iplt->output_offset
2586 + h ->plt.offset);
2587 goto do_relocation;
2588 }
2589 else
2590 {
2591 /* For shared objects a runtime relocation is needed. */
2592
2593 Elf_Internal_Rela outrel;
2594 asection *sreloc;
2595
2596 /* Need a dynamic relocation to get the real function
2597 address. */
2598 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2599 info,
2600 input_section,
2601 rel->r_offset);
2602 if (outrel.r_offset == (bfd_vma) -1
2603 || outrel.r_offset == (bfd_vma) -2)
2604 abort ();
2605
2606 outrel.r_offset += (input_section->output_section->vma
2607 + input_section->output_offset);
2608
2609 if (h->dynindx == -1
2610 || h->forced_local
2611 || bfd_link_executable (info))
2612 {
2613 /* This symbol is resolved locally. */
2614 outrel.r_info = ELF64_R_INFO (0, R_390_IRELATIVE);
2615 outrel.r_addend = (h->root.u.def.value
2616 + h->root.u.def.section->output_section->vma
2617 + h->root.u.def.section->output_offset);
2618 }
2619 else
2620 {
2621 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2622 outrel.r_addend = 0;
2623 }
2624
2625 sreloc = htab->elf.irelifunc;
2626 elf_append_rela (output_bfd, sreloc, &outrel);
2627
2628 /* If this reloc is against an external symbol, we
2629 do not want to fiddle with the addend. Otherwise,
2630 we need to include the symbol value so that it
2631 becomes an addend for the dynamic reloc. For an
2632 internal symbol, we have updated addend. */
2633 continue;
2634 }
2635 }
2636
2637 if ((bfd_link_pic (info)
2638 && (h == NULL
2639 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2640 && !resolved_to_zero)
2641 || h->root.type != bfd_link_hash_undefweak)
2642 && ((r_type != R_390_PC16
2643 && r_type != R_390_PC12DBL
2644 && r_type != R_390_PC16DBL
2645 && r_type != R_390_PC24DBL
2646 && r_type != R_390_PC32
2647 && r_type != R_390_PC32DBL
2648 && r_type != R_390_PC64)
2649 || !SYMBOL_CALLS_LOCAL (info, h)))
2650 || (ELIMINATE_COPY_RELOCS
2651 && !bfd_link_pic (info)
2652 && h != NULL
2653 && h->dynindx != -1
2654 && !h->non_got_ref
2655 && ((h->def_dynamic
2656 && !h->def_regular)
2657 || h->root.type == bfd_link_hash_undefweak
2658 || h->root.type == bfd_link_hash_undefined)))
2659 {
2660 Elf_Internal_Rela outrel;
2661 bool skip, relocate;
2662 asection *sreloc;
2663 bfd_byte *loc;
2664
2665 /* When generating a shared object, these relocations
2666 are copied into the output file to be resolved at run
2667 time. */
2668 skip = false;
2669 relocate = false;
2670
2671 outrel.r_offset =
2672 _bfd_elf_section_offset (output_bfd, info, input_section,
2673 rel->r_offset);
2674 if (outrel.r_offset == (bfd_vma) -1)
2675 skip = true;
2676 else if (outrel.r_offset == (bfd_vma) -2)
2677 skip = true, relocate = true;
2678
2679 outrel.r_offset += (input_section->output_section->vma
2680 + input_section->output_offset);
2681
2682 if (skip)
2683 memset (&outrel, 0, sizeof outrel);
2684 else if (h != NULL
2685 && h->dynindx != -1
2686 && (r_type == R_390_PC16
2687 || r_type == R_390_PC12DBL
2688 || r_type == R_390_PC16DBL
2689 || r_type == R_390_PC24DBL
2690 || r_type == R_390_PC32
2691 || r_type == R_390_PC32DBL
2692 || r_type == R_390_PC64
2693 || !bfd_link_pic (info)
2694 || !SYMBOLIC_BIND (info, h)
2695 || !h->def_regular))
2696 {
2697 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2698 outrel.r_addend = rel->r_addend;
2699 }
2700 else
2701 {
2702 /* This symbol is local, or marked to become local. */
2703 outrel.r_addend = relocation + rel->r_addend;
2704 if (r_type == R_390_64)
2705 {
2706 relocate = true;
2707 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
2708 }
2709 else
2710 {
2711 long sindx;
2712
2713 if (bfd_is_abs_section (sec))
2714 sindx = 0;
2715 else if (sec == NULL || sec->owner == NULL)
2716 {
2717 bfd_set_error(bfd_error_bad_value);
2718 return false;
2719 }
2720 else
2721 {
2722 asection *osec;
2723
2724 osec = sec->output_section;
2725 sindx = elf_section_data (osec)->dynindx;
2726
2727 if (sindx == 0)
2728 {
2729 osec = htab->elf.text_index_section;
2730 sindx = elf_section_data (osec)->dynindx;
2731 }
2732 BFD_ASSERT (sindx != 0);
2733
2734 /* We are turning this relocation into one
2735 against a section symbol, so subtract out
2736 the output section's address but not the
2737 offset of the input section in the output
2738 section. */
2739 outrel.r_addend -= osec->vma;
2740 }
2741 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2742 }
2743 }
2744
2745 sreloc = elf_section_data (input_section)->sreloc;
2746 if (sreloc == NULL)
2747 abort ();
2748
2749 loc = sreloc->contents;
2750 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2751 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2752
2753 /* If this reloc is against an external symbol, we do
2754 not want to fiddle with the addend. Otherwise, we
2755 need to include the symbol value so that it becomes
2756 an addend for the dynamic reloc. */
2757 if (! relocate)
2758 continue;
2759 }
2760
2761 break;
2762
2763 /* Relocations for tls literal pool entries. */
2764 case R_390_TLS_IE64:
2765 if (bfd_link_dll (info))
2766 {
2767 Elf_Internal_Rela outrel;
2768 asection *sreloc;
2769 bfd_byte *loc;
2770
2771 outrel.r_offset = rel->r_offset
2772 + input_section->output_section->vma
2773 + input_section->output_offset;
2774 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
2775 sreloc = elf_section_data (input_section)->sreloc;
2776 if (sreloc == NULL)
2777 abort ();
2778 loc = sreloc->contents;
2779 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2780 bfd_elf64_swap_reloc_out (output_bfd, &outrel, loc);
2781 }
2782 /* Fall through. */
2783
2784 case R_390_TLS_GD64:
2785 case R_390_TLS_GOTIE64:
2786 r_type = elf_s390_tls_transition (info, r_type, h == NULL);
2787 tls_type = GOT_UNKNOWN;
2788 if (h == NULL && local_got_offsets)
2789 tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
2790 else if (h != NULL)
2791 {
2792 tls_type = elf_s390_hash_entry(h)->tls_type;
2793 if (!bfd_link_dll (info) && h->dynindx == -1 && tls_type >= GOT_TLS_IE)
2794 r_type = R_390_TLS_LE64;
2795 }
2796 if (r_type == R_390_TLS_GD64 && tls_type >= GOT_TLS_IE)
2797 r_type = R_390_TLS_IE64;
2798
2799 if (r_type == R_390_TLS_LE64)
2800 {
2801 /* This relocation gets optimized away by the local exec
2802 access optimization. */
2803 BFD_ASSERT (! unresolved_reloc);
2804 bfd_put_64 (output_bfd, -tpoff (info, relocation) + rel->r_addend,
2805 contents + rel->r_offset);
2806 continue;
2807 }
2808
2809 if (htab->elf.sgot == NULL)
2810 abort ();
2811
2812 if (h != NULL)
2813 off = h->got.offset;
2814 else
2815 {
2816 if (local_got_offsets == NULL)
2817 abort ();
2818
2819 off = local_got_offsets[r_symndx];
2820 }
2821
2822 emit_tls_relocs:
2823
2824 if ((off & 1) != 0)
2825 off &= ~1;
2826 else
2827 {
2828 Elf_Internal_Rela outrel;
2829 bfd_byte *loc;
2830 int dr_type, indx;
2831
2832 if (htab->elf.srelgot == NULL)
2833 abort ();
2834
2835 outrel.r_offset = (htab->elf.sgot->output_section->vma
2836 + htab->elf.sgot->output_offset + off);
2837
2838 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2839 if (r_type == R_390_TLS_GD64)
2840 dr_type = R_390_TLS_DTPMOD;
2841 else
2842 dr_type = R_390_TLS_TPOFF;
2843 if (dr_type == R_390_TLS_TPOFF && indx == 0)
2844 outrel.r_addend = relocation - dtpoff_base (info);
2845 else
2846 outrel.r_addend = 0;
2847 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2848 loc = htab->elf.srelgot->contents;
2849 loc += htab->elf.srelgot->reloc_count++
2850 * sizeof (Elf64_External_Rela);
2851 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2852
2853 if (r_type == R_390_TLS_GD64)
2854 {
2855 if (indx == 0)
2856 {
2857 BFD_ASSERT (! unresolved_reloc);
2858 bfd_put_64 (output_bfd,
2859 relocation - dtpoff_base (info),
2860 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
2861 }
2862 else
2863 {
2864 outrel.r_info = ELF64_R_INFO (indx, R_390_TLS_DTPOFF);
2865 outrel.r_offset += GOT_ENTRY_SIZE;
2866 outrel.r_addend = 0;
2867 htab->elf.srelgot->reloc_count++;
2868 loc += sizeof (Elf64_External_Rela);
2869 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2870 }
2871 }
2872
2873 if (h != NULL)
2874 h->got.offset |= 1;
2875 else
2876 local_got_offsets[r_symndx] |= 1;
2877 }
2878
2879 if (off >= (bfd_vma) -2)
2880 abort ();
2881 if (r_type == ELF64_R_TYPE (rel->r_info))
2882 {
2883 relocation = htab->elf.sgot->output_offset + off;
2884 if (r_type == R_390_TLS_IE64 || r_type == R_390_TLS_IEENT)
2885 relocation += htab->elf.sgot->output_section->vma;
2886 unresolved_reloc = false;
2887 }
2888 else
2889 {
2890 bfd_put_64 (output_bfd, htab->elf.sgot->output_offset + off,
2891 contents + rel->r_offset);
2892 continue;
2893 }
2894 break;
2895
2896 case R_390_TLS_GOTIE12:
2897 case R_390_TLS_GOTIE20:
2898 case R_390_TLS_IEENT:
2899 if (h == NULL)
2900 {
2901 if (local_got_offsets == NULL)
2902 abort();
2903 off = local_got_offsets[r_symndx];
2904 if (bfd_link_dll (info))
2905 goto emit_tls_relocs;
2906 }
2907 else
2908 {
2909 off = h->got.offset;
2910 tls_type = elf_s390_hash_entry(h)->tls_type;
2911 if (bfd_link_dll (info) || h->dynindx != -1 || tls_type < GOT_TLS_IE)
2912 goto emit_tls_relocs;
2913 }
2914
2915 if (htab->elf.sgot == NULL)
2916 abort ();
2917
2918 BFD_ASSERT (! unresolved_reloc);
2919 bfd_put_64 (output_bfd, -tpoff (info, relocation),
2920 htab->elf.sgot->contents + off);
2921 relocation = htab->elf.sgot->output_offset + off;
2922 if (r_type == R_390_TLS_IEENT)
2923 relocation += htab->elf.sgot->output_section->vma;
2924 unresolved_reloc = false;
2925 break;
2926
2927 case R_390_TLS_LDM64:
2928 if (! bfd_link_dll (info))
2929 /* The literal pool entry this relocation refers to gets ignored
2930 by the optimized code of the local exec model. Do nothing
2931 and the value will turn out zero. */
2932 continue;
2933
2934 if (htab->elf.sgot == NULL)
2935 abort ();
2936
2937 off = htab->tls_ldm_got.offset;
2938 if (off & 1)
2939 off &= ~1;
2940 else
2941 {
2942 Elf_Internal_Rela outrel;
2943 bfd_byte *loc;
2944
2945 if (htab->elf.srelgot == NULL)
2946 abort ();
2947
2948 outrel.r_offset = (htab->elf.sgot->output_section->vma
2949 + htab->elf.sgot->output_offset + off);
2950
2951 bfd_put_64 (output_bfd, 0,
2952 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
2953 outrel.r_info = ELF64_R_INFO (0, R_390_TLS_DTPMOD);
2954 outrel.r_addend = 0;
2955 loc = htab->elf.srelgot->contents;
2956 loc += htab->elf.srelgot->reloc_count++
2957 * sizeof (Elf64_External_Rela);
2958 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2959 htab->tls_ldm_got.offset |= 1;
2960 }
2961 relocation = htab->elf.sgot->output_offset + off;
2962 unresolved_reloc = false;
2963 break;
2964
2965 case R_390_TLS_LE64:
2966 if (bfd_link_dll (info))
2967 {
2968 /* Linking a shared library with non-fpic code requires
2969 a R_390_TLS_TPOFF relocation. */
2970 Elf_Internal_Rela outrel;
2971 asection *sreloc;
2972 bfd_byte *loc;
2973 int indx;
2974
2975 outrel.r_offset = rel->r_offset
2976 + input_section->output_section->vma
2977 + input_section->output_offset;
2978 if (h != NULL && h->dynindx != -1)
2979 indx = h->dynindx;
2980 else
2981 indx = 0;
2982 outrel.r_info = ELF64_R_INFO (indx, R_390_TLS_TPOFF);
2983 if (indx == 0)
2984 outrel.r_addend = relocation - dtpoff_base (info);
2985 else
2986 outrel.r_addend = 0;
2987 sreloc = elf_section_data (input_section)->sreloc;
2988 if (sreloc == NULL)
2989 abort ();
2990 loc = sreloc->contents;
2991 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2992 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2993 }
2994 else
2995 {
2996 BFD_ASSERT (! unresolved_reloc);
2997 bfd_put_64 (output_bfd, -tpoff (info, relocation) + rel->r_addend,
2998 contents + rel->r_offset);
2999 }
3000 continue;
3001
3002 case R_390_TLS_LDO64:
3003 if (bfd_link_dll (info) || (input_section->flags & SEC_DEBUGGING))
3004 relocation -= dtpoff_base (info);
3005 else
3006 /* When converting LDO to LE, we must negate. */
3007 relocation = -tpoff (info, relocation);
3008 break;
3009
3010 /* Relocations for tls instructions. */
3011 case R_390_TLS_LOAD:
3012 case R_390_TLS_GDCALL:
3013 case R_390_TLS_LDCALL:
3014 tls_type = GOT_UNKNOWN;
3015 if (h == NULL && local_got_offsets)
3016 tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
3017 else if (h != NULL)
3018 tls_type = elf_s390_hash_entry(h)->tls_type;
3019
3020 if (tls_type == GOT_TLS_GD)
3021 continue;
3022
3023 if (r_type == R_390_TLS_LOAD)
3024 {
3025 if (!bfd_link_dll (info) && (h == NULL || h->dynindx == -1))
3026 {
3027 /* IE->LE transition. Four valid cases:
3028 lg %rx,(0,%ry) -> sllg %rx,%ry,0
3029 lg %rx,(%ry,0) -> sllg %rx,%ry,0
3030 lg %rx,(%ry,%r12) -> sllg %rx,%ry,0
3031 lg %rx,(%r12,%ry) -> sllg %rx,%ry,0 */
3032 unsigned int insn0, insn1, ry;
3033
3034 insn0 = bfd_get_32 (input_bfd, contents + rel->r_offset);
3035 insn1 = bfd_get_16 (input_bfd, contents + rel->r_offset + 4);
3036 if (insn1 != 0x0004)
3037 {
3038 invalid_tls_insn (input_bfd, input_section, rel);
3039 return false;
3040 }
3041 if ((insn0 & 0xff00f000) == 0xe3000000)
3042 /* lg %rx,0(%ry,0) -> sllg %rx,%ry,0 */
3043 ry = (insn0 & 0x000f0000);
3044 else if ((insn0 & 0xff0f0000) == 0xe3000000)
3045 /* lg %rx,0(0,%ry) -> sllg %rx,%ry,0 */
3046 ry = (insn0 & 0x0000f000) << 4;
3047 else if ((insn0 & 0xff00f000) == 0xe300c000)
3048 /* lg %rx,0(%ry,%r12) -> sllg %rx,%ry,0 */
3049 ry = (insn0 & 0x000f0000);
3050 else if ((insn0 & 0xff0f0000) == 0xe30c0000)
3051 /* lg %rx,0(%r12,%ry) -> sllg %rx,%ry,0 */
3052 ry = (insn0 & 0x0000f000) << 4;
3053 else
3054 {
3055 invalid_tls_insn (input_bfd, input_section, rel);
3056 return false;
3057 }
3058 insn0 = 0xeb000000 | (insn0 & 0x00f00000) | ry;
3059 insn1 = 0x000d;
3060 bfd_put_32 (output_bfd, insn0, contents + rel->r_offset);
3061 bfd_put_16 (output_bfd, insn1, contents + rel->r_offset + 4);
3062 }
3063 }
3064 else if (r_type == R_390_TLS_GDCALL)
3065 {
3066 unsigned int insn0, insn1;
3067
3068 insn0 = bfd_get_32 (input_bfd, contents + rel->r_offset);
3069 insn1 = bfd_get_16 (input_bfd, contents + rel->r_offset + 4);
3070 if ((insn0 & 0xffff0000) != 0xc0e50000)
3071 {
3072 invalid_tls_insn (input_bfd, input_section, rel);
3073 return false;
3074 }
3075 if (!bfd_link_dll (info) && (h == NULL || h->dynindx == -1))
3076 {
3077 /* GD->LE transition.
3078 brasl %r14,__tls_get_addr@plt -> brcl 0,. */
3079 insn0 = 0xc0040000;
3080 insn1 = 0x0000;
3081 }
3082 else
3083 {
3084 /* GD->IE transition.
3085 brasl %r14,__tls_get_addr@plt -> lg %r2,0(%r2,%r12) */
3086 insn0 = 0xe322c000;
3087 insn1 = 0x0004;
3088 }
3089 bfd_put_32 (output_bfd, insn0, contents + rel->r_offset);
3090 bfd_put_16 (output_bfd, insn1, contents + rel->r_offset + 4);
3091 }
3092 else if (r_type == R_390_TLS_LDCALL)
3093 {
3094 if (!bfd_link_dll (info))
3095 {
3096 unsigned int insn0, insn1;
3097
3098 insn0 = bfd_get_32 (input_bfd, contents + rel->r_offset);
3099 insn1 = bfd_get_16 (input_bfd, contents + rel->r_offset + 4);
3100 if ((insn0 & 0xffff0000) != 0xc0e50000)
3101 {
3102 invalid_tls_insn (input_bfd, input_section, rel);
3103 return false;
3104 }
3105 /* LD->LE transition.
3106 brasl %r14,__tls_get_addr@plt -> brcl 0,. */
3107 insn0 = 0xc0040000;
3108 insn1 = 0x0000;
3109 bfd_put_32 (output_bfd, insn0, contents + rel->r_offset);
3110 bfd_put_16 (output_bfd, insn1, contents + rel->r_offset + 4);
3111 }
3112 }
3113 continue;
3114
3115 default:
3116 break;
3117 }
3118
3119 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3120 because such sections are not SEC_ALLOC and thus ld.so will
3121 not process them. */
3122 if (unresolved_reloc
3123 && !((input_section->flags & SEC_DEBUGGING) != 0
3124 && h->def_dynamic)
3125 && _bfd_elf_section_offset (output_bfd, info, input_section,
3126 rel->r_offset) != (bfd_vma) -1)
3127 _bfd_error_handler
3128 /* xgettext:c-format */
3129 (_("%pB(%pA+%#" PRIx64 "): "
3130 "unresolvable %s relocation against symbol `%s'"),
3131 input_bfd,
3132 input_section,
3133 (uint64_t) rel->r_offset,
3134 howto->name,
3135 h->root.root.string);
3136
3137 do_relocation:
3138
3139 /* When applying a 24 bit reloc we need to start one byte
3140 earlier. Otherwise the 32 bit get/put bfd operations might
3141 access a byte after the actual section. */
3142 if (r_type == R_390_PC24DBL
3143 || r_type == R_390_PLT24DBL)
3144 rel->r_offset--;
3145
3146 /* Issue an error if the right shift implied by the relocation
3147 would drop bits from the symbol value. */
3148 if (howto->rightshift
3149 && (relocation & (((bfd_vma)1 << howto->rightshift) - 1)))
3150 {
3151 _bfd_error_handler
3152 /* xgettext:c-format */
3153 (_("%pB(%pA+%#" PRIx64 "): "
3154 "misaligned symbol `%s' (%#" PRIx64 ") for relocation %s"),
3155 input_bfd,
3156 input_section,
3157 (uint64_t) rel->r_offset,
3158 h->root.root.string,
3159 (uint64_t)relocation,
3160 howto->name);
3161 return false;
3162 }
3163
3164 if (r_type == R_390_20
3165 || r_type == R_390_GOT20
3166 || r_type == R_390_GOTPLT20
3167 || r_type == R_390_TLS_GOTIE20)
3168 {
3169 relocation += rel->r_addend;
3170 relocation = (relocation&0xfff) << 8 | (relocation&0xff000) >> 12;
3171 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3172 contents, rel->r_offset,
3173 relocation, 0);
3174 }
3175 else
3176 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3177 contents, rel->r_offset,
3178 relocation, rel->r_addend);
3179
3180 if (r != bfd_reloc_ok)
3181 {
3182 const char *name;
3183
3184 if (h != NULL)
3185 name = h->root.root.string;
3186 else
3187 {
3188 name = bfd_elf_string_from_elf_section (input_bfd,
3189 symtab_hdr->sh_link,
3190 sym->st_name);
3191 if (name == NULL)
3192 return false;
3193 if (*name == '\0')
3194 name = bfd_section_name (sec);
3195 }
3196
3197 if (r == bfd_reloc_overflow)
3198 (*info->callbacks->reloc_overflow)
3199 (info, (h ? &h->root : NULL), name, howto->name,
3200 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3201 else
3202 {
3203 _bfd_error_handler
3204 /* xgettext:c-format */
3205 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
3206 input_bfd, input_section,
3207 (uint64_t) rel->r_offset, name, (int) r);
3208 return false;
3209 }
3210 }
3211 }
3212
3213 return true;
3214 }
3215
3216 /* Generate the PLT slots together with the dynamic relocations needed
3217 for IFUNC symbols. */
3218
3219 static void
3220 elf_s390_finish_ifunc_symbol (bfd *output_bfd,
3221 struct bfd_link_info *info,
3222 struct elf_link_hash_entry *h,
3223 struct elf_s390_link_hash_table *htab,
3224 bfd_vma plt_offset,
3225 bfd_vma resolver_address)
3226 {
3227 bfd_vma plt_index;
3228 bfd_vma got_offset;
3229 Elf_Internal_Rela rela;
3230 bfd_byte *loc;
3231 asection *plt, *gotplt, *relplt;
3232
3233 if (htab->elf.iplt == NULL
3234 || htab->elf.igotplt == NULL
3235 || htab->elf.irelplt == NULL)
3236 abort ();
3237
3238 /* Index of the PLT slot within iplt section. */
3239 plt_index = plt_offset / PLT_ENTRY_SIZE;
3240 plt = htab->elf.iplt;
3241 /* Offset into the igot.plt section. */
3242 got_offset = plt_index * GOT_ENTRY_SIZE;
3243 gotplt = htab->elf.igotplt;
3244 relplt = htab->elf.irelplt;
3245
3246 /* Fill in the blueprint of a PLT. */
3247 memcpy (plt->contents + plt_offset, elf_s390x_plt_entry,
3248 PLT_ENTRY_SIZE);
3249
3250 /* Fixup the relative address to the GOT entry */
3251 bfd_put_32 (output_bfd,
3252 (gotplt->output_section->vma +
3253 gotplt->output_offset + got_offset
3254 - (plt->output_section->vma +
3255 plt->output_offset +
3256 plt_offset))/2,
3257 plt->contents + plt_offset + 2);
3258 /* Fixup the relative branch to PLT 0 */
3259 bfd_put_32 (output_bfd, - (plt->output_offset +
3260 (PLT_ENTRY_SIZE * plt_index) + 22)/2,
3261 plt->contents + plt_offset + 24);
3262 /* Fixup offset into .rela.plt section. */
3263 bfd_put_32 (output_bfd, relplt->output_offset +
3264 plt_index * sizeof (Elf64_External_Rela),
3265 plt->contents + plt_offset + 28);
3266
3267 /* Fill in the entry in the global offset table.
3268 Points to instruction after GOT offset. */
3269 bfd_put_64 (output_bfd,
3270 (plt->output_section->vma
3271 + plt->output_offset
3272 + plt_offset
3273 + 14),
3274 gotplt->contents + got_offset);
3275
3276 /* Fill in the entry in the .rela.plt section. */
3277 rela.r_offset = (gotplt->output_section->vma
3278 + gotplt->output_offset
3279 + got_offset);
3280
3281 if (!h
3282 || h->dynindx == -1
3283 || ((bfd_link_executable (info)
3284 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3285 && h->def_regular))
3286 {
3287 /* The symbol can be locally resolved. */
3288 rela.r_info = ELF64_R_INFO (0, R_390_IRELATIVE);
3289 rela.r_addend = resolver_address;
3290 }
3291 else
3292 {
3293 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT);
3294 rela.r_addend = 0;
3295 }
3296
3297 loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3298 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3299 }
3300
3301
3302 /* Finish up dynamic symbol handling. We set the contents of various
3303 dynamic sections here. */
3304
3305 static bool
3306 elf_s390_finish_dynamic_symbol (bfd *output_bfd,
3307 struct bfd_link_info *info,
3308 struct elf_link_hash_entry *h,
3309 Elf_Internal_Sym *sym)
3310 {
3311 struct elf_s390_link_hash_table *htab;
3312 struct elf_s390_link_hash_entry *eh = (struct elf_s390_link_hash_entry*)h;
3313
3314 htab = elf_s390_hash_table (info);
3315
3316 if (h->plt.offset != (bfd_vma) -1)
3317 {
3318 bfd_vma plt_index;
3319 bfd_vma gotplt_offset;
3320 Elf_Internal_Rela rela;
3321 bfd_byte *loc;
3322
3323 /* This symbol has an entry in the procedure linkage table. Set
3324 it up. */
3325 if (s390_is_ifunc_symbol_p (h) && h->def_regular)
3326 {
3327 elf_s390_finish_ifunc_symbol (output_bfd, info, h,
3328 htab, h->plt.offset,
3329 eh->ifunc_resolver_address +
3330 eh->ifunc_resolver_section->output_offset +
3331 eh->ifunc_resolver_section->output_section->vma);
3332
3333 /* Do not return yet. Handling of explicit GOT slots of
3334 IFUNC symbols is below. */
3335 }
3336 else
3337 {
3338 if (h->dynindx == -1
3339 || htab->elf.splt == NULL
3340 || htab->elf.sgotplt == NULL
3341 || htab->elf.srelplt == NULL)
3342 abort ();
3343
3344 /* Calc. index no.
3345 Current offset - size first entry / entry size. */
3346 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3347
3348 /* The slots in the .got.plt correspond to the PLT slots in
3349 the same order. */
3350 gotplt_offset = plt_index * GOT_ENTRY_SIZE;
3351
3352 /* If .got.plt comes first it needs to contain the 3 header
3353 entries. */
3354 if (!s390_gotplt_after_got_p (info))
3355 gotplt_offset += 3 * GOT_ENTRY_SIZE;
3356
3357 /* Fill in the blueprint of a PLT. */
3358 memcpy (htab->elf.splt->contents + h->plt.offset, elf_s390x_plt_entry,
3359 PLT_ENTRY_SIZE);
3360
3361 /* The first instruction in the PLT entry is a LARL loading
3362 the address of the GOT slot. We write the 4 byte
3363 immediate operand of the LARL instruction here. */
3364 bfd_put_32 (output_bfd,
3365 (htab->elf.sgotplt->output_section->vma +
3366 htab->elf.sgotplt->output_offset + gotplt_offset
3367 - (htab->elf.splt->output_section->vma +
3368 htab->elf.splt->output_offset +
3369 h->plt.offset))/2,
3370 htab->elf.splt->contents + h->plt.offset + 2);
3371 /* Fixup the relative branch to PLT 0 */
3372 bfd_put_32 (output_bfd, - (PLT_FIRST_ENTRY_SIZE +
3373 (PLT_ENTRY_SIZE * plt_index) + 22)/2,
3374 htab->elf.splt->contents + h->plt.offset + 24);
3375 /* Fixup offset into .rela.plt section. */
3376 bfd_put_32 (output_bfd, plt_index * sizeof (Elf64_External_Rela),
3377 htab->elf.splt->contents + h->plt.offset + 28);
3378
3379 /* Fill in the entry in the global offset table.
3380 Points to instruction after GOT offset. */
3381 bfd_put_64 (output_bfd,
3382 (htab->elf.splt->output_section->vma
3383 + htab->elf.splt->output_offset
3384 + h->plt.offset
3385 + 14),
3386 htab->elf.sgotplt->contents + gotplt_offset);
3387
3388 /* Fill in the entry in the .rela.plt section. */
3389 rela.r_offset = (htab->elf.sgotplt->output_section->vma
3390 + htab->elf.sgotplt->output_offset
3391 + gotplt_offset);
3392 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT);
3393 rela.r_addend = 0;
3394 loc = htab->elf.srelplt->contents + plt_index *
3395 sizeof (Elf64_External_Rela);
3396 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3397
3398 if (!h->def_regular)
3399 {
3400 /* Mark the symbol as undefined, rather than as defined in
3401 the .plt section. Leave the value alone. This is a clue
3402 for the dynamic linker, to make function pointer
3403 comparisons work between an application and shared
3404 library. */
3405 sym->st_shndx = SHN_UNDEF;
3406 }
3407 }
3408 }
3409
3410 if (h->got.offset != (bfd_vma) -1
3411 && elf_s390_hash_entry(h)->tls_type != GOT_TLS_GD
3412 && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE
3413 && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE_NLT)
3414 {
3415 Elf_Internal_Rela rela;
3416 bfd_byte *loc;
3417
3418 /* This symbol has an entry in the global offset table. Set it
3419 up. */
3420 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3421 abort ();
3422
3423 rela.r_offset = (htab->elf.sgot->output_section->vma
3424 + htab->elf.sgot->output_offset
3425 + (h->got.offset &~ (bfd_vma) 1));
3426
3427 if (h->def_regular && s390_is_ifunc_symbol_p (h))
3428 {
3429 if (bfd_link_pic (info))
3430 {
3431 /* An explicit GOT slot usage needs GLOB_DAT. If the
3432 symbol references local the implicit got.iplt slot
3433 will be used and the IRELATIVE reloc has been created
3434 above. */
3435 goto do_glob_dat;
3436 }
3437 else
3438 {
3439 /* For non-shared objects explicit GOT slots must be
3440 filled with the PLT slot address for pointer
3441 equality reasons. */
3442 bfd_put_64 (output_bfd, (htab->elf.iplt->output_section->vma
3443 + htab->elf.iplt->output_offset
3444 + h->plt.offset),
3445 htab->elf.sgot->contents + h->got.offset);
3446 return true;
3447 }
3448 }
3449 else if (SYMBOL_REFERENCES_LOCAL (info, h))
3450 {
3451 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
3452 return true;
3453
3454 /* If this is a static link, or it is a -Bsymbolic link and
3455 the symbol is defined locally or was forced to be local
3456 because of a version file, we just want to emit a
3457 RELATIVE reloc. The entry in the global offset table
3458 will already have been initialized in the
3459 relocate_section function. */
3460 if (!(h->def_regular || ELF_COMMON_DEF_P (h)))
3461 return false;
3462 BFD_ASSERT((h->got.offset & 1) != 0);
3463 rela.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
3464 rela.r_addend = (h->root.u.def.value
3465 + h->root.u.def.section->output_section->vma
3466 + h->root.u.def.section->output_offset);
3467 }
3468 else
3469 {
3470 BFD_ASSERT((h->got.offset & 1) == 0);
3471 do_glob_dat:
3472 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgot->contents + h->got.offset);
3473 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_GLOB_DAT);
3474 rela.r_addend = 0;
3475 }
3476
3477 loc = htab->elf.srelgot->contents;
3478 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3479 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3480 }
3481
3482 if (h->needs_copy)
3483 {
3484 Elf_Internal_Rela rela;
3485 asection *s;
3486 bfd_byte *loc;
3487
3488 /* This symbols needs a copy reloc. Set it up. */
3489
3490 if (h->dynindx == -1
3491 || (h->root.type != bfd_link_hash_defined
3492 && h->root.type != bfd_link_hash_defweak)
3493 || htab->elf.srelbss == NULL)
3494 abort ();
3495
3496 rela.r_offset = (h->root.u.def.value
3497 + h->root.u.def.section->output_section->vma
3498 + h->root.u.def.section->output_offset);
3499 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_COPY);
3500 rela.r_addend = 0;
3501 if (h->root.u.def.section == htab->elf.sdynrelro)
3502 s = htab->elf.sreldynrelro;
3503 else
3504 s = htab->elf.srelbss;
3505 loc = s->contents + s->reloc_count++ * sizeof (Elf64_External_Rela);
3506 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3507 }
3508
3509 /* Mark some specially defined symbols as absolute. */
3510 if (h == htab->elf.hdynamic
3511 || h == htab->elf.hgot
3512 || h == htab->elf.hplt)
3513 sym->st_shndx = SHN_ABS;
3514
3515 return true;
3516 }
3517
3518 /* Used to decide how to sort relocs in an optimal manner for the
3519 dynamic linker, before writing them out. */
3520
3521 static enum elf_reloc_type_class
3522 elf_s390_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3523 const asection *rel_sec ATTRIBUTE_UNUSED,
3524 const Elf_Internal_Rela *rela)
3525 {
3526 bfd *abfd = info->output_bfd;
3527 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3528 struct elf_s390_link_hash_table *htab = elf_s390_hash_table (info);
3529 unsigned long r_symndx = ELF64_R_SYM (rela->r_info);
3530 Elf_Internal_Sym sym;
3531
3532 if (htab->elf.dynsym == NULL
3533 || !bed->s->swap_symbol_in (abfd,
3534 (htab->elf.dynsym->contents
3535 + r_symndx * bed->s->sizeof_sym),
3536 0, &sym))
3537 abort ();
3538
3539 /* Check relocation against STT_GNU_IFUNC symbol. */
3540 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
3541 return reloc_class_ifunc;
3542
3543 switch ((int) ELF64_R_TYPE (rela->r_info))
3544 {
3545 case R_390_RELATIVE:
3546 return reloc_class_relative;
3547 case R_390_JMP_SLOT:
3548 return reloc_class_plt;
3549 case R_390_COPY:
3550 return reloc_class_copy;
3551 default:
3552 return reloc_class_normal;
3553 }
3554 }
3555
3556 /* Finish up the dynamic sections. */
3557
3558 static bool
3559 elf_s390_finish_dynamic_sections (bfd *output_bfd,
3560 struct bfd_link_info *info)
3561 {
3562 struct elf_s390_link_hash_table *htab;
3563 bfd *dynobj;
3564 asection *sdyn;
3565 bfd *ibfd;
3566 unsigned int i;
3567
3568 htab = elf_s390_hash_table (info);
3569 if (htab == NULL)
3570 return false;
3571
3572 dynobj = htab->elf.dynobj;
3573 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3574
3575 if (htab->elf.dynamic_sections_created)
3576 {
3577 Elf64_External_Dyn *dyncon, *dynconend;
3578
3579 if (sdyn == NULL || htab->elf.sgot == NULL)
3580 abort ();
3581
3582 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3583 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3584 for (; dyncon < dynconend; dyncon++)
3585 {
3586 Elf_Internal_Dyn dyn;
3587 asection *s;
3588
3589 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3590
3591 switch (dyn.d_tag)
3592 {
3593 default:
3594 continue;
3595
3596 case DT_PLTGOT:
3597 /* DT_PLTGOT matches _GLOBAL_OFFSET_TABLE_ */
3598 dyn.d_un.d_ptr = s390_got_pointer (info);
3599 break;
3600
3601 case DT_JMPREL:
3602 s = htab->elf.srelplt;
3603 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3604 break;
3605
3606 case DT_PLTRELSZ:
3607 dyn.d_un.d_val = htab->elf.srelplt->size;
3608 if (htab->elf.irelplt)
3609 dyn.d_un.d_val += htab->elf.irelplt->size;
3610 break;
3611
3612 case DT_RELASZ:
3613 /* The procedure linkage table relocs (DT_JMPREL) should
3614 not be included in the overall relocs (DT_RELA).
3615 Therefore, we override the DT_RELASZ entry here to
3616 make it not include the JMPREL relocs. Since the
3617 linker script arranges for .rela.plt to follow all
3618 other relocation sections, we don't have to worry
3619 about changing the DT_RELA entry. */
3620 dyn.d_un.d_val -= htab->elf.srelplt->size;
3621 if (htab->elf.irelplt)
3622 dyn.d_un.d_val -= htab->elf.irelplt->size;
3623 break;
3624 }
3625
3626 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3627 }
3628
3629 /* Fill in the special first entry in the procedure linkage table. */
3630 if (htab->elf.splt && htab->elf.splt->size > 0)
3631 {
3632 /* fill in blueprint for plt 0 entry */
3633 memcpy (htab->elf.splt->contents, elf_s390x_first_plt_entry,
3634 PLT_FIRST_ENTRY_SIZE);
3635 /* The second instruction in the first PLT entry is a LARL
3636 loading the GOT pointer. Fill in the LARL immediate
3637 address. */
3638 bfd_put_32 (output_bfd,
3639 (s390_got_pointer (info)
3640 - htab->elf.splt->output_section->vma
3641 - htab->elf.splt->output_offset - 6)/2,
3642 htab->elf.splt->contents + 8);
3643 }
3644 if (elf_section_data (htab->elf.splt->output_section) != NULL)
3645 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
3646 = PLT_ENTRY_SIZE;
3647 }
3648
3649 if (htab->elf.hgot && htab->elf.hgot->root.u.def.section)
3650 {
3651 /* Fill in the first three entries in the global offset table. */
3652 if (htab->elf.hgot->root.u.def.section->size > 0)
3653 {
3654 bfd_put_64 (output_bfd,
3655 (sdyn == NULL ? (bfd_vma) 0
3656 : sdyn->output_section->vma + sdyn->output_offset),
3657 htab->elf.hgot->root.u.def.section->contents);
3658 /* One entry for shared object struct ptr. */
3659 bfd_put_64 (output_bfd, (bfd_vma) 0,
3660 htab->elf.hgot->root.u.def.section->contents + 8);
3661 /* One entry for _dl_runtime_resolve. */
3662 bfd_put_64 (output_bfd, (bfd_vma) 0,
3663 htab->elf.hgot->root.u.def.section->contents + 16);
3664 }
3665 if (htab->elf.sgot != NULL && htab->elf.sgot->size > 0)
3666 elf_section_data (htab->elf.sgot->output_section)
3667 ->this_hdr.sh_entsize = 8;
3668 }
3669
3670 /* Finish dynamic symbol for local IFUNC symbols. */
3671 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3672 {
3673 struct plt_entry *local_plt;
3674 Elf_Internal_Sym *isym;
3675 Elf_Internal_Shdr *symtab_hdr;
3676
3677 symtab_hdr = &elf_symtab_hdr (ibfd);
3678
3679 if (!is_s390_elf (ibfd))
3680 continue;
3681
3682 local_plt = elf_s390_local_plt (ibfd);
3683 if (local_plt != NULL)
3684 for (i = 0; i < symtab_hdr->sh_info; i++)
3685 {
3686 if (local_plt[i].plt.offset != (bfd_vma) -1)
3687 {
3688 asection *sec = local_plt[i].sec;
3689 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd, i);
3690 if (isym == NULL)
3691 return false;
3692
3693 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
3694 elf_s390_finish_ifunc_symbol (output_bfd, info, NULL, htab,
3695 local_plt[i].plt.offset,
3696 isym->st_value
3697 + sec->output_section->vma
3698 + sec->output_offset);
3699
3700 }
3701 }
3702 }
3703
3704 return true;
3705 }
3706
3707 /* Support for core dump NOTE sections. */
3709
3710 static bool
3711 elf_s390_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3712 {
3713 int offset;
3714 size_t size;
3715
3716 switch (note->descsz)
3717 {
3718 default:
3719 return false;
3720
3721 case 336: /* sizeof(struct elf_prstatus) on s390x */
3722 /* pr_cursig */
3723 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3724
3725 /* pr_pid */
3726 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
3727
3728 /* pr_reg */
3729 offset = 112;
3730 size = 216;
3731 break;
3732 }
3733
3734 /* Make a ".reg/999" section. */
3735 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3736 size, note->descpos + offset);
3737 }
3738
3739 static bool
3740 elf_s390_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3741 {
3742 switch (note->descsz)
3743 {
3744 default:
3745 return false;
3746
3747 case 136: /* sizeof(struct elf_prpsinfo) on s390x */
3748 elf_tdata (abfd)->core->pid
3749 = bfd_get_32 (abfd, note->descdata + 24);
3750 elf_tdata (abfd)->core->program
3751 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
3752 elf_tdata (abfd)->core->command
3753 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3754 }
3755
3756 /* Note that for some reason, a spurious space is tacked
3757 onto the end of the args in some (at least one anyway)
3758 implementations, so strip it off if it exists. */
3759
3760 {
3761 char *command = elf_tdata (abfd)->core->command;
3762 int n = strlen (command);
3763
3764 if (0 < n && command[n - 1] == ' ')
3765 command[n - 1] = '\0';
3766 }
3767
3768 return true;
3769 }
3770
3771 static char *
3772 elf_s390_write_core_note (bfd *abfd, char *buf, int *bufsiz,
3773 int note_type, ...)
3774 {
3775 va_list ap;
3776
3777 switch (note_type)
3778 {
3779 default:
3780 return NULL;
3781
3782 case NT_PRPSINFO:
3783 {
3784 char data[136] ATTRIBUTE_NONSTRING = { 0 };
3785 const char *fname, *psargs;
3786
3787 va_start (ap, note_type);
3788 fname = va_arg (ap, const char *);
3789 psargs = va_arg (ap, const char *);
3790 va_end (ap);
3791
3792 strncpy (data + 40, fname, 16);
3793 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3794 DIAGNOSTIC_PUSH;
3795 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
3796 -Wstringop-truncation:
3797 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
3798 */
3799 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
3800 #endif
3801 strncpy (data + 56, psargs, 80);
3802 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3803 DIAGNOSTIC_POP;
3804 #endif
3805 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
3806 &data, sizeof (data));
3807 }
3808
3809 case NT_PRSTATUS:
3810 {
3811 char data[336] = { 0 };
3812 long pid;
3813 int cursig;
3814 const void *gregs;
3815
3816 va_start (ap, note_type);
3817 pid = va_arg (ap, long);
3818 cursig = va_arg (ap, int);
3819 gregs = va_arg (ap, const void *);
3820 va_end (ap);
3821
3822 bfd_put_16 (abfd, cursig, data + 12);
3823 bfd_put_32 (abfd, pid, data + 32);
3824 memcpy (data + 112, gregs, 216);
3825 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
3826 &data, sizeof (data));
3827 }
3828 }
3829 /* NOTREACHED */
3830 }
3831
3832 /* Return address for Ith PLT stub in section PLT, for relocation REL
3834 or (bfd_vma) -1 if it should not be included. */
3835
3836 static bfd_vma
3837 elf_s390_plt_sym_val (bfd_vma i, const asection *plt,
3838 const arelent *rel ATTRIBUTE_UNUSED)
3839 {
3840 return plt->vma + PLT_FIRST_ENTRY_SIZE + i * PLT_ENTRY_SIZE;
3841 }
3842
3843 /* Merge backend specific data from an object file to the output
3844 object file when linking. */
3845
3846 static bool
3847 elf64_s390_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3848 {
3849 if (!is_s390_elf (ibfd) || !is_s390_elf (info->output_bfd))
3850 return true;
3851
3852 return elf_s390_merge_obj_attributes (ibfd, info);
3853 }
3854
3855 /* We may add a PT_S390_PGSTE program header. */
3856
3857 static int
3858 elf_s390_additional_program_headers (bfd *abfd ATTRIBUTE_UNUSED,
3859 struct bfd_link_info *info)
3860 {
3861 struct elf_s390_link_hash_table *htab;
3862
3863 if (info)
3864 {
3865 htab = elf_s390_hash_table (info);
3866 if (htab)
3867 return htab->params->pgste;
3868 }
3869 return 0;
3870 }
3871
3872
3873 /* Add the PT_S390_PGSTE program header. */
3874
3875 static bool
3876 elf_s390_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
3877 {
3878 struct elf_s390_link_hash_table *htab;
3879 struct elf_segment_map *m, *pm = NULL;
3880
3881 if (!abfd || !info)
3882 return true;
3883
3884 htab = elf_s390_hash_table (info);
3885 if (!htab || !htab->params->pgste)
3886 return true;
3887
3888 /* If there is already a PT_S390_PGSTE header, avoid adding
3889 another. */
3890 m = elf_seg_map (abfd);
3891 while (m && m->p_type != PT_S390_PGSTE)
3892 {
3893 pm = m;
3894 m = m->next;
3895 }
3896
3897 if (m)
3898 return true;
3899
3900 m = (struct elf_segment_map *)
3901 bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3902 if (m == NULL)
3903 return false;
3904 m->p_type = PT_S390_PGSTE;
3905 m->count = 0;
3906 m->next = NULL;
3907 if (pm)
3908 pm->next = m;
3909
3910 return true;
3911 }
3912
3913 bool
3914 bfd_elf_s390_set_options (struct bfd_link_info *info,
3915 struct s390_elf_params *params)
3916 {
3917 struct elf_s390_link_hash_table *htab;
3918
3919 if (info)
3920 {
3921 htab = elf_s390_hash_table (info);
3922 if (htab)
3923 htab->params = params;
3924 }
3925
3926 return true;
3927 }
3928
3929
3930 /* Why was the hash table entry size definition changed from
3931 ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and
3932 this is the only reason for the s390_elf64_size_info structure. */
3933
3934 const struct elf_size_info s390_elf64_size_info =
3935 {
3936 sizeof (Elf64_External_Ehdr),
3937 sizeof (Elf64_External_Phdr),
3938 sizeof (Elf64_External_Shdr),
3939 sizeof (Elf64_External_Rel),
3940 sizeof (Elf64_External_Rela),
3941 sizeof (Elf64_External_Sym),
3942 sizeof (Elf64_External_Dyn),
3943 sizeof (Elf_External_Note),
3944 8, /* hash-table entry size. */
3945 1, /* internal relocations per external relocations. */
3946 64, /* arch_size. */
3947 3, /* log_file_align. */
3948 ELFCLASS64, EV_CURRENT,
3949 bfd_elf64_write_out_phdrs,
3950 bfd_elf64_write_shdrs_and_ehdr,
3951 bfd_elf64_checksum_contents,
3952 bfd_elf64_write_relocs,
3953 bfd_elf64_swap_symbol_in,
3954 bfd_elf64_swap_symbol_out,
3955 bfd_elf64_slurp_reloc_table,
3956 bfd_elf64_slurp_symbol_table,
3957 bfd_elf64_swap_dyn_in,
3958 bfd_elf64_swap_dyn_out,
3959 bfd_elf64_swap_reloc_in,
3960 bfd_elf64_swap_reloc_out,
3961 bfd_elf64_swap_reloca_in,
3962 bfd_elf64_swap_reloca_out
3963 };
3964
3965 #define TARGET_BIG_SYM s390_elf64_vec
3966 #define TARGET_BIG_NAME "elf64-s390"
3967 #define ELF_ARCH bfd_arch_s390
3968 #define ELF_TARGET_ID S390_ELF_DATA
3969 #define ELF_MACHINE_CODE EM_S390
3970 #define ELF_MACHINE_ALT1 EM_S390_OLD
3971 #define ELF_MAXPAGESIZE 0x1000
3972
3973 #define elf_backend_size_info s390_elf64_size_info
3974
3975 #define elf_backend_can_gc_sections 1
3976 #define elf_backend_can_refcount 1
3977 #define elf_backend_want_got_plt 1
3978 #define elf_backend_plt_readonly 1
3979 #define elf_backend_want_plt_sym 0
3980 #define elf_backend_got_header_size 24
3981 #define elf_backend_want_dynrelro 1
3982 #define elf_backend_rela_normal 1
3983
3984 #define elf_info_to_howto elf_s390_info_to_howto
3985
3986 #define bfd_elf64_bfd_is_local_label_name elf_s390_is_local_label_name
3987 #define bfd_elf64_bfd_link_hash_table_create elf_s390_link_hash_table_create
3988 #define bfd_elf64_bfd_reloc_type_lookup elf_s390_reloc_type_lookup
3989 #define bfd_elf64_bfd_reloc_name_lookup elf_s390_reloc_name_lookup
3990 #define bfd_elf64_bfd_merge_private_bfd_data elf64_s390_merge_private_bfd_data
3991
3992 #define elf_backend_adjust_dynamic_symbol elf_s390_adjust_dynamic_symbol
3993 #define elf_backend_check_relocs elf_s390_check_relocs
3994 #define elf_backend_copy_indirect_symbol elf_s390_copy_indirect_symbol
3995 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
3996 #define elf_backend_finish_dynamic_sections elf_s390_finish_dynamic_sections
3997 #define elf_backend_finish_dynamic_symbol elf_s390_finish_dynamic_symbol
3998 #define elf_backend_gc_mark_hook elf_s390_gc_mark_hook
3999 #define elf_backend_reloc_type_class elf_s390_reloc_type_class
4000 #define elf_backend_relocate_section elf_s390_relocate_section
4001 #define elf_backend_late_size_sections elf_s390_late_size_sections
4002 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4003 #define elf_backend_grok_prstatus elf_s390_grok_prstatus
4004 #define elf_backend_grok_psinfo elf_s390_grok_psinfo
4005 #define elf_backend_write_core_note elf_s390_write_core_note
4006 #define elf_backend_plt_sym_val elf_s390_plt_sym_val
4007 #define elf_backend_sort_relocs_p elf_s390_elf_sort_relocs_p
4008 #define elf_backend_additional_program_headers elf_s390_additional_program_headers
4009 #define elf_backend_modify_segment_map elf_s390_modify_segment_map
4010
4011 #define bfd_elf64_mkobject elf_s390_mkobject
4012 #define elf_backend_object_p elf_s390_object_p
4013
4014 #include "elf64-target.h"
4015