Support for more than 64k ELF sections.
[binutils.git] / bfd / elf64-s390.c
blob0e3abf395140ca1752c0307911b936a4ab7d1f01
1 /* IBM S/390-specific support for 64-bit ELF
2 Copyright 2000, 2001 Free Software Foundation, Inc.
3 Contributed Martin Schwidefsky (schwidefsky@de.ibm.com).
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
28 static reloc_howto_type *elf_s390_reloc_type_lookup
29 PARAMS ((bfd *, bfd_reloc_code_real_type));
30 static void elf_s390_info_to_howto
31 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
32 static boolean elf_s390_is_local_label_name
33 PARAMS ((bfd *, const char *));
34 static struct bfd_hash_entry *link_hash_newfunc
35 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
36 static struct bfd_link_hash_table *elf_s390_link_hash_table_create
37 PARAMS ((bfd *));
38 static boolean create_got_section
39 PARAMS((bfd *, struct bfd_link_info *));
40 static boolean elf_s390_create_dynamic_sections
41 PARAMS((bfd *, struct bfd_link_info *));
42 static void elf_s390_copy_indirect_symbol
43 PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
44 static boolean elf_s390_check_relocs
45 PARAMS ((bfd *, struct bfd_link_info *, asection *,
46 const Elf_Internal_Rela *));
47 static asection *elf_s390_gc_mark_hook
48 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
49 struct elf_link_hash_entry *, Elf_Internal_Sym *));
50 static boolean elf_s390_gc_sweep_hook
51 PARAMS ((bfd *, struct bfd_link_info *, asection *,
52 const Elf_Internal_Rela *));
53 static boolean elf_s390_adjust_dynamic_symbol
54 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
55 static boolean allocate_dynrelocs
56 PARAMS ((struct elf_link_hash_entry *, PTR));
57 static boolean readonly_dynrelocs
58 PARAMS ((struct elf_link_hash_entry *, PTR));
59 static boolean elf_s390_size_dynamic_sections
60 PARAMS ((bfd *, struct bfd_link_info *));
61 static boolean elf_s390_relocate_section
62 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
63 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
64 static boolean elf_s390_finish_dynamic_symbol
65 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
66 Elf_Internal_Sym *));
67 static enum elf_reloc_type_class elf_s390_reloc_type_class
68 PARAMS ((const Elf_Internal_Rela *));
69 static boolean elf_s390_finish_dynamic_sections
70 PARAMS ((bfd *, struct bfd_link_info *));
71 static boolean elf_s390_object_p PARAMS ((bfd *));
73 #define USE_RELA 1 /* We want RELA relocations, not REL. */
75 #include "elf/s390.h"
77 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
78 from smaller values. Start with zero, widen, *then* decrement. */
79 #define MINUS_ONE (((bfd_vma)0) - 1)
81 /* The relocation "howto" table. */
82 static reloc_howto_type elf_howto_table[] =
84 HOWTO (R_390_NONE, /* type */
85 0, /* rightshift */
86 0, /* size (0 = byte, 1 = short, 2 = long) */
87 0, /* bitsize */
88 false, /* pc_relative */
89 0, /* bitpos */
90 complain_overflow_dont, /* complain_on_overflow */
91 bfd_elf_generic_reloc, /* special_function */
92 "R_390_NONE", /* name */
93 false, /* partial_inplace */
94 0, /* src_mask */
95 0, /* dst_mask */
96 false), /* pcrel_offset */
98 HOWTO(R_390_8, 0, 0, 8, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_8", false, 0,0x000000ff, false),
99 HOWTO(R_390_12, 0, 1, 12, false, 0, complain_overflow_dont, bfd_elf_generic_reloc, "R_390_12", false, 0,0x00000fff, false),
100 HOWTO(R_390_16, 0, 1, 16, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_16", false, 0,0x0000ffff, false),
101 HOWTO(R_390_32, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_32", false, 0,0xffffffff, false),
102 HOWTO(R_390_PC32, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC32", false, 0,0xffffffff, true),
103 HOWTO(R_390_GOT12, 0, 1, 12, false, 0, complain_overflow_dont, bfd_elf_generic_reloc, "R_390_GOT12", false, 0,0x00000fff, false),
104 HOWTO(R_390_GOT32, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT32", false, 0,0xffffffff, false),
105 HOWTO(R_390_PLT32, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT32", false, 0,0xffffffff, true),
106 HOWTO(R_390_COPY, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_COPY", false, 0,MINUS_ONE, false),
107 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),
108 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),
109 HOWTO(R_390_RELATIVE, 0, 4, 64, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_RELATIVE",false, 0,MINUS_ONE, false),
110 HOWTO(R_390_GOTOFF, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTOFF", false, 0,MINUS_ONE, false),
111 HOWTO(R_390_GOTPC, 0, 4, 64, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTPC", false, 0,MINUS_ONE, true),
112 HOWTO(R_390_GOT16, 0, 1, 16, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT16", false, 0,0x0000ffff, false),
113 HOWTO(R_390_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16", false, 0,0x0000ffff, true),
114 HOWTO(R_390_PC16DBL, 1, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16DBL", false, 0,0x0000ffff, true),
115 HOWTO(R_390_PLT16DBL, 1, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT16DBL", false, 0,0x0000ffff, true),
116 HOWTO(R_390_PC32DBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC32DBL", false, 0,0xffffffff, true),
117 HOWTO(R_390_PLT32DBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT32DBL", false, 0,0xffffffff, true),
118 HOWTO(R_390_GOTPCDBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTPCDBL", false, 0,MINUS_ONE, true),
119 HOWTO(R_390_64, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_64", false, 0,MINUS_ONE, false),
120 HOWTO(R_390_PC64, 0, 4, 64, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC64", false, 0,MINUS_ONE, true),
121 HOWTO(R_390_GOT64, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT64", false, 0,MINUS_ONE, false),
122 HOWTO(R_390_PLT64, 0, 4, 64, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT64", false, 0,MINUS_ONE, true),
123 HOWTO(R_390_GOTENT, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTENT", false, 0,MINUS_ONE, true),
126 /* GNU extension to record C++ vtable hierarchy. */
127 static reloc_howto_type elf64_s390_vtinherit_howto =
128 HOWTO (R_390_GNU_VTINHERIT, 0,4,0,false,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", false,0, 0, false);
129 static reloc_howto_type elf64_s390_vtentry_howto =
130 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);
132 static reloc_howto_type *
133 elf_s390_reloc_type_lookup (abfd, code)
134 bfd *abfd ATTRIBUTE_UNUSED;
135 bfd_reloc_code_real_type code;
137 switch (code)
139 case BFD_RELOC_NONE:
140 return &elf_howto_table[(int) R_390_NONE];
141 case BFD_RELOC_8:
142 return &elf_howto_table[(int) R_390_8];
143 case BFD_RELOC_390_12:
144 return &elf_howto_table[(int) R_390_12];
145 case BFD_RELOC_16:
146 return &elf_howto_table[(int) R_390_16];
147 case BFD_RELOC_32:
148 return &elf_howto_table[(int) R_390_32];
149 case BFD_RELOC_CTOR:
150 return &elf_howto_table[(int) R_390_32];
151 case BFD_RELOC_32_PCREL:
152 return &elf_howto_table[(int) R_390_PC32];
153 case BFD_RELOC_390_GOT12:
154 return &elf_howto_table[(int) R_390_GOT12];
155 case BFD_RELOC_32_GOT_PCREL:
156 return &elf_howto_table[(int) R_390_GOT32];
157 case BFD_RELOC_390_PLT32:
158 return &elf_howto_table[(int) R_390_PLT32];
159 case BFD_RELOC_390_COPY:
160 return &elf_howto_table[(int) R_390_COPY];
161 case BFD_RELOC_390_GLOB_DAT:
162 return &elf_howto_table[(int) R_390_GLOB_DAT];
163 case BFD_RELOC_390_JMP_SLOT:
164 return &elf_howto_table[(int) R_390_JMP_SLOT];
165 case BFD_RELOC_390_RELATIVE:
166 return &elf_howto_table[(int) R_390_RELATIVE];
167 case BFD_RELOC_32_GOTOFF:
168 return &elf_howto_table[(int) R_390_GOTOFF];
169 case BFD_RELOC_390_GOTPC:
170 return &elf_howto_table[(int) R_390_GOTPC];
171 case BFD_RELOC_390_GOT16:
172 return &elf_howto_table[(int) R_390_GOT16];
173 case BFD_RELOC_16_PCREL:
174 return &elf_howto_table[(int) R_390_PC16];
175 case BFD_RELOC_390_PC16DBL:
176 return &elf_howto_table[(int) R_390_PC16DBL];
177 case BFD_RELOC_390_PLT16DBL:
178 return &elf_howto_table[(int) R_390_PLT16DBL];
179 case BFD_RELOC_VTABLE_INHERIT:
180 return &elf64_s390_vtinherit_howto;
181 case BFD_RELOC_VTABLE_ENTRY:
182 return &elf64_s390_vtentry_howto;
183 case BFD_RELOC_390_PC32DBL:
184 return &elf_howto_table[(int) R_390_PC32DBL];
185 case BFD_RELOC_390_PLT32DBL:
186 return &elf_howto_table[(int) R_390_PLT32DBL];
187 case BFD_RELOC_390_GOTPCDBL:
188 return &elf_howto_table[(int) R_390_GOTPCDBL];
189 case BFD_RELOC_64:
190 return &elf_howto_table[(int) R_390_64];
191 case BFD_RELOC_64_PCREL:
192 return &elf_howto_table[(int) R_390_PC64];
193 case BFD_RELOC_390_GOT64:
194 return &elf_howto_table[(int) R_390_GOT64];
195 case BFD_RELOC_390_PLT64:
196 return &elf_howto_table[(int) R_390_PLT64];
197 case BFD_RELOC_390_GOTENT:
198 return &elf_howto_table[(int) R_390_GOTENT];
199 default:
200 break;
202 return 0;
205 /* We need to use ELF64_R_TYPE so we have our own copy of this function,
206 and elf64-s390.c has its own copy. */
208 static void
209 elf_s390_info_to_howto (abfd, cache_ptr, dst)
210 bfd *abfd ATTRIBUTE_UNUSED;
211 arelent *cache_ptr;
212 Elf_Internal_Rela *dst;
214 switch (ELF64_R_TYPE(dst->r_info))
216 case R_390_GNU_VTINHERIT:
217 cache_ptr->howto = &elf64_s390_vtinherit_howto;
218 break;
220 case R_390_GNU_VTENTRY:
221 cache_ptr->howto = &elf64_s390_vtentry_howto;
222 break;
224 default:
225 BFD_ASSERT (ELF64_R_TYPE(dst->r_info) < (unsigned int) R_390_max);
226 cache_ptr->howto = &elf_howto_table[ELF64_R_TYPE(dst->r_info)];
230 static boolean
231 elf_s390_is_local_label_name (abfd, name)
232 bfd *abfd;
233 const char *name;
235 if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L'))
236 return true;
238 return _bfd_elf_is_local_label_name (abfd, name);
241 /* Functions for the 390 ELF linker. */
243 /* The name of the dynamic interpreter. This is put in the .interp
244 section. */
246 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
248 /* The size in bytes of the first entry in the procedure linkage table. */
249 #define PLT_FIRST_ENTRY_SIZE 32
250 /* The size in bytes of an entry in the procedure linkage table. */
251 #define PLT_ENTRY_SIZE 32
253 #define GOT_ENTRY_SIZE 8
255 /* The first three entries in a procedure linkage table are reserved,
256 and the initial contents are unimportant (we zero them out).
257 Subsequent entries look like this. See the SVR4 ABI 386
258 supplement to see how this works. */
260 /* For the s390, simple addr offset can only be 0 - 4096.
261 To use the full 16777216 TB address space, several instructions
262 are needed to load an address in a register and execute
263 a branch( or just saving the address)
265 Furthermore, only r 0 and 1 are free to use!!! */
267 /* The first 3 words in the GOT are then reserved.
268 Word 0 is the address of the dynamic table.
269 Word 1 is a pointer to a structure describing the object
270 Word 2 is used to point to the loader entry address.
272 The code for PLT entries looks like this:
274 The GOT holds the address in the PLT to be executed.
275 The loader then gets:
276 24(15) = Pointer to the structure describing the object.
277 28(15) = Offset in symbol table
278 The loader must then find the module where the function is
279 and insert the address in the GOT.
281 PLT1: LARL 1,<fn>@GOTENT # 6 bytes Load address of GOT entry in r1
282 LG 1,0(1) # 6 bytes Load address from GOT in r1
283 BCR 15,1 # 2 bytes Jump to address
284 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
285 LGF 1,12(1) # 6 bytes Load offset in symbl table in r1
286 BRCL 15,-x # 6 bytes Jump to start of PLT
287 .long ? # 4 bytes offset into symbol table
289 Total = 32 bytes per PLT entry
290 Fixup at offset 2: relative address to GOT entry
291 Fixup at offset 22: relative branch to PLT0
292 Fixup at offset 28: 32 bit offset into symbol table
294 A 32 bit offset into the symbol table is enough. It allows for symbol
295 tables up to a size of 2 gigabyte. A single dynamic object (the main
296 program, any shared library) is limited to 4GB in size and I want to see
297 the program that manages to have a symbol table of more than 2 GB with a
298 total size of at max 4 GB. */
300 #define PLT_ENTRY_WORD0 (bfd_vma) 0xc0100000
301 #define PLT_ENTRY_WORD1 (bfd_vma) 0x0000e310
302 #define PLT_ENTRY_WORD2 (bfd_vma) 0x10000004
303 #define PLT_ENTRY_WORD3 (bfd_vma) 0x07f10d10
304 #define PLT_ENTRY_WORD4 (bfd_vma) 0xe310100c
305 #define PLT_ENTRY_WORD5 (bfd_vma) 0x0014c0f4
306 #define PLT_ENTRY_WORD6 (bfd_vma) 0x00000000
307 #define PLT_ENTRY_WORD7 (bfd_vma) 0x00000000
309 /* The first PLT entry pushes the offset into the symbol table
310 from R1 onto the stack at 8(15) and the loader object info
311 at 12(15), loads the loader address in R1 and jumps to it. */
313 /* The first entry in the PLT:
315 PLT0:
316 STG 1,56(15) # r1 contains the offset into the symbol table
317 LARL 1,_GLOBAL_OFFSET_TABLE # load address of global offset table
318 MVC 48(8,15),8(1) # move loader ino (object struct address) to stack
319 LG 1,16(1) # get entry address of loader
320 BCR 15,1 # jump to loader
322 Fixup at offset 8: relative address to start of GOT. */
324 #define PLT_FIRST_ENTRY_WORD0 (bfd_vma) 0xe310f038
325 #define PLT_FIRST_ENTRY_WORD1 (bfd_vma) 0x0024c010
326 #define PLT_FIRST_ENTRY_WORD2 (bfd_vma) 0x00000000
327 #define PLT_FIRST_ENTRY_WORD3 (bfd_vma) 0xd207f030
328 #define PLT_FIRST_ENTRY_WORD4 (bfd_vma) 0x1008e310
329 #define PLT_FIRST_ENTRY_WORD5 (bfd_vma) 0x10100004
330 #define PLT_FIRST_ENTRY_WORD6 (bfd_vma) 0x07f10700
331 #define PLT_FIRST_ENTRY_WORD7 (bfd_vma) 0x07000700
333 /* The s390 linker needs to keep track of the number of relocs that it
334 decides to copy as dynamic relocs in check_relocs for each symbol.
335 This is so that it can later discard them if they are found to be
336 unnecessary. We store the information in a field extending the
337 regular ELF linker hash table. */
339 struct elf_s390_dyn_relocs
341 struct elf_s390_dyn_relocs *next;
343 /* The input section of the reloc. */
344 asection *sec;
346 /* Total number of relocs copied for the input section. */
347 bfd_size_type count;
349 /* Number of pc-relative relocs copied for the input section. */
350 bfd_size_type pc_count;
353 /* s390 ELF linker hash entry. */
355 struct elf_s390_link_hash_entry
357 struct elf_link_hash_entry elf;
359 /* Track dynamic relocs copied for this symbol. */
360 struct elf_s390_dyn_relocs *dyn_relocs;
363 /* s390 ELF linker hash table. */
365 struct elf_s390_link_hash_table
367 struct elf_link_hash_table elf;
369 /* Short-cuts to get to dynamic linker sections. */
370 asection *sgot;
371 asection *sgotplt;
372 asection *srelgot;
373 asection *splt;
374 asection *srelplt;
375 asection *sdynbss;
376 asection *srelbss;
378 /* Small local sym to section mapping cache. */
379 struct sym_sec_cache sym_sec;
382 /* Get the s390 ELF linker hash table from a link_info structure. */
384 #define elf_s390_hash_table(p) \
385 ((struct elf_s390_link_hash_table *) ((p)->hash))
387 /* Create an entry in an s390 ELF linker hash table. */
389 static struct bfd_hash_entry *
390 link_hash_newfunc (entry, table, string)
391 struct bfd_hash_entry *entry;
392 struct bfd_hash_table *table;
393 const char *string;
395 /* Allocate the structure if it has not already been allocated by a
396 subclass. */
397 if (entry == NULL)
399 entry = bfd_hash_allocate (table,
400 sizeof (struct elf_s390_link_hash_entry));
401 if (entry == NULL)
402 return entry;
405 /* Call the allocation method of the superclass. */
406 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
407 if (entry != NULL)
409 struct elf_s390_link_hash_entry *eh;
411 eh = (struct elf_s390_link_hash_entry *) entry;
412 eh->dyn_relocs = NULL;
415 return entry;
418 /* Create an s390 ELF linker hash table. */
420 static struct bfd_link_hash_table *
421 elf_s390_link_hash_table_create (abfd)
422 bfd *abfd;
424 struct elf_s390_link_hash_table *ret;
425 bfd_size_type amt = sizeof (struct elf_s390_link_hash_table);
427 ret = (struct elf_s390_link_hash_table *) bfd_alloc (abfd, amt);
428 if (ret == NULL)
429 return NULL;
431 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
433 bfd_release (abfd, ret);
434 return NULL;
437 ret->sgot = NULL;
438 ret->sgotplt = NULL;
439 ret->srelgot = NULL;
440 ret->splt = NULL;
441 ret->srelplt = NULL;
442 ret->sdynbss = NULL;
443 ret->srelbss = NULL;
444 ret->sym_sec.abfd = NULL;
446 return &ret->elf.root;
449 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
450 shortcuts to them in our hash table. */
452 static boolean
453 create_got_section (dynobj, info)
454 bfd *dynobj;
455 struct bfd_link_info *info;
457 struct elf_s390_link_hash_table *htab;
459 if (! _bfd_elf_create_got_section (dynobj, info))
460 return false;
462 htab = elf_s390_hash_table (info);
463 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
464 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
465 if (!htab->sgot || !htab->sgotplt)
466 abort ();
468 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
469 if (htab->srelgot == NULL
470 || ! bfd_set_section_flags (dynobj, htab->srelgot,
471 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
472 | SEC_IN_MEMORY | SEC_LINKER_CREATED
473 | SEC_READONLY))
474 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
475 return false;
476 return true;
479 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
480 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
481 hash table. */
483 static boolean
484 elf_s390_create_dynamic_sections (dynobj, info)
485 bfd *dynobj;
486 struct bfd_link_info *info;
488 struct elf_s390_link_hash_table *htab;
490 htab = elf_s390_hash_table (info);
491 if (!htab->sgot && !create_got_section (dynobj, info))
492 return false;
494 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
495 return false;
497 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
498 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
499 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
500 if (!info->shared)
501 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
503 if (!htab->splt || !htab->srelplt || !htab->sdynbss
504 || (!info->shared && !htab->srelbss))
505 abort ();
507 return true;
510 /* Copy the extra info we tack onto an elf_link_hash_entry. */
512 static void
513 elf_s390_copy_indirect_symbol (dir, ind)
514 struct elf_link_hash_entry *dir, *ind;
516 struct elf_s390_link_hash_entry *edir, *eind;
518 edir = (struct elf_s390_link_hash_entry *) dir;
519 eind = (struct elf_s390_link_hash_entry *) ind;
521 if (eind->dyn_relocs != NULL)
523 if (edir->dyn_relocs != NULL)
525 struct elf_s390_dyn_relocs **pp;
526 struct elf_s390_dyn_relocs *p;
528 if (ind->root.type == bfd_link_hash_indirect)
529 abort ();
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; )
535 struct elf_s390_dyn_relocs *q;
537 for (q = edir->dyn_relocs; q != NULL; q = q->next)
538 if (q->sec == p->sec)
540 q->pc_count += p->pc_count;
541 q->count += p->count;
542 *pp = p->next;
543 break;
545 if (q == NULL)
546 pp = &p->next;
548 *pp = edir->dyn_relocs;
551 edir->dyn_relocs = eind->dyn_relocs;
552 eind->dyn_relocs = NULL;
555 _bfd_elf_link_hash_copy_indirect (dir, ind);
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. */
562 static boolean
563 elf_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;
569 struct elf_s390_link_hash_table *htab;
570 Elf_Internal_Shdr *symtab_hdr;
571 struct elf_link_hash_entry **sym_hashes;
572 const Elf_Internal_Rela *rel;
573 const Elf_Internal_Rela *rel_end;
574 asection *sreloc;
576 if (info->relocateable)
577 return true;
579 htab = elf_s390_hash_table (info);
580 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
581 sym_hashes = elf_sym_hashes (abfd);
583 sreloc = NULL;
585 rel_end = relocs + sec->reloc_count;
586 for (rel = relocs; rel < rel_end; rel++)
588 unsigned long r_symndx;
589 struct elf_link_hash_entry *h;
591 r_symndx = ELF64_R_SYM (rel->r_info);
593 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
595 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
596 bfd_archive_filename (abfd),
597 r_symndx);
598 return false;
601 if (r_symndx < symtab_hdr->sh_info)
602 h = NULL;
603 else
604 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
606 switch (ELF64_R_TYPE (rel->r_info))
608 case R_390_GOT12:
609 case R_390_GOT16:
610 case R_390_GOT32:
611 case R_390_GOT64:
612 case R_390_GOTENT:
613 /* This symbol requires a global offset table entry. */
614 if (h != NULL)
616 h->got.refcount += 1;
618 else
620 bfd_signed_vma *local_got_refcounts;
622 /* This is a global offset table entry for a local symbol. */
623 local_got_refcounts = elf_local_got_refcounts (abfd);
624 if (local_got_refcounts == NULL)
626 bfd_size_type size;
628 size = symtab_hdr->sh_info;
629 size *= sizeof (bfd_signed_vma);
630 local_got_refcounts = ((bfd_signed_vma *)
631 bfd_zalloc (abfd, size));
632 if (local_got_refcounts == NULL)
633 return false;
634 elf_local_got_refcounts (abfd) = local_got_refcounts;
636 local_got_refcounts[r_symndx] += 1;
638 /* Fall through */
640 case R_390_GOTOFF:
641 case R_390_GOTPC:
642 case R_390_GOTPCDBL:
643 if (htab->sgot == NULL)
645 if (htab->elf.dynobj == NULL)
646 htab->elf.dynobj = abfd;
647 if (!create_got_section (htab->elf.dynobj, info))
648 return false;
650 break;
652 case R_390_PLT16DBL:
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
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. */
663 /* If this is a local symbol, we resolve it directly without
664 creating a procedure linkage table entry. */
665 if (h == NULL)
666 continue;
668 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
669 h->plt.refcount += 1;
670 break;
672 case R_390_8:
673 case R_390_16:
674 case R_390_32:
675 case R_390_64:
676 case R_390_PC16:
677 case R_390_PC16DBL:
678 case R_390_PC32:
679 case R_390_PC32DBL:
680 case R_390_PC64:
681 if (h != NULL && !info->shared)
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;
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;
696 /* If we are creating a shared library, and this is a reloc
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.
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)))
736 struct elf_s390_dyn_relocs *p;
737 struct elf_s390_dyn_relocs **head;
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. */
742 if (sreloc == NULL)
744 const char *name;
745 bfd *dynobj;
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)
752 return false;
754 if (strncmp (name, ".rela", 5) != 0
755 || strcmp (bfd_get_section_name (abfd, sec),
756 name + 5) != 0)
758 (*_bfd_error_handler)
759 (_("%s: bad relocation section name `%s\'"),
760 bfd_archive_filename (abfd), name);
763 if (htab->elf.dynobj == NULL)
764 htab->elf.dynobj = abfd;
766 dynobj = htab->elf.dynobj;
767 sreloc = bfd_get_section_by_name (dynobj, name);
768 if (sreloc == NULL)
770 flagword flags;
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)
779 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
780 return false;
782 elf_section_data (sec)->sreloc = sreloc;
785 /* If this is a global symbol, we count the number of
786 relocations we need for this symbol. */
787 if (h != NULL)
789 head = &((struct elf_s390_link_hash_entry *) h)->dyn_relocs;
791 else
793 /* Track dynamic relocs needed for local syms too.
794 We really need local syms available to do this
795 easily. Oh well. */
797 asection *s;
798 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
799 sec, r_symndx);
800 if (s == NULL)
801 return false;
803 head = ((struct elf_s390_dyn_relocs **)
804 &elf_section_data (s)->local_dynrel);
807 p = *head;
808 if (p == NULL || p->sec != sec)
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)
814 return false;
815 p->next = *head;
816 *head = p;
817 p->sec = sec;
818 p->count = 0;
819 p->pc_count = 0;
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;
830 break;
832 /* This relocation describes the C++ object vtable hierarchy.
833 Reconstruct it for later use during GC. */
834 case R_390_GNU_VTINHERIT:
835 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
836 return false;
837 break;
839 /* This relocation describes which C++ vtable entries are actually
840 used. Record for later use during GC. */
841 case R_390_GNU_VTENTRY:
842 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_offset))
843 return false;
844 break;
846 default:
847 break;
851 return true;
854 /* Return the section that should be marked against GC for a given
855 relocation. */
857 static asection *
858 elf_s390_gc_mark_hook (abfd, info, rel, h, sym)
859 bfd *abfd;
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;
865 if (h != NULL)
867 switch (ELF64_R_TYPE (rel->r_info))
869 case R_390_GNU_VTINHERIT:
870 case R_390_GNU_VTENTRY:
871 break;
873 default:
874 switch (h->root.type)
876 case bfd_link_hash_defined:
877 case bfd_link_hash_defweak:
878 return h->root.u.def.section;
880 case bfd_link_hash_common:
881 return h->root.u.c.p->section;
883 default:
884 break;
888 else
890 return bfd_section_from_elf_index (abfd, sym->st_shndx);
893 return NULL;
896 /* Update the got entry reference counts for the section being removed. */
898 static boolean
899 elf_s390_gc_sweep_hook (abfd, info, sec, relocs)
900 bfd *abfd;
901 struct bfd_link_info *info;
902 asection *sec;
903 const Elf_Internal_Rela *relocs;
905 Elf_Internal_Shdr *symtab_hdr;
906 struct elf_link_hash_entry **sym_hashes;
907 bfd_signed_vma *local_got_refcounts;
908 const Elf_Internal_Rela *rel, *relend;
909 unsigned long r_symndx;
910 struct elf_link_hash_entry *h;
912 elf_section_data (sec)->local_dynrel = NULL;
914 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
915 sym_hashes = elf_sym_hashes (abfd);
916 local_got_refcounts = elf_local_got_refcounts (abfd);
918 relend = relocs + sec->reloc_count;
919 for (rel = relocs; rel < relend; rel++)
920 switch (ELF64_R_TYPE (rel->r_info))
922 case R_390_GOT12:
923 case R_390_GOT16:
924 case R_390_GOT32:
925 case R_390_GOT64:
926 case R_390_GOTOFF:
927 case R_390_GOTPC:
928 case R_390_GOTPCDBL:
929 case R_390_GOTENT:
930 r_symndx = ELF64_R_SYM (rel->r_info);
931 if (r_symndx >= symtab_hdr->sh_info)
933 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
934 if (h->got.refcount > 0)
935 h->got.refcount -= 1;
937 else if (local_got_refcounts != NULL)
939 if (local_got_refcounts[r_symndx] > 0)
940 local_got_refcounts[r_symndx] -= 1;
942 break;
944 case R_390_8:
945 case R_390_12:
946 case R_390_16:
947 case R_390_32:
948 case R_390_64:
949 case R_390_PC16:
950 case R_390_PC16DBL:
951 case R_390_PC32:
952 case R_390_PC32DBL:
953 case R_390_PC64:
954 r_symndx = ELF64_R_SYM (rel->r_info);
955 if (r_symndx >= symtab_hdr->sh_info)
957 struct elf_s390_link_hash_entry *eh;
958 struct elf_s390_dyn_relocs **pp;
959 struct elf_s390_dyn_relocs *p;
961 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
963 if (!info->shared && h->plt.refcount > 0)
964 h->plt.refcount -= 1;
966 eh = (struct elf_s390_link_hash_entry *) h;
968 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
969 if (p->sec == sec)
971 if (ELF64_R_TYPE (rel->r_info) == R_390_PC16
972 || ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL
973 || ELF64_R_TYPE (rel->r_info) == R_390_PC32)
974 p->pc_count -= 1;
975 p->count -= 1;
976 if (p->count == 0)
977 *pp = p->next;
978 break;
981 break;
983 case R_390_PLT16DBL:
984 case R_390_PLT32:
985 case R_390_PLT32DBL:
986 case R_390_PLT64:
987 r_symndx = ELF64_R_SYM (rel->r_info);
988 if (r_symndx >= symtab_hdr->sh_info)
990 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
991 if (h->plt.refcount > 0)
992 h->plt.refcount -= 1;
994 break;
996 default:
997 break;
1000 return true;
1003 /* Adjust a symbol defined by a dynamic object and referenced by a
1004 regular object. The current definition is in some section of the
1005 dynamic object, but we're not including those sections. We have to
1006 change the definition to something the rest of the link can
1007 understand. */
1009 static boolean
1010 elf_s390_adjust_dynamic_symbol (info, h)
1011 struct bfd_link_info *info;
1012 struct elf_link_hash_entry *h;
1014 struct elf_s390_link_hash_table *htab;
1015 struct elf_s390_link_hash_entry * eh;
1016 struct elf_s390_dyn_relocs *p;
1017 asection *s;
1018 unsigned int power_of_two;
1020 /* If this is a function, put it in the procedure linkage table. We
1021 will fill in the contents of the procedure linkage table later
1022 (although we could actually do it here). */
1023 if (h->type == STT_FUNC
1024 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1026 if (h->plt.refcount <= 0
1027 || (! info->shared
1028 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1029 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
1031 /* This case can occur if we saw a PLT32 reloc in an input
1032 file, but the symbol was never referred to by a dynamic
1033 object, or if all references were garbage collected. In
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. */
1036 h->plt.offset = (bfd_vma) -1;
1037 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1040 return true;
1042 else
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. */
1048 h->plt.offset = (bfd_vma) -1;
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)
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;
1059 return true;
1062 /* This is a reference to a symbol defined by a dynamic object which
1063 is not a function. */
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)
1070 return true;
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)
1075 return true;
1077 /* If -z nocopyreloc was given, we won't generate them either. */
1078 if (info->nocopyreloc)
1080 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1081 return true;
1084 eh = (struct elf_s390_link_hash_entry *) h;
1085 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1087 s = p->sec->output_section;
1088 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1089 break;
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)
1096 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1097 return true;
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. */
1110 htab = elf_s390_hash_table (info);
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. */
1115 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1117 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
1118 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
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;
1127 /* Apply the required alignment. */
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))
1132 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1133 return false;
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;
1140 /* Increment the section size to make room for the symbol. */
1141 s->_raw_size += h->size;
1143 return true;
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))
1157 /* Allocate space in .plt, .got and associated reloc sections for
1158 dynamic relocs. */
1160 static boolean
1161 allocate_dynrelocs (h, inf)
1162 struct elf_link_hash_entry *h;
1163 PTR inf;
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;
1170 if (h->root.type == bfd_link_hash_indirect
1171 || h->root.type == bfd_link_hash_warning)
1172 return true;
1174 info = (struct bfd_link_info *) inf;
1175 htab = elf_s390_hash_table (info);
1177 if (htab->elf.dynamic_sections_created
1178 && h->plt.refcount > 0)
1180 /* Make sure this symbol is output as a dynamic symbol.
1181 Undefined weak syms won't yet be marked as dynamic. */
1182 if (h->dynindx == -1
1183 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1185 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1186 return false;
1189 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1191 asection *s = htab->splt;
1193 /* If this is the first .plt entry, make room for the special
1194 first entry. */
1195 if (s->_raw_size == 0)
1196 s->_raw_size += PLT_FIRST_ENTRY_SIZE;
1198 h->plt.offset = s->_raw_size;
1200 /* If this symbol is not defined in a regular file, and we are
1201 not generating a shared library, then set the symbol to this
1202 location in the .plt. This is required to make function
1203 pointers compare as equal between the normal executable and
1204 the shared library. */
1205 if (! info->shared
1206 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1208 h->root.u.def.section = s;
1209 h->root.u.def.value = h->plt.offset;
1212 /* Make room for this entry. */
1213 s->_raw_size += PLT_ENTRY_SIZE;
1215 /* We also need to make an entry in the .got.plt section, which
1216 will be placed in the .got section by the linker script. */
1217 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1219 /* We also need to make an entry in the .rela.plt section. */
1220 htab->srelplt->_raw_size += sizeof (Elf64_External_Rela);
1222 else
1224 h->plt.offset = (bfd_vma) -1;
1225 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1228 else
1230 h->plt.offset = (bfd_vma) -1;
1231 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1234 if (h->got.refcount > 0)
1236 asection *s;
1237 boolean dyn;
1239 /* Make sure this symbol is output as a dynamic symbol.
1240 Undefined weak syms won't yet be marked as dynamic. */
1241 if (h->dynindx == -1
1242 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1244 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1245 return false;
1248 s = htab->sgot;
1249 h->got.offset = s->_raw_size;
1250 s->_raw_size += GOT_ENTRY_SIZE;
1251 dyn = htab->elf.dynamic_sections_created;
1252 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1253 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1255 else
1256 h->got.offset = (bfd_vma) -1;
1258 eh = (struct elf_s390_link_hash_entry *) h;
1259 if (eh->dyn_relocs == NULL)
1260 return true;
1262 /* In the shared -Bsymbolic case, discard space allocated for
1263 dynamic pc-relative relocs against symbols which turn out to be
1264 defined in regular objects. For the normal shared case, discard
1265 space for pc-relative relocs that have become local due to symbol
1266 visibility changes. */
1268 if (info->shared)
1270 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1271 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1272 || info->symbolic))
1274 struct elf_s390_dyn_relocs **pp;
1276 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1278 p->count -= p->pc_count;
1279 p->pc_count = 0;
1280 if (p->count == 0)
1281 *pp = p->next;
1282 else
1283 pp = &p->next;
1287 else
1289 /* For the non-shared case, discard space for relocs against
1290 symbols which turn out to need copy relocs or are not
1291 dynamic. */
1293 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1294 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1295 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1296 || (htab->elf.dynamic_sections_created
1297 && (h->root.type == bfd_link_hash_undefweak
1298 || h->root.type == bfd_link_hash_undefined))))
1300 /* Make sure this symbol is output as a dynamic symbol.
1301 Undefined weak syms won't yet be marked as dynamic. */
1302 if (h->dynindx == -1
1303 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1305 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1306 return false;
1309 /* If that succeeded, we know we'll be keeping all the
1310 relocs. */
1311 if (h->dynindx != -1)
1312 goto keep;
1315 eh->dyn_relocs = NULL;
1317 keep: ;
1320 /* Finally, allocate space. */
1321 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1323 asection *sreloc = elf_section_data (p->sec)->sreloc;
1324 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
1327 return true;
1330 /* Find any dynamic relocs that apply to read-only sections. */
1332 static boolean
1333 readonly_dynrelocs (h, inf)
1334 struct elf_link_hash_entry *h;
1335 PTR inf;
1337 struct elf_s390_link_hash_entry *eh;
1338 struct elf_s390_dyn_relocs *p;
1340 eh = (struct elf_s390_link_hash_entry *) h;
1341 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1343 asection *s = p->sec->output_section;
1345 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1347 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1349 info->flags |= DF_TEXTREL;
1351 /* Not an error, just cut short the traversal. */
1352 return false;
1355 return true;
1358 /* Set the sizes of the dynamic sections. */
1360 static boolean
1361 elf_s390_size_dynamic_sections (output_bfd, info)
1362 bfd *output_bfd ATTRIBUTE_UNUSED;
1363 struct bfd_link_info *info;
1365 struct elf_s390_link_hash_table *htab;
1366 bfd *dynobj;
1367 asection *s;
1368 boolean relocs;
1369 bfd *ibfd;
1371 htab = elf_s390_hash_table (info);
1372 dynobj = htab->elf.dynobj;
1373 if (dynobj == NULL)
1374 abort ();
1376 if (htab->elf.dynamic_sections_created)
1378 /* Set the contents of the .interp section to the interpreter. */
1379 if (! info->shared)
1381 s = bfd_get_section_by_name (dynobj, ".interp");
1382 if (s == NULL)
1383 abort ();
1384 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1385 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1389 /* Set up .got offsets for local syms, and space for local dynamic
1390 relocs. */
1391 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1393 bfd_signed_vma *local_got;
1394 bfd_signed_vma *end_local_got;
1395 bfd_size_type locsymcount;
1396 Elf_Internal_Shdr *symtab_hdr;
1397 asection *srela;
1399 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1400 continue;
1402 for (s = ibfd->sections; s != NULL; s = s->next)
1404 struct elf_s390_dyn_relocs *p;
1406 for (p = *((struct elf_s390_dyn_relocs **)
1407 &elf_section_data (s)->local_dynrel);
1408 p != NULL;
1409 p = p->next)
1411 if (!bfd_is_abs_section (p->sec)
1412 && bfd_is_abs_section (p->sec->output_section))
1414 /* Input section has been discarded, either because
1415 it is a copy of a linkonce section or due to
1416 linker script /DISCARD/, so we'll be discarding
1417 the relocs too. */
1419 else
1421 srela = elf_section_data (p->sec)->sreloc;
1422 srela->_raw_size += p->count * sizeof (Elf64_External_Rela);
1427 local_got = elf_local_got_refcounts (ibfd);
1428 if (!local_got)
1429 continue;
1431 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1432 locsymcount = symtab_hdr->sh_info;
1433 end_local_got = local_got + locsymcount;
1434 s = htab->sgot;
1435 srela = htab->srelgot;
1436 for (; local_got < end_local_got; ++local_got)
1438 if (*local_got > 0)
1440 *local_got = s->_raw_size;
1441 s->_raw_size += GOT_ENTRY_SIZE;
1442 if (info->shared)
1443 srela->_raw_size += sizeof (Elf64_External_Rela);
1445 else
1446 *local_got = (bfd_vma) -1;
1450 /* Allocate global sym .plt and .got entries, and space for global
1451 sym dynamic relocs. */
1452 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1454 /* We now have determined the sizes of the various dynamic sections.
1455 Allocate memory for them. */
1456 relocs = false;
1457 for (s = dynobj->sections; s != NULL; s = s->next)
1459 if ((s->flags & SEC_LINKER_CREATED) == 0)
1460 continue;
1462 if (s == htab->splt
1463 || s == htab->sgot
1464 || s == htab->sgotplt)
1466 /* Strip this section if we don't need it; see the
1467 comment below. */
1469 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1471 if (s->_raw_size != 0 && s != htab->srelplt)
1472 relocs = true;
1474 /* We use the reloc_count field as a counter if we need
1475 to copy relocs into the output file. */
1476 s->reloc_count = 0;
1478 else
1480 /* It's not one of our sections, so don't allocate space. */
1481 continue;
1484 if (s->_raw_size == 0)
1486 /* If we don't need this section, strip it from the
1487 output file. This is to handle .rela.bss and
1488 .rela.plt. We must create it in
1489 create_dynamic_sections, because it must be created
1490 before the linker maps input sections to output
1491 sections. The linker does that before
1492 adjust_dynamic_symbol is called, and it is that
1493 function which decides whether anything needs to go
1494 into these sections. */
1496 _bfd_strip_section_from_output (info, s);
1497 continue;
1500 /* Allocate memory for the section contents. We use bfd_zalloc
1501 here in case unused entries are not reclaimed before the
1502 section's contents are written out. This should not happen,
1503 but this way if it does, we get a R_390_NONE reloc instead
1504 of garbage. */
1505 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1506 if (s->contents == NULL)
1507 return false;
1510 if (htab->elf.dynamic_sections_created)
1512 /* Add some entries to the .dynamic section. We fill in the
1513 values later, in elf_s390_finish_dynamic_sections, but we
1514 must add the entries now so that we get the correct size for
1515 the .dynamic section. The DT_DEBUG entry is filled in by the
1516 dynamic linker and used by the debugger. */
1517 #define add_dynamic_entry(TAG, VAL) \
1518 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1520 if (! info->shared)
1522 if (!add_dynamic_entry (DT_DEBUG, 0))
1523 return false;
1526 if (htab->splt->_raw_size != 0)
1528 if (!add_dynamic_entry (DT_PLTGOT, 0)
1529 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1530 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1531 || !add_dynamic_entry (DT_JMPREL, 0))
1532 return false;
1535 if (relocs)
1537 if (!add_dynamic_entry (DT_RELA, 0)
1538 || !add_dynamic_entry (DT_RELASZ, 0)
1539 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1540 return false;
1542 /* If any dynamic relocs apply to a read-only section,
1543 then we need a DT_TEXTREL entry. */
1544 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, (PTR) info);
1546 if ((info->flags & DF_TEXTREL) != 0)
1548 if (!add_dynamic_entry (DT_TEXTREL, 0))
1549 return false;
1553 #undef add_dynamic_entry
1555 return true;
1558 /* Relocate a 390 ELF section. */
1560 static boolean
1561 elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
1562 contents, relocs, local_syms, local_sections)
1563 bfd *output_bfd;
1564 struct bfd_link_info *info;
1565 bfd *input_bfd;
1566 asection *input_section;
1567 bfd_byte *contents;
1568 Elf_Internal_Rela *relocs;
1569 Elf_Internal_Sym *local_syms;
1570 asection **local_sections;
1572 struct elf_s390_link_hash_table *htab;
1573 Elf_Internal_Shdr *symtab_hdr;
1574 struct elf_link_hash_entry **sym_hashes;
1575 bfd_vma *local_got_offsets;
1576 Elf_Internal_Rela *rel;
1577 Elf_Internal_Rela *relend;
1579 htab = elf_s390_hash_table (info);
1580 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1581 sym_hashes = elf_sym_hashes (input_bfd);
1582 local_got_offsets = elf_local_got_offsets (input_bfd);
1584 rel = relocs;
1585 relend = relocs + input_section->reloc_count;
1586 for (; rel < relend; rel++)
1588 int r_type;
1589 reloc_howto_type *howto;
1590 unsigned long r_symndx;
1591 struct elf_link_hash_entry *h;
1592 Elf_Internal_Sym *sym;
1593 asection *sec;
1594 bfd_vma off;
1595 bfd_vma relocation;
1596 boolean unresolved_reloc;
1597 bfd_reloc_status_type r;
1599 r_type = ELF64_R_TYPE (rel->r_info);
1600 if (r_type == (int) R_390_GNU_VTINHERIT
1601 || r_type == (int) R_390_GNU_VTENTRY)
1602 continue;
1603 if (r_type < 0 || r_type >= (int) R_390_max)
1605 bfd_set_error (bfd_error_bad_value);
1606 return false;
1608 howto = elf_howto_table + r_type;
1610 r_symndx = ELF64_R_SYM (rel->r_info);
1612 if (info->relocateable)
1614 /* This is a relocateable link. We don't have to change
1615 anything, unless the reloc is against a section symbol,
1616 in which case we have to adjust according to where the
1617 section symbol winds up in the output section. */
1618 if (r_symndx < symtab_hdr->sh_info)
1620 sym = local_syms + r_symndx;
1621 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1623 sec = local_sections[r_symndx];
1624 rel->r_addend += sec->output_offset + sym->st_value;
1628 continue;
1631 /* This is a final link. */
1632 h = NULL;
1633 sym = NULL;
1634 sec = NULL;
1635 unresolved_reloc = false;
1636 if (r_symndx < symtab_hdr->sh_info)
1638 sym = local_syms + r_symndx;
1639 sec = local_sections[r_symndx];
1640 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1642 else
1644 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1645 while (h->root.type == bfd_link_hash_indirect
1646 || h->root.type == bfd_link_hash_warning)
1647 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1649 if (h->root.type == bfd_link_hash_defined
1650 || h->root.type == bfd_link_hash_defweak)
1652 sec = h->root.u.def.section;
1653 if (sec->output_section == NULL)
1655 /* Set a flag that will be cleared later if we find a
1656 relocation value for this symbol. output_section
1657 is typically NULL for symbols satisfied by a shared
1658 library. */
1659 unresolved_reloc = true;
1660 relocation = 0;
1662 else
1663 relocation = (h->root.u.def.value
1664 + sec->output_section->vma
1665 + sec->output_offset);
1667 else if (h->root.type == bfd_link_hash_undefweak)
1668 relocation = 0;
1669 else if (info->shared
1670 && (!info->symbolic || info->allow_shlib_undefined)
1671 && !info->no_undefined
1672 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1673 relocation = 0;
1674 else
1676 if (! ((*info->callbacks->undefined_symbol)
1677 (info, h->root.root.string, input_bfd,
1678 input_section, rel->r_offset,
1679 (!info->shared || info->no_undefined
1680 || ELF_ST_VISIBILITY (h->other)))))
1681 return false;
1682 relocation = 0;
1686 switch (r_type)
1688 case R_390_GOT12:
1689 case R_390_GOT16:
1690 case R_390_GOT32:
1691 case R_390_GOT64:
1692 case R_390_GOTENT:
1693 /* Relocation is to the entry for this symbol in the global
1694 offset table. */
1695 if (htab->sgot == NULL)
1696 abort ();
1698 if (h != NULL)
1700 boolean dyn;
1702 off = h->got.offset;
1703 dyn = htab->elf.dynamic_sections_created;
1704 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
1705 || (info->shared
1706 && (info->symbolic
1707 || h->dynindx == -1
1708 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
1709 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1711 /* This is actually a static link, or it is a
1712 -Bsymbolic link and the symbol is defined
1713 locally, or the symbol was forced to be local
1714 because of a version file. We must initialize
1715 this entry in the global offset table. Since the
1716 offset must always be a multiple of 2, we use the
1717 least significant bit to record whether we have
1718 initialized it already.
1720 When doing a dynamic link, we create a .rel.got
1721 relocation entry to initialize the value. This
1722 is done in the finish_dynamic_symbol routine. */
1723 if ((off & 1) != 0)
1724 off &= ~1;
1725 else
1727 bfd_put_64 (output_bfd, relocation,
1728 htab->sgot->contents + off);
1729 h->got.offset |= 1;
1732 else
1733 unresolved_reloc = false;
1735 else
1737 if (local_got_offsets == NULL)
1738 abort ();
1740 off = local_got_offsets[r_symndx];
1742 /* The offset must always be a multiple of 8. We use
1743 the least significant bit to record whether we have
1744 already generated the necessary reloc. */
1745 if ((off & 1) != 0)
1746 off &= ~1;
1747 else
1749 bfd_put_64 (output_bfd, relocation,
1750 htab->sgot->contents + off);
1752 if (info->shared)
1754 asection *srelgot;
1755 Elf_Internal_Rela outrel;
1756 Elf64_External_Rela *loc;
1758 srelgot = htab->srelgot;
1759 if (srelgot == NULL)
1760 abort ();
1762 outrel.r_offset = (htab->sgot->output_section->vma
1763 + htab->sgot->output_offset
1764 + off);
1765 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
1766 outrel.r_addend = relocation;
1767 loc = (Elf64_External_Rela *) srelgot->contents;
1768 loc += srelgot->reloc_count++;
1769 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1772 local_got_offsets[r_symndx] |= 1;
1776 if (off >= (bfd_vma) -2)
1777 abort ();
1779 relocation = htab->sgot->output_offset + off;
1782 * For @GOTENT the relocation is against the offset between
1783 * the instruction and the symbols entry in the GOT and not
1784 * between the start of the GOT and the symbols entry. We
1785 * add the vma of the GOT to get the correct value.
1787 if (r_type == R_390_GOTENT)
1788 relocation += htab->sgot->output_section->vma;
1790 break;
1792 case R_390_GOTOFF:
1793 /* Relocation is relative to the start of the global offset
1794 table. */
1796 /* Note that sgot->output_offset is not involved in this
1797 calculation. We always want the start of .got. If we
1798 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1799 permitted by the ABI, we might have to change this
1800 calculation. */
1801 relocation -= htab->sgot->output_section->vma;
1803 break;
1805 case R_390_GOTPC:
1806 case R_390_GOTPCDBL:
1807 /* Use global offset table as symbol value. */
1808 relocation = htab->sgot->output_section->vma;
1809 unresolved_reloc = false;
1810 break;
1812 case R_390_PLT16DBL:
1813 case R_390_PLT32:
1814 case R_390_PLT32DBL:
1815 case R_390_PLT64:
1816 /* Relocation is to the entry for this symbol in the
1817 procedure linkage table. */
1819 /* Resolve a PLT32 reloc against a local symbol directly,
1820 without using the procedure linkage table. */
1821 if (h == NULL)
1822 break;
1824 if (h->plt.offset == (bfd_vma) -1
1825 || htab->splt == NULL)
1827 /* We didn't make a PLT entry for this symbol. This
1828 happens when statically linking PIC code, or when
1829 using -Bsymbolic. */
1830 break;
1833 relocation = (htab->splt->output_section->vma
1834 + htab->splt->output_offset
1835 + h->plt.offset);
1836 unresolved_reloc = false;
1837 break;
1839 case R_390_8:
1840 case R_390_16:
1841 case R_390_32:
1842 case R_390_64:
1843 case R_390_PC16:
1844 case R_390_PC16DBL:
1845 case R_390_PC32:
1846 case R_390_PC32DBL:
1847 case R_390_PC64:
1848 /* r_symndx will be zero only for relocs against symbols
1849 from removed linkonce sections, or sections discarded by
1850 a linker script. */
1851 if (r_symndx == 0
1852 || (input_section->flags & SEC_ALLOC) == 0)
1853 break;
1855 if ((info->shared
1856 && ((r_type != R_390_PC16
1857 && r_type != R_390_PC16DBL
1858 && r_type != R_390_PC32
1859 && r_type != R_390_PC32DBL
1860 && r_type != R_390_PC64)
1861 || (h != NULL
1862 && h->dynindx != -1
1863 && (! info->symbolic
1864 || (h->elf_link_hash_flags
1865 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1866 || (!info->shared
1867 && h != NULL
1868 && h->dynindx != -1
1869 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1870 && (((h->elf_link_hash_flags
1871 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1872 && (h->elf_link_hash_flags
1873 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1874 || h->root.type == bfd_link_hash_undefweak
1875 || h->root.type == bfd_link_hash_undefined)))
1877 Elf_Internal_Rela outrel;
1878 boolean skip, relocate;
1879 asection *sreloc;
1880 Elf64_External_Rela *loc;
1882 /* When generating a shared object, these relocations
1883 are copied into the output file to be resolved at run
1884 time. */
1886 skip = false;
1888 outrel.r_offset =
1889 _bfd_elf_section_offset (output_bfd, info, input_section,
1890 rel->r_offset);
1891 if (outrel.r_offset == (bfd_vma) -1)
1892 skip = true;
1894 outrel.r_offset += (input_section->output_section->vma
1895 + input_section->output_offset);
1897 if (skip)
1899 memset (&outrel, 0, sizeof outrel);
1900 relocate = false;
1902 else if (h != NULL
1903 && h->dynindx != -1
1904 && (r_type == R_390_PC16
1905 || r_type == R_390_PC16DBL
1906 || r_type == R_390_PC32
1907 || r_type == R_390_PC32DBL
1908 || r_type == R_390_PC64
1909 || !info->shared
1910 || !info->symbolic
1911 || (h->elf_link_hash_flags
1912 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1914 relocate = false;
1915 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1916 outrel.r_addend = rel->r_addend;
1918 else
1920 /* This symbol is local, or marked to become local. */
1921 relocate = true;
1922 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
1923 outrel.r_addend = relocation + rel->r_addend;
1926 sreloc = elf_section_data (input_section)->sreloc;
1927 if (sreloc == NULL)
1928 abort ();
1930 loc = (Elf64_External_Rela *) sreloc->contents;
1931 loc += sreloc->reloc_count++;
1932 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1934 /* If this reloc is against an external symbol, we do
1935 not want to fiddle with the addend. Otherwise, we
1936 need to include the symbol value so that it becomes
1937 an addend for the dynamic reloc. */
1938 if (! relocate)
1939 continue;
1942 break;
1944 default:
1945 break;
1948 if (unresolved_reloc
1949 && !(info->shared
1950 && (input_section->flags & SEC_DEBUGGING) != 0
1951 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1952 (*_bfd_error_handler)
1953 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1954 bfd_archive_filename (input_bfd),
1955 bfd_get_section_name (input_bfd, input_section),
1956 (long) rel->r_offset,
1957 h->root.root.string);
1959 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1960 contents, rel->r_offset,
1961 relocation, rel->r_addend);
1963 if (r != bfd_reloc_ok)
1965 const char *name;
1967 if (h != NULL)
1968 name = h->root.root.string;
1969 else
1971 name = bfd_elf_string_from_elf_section (input_bfd,
1972 symtab_hdr->sh_link,
1973 sym->st_name);
1974 if (name == NULL)
1975 return false;
1976 if (*name == '\0')
1977 name = bfd_section_name (input_bfd, sec);
1980 if (r == bfd_reloc_overflow)
1983 if (! ((*info->callbacks->reloc_overflow)
1984 (info, name, howto->name, (bfd_vma) 0,
1985 input_bfd, input_section, rel->r_offset)))
1986 return false;
1988 else
1990 (*_bfd_error_handler)
1991 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
1992 bfd_archive_filename (input_bfd),
1993 bfd_get_section_name (input_bfd, input_section),
1994 (long) rel->r_offset, name, (int) r);
1995 return false;
2000 return true;
2003 /* Finish up dynamic symbol handling. We set the contents of various
2004 dynamic sections here. */
2006 static boolean
2007 elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym)
2008 bfd *output_bfd;
2009 struct bfd_link_info *info;
2010 struct elf_link_hash_entry *h;
2011 Elf_Internal_Sym *sym;
2013 struct elf_s390_link_hash_table *htab;
2015 htab = elf_s390_hash_table (info);
2017 if (h->plt.offset != (bfd_vma) -1)
2019 bfd_vma plt_index;
2020 bfd_vma got_offset;
2021 Elf_Internal_Rela rela;
2022 Elf64_External_Rela *loc;
2024 /* This symbol has an entry in the procedure linkage table. Set
2025 it up. */
2027 if (h->dynindx == -1
2028 || htab->splt == NULL
2029 || htab->sgotplt == NULL
2030 || htab->srelplt == NULL)
2031 abort ();
2033 /* Calc. index no.
2034 Current offset - size first entry / entry size. */
2035 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
2037 /* Offset in GOT is PLT index plus GOT headers(3) times 8,
2038 addr & GOT addr. */
2039 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2041 /* Fill in the blueprint of a PLT. */
2042 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD0,
2043 htab->splt->contents + h->plt.offset);
2044 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD1,
2045 htab->splt->contents + h->plt.offset + 4);
2046 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
2047 htab->splt->contents + h->plt.offset + 8);
2048 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
2049 htab->splt->contents + h->plt.offset + 12);
2050 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD4,
2051 htab->splt->contents + h->plt.offset + 16);
2052 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD5,
2053 htab->splt->contents + h->plt.offset + 20);
2054 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD6,
2055 htab->splt->contents + h->plt.offset + 24);
2056 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD7,
2057 htab->splt->contents + h->plt.offset + 28);
2058 /* Fixup the relative address to the GOT entry */
2059 bfd_put_32 (output_bfd,
2060 (htab->sgotplt->output_section->vma +
2061 htab->sgotplt->output_offset + got_offset
2062 - (htab->splt->output_section->vma + h->plt.offset))/2,
2063 htab->splt->contents + h->plt.offset + 2);
2064 /* Fixup the relative branch to PLT 0 */
2065 bfd_put_32 (output_bfd, - (PLT_FIRST_ENTRY_SIZE +
2066 (PLT_ENTRY_SIZE * plt_index) + 22)/2,
2067 htab->splt->contents + h->plt.offset + 24);
2068 /* Fixup offset into symbol table */
2069 bfd_put_32 (output_bfd, plt_index * sizeof (Elf64_External_Rela),
2070 htab->splt->contents + h->plt.offset + 28);
2072 /* Fill in the entry in the global offset table.
2073 Points to instruction after GOT offset. */
2074 bfd_put_64 (output_bfd,
2075 (htab->splt->output_section->vma
2076 + htab->splt->output_offset
2077 + h->plt.offset
2078 + 14),
2079 htab->sgotplt->contents + got_offset);
2081 /* Fill in the entry in the .rela.plt section. */
2082 rela.r_offset = (htab->sgotplt->output_section->vma
2083 + htab->sgotplt->output_offset
2084 + got_offset);
2085 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT);
2086 rela.r_addend = 0;
2087 loc = (Elf64_External_Rela *) htab->srelplt->contents + plt_index;
2088 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2090 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2092 /* Mark the symbol as undefined, rather than as defined in
2093 the .plt section. Leave the value alone. This is a clue
2094 for the dynamic linker, to make function pointer
2095 comparisons work between an application and shared
2096 library. */
2097 sym->st_shndx = SHN_UNDEF;
2101 if (h->got.offset != (bfd_vma) -1)
2103 Elf_Internal_Rela rela;
2104 Elf64_External_Rela *loc;
2106 /* This symbol has an entry in the global offset table. Set it
2107 up. */
2109 if (htab->sgot == NULL || htab->srelgot == NULL)
2110 abort ();
2112 rela.r_offset = (htab->sgot->output_section->vma
2113 + htab->sgot->output_offset
2114 + (h->got.offset &~ (bfd_vma) 1));
2116 /* If this is a static link, or it is a -Bsymbolic link and the
2117 symbol is defined locally or was forced to be local because
2118 of a version file, we just want to emit a RELATIVE reloc.
2119 The entry in the global offset table will already have been
2120 initialized in the relocate_section function. */
2121 if (info->shared
2122 && (info->symbolic
2123 || h->dynindx == -1
2124 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2125 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2127 BFD_ASSERT((h->got.offset & 1) != 0);
2128 rela.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
2129 rela.r_addend = (h->root.u.def.value
2130 + h->root.u.def.section->output_section->vma
2131 + h->root.u.def.section->output_offset);
2133 else
2135 BFD_ASSERT((h->got.offset & 1) == 0);
2136 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgot->contents + h->got.offset);
2137 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_GLOB_DAT);
2138 rela.r_addend = 0;
2141 loc = (Elf64_External_Rela *) htab->srelgot->contents;
2142 loc += htab->srelgot->reloc_count++;
2143 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2146 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2148 Elf_Internal_Rela rela;
2149 Elf64_External_Rela *loc;
2151 /* This symbols needs a copy reloc. Set it up. */
2153 if (h->dynindx == -1
2154 || (h->root.type != bfd_link_hash_defined
2155 && h->root.type != bfd_link_hash_defweak)
2156 || htab->srelbss == NULL)
2157 abort ();
2159 rela.r_offset = (h->root.u.def.value
2160 + h->root.u.def.section->output_section->vma
2161 + h->root.u.def.section->output_offset);
2162 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_COPY);
2163 rela.r_addend = 0;
2164 loc = (Elf64_External_Rela *) htab->srelbss->contents;
2165 loc += htab->srelbss->reloc_count++;
2166 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2169 /* Mark some specially defined symbols as absolute. */
2170 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2171 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2172 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2173 sym->st_shndx = SHN_ABS;
2175 return true;
2178 /* Used to decide how to sort relocs in an optimal manner for the
2179 dynamic linker, before writing them out. */
2181 static enum elf_reloc_type_class
2182 elf_s390_reloc_type_class (rela)
2183 const Elf_Internal_Rela *rela;
2185 switch ((int) ELF64_R_TYPE (rela->r_info))
2187 case R_390_RELATIVE:
2188 return reloc_class_relative;
2189 case R_390_JMP_SLOT:
2190 return reloc_class_plt;
2191 case R_390_COPY:
2192 return reloc_class_copy;
2193 default:
2194 return reloc_class_normal;
2198 /* Finish up the dynamic sections. */
2200 static boolean
2201 elf_s390_finish_dynamic_sections (output_bfd, info)
2202 bfd *output_bfd;
2203 struct bfd_link_info *info;
2205 struct elf_s390_link_hash_table *htab;
2206 bfd *dynobj;
2207 asection *sdyn;
2209 htab = elf_s390_hash_table (info);
2210 dynobj = htab->elf.dynobj;
2211 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2213 if (htab->elf.dynamic_sections_created)
2215 Elf64_External_Dyn *dyncon, *dynconend;
2217 if (sdyn == NULL || htab->sgot == NULL)
2218 abort ();
2220 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2221 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2222 for (; dyncon < dynconend; dyncon++)
2224 Elf_Internal_Dyn dyn;
2225 asection *s;
2227 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2229 switch (dyn.d_tag)
2231 default:
2232 continue;
2234 case DT_PLTGOT:
2235 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
2236 break;
2238 case DT_JMPREL:
2239 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2240 break;
2242 case DT_PLTRELSZ:
2243 s = htab->srelplt->output_section;
2244 if (s->_cooked_size != 0)
2245 dyn.d_un.d_val = s->_cooked_size;
2246 else
2247 dyn.d_un.d_val = s->_raw_size;
2248 break;
2250 case DT_RELASZ:
2251 /* The procedure linkage table relocs (DT_JMPREL) should
2252 not be included in the overall relocs (DT_RELA).
2253 Therefore, we override the DT_RELASZ entry here to
2254 make it not include the JMPREL relocs. Since the
2255 linker script arranges for .rela.plt to follow all
2256 other relocation sections, we don't have to worry
2257 about changing the DT_RELA entry. */
2258 s = htab->srelplt->output_section;
2259 if (s->_cooked_size != 0)
2260 dyn.d_un.d_val -= s->_cooked_size;
2261 else
2262 dyn.d_un.d_val -= s->_raw_size;
2263 break;
2266 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2269 /* Fill in the special first entry in the procedure linkage table. */
2270 if (htab->splt && htab->splt->_raw_size > 0)
2272 /* fill in blueprint for plt 0 entry */
2273 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD0,
2274 htab->splt->contents );
2275 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD1,
2276 htab->splt->contents +4 );
2277 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD3,
2278 htab->splt->contents +12 );
2279 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD4,
2280 htab->splt->contents +16 );
2281 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD5,
2282 htab->splt->contents +20 );
2283 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD6,
2284 htab->splt->contents + 24);
2285 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD7,
2286 htab->splt->contents + 28 );
2287 /* Fixup relative address to start of GOT */
2288 bfd_put_32 (output_bfd,
2289 (htab->sgotplt->output_section->vma +
2290 htab->sgotplt->output_offset
2291 - htab->splt->output_section->vma - 6)/2,
2292 htab->splt->contents + 8);
2294 elf_section_data (htab->splt->output_section)
2295 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
2298 if (htab->sgotplt)
2300 /* Fill in the first three entries in the global offset table. */
2301 if (htab->sgotplt->_raw_size > 0)
2303 bfd_put_64 (output_bfd,
2304 (sdyn == NULL ? (bfd_vma) 0
2305 : sdyn->output_section->vma + sdyn->output_offset),
2306 htab->sgotplt->contents);
2307 /* One entry for shared object struct ptr. */
2308 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8);
2309 /* One entry for _dl_runtime_resolve. */
2310 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 12);
2313 elf_section_data (htab->sgot->output_section)
2314 ->this_hdr.sh_entsize = 8;
2316 return true;
2319 static boolean
2320 elf_s390_object_p (abfd)
2321 bfd *abfd;
2323 return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_esame);
2327 * Why was the hash table entry size definition changed from
2328 * ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and
2329 * this is the only reason for the s390_elf64_size_info structure.
2332 const struct elf_size_info s390_elf64_size_info =
2334 sizeof (Elf64_External_Ehdr),
2335 sizeof (Elf64_External_Phdr),
2336 sizeof (Elf64_External_Shdr),
2337 sizeof (Elf64_External_Rel),
2338 sizeof (Elf64_External_Rela),
2339 sizeof (Elf64_External_Sym),
2340 sizeof (Elf64_External_Dyn),
2341 sizeof (Elf_External_Note),
2342 8, /* hash-table entry size */
2343 1, /* internal relocations per external relocations */
2344 64, /* arch_size */
2345 8, /* file_align */
2346 ELFCLASS64, EV_CURRENT,
2347 bfd_elf64_write_out_phdrs,
2348 bfd_elf64_write_shdrs_and_ehdr,
2349 bfd_elf64_write_relocs,
2350 bfd_elf64_swap_symbol_out,
2351 bfd_elf64_slurp_reloc_table,
2352 bfd_elf64_slurp_symbol_table,
2353 bfd_elf64_swap_dyn_in,
2354 bfd_elf64_swap_dyn_out,
2355 NULL,
2356 NULL,
2357 NULL,
2358 NULL
2361 #define TARGET_BIG_SYM bfd_elf64_s390_vec
2362 #define TARGET_BIG_NAME "elf64-s390"
2363 #define ELF_ARCH bfd_arch_s390
2364 #define ELF_MACHINE_CODE EM_S390
2365 #define ELF_MACHINE_ALT1 EM_S390_OLD
2366 #define ELF_MAXPAGESIZE 0x1000
2368 #define elf_backend_size_info s390_elf64_size_info
2370 #define elf_backend_can_gc_sections 1
2371 #define elf_backend_can_refcount 1
2372 #define elf_backend_want_got_plt 1
2373 #define elf_backend_plt_readonly 1
2374 #define elf_backend_want_plt_sym 0
2375 #define elf_backend_got_header_size 24
2376 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2378 #define elf_info_to_howto elf_s390_info_to_howto
2380 #define bfd_elf64_bfd_is_local_label_name elf_s390_is_local_label_name
2381 #define bfd_elf64_bfd_link_hash_table_create elf_s390_link_hash_table_create
2382 #define bfd_elf64_bfd_reloc_type_lookup elf_s390_reloc_type_lookup
2384 #define elf_backend_adjust_dynamic_symbol elf_s390_adjust_dynamic_symbol
2385 #define elf_backend_check_relocs elf_s390_check_relocs
2386 #define elf_backend_copy_indirect_symbol elf_s390_copy_indirect_symbol
2387 #define elf_backend_create_dynamic_sections elf_s390_create_dynamic_sections
2388 #define elf_backend_finish_dynamic_sections elf_s390_finish_dynamic_sections
2389 #define elf_backend_finish_dynamic_symbol elf_s390_finish_dynamic_symbol
2390 #define elf_backend_gc_mark_hook elf_s390_gc_mark_hook
2391 #define elf_backend_gc_sweep_hook elf_s390_gc_sweep_hook
2392 #define elf_backend_reloc_type_class elf_s390_reloc_type_class
2393 #define elf_backend_relocate_section elf_s390_relocate_section
2394 #define elf_backend_size_dynamic_sections elf_s390_size_dynamic_sections
2395 #define elf_backend_reloc_type_class elf_s390_reloc_type_class
2397 #define elf_backend_object_p elf_s390_object_p
2399 #include "elf64-target.h"