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