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