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