OSDN Git Service

* elf-m10200.c (mn10200_elf_relax_section): Cast assignment to
[pf3gnuchains/pf3gnuchains4x.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 == 0)
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                     }
552
553                   sgot->_raw_size += 8;
554                   srelgot->_raw_size += sizeof (Elf64_External_Rela);
555                 }
556               h->got.refcount += 1;
557             }
558           else
559             {
560               /* This is a global offset table entry for a local symbol.  */
561               if (local_got_refcounts == NULL)
562                 {
563                   bfd_size_type size;
564
565                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
566                   local_got_refcounts = ((bfd_signed_vma *)
567                                          bfd_zalloc (abfd, size));
568                   if (local_got_refcounts == NULL)
569                     return false;
570                   elf_local_got_refcounts (abfd) = local_got_refcounts;
571                 }
572               if (local_got_refcounts[r_symndx] == 0)
573                 {
574                   sgot->_raw_size += 8;
575                   if (info->shared)
576                     {
577                       /* If we are generating a shared object, we need to
578                          output a R_390_RELATIVE reloc so that the dynamic
579                          linker can adjust this GOT entry.  */
580                       srelgot->_raw_size += sizeof (Elf64_External_Rela);
581                     }
582                 }
583               local_got_refcounts[r_symndx] += 1;
584             }
585           break;
586
587         case R_390_PLT16DBL:
588         case R_390_PLT32:
589         case R_390_PLT32DBL:
590         case R_390_PLT64:
591           /* This symbol requires a procedure linkage table entry.  We
592              actually build the entry in adjust_dynamic_symbol,
593              because this might be a case of linking PIC code which is
594              never referenced by a dynamic object, in which case we
595              don't need to generate a procedure linkage table entry
596              after all.  */
597
598           /* If this is a local symbol, we resolve it directly without
599              creating a procedure linkage table entry.  */
600           if (h == NULL)
601             continue;
602
603           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
604           h->plt.refcount += 1;
605           break;
606
607         case R_390_8:
608         case R_390_16:
609         case R_390_32:
610         case R_390_64:
611         case R_390_PC16:
612         case R_390_PC16DBL:
613         case R_390_PC32:
614         case R_390_PC32DBL:
615         case R_390_PC64:
616           if (h != NULL)
617             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
618
619           /* If we are creating a shared library, and this is a reloc
620              against a global symbol, or a non PC relative reloc
621              against a local symbol, then we need to copy the reloc
622              into the shared library.  However, if we are linking with
623              -Bsymbolic, we do not need to copy a reloc against a
624              global symbol which is defined in an object we are
625              including in the link (i.e., DEF_REGULAR is set).  At
626              this point we have not seen all the input files, so it is
627              possible that DEF_REGULAR is not set now but will be set
628              later (it is never cleared).  We account for that
629              possibility below by storing information in the
630              pcrel_relocs_copied field of the hash table entry.  */
631           if (info->shared
632               && (sec->flags & SEC_ALLOC) != 0
633               && (ELF64_R_TYPE (rel->r_info) == R_390_8
634                   || ELF64_R_TYPE (rel->r_info) == R_390_16
635                   || ELF64_R_TYPE (rel->r_info) == R_390_32
636                   || ELF64_R_TYPE (rel->r_info) == R_390_64
637                   || (h != NULL
638                       && h->dynindx != -1
639                       && (! info->symbolic
640                           || (h->elf_link_hash_flags
641                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
642             {
643               /* When creating a shared object, we must copy these
644                  reloc types into the output file.  We create a reloc
645                  section in dynobj and make room for this reloc.  */
646               if (sreloc == NULL)
647                 {
648                   const char *name;
649
650                   name = (bfd_elf_string_from_elf_section
651                           (abfd,
652                            elf_elfheader (abfd)->e_shstrndx,
653                            elf_section_data (sec)->rel_hdr.sh_name));
654                   if (name == NULL)
655                     return false;
656
657                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
658                               && strcmp (bfd_get_section_name (abfd, sec),
659                                          name + 5) == 0);
660
661                   sreloc = bfd_get_section_by_name (dynobj, name);
662                   if (sreloc == NULL)
663                     {
664                       flagword flags;
665
666                       sreloc = bfd_make_section (dynobj, name);
667                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
668                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
669                       if ((sec->flags & SEC_ALLOC) != 0)
670                         flags |= SEC_ALLOC | SEC_LOAD;
671                       if (sreloc == NULL
672                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
673                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
674                         return false;
675                     }
676                   if (sec->flags & SEC_READONLY)
677                     info->flags |= DF_TEXTREL;
678                 }
679
680               sreloc->_raw_size += sizeof (Elf64_External_Rela);
681
682               /* If we are linking with -Bsymbolic, and this is a
683                  global symbol, we count the number of PC relative
684                  relocations we have entered for this symbol, so that
685                  we can discard them again if the symbol is later
686                  defined by a regular object.  Note that this function
687                  is only called if we are using an elf64_s390 linker
688                  hash table, which means that h is really a pointer to
689                  an elf64_s390_link_hash_entry.  */
690               if (h != NULL
691                   && (ELF64_R_TYPE (rel->r_info) == R_390_PC16 ||
692                       ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL ||
693                       ELF64_R_TYPE (rel->r_info) == R_390_PC32 ||
694                       ELF64_R_TYPE (rel->r_info) == R_390_PC32DBL ||
695                       ELF64_R_TYPE (rel->r_info) == R_390_PC64))
696                 {
697                   struct elf_s390_link_hash_entry *eh;
698                   struct elf_s390_pcrel_relocs_copied *p;
699
700                   eh = (struct elf_s390_link_hash_entry *) h;
701
702                   for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
703                     if (p->section == sreloc)
704                       break;
705
706                   if (p == NULL)
707                     {
708                       p = ((struct elf_s390_pcrel_relocs_copied *)
709                            bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
710                       if (p == NULL)
711                         return false;
712                       p->next = eh->pcrel_relocs_copied;
713                       eh->pcrel_relocs_copied = p;
714                       p->section = sreloc;
715                       p->count = 0;
716                     }
717
718                   ++p->count;
719                 }
720             }
721
722           break;
723
724           /* This relocation describes the C++ object vtable hierarchy.
725              Reconstruct it for later use during GC.  */
726         case R_390_GNU_VTINHERIT:
727           if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
728             return false;
729           break;
730
731           /* This relocation describes which C++ vtable entries are actually
732              used.  Record for later use during GC.  */
733         case R_390_GNU_VTENTRY:
734           if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
735             return false;
736           break;
737
738         default:
739           break;
740         }
741     }
742
743   return true;
744 }
745
746 /* Return the section that should be marked against GC for a given
747    relocation.  */
748
749 static asection *
750 elf_s390_gc_mark_hook (abfd, info, rel, h, sym)
751      bfd *abfd;
752      struct bfd_link_info *info ATTRIBUTE_UNUSED;
753      Elf_Internal_Rela *rel;
754      struct elf_link_hash_entry *h;
755      Elf_Internal_Sym *sym;
756 {
757   if (h != NULL)
758     {
759       switch (ELF64_R_TYPE (rel->r_info))
760         {
761         case R_390_GNU_VTINHERIT:
762         case R_390_GNU_VTENTRY:
763           break;
764
765         default:
766           switch (h->root.type)
767             {
768             case bfd_link_hash_defined:
769             case bfd_link_hash_defweak:
770               return h->root.u.def.section;
771
772             case bfd_link_hash_common:
773               return h->root.u.c.p->section;
774
775             default:
776               break;
777             }
778         }
779     }
780   else
781     {
782       if (!(elf_bad_symtab (abfd)
783             && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
784           && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
785                 && sym->st_shndx != SHN_COMMON))
786         {
787           return bfd_section_from_elf_index (abfd, sym->st_shndx);
788         }
789     }
790
791   return NULL;
792 }
793
794 /* Update the got entry reference counts for the section being removed.  */
795
796 static boolean
797 elf_s390_gc_sweep_hook (abfd, info, sec, relocs)
798      bfd *abfd ATTRIBUTE_UNUSED;
799      struct bfd_link_info *info ATTRIBUTE_UNUSED;
800      asection *sec ATTRIBUTE_UNUSED;
801      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
802 {
803   Elf_Internal_Shdr *symtab_hdr;
804   struct elf_link_hash_entry **sym_hashes;
805   bfd_signed_vma *local_got_refcounts;
806   const Elf_Internal_Rela *rel, *relend;
807   unsigned long r_symndx;
808   struct elf_link_hash_entry *h;
809   bfd *dynobj;
810   asection *sgot;
811   asection *srelgot;
812
813   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
814   sym_hashes = elf_sym_hashes (abfd);
815   local_got_refcounts = elf_local_got_refcounts (abfd);
816
817   dynobj = elf_hash_table (info)->dynobj;
818   if (dynobj == NULL)
819     return true;
820
821   sgot = bfd_get_section_by_name (dynobj, ".got");
822   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
823
824   relend = relocs + sec->reloc_count;
825   for (rel = relocs; rel < relend; rel++)
826     switch (ELF64_R_TYPE (rel->r_info))
827       {
828       case R_390_GOT12:
829       case R_390_GOT16:
830       case R_390_GOT32:
831       case R_390_GOT64:
832       case R_390_GOTOFF:
833       case R_390_GOTPC:
834       case R_390_GOTPCDBL:
835       case R_390_GOTENT:
836         r_symndx = ELF64_R_SYM (rel->r_info);
837         if (r_symndx >= symtab_hdr->sh_info)
838           {
839             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
840             if (h->got.refcount > 0)
841               {
842                 h->got.refcount -= 1;
843                 if (h->got.refcount == 0)
844                   {
845                     sgot->_raw_size -= 8;
846                     srelgot->_raw_size -= sizeof (Elf64_External_Rela);
847                   }
848               }
849           }
850         else if (local_got_refcounts != NULL)
851           {
852             if (local_got_refcounts[r_symndx] > 0)
853               {
854                 local_got_refcounts[r_symndx] -= 1;
855                 if (local_got_refcounts[r_symndx] == 0)
856                   {
857                     sgot->_raw_size -= 8;
858                     if (info->shared)
859                       srelgot->_raw_size -= sizeof (Elf64_External_Rela);
860                   }
861               }
862           }
863         break;
864
865       case R_390_PLT16DBL:
866       case R_390_PLT32:
867       case R_390_PLT32DBL:
868       case R_390_PLT64:
869         r_symndx = ELF64_R_SYM (rel->r_info);
870         if (r_symndx >= symtab_hdr->sh_info)
871           {
872             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
873             if (h->plt.refcount > 0)
874               h->plt.refcount -= 1;
875           }
876         break;
877
878       default:
879         break;
880       }
881
882   return true;
883 }
884
885 /* Adjust a symbol defined by a dynamic object and referenced by a
886    regular object.  The current definition is in some section of the
887    dynamic object, but we're not including those sections.  We have to
888    change the definition to something the rest of the link can
889    understand.  */
890
891 static boolean
892 elf_s390_adjust_dynamic_symbol (info, h)
893      struct bfd_link_info *info;
894      struct elf_link_hash_entry *h;
895 {
896   bfd *dynobj;
897   asection *s;
898   unsigned int power_of_two;
899
900   dynobj = elf_hash_table (info)->dynobj;
901
902   /* Make sure we know what is going on here.  */
903   BFD_ASSERT (dynobj != NULL
904               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
905                   || h->weakdef != NULL
906                   || ((h->elf_link_hash_flags
907                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
908                       && (h->elf_link_hash_flags
909                           & ELF_LINK_HASH_REF_REGULAR) != 0
910                       && (h->elf_link_hash_flags
911                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
912
913   /* If this is a function, put it in the procedure linkage table.  We
914      will fill in the contents of the procedure linkage table later
915      (although we could actually do it here). */
916   if (h->type == STT_FUNC
917       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
918     {
919       if ((! info->shared
920            && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
921            && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
922           || (info->shared && h->plt.refcount <= 0))
923         {
924           /* This case can occur if we saw a PLT32 reloc in an input
925              file, but the symbol was never referred to by a dynamic
926              object.  In such a case, we don't actually need to build
927              a procedure linkage table, and we can just do a PC32
928              reloc instead.  */
929           h->plt.offset = (bfd_vma) -1;
930           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
931           return true;
932         }
933
934       /* Make sure this symbol is output as a dynamic symbol.  */
935       if (h->dynindx == -1)
936         {
937           if (! bfd_elf64_link_record_dynamic_symbol (info, h))
938             return false;
939         }
940
941       s = bfd_get_section_by_name (dynobj, ".plt");
942       BFD_ASSERT (s != NULL);
943
944
945       /* The first  entry in .plt is reserved.  */
946       if (s->_raw_size == 0)
947         s->_raw_size = PLT_FIRST_ENTRY_SIZE;
948
949      /* If this symbol is not defined in a regular file, and we are
950        not generating a shared library, then set the symbol to this
951        location in the .plt.  This is required to make function
952        pointers compare as equal between the normal executable and
953        the shared library.  */
954      if (! info->shared
955         && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
956       {
957         h->root.u.def.section = s;
958         h->root.u.def.value = s->_raw_size;
959       }
960
961       h->plt.offset = s->_raw_size;
962
963       /* Make room for this entry.  */
964       s->_raw_size += PLT_ENTRY_SIZE;
965
966       /* We also need to make an entry in the .got.plt section, which
967          will be placed in the .got section by the linker script.  */
968       s = bfd_get_section_by_name (dynobj, ".got.plt");
969       BFD_ASSERT (s != NULL);
970       s->_raw_size += GOT_ENTRY_SIZE;
971
972       /* We also need to make an entry in the .rela.plt section.  */
973       s = bfd_get_section_by_name (dynobj, ".rela.plt");
974       BFD_ASSERT (s != NULL);
975       s->_raw_size += sizeof (Elf64_External_Rela);
976
977       return true;
978     }
979   else
980     h->plt.offset = (bfd_vma) -1;
981
982   /* If this is a weak symbol, and there is a real definition, the
983      processor independent code will have arranged for us to see the
984      real definition first, and we can just use the same value.  */
985   if (h->weakdef != NULL)
986     {
987       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
988                   || h->weakdef->root.type == bfd_link_hash_defweak);
989       h->root.u.def.section = h->weakdef->root.u.def.section;
990       h->root.u.def.value = h->weakdef->root.u.def.value;
991       return true;
992     }
993
994   /* This is a reference to a symbol defined by a dynamic object which
995      is not a function.  */
996
997   /* If we are creating a shared library, we must presume that the
998      only references to the symbol are via the global offset table.
999      For such cases we need not do anything here; the relocations will
1000      be handled correctly by relocate_section.  */
1001   if (info->shared)
1002     return true;
1003
1004   /* If there are no references to this symbol that do not use the
1005      GOT, we don't need to generate a copy reloc.  */
1006   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1007     return true;
1008
1009   /* We must allocate the symbol in our .dynbss section, which will
1010      become part of the .bss section of the executable.  There will be
1011      an entry for this symbol in the .dynsym section.  The dynamic
1012      object will contain position independent code, so all references
1013      from the dynamic object to this symbol will go through the global
1014      offset table.  The dynamic linker will use the .dynsym entry to
1015      determine the address it must put in the global offset table, so
1016      both the dynamic object and the regular object will refer to the
1017      same memory location for the variable.  */
1018
1019   s = bfd_get_section_by_name (dynobj, ".dynbss");
1020   BFD_ASSERT (s != NULL);
1021
1022   /* We must generate a R_390_COPY reloc to tell the dynamic linker
1023      to copy the initial value out of the dynamic object and into the
1024      runtime process image.  We need to remember the offset into the
1025      .rel.bss section we are going to use.  */
1026   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1027     {
1028       asection *srel;
1029
1030       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1031       BFD_ASSERT (srel != NULL);
1032       srel->_raw_size += sizeof (Elf64_External_Rela);
1033       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1034     }
1035
1036   /* We need to figure out the alignment required for this symbol.  I
1037      have no idea how ELF linkers handle this.  */
1038   power_of_two = bfd_log2 (h->size);
1039   if (power_of_two > 3)
1040     power_of_two = 3;
1041
1042   /* Apply the required alignment.  */
1043   s->_raw_size = BFD_ALIGN (s->_raw_size,
1044                             (bfd_size_type) (1 << power_of_two));
1045   if (power_of_two > bfd_get_section_alignment (dynobj, s))
1046     {
1047       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1048         return false;
1049     }
1050
1051   /* Define the symbol as being at this point in the section.  */
1052   h->root.u.def.section = s;
1053   h->root.u.def.value = s->_raw_size;
1054
1055   /* Increment the section size to make room for the symbol.  */
1056   s->_raw_size += h->size;
1057
1058   return true;
1059 }
1060
1061 /* Set the sizes of the dynamic sections.  */
1062
1063 static boolean
1064 elf_s390_size_dynamic_sections (output_bfd, info)
1065      bfd *output_bfd ATTRIBUTE_UNUSED;
1066      struct bfd_link_info *info;
1067 {
1068   bfd *dynobj;
1069   asection *s;
1070   boolean relocs;
1071   boolean plt;
1072
1073   dynobj = elf_hash_table (info)->dynobj;
1074   BFD_ASSERT (dynobj != NULL);
1075
1076   if (elf_hash_table (info)->dynamic_sections_created)
1077     {
1078       /* Set the contents of the .interp section to the interpreter.  */
1079       if (! info->shared)
1080         {
1081           s = bfd_get_section_by_name (dynobj, ".interp");
1082           BFD_ASSERT (s != NULL);
1083           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1084           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1085         }
1086     }
1087   else
1088     {
1089       /* We may have created entries in the .rela.got section.
1090          However, if we are not creating the dynamic sections, we will
1091          not actually use these entries.  Reset the size of .rela.got,
1092          which will cause it to get stripped from the output file
1093          below.  */
1094       s = bfd_get_section_by_name (dynobj, ".rela.got");
1095       if (s != NULL)
1096         s->_raw_size = 0;
1097     }
1098
1099   /* If this is a -Bsymbolic shared link, then we need to discard all
1100      PC relative relocs against symbols defined in a regular object.
1101      We allocated space for them in the check_relocs routine, but we
1102      will not fill them in in the relocate_section routine.  */
1103   if (info->shared)
1104     elf_s390_link_hash_traverse (elf_s390_hash_table (info),
1105                                  elf_s390_discard_copies,
1106                                  (PTR) info);
1107
1108   /* The check_relocs and adjust_dynamic_symbol entry points have
1109      determined the sizes of the various dynamic sections.  Allocate
1110      memory for them.  */
1111   plt = false;
1112   relocs = false;
1113   for (s = dynobj->sections; s != NULL; s = s->next)
1114     {
1115       const char *name;
1116       boolean strip;
1117
1118       if ((s->flags & SEC_LINKER_CREATED) == 0)
1119         continue;
1120
1121       /* It's OK to base decisions on the section name, because none
1122          of the dynobj section names depend upon the input files.  */
1123       name = bfd_get_section_name (dynobj, s);
1124
1125       strip = false;
1126
1127       if (strcmp (name, ".plt") == 0)
1128         {
1129           if (s->_raw_size == 0)
1130             {
1131               /* Strip this section if we don't need it; see the
1132                  comment below.  */
1133               strip = true;
1134             }
1135           else
1136             {
1137               /* Remember whether there is a PLT.  */
1138               plt = true;
1139             }
1140         }
1141       else if (strncmp (name, ".rela", 5) == 0)
1142         {
1143           if (s->_raw_size == 0)
1144             {
1145               /* If we don't need this section, strip it from the
1146                  output file.  This is to handle .rela.bss and
1147                  .rel.plt.  We must create it in
1148                  create_dynamic_sections, because it must be created
1149                  before the linker maps input sections to output
1150                  sections.  The linker does that before
1151                  adjust_dynamic_symbol is called, and it is that
1152                  function which decides whether anything needs to go
1153                  into these sections.  */
1154               strip = true;
1155             }
1156           else
1157             {
1158               /* Remember whether there are any reloc sections other
1159                  than .rela.plt.  */
1160               if (strcmp (name, ".rela.plt") != 0)
1161                 relocs = true;
1162
1163               /* We use the reloc_count field as a counter if we need
1164                  to copy relocs into the output file.  */
1165               s->reloc_count = 0;
1166             }
1167         }
1168       else if (strncmp (name, ".got", 4) != 0)
1169         {
1170           /* It's not one of our sections, so don't allocate space.  */
1171           continue;
1172         }
1173
1174       if (strip)
1175         {
1176           _bfd_strip_section_from_output (info, s);
1177           continue;
1178         }
1179
1180       /* Allocate memory for the section contents.  */
1181       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1182       if (s->contents == NULL && s->_raw_size != 0)
1183         return false;
1184     }
1185
1186   if (elf_hash_table (info)->dynamic_sections_created)
1187     {
1188       /* Add some entries to the .dynamic section.  We fill in the
1189          values later, in elf_s390_finish_dynamic_sections, but we
1190          must add the entries now so that we get the correct size for
1191          the .dynamic section.  The DT_DEBUG entry is filled in by the
1192          dynamic linker and used by the debugger.  */
1193 #define add_dynamic_entry(TAG, VAL) \
1194   bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1195
1196       if (! info->shared)
1197         {
1198           if (!add_dynamic_entry (DT_DEBUG, 0))
1199             return false;
1200         }
1201
1202       if (plt)
1203         {
1204           if (!add_dynamic_entry (DT_PLTGOT, 0)
1205               || !add_dynamic_entry (DT_PLTRELSZ, 0)
1206               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1207               || !add_dynamic_entry (DT_JMPREL, 0))
1208             return false;
1209         }
1210
1211       if (relocs)
1212         {
1213           if (!add_dynamic_entry (DT_RELA, 0)
1214               || !add_dynamic_entry (DT_RELASZ, 0)
1215               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1216             return false;
1217          }
1218
1219       if ((info->flags & DF_TEXTREL) != 0)
1220         {
1221           if (!add_dynamic_entry (DT_TEXTREL, 0))
1222             return false;
1223           info->flags |= DF_TEXTREL;
1224         }
1225     }
1226 #undef add_dynamic_entry
1227
1228   return true;
1229 }
1230
1231 /* This function is called via elf64_s390_link_hash_traverse if we are
1232    creating a shared object with -Bsymbolic.  It discards the space
1233    allocated to copy PC relative relocs against symbols which are
1234    defined in regular objects.  We allocated space for them in the
1235    check_relocs routine, but we won't fill them in in the
1236    relocate_section routine.  */
1237
1238 /*ARGSUSED*/
1239 static boolean
1240 elf_s390_discard_copies (h, inf)
1241      struct elf_s390_link_hash_entry *h;
1242      PTR inf;
1243 {
1244   struct elf_s390_pcrel_relocs_copied *s;
1245   struct bfd_link_info *info = (struct bfd_link_info *) inf;
1246
1247   /* If a symbol has been forced local or we have found a regular
1248      definition for the symbolic link case, then we won't be needing
1249      any relocs.  */
1250   if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1251       && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1252           || info->symbolic))
1253     {
1254       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1255         s->section->_raw_size -= s->count * sizeof (Elf64_External_Rela);
1256     }
1257
1258   return true;
1259 }
1260 /* Relocate a 390 ELF section.  */
1261
1262 static boolean
1263 elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
1264                               contents, relocs, local_syms, local_sections)
1265      bfd *output_bfd;
1266      struct bfd_link_info *info;
1267      bfd *input_bfd;
1268      asection *input_section;
1269      bfd_byte *contents;
1270      Elf_Internal_Rela *relocs;
1271      Elf_Internal_Sym *local_syms;
1272      asection **local_sections;
1273 {
1274   bfd *dynobj;
1275   Elf_Internal_Shdr *symtab_hdr;
1276   struct elf_link_hash_entry **sym_hashes;
1277   bfd_vma *local_got_offsets;
1278   asection *sgot;
1279   asection *splt;
1280   asection *sreloc;
1281   Elf_Internal_Rela *rel;
1282   Elf_Internal_Rela *relend;
1283
1284   dynobj = elf_hash_table (info)->dynobj;
1285   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1286   sym_hashes = elf_sym_hashes (input_bfd);
1287   local_got_offsets = elf_local_got_offsets (input_bfd);
1288
1289   sgot = NULL;
1290   splt = NULL;
1291   sreloc = NULL;
1292   if (dynobj != NULL)
1293     {
1294       splt = bfd_get_section_by_name (dynobj, ".plt");
1295       sgot = bfd_get_section_by_name (dynobj, ".got");
1296     }
1297
1298   rel = relocs;
1299   relend = relocs + input_section->reloc_count;
1300   for (; rel < relend; rel++)
1301     {
1302       int r_type;
1303       reloc_howto_type *howto;
1304       unsigned long r_symndx;
1305       struct elf_link_hash_entry *h;
1306       Elf_Internal_Sym *sym;
1307       asection *sec;
1308       bfd_vma relocation;
1309       bfd_reloc_status_type r;
1310
1311       r_type = ELF64_R_TYPE (rel->r_info);
1312       if (r_type == R_390_GNU_VTINHERIT
1313           || r_type == R_390_GNU_VTENTRY)
1314         continue;
1315       if (r_type < 0 || r_type >= (int) R_390_max)
1316         {
1317           bfd_set_error (bfd_error_bad_value);
1318           return false;
1319         }
1320       howto = elf_howto_table + r_type;
1321
1322       r_symndx = ELF64_R_SYM (rel->r_info);
1323
1324       if (info->relocateable)
1325         {
1326           /* This is a relocateable link.  We don't have to change
1327              anything, unless the reloc is against a section symbol,
1328              in which case we have to adjust according to where the
1329              section symbol winds up in the output section.  */
1330           if (r_symndx < symtab_hdr->sh_info)
1331             {
1332               sym = local_syms + r_symndx;
1333               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1334                 {
1335                   sec = local_sections[r_symndx];
1336                   rel->r_addend += sec->output_offset + sym->st_value;
1337                 }
1338             }
1339
1340           continue;
1341         }
1342
1343       /* This is a final link.  */
1344       h = NULL;
1345       sym = NULL;
1346       sec = NULL;
1347       if (r_symndx < symtab_hdr->sh_info)
1348         {
1349           sym = local_syms + r_symndx;
1350           sec = local_sections[r_symndx];
1351           relocation = (sec->output_section->vma
1352                         + sec->output_offset
1353                         + sym->st_value);
1354         }
1355       else
1356         {
1357           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1358           while (h->root.type == bfd_link_hash_indirect
1359                  || h->root.type == bfd_link_hash_warning)
1360             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1361           if (h->root.type == bfd_link_hash_defined
1362               || h->root.type == bfd_link_hash_defweak)
1363             {
1364               sec = h->root.u.def.section;
1365               if ((r_type == R_390_GOTPC
1366                    || r_type == R_390_GOTPCDBL)
1367                   || ((r_type == R_390_PLT16DBL ||
1368                        r_type == R_390_PLT32 ||
1369                        r_type == R_390_PLT32DBL ||
1370                        r_type == R_390_PLT64)
1371                       && splt != NULL
1372                       && h->plt.offset != (bfd_vma) -1)
1373                   || ((r_type == R_390_GOT12 ||
1374                        r_type == R_390_GOT16 ||
1375                        r_type == R_390_GOT32 ||
1376                        r_type == R_390_GOT64 ||
1377                        r_type == R_390_GOTENT)
1378                       && elf_hash_table (info)->dynamic_sections_created
1379                       && (! info->shared
1380                           || (! info->symbolic && h->dynindx != -1)
1381                           || (h->elf_link_hash_flags
1382                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
1383                   || (info->shared
1384                       && ((! info->symbolic && h->dynindx != -1)
1385                           || (h->elf_link_hash_flags
1386                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
1387                       && ( r_type == R_390_8 ||
1388                            r_type == R_390_16 ||
1389                            r_type == R_390_32 ||
1390                            r_type == R_390_64 ||
1391                            r_type == R_390_PC16 ||
1392                            r_type == R_390_PC16DBL ||
1393                            r_type == R_390_PC32 ||
1394                            r_type == R_390_PC32DBL ||
1395                            r_type == R_390_PC64)
1396                       && ((input_section->flags & SEC_ALLOC) != 0
1397                           /* DWARF will emit R_386_32 relocations in its
1398                              sections against symbols defined externally
1399                              in shared libraries.  We can't do anything
1400                              with them here.  */
1401                           || ((input_section->flags & SEC_DEBUGGING) != 0
1402                               && (h->elf_link_hash_flags
1403                                   & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
1404                 {
1405                   /* In these cases, we don't need the relocation
1406                      value.  We check specially because in some
1407                      obscure cases sec->output_section will be NULL.  */
1408                   relocation = 0;
1409                 }
1410               else if (sec->output_section == NULL)
1411                 {
1412                   (*_bfd_error_handler)
1413                     (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1414                      bfd_archive_filename (input_bfd), h->root.root.string,
1415                      bfd_get_section_name (input_bfd, input_section));
1416                   relocation = 0;
1417                 }
1418               else
1419                 relocation = (h->root.u.def.value
1420                               + sec->output_section->vma
1421                               + sec->output_offset);
1422             }
1423           else if (h->root.type == bfd_link_hash_undefweak)
1424             relocation = 0;
1425           else if (info->shared
1426                    && (!info->symbolic || info->allow_shlib_undefined)
1427                    && !info->no_undefined
1428                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1429             relocation = 0;
1430           else
1431             {
1432               if (! ((*info->callbacks->undefined_symbol)
1433                      (info, h->root.root.string, input_bfd,
1434               input_section, rel->r_offset,
1435                      (!info->shared || info->no_undefined
1436                       || ELF_ST_VISIBILITY (h->other)))))
1437                 return false;
1438               relocation = 0;
1439             }
1440         }
1441
1442       switch (r_type)
1443         {
1444         case R_390_GOT12:
1445         case R_390_GOT16:
1446         case R_390_GOT32:
1447         case R_390_GOT64:
1448         case R_390_GOTENT:
1449           /* Relocation is to the entry for this symbol in the global
1450              offset table.  */
1451           BFD_ASSERT (sgot != NULL);
1452
1453           if (h != NULL)
1454             {
1455               bfd_vma off;
1456
1457               off = h->got.offset;
1458               BFD_ASSERT (off != (bfd_vma) -1);
1459
1460               if (! elf_hash_table (info)->dynamic_sections_created
1461                   || (info->shared
1462                       && (info->symbolic || h->dynindx == -1)
1463                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1464                 {
1465                   /* This is actually a static link, or it is a
1466                      -Bsymbolic link and the symbol is defined
1467                      locally, or the symbol was forced to be local
1468                      because of a version file.  We must initialize
1469                      this entry in the global offset table.  Since the
1470                      offset must always be a multiple of 2, we use the
1471                      least significant bit to record whether we have
1472                      initialized it already.
1473
1474                      When doing a dynamic link, we create a .rel.got
1475                      relocation entry to initialize the value.  This
1476                      is done in the finish_dynamic_symbol routine.  */
1477                   if ((off & 1) != 0)
1478                     off &= ~1;
1479                   else
1480                     {
1481                       bfd_put_64 (output_bfd, relocation,
1482                                   sgot->contents + off);
1483                       h->got.offset |= 1;
1484                     }
1485                 }
1486               relocation = sgot->output_offset + off;
1487             }
1488           else
1489             {
1490               bfd_vma off;
1491
1492               BFD_ASSERT (local_got_offsets != NULL
1493                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
1494
1495               off = local_got_offsets[r_symndx];
1496
1497               /* The offset must always be a multiple of 8.  We use
1498                  the least significant bit to record whether we have
1499                  already generated the necessary reloc.  */
1500               if ((off & 1) != 0)
1501                 off &= ~1;
1502               else
1503                 {
1504                   bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1505
1506                   if (info->shared)
1507                     {
1508                       asection *srelgot;
1509                       Elf_Internal_Rela outrel;
1510
1511                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1512                       BFD_ASSERT (srelgot != NULL);
1513
1514                       outrel.r_offset = (sgot->output_section->vma
1515                                          + sgot->output_offset
1516                                          + off);
1517                       outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
1518                       outrel.r_addend = relocation;
1519                       bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1520                                                 (((Elf64_External_Rela *)
1521                                                   srelgot->contents)
1522                                                  + srelgot->reloc_count));
1523                       ++srelgot->reloc_count;
1524                     }
1525
1526                   local_got_offsets[r_symndx] |= 1;
1527                 }
1528
1529               relocation = sgot->output_offset + off;
1530             }
1531
1532           /*
1533            * For @GOTENT the relocation is against the offset between
1534            * the instruction and the symbols entry in the GOT and not
1535            * between the start of the GOT and the symbols entry. We
1536            * add the vma of the GOT to get the correct value.
1537            */
1538           if (r_type == R_390_GOTENT)
1539             relocation += sgot->output_section->vma;
1540
1541           break;
1542
1543         case R_390_GOTOFF:
1544           /* Relocation is relative to the start of the global offset
1545              table.  */
1546
1547           if (sgot == NULL)
1548             {
1549               sgot = bfd_get_section_by_name (dynobj, ".got");
1550               BFD_ASSERT (sgot != NULL);
1551             }
1552
1553           /* Note that sgot->output_offset is not involved in this
1554              calculation.  We always want the start of .got.  If we
1555              defined _GLOBAL_OFFSET_TABLE in a different way, as is
1556              permitted by the ABI, we might have to change this
1557              calculation.  */
1558           relocation -= sgot->output_section->vma;
1559
1560           break;
1561
1562         case R_390_GOTPC:
1563         case R_390_GOTPCDBL:
1564           /* Use global offset table as symbol value.  */
1565
1566           if (sgot == NULL)
1567             {
1568               sgot = bfd_get_section_by_name (dynobj, ".got");
1569               BFD_ASSERT (sgot != NULL);
1570             }
1571
1572           relocation = sgot->output_section->vma;
1573
1574           break;
1575
1576         case R_390_PLT16DBL:
1577         case R_390_PLT32:
1578         case R_390_PLT32DBL:
1579         case R_390_PLT64:
1580           /* Relocation is to the entry for this symbol in the
1581              procedure linkage table.  */
1582
1583           /* Resolve a PLT32 reloc against a local symbol directly,
1584              without using the procedure linkage table.  */
1585           if (h == NULL)
1586             break;
1587
1588           if (h->plt.offset == (bfd_vma) -1 || splt == NULL)
1589             {
1590               /* We didn't make a PLT entry for this symbol.  This
1591                  happens when statically linking PIC code, or when
1592                  using -Bsymbolic.  */
1593               break;
1594             }
1595
1596           relocation = (splt->output_section->vma
1597                         + splt->output_offset
1598                         + h->plt.offset);
1599
1600           break;
1601
1602         case R_390_8:
1603         case R_390_16:
1604         case R_390_32:
1605         case R_390_64:
1606         case R_390_PC16:
1607         case R_390_PC16DBL:
1608         case R_390_PC32:
1609         case R_390_PC32DBL:
1610         case R_390_PC64:
1611           if (info->shared
1612               && (input_section->flags & SEC_ALLOC) != 0
1613               && (r_type == R_390_8
1614                   || r_type == R_390_16
1615                   || r_type == R_390_32
1616                   || r_type == R_390_64
1617                   || (h != NULL
1618                       && h->dynindx != -1
1619                       && (! info->symbolic
1620                           || (h->elf_link_hash_flags
1621                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1622             {
1623               Elf_Internal_Rela outrel;
1624               boolean skip, relocate;
1625
1626               /* When generating a shared object, these relocations
1627                  are copied into the output file to be resolved at run
1628                  time.  */
1629
1630               if (sreloc == NULL)
1631                 {
1632                   const char *name;
1633
1634                   name = (bfd_elf_string_from_elf_section
1635                           (input_bfd,
1636                            elf_elfheader (input_bfd)->e_shstrndx,
1637                            elf_section_data (input_section)->rel_hdr.sh_name));
1638                   if (name == NULL)
1639                     return false;
1640
1641                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1642                               && strcmp (bfd_get_section_name (input_bfd,
1643                                                                input_section),
1644                                          name + 5) == 0);
1645
1646                   sreloc = bfd_get_section_by_name (dynobj, name);
1647                   BFD_ASSERT (sreloc != NULL);
1648                 }
1649
1650               skip = false;
1651
1652               if (elf_section_data (input_section)->stab_info == NULL)
1653                 outrel.r_offset = rel->r_offset;
1654               else
1655                 {
1656                   bfd_vma off;
1657
1658                   off = (_bfd_stab_section_offset
1659                          (output_bfd, &elf_hash_table (info)->stab_info,
1660                           input_section,
1661                           &elf_section_data (input_section)->stab_info,
1662                           rel->r_offset));
1663                   if (off == (bfd_vma) -1)
1664                     skip = true;
1665                   outrel.r_offset = off;
1666                 }
1667
1668               outrel.r_offset += (input_section->output_section->vma
1669                                   + input_section->output_offset);
1670
1671               if (skip)
1672                 {
1673                   memset (&outrel, 0, sizeof outrel);
1674                   relocate = false;
1675                 }
1676               else if (r_type == R_390_PC16 ||
1677                        r_type == R_390_PC16DBL ||
1678                        r_type == R_390_PC32 ||
1679                        r_type == R_390_PC32DBL ||
1680                        r_type == R_390_PC64)
1681                 {
1682                   BFD_ASSERT (h != NULL && h->dynindx != -1);
1683                   relocate = false;
1684                   outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1685                   outrel.r_addend = relocation + rel->r_addend;
1686                 }
1687               else
1688                 {
1689                   /* h->dynindx may be -1 if this symbol was marked to
1690                      become local.  */
1691                   if (h == NULL
1692                       || ((info->symbolic || h->dynindx == -1)
1693                           && (h->elf_link_hash_flags
1694                               & ELF_LINK_HASH_DEF_REGULAR) != 0))
1695                     {
1696                       relocate = true;
1697                       outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
1698                       outrel.r_addend = relocation + rel->r_addend;
1699                     }
1700                   else
1701                     {
1702                       BFD_ASSERT (h->dynindx != -1);
1703                       relocate = false;
1704                       outrel.r_info = ELF64_R_INFO (h->dynindx, R_390_64);
1705                       outrel.r_addend = relocation + rel->r_addend;
1706                     }
1707                 }
1708
1709               bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1710                                         (((Elf64_External_Rela *)
1711                                           sreloc->contents)
1712                                          + sreloc->reloc_count));
1713               ++sreloc->reloc_count;
1714
1715               /* If this reloc is against an external symbol, we do
1716                  not want to fiddle with the addend.  Otherwise, we
1717                  need to include the symbol value so that it becomes
1718                  an addend for the dynamic reloc.  */
1719               if (! relocate)
1720                 continue;
1721             }
1722
1723           break;
1724
1725         default:
1726           break;
1727         }
1728
1729       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1730                                       contents, rel->r_offset,
1731                                       relocation, rel->r_addend);
1732
1733       if (r != bfd_reloc_ok)
1734         {
1735           switch (r)
1736             {
1737             default:
1738             case bfd_reloc_outofrange:
1739               abort ();
1740             case bfd_reloc_overflow:
1741               {
1742                 const char *name;
1743
1744                 if (h != NULL)
1745                   name = h->root.root.string;
1746                 else
1747                   {
1748                     name = bfd_elf_string_from_elf_section (input_bfd,
1749                                                             symtab_hdr->sh_link,
1750                                                             sym->st_name);
1751                     if (name == NULL)
1752                       return false;
1753                     if (*name == '\0')
1754                       name = bfd_section_name (input_bfd, sec);
1755                   }
1756                 if (! ((*info->callbacks->reloc_overflow)
1757                        (info, name, howto->name, (bfd_vma) 0,
1758                         input_bfd, input_section, rel->r_offset)))
1759                   return false;
1760               }
1761               break;
1762             }
1763         }
1764     }
1765
1766   return true;
1767 }
1768
1769 /* Finish up dynamic symbol handling.  We set the contents of various
1770    dynamic sections here.  */
1771
1772 static boolean
1773 elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym)
1774      bfd *output_bfd;
1775      struct bfd_link_info *info;
1776      struct elf_link_hash_entry *h;
1777      Elf_Internal_Sym *sym;
1778 {
1779   bfd *dynobj;
1780
1781   dynobj = elf_hash_table (info)->dynobj;
1782
1783   if (h->plt.offset != (bfd_vma) -1)
1784     {
1785       asection *splt;
1786       asection *srela;
1787       Elf_Internal_Rela rela;
1788       bfd_vma got_offset;
1789       bfd_vma plt_index;
1790       asection *sgot;
1791
1792       /* This symbol has an entry in the procedure linkage table.  Set
1793          it up.  */
1794
1795       BFD_ASSERT (h->dynindx != -1);
1796
1797       splt = bfd_get_section_by_name (dynobj, ".plt");
1798       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1799       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1800       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1801
1802       /* Calc. index no.
1803          Current offset - size first entry / entry size.  */
1804       plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
1805
1806       /* Offset in GOT is PLT index plus GOT headers(3) times 8,
1807          addr & GOT addr.  */
1808       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
1809
1810       /* Fill in the blueprint of a PLT.  */
1811       bfd_put_32 (output_bfd, PLT_ENTRY_WORD0,
1812                   splt->contents + h->plt.offset);
1813       bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
1814                   splt->contents + h->plt.offset + 4);
1815       bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1816                   splt->contents + h->plt.offset + 8);
1817       bfd_put_32 (output_bfd, PLT_ENTRY_WORD3,
1818                   splt->contents + h->plt.offset + 12);
1819       bfd_put_32 (output_bfd, PLT_ENTRY_WORD4,
1820                   splt->contents + h->plt.offset + 16);
1821       bfd_put_32 (output_bfd, PLT_ENTRY_WORD5,
1822                   splt->contents + h->plt.offset + 20);
1823       bfd_put_32 (output_bfd, PLT_ENTRY_WORD6,
1824                   splt->contents + h->plt.offset + 24);
1825       bfd_put_32 (output_bfd, PLT_ENTRY_WORD7,
1826                   splt->contents + h->plt.offset + 28);
1827       /* Fixup the relative address to the GOT entry */
1828       bfd_put_32 (output_bfd,
1829                   (sgot->output_section->vma + sgot->output_offset + got_offset
1830                    - (splt->output_section->vma + h->plt.offset))/2,
1831                   splt->contents + h->plt.offset + 2);
1832       /* Fixup the relative branch to PLT 0 */
1833       bfd_put_32 (output_bfd, - (PLT_FIRST_ENTRY_SIZE +
1834                                  (PLT_ENTRY_SIZE * plt_index) + 22)/2,
1835                   splt->contents + h->plt.offset + 24);
1836       /* Fixup offset into symbol table */
1837       bfd_put_32 (output_bfd, plt_index * sizeof (Elf64_External_Rela),
1838                   splt->contents + h->plt.offset + 28);
1839
1840       /* Fill in the entry in the .rela.plt section.  */
1841       rela.r_offset = (sgot->output_section->vma
1842                        + sgot->output_offset
1843                        + got_offset);
1844       rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT);
1845       rela.r_addend = 0;
1846       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1847                                  ((Elf64_External_Rela *) srela->contents
1848                                   + plt_index ));
1849
1850       /* Fill in the entry in the global offset table.
1851          Points to instruction after GOT offset.  */
1852       bfd_put_64 (output_bfd,
1853                   (splt->output_section->vma
1854                    + splt->output_offset
1855                    + h->plt.offset
1856                    + 14),
1857                   sgot->contents + got_offset);
1858
1859
1860       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1861         {
1862           /* Mark the symbol as undefined, rather than as defined in
1863              the .plt section.  Leave the value alone.  */
1864           sym->st_shndx = SHN_UNDEF;
1865         }
1866     }
1867
1868   if (h->got.offset != (bfd_vma) -1)
1869     {
1870       asection *sgot;
1871       asection *srela;
1872       Elf_Internal_Rela rela;
1873
1874       /* This symbol has an entry in the global offset table.  Set it
1875          up.  */
1876
1877       sgot = bfd_get_section_by_name (dynobj, ".got");
1878       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1879       BFD_ASSERT (sgot != NULL && srela != NULL);
1880
1881       rela.r_offset = (sgot->output_section->vma
1882                        + sgot->output_offset
1883                        + (h->got.offset &~ (bfd_vma) 1));
1884
1885       /* If this is a static link, or it is a -Bsymbolic link and the
1886          symbol is defined locally or was forced to be local because
1887          of a version file, we just want to emit a RELATIVE reloc.
1888          The entry in the global offset table will already have been
1889          initialized in the relocate_section function.  */
1890       if (! elf_hash_table (info)->dynamic_sections_created
1891           || (info->shared
1892               && (info->symbolic || h->dynindx == -1)
1893               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1894         {
1895           rela.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
1896           rela.r_addend = (h->root.u.def.value
1897                            + h->root.u.def.section->output_section->vma
1898                            + h->root.u.def.section->output_offset);
1899         }
1900       else
1901         {
1902           BFD_ASSERT((h->got.offset & 1) == 0);
1903           bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1904           rela.r_info = ELF64_R_INFO (h->dynindx, R_390_GLOB_DAT);
1905           rela.r_addend = 0;
1906         }
1907
1908       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1909                                  ((Elf64_External_Rela *) srela->contents
1910                                   + srela->reloc_count));
1911       ++srela->reloc_count;
1912     }
1913
1914   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1915     {
1916       asection *s;
1917       Elf_Internal_Rela rela;
1918
1919       /* This symbols needs a copy reloc.  Set it up.  */
1920
1921       BFD_ASSERT (h->dynindx != -1
1922                   && (h->root.type == bfd_link_hash_defined
1923                       || h->root.type == bfd_link_hash_defweak));
1924
1925
1926       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1927                                    ".rela.bss");
1928       BFD_ASSERT (s != NULL);
1929
1930       rela.r_offset = (h->root.u.def.value
1931                        + h->root.u.def.section->output_section->vma
1932                        + h->root.u.def.section->output_offset);
1933       rela.r_info = ELF64_R_INFO (h->dynindx, R_390_COPY);
1934       rela.r_addend = 0;
1935       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1936                                  ((Elf64_External_Rela *) s->contents
1937                                   + s->reloc_count));
1938       ++s->reloc_count;
1939     }
1940
1941   /* Mark some specially defined symbols as absolute.  */
1942   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1943       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1944       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1945     sym->st_shndx = SHN_ABS;
1946
1947   return true;
1948 }
1949
1950 /* Finish up the dynamic sections.  */
1951
1952 static boolean
1953 elf_s390_finish_dynamic_sections (output_bfd, info)
1954      bfd *output_bfd;
1955      struct bfd_link_info *info;
1956 {
1957   bfd *dynobj;
1958   asection *sdyn;
1959   asection *sgot;
1960
1961   dynobj = elf_hash_table (info)->dynobj;
1962
1963   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1964   BFD_ASSERT (sgot != NULL);
1965   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1966
1967   if (elf_hash_table (info)->dynamic_sections_created)
1968     {
1969       asection *splt;
1970       Elf64_External_Dyn *dyncon, *dynconend;
1971
1972       BFD_ASSERT (sdyn != NULL);
1973
1974       dyncon = (Elf64_External_Dyn *) sdyn->contents;
1975       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1976       for (; dyncon < dynconend; dyncon++)
1977         {
1978           Elf_Internal_Dyn dyn;
1979           const char *name;
1980           asection *s;
1981
1982           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
1983
1984           switch (dyn.d_tag)
1985             {
1986             default:
1987               break;
1988
1989             case DT_PLTGOT:
1990               name = ".got";
1991               goto get_vma;
1992             case DT_JMPREL:
1993               name = ".rela.plt";
1994             get_vma:
1995               s = bfd_get_section_by_name(output_bfd, name);
1996               BFD_ASSERT (s != NULL);
1997               dyn.d_un.d_ptr = s->vma;
1998               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
1999               break;
2000
2001             case DT_PLTRELSZ:
2002               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2003               BFD_ASSERT (s != NULL);
2004               if (s->_cooked_size != 0)
2005                 dyn.d_un.d_val = s->_cooked_size;
2006               else
2007                 dyn.d_un.d_val = s->_raw_size;
2008               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2009               break;
2010
2011             case DT_RELASZ:
2012               /* The procedure linkage table relocs (DT_JMPREL) should
2013                  not be included in the overall relocs (DT_RELA).
2014                  Therefore, we override the DT_RELASZ entry here to
2015                  make it not include the JMPREL relocs.  Since the
2016                  linker script arranges for .rela.plt to follow all
2017                  other relocation sections, we don't have to worry
2018                  about changing the DT_RELA entry.  */
2019               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2020               if (s != NULL)
2021                 {
2022                   if (s->_cooked_size != 0)
2023                     dyn.d_un.d_val -= s->_cooked_size;
2024                   else
2025                     dyn.d_un.d_val -= s->_raw_size;
2026                 }
2027               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2028               break;
2029             }
2030         }
2031
2032       /* Fill in the special first entry in the procedure linkage table.  */
2033       splt = bfd_get_section_by_name (dynobj, ".plt");
2034       if (splt && splt->_raw_size > 0)
2035         {
2036           /* fill in blueprint for plt 0 entry */
2037           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD0,
2038                       splt->contents );
2039           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD1,
2040                       splt->contents +4 );
2041           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD3,
2042                       splt->contents +12 );
2043           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD4,
2044                       splt->contents +16 );
2045           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD5,
2046                       splt->contents +20 );
2047           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD6,
2048                       splt->contents + 24);
2049           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD7,
2050                       splt->contents + 28 );
2051           /* Fixup relative address to start of GOT */
2052           bfd_put_32 (output_bfd,
2053                       (sgot->output_section->vma + sgot->output_offset
2054                        - splt->output_section->vma - 6)/2,
2055                       splt->contents + 8);
2056         }
2057
2058       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
2059         PLT_ENTRY_SIZE;
2060     }
2061
2062   /* Set the first entry in the global offset table to the address of
2063      the dynamic section.  */
2064   if (sgot->_raw_size > 0)
2065     {
2066       if (sdyn == NULL)
2067         bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
2068       else
2069         bfd_put_64 (output_bfd,
2070                     sdyn->output_section->vma + sdyn->output_offset,
2071                     sgot->contents);
2072
2073       /* One entry for shared object struct ptr.  */
2074       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2075       /* One entry for _dl_runtime_resolve.  */
2076       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + 12);
2077     }
2078
2079   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 8;
2080
2081   return true;
2082 }
2083
2084 static boolean
2085 elf_s390_object_p (abfd)
2086      bfd *abfd;
2087 {
2088   return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_esame);
2089 }
2090
2091
2092 static enum elf_reloc_type_class
2093 elf_s390_reloc_type_class (rela)
2094      const Elf_Internal_Rela *rela;
2095 {
2096   switch ((int) ELF64_R_TYPE (rela->r_info))
2097     {
2098     case R_390_RELATIVE:
2099       return reloc_class_relative;
2100     case R_390_JMP_SLOT:
2101       return reloc_class_plt;
2102     case R_390_COPY:
2103       return reloc_class_copy;
2104     default:
2105       return reloc_class_normal;
2106     }
2107 }
2108
2109 /*
2110  * Why was the hash table entry size definition changed from
2111  * ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and
2112  * this is the only reason for the s390_elf64_size_info structure.
2113  */
2114
2115 const struct elf_size_info s390_elf64_size_info =
2116 {
2117   sizeof (Elf64_External_Ehdr),
2118   sizeof (Elf64_External_Phdr),
2119   sizeof (Elf64_External_Shdr),
2120   sizeof (Elf64_External_Rel),
2121   sizeof (Elf64_External_Rela),
2122   sizeof (Elf64_External_Sym),
2123   sizeof (Elf64_External_Dyn),
2124   sizeof (Elf_External_Note),
2125   8,            /* hash-table entry size */
2126   1,            /* internal relocations per external relocations */
2127   64,           /* arch_size */
2128   8,            /* file_align */
2129   ELFCLASS64, EV_CURRENT,
2130   bfd_elf64_write_out_phdrs,
2131   bfd_elf64_write_shdrs_and_ehdr,
2132   bfd_elf64_write_relocs,
2133   bfd_elf64_swap_symbol_out,
2134   bfd_elf64_slurp_reloc_table,
2135   bfd_elf64_slurp_symbol_table,
2136   bfd_elf64_swap_dyn_in,
2137   bfd_elf64_swap_dyn_out,
2138   NULL,
2139   NULL,
2140   NULL,
2141   NULL
2142 };
2143
2144 #define TARGET_BIG_SYM  bfd_elf64_s390_vec
2145 #define TARGET_BIG_NAME "elf64-s390"
2146 #define ELF_ARCH        bfd_arch_s390
2147 #define ELF_MACHINE_CODE EM_S390
2148 #define ELF_MACHINE_ALT1 EM_S390_OLD
2149 #define ELF_MAXPAGESIZE 0x1000
2150
2151 #define elf_backend_size_info           s390_elf64_size_info
2152
2153 #define elf_backend_can_gc_sections     1
2154 #define elf_backend_can_refcount        1
2155 #define elf_backend_want_got_plt        1
2156 #define elf_backend_plt_readonly        1
2157 #define elf_backend_want_plt_sym        0
2158 #define elf_backend_got_header_size     24
2159 #define elf_backend_plt_header_size     PLT_ENTRY_SIZE
2160
2161 #define elf_info_to_howto               elf_s390_info_to_howto
2162
2163 #define bfd_elf64_bfd_final_link              _bfd_elf64_gc_common_final_link
2164 #define bfd_elf64_bfd_is_local_label_name     elf_s390_is_local_label_name
2165 #define bfd_elf64_bfd_link_hash_table_create  elf_s390_link_hash_table_create
2166 #define bfd_elf64_bfd_reloc_type_lookup       elf_s390_reloc_type_lookup
2167
2168 #define elf_backend_adjust_dynamic_symbol     elf_s390_adjust_dynamic_symbol
2169 #define elf_backend_check_relocs              elf_s390_check_relocs
2170 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
2171 #define elf_backend_finish_dynamic_sections   elf_s390_finish_dynamic_sections
2172 #define elf_backend_finish_dynamic_symbol     elf_s390_finish_dynamic_symbol
2173 #define elf_backend_gc_mark_hook              elf_s390_gc_mark_hook
2174 #define elf_backend_gc_sweep_hook             elf_s390_gc_sweep_hook
2175 #define elf_backend_relocate_section          elf_s390_relocate_section
2176 #define elf_backend_size_dynamic_sections     elf_s390_size_dynamic_sections
2177 #define elf_backend_reloc_type_class          elf_s390_reloc_type_class
2178
2179 #define elf_backend_object_p                  elf_s390_object_p
2180
2181 #include "elf64-target.h"