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