* aix-thread.c (special_register_p, supply_sprs64, supply_sprs32)
[deliverable/binutils-gdb.git] / bfd / elf32-s390.c
CommitLineData
a85d7ed0 1/* IBM S/390-specific support for 32-bit ELF
e92d460e 2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
a85d7ed0
NC
3 Contributed by Carl B. Pedersen and Martin Schwidefsky.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22#include "bfd.h"
23#include "sysdep.h"
24#include "bfdlink.h"
25#include "libbfd.h"
26#include "elf-bfd.h"
27
28static reloc_howto_type *elf_s390_reloc_type_lookup
29 PARAMS ((bfd *, bfd_reloc_code_real_type));
30static void elf_s390_info_to_howto
31 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
0451c93c
MS
32static boolean elf_s390_is_local_label_name
33 PARAMS ((bfd *, const char *));
34static struct bfd_hash_entry *link_hash_newfunc
a85d7ed0
NC
35 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
36static struct bfd_link_hash_table *elf_s390_link_hash_table_create
37 PARAMS ((bfd *));
0451c93c
MS
38static boolean create_got_section
39 PARAMS((bfd *, struct bfd_link_info *));
40static boolean elf_s390_create_dynamic_sections
41 PARAMS((bfd *, struct bfd_link_info *));
42static void elf_s390_copy_indirect_symbol
43 PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
a85d7ed0
NC
44static boolean elf_s390_check_relocs
45 PARAMS ((bfd *, struct bfd_link_info *, asection *,
46 const Elf_Internal_Rela *));
5a65713f 47static asection *elf_s390_gc_mark_hook
1e2f5b6e 48 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
0451c93c 49 struct elf_link_hash_entry *, Elf_Internal_Sym *));
5a65713f
AJ
50static boolean elf_s390_gc_sweep_hook
51 PARAMS ((bfd *, struct bfd_link_info *, asection *,
52 const Elf_Internal_Rela *));
a85d7ed0
NC
53static boolean elf_s390_adjust_dynamic_symbol
54 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
0451c93c
MS
55static boolean allocate_dynrelocs
56 PARAMS ((struct elf_link_hash_entry *, PTR));
57static boolean readonly_dynrelocs
58 PARAMS ((struct elf_link_hash_entry *, PTR));
a85d7ed0
NC
59static boolean elf_s390_size_dynamic_sections
60 PARAMS ((bfd *, struct bfd_link_info *));
61static boolean elf_s390_relocate_section
62 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
63 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
64static boolean elf_s390_finish_dynamic_symbol
65 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
66 Elf_Internal_Sym *));
0451c93c
MS
67static enum elf_reloc_type_class elf_s390_reloc_type_class
68 PARAMS ((const Elf_Internal_Rela *));
a85d7ed0
NC
69static boolean elf_s390_finish_dynamic_sections
70 PARAMS ((bfd *, struct bfd_link_info *));
5a65713f 71static boolean elf_s390_object_p PARAMS ((bfd *));
248866a8 72static boolean elf_s390_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
a85d7ed0 73
a85d7ed0
NC
74#include "elf/s390.h"
75
76/* The relocation "howto" table. */
77
78static reloc_howto_type elf_howto_table[] =
79{
80 HOWTO (R_390_NONE, /* type */
81 0, /* rightshift */
82 0, /* size (0 = byte, 1 = short, 2 = long) */
83 0, /* bitsize */
84 false, /* pc_relative */
85 0, /* bitpos */
86 complain_overflow_dont, /* complain_on_overflow */
87 bfd_elf_generic_reloc, /* special_function */
88 "R_390_NONE", /* name */
89 false, /* partial_inplace */
90 0, /* src_mask */
91 0, /* dst_mask */
92 false), /* pcrel_offset */
93
94 HOWTO(R_390_8, 0, 0, 8, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_8", false, 0,0x000000ff, false),
95 HOWTO(R_390_12, 0, 1, 12, false, 0, complain_overflow_dont, bfd_elf_generic_reloc, "R_390_12", false, 0,0x00000fff, false),
96 HOWTO(R_390_16, 0, 1, 16, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_16", false, 0,0x0000ffff, false),
97 HOWTO(R_390_32, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_32", false, 0,0xffffffff, false),
98 HOWTO(R_390_PC32, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC32", false, 0,0xffffffff, true),
99 HOWTO(R_390_GOT12, 0, 1, 12, false, 0, complain_overflow_dont, bfd_elf_generic_reloc, "R_390_GOT12", false, 0,0x00000fff, false),
100 HOWTO(R_390_GOT32, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT32", false, 0,0xffffffff, false),
101 HOWTO(R_390_PLT32, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT32", false, 0,0xffffffff, true),
102 HOWTO(R_390_COPY, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_COPY", false, 0,0xffffffff, false),
103 HOWTO(R_390_GLOB_DAT, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GLOB_DAT",false, 0,0xffffffff, false),
104 HOWTO(R_390_JMP_SLOT, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_JMP_SLOT",false, 0,0xffffffff, false),
105 HOWTO(R_390_RELATIVE, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_RELATIVE",false, 0,0xffffffff, false),
106 HOWTO(R_390_GOTOFF, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTOFF", false, 0,0xffffffff, false),
107 HOWTO(R_390_GOTPC, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTPC", false, 0,0xffffffff, true),
108 HOWTO(R_390_GOT16, 0, 1, 16, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT16", false, 0,0x0000ffff, false),
109 HOWTO(R_390_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16", false, 0,0x0000ffff, true),
110 HOWTO(R_390_PC16DBL, 1, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16DBL", false, 0,0x0000ffff, true),
111 HOWTO(R_390_PLT16DBL, 1, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT16DBL", false, 0,0x0000ffff, true),
befc3abb
MS
112 HOWTO(R_390_PC32DBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC32DBL", false, 0,0xffffffff, true),
113 HOWTO(R_390_PLT32DBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT32DBL", false, 0,0xffffffff, true),
114 HOWTO(R_390_GOTPCDBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTPCDBL", false, 0,0xffffffff, true),
115 HOWTO(R_390_GOTENT, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTENT", false, 0,0xffffffff, true),
a85d7ed0
NC
116};
117
118/* GNU extension to record C++ vtable hierarchy. */
119static reloc_howto_type elf32_s390_vtinherit_howto =
120 HOWTO (R_390_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", false,0, 0, false);
121static reloc_howto_type elf32_s390_vtentry_howto =
dc810e39 122 HOWTO (R_390_GNU_VTENTRY, 0,2,0,false,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_390_GNU_VTENTRY", false,0,0, false);
a85d7ed0
NC
123
124static reloc_howto_type *
125elf_s390_reloc_type_lookup (abfd, code)
126 bfd *abfd ATTRIBUTE_UNUSED;
127 bfd_reloc_code_real_type code;
128{
ec338859 129 switch (code)
0451c93c
MS
130 {
131 case BFD_RELOC_NONE:
132 return &elf_howto_table[(int) R_390_NONE];
133 case BFD_RELOC_8:
134 return &elf_howto_table[(int) R_390_8];
135 case BFD_RELOC_390_12:
136 return &elf_howto_table[(int) R_390_12];
137 case BFD_RELOC_16:
138 return &elf_howto_table[(int) R_390_16];
139 case BFD_RELOC_32:
140 return &elf_howto_table[(int) R_390_32];
141 case BFD_RELOC_CTOR:
142 return &elf_howto_table[(int) R_390_32];
143 case BFD_RELOC_32_PCREL:
144 return &elf_howto_table[(int) R_390_PC32];
145 case BFD_RELOC_390_GOT12:
146 return &elf_howto_table[(int) R_390_GOT12];
147 case BFD_RELOC_32_GOT_PCREL:
148 return &elf_howto_table[(int) R_390_GOT32];
149 case BFD_RELOC_390_PLT32:
150 return &elf_howto_table[(int) R_390_PLT32];
151 case BFD_RELOC_390_COPY:
152 return &elf_howto_table[(int) R_390_COPY];
153 case BFD_RELOC_390_GLOB_DAT:
154 return &elf_howto_table[(int) R_390_GLOB_DAT];
155 case BFD_RELOC_390_JMP_SLOT:
156 return &elf_howto_table[(int) R_390_JMP_SLOT];
157 case BFD_RELOC_390_RELATIVE:
158 return &elf_howto_table[(int) R_390_RELATIVE];
159 case BFD_RELOC_32_GOTOFF:
160 return &elf_howto_table[(int) R_390_GOTOFF];
161 case BFD_RELOC_390_GOTPC:
162 return &elf_howto_table[(int) R_390_GOTPC];
163 case BFD_RELOC_390_GOT16:
164 return &elf_howto_table[(int) R_390_GOT16];
165 case BFD_RELOC_16_PCREL:
166 return &elf_howto_table[(int) R_390_PC16];
167 case BFD_RELOC_390_PC16DBL:
168 return &elf_howto_table[(int) R_390_PC16DBL];
169 case BFD_RELOC_390_PLT16DBL:
170 return &elf_howto_table[(int) R_390_PLT16DBL];
befc3abb
MS
171 case BFD_RELOC_390_PC32DBL:
172 return &elf_howto_table[(int) R_390_PC32DBL];
173 case BFD_RELOC_390_PLT32DBL:
174 return &elf_howto_table[(int) R_390_PLT32DBL];
175 case BFD_RELOC_390_GOTPCDBL:
176 return &elf_howto_table[(int) R_390_GOTPCDBL];
177 case BFD_RELOC_390_GOTENT:
178 return &elf_howto_table[(int) R_390_GOTENT];
0451c93c
MS
179 case BFD_RELOC_VTABLE_INHERIT:
180 return &elf32_s390_vtinherit_howto;
181 case BFD_RELOC_VTABLE_ENTRY:
182 return &elf32_s390_vtentry_howto;
183 default:
184 break;
185 }
a85d7ed0
NC
186 return 0;
187}
188
189/* We need to use ELF32_R_TYPE so we have our own copy of this function,
190 and elf32-s390.c has its own copy. */
191
192static void
193elf_s390_info_to_howto (abfd, cache_ptr, dst)
194 bfd *abfd ATTRIBUTE_UNUSED;
195 arelent *cache_ptr;
196 Elf_Internal_Rela *dst;
197{
198 switch (ELF32_R_TYPE(dst->r_info))
199 {
200 case R_390_GNU_VTINHERIT:
201 cache_ptr->howto = &elf32_s390_vtinherit_howto;
202 break;
203
204 case R_390_GNU_VTENTRY:
205 cache_ptr->howto = &elf32_s390_vtentry_howto;
206 break;
207
208 default:
209 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_390_max);
210 cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
dc810e39 211 }
a85d7ed0
NC
212}
213
214static boolean
215elf_s390_is_local_label_name (abfd, name)
216 bfd *abfd;
217 const char *name;
218{
219 if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L'))
220 return true;
221
222 return _bfd_elf_is_local_label_name (abfd, name);
223}
224
225/* Functions for the 390 ELF linker. */
226
227/* The name of the dynamic interpreter. This is put in the .interp
228 section. */
229
230#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
231
a85d7ed0
NC
232/* The size in bytes of the first entry in the procedure linkage table. */
233#define PLT_FIRST_ENTRY_SIZE 32
234/* The size in bytes of an entry in the procedure linkage table. */
dc810e39 235#define PLT_ENTRY_SIZE 32
a85d7ed0
NC
236
237#define GOT_ENTRY_SIZE 4
238
239/* The first three entries in a procedure linkage table are reserved,
240 and the initial contents are unimportant (we zero them out).
241 Subsequent entries look like this. See the SVR4 ABI 386
242 supplement to see how this works. */
243
244/* For the s390, simple addr offset can only be 0 - 4096.
245 To use the full 2 GB address space, several instructions
246 are needed to load an address in a register and execute
247 a branch( or just saving the address)
248
dc810e39 249 Furthermore, only r 0 and 1 are free to use!!! */
a85d7ed0
NC
250
251/* The first 3 words in the GOT are then reserved.
252 Word 0 is the address of the dynamic table.
253 Word 1 is a pointer to a structure describing the object
254 Word 2 is used to point to the loader entry address.
255
256 The code for position independand PLT entries looks like this:
257
258 r12 holds addr of the current GOT at entry to the PLT
259
260 The GOT holds the address in the PLT to be executed.
261 The loader then gets:
262 24(15) = Pointer to the structure describing the object.
dc810e39 263 28(15) = Offset in symbol table
a85d7ed0
NC
264
265 The loader must then find the module where the function is
266 and insert the address in the GOT.
267
268 Note: 390 can only address +- 64 K relative.
269 We check if offset > 65536, then make a relative branch -64xxx
270 back to a previous defined branch
271
272PLT1: BASR 1,0 # 2 bytes
273 L 1,22(1) # 4 bytes Load offset in GOT in r 1
274 L 1,(1,12) # 4 bytes Load address from GOT in r1
275 BCR 15,1 # 2 bytes Jump to address
276RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
277 L 1,14(1) # 4 bytes Load offset in symol table in r1
278 BRC 15,-x # 4 bytes Jump to start of PLT
279 .word 0 # 2 bytes filler
280 .long ? # 4 bytes offset in GOT
281 .long ? # 4 bytes offset into symbol table
282
283 This was the general case. There are two additional, optimizes PLT
284 definitions. One for GOT offsets < 4096 and one for GOT offsets < 32768.
285 First the one for GOT offsets < 4096:
286
287PLT1: L 1,<offset>(12) # 4 bytes Load address from GOT in R1
288 BCR 15,1 # 2 bytes Jump to address
289 .word 0,0,0 # 6 bytes filler
290RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
291 L 1,14(1) # 4 bytes Load offset in symbol table in r1
292 BRC 15,-x # 4 bytes Jump to start of PLT
293 .word 0,0,0 # 6 bytes filler
294 .long ? # 4 bytes offset into symbol table
295
296 Second the one for GOT offsets < 32768:
297
298PLT1: LHI 1,<offset> # 4 bytes Load offset in GOT to r1
299 L 1,(1,12) # 4 bytes Load address from GOT to r1
300 BCR 15,1 # 2 bytes Jump to address
301 .word 0 # 2 bytes filler
302RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
303 L 1,14(1) # 4 bytes Load offset in symbol table in r1
304 BRC 15,-x # 4 bytes Jump to start of PLT
305 .word 0,0,0 # 6 bytes filler
306 .long ? # 4 bytes offset into symbol table
307
308Total = 32 bytes per PLT entry
309
310 The code for static build PLT entries looks like this:
311
312PLT1: BASR 1,0 # 2 bytes
313 L 1,22(1) # 4 bytes Load address of GOT entry
314 L 1,0(0,1) # 4 bytes Load address from GOT in r1
315 BCR 15,1 # 2 bytes Jump to address
316RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
317 L 1,14(1) # 4 bytes Load offset in symbol table in r1
318 BRC 15,-x # 4 bytes Jump to start of PLT
319 .word 0 # 2 bytes filler
320 .long ? # 4 bytes address of GOT entry
321 .long ? # 4 bytes offset into symbol table */
322
323#define PLT_PIC_ENTRY_WORD0 0x0d105810
324#define PLT_PIC_ENTRY_WORD1 0x10165811
325#define PLT_PIC_ENTRY_WORD2 0xc00007f1
326#define PLT_PIC_ENTRY_WORD3 0x0d105810
327#define PLT_PIC_ENTRY_WORD4 0x100ea7f4
328
329#define PLT_PIC12_ENTRY_WORD0 0x5810c000
330#define PLT_PIC12_ENTRY_WORD1 0x07f10000
331#define PLT_PIC12_ENTRY_WORD2 0x00000000
332#define PLT_PIC12_ENTRY_WORD3 0x0d105810
333#define PLT_PIC12_ENTRY_WORD4 0x100ea7f4
334
335#define PLT_PIC16_ENTRY_WORD0 0xa7180000
336#define PLT_PIC16_ENTRY_WORD1 0x5811c000
337#define PLT_PIC16_ENTRY_WORD2 0x07f10000
338#define PLT_PIC16_ENTRY_WORD3 0x0d105810
339#define PLT_PIC16_ENTRY_WORD4 0x100ea7f4
340
341#define PLT_ENTRY_WORD0 0x0d105810
342#define PLT_ENTRY_WORD1 0x10165810
343#define PLT_ENTRY_WORD2 0x100007f1
344#define PLT_ENTRY_WORD3 0x0d105810
345#define PLT_ENTRY_WORD4 0x100ea7f4
346
347/* The first PLT entry pushes the offset into the symbol table
348 from R1 onto the stack at 8(15) and the loader object info
349 at 12(15), loads the loader address in R1 and jumps to it. */
350
351/* The first entry in the PLT for PIC code:
352
353PLT0:
354 ST 1,28(15) # R1 has offset into symbol table
355 L 1,4(12) # Get loader ino(object struct address)
dc810e39 356 ST 1,24(15) # Store address
a85d7ed0
NC
357 L 1,8(12) # Entry address of loader in R1
358 BR 1 # Jump to loader
359
360 The first entry in the PLT for static code:
361
362PLT0:
363 ST 1,28(15) # R1 has offset into symbol table
364 BASR 1,0
365 L 1,18(0,1) # Get address of GOT
366 MVC 24(4,15),4(1) # Move loader ino to stack
367 L 1,8(1) # Get address of loader
368 BR 1 # Jump to loader
369 .word 0 # filler
370 .long got # address of GOT */
371
372#define PLT_PIC_FIRST_ENTRY_WORD0 0x5010f01c
373#define PLT_PIC_FIRST_ENTRY_WORD1 0x5810c004
374#define PLT_PIC_FIRST_ENTRY_WORD2 0x5010f018
375#define PLT_PIC_FIRST_ENTRY_WORD3 0x5810c008
376#define PLT_PIC_FIRST_ENTRY_WORD4 0x07f10000
377
378#define PLT_FIRST_ENTRY_WORD0 0x5010f01c
379#define PLT_FIRST_ENTRY_WORD1 0x0d105810
380#define PLT_FIRST_ENTRY_WORD2 0x1012D203
381#define PLT_FIRST_ENTRY_WORD3 0xf0181004
382#define PLT_FIRST_ENTRY_WORD4 0x58101008
383#define PLT_FIRST_ENTRY_WORD5 0x07f10000
384
385/* The s390 linker needs to keep track of the number of relocs that it
0451c93c
MS
386 decides to copy as dynamic relocs in check_relocs for each symbol.
387 This is so that it can later discard them if they are found to be
388 unnecessary. We store the information in a field extending the
389 regular ELF linker hash table. */
a85d7ed0 390
0451c93c 391struct elf_s390_dyn_relocs
a85d7ed0 392{
0451c93c
MS
393 struct elf_s390_dyn_relocs *next;
394
395 /* The input section of the reloc. */
396 asection *sec;
397
398 /* Total number of relocs copied for the input section. */
a85d7ed0 399 bfd_size_type count;
0451c93c
MS
400
401 /* Number of pc-relative relocs copied for the input section. */
402 bfd_size_type pc_count;
a85d7ed0
NC
403};
404
405/* s390 ELF linker hash entry. */
406
407struct elf_s390_link_hash_entry
408{
0451c93c 409 struct elf_link_hash_entry elf;
a85d7ed0 410
0451c93c
MS
411 /* Track dynamic relocs copied for this symbol. */
412 struct elf_s390_dyn_relocs *dyn_relocs;
a85d7ed0
NC
413};
414
415/* s390 ELF linker hash table. */
416
417struct elf_s390_link_hash_table
418{
0451c93c 419 struct elf_link_hash_table elf;
a85d7ed0 420
0451c93c
MS
421 /* Short-cuts to get to dynamic linker sections. */
422 asection *sgot;
423 asection *sgotplt;
424 asection *srelgot;
425 asection *splt;
426 asection *srelplt;
427 asection *sdynbss;
428 asection *srelbss;
ec338859
AM
429
430 /* Small local sym to section mapping cache. */
431 struct sym_sec_cache sym_sec;
0451c93c 432};
a85d7ed0
NC
433
434/* Get the s390 ELF linker hash table from a link_info structure. */
435
436#define elf_s390_hash_table(p) \
437 ((struct elf_s390_link_hash_table *) ((p)->hash))
438
439/* Create an entry in an s390 ELF linker hash table. */
440
441static struct bfd_hash_entry *
0451c93c 442link_hash_newfunc (entry, table, string)
a85d7ed0
NC
443 struct bfd_hash_entry *entry;
444 struct bfd_hash_table *table;
445 const char *string;
446{
a85d7ed0
NC
447 /* Allocate the structure if it has not already been allocated by a
448 subclass. */
0451c93c
MS
449 if (entry == NULL)
450 {
451 entry = bfd_hash_allocate (table,
452 sizeof (struct elf_s390_link_hash_entry));
453 if (entry == NULL)
454 return entry;
455 }
ec338859 456
a85d7ed0 457 /* Call the allocation method of the superclass. */
0451c93c
MS
458 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
459 if (entry != NULL)
a85d7ed0 460 {
0451c93c 461 struct elf_s390_link_hash_entry *eh;
ec338859 462
0451c93c
MS
463 eh = (struct elf_s390_link_hash_entry *) entry;
464 eh->dyn_relocs = NULL;
a85d7ed0
NC
465 }
466
0451c93c 467 return entry;
a85d7ed0
NC
468}
469
470/* Create an s390 ELF linker hash table. */
471
472static struct bfd_link_hash_table *
473elf_s390_link_hash_table_create (abfd)
474 bfd *abfd;
475{
476 struct elf_s390_link_hash_table *ret;
dc810e39 477 bfd_size_type amt = sizeof (struct elf_s390_link_hash_table);
a85d7ed0 478
e2d34d7d 479 ret = (struct elf_s390_link_hash_table *) bfd_malloc (amt);
0451c93c 480 if (ret == NULL)
a85d7ed0
NC
481 return NULL;
482
0451c93c 483 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
a85d7ed0 484 {
e2d34d7d 485 free (ret);
a85d7ed0
NC
486 return NULL;
487 }
488
0451c93c
MS
489 ret->sgot = NULL;
490 ret->sgotplt = NULL;
491 ret->srelgot = NULL;
492 ret->splt = NULL;
493 ret->srelplt = NULL;
494 ret->sdynbss = NULL;
495 ret->srelbss = NULL;
ec338859 496 ret->sym_sec.abfd = NULL;
0451c93c
MS
497
498 return &ret->elf.root;
499}
500
501/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
502 shortcuts to them in our hash table. */
503
504static boolean
505create_got_section (dynobj, info)
506 bfd *dynobj;
507 struct bfd_link_info *info;
508{
509 struct elf_s390_link_hash_table *htab;
510
511 if (! _bfd_elf_create_got_section (dynobj, info))
512 return false;
513
514 htab = elf_s390_hash_table (info);
515 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
516 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
517 if (!htab->sgot || !htab->sgotplt)
518 abort ();
519
520 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
521 if (htab->srelgot == NULL
522 || ! bfd_set_section_flags (dynobj, htab->srelgot,
523 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
524 | SEC_IN_MEMORY | SEC_LINKER_CREATED
525 | SEC_READONLY))
526 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
527 return false;
528 return true;
529}
530
531/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
532 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
533 hash table. */
534
535static boolean
536elf_s390_create_dynamic_sections (dynobj, info)
537 bfd *dynobj;
538 struct bfd_link_info *info;
539{
540 struct elf_s390_link_hash_table *htab;
541
542 htab = elf_s390_hash_table (info);
543 if (!htab->sgot && !create_got_section (dynobj, info))
544 return false;
545
546 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
547 return false;
548
549 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
550 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
551 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
552 if (!info->shared)
553 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
554
555 if (!htab->splt || !htab->srelplt || !htab->sdynbss
556 || (!info->shared && !htab->srelbss))
557 abort ();
558
559 return true;
a85d7ed0
NC
560}
561
0451c93c
MS
562/* Copy the extra info we tack onto an elf_link_hash_entry. */
563
564static void
565elf_s390_copy_indirect_symbol (dir, ind)
566 struct elf_link_hash_entry *dir, *ind;
567{
568 struct elf_s390_link_hash_entry *edir, *eind;
569
570 edir = (struct elf_s390_link_hash_entry *) dir;
571 eind = (struct elf_s390_link_hash_entry *) ind;
572
573 if (eind->dyn_relocs != NULL)
574 {
575 if (edir->dyn_relocs != NULL)
576 {
577 struct elf_s390_dyn_relocs **pp;
578 struct elf_s390_dyn_relocs *p;
579
580 if (ind->root.type == bfd_link_hash_indirect)
581 abort ();
582
583 /* Add reloc counts against the weak sym to the strong sym
584 list. Merge any entries against the same section. */
585 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
586 {
587 struct elf_s390_dyn_relocs *q;
588
589 for (q = edir->dyn_relocs; q != NULL; q = q->next)
590 if (q->sec == p->sec)
591 {
592 q->pc_count += p->pc_count;
593 q->count += p->count;
594 *pp = p->next;
595 break;
596 }
597 if (q == NULL)
598 pp = &p->next;
599 }
600 *pp = edir->dyn_relocs;
601 }
602
603 edir->dyn_relocs = eind->dyn_relocs;
604 eind->dyn_relocs = NULL;
605 }
606
607 _bfd_elf_link_hash_copy_indirect (dir, ind);
608}
a85d7ed0
NC
609
610/* Look through the relocs for a section during the first phase, and
611 allocate space in the global offset table or procedure linkage
612 table. */
613
614static boolean
615elf_s390_check_relocs (abfd, info, sec, relocs)
616 bfd *abfd;
617 struct bfd_link_info *info;
618 asection *sec;
619 const Elf_Internal_Rela *relocs;
620{
0451c93c 621 struct elf_s390_link_hash_table *htab;
a85d7ed0
NC
622 Elf_Internal_Shdr *symtab_hdr;
623 struct elf_link_hash_entry **sym_hashes;
a85d7ed0
NC
624 const Elf_Internal_Rela *rel;
625 const Elf_Internal_Rela *rel_end;
a85d7ed0
NC
626 asection *sreloc;
627
628 if (info->relocateable)
629 return true;
630
0451c93c 631 htab = elf_s390_hash_table (info);
a85d7ed0
NC
632 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
633 sym_hashes = elf_sym_hashes (abfd);
a85d7ed0 634
a85d7ed0
NC
635 sreloc = NULL;
636
637 rel_end = relocs + sec->reloc_count;
638 for (rel = relocs; rel < rel_end; rel++)
639 {
640 unsigned long r_symndx;
641 struct elf_link_hash_entry *h;
642
643 r_symndx = ELF32_R_SYM (rel->r_info);
644
0451c93c
MS
645 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
646 {
647 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
648 bfd_archive_filename (abfd),
649 r_symndx);
650 return false;
651 }
652
a85d7ed0
NC
653 if (r_symndx < symtab_hdr->sh_info)
654 h = NULL;
655 else
dc810e39 656 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
a85d7ed0 657
a85d7ed0
NC
658 switch (ELF32_R_TYPE (rel->r_info))
659 {
660 case R_390_GOT12:
661 case R_390_GOT16:
662 case R_390_GOT32:
befc3abb 663 case R_390_GOTENT:
a85d7ed0 664 /* This symbol requires a global offset table entry. */
a85d7ed0
NC
665 if (h != NULL)
666 {
51b64d56 667 h->got.refcount += 1;
a85d7ed0
NC
668 }
669 else
670 {
0451c93c
MS
671 bfd_signed_vma *local_got_refcounts;
672
a85d7ed0 673 /* This is a global offset table entry for a local symbol. */
0451c93c 674 local_got_refcounts = elf_local_got_refcounts (abfd);
a85d7ed0
NC
675 if (local_got_refcounts == NULL)
676 {
dc810e39 677 bfd_size_type size;
a85d7ed0 678
dc810e39
AM
679 size = symtab_hdr->sh_info;
680 size *= sizeof (bfd_signed_vma);
0451c93c
MS
681 local_got_refcounts = ((bfd_signed_vma *)
682 bfd_zalloc (abfd, size));
a85d7ed0
NC
683 if (local_got_refcounts == NULL)
684 return false;
685 elf_local_got_refcounts (abfd) = local_got_refcounts;
a85d7ed0 686 }
51b64d56 687 local_got_refcounts[r_symndx] += 1;
a85d7ed0 688 }
0451c93c
MS
689 /* Fall through */
690
691 case R_390_GOTOFF:
692 case R_390_GOTPC:
befc3abb 693 case R_390_GOTPCDBL:
0451c93c
MS
694 if (htab->sgot == NULL)
695 {
696 if (htab->elf.dynobj == NULL)
697 htab->elf.dynobj = abfd;
698 if (!create_got_section (htab->elf.dynobj, info))
699 return false;
700 }
a85d7ed0
NC
701 break;
702
703 case R_390_PLT16DBL:
befc3abb 704 case R_390_PLT32DBL:
a85d7ed0
NC
705 case R_390_PLT32:
706 /* This symbol requires a procedure linkage table entry. We
707 actually build the entry in adjust_dynamic_symbol,
708 because this might be a case of linking PIC code which is
709 never referenced by a dynamic object, in which case we
710 don't need to generate a procedure linkage table entry
711 after all. */
712
713 /* If this is a local symbol, we resolve it directly without
714 creating a procedure linkage table entry. */
715 if (h == NULL)
716 continue;
717
51b64d56
AM
718 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
719 h->plt.refcount += 1;
a85d7ed0
NC
720 break;
721
722 case R_390_8:
723 case R_390_16:
724 case R_390_32:
725 case R_390_PC16:
726 case R_390_PC16DBL:
befc3abb 727 case R_390_PC32DBL:
a85d7ed0 728 case R_390_PC32:
0451c93c
MS
729 if (h != NULL && !info->shared)
730 {
731 /* If this reloc is in a read-only section, we might
732 need a copy reloc. We can't check reliably at this
733 stage whether the section is read-only, as input
734 sections have not yet been mapped to output sections.
735 Tentatively set the flag for now, and correct in
736 adjust_dynamic_symbol. */
737 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
ec338859 738
0451c93c
MS
739 /* We may need a .plt entry if the function this reloc
740 refers to is in a shared lib. */
741 h->plt.refcount += 1;
742 }
a85d7ed0
NC
743
744 /* If we are creating a shared library, and this is a reloc
0451c93c
MS
745 against a global symbol, or a non PC relative reloc
746 against a local symbol, then we need to copy the reloc
747 into the shared library. However, if we are linking with
748 -Bsymbolic, we do not need to copy a reloc against a
749 global symbol which is defined in an object we are
750 including in the link (i.e., DEF_REGULAR is set). At
751 this point we have not seen all the input files, so it is
752 possible that DEF_REGULAR is not set now but will be set
753 later (it is never cleared). In case of a weak definition,
754 DEF_REGULAR may be cleared later by a strong definition in
755 a shared library. We account for that possibility below by
756 storing information in the relocs_copied field of the hash
757 table entry. A similar situation occurs when creating
758 shared libraries and symbol visibility changes render the
759 symbol local.
760
761 If on the other hand, we are creating an executable, we
762 may need to keep relocations for symbols satisfied by a
763 dynamic library if we manage to avoid copy relocs for the
764 symbol. */
765 if ((info->shared
766 && (sec->flags & SEC_ALLOC) != 0
ec338859 767 && ((ELF32_R_TYPE (rel->r_info) != R_390_PC16
0451c93c 768 && ELF32_R_TYPE (rel->r_info) != R_390_PC16DBL
befc3abb 769 && ELF32_R_TYPE (rel->r_info) != R_390_PC32DBL
0451c93c
MS
770 && ELF32_R_TYPE (rel->r_info) != R_390_PC32)
771 || (h != NULL
772 && (! info->symbolic
773 || h->root.type == bfd_link_hash_defweak
774 || (h->elf_link_hash_flags
775 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
776 || (!info->shared
777 && (sec->flags & SEC_ALLOC) != 0
778 && h != NULL
779 && (h->root.type == bfd_link_hash_defweak
780 || (h->elf_link_hash_flags
781 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
a85d7ed0 782 {
ec338859
AM
783 struct elf_s390_dyn_relocs *p;
784 struct elf_s390_dyn_relocs **head;
785
0451c93c
MS
786 /* We must copy these reloc types into the output file.
787 Create a reloc section in dynobj and make room for
788 this reloc. */
a85d7ed0
NC
789 if (sreloc == NULL)
790 {
791 const char *name;
0451c93c 792 bfd *dynobj;
ec338859 793
a85d7ed0
NC
794 name = (bfd_elf_string_from_elf_section
795 (abfd,
796 elf_elfheader (abfd)->e_shstrndx,
797 elf_section_data (sec)->rel_hdr.sh_name));
798 if (name == NULL)
799 return false;
800
0451c93c
MS
801 if (strncmp (name, ".rela", 5) != 0
802 || strcmp (bfd_get_section_name (abfd, sec),
803 name + 5) != 0)
804 {
805 (*_bfd_error_handler)
806 (_("%s: bad relocation section name `%s\'"),
807 bfd_archive_filename (abfd), name);
808 }
809
810 if (htab->elf.dynobj == NULL)
811 htab->elf.dynobj = abfd;
a85d7ed0 812
0451c93c 813 dynobj = htab->elf.dynobj;
a85d7ed0
NC
814 sreloc = bfd_get_section_by_name (dynobj, name);
815 if (sreloc == NULL)
816 {
817 flagword flags;
818
819 sreloc = bfd_make_section (dynobj, name);
820 flags = (SEC_HAS_CONTENTS | SEC_READONLY
821 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
822 if ((sec->flags & SEC_ALLOC) != 0)
823 flags |= SEC_ALLOC | SEC_LOAD;
824 if (sreloc == NULL
825 || ! bfd_set_section_flags (dynobj, sreloc, flags)
826 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
827 return false;
828 }
0451c93c 829 elf_section_data (sec)->sreloc = sreloc;
a85d7ed0
NC
830 }
831
0451c93c
MS
832 /* If this is a global symbol, we count the number of
833 relocations we need for this symbol. */
834 if (h != NULL)
a85d7ed0 835 {
ec338859 836 head = &((struct elf_s390_link_hash_entry *) h)->dyn_relocs;
0451c93c
MS
837 }
838 else
839 {
ec338859
AM
840 /* Track dynamic relocs needed for local syms too.
841 We really need local syms available to do this
842 easily. Oh well. */
843
844 asection *s;
845 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
846 sec, r_symndx);
847 if (s == NULL)
848 return false;
849
850 head = ((struct elf_s390_dyn_relocs **)
851 &elf_section_data (s)->local_dynrel);
852 }
853
854 p = *head;
855 if (p == NULL || p->sec != sec)
856 {
857 bfd_size_type amt = sizeof *p;
858 p = ((struct elf_s390_dyn_relocs *)
859 bfd_alloc (htab->elf.dynobj, amt));
860 if (p == NULL)
861 return false;
862 p->next = *head;
863 *head = p;
864 p->sec = sec;
865 p->count = 0;
866 p->pc_count = 0;
a85d7ed0 867 }
ec338859
AM
868
869 p->count += 1;
870 if (ELF32_R_TYPE (rel->r_info) == R_390_PC16
871 || ELF32_R_TYPE (rel->r_info) == R_390_PC16DBL
befc3abb 872 || ELF32_R_TYPE (rel->r_info) == R_390_PC32DBL
ec338859
AM
873 || ELF32_R_TYPE (rel->r_info) == R_390_PC32)
874 p->pc_count += 1;
a85d7ed0 875 }
a85d7ed0 876 break;
ec338859 877
a85d7ed0
NC
878 /* This relocation describes the C++ object vtable hierarchy.
879 Reconstruct it for later use during GC. */
880 case R_390_GNU_VTINHERIT:
881 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
882 return false;
883 break;
ec338859 884
a85d7ed0
NC
885 /* This relocation describes which C++ vtable entries are actually
886 used. Record for later use during GC. */
887 case R_390_GNU_VTENTRY:
db727808 888 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
a85d7ed0
NC
889 return false;
890 break;
ec338859 891
a85d7ed0
NC
892 default:
893 break;
894 }
895 }
896
897 return true;
898}
899
900/* Return the section that should be marked against GC for a given
901 relocation. */
902
903static asection *
1e2f5b6e
AM
904elf_s390_gc_mark_hook (sec, info, rel, h, sym)
905 asection *sec;
a85d7ed0
NC
906 struct bfd_link_info *info ATTRIBUTE_UNUSED;
907 Elf_Internal_Rela *rel;
908 struct elf_link_hash_entry *h;
909 Elf_Internal_Sym *sym;
910{
911 if (h != NULL)
912 {
913 switch (ELF32_R_TYPE (rel->r_info))
914 {
915 case R_390_GNU_VTINHERIT:
916 case R_390_GNU_VTENTRY:
917 break;
918
919 default:
920 switch (h->root.type)
921 {
922 case bfd_link_hash_defined:
923 case bfd_link_hash_defweak:
924 return h->root.u.def.section;
925
926 case bfd_link_hash_common:
927 return h->root.u.c.p->section;
928
929 default:
930 break;
931 }
932 }
933 }
934 else
1e2f5b6e 935 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
a85d7ed0
NC
936
937 return NULL;
938}
939
940/* Update the got entry reference counts for the section being removed. */
941
942static boolean
943elf_s390_gc_sweep_hook (abfd, info, sec, relocs)
0451c93c
MS
944 bfd *abfd;
945 struct bfd_link_info *info;
946 asection *sec;
947 const Elf_Internal_Rela *relocs;
a85d7ed0
NC
948{
949 Elf_Internal_Shdr *symtab_hdr;
950 struct elf_link_hash_entry **sym_hashes;
951 bfd_signed_vma *local_got_refcounts;
952 const Elf_Internal_Rela *rel, *relend;
953 unsigned long r_symndx;
954 struct elf_link_hash_entry *h;
a85d7ed0 955
ec338859 956 elf_section_data (sec)->local_dynrel = NULL;
a85d7ed0 957
0451c93c
MS
958 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
959 sym_hashes = elf_sym_hashes (abfd);
960 local_got_refcounts = elf_local_got_refcounts (abfd);
a85d7ed0
NC
961
962 relend = relocs + sec->reloc_count;
963 for (rel = relocs; rel < relend; rel++)
964 switch (ELF32_R_TYPE (rel->r_info))
965 {
966 case R_390_GOT12:
967 case R_390_GOT16:
968 case R_390_GOT32:
969 case R_390_GOTOFF:
970 case R_390_GOTPC:
befc3abb
MS
971 case R_390_GOTPCDBL:
972 case R_390_GOTENT:
a85d7ed0
NC
973 r_symndx = ELF32_R_SYM (rel->r_info);
974 if (r_symndx >= symtab_hdr->sh_info)
975 {
976 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
977 if (h->got.refcount > 0)
0451c93c 978 h->got.refcount -= 1;
a85d7ed0
NC
979 }
980 else if (local_got_refcounts != NULL)
981 {
982 if (local_got_refcounts[r_symndx] > 0)
0451c93c
MS
983 local_got_refcounts[r_symndx] -= 1;
984 }
985 break;
986
987 case R_390_8:
988 case R_390_12:
989 case R_390_16:
990 case R_390_32:
991 case R_390_PC16:
992 case R_390_PC16DBL:
befc3abb 993 case R_390_PC32DBL:
0451c93c
MS
994 case R_390_PC32:
995 r_symndx = ELF32_R_SYM (rel->r_info);
996 if (r_symndx >= symtab_hdr->sh_info)
997 {
998 struct elf_s390_link_hash_entry *eh;
999 struct elf_s390_dyn_relocs **pp;
1000 struct elf_s390_dyn_relocs *p;
1001
1002 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1003
1004 if (!info->shared && h->plt.refcount > 0)
1005 h->plt.refcount -= 1;
1006
1007 eh = (struct elf_s390_link_hash_entry *) h;
1008
1009 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1010 if (p->sec == sec)
1011 {
1012 if (ELF32_R_TYPE (rel->r_info) == R_390_PC16
1013 || ELF32_R_TYPE (rel->r_info) == R_390_PC16DBL
befc3abb 1014 || ELF32_R_TYPE (rel->r_info) == R_390_PC32DBL
0451c93c
MS
1015 || ELF32_R_TYPE (rel->r_info) == R_390_PC32)
1016 p->pc_count -= 1;
1017 p->count -= 1;
1018 if (p->count == 0)
1019 *pp = p->next;
1020 break;
1021 }
a85d7ed0
NC
1022 }
1023 break;
1024
1025 case R_390_PLT16DBL:
befc3abb 1026 case R_390_PLT32DBL:
a85d7ed0
NC
1027 case R_390_PLT32:
1028 r_symndx = ELF32_R_SYM (rel->r_info);
1029 if (r_symndx >= symtab_hdr->sh_info)
1030 {
1031 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1032 if (h->plt.refcount > 0)
1033 h->plt.refcount -= 1;
1034 }
1035 break;
1036
1037 default:
1038 break;
1039 }
1040
1041 return true;
1042}
1043
1044/* Adjust a symbol defined by a dynamic object and referenced by a
1045 regular object. The current definition is in some section of the
1046 dynamic object, but we're not including those sections. We have to
1047 change the definition to something the rest of the link can
1048 understand. */
1049
1050static boolean
1051elf_s390_adjust_dynamic_symbol (info, h)
1052 struct bfd_link_info *info;
1053 struct elf_link_hash_entry *h;
1054{
0451c93c
MS
1055 struct elf_s390_link_hash_table *htab;
1056 struct elf_s390_link_hash_entry * eh;
1057 struct elf_s390_dyn_relocs *p;
a85d7ed0
NC
1058 asection *s;
1059 unsigned int power_of_two;
1060
a85d7ed0
NC
1061 /* If this is a function, put it in the procedure linkage table. We
1062 will fill in the contents of the procedure linkage table later
cedb70c5 1063 (although we could actually do it here). */
a85d7ed0
NC
1064 if (h->type == STT_FUNC
1065 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1066 {
0451c93c
MS
1067 if (h->plt.refcount <= 0
1068 || (! info->shared
1069 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
f9cd9119
MS
1070 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1071 && h->root.type != bfd_link_hash_undefweak
1072 && h->root.type != bfd_link_hash_undefined))
a85d7ed0
NC
1073 {
1074 /* This case can occur if we saw a PLT32 reloc in an input
1075 file, but the symbol was never referred to by a dynamic
1076 object, or if all references were garbage collected. In
1077 such a case, we don't actually need to build a procedure
1078 linkage table, and we can just do a PC32 reloc instead. */
1079 h->plt.offset = (bfd_vma) -1;
1080 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
a85d7ed0
NC
1081 }
1082
a85d7ed0
NC
1083 return true;
1084 }
bbd7ec4a 1085 else
0451c93c
MS
1086 /* It's possible that we incorrectly decided a .plt reloc was
1087 needed for an R_390_PC32 reloc to a non-function sym in
1088 check_relocs. We can't decide accurately between function and
1089 non-function syms in check-relocs; Objects loaded later in
1090 the link may change h->type. So fix it now. */
bbd7ec4a 1091 h->plt.offset = (bfd_vma) -1;
a85d7ed0
NC
1092
1093 /* If this is a weak symbol, and there is a real definition, the
1094 processor independent code will have arranged for us to see the
1095 real definition first, and we can just use the same value. */
1096 if (h->weakdef != NULL)
1097 {
1098 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1099 || h->weakdef->root.type == bfd_link_hash_defweak);
1100 h->root.u.def.section = h->weakdef->root.u.def.section;
1101 h->root.u.def.value = h->weakdef->root.u.def.value;
1102 return true;
1103 }
1104
1105 /* This is a reference to a symbol defined by a dynamic object which
1106 is not a function. */
1107
1108 /* If we are creating a shared library, we must presume that the
1109 only references to the symbol are via the global offset table.
1110 For such cases we need not do anything here; the relocations will
1111 be handled correctly by relocate_section. */
1112 if (info->shared)
1113 return true;
1114
1115 /* If there are no references to this symbol that do not use the
1116 GOT, we don't need to generate a copy reloc. */
1117 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1118 return true;
1119
0451c93c
MS
1120 /* If -z nocopyreloc was given, we won't generate them either. */
1121 if (info->nocopyreloc)
1122 {
1123 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1124 return true;
1125 }
1126
1127 eh = (struct elf_s390_link_hash_entry *) h;
1128 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1129 {
1130 s = p->sec->output_section;
1131 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1132 break;
1133 }
1134
1135 /* If we didn't find any dynamic relocs in read-only sections, then
ec338859 1136 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
0451c93c
MS
1137 if (p == NULL)
1138 {
1139 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1140 return true;
1141 }
1142
a85d7ed0
NC
1143 /* We must allocate the symbol in our .dynbss section, which will
1144 become part of the .bss section of the executable. There will be
1145 an entry for this symbol in the .dynsym section. The dynamic
1146 object will contain position independent code, so all references
1147 from the dynamic object to this symbol will go through the global
1148 offset table. The dynamic linker will use the .dynsym entry to
1149 determine the address it must put in the global offset table, so
1150 both the dynamic object and the regular object will refer to the
1151 same memory location for the variable. */
1152
0451c93c 1153 htab = elf_s390_hash_table (info);
a85d7ed0 1154
0451c93c
MS
1155 /* We must generate a R_390_COPY reloc to tell the dynamic linker to
1156 copy the initial value out of the dynamic object and into the
1157 runtime process image. */
a85d7ed0
NC
1158 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1159 {
0451c93c 1160 htab->srelbss->_raw_size += sizeof (Elf32_External_Rela);
a85d7ed0
NC
1161 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1162 }
1163
1164 /* We need to figure out the alignment required for this symbol. I
1165 have no idea how ELF linkers handle this. */
1166 power_of_two = bfd_log2 (h->size);
1167 if (power_of_two > 3)
1168 power_of_two = 3;
1169
1170 /* Apply the required alignment. */
0451c93c
MS
1171 s = htab->sdynbss;
1172 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1173 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
a85d7ed0 1174 {
0451c93c 1175 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
a85d7ed0
NC
1176 return false;
1177 }
1178
1179 /* Define the symbol as being at this point in the section. */
1180 h->root.u.def.section = s;
1181 h->root.u.def.value = s->_raw_size;
1182
1183 /* Increment the section size to make room for the symbol. */
1184 s->_raw_size += h->size;
1185
1186 return true;
1187}
1188
0451c93c
MS
1189/* This is the condition under which elf_s390_finish_dynamic_symbol
1190 will be called from elflink.h. If elflink.h doesn't call our
1191 finish_dynamic_symbol routine, we'll need to do something about
1192 initializing any .plt and .got entries in elf_s390_relocate_section. */
1193#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1194 ((DYN) \
1195 && ((INFO)->shared \
1196 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1197 && ((H)->dynindx != -1 \
1198 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1199
1200/* Allocate space in .plt, .got and associated reloc sections for
1201 dynamic relocs. */
1202
1203static boolean
1204allocate_dynrelocs (h, inf)
1205 struct elf_link_hash_entry *h;
1206 PTR inf;
1207{
1208 struct bfd_link_info *info;
1209 struct elf_s390_link_hash_table *htab;
1210 struct elf_s390_link_hash_entry *eh;
1211 struct elf_s390_dyn_relocs *p;
1212
e92d460e 1213 if (h->root.type == bfd_link_hash_indirect)
0451c93c
MS
1214 return true;
1215
e92d460e
AM
1216 if (h->root.type == bfd_link_hash_warning)
1217 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1218
0451c93c
MS
1219 info = (struct bfd_link_info *) inf;
1220 htab = elf_s390_hash_table (info);
1221
1222 if (htab->elf.dynamic_sections_created
1223 && h->plt.refcount > 0)
1224 {
1225 /* Make sure this symbol is output as a dynamic symbol.
1226 Undefined weak syms won't yet be marked as dynamic. */
1227 if (h->dynindx == -1
1228 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1229 {
1230 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1231 return false;
1232 }
1233
1234 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1235 {
1236 asection *s = htab->splt;
1237
1238 /* If this is the first .plt entry, make room for the special
1239 first entry. */
1240 if (s->_raw_size == 0)
1241 s->_raw_size += PLT_FIRST_ENTRY_SIZE;
1242
1243 h->plt.offset = s->_raw_size;
1244
1245 /* If this symbol is not defined in a regular file, and we are
1246 not generating a shared library, then set the symbol to this
1247 location in the .plt. This is required to make function
1248 pointers compare as equal between the normal executable and
1249 the shared library. */
1250 if (! info->shared
1251 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1252 {
1253 h->root.u.def.section = s;
1254 h->root.u.def.value = h->plt.offset;
1255 }
ec338859 1256
0451c93c
MS
1257 /* Make room for this entry. */
1258 s->_raw_size += PLT_ENTRY_SIZE;
ec338859 1259
0451c93c
MS
1260 /* We also need to make an entry in the .got.plt section, which
1261 will be placed in the .got section by the linker script. */
1262 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1263
1264 /* We also need to make an entry in the .rela.plt section. */
1265 htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
1266 }
1267 else
1268 {
1269 h->plt.offset = (bfd_vma) -1;
1270 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1271 }
ec338859 1272 }
0451c93c
MS
1273 else
1274 {
1275 h->plt.offset = (bfd_vma) -1;
1276 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
ec338859
AM
1277 }
1278
0451c93c
MS
1279 if (h->got.refcount > 0)
1280 {
1281 asection *s;
1282 boolean dyn;
1283
1284 /* Make sure this symbol is output as a dynamic symbol.
1285 Undefined weak syms won't yet be marked as dynamic. */
1286 if (h->dynindx == -1
1287 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1288 {
1289 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1290 return false;
1291 }
1292
1293 s = htab->sgot;
1294 h->got.offset = s->_raw_size;
1295 s->_raw_size += GOT_ENTRY_SIZE;
1296 dyn = htab->elf.dynamic_sections_created;
1297 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1298 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1299 }
1300 else
1301 h->got.offset = (bfd_vma) -1;
1302
1303 eh = (struct elf_s390_link_hash_entry *) h;
1304 if (eh->dyn_relocs == NULL)
1305 return true;
1306
1307 /* In the shared -Bsymbolic case, discard space allocated for
1308 dynamic pc-relative relocs against symbols which turn out to be
1309 defined in regular objects. For the normal shared case, discard
1310 space for pc-relative relocs that have become local due to symbol
1311 visibility changes. */
1312
1313 if (info->shared)
1314 {
1315 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1316 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1317 || info->symbolic))
1318 {
1319 struct elf_s390_dyn_relocs **pp;
1320
1321 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1322 {
1323 p->count -= p->pc_count;
1324 p->pc_count = 0;
1325 if (p->count == 0)
1326 *pp = p->next;
1327 else
1328 pp = &p->next;
1329 }
1330 }
1331 }
1332 else
1333 {
1334 /* For the non-shared case, discard space for relocs against
1335 symbols which turn out to need copy relocs or are not
1336 dynamic. */
1337
1338 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1339 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1340 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1341 || (htab->elf.dynamic_sections_created
1342 && (h->root.type == bfd_link_hash_undefweak
1343 || h->root.type == bfd_link_hash_undefined))))
1344 {
1345 /* Make sure this symbol is output as a dynamic symbol.
1346 Undefined weak syms won't yet be marked as dynamic. */
1347 if (h->dynindx == -1
1348 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1349 {
1350 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1351 return false;
1352 }
1353
1354 /* If that succeeded, we know we'll be keeping all the
1355 relocs. */
1356 if (h->dynindx != -1)
1357 goto keep;
1358 }
1359
1360 eh->dyn_relocs = NULL;
1361
ec338859 1362 keep: ;
0451c93c
MS
1363 }
1364
1365 /* Finally, allocate space. */
1366 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1367 {
1368 asection *sreloc = elf_section_data (p->sec)->sreloc;
1369 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
1370 }
1371
1372 return true;
1373}
1374
1375/* Find any dynamic relocs that apply to read-only sections. */
1376
1377static boolean
1378readonly_dynrelocs (h, inf)
1379 struct elf_link_hash_entry *h;
1380 PTR inf;
1381{
1382 struct elf_s390_link_hash_entry *eh;
1383 struct elf_s390_dyn_relocs *p;
1384
e92d460e
AM
1385 if (h->root.type == bfd_link_hash_warning)
1386 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1387
0451c93c
MS
1388 eh = (struct elf_s390_link_hash_entry *) h;
1389 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1390 {
1391 asection *s = p->sec->output_section;
1392
1393 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1394 {
1395 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1396
1397 info->flags |= DF_TEXTREL;
1398
1399 /* Not an error, just cut short the traversal. */
1400 return false;
1401 }
1402 }
1403 return true;
1404}
1405
a85d7ed0
NC
1406/* Set the sizes of the dynamic sections. */
1407
1408static boolean
1409elf_s390_size_dynamic_sections (output_bfd, info)
29c2fb7c 1410 bfd *output_bfd ATTRIBUTE_UNUSED;
a85d7ed0
NC
1411 struct bfd_link_info *info;
1412{
0451c93c 1413 struct elf_s390_link_hash_table *htab;
a85d7ed0
NC
1414 bfd *dynobj;
1415 asection *s;
a85d7ed0 1416 boolean relocs;
0451c93c 1417 bfd *ibfd;
a85d7ed0 1418
0451c93c
MS
1419 htab = elf_s390_hash_table (info);
1420 dynobj = htab->elf.dynobj;
1421 if (dynobj == NULL)
1422 abort ();
a85d7ed0 1423
0451c93c 1424 if (htab->elf.dynamic_sections_created)
a85d7ed0
NC
1425 {
1426 /* Set the contents of the .interp section to the interpreter. */
1427 if (! info->shared)
1428 {
1429 s = bfd_get_section_by_name (dynobj, ".interp");
0451c93c
MS
1430 if (s == NULL)
1431 abort ();
a85d7ed0
NC
1432 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1433 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1434 }
1435 }
a85d7ed0 1436
0451c93c
MS
1437 /* Set up .got offsets for local syms, and space for local dynamic
1438 relocs. */
1439 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
a85d7ed0 1440 {
0451c93c
MS
1441 bfd_signed_vma *local_got;
1442 bfd_signed_vma *end_local_got;
1443 bfd_size_type locsymcount;
1444 Elf_Internal_Shdr *symtab_hdr;
1445 asection *srela;
a85d7ed0 1446
0451c93c 1447 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
a85d7ed0
NC
1448 continue;
1449
0451c93c 1450 for (s = ibfd->sections; s != NULL; s = s->next)
a85d7ed0 1451 {
ec338859 1452 struct elf_s390_dyn_relocs *p;
0451c93c 1453
ec338859
AM
1454 for (p = *((struct elf_s390_dyn_relocs **)
1455 &elf_section_data (s)->local_dynrel);
1456 p != NULL;
1457 p = p->next)
a85d7ed0 1458 {
ec338859
AM
1459 if (!bfd_is_abs_section (p->sec)
1460 && bfd_is_abs_section (p->sec->output_section))
1461 {
1462 /* Input section has been discarded, either because
1463 it is a copy of a linkonce section or due to
1464 linker script /DISCARD/, so we'll be discarding
1465 the relocs too. */
1466 }
248866a8 1467 else if (p->count != 0)
ec338859
AM
1468 {
1469 srela = elf_section_data (p->sec)->sreloc;
1470 srela->_raw_size += p->count * sizeof (Elf32_External_Rela);
248866a8
AM
1471 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1472 info->flags |= DF_TEXTREL;
ec338859 1473 }
a85d7ed0
NC
1474 }
1475 }
0451c93c
MS
1476
1477 local_got = elf_local_got_refcounts (ibfd);
1478 if (!local_got)
1479 continue;
1480
1481 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1482 locsymcount = symtab_hdr->sh_info;
1483 end_local_got = local_got + locsymcount;
1484 s = htab->sgot;
1485 srela = htab->srelgot;
1486 for (; local_got < end_local_got; ++local_got)
a85d7ed0 1487 {
0451c93c 1488 if (*local_got > 0)
a85d7ed0 1489 {
0451c93c
MS
1490 *local_got = s->_raw_size;
1491 s->_raw_size += GOT_ENTRY_SIZE;
1492 if (info->shared)
1493 srela->_raw_size += sizeof (Elf32_External_Rela);
a85d7ed0
NC
1494 }
1495 else
0451c93c
MS
1496 *local_got = (bfd_vma) -1;
1497 }
1498 }
1499
1500 /* Allocate global sym .plt and .got entries, and space for global
1501 sym dynamic relocs. */
1502 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1503
1504 /* We now have determined the sizes of the various dynamic sections.
1505 Allocate memory for them. */
1506 relocs = false;
1507 for (s = dynobj->sections; s != NULL; s = s->next)
1508 {
1509 if ((s->flags & SEC_LINKER_CREATED) == 0)
1510 continue;
1511
1512 if (s == htab->splt
1513 || s == htab->sgot
1514 || s == htab->sgotplt)
1515 {
1516 /* Strip this section if we don't need it; see the
1517 comment below. */
a85d7ed0 1518 }
0451c93c
MS
1519 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1520 {
1521 if (s->_raw_size != 0 && s != htab->srelplt)
1522 relocs = true;
ec338859 1523
0451c93c
MS
1524 /* We use the reloc_count field as a counter if we need
1525 to copy relocs into the output file. */
1526 s->reloc_count = 0;
1527 }
1528 else
a85d7ed0
NC
1529 {
1530 /* It's not one of our sections, so don't allocate space. */
1531 continue;
1532 }
1533
0451c93c 1534 if (s->_raw_size == 0)
a85d7ed0 1535 {
0451c93c
MS
1536 /* If we don't need this section, strip it from the
1537 output file. This is to handle .rela.bss and
1538 .rela.plt. We must create it in
1539 create_dynamic_sections, because it must be created
1540 before the linker maps input sections to output
1541 sections. The linker does that before
1542 adjust_dynamic_symbol is called, and it is that
1543 function which decides whether anything needs to go
1544 into these sections. */
1545
a85d7ed0
NC
1546 _bfd_strip_section_from_output (info, s);
1547 continue;
1548 }
1549
0451c93c
MS
1550 /* Allocate memory for the section contents. We use bfd_zalloc
1551 here in case unused entries are not reclaimed before the
1552 section's contents are written out. This should not happen,
1553 but this way if it does, we get a R_390_NONE reloc instead
1554 of garbage. */
1555 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1556 if (s->contents == NULL)
a85d7ed0
NC
1557 return false;
1558 }
ec338859 1559
0451c93c 1560 if (htab->elf.dynamic_sections_created)
a85d7ed0
NC
1561 {
1562 /* Add some entries to the .dynamic section. We fill in the
1563 values later, in elf_s390_finish_dynamic_sections, but we
1564 must add the entries now so that we get the correct size for
1565 the .dynamic section. The DT_DEBUG entry is filled in by the
1566 dynamic linker and used by the debugger. */
dc810e39
AM
1567#define add_dynamic_entry(TAG, VAL) \
1568 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
ec338859 1569
a85d7ed0
NC
1570 if (! info->shared)
1571 {
dc810e39 1572 if (!add_dynamic_entry (DT_DEBUG, 0))
a85d7ed0
NC
1573 return false;
1574 }
ec338859 1575
0451c93c 1576 if (htab->splt->_raw_size != 0)
a85d7ed0 1577 {
dc810e39
AM
1578 if (!add_dynamic_entry (DT_PLTGOT, 0)
1579 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1580 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1581 || !add_dynamic_entry (DT_JMPREL, 0))
a85d7ed0
NC
1582 return false;
1583 }
ec338859 1584
a85d7ed0
NC
1585 if (relocs)
1586 {
dc810e39
AM
1587 if (!add_dynamic_entry (DT_RELA, 0)
1588 || !add_dynamic_entry (DT_RELASZ, 0)
1589 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
a85d7ed0 1590 return false;
a85d7ed0 1591
0451c93c
MS
1592 /* If any dynamic relocs apply to a read-only section,
1593 then we need a DT_TEXTREL entry. */
248866a8
AM
1594 if ((info->flags & DF_TEXTREL) == 0)
1595 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1596 (PTR) info);
ec338859 1597
0451c93c
MS
1598 if ((info->flags & DF_TEXTREL) != 0)
1599 {
1600 if (!add_dynamic_entry (DT_TEXTREL, 0))
1601 return false;
1602 }
a85d7ed0
NC
1603 }
1604 }
dc810e39 1605#undef add_dynamic_entry
ec338859 1606
a85d7ed0
NC
1607 return true;
1608}
1609
a85d7ed0
NC
1610/* Relocate a 390 ELF section. */
1611
1612static boolean
1613elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
1614 contents, relocs, local_syms, local_sections)
1615 bfd *output_bfd;
1616 struct bfd_link_info *info;
1617 bfd *input_bfd;
1618 asection *input_section;
1619 bfd_byte *contents;
1620 Elf_Internal_Rela *relocs;
1621 Elf_Internal_Sym *local_syms;
1622 asection **local_sections;
1623{
0451c93c 1624 struct elf_s390_link_hash_table *htab;
a85d7ed0
NC
1625 Elf_Internal_Shdr *symtab_hdr;
1626 struct elf_link_hash_entry **sym_hashes;
1627 bfd_vma *local_got_offsets;
a85d7ed0
NC
1628 Elf_Internal_Rela *rel;
1629 Elf_Internal_Rela *relend;
1630
b491616a
AM
1631 if (info->relocateable)
1632 return true;
1633
0451c93c 1634 htab = elf_s390_hash_table (info);
a85d7ed0
NC
1635 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1636 sym_hashes = elf_sym_hashes (input_bfd);
1637 local_got_offsets = elf_local_got_offsets (input_bfd);
1638
a85d7ed0
NC
1639 rel = relocs;
1640 relend = relocs + input_section->reloc_count;
1641 for (; rel < relend; rel++)
1642 {
1643 int r_type;
1644 reloc_howto_type *howto;
1645 unsigned long r_symndx;
1646 struct elf_link_hash_entry *h;
1647 Elf_Internal_Sym *sym;
1648 asection *sec;
0451c93c 1649 bfd_vma off;
a85d7ed0 1650 bfd_vma relocation;
0451c93c 1651 boolean unresolved_reloc;
a85d7ed0 1652 bfd_reloc_status_type r;
ec338859 1653
a85d7ed0
NC
1654 r_type = ELF32_R_TYPE (rel->r_info);
1655 if (r_type == (int) R_390_GNU_VTINHERIT
1656 || r_type == (int) R_390_GNU_VTENTRY)
1657 continue;
1658 if (r_type < 0 || r_type >= (int) R_390_max)
1659 {
1660 bfd_set_error (bfd_error_bad_value);
1661 return false;
1662 }
ec338859 1663
b491616a 1664 howto = elf_howto_table + r_type;
a85d7ed0 1665 r_symndx = ELF32_R_SYM (rel->r_info);
a85d7ed0
NC
1666 h = NULL;
1667 sym = NULL;
1668 sec = NULL;
0451c93c 1669 unresolved_reloc = false;
a85d7ed0
NC
1670 if (r_symndx < symtab_hdr->sh_info)
1671 {
1672 sym = local_syms + r_symndx;
1673 sec = local_sections[r_symndx];
f8df10f4 1674 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
a85d7ed0
NC
1675 }
1676 else
1677 {
1678 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1679 while (h->root.type == bfd_link_hash_indirect
1680 || h->root.type == bfd_link_hash_warning)
1681 h = (struct elf_link_hash_entry *) h->root.u.i.link;
ec338859 1682
a85d7ed0
NC
1683 if (h->root.type == bfd_link_hash_defined
1684 || h->root.type == bfd_link_hash_defweak)
1685 {
1686 sec = h->root.u.def.section;
27018c3f 1687 if (sec->output_section == NULL)
a85d7ed0 1688 {
0451c93c
MS
1689 /* Set a flag that will be cleared later if we find a
1690 relocation value for this symbol. output_section
1691 is typically NULL for symbols satisfied by a shared
1692 library. */
1693 unresolved_reloc = true;
a85d7ed0
NC
1694 relocation = 0;
1695 }
1696 else
1697 relocation = (h->root.u.def.value
1698 + sec->output_section->vma
1699 + sec->output_offset);
1700 }
1701 else if (h->root.type == bfd_link_hash_undefweak)
1702 relocation = 0;
671bae9c
NC
1703 else if (info->shared
1704 && (!info->symbolic || info->allow_shlib_undefined)
a85d7ed0
NC
1705 && !info->no_undefined
1706 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1707 relocation = 0;
1708 else
1709 {
1710 if (! ((*info->callbacks->undefined_symbol)
1711 (info, h->root.root.string, input_bfd,
1712 input_section, rel->r_offset,
0451c93c
MS
1713 (!info->shared || info->no_undefined
1714 || ELF_ST_VISIBILITY (h->other)))))
a85d7ed0
NC
1715 return false;
1716 relocation = 0;
1717 }
1718 }
ec338859 1719
a85d7ed0
NC
1720 switch (r_type)
1721 {
1722 case R_390_GOT12:
1723 case R_390_GOT16:
1724 case R_390_GOT32:
befc3abb 1725 case R_390_GOTENT:
a85d7ed0
NC
1726 /* Relocation is to the entry for this symbol in the global
1727 offset table. */
0451c93c
MS
1728 if (htab->sgot == NULL)
1729 abort ();
ec338859 1730
a85d7ed0
NC
1731 if (h != NULL)
1732 {
0451c93c 1733 boolean dyn;
ec338859 1734
a85d7ed0 1735 off = h->got.offset;
0451c93c
MS
1736 dyn = htab->elf.dynamic_sections_created;
1737 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
a85d7ed0 1738 || (info->shared
0451c93c
MS
1739 && (info->symbolic
1740 || h->dynindx == -1
1741 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
a85d7ed0
NC
1742 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1743 {
1744 /* This is actually a static link, or it is a
1745 -Bsymbolic link and the symbol is defined
1746 locally, or the symbol was forced to be local
1747 because of a version file. We must initialize
1748 this entry in the global offset table. Since the
1749 offset must always be a multiple of 2, we use the
1750 least significant bit to record whether we have
1751 initialized it already.
1752
1753 When doing a dynamic link, we create a .rel.got
1754 relocation entry to initialize the value. This
1755 is done in the finish_dynamic_symbol routine. */
1756 if ((off & 1) != 0)
1757 off &= ~1;
1758 else
1759 {
1760 bfd_put_32 (output_bfd, relocation,
0451c93c 1761 htab->sgot->contents + off);
a85d7ed0
NC
1762 h->got.offset |= 1;
1763 }
1764 }
0451c93c
MS
1765 else
1766 unresolved_reloc = false;
a85d7ed0
NC
1767 }
1768 else
1769 {
0451c93c
MS
1770 if (local_got_offsets == NULL)
1771 abort ();
a85d7ed0
NC
1772
1773 off = local_got_offsets[r_symndx];
1774
1775 /* The offset must always be a multiple of 4. We use
1776 the least significant bit to record whether we have
1777 already generated the necessary reloc. */
1778 if ((off & 1) != 0)
1779 off &= ~1;
1780 else
1781 {
0451c93c
MS
1782 bfd_put_32 (output_bfd, relocation,
1783 htab->sgot->contents + off);
a85d7ed0
NC
1784
1785 if (info->shared)
1786 {
1787 asection *srelgot;
1788 Elf_Internal_Rela outrel;
0451c93c 1789 Elf32_External_Rela *loc;
a85d7ed0 1790
0451c93c
MS
1791 srelgot = htab->srelgot;
1792 if (srelgot == NULL)
1793 abort ();
a85d7ed0 1794
0451c93c
MS
1795 outrel.r_offset = (htab->sgot->output_section->vma
1796 + htab->sgot->output_offset
a85d7ed0
NC
1797 + off);
1798 outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
1799 outrel.r_addend = relocation;
0451c93c
MS
1800 loc = (Elf32_External_Rela *) srelgot->contents;
1801 loc += srelgot->reloc_count++;
1802 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
a85d7ed0
NC
1803 }
1804
1805 local_got_offsets[r_symndx] |= 1;
1806 }
a85d7ed0
NC
1807 }
1808
0451c93c
MS
1809 if (off >= (bfd_vma) -2)
1810 abort ();
a85d7ed0 1811
0451c93c 1812 relocation = htab->sgot->output_offset + off;
befc3abb
MS
1813
1814 /*
1815 * For @GOTENT the relocation is against the offset between
1816 * the instruction and the symbols entry in the GOT and not
1817 * between the start of the GOT and the symbols entry. We
1818 * add the vma of the GOT to get the correct value.
1819 */
1820 if (r_type == R_390_GOTENT)
1821 relocation += htab->sgot->output_section->vma;
1822
a85d7ed0 1823 break;
dc810e39 1824
a85d7ed0
NC
1825 case R_390_GOTOFF:
1826 /* Relocation is relative to the start of the global offset
1827 table. */
1828
a85d7ed0
NC
1829 /* Note that sgot->output_offset is not involved in this
1830 calculation. We always want the start of .got. If we
1831 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1832 permitted by the ABI, we might have to change this
1833 calculation. */
0451c93c 1834 relocation -= htab->sgot->output_section->vma;
a85d7ed0
NC
1835 break;
1836
1837 case R_390_GOTPC:
befc3abb 1838 case R_390_GOTPCDBL:
a85d7ed0 1839 /* Use global offset table as symbol value. */
0451c93c
MS
1840 relocation = htab->sgot->output_section->vma;
1841 unresolved_reloc = false;
a85d7ed0
NC
1842 break;
1843
1844 case R_390_PLT16DBL:
befc3abb 1845 case R_390_PLT32DBL:
a85d7ed0
NC
1846 case R_390_PLT32:
1847 /* Relocation is to the entry for this symbol in the
1848 procedure linkage table. */
1849
1850 /* Resolve a PLT32 reloc against a local symbol directly,
1851 without using the procedure linkage table. */
1852 if (h == NULL)
1853 break;
1854
0451c93c
MS
1855 if (h->plt.offset == (bfd_vma) -1
1856 || htab->splt == NULL)
a85d7ed0
NC
1857 {
1858 /* We didn't make a PLT entry for this symbol. This
1859 happens when statically linking PIC code, or when
1860 using -Bsymbolic. */
1861 break;
1862 }
1863
0451c93c
MS
1864 relocation = (htab->splt->output_section->vma
1865 + htab->splt->output_offset
a85d7ed0 1866 + h->plt.offset);
0451c93c 1867 unresolved_reloc = false;
a85d7ed0
NC
1868 break;
1869
1870 case R_390_8:
1871 case R_390_16:
1872 case R_390_32:
1873 case R_390_PC16:
1874 case R_390_PC16DBL:
befc3abb 1875 case R_390_PC32DBL:
a85d7ed0 1876 case R_390_PC32:
ec338859
AM
1877 /* r_symndx will be zero only for relocs against symbols
1878 from removed linkonce sections, or sections discarded by
1879 a linker script. */
1880 if (r_symndx == 0
1881 || (input_section->flags & SEC_ALLOC) == 0)
1882 break;
1883
0451c93c 1884 if ((info->shared
0451c93c
MS
1885 && ((r_type != R_390_PC16
1886 && r_type != R_390_PC16DBL
befc3abb 1887 && r_type != R_390_PC32DBL
0451c93c
MS
1888 && r_type != R_390_PC32)
1889 || (h != NULL
1890 && h->dynindx != -1
1891 && (! info->symbolic
1892 || (h->elf_link_hash_flags
1893 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1894 || (!info->shared
0451c93c
MS
1895 && h != NULL
1896 && h->dynindx != -1
1897 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1898 && (((h->elf_link_hash_flags
1899 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1900 && (h->elf_link_hash_flags
1901 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1902 || h->root.type == bfd_link_hash_undefweak
1903 || h->root.type == bfd_link_hash_undefined)))
a85d7ed0
NC
1904 {
1905 Elf_Internal_Rela outrel;
1906 boolean skip, relocate;
0451c93c
MS
1907 asection *sreloc;
1908 Elf32_External_Rela *loc;
ec338859 1909
0451c93c
MS
1910 /* When generating a shared object, these relocations
1911 are copied into the output file to be resolved at run
1912 time. */
ec338859 1913
a85d7ed0 1914 skip = false;
0bb2d96a 1915 relocate = false;
ec338859 1916
c629eae0
JJ
1917 outrel.r_offset =
1918 _bfd_elf_section_offset (output_bfd, info, input_section,
1919 rel->r_offset);
1920 if (outrel.r_offset == (bfd_vma) -1)
1921 skip = true;
0bb2d96a
JJ
1922 else if (outrel.r_offset == (bfd_vma) -2)
1923 skip = true, relocate = true;
a85d7ed0
NC
1924 outrel.r_offset += (input_section->output_section->vma
1925 + input_section->output_offset);
ec338859 1926
a85d7ed0 1927 if (skip)
0bb2d96a 1928 memset (&outrel, 0, sizeof outrel);
0451c93c
MS
1929 else if (h != NULL
1930 && h->dynindx != -1
1931 && (r_type == R_390_PC16
1932 || r_type == R_390_PC16DBL
befc3abb 1933 || r_type == R_390_PC32DBL
0451c93c
MS
1934 || r_type == R_390_PC32
1935 || !info->shared
1936 || !info->symbolic
1937 || (h->elf_link_hash_flags
1938 & ELF_LINK_HASH_DEF_REGULAR) == 0))
a85d7ed0 1939 {
a85d7ed0 1940 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
27018c3f 1941 outrel.r_addend = rel->r_addend;
a85d7ed0
NC
1942 }
1943 else
1944 {
0451c93c
MS
1945 /* This symbol is local, or marked to become local. */
1946 relocate = true;
1947 outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
1948 outrel.r_addend = relocation + rel->r_addend;
a85d7ed0 1949 }
ec338859 1950
0451c93c
MS
1951 sreloc = elf_section_data (input_section)->sreloc;
1952 if (sreloc == NULL)
1953 abort ();
ec338859 1954
0451c93c
MS
1955 loc = (Elf32_External_Rela *) sreloc->contents;
1956 loc += sreloc->reloc_count++;
1957 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
ec338859 1958
a85d7ed0
NC
1959 /* If this reloc is against an external symbol, we do
1960 not want to fiddle with the addend. Otherwise, we
1961 need to include the symbol value so that it becomes
1962 an addend for the dynamic reloc. */
1963 if (! relocate)
1964 continue;
1965 }
0451c93c 1966 break;
ec338859 1967
0451c93c
MS
1968 default:
1969 break;
1970 }
ec338859 1971
239e1f3a
AM
1972 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1973 because such sections are not SEC_ALLOC and thus ld.so will
1974 not process them. */
0451c93c 1975 if (unresolved_reloc
239e1f3a 1976 && !((input_section->flags & SEC_DEBUGGING) != 0
0451c93c
MS
1977 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1978 (*_bfd_error_handler)
1979 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1980 bfd_archive_filename (input_bfd),
1981 bfd_get_section_name (input_bfd, input_section),
1982 (long) rel->r_offset,
1983 h->root.root.string);
ec338859 1984
a85d7ed0 1985 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
0451c93c
MS
1986 contents, rel->r_offset,
1987 relocation, rel->r_addend);
ec338859 1988
a85d7ed0
NC
1989 if (r != bfd_reloc_ok)
1990 {
0451c93c 1991 const char *name;
ec338859 1992
0451c93c
MS
1993 if (h != NULL)
1994 name = h->root.root.string;
1995 else
a85d7ed0 1996 {
0451c93c
MS
1997 name = bfd_elf_string_from_elf_section (input_bfd,
1998 symtab_hdr->sh_link,
1999 sym->st_name);
2000 if (name == NULL)
2001 return false;
2002 if (*name == '\0')
2003 name = bfd_section_name (input_bfd, sec);
2004 }
ec338859 2005
0451c93c
MS
2006 if (r == bfd_reloc_overflow)
2007 {
ec338859 2008
0451c93c
MS
2009 if (! ((*info->callbacks->reloc_overflow)
2010 (info, name, howto->name, (bfd_vma) 0,
2011 input_bfd, input_section, rel->r_offset)))
2012 return false;
2013 }
2014 else
2015 {
2016 (*_bfd_error_handler)
2017 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
2018 bfd_archive_filename (input_bfd),
2019 bfd_get_section_name (input_bfd, input_section),
2020 (long) rel->r_offset, name, (int) r);
2021 return false;
a85d7ed0
NC
2022 }
2023 }
2024 }
ec338859 2025
a85d7ed0
NC
2026 return true;
2027}
2028
2029/* Finish up dynamic symbol handling. We set the contents of various
2030 dynamic sections here. */
2031
2032static boolean
2033elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym)
2034 bfd *output_bfd;
2035 struct bfd_link_info *info;
2036 struct elf_link_hash_entry *h;
2037 Elf_Internal_Sym *sym;
2038{
0451c93c 2039 struct elf_s390_link_hash_table *htab;
ec338859 2040
0451c93c 2041 htab = elf_s390_hash_table (info);
ec338859 2042
a85d7ed0
NC
2043 if (h->plt.offset != (bfd_vma) -1)
2044 {
0451c93c
MS
2045 bfd_vma plt_index;
2046 bfd_vma got_offset;
a85d7ed0 2047 Elf_Internal_Rela rela;
0451c93c 2048 Elf32_External_Rela *loc;
a85d7ed0 2049 bfd_vma relative_offset;
ec338859 2050
a85d7ed0
NC
2051 /* This symbol has an entry in the procedure linkage table. Set
2052 it up. */
ec338859 2053
0451c93c
MS
2054 if (h->dynindx == -1
2055 || htab->splt == NULL
2056 || htab->sgotplt == NULL
2057 || htab->srelplt == NULL)
2058 abort ();
a85d7ed0 2059
dc810e39 2060 /* Calc. index no.
a85d7ed0
NC
2061 Current offset - size first entry / entry size. */
2062 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
2063
2064 /* Offset in GOT is PLT index plus GOT headers(3) times 4,
2065 addr & GOT addr. */
2066 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2067
2068 /* S390 uses halfwords for relative branch calc! */
dc810e39
AM
2069 relative_offset = - ((PLT_FIRST_ENTRY_SIZE +
2070 (PLT_ENTRY_SIZE * plt_index) + 18) / 2);
a85d7ed0
NC
2071 /* If offset is > 32768, branch to a previous branch
2072 390 can only handle +-64 K jumps. */
dc810e39
AM
2073 if ( -32768 > (int) relative_offset )
2074 relative_offset =
2075 -(unsigned) (((65536 / PLT_ENTRY_SIZE - 1) * PLT_ENTRY_SIZE) / 2);
a85d7ed0
NC
2076
2077 /* Fill in the entry in the procedure linkage table. */
2078 if (!info->shared)
0451c93c
MS
2079 {
2080 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD0,
2081 htab->splt->contents + h->plt.offset);
2082 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD1,
2083 htab->splt->contents + h->plt.offset + 4);
2084 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
2085 htab->splt->contents + h->plt.offset + 8);
2086 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
2087 htab->splt->contents + h->plt.offset + 12);
2088 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD4,
2089 htab->splt->contents + h->plt.offset + 16);
2090 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2091 htab->splt->contents + h->plt.offset + 20);
2092 bfd_put_32 (output_bfd,
2093 (htab->sgotplt->output_section->vma
2094 + htab->sgotplt->output_offset
2095 + got_offset),
2096 htab->splt->contents + h->plt.offset + 24);
2097 }
a85d7ed0 2098 else if (got_offset < 4096)
0451c93c
MS
2099 {
2100 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD0 + got_offset,
2101 htab->splt->contents + h->plt.offset);
2102 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD1,
2103 htab->splt->contents + h->plt.offset + 4);
2104 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD2,
2105 htab->splt->contents + h->plt.offset + 8);
2106 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD3,
2107 htab->splt->contents + h->plt.offset + 12);
2108 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD4,
2109 htab->splt->contents + h->plt.offset + 16);
2110 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2111 htab->splt->contents + h->plt.offset + 20);
2112 bfd_put_32 (output_bfd, (bfd_vma) 0,
2113 htab->splt->contents + h->plt.offset + 24);
2114 }
a85d7ed0 2115 else if (got_offset < 32768)
0451c93c
MS
2116 {
2117 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD0 + got_offset,
2118 htab->splt->contents + h->plt.offset);
2119 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD1,
2120 htab->splt->contents + h->plt.offset + 4);
2121 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD2,
2122 htab->splt->contents + h->plt.offset + 8);
2123 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD3,
2124 htab->splt->contents + h->plt.offset + 12);
2125 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD4,
2126 htab->splt->contents + h->plt.offset + 16);
2127 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2128 htab->splt->contents + h->plt.offset + 20);
2129 bfd_put_32 (output_bfd, (bfd_vma) 0,
2130 htab->splt->contents + h->plt.offset + 24);
2131 }
a85d7ed0 2132 else
0451c93c
MS
2133 {
2134 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD0,
2135 htab->splt->contents + h->plt.offset);
2136 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD1,
2137 htab->splt->contents + h->plt.offset + 4);
2138 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
2139 htab->splt->contents + h->plt.offset + 8);
2140 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
2141 htab->splt->contents + h->plt.offset + 12);
2142 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
2143 htab->splt->contents + h->plt.offset + 16);
2144 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2145 htab->splt->contents + h->plt.offset + 20);
2146 bfd_put_32 (output_bfd, got_offset,
2147 htab->splt->contents + h->plt.offset + 24);
2148 }
a85d7ed0
NC
2149 /* Insert offset into reloc. table here. */
2150 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
0451c93c 2151 htab->splt->contents + h->plt.offset + 28);
a85d7ed0
NC
2152
2153 /* Fill in the entry in the global offset table.
2154 Points to instruction after GOT offset. */
2155 bfd_put_32 (output_bfd,
0451c93c
MS
2156 (htab->splt->output_section->vma
2157 + htab->splt->output_offset
a85d7ed0
NC
2158 + h->plt.offset
2159 + 12),
0451c93c 2160 htab->sgotplt->contents + got_offset);
a85d7ed0 2161
0451c93c
MS
2162 /* Fill in the entry in the .rela.plt section. */
2163 rela.r_offset = (htab->sgotplt->output_section->vma
2164 + htab->sgotplt->output_offset
2165 + got_offset);
2166 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_JMP_SLOT);
2167 rela.r_addend = 0;
2168 loc = (Elf32_External_Rela *) htab->srelplt->contents + plt_index;
2169 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
ec338859 2170
a85d7ed0
NC
2171 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2172 {
2173 /* Mark the symbol as undefined, rather than as defined in
0451c93c
MS
2174 the .plt section. Leave the value alone. This is a clue
2175 for the dynamic linker, to make function pointer
2176 comparisons work between an application and shared
ec338859 2177 library. */
a85d7ed0
NC
2178 sym->st_shndx = SHN_UNDEF;
2179 }
2180 }
ec338859 2181
a85d7ed0
NC
2182 if (h->got.offset != (bfd_vma) -1)
2183 {
a85d7ed0 2184 Elf_Internal_Rela rela;
0451c93c 2185 Elf32_External_Rela *loc;
a85d7ed0
NC
2186
2187 /* This symbol has an entry in the global offset table. Set it
2188 up. */
2189
0451c93c
MS
2190 if (htab->sgot == NULL || htab->srelgot == NULL)
2191 abort ();
a85d7ed0 2192
0451c93c
MS
2193 rela.r_offset = (htab->sgot->output_section->vma
2194 + htab->sgot->output_offset
dc810e39 2195 + (h->got.offset &~ (bfd_vma) 1));
a85d7ed0
NC
2196
2197 /* If this is a static link, or it is a -Bsymbolic link and the
2198 symbol is defined locally or was forced to be local because
2199 of a version file, we just want to emit a RELATIVE reloc.
2200 The entry in the global offset table will already have been
2201 initialized in the relocate_section function. */
0451c93c
MS
2202 if (info->shared
2203 && (info->symbolic
2204 || h->dynindx == -1
2205 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2206 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2207 {
2208 BFD_ASSERT((h->got.offset & 1) != 0);
2209 rela.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2210 rela.r_addend = (h->root.u.def.value
a85d7ed0
NC
2211 + h->root.u.def.section->output_section->vma
2212 + h->root.u.def.section->output_offset);
2213 }
2214 else
2215 {
2216 BFD_ASSERT((h->got.offset & 1) == 0);
0451c93c 2217 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgot->contents + h->got.offset);
a85d7ed0
NC
2218 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_GLOB_DAT);
2219 rela.r_addend = 0;
2220 }
2221
0451c93c
MS
2222 loc = (Elf32_External_Rela *) htab->srelgot->contents;
2223 loc += htab->srelgot->reloc_count++;
2224 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
a85d7ed0
NC
2225 }
2226
2227 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2228 {
a85d7ed0 2229 Elf_Internal_Rela rela;
0451c93c 2230 Elf32_External_Rela *loc;
a85d7ed0
NC
2231
2232 /* This symbols needs a copy reloc. Set it up. */
2233
0451c93c
MS
2234 if (h->dynindx == -1
2235 || (h->root.type != bfd_link_hash_defined
2236 && h->root.type != bfd_link_hash_defweak)
2237 || htab->srelbss == NULL)
2238 abort ();
a85d7ed0
NC
2239
2240 rela.r_offset = (h->root.u.def.value
2241 + h->root.u.def.section->output_section->vma
2242 + h->root.u.def.section->output_offset);
2243 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_COPY);
2244 rela.r_addend = 0;
0451c93c
MS
2245 loc = (Elf32_External_Rela *) htab->srelbss->contents;
2246 loc += htab->srelbss->reloc_count++;
2247 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
a85d7ed0
NC
2248 }
2249
2250 /* Mark some specially defined symbols as absolute. */
2251 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2252 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2253 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2254 sym->st_shndx = SHN_ABS;
2255
2256 return true;
2257}
2258
0451c93c
MS
2259/* Used to decide how to sort relocs in an optimal manner for the
2260 dynamic linker, before writing them out. */
2261
2262static enum elf_reloc_type_class
2263elf_s390_reloc_type_class (rela)
2264 const Elf_Internal_Rela *rela;
2265{
2266 switch ((int) ELF32_R_TYPE (rela->r_info))
2267 {
2268 case R_390_RELATIVE:
2269 return reloc_class_relative;
2270 case R_390_JMP_SLOT:
2271 return reloc_class_plt;
2272 case R_390_COPY:
2273 return reloc_class_copy;
2274 default:
2275 return reloc_class_normal;
2276 }
2277}
2278
a85d7ed0
NC
2279/* Finish up the dynamic sections. */
2280
2281static boolean
2282elf_s390_finish_dynamic_sections (output_bfd, info)
2283 bfd *output_bfd;
2284 struct bfd_link_info *info;
2285{
0451c93c 2286 struct elf_s390_link_hash_table *htab;
a85d7ed0
NC
2287 bfd *dynobj;
2288 asection *sdyn;
a85d7ed0 2289
0451c93c
MS
2290 htab = elf_s390_hash_table (info);
2291 dynobj = htab->elf.dynobj;
a85d7ed0
NC
2292 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2293
0451c93c 2294 if (htab->elf.dynamic_sections_created)
a85d7ed0 2295 {
a85d7ed0
NC
2296 Elf32_External_Dyn *dyncon, *dynconend;
2297
0451c93c
MS
2298 if (sdyn == NULL || htab->sgot == NULL)
2299 abort ();
a85d7ed0
NC
2300
2301 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2302 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2303 for (; dyncon < dynconend; dyncon++)
2304 {
2305 Elf_Internal_Dyn dyn;
a85d7ed0 2306 asection *s;
ec338859 2307
a85d7ed0 2308 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
ec338859 2309
a85d7ed0
NC
2310 switch (dyn.d_tag)
2311 {
2312 default:
0451c93c 2313 continue;
ec338859 2314
a85d7ed0 2315 case DT_PLTGOT:
0451c93c
MS
2316 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
2317 break;
ec338859 2318
a85d7ed0 2319 case DT_JMPREL:
0451c93c 2320 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
a85d7ed0
NC
2321 break;
2322
2323 case DT_PLTRELSZ:
0451c93c 2324 s = htab->srelplt->output_section;
a85d7ed0
NC
2325 if (s->_cooked_size != 0)
2326 dyn.d_un.d_val = s->_cooked_size;
2327 else
2328 dyn.d_un.d_val = s->_raw_size;
a85d7ed0
NC
2329 break;
2330 }
0451c93c
MS
2331
2332 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
a85d7ed0
NC
2333 }
2334
2335 /* Fill in the special first entry in the procedure linkage table. */
0451c93c 2336 if (htab->splt && htab->splt->_raw_size > 0)
a85d7ed0 2337 {
0451c93c 2338 memset (htab->splt->contents, 0, PLT_FIRST_ENTRY_SIZE);
a85d7ed0 2339 if (info->shared)
0451c93c 2340 {
dc810e39 2341 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD0,
0451c93c 2342 htab->splt->contents );
dc810e39 2343 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD1,
0451c93c 2344 htab->splt->contents +4 );
dc810e39 2345 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD2,
0451c93c 2346 htab->splt->contents +8 );
dc810e39 2347 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD3,
0451c93c 2348 htab->splt->contents +12 );
dc810e39 2349 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD4,
0451c93c 2350 htab->splt->contents +16 );
a85d7ed0
NC
2351 }
2352 else
2353 {
0451c93c
MS
2354 bfd_put_32 (output_bfd, (bfd_vma)PLT_FIRST_ENTRY_WORD0,
2355 htab->splt->contents );
dc810e39 2356 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD1,
0451c93c 2357 htab->splt->contents +4 );
dc810e39 2358 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD2,
0451c93c 2359 htab->splt->contents +8 );
dc810e39 2360 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD3,
0451c93c 2361 htab->splt->contents +12 );
dc810e39 2362 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD4,
0451c93c 2363 htab->splt->contents +16 );
dc810e39 2364 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD5,
0451c93c 2365 htab->splt->contents +20 );
a85d7ed0 2366 bfd_put_32 (output_bfd,
0451c93c
MS
2367 htab->sgotplt->output_section->vma
2368 + htab->sgotplt->output_offset,
2369 htab->splt->contents + 24);
a85d7ed0 2370 }
0451c93c
MS
2371 elf_section_data (htab->splt->output_section)
2372 ->this_hdr.sh_entsize = 4;
a85d7ed0
NC
2373 }
2374
2375 }
2376
0451c93c 2377 if (htab->sgotplt)
a85d7ed0 2378 {
0451c93c
MS
2379 /* Fill in the first three entries in the global offset table. */
2380 if (htab->sgotplt->_raw_size > 0)
2381 {
2382 bfd_put_32 (output_bfd,
2383 (sdyn == NULL ? (bfd_vma) 0
2384 : sdyn->output_section->vma + sdyn->output_offset),
2385 htab->sgotplt->contents);
2386 /* One entry for shared object struct ptr. */
2387 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 4);
2388 /* One entry for _dl_runtime_resolve. */
2389 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8);
2390 }
a85d7ed0 2391
0451c93c
MS
2392 elf_section_data (htab->sgotplt->output_section)
2393 ->this_hdr.sh_entsize = 4;
2394 }
a85d7ed0
NC
2395 return true;
2396}
2397
2398static boolean
2399elf_s390_object_p (abfd)
2400 bfd *abfd;
2401{
befc3abb 2402 return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_31);
a85d7ed0
NC
2403}
2404
979f4a38
NC
2405static boolean
2406elf_s390_grok_prstatus (abfd, note)
2407 bfd * abfd;
2408 Elf_Internal_Note * note;
2409{
2410 int offset;
2411 unsigned int raw_size;
2412
2413 switch (note->descsz)
2414 {
2415 default:
2416 return false;
2417
2418 case 224: /* S/390 Linux. */
2419 /* pr_cursig */
2420 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2421
2422 /* pr_pid */
2423 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
2424
2425 /* pr_reg */
2426 offset = 72;
2427 raw_size = 144;
2428 break;
2429 }
2430
2431 /* Make a ".reg/999" section. */
2432 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2433 raw_size, note->descpos + offset);
2434}
2435
a85d7ed0
NC
2436#define TARGET_BIG_SYM bfd_elf32_s390_vec
2437#define TARGET_BIG_NAME "elf32-s390"
2438#define ELF_ARCH bfd_arch_s390
2439#define ELF_MACHINE_CODE EM_S390
2440#define ELF_MACHINE_ALT1 EM_S390_OLD
2441#define ELF_MAXPAGESIZE 0x1000
2442
2443#define elf_backend_can_gc_sections 1
51b64d56 2444#define elf_backend_can_refcount 1
a85d7ed0
NC
2445#define elf_backend_want_got_plt 1
2446#define elf_backend_plt_readonly 1
2447#define elf_backend_want_plt_sym 0
2448#define elf_backend_got_header_size 12
2449#define elf_backend_plt_header_size PLT_ENTRY_SIZE
b491616a 2450#define elf_backend_rela_normal 1
a85d7ed0
NC
2451
2452#define elf_info_to_howto elf_s390_info_to_howto
2453
a85d7ed0
NC
2454#define bfd_elf32_bfd_is_local_label_name elf_s390_is_local_label_name
2455#define bfd_elf32_bfd_link_hash_table_create elf_s390_link_hash_table_create
2456#define bfd_elf32_bfd_reloc_type_lookup elf_s390_reloc_type_lookup
2457
2458#define elf_backend_adjust_dynamic_symbol elf_s390_adjust_dynamic_symbol
2459#define elf_backend_check_relocs elf_s390_check_relocs
0451c93c
MS
2460#define elf_backend_copy_indirect_symbol elf_s390_copy_indirect_symbol
2461#define elf_backend_create_dynamic_sections elf_s390_create_dynamic_sections
a85d7ed0
NC
2462#define elf_backend_finish_dynamic_sections elf_s390_finish_dynamic_sections
2463#define elf_backend_finish_dynamic_symbol elf_s390_finish_dynamic_symbol
2464#define elf_backend_gc_mark_hook elf_s390_gc_mark_hook
2465#define elf_backend_gc_sweep_hook elf_s390_gc_sweep_hook
0451c93c 2466#define elf_backend_reloc_type_class elf_s390_reloc_type_class
a85d7ed0
NC
2467#define elf_backend_relocate_section elf_s390_relocate_section
2468#define elf_backend_size_dynamic_sections elf_s390_size_dynamic_sections
29c2fb7c 2469#define elf_backend_reloc_type_class elf_s390_reloc_type_class
979f4a38 2470#define elf_backend_grok_prstatus elf_s390_grok_prstatus
a85d7ed0
NC
2471
2472#define elf_backend_object_p elf_s390_object_p
2473
2474#include "elf32-target.h"
This page took 0.202121 seconds and 4 git commands to generate.