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