OSDN Git Service

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