* elf64-s390.c (elf_s390_check_relocs): Set DF_TEXTREL if the
[external/binutils.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     0xc0100000
293 #define PLT_ENTRY_WORD1     0x0000e310
294 #define PLT_ENTRY_WORD2     0x10000004
295 #define PLT_ENTRY_WORD3     0x07f10d10
296 #define PLT_ENTRY_WORD4     0xe310100c
297 #define PLT_ENTRY_WORD5     0x0014c0f4
298 #define PLT_ENTRY_WORD6     0x00000000
299 #define PLT_ENTRY_WORD7     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     0xe310f038
317 #define PLT_FIRST_ENTRY_WORD1     0x0024c010
318 #define PLT_FIRST_ENTRY_WORD2     0x00000000
319 #define PLT_FIRST_ENTRY_WORD3     0xd207f030
320 #define PLT_FIRST_ENTRY_WORD4     0x1008e310
321 #define PLT_FIRST_ENTRY_WORD5     0x10100004
322 #define PLT_FIRST_ENTRY_WORD6     0x07f10700
323 #define PLT_FIRST_ENTRY_WORD7     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
419   ret = ((struct elf_s390_link_hash_table *)
420          bfd_alloc (abfd, sizeof (struct elf_s390_link_hash_table)));
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                   size_t 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);
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, 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       if (! info->shared)
1204         {
1205           if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
1206             return false;
1207         }
1208
1209       if (plt)
1210         {
1211           if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0)
1212               || ! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1213               || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1214               || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
1215             return false;
1216         }
1217
1218       if (relocs)
1219         {
1220           if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
1221               || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
1222               || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
1223                                             sizeof (Elf64_External_Rela)))
1224             return false;
1225          }
1226
1227       if ((info->flags & DF_TEXTREL) != 0)
1228         {
1229           if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
1230             return false;
1231           info->flags |= DF_TEXTREL;
1232         }
1233     }
1234
1235   return true;
1236 }
1237
1238 /* This function is called via elf64_s390_link_hash_traverse if we are
1239    creating a shared object with -Bsymbolic.  It discards the space
1240    allocated to copy PC relative relocs against symbols which are
1241    defined in regular objects.  We allocated space for them in the
1242    check_relocs routine, but we won't fill them in in the
1243    relocate_section routine.  */
1244
1245 /*ARGSUSED*/
1246 static boolean
1247 elf_s390_discard_copies (h, inf)
1248      struct elf_s390_link_hash_entry *h;
1249      PTR inf;
1250 {
1251   struct elf_s390_pcrel_relocs_copied *s;
1252   struct bfd_link_info *info = (struct bfd_link_info *) inf;
1253
1254   /* If a symbol has been forced local or we have found a regular
1255      definition for the symbolic link case, then we won't be needing
1256      any relocs.  */
1257   if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1258       && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1259           || info->symbolic))
1260     {
1261       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1262         s->section->_raw_size -= s->count * sizeof (Elf64_External_Rela);
1263     }
1264
1265   return true;
1266 }
1267 /* Relocate a 390 ELF section.  */
1268
1269 static boolean
1270 elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
1271                               contents, relocs, local_syms, local_sections)
1272      bfd *output_bfd;
1273      struct bfd_link_info *info;
1274      bfd *input_bfd;
1275      asection *input_section;
1276      bfd_byte *contents;
1277      Elf_Internal_Rela *relocs;
1278      Elf_Internal_Sym *local_syms;
1279      asection **local_sections;
1280 {
1281   bfd *dynobj;
1282   Elf_Internal_Shdr *symtab_hdr;
1283   struct elf_link_hash_entry **sym_hashes;
1284   bfd_vma *local_got_offsets;
1285   asection *sgot;
1286   asection *splt;
1287   asection *sreloc;
1288   Elf_Internal_Rela *rel;
1289   Elf_Internal_Rela *relend;
1290
1291   dynobj = elf_hash_table (info)->dynobj;
1292   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1293   sym_hashes = elf_sym_hashes (input_bfd);
1294   local_got_offsets = elf_local_got_offsets (input_bfd);
1295
1296   sgot = NULL;
1297   splt = NULL;
1298   sreloc = NULL;
1299   if (dynobj != NULL)
1300     {
1301       splt = bfd_get_section_by_name (dynobj, ".plt");
1302       sgot = bfd_get_section_by_name (dynobj, ".got");
1303     }
1304
1305   rel = relocs;
1306   relend = relocs + input_section->reloc_count;
1307   for (; rel < relend; rel++)
1308     {
1309       int r_type;
1310       reloc_howto_type *howto;
1311       unsigned long r_symndx;
1312       struct elf_link_hash_entry *h;
1313       Elf_Internal_Sym *sym;
1314       asection *sec;
1315       bfd_vma relocation;
1316       bfd_reloc_status_type r;
1317
1318       r_type = ELF64_R_TYPE (rel->r_info);
1319       if (r_type == R_390_GNU_VTINHERIT
1320           || r_type == R_390_GNU_VTENTRY)
1321         continue;
1322       if (r_type < 0 || r_type >= (int) R_390_max)
1323         {
1324           bfd_set_error (bfd_error_bad_value);
1325           return false;
1326         }
1327       howto = elf_howto_table + r_type;
1328
1329       r_symndx = ELF64_R_SYM (rel->r_info);
1330
1331       if (info->relocateable)
1332         {
1333           /* This is a relocateable link.  We don't have to change
1334              anything, unless the reloc is against a section symbol,
1335              in which case we have to adjust according to where the
1336              section symbol winds up in the output section.  */
1337           if (r_symndx < symtab_hdr->sh_info)
1338             {
1339               sym = local_syms + r_symndx;
1340               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1341                 {
1342                   sec = local_sections[r_symndx];
1343                   rel->r_addend += sec->output_offset + sym->st_value;
1344                 }
1345             }
1346
1347           continue;
1348         }
1349
1350       /* This is a final link.  */
1351       h = NULL;
1352       sym = NULL;
1353       sec = NULL;
1354       if (r_symndx < symtab_hdr->sh_info)
1355         {
1356           sym = local_syms + r_symndx;
1357           sec = local_sections[r_symndx];
1358           relocation = (sec->output_section->vma
1359                         + sec->output_offset
1360                         + sym->st_value);
1361         }
1362       else
1363         {
1364           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1365           while (h->root.type == bfd_link_hash_indirect
1366                  || h->root.type == bfd_link_hash_warning)
1367             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1368           if (h->root.type == bfd_link_hash_defined
1369               || h->root.type == bfd_link_hash_defweak)
1370             {
1371               sec = h->root.u.def.section;
1372               if ((r_type == R_390_GOTPC
1373                    || r_type == R_390_GOTPCDBL)
1374                   || ((r_type == R_390_PLT16DBL ||
1375                        r_type == R_390_PLT32 ||
1376                        r_type == R_390_PLT32DBL ||
1377                        r_type == R_390_PLT64)
1378                       && splt != NULL
1379                       && h->plt.offset != (bfd_vma) -1)
1380                   || ((r_type == R_390_GOT12 ||
1381                        r_type == R_390_GOT16 ||
1382                        r_type == R_390_GOT32 ||
1383                        r_type == R_390_GOT64 ||
1384                        r_type == R_390_GOTENT)
1385                       && elf_hash_table (info)->dynamic_sections_created
1386                       && (! info->shared
1387                           || (! info->symbolic && h->dynindx != -1)
1388                           || (h->elf_link_hash_flags
1389                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
1390                   || (info->shared
1391                       && ((! info->symbolic && h->dynindx != -1)
1392                           || (h->elf_link_hash_flags
1393                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
1394                       && ( r_type == R_390_8 ||
1395                            r_type == R_390_16 ||
1396                            r_type == R_390_32 ||
1397                            r_type == R_390_64 ||
1398                            r_type == R_390_PC16 ||
1399                            r_type == R_390_PC16DBL ||
1400                            r_type == R_390_PC32 ||
1401                            r_type == R_390_PC32DBL ||
1402                            r_type == R_390_PC64)
1403                       && ((input_section->flags & SEC_ALLOC) != 0
1404                           /* DWARF will emit R_386_32 relocations in its
1405                              sections against symbols defined externally
1406                              in shared libraries.  We can't do anything
1407                              with them here.  */
1408                           || ((input_section->flags & SEC_DEBUGGING) != 0
1409                               && (h->elf_link_hash_flags
1410                                   & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
1411                 {
1412                   /* In these cases, we don't need the relocation
1413                      value.  We check specially because in some
1414                      obscure cases sec->output_section will be NULL.  */
1415                   relocation = 0;
1416                 }
1417               else if (sec->output_section == NULL)
1418                 {
1419                   (*_bfd_error_handler)
1420                     (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1421                      bfd_get_filename (input_bfd), h->root.root.string,
1422                      bfd_get_section_name (input_bfd, input_section));
1423                   relocation = 0;
1424                 }
1425               else
1426                 relocation = (h->root.u.def.value
1427                               + sec->output_section->vma
1428                               + sec->output_offset);
1429             }
1430           else if (h->root.type == bfd_link_hash_undefweak)
1431             relocation = 0;
1432           else if (info->shared && !info->symbolic
1433                    && !info->no_undefined
1434                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1435             relocation = 0;
1436           else
1437             {
1438               if (! ((*info->callbacks->undefined_symbol)
1439                      (info, h->root.root.string, input_bfd,
1440               input_section, rel->r_offset,
1441                      (!info->shared || info->no_undefined
1442                       || ELF_ST_VISIBILITY (h->other)))))
1443                 return false;
1444               relocation = 0;
1445             }
1446         }
1447
1448       switch (r_type)
1449         {
1450         case R_390_GOT12:
1451         case R_390_GOT16:
1452         case R_390_GOT32:
1453         case R_390_GOT64:
1454         case R_390_GOTENT:
1455           /* Relocation is to the entry for this symbol in the global
1456              offset table.  */
1457           BFD_ASSERT (sgot != NULL);
1458
1459           if (h != NULL)
1460             {
1461               bfd_vma off;
1462
1463               off = h->got.offset;
1464               BFD_ASSERT (off != (bfd_vma) -1);
1465
1466               if (! elf_hash_table (info)->dynamic_sections_created
1467                   || (info->shared
1468                       && (info->symbolic || h->dynindx == -1)
1469                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1470                 {
1471                   /* This is actually a static link, or it is a
1472                      -Bsymbolic link and the symbol is defined
1473                      locally, or the symbol was forced to be local
1474                      because of a version file.  We must initialize
1475                      this entry in the global offset table.  Since the
1476                      offset must always be a multiple of 2, we use the
1477                      least significant bit to record whether we have
1478                      initialized it already.
1479
1480                      When doing a dynamic link, we create a .rel.got
1481                      relocation entry to initialize the value.  This
1482                      is done in the finish_dynamic_symbol routine.  */
1483                   if ((off & 1) != 0)
1484                     off &= ~1;
1485                   else
1486                     {
1487                       bfd_put_64 (output_bfd, relocation,
1488                                   sgot->contents + off);
1489                       h->got.offset |= 1;
1490                     }
1491                 }
1492               relocation = sgot->output_offset + off;
1493             }
1494           else
1495             {
1496               bfd_vma off;
1497
1498               BFD_ASSERT (local_got_offsets != NULL
1499                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
1500
1501               off = local_got_offsets[r_symndx];
1502
1503               /* The offset must always be a multiple of 8.  We use
1504                  the least significant bit to record whether we have
1505                  already generated the necessary reloc.  */
1506               if ((off & 1) != 0)
1507                 off &= ~1;
1508               else
1509                 {
1510                   bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1511
1512                   if (info->shared)
1513                     {
1514                       asection *srelgot;
1515                       Elf_Internal_Rela outrel;
1516
1517                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1518                       BFD_ASSERT (srelgot != NULL);
1519
1520                       outrel.r_offset = (sgot->output_section->vma
1521                                          + sgot->output_offset
1522                                          + off);
1523                       outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
1524                       outrel.r_addend = relocation;
1525                       bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1526                                                 (((Elf64_External_Rela *)
1527                                                   srelgot->contents)
1528                                                  + srelgot->reloc_count));
1529                       ++srelgot->reloc_count;
1530                     }
1531
1532                   local_got_offsets[r_symndx] |= 1;
1533                 }
1534
1535               relocation = sgot->output_offset + off;
1536             }
1537
1538           /*
1539            * For @GOTENT the relocation is against the offset between
1540            * the instruction and the symbols entry in the GOT and not
1541            * between the start of the GOT and the symbols entry. We
1542            * add the vma of the GOT to get the correct value.
1543            */
1544           if (r_type == R_390_GOTENT)
1545             relocation += sgot->output_section->vma;
1546
1547           break;
1548
1549         case R_390_GOTOFF:
1550           /* Relocation is relative to the start of the global offset
1551              table.  */
1552
1553           if (sgot == NULL)
1554             {
1555               sgot = bfd_get_section_by_name (dynobj, ".got");
1556               BFD_ASSERT (sgot != NULL);
1557             }
1558
1559           /* Note that sgot->output_offset is not involved in this
1560              calculation.  We always want the start of .got.  If we
1561              defined _GLOBAL_OFFSET_TABLE in a different way, as is
1562              permitted by the ABI, we might have to change this
1563              calculation.  */
1564           relocation -= sgot->output_section->vma;
1565
1566           break;
1567
1568         case R_390_GOTPC:
1569         case R_390_GOTPCDBL:
1570           /* Use global offset table as symbol value.  */
1571
1572           if (sgot == NULL)
1573             {
1574               sgot = bfd_get_section_by_name (dynobj, ".got");
1575               BFD_ASSERT (sgot != NULL);
1576             }
1577
1578           relocation = sgot->output_section->vma;
1579
1580           break;
1581
1582         case R_390_PLT16DBL:
1583         case R_390_PLT32:
1584         case R_390_PLT32DBL:
1585         case R_390_PLT64:
1586           /* Relocation is to the entry for this symbol in the
1587              procedure linkage table.  */
1588
1589           /* Resolve a PLT32 reloc against a local symbol directly,
1590              without using the procedure linkage table.  */
1591           if (h == NULL)
1592             break;
1593
1594           if (h->plt.offset == (bfd_vma) -1 || splt == NULL)
1595             {
1596               /* We didn't make a PLT entry for this symbol.  This
1597                  happens when statically linking PIC code, or when
1598                  using -Bsymbolic.  */
1599               break;
1600             }
1601
1602           relocation = (splt->output_section->vma
1603                         + splt->output_offset
1604                         + h->plt.offset);
1605
1606           break;
1607
1608         case R_390_8:
1609         case R_390_16:
1610         case R_390_32:
1611         case R_390_64:
1612         case R_390_PC16:
1613         case R_390_PC16DBL:
1614         case R_390_PC32:
1615         case R_390_PC32DBL:
1616         case R_390_PC64:
1617           if (info->shared
1618               && (input_section->flags & SEC_ALLOC) != 0
1619               && (r_type == R_390_8
1620                   || r_type == R_390_16
1621                   || r_type == R_390_32
1622                   || r_type == R_390_64
1623                   || (h != NULL
1624                       && h->dynindx != -1
1625                       && (! info->symbolic
1626                           || (h->elf_link_hash_flags
1627                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1628             {
1629               Elf_Internal_Rela outrel;
1630               boolean skip, relocate;
1631
1632               /* When generating a shared object, these relocations
1633                  are copied into the output file to be resolved at run
1634                  time.  */
1635
1636               if (sreloc == NULL)
1637                 {
1638                   const char *name;
1639
1640                   name = (bfd_elf_string_from_elf_section
1641                           (input_bfd,
1642                            elf_elfheader (input_bfd)->e_shstrndx,
1643                            elf_section_data (input_section)->rel_hdr.sh_name));
1644                   if (name == NULL)
1645                     return false;
1646
1647                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1648                               && strcmp (bfd_get_section_name (input_bfd,
1649                                                                input_section),
1650                                          name + 5) == 0);
1651
1652                   sreloc = bfd_get_section_by_name (dynobj, name);
1653                   BFD_ASSERT (sreloc != NULL);
1654                 }
1655
1656               skip = false;
1657
1658               if (elf_section_data (input_section)->stab_info == NULL)
1659                 outrel.r_offset = rel->r_offset;
1660               else
1661                 {
1662                   bfd_vma off;
1663
1664                   off = (_bfd_stab_section_offset
1665                          (output_bfd, &elf_hash_table (info)->stab_info,
1666                           input_section,
1667                           &elf_section_data (input_section)->stab_info,
1668                           rel->r_offset));
1669                   if (off == (bfd_vma) -1)
1670                     skip = true;
1671                   outrel.r_offset = off;
1672                 }
1673
1674               outrel.r_offset += (input_section->output_section->vma
1675                                   + input_section->output_offset);
1676
1677               if (skip)
1678                 {
1679                   memset (&outrel, 0, sizeof outrel);
1680                   relocate = false;
1681                 }
1682               else if (r_type == R_390_PC16 ||
1683                        r_type == R_390_PC16DBL ||
1684                        r_type == R_390_PC32 ||
1685                        r_type == R_390_PC32DBL ||
1686                        r_type == R_390_PC64)
1687                 {
1688                   BFD_ASSERT (h != NULL && h->dynindx != -1);
1689                   relocate = false;
1690                   outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1691                   outrel.r_addend = relocation + rel->r_addend;
1692                 }
1693               else
1694                 {
1695                   /* h->dynindx may be -1 if this symbol was marked to
1696                      become local.  */
1697                   if (h == NULL
1698                       || ((info->symbolic || h->dynindx == -1)
1699                           && (h->elf_link_hash_flags
1700                               & ELF_LINK_HASH_DEF_REGULAR) != 0))
1701                     {
1702                       relocate = true;
1703                       outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
1704                       outrel.r_addend = relocation + rel->r_addend;
1705                     }
1706                   else
1707                     {
1708                       BFD_ASSERT (h->dynindx != -1);
1709                       relocate = false;
1710                       outrel.r_info = ELF64_R_INFO (h->dynindx, R_390_64);
1711                       outrel.r_addend = relocation + rel->r_addend;
1712                     }
1713                 }
1714
1715               bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1716                                         (((Elf64_External_Rela *)
1717                                           sreloc->contents)
1718                                          + sreloc->reloc_count));
1719               ++sreloc->reloc_count;
1720
1721               /* If this reloc is against an external symbol, we do
1722                  not want to fiddle with the addend.  Otherwise, we
1723                  need to include the symbol value so that it becomes
1724                  an addend for the dynamic reloc.  */
1725               if (! relocate)
1726                 continue;
1727             }
1728
1729           break;
1730
1731         default:
1732           break;
1733         }
1734
1735       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1736                                       contents, rel->r_offset,
1737                                       relocation, rel->r_addend);
1738
1739       if (r != bfd_reloc_ok)
1740         {
1741           switch (r)
1742             {
1743             default:
1744             case bfd_reloc_outofrange:
1745               abort ();
1746             case bfd_reloc_overflow:
1747               {
1748                 const char *name;
1749
1750                 if (h != NULL)
1751                   name = h->root.root.string;
1752                 else
1753                   {
1754                     name = bfd_elf_string_from_elf_section (input_bfd,
1755                                                             symtab_hdr->sh_link,
1756                                                             sym->st_name);
1757                     if (name == NULL)
1758                       return false;
1759                     if (*name == '\0')
1760                       name = bfd_section_name (input_bfd, sec);
1761                   }
1762                 if (! ((*info->callbacks->reloc_overflow)
1763                        (info, name, howto->name, (bfd_vma) 0,
1764                         input_bfd, input_section, rel->r_offset)))
1765                   return false;
1766               }
1767               break;
1768             }
1769         }
1770     }
1771
1772   return true;
1773 }
1774
1775 /* Finish up dynamic symbol handling.  We set the contents of various
1776    dynamic sections here.  */
1777
1778 static boolean
1779 elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym)
1780      bfd *output_bfd;
1781      struct bfd_link_info *info;
1782      struct elf_link_hash_entry *h;
1783      Elf_Internal_Sym *sym;
1784 {
1785   bfd *dynobj;
1786
1787   dynobj = elf_hash_table (info)->dynobj;
1788
1789   if (h->plt.offset != (bfd_vma) -1)
1790     {
1791       asection *splt;
1792       asection *srela;
1793       Elf_Internal_Rela rela;
1794       bfd_vma got_offset;
1795       bfd_vma plt_index;
1796       asection *sgot;
1797
1798       /* This symbol has an entry in the procedure linkage table.  Set
1799          it up.  */
1800
1801       BFD_ASSERT (h->dynindx != -1);
1802
1803       splt = bfd_get_section_by_name (dynobj, ".plt");
1804       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1805       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1806       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1807
1808       /* Calc. index no.
1809          Current offset - size first entry / entry size.  */
1810       plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
1811
1812       /* Offset in GOT is PLT index plus GOT headers(3) times 8,
1813          addr & GOT addr.  */
1814       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
1815
1816       /* Fill in the blueprint of a PLT.  */
1817       bfd_put_32 (output_bfd, PLT_ENTRY_WORD0,
1818                   splt->contents + h->plt.offset);
1819       bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
1820                   splt->contents + h->plt.offset + 4);
1821       bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1822                   splt->contents + h->plt.offset + 8);
1823       bfd_put_32 (output_bfd, PLT_ENTRY_WORD3,
1824                   splt->contents + h->plt.offset + 12);
1825       bfd_put_32 (output_bfd, PLT_ENTRY_WORD4,
1826                   splt->contents + h->plt.offset + 16);
1827       bfd_put_32 (output_bfd, PLT_ENTRY_WORD5,
1828                   splt->contents + h->plt.offset + 20);
1829       bfd_put_32 (output_bfd, PLT_ENTRY_WORD6,
1830                   splt->contents + h->plt.offset + 24);
1831       bfd_put_32 (output_bfd, PLT_ENTRY_WORD7,
1832                   splt->contents + h->plt.offset + 28);
1833       /* Fixup the relative address to the GOT entry */
1834       bfd_put_32 (output_bfd,
1835                   (sgot->output_section->vma + sgot->output_offset + got_offset
1836                    - (splt->output_section->vma + h->plt.offset))/2,
1837                   splt->contents + h->plt.offset + 2);
1838       /* Fixup the relative branch to PLT 0 */
1839       bfd_put_32 (output_bfd, - (PLT_FIRST_ENTRY_SIZE +
1840                                  (PLT_ENTRY_SIZE * plt_index) + 22)/2,
1841                   splt->contents + h->plt.offset + 24);
1842       /* Fixup offset into symbol table */
1843       bfd_put_32 (output_bfd, plt_index * sizeof (Elf64_External_Rela),
1844                   splt->contents + h->plt.offset + 28);
1845
1846       /* Fill in the entry in the .rela.plt section.  */
1847       rela.r_offset = (sgot->output_section->vma
1848                        + sgot->output_offset
1849                        + got_offset);
1850       rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT);
1851       rela.r_addend = 0;
1852       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1853                                  ((Elf64_External_Rela *) srela->contents
1854                                   + plt_index ));
1855
1856       /* Fill in the entry in the global offset table.
1857          Points to instruction after GOT offset.  */
1858       bfd_put_64 (output_bfd,
1859                   (splt->output_section->vma
1860                    + splt->output_offset
1861                    + h->plt.offset
1862                    + 14),
1863                   sgot->contents + got_offset);
1864
1865
1866       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1867         {
1868           /* Mark the symbol as undefined, rather than as defined in
1869              the .plt section.  Leave the value alone.  */
1870           sym->st_shndx = SHN_UNDEF;
1871         }
1872     }
1873
1874   if (h->got.offset != (bfd_vma) -1)
1875     {
1876       asection *sgot;
1877       asection *srela;
1878       Elf_Internal_Rela rela;
1879
1880       /* This symbol has an entry in the global offset table.  Set it
1881          up.  */
1882
1883       sgot = bfd_get_section_by_name (dynobj, ".got");
1884       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1885       BFD_ASSERT (sgot != NULL && srela != NULL);
1886
1887       rela.r_offset = (sgot->output_section->vma
1888                        + sgot->output_offset
1889                        + (h->got.offset &~ 1));
1890
1891       /* If this is a static link, or it is a -Bsymbolic link and the
1892          symbol is defined locally or was forced to be local because
1893          of a version file, we just want to emit a RELATIVE reloc.
1894          The entry in the global offset table will already have been
1895          initialized in the relocate_section function.  */
1896       if (! elf_hash_table (info)->dynamic_sections_created
1897           || (info->shared
1898               && (info->symbolic || h->dynindx == -1)
1899               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1900         {
1901           rela.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
1902           rela.r_addend = (h->root.u.def.value
1903                            + h->root.u.def.section->output_section->vma
1904                            + h->root.u.def.section->output_offset);
1905         }
1906       else
1907         {
1908           BFD_ASSERT((h->got.offset & 1) == 0);
1909           bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1910           rela.r_info = ELF64_R_INFO (h->dynindx, R_390_GLOB_DAT);
1911           rela.r_addend = 0;
1912         }
1913
1914       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1915                                  ((Elf64_External_Rela *) srela->contents
1916                                   + srela->reloc_count));
1917       ++srela->reloc_count;
1918     }
1919
1920   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1921     {
1922       asection *s;
1923       Elf_Internal_Rela rela;
1924
1925       /* This symbols needs a copy reloc.  Set it up.  */
1926
1927       BFD_ASSERT (h->dynindx != -1
1928                   && (h->root.type == bfd_link_hash_defined
1929                       || h->root.type == bfd_link_hash_defweak));
1930
1931
1932       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1933                                    ".rela.bss");
1934       BFD_ASSERT (s != NULL);
1935
1936       rela.r_offset = (h->root.u.def.value
1937                        + h->root.u.def.section->output_section->vma
1938                        + h->root.u.def.section->output_offset);
1939       rela.r_info = ELF64_R_INFO (h->dynindx, R_390_COPY);
1940       rela.r_addend = 0;
1941       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1942                                  ((Elf64_External_Rela *) s->contents
1943                                   + s->reloc_count));
1944       ++s->reloc_count;
1945     }
1946
1947   /* Mark some specially defined symbols as absolute.  */
1948   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1949       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1950       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1951     sym->st_shndx = SHN_ABS;
1952
1953   return true;
1954 }
1955
1956 /* Finish up the dynamic sections.  */
1957
1958 static boolean
1959 elf_s390_finish_dynamic_sections (output_bfd, info)
1960      bfd *output_bfd;
1961      struct bfd_link_info *info;
1962 {
1963   bfd *dynobj;
1964   asection *sdyn;
1965   asection *sgot;
1966
1967   dynobj = elf_hash_table (info)->dynobj;
1968
1969   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1970   BFD_ASSERT (sgot != NULL);
1971   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1972
1973   if (elf_hash_table (info)->dynamic_sections_created)
1974     {
1975       asection *splt;
1976       Elf64_External_Dyn *dyncon, *dynconend;
1977
1978       BFD_ASSERT (sdyn != NULL);
1979
1980       dyncon = (Elf64_External_Dyn *) sdyn->contents;
1981       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1982       for (; dyncon < dynconend; dyncon++)
1983         {
1984           Elf_Internal_Dyn dyn;
1985           const char *name;
1986           asection *s;
1987
1988           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
1989
1990           switch (dyn.d_tag)
1991             {
1992             default:
1993               break;
1994
1995             case DT_PLTGOT:
1996               name = ".got";
1997               goto get_vma;
1998             case DT_JMPREL:
1999               name = ".rela.plt";
2000             get_vma:
2001               s = bfd_get_section_by_name(output_bfd, name);
2002               BFD_ASSERT (s != NULL);
2003               dyn.d_un.d_ptr = s->vma;
2004               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2005               break;
2006
2007             case DT_PLTRELSZ:
2008               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2009               BFD_ASSERT (s != NULL);
2010               if (s->_cooked_size != 0)
2011                 dyn.d_un.d_val = s->_cooked_size;
2012               else
2013                 dyn.d_un.d_val = s->_raw_size;
2014               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2015               break;
2016
2017             case DT_RELASZ:
2018               /* The procedure linkage table relocs (DT_JMPREL) should
2019                  not be included in the overall relocs (DT_RELA).
2020                  Therefore, we override the DT_RELASZ entry here to
2021                  make it not include the JMPREL relocs.  Since the
2022                  linker script arranges for .rela.plt to follow all
2023                  other relocation sections, we don't have to worry
2024                  about changing the DT_RELA entry.  */
2025               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2026               if (s != NULL)
2027                 {
2028                   if (s->_cooked_size != 0)
2029                     dyn.d_un.d_val -= s->_cooked_size;
2030                   else
2031                     dyn.d_un.d_val -= s->_raw_size;
2032                 }
2033               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2034               break;
2035             }
2036         }
2037
2038       /* Fill in the special first entry in the procedure linkage table.  */
2039       splt = bfd_get_section_by_name (dynobj, ".plt");
2040       if (splt && splt->_raw_size > 0)
2041         {
2042           /* fill in blueprint for plt 0 entry */
2043           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD0,
2044                       splt->contents );
2045           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD1,
2046                       splt->contents +4 );
2047           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD3,
2048                       splt->contents +12 );
2049           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD4,
2050                       splt->contents +16 );
2051           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD5,
2052                       splt->contents +20 );
2053           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD6,
2054                       splt->contents + 24);
2055           bfd_put_32 (output_bfd, PLT_FIRST_ENTRY_WORD7,
2056                       splt->contents + 28 );
2057           /* Fixup relative address to start of GOT */
2058           bfd_put_32 (output_bfd,
2059                       (sgot->output_section->vma + sgot->output_offset
2060                        - splt->output_section->vma - 6)/2,
2061                       splt->contents + 8);
2062         }
2063
2064       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
2065         PLT_ENTRY_SIZE;
2066     }
2067
2068   /* Set the first entry in the global offset table to the address of
2069      the dynamic section.  */
2070   if (sgot->_raw_size > 0)
2071     {
2072       if (sdyn == NULL)
2073         bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
2074       else
2075         bfd_put_64 (output_bfd,
2076                     sdyn->output_section->vma + sdyn->output_offset,
2077                     sgot->contents);
2078
2079       /* One entry for shared object struct ptr.  */
2080       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2081       /* One entry for _dl_runtime_resolve.  */
2082       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + 12);
2083     }
2084
2085   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 8;
2086
2087   return true;
2088 }
2089
2090 static boolean
2091 elf_s390_object_p (abfd)
2092      bfd *abfd;
2093 {
2094   return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_esame);
2095 }
2096
2097
2098 static enum elf_reloc_type_class
2099 elf_s390_reloc_type_class (type)
2100      int type;
2101 {
2102   switch (type)
2103     {
2104     case R_390_RELATIVE:
2105       return reloc_class_relative;
2106     case R_390_JMP_SLOT:
2107       return reloc_class_plt;
2108     case R_390_COPY:
2109       return reloc_class_copy;
2110     default:
2111       return reloc_class_normal;
2112     }
2113 }
2114
2115 /*
2116  * Why was the hash table entry size definition changed from
2117  * ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and
2118  * this is the only reason for the s390_elf64_size_info structure.
2119  */
2120
2121 const struct elf_size_info s390_elf64_size_info =
2122 {
2123   sizeof (Elf64_External_Ehdr),
2124   sizeof (Elf64_External_Phdr),
2125   sizeof (Elf64_External_Shdr),
2126   sizeof (Elf64_External_Rel),
2127   sizeof (Elf64_External_Rela),
2128   sizeof (Elf64_External_Sym),
2129   sizeof (Elf64_External_Dyn),
2130   sizeof (Elf_External_Note),
2131   8,            /* hash-table entry size */
2132   1,            /* internal relocations per external relocations */
2133   64,           /* arch_size */
2134   8,            /* file_align */
2135   ELFCLASS64, EV_CURRENT,
2136   bfd_elf64_write_out_phdrs,
2137   bfd_elf64_write_shdrs_and_ehdr,
2138   bfd_elf64_write_relocs,
2139   bfd_elf64_swap_symbol_out,
2140   bfd_elf64_slurp_reloc_table,
2141   bfd_elf64_slurp_symbol_table,
2142   bfd_elf64_swap_dyn_in,
2143   bfd_elf64_swap_dyn_out,
2144   NULL,
2145   NULL,
2146   NULL,
2147   NULL
2148 };
2149
2150 #define TARGET_BIG_SYM  bfd_elf64_s390_vec
2151 #define TARGET_BIG_NAME "elf64-s390"
2152 #define ELF_ARCH        bfd_arch_s390
2153 #define ELF_MACHINE_CODE EM_S390
2154 #define ELF_MACHINE_ALT1 EM_S390_OLD
2155 #define ELF_MAXPAGESIZE 0x1000
2156
2157 #define elf_backend_size_info           s390_elf64_size_info
2158
2159 #define elf_backend_can_gc_sections     1
2160 #define elf_backend_want_got_plt        1
2161 #define elf_backend_plt_readonly        1
2162 #define elf_backend_want_plt_sym        0
2163 #define elf_backend_got_header_size     24
2164 #define elf_backend_plt_header_size     PLT_ENTRY_SIZE
2165
2166 #define elf_info_to_howto               elf_s390_info_to_howto
2167
2168 #define bfd_elf64_bfd_final_link              _bfd_elf64_gc_common_final_link
2169 #define bfd_elf64_bfd_is_local_label_name     elf_s390_is_local_label_name
2170 #define bfd_elf64_bfd_link_hash_table_create  elf_s390_link_hash_table_create
2171 #define bfd_elf64_bfd_reloc_type_lookup       elf_s390_reloc_type_lookup
2172
2173 #define elf_backend_adjust_dynamic_symbol     elf_s390_adjust_dynamic_symbol
2174 #define elf_backend_check_relocs              elf_s390_check_relocs
2175 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
2176 #define elf_backend_finish_dynamic_sections   elf_s390_finish_dynamic_sections
2177 #define elf_backend_finish_dynamic_symbol     elf_s390_finish_dynamic_symbol
2178 #define elf_backend_gc_mark_hook              elf_s390_gc_mark_hook
2179 #define elf_backend_gc_sweep_hook             elf_s390_gc_sweep_hook
2180 #define elf_backend_relocate_section          elf_s390_relocate_section
2181 #define elf_backend_size_dynamic_sections     elf_s390_size_dynamic_sections
2182 #define elf_backend_reloc_type_class          elf_s390_reloc_type_class
2183
2184 #define elf_backend_object_p                  elf_s390_object_p
2185
2186 #include "elf64-target.h"