* write.c (write_relocs): Use xcalloc. Fix relocs initialization
[binutils.git] / bfd / elf32-s390.c
blob97be1dbd013052ed08ae5b0e4b7d0d9026f25760
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.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
28 static reloc_howto_type *elf_s390_reloc_type_lookup
29 PARAMS ((bfd *, bfd_reloc_code_real_type));
30 static void elf_s390_info_to_howto
31 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
32 static bfd_boolean elf_s390_is_local_label_name
33 PARAMS ((bfd *, const char *));
34 static struct bfd_hash_entry *link_hash_newfunc
35 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
36 static struct bfd_link_hash_table *elf_s390_link_hash_table_create
37 PARAMS ((bfd *));
38 static bfd_boolean create_got_section
39 PARAMS((bfd *, struct bfd_link_info *));
40 static bfd_boolean elf_s390_create_dynamic_sections
41 PARAMS((bfd *, struct bfd_link_info *));
42 static void elf_s390_copy_indirect_symbol
43 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
44 struct elf_link_hash_entry *));
45 static bfd_boolean elf_s390_check_relocs
46 PARAMS ((bfd *, struct bfd_link_info *, asection *,
47 const Elf_Internal_Rela *));
48 static asection *elf_s390_gc_mark_hook
49 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
50 struct elf_link_hash_entry *, Elf_Internal_Sym *));
51 static bfd_boolean elf_s390_gc_sweep_hook
52 PARAMS ((bfd *, struct bfd_link_info *, asection *,
53 const Elf_Internal_Rela *));
54 struct elf_s390_link_hash_entry;
55 static void elf_s390_adjust_gotplt
56 PARAMS ((struct elf_s390_link_hash_entry *));
57 static bfd_boolean elf_s390_adjust_dynamic_symbol
58 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
59 static bfd_boolean allocate_dynrelocs
60 PARAMS ((struct elf_link_hash_entry *, PTR));
61 static bfd_boolean readonly_dynrelocs
62 PARAMS ((struct elf_link_hash_entry *, PTR));
63 static bfd_boolean elf_s390_size_dynamic_sections
64 PARAMS ((bfd *, struct bfd_link_info *));
65 static bfd_boolean elf_s390_relocate_section
66 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
67 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
68 static bfd_boolean elf_s390_finish_dynamic_symbol
69 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
70 Elf_Internal_Sym *));
71 static enum elf_reloc_type_class elf_s390_reloc_type_class
72 PARAMS ((const Elf_Internal_Rela *));
73 static bfd_boolean elf_s390_finish_dynamic_sections
74 PARAMS ((bfd *, struct bfd_link_info *));
75 static bfd_boolean elf_s390_mkobject
76 PARAMS ((bfd *));
77 static bfd_boolean elf_s390_object_p
78 PARAMS ((bfd *));
79 static bfd_boolean elf_s390_grok_prstatus
80 PARAMS ((bfd *, Elf_Internal_Note *));
81 static int elf_s390_tls_transition
82 PARAMS ((struct bfd_link_info *, int, int));
83 static bfd_reloc_status_type s390_tls_reloc
84 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
85 static bfd_vma dtpoff_base
86 PARAMS ((struct bfd_link_info *));
87 static bfd_vma tpoff
88 PARAMS ((struct bfd_link_info *, bfd_vma));
89 static void invalid_tls_insn
90 PARAMS ((bfd *, asection *, Elf_Internal_Rela *));
92 #include "elf/s390.h"
94 /* The relocation "howto" table. */
96 static reloc_howto_type elf_howto_table[] =
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 */
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),
213 /* GNU extension to record C++ vtable hierarchy. */
214 static reloc_howto_type elf32_s390_vtinherit_howto =
215 HOWTO (R_390_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", FALSE,0, 0, FALSE);
216 static reloc_howto_type elf32_s390_vtentry_howto =
217 HOWTO (R_390_GNU_VTENTRY, 0,2,0,FALSE,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_390_GNU_VTENTRY", FALSE,0,0, FALSE);
219 static reloc_howto_type *
220 elf_s390_reloc_type_lookup (abfd, code)
221 bfd *abfd ATTRIBUTE_UNUSED;
222 bfd_reloc_code_real_type code;
224 switch (code)
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;
323 return 0;
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. */
329 static void
330 elf_s390_info_to_howto (abfd, cache_ptr, dst)
331 bfd *abfd ATTRIBUTE_UNUSED;
332 arelent *cache_ptr;
333 Elf_Internal_Rela *dst;
335 switch (ELF32_R_TYPE(dst->r_info))
337 case R_390_GNU_VTINHERIT:
338 cache_ptr->howto = &elf32_s390_vtinherit_howto;
339 break;
341 case R_390_GNU_VTENTRY:
342 cache_ptr->howto = &elf32_s390_vtentry_howto;
343 break;
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)];
351 /* A relocation function which doesn't do anything. */
352 static bfd_reloc_status_type
353 s390_tls_reloc (abfd, reloc_entry, symbol, data, input_section,
354 output_bfd, error_message)
355 bfd *abfd ATTRIBUTE_UNUSED;
356 arelent *reloc_entry;
357 asymbol *symbol ATTRIBUTE_UNUSED;
358 PTR data ATTRIBUTE_UNUSED;
359 asection *input_section;
360 bfd *output_bfd;
361 char **error_message ATTRIBUTE_UNUSED;
363 if (output_bfd)
364 reloc_entry->address += input_section->output_offset;
365 return bfd_reloc_ok;
368 static bfd_boolean
369 elf_s390_is_local_label_name (abfd, name)
370 bfd *abfd;
371 const char *name;
373 if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L'))
374 return TRUE;
376 return _bfd_elf_is_local_label_name (abfd, name);
379 /* Functions for the 390 ELF linker. */
381 /* The name of the dynamic interpreter. This is put in the .interp
382 section. */
384 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
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
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
397 #define GOT_ENTRY_SIZE 4
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. */
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)
409 Furthermore, only r 0 and 1 are free to use!!! */
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.
416 The code for position independand PLT entries looks like this:
418 r12 holds addr of the current GOT at entry to the PLT
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
425 The loader must then find the module where the function is
426 and insert the address in the GOT.
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
432 PLT1: 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
436 RET1: 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
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:
447 PLT1: 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
450 RET1: 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
456 Second the one for GOT offsets < 32768:
458 PLT1: 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
462 RET1: 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
468 Total = 32 bytes per PLT entry
470 The code for static build PLT entries looks like this:
472 PLT1: 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
476 RET1: 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 */
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
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
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
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
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. */
511 /* The first entry in the PLT for PIC code:
513 PLT0:
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
520 The first entry in the PLT for static code:
522 PLT0:
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 */
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
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
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. */
551 struct elf_s390_dyn_relocs
553 struct elf_s390_dyn_relocs *next;
555 /* The input section of the reloc. */
556 asection *sec;
558 /* Total number of relocs copied for the input section. */
559 bfd_size_type count;
561 /* Number of pc-relative relocs copied for the input section. */
562 bfd_size_type pc_count;
565 /* s390 ELF linker hash entry. */
567 struct elf_s390_link_hash_entry
569 struct elf_link_hash_entry elf;
571 /* Track dynamic relocs copied for this symbol. */
572 struct elf_s390_dyn_relocs *dyn_relocs;
574 /* Number of GOTPLT references for a function. */
575 bfd_signed_vma gotplt_refcount;
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;
585 #define elf_s390_hash_entry(ent) \
586 ((struct elf_s390_link_hash_entry *)(ent))
588 struct elf_s390_obj_tdata
590 struct elf_obj_tdata root;
592 /* tls_type for each local got entry. */
593 char *local_got_tls_type;
596 #define elf_s390_tdata(abfd) \
597 ((struct elf_s390_obj_tdata *) (abfd)->tdata.any)
599 #define elf_s390_local_got_tls_type(abfd) \
600 (elf_s390_tdata (abfd)->local_got_tls_type)
602 static bfd_boolean
603 elf_s390_mkobject (abfd)
604 bfd *abfd;
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;
613 static bfd_boolean
614 elf_s390_object_p (abfd)
615 bfd *abfd;
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);
629 /* s390 ELF linker hash table. */
631 struct elf_s390_link_hash_table
633 struct elf_link_hash_table elf;
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;
644 union {
645 bfd_signed_vma refcount;
646 bfd_vma offset;
647 } tls_ldm_got;
649 /* Small local sym to section mapping cache. */
650 struct sym_sec_cache sym_sec;
653 /* Get the s390 ELF linker hash table from a link_info structure. */
655 #define elf_s390_hash_table(p) \
656 ((struct elf_s390_link_hash_table *) ((p)->hash))
658 /* Create an entry in an s390 ELF linker hash table. */
660 static struct bfd_hash_entry *
661 link_hash_newfunc (entry, table, string)
662 struct bfd_hash_entry *entry;
663 struct bfd_hash_table *table;
664 const char *string;
666 /* Allocate the structure if it has not already been allocated by a
667 subclass. */
668 if (entry == NULL)
670 entry = bfd_hash_allocate (table,
671 sizeof (struct elf_s390_link_hash_entry));
672 if (entry == NULL)
673 return entry;
676 /* Call the allocation method of the superclass. */
677 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
678 if (entry != NULL)
680 struct elf_s390_link_hash_entry *eh;
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;
688 return entry;
691 /* Create an s390 ELF linker hash table. */
693 static struct bfd_link_hash_table *
694 elf_s390_link_hash_table_create (abfd)
695 bfd *abfd;
697 struct elf_s390_link_hash_table *ret;
698 bfd_size_type amt = sizeof (struct elf_s390_link_hash_table);
700 ret = (struct elf_s390_link_hash_table *) bfd_malloc (amt);
701 if (ret == NULL)
702 return NULL;
704 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
706 free (ret);
707 return NULL;
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;
720 return &ret->elf.root;
723 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
724 shortcuts to them in our hash table. */
726 static bfd_boolean
727 create_got_section (dynobj, info)
728 bfd *dynobj;
729 struct bfd_link_info *info;
731 struct elf_s390_link_hash_table *htab;
733 if (! _bfd_elf_create_got_section (dynobj, info))
734 return FALSE;
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 ();
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;
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. */
757 static bfd_boolean
758 elf_s390_create_dynamic_sections (dynobj, info)
759 bfd *dynobj;
760 struct bfd_link_info *info;
762 struct elf_s390_link_hash_table *htab;
764 htab = elf_s390_hash_table (info);
765 if (!htab->sgot && !create_got_section (dynobj, info))
766 return FALSE;
768 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
769 return FALSE;
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");
777 if (!htab->splt || !htab->srelplt || !htab->sdynbss
778 || (!info->shared && !htab->srelbss))
779 abort ();
781 return TRUE;
784 /* Copy the extra info we tack onto an elf_link_hash_entry. */
786 static void
787 elf_s390_copy_indirect_symbol (bed, dir, ind)
788 struct elf_backend_data *bed;
789 struct elf_link_hash_entry *dir, *ind;
791 struct elf_s390_link_hash_entry *edir, *eind;
793 edir = (struct elf_s390_link_hash_entry *) dir;
794 eind = (struct elf_s390_link_hash_entry *) ind;
796 if (eind->dyn_relocs != NULL)
798 if (edir->dyn_relocs != NULL)
800 struct elf_s390_dyn_relocs **pp;
801 struct elf_s390_dyn_relocs *p;
803 if (ind->root.type == bfd_link_hash_indirect)
804 abort ();
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; )
810 struct elf_s390_dyn_relocs *q;
812 for (q = edir->dyn_relocs; q != NULL; q = q->next)
813 if (q->sec == p->sec)
815 q->pc_count += p->pc_count;
816 q->count += p->count;
817 *pp = p->next;
818 break;
820 if (q == NULL)
821 pp = &p->next;
823 *pp = edir->dyn_relocs;
826 edir->dyn_relocs = eind->dyn_relocs;
827 eind->dyn_relocs = NULL;
830 if (ind->root.type == bfd_link_hash_indirect
831 && dir->got.refcount <= 0)
833 edir->tls_type = eind->tls_type;
834 eind->tls_type = GOT_UNKNOWN;
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);
851 static int
852 elf_s390_tls_transition (info, r_type, is_local)
853 struct bfd_link_info *info;
854 int r_type;
855 int is_local;
857 if (info->shared)
858 return r_type;
860 switch (r_type)
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;
875 return r_type;
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. */
882 static bfd_boolean
883 elf_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;
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;
898 if (info->relocateable)
899 return TRUE;
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);
906 sreloc = NULL;
908 rel_end = relocs + sec->reloc_count;
909 for (rel = relocs; rel < rel_end; rel++)
911 unsigned int r_type;
912 unsigned long r_symndx;
913 struct elf_link_hash_entry *h;
915 r_symndx = ELF32_R_SYM (rel->r_info);
917 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
919 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
920 bfd_archive_filename (abfd),
921 r_symndx);
922 return FALSE;
925 if (r_symndx < symtab_hdr->sh_info)
926 h = NULL;
927 else
928 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
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)
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)
954 bfd_size_type size;
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);
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)
973 if (htab->elf.dynobj == NULL)
974 htab->elf.dynobj = abfd;
975 if (!create_got_section (htab->elf.dynobj, info))
976 return FALSE;
980 switch (r_type)
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;
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. */
1001 /* If this is a local symbol, we resolve it directly without
1002 creating a procedure linkage table entry. */
1003 if (h != NULL)
1005 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1006 h->plt.refcount += 1;
1008 break;
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)
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;
1028 else
1029 local_got_refcounts[r_symndx] += 1;
1030 break;
1032 case R_390_TLS_LDM32:
1033 htab->tls_ldm_got.refcount += 1;
1034 break;
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. */
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)
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;
1072 if (h != NULL)
1074 h->got.refcount += 1;
1075 old_tls_type = elf_s390_hash_entry(h)->tls_type;
1077 else
1079 local_got_refcounts[r_symndx] += 1;
1080 old_tls_type = elf_s390_local_got_tls_type (abfd) [r_symndx];
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)
1086 if (old_tls_type == GOT_NORMAL || tls_type == GOT_NORMAL)
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;
1093 if (old_tls_type > tls_type)
1094 tls_type = old_tls_type;
1097 if (old_tls_type != tls_type)
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;
1105 if (r_type != R_390_TLS_IE32)
1106 break;
1107 /* Fall through. */
1109 case R_390_TLS_LE32:
1110 if (!info->shared)
1111 break;
1112 info->flags |= DF_STATIC_TLS;
1113 /* Fall through. */
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)
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;
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;
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.
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)))
1177 struct elf_s390_dyn_relocs *p;
1178 struct elf_s390_dyn_relocs **head;
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)
1185 const char *name;
1186 bfd *dynobj;
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;
1195 if (strncmp (name, ".rela", 5) != 0
1196 || strcmp (bfd_get_section_name (abfd, sec),
1197 name + 5) != 0)
1199 (*_bfd_error_handler)
1200 (_("%s: bad relocation section name `%s\'"),
1201 bfd_archive_filename (abfd), name);
1204 if (htab->elf.dynobj == NULL)
1205 htab->elf.dynobj = abfd;
1207 dynobj = htab->elf.dynobj;
1208 sreloc = bfd_get_section_by_name (dynobj, name);
1209 if (sreloc == NULL)
1211 flagword flags;
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;
1223 elf_section_data (sec)->sreloc = sreloc;
1226 /* If this is a global symbol, we count the number of
1227 relocations we need for this symbol. */
1228 if (h != NULL)
1230 head = &((struct elf_s390_link_hash_entry *) h)->dyn_relocs;
1232 else
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;
1239 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1240 sec, r_symndx);
1241 if (s == NULL)
1242 return FALSE;
1244 head = ((struct elf_s390_dyn_relocs **)
1245 &elf_section_data (s)->local_dynrel);
1248 p = *head;
1249 if (p == NULL || p->sec != sec)
1251 bfd_size_type amt = sizeof *p;
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;
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;
1271 break;
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;
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;
1287 default:
1288 break;
1292 return TRUE;
1295 /* Return the section that should be marked against GC for a given
1296 relocation. */
1298 static asection *
1299 elf_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;
1306 if (h != NULL)
1308 switch (ELF32_R_TYPE (rel->r_info))
1310 case R_390_GNU_VTINHERIT:
1311 case R_390_GNU_VTENTRY:
1312 break;
1314 default:
1315 switch (h->root.type)
1317 case bfd_link_hash_defined:
1318 case bfd_link_hash_defweak:
1319 return h->root.u.def.section;
1321 case bfd_link_hash_common:
1322 return h->root.u.c.p->section;
1324 default:
1325 break;
1329 else
1330 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1332 return NULL;
1335 /* Update the got entry reference counts for the section being removed. */
1337 static bfd_boolean
1338 elf_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;
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;
1349 elf_section_data (sec)->local_dynrel = NULL;
1351 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1352 sym_hashes = elf_sym_hashes (abfd);
1353 local_got_refcounts = elf_local_got_refcounts (abfd);
1355 relend = relocs + sec->reloc_count;
1356 for (rel = relocs; rel < relend; rel++)
1358 unsigned long r_symndx;
1359 unsigned int r_type;
1360 struct elf_link_hash_entry *h = NULL;
1362 r_symndx = ELF32_R_SYM (rel->r_info);
1363 if (r_symndx >= symtab_hdr->sh_info)
1365 struct elf_s390_link_hash_entry *eh;
1366 struct elf_s390_dyn_relocs **pp;
1367 struct elf_s390_dyn_relocs *p;
1369 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1370 eh = (struct elf_s390_link_hash_entry *) h;
1372 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1373 if (p->sec == sec)
1375 /* Everything must go for SEC. */
1376 *pp = p->next;
1377 break;
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)
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;
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)
1405 if (h->got.refcount > 0)
1406 h->got.refcount -= 1;
1408 else if (local_got_refcounts != NULL)
1410 if (local_got_refcounts[r_symndx] > 0)
1411 local_got_refcounts[r_symndx] -= 1;
1413 break;
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. */
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)
1434 if (h->plt.refcount > 0)
1435 h->plt.refcount -= 1;
1437 break;
1439 case R_390_GOTPLT12:
1440 case R_390_GOTPLT16:
1441 case R_390_GOTPLT32:
1442 case R_390_GOTPLTENT:
1443 if (h != NULL)
1445 if (h->plt.refcount > 0)
1447 ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount--;
1448 h->plt.refcount -= 1;
1451 else if (local_got_refcounts != NULL)
1453 if (local_got_refcounts[r_symndx] > 0)
1454 local_got_refcounts[r_symndx] -= 1;
1456 break;
1458 default:
1459 break;
1463 return TRUE;
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). */
1473 static void
1474 elf_s390_adjust_gotplt (h)
1475 struct elf_s390_link_hash_entry *h;
1477 if (h->elf.root.type == bfd_link_hash_warning)
1478 h = (struct elf_s390_link_hash_entry *) h->elf.root.u.i.link;
1480 if (h->gotplt_refcount <= 0)
1481 return;
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;
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. */
1495 static bfd_boolean
1496 elf_s390_adjust_dynamic_symbol (info, h)
1497 struct bfd_link_info *info;
1498 struct elf_link_hash_entry *h;
1500 struct elf_s390_link_hash_table *htab;
1501 asection *s;
1502 unsigned int power_of_two;
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)
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))
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);
1527 return TRUE;
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;
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)
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;
1553 /* This is a reference to a symbol defined by a dynamic object which
1554 is not a function. */
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;
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;
1568 /* If -z nocopyreloc was given, we won't generate them either. */
1569 if (info->nocopyreloc)
1571 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1572 return TRUE;
1575 if (ELIMINATE_COPY_RELOCS)
1577 struct elf_s390_link_hash_entry * eh;
1578 struct elf_s390_dyn_relocs *p;
1580 eh = (struct elf_s390_link_hash_entry *) h;
1581 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1583 s = p->sec->output_section;
1584 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1585 break;
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)
1592 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1593 return TRUE;
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. */
1607 htab = elf_s390_hash_table (info);
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)
1614 htab->srelbss->_raw_size += sizeof (Elf32_External_Rela);
1615 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
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;
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))
1629 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1630 return FALSE;
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;
1637 /* Increment the section size to make room for the symbol. */
1638 s->_raw_size += h->size;
1640 return TRUE;
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, SHARED, H) \
1648 ((DYN) \
1649 && ((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))
1654 /* Allocate space in .plt, .got and associated reloc sections for
1655 dynamic relocs. */
1657 static bfd_boolean
1658 allocate_dynrelocs (h, inf)
1659 struct elf_link_hash_entry *h;
1660 PTR inf;
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;
1667 if (h->root.type == bfd_link_hash_indirect)
1668 return TRUE;
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;
1676 info = (struct bfd_link_info *) inf;
1677 htab = elf_s390_hash_table (info);
1679 if (htab->elf.dynamic_sections_created
1680 && h->plt.refcount > 0
1681 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1682 || h->root.type != bfd_link_hash_undefweak))
1684 /* Make sure this symbol is output as a dynamic symbol.
1685 Undefined weak syms won't yet be marked as dynamic. */
1686 if (h->dynindx == -1
1687 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1689 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1690 return FALSE;
1693 if (info->shared
1694 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1696 asection *s = htab->splt;
1698 /* If this is the first .plt entry, make room for the special
1699 first entry. */
1700 if (s->_raw_size == 0)
1701 s->_raw_size += PLT_FIRST_ENTRY_SIZE;
1703 h->plt.offset = s->_raw_size;
1705 /* If this symbol is not defined in a regular file, and we are
1706 not generating a shared library, then set the symbol to this
1707 location in the .plt. This is required to make function
1708 pointers compare as equal between the normal executable and
1709 the shared library. */
1710 if (! info->shared
1711 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1713 h->root.u.def.section = s;
1714 h->root.u.def.value = h->plt.offset;
1717 /* Make room for this entry. */
1718 s->_raw_size += PLT_ENTRY_SIZE;
1720 /* We also need to make an entry in the .got.plt section, which
1721 will be placed in the .got section by the linker script. */
1722 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1724 /* We also need to make an entry in the .rela.plt section. */
1725 htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
1727 else
1729 h->plt.offset = (bfd_vma) -1;
1730 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1731 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1734 else
1736 h->plt.offset = (bfd_vma) -1;
1737 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1738 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1741 /* If R_390_TLS_{IE32,GOTIE32,GOTIE12,IEENT} symbol is now local to
1742 the binary, we can optimize a bit. IE32 and GOTIE32 get converted
1743 to R_390_TLS_LE32 requiring no TLS entry. For GOTIE12 and IEENT
1744 we can save the dynamic TLS relocation. */
1745 if (h->got.refcount > 0
1746 && !info->shared
1747 && h->dynindx == -1
1748 && elf_s390_hash_entry(h)->tls_type >= GOT_TLS_IE)
1750 if (elf_s390_hash_entry(h)->tls_type == GOT_TLS_IE_NLT)
1751 /* For the GOTIE access without a literal pool entry the offset has
1752 to be stored somewhere. The immediate value in the instruction
1753 is not bit enough so the value is stored in the got. */
1755 h->got.offset = htab->sgot->_raw_size;
1756 htab->sgot->_raw_size += GOT_ENTRY_SIZE;
1758 else
1759 h->got.offset = (bfd_vma) -1;
1761 else if (h->got.refcount > 0)
1763 asection *s;
1764 bfd_boolean dyn;
1765 int tls_type = elf_s390_hash_entry(h)->tls_type;
1767 /* Make sure this symbol is output as a dynamic symbol.
1768 Undefined weak syms won't yet be marked as dynamic. */
1769 if (h->dynindx == -1
1770 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1772 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1773 return FALSE;
1776 s = htab->sgot;
1777 h->got.offset = s->_raw_size;
1778 s->_raw_size += GOT_ENTRY_SIZE;
1779 /* R_390_TLS_GD32 needs 2 consecutive GOT slots. */
1780 if (tls_type == GOT_TLS_GD)
1781 s->_raw_size += GOT_ENTRY_SIZE;
1782 dyn = htab->elf.dynamic_sections_created;
1783 /* R_390_TLS_IE32 needs one dynamic relocation,
1784 R_390_TLS_GD32 needs one if local symbol and two if global. */
1785 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1786 || tls_type >= GOT_TLS_IE)
1787 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1788 else if (tls_type == GOT_TLS_GD)
1789 htab->srelgot->_raw_size += 2 * sizeof (Elf32_External_Rela);
1790 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1791 || h->root.type != bfd_link_hash_undefweak)
1792 && (info->shared
1793 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1794 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1796 else
1797 h->got.offset = (bfd_vma) -1;
1799 eh = (struct elf_s390_link_hash_entry *) h;
1800 if (eh->dyn_relocs == NULL)
1801 return TRUE;
1803 /* In the shared -Bsymbolic case, discard space allocated for
1804 dynamic pc-relative relocs against symbols which turn out to be
1805 defined in regular objects. For the normal shared case, discard
1806 space for pc-relative relocs that have become local due to symbol
1807 visibility changes. */
1809 if (info->shared)
1811 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1812 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1813 || info->symbolic))
1815 struct elf_s390_dyn_relocs **pp;
1817 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1819 p->count -= p->pc_count;
1820 p->pc_count = 0;
1821 if (p->count == 0)
1822 *pp = p->next;
1823 else
1824 pp = &p->next;
1828 /* Also discard relocs on undefined weak syms with non-default
1829 visibility. */
1830 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1831 && h->root.type == bfd_link_hash_undefweak)
1832 eh->dyn_relocs = NULL;
1834 else if (ELIMINATE_COPY_RELOCS)
1836 /* For the non-shared case, discard space for relocs against
1837 symbols which turn out to need copy relocs or are not
1838 dynamic. */
1840 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1841 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1842 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1843 || (htab->elf.dynamic_sections_created
1844 && (h->root.type == bfd_link_hash_undefweak
1845 || h->root.type == bfd_link_hash_undefined))))
1847 /* Make sure this symbol is output as a dynamic symbol.
1848 Undefined weak syms won't yet be marked as dynamic. */
1849 if (h->dynindx == -1
1850 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1852 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1853 return FALSE;
1856 /* If that succeeded, we know we'll be keeping all the
1857 relocs. */
1858 if (h->dynindx != -1)
1859 goto keep;
1862 eh->dyn_relocs = NULL;
1864 keep: ;
1867 /* Finally, allocate space. */
1868 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1870 asection *sreloc = elf_section_data (p->sec)->sreloc;
1872 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
1875 return TRUE;
1878 /* Find any dynamic relocs that apply to read-only sections. */
1880 static bfd_boolean
1881 readonly_dynrelocs (h, inf)
1882 struct elf_link_hash_entry *h;
1883 PTR inf;
1885 struct elf_s390_link_hash_entry *eh;
1886 struct elf_s390_dyn_relocs *p;
1888 if (h->root.type == bfd_link_hash_warning)
1889 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1891 eh = (struct elf_s390_link_hash_entry *) h;
1892 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1894 asection *s = p->sec->output_section;
1896 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1898 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1900 info->flags |= DF_TEXTREL;
1902 /* Not an error, just cut short the traversal. */
1903 return FALSE;
1906 return TRUE;
1909 /* Set the sizes of the dynamic sections. */
1911 static bfd_boolean
1912 elf_s390_size_dynamic_sections (output_bfd, info)
1913 bfd *output_bfd ATTRIBUTE_UNUSED;
1914 struct bfd_link_info *info;
1916 struct elf_s390_link_hash_table *htab;
1917 bfd *dynobj;
1918 asection *s;
1919 bfd_boolean relocs;
1920 bfd *ibfd;
1922 htab = elf_s390_hash_table (info);
1923 dynobj = htab->elf.dynobj;
1924 if (dynobj == NULL)
1925 abort ();
1927 if (htab->elf.dynamic_sections_created)
1929 /* Set the contents of the .interp section to the interpreter. */
1930 if (info->executable)
1932 s = bfd_get_section_by_name (dynobj, ".interp");
1933 if (s == NULL)
1934 abort ();
1935 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1936 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1940 /* Set up .got offsets for local syms, and space for local dynamic
1941 relocs. */
1942 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1944 bfd_signed_vma *local_got;
1945 bfd_signed_vma *end_local_got;
1946 char *local_tls_type;
1947 bfd_size_type locsymcount;
1948 Elf_Internal_Shdr *symtab_hdr;
1949 asection *srela;
1951 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1952 continue;
1954 for (s = ibfd->sections; s != NULL; s = s->next)
1956 struct elf_s390_dyn_relocs *p;
1958 for (p = *((struct elf_s390_dyn_relocs **)
1959 &elf_section_data (s)->local_dynrel);
1960 p != NULL;
1961 p = p->next)
1963 if (!bfd_is_abs_section (p->sec)
1964 && bfd_is_abs_section (p->sec->output_section))
1966 /* Input section has been discarded, either because
1967 it is a copy of a linkonce section or due to
1968 linker script /DISCARD/, so we'll be discarding
1969 the relocs too. */
1971 else if (p->count != 0)
1973 srela = elf_section_data (p->sec)->sreloc;
1974 srela->_raw_size += p->count * sizeof (Elf32_External_Rela);
1975 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1976 info->flags |= DF_TEXTREL;
1981 local_got = elf_local_got_refcounts (ibfd);
1982 if (!local_got)
1983 continue;
1985 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1986 locsymcount = symtab_hdr->sh_info;
1987 end_local_got = local_got + locsymcount;
1988 local_tls_type = elf_s390_local_got_tls_type (ibfd);
1989 s = htab->sgot;
1990 srela = htab->srelgot;
1991 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1993 if (*local_got > 0)
1995 *local_got = s->_raw_size;
1996 s->_raw_size += GOT_ENTRY_SIZE;
1997 if (*local_tls_type == GOT_TLS_GD)
1998 s->_raw_size += GOT_ENTRY_SIZE;
1999 if (info->shared)
2000 srela->_raw_size += sizeof (Elf32_External_Rela);
2002 else
2003 *local_got = (bfd_vma) -1;
2007 if (htab->tls_ldm_got.refcount > 0)
2009 /* Allocate 2 got entries and 1 dynamic reloc for R_390_TLS_LDM32
2010 relocs. */
2011 htab->tls_ldm_got.offset = htab->sgot->_raw_size;
2012 htab->sgot->_raw_size += 2 * GOT_ENTRY_SIZE;
2013 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
2015 else
2016 htab->tls_ldm_got.offset = -1;
2018 /* Allocate global sym .plt and .got entries, and space for global
2019 sym dynamic relocs. */
2020 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
2022 /* We now have determined the sizes of the various dynamic sections.
2023 Allocate memory for them. */
2024 relocs = FALSE;
2025 for (s = dynobj->sections; s != NULL; s = s->next)
2027 if ((s->flags & SEC_LINKER_CREATED) == 0)
2028 continue;
2030 if (s == htab->splt
2031 || s == htab->sgot
2032 || s == htab->sgotplt)
2034 /* Strip this section if we don't need it; see the
2035 comment below. */
2037 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2039 if (s->_raw_size != 0)
2040 relocs = TRUE;
2042 /* We use the reloc_count field as a counter if we need
2043 to copy relocs into the output file. */
2044 s->reloc_count = 0;
2046 else
2048 /* It's not one of our sections, so don't allocate space. */
2049 continue;
2052 if (s->_raw_size == 0)
2054 /* If we don't need this section, strip it from the
2055 output file. This is to handle .rela.bss and
2056 .rela.plt. We must create it in
2057 create_dynamic_sections, because it must be created
2058 before the linker maps input sections to output
2059 sections. The linker does that before
2060 adjust_dynamic_symbol is called, and it is that
2061 function which decides whether anything needs to go
2062 into these sections. */
2064 _bfd_strip_section_from_output (info, s);
2065 continue;
2068 /* Allocate memory for the section contents. We use bfd_zalloc
2069 here in case unused entries are not reclaimed before the
2070 section's contents are written out. This should not happen,
2071 but this way if it does, we get a R_390_NONE reloc instead
2072 of garbage. */
2073 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2074 if (s->contents == NULL)
2075 return FALSE;
2078 if (htab->elf.dynamic_sections_created)
2080 /* Add some entries to the .dynamic section. We fill in the
2081 values later, in elf_s390_finish_dynamic_sections, but we
2082 must add the entries now so that we get the correct size for
2083 the .dynamic section. The DT_DEBUG entry is filled in by the
2084 dynamic linker and used by the debugger. */
2085 #define add_dynamic_entry(TAG, VAL) \
2086 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2088 if (info->executable)
2090 if (!add_dynamic_entry (DT_DEBUG, 0))
2091 return FALSE;
2094 if (htab->splt->_raw_size != 0)
2096 if (!add_dynamic_entry (DT_PLTGOT, 0)
2097 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2098 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2099 || !add_dynamic_entry (DT_JMPREL, 0))
2100 return FALSE;
2103 if (relocs)
2105 if (!add_dynamic_entry (DT_RELA, 0)
2106 || !add_dynamic_entry (DT_RELASZ, 0)
2107 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2108 return FALSE;
2110 /* If any dynamic relocs apply to a read-only section,
2111 then we need a DT_TEXTREL entry. */
2112 if ((info->flags & DF_TEXTREL) == 0)
2113 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2114 (PTR) info);
2116 if ((info->flags & DF_TEXTREL) != 0)
2118 if (!add_dynamic_entry (DT_TEXTREL, 0))
2119 return FALSE;
2123 #undef add_dynamic_entry
2125 return TRUE;
2128 /* Return the base VMA address which should be subtracted from real addresses
2129 when resolving @dtpoff relocation.
2130 This is PT_TLS segment p_vaddr. */
2132 static bfd_vma
2133 dtpoff_base (info)
2134 struct bfd_link_info *info;
2136 /* If tls_segment is NULL, we should have signalled an error already. */
2137 if (elf_hash_table (info)->tls_segment == NULL)
2138 return 0;
2139 return elf_hash_table (info)->tls_segment->start;
2142 /* Return the relocation value for @tpoff relocation
2143 if STT_TLS virtual address is ADDRESS. */
2145 static bfd_vma
2146 tpoff (info, address)
2147 struct bfd_link_info *info;
2148 bfd_vma address;
2150 struct elf_link_tls_segment *tls_segment
2151 = elf_hash_table (info)->tls_segment;
2153 /* If tls_segment is NULL, we should have signalled an error already. */
2154 if (tls_segment == NULL)
2155 return 0;
2156 return (align_power (tls_segment->size, tls_segment->align)
2157 + tls_segment->start - address);
2160 /* Complain if TLS instruction relocation is against an invalid
2161 instruction. */
2163 static void
2164 invalid_tls_insn (input_bfd, input_section, rel)
2165 bfd *input_bfd;
2166 asection *input_section;
2167 Elf_Internal_Rela *rel;
2169 reloc_howto_type *howto;
2171 howto = elf_howto_table + ELF32_R_TYPE (rel->r_info);
2172 (*_bfd_error_handler)
2173 (_("%s(%s+0x%lx): invalid instruction for TLS relocation %s"),
2174 bfd_archive_filename (input_bfd),
2175 bfd_get_section_name (input_bfd, input_section),
2176 (long) rel->r_offset,
2177 howto->name);
2180 /* Relocate a 390 ELF section. */
2182 static bfd_boolean
2183 elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
2184 contents, relocs, local_syms, local_sections)
2185 bfd *output_bfd;
2186 struct bfd_link_info *info;
2187 bfd *input_bfd;
2188 asection *input_section;
2189 bfd_byte *contents;
2190 Elf_Internal_Rela *relocs;
2191 Elf_Internal_Sym *local_syms;
2192 asection **local_sections;
2194 struct elf_s390_link_hash_table *htab;
2195 Elf_Internal_Shdr *symtab_hdr;
2196 struct elf_link_hash_entry **sym_hashes;
2197 bfd_vma *local_got_offsets;
2198 Elf_Internal_Rela *rel;
2199 Elf_Internal_Rela *relend;
2201 if (info->relocateable)
2202 return TRUE;
2204 htab = elf_s390_hash_table (info);
2205 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2206 sym_hashes = elf_sym_hashes (input_bfd);
2207 local_got_offsets = elf_local_got_offsets (input_bfd);
2209 rel = relocs;
2210 relend = relocs + input_section->reloc_count;
2211 for (; rel < relend; rel++)
2213 unsigned int r_type;
2214 reloc_howto_type *howto;
2215 unsigned long r_symndx;
2216 struct elf_link_hash_entry *h;
2217 Elf_Internal_Sym *sym;
2218 asection *sec;
2219 bfd_vma off;
2220 bfd_vma relocation;
2221 bfd_boolean unresolved_reloc;
2222 bfd_reloc_status_type r;
2223 int tls_type;
2225 r_type = ELF32_R_TYPE (rel->r_info);
2226 if (r_type == (int) R_390_GNU_VTINHERIT
2227 || r_type == (int) R_390_GNU_VTENTRY)
2228 continue;
2229 if (r_type >= (int) R_390_max)
2231 bfd_set_error (bfd_error_bad_value);
2232 return FALSE;
2235 howto = elf_howto_table + r_type;
2236 r_symndx = ELF32_R_SYM (rel->r_info);
2238 /* This is a final link. */
2239 h = NULL;
2240 sym = NULL;
2241 sec = NULL;
2242 unresolved_reloc = FALSE;
2243 if (r_symndx < symtab_hdr->sh_info)
2245 sym = local_syms + r_symndx;
2246 sec = local_sections[r_symndx];
2247 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
2249 else
2251 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2252 while (h->root.type == bfd_link_hash_indirect
2253 || h->root.type == bfd_link_hash_warning)
2254 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2256 if (h->root.type == bfd_link_hash_defined
2257 || h->root.type == bfd_link_hash_defweak)
2259 sec = h->root.u.def.section;
2260 if (sec->output_section == NULL)
2262 /* Set a flag that will be cleared later if we find a
2263 relocation value for this symbol. output_section
2264 is typically NULL for symbols satisfied by a shared
2265 library. */
2266 unresolved_reloc = TRUE;
2267 relocation = 0;
2269 else
2270 relocation = (h->root.u.def.value
2271 + sec->output_section->vma
2272 + sec->output_offset);
2274 else if (h->root.type == bfd_link_hash_undefweak)
2275 relocation = 0;
2276 else if (info->shared
2277 && !info->no_undefined
2278 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2279 relocation = 0;
2280 else
2282 if (! ((*info->callbacks->undefined_symbol)
2283 (info, h->root.root.string, input_bfd,
2284 input_section, rel->r_offset,
2285 (!info->shared || info->no_undefined
2286 || ELF_ST_VISIBILITY (h->other)))))
2287 return FALSE;
2288 relocation = 0;
2292 switch (r_type)
2294 case R_390_GOTPLT12:
2295 case R_390_GOTPLT16:
2296 case R_390_GOTPLT32:
2297 case R_390_GOTPLTENT:
2298 /* There are three cases for a GOTPLT relocation. 1) The
2299 relocation is against the jump slot entry of a plt that
2300 will get emitted to the output file. 2) The relocation
2301 is against the jump slot of a plt entry that has been
2302 removed. elf_s390_adjust_gotplt has created a GOT entry
2303 as replacement. 3) The relocation is against a local symbol.
2304 Cases 2) and 3) are the same as the GOT relocation code
2305 so we just have to test for case 1 and fall through for
2306 the other two. */
2307 if (h != NULL && h->plt.offset != (bfd_vma) -1)
2309 bfd_vma plt_index;
2311 /* Calc. index no.
2312 Current offset - size first entry / entry size. */
2313 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) /
2314 PLT_ENTRY_SIZE;
2316 /* Offset in GOT is PLT index plus GOT headers(3) times 4,
2317 addr & GOT addr. */
2318 relocation = (plt_index + 3) * GOT_ENTRY_SIZE;
2319 unresolved_reloc = FALSE;
2321 if (r_type == R_390_GOTPLTENT)
2322 relocation += htab->sgot->output_section->vma;
2323 break;
2325 /* Fall through. */
2327 case R_390_GOT12:
2328 case R_390_GOT16:
2329 case R_390_GOT32:
2330 case R_390_GOTENT:
2331 /* Relocation is to the entry for this symbol in the global
2332 offset table. */
2333 if (htab->sgot == NULL)
2334 abort ();
2336 if (h != NULL)
2338 bfd_boolean dyn;
2340 off = h->got.offset;
2341 dyn = htab->elf.dynamic_sections_created;
2342 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2343 || (info->shared
2344 && (info->symbolic
2345 || h->dynindx == -1
2346 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2347 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2348 || (ELF_ST_VISIBILITY (h->other)
2349 && h->root.type == bfd_link_hash_undefweak))
2351 /* This is actually a static link, or it is a
2352 -Bsymbolic link and the symbol is defined
2353 locally, or the symbol was forced to be local
2354 because of a version file. We must initialize
2355 this entry in the global offset table. Since the
2356 offset must always be a multiple of 2, we use the
2357 least significant bit to record whether we have
2358 initialized it already.
2360 When doing a dynamic link, we create a .rel.got
2361 relocation entry to initialize the value. This
2362 is done in the finish_dynamic_symbol routine. */
2363 if ((off & 1) != 0)
2364 off &= ~1;
2365 else
2367 bfd_put_32 (output_bfd, relocation,
2368 htab->sgot->contents + off);
2369 h->got.offset |= 1;
2372 else
2373 unresolved_reloc = FALSE;
2375 else
2377 if (local_got_offsets == NULL)
2378 abort ();
2380 off = local_got_offsets[r_symndx];
2382 /* The offset must always be a multiple of 4. We use
2383 the least significant bit to record whether we have
2384 already generated the necessary reloc. */
2385 if ((off & 1) != 0)
2386 off &= ~1;
2387 else
2389 bfd_put_32 (output_bfd, relocation,
2390 htab->sgot->contents + off);
2392 if (info->shared)
2394 asection *srelgot;
2395 Elf_Internal_Rela outrel;
2396 bfd_byte *loc;
2398 srelgot = htab->srelgot;
2399 if (srelgot == NULL)
2400 abort ();
2402 outrel.r_offset = (htab->sgot->output_section->vma
2403 + htab->sgot->output_offset
2404 + off);
2405 outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2406 outrel.r_addend = relocation;
2407 loc = srelgot->contents;
2408 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
2409 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2412 local_got_offsets[r_symndx] |= 1;
2416 if (off >= (bfd_vma) -2)
2417 abort ();
2419 relocation = htab->sgot->output_offset + off;
2421 /* For @GOTENT the relocation is against the offset between
2422 the instruction and the symbols entry in the GOT and not
2423 between the start of the GOT and the symbols entry. We
2424 add the vma of the GOT to get the correct value. */
2425 if ( r_type == R_390_GOTENT
2426 || r_type == R_390_GOTPLTENT)
2427 relocation += htab->sgot->output_section->vma;
2429 break;
2431 case R_390_GOTOFF16:
2432 case R_390_GOTOFF32:
2433 /* Relocation is relative to the start of the global offset
2434 table. */
2436 /* Note that sgot->output_offset is not involved in this
2437 calculation. We always want the start of .got. If we
2438 defined _GLOBAL_OFFSET_TABLE in a different way, as is
2439 permitted by the ABI, we might have to change this
2440 calculation. */
2441 relocation -= htab->sgot->output_section->vma;
2442 break;
2444 case R_390_GOTPC:
2445 case R_390_GOTPCDBL:
2446 /* Use global offset table as symbol value. */
2447 relocation = htab->sgot->output_section->vma;
2448 unresolved_reloc = FALSE;
2449 break;
2451 case R_390_PLT16DBL:
2452 case R_390_PLT32DBL:
2453 case R_390_PLT32:
2454 /* Relocation is to the entry for this symbol in the
2455 procedure linkage table. */
2457 /* Resolve a PLT32 reloc against a local symbol directly,
2458 without using the procedure linkage table. */
2459 if (h == NULL)
2460 break;
2462 if (h->plt.offset == (bfd_vma) -1
2463 || htab->splt == NULL)
2465 /* We didn't make a PLT entry for this symbol. This
2466 happens when statically linking PIC code, or when
2467 using -Bsymbolic. */
2468 break;
2471 relocation = (htab->splt->output_section->vma
2472 + htab->splt->output_offset
2473 + h->plt.offset);
2474 unresolved_reloc = FALSE;
2475 break;
2477 case R_390_PLTOFF16:
2478 case R_390_PLTOFF32:
2479 /* Relocation is to the entry for this symbol in the
2480 procedure linkage table relative to the start of the GOT. */
2482 /* For local symbols or if we didn't make a PLT entry for
2483 this symbol resolve the symbol directly. */
2484 if ( h == NULL
2485 || h->plt.offset == (bfd_vma) -1
2486 || htab->splt == NULL)
2488 relocation -= htab->sgot->output_section->vma;
2489 break;
2492 relocation = (htab->splt->output_section->vma
2493 + htab->splt->output_offset
2494 + h->plt.offset
2495 - htab->sgot->output_section->vma);
2496 unresolved_reloc = FALSE;
2497 break;
2499 case R_390_8:
2500 case R_390_16:
2501 case R_390_32:
2502 case R_390_PC16:
2503 case R_390_PC16DBL:
2504 case R_390_PC32DBL:
2505 case R_390_PC32:
2506 /* r_symndx will be zero only for relocs against symbols
2507 from removed linkonce sections, or sections discarded by
2508 a linker script. */
2509 if (r_symndx == 0
2510 || (input_section->flags & SEC_ALLOC) == 0)
2511 break;
2513 if ((info->shared
2514 && (h == NULL
2515 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2516 || h->root.type != bfd_link_hash_undefweak)
2517 && ((r_type != R_390_PC16
2518 && r_type != R_390_PC16DBL
2519 && r_type != R_390_PC32DBL
2520 && r_type != R_390_PC32)
2521 || (h != NULL
2522 && h->dynindx != -1
2523 && (! info->symbolic
2524 || (h->elf_link_hash_flags
2525 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2526 || (ELIMINATE_COPY_RELOCS
2527 && !info->shared
2528 && h != NULL
2529 && h->dynindx != -1
2530 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2531 && (((h->elf_link_hash_flags
2532 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2533 && (h->elf_link_hash_flags
2534 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2535 || h->root.type == bfd_link_hash_undefweak
2536 || h->root.type == bfd_link_hash_undefined)))
2538 Elf_Internal_Rela outrel;
2539 bfd_boolean skip, relocate;
2540 asection *sreloc;
2541 bfd_byte *loc;
2543 /* When generating a shared object, these relocations
2544 are copied into the output file to be resolved at run
2545 time. */
2547 skip = FALSE;
2548 relocate = FALSE;
2550 outrel.r_offset =
2551 _bfd_elf_section_offset (output_bfd, info, input_section,
2552 rel->r_offset);
2553 if (outrel.r_offset == (bfd_vma) -1)
2554 skip = TRUE;
2555 else if (outrel.r_offset == (bfd_vma) -2)
2556 skip = TRUE, relocate = TRUE;
2557 outrel.r_offset += (input_section->output_section->vma
2558 + input_section->output_offset);
2560 if (skip)
2561 memset (&outrel, 0, sizeof outrel);
2562 else if (h != NULL
2563 && h->dynindx != -1
2564 && (r_type == R_390_PC16
2565 || r_type == R_390_PC16DBL
2566 || r_type == R_390_PC32DBL
2567 || r_type == R_390_PC32
2568 || !info->shared
2569 || !info->symbolic
2570 || (h->elf_link_hash_flags
2571 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2573 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2574 outrel.r_addend = rel->r_addend;
2576 else
2578 /* This symbol is local, or marked to become local. */
2579 relocate = TRUE;
2580 outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2581 outrel.r_addend = relocation + rel->r_addend;
2584 sreloc = elf_section_data (input_section)->sreloc;
2585 if (sreloc == NULL)
2586 abort ();
2588 loc = sreloc->contents;
2589 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2590 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2592 /* If this reloc is against an external symbol, we do
2593 not want to fiddle with the addend. Otherwise, we
2594 need to include the symbol value so that it becomes
2595 an addend for the dynamic reloc. */
2596 if (! relocate)
2597 continue;
2599 break;
2601 /* Relocations for tls literal pool entries. */
2602 case R_390_TLS_IE32:
2603 if (info->shared)
2605 Elf_Internal_Rela outrel;
2606 asection *sreloc;
2607 bfd_byte *loc;
2609 outrel.r_offset = rel->r_offset
2610 + input_section->output_section->vma
2611 + input_section->output_offset;
2612 outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2613 sreloc = elf_section_data (input_section)->sreloc;
2614 if (sreloc == NULL)
2615 abort ();
2616 loc = sreloc->contents;
2617 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2618 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2620 /* Fall through. */
2622 case R_390_TLS_GD32:
2623 case R_390_TLS_GOTIE32:
2624 r_type = elf_s390_tls_transition (info, r_type, h == NULL);
2625 tls_type = GOT_UNKNOWN;
2626 if (h == NULL && local_got_offsets)
2627 tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
2628 else if (h != NULL)
2630 tls_type = elf_s390_hash_entry(h)->tls_type;
2631 if (!info->shared && h->dynindx == -1 && tls_type >= GOT_TLS_IE)
2632 r_type = R_390_TLS_LE32;
2634 if (r_type == R_390_TLS_GD32 && tls_type >= GOT_TLS_IE)
2635 r_type = R_390_TLS_IE32;
2637 if (r_type == R_390_TLS_LE32)
2639 /* This relocation gets optimized away by the local exec
2640 access optimization. */
2641 BFD_ASSERT (! unresolved_reloc);
2642 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2643 contents + rel->r_offset);
2644 continue;
2647 if (htab->sgot == NULL)
2648 abort ();
2650 if (h != NULL)
2651 off = h->got.offset;
2652 else
2654 if (local_got_offsets == NULL)
2655 abort ();
2657 off = local_got_offsets[r_symndx];
2660 emit_tls_relocs:
2662 if ((off & 1) != 0)
2663 off &= ~1;
2664 else
2666 Elf_Internal_Rela outrel;
2667 bfd_byte *loc;
2668 int dr_type, indx;
2670 if (htab->srelgot == NULL)
2671 abort ();
2673 outrel.r_offset = (htab->sgot->output_section->vma
2674 + htab->sgot->output_offset + off);
2676 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2677 if (r_type == R_390_TLS_GD32)
2678 dr_type = R_390_TLS_DTPMOD;
2679 else
2680 dr_type = R_390_TLS_TPOFF;
2681 if (dr_type == R_390_TLS_TPOFF && indx == 0)
2682 outrel.r_addend = relocation - dtpoff_base (info);
2683 else
2684 outrel.r_addend = 0;
2685 outrel.r_info = ELF32_R_INFO (indx, dr_type);
2686 loc = htab->srelgot->contents;
2687 loc += htab->srelgot->reloc_count++
2688 * sizeof (Elf32_External_Rela);
2689 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2691 if (r_type == R_390_TLS_GD32)
2693 if (indx == 0)
2695 BFD_ASSERT (! unresolved_reloc);
2696 bfd_put_32 (output_bfd,
2697 relocation - dtpoff_base (info),
2698 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2700 else
2702 outrel.r_info = ELF32_R_INFO (indx, R_390_TLS_DTPOFF);
2703 outrel.r_offset += GOT_ENTRY_SIZE;
2704 outrel.r_addend = 0;
2705 htab->srelgot->reloc_count++;
2706 loc += sizeof (Elf32_External_Rela);
2707 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2711 if (h != NULL)
2712 h->got.offset |= 1;
2713 else
2714 local_got_offsets[r_symndx] |= 1;
2717 if (off >= (bfd_vma) -2)
2718 abort ();
2719 if (r_type == ELF32_R_TYPE (rel->r_info))
2721 relocation = htab->sgot->output_offset + off;
2722 if (r_type == R_390_TLS_IE32 || r_type == R_390_TLS_IEENT)
2723 relocation += htab->sgot->output_section->vma;
2724 unresolved_reloc = FALSE;
2726 else
2728 bfd_put_32 (output_bfd, htab->sgot->output_offset + off,
2729 contents + rel->r_offset);
2730 continue;
2732 break;
2734 case R_390_TLS_GOTIE12:
2735 case R_390_TLS_IEENT:
2736 if (h == NULL)
2738 if (local_got_offsets == NULL)
2739 abort();
2740 off = local_got_offsets[r_symndx];
2741 if (info->shared)
2742 goto emit_tls_relocs;
2744 else
2746 off = h->got.offset;
2747 tls_type = elf_s390_hash_entry(h)->tls_type;
2748 if (info->shared || h->dynindx != -1 || tls_type < GOT_TLS_IE)
2749 goto emit_tls_relocs;
2752 if (htab->sgot == NULL)
2753 abort ();
2755 BFD_ASSERT (! unresolved_reloc);
2756 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2757 htab->sgot->contents + off);
2758 relocation = htab->sgot->output_offset + off;
2759 if (r_type == R_390_TLS_IEENT)
2760 relocation += htab->sgot->output_section->vma;
2761 unresolved_reloc = FALSE;
2762 break;
2764 case R_390_TLS_LDM32:
2765 if (! info->shared)
2766 /* The literal pool entry this relocation refers to gets ignored
2767 by the optimized code of the local exec model. Do nothing
2768 and the value will turn out zero. */
2769 continue;
2771 if (htab->sgot == NULL)
2772 abort ();
2774 off = htab->tls_ldm_got.offset;
2775 if (off & 1)
2776 off &= ~1;
2777 else
2779 Elf_Internal_Rela outrel;
2780 bfd_byte *loc;
2782 if (htab->srelgot == NULL)
2783 abort ();
2785 outrel.r_offset = (htab->sgot->output_section->vma
2786 + htab->sgot->output_offset + off);
2788 bfd_put_32 (output_bfd, 0,
2789 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2790 outrel.r_info = ELF32_R_INFO (0, R_390_TLS_DTPMOD);
2791 outrel.r_addend = 0;
2792 loc = htab->srelgot->contents;
2793 loc += htab->srelgot->reloc_count++
2794 * sizeof (Elf32_External_Rela);
2795 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2796 htab->tls_ldm_got.offset |= 1;
2798 relocation = htab->sgot->output_offset + off;
2799 unresolved_reloc = FALSE;
2800 break;
2802 case R_390_TLS_LE32:
2803 if (info->shared)
2805 /* Linking a shared library with non-fpic code requires
2806 a R_390_TLS_TPOFF relocation. */
2807 Elf_Internal_Rela outrel;
2808 asection *sreloc;
2809 bfd_byte *loc;
2810 int indx;
2812 outrel.r_offset = rel->r_offset
2813 + input_section->output_section->vma
2814 + input_section->output_offset;
2815 if (h != NULL && h->dynindx != -1)
2816 indx = h->dynindx;
2817 else
2818 indx = 0;
2819 outrel.r_info = ELF32_R_INFO (indx, R_390_TLS_TPOFF);
2820 if (indx == 0)
2821 outrel.r_addend = relocation - dtpoff_base (info);
2822 else
2823 outrel.r_addend = 0;
2824 sreloc = elf_section_data (input_section)->sreloc;
2825 if (sreloc == NULL)
2826 abort ();
2827 loc = sreloc->contents;
2828 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2829 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2831 else
2833 BFD_ASSERT (! unresolved_reloc);
2834 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2835 contents + rel->r_offset);
2837 continue;
2839 case R_390_TLS_LDO32:
2840 if (info->shared || (input_section->flags & SEC_CODE) == 0)
2841 relocation -= dtpoff_base (info);
2842 else
2843 /* When converting LDO to LE, we must negate. */
2844 relocation = -tpoff (info, relocation);
2845 break;
2847 /* Relocations for tls instructions. */
2848 case R_390_TLS_LOAD:
2849 case R_390_TLS_GDCALL:
2850 case R_390_TLS_LDCALL:
2851 tls_type = GOT_UNKNOWN;
2852 if (h == NULL && local_got_offsets)
2853 tls_type = elf_s390_local_got_tls_type (input_bfd) [r_symndx];
2854 else if (h != NULL)
2855 tls_type = elf_s390_hash_entry(h)->tls_type;
2857 if (tls_type == GOT_TLS_GD)
2858 continue;
2860 if (r_type == R_390_TLS_LOAD)
2862 if (!info->shared && (h == NULL || h->dynindx == -1))
2864 /* IE->LE transition. Four valid cases:
2865 l %rx,0(0,%ry) -> lr %rx,%ry + bcr 0,0
2866 l %rx,0(%ry,0) -> lr %rx,%ry + bcr 0,0
2867 l %rx,0(%ry,%r12) -> lr %rx,%ry + bcr 0,0
2868 l %rx,0(%r12,%ry) -> lr %rx,%ry + bcr 0,0 */
2869 unsigned int insn, ry;
2871 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2872 ry = 0;
2873 if ((insn & 0xff00f000) == 0x58000000)
2874 /* l %rx,0(%ry,0) -> lr %rx,%ry + bcr 0,0 */
2875 ry = (insn & 0x000f0000);
2876 else if ((insn & 0xff0f0000) == 0x58000000)
2877 /* l %rx,0(0,%ry) -> lr %rx,%ry + bcr 0,0 */
2878 ry = (insn & 0x0000f000) << 4;
2879 else if ((insn & 0xff00f000) == 0x5800c000)
2880 /* l %rx,0(%ry,%r12) -> lr %rx,%ry + bcr 0,0 */
2881 ry = (insn & 0x000f0000);
2882 else if ((insn & 0xff0f0000) == 0x580c0000)
2883 /* l %rx,0(%r12,%ry) -> lr %rx,%ry + bcr 0,0 */
2884 ry = (insn & 0x0000f000) << 4;
2885 else
2886 invalid_tls_insn (input_bfd, input_section, rel);
2887 insn = 0x18000700 | (insn & 0x00f00000) | ry;
2888 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
2891 else if (r_type == R_390_TLS_GDCALL)
2893 unsigned int insn;
2895 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2896 if ((insn & 0xff000fff) != 0x4d000000)
2897 invalid_tls_insn (input_bfd, input_section, rel);
2898 if (!info->shared && (h == NULL || h->dynindx == -1))
2899 /* GD->LE transition.
2900 bas %r14,0(%rx,%r13) -> bc 0,0 */
2901 insn = 0x47000000;
2902 else
2903 /* GD->IE transition.
2904 bas %r14,0(%rx,%r13) -> l %r2,0(%r2,%r12) */
2905 insn = 0x5822c000;
2906 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
2908 else if (r_type == R_390_TLS_LDCALL)
2910 if (!info->shared)
2912 unsigned int insn;
2914 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2915 if ((insn & 0xff000fff) != 0x4d000000)
2916 invalid_tls_insn (input_bfd, input_section, rel);
2917 /* LD->LE transition.
2918 bas %r14,0(%rx,%r13) -> bc 0,0 */
2919 insn = 0x47000000;
2920 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
2923 continue;
2925 default:
2926 break;
2929 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2930 because such sections are not SEC_ALLOC and thus ld.so will
2931 not process them. */
2932 if (unresolved_reloc
2933 && !((input_section->flags & SEC_DEBUGGING) != 0
2934 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2935 (*_bfd_error_handler)
2936 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2937 bfd_archive_filename (input_bfd),
2938 bfd_get_section_name (input_bfd, input_section),
2939 (long) rel->r_offset,
2940 h->root.root.string);
2942 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2943 contents, rel->r_offset,
2944 relocation, rel->r_addend);
2946 if (r != bfd_reloc_ok)
2948 const char *name;
2950 if (h != NULL)
2951 name = h->root.root.string;
2952 else
2954 name = bfd_elf_string_from_elf_section (input_bfd,
2955 symtab_hdr->sh_link,
2956 sym->st_name);
2957 if (name == NULL)
2958 return FALSE;
2959 if (*name == '\0')
2960 name = bfd_section_name (input_bfd, sec);
2963 if (r == bfd_reloc_overflow)
2966 if (! ((*info->callbacks->reloc_overflow)
2967 (info, name, howto->name, (bfd_vma) 0,
2968 input_bfd, input_section, rel->r_offset)))
2969 return FALSE;
2971 else
2973 (*_bfd_error_handler)
2974 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
2975 bfd_archive_filename (input_bfd),
2976 bfd_get_section_name (input_bfd, input_section),
2977 (long) rel->r_offset, name, (int) r);
2978 return FALSE;
2983 return TRUE;
2986 /* Finish up dynamic symbol handling. We set the contents of various
2987 dynamic sections here. */
2989 static bfd_boolean
2990 elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym)
2991 bfd *output_bfd;
2992 struct bfd_link_info *info;
2993 struct elf_link_hash_entry *h;
2994 Elf_Internal_Sym *sym;
2996 struct elf_s390_link_hash_table *htab;
2998 htab = elf_s390_hash_table (info);
3000 if (h->plt.offset != (bfd_vma) -1)
3002 bfd_vma plt_index;
3003 bfd_vma got_offset;
3004 Elf_Internal_Rela rela;
3005 bfd_byte *loc;
3006 bfd_vma relative_offset;
3008 /* This symbol has an entry in the procedure linkage table. Set
3009 it up. */
3010 if (h->dynindx == -1
3011 || htab->splt == NULL
3012 || htab->sgotplt == NULL
3013 || htab->srelplt == NULL)
3014 abort ();
3016 /* Calc. index no.
3017 Current offset - size first entry / entry size. */
3018 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3020 /* Offset in GOT is PLT index plus GOT headers(3) times 4,
3021 addr & GOT addr. */
3022 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3024 /* S390 uses halfwords for relative branch calc! */
3025 relative_offset = - ((PLT_FIRST_ENTRY_SIZE +
3026 (PLT_ENTRY_SIZE * plt_index) + 18) / 2);
3027 /* If offset is > 32768, branch to a previous branch
3028 390 can only handle +-64 K jumps. */
3029 if ( -32768 > (int) relative_offset )
3030 relative_offset
3031 = -(unsigned) (((65536 / PLT_ENTRY_SIZE - 1) * PLT_ENTRY_SIZE) / 2);
3033 /* Fill in the entry in the procedure linkage table. */
3034 if (!info->shared)
3036 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD0,
3037 htab->splt->contents + h->plt.offset);
3038 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD1,
3039 htab->splt->contents + h->plt.offset + 4);
3040 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
3041 htab->splt->contents + h->plt.offset + 8);
3042 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
3043 htab->splt->contents + h->plt.offset + 12);
3044 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD4,
3045 htab->splt->contents + h->plt.offset + 16);
3046 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
3047 htab->splt->contents + h->plt.offset + 20);
3048 bfd_put_32 (output_bfd,
3049 (htab->sgotplt->output_section->vma
3050 + htab->sgotplt->output_offset
3051 + got_offset),
3052 htab->splt->contents + h->plt.offset + 24);
3054 else if (got_offset < 4096)
3056 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD0 + got_offset,
3057 htab->splt->contents + h->plt.offset);
3058 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD1,
3059 htab->splt->contents + h->plt.offset + 4);
3060 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD2,
3061 htab->splt->contents + h->plt.offset + 8);
3062 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD3,
3063 htab->splt->contents + h->plt.offset + 12);
3064 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_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);
3071 else if (got_offset < 32768)
3073 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD0 + got_offset,
3074 htab->splt->contents + h->plt.offset);
3075 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD1,
3076 htab->splt->contents + h->plt.offset + 4);
3077 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD2,
3078 htab->splt->contents + h->plt.offset + 8);
3079 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD3,
3080 htab->splt->contents + h->plt.offset + 12);
3081 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_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, (bfd_vma) 0,
3086 htab->splt->contents + h->plt.offset + 24);
3088 else
3090 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD0,
3091 htab->splt->contents + h->plt.offset);
3092 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD1,
3093 htab->splt->contents + h->plt.offset + 4);
3094 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
3095 htab->splt->contents + h->plt.offset + 8);
3096 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
3097 htab->splt->contents + h->plt.offset + 12);
3098 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
3099 htab->splt->contents + h->plt.offset + 16);
3100 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
3101 htab->splt->contents + h->plt.offset + 20);
3102 bfd_put_32 (output_bfd, got_offset,
3103 htab->splt->contents + h->plt.offset + 24);
3105 /* Insert offset into reloc. table here. */
3106 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
3107 htab->splt->contents + h->plt.offset + 28);
3109 /* Fill in the entry in the global offset table.
3110 Points to instruction after GOT offset. */
3111 bfd_put_32 (output_bfd,
3112 (htab->splt->output_section->vma
3113 + htab->splt->output_offset
3114 + h->plt.offset
3115 + 12),
3116 htab->sgotplt->contents + got_offset);
3118 /* Fill in the entry in the .rela.plt section. */
3119 rela.r_offset = (htab->sgotplt->output_section->vma
3120 + htab->sgotplt->output_offset
3121 + got_offset);
3122 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_JMP_SLOT);
3123 rela.r_addend = 0;
3124 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
3125 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3127 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3129 /* Mark the symbol as undefined, rather than as defined in
3130 the .plt section. Leave the value alone. This is a clue
3131 for the dynamic linker, to make function pointer
3132 comparisons work between an application and shared
3133 library. */
3134 sym->st_shndx = SHN_UNDEF;
3138 if (h->got.offset != (bfd_vma) -1
3139 && elf_s390_hash_entry(h)->tls_type != GOT_TLS_GD
3140 && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE
3141 && elf_s390_hash_entry(h)->tls_type != GOT_TLS_IE_NLT)
3143 Elf_Internal_Rela rela;
3144 bfd_byte *loc;
3146 /* This symbol has an entry in the global offset table. Set it
3147 up. */
3149 if (htab->sgot == NULL || htab->srelgot == NULL)
3150 abort ();
3152 rela.r_offset = (htab->sgot->output_section->vma
3153 + htab->sgot->output_offset
3154 + (h->got.offset &~ (bfd_vma) 1));
3156 /* If this is a static link, or it is a -Bsymbolic link and the
3157 symbol is defined locally or was forced to be local because
3158 of a version file, we just want to emit a RELATIVE reloc.
3159 The entry in the global offset table will already have been
3160 initialized in the relocate_section function. */
3161 if (info->shared
3162 && (info->symbolic
3163 || h->dynindx == -1
3164 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3165 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3167 BFD_ASSERT((h->got.offset & 1) != 0);
3168 rela.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
3169 rela.r_addend = (h->root.u.def.value
3170 + h->root.u.def.section->output_section->vma
3171 + h->root.u.def.section->output_offset);
3173 else
3175 BFD_ASSERT((h->got.offset & 1) == 0);
3176 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgot->contents + h->got.offset);
3177 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_GLOB_DAT);
3178 rela.r_addend = 0;
3181 loc = htab->srelgot->contents;
3182 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3183 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3186 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3188 Elf_Internal_Rela rela;
3189 bfd_byte *loc;
3191 /* This symbols needs a copy reloc. Set it up. */
3193 if (h->dynindx == -1
3194 || (h->root.type != bfd_link_hash_defined
3195 && h->root.type != bfd_link_hash_defweak)
3196 || htab->srelbss == NULL)
3197 abort ();
3199 rela.r_offset = (h->root.u.def.value
3200 + h->root.u.def.section->output_section->vma
3201 + h->root.u.def.section->output_offset);
3202 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_COPY);
3203 rela.r_addend = 0;
3204 loc = htab->srelbss->contents;
3205 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rela);
3206 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3209 /* Mark some specially defined symbols as absolute. */
3210 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3211 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3212 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3213 sym->st_shndx = SHN_ABS;
3215 return TRUE;
3218 /* Used to decide how to sort relocs in an optimal manner for the
3219 dynamic linker, before writing them out. */
3221 static enum elf_reloc_type_class
3222 elf_s390_reloc_type_class (rela)
3223 const Elf_Internal_Rela *rela;
3225 switch ((int) ELF32_R_TYPE (rela->r_info))
3227 case R_390_RELATIVE:
3228 return reloc_class_relative;
3229 case R_390_JMP_SLOT:
3230 return reloc_class_plt;
3231 case R_390_COPY:
3232 return reloc_class_copy;
3233 default:
3234 return reloc_class_normal;
3238 /* Finish up the dynamic sections. */
3240 static bfd_boolean
3241 elf_s390_finish_dynamic_sections (output_bfd, info)
3242 bfd *output_bfd;
3243 struct bfd_link_info *info;
3245 struct elf_s390_link_hash_table *htab;
3246 bfd *dynobj;
3247 asection *sdyn;
3249 htab = elf_s390_hash_table (info);
3250 dynobj = htab->elf.dynobj;
3251 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3253 if (htab->elf.dynamic_sections_created)
3255 Elf32_External_Dyn *dyncon, *dynconend;
3257 if (sdyn == NULL || htab->sgot == NULL)
3258 abort ();
3260 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3261 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3262 for (; dyncon < dynconend; dyncon++)
3264 Elf_Internal_Dyn dyn;
3265 asection *s;
3267 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3269 switch (dyn.d_tag)
3271 default:
3272 continue;
3274 case DT_PLTGOT:
3275 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
3276 break;
3278 case DT_JMPREL:
3279 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3280 break;
3282 case DT_PLTRELSZ:
3283 s = htab->srelplt->output_section;
3284 if (s->_cooked_size != 0)
3285 dyn.d_un.d_val = s->_cooked_size;
3286 else
3287 dyn.d_un.d_val = s->_raw_size;
3288 break;
3291 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3294 /* Fill in the special first entry in the procedure linkage table. */
3295 if (htab->splt && htab->splt->_raw_size > 0)
3297 memset (htab->splt->contents, 0, PLT_FIRST_ENTRY_SIZE);
3298 if (info->shared)
3300 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD0,
3301 htab->splt->contents );
3302 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD1,
3303 htab->splt->contents +4 );
3304 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD2,
3305 htab->splt->contents +8 );
3306 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD3,
3307 htab->splt->contents +12 );
3308 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD4,
3309 htab->splt->contents +16 );
3311 else
3313 bfd_put_32 (output_bfd, (bfd_vma)PLT_FIRST_ENTRY_WORD0,
3314 htab->splt->contents );
3315 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD1,
3316 htab->splt->contents +4 );
3317 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD2,
3318 htab->splt->contents +8 );
3319 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD3,
3320 htab->splt->contents +12 );
3321 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD4,
3322 htab->splt->contents +16 );
3323 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD5,
3324 htab->splt->contents +20 );
3325 bfd_put_32 (output_bfd,
3326 htab->sgotplt->output_section->vma
3327 + htab->sgotplt->output_offset,
3328 htab->splt->contents + 24);
3330 elf_section_data (htab->splt->output_section)
3331 ->this_hdr.sh_entsize = 4;
3336 if (htab->sgotplt)
3338 /* Fill in the first three entries in the global offset table. */
3339 if (htab->sgotplt->_raw_size > 0)
3341 bfd_put_32 (output_bfd,
3342 (sdyn == NULL ? (bfd_vma) 0
3343 : sdyn->output_section->vma + sdyn->output_offset),
3344 htab->sgotplt->contents);
3345 /* One entry for shared object struct ptr. */
3346 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 4);
3347 /* One entry for _dl_runtime_resolve. */
3348 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8);
3351 elf_section_data (htab->sgotplt->output_section)
3352 ->this_hdr.sh_entsize = 4;
3354 return TRUE;
3357 static bfd_boolean
3358 elf_s390_grok_prstatus (abfd, note)
3359 bfd * abfd;
3360 Elf_Internal_Note * note;
3362 int offset;
3363 unsigned int raw_size;
3365 switch (note->descsz)
3367 default:
3368 return FALSE;
3370 case 224: /* S/390 Linux. */
3371 /* pr_cursig */
3372 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
3374 /* pr_pid */
3375 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
3377 /* pr_reg */
3378 offset = 72;
3379 raw_size = 144;
3380 break;
3383 /* Make a ".reg/999" section. */
3384 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3385 raw_size, note->descpos + offset);
3388 #define TARGET_BIG_SYM bfd_elf32_s390_vec
3389 #define TARGET_BIG_NAME "elf32-s390"
3390 #define ELF_ARCH bfd_arch_s390
3391 #define ELF_MACHINE_CODE EM_S390
3392 #define ELF_MACHINE_ALT1 EM_S390_OLD
3393 #define ELF_MAXPAGESIZE 0x1000
3395 #define elf_backend_can_gc_sections 1
3396 #define elf_backend_can_refcount 1
3397 #define elf_backend_want_got_plt 1
3398 #define elf_backend_plt_readonly 1
3399 #define elf_backend_want_plt_sym 0
3400 #define elf_backend_got_header_size 12
3401 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
3402 #define elf_backend_rela_normal 1
3404 #define elf_info_to_howto elf_s390_info_to_howto
3406 #define bfd_elf32_bfd_is_local_label_name elf_s390_is_local_label_name
3407 #define bfd_elf32_bfd_link_hash_table_create elf_s390_link_hash_table_create
3408 #define bfd_elf32_bfd_reloc_type_lookup elf_s390_reloc_type_lookup
3410 #define elf_backend_adjust_dynamic_symbol elf_s390_adjust_dynamic_symbol
3411 #define elf_backend_check_relocs elf_s390_check_relocs
3412 #define elf_backend_copy_indirect_symbol elf_s390_copy_indirect_symbol
3413 #define elf_backend_create_dynamic_sections elf_s390_create_dynamic_sections
3414 #define elf_backend_finish_dynamic_sections elf_s390_finish_dynamic_sections
3415 #define elf_backend_finish_dynamic_symbol elf_s390_finish_dynamic_symbol
3416 #define elf_backend_gc_mark_hook elf_s390_gc_mark_hook
3417 #define elf_backend_gc_sweep_hook elf_s390_gc_sweep_hook
3418 #define elf_backend_reloc_type_class elf_s390_reloc_type_class
3419 #define elf_backend_relocate_section elf_s390_relocate_section
3420 #define elf_backend_size_dynamic_sections elf_s390_size_dynamic_sections
3421 #define elf_backend_reloc_type_class elf_s390_reloc_type_class
3422 #define elf_backend_grok_prstatus elf_s390_grok_prstatus
3424 #define bfd_elf32_mkobject elf_s390_mkobject
3425 #define elf_backend_object_p elf_s390_object_p
3427 #include "elf32-target.h"