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