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