OSDN Git Service

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