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