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