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