OSDN Git Service

Fix the behaviour of --allow-shlib-undefined, so that it does what it claims
[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   unsigned long r_symndx;
1331   int r_type;
1332   struct elf_link_hash_entry *h;
1333
1334   elf_section_data (sec)->local_dynrel = NULL;
1335
1336   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1337   sym_hashes = elf_sym_hashes (abfd);
1338   local_got_refcounts = elf_local_got_refcounts (abfd);
1339
1340   relend = relocs + sec->reloc_count;
1341   for (rel = relocs; rel < relend; rel++)
1342     {
1343       r_symndx = ELF32_R_SYM (rel->r_info);
1344
1345       if (r_symndx < symtab_hdr->sh_info)
1346         h = NULL;
1347       else
1348         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1349
1350       r_type = elf_s390_tls_transition (info,
1351                                         ELF32_R_TYPE (rel->r_info),
1352                                         r_symndx >= symtab_hdr->sh_info);
1353       switch (r_type)
1354         {
1355         case R_390_TLS_LDM32:
1356           if (elf_s390_hash_table (info)->tls_ldm_got.refcount > 0)
1357             elf_s390_hash_table (info)->tls_ldm_got.refcount -= 1;
1358           break;
1359
1360         case R_390_TLS_GD32:
1361         case R_390_TLS_IE32:
1362         case R_390_TLS_GOTIE12:
1363         case R_390_TLS_GOTIE32:
1364         case R_390_TLS_IEENT:
1365         case R_390_GOT12:
1366         case R_390_GOT16:
1367         case R_390_GOT32:
1368         case R_390_GOTOFF16:
1369         case R_390_GOTOFF32:
1370         case R_390_GOTPC:
1371         case R_390_GOTPCDBL:
1372         case R_390_GOTENT:
1373           if (h != NULL)
1374             {
1375               if (h->got.refcount > 0)
1376                 h->got.refcount -= 1;
1377             }
1378           else if (local_got_refcounts != NULL)
1379             {
1380               if (local_got_refcounts[r_symndx] > 0)
1381                 local_got_refcounts[r_symndx] -= 1;
1382             }
1383           if (r_type != R_390_TLS_IE32)
1384             break;
1385           /* Fall through.  */
1386           
1387         case R_390_TLS_LE32:
1388           if (!info->shared)
1389             break;
1390           /* Fall through.  */
1391
1392         case R_390_8:
1393         case R_390_12:
1394         case R_390_16:
1395         case R_390_32:
1396         case R_390_PC16:
1397         case R_390_PC16DBL:
1398         case R_390_PC32DBL:
1399         case R_390_PC32:
1400           if (h != NULL)
1401             {
1402               struct elf_s390_link_hash_entry *eh;
1403               struct elf_s390_dyn_relocs **pp;
1404               struct elf_s390_dyn_relocs *p;
1405               
1406               if (!info->shared && h->plt.refcount > 0)
1407                 h->plt.refcount -= 1;
1408               
1409               eh = (struct elf_s390_link_hash_entry *) h;
1410               
1411               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1412                 if (p->sec == sec)
1413                   {
1414                     if (ELF32_R_TYPE (rel->r_info) == R_390_PC16
1415                         || ELF32_R_TYPE (rel->r_info) == R_390_PC16DBL
1416                         || ELF32_R_TYPE (rel->r_info) == R_390_PC32DBL
1417                         || ELF32_R_TYPE (rel->r_info) == R_390_PC32)
1418                       p->pc_count -= 1;
1419                     p->count -= 1;
1420                     if (p->count == 0)
1421                       *pp = p->next;
1422                     break;
1423                   }
1424             }
1425           break;
1426           
1427         case R_390_PLT16DBL:
1428         case R_390_PLT32DBL:
1429         case R_390_PLT32:
1430         case R_390_PLTOFF16:
1431         case R_390_PLTOFF32:
1432           if (h != NULL)
1433             {
1434               if (h->plt.refcount > 0)
1435                 h->plt.refcount -= 1;
1436             }
1437           break;
1438
1439         case R_390_GOTPLT12:
1440         case R_390_GOTPLT16:
1441         case R_390_GOTPLT32:
1442         case R_390_GOTPLTENT:
1443           if (h != NULL)
1444             {
1445               if (h->plt.refcount > 0)
1446                 {
1447                   ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount--;
1448                   h->plt.refcount -= 1;
1449                 }
1450             }
1451           else if (local_got_refcounts != NULL)
1452             {
1453               if (local_got_refcounts[r_symndx] > 0)
1454                 local_got_refcounts[r_symndx] -= 1;
1455             }
1456           break;
1457
1458         default:
1459           break;
1460         }
1461     }
1462
1463   return TRUE;
1464 }
1465
1466 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1467    entry but we found we will not create any.  Called when we find we will
1468    not have any PLT for this symbol, by for example
1469    elf_s390_adjust_dynamic_symbol when we're doing a proper dynamic link,
1470    or elf_s390_size_dynamic_sections if no dynamic sections will be
1471    created (we're only linking static objects).  */
1472
1473 static void
1474 elf_s390_adjust_gotplt (h)
1475      struct elf_s390_link_hash_entry *h;
1476 {
1477   if (h->elf.root.type == bfd_link_hash_warning)
1478     h = (struct elf_s390_link_hash_entry *) h->elf.root.u.i.link;
1479
1480   if (h->gotplt_refcount <= 0)
1481     return;
1482
1483   /* We simply add the number of gotplt references to the number
1484    * of got references for this symbol.  */
1485   h->elf.got.refcount += h->gotplt_refcount;
1486   h->gotplt_refcount = -1;
1487 }
1488
1489 /* Adjust a symbol defined by a dynamic object and referenced by a
1490    regular object.  The current definition is in some section of the
1491    dynamic object, but we're not including those sections.  We have to
1492    change the definition to something the rest of the link can
1493    understand.  */
1494
1495 static bfd_boolean
1496 elf_s390_adjust_dynamic_symbol (info, h)
1497      struct bfd_link_info *info;
1498      struct elf_link_hash_entry *h;
1499 {
1500   struct elf_s390_link_hash_table *htab;
1501   struct elf_s390_link_hash_entry * eh;
1502   struct elf_s390_dyn_relocs *p;
1503   asection *s;
1504   unsigned int power_of_two;
1505
1506   /* If this is a function, put it in the procedure linkage table.  We
1507      will fill in the contents of the procedure linkage table later
1508      (although we could actually do it here).  */
1509   if (h->type == STT_FUNC
1510       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1511     {
1512       if (h->plt.refcount <= 0
1513           || (! info->shared
1514               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1515               && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1516               && h->root.type != bfd_link_hash_undefweak
1517               && h->root.type != bfd_link_hash_undefined))
1518         {
1519           /* This case can occur if we saw a PLT32 reloc in an input
1520              file, but the symbol was never referred to by a dynamic
1521              object, or if all references were garbage collected.  In
1522              such a case, we don't actually need to build a procedure
1523              linkage table, and we can just do a PC32 reloc instead.  */
1524           h->plt.offset = (bfd_vma) -1;
1525           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1526           elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1527         }
1528
1529       return TRUE;
1530     }
1531   else
1532     /* It's possible that we incorrectly decided a .plt reloc was
1533        needed for an R_390_PC32 reloc to a non-function sym in
1534        check_relocs.  We can't decide accurately between function and
1535        non-function syms in check-relocs;  Objects loaded later in
1536        the link may change h->type.  So fix it now.  */
1537     h->plt.offset = (bfd_vma) -1;
1538
1539   /* If this is a weak symbol, and there is a real definition, the
1540      processor independent code will have arranged for us to see the
1541      real definition first, and we can just use the same value.  */
1542   if (h->weakdef != NULL)
1543     {
1544       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1545                   || h->weakdef->root.type == bfd_link_hash_defweak);
1546       h->root.u.def.section = h->weakdef->root.u.def.section;
1547       h->root.u.def.value = h->weakdef->root.u.def.value;
1548       return TRUE;
1549     }
1550
1551   /* This is a reference to a symbol defined by a dynamic object which
1552      is not a function.  */
1553
1554   /* If we are creating a shared library, we must presume that the
1555      only references to the symbol are via the global offset table.
1556      For such cases we need not do anything here; the relocations will
1557      be handled correctly by relocate_section.  */
1558   if (info->shared)
1559     return TRUE;
1560
1561   /* If there are no references to this symbol that do not use the
1562      GOT, we don't need to generate a copy reloc.  */
1563   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1564     return TRUE;
1565
1566   /* If -z nocopyreloc was given, we won't generate them either.  */
1567   if (info->nocopyreloc)
1568     {
1569       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1570       return TRUE;
1571     }
1572
1573   eh = (struct elf_s390_link_hash_entry *) h;
1574   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1575     {
1576       s = p->sec->output_section;
1577       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1578         break;
1579     }
1580
1581   /* If we didn't find any dynamic relocs in read-only sections, then
1582      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1583   if (p == NULL)
1584     {
1585       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1586       return TRUE;
1587     }
1588
1589   /* We must allocate the symbol in our .dynbss section, which will
1590      become part of the .bss section of the executable.  There will be
1591      an entry for this symbol in the .dynsym section.  The dynamic
1592      object will contain position independent code, so all references
1593      from the dynamic object to this symbol will go through the global
1594      offset table.  The dynamic linker will use the .dynsym entry to
1595      determine the address it must put in the global offset table, so
1596      both the dynamic object and the regular object will refer to the
1597      same memory location for the variable.  */
1598
1599   htab = elf_s390_hash_table (info);
1600
1601   /* We must generate a R_390_COPY reloc to tell the dynamic linker to
1602      copy the initial value out of the dynamic object and into the
1603      runtime process image.  */
1604   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1605     {
1606       htab->srelbss->_raw_size += sizeof (Elf32_External_Rela);
1607       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1608     }
1609
1610   /* We need to figure out the alignment required for this symbol.  I
1611      have no idea how ELF linkers handle this.  */
1612   power_of_two = bfd_log2 (h->size);
1613   if (power_of_two > 3)
1614     power_of_two = 3;
1615
1616   /* Apply the required alignment.  */
1617   s = htab->sdynbss;
1618   s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1619   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1620     {
1621       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1622         return FALSE;
1623     }
1624
1625   /* Define the symbol as being at this point in the section.  */
1626   h->root.u.def.section = s;
1627   h->root.u.def.value = s->_raw_size;
1628
1629   /* Increment the section size to make room for the symbol.  */
1630   s->_raw_size += h->size;
1631
1632   return TRUE;
1633 }
1634
1635 /* This is the condition under which elf_s390_finish_dynamic_symbol
1636    will be called from elflink.h.  If elflink.h doesn't call our
1637    finish_dynamic_symbol routine, we'll need to do something about
1638    initializing any .plt and .got entries in elf_s390_relocate_section.  */
1639 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1640   ((DYN)                                                                \
1641    && ((INFO)->shared                                                   \
1642        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)      \
1643    && ((H)->dynindx != -1                                               \
1644        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1645
1646 /* Allocate space in .plt, .got and associated reloc sections for
1647    dynamic relocs.  */
1648
1649 static bfd_boolean
1650 allocate_dynrelocs (h, inf)
1651      struct elf_link_hash_entry *h;
1652      PTR inf;
1653 {
1654   struct bfd_link_info *info;
1655   struct elf_s390_link_hash_table *htab;
1656   struct elf_s390_link_hash_entry *eh;
1657   struct elf_s390_dyn_relocs *p;
1658
1659   if (h->root.type == bfd_link_hash_indirect)
1660     return TRUE;
1661
1662   if (h->root.type == bfd_link_hash_warning)
1663     /* When warning symbols are created, they **replace** the "real"
1664        entry in the hash table, thus we never get to see the real
1665        symbol in a hash traversal.  So look at it now.  */
1666     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1667
1668   info = (struct bfd_link_info *) inf;
1669   htab = elf_s390_hash_table (info);
1670
1671   if (htab->elf.dynamic_sections_created
1672       && h->plt.refcount > 0)
1673     {
1674       /* Make sure this symbol is output as a dynamic symbol.
1675          Undefined weak syms won't yet be marked as dynamic.  */
1676       if (h->dynindx == -1
1677           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1678         {
1679           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1680             return FALSE;
1681         }
1682
1683       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1684         {
1685           asection *s = htab->splt;
1686
1687           /* If this is the first .plt entry, make room for the special
1688              first entry.  */
1689           if (s->_raw_size == 0)
1690             s->_raw_size += PLT_FIRST_ENTRY_SIZE;
1691
1692           h->plt.offset = s->_raw_size;
1693
1694           /* If this symbol is not defined in a regular file, and we are
1695              not generating a shared library, then set the symbol to this
1696              location in the .plt.  This is required to make function
1697              pointers compare as equal between the normal executable and
1698              the shared library.  */
1699           if (! info->shared
1700               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1701             {
1702               h->root.u.def.section = s;
1703               h->root.u.def.value = h->plt.offset;
1704             }
1705
1706           /* Make room for this entry.  */
1707           s->_raw_size += PLT_ENTRY_SIZE;
1708
1709           /* We also need to make an entry in the .got.plt section, which
1710              will be placed in the .got section by the linker script.  */
1711           htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1712
1713           /* We also need to make an entry in the .rela.plt section.  */
1714           htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
1715         }
1716       else
1717         {
1718           h->plt.offset = (bfd_vma) -1;
1719           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1720           elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1721         }
1722     }
1723   else
1724     {
1725       h->plt.offset = (bfd_vma) -1;
1726       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1727       elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1728     }
1729
1730   /* If R_390_TLS_{IE32,GOTIE32,GOTIE12,IEENT} symbol is now local to
1731      the binary, we can optimize a bit. IE32 and GOTIE32 get converted
1732      to R_390_TLS_LE32 requiring no TLS entry. For GOTIE12 and IEENT
1733      we can save the dynamic TLS relocation.  */
1734   if (h->got.refcount > 0
1735       && !info->shared
1736       && h->dynindx == -1
1737       && elf_s390_hash_entry(h)->tls_type >= GOT_TLS_IE)
1738     {
1739       if (elf_s390_hash_entry(h)->tls_type == GOT_TLS_IE_NLT)
1740         /* For the GOTIE access without a literal pool entry the offset has
1741            to be stored somewhere. The immediate value in the instruction
1742            is not bit enough so the value is stored in the got.  */
1743         {
1744           h->got.offset = htab->sgot->_raw_size;
1745           htab->sgot->_raw_size += GOT_ENTRY_SIZE;
1746         }
1747       else
1748         h->got.offset = (bfd_vma) -1;
1749     }
1750   else if (h->got.refcount > 0)
1751    {
1752       asection *s;
1753       bfd_boolean dyn;
1754       int tls_type = elf_s390_hash_entry(h)->tls_type;
1755
1756       /* Make sure this symbol is output as a dynamic symbol.
1757          Undefined weak syms won't yet be marked as dynamic.  */
1758       if (h->dynindx == -1
1759           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1760         {
1761           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1762             return FALSE;
1763         }
1764
1765       s = htab->sgot;
1766       h->got.offset = s->_raw_size;
1767       s->_raw_size += GOT_ENTRY_SIZE;
1768       /* R_390_TLS_GD32 needs 2 consecutive GOT slots.  */
1769       if (tls_type == GOT_TLS_GD)
1770         s->_raw_size += GOT_ENTRY_SIZE;
1771       dyn = htab->elf.dynamic_sections_created;
1772       /* R_390_TLS_IE32 needs one dynamic relocation,
1773          R_390_TLS_GD32 needs one if local symbol and two if global.  */
1774       if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1775           || tls_type >= GOT_TLS_IE)
1776         htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1777       else if (tls_type == GOT_TLS_GD)
1778         htab->srelgot->_raw_size += 2 * sizeof (Elf32_External_Rela);
1779       else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1780         htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1781     }
1782   else
1783     h->got.offset = (bfd_vma) -1;
1784
1785   eh = (struct elf_s390_link_hash_entry *) h;
1786   if (eh->dyn_relocs == NULL)
1787     return TRUE;
1788
1789   /* In the shared -Bsymbolic case, discard space allocated for
1790      dynamic pc-relative relocs against symbols which turn out to be
1791      defined in regular objects.  For the normal shared case, discard
1792      space for pc-relative relocs that have become local due to symbol
1793      visibility changes.  */
1794
1795   if (info->shared)
1796     {
1797       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1798           && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1799               || info->symbolic))
1800         {
1801           struct elf_s390_dyn_relocs **pp;
1802
1803           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1804             {
1805               p->count -= p->pc_count;
1806               p->pc_count = 0;
1807               if (p->count == 0)
1808                 *pp = p->next;
1809               else
1810                 pp = &p->next;
1811             }
1812         }
1813     }
1814   else
1815     {
1816       /* For the non-shared case, discard space for relocs against
1817          symbols which turn out to need copy relocs or are not
1818          dynamic.  */
1819
1820       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1821           && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1822                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1823               || (htab->elf.dynamic_sections_created
1824                   && (h->root.type == bfd_link_hash_undefweak
1825                       || h->root.type == bfd_link_hash_undefined))))
1826         {
1827           /* Make sure this symbol is output as a dynamic symbol.
1828              Undefined weak syms won't yet be marked as dynamic.  */
1829           if (h->dynindx == -1
1830               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1831             {
1832               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1833                 return FALSE;
1834             }
1835
1836           /* If that succeeded, we know we'll be keeping all the
1837              relocs.  */
1838           if (h->dynindx != -1)
1839             goto keep;
1840         }
1841
1842       eh->dyn_relocs = NULL;
1843
1844     keep: ;
1845     }
1846
1847   /* Finally, allocate space.  */
1848   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1849     {
1850       asection *sreloc = elf_section_data (p->sec)->sreloc;
1851
1852       sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
1853     }
1854
1855   return TRUE;
1856 }
1857
1858 /* Find any dynamic relocs that apply to read-only sections.  */
1859
1860 static bfd_boolean
1861 readonly_dynrelocs (h, inf)
1862      struct elf_link_hash_entry *h;
1863      PTR inf;
1864 {
1865   struct elf_s390_link_hash_entry *eh;
1866   struct elf_s390_dyn_relocs *p;
1867
1868   if (h->root.type == bfd_link_hash_warning)
1869     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1870
1871   eh = (struct elf_s390_link_hash_entry *) h;
1872   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1873     {
1874       asection *s = p->sec->output_section;
1875
1876       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1877         {
1878           struct bfd_link_info *info = (struct bfd_link_info *) inf;
1879
1880           info->flags |= DF_TEXTREL;
1881
1882           /* Not an error, just cut short the traversal.  */
1883           return FALSE;
1884         }
1885     }
1886   return TRUE;
1887 }
1888
1889 /* Set the sizes of the dynamic sections.  */
1890
1891 static bfd_boolean
1892 elf_s390_size_dynamic_sections (output_bfd, info)
1893      bfd *output_bfd ATTRIBUTE_UNUSED;
1894      struct bfd_link_info *info;
1895 {
1896   struct elf_s390_link_hash_table *htab;
1897   bfd *dynobj;
1898   asection *s;
1899   bfd_boolean relocs;
1900   bfd *ibfd;
1901
1902   htab = elf_s390_hash_table (info);
1903   dynobj = htab->elf.dynobj;
1904   if (dynobj == NULL)
1905     abort ();
1906
1907   if (htab->elf.dynamic_sections_created)
1908     {
1909       /* Set the contents of the .interp section to the interpreter.  */
1910       if (! info->shared)
1911         {
1912           s = bfd_get_section_by_name (dynobj, ".interp");
1913           if (s == NULL)
1914             abort ();
1915           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1916           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1917         }
1918     }
1919
1920   /* Set up .got offsets for local syms, and space for local dynamic
1921      relocs.  */
1922   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1923     {
1924       bfd_signed_vma *local_got;
1925       bfd_signed_vma *end_local_got;
1926       char *local_tls_type;
1927       bfd_size_type locsymcount;
1928       Elf_Internal_Shdr *symtab_hdr;
1929       asection *srela;
1930
1931       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1932         continue;
1933
1934       for (s = ibfd->sections; s != NULL; s = s->next)
1935         {
1936           struct elf_s390_dyn_relocs *p;
1937
1938           for (p = *((struct elf_s390_dyn_relocs **)
1939                      &elf_section_data (s)->local_dynrel);
1940                p != NULL;
1941                p = p->next)
1942             {
1943               if (!bfd_is_abs_section (p->sec)
1944                   && bfd_is_abs_section (p->sec->output_section))
1945                 {
1946                   /* Input section has been discarded, either because
1947                      it is a copy of a linkonce section or due to
1948                      linker script /DISCARD/, so we'll be discarding
1949                      the relocs too.  */
1950                 }
1951               else if (p->count != 0)
1952                 {
1953                   srela = elf_section_data (p->sec)->sreloc;
1954                   srela->_raw_size += p->count * sizeof (Elf32_External_Rela);
1955                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1956                     info->flags |= DF_TEXTREL;
1957                 }
1958             }
1959         }
1960
1961       local_got = elf_local_got_refcounts (ibfd);
1962       if (!local_got)
1963         continue;
1964
1965       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1966       locsymcount = symtab_hdr->sh_info;
1967       end_local_got = local_got + locsymcount;
1968       local_tls_type = elf_s390_local_got_tls_type (ibfd);
1969       s = htab->sgot;
1970       srela = htab->srelgot;
1971       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1972         {
1973           if (*local_got > 0)
1974             {
1975               *local_got = s->_raw_size;
1976               s->_raw_size += GOT_ENTRY_SIZE;
1977               if (*local_tls_type == GOT_TLS_GD)
1978                 s->_raw_size += GOT_ENTRY_SIZE;
1979               if (info->shared)
1980                 srela->_raw_size += sizeof (Elf32_External_Rela);
1981             }
1982           else
1983             *local_got = (bfd_vma) -1;
1984         }
1985     }
1986
1987   if (htab->tls_ldm_got.refcount > 0)
1988     {
1989       /* Allocate 2 got entries and 1 dynamic reloc for R_390_TLS_LDM32
1990          relocs.  */
1991       htab->tls_ldm_got.offset = htab->sgot->_raw_size;
1992       htab->sgot->_raw_size += 2 * GOT_ENTRY_SIZE;
1993       htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1994     }
1995   else
1996     htab->tls_ldm_got.offset = -1;
1997
1998   /* Allocate global sym .plt and .got entries, and space for global
1999      sym dynamic relocs.  */
2000   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
2001
2002   /* We now have determined the sizes of the various dynamic sections.
2003      Allocate memory for them.  */
2004   relocs = FALSE;
2005   for (s = dynobj->sections; s != NULL; s = s->next)
2006     {
2007       if ((s->flags & SEC_LINKER_CREATED) == 0)
2008         continue;
2009
2010       if (s == htab->splt
2011           || s == htab->sgot
2012           || s == htab->sgotplt)
2013         {
2014           /* Strip this section if we don't need it; see the
2015              comment below.  */
2016         }
2017       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2018         {
2019           if (s->_raw_size != 0)
2020             relocs = TRUE;
2021
2022           /* We use the reloc_count field as a counter if we need
2023              to copy relocs into the output file.  */
2024           s->reloc_count = 0;
2025         }
2026       else
2027         {
2028           /* It's not one of our sections, so don't allocate space.  */
2029           continue;
2030         }
2031
2032       if (s->_raw_size == 0)
2033         {
2034           /* If we don't need this section, strip it from the
2035              output file.  This is to handle .rela.bss and
2036              .rela.plt.  We must create it in
2037              create_dynamic_sections, because it must be created
2038              before the linker maps input sections to output
2039              sections.  The linker does that before
2040              adjust_dynamic_symbol is called, and it is that
2041              function which decides whether anything needs to go
2042              into these sections.  */
2043
2044           _bfd_strip_section_from_output (info, s);
2045           continue;
2046         }
2047
2048       /* Allocate memory for the section contents.  We use bfd_zalloc
2049          here in case unused entries are not reclaimed before the
2050          section's contents are written out.  This should not happen,
2051          but this way if it does, we get a R_390_NONE reloc instead
2052          of garbage.  */
2053       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2054       if (s->contents == NULL)
2055         return FALSE;
2056     }
2057
2058   if (htab->elf.dynamic_sections_created)
2059     {
2060       /* Add some entries to the .dynamic section.  We fill in the
2061          values later, in elf_s390_finish_dynamic_sections, but we
2062          must add the entries now so that we get the correct size for
2063          the .dynamic section.  The DT_DEBUG entry is filled in by the
2064          dynamic linker and used by the debugger.  */
2065 #define add_dynamic_entry(TAG, VAL) \
2066   bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2067
2068       if (! info->shared)
2069         {
2070           if (!add_dynamic_entry (DT_DEBUG, 0))
2071             return FALSE;
2072         }
2073
2074       if (htab->splt->_raw_size != 0)
2075         {
2076           if (!add_dynamic_entry (DT_PLTGOT, 0)
2077               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2078               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2079               || !add_dynamic_entry (DT_JMPREL, 0))
2080             return FALSE;
2081         }
2082
2083       if (relocs)
2084         {
2085           if (!add_dynamic_entry (DT_RELA, 0)
2086               || !add_dynamic_entry (DT_RELASZ, 0)
2087               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2088             return FALSE;
2089
2090           /* If any dynamic relocs apply to a read-only section,
2091              then we need a DT_TEXTREL entry.  */
2092           if ((info->flags & DF_TEXTREL) == 0)
2093             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2094                                     (PTR) info);
2095
2096           if ((info->flags & DF_TEXTREL) != 0)
2097             {
2098               if (!add_dynamic_entry (DT_TEXTREL, 0))
2099                 return FALSE;
2100             }
2101         }
2102     }
2103 #undef add_dynamic_entry
2104
2105   return TRUE;
2106 }
2107
2108 /* Return the base VMA address which should be subtracted from real addresses
2109    when resolving @dtpoff relocation.
2110    This is PT_TLS segment p_vaddr.  */
2111
2112 static bfd_vma
2113 dtpoff_base (info)
2114      struct bfd_link_info *info;
2115 {
2116   /* If tls_segment is NULL, we should have signalled an error already.  */
2117   if (elf_hash_table (info)->tls_segment == NULL)
2118     return 0;
2119   return elf_hash_table (info)->tls_segment->start;
2120 }
2121
2122 /* Return the relocation value for @tpoff relocation
2123    if STT_TLS virtual address is ADDRESS.  */
2124
2125 static bfd_vma
2126 tpoff (info, address)
2127      struct bfd_link_info *info;
2128      bfd_vma address;
2129 {
2130   struct elf_link_tls_segment *tls_segment
2131     = elf_hash_table (info)->tls_segment;
2132
2133   /* If tls_segment is NULL, we should have signalled an error already.  */
2134   if (tls_segment == NULL)
2135     return 0;
2136   return (align_power (tls_segment->size, tls_segment->align)
2137           + tls_segment->start - address);
2138 }
2139
2140 /* Complain if TLS instruction relocation is against an invalid
2141    instruction.  */
2142
2143 static void
2144 invalid_tls_insn (input_bfd, input_section, rel)
2145      bfd *input_bfd;
2146      asection *input_section;
2147      Elf_Internal_Rela *rel;
2148 {
2149   reloc_howto_type *howto;
2150
2151   howto = elf_howto_table + ELF32_R_TYPE (rel->r_info);
2152   (*_bfd_error_handler)
2153     (_("%s(%s+0x%lx): invalid instruction for TLS relocation %s"),
2154      bfd_archive_filename (input_bfd),
2155      bfd_get_section_name (input_bfd, input_section),
2156      (long) rel->r_offset,
2157      howto->name);
2158 }
2159
2160 /* Relocate a 390 ELF section.  */
2161
2162 static bfd_boolean
2163 elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
2164                               contents, relocs, local_syms, local_sections)
2165      bfd *output_bfd;
2166      struct bfd_link_info *info;
2167      bfd *input_bfd;
2168      asection *input_section;
2169      bfd_byte *contents;
2170      Elf_Internal_Rela *relocs;
2171      Elf_Internal_Sym *local_syms;
2172      asection **local_sections;
2173 {
2174   struct elf_s390_link_hash_table *htab;
2175   Elf_Internal_Shdr *symtab_hdr;
2176   struct elf_link_hash_entry **sym_hashes;
2177   bfd_vma *local_got_offsets;
2178   Elf_Internal_Rela *rel;
2179   Elf_Internal_Rela *relend;
2180
2181   if (info->relocateable)
2182     return TRUE;
2183
2184   htab = elf_s390_hash_table (info);
2185   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2186   sym_hashes = elf_sym_hashes (input_bfd);
2187   local_got_offsets = elf_local_got_offsets (input_bfd);
2188
2189   rel = relocs;
2190   relend = relocs + input_section->reloc_count;
2191   for (; rel < relend; rel++)
2192     {
2193       unsigned int r_type;
2194       reloc_howto_type *howto;
2195       unsigned long r_symndx;
2196       struct elf_link_hash_entry *h;
2197       Elf_Internal_Sym *sym;
2198       asection *sec;
2199       bfd_vma off;
2200       bfd_vma relocation;
2201       bfd_boolean unresolved_reloc;
2202       bfd_reloc_status_type r;
2203       int tls_type;
2204
2205       r_type = ELF32_R_TYPE (rel->r_info);
2206       if (r_type == (int) R_390_GNU_VTINHERIT
2207           || r_type == (int) R_390_GNU_VTENTRY)
2208         continue;
2209       if (r_type >= (int) R_390_max)
2210         {
2211           bfd_set_error (bfd_error_bad_value);
2212           return FALSE;
2213         }
2214
2215       howto = elf_howto_table + r_type;
2216       r_symndx = ELF32_R_SYM (rel->r_info);
2217
2218       /* This is a final link.  */
2219       h = NULL;
2220       sym = NULL;
2221       sec = NULL;
2222       unresolved_reloc = FALSE;
2223       if (r_symndx < symtab_hdr->sh_info)
2224         {
2225           sym = local_syms + r_symndx;
2226           sec = local_sections[r_symndx];
2227           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
2228         }
2229       else
2230         {
2231           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2232           while (h->root.type == bfd_link_hash_indirect
2233                  || h->root.type == bfd_link_hash_warning)
2234             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2235
2236           if (h->root.type == bfd_link_hash_defined
2237               || h->root.type == bfd_link_hash_defweak)
2238             {
2239               sec = h->root.u.def.section;
2240               if (sec->output_section == NULL)
2241                 {
2242                   /* Set a flag that will be cleared later if we find a
2243                      relocation value for this symbol.  output_section
2244                      is typically NULL for symbols satisfied by a shared
2245                      library.  */
2246                   unresolved_reloc = TRUE;
2247                   relocation = 0;
2248                 }
2249               else
2250                 relocation = (h->root.u.def.value
2251                               + sec->output_section->vma
2252                               + sec->output_offset);
2253             }
2254           else if (h->root.type == bfd_link_hash_undefweak)
2255             relocation = 0;
2256           else if (info->shared
2257                    && !info->no_undefined
2258                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2259             relocation = 0;
2260           else
2261             {
2262               if (! ((*info->callbacks->undefined_symbol)
2263                      (info, h->root.root.string, input_bfd,
2264                       input_section, rel->r_offset,
2265                       (!info->shared || info->no_undefined
2266                        || ELF_ST_VISIBILITY (h->other)))))
2267                 return FALSE;
2268               relocation = 0;
2269             }
2270         }
2271
2272       switch (r_type)
2273         {
2274         case R_390_GOTPLT12:
2275         case R_390_GOTPLT16:
2276         case R_390_GOTPLT32:
2277         case R_390_GOTPLTENT:
2278           /* There are three cases for a GOTPLT relocation. 1) The
2279              relocation is against the jump slot entry of a plt that
2280              will get emitted to the output file. 2) The relocation
2281              is against the jump slot of a plt entry that has been
2282              removed. elf_s390_adjust_gotplt has created a GOT entry
2283              as replacement. 3) The relocation is against a local symbol.
2284              Cases 2) and 3) are the same as the GOT relocation code
2285              so we just have to test for case 1 and fall through for
2286              the other two.  */
2287           if (h != NULL && h->plt.offset != (bfd_vma) -1)
2288             {
2289               bfd_vma plt_index;
2290
2291               /* Calc. index no.
2292                  Current offset - size first entry / entry size.  */
2293               plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) /
2294                 PLT_ENTRY_SIZE;
2295               
2296               /* Offset in GOT is PLT index plus GOT headers(3) times 4,
2297                  addr & GOT addr.  */
2298               relocation = (plt_index + 3) * GOT_ENTRY_SIZE;
2299               unresolved_reloc = FALSE;
2300
2301               if (r_type == R_390_GOTPLTENT)
2302                 relocation += htab->sgot->output_section->vma;
2303               break;
2304             }
2305           /* Fall through.  */
2306
2307         case R_390_GOT12:
2308         case R_390_GOT16:
2309         case R_390_GOT32:
2310         case R_390_GOTENT:
2311           /* Relocation is to the entry for this symbol in the global
2312              offset table.  */
2313           if (htab->sgot == NULL)
2314             abort ();
2315
2316           if (h != NULL)
2317             {
2318               bfd_boolean dyn;
2319
2320               off = h->got.offset;
2321               dyn = htab->elf.dynamic_sections_created;
2322               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
2323                   || (info->shared
2324                       && (info->symbolic
2325                           || h->dynindx == -1
2326                           || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2327                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2328                 {
2329                   /* This is actually a static link, or it is a
2330                      -Bsymbolic link and the symbol is defined
2331                      locally, or the symbol was forced to be local
2332                      because of a version file.  We must initialize
2333                      this entry in the global offset table.  Since the
2334                      offset must always be a multiple of 2, we use the
2335                      least significant bit to record whether we have
2336                      initialized it already.
2337
2338                      When doing a dynamic link, we create a .rel.got
2339                      relocation entry to initialize the value.  This
2340                      is done in the finish_dynamic_symbol routine.  */
2341                   if ((off & 1) != 0)
2342                     off &= ~1;
2343                   else
2344                     {
2345                       bfd_put_32 (output_bfd, relocation,
2346                                   htab->sgot->contents + off);
2347                       h->got.offset |= 1;
2348                     }
2349                 }
2350               else
2351                 unresolved_reloc = FALSE;
2352             }
2353           else
2354             {
2355               if (local_got_offsets == NULL)
2356                 abort ();
2357
2358               off = local_got_offsets[r_symndx];
2359
2360               /* The offset must always be a multiple of 4.  We use
2361                  the least significant bit to record whether we have
2362                  already generated the necessary reloc.  */
2363               if ((off & 1) != 0)
2364                 off &= ~1;
2365               else
2366                 {
2367                   bfd_put_32 (output_bfd, relocation,
2368                               htab->sgot->contents + off);
2369
2370                   if (info->shared)
2371                     {
2372                       asection *srelgot;
2373                       Elf_Internal_Rela outrel;
2374                       bfd_byte *loc;
2375
2376                       srelgot = htab->srelgot;
2377                       if (srelgot == NULL)
2378                         abort ();
2379
2380                       outrel.r_offset = (htab->sgot->output_section->vma
2381                                          + htab->sgot->output_offset
2382                                          + off);
2383                       outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2384                       outrel.r_addend = relocation;
2385                       loc = srelgot->contents;
2386                       loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
2387                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2388                     }
2389
2390                   local_got_offsets[r_symndx] |= 1;
2391                 }
2392             }
2393
2394           if (off >= (bfd_vma) -2)
2395             abort ();
2396
2397           relocation = htab->sgot->output_offset + off;
2398
2399           /* For @GOTENT the relocation is against the offset between
2400              the instruction and the symbols entry in the GOT and not
2401              between the start of the GOT and the symbols entry. We
2402              add the vma of the GOT to get the correct value.  */
2403           if (   r_type == R_390_GOTENT
2404               || r_type == R_390_GOTPLTENT)
2405             relocation += htab->sgot->output_section->vma;
2406
2407           break;
2408
2409         case R_390_GOTOFF16:
2410         case R_390_GOTOFF32:
2411           /* Relocation is relative to the start of the global offset
2412              table.  */
2413
2414           /* Note that sgot->output_offset is not involved in this
2415              calculation.  We always want the start of .got.  If we
2416              defined _GLOBAL_OFFSET_TABLE in a different way, as is
2417              permitted by the ABI, we might have to change this
2418              calculation.  */
2419           relocation -= htab->sgot->output_section->vma;
2420           break;
2421
2422         case R_390_GOTPC:
2423         case R_390_GOTPCDBL:
2424           /* Use global offset table as symbol value.  */
2425           relocation = htab->sgot->output_section->vma;
2426           unresolved_reloc = FALSE;
2427           break;
2428
2429         case R_390_PLT16DBL:
2430         case R_390_PLT32DBL:
2431         case R_390_PLT32:
2432           /* Relocation is to the entry for this symbol in the
2433              procedure linkage table.  */
2434
2435           /* Resolve a PLT32 reloc against a local symbol directly,
2436              without using the procedure linkage table.  */
2437           if (h == NULL)
2438             break;
2439
2440           if (h->plt.offset == (bfd_vma) -1
2441               || htab->splt == NULL)
2442             {
2443               /* We didn't make a PLT entry for this symbol.  This
2444                  happens when statically linking PIC code, or when
2445                  using -Bsymbolic.  */
2446               break;
2447             }
2448
2449           relocation = (htab->splt->output_section->vma
2450                         + htab->splt->output_offset
2451                         + h->plt.offset);
2452           unresolved_reloc = FALSE;
2453           break;
2454
2455         case R_390_PLTOFF16:
2456         case R_390_PLTOFF32:
2457           /* Relocation is to the entry for this symbol in the
2458              procedure linkage table relative to the start of the GOT.  */
2459
2460           /* For local symbols or if we didn't make a PLT entry for
2461              this symbol resolve the symbol directly.  */
2462           if (   h == NULL
2463               || h->plt.offset == (bfd_vma) -1
2464               || htab->splt == NULL)
2465             {
2466               relocation -= htab->sgot->output_section->vma;
2467               break;
2468             }
2469
2470           relocation = (htab->splt->output_section->vma
2471                         + htab->splt->output_offset
2472                         + h->plt.offset
2473                         - htab->sgot->output_section->vma);
2474           unresolved_reloc = FALSE;
2475           break;
2476
2477         case R_390_8:
2478         case R_390_16:
2479         case R_390_32:
2480         case R_390_PC16:
2481         case R_390_PC16DBL:
2482         case R_390_PC32DBL:
2483         case R_390_PC32:
2484           /* r_symndx will be zero only for relocs against symbols
2485              from removed linkonce sections, or sections discarded by
2486              a linker script.  */
2487           if (r_symndx == 0
2488               || (input_section->flags & SEC_ALLOC) == 0)
2489             break;
2490
2491           if ((info->shared
2492                && ((r_type != R_390_PC16
2493                     && r_type != R_390_PC16DBL
2494                     && r_type != R_390_PC32DBL
2495                     && r_type != R_390_PC32)
2496                    || (h != NULL
2497                        && h->dynindx != -1
2498                        && (! info->symbolic
2499                            || (h->elf_link_hash_flags
2500                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2501               || (!info->shared
2502                   && h != NULL
2503                   && h->dynindx != -1
2504                   && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2505                   && (((h->elf_link_hash_flags
2506                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2507                        && (h->elf_link_hash_flags
2508                            & ELF_LINK_HASH_DEF_REGULAR) == 0)
2509                       || h->root.type == bfd_link_hash_undefweak
2510                       || h->root.type == bfd_link_hash_undefined)))
2511             {
2512               Elf_Internal_Rela outrel;
2513               bfd_boolean skip, relocate;
2514               asection *sreloc;
2515               bfd_byte *loc;
2516
2517               /* When generating a shared object, these relocations
2518                  are copied into the output file to be resolved at run
2519                  time.  */
2520
2521               skip = FALSE;
2522               relocate = FALSE;
2523
2524               outrel.r_offset =
2525                 _bfd_elf_section_offset (output_bfd, info, input_section,
2526                                          rel->r_offset);
2527               if (outrel.r_offset == (bfd_vma) -1)
2528                 skip = TRUE;
2529               else if (outrel.r_offset == (bfd_vma) -2)
2530                 skip = TRUE, relocate = TRUE;
2531               outrel.r_offset += (input_section->output_section->vma
2532                                   + input_section->output_offset);
2533
2534               if (skip)
2535                 memset (&outrel, 0, sizeof outrel);
2536               else if (h != NULL
2537                        && h->dynindx != -1
2538                        && (r_type == R_390_PC16
2539                            || r_type == R_390_PC16DBL
2540                            || r_type == R_390_PC32DBL
2541                            || r_type == R_390_PC32
2542                            || !info->shared
2543                            || !info->symbolic
2544                            || (h->elf_link_hash_flags
2545                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
2546                 {
2547                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2548                   outrel.r_addend = rel->r_addend;
2549                 }
2550               else
2551                 {
2552                   /* This symbol is local, or marked to become local.  */
2553                   relocate = TRUE;
2554                   outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2555                   outrel.r_addend = relocation + rel->r_addend;
2556                 }
2557
2558               sreloc = elf_section_data (input_section)->sreloc;
2559               if (sreloc == NULL)
2560                 abort ();
2561
2562               loc = sreloc->contents;
2563               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2564               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2565
2566               /* If this reloc is against an external symbol, we do
2567                  not want to fiddle with the addend.  Otherwise, we
2568                  need to include the symbol value so that it becomes
2569                  an addend for the dynamic reloc.  */
2570               if (! relocate)
2571                 continue;
2572             }
2573           break;
2574
2575           /* Relocations for tls literal pool entries.  */
2576         case R_390_TLS_IE32:
2577           if (info->shared)
2578             {
2579               Elf_Internal_Rela outrel;
2580               asection *sreloc;
2581               bfd_byte *loc;
2582
2583               outrel.r_offset = rel->r_offset
2584                                 + input_section->output_section->vma
2585                                 + input_section->output_offset;
2586               outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2587               sreloc = elf_section_data (input_section)->sreloc;
2588               if (sreloc == NULL)
2589                 abort ();
2590               loc = sreloc->contents;
2591               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2592               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2593             }
2594           /* Fall through.  */
2595
2596         case R_390_TLS_GD32:
2597         case R_390_TLS_GOTIE32:
2598           r_type = elf_s390_tls_transition (info, r_type, h == NULL);
2599           tls_type = GOT_UNKNOWN;
2600           if (h == NULL && local_got_offsets)
2601             tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
2602           else if (h != NULL)
2603             {
2604               tls_type = elf_s390_hash_entry(h)->tls_type;
2605               if (!info->shared && h->dynindx == -1 && tls_type >= GOT_TLS_IE)
2606                 r_type = R_390_TLS_LE32;
2607             }
2608           if (r_type == R_390_TLS_GD32 && tls_type >= GOT_TLS_IE)
2609             r_type = R_390_TLS_IE32;
2610
2611           if (r_type == R_390_TLS_LE32)
2612             {
2613               /* This relocation gets optimized away by the local exec
2614                  access optimization.  */
2615               BFD_ASSERT (! unresolved_reloc);
2616               bfd_put_32 (output_bfd, -tpoff (info, relocation),
2617                           contents + rel->r_offset);
2618               continue;
2619             }
2620
2621           if (htab->sgot == NULL)
2622             abort ();
2623
2624           if (h != NULL)
2625             off = h->got.offset;
2626           else
2627             {
2628               if (local_got_offsets == NULL)
2629                 abort ();
2630
2631               off = local_got_offsets[r_symndx];
2632             }
2633
2634         emit_tls_relocs:
2635
2636           if ((off & 1) != 0)
2637             off &= ~1;
2638           else
2639             {
2640               Elf_Internal_Rela outrel;
2641               bfd_byte *loc;
2642               int dr_type, indx;
2643
2644               if (htab->srelgot == NULL)
2645                 abort ();
2646
2647               outrel.r_offset = (htab->sgot->output_section->vma
2648                                  + htab->sgot->output_offset + off);
2649
2650               indx = h && h->dynindx != -1 ? h->dynindx : 0;
2651               if (r_type == R_390_TLS_GD32)
2652                 dr_type = R_390_TLS_DTPMOD;
2653               else
2654                 dr_type = R_390_TLS_TPOFF;
2655               if (dr_type == R_390_TLS_TPOFF && indx == 0)
2656                 outrel.r_addend = relocation - dtpoff_base (info);
2657               else
2658                 outrel.r_addend = 0;
2659               outrel.r_info = ELF32_R_INFO (indx, dr_type);
2660               loc = htab->srelgot->contents;
2661               loc += htab->srelgot->reloc_count++
2662                 * sizeof (Elf32_External_Rela);
2663               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2664
2665               if (r_type == R_390_TLS_GD32)
2666                 {
2667                   if (indx == 0)
2668                     {
2669                       BFD_ASSERT (! unresolved_reloc);
2670                       bfd_put_32 (output_bfd,
2671                                   relocation - dtpoff_base (info),
2672                                   htab->sgot->contents + off + GOT_ENTRY_SIZE);
2673                     }
2674                   else
2675                     {
2676                       outrel.r_info = ELF32_R_INFO (indx, R_390_TLS_DTPOFF);
2677                       outrel.r_offset += GOT_ENTRY_SIZE;
2678                       outrel.r_addend = 0;
2679                       htab->srelgot->reloc_count++;
2680                       loc += sizeof (Elf32_External_Rela);
2681                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2682                     }
2683                 }
2684
2685               if (h != NULL)
2686                 h->got.offset |= 1;
2687               else
2688                 local_got_offsets[r_symndx] |= 1;
2689             }
2690
2691           if (off >= (bfd_vma) -2)
2692             abort ();
2693           if (r_type == ELF32_R_TYPE (rel->r_info))
2694             {
2695               relocation = htab->sgot->output_offset + off;
2696               if (r_type == R_390_TLS_IE32 || r_type == R_390_TLS_IEENT)
2697                 relocation += htab->sgot->output_section->vma;
2698               unresolved_reloc = FALSE;
2699             }
2700           else
2701             {
2702               bfd_put_32 (output_bfd, htab->sgot->output_offset + off,
2703                           contents + rel->r_offset);
2704               continue;
2705             }
2706           break;
2707
2708         case R_390_TLS_GOTIE12:
2709         case R_390_TLS_IEENT:
2710           if (h == NULL)
2711             {
2712               if (local_got_offsets == NULL)
2713                 abort();
2714               off = local_got_offsets[r_symndx];
2715               if (info->shared)
2716                 goto emit_tls_relocs;
2717             }
2718           else
2719             {
2720               off = h->got.offset;
2721               tls_type = elf_s390_hash_entry(h)->tls_type;
2722               if (info->shared || h->dynindx != -1 || tls_type < GOT_TLS_IE)
2723                 goto emit_tls_relocs;
2724             }
2725
2726           if (htab->sgot == NULL)
2727             abort ();
2728
2729           BFD_ASSERT (! unresolved_reloc);
2730           bfd_put_32 (output_bfd, -tpoff (info, relocation),
2731                       htab->sgot->contents + off);
2732           relocation = htab->sgot->output_offset + off;
2733           if (r_type == R_390_TLS_IEENT)
2734             relocation += htab->sgot->output_section->vma;
2735           unresolved_reloc = FALSE;
2736           break;
2737
2738         case R_390_TLS_LDM32:
2739           if (! info->shared)
2740             /* The literal pool entry this relocation refers to gets ignored
2741                by the optimized code of the local exec model. Do nothing
2742                and the value will turn out zero.  */
2743             continue;
2744
2745           if (htab->sgot == NULL)
2746             abort ();
2747
2748           off = htab->tls_ldm_got.offset;
2749           if (off & 1)
2750             off &= ~1;
2751           else
2752             {
2753               Elf_Internal_Rela outrel;
2754               bfd_byte *loc;
2755
2756               if (htab->srelgot == NULL)
2757                 abort ();
2758
2759               outrel.r_offset = (htab->sgot->output_section->vma
2760                                  + htab->sgot->output_offset + off);
2761
2762               bfd_put_32 (output_bfd, 0,
2763                           htab->sgot->contents + off + GOT_ENTRY_SIZE);
2764               outrel.r_info = ELF32_R_INFO (0, R_390_TLS_DTPMOD);
2765               outrel.r_addend = 0;
2766               loc = htab->srelgot->contents;
2767               loc += htab->srelgot->reloc_count++
2768                 * sizeof (Elf32_External_Rela);
2769               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2770               htab->tls_ldm_got.offset |= 1;
2771             }
2772           relocation = htab->sgot->output_offset + off;
2773           unresolved_reloc = FALSE;       
2774           break;
2775
2776         case R_390_TLS_LE32:
2777           if (info->shared)
2778             {
2779               /* Linking a shared library with non-fpic code requires
2780                  a R_390_TLS_TPOFF relocation.  */
2781               Elf_Internal_Rela outrel;
2782               asection *sreloc;
2783               bfd_byte *loc;
2784               int indx;
2785
2786               outrel.r_offset = rel->r_offset
2787                                 + input_section->output_section->vma
2788                                 + input_section->output_offset;
2789               if (h != NULL && h->dynindx != -1)
2790                 indx = h->dynindx;
2791               else
2792                 indx = 0;
2793               outrel.r_info = ELF32_R_INFO (indx, R_390_TLS_TPOFF);
2794               if (indx == 0)
2795                 outrel.r_addend = relocation - dtpoff_base (info);
2796               else
2797                 outrel.r_addend = 0;
2798               sreloc = elf_section_data (input_section)->sreloc;
2799               if (sreloc == NULL)
2800                 abort ();
2801               loc = sreloc->contents;
2802               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2803               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2804             }
2805           else
2806             {
2807               BFD_ASSERT (! unresolved_reloc);
2808               bfd_put_32 (output_bfd, -tpoff (info, relocation),
2809                           contents + rel->r_offset);
2810             }
2811           continue;
2812
2813         case R_390_TLS_LDO32:
2814           if (info->shared || (input_section->flags & SEC_CODE) == 0)
2815             relocation -= dtpoff_base (info);
2816           else
2817             /* When converting LDO to LE, we must negate.  */
2818             relocation = -tpoff (info, relocation);
2819           break;
2820
2821           /* Relocations for tls instructions.  */
2822         case R_390_TLS_LOAD:
2823         case R_390_TLS_GDCALL:
2824         case R_390_TLS_LDCALL:
2825           tls_type = GOT_UNKNOWN;
2826           if (h == NULL && local_got_offsets)
2827             tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
2828           else if (h != NULL)
2829             tls_type = elf_s390_hash_entry(h)->tls_type;
2830
2831           if (tls_type == GOT_TLS_GD)
2832             continue;
2833
2834           if (r_type == R_390_TLS_LOAD)
2835             {
2836               if (!info->shared && (h == NULL || h->dynindx == -1))
2837                 {
2838                   /* IE->LE transition. Four valid cases:
2839                      l %rx,0(0,%ry)    -> lr %rx,%ry + bcr 0,0
2840                      l %rx,0(%ry,0)    -> lr %rx,%ry + bcr 0,0
2841                      l %rx,0(%ry,%r12) -> lr %rx,%ry + bcr 0,0
2842                      l %rx,0(%r12,%ry) -> lr %rx,%ry + bcr 0,0 */
2843                   unsigned int insn, ry;
2844
2845                   insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2846                   ry = 0;
2847                   if ((insn & 0xff00f000) == 0x58000000)
2848                     /* l %rx,0(%ry,0) -> lr %rx,%ry + bcr 0,0  */
2849                     ry = (insn & 0x000f0000);
2850                   else if ((insn & 0xff0f0000) == 0x58000000)
2851                     /* l %rx,0(0,%ry) -> lr %rx,%ry + bcr 0,0  */
2852                     ry = (insn & 0x0000f000) << 4;
2853                   else if ((insn & 0xff00f000) == 0x5800c000)
2854                     /* l %rx,0(%ry,%r12) -> lr %rx,%ry + bcr 0,0  */
2855                     ry = (insn & 0x000f0000);
2856                   else if ((insn & 0xff0f0000) == 0x580c0000)
2857                     /* l %rx,0(%r12,%ry) -> lr %rx,%ry + bcr 0,0  */
2858                     ry = (insn & 0x0000f000) << 4;
2859                   else
2860                     invalid_tls_insn (input_bfd, input_section, rel);
2861                   insn = 0x18000700 | (insn & 0x00f00000) | ry;
2862                   bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
2863                 }
2864             }
2865           else if (r_type == R_390_TLS_GDCALL)
2866             {
2867               unsigned int insn;
2868
2869               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2870               if ((insn & 0xff000fff) != 0x4d000000)
2871                 invalid_tls_insn (input_bfd, input_section, rel);
2872               if (!info->shared && (h == NULL || h->dynindx == -1))
2873                 /* GD->LE transition.
2874                    bas %r14,0(%rx,%r13) -> bc 0,0  */
2875                 insn = 0x47000000;
2876               else
2877                 /* GD->IE transition.
2878                    bas %r14,0(%rx,%r13) -> l %r2,0(%r2,%r12)  */
2879                 insn = 0x5822c000;
2880               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
2881             }
2882           else if (r_type == R_390_TLS_LDCALL)
2883             {
2884               if (!info->shared)
2885                 {
2886                   unsigned int insn;
2887
2888                   insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2889                   if ((insn & 0xff000fff) != 0x4d000000)
2890                     invalid_tls_insn (input_bfd, input_section, rel);
2891                   /* LD->LE transition.
2892                      bas %r14,0(%rx,%r13) -> bc 0,0  */
2893                   insn = 0x47000000;
2894                   bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
2895                 }
2896             }
2897           continue;
2898
2899         default:
2900           break;
2901         }
2902
2903       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2904          because such sections are not SEC_ALLOC and thus ld.so will
2905          not process them.  */
2906       if (unresolved_reloc
2907           && !((input_section->flags & SEC_DEBUGGING) != 0
2908                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2909         (*_bfd_error_handler)
2910           (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2911            bfd_archive_filename (input_bfd),
2912            bfd_get_section_name (input_bfd, input_section),
2913            (long) rel->r_offset,
2914            h->root.root.string);
2915
2916       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2917                                     contents, rel->r_offset,
2918                                     relocation, rel->r_addend);
2919
2920       if (r != bfd_reloc_ok)
2921         {
2922           const char *name;
2923
2924           if (h != NULL)
2925             name = h->root.root.string;
2926           else
2927             {
2928               name = bfd_elf_string_from_elf_section (input_bfd,
2929                                                       symtab_hdr->sh_link,
2930                                                       sym->st_name);
2931               if (name == NULL)
2932                 return FALSE;
2933               if (*name == '\0')
2934                 name = bfd_section_name (input_bfd, sec);
2935             }
2936
2937           if (r == bfd_reloc_overflow)
2938             {
2939
2940               if (! ((*info->callbacks->reloc_overflow)
2941                      (info, name, howto->name, (bfd_vma) 0,
2942                       input_bfd, input_section, rel->r_offset)))
2943                 return FALSE;
2944             }
2945           else
2946             {
2947               (*_bfd_error_handler)
2948                 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
2949                  bfd_archive_filename (input_bfd),
2950                  bfd_get_section_name (input_bfd, input_section),
2951                  (long) rel->r_offset, name, (int) r);
2952               return FALSE;
2953             }
2954         }
2955     }
2956
2957   return TRUE;
2958 }
2959
2960 /* Finish up dynamic symbol handling.  We set the contents of various
2961    dynamic sections here.  */
2962
2963 static bfd_boolean
2964 elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym)
2965      bfd *output_bfd;
2966      struct bfd_link_info *info;
2967      struct elf_link_hash_entry *h;
2968      Elf_Internal_Sym *sym;
2969 {
2970   struct elf_s390_link_hash_table *htab;
2971
2972   htab = elf_s390_hash_table (info);
2973
2974   if (h->plt.offset != (bfd_vma) -1)
2975     {
2976       bfd_vma plt_index;
2977       bfd_vma got_offset;
2978       Elf_Internal_Rela rela;
2979       bfd_byte *loc;
2980       bfd_vma relative_offset;
2981
2982       /* This symbol has an entry in the procedure linkage table.  Set
2983          it up.  */
2984       if (h->dynindx == -1
2985           || htab->splt == NULL
2986           || htab->sgotplt == NULL
2987           || htab->srelplt == NULL)
2988         abort ();
2989
2990       /* Calc. index no.
2991          Current offset - size first entry / entry size.  */
2992       plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
2993
2994       /* Offset in GOT is PLT index plus GOT headers(3) times 4,
2995          addr & GOT addr.  */
2996       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2997
2998       /* S390 uses halfwords for relative branch calc!  */
2999       relative_offset = - ((PLT_FIRST_ENTRY_SIZE +
3000                            (PLT_ENTRY_SIZE * plt_index) + 18) / 2);
3001       /* If offset is > 32768, branch to a previous branch
3002          390 can only handle +-64 K jumps.  */
3003       if ( -32768 > (int) relative_offset )
3004           relative_offset =
3005             -(unsigned) (((65536 / PLT_ENTRY_SIZE - 1) * PLT_ENTRY_SIZE) / 2);
3006
3007       /* Fill in the entry in the procedure linkage table.  */
3008       if (!info->shared)
3009         {
3010           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD0,
3011                       htab->splt->contents + h->plt.offset);
3012           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD1,
3013                       htab->splt->contents + h->plt.offset + 4);
3014           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
3015                       htab->splt->contents + h->plt.offset + 8);
3016           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
3017                       htab->splt->contents + h->plt.offset + 12);
3018           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD4,
3019                       htab->splt->contents + h->plt.offset + 16);
3020           bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
3021                       htab->splt->contents + h->plt.offset + 20);
3022           bfd_put_32 (output_bfd,
3023                       (htab->sgotplt->output_section->vma
3024                        + htab->sgotplt->output_offset
3025                        + got_offset),
3026                       htab->splt->contents + h->plt.offset + 24);
3027         }
3028       else if (got_offset < 4096)
3029         {
3030           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD0 + got_offset,
3031                       htab->splt->contents + h->plt.offset);
3032           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD1,
3033                       htab->splt->contents + h->plt.offset + 4);
3034           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD2,
3035                       htab->splt->contents + h->plt.offset + 8);
3036           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD3,
3037                       htab->splt->contents + h->plt.offset + 12);
3038           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD4,
3039                       htab->splt->contents + h->plt.offset + 16);
3040           bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
3041                       htab->splt->contents + h->plt.offset + 20);
3042           bfd_put_32 (output_bfd, (bfd_vma) 0,
3043                       htab->splt->contents + h->plt.offset + 24);
3044         }
3045       else if (got_offset < 32768)
3046         {
3047           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD0 + got_offset,
3048                       htab->splt->contents + h->plt.offset);
3049           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD1,
3050                       htab->splt->contents + h->plt.offset + 4);
3051           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD2,
3052                       htab->splt->contents + h->plt.offset + 8);
3053           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD3,
3054                       htab->splt->contents + h->plt.offset + 12);
3055           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD4,
3056                       htab->splt->contents + h->plt.offset + 16);
3057           bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
3058                       htab->splt->contents + h->plt.offset + 20);
3059           bfd_put_32 (output_bfd, (bfd_vma) 0,
3060                       htab->splt->contents + h->plt.offset + 24);
3061         }
3062       else
3063         {
3064           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD0,
3065                       htab->splt->contents + h->plt.offset);
3066           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD1,
3067                       htab->splt->contents + h->plt.offset + 4);
3068           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
3069                       htab->splt->contents + h->plt.offset + 8);
3070           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
3071                       htab->splt->contents + h->plt.offset + 12);
3072           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
3073                       htab->splt->contents + h->plt.offset + 16);
3074           bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
3075                       htab->splt->contents + h->plt.offset + 20);
3076           bfd_put_32 (output_bfd, got_offset,
3077                       htab->splt->contents + h->plt.offset + 24);
3078         }
3079       /* Insert offset into  reloc. table here.  */
3080       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
3081                   htab->splt->contents + h->plt.offset + 28);
3082
3083       /* Fill in the entry in the global offset table.
3084          Points to instruction after GOT offset.  */
3085       bfd_put_32 (output_bfd,
3086                   (htab->splt->output_section->vma
3087                    + htab->splt->output_offset
3088                    + h->plt.offset
3089                    + 12),
3090                   htab->sgotplt->contents + got_offset);
3091
3092       /* Fill in the entry in the .rela.plt section.  */
3093       rela.r_offset = (htab->sgotplt->output_section->vma
3094                        + htab->sgotplt->output_offset
3095                        + got_offset);
3096       rela.r_info = ELF32_R_INFO (h->dynindx, R_390_JMP_SLOT);
3097       rela.r_addend = 0;
3098       loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
3099       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3100
3101       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3102         {
3103           /* Mark the symbol as undefined, rather than as defined in
3104              the .plt section.  Leave the value alone.  This is a clue
3105              for the dynamic linker, to make function pointer
3106              comparisons work between an application and shared
3107              library.  */
3108           sym->st_shndx = SHN_UNDEF;
3109         }
3110     }
3111
3112   if (h->got.offset != (bfd_vma) -1
3113       && elf_s390_hash_entry(h)->tls_type != GOT_TLS_GD
3114       && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE
3115       && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE_NLT)
3116     {
3117       Elf_Internal_Rela rela;
3118       bfd_byte *loc;
3119
3120       /* This symbol has an entry in the global offset table.  Set it
3121          up.  */
3122
3123       if (htab->sgot == NULL || htab->srelgot == NULL)
3124         abort ();
3125
3126       rela.r_offset = (htab->sgot->output_section->vma
3127                        + htab->sgot->output_offset
3128                        + (h->got.offset &~ (bfd_vma) 1));
3129
3130       /* If this is a static link, or it is a -Bsymbolic link and the
3131          symbol is defined locally or was forced to be local because
3132          of a version file, we just want to emit a RELATIVE reloc.
3133          The entry in the global offset table will already have been
3134          initialized in the relocate_section function.  */
3135       if (info->shared
3136           && (info->symbolic
3137               || h->dynindx == -1
3138               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3139           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3140         {
3141           BFD_ASSERT((h->got.offset & 1) != 0);
3142           rela.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
3143           rela.r_addend = (h->root.u.def.value
3144                            + h->root.u.def.section->output_section->vma
3145                            + h->root.u.def.section->output_offset);
3146         }
3147       else
3148         {
3149           BFD_ASSERT((h->got.offset & 1) == 0);
3150           bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgot->contents + h->got.offset);
3151           rela.r_info = ELF32_R_INFO (h->dynindx, R_390_GLOB_DAT);
3152           rela.r_addend = 0;
3153         }
3154
3155       loc = htab->srelgot->contents;
3156       loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3157       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3158     }
3159
3160   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3161     {
3162       Elf_Internal_Rela rela;
3163       bfd_byte *loc;
3164
3165       /* This symbols needs a copy reloc.  Set it up.  */
3166
3167       if (h->dynindx == -1
3168           || (h->root.type != bfd_link_hash_defined
3169               && h->root.type != bfd_link_hash_defweak)
3170           || htab->srelbss == NULL)
3171         abort ();
3172
3173       rela.r_offset = (h->root.u.def.value
3174                        + h->root.u.def.section->output_section->vma
3175                        + h->root.u.def.section->output_offset);
3176       rela.r_info = ELF32_R_INFO (h->dynindx, R_390_COPY);
3177       rela.r_addend = 0;
3178       loc = htab->srelbss->contents;
3179       loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rela);
3180       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3181     }
3182
3183   /* Mark some specially defined symbols as absolute.  */
3184   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3185       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3186       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3187     sym->st_shndx = SHN_ABS;
3188
3189   return TRUE;
3190 }
3191
3192 /* Used to decide how to sort relocs in an optimal manner for the
3193    dynamic linker, before writing them out.  */
3194
3195 static enum elf_reloc_type_class
3196 elf_s390_reloc_type_class (rela)
3197      const Elf_Internal_Rela *rela;
3198 {
3199   switch ((int) ELF32_R_TYPE (rela->r_info))
3200     {
3201     case R_390_RELATIVE:
3202       return reloc_class_relative;
3203     case R_390_JMP_SLOT:
3204       return reloc_class_plt;
3205     case R_390_COPY:
3206       return reloc_class_copy;
3207     default:
3208       return reloc_class_normal;
3209     }
3210 }
3211
3212 /* Finish up the dynamic sections.  */
3213
3214 static bfd_boolean
3215 elf_s390_finish_dynamic_sections (output_bfd, info)
3216      bfd *output_bfd;
3217      struct bfd_link_info *info;
3218 {
3219   struct elf_s390_link_hash_table *htab;
3220   bfd *dynobj;
3221   asection *sdyn;
3222
3223   htab = elf_s390_hash_table (info);
3224   dynobj = htab->elf.dynobj;
3225   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3226
3227   if (htab->elf.dynamic_sections_created)
3228     {
3229       Elf32_External_Dyn *dyncon, *dynconend;
3230
3231       if (sdyn == NULL || htab->sgot == NULL)
3232         abort ();
3233
3234       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3235       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3236       for (; dyncon < dynconend; dyncon++)
3237         {
3238           Elf_Internal_Dyn dyn;
3239           asection *s;
3240
3241           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3242
3243           switch (dyn.d_tag)
3244             {
3245             default:
3246               continue;
3247
3248             case DT_PLTGOT:
3249               dyn.d_un.d_ptr = htab->sgot->output_section->vma;
3250               break;
3251
3252             case DT_JMPREL:
3253               dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3254               break;
3255
3256             case DT_PLTRELSZ:
3257               s = htab->srelplt->output_section;
3258               if (s->_cooked_size != 0)
3259                 dyn.d_un.d_val = s->_cooked_size;
3260               else
3261                 dyn.d_un.d_val = s->_raw_size;
3262               break;
3263             }
3264
3265           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3266         }
3267
3268       /* Fill in the special first entry in the procedure linkage table.  */
3269       if (htab->splt && htab->splt->_raw_size > 0)
3270         {
3271           memset (htab->splt->contents, 0, PLT_FIRST_ENTRY_SIZE);
3272           if (info->shared)
3273             {
3274               bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD0,
3275                           htab->splt->contents );
3276               bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD1,
3277                           htab->splt->contents +4 );
3278               bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD2,
3279                           htab->splt->contents +8 );
3280               bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD3,
3281                           htab->splt->contents +12 );
3282               bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD4,
3283                           htab->splt->contents +16 );
3284            }
3285           else
3286            {
3287               bfd_put_32 (output_bfd, (bfd_vma)PLT_FIRST_ENTRY_WORD0,
3288                           htab->splt->contents );
3289               bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD1,
3290                           htab->splt->contents +4 );
3291               bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD2,
3292                           htab->splt->contents +8 );
3293               bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD3,
3294                           htab->splt->contents +12 );
3295               bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD4,
3296                           htab->splt->contents +16 );
3297               bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD5,
3298                           htab->splt->contents +20 );
3299               bfd_put_32 (output_bfd,
3300                           htab->sgotplt->output_section->vma
3301                           + htab->sgotplt->output_offset,
3302                           htab->splt->contents + 24);
3303            }
3304           elf_section_data (htab->splt->output_section)
3305             ->this_hdr.sh_entsize = 4;
3306         }
3307
3308     }
3309
3310   if (htab->sgotplt)
3311     {
3312       /* Fill in the first three entries in the global offset table.  */
3313       if (htab->sgotplt->_raw_size > 0)
3314         {
3315           bfd_put_32 (output_bfd,
3316                       (sdyn == NULL ? (bfd_vma) 0
3317                        : sdyn->output_section->vma + sdyn->output_offset),
3318                       htab->sgotplt->contents);
3319           /* One entry for shared object struct ptr.  */
3320           bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 4);
3321           /* One entry for _dl_runtime_resolve.  */
3322           bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8);
3323         }
3324
3325       elf_section_data (htab->sgotplt->output_section)
3326         ->this_hdr.sh_entsize = 4;
3327     }
3328   return TRUE;
3329 }
3330
3331 static bfd_boolean
3332 elf_s390_grok_prstatus (abfd, note)
3333      bfd * abfd;
3334      Elf_Internal_Note * note;
3335 {
3336   int offset;
3337   unsigned int raw_size;
3338
3339   switch (note->descsz)
3340     {
3341       default:
3342         return FALSE;
3343
3344       case 224:         /* S/390 Linux.  */
3345         /* pr_cursig */
3346         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
3347
3348         /* pr_pid */
3349         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
3350
3351         /* pr_reg */
3352         offset = 72;
3353         raw_size = 144;
3354         break;
3355     }
3356
3357   /* Make a ".reg/999" section.  */
3358   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3359                                           raw_size, note->descpos + offset);
3360 }
3361
3362 #define TARGET_BIG_SYM  bfd_elf32_s390_vec
3363 #define TARGET_BIG_NAME "elf32-s390"
3364 #define ELF_ARCH        bfd_arch_s390
3365 #define ELF_MACHINE_CODE EM_S390
3366 #define ELF_MACHINE_ALT1 EM_S390_OLD
3367 #define ELF_MAXPAGESIZE 0x1000
3368
3369 #define elf_backend_can_gc_sections     1
3370 #define elf_backend_can_refcount        1
3371 #define elf_backend_want_got_plt        1
3372 #define elf_backend_plt_readonly        1
3373 #define elf_backend_want_plt_sym        0
3374 #define elf_backend_got_header_size     12
3375 #define elf_backend_plt_header_size     PLT_ENTRY_SIZE
3376 #define elf_backend_rela_normal         1
3377
3378 #define elf_info_to_howto                     elf_s390_info_to_howto
3379
3380 #define bfd_elf32_bfd_is_local_label_name     elf_s390_is_local_label_name
3381 #define bfd_elf32_bfd_link_hash_table_create  elf_s390_link_hash_table_create
3382 #define bfd_elf32_bfd_reloc_type_lookup       elf_s390_reloc_type_lookup
3383
3384 #define elf_backend_adjust_dynamic_symbol     elf_s390_adjust_dynamic_symbol
3385 #define elf_backend_check_relocs              elf_s390_check_relocs
3386 #define elf_backend_copy_indirect_symbol      elf_s390_copy_indirect_symbol
3387 #define elf_backend_create_dynamic_sections   elf_s390_create_dynamic_sections
3388 #define elf_backend_finish_dynamic_sections   elf_s390_finish_dynamic_sections
3389 #define elf_backend_finish_dynamic_symbol     elf_s390_finish_dynamic_symbol
3390 #define elf_backend_gc_mark_hook              elf_s390_gc_mark_hook
3391 #define elf_backend_gc_sweep_hook             elf_s390_gc_sweep_hook
3392 #define elf_backend_reloc_type_class          elf_s390_reloc_type_class
3393 #define elf_backend_relocate_section          elf_s390_relocate_section
3394 #define elf_backend_size_dynamic_sections     elf_s390_size_dynamic_sections
3395 #define elf_backend_reloc_type_class          elf_s390_reloc_type_class
3396 #define elf_backend_grok_prstatus             elf_s390_grok_prstatus
3397
3398 #define bfd_elf32_mkobject              elf_s390_mkobject
3399 #define elf_backend_object_p            elf_s390_object_p
3400
3401 #include "elf32-target.h"