]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-s390.c
* aix386-core.c: Remove use of PTR and PARAMS macros.
[thirdparty/binutils-gdb.git] / bfd / elf64-s390.c
CommitLineData
a85d7ed0 1/* IBM S/390-specific support for 64-bit ELF
4dfe6ac6 2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
1d7e9d18 3 2010, 2011, 2012 Free Software Foundation, Inc.
a85d7ed0
NC
4 Contributed Martin Schwidefsky (schwidefsky@de.ibm.com).
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or
a85d7ed0
NC
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
3e110533 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
53e09e0a 21 02110-1301, USA. */
a85d7ed0 22
a85d7ed0 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
a85d7ed0
NC
25#include "bfdlink.h"
26#include "libbfd.h"
27#include "elf-bfd.h"
a85d7ed0
NC
28#include "elf/s390.h"
29
30/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
31 from smaller values. Start with zero, widen, *then* decrement. */
32#define MINUS_ONE (((bfd_vma)0) - 1)
33
2c3fc389
NC
34static bfd_reloc_status_type
35s390_tls_reloc (bfd *, arelent *, asymbol *, void *,
36 asection *, bfd *, char **);
37static bfd_reloc_status_type
38s390_elf_ldisp_reloc (bfd *, arelent *, asymbol *, void *,
39 asection *, bfd *, char **);
40
a85d7ed0
NC
41/* The relocation "howto" table. */
42static reloc_howto_type elf_howto_table[] =
43{
44 HOWTO (R_390_NONE, /* type */
45 0, /* rightshift */
46 0, /* size (0 = byte, 1 = short, 2 = long) */
47 0, /* bitsize */
b34976b6 48 FALSE, /* pc_relative */
a85d7ed0
NC
49 0, /* bitpos */
50 complain_overflow_dont, /* complain_on_overflow */
51 bfd_elf_generic_reloc, /* special_function */
52 "R_390_NONE", /* name */
b34976b6 53 FALSE, /* partial_inplace */
a85d7ed0
NC
54 0, /* src_mask */
55 0, /* dst_mask */
b34976b6
AM
56 FALSE), /* pcrel_offset */
57
5236c819
MS
58 HOWTO(R_390_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_390_8", FALSE, 0,0x000000ff, FALSE),
60 HOWTO(R_390_12, 0, 1, 12, FALSE, 0, complain_overflow_dont,
61 bfd_elf_generic_reloc, "R_390_12", FALSE, 0,0x00000fff, FALSE),
62 HOWTO(R_390_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_390_16", FALSE, 0,0x0000ffff, FALSE),
64 HOWTO(R_390_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
65 bfd_elf_generic_reloc, "R_390_32", FALSE, 0,0xffffffff, FALSE),
66 HOWTO(R_390_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
67 bfd_elf_generic_reloc, "R_390_PC32", FALSE, 0,0xffffffff, TRUE),
15f8604d 68 HOWTO(R_390_GOT12, 0, 1, 12, FALSE, 0, complain_overflow_bitfield,
5236c819
MS
69 bfd_elf_generic_reloc, "R_390_GOT12", FALSE, 0,0x00000fff, FALSE),
70 HOWTO(R_390_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
71 bfd_elf_generic_reloc, "R_390_GOT32", FALSE, 0,0xffffffff, FALSE),
72 HOWTO(R_390_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
73 bfd_elf_generic_reloc, "R_390_PLT32", FALSE, 0,0xffffffff, TRUE),
74 HOWTO(R_390_COPY, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
75 bfd_elf_generic_reloc, "R_390_COPY", FALSE, 0,MINUS_ONE, FALSE),
76 HOWTO(R_390_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_390_GLOB_DAT", FALSE, 0,MINUS_ONE, FALSE),
78 HOWTO(R_390_JMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_390_JMP_SLOT", FALSE, 0,MINUS_ONE, FALSE),
80 HOWTO(R_390_RELATIVE, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_390_RELATIVE", FALSE, 0,MINUS_ONE, FALSE),
82 HOWTO(R_390_GOTOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_390_GOTOFF32", FALSE, 0,MINUS_ONE, FALSE),
84 HOWTO(R_390_GOTPC, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_390_GOTPC", FALSE, 0,MINUS_ONE, TRUE),
86 HOWTO(R_390_GOT16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
87 bfd_elf_generic_reloc, "R_390_GOT16", FALSE, 0,0x0000ffff, FALSE),
88 HOWTO(R_390_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
89 bfd_elf_generic_reloc, "R_390_PC16", FALSE, 0,0x0000ffff, TRUE),
90 HOWTO(R_390_PC16DBL, 1, 1, 16, TRUE, 0, complain_overflow_bitfield,
91 bfd_elf_generic_reloc, "R_390_PC16DBL", FALSE, 0,0x0000ffff, TRUE),
92 HOWTO(R_390_PLT16DBL, 1, 1, 16, TRUE, 0, complain_overflow_bitfield,
93 bfd_elf_generic_reloc, "R_390_PLT16DBL", FALSE, 0,0x0000ffff, TRUE),
94 HOWTO(R_390_PC32DBL, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
95 bfd_elf_generic_reloc, "R_390_PC32DBL", FALSE, 0,0xffffffff, TRUE),
96 HOWTO(R_390_PLT32DBL, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
97 bfd_elf_generic_reloc, "R_390_PLT32DBL", FALSE, 0,0xffffffff, TRUE),
98 HOWTO(R_390_GOTPCDBL, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
99 bfd_elf_generic_reloc, "R_390_GOTPCDBL", FALSE, 0,MINUS_ONE, TRUE),
100 HOWTO(R_390_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
101 bfd_elf_generic_reloc, "R_390_64", FALSE, 0,MINUS_ONE, FALSE),
102 HOWTO(R_390_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
103 bfd_elf_generic_reloc, "R_390_PC64", FALSE, 0,MINUS_ONE, TRUE),
104 HOWTO(R_390_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
105 bfd_elf_generic_reloc, "R_390_GOT64", FALSE, 0,MINUS_ONE, FALSE),
106 HOWTO(R_390_PLT64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_390_PLT64", FALSE, 0,MINUS_ONE, TRUE),
108 HOWTO(R_390_GOTENT, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_390_GOTENT", FALSE, 0,MINUS_ONE, TRUE),
110 HOWTO(R_390_GOTOFF16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
111 bfd_elf_generic_reloc, "R_390_GOTOFF16", FALSE, 0,0x0000ffff, FALSE),
112 HOWTO(R_390_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
113 bfd_elf_generic_reloc, "R_390_GOTOFF64", FALSE, 0,MINUS_ONE, FALSE),
114 HOWTO(R_390_GOTPLT12, 0, 1, 12, FALSE, 0, complain_overflow_dont,
115 bfd_elf_generic_reloc, "R_390_GOTPLT12", FALSE, 0,0x00000fff, FALSE),
116 HOWTO(R_390_GOTPLT16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
117 bfd_elf_generic_reloc, "R_390_GOTPLT16", FALSE, 0,0x0000ffff, FALSE),
118 HOWTO(R_390_GOTPLT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
119 bfd_elf_generic_reloc, "R_390_GOTPLT32", FALSE, 0,0xffffffff, FALSE),
120 HOWTO(R_390_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
121 bfd_elf_generic_reloc, "R_390_GOTPLT64", FALSE, 0,MINUS_ONE, FALSE),
122 HOWTO(R_390_GOTPLTENT, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
123 bfd_elf_generic_reloc, "R_390_GOTPLTENT",FALSE, 0,MINUS_ONE, TRUE),
124 HOWTO(R_390_PLTOFF16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
125 bfd_elf_generic_reloc, "R_390_PLTOFF16", FALSE, 0,0x0000ffff, FALSE),
126 HOWTO(R_390_PLTOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
127 bfd_elf_generic_reloc, "R_390_PLTOFF32", FALSE, 0,0xffffffff, FALSE),
128 HOWTO(R_390_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
129 bfd_elf_generic_reloc, "R_390_PLTOFF64", FALSE, 0,MINUS_ONE, FALSE),
69fc87f1
MS
130 HOWTO(R_390_TLS_LOAD, 0, 0, 0, FALSE, 0, complain_overflow_dont,
131 s390_tls_reloc, "R_390_TLS_LOAD", FALSE, 0, 0, FALSE),
132 HOWTO(R_390_TLS_GDCALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
133 s390_tls_reloc, "R_390_TLS_GDCALL", FALSE, 0, 0, FALSE),
134 HOWTO(R_390_TLS_LDCALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
135 s390_tls_reloc, "R_390_TLS_LDCALL", FALSE, 0, 0, FALSE),
136 EMPTY_HOWTO (R_390_TLS_GD32), /* Empty entry for R_390_TLS_GD32. */
137 HOWTO(R_390_TLS_GD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
138 bfd_elf_generic_reloc, "R_390_TLS_GD64", FALSE, 0, MINUS_ONE, FALSE),
139 HOWTO(R_390_TLS_GOTIE12, 0, 1, 12, FALSE, 0, complain_overflow_dont,
140 bfd_elf_generic_reloc, "R_390_TLS_GOTIE12", FALSE, 0, 0x00000fff, FALSE),
141 EMPTY_HOWTO (R_390_TLS_GOTIE32), /* Empty entry for R_390_TLS_GOTIE32. */
142 HOWTO(R_390_TLS_GOTIE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
143 bfd_elf_generic_reloc, "R_390_TLS_GOTIE64", FALSE, 0, MINUS_ONE, FALSE),
144 EMPTY_HOWTO (R_390_TLS_LDM32), /* Empty entry for R_390_TLS_LDM32. */
145 HOWTO(R_390_TLS_LDM64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
146 bfd_elf_generic_reloc, "R_390_TLS_LDM64", FALSE, 0, MINUS_ONE, FALSE),
147 EMPTY_HOWTO (R_390_TLS_IE32), /* Empty entry for R_390_TLS_IE32. */
148 HOWTO(R_390_TLS_IE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
149 bfd_elf_generic_reloc, "R_390_TLS_IE64", FALSE, 0, MINUS_ONE, FALSE),
150 HOWTO(R_390_TLS_IEENT, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
151 bfd_elf_generic_reloc, "R_390_TLS_IEENT", FALSE, 0, MINUS_ONE, TRUE),
152 EMPTY_HOWTO (R_390_TLS_LE32), /* Empty entry for R_390_TLS_LE32. */
153 HOWTO(R_390_TLS_LE64, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
154 bfd_elf_generic_reloc, "R_390_TLS_LE64", FALSE, 0, MINUS_ONE, FALSE),
155 EMPTY_HOWTO (R_390_TLS_LDO32), /* Empty entry for R_390_TLS_LDO32. */
156 HOWTO(R_390_TLS_LDO64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
157 bfd_elf_generic_reloc, "R_390_TLS_LDO64", FALSE, 0, MINUS_ONE, FALSE),
158 HOWTO(R_390_TLS_DTPMOD, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
159 bfd_elf_generic_reloc, "R_390_TLS_DTPMOD", FALSE, 0, MINUS_ONE, FALSE),
160 HOWTO(R_390_TLS_DTPOFF, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
161 bfd_elf_generic_reloc, "R_390_TLS_DTPOFF", FALSE, 0, MINUS_ONE, FALSE),
162 HOWTO(R_390_TLS_TPOFF, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
163 bfd_elf_generic_reloc, "R_390_TLS_TPOFF", FALSE, 0, MINUS_ONE, FALSE),
bd1ea41b
MS
164 HOWTO(R_390_20, 0, 2, 20, FALSE, 8, complain_overflow_dont,
165 s390_elf_ldisp_reloc, "R_390_20", FALSE, 0,0x0fffff00, FALSE),
166 HOWTO(R_390_GOT20, 0, 2, 20, FALSE, 8, complain_overflow_dont,
167 s390_elf_ldisp_reloc, "R_390_GOT20", FALSE, 0,0x0fffff00, FALSE),
168 HOWTO(R_390_GOTPLT20, 0, 2, 20, FALSE, 8, complain_overflow_dont,
169 s390_elf_ldisp_reloc, "R_390_GOTPLT20", FALSE, 0,0x0fffff00, FALSE),
170 HOWTO(R_390_TLS_GOTIE20, 0, 2, 20, FALSE, 8, complain_overflow_dont,
171 s390_elf_ldisp_reloc, "R_390_TLS_GOTIE20", FALSE, 0,0x0fffff00, FALSE),
a85d7ed0
NC
172};
173
174/* GNU extension to record C++ vtable hierarchy. */
175static reloc_howto_type elf64_s390_vtinherit_howto =
b34976b6 176 HOWTO (R_390_GNU_VTINHERIT, 0,4,0,FALSE,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", FALSE,0, 0, FALSE);
a85d7ed0 177static reloc_howto_type elf64_s390_vtentry_howto =
b34976b6 178 HOWTO (R_390_GNU_VTENTRY, 0,4,0,FALSE,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_390_GNU_VTENTRY", FALSE,0,0, FALSE);
a85d7ed0
NC
179
180static reloc_howto_type *
2c3fc389
NC
181elf_s390_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
182 bfd_reloc_code_real_type code)
a85d7ed0 183{
0451c93c
MS
184 switch (code)
185 {
186 case BFD_RELOC_NONE:
187 return &elf_howto_table[(int) R_390_NONE];
188 case BFD_RELOC_8:
189 return &elf_howto_table[(int) R_390_8];
190 case BFD_RELOC_390_12:
191 return &elf_howto_table[(int) R_390_12];
192 case BFD_RELOC_16:
193 return &elf_howto_table[(int) R_390_16];
194 case BFD_RELOC_32:
195 return &elf_howto_table[(int) R_390_32];
196 case BFD_RELOC_CTOR:
197 return &elf_howto_table[(int) R_390_32];
198 case BFD_RELOC_32_PCREL:
199 return &elf_howto_table[(int) R_390_PC32];
200 case BFD_RELOC_390_GOT12:
201 return &elf_howto_table[(int) R_390_GOT12];
202 case BFD_RELOC_32_GOT_PCREL:
203 return &elf_howto_table[(int) R_390_GOT32];
204 case BFD_RELOC_390_PLT32:
205 return &elf_howto_table[(int) R_390_PLT32];
206 case BFD_RELOC_390_COPY:
207 return &elf_howto_table[(int) R_390_COPY];
208 case BFD_RELOC_390_GLOB_DAT:
209 return &elf_howto_table[(int) R_390_GLOB_DAT];
210 case BFD_RELOC_390_JMP_SLOT:
211 return &elf_howto_table[(int) R_390_JMP_SLOT];
212 case BFD_RELOC_390_RELATIVE:
213 return &elf_howto_table[(int) R_390_RELATIVE];
214 case BFD_RELOC_32_GOTOFF:
5236c819 215 return &elf_howto_table[(int) R_390_GOTOFF32];
0451c93c
MS
216 case BFD_RELOC_390_GOTPC:
217 return &elf_howto_table[(int) R_390_GOTPC];
218 case BFD_RELOC_390_GOT16:
219 return &elf_howto_table[(int) R_390_GOT16];
220 case BFD_RELOC_16_PCREL:
221 return &elf_howto_table[(int) R_390_PC16];
222 case BFD_RELOC_390_PC16DBL:
223 return &elf_howto_table[(int) R_390_PC16DBL];
224 case BFD_RELOC_390_PLT16DBL:
225 return &elf_howto_table[(int) R_390_PLT16DBL];
0451c93c
MS
226 case BFD_RELOC_390_PC32DBL:
227 return &elf_howto_table[(int) R_390_PC32DBL];
228 case BFD_RELOC_390_PLT32DBL:
229 return &elf_howto_table[(int) R_390_PLT32DBL];
230 case BFD_RELOC_390_GOTPCDBL:
231 return &elf_howto_table[(int) R_390_GOTPCDBL];
232 case BFD_RELOC_64:
233 return &elf_howto_table[(int) R_390_64];
234 case BFD_RELOC_64_PCREL:
235 return &elf_howto_table[(int) R_390_PC64];
236 case BFD_RELOC_390_GOT64:
237 return &elf_howto_table[(int) R_390_GOT64];
238 case BFD_RELOC_390_PLT64:
239 return &elf_howto_table[(int) R_390_PLT64];
240 case BFD_RELOC_390_GOTENT:
241 return &elf_howto_table[(int) R_390_GOTENT];
5236c819
MS
242 case BFD_RELOC_16_GOTOFF:
243 return &elf_howto_table[(int) R_390_GOTOFF16];
244 case BFD_RELOC_390_GOTOFF64:
245 return &elf_howto_table[(int) R_390_GOTOFF64];
246 case BFD_RELOC_390_GOTPLT12:
247 return &elf_howto_table[(int) R_390_GOTPLT12];
248 case BFD_RELOC_390_GOTPLT16:
249 return &elf_howto_table[(int) R_390_GOTPLT16];
250 case BFD_RELOC_390_GOTPLT32:
251 return &elf_howto_table[(int) R_390_GOTPLT32];
252 case BFD_RELOC_390_GOTPLT64:
253 return &elf_howto_table[(int) R_390_GOTPLT64];
254 case BFD_RELOC_390_GOTPLTENT:
255 return &elf_howto_table[(int) R_390_GOTPLTENT];
256 case BFD_RELOC_390_PLTOFF16:
257 return &elf_howto_table[(int) R_390_PLTOFF16];
258 case BFD_RELOC_390_PLTOFF32:
259 return &elf_howto_table[(int) R_390_PLTOFF32];
260 case BFD_RELOC_390_PLTOFF64:
261 return &elf_howto_table[(int) R_390_PLTOFF64];
69fc87f1
MS
262 case BFD_RELOC_390_TLS_LOAD:
263 return &elf_howto_table[(int) R_390_TLS_LOAD];
264 case BFD_RELOC_390_TLS_GDCALL:
265 return &elf_howto_table[(int) R_390_TLS_GDCALL];
266 case BFD_RELOC_390_TLS_LDCALL:
267 return &elf_howto_table[(int) R_390_TLS_LDCALL];
268 case BFD_RELOC_390_TLS_GD64:
269 return &elf_howto_table[(int) R_390_TLS_GD64];
270 case BFD_RELOC_390_TLS_GOTIE12:
271 return &elf_howto_table[(int) R_390_TLS_GOTIE12];
272 case BFD_RELOC_390_TLS_GOTIE64:
273 return &elf_howto_table[(int) R_390_TLS_GOTIE64];
274 case BFD_RELOC_390_TLS_LDM64:
275 return &elf_howto_table[(int) R_390_TLS_LDM64];
276 case BFD_RELOC_390_TLS_IE64:
277 return &elf_howto_table[(int) R_390_TLS_IE64];
278 case BFD_RELOC_390_TLS_IEENT:
279 return &elf_howto_table[(int) R_390_TLS_IEENT];
280 case BFD_RELOC_390_TLS_LE64:
281 return &elf_howto_table[(int) R_390_TLS_LE64];
282 case BFD_RELOC_390_TLS_LDO64:
283 return &elf_howto_table[(int) R_390_TLS_LDO64];
284 case BFD_RELOC_390_TLS_DTPMOD:
285 return &elf_howto_table[(int) R_390_TLS_DTPMOD];
286 case BFD_RELOC_390_TLS_DTPOFF:
287 return &elf_howto_table[(int) R_390_TLS_DTPOFF];
288 case BFD_RELOC_390_TLS_TPOFF:
289 return &elf_howto_table[(int) R_390_TLS_TPOFF];
bd1ea41b
MS
290 case BFD_RELOC_390_20:
291 return &elf_howto_table[(int) R_390_20];
292 case BFD_RELOC_390_GOT20:
293 return &elf_howto_table[(int) R_390_GOT20];
294 case BFD_RELOC_390_GOTPLT20:
295 return &elf_howto_table[(int) R_390_GOTPLT20];
296 case BFD_RELOC_390_TLS_GOTIE20:
297 return &elf_howto_table[(int) R_390_TLS_GOTIE20];
5236c819
MS
298 case BFD_RELOC_VTABLE_INHERIT:
299 return &elf64_s390_vtinherit_howto;
300 case BFD_RELOC_VTABLE_ENTRY:
301 return &elf64_s390_vtentry_howto;
0451c93c
MS
302 default:
303 break;
304 }
a85d7ed0
NC
305 return 0;
306}
307
157090f7
AM
308static reloc_howto_type *
309elf_s390_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
310 const char *r_name)
311{
312 unsigned int i;
313
314 for (i = 0;
315 i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]);
316 i++)
317 if (elf_howto_table[i].name != NULL
318 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
319 return &elf_howto_table[i];
320
321 if (strcasecmp (elf64_s390_vtinherit_howto.name, r_name) == 0)
322 return &elf64_s390_vtinherit_howto;
323 if (strcasecmp (elf64_s390_vtentry_howto.name, r_name) == 0)
324 return &elf64_s390_vtentry_howto;
325
326 return NULL;
327}
328
a85d7ed0
NC
329/* We need to use ELF64_R_TYPE so we have our own copy of this function,
330 and elf64-s390.c has its own copy. */
331
332static void
2c3fc389
NC
333elf_s390_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
334 arelent *cache_ptr,
335 Elf_Internal_Rela *dst)
a85d7ed0 336{
d0fb9a8d
JJ
337 unsigned int r_type = ELF64_R_TYPE(dst->r_info);
338 switch (r_type)
a85d7ed0
NC
339 {
340 case R_390_GNU_VTINHERIT:
341 cache_ptr->howto = &elf64_s390_vtinherit_howto;
342 break;
343
344 case R_390_GNU_VTENTRY:
345 cache_ptr->howto = &elf64_s390_vtentry_howto;
346 break;
347
348 default:
d0fb9a8d
JJ
349 if (r_type >= sizeof (elf_howto_table) / sizeof (elf_howto_table[0]))
350 {
351 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
352 abfd, (int) r_type);
353 r_type = R_390_NONE;
354 }
355 cache_ptr->howto = &elf_howto_table[r_type];
99c79b2e 356 }
a85d7ed0
NC
357}
358
69fc87f1
MS
359/* A relocation function which doesn't do anything. */
360static bfd_reloc_status_type
2c3fc389
NC
361s390_tls_reloc (bfd *abfd ATTRIBUTE_UNUSED,
362 arelent *reloc_entry,
363 asymbol *symbol ATTRIBUTE_UNUSED,
364 void * data ATTRIBUTE_UNUSED,
365 asection *input_section,
366 bfd *output_bfd,
367 char **error_message ATTRIBUTE_UNUSED)
69fc87f1
MS
368{
369 if (output_bfd)
370 reloc_entry->address += input_section->output_offset;
371 return bfd_reloc_ok;
372}
373
bd1ea41b
MS
374/* Handle the large displacement relocs. */
375static bfd_reloc_status_type
2c3fc389
NC
376s390_elf_ldisp_reloc (bfd *abfd,
377 arelent *reloc_entry,
378 asymbol *symbol,
379 void * data,
380 asection *input_section,
381 bfd *output_bfd,
382 char **error_message ATTRIBUTE_UNUSED)
bd1ea41b
MS
383{
384 reloc_howto_type *howto = reloc_entry->howto;
385 bfd_vma relocation;
386 bfd_vma insn;
387
388 if (output_bfd != (bfd *) NULL
389 && (symbol->flags & BSF_SECTION_SYM) == 0
390 && (! howto->partial_inplace
391 || reloc_entry->addend == 0))
392 {
393 reloc_entry->address += input_section->output_offset;
394 return bfd_reloc_ok;
395 }
396 if (output_bfd != NULL)
397 return bfd_reloc_continue;
398
07515404 399 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
bd1ea41b
MS
400 return bfd_reloc_outofrange;
401
402 relocation = (symbol->value
403 + symbol->section->output_section->vma
404 + symbol->section->output_offset);
405 relocation += reloc_entry->addend;
406 if (howto->pc_relative)
407 {
408 relocation -= (input_section->output_section->vma
409 + input_section->output_offset);
410 relocation -= reloc_entry->address;
411 }
412
413 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
414 insn |= (relocation & 0xfff) << 16 | (relocation & 0xff000) >> 4;
415 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
416
417 if ((bfd_signed_vma) relocation < - 0x80000
418 || (bfd_signed_vma) relocation > 0x7ffff)
419 return bfd_reloc_overflow;
420 else
421 return bfd_reloc_ok;
422}
423
b34976b6 424static bfd_boolean
2c3fc389 425elf_s390_is_local_label_name (bfd *abfd, const char *name)
a85d7ed0
NC
426{
427 if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L'))
b34976b6 428 return TRUE;
a85d7ed0
NC
429
430 return _bfd_elf_is_local_label_name (abfd, name);
431}
432
433/* Functions for the 390 ELF linker. */
434
435/* The name of the dynamic interpreter. This is put in the .interp
436 section. */
437
87049b0d 438#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
a85d7ed0 439
64285810
MS
440/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
441 copying dynamic variables from a shared lib into an app's dynbss
442 section, and instead use a dynamic relocation to point into the
443 shared lib. */
444#define ELIMINATE_COPY_RELOCS 1
445
a85d7ed0
NC
446/* The size in bytes of the first entry in the procedure linkage table. */
447#define PLT_FIRST_ENTRY_SIZE 32
448/* The size in bytes of an entry in the procedure linkage table. */
99c79b2e 449#define PLT_ENTRY_SIZE 32
a85d7ed0
NC
450
451#define GOT_ENTRY_SIZE 8
452
453/* The first three entries in a procedure linkage table are reserved,
454 and the initial contents are unimportant (we zero them out).
455 Subsequent entries look like this. See the SVR4 ABI 386
456 supplement to see how this works. */
457
458/* For the s390, simple addr offset can only be 0 - 4096.
459 To use the full 16777216 TB address space, several instructions
460 are needed to load an address in a register and execute
461 a branch( or just saving the address)
462
99c79b2e 463 Furthermore, only r 0 and 1 are free to use!!! */
a85d7ed0
NC
464
465/* The first 3 words in the GOT are then reserved.
466 Word 0 is the address of the dynamic table.
467 Word 1 is a pointer to a structure describing the object
468 Word 2 is used to point to the loader entry address.
469
470 The code for PLT entries looks like this:
471
472 The GOT holds the address in the PLT to be executed.
473 The loader then gets:
474 24(15) = Pointer to the structure describing the object.
99c79b2e 475 28(15) = Offset in symbol table
a85d7ed0
NC
476 The loader must then find the module where the function is
477 and insert the address in the GOT.
478
479 PLT1: LARL 1,<fn>@GOTENT # 6 bytes Load address of GOT entry in r1
480 LG 1,0(1) # 6 bytes Load address from GOT in r1
481 BCR 15,1 # 2 bytes Jump to address
482 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
483 LGF 1,12(1) # 6 bytes Load offset in symbl table in r1
484 BRCL 15,-x # 6 bytes Jump to start of PLT
a7e28bbe 485 .long ? # 4 bytes offset into .rela.plt
a85d7ed0
NC
486
487 Total = 32 bytes per PLT entry
488 Fixup at offset 2: relative address to GOT entry
489 Fixup at offset 22: relative branch to PLT0
a7e28bbe 490 Fixup at offset 28: 32 bit offset into .rela.plt
a85d7ed0 491
a7e28bbe
AK
492 A 32 bit offset into the symbol table is enough. It allows for
493 .rela.plt sections up to a size of 2 gigabyte. A single dynamic
494 object (the main program, any shared library) is limited to 4GB in
495 size. Having a .rela.plt of 2GB would already make the .plt
496 section bigger than 8GB. */
a85d7ed0 497
9aa17453
AK
498static const bfd_byte elf_s390x_plt_entry[PLT_ENTRY_SIZE] =
499 {
500 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, /* larl %r1,. */
501 0xe3, 0x10, 0x10, 0x00, 0x00, 0x04, /* lg %r1,0(%r1) */
502 0x07, 0xf1, /* br %r1 */
503 0x0d, 0x10, /* basr %r1,%r0 */
504 0xe3, 0x10, 0x10, 0x0c, 0x00, 0x14, /* lgf %r1,12(%r1) */
505 0xc0, 0xf4, 0x00, 0x00, 0x00, 0x00, /* jg first plt */
506 0x00, 0x00, 0x00, 0x00 /* .long 0x00000000 */
507 };
a85d7ed0
NC
508
509/* The first PLT entry pushes the offset into the symbol table
a7e28bbe
AK
510 from R1 onto the stack at 56(15) and the loader object info
511 at 48(15), loads the loader address in R1 and jumps to it. */
a85d7ed0
NC
512
513/* The first entry in the PLT:
514
515 PLT0:
516 STG 1,56(15) # r1 contains the offset into the symbol table
517 LARL 1,_GLOBAL_OFFSET_TABLE # load address of global offset table
518 MVC 48(8,15),8(1) # move loader ino (object struct address) to stack
519 LG 1,16(1) # get entry address of loader
520 BCR 15,1 # jump to loader
521
522 Fixup at offset 8: relative address to start of GOT. */
523
9aa17453
AK
524static const bfd_byte elf_s390x_first_plt_entry[PLT_FIRST_ENTRY_SIZE] =
525 {
526 0xe3, 0x10, 0xf0, 0x38, 0x00, 0x24, /* stg %r1,56(%r15) */
527 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, /* larl %r1,. */
528 0xd2, 0x07, 0xf0, 0x30, 0x10, 0x08, /* mvc 48(8,%r15),8(%r1) */
529 0xe3, 0x10, 0x10, 0x10, 0x00, 0x04, /* lg %r1,16(%r1) */
530 0x07, 0xf1, /* br %r1 */
531 0x07, 0x00, /* nopr %r0 */
532 0x07, 0x00, /* nopr %r0 */
533 0x07, 0x00 /* nopr %r0 */
534 };
a85d7ed0 535
a85d7ed0
NC
536
537/* s390 ELF linker hash entry. */
538
539struct elf_s390_link_hash_entry
540{
0451c93c 541 struct elf_link_hash_entry elf;
a85d7ed0 542
0451c93c 543 /* Track dynamic relocs copied for this symbol. */
00d8c7a9 544 struct elf_dyn_relocs *dyn_relocs;
5236c819
MS
545
546 /* Number of GOTPLT references for a function. */
547 bfd_signed_vma gotplt_refcount;
69fc87f1
MS
548
549#define GOT_UNKNOWN 0
550#define GOT_NORMAL 1
551#define GOT_TLS_GD 2
552#define GOT_TLS_IE 3
553#define GOT_TLS_IE_NLT 3
554 unsigned char tls_type;
a85d7ed0
NC
555};
556
69fc87f1
MS
557#define elf_s390_hash_entry(ent) \
558 ((struct elf_s390_link_hash_entry *)(ent))
559
0ffa91dd
NC
560/* NOTE: Keep this structure in sync with
561 the one declared in elf32-s390.c. */
69fc87f1
MS
562struct elf_s390_obj_tdata
563{
564 struct elf_obj_tdata root;
565
0ffa91dd 566 /* TLS type for each local got entry. */
69fc87f1
MS
567 char *local_got_tls_type;
568};
569
570#define elf_s390_tdata(abfd) \
571 ((struct elf_s390_obj_tdata *) (abfd)->tdata.any)
572
573#define elf_s390_local_got_tls_type(abfd) \
574 (elf_s390_tdata (abfd)->local_got_tls_type)
575
0ffa91dd
NC
576#define is_s390_elf(bfd) \
577 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
578 && elf_tdata (bfd) != NULL \
4dfe6ac6 579 && elf_object_id (bfd) == S390_ELF_DATA)
0ffa91dd 580
69fc87f1 581static bfd_boolean
62d7a5f6 582elf_s390_mkobject (bfd *abfd)
69fc87f1 583{
0ffa91dd 584 return bfd_elf_allocate_object (abfd, sizeof (struct elf_s390_obj_tdata),
4dfe6ac6 585 S390_ELF_DATA);
69fc87f1
MS
586}
587
588static bfd_boolean
2c3fc389 589elf_s390_object_p (bfd *abfd)
69fc87f1 590{
69fc87f1
MS
591 /* Set the right machine number for an s390 elf32 file. */
592 return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_64);
593}
594
a85d7ed0
NC
595/* s390 ELF linker hash table. */
596
597struct elf_s390_link_hash_table
598{
0451c93c 599 struct elf_link_hash_table elf;
a85d7ed0 600
0451c93c 601 /* Short-cuts to get to dynamic linker sections. */
0451c93c
MS
602 asection *sdynbss;
603 asection *srelbss;
ec338859 604
69fc87f1
MS
605 union {
606 bfd_signed_vma refcount;
607 bfd_vma offset;
608 } tls_ldm_got;
609
87d72d41
AM
610 /* Small local sym cache. */
611 struct sym_cache sym_cache;
0451c93c 612};
a85d7ed0
NC
613
614/* Get the s390 ELF linker hash table from a link_info structure. */
615
616#define elf_s390_hash_table(p) \
4dfe6ac6
NC
617 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
618 == S390_ELF_DATA ? ((struct elf_s390_link_hash_table *) ((p)->hash)) : NULL)
a85d7ed0
NC
619
620/* Create an entry in an s390 ELF linker hash table. */
621
622static struct bfd_hash_entry *
2c3fc389
NC
623link_hash_newfunc (struct bfd_hash_entry *entry,
624 struct bfd_hash_table *table,
625 const char *string)
a85d7ed0 626{
a85d7ed0
NC
627 /* Allocate the structure if it has not already been allocated by a
628 subclass. */
0451c93c
MS
629 if (entry == NULL)
630 {
631 entry = bfd_hash_allocate (table,
632 sizeof (struct elf_s390_link_hash_entry));
633 if (entry == NULL)
634 return entry;
635 }
a85d7ed0
NC
636
637 /* Call the allocation method of the superclass. */
0451c93c
MS
638 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
639 if (entry != NULL)
a85d7ed0 640 {
0451c93c
MS
641 struct elf_s390_link_hash_entry *eh;
642
643 eh = (struct elf_s390_link_hash_entry *) entry;
644 eh->dyn_relocs = NULL;
5236c819 645 eh->gotplt_refcount = 0;
69fc87f1 646 eh->tls_type = GOT_UNKNOWN;
a85d7ed0
NC
647 }
648
0451c93c 649 return entry;
a85d7ed0
NC
650}
651
652/* Create an s390 ELF linker hash table. */
653
654static struct bfd_link_hash_table *
2c3fc389 655elf_s390_link_hash_table_create (bfd *abfd)
a85d7ed0
NC
656{
657 struct elf_s390_link_hash_table *ret;
dc810e39 658 bfd_size_type amt = sizeof (struct elf_s390_link_hash_table);
a85d7ed0 659
e2d34d7d 660 ret = (struct elf_s390_link_hash_table *) bfd_malloc (amt);
0451c93c 661 if (ret == NULL)
a85d7ed0
NC
662 return NULL;
663
66eb6687 664 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
665 sizeof (struct elf_s390_link_hash_entry),
666 S390_ELF_DATA))
a85d7ed0 667 {
e2d34d7d 668 free (ret);
a85d7ed0
NC
669 return NULL;
670 }
671
f41345a7
AK
672 ret->elf.sgot = NULL;
673 ret->elf.sgotplt = NULL;
674 ret->elf.srelgot = NULL;
675 ret->elf.splt = NULL;
676 ret->elf.srelplt = NULL;
0451c93c
MS
677 ret->sdynbss = NULL;
678 ret->srelbss = NULL;
69fc87f1 679 ret->tls_ldm_got.refcount = 0;
87d72d41 680 ret->sym_cache.abfd = NULL;
0451c93c
MS
681
682 return &ret->elf.root;
683}
684
685/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
686 shortcuts to them in our hash table. */
687
b34976b6 688static bfd_boolean
4dfe6ac6
NC
689create_got_section (bfd *dynobj,
690 struct bfd_link_info *info)
0451c93c
MS
691{
692 struct elf_s390_link_hash_table *htab;
693
694 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 695 return FALSE;
0451c93c
MS
696
697 htab = elf_s390_hash_table (info);
4dfe6ac6
NC
698 if (htab == NULL)
699 return FALSE;
700
3d4d4302
AM
701 htab->elf.sgot = bfd_get_linker_section (dynobj, ".got");
702 htab->elf.sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
703 htab->elf.srelgot = bfd_get_linker_section (dynobj, ".rela.got");
f41345a7 704 if (!htab->elf.sgot || !htab->elf.sgotplt || !htab->elf.srelgot)
0451c93c 705 abort ();
b34976b6 706 return TRUE;
0451c93c
MS
707}
708
709/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
710 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
711 hash table. */
712
b34976b6 713static bfd_boolean
4dfe6ac6
NC
714elf_s390_create_dynamic_sections (bfd *dynobj,
715 struct bfd_link_info *info)
0451c93c
MS
716{
717 struct elf_s390_link_hash_table *htab;
718
719 htab = elf_s390_hash_table (info);
4dfe6ac6
NC
720 if (htab == NULL)
721 return FALSE;
722
f41345a7 723 if (!htab->elf.sgot && !create_got_section (dynobj, info))
b34976b6 724 return FALSE;
0451c93c
MS
725
726 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 727 return FALSE;
0451c93c 728
3d4d4302
AM
729 htab->elf.splt = bfd_get_linker_section (dynobj, ".plt");
730 htab->elf.srelplt = bfd_get_linker_section (dynobj, ".rela.plt");
731 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
0451c93c 732 if (!info->shared)
3d4d4302 733 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
0451c93c 734
f41345a7 735 if (!htab->elf.splt || !htab->elf.srelplt || !htab->sdynbss
0451c93c
MS
736 || (!info->shared && !htab->srelbss))
737 abort ();
738
b34976b6 739 return TRUE;
a85d7ed0
NC
740}
741
0451c93c
MS
742/* Copy the extra info we tack onto an elf_link_hash_entry. */
743
744static void
2c3fc389
NC
745elf_s390_copy_indirect_symbol (struct bfd_link_info *info,
746 struct elf_link_hash_entry *dir,
747 struct elf_link_hash_entry *ind)
0451c93c
MS
748{
749 struct elf_s390_link_hash_entry *edir, *eind;
750
751 edir = (struct elf_s390_link_hash_entry *) dir;
752 eind = (struct elf_s390_link_hash_entry *) ind;
753
754 if (eind->dyn_relocs != NULL)
755 {
756 if (edir->dyn_relocs != NULL)
757 {
00d8c7a9
AK
758 struct elf_dyn_relocs **pp;
759 struct elf_dyn_relocs *p;
0451c93c 760
fcfa13d2 761 /* Add reloc counts against the indirect sym to the direct sym
0451c93c
MS
762 list. Merge any entries against the same section. */
763 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
764 {
00d8c7a9 765 struct elf_dyn_relocs *q;
0451c93c
MS
766
767 for (q = edir->dyn_relocs; q != NULL; q = q->next)
768 if (q->sec == p->sec)
769 {
770 q->pc_count += p->pc_count;
771 q->count += p->count;
772 *pp = p->next;
773 break;
774 }
775 if (q == NULL)
776 pp = &p->next;
777 }
778 *pp = edir->dyn_relocs;
779 }
780
781 edir->dyn_relocs = eind->dyn_relocs;
782 eind->dyn_relocs = NULL;
783 }
784
69fc87f1
MS
785 if (ind->root.type == bfd_link_hash_indirect
786 && dir->got.refcount <= 0)
787 {
788 edir->tls_type = eind->tls_type;
789 eind->tls_type = GOT_UNKNOWN;
790 }
791
64285810
MS
792 if (ELIMINATE_COPY_RELOCS
793 && ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
794 && dir->dynamic_adjusted)
795 {
796 /* If called to transfer flags for a weakdef during processing
797 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
798 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
799 dir->ref_dynamic |= ind->ref_dynamic;
800 dir->ref_regular |= ind->ref_regular;
801 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
802 dir->needs_plt |= ind->needs_plt;
803 }
64285810 804 else
fcfa13d2 805 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
0451c93c 806}
a85d7ed0 807
69fc87f1 808static int
2c3fc389
NC
809elf_s390_tls_transition (struct bfd_link_info *info,
810 int r_type,
811 int is_local)
69fc87f1
MS
812{
813 if (info->shared)
814 return r_type;
815
816 switch (r_type)
817 {
818 case R_390_TLS_GD64:
819 case R_390_TLS_IE64:
820 if (is_local)
821 return R_390_TLS_LE64;
822 return R_390_TLS_IE64;
823 case R_390_TLS_GOTIE64:
824 if (is_local)
825 return R_390_TLS_LE64;
826 return R_390_TLS_GOTIE64;
827 case R_390_TLS_LDM64:
828 return R_390_TLS_LE64;
829 }
830
831 return r_type;
832}
833
a85d7ed0
NC
834/* Look through the relocs for a section during the first phase, and
835 allocate space in the global offset table or procedure linkage
836 table. */
837
b34976b6 838static bfd_boolean
4dfe6ac6
NC
839elf_s390_check_relocs (bfd *abfd,
840 struct bfd_link_info *info,
841 asection *sec,
842 const Elf_Internal_Rela *relocs)
a85d7ed0 843{
0451c93c 844 struct elf_s390_link_hash_table *htab;
a85d7ed0
NC
845 Elf_Internal_Shdr *symtab_hdr;
846 struct elf_link_hash_entry **sym_hashes;
a85d7ed0
NC
847 const Elf_Internal_Rela *rel;
848 const Elf_Internal_Rela *rel_end;
a85d7ed0 849 asection *sreloc;
5236c819 850 bfd_signed_vma *local_got_refcounts;
69fc87f1 851 int tls_type, old_tls_type;
a85d7ed0 852
1049f94e 853 if (info->relocatable)
b34976b6 854 return TRUE;
a85d7ed0 855
0ffa91dd
NC
856 BFD_ASSERT (is_s390_elf (abfd));
857
0451c93c 858 htab = elf_s390_hash_table (info);
4dfe6ac6
NC
859 if (htab == NULL)
860 return FALSE;
861
0ffa91dd 862 symtab_hdr = &elf_symtab_hdr (abfd);
a85d7ed0 863 sym_hashes = elf_sym_hashes (abfd);
5236c819 864 local_got_refcounts = elf_local_got_refcounts (abfd);
a85d7ed0 865
a85d7ed0
NC
866 sreloc = NULL;
867
868 rel_end = relocs + sec->reloc_count;
869 for (rel = relocs; rel < rel_end; rel++)
870 {
69fc87f1 871 unsigned int r_type;
a85d7ed0
NC
872 unsigned long r_symndx;
873 struct elf_link_hash_entry *h;
874
875 r_symndx = ELF64_R_SYM (rel->r_info);
876
0451c93c
MS
877 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
878 {
d003868e
AM
879 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
880 abfd,
0451c93c 881 r_symndx);
b34976b6 882 return FALSE;
0451c93c
MS
883 }
884
a85d7ed0
NC
885 if (r_symndx < symtab_hdr->sh_info)
886 h = NULL;
887 else
973a3492
L
888 {
889 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
890 while (h->root.type == bfd_link_hash_indirect
891 || h->root.type == bfd_link_hash_warning)
892 h = (struct elf_link_hash_entry *) h->root.u.i.link;
893 }
a85d7ed0 894
5236c819
MS
895 /* Create got section and local_got_refcounts array if they
896 are needed. */
69fc87f1
MS
897 r_type = elf_s390_tls_transition (info,
898 ELF64_R_TYPE (rel->r_info),
899 h == NULL);
900 switch (r_type)
a85d7ed0
NC
901 {
902 case R_390_GOT12:
947216bf 903 case R_390_GOT16:
bd1ea41b 904 case R_390_GOT20:
a85d7ed0
NC
905 case R_390_GOT32:
906 case R_390_GOT64:
907 case R_390_GOTENT:
5236c819
MS
908 case R_390_GOTPLT12:
909 case R_390_GOTPLT16:
bd1ea41b 910 case R_390_GOTPLT20:
5236c819
MS
911 case R_390_GOTPLT32:
912 case R_390_GOTPLT64:
913 case R_390_GOTPLTENT:
69fc87f1
MS
914 case R_390_TLS_GD64:
915 case R_390_TLS_GOTIE12:
bd1ea41b 916 case R_390_TLS_GOTIE20:
69fc87f1
MS
917 case R_390_TLS_GOTIE64:
918 case R_390_TLS_IEENT:
919 case R_390_TLS_IE64:
920 case R_390_TLS_LDM64:
5236c819
MS
921 if (h == NULL
922 && local_got_refcounts == NULL)
a85d7ed0 923 {
5236c819 924 bfd_size_type size;
0451c93c 925
5236c819 926 size = symtab_hdr->sh_info;
69fc87f1 927 size *= (sizeof (bfd_signed_vma) + sizeof(char));
5236c819
MS
928 local_got_refcounts = ((bfd_signed_vma *)
929 bfd_zalloc (abfd, size));
a85d7ed0 930 if (local_got_refcounts == NULL)
5236c819
MS
931 return FALSE;
932 elf_local_got_refcounts (abfd) = local_got_refcounts;
69fc87f1
MS
933 elf_s390_local_got_tls_type (abfd)
934 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
a85d7ed0 935 }
5236c819
MS
936 /* Fall through. */
937 case R_390_GOTOFF16:
938 case R_390_GOTOFF32:
939 case R_390_GOTOFF64:
0451c93c
MS
940 case R_390_GOTPC:
941 case R_390_GOTPCDBL:
f41345a7 942 if (htab->elf.sgot == NULL)
0451c93c
MS
943 {
944 if (htab->elf.dynobj == NULL)
945 htab->elf.dynobj = abfd;
946 if (!create_got_section (htab->elf.dynobj, info))
b34976b6 947 return FALSE;
0451c93c 948 }
5236c819
MS
949 }
950
69fc87f1 951 switch (r_type)
5236c819 952 {
5236c819
MS
953 case R_390_GOTOFF16:
954 case R_390_GOTOFF32:
955 case R_390_GOTOFF64:
956 case R_390_GOTPC:
957 case R_390_GOTPCDBL:
958 /* Got is created, nothing to be done. */
0451c93c 959 break;
ec338859 960
947216bf 961 case R_390_PLT16DBL:
a85d7ed0
NC
962 case R_390_PLT32:
963 case R_390_PLT32DBL:
964 case R_390_PLT64:
5236c819
MS
965 case R_390_PLTOFF16:
966 case R_390_PLTOFF32:
967 case R_390_PLTOFF64:
a85d7ed0 968 /* This symbol requires a procedure linkage table entry. We
947216bf
AM
969 actually build the entry in adjust_dynamic_symbol,
970 because this might be a case of linking PIC code which is
971 never referenced by a dynamic object, in which case we
972 don't need to generate a procedure linkage table entry
973 after all. */
ec338859 974
a85d7ed0 975 /* If this is a local symbol, we resolve it directly without
947216bf 976 creating a procedure linkage table entry. */
5236c819
MS
977 if (h != NULL)
978 {
f5385ebf 979 h->needs_plt = 1;
5236c819
MS
980 h->plt.refcount += 1;
981 }
982 break;
ec338859 983
5236c819
MS
984 case R_390_GOTPLT12:
985 case R_390_GOTPLT16:
bd1ea41b 986 case R_390_GOTPLT20:
5236c819
MS
987 case R_390_GOTPLT32:
988 case R_390_GOTPLT64:
989 case R_390_GOTPLTENT:
990 /* This symbol requires either a procedure linkage table entry
991 or an entry in the local got. We actually build the entry
992 in adjust_dynamic_symbol because whether this is really a
993 global reference can change and with it the fact if we have
994 to create a plt entry or a local got entry. To be able to
995 make a once global symbol a local one we have to keep track
996 of the number of gotplt references that exist for this
997 symbol. */
998 if (h != NULL)
999 {
1000 ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount++;
f5385ebf 1001 h->needs_plt = 1;
5236c819
MS
1002 h->plt.refcount += 1;
1003 }
69fc87f1
MS
1004 else
1005 local_got_refcounts[r_symndx] += 1;
1006 break;
1007
1008 case R_390_TLS_LDM64:
1009 htab->tls_ldm_got.refcount += 1;
1010 break;
1011
1012 case R_390_TLS_IE64:
1013 case R_390_TLS_GOTIE12:
bd1ea41b 1014 case R_390_TLS_GOTIE20:
69fc87f1
MS
1015 case R_390_TLS_GOTIE64:
1016 case R_390_TLS_IEENT:
1017 if (info->shared)
1018 info->flags |= DF_STATIC_TLS;
1019 /* Fall through */
1020
1021 case R_390_GOT12:
26e41594 1022 case R_390_GOT16:
bd1ea41b 1023 case R_390_GOT20:
69fc87f1
MS
1024 case R_390_GOT32:
1025 case R_390_GOT64:
1026 case R_390_GOTENT:
1027 case R_390_TLS_GD64:
1028 /* This symbol requires a global offset table entry. */
1029 switch (r_type)
1030 {
1031 default:
1032 case R_390_GOT12:
1033 case R_390_GOT16:
bd1ea41b 1034 case R_390_GOT20:
69fc87f1
MS
1035 case R_390_GOT32:
1036 case R_390_GOTENT:
1037 tls_type = GOT_NORMAL;
1038 break;
1039 case R_390_TLS_GD64:
1040 tls_type = GOT_TLS_GD;
1041 break;
1042 case R_390_TLS_IE64:
1043 case R_390_TLS_GOTIE64:
1044 tls_type = GOT_TLS_IE;
1045 break;
1046 case R_390_TLS_GOTIE12:
bd1ea41b 1047 case R_390_TLS_GOTIE20:
69fc87f1
MS
1048 case R_390_TLS_IEENT:
1049 tls_type = GOT_TLS_IE_NLT;
1050 break;
1051 }
1052
1053 if (h != NULL)
1054 {
1055 h->got.refcount += 1;
1056 old_tls_type = elf_s390_hash_entry(h)->tls_type;
1057 }
5236c819
MS
1058 else
1059 {
1060 local_got_refcounts[r_symndx] += 1;
69fc87f1 1061 old_tls_type = elf_s390_local_got_tls_type (abfd) [r_symndx];
5236c819 1062 }
69fc87f1
MS
1063 /* If a TLS symbol is accessed using IE at least once,
1064 there is no point to use dynamic model for it. */
1065 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN)
1066 {
1067 if (old_tls_type == GOT_NORMAL || tls_type == GOT_NORMAL)
1068 {
1069 (*_bfd_error_handler)
d003868e
AM
1070 (_("%B: `%s' accessed both as normal and thread local symbol"),
1071 abfd, h->root.root.string);
69fc87f1
MS
1072 return FALSE;
1073 }
1074 if (old_tls_type > tls_type)
1075 tls_type = old_tls_type;
1076 }
1077
1078 if (old_tls_type != tls_type)
1079 {
1080 if (h != NULL)
1081 elf_s390_hash_entry (h)->tls_type = tls_type;
1082 else
1083 elf_s390_local_got_tls_type (abfd) [r_symndx] = tls_type;
1084 }
1085
1086 if (r_type != R_390_TLS_IE64)
1087 break;
1088 /* Fall through */
1089
1090 case R_390_TLS_LE64:
1091 if (!info->shared)
1092 break;
1093 info->flags |= DF_STATIC_TLS;
1094 /* Fall through */
ec338859 1095
947216bf
AM
1096 case R_390_8:
1097 case R_390_16:
a85d7ed0
NC
1098 case R_390_32:
1099 case R_390_64:
947216bf
AM
1100 case R_390_PC16:
1101 case R_390_PC16DBL:
a85d7ed0
NC
1102 case R_390_PC32:
1103 case R_390_PC32DBL:
1104 case R_390_PC64:
0451c93c
MS
1105 if (h != NULL && !info->shared)
1106 {
1107 /* If this reloc is in a read-only section, we might
1108 need a copy reloc. We can't check reliably at this
1109 stage whether the section is read-only, as input
1110 sections have not yet been mapped to output sections.
1111 Tentatively set the flag for now, and correct in
1112 adjust_dynamic_symbol. */
f5385ebf 1113 h->non_got_ref = 1;
ec338859 1114
0451c93c
MS
1115 /* We may need a .plt entry if the function this reloc
1116 refers to is in a shared lib. */
1117 h->plt.refcount += 1;
1118 }
ec338859 1119
a85d7ed0 1120 /* If we are creating a shared library, and this is a reloc
0451c93c
MS
1121 against a global symbol, or a non PC relative reloc
1122 against a local symbol, then we need to copy the reloc
1123 into the shared library. However, if we are linking with
1124 -Bsymbolic, we do not need to copy a reloc against a
1125 global symbol which is defined in an object we are
1126 including in the link (i.e., DEF_REGULAR is set). At
1127 this point we have not seen all the input files, so it is
1128 possible that DEF_REGULAR is not set now but will be set
1129 later (it is never cleared). In case of a weak definition,
1130 DEF_REGULAR may be cleared later by a strong definition in
1131 a shared library. We account for that possibility below by
1132 storing information in the relocs_copied field of the hash
1133 table entry. A similar situation occurs when creating
1134 shared libraries and symbol visibility changes render the
1135 symbol local.
1136
1137 If on the other hand, we are creating an executable, we
1138 may need to keep relocations for symbols satisfied by a
1139 dynamic library if we manage to avoid copy relocs for the
1140 symbol. */
1141 if ((info->shared
1142 && (sec->flags & SEC_ALLOC) != 0
1143 && ((ELF64_R_TYPE (rel->r_info) != R_390_PC16
1144 && ELF64_R_TYPE (rel->r_info) != R_390_PC16DBL
1145 && ELF64_R_TYPE (rel->r_info) != R_390_PC32
1146 && ELF64_R_TYPE (rel->r_info) != R_390_PC32DBL
1147 && ELF64_R_TYPE (rel->r_info) != R_390_PC64)
1148 || (h != NULL
8c21ca21 1149 && (! SYMBOLIC_BIND (info, h)
0451c93c 1150 || h->root.type == bfd_link_hash_defweak
f5385ebf 1151 || !h->def_regular))))
64285810
MS
1152 || (ELIMINATE_COPY_RELOCS
1153 && !info->shared
0451c93c
MS
1154 && (sec->flags & SEC_ALLOC) != 0
1155 && h != NULL
1156 && (h->root.type == bfd_link_hash_defweak
f5385ebf 1157 || !h->def_regular)))
a85d7ed0 1158 {
00d8c7a9
AK
1159 struct elf_dyn_relocs *p;
1160 struct elf_dyn_relocs **head;
ec338859 1161
0451c93c
MS
1162 /* We must copy these reloc types into the output file.
1163 Create a reloc section in dynobj and make room for
1164 this reloc. */
a85d7ed0
NC
1165 if (sreloc == NULL)
1166 {
440e9cd2
AK
1167 if (htab->elf.dynobj == NULL)
1168 htab->elf.dynobj = abfd;
1169
83bac4b0
NC
1170 sreloc = _bfd_elf_make_dynamic_reloc_section
1171 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
ec338859 1172
a85d7ed0 1173 if (sreloc == NULL)
83bac4b0 1174 return FALSE;
a85d7ed0 1175 }
ec338859 1176
0451c93c
MS
1177 /* If this is a global symbol, we count the number of
1178 relocations we need for this symbol. */
1179 if (h != NULL)
a85d7ed0 1180 {
ec338859 1181 head = &((struct elf_s390_link_hash_entry *) h)->dyn_relocs;
0451c93c
MS
1182 }
1183 else
1184 {
ec338859
AM
1185 /* Track dynamic relocs needed for local syms too.
1186 We really need local syms available to do this
1187 easily. Oh well. */
ec338859 1188 asection *s;
6edfbbad 1189 void *vpp;
87d72d41 1190 Elf_Internal_Sym *isym;
6edfbbad 1191
87d72d41
AM
1192 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1193 abfd, r_symndx);
1194 if (isym == NULL)
b34976b6 1195 return FALSE;
ec338859 1196
87d72d41
AM
1197 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1198 if (s == NULL)
1199 s = sec;
1200
6edfbbad 1201 vpp = &elf_section_data (s)->local_dynrel;
00d8c7a9 1202 head = (struct elf_dyn_relocs **) vpp;
ec338859
AM
1203 }
1204
1205 p = *head;
1206 if (p == NULL || p->sec != sec)
1207 {
1208 bfd_size_type amt = sizeof *p;
00d8c7a9 1209 p = ((struct elf_dyn_relocs *)
ec338859
AM
1210 bfd_alloc (htab->elf.dynobj, amt));
1211 if (p == NULL)
b34976b6 1212 return FALSE;
ec338859
AM
1213 p->next = *head;
1214 *head = p;
1215 p->sec = sec;
1216 p->count = 0;
1217 p->pc_count = 0;
a85d7ed0 1218 }
ec338859
AM
1219
1220 p->count += 1;
1221 if (ELF64_R_TYPE (rel->r_info) == R_390_PC16
1222 || ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL
1223 || ELF64_R_TYPE (rel->r_info) == R_390_PC32
1224 || ELF64_R_TYPE (rel->r_info) == R_390_PC32DBL
1225 || ELF64_R_TYPE (rel->r_info) == R_390_PC64)
1226 p->pc_count += 1;
a85d7ed0 1227 }
a85d7ed0 1228 break;
ec338859 1229
a85d7ed0
NC
1230 /* This relocation describes the C++ object vtable hierarchy.
1231 Reconstruct it for later use during GC. */
947216bf 1232 case R_390_GNU_VTINHERIT:
c152c796 1233 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 1234 return FALSE;
947216bf 1235 break;
ec338859 1236
a85d7ed0
NC
1237 /* This relocation describes which C++ vtable entries are actually
1238 used. Record for later use during GC. */
947216bf 1239 case R_390_GNU_VTENTRY:
d17e0c6e
JB
1240 BFD_ASSERT (h != NULL);
1241 if (h != NULL
1242 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 1243 return FALSE;
947216bf 1244 break;
ec338859 1245
a85d7ed0
NC
1246 default:
1247 break;
1248 }
1249 }
1250
b34976b6 1251 return TRUE;
a85d7ed0
NC
1252}
1253
1254/* Return the section that should be marked against GC for a given
1255 relocation. */
1256
1257static asection *
07adf181
AM
1258elf_s390_gc_mark_hook (asection *sec,
1259 struct bfd_link_info *info,
1260 Elf_Internal_Rela *rel,
1261 struct elf_link_hash_entry *h,
1262 Elf_Internal_Sym *sym)
a85d7ed0
NC
1263{
1264 if (h != NULL)
07adf181
AM
1265 switch (ELF64_R_TYPE (rel->r_info))
1266 {
1267 case R_390_GNU_VTINHERIT:
1268 case R_390_GNU_VTENTRY:
1269 return NULL;
1270 }
1271
1272 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
a85d7ed0
NC
1273}
1274
1275/* Update the got entry reference counts for the section being removed. */
1276
b34976b6 1277static bfd_boolean
07adf181
AM
1278elf_s390_gc_sweep_hook (bfd *abfd,
1279 struct bfd_link_info *info,
1280 asection *sec,
1281 const Elf_Internal_Rela *relocs)
a85d7ed0 1282{
4dfe6ac6 1283 struct elf_s390_link_hash_table *htab;
a85d7ed0
NC
1284 Elf_Internal_Shdr *symtab_hdr;
1285 struct elf_link_hash_entry **sym_hashes;
1286 bfd_signed_vma *local_got_refcounts;
1287 const Elf_Internal_Rela *rel, *relend;
a85d7ed0 1288
7dda2462
TG
1289 if (info->relocatable)
1290 return TRUE;
1291
4dfe6ac6
NC
1292 htab = elf_s390_hash_table (info);
1293 if (htab == NULL)
1294 return FALSE;
1295
ec338859 1296 elf_section_data (sec)->local_dynrel = NULL;
a85d7ed0 1297
0ffa91dd 1298 symtab_hdr = &elf_symtab_hdr (abfd);
0451c93c
MS
1299 sym_hashes = elf_sym_hashes (abfd);
1300 local_got_refcounts = elf_local_got_refcounts (abfd);
a85d7ed0
NC
1301
1302 relend = relocs + sec->reloc_count;
1303 for (rel = relocs; rel < relend; rel++)
5236c819 1304 {
26e41594
AM
1305 unsigned long r_symndx;
1306 unsigned int r_type;
1307 struct elf_link_hash_entry *h = NULL;
1308
5236c819 1309 r_symndx = ELF64_R_SYM (rel->r_info);
26e41594
AM
1310 if (r_symndx >= symtab_hdr->sh_info)
1311 {
1312 struct elf_s390_link_hash_entry *eh;
00d8c7a9
AK
1313 struct elf_dyn_relocs **pp;
1314 struct elf_dyn_relocs *p;
5236c819 1315
26e41594 1316 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
1317 while (h->root.type == bfd_link_hash_indirect
1318 || h->root.type == bfd_link_hash_warning)
1319 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 1320 eh = (struct elf_s390_link_hash_entry *) h;
5236c819 1321
26e41594
AM
1322 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1323 if (p->sec == sec)
1324 {
1325 /* Everything must go for SEC. */
1326 *pp = p->next;
1327 break;
1328 }
1329 }
1330
1331 r_type = ELF64_R_TYPE (rel->r_info);
1332 r_type = elf_s390_tls_transition (info, r_type, h != NULL);
69fc87f1 1333 switch (r_type)
5236c819 1334 {
69fc87f1 1335 case R_390_TLS_LDM64:
4dfe6ac6
NC
1336 if (htab->tls_ldm_got.refcount > 0)
1337 htab->tls_ldm_got.refcount -= 1;
69fc87f1
MS
1338 break;
1339
1340 case R_390_TLS_GD64:
1341 case R_390_TLS_IE64:
1342 case R_390_TLS_GOTIE12:
bd1ea41b 1343 case R_390_TLS_GOTIE20:
69fc87f1
MS
1344 case R_390_TLS_GOTIE64:
1345 case R_390_TLS_IEENT:
5236c819
MS
1346 case R_390_GOT12:
1347 case R_390_GOT16:
bd1ea41b 1348 case R_390_GOT20:
5236c819
MS
1349 case R_390_GOT32:
1350 case R_390_GOT64:
1351 case R_390_GOTOFF16:
1352 case R_390_GOTOFF32:
1353 case R_390_GOTOFF64:
1354 case R_390_GOTPC:
1355 case R_390_GOTPCDBL:
1356 case R_390_GOTENT:
1357 if (h != NULL)
1358 {
1359 if (h->got.refcount > 0)
1360 h->got.refcount -= 1;
1361 }
1362 else if (local_got_refcounts != NULL)
1363 {
1364 if (local_got_refcounts[r_symndx] > 0)
1365 local_got_refcounts[r_symndx] -= 1;
1366 }
26e41594 1367 break;
a85d7ed0 1368
5236c819
MS
1369 case R_390_8:
1370 case R_390_12:
1371 case R_390_16:
bd1ea41b 1372 case R_390_20:
5236c819
MS
1373 case R_390_32:
1374 case R_390_64:
1375 case R_390_PC16:
1376 case R_390_PC16DBL:
1377 case R_390_PC32:
1378 case R_390_PC32DBL:
1379 case R_390_PC64:
26e41594
AM
1380 if (info->shared)
1381 break;
1382 /* Fall through */
a85d7ed0 1383
69fc87f1
MS
1384 case R_390_PLT16DBL:
1385 case R_390_PLT32:
1386 case R_390_PLT32DBL:
1387 case R_390_PLT64:
1388 case R_390_PLTOFF16:
1389 case R_390_PLTOFF32:
1390 case R_390_PLTOFF64:
1391 if (h != NULL)
1392 {
1393 if (h->plt.refcount > 0)
1394 h->plt.refcount -= 1;
1395 }
1396 break;
1397
1398 case R_390_GOTPLT12:
1399 case R_390_GOTPLT16:
bd1ea41b 1400 case R_390_GOTPLT20:
69fc87f1
MS
1401 case R_390_GOTPLT32:
1402 case R_390_GOTPLT64:
1403 case R_390_GOTPLTENT:
1404 if (h != NULL)
1405 {
1406 if (h->plt.refcount > 0)
1407 {
1408 ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount--;
1409 h->plt.refcount -= 1;
1410 }
1411 }
1412 else if (local_got_refcounts != NULL)
1413 {
1414 if (local_got_refcounts[r_symndx] > 0)
1415 local_got_refcounts[r_symndx] -= 1;
1416 }
1417 break;
1418
5236c819
MS
1419 default:
1420 break;
1421 }
1422 }
a85d7ed0 1423
b34976b6 1424 return TRUE;
a85d7ed0
NC
1425}
1426
5236c819
MS
1427/* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1428 entry but we found we will not create any. Called when we find we will
1429 not have any PLT for this symbol, by for example
1430 elf_s390_adjust_dynamic_symbol when we're doing a proper dynamic link,
1431 or elf_s390_size_dynamic_sections if no dynamic sections will be
1432 created (we're only linking static objects). */
1433
1434static void
2c3fc389 1435elf_s390_adjust_gotplt (struct elf_s390_link_hash_entry *h)
5236c819
MS
1436{
1437 if (h->elf.root.type == bfd_link_hash_warning)
1438 h = (struct elf_s390_link_hash_entry *) h->elf.root.u.i.link;
1439
1440 if (h->gotplt_refcount <= 0)
1441 return;
1442
1443 /* We simply add the number of gotplt references to the number
1444 * of got references for this symbol. */
1445 h->elf.got.refcount += h->gotplt_refcount;
1446 h->gotplt_refcount = -1;
1447}
1448
a85d7ed0
NC
1449/* Adjust a symbol defined by a dynamic object and referenced by a
1450 regular object. The current definition is in some section of the
1451 dynamic object, but we're not including those sections. We have to
1452 change the definition to something the rest of the link can
1453 understand. */
1454
b34976b6 1455static bfd_boolean
4dfe6ac6
NC
1456elf_s390_adjust_dynamic_symbol (struct bfd_link_info *info,
1457 struct elf_link_hash_entry *h)
a85d7ed0 1458{
0451c93c 1459 struct elf_s390_link_hash_table *htab;
a85d7ed0 1460 asection *s;
a85d7ed0 1461
a85d7ed0
NC
1462 /* If this is a function, put it in the procedure linkage table. We
1463 will fill in the contents of the procedure linkage table later
cedb70c5 1464 (although we could actually do it here). */
a85d7ed0 1465 if (h->type == STT_FUNC
f5385ebf 1466 || h->needs_plt)
a85d7ed0 1467 {
0451c93c 1468 if (h->plt.refcount <= 0
8c21ca21
AK
1469 || SYMBOL_CALLS_LOCAL (info, h)
1470 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1471 && h->root.type == bfd_link_hash_undefweak))
a85d7ed0
NC
1472 {
1473 /* This case can occur if we saw a PLT32 reloc in an input
947216bf
AM
1474 file, but the symbol was never referred to by a dynamic
1475 object, or if all references were garbage collected. In
0451c93c
MS
1476 such a case, we don't actually need to build a procedure
1477 linkage table, and we can just do a PC32 reloc instead. */
a85d7ed0 1478 h->plt.offset = (bfd_vma) -1;
f5385ebf 1479 h->needs_plt = 0;
5236c819 1480 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
a85d7ed0
NC
1481 }
1482
b34976b6 1483 return TRUE;
a85d7ed0 1484 }
bbd7ec4a 1485 else
0451c93c
MS
1486 /* It's possible that we incorrectly decided a .plt reloc was
1487 needed for an R_390_PC32 reloc to a non-function sym in
1488 check_relocs. We can't decide accurately between function and
1489 non-function syms in check-relocs; Objects loaded later in
1490 the link may change h->type. So fix it now. */
bbd7ec4a 1491 h->plt.offset = (bfd_vma) -1;
a85d7ed0
NC
1492
1493 /* If this is a weak symbol, and there is a real definition, the
1494 processor independent code will have arranged for us to see the
1495 real definition first, and we can just use the same value. */
f6e332e6 1496 if (h->u.weakdef != NULL)
a85d7ed0 1497 {
f6e332e6
AM
1498 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1499 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1500 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1501 h->root.u.def.value = h->u.weakdef->root.u.def.value;
64285810 1502 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 1503 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 1504 return TRUE;
a85d7ed0
NC
1505 }
1506
1507 /* This is a reference to a symbol defined by a dynamic object which
1508 is not a function. */
1509
1510 /* If we are creating a shared library, we must presume that the
1511 only references to the symbol are via the global offset table.
1512 For such cases we need not do anything here; the relocations will
1513 be handled correctly by relocate_section. */
1514 if (info->shared)
b34976b6 1515 return TRUE;
a85d7ed0
NC
1516
1517 /* If there are no references to this symbol that do not use the
1518 GOT, we don't need to generate a copy reloc. */
f5385ebf 1519 if (!h->non_got_ref)
b34976b6 1520 return TRUE;
a85d7ed0 1521
0451c93c
MS
1522 /* If -z nocopyreloc was given, we won't generate them either. */
1523 if (info->nocopyreloc)
1524 {
f5385ebf 1525 h->non_got_ref = 0;
b34976b6 1526 return TRUE;
0451c93c
MS
1527 }
1528
64285810 1529 if (ELIMINATE_COPY_RELOCS)
0451c93c 1530 {
64285810 1531 struct elf_s390_link_hash_entry * eh;
00d8c7a9 1532 struct elf_dyn_relocs *p;
0451c93c 1533
64285810
MS
1534 eh = (struct elf_s390_link_hash_entry *) h;
1535 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1536 {
1537 s = p->sec->output_section;
1538 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1539 break;
1540 }
1541
1542 /* If we didn't find any dynamic relocs in read-only sections, then
1543 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1544 if (p == NULL)
1545 {
f5385ebf 1546 h->non_got_ref = 0;
64285810
MS
1547 return TRUE;
1548 }
0451c93c
MS
1549 }
1550
a85d7ed0
NC
1551 /* We must allocate the symbol in our .dynbss section, which will
1552 become part of the .bss section of the executable. There will be
1553 an entry for this symbol in the .dynsym section. The dynamic
1554 object will contain position independent code, so all references
1555 from the dynamic object to this symbol will go through the global
1556 offset table. The dynamic linker will use the .dynsym entry to
1557 determine the address it must put in the global offset table, so
1558 both the dynamic object and the regular object will refer to the
1559 same memory location for the variable. */
1560
0451c93c 1561 htab = elf_s390_hash_table (info);
4dfe6ac6
NC
1562 if (htab == NULL)
1563 return FALSE;
a85d7ed0 1564
0451c93c
MS
1565 /* We must generate a R_390_COPY reloc to tell the dynamic linker to
1566 copy the initial value out of the dynamic object and into the
1567 runtime process image. */
1d7e9d18 1568 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
a85d7ed0 1569 {
eea6121a 1570 htab->srelbss->size += sizeof (Elf64_External_Rela);
f5385ebf 1571 h->needs_copy = 1;
a85d7ed0
NC
1572 }
1573
0451c93c 1574 s = htab->sdynbss;
a85d7ed0 1575
027297b7 1576 return _bfd_elf_adjust_dynamic_copy (h, s);
a85d7ed0
NC
1577}
1578
0451c93c
MS
1579/* Allocate space in .plt, .got and associated reloc sections for
1580 dynamic relocs. */
1581
b34976b6 1582static bfd_boolean
4dfe6ac6
NC
1583allocate_dynrelocs (struct elf_link_hash_entry *h,
1584 void * inf)
0451c93c
MS
1585{
1586 struct bfd_link_info *info;
1587 struct elf_s390_link_hash_table *htab;
1588 struct elf_s390_link_hash_entry *eh;
00d8c7a9 1589 struct elf_dyn_relocs *p;
0451c93c 1590
e92d460e 1591 if (h->root.type == bfd_link_hash_indirect)
b34976b6 1592 return TRUE;
0451c93c
MS
1593
1594 info = (struct bfd_link_info *) inf;
1595 htab = elf_s390_hash_table (info);
4dfe6ac6
NC
1596 if (htab == NULL)
1597 return FALSE;
0451c93c
MS
1598
1599 if (htab->elf.dynamic_sections_created
8c21ca21 1600 && h->plt.refcount > 0)
0451c93c
MS
1601 {
1602 /* Make sure this symbol is output as a dynamic symbol.
1603 Undefined weak syms won't yet be marked as dynamic. */
1604 if (h->dynindx == -1
f5385ebf 1605 && !h->forced_local)
0451c93c 1606 {
c152c796 1607 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1608 return FALSE;
0451c93c
MS
1609 }
1610
82058a73 1611 if (info->shared
4ec72bde 1612 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
0451c93c 1613 {
f41345a7 1614 asection *s = htab->elf.splt;
0451c93c
MS
1615
1616 /* If this is the first .plt entry, make room for the special
1617 first entry. */
eea6121a
AM
1618 if (s->size == 0)
1619 s->size += PLT_FIRST_ENTRY_SIZE;
0451c93c 1620
eea6121a 1621 h->plt.offset = s->size;
0451c93c
MS
1622
1623 /* If this symbol is not defined in a regular file, and we are
1624 not generating a shared library, then set the symbol to this
1625 location in the .plt. This is required to make function
1626 pointers compare as equal between the normal executable and
1627 the shared library. */
1628 if (! info->shared
f5385ebf 1629 && !h->def_regular)
0451c93c
MS
1630 {
1631 h->root.u.def.section = s;
1632 h->root.u.def.value = h->plt.offset;
1633 }
ec338859 1634
0451c93c 1635 /* Make room for this entry. */
eea6121a 1636 s->size += PLT_ENTRY_SIZE;
ec338859 1637
0451c93c
MS
1638 /* We also need to make an entry in the .got.plt section, which
1639 will be placed in the .got section by the linker script. */
f41345a7 1640 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
0451c93c
MS
1641
1642 /* We also need to make an entry in the .rela.plt section. */
f41345a7 1643 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
0451c93c
MS
1644 }
1645 else
1646 {
1647 h->plt.offset = (bfd_vma) -1;
f5385ebf 1648 h->needs_plt = 0;
5236c819 1649 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
0451c93c
MS
1650 }
1651 }
1652 else
1653 {
1654 h->plt.offset = (bfd_vma) -1;
f5385ebf 1655 h->needs_plt = 0;
5236c819 1656 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
0451c93c
MS
1657 }
1658
69fc87f1
MS
1659 /* If R_390_TLS_{IE64,GOTIE64,GOTIE12,IEENT} symbol is now local to
1660 the binary, we can optimize a bit. IE64 and GOTIE64 get converted
1661 to R_390_TLS_LE64 requiring no TLS entry. For GOTIE12 and IEENT
1662 we can save the dynamic TLS relocation. */
1663 if (h->got.refcount > 0
1664 && !info->shared
1665 && h->dynindx == -1
1666 && elf_s390_hash_entry(h)->tls_type >= GOT_TLS_IE)
1667 {
1668 if (elf_s390_hash_entry(h)->tls_type == GOT_TLS_IE_NLT)
1669 /* For the GOTIE access without a literal pool entry the offset has
1670 to be stored somewhere. The immediate value in the instruction
1671 is not bit enough so the value is stored in the got. */
1672 {
f41345a7
AK
1673 h->got.offset = htab->elf.sgot->size;
1674 htab->elf.sgot->size += GOT_ENTRY_SIZE;
69fc87f1
MS
1675 }
1676 else
1677 h->got.offset = (bfd_vma) -1;
1678 }
1679 else if (h->got.refcount > 0)
0451c93c
MS
1680 {
1681 asection *s;
b34976b6 1682 bfd_boolean dyn;
69fc87f1 1683 int tls_type = elf_s390_hash_entry(h)->tls_type;
0451c93c
MS
1684
1685 /* Make sure this symbol is output as a dynamic symbol.
1686 Undefined weak syms won't yet be marked as dynamic. */
1687 if (h->dynindx == -1
f5385ebf 1688 && !h->forced_local)
0451c93c 1689 {
c152c796 1690 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1691 return FALSE;
0451c93c
MS
1692 }
1693
f41345a7 1694 s = htab->elf.sgot;
eea6121a
AM
1695 h->got.offset = s->size;
1696 s->size += GOT_ENTRY_SIZE;
69fc87f1
MS
1697 /* R_390_TLS_GD64 needs 2 consecutive GOT slots. */
1698 if (tls_type == GOT_TLS_GD)
eea6121a 1699 s->size += GOT_ENTRY_SIZE;
0451c93c 1700 dyn = htab->elf.dynamic_sections_created;
69fc87f1
MS
1701 /* R_390_TLS_IE64 needs one dynamic relocation,
1702 R_390_TLS_GD64 needs one if local symbol and two if global. */
1703 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1704 || tls_type >= GOT_TLS_IE)
f41345a7 1705 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
69fc87f1 1706 else if (tls_type == GOT_TLS_GD)
f41345a7 1707 htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
82058a73
MS
1708 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1709 || h->root.type != bfd_link_hash_undefweak)
1710 && (info->shared
1711 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
f41345a7 1712 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
0451c93c
MS
1713 }
1714 else
1715 h->got.offset = (bfd_vma) -1;
1716
1717 eh = (struct elf_s390_link_hash_entry *) h;
1718 if (eh->dyn_relocs == NULL)
b34976b6 1719 return TRUE;
0451c93c
MS
1720
1721 /* In the shared -Bsymbolic case, discard space allocated for
1722 dynamic pc-relative relocs against symbols which turn out to be
1723 defined in regular objects. For the normal shared case, discard
1724 space for pc-relative relocs that have become local due to symbol
1725 visibility changes. */
1726
1727 if (info->shared)
1728 {
8c21ca21 1729 if (SYMBOL_CALLS_LOCAL (info, h))
0451c93c 1730 {
00d8c7a9 1731 struct elf_dyn_relocs **pp;
0451c93c
MS
1732
1733 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1734 {
1735 p->count -= p->pc_count;
1736 p->pc_count = 0;
1737 if (p->count == 0)
1738 *pp = p->next;
1739 else
1740 pp = &p->next;
1741 }
1742 }
82058a73
MS
1743
1744 /* Also discard relocs on undefined weak syms with non-default
1745 visibility. */
22d606e9 1746 if (eh->dyn_relocs != NULL
82058a73 1747 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
1748 {
1749 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1750 eh->dyn_relocs = NULL;
1751
1752 /* Make sure undefined weak symbols are output as a dynamic
1753 symbol in PIEs. */
1754 else if (h->dynindx == -1
1755 && !h->forced_local)
1756 {
1757 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1758 return FALSE;
1759 }
1760 }
0451c93c 1761 }
64285810 1762 else if (ELIMINATE_COPY_RELOCS)
0451c93c
MS
1763 {
1764 /* For the non-shared case, discard space for relocs against
1765 symbols which turn out to need copy relocs or are not
1766 dynamic. */
1767
f5385ebf
AM
1768 if (!h->non_got_ref
1769 && ((h->def_dynamic
1770 && !h->def_regular)
0451c93c
MS
1771 || (htab->elf.dynamic_sections_created
1772 && (h->root.type == bfd_link_hash_undefweak
1773 || h->root.type == bfd_link_hash_undefined))))
1774 {
1775 /* Make sure this symbol is output as a dynamic symbol.
1776 Undefined weak syms won't yet be marked as dynamic. */
1777 if (h->dynindx == -1
f5385ebf 1778 && !h->forced_local)
0451c93c 1779 {
c152c796 1780 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1781 return FALSE;
0451c93c
MS
1782 }
1783
1784 /* If that succeeded, we know we'll be keeping all the
1785 relocs. */
1786 if (h->dynindx != -1)
1787 goto keep;
1788 }
1789
1790 eh->dyn_relocs = NULL;
1791
ec338859 1792 keep: ;
0451c93c
MS
1793 }
1794
1795 /* Finally, allocate space. */
1796 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1797 {
1798 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 1799 sreloc->size += p->count * sizeof (Elf64_External_Rela);
0451c93c
MS
1800 }
1801
b34976b6 1802 return TRUE;
0451c93c
MS
1803}
1804
1805/* Find any dynamic relocs that apply to read-only sections. */
1806
b34976b6 1807static bfd_boolean
2c3fc389 1808readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
0451c93c
MS
1809{
1810 struct elf_s390_link_hash_entry *eh;
00d8c7a9 1811 struct elf_dyn_relocs *p;
0451c93c
MS
1812
1813 eh = (struct elf_s390_link_hash_entry *) h;
1814 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1815 {
1816 asection *s = p->sec->output_section;
1817
1818 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1819 {
1820 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1821
1822 info->flags |= DF_TEXTREL;
1823
1824 /* Not an error, just cut short the traversal. */
b34976b6 1825 return FALSE;
0451c93c
MS
1826 }
1827 }
b34976b6 1828 return TRUE;
0451c93c
MS
1829}
1830
a85d7ed0
NC
1831/* Set the sizes of the dynamic sections. */
1832
b34976b6 1833static bfd_boolean
4dfe6ac6
NC
1834elf_s390_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1835 struct bfd_link_info *info)
a85d7ed0 1836{
0451c93c 1837 struct elf_s390_link_hash_table *htab;
a85d7ed0
NC
1838 bfd *dynobj;
1839 asection *s;
b34976b6 1840 bfd_boolean relocs;
0451c93c 1841 bfd *ibfd;
a85d7ed0 1842
0451c93c 1843 htab = elf_s390_hash_table (info);
4dfe6ac6
NC
1844 if (htab == NULL)
1845 return FALSE;
1846
0451c93c
MS
1847 dynobj = htab->elf.dynobj;
1848 if (dynobj == NULL)
1849 abort ();
a85d7ed0 1850
0451c93c 1851 if (htab->elf.dynamic_sections_created)
a85d7ed0
NC
1852 {
1853 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 1854 if (info->executable)
a85d7ed0 1855 {
3d4d4302 1856 s = bfd_get_linker_section (dynobj, ".interp");
0451c93c
MS
1857 if (s == NULL)
1858 abort ();
eea6121a 1859 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
a85d7ed0
NC
1860 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1861 }
1862 }
a85d7ed0 1863
0451c93c
MS
1864 /* Set up .got offsets for local syms, and space for local dynamic
1865 relocs. */
1866 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
a85d7ed0 1867 {
0451c93c
MS
1868 bfd_signed_vma *local_got;
1869 bfd_signed_vma *end_local_got;
69fc87f1 1870 char *local_tls_type;
0451c93c
MS
1871 bfd_size_type locsymcount;
1872 Elf_Internal_Shdr *symtab_hdr;
1873 asection *srela;
a85d7ed0 1874
0ffa91dd 1875 if (! is_s390_elf (ibfd))
a85d7ed0
NC
1876 continue;
1877
0451c93c 1878 for (s = ibfd->sections; s != NULL; s = s->next)
a85d7ed0 1879 {
00d8c7a9 1880 struct elf_dyn_relocs *p;
0451c93c 1881
6edfbbad 1882 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
a85d7ed0 1883 {
ec338859
AM
1884 if (!bfd_is_abs_section (p->sec)
1885 && bfd_is_abs_section (p->sec->output_section))
1886 {
1887 /* Input section has been discarded, either because
1888 it is a copy of a linkonce section or due to
1889 linker script /DISCARD/, so we'll be discarding
1890 the relocs too. */
1891 }
248866a8 1892 else if (p->count != 0)
ec338859
AM
1893 {
1894 srela = elf_section_data (p->sec)->sreloc;
eea6121a 1895 srela->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
1896 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1897 info->flags |= DF_TEXTREL;
ec338859 1898 }
a85d7ed0
NC
1899 }
1900 }
0451c93c
MS
1901
1902 local_got = elf_local_got_refcounts (ibfd);
1903 if (!local_got)
1904 continue;
1905
0ffa91dd 1906 symtab_hdr = &elf_symtab_hdr (ibfd);
0451c93c
MS
1907 locsymcount = symtab_hdr->sh_info;
1908 end_local_got = local_got + locsymcount;
69fc87f1 1909 local_tls_type = elf_s390_local_got_tls_type (ibfd);
f41345a7
AK
1910 s = htab->elf.sgot;
1911 srela = htab->elf.srelgot;
69fc87f1 1912 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
a85d7ed0 1913 {
0451c93c 1914 if (*local_got > 0)
a85d7ed0 1915 {
eea6121a
AM
1916 *local_got = s->size;
1917 s->size += GOT_ENTRY_SIZE;
69fc87f1 1918 if (*local_tls_type == GOT_TLS_GD)
eea6121a 1919 s->size += GOT_ENTRY_SIZE;
0451c93c 1920 if (info->shared)
eea6121a 1921 srela->size += sizeof (Elf64_External_Rela);
a85d7ed0
NC
1922 }
1923 else
0451c93c 1924 *local_got = (bfd_vma) -1;
a85d7ed0 1925 }
0451c93c
MS
1926 }
1927
69fc87f1
MS
1928 if (htab->tls_ldm_got.refcount > 0)
1929 {
1930 /* Allocate 2 got entries and 1 dynamic reloc for R_390_TLS_LDM64
1931 relocs. */
f41345a7
AK
1932 htab->tls_ldm_got.offset = htab->elf.sgot->size;
1933 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
1934 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
69fc87f1
MS
1935 }
1936 else
1937 htab->tls_ldm_got.offset = -1;
1938
0451c93c
MS
1939 /* Allocate global sym .plt and .got entries, and space for global
1940 sym dynamic relocs. */
2c3fc389 1941 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
0451c93c
MS
1942
1943 /* We now have determined the sizes of the various dynamic sections.
1944 Allocate memory for them. */
b34976b6 1945 relocs = FALSE;
0451c93c
MS
1946 for (s = dynobj->sections; s != NULL; s = s->next)
1947 {
1948 if ((s->flags & SEC_LINKER_CREATED) == 0)
1949 continue;
1950
f41345a7
AK
1951 if (s == htab->elf.splt
1952 || s == htab->elf.sgot
1953 || s == htab->elf.sgotplt
c456f082 1954 || s == htab->sdynbss)
0451c93c
MS
1955 {
1956 /* Strip this section if we don't need it; see the
1957 comment below. */
1958 }
0112cd26 1959 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
0451c93c 1960 {
f41345a7 1961 if (s->size != 0 && s != htab->elf.srelplt)
b34976b6 1962 relocs = TRUE;
ec338859 1963
0451c93c
MS
1964 /* We use the reloc_count field as a counter if we need
1965 to copy relocs into the output file. */
1966 s->reloc_count = 0;
1967 }
1968 else
a85d7ed0
NC
1969 {
1970 /* It's not one of our sections, so don't allocate space. */
1971 continue;
1972 }
1973
eea6121a 1974 if (s->size == 0)
a85d7ed0 1975 {
0451c93c
MS
1976 /* If we don't need this section, strip it from the
1977 output file. This is to handle .rela.bss and
1978 .rela.plt. We must create it in
1979 create_dynamic_sections, because it must be created
1980 before the linker maps input sections to output
1981 sections. The linker does that before
1982 adjust_dynamic_symbol is called, and it is that
1983 function which decides whether anything needs to go
1984 into these sections. */
1985
8423293d 1986 s->flags |= SEC_EXCLUDE;
a85d7ed0
NC
1987 continue;
1988 }
1989
c456f082
AM
1990 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1991 continue;
1992
0451c93c
MS
1993 /* Allocate memory for the section contents. We use bfd_zalloc
1994 here in case unused entries are not reclaimed before the
1995 section's contents are written out. This should not happen,
1996 but this way if it does, we get a R_390_NONE reloc instead
1997 of garbage. */
eea6121a 1998 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
0451c93c 1999 if (s->contents == NULL)
b34976b6 2000 return FALSE;
a85d7ed0
NC
2001 }
2002
0451c93c 2003 if (htab->elf.dynamic_sections_created)
a85d7ed0
NC
2004 {
2005 /* Add some entries to the .dynamic section. We fill in the
2006 values later, in elf_s390_finish_dynamic_sections, but we
2007 must add the entries now so that we get the correct size for
2008 the .dynamic section. The DT_DEBUG entry is filled in by the
2009 dynamic linker and used by the debugger. */
dc810e39 2010#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2011 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 2012
36af4a4e 2013 if (info->executable)
a85d7ed0 2014 {
dc810e39 2015 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 2016 return FALSE;
a85d7ed0
NC
2017 }
2018
f41345a7 2019 if (htab->elf.splt->size != 0)
a85d7ed0 2020 {
dc810e39
AM
2021 if (!add_dynamic_entry (DT_PLTGOT, 0)
2022 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2023 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2024 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 2025 return FALSE;
a85d7ed0
NC
2026 }
2027
2028 if (relocs)
947216bf
AM
2029 {
2030 if (!add_dynamic_entry (DT_RELA, 0)
2031 || !add_dynamic_entry (DT_RELASZ, 0)
2032 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 2033 return FALSE;
ec338859 2034
0451c93c
MS
2035 /* If any dynamic relocs apply to a read-only section,
2036 then we need a DT_TEXTREL entry. */
248866a8
AM
2037 if ((info->flags & DF_TEXTREL) == 0)
2038 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2c3fc389 2039 info);
ec338859 2040
0451c93c
MS
2041 if ((info->flags & DF_TEXTREL) != 0)
2042 {
2043 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 2044 return FALSE;
0451c93c 2045 }
a85d7ed0
NC
2046 }
2047 }
dc810e39 2048#undef add_dynamic_entry
a85d7ed0 2049
b34976b6 2050 return TRUE;
a85d7ed0
NC
2051}
2052
69fc87f1
MS
2053/* Return the base VMA address which should be subtracted from real addresses
2054 when resolving @dtpoff relocation.
2055 This is PT_TLS segment p_vaddr. */
2056
2057static bfd_vma
2c3fc389 2058dtpoff_base (struct bfd_link_info *info)
69fc87f1 2059{
e1918d23
AM
2060 /* If tls_sec is NULL, we should have signalled an error already. */
2061 if (elf_hash_table (info)->tls_sec == NULL)
69fc87f1 2062 return 0;
e1918d23 2063 return elf_hash_table (info)->tls_sec->vma;
69fc87f1
MS
2064}
2065
2066/* Return the relocation value for @tpoff relocation
2067 if STT_TLS virtual address is ADDRESS. */
2068
2069static bfd_vma
2c3fc389 2070tpoff (struct bfd_link_info *info, bfd_vma address)
69fc87f1 2071{
e1918d23 2072 struct elf_link_hash_table *htab = elf_hash_table (info);
69fc87f1 2073
e1918d23
AM
2074 /* If tls_sec is NULL, we should have signalled an error already. */
2075 if (htab->tls_sec == NULL)
69fc87f1 2076 return 0;
e1918d23 2077 return htab->tls_size + htab->tls_sec->vma - address;
69fc87f1
MS
2078}
2079
2080/* Complain if TLS instruction relocation is against an invalid
2081 instruction. */
2082
2083static void
2c3fc389
NC
2084invalid_tls_insn (bfd *input_bfd,
2085 asection *input_section,
2086 Elf_Internal_Rela *rel)
69fc87f1
MS
2087{
2088 reloc_howto_type *howto;
2089
2090 howto = elf_howto_table + ELF64_R_TYPE (rel->r_info);
2091 (*_bfd_error_handler)
d003868e
AM
2092 (_("%B(%A+0x%lx): invalid instruction for TLS relocation %s"),
2093 input_bfd,
2094 input_section,
69fc87f1
MS
2095 (long) rel->r_offset,
2096 howto->name);
77df2968 2097 bfd_set_error (bfd_error_bad_value);
69fc87f1
MS
2098}
2099
a85d7ed0
NC
2100/* Relocate a 390 ELF section. */
2101
b34976b6 2102static bfd_boolean
4dfe6ac6
NC
2103elf_s390_relocate_section (bfd *output_bfd,
2104 struct bfd_link_info *info,
2105 bfd *input_bfd,
2106 asection *input_section,
2107 bfd_byte *contents,
2108 Elf_Internal_Rela *relocs,
2109 Elf_Internal_Sym *local_syms,
2110 asection **local_sections)
a85d7ed0 2111{
0451c93c 2112 struct elf_s390_link_hash_table *htab;
a85d7ed0
NC
2113 Elf_Internal_Shdr *symtab_hdr;
2114 struct elf_link_hash_entry **sym_hashes;
2115 bfd_vma *local_got_offsets;
a85d7ed0
NC
2116 Elf_Internal_Rela *rel;
2117 Elf_Internal_Rela *relend;
2118
0ffa91dd
NC
2119 BFD_ASSERT (is_s390_elf (input_bfd));
2120
0451c93c 2121 htab = elf_s390_hash_table (info);
4dfe6ac6
NC
2122 if (htab == NULL)
2123 return FALSE;
2124
0ffa91dd 2125 symtab_hdr = &elf_symtab_hdr (input_bfd);
a85d7ed0
NC
2126 sym_hashes = elf_sym_hashes (input_bfd);
2127 local_got_offsets = elf_local_got_offsets (input_bfd);
2128
a85d7ed0
NC
2129 rel = relocs;
2130 relend = relocs + input_section->reloc_count;
2131 for (; rel < relend; rel++)
2132 {
5236c819 2133 unsigned int r_type;
a85d7ed0
NC
2134 reloc_howto_type *howto;
2135 unsigned long r_symndx;
2136 struct elf_link_hash_entry *h;
2137 Elf_Internal_Sym *sym;
2138 asection *sec;
0451c93c 2139 bfd_vma off;
a85d7ed0 2140 bfd_vma relocation;
b34976b6 2141 bfd_boolean unresolved_reloc;
a85d7ed0 2142 bfd_reloc_status_type r;
69fc87f1 2143 int tls_type;
a85d7ed0
NC
2144
2145 r_type = ELF64_R_TYPE (rel->r_info);
0451c93c 2146 if (r_type == (int) R_390_GNU_VTINHERIT
947216bf
AM
2147 || r_type == (int) R_390_GNU_VTENTRY)
2148 continue;
5236c819 2149 if (r_type >= (int) R_390_max)
a85d7ed0
NC
2150 {
2151 bfd_set_error (bfd_error_bad_value);
b34976b6 2152 return FALSE;
a85d7ed0 2153 }
a85d7ed0 2154
b491616a 2155 howto = elf_howto_table + r_type;
a85d7ed0 2156 r_symndx = ELF64_R_SYM (rel->r_info);
5236c819 2157
a85d7ed0
NC
2158 h = NULL;
2159 sym = NULL;
2160 sec = NULL;
b34976b6 2161 unresolved_reloc = FALSE;
a85d7ed0
NC
2162 if (r_symndx < symtab_hdr->sh_info)
2163 {
2164 sym = local_syms + r_symndx;
2165 sec = local_sections[r_symndx];
8517fae7 2166 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
a85d7ed0
NC
2167 }
2168 else
2169 {
560e09e9 2170 bfd_boolean warned ATTRIBUTE_UNUSED;
0451c93c 2171
b2a8e766
AM
2172 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2173 r_symndx, symtab_hdr, sym_hashes,
2174 h, sec, relocation,
2175 unresolved_reloc, warned);
a85d7ed0
NC
2176 }
2177
dbaa2011 2178 if (sec != NULL && discarded_section (sec))
e4067dbb 2179 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 2180 rel, 1, relend, howto, 0, contents);
ab96bf03
AM
2181
2182 if (info->relocatable)
2183 continue;
2184
a85d7ed0
NC
2185 switch (r_type)
2186 {
5236c819
MS
2187 case R_390_GOTPLT12:
2188 case R_390_GOTPLT16:
bd1ea41b 2189 case R_390_GOTPLT20:
5236c819
MS
2190 case R_390_GOTPLT32:
2191 case R_390_GOTPLT64:
2192 case R_390_GOTPLTENT:
2193 /* There are three cases for a GOTPLT relocation. 1) The
2194 relocation is against the jump slot entry of a plt that
2195 will get emitted to the output file. 2) The relocation
2196 is against the jump slot of a plt entry that has been
2197 removed. elf_s390_adjust_gotplt has created a GOT entry
2198 as replacement. 3) The relocation is against a local symbol.
2199 Cases 2) and 3) are the same as the GOT relocation code
2200 so we just have to test for case 1 and fall through for
2201 the other two. */
2202 if (h != NULL && h->plt.offset != (bfd_vma) -1)
2203 {
2204 bfd_vma plt_index;
2205
2206 /* Calc. index no.
2207 Current offset - size first entry / entry size. */
2208 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) /
2209 PLT_ENTRY_SIZE;
26e41594 2210
5236c819
MS
2211 /* Offset in GOT is PLT index plus GOT headers(3) times 4,
2212 addr & GOT addr. */
2213 relocation = (plt_index + 3) * GOT_ENTRY_SIZE;
2214 unresolved_reloc = FALSE;
2215
2216 if (r_type == R_390_GOTPLTENT)
f41345a7 2217 relocation += htab->elf.sgot->output_section->vma;
5236c819
MS
2218 break;
2219 }
2220 /* Fall through. */
2221
947216bf
AM
2222 case R_390_GOT12:
2223 case R_390_GOT16:
bd1ea41b 2224 case R_390_GOT20:
947216bf
AM
2225 case R_390_GOT32:
2226 case R_390_GOT64:
2227 case R_390_GOTENT:
2228 /* Relocation is to the entry for this symbol in the global
2229 offset table. */
f41345a7 2230 if (htab->elf.sgot == NULL)
0451c93c 2231 abort ();
a85d7ed0 2232
947216bf
AM
2233 if (h != NULL)
2234 {
b34976b6 2235 bfd_boolean dyn;
a85d7ed0 2236
947216bf 2237 off = h->got.offset;
0451c93c 2238 dyn = htab->elf.dynamic_sections_created;
4ec72bde 2239 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
947216bf 2240 || (info->shared
8c21ca21 2241 && SYMBOL_REFERENCES_LOCAL (info, h))
82058a73
MS
2242 || (ELF_ST_VISIBILITY (h->other)
2243 && h->root.type == bfd_link_hash_undefweak))
947216bf
AM
2244 {
2245 /* This is actually a static link, or it is a
2246 -Bsymbolic link and the symbol is defined
2247 locally, or the symbol was forced to be local
2248 because of a version file. We must initialize
2249 this entry in the global offset table. Since the
2250 offset must always be a multiple of 2, we use the
2251 least significant bit to record whether we have
2252 initialized it already.
2253
2254 When doing a dynamic link, we create a .rel.got
2255 relocation entry to initialize the value. This
2256 is done in the finish_dynamic_symbol routine. */
2257 if ((off & 1) != 0)
2258 off &= ~1;
2259 else
2260 {
a85d7ed0 2261 bfd_put_64 (output_bfd, relocation,
f41345a7 2262 htab->elf.sgot->contents + off);
947216bf
AM
2263 h->got.offset |= 1;
2264 }
2265 }
0451c93c 2266 else
b34976b6 2267 unresolved_reloc = FALSE;
947216bf
AM
2268 }
2269 else
2270 {
0451c93c
MS
2271 if (local_got_offsets == NULL)
2272 abort ();
a85d7ed0 2273
947216bf 2274 off = local_got_offsets[r_symndx];
a85d7ed0 2275
947216bf
AM
2276 /* The offset must always be a multiple of 8. We use
2277 the least significant bit to record whether we have
2278 already generated the necessary reloc. */
2279 if ((off & 1) != 0)
2280 off &= ~1;
2281 else
2282 {
2283 bfd_put_64 (output_bfd, relocation,
f41345a7 2284 htab->elf.sgot->contents + off);
a85d7ed0 2285
947216bf
AM
2286 if (info->shared)
2287 {
2288 asection *s;
2289 Elf_Internal_Rela outrel;
2290 bfd_byte *loc;
a85d7ed0 2291
f41345a7 2292 s = htab->elf.srelgot;
947216bf 2293 if (s == NULL)
0451c93c 2294 abort ();
a85d7ed0 2295
f41345a7
AK
2296 outrel.r_offset = (htab->elf.sgot->output_section->vma
2297 + htab->elf.sgot->output_offset
947216bf
AM
2298 + off);
2299 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
a85d7ed0 2300 outrel.r_addend = relocation;
947216bf
AM
2301 loc = s->contents;
2302 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2303 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2304 }
a85d7ed0 2305
947216bf
AM
2306 local_got_offsets[r_symndx] |= 1;
2307 }
2308 }
a85d7ed0 2309
0451c93c
MS
2310 if (off >= (bfd_vma) -2)
2311 abort ();
2312
f41345a7 2313 relocation = htab->elf.sgot->output_offset + off;
0451c93c 2314
ae9a127f
NC
2315 /* For @GOTENT the relocation is against the offset between
2316 the instruction and the symbols entry in the GOT and not
2317 between the start of the GOT and the symbols entry. We
2318 add the vma of the GOT to get the correct value. */
5236c819
MS
2319 if ( r_type == R_390_GOTENT
2320 || r_type == R_390_GOTPLTENT)
f41345a7 2321 relocation += htab->elf.sgot->output_section->vma;
a85d7ed0 2322
947216bf 2323 break;
99c79b2e 2324
5236c819
MS
2325 case R_390_GOTOFF16:
2326 case R_390_GOTOFF32:
2327 case R_390_GOTOFF64:
947216bf
AM
2328 /* Relocation is relative to the start of the global offset
2329 table. */
a85d7ed0 2330
947216bf
AM
2331 /* Note that sgot->output_offset is not involved in this
2332 calculation. We always want the start of .got. If we
2333 defined _GLOBAL_OFFSET_TABLE in a different way, as is
2334 permitted by the ABI, we might have to change this
2335 calculation. */
f41345a7 2336 relocation -= htab->elf.sgot->output_section->vma;
947216bf 2337 break;
a85d7ed0 2338
947216bf 2339 case R_390_GOTPC:
a85d7ed0 2340 case R_390_GOTPCDBL:
947216bf 2341 /* Use global offset table as symbol value. */
f41345a7 2342 relocation = htab->elf.sgot->output_section->vma;
b34976b6 2343 unresolved_reloc = FALSE;
947216bf 2344 break;
a85d7ed0 2345
947216bf
AM
2346 case R_390_PLT16DBL:
2347 case R_390_PLT32:
2348 case R_390_PLT32DBL:
2349 case R_390_PLT64:
2350 /* Relocation is to the entry for this symbol in the
2351 procedure linkage table. */
a85d7ed0 2352
947216bf
AM
2353 /* Resolve a PLT32 reloc against a local symbol directly,
2354 without using the procedure linkage table. */
2355 if (h == NULL)
2356 break;
a85d7ed0 2357
947216bf 2358 if (h->plt.offset == (bfd_vma) -1
f41345a7 2359 || htab->elf.splt == NULL)
947216bf
AM
2360 {
2361 /* We didn't make a PLT entry for this symbol. This
2362 happens when statically linking PIC code, or when
2363 using -Bsymbolic. */
2364 break;
2365 }
2366
f41345a7
AK
2367 relocation = (htab->elf.splt->output_section->vma
2368 + htab->elf.splt->output_offset
947216bf 2369 + h->plt.offset);
b34976b6 2370 unresolved_reloc = FALSE;
26e41594 2371 break;
5236c819
MS
2372
2373 case R_390_PLTOFF16:
2374 case R_390_PLTOFF32:
2375 case R_390_PLTOFF64:
26e41594
AM
2376 /* Relocation is to the entry for this symbol in the
2377 procedure linkage table relative to the start of the GOT. */
5236c819
MS
2378
2379 /* For local symbols or if we didn't make a PLT entry for
2380 this symbol resolve the symbol directly. */
26e41594 2381 if ( h == NULL
5236c819 2382 || h->plt.offset == (bfd_vma) -1
f41345a7 2383 || htab->elf.splt == NULL)
5236c819 2384 {
f41345a7 2385 relocation -= htab->elf.sgot->output_section->vma;
5236c819
MS
2386 break;
2387 }
2388
f41345a7
AK
2389 relocation = (htab->elf.splt->output_section->vma
2390 + htab->elf.splt->output_offset
26e41594 2391 + h->plt.offset
f41345a7 2392 - htab->elf.sgot->output_section->vma);
5236c819 2393 unresolved_reloc = FALSE;
947216bf
AM
2394 break;
2395
2396 case R_390_8:
2397 case R_390_16:
2398 case R_390_32:
2399 case R_390_64:
2400 case R_390_PC16:
2401 case R_390_PC16DBL:
2402 case R_390_PC32:
a85d7ed0 2403 case R_390_PC32DBL:
947216bf 2404 case R_390_PC64:
b1e24c02 2405 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
2406 break;
2407
947216bf 2408 if ((info->shared
82058a73
MS
2409 && (h == NULL
2410 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2411 || h->root.type != bfd_link_hash_undefweak)
0451c93c
MS
2412 && ((r_type != R_390_PC16
2413 && r_type != R_390_PC16DBL
2414 && r_type != R_390_PC32
2415 && r_type != R_390_PC32DBL
2416 && r_type != R_390_PC64)
8c21ca21 2417 || !SYMBOL_CALLS_LOCAL (info, h)))
64285810
MS
2418 || (ELIMINATE_COPY_RELOCS
2419 && !info->shared
0451c93c
MS
2420 && h != NULL
2421 && h->dynindx != -1
f5385ebf
AM
2422 && !h->non_got_ref
2423 && ((h->def_dynamic
2424 && !h->def_regular)
0451c93c
MS
2425 || h->root.type == bfd_link_hash_undefweak
2426 || h->root.type == bfd_link_hash_undefined)))
947216bf
AM
2427 {
2428 Elf_Internal_Rela outrel;
b34976b6 2429 bfd_boolean skip, relocate;
0451c93c 2430 asection *sreloc;
947216bf 2431 bfd_byte *loc;
a85d7ed0 2432
947216bf
AM
2433 /* When generating a shared object, these relocations
2434 are copied into the output file to be resolved at run
2435 time. */
b34976b6
AM
2436 skip = FALSE;
2437 relocate = FALSE;
a85d7ed0 2438
c629eae0
JJ
2439 outrel.r_offset =
2440 _bfd_elf_section_offset (output_bfd, info, input_section,
2441 rel->r_offset);
2442 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2443 skip = TRUE;
0bb2d96a 2444 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2445 skip = TRUE, relocate = TRUE;
a85d7ed0 2446
947216bf
AM
2447 outrel.r_offset += (input_section->output_section->vma
2448 + input_section->output_offset);
a85d7ed0 2449
947216bf 2450 if (skip)
0bb2d96a 2451 memset (&outrel, 0, sizeof outrel);
947216bf 2452 else if (h != NULL
0451c93c
MS
2453 && h->dynindx != -1
2454 && (r_type == R_390_PC16
2455 || r_type == R_390_PC16DBL
2456 || r_type == R_390_PC32
2457 || r_type == R_390_PC32DBL
2458 || r_type == R_390_PC64
2459 || !info->shared
8c21ca21 2460 || !SYMBOLIC_BIND (info, h)
f5385ebf 2461 || !h->def_regular))
947216bf
AM
2462 {
2463 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
27018c3f 2464 outrel.r_addend = rel->r_addend;
947216bf
AM
2465 }
2466 else
2467 {
0451c93c 2468 /* This symbol is local, or marked to become local. */
90ced0dd 2469 outrel.r_addend = relocation + rel->r_addend;
b5727d75
MS
2470 if (r_type == R_390_64)
2471 {
2472 relocate = TRUE;
2473 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
b5727d75
MS
2474 }
2475 else
2476 {
2477 long sindx;
2478
90ced0dd 2479 if (bfd_is_abs_section (sec))
b5727d75
MS
2480 sindx = 0;
2481 else if (sec == NULL || sec->owner == NULL)
2482 {
2483 bfd_set_error(bfd_error_bad_value);
2484 return FALSE;
2485 }
2486 else
2487 {
2488 asection *osec;
2489
2490 osec = sec->output_section;
2491 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
2492
2493 if (sindx == 0)
2494 {
2495 osec = htab->elf.text_index_section;
2496 sindx = elf_section_data (osec)->dynindx;
2497 }
2498 BFD_ASSERT (sindx != 0);
90ced0dd
MS
2499
2500 /* We are turning this relocation into one
2501 against a section symbol, so subtract out
2502 the output section's address but not the
2503 offset of the input section in the output
2504 section. */
90ced0dd 2505 outrel.r_addend -= osec->vma;
b5727d75
MS
2506 }
2507 outrel.r_info = ELF64_R_INFO (sindx, r_type);
b5727d75 2508 }
0451c93c 2509 }
a85d7ed0 2510
0451c93c
MS
2511 sreloc = elf_section_data (input_section)->sreloc;
2512 if (sreloc == NULL)
2513 abort ();
2514
947216bf
AM
2515 loc = sreloc->contents;
2516 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2517 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
a85d7ed0 2518
947216bf
AM
2519 /* If this reloc is against an external symbol, we do
2520 not want to fiddle with the addend. Otherwise, we
2521 need to include the symbol value so that it becomes
2522 an addend for the dynamic reloc. */
2523 if (! relocate)
2524 continue;
2525 }
a85d7ed0 2526
947216bf 2527 break;
a85d7ed0 2528
69fc87f1
MS
2529 /* Relocations for tls literal pool entries. */
2530 case R_390_TLS_IE64:
2531 if (info->shared)
2532 {
2533 Elf_Internal_Rela outrel;
2534 asection *sreloc;
2535 bfd_byte *loc;
2536
2537 outrel.r_offset = rel->r_offset
2538 + input_section->output_section->vma
2539 + input_section->output_offset;
2540 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
2541 sreloc = elf_section_data (input_section)->sreloc;
2542 if (sreloc == NULL)
2543 abort ();
2544 loc = sreloc->contents;
2545 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2546 bfd_elf64_swap_reloc_out (output_bfd, &outrel, loc);
2547 }
ae9a127f 2548 /* Fall through. */
69fc87f1
MS
2549
2550 case R_390_TLS_GD64:
2551 case R_390_TLS_GOTIE64:
2552 r_type = elf_s390_tls_transition (info, r_type, h == NULL);
2553 tls_type = GOT_UNKNOWN;
2554 if (h == NULL && local_got_offsets)
2555 tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
2556 else if (h != NULL)
2557 {
2558 tls_type = elf_s390_hash_entry(h)->tls_type;
2559 if (!info->shared && h->dynindx == -1 && tls_type >= GOT_TLS_IE)
2560 r_type = R_390_TLS_LE64;
2561 }
2562 if (r_type == R_390_TLS_GD64 && tls_type >= GOT_TLS_IE)
2563 r_type = R_390_TLS_IE64;
2564
2565 if (r_type == R_390_TLS_LE64)
2566 {
2567 /* This relocation gets optimized away by the local exec
2568 access optimization. */
2569 BFD_ASSERT (! unresolved_reloc);
2570 bfd_put_64 (output_bfd, -tpoff (info, relocation),
2571 contents + rel->r_offset);
2572 continue;
2573 }
2574
f41345a7 2575 if (htab->elf.sgot == NULL)
69fc87f1
MS
2576 abort ();
2577
2578 if (h != NULL)
2579 off = h->got.offset;
2580 else
2581 {
2582 if (local_got_offsets == NULL)
2583 abort ();
2584
2585 off = local_got_offsets[r_symndx];
2586 }
2587
2588 emit_tls_relocs:
2589
2590 if ((off & 1) != 0)
2591 off &= ~1;
26e41594 2592 else
69fc87f1
MS
2593 {
2594 Elf_Internal_Rela outrel;
2595 bfd_byte *loc;
2596 int dr_type, indx;
2597
f41345a7 2598 if (htab->elf.srelgot == NULL)
69fc87f1
MS
2599 abort ();
2600
f41345a7
AK
2601 outrel.r_offset = (htab->elf.sgot->output_section->vma
2602 + htab->elf.sgot->output_offset + off);
69fc87f1
MS
2603
2604 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2605 if (r_type == R_390_TLS_GD64)
2606 dr_type = R_390_TLS_DTPMOD;
2607 else
2608 dr_type = R_390_TLS_TPOFF;
2609 if (dr_type == R_390_TLS_TPOFF && indx == 0)
2610 outrel.r_addend = relocation - dtpoff_base (info);
2611 else
2612 outrel.r_addend = 0;
2613 outrel.r_info = ELF64_R_INFO (indx, dr_type);
f41345a7
AK
2614 loc = htab->elf.srelgot->contents;
2615 loc += htab->elf.srelgot->reloc_count++
69fc87f1
MS
2616 * sizeof (Elf64_External_Rela);
2617 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2618
2619 if (r_type == R_390_TLS_GD64)
2620 {
2621 if (indx == 0)
2622 {
2623 BFD_ASSERT (! unresolved_reloc);
2624 bfd_put_64 (output_bfd,
2625 relocation - dtpoff_base (info),
f41345a7 2626 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
69fc87f1
MS
2627 }
2628 else
2629 {
2630 outrel.r_info = ELF64_R_INFO (indx, R_390_TLS_DTPOFF);
2631 outrel.r_offset += GOT_ENTRY_SIZE;
2632 outrel.r_addend = 0;
f41345a7 2633 htab->elf.srelgot->reloc_count++;
69fc87f1
MS
2634 loc += sizeof (Elf64_External_Rela);
2635 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2636 }
2637 }
2638
2639 if (h != NULL)
2640 h->got.offset |= 1;
2641 else
2642 local_got_offsets[r_symndx] |= 1;
2643 }
2644
2645 if (off >= (bfd_vma) -2)
2646 abort ();
2647 if (r_type == ELF64_R_TYPE (rel->r_info))
2648 {
f41345a7 2649 relocation = htab->elf.sgot->output_offset + off;
69fc87f1 2650 if (r_type == R_390_TLS_IE64 || r_type == R_390_TLS_IEENT)
f41345a7 2651 relocation += htab->elf.sgot->output_section->vma;
69fc87f1
MS
2652 unresolved_reloc = FALSE;
2653 }
2654 else
2655 {
f41345a7 2656 bfd_put_64 (output_bfd, htab->elf.sgot->output_offset + off,
69fc87f1
MS
2657 contents + rel->r_offset);
2658 continue;
2659 }
2660 break;
2661
2662 case R_390_TLS_GOTIE12:
bd1ea41b 2663 case R_390_TLS_GOTIE20:
69fc87f1
MS
2664 case R_390_TLS_IEENT:
2665 if (h == NULL)
2666 {
2667 if (local_got_offsets == NULL)
2668 abort();
2669 off = local_got_offsets[r_symndx];
2670 if (info->shared)
2671 goto emit_tls_relocs;
2672 }
2673 else
2674 {
2675 off = h->got.offset;
2676 tls_type = elf_s390_hash_entry(h)->tls_type;
2677 if (info->shared || h->dynindx != -1 || tls_type < GOT_TLS_IE)
2678 goto emit_tls_relocs;
2679 }
2680
f41345a7 2681 if (htab->elf.sgot == NULL)
69fc87f1
MS
2682 abort ();
2683
2684 BFD_ASSERT (! unresolved_reloc);
2685 bfd_put_64 (output_bfd, -tpoff (info, relocation),
f41345a7
AK
2686 htab->elf.sgot->contents + off);
2687 relocation = htab->elf.sgot->output_offset + off;
69fc87f1 2688 if (r_type == R_390_TLS_IEENT)
f41345a7 2689 relocation += htab->elf.sgot->output_section->vma;
69fc87f1
MS
2690 unresolved_reloc = FALSE;
2691 break;
2692
2693 case R_390_TLS_LDM64:
2694 if (! info->shared)
2695 /* The literal pool entry this relocation refers to gets ignored
2696 by the optimized code of the local exec model. Do nothing
2697 and the value will turn out zero. */
2698 continue;
2699
f41345a7 2700 if (htab->elf.sgot == NULL)
69fc87f1
MS
2701 abort ();
2702
2703 off = htab->tls_ldm_got.offset;
2704 if (off & 1)
2705 off &= ~1;
2706 else
2707 {
2708 Elf_Internal_Rela outrel;
2709 bfd_byte *loc;
2710
f41345a7 2711 if (htab->elf.srelgot == NULL)
69fc87f1
MS
2712 abort ();
2713
f41345a7
AK
2714 outrel.r_offset = (htab->elf.sgot->output_section->vma
2715 + htab->elf.sgot->output_offset + off);
69fc87f1
MS
2716
2717 bfd_put_64 (output_bfd, 0,
f41345a7 2718 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
69fc87f1
MS
2719 outrel.r_info = ELF64_R_INFO (0, R_390_TLS_DTPMOD);
2720 outrel.r_addend = 0;
f41345a7
AK
2721 loc = htab->elf.srelgot->contents;
2722 loc += htab->elf.srelgot->reloc_count++
69fc87f1
MS
2723 * sizeof (Elf64_External_Rela);
2724 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2725 htab->tls_ldm_got.offset |= 1;
2726 }
f41345a7 2727 relocation = htab->elf.sgot->output_offset + off;
26e41594 2728 unresolved_reloc = FALSE;
69fc87f1
MS
2729 break;
2730
2731 case R_390_TLS_LE64:
2732 if (info->shared)
2733 {
2734 /* Linking a shared library with non-fpic code requires
2735 a R_390_TLS_TPOFF relocation. */
2736 Elf_Internal_Rela outrel;
2737 asection *sreloc;
2738 bfd_byte *loc;
2739 int indx;
2740
2741 outrel.r_offset = rel->r_offset
2742 + input_section->output_section->vma
2743 + input_section->output_offset;
2744 if (h != NULL && h->dynindx != -1)
2745 indx = h->dynindx;
2746 else
2747 indx = 0;
2748 outrel.r_info = ELF64_R_INFO (indx, R_390_TLS_TPOFF);
2749 if (indx == 0)
2750 outrel.r_addend = relocation - dtpoff_base (info);
2751 else
2752 outrel.r_addend = 0;
2753 sreloc = elf_section_data (input_section)->sreloc;
2754 if (sreloc == NULL)
2755 abort ();
2756 loc = sreloc->contents;
2757 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2758 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2759 }
2760 else
2761 {
2762 BFD_ASSERT (! unresolved_reloc);
2763 bfd_put_64 (output_bfd, -tpoff (info, relocation),
2764 contents + rel->r_offset);
2765 }
2766 continue;
2767
2768 case R_390_TLS_LDO64:
de8bee76 2769 if (info->shared || (input_section->flags & SEC_DEBUGGING))
69fc87f1
MS
2770 relocation -= dtpoff_base (info);
2771 else
2772 /* When converting LDO to LE, we must negate. */
2773 relocation = -tpoff (info, relocation);
2774 break;
2775
2776 /* Relocations for tls instructions. */
2777 case R_390_TLS_LOAD:
2778 case R_390_TLS_GDCALL:
2779 case R_390_TLS_LDCALL:
2780 tls_type = GOT_UNKNOWN;
2781 if (h == NULL && local_got_offsets)
2782 tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
2783 else if (h != NULL)
2784 tls_type = elf_s390_hash_entry(h)->tls_type;
2785
2786 if (tls_type == GOT_TLS_GD)
2787 continue;
2788
2789 if (r_type == R_390_TLS_LOAD)
2790 {
2791 if (!info->shared && (h == NULL || h->dynindx == -1))
2792 {
2793 /* IE->LE transition. Four valid cases:
2794 lg %rx,(0,%ry) -> sllg %rx,%ry,0
2795 lg %rx,(%ry,0) -> sllg %rx,%ry,0
2796 lg %rx,(%ry,%r12) -> sllg %rx,%ry,0
2797 lg %rx,(%r12,%ry) -> sllg %rx,%ry,0 */
2798 unsigned int insn0, insn1, ry;
2799
2800 insn0 = bfd_get_32 (input_bfd, contents + rel->r_offset);
2801 insn1 = bfd_get_16 (input_bfd, contents + rel->r_offset + 4);
2802 if (insn1 != 0x0004)
2803 invalid_tls_insn (input_bfd, input_section, rel);
2804 ry = 0;
2805 if ((insn0 & 0xff00f000) == 0xe3000000)
2806 /* lg %rx,0(%ry,0) -> sllg %rx,%ry,0 */
2807 ry = (insn0 & 0x000f0000);
2808 else if ((insn0 & 0xff0f0000) == 0xe3000000)
2809 /* lg %rx,0(0,%ry) -> sllg %rx,%ry,0 */
2810 ry = (insn0 & 0x0000f000) << 4;
2811 else if ((insn0 & 0xff00f000) == 0xe300c000)
2812 /* lg %rx,0(%ry,%r12) -> sllg %rx,%ry,0 */
2813 ry = (insn0 & 0x000f0000);
2814 else if ((insn0 & 0xff0f0000) == 0xe30c0000)
2815 /* lg %rx,0(%r12,%ry) -> sllg %rx,%ry,0 */
2816 ry = (insn0 & 0x0000f000) << 4;
2817 else
2818 invalid_tls_insn (input_bfd, input_section, rel);
2819 insn0 = 0xeb000000 | (insn0 & 0x00f00000) | ry;
2820 insn1 = 0x000d;
2821 bfd_put_32 (output_bfd, insn0, contents + rel->r_offset);
2822 bfd_put_16 (output_bfd, insn1, contents + rel->r_offset + 4);
2823 }
2824 }
2825 else if (r_type == R_390_TLS_GDCALL)
2826 {
2827 unsigned int insn0, insn1;
2828
2829 insn0 = bfd_get_32 (input_bfd, contents + rel->r_offset);
2830 insn1 = bfd_get_16 (input_bfd, contents + rel->r_offset + 4);
2831 if ((insn0 & 0xffff0000) != 0xc0e50000)
2832 invalid_tls_insn (input_bfd, input_section, rel);
2833 if (!info->shared && (h == NULL || h->dynindx == -1))
2834 {
2835 /* GD->LE transition.
2836 brasl %r14,__tls_get_addr@plt -> brcl 0,. */
2837 insn0 = 0xc0040000;
2838 insn1 = 0x0000;
2839 }
2840 else
2841 {
2842 /* GD->IE transition.
2843 brasl %r14,__tls_get_addr@plt -> lg %r2,0(%r2,%r12) */
2844 insn0 = 0xe322c000;
2845 insn1 = 0x0004;
2846 }
2847 bfd_put_32 (output_bfd, insn0, contents + rel->r_offset);
2848 bfd_put_16 (output_bfd, insn1, contents + rel->r_offset + 4);
2849 }
2850 else if (r_type == R_390_TLS_LDCALL)
2851 {
2852 if (!info->shared)
2853 {
2854 unsigned int insn0, insn1;
2855
2856 insn0 = bfd_get_32 (input_bfd, contents + rel->r_offset);
2857 insn1 = bfd_get_16 (input_bfd, contents + rel->r_offset + 4);
2858 if ((insn0 & 0xffff0000) != 0xc0e50000)
2859 invalid_tls_insn (input_bfd, input_section, rel);
2860 /* LD->LE transition.
2861 brasl %r14,__tls_get_addr@plt -> brcl 0,. */
2862 insn0 = 0xc0040000;
2863 insn1 = 0x0000;
2864 bfd_put_32 (output_bfd, insn0, contents + rel->r_offset);
2865 bfd_put_16 (output_bfd, insn1, contents + rel->r_offset + 4);
2866 }
2867 }
2868 continue;
2869
947216bf
AM
2870 default:
2871 break;
2872 }
a85d7ed0 2873
239e1f3a
AM
2874 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2875 because such sections are not SEC_ALLOC and thus ld.so will
2876 not process them. */
0451c93c 2877 if (unresolved_reloc
239e1f3a 2878 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
2879 && h->def_dynamic)
2880 && _bfd_elf_section_offset (output_bfd, info, input_section,
2881 rel->r_offset) != (bfd_vma) -1)
0451c93c 2882 (*_bfd_error_handler)
843fe662 2883 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
2884 input_bfd,
2885 input_section,
0451c93c 2886 (long) rel->r_offset,
843fe662 2887 howto->name,
0451c93c
MS
2888 h->root.root.string);
2889
bd1ea41b
MS
2890 if (r_type == R_390_20
2891 || r_type == R_390_GOT20
2892 || r_type == R_390_GOTPLT20
2893 || r_type == R_390_TLS_GOTIE20)
2894 {
2895 relocation += rel->r_addend;
2896 relocation = (relocation&0xfff) << 8 | (relocation&0xff000) >> 12;
2897 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2898 contents, rel->r_offset,
2899 relocation, 0);
2900 }
2901 else
2902 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
a85d7ed0
NC
2903 contents, rel->r_offset,
2904 relocation, rel->r_addend);
2905
2906 if (r != bfd_reloc_ok)
2907 {
0451c93c 2908 const char *name;
ec338859 2909
0451c93c
MS
2910 if (h != NULL)
2911 name = h->root.root.string;
2912 else
a85d7ed0 2913 {
0451c93c
MS
2914 name = bfd_elf_string_from_elf_section (input_bfd,
2915 symtab_hdr->sh_link,
2916 sym->st_name);
2917 if (name == NULL)
b34976b6 2918 return FALSE;
0451c93c
MS
2919 if (*name == '\0')
2920 name = bfd_section_name (input_bfd, sec);
2921 }
ec338859 2922
0451c93c
MS
2923 if (r == bfd_reloc_overflow)
2924 {
ec338859 2925
0451c93c 2926 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
2927 (info, (h ? &h->root : NULL), name, howto->name,
2928 (bfd_vma) 0, input_bfd, input_section,
2929 rel->r_offset)))
b34976b6 2930 return FALSE;
0451c93c
MS
2931 }
2932 else
2933 {
2934 (*_bfd_error_handler)
d003868e
AM
2935 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2936 input_bfd, input_section,
0451c93c 2937 (long) rel->r_offset, name, (int) r);
b34976b6 2938 return FALSE;
a85d7ed0
NC
2939 }
2940 }
2941 }
2942
b34976b6 2943 return TRUE;
a85d7ed0
NC
2944}
2945
2946/* Finish up dynamic symbol handling. We set the contents of various
2947 dynamic sections here. */
2948
b34976b6 2949static bfd_boolean
4dfe6ac6
NC
2950elf_s390_finish_dynamic_symbol (bfd *output_bfd,
2951 struct bfd_link_info *info,
2952 struct elf_link_hash_entry *h,
2953 Elf_Internal_Sym *sym)
a85d7ed0 2954{
0451c93c 2955 struct elf_s390_link_hash_table *htab;
a85d7ed0 2956
0451c93c 2957 htab = elf_s390_hash_table (info);
4dfe6ac6
NC
2958 if (htab == NULL)
2959 return FALSE;
a85d7ed0
NC
2960
2961 if (h->plt.offset != (bfd_vma) -1)
2962 {
a85d7ed0 2963 bfd_vma plt_index;
0451c93c
MS
2964 bfd_vma got_offset;
2965 Elf_Internal_Rela rela;
947216bf 2966 bfd_byte *loc;
a85d7ed0
NC
2967
2968 /* This symbol has an entry in the procedure linkage table. Set
947216bf 2969 it up. */
a85d7ed0 2970
0451c93c 2971 if (h->dynindx == -1
f41345a7
AK
2972 || htab->elf.splt == NULL
2973 || htab->elf.sgotplt == NULL
2974 || htab->elf.srelplt == NULL)
0451c93c 2975 abort ();
a85d7ed0 2976
99c79b2e 2977 /* Calc. index no.
947216bf 2978 Current offset - size first entry / entry size. */
a85d7ed0
NC
2979 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
2980
2981 /* Offset in GOT is PLT index plus GOT headers(3) times 8,
947216bf 2982 addr & GOT addr. */
a85d7ed0
NC
2983 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2984
2985 /* Fill in the blueprint of a PLT. */
f41345a7 2986 memcpy (htab->elf.splt->contents + h->plt.offset, elf_s390x_plt_entry,
9aa17453
AK
2987 PLT_ENTRY_SIZE);
2988
a85d7ed0
NC
2989 /* Fixup the relative address to the GOT entry */
2990 bfd_put_32 (output_bfd,
f41345a7
AK
2991 (htab->elf.sgotplt->output_section->vma +
2992 htab->elf.sgotplt->output_offset + got_offset
2993 - (htab->elf.splt->output_section->vma + h->plt.offset))/2,
2994 htab->elf.splt->contents + h->plt.offset + 2);
a85d7ed0
NC
2995 /* Fixup the relative branch to PLT 0 */
2996 bfd_put_32 (output_bfd, - (PLT_FIRST_ENTRY_SIZE +
2997 (PLT_ENTRY_SIZE * plt_index) + 22)/2,
f41345a7 2998 htab->elf.splt->contents + h->plt.offset + 24);
a7e28bbe 2999 /* Fixup offset into .rela.plt section. */
a85d7ed0 3000 bfd_put_32 (output_bfd, plt_index * sizeof (Elf64_External_Rela),
f41345a7 3001 htab->elf.splt->contents + h->plt.offset + 28);
a85d7ed0
NC
3002
3003 /* Fill in the entry in the global offset table.
947216bf 3004 Points to instruction after GOT offset. */
a85d7ed0 3005 bfd_put_64 (output_bfd,
f41345a7
AK
3006 (htab->elf.splt->output_section->vma
3007 + htab->elf.splt->output_offset
a85d7ed0
NC
3008 + h->plt.offset
3009 + 14),
f41345a7 3010 htab->elf.sgotplt->contents + got_offset);
a85d7ed0 3011
0451c93c 3012 /* Fill in the entry in the .rela.plt section. */
f41345a7
AK
3013 rela.r_offset = (htab->elf.sgotplt->output_section->vma
3014 + htab->elf.sgotplt->output_offset
0451c93c
MS
3015 + got_offset);
3016 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT);
3017 rela.r_addend = 0;
f41345a7 3018 loc = htab->elf.srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
0451c93c 3019 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a85d7ed0 3020
f5385ebf 3021 if (!h->def_regular)
a85d7ed0
NC
3022 {
3023 /* Mark the symbol as undefined, rather than as defined in
0451c93c
MS
3024 the .plt section. Leave the value alone. This is a clue
3025 for the dynamic linker, to make function pointer
3026 comparisons work between an application and shared
3027 library. */
a85d7ed0
NC
3028 sym->st_shndx = SHN_UNDEF;
3029 }
3030 }
3031
69fc87f1
MS
3032 if (h->got.offset != (bfd_vma) -1
3033 && elf_s390_hash_entry(h)->tls_type != GOT_TLS_GD
3034 && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE
3035 && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE_NLT)
a85d7ed0 3036 {
a85d7ed0 3037 Elf_Internal_Rela rela;
947216bf 3038 bfd_byte *loc;
a85d7ed0
NC
3039
3040 /* This symbol has an entry in the global offset table. Set it
947216bf 3041 up. */
f41345a7 3042 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
0451c93c 3043 abort ();
a85d7ed0 3044
f41345a7
AK
3045 rela.r_offset = (htab->elf.sgot->output_section->vma
3046 + htab->elf.sgot->output_offset
dc810e39 3047 + (h->got.offset &~ (bfd_vma) 1));
a85d7ed0
NC
3048
3049 /* If this is a static link, or it is a -Bsymbolic link and the
3050 symbol is defined locally or was forced to be local because
3051 of a version file, we just want to emit a RELATIVE reloc.
3052 The entry in the global offset table will already have been
3053 initialized in the relocate_section function. */
0451c93c 3054 if (info->shared
8c21ca21 3055 && SYMBOL_REFERENCES_LOCAL (info, h))
947216bf 3056 {
8c21ca21
AK
3057 if (!h->def_regular)
3058 return FALSE;
0451c93c 3059 BFD_ASSERT((h->got.offset & 1) != 0);
947216bf
AM
3060 rela.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
3061 rela.r_addend = (h->root.u.def.value
3062 + h->root.u.def.section->output_section->vma
3063 + h->root.u.def.section->output_offset);
3064 }
a85d7ed0
NC
3065 else
3066 {
3067 BFD_ASSERT((h->got.offset & 1) == 0);
f41345a7 3068 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgot->contents + h->got.offset);
a85d7ed0 3069 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_GLOB_DAT);
947216bf
AM
3070 rela.r_addend = 0;
3071 }
a85d7ed0 3072
f41345a7
AK
3073 loc = htab->elf.srelgot->contents;
3074 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
0451c93c 3075 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a85d7ed0
NC
3076 }
3077
f5385ebf 3078 if (h->needs_copy)
a85d7ed0 3079 {
a85d7ed0 3080 Elf_Internal_Rela rela;
947216bf 3081 bfd_byte *loc;
a85d7ed0
NC
3082
3083 /* This symbols needs a copy reloc. Set it up. */
3084
0451c93c
MS
3085 if (h->dynindx == -1
3086 || (h->root.type != bfd_link_hash_defined
3087 && h->root.type != bfd_link_hash_defweak)
3088 || htab->srelbss == NULL)
3089 abort ();
a85d7ed0
NC
3090
3091 rela.r_offset = (h->root.u.def.value
3092 + h->root.u.def.section->output_section->vma
3093 + h->root.u.def.section->output_offset);
3094 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_COPY);
3095 rela.r_addend = 0;
947216bf
AM
3096 loc = htab->srelbss->contents;
3097 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
0451c93c 3098 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a85d7ed0
NC
3099 }
3100
3101 /* Mark some specially defined symbols as absolute. */
3102 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1
RS
3103 || h == htab->elf.hgot
3104 || h == htab->elf.hplt)
a85d7ed0
NC
3105 sym->st_shndx = SHN_ABS;
3106
b34976b6 3107 return TRUE;
a85d7ed0
NC
3108}
3109
0451c93c
MS
3110/* Used to decide how to sort relocs in an optimal manner for the
3111 dynamic linker, before writing them out. */
3112
3113static enum elf_reloc_type_class
2c3fc389 3114elf_s390_reloc_type_class (const Elf_Internal_Rela *rela)
0451c93c
MS
3115{
3116 switch ((int) ELF64_R_TYPE (rela->r_info))
3117 {
3118 case R_390_RELATIVE:
3119 return reloc_class_relative;
3120 case R_390_JMP_SLOT:
3121 return reloc_class_plt;
3122 case R_390_COPY:
3123 return reloc_class_copy;
3124 default:
3125 return reloc_class_normal;
3126 }
3127}
3128
a85d7ed0
NC
3129/* Finish up the dynamic sections. */
3130
b34976b6 3131static bfd_boolean
4dfe6ac6
NC
3132elf_s390_finish_dynamic_sections (bfd *output_bfd,
3133 struct bfd_link_info *info)
a85d7ed0 3134{
0451c93c 3135 struct elf_s390_link_hash_table *htab;
a85d7ed0
NC
3136 bfd *dynobj;
3137 asection *sdyn;
a85d7ed0 3138
0451c93c 3139 htab = elf_s390_hash_table (info);
4dfe6ac6
NC
3140 if (htab == NULL)
3141 return FALSE;
3142
0451c93c 3143 dynobj = htab->elf.dynobj;
3d4d4302 3144 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
a85d7ed0 3145
0451c93c 3146 if (htab->elf.dynamic_sections_created)
a85d7ed0 3147 {
a85d7ed0
NC
3148 Elf64_External_Dyn *dyncon, *dynconend;
3149
f41345a7 3150 if (sdyn == NULL || htab->elf.sgot == NULL)
0451c93c 3151 abort ();
a85d7ed0
NC
3152
3153 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 3154 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
a85d7ed0
NC
3155 for (; dyncon < dynconend; dyncon++)
3156 {
3157 Elf_Internal_Dyn dyn;
a85d7ed0 3158 asection *s;
ec338859 3159
a85d7ed0 3160 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
ec338859 3161
a85d7ed0
NC
3162 switch (dyn.d_tag)
3163 {
3164 default:
0451c93c 3165 continue;
ec338859 3166
a85d7ed0 3167 case DT_PLTGOT:
f41345a7 3168 dyn.d_un.d_ptr = htab->elf.sgot->output_section->vma;
0451c93c 3169 break;
ec338859 3170
a85d7ed0 3171 case DT_JMPREL:
f41345a7 3172 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
a85d7ed0 3173 break;
ec338859 3174
a85d7ed0 3175 case DT_PLTRELSZ:
f41345a7 3176 s = htab->elf.srelplt->output_section;
eea6121a 3177 dyn.d_un.d_val = s->size;
a85d7ed0 3178 break;
ec338859 3179
a85d7ed0
NC
3180 case DT_RELASZ:
3181 /* The procedure linkage table relocs (DT_JMPREL) should
3182 not be included in the overall relocs (DT_RELA).
3183 Therefore, we override the DT_RELASZ entry here to
3184 make it not include the JMPREL relocs. Since the
3185 linker script arranges for .rela.plt to follow all
3186 other relocation sections, we don't have to worry
3187 about changing the DT_RELA entry. */
f41345a7 3188 s = htab->elf.srelplt->output_section;
eea6121a 3189 dyn.d_un.d_val -= s->size;
a85d7ed0
NC
3190 break;
3191 }
0451c93c
MS
3192
3193 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
a85d7ed0
NC
3194 }
3195
3196 /* Fill in the special first entry in the procedure linkage table. */
f41345a7 3197 if (htab->elf.splt && htab->elf.splt->size > 0)
a85d7ed0
NC
3198 {
3199 /* fill in blueprint for plt 0 entry */
f41345a7 3200 memcpy (htab->elf.splt->contents, elf_s390x_first_plt_entry,
9aa17453 3201 PLT_FIRST_ENTRY_SIZE);
a85d7ed0
NC
3202 /* Fixup relative address to start of GOT */
3203 bfd_put_32 (output_bfd,
f41345a7
AK
3204 (htab->elf.sgotplt->output_section->vma +
3205 htab->elf.sgotplt->output_offset
3206 - htab->elf.splt->output_section->vma - 6)/2,
3207 htab->elf.splt->contents + 8);
a85d7ed0 3208 }
f41345a7 3209 elf_section_data (htab->elf.splt->output_section)
0451c93c 3210 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
a85d7ed0
NC
3211 }
3212
f41345a7 3213 if (htab->elf.sgotplt)
a85d7ed0 3214 {
0451c93c 3215 /* Fill in the first three entries in the global offset table. */
f41345a7 3216 if (htab->elf.sgotplt->size > 0)
0451c93c
MS
3217 {
3218 bfd_put_64 (output_bfd,
3219 (sdyn == NULL ? (bfd_vma) 0
3220 : sdyn->output_section->vma + sdyn->output_offset),
f41345a7 3221 htab->elf.sgotplt->contents);
0451c93c 3222 /* One entry for shared object struct ptr. */
f41345a7 3223 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + 8);
0451c93c 3224 /* One entry for _dl_runtime_resolve. */
f41345a7 3225 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + 12);
0451c93c 3226 }
a85d7ed0 3227
f41345a7 3228 elf_section_data (htab->elf.sgot->output_section)
0451c93c
MS
3229 ->this_hdr.sh_entsize = 8;
3230 }
b34976b6 3231 return TRUE;
a85d7ed0
NC
3232}
3233
4c45e5c9
JJ
3234/* Return address for Ith PLT stub in section PLT, for relocation REL
3235 or (bfd_vma) -1 if it should not be included. */
3236
3237static bfd_vma
3238elf_s390_plt_sym_val (bfd_vma i, const asection *plt,
3239 const arelent *rel ATTRIBUTE_UNUSED)
3240{
3241 return plt->vma + PLT_FIRST_ENTRY_SIZE + i * PLT_ENTRY_SIZE;
3242}
3243
3244
ae9a127f
NC
3245/* Why was the hash table entry size definition changed from
3246 ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and
3247 this is the only reason for the s390_elf64_size_info structure. */
a85d7ed0
NC
3248
3249const struct elf_size_info s390_elf64_size_info =
3250{
3251 sizeof (Elf64_External_Ehdr),
3252 sizeof (Elf64_External_Phdr),
3253 sizeof (Elf64_External_Shdr),
3254 sizeof (Elf64_External_Rel),
3255 sizeof (Elf64_External_Rela),
3256 sizeof (Elf64_External_Sym),
3257 sizeof (Elf64_External_Dyn),
3258 sizeof (Elf_External_Note),
ae9a127f
NC
3259 8, /* hash-table entry size. */
3260 1, /* internal relocations per external relocations. */
3261 64, /* arch_size. */
45d6a902 3262 3, /* log_file_align. */
a85d7ed0
NC
3263 ELFCLASS64, EV_CURRENT,
3264 bfd_elf64_write_out_phdrs,
3265 bfd_elf64_write_shdrs_and_ehdr,
1489a3a0 3266 bfd_elf64_checksum_contents,
a85d7ed0 3267 bfd_elf64_write_relocs,
73ff0d56 3268 bfd_elf64_swap_symbol_in,
a85d7ed0
NC
3269 bfd_elf64_swap_symbol_out,
3270 bfd_elf64_slurp_reloc_table,
3271 bfd_elf64_slurp_symbol_table,
3272 bfd_elf64_swap_dyn_in,
3273 bfd_elf64_swap_dyn_out,
947216bf
AM
3274 bfd_elf64_swap_reloc_in,
3275 bfd_elf64_swap_reloc_out,
3276 bfd_elf64_swap_reloca_in,
3277 bfd_elf64_swap_reloca_out
a85d7ed0
NC
3278};
3279
3280#define TARGET_BIG_SYM bfd_elf64_s390_vec
3281#define TARGET_BIG_NAME "elf64-s390"
3282#define ELF_ARCH bfd_arch_s390
ae95ffa6 3283#define ELF_TARGET_ID S390_ELF_DATA
a85d7ed0
NC
3284#define ELF_MACHINE_CODE EM_S390
3285#define ELF_MACHINE_ALT1 EM_S390_OLD
3286#define ELF_MAXPAGESIZE 0x1000
3287
3288#define elf_backend_size_info s390_elf64_size_info
3289
3290#define elf_backend_can_gc_sections 1
51b64d56 3291#define elf_backend_can_refcount 1
a85d7ed0
NC
3292#define elf_backend_want_got_plt 1
3293#define elf_backend_plt_readonly 1
3294#define elf_backend_want_plt_sym 0
3295#define elf_backend_got_header_size 24
b491616a 3296#define elf_backend_rela_normal 1
a85d7ed0
NC
3297
3298#define elf_info_to_howto elf_s390_info_to_howto
3299
a85d7ed0
NC
3300#define bfd_elf64_bfd_is_local_label_name elf_s390_is_local_label_name
3301#define bfd_elf64_bfd_link_hash_table_create elf_s390_link_hash_table_create
3302#define bfd_elf64_bfd_reloc_type_lookup elf_s390_reloc_type_lookup
157090f7 3303#define bfd_elf64_bfd_reloc_name_lookup elf_s390_reloc_name_lookup
a85d7ed0
NC
3304
3305#define elf_backend_adjust_dynamic_symbol elf_s390_adjust_dynamic_symbol
3306#define elf_backend_check_relocs elf_s390_check_relocs
0451c93c
MS
3307#define elf_backend_copy_indirect_symbol elf_s390_copy_indirect_symbol
3308#define elf_backend_create_dynamic_sections elf_s390_create_dynamic_sections
a85d7ed0
NC
3309#define elf_backend_finish_dynamic_sections elf_s390_finish_dynamic_sections
3310#define elf_backend_finish_dynamic_symbol elf_s390_finish_dynamic_symbol
3311#define elf_backend_gc_mark_hook elf_s390_gc_mark_hook
3312#define elf_backend_gc_sweep_hook elf_s390_gc_sweep_hook
0451c93c 3313#define elf_backend_reloc_type_class elf_s390_reloc_type_class
a85d7ed0
NC
3314#define elf_backend_relocate_section elf_s390_relocate_section
3315#define elf_backend_size_dynamic_sections elf_s390_size_dynamic_sections
74541ad4 3316#define elf_backend_init_index_section _bfd_elf_init_1_index_section
29c2fb7c 3317#define elf_backend_reloc_type_class elf_s390_reloc_type_class
4c45e5c9 3318#define elf_backend_plt_sym_val elf_s390_plt_sym_val
a85d7ed0 3319
69fc87f1
MS
3320#define bfd_elf64_mkobject elf_s390_mkobject
3321#define elf_backend_object_p elf_s390_object_p
a85d7ed0 3322
2c3fc389
NC
3323/* Enable ELF64 archive functions. */
3324#define bfd_elf64_archive_functions
3325extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *);
3326extern bfd_boolean bfd_elf64_archive_write_armap (bfd *, unsigned int, struct orl *, unsigned int, int);
3327
3328#define bfd_elf64_archive_slurp_extended_name_table _bfd_archive_coff_slurp_extended_name_table
3329#define bfd_elf64_archive_construct_extended_name_table _bfd_archive_coff_construct_extended_name_table
3330#define bfd_elf64_archive_truncate_arname _bfd_archive_coff_truncate_arname
3331#define bfd_elf64_archive_read_ar_hdr _bfd_archive_coff_read_ar_hdr
3332#define bfd_elf64_archive_write_ar_hdr _bfd_archive_coff_write_ar_hdr
3333#define bfd_elf64_archive_openr_next_archived_file _bfd_archive_coff_openr_next_archived_file
3334#define bfd_elf64_archive_get_elt_at_index _bfd_archive_coff_get_elt_at_index
3335#define bfd_elf64_archive_generic_stat_arch_elt _bfd_archive_coff_generic_stat_arch_elt
3336#define bfd_elf64_archive_update_armap_timestamp _bfd_archive_coff_update_armap_timestamp
3337
a85d7ed0 3338#include "elf64-target.h"