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