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