OSDN Git Service

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