OSDN Git Service

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