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