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