source: trunk/src/binutils/bfd/elf32-s390.c@ 657

Last change on this file since 657 was 607, checked in by bird, 22 years ago

Initial revision

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