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