* Makefile.in: Regenerate.
[binutils.git] / bfd / elf32-s390.c
blobbbc2ead947664b848e7c4b10e9631cb10dc43da6
1 /* IBM S/390-specific support for 32-bit ELF
2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Carl B. Pedersen and Martin Schwidefsky.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
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
33 PARAMS ((bfd *, const char *));
34 static struct bfd_hash_entry *link_hash_newfunc
35 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
36 static struct bfd_link_hash_table *elf_s390_link_hash_table_create
37 PARAMS ((bfd *));
38 static boolean create_got_section
39 PARAMS((bfd *, struct bfd_link_info *));
40 static boolean elf_s390_create_dynamic_sections
41 PARAMS((bfd *, struct bfd_link_info *));
42 static void elf_s390_copy_indirect_symbol
43 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
44 struct elf_link_hash_entry *));
45 static boolean elf_s390_check_relocs
46 PARAMS ((bfd *, struct bfd_link_info *, asection *,
47 const Elf_Internal_Rela *));
48 static asection *elf_s390_gc_mark_hook
49 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
50 struct elf_link_hash_entry *, Elf_Internal_Sym *));
51 static boolean elf_s390_gc_sweep_hook
52 PARAMS ((bfd *, struct bfd_link_info *, asection *,
53 const Elf_Internal_Rela *));
54 static boolean elf_s390_adjust_dynamic_symbol
55 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
56 static boolean allocate_dynrelocs
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58 static boolean readonly_dynrelocs
59 PARAMS ((struct elf_link_hash_entry *, PTR));
60 static boolean elf_s390_size_dynamic_sections
61 PARAMS ((bfd *, struct bfd_link_info *));
62 static boolean elf_s390_relocate_section
63 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
64 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
65 static boolean elf_s390_finish_dynamic_symbol
66 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
67 Elf_Internal_Sym *));
68 static enum elf_reloc_type_class elf_s390_reloc_type_class
69 PARAMS ((const Elf_Internal_Rela *));
70 static boolean elf_s390_finish_dynamic_sections
71 PARAMS ((bfd *, struct bfd_link_info *));
72 static boolean elf_s390_object_p PARAMS ((bfd *));
73 static boolean elf_s390_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
75 #include "elf/s390.h"
77 /* The relocation "howto" table. */
79 static reloc_howto_type elf_howto_table[] =
81 HOWTO (R_390_NONE, /* type */
82 0, /* rightshift */
83 0, /* size (0 = byte, 1 = short, 2 = long) */
84 0, /* bitsize */
85 false, /* pc_relative */
86 0, /* bitpos */
87 complain_overflow_dont, /* complain_on_overflow */
88 bfd_elf_generic_reloc, /* special_function */
89 "R_390_NONE", /* name */
90 false, /* partial_inplace */
91 0, /* src_mask */
92 0, /* dst_mask */
93 false), /* pcrel_offset */
95 HOWTO(R_390_8, 0, 0, 8, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_8", false, 0,0x000000ff, false),
96 HOWTO(R_390_12, 0, 1, 12, false, 0, complain_overflow_dont, bfd_elf_generic_reloc, "R_390_12", false, 0,0x00000fff, false),
97 HOWTO(R_390_16, 0, 1, 16, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_16", false, 0,0x0000ffff, false),
98 HOWTO(R_390_32, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_32", false, 0,0xffffffff, false),
99 HOWTO(R_390_PC32, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC32", false, 0,0xffffffff, true),
100 HOWTO(R_390_GOT12, 0, 1, 12, false, 0, complain_overflow_dont, bfd_elf_generic_reloc, "R_390_GOT12", false, 0,0x00000fff, false),
101 HOWTO(R_390_GOT32, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT32", false, 0,0xffffffff, false),
102 HOWTO(R_390_PLT32, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT32", false, 0,0xffffffff, true),
103 HOWTO(R_390_COPY, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_COPY", false, 0,0xffffffff, false),
104 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),
105 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),
106 HOWTO(R_390_RELATIVE, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_RELATIVE",false, 0,0xffffffff, false),
107 HOWTO(R_390_GOTOFF, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTOFF", false, 0,0xffffffff, false),
108 HOWTO(R_390_GOTPC, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTPC", false, 0,0xffffffff, true),
109 HOWTO(R_390_GOT16, 0, 1, 16, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT16", false, 0,0x0000ffff, false),
110 HOWTO(R_390_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16", false, 0,0x0000ffff, true),
111 HOWTO(R_390_PC16DBL, 1, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16DBL", false, 0,0x0000ffff, true),
112 HOWTO(R_390_PLT16DBL, 1, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT16DBL", false, 0,0x0000ffff, true),
113 HOWTO(R_390_PC32DBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC32DBL", false, 0,0xffffffff, true),
114 HOWTO(R_390_PLT32DBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT32DBL", false, 0,0xffffffff, true),
115 HOWTO(R_390_GOTPCDBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTPCDBL", false, 0,0xffffffff, true),
116 HOWTO(R_390_GOTENT, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTENT", false, 0,0xffffffff, true),
119 /* GNU extension to record C++ vtable hierarchy. */
120 static reloc_howto_type elf32_s390_vtinherit_howto =
121 HOWTO (R_390_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", false,0, 0, false);
122 static reloc_howto_type elf32_s390_vtentry_howto =
123 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);
125 static reloc_howto_type *
126 elf_s390_reloc_type_lookup (abfd, code)
127 bfd *abfd ATTRIBUTE_UNUSED;
128 bfd_reloc_code_real_type code;
130 switch (code)
132 case BFD_RELOC_NONE:
133 return &elf_howto_table[(int) R_390_NONE];
134 case BFD_RELOC_8:
135 return &elf_howto_table[(int) R_390_8];
136 case BFD_RELOC_390_12:
137 return &elf_howto_table[(int) R_390_12];
138 case BFD_RELOC_16:
139 return &elf_howto_table[(int) R_390_16];
140 case BFD_RELOC_32:
141 return &elf_howto_table[(int) R_390_32];
142 case BFD_RELOC_CTOR:
143 return &elf_howto_table[(int) R_390_32];
144 case BFD_RELOC_32_PCREL:
145 return &elf_howto_table[(int) R_390_PC32];
146 case BFD_RELOC_390_GOT12:
147 return &elf_howto_table[(int) R_390_GOT12];
148 case BFD_RELOC_32_GOT_PCREL:
149 return &elf_howto_table[(int) R_390_GOT32];
150 case BFD_RELOC_390_PLT32:
151 return &elf_howto_table[(int) R_390_PLT32];
152 case BFD_RELOC_390_COPY:
153 return &elf_howto_table[(int) R_390_COPY];
154 case BFD_RELOC_390_GLOB_DAT:
155 return &elf_howto_table[(int) R_390_GLOB_DAT];
156 case BFD_RELOC_390_JMP_SLOT:
157 return &elf_howto_table[(int) R_390_JMP_SLOT];
158 case BFD_RELOC_390_RELATIVE:
159 return &elf_howto_table[(int) R_390_RELATIVE];
160 case BFD_RELOC_32_GOTOFF:
161 return &elf_howto_table[(int) R_390_GOTOFF];
162 case BFD_RELOC_390_GOTPC:
163 return &elf_howto_table[(int) R_390_GOTPC];
164 case BFD_RELOC_390_GOT16:
165 return &elf_howto_table[(int) R_390_GOT16];
166 case BFD_RELOC_16_PCREL:
167 return &elf_howto_table[(int) R_390_PC16];
168 case BFD_RELOC_390_PC16DBL:
169 return &elf_howto_table[(int) R_390_PC16DBL];
170 case BFD_RELOC_390_PLT16DBL:
171 return &elf_howto_table[(int) R_390_PLT16DBL];
172 case BFD_RELOC_390_PC32DBL:
173 return &elf_howto_table[(int) R_390_PC32DBL];
174 case BFD_RELOC_390_PLT32DBL:
175 return &elf_howto_table[(int) R_390_PLT32DBL];
176 case BFD_RELOC_390_GOTPCDBL:
177 return &elf_howto_table[(int) R_390_GOTPCDBL];
178 case BFD_RELOC_390_GOTENT:
179 return &elf_howto_table[(int) R_390_GOTENT];
180 case BFD_RELOC_VTABLE_INHERIT:
181 return &elf32_s390_vtinherit_howto;
182 case BFD_RELOC_VTABLE_ENTRY:
183 return &elf32_s390_vtentry_howto;
184 default:
185 break;
187 return 0;
190 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
191 and elf32-s390.c has its own copy. */
193 static void
194 elf_s390_info_to_howto (abfd, cache_ptr, dst)
195 bfd *abfd ATTRIBUTE_UNUSED;
196 arelent *cache_ptr;
197 Elf_Internal_Rela *dst;
199 switch (ELF32_R_TYPE(dst->r_info))
201 case R_390_GNU_VTINHERIT:
202 cache_ptr->howto = &elf32_s390_vtinherit_howto;
203 break;
205 case R_390_GNU_VTENTRY:
206 cache_ptr->howto = &elf32_s390_vtentry_howto;
207 break;
209 default:
210 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_390_max);
211 cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
215 static boolean
216 elf_s390_is_local_label_name (abfd, name)
217 bfd *abfd;
218 const char *name;
220 if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L'))
221 return true;
223 return _bfd_elf_is_local_label_name (abfd, name);
226 /* Functions for the 390 ELF linker. */
228 /* The name of the dynamic interpreter. This is put in the .interp
229 section. */
231 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
233 /* The size in bytes of the first entry in the procedure linkage table. */
234 #define PLT_FIRST_ENTRY_SIZE 32
235 /* The size in bytes of an entry in the procedure linkage table. */
236 #define PLT_ENTRY_SIZE 32
238 #define GOT_ENTRY_SIZE 4
240 /* The first three entries in a procedure linkage table are reserved,
241 and the initial contents are unimportant (we zero them out).
242 Subsequent entries look like this. See the SVR4 ABI 386
243 supplement to see how this works. */
245 /* For the s390, simple addr offset can only be 0 - 4096.
246 To use the full 2 GB address space, several instructions
247 are needed to load an address in a register and execute
248 a branch( or just saving the address)
250 Furthermore, only r 0 and 1 are free to use!!! */
252 /* The first 3 words in the GOT are then reserved.
253 Word 0 is the address of the dynamic table.
254 Word 1 is a pointer to a structure describing the object
255 Word 2 is used to point to the loader entry address.
257 The code for position independand PLT entries looks like this:
259 r12 holds addr of the current GOT at entry to the PLT
261 The GOT holds the address in the PLT to be executed.
262 The loader then gets:
263 24(15) = Pointer to the structure describing the object.
264 28(15) = Offset in symbol table
266 The loader must then find the module where the function is
267 and insert the address in the GOT.
269 Note: 390 can only address +- 64 K relative.
270 We check if offset > 65536, then make a relative branch -64xxx
271 back to a previous defined branch
273 PLT1: BASR 1,0 # 2 bytes
274 L 1,22(1) # 4 bytes Load offset in GOT in r 1
275 L 1,(1,12) # 4 bytes Load address from GOT in r1
276 BCR 15,1 # 2 bytes Jump to address
277 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
278 L 1,14(1) # 4 bytes Load offset in symol table in r1
279 BRC 15,-x # 4 bytes Jump to start of PLT
280 .word 0 # 2 bytes filler
281 .long ? # 4 bytes offset in GOT
282 .long ? # 4 bytes offset into symbol table
284 This was the general case. There are two additional, optimizes PLT
285 definitions. One for GOT offsets < 4096 and one for GOT offsets < 32768.
286 First the one for GOT offsets < 4096:
288 PLT1: L 1,<offset>(12) # 4 bytes Load address from GOT in R1
289 BCR 15,1 # 2 bytes Jump to address
290 .word 0,0,0 # 6 bytes filler
291 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
292 L 1,14(1) # 4 bytes Load offset in symbol table in r1
293 BRC 15,-x # 4 bytes Jump to start of PLT
294 .word 0,0,0 # 6 bytes filler
295 .long ? # 4 bytes offset into symbol table
297 Second the one for GOT offsets < 32768:
299 PLT1: LHI 1,<offset> # 4 bytes Load offset in GOT to r1
300 L 1,(1,12) # 4 bytes Load address from GOT to r1
301 BCR 15,1 # 2 bytes Jump to address
302 .word 0 # 2 bytes filler
303 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
304 L 1,14(1) # 4 bytes Load offset in symbol table in r1
305 BRC 15,-x # 4 bytes Jump to start of PLT
306 .word 0,0,0 # 6 bytes filler
307 .long ? # 4 bytes offset into symbol table
309 Total = 32 bytes per PLT entry
311 The code for static build PLT entries looks like this:
313 PLT1: BASR 1,0 # 2 bytes
314 L 1,22(1) # 4 bytes Load address of GOT entry
315 L 1,0(0,1) # 4 bytes Load address from GOT in r1
316 BCR 15,1 # 2 bytes Jump to address
317 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
318 L 1,14(1) # 4 bytes Load offset in symbol table in r1
319 BRC 15,-x # 4 bytes Jump to start of PLT
320 .word 0 # 2 bytes filler
321 .long ? # 4 bytes address of GOT entry
322 .long ? # 4 bytes offset into symbol table */
324 #define PLT_PIC_ENTRY_WORD0 0x0d105810
325 #define PLT_PIC_ENTRY_WORD1 0x10165811
326 #define PLT_PIC_ENTRY_WORD2 0xc00007f1
327 #define PLT_PIC_ENTRY_WORD3 0x0d105810
328 #define PLT_PIC_ENTRY_WORD4 0x100ea7f4
330 #define PLT_PIC12_ENTRY_WORD0 0x5810c000
331 #define PLT_PIC12_ENTRY_WORD1 0x07f10000
332 #define PLT_PIC12_ENTRY_WORD2 0x00000000
333 #define PLT_PIC12_ENTRY_WORD3 0x0d105810
334 #define PLT_PIC12_ENTRY_WORD4 0x100ea7f4
336 #define PLT_PIC16_ENTRY_WORD0 0xa7180000
337 #define PLT_PIC16_ENTRY_WORD1 0x5811c000
338 #define PLT_PIC16_ENTRY_WORD2 0x07f10000
339 #define PLT_PIC16_ENTRY_WORD3 0x0d105810
340 #define PLT_PIC16_ENTRY_WORD4 0x100ea7f4
342 #define PLT_ENTRY_WORD0 0x0d105810
343 #define PLT_ENTRY_WORD1 0x10165810
344 #define PLT_ENTRY_WORD2 0x100007f1
345 #define PLT_ENTRY_WORD3 0x0d105810
346 #define PLT_ENTRY_WORD4 0x100ea7f4
348 /* The first PLT entry pushes the offset into the symbol table
349 from R1 onto the stack at 8(15) and the loader object info
350 at 12(15), loads the loader address in R1 and jumps to it. */
352 /* The first entry in the PLT for PIC code:
354 PLT0:
355 ST 1,28(15) # R1 has offset into symbol table
356 L 1,4(12) # Get loader ino(object struct address)
357 ST 1,24(15) # Store address
358 L 1,8(12) # Entry address of loader in R1
359 BR 1 # Jump to loader
361 The first entry in the PLT for static code:
363 PLT0:
364 ST 1,28(15) # R1 has offset into symbol table
365 BASR 1,0
366 L 1,18(0,1) # Get address of GOT
367 MVC 24(4,15),4(1) # Move loader ino to stack
368 L 1,8(1) # Get address of loader
369 BR 1 # Jump to loader
370 .word 0 # filler
371 .long got # address of GOT */
373 #define PLT_PIC_FIRST_ENTRY_WORD0 0x5010f01c
374 #define PLT_PIC_FIRST_ENTRY_WORD1 0x5810c004
375 #define PLT_PIC_FIRST_ENTRY_WORD2 0x5010f018
376 #define PLT_PIC_FIRST_ENTRY_WORD3 0x5810c008
377 #define PLT_PIC_FIRST_ENTRY_WORD4 0x07f10000
379 #define PLT_FIRST_ENTRY_WORD0 0x5010f01c
380 #define PLT_FIRST_ENTRY_WORD1 0x0d105810
381 #define PLT_FIRST_ENTRY_WORD2 0x1012D203
382 #define PLT_FIRST_ENTRY_WORD3 0xf0181004
383 #define PLT_FIRST_ENTRY_WORD4 0x58101008
384 #define PLT_FIRST_ENTRY_WORD5 0x07f10000
386 /* The s390 linker needs to keep track of the number of relocs that it
387 decides to copy as dynamic relocs in check_relocs for each symbol.
388 This is so that it can later discard them if they are found to be
389 unnecessary. We store the information in a field extending the
390 regular ELF linker hash table. */
392 struct elf_s390_dyn_relocs
394 struct elf_s390_dyn_relocs *next;
396 /* The input section of the reloc. */
397 asection *sec;
399 /* Total number of relocs copied for the input section. */
400 bfd_size_type count;
402 /* Number of pc-relative relocs copied for the input section. */
403 bfd_size_type pc_count;
406 /* s390 ELF linker hash entry. */
408 struct elf_s390_link_hash_entry
410 struct elf_link_hash_entry elf;
412 /* Track dynamic relocs copied for this symbol. */
413 struct elf_s390_dyn_relocs *dyn_relocs;
416 /* s390 ELF linker hash table. */
418 struct elf_s390_link_hash_table
420 struct elf_link_hash_table elf;
422 /* Short-cuts to get to dynamic linker sections. */
423 asection *sgot;
424 asection *sgotplt;
425 asection *srelgot;
426 asection *splt;
427 asection *srelplt;
428 asection *sdynbss;
429 asection *srelbss;
431 /* Small local sym to section mapping cache. */
432 struct sym_sec_cache sym_sec;
435 /* Get the s390 ELF linker hash table from a link_info structure. */
437 #define elf_s390_hash_table(p) \
438 ((struct elf_s390_link_hash_table *) ((p)->hash))
440 /* Create an entry in an s390 ELF linker hash table. */
442 static struct bfd_hash_entry *
443 link_hash_newfunc (entry, table, string)
444 struct bfd_hash_entry *entry;
445 struct bfd_hash_table *table;
446 const char *string;
448 /* Allocate the structure if it has not already been allocated by a
449 subclass. */
450 if (entry == NULL)
452 entry = bfd_hash_allocate (table,
453 sizeof (struct elf_s390_link_hash_entry));
454 if (entry == NULL)
455 return entry;
458 /* Call the allocation method of the superclass. */
459 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
460 if (entry != NULL)
462 struct elf_s390_link_hash_entry *eh;
464 eh = (struct elf_s390_link_hash_entry *) entry;
465 eh->dyn_relocs = NULL;
468 return entry;
471 /* Create an s390 ELF linker hash table. */
473 static struct bfd_link_hash_table *
474 elf_s390_link_hash_table_create (abfd)
475 bfd *abfd;
477 struct elf_s390_link_hash_table *ret;
478 bfd_size_type amt = sizeof (struct elf_s390_link_hash_table);
480 ret = (struct elf_s390_link_hash_table *) bfd_malloc (amt);
481 if (ret == NULL)
482 return NULL;
484 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
486 free (ret);
487 return NULL;
490 ret->sgot = NULL;
491 ret->sgotplt = NULL;
492 ret->srelgot = NULL;
493 ret->splt = NULL;
494 ret->srelplt = NULL;
495 ret->sdynbss = NULL;
496 ret->srelbss = NULL;
497 ret->sym_sec.abfd = NULL;
499 return &ret->elf.root;
502 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
503 shortcuts to them in our hash table. */
505 static boolean
506 create_got_section (dynobj, info)
507 bfd *dynobj;
508 struct bfd_link_info *info;
510 struct elf_s390_link_hash_table *htab;
512 if (! _bfd_elf_create_got_section (dynobj, info))
513 return false;
515 htab = elf_s390_hash_table (info);
516 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
517 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
518 if (!htab->sgot || !htab->sgotplt)
519 abort ();
521 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
522 if (htab->srelgot == NULL
523 || ! bfd_set_section_flags (dynobj, htab->srelgot,
524 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
525 | SEC_IN_MEMORY | SEC_LINKER_CREATED
526 | SEC_READONLY))
527 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
528 return false;
529 return true;
532 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
533 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
534 hash table. */
536 static boolean
537 elf_s390_create_dynamic_sections (dynobj, info)
538 bfd *dynobj;
539 struct bfd_link_info *info;
541 struct elf_s390_link_hash_table *htab;
543 htab = elf_s390_hash_table (info);
544 if (!htab->sgot && !create_got_section (dynobj, info))
545 return false;
547 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
548 return false;
550 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
551 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
552 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
553 if (!info->shared)
554 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
556 if (!htab->splt || !htab->srelplt || !htab->sdynbss
557 || (!info->shared && !htab->srelbss))
558 abort ();
560 return true;
563 /* Copy the extra info we tack onto an elf_link_hash_entry. */
565 static void
566 elf_s390_copy_indirect_symbol (bed, dir, ind)
567 struct elf_backend_data *bed;
568 struct elf_link_hash_entry *dir, *ind;
570 struct elf_s390_link_hash_entry *edir, *eind;
572 edir = (struct elf_s390_link_hash_entry *) dir;
573 eind = (struct elf_s390_link_hash_entry *) ind;
575 if (eind->dyn_relocs != NULL)
577 if (edir->dyn_relocs != NULL)
579 struct elf_s390_dyn_relocs **pp;
580 struct elf_s390_dyn_relocs *p;
582 if (ind->root.type == bfd_link_hash_indirect)
583 abort ();
585 /* Add reloc counts against the weak sym to the strong sym
586 list. Merge any entries against the same section. */
587 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
589 struct elf_s390_dyn_relocs *q;
591 for (q = edir->dyn_relocs; q != NULL; q = q->next)
592 if (q->sec == p->sec)
594 q->pc_count += p->pc_count;
595 q->count += p->count;
596 *pp = p->next;
597 break;
599 if (q == NULL)
600 pp = &p->next;
602 *pp = edir->dyn_relocs;
605 edir->dyn_relocs = eind->dyn_relocs;
606 eind->dyn_relocs = NULL;
609 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
612 /* Look through the relocs for a section during the first phase, and
613 allocate space in the global offset table or procedure linkage
614 table. */
616 static boolean
617 elf_s390_check_relocs (abfd, info, sec, relocs)
618 bfd *abfd;
619 struct bfd_link_info *info;
620 asection *sec;
621 const Elf_Internal_Rela *relocs;
623 struct elf_s390_link_hash_table *htab;
624 Elf_Internal_Shdr *symtab_hdr;
625 struct elf_link_hash_entry **sym_hashes;
626 const Elf_Internal_Rela *rel;
627 const Elf_Internal_Rela *rel_end;
628 asection *sreloc;
630 if (info->relocateable)
631 return true;
633 htab = elf_s390_hash_table (info);
634 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
635 sym_hashes = elf_sym_hashes (abfd);
637 sreloc = NULL;
639 rel_end = relocs + sec->reloc_count;
640 for (rel = relocs; rel < rel_end; rel++)
642 unsigned long r_symndx;
643 struct elf_link_hash_entry *h;
645 r_symndx = ELF32_R_SYM (rel->r_info);
647 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
649 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
650 bfd_archive_filename (abfd),
651 r_symndx);
652 return false;
655 if (r_symndx < symtab_hdr->sh_info)
656 h = NULL;
657 else
658 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
660 switch (ELF32_R_TYPE (rel->r_info))
662 case R_390_GOT12:
663 case R_390_GOT16:
664 case R_390_GOT32:
665 case R_390_GOTENT:
666 /* This symbol requires a global offset table entry. */
667 if (h != NULL)
669 h->got.refcount += 1;
671 else
673 bfd_signed_vma *local_got_refcounts;
675 /* This is a global offset table entry for a local symbol. */
676 local_got_refcounts = elf_local_got_refcounts (abfd);
677 if (local_got_refcounts == NULL)
679 bfd_size_type size;
681 size = symtab_hdr->sh_info;
682 size *= sizeof (bfd_signed_vma);
683 local_got_refcounts = ((bfd_signed_vma *)
684 bfd_zalloc (abfd, size));
685 if (local_got_refcounts == NULL)
686 return false;
687 elf_local_got_refcounts (abfd) = local_got_refcounts;
689 local_got_refcounts[r_symndx] += 1;
691 /* Fall through */
693 case R_390_GOTOFF:
694 case R_390_GOTPC:
695 case R_390_GOTPCDBL:
696 if (htab->sgot == NULL)
698 if (htab->elf.dynobj == NULL)
699 htab->elf.dynobj = abfd;
700 if (!create_got_section (htab->elf.dynobj, info))
701 return false;
703 break;
705 case R_390_PLT16DBL:
706 case R_390_PLT32DBL:
707 case R_390_PLT32:
708 /* This symbol requires a procedure linkage table entry. We
709 actually build the entry in adjust_dynamic_symbol,
710 because this might be a case of linking PIC code which is
711 never referenced by a dynamic object, in which case we
712 don't need to generate a procedure linkage table entry
713 after all. */
715 /* If this is a local symbol, we resolve it directly without
716 creating a procedure linkage table entry. */
717 if (h == NULL)
718 continue;
720 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
721 h->plt.refcount += 1;
722 break;
724 case R_390_8:
725 case R_390_16:
726 case R_390_32:
727 case R_390_PC16:
728 case R_390_PC16DBL:
729 case R_390_PC32DBL:
730 case R_390_PC32:
731 if (h != NULL && !info->shared)
733 /* If this reloc is in a read-only section, we might
734 need a copy reloc. We can't check reliably at this
735 stage whether the section is read-only, as input
736 sections have not yet been mapped to output sections.
737 Tentatively set the flag for now, and correct in
738 adjust_dynamic_symbol. */
739 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
741 /* We may need a .plt entry if the function this reloc
742 refers to is in a shared lib. */
743 h->plt.refcount += 1;
746 /* If we are creating a shared library, and this is a reloc
747 against a global symbol, or a non PC relative reloc
748 against a local symbol, then we need to copy the reloc
749 into the shared library. However, if we are linking with
750 -Bsymbolic, we do not need to copy a reloc against a
751 global symbol which is defined in an object we are
752 including in the link (i.e., DEF_REGULAR is set). At
753 this point we have not seen all the input files, so it is
754 possible that DEF_REGULAR is not set now but will be set
755 later (it is never cleared). In case of a weak definition,
756 DEF_REGULAR may be cleared later by a strong definition in
757 a shared library. We account for that possibility below by
758 storing information in the relocs_copied field of the hash
759 table entry. A similar situation occurs when creating
760 shared libraries and symbol visibility changes render the
761 symbol local.
763 If on the other hand, we are creating an executable, we
764 may need to keep relocations for symbols satisfied by a
765 dynamic library if we manage to avoid copy relocs for the
766 symbol. */
767 if ((info->shared
768 && (sec->flags & SEC_ALLOC) != 0
769 && ((ELF32_R_TYPE (rel->r_info) != R_390_PC16
770 && ELF32_R_TYPE (rel->r_info) != R_390_PC16DBL
771 && ELF32_R_TYPE (rel->r_info) != R_390_PC32DBL
772 && ELF32_R_TYPE (rel->r_info) != R_390_PC32)
773 || (h != NULL
774 && (! info->symbolic
775 || h->root.type == bfd_link_hash_defweak
776 || (h->elf_link_hash_flags
777 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
778 || (!info->shared
779 && (sec->flags & SEC_ALLOC) != 0
780 && h != NULL
781 && (h->root.type == bfd_link_hash_defweak
782 || (h->elf_link_hash_flags
783 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
785 struct elf_s390_dyn_relocs *p;
786 struct elf_s390_dyn_relocs **head;
788 /* We must copy these reloc types into the output file.
789 Create a reloc section in dynobj and make room for
790 this reloc. */
791 if (sreloc == NULL)
793 const char *name;
794 bfd *dynobj;
796 name = (bfd_elf_string_from_elf_section
797 (abfd,
798 elf_elfheader (abfd)->e_shstrndx,
799 elf_section_data (sec)->rel_hdr.sh_name));
800 if (name == NULL)
801 return false;
803 if (strncmp (name, ".rela", 5) != 0
804 || strcmp (bfd_get_section_name (abfd, sec),
805 name + 5) != 0)
807 (*_bfd_error_handler)
808 (_("%s: bad relocation section name `%s\'"),
809 bfd_archive_filename (abfd), name);
812 if (htab->elf.dynobj == NULL)
813 htab->elf.dynobj = abfd;
815 dynobj = htab->elf.dynobj;
816 sreloc = bfd_get_section_by_name (dynobj, name);
817 if (sreloc == NULL)
819 flagword flags;
821 sreloc = bfd_make_section (dynobj, name);
822 flags = (SEC_HAS_CONTENTS | SEC_READONLY
823 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
824 if ((sec->flags & SEC_ALLOC) != 0)
825 flags |= SEC_ALLOC | SEC_LOAD;
826 if (sreloc == NULL
827 || ! bfd_set_section_flags (dynobj, sreloc, flags)
828 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
829 return false;
831 elf_section_data (sec)->sreloc = sreloc;
834 /* If this is a global symbol, we count the number of
835 relocations we need for this symbol. */
836 if (h != NULL)
838 head = &((struct elf_s390_link_hash_entry *) h)->dyn_relocs;
840 else
842 /* Track dynamic relocs needed for local syms too.
843 We really need local syms available to do this
844 easily. Oh well. */
846 asection *s;
847 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
848 sec, r_symndx);
849 if (s == NULL)
850 return false;
852 head = ((struct elf_s390_dyn_relocs **)
853 &elf_section_data (s)->local_dynrel);
856 p = *head;
857 if (p == NULL || p->sec != sec)
859 bfd_size_type amt = sizeof *p;
860 p = ((struct elf_s390_dyn_relocs *)
861 bfd_alloc (htab->elf.dynobj, amt));
862 if (p == NULL)
863 return false;
864 p->next = *head;
865 *head = p;
866 p->sec = sec;
867 p->count = 0;
868 p->pc_count = 0;
871 p->count += 1;
872 if (ELF32_R_TYPE (rel->r_info) == R_390_PC16
873 || ELF32_R_TYPE (rel->r_info) == R_390_PC16DBL
874 || ELF32_R_TYPE (rel->r_info) == R_390_PC32DBL
875 || ELF32_R_TYPE (rel->r_info) == R_390_PC32)
876 p->pc_count += 1;
878 break;
880 /* This relocation describes the C++ object vtable hierarchy.
881 Reconstruct it for later use during GC. */
882 case R_390_GNU_VTINHERIT:
883 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
884 return false;
885 break;
887 /* This relocation describes which C++ vtable entries are actually
888 used. Record for later use during GC. */
889 case R_390_GNU_VTENTRY:
890 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
891 return false;
892 break;
894 default:
895 break;
899 return true;
902 /* Return the section that should be marked against GC for a given
903 relocation. */
905 static asection *
906 elf_s390_gc_mark_hook (sec, info, rel, h, sym)
907 asection *sec;
908 struct bfd_link_info *info ATTRIBUTE_UNUSED;
909 Elf_Internal_Rela *rel;
910 struct elf_link_hash_entry *h;
911 Elf_Internal_Sym *sym;
913 if (h != NULL)
915 switch (ELF32_R_TYPE (rel->r_info))
917 case R_390_GNU_VTINHERIT:
918 case R_390_GNU_VTENTRY:
919 break;
921 default:
922 switch (h->root.type)
924 case bfd_link_hash_defined:
925 case bfd_link_hash_defweak:
926 return h->root.u.def.section;
928 case bfd_link_hash_common:
929 return h->root.u.c.p->section;
931 default:
932 break;
936 else
937 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
939 return NULL;
942 /* Update the got entry reference counts for the section being removed. */
944 static boolean
945 elf_s390_gc_sweep_hook (abfd, info, sec, relocs)
946 bfd *abfd;
947 struct bfd_link_info *info;
948 asection *sec;
949 const Elf_Internal_Rela *relocs;
951 Elf_Internal_Shdr *symtab_hdr;
952 struct elf_link_hash_entry **sym_hashes;
953 bfd_signed_vma *local_got_refcounts;
954 const Elf_Internal_Rela *rel, *relend;
955 unsigned long r_symndx;
956 struct elf_link_hash_entry *h;
958 elf_section_data (sec)->local_dynrel = NULL;
960 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
961 sym_hashes = elf_sym_hashes (abfd);
962 local_got_refcounts = elf_local_got_refcounts (abfd);
964 relend = relocs + sec->reloc_count;
965 for (rel = relocs; rel < relend; rel++)
966 switch (ELF32_R_TYPE (rel->r_info))
968 case R_390_GOT12:
969 case R_390_GOT16:
970 case R_390_GOT32:
971 case R_390_GOTOFF:
972 case R_390_GOTPC:
973 case R_390_GOTPCDBL:
974 case R_390_GOTENT:
975 r_symndx = ELF32_R_SYM (rel->r_info);
976 if (r_symndx >= symtab_hdr->sh_info)
978 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
979 if (h->got.refcount > 0)
980 h->got.refcount -= 1;
982 else if (local_got_refcounts != NULL)
984 if (local_got_refcounts[r_symndx] > 0)
985 local_got_refcounts[r_symndx] -= 1;
987 break;
989 case R_390_8:
990 case R_390_12:
991 case R_390_16:
992 case R_390_32:
993 case R_390_PC16:
994 case R_390_PC16DBL:
995 case R_390_PC32DBL:
996 case R_390_PC32:
997 r_symndx = ELF32_R_SYM (rel->r_info);
998 if (r_symndx >= symtab_hdr->sh_info)
1000 struct elf_s390_link_hash_entry *eh;
1001 struct elf_s390_dyn_relocs **pp;
1002 struct elf_s390_dyn_relocs *p;
1004 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1006 if (!info->shared && h->plt.refcount > 0)
1007 h->plt.refcount -= 1;
1009 eh = (struct elf_s390_link_hash_entry *) h;
1011 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1012 if (p->sec == sec)
1014 if (ELF32_R_TYPE (rel->r_info) == R_390_PC16
1015 || ELF32_R_TYPE (rel->r_info) == R_390_PC16DBL
1016 || ELF32_R_TYPE (rel->r_info) == R_390_PC32DBL
1017 || ELF32_R_TYPE (rel->r_info) == R_390_PC32)
1018 p->pc_count -= 1;
1019 p->count -= 1;
1020 if (p->count == 0)
1021 *pp = p->next;
1022 break;
1025 break;
1027 case R_390_PLT16DBL:
1028 case R_390_PLT32DBL:
1029 case R_390_PLT32:
1030 r_symndx = ELF32_R_SYM (rel->r_info);
1031 if (r_symndx >= symtab_hdr->sh_info)
1033 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1034 if (h->plt.refcount > 0)
1035 h->plt.refcount -= 1;
1037 break;
1039 default:
1040 break;
1043 return true;
1046 /* Adjust a symbol defined by a dynamic object and referenced by a
1047 regular object. The current definition is in some section of the
1048 dynamic object, but we're not including those sections. We have to
1049 change the definition to something the rest of the link can
1050 understand. */
1052 static boolean
1053 elf_s390_adjust_dynamic_symbol (info, h)
1054 struct bfd_link_info *info;
1055 struct elf_link_hash_entry *h;
1057 struct elf_s390_link_hash_table *htab;
1058 struct elf_s390_link_hash_entry * eh;
1059 struct elf_s390_dyn_relocs *p;
1060 asection *s;
1061 unsigned int power_of_two;
1063 /* If this is a function, put it in the procedure linkage table. We
1064 will fill in the contents of the procedure linkage table later
1065 (although we could actually do it here). */
1066 if (h->type == STT_FUNC
1067 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1069 if (h->plt.refcount <= 0
1070 || (! info->shared
1071 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1072 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1073 && h->root.type != bfd_link_hash_undefweak
1074 && h->root.type != bfd_link_hash_undefined))
1076 /* This case can occur if we saw a PLT32 reloc in an input
1077 file, but the symbol was never referred to by a dynamic
1078 object, or if all references were garbage collected. In
1079 such a case, we don't actually need to build a procedure
1080 linkage table, and we can just do a PC32 reloc instead. */
1081 h->plt.offset = (bfd_vma) -1;
1082 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1085 return true;
1087 else
1088 /* It's possible that we incorrectly decided a .plt reloc was
1089 needed for an R_390_PC32 reloc to a non-function sym in
1090 check_relocs. We can't decide accurately between function and
1091 non-function syms in check-relocs; Objects loaded later in
1092 the link may change h->type. So fix it now. */
1093 h->plt.offset = (bfd_vma) -1;
1095 /* If this is a weak symbol, and there is a real definition, the
1096 processor independent code will have arranged for us to see the
1097 real definition first, and we can just use the same value. */
1098 if (h->weakdef != NULL)
1100 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1101 || h->weakdef->root.type == bfd_link_hash_defweak);
1102 h->root.u.def.section = h->weakdef->root.u.def.section;
1103 h->root.u.def.value = h->weakdef->root.u.def.value;
1104 return true;
1107 /* This is a reference to a symbol defined by a dynamic object which
1108 is not a function. */
1110 /* If we are creating a shared library, we must presume that the
1111 only references to the symbol are via the global offset table.
1112 For such cases we need not do anything here; the relocations will
1113 be handled correctly by relocate_section. */
1114 if (info->shared)
1115 return true;
1117 /* If there are no references to this symbol that do not use the
1118 GOT, we don't need to generate a copy reloc. */
1119 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1120 return true;
1122 /* If -z nocopyreloc was given, we won't generate them either. */
1123 if (info->nocopyreloc)
1125 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1126 return true;
1129 eh = (struct elf_s390_link_hash_entry *) h;
1130 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1132 s = p->sec->output_section;
1133 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1134 break;
1137 /* If we didn't find any dynamic relocs in read-only sections, then
1138 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1139 if (p == NULL)
1141 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1142 return true;
1145 /* We must allocate the symbol in our .dynbss section, which will
1146 become part of the .bss section of the executable. There will be
1147 an entry for this symbol in the .dynsym section. The dynamic
1148 object will contain position independent code, so all references
1149 from the dynamic object to this symbol will go through the global
1150 offset table. The dynamic linker will use the .dynsym entry to
1151 determine the address it must put in the global offset table, so
1152 both the dynamic object and the regular object will refer to the
1153 same memory location for the variable. */
1155 htab = elf_s390_hash_table (info);
1157 /* We must generate a R_390_COPY reloc to tell the dynamic linker to
1158 copy the initial value out of the dynamic object and into the
1159 runtime process image. */
1160 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1162 htab->srelbss->_raw_size += sizeof (Elf32_External_Rela);
1163 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1166 /* We need to figure out the alignment required for this symbol. I
1167 have no idea how ELF linkers handle this. */
1168 power_of_two = bfd_log2 (h->size);
1169 if (power_of_two > 3)
1170 power_of_two = 3;
1172 /* Apply the required alignment. */
1173 s = htab->sdynbss;
1174 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1175 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1177 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1178 return false;
1181 /* Define the symbol as being at this point in the section. */
1182 h->root.u.def.section = s;
1183 h->root.u.def.value = s->_raw_size;
1185 /* Increment the section size to make room for the symbol. */
1186 s->_raw_size += h->size;
1188 return true;
1191 /* This is the condition under which elf_s390_finish_dynamic_symbol
1192 will be called from elflink.h. If elflink.h doesn't call our
1193 finish_dynamic_symbol routine, we'll need to do something about
1194 initializing any .plt and .got entries in elf_s390_relocate_section. */
1195 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1196 ((DYN) \
1197 && ((INFO)->shared \
1198 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1199 && ((H)->dynindx != -1 \
1200 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1202 /* Allocate space in .plt, .got and associated reloc sections for
1203 dynamic relocs. */
1205 static boolean
1206 allocate_dynrelocs (h, inf)
1207 struct elf_link_hash_entry *h;
1208 PTR inf;
1210 struct bfd_link_info *info;
1211 struct elf_s390_link_hash_table *htab;
1212 struct elf_s390_link_hash_entry *eh;
1213 struct elf_s390_dyn_relocs *p;
1215 if (h->root.type == bfd_link_hash_indirect)
1216 return true;
1218 if (h->root.type == bfd_link_hash_warning)
1219 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1221 info = (struct bfd_link_info *) inf;
1222 htab = elf_s390_hash_table (info);
1224 if (htab->elf.dynamic_sections_created
1225 && h->plt.refcount > 0)
1227 /* Make sure this symbol is output as a dynamic symbol.
1228 Undefined weak syms won't yet be marked as dynamic. */
1229 if (h->dynindx == -1
1230 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1232 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1233 return false;
1236 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1238 asection *s = htab->splt;
1240 /* If this is the first .plt entry, make room for the special
1241 first entry. */
1242 if (s->_raw_size == 0)
1243 s->_raw_size += PLT_FIRST_ENTRY_SIZE;
1245 h->plt.offset = s->_raw_size;
1247 /* If this symbol is not defined in a regular file, and we are
1248 not generating a shared library, then set the symbol to this
1249 location in the .plt. This is required to make function
1250 pointers compare as equal between the normal executable and
1251 the shared library. */
1252 if (! info->shared
1253 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1255 h->root.u.def.section = s;
1256 h->root.u.def.value = h->plt.offset;
1259 /* Make room for this entry. */
1260 s->_raw_size += PLT_ENTRY_SIZE;
1262 /* We also need to make an entry in the .got.plt section, which
1263 will be placed in the .got section by the linker script. */
1264 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1266 /* We also need to make an entry in the .rela.plt section. */
1267 htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
1269 else
1271 h->plt.offset = (bfd_vma) -1;
1272 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1275 else
1277 h->plt.offset = (bfd_vma) -1;
1278 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1281 if (h->got.refcount > 0)
1283 asection *s;
1284 boolean dyn;
1286 /* Make sure this symbol is output as a dynamic symbol.
1287 Undefined weak syms won't yet be marked as dynamic. */
1288 if (h->dynindx == -1
1289 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1291 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1292 return false;
1295 s = htab->sgot;
1296 h->got.offset = s->_raw_size;
1297 s->_raw_size += GOT_ENTRY_SIZE;
1298 dyn = htab->elf.dynamic_sections_created;
1299 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1300 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1302 else
1303 h->got.offset = (bfd_vma) -1;
1305 eh = (struct elf_s390_link_hash_entry *) h;
1306 if (eh->dyn_relocs == NULL)
1307 return true;
1309 /* In the shared -Bsymbolic case, discard space allocated for
1310 dynamic pc-relative relocs against symbols which turn out to be
1311 defined in regular objects. For the normal shared case, discard
1312 space for pc-relative relocs that have become local due to symbol
1313 visibility changes. */
1315 if (info->shared)
1317 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1318 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1319 || info->symbolic))
1321 struct elf_s390_dyn_relocs **pp;
1323 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1325 p->count -= p->pc_count;
1326 p->pc_count = 0;
1327 if (p->count == 0)
1328 *pp = p->next;
1329 else
1330 pp = &p->next;
1334 else
1336 /* For the non-shared case, discard space for relocs against
1337 symbols which turn out to need copy relocs or are not
1338 dynamic. */
1340 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1341 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1342 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1343 || (htab->elf.dynamic_sections_created
1344 && (h->root.type == bfd_link_hash_undefweak
1345 || h->root.type == bfd_link_hash_undefined))))
1347 /* Make sure this symbol is output as a dynamic symbol.
1348 Undefined weak syms won't yet be marked as dynamic. */
1349 if (h->dynindx == -1
1350 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1352 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1353 return false;
1356 /* If that succeeded, we know we'll be keeping all the
1357 relocs. */
1358 if (h->dynindx != -1)
1359 goto keep;
1362 eh->dyn_relocs = NULL;
1364 keep: ;
1367 /* Finally, allocate space. */
1368 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1370 asection *sreloc = elf_section_data (p->sec)->sreloc;
1371 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
1374 return true;
1377 /* Find any dynamic relocs that apply to read-only sections. */
1379 static boolean
1380 readonly_dynrelocs (h, inf)
1381 struct elf_link_hash_entry *h;
1382 PTR inf;
1384 struct elf_s390_link_hash_entry *eh;
1385 struct elf_s390_dyn_relocs *p;
1387 if (h->root.type == bfd_link_hash_warning)
1388 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1390 eh = (struct elf_s390_link_hash_entry *) h;
1391 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1393 asection *s = p->sec->output_section;
1395 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1397 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1399 info->flags |= DF_TEXTREL;
1401 /* Not an error, just cut short the traversal. */
1402 return false;
1405 return true;
1408 /* Set the sizes of the dynamic sections. */
1410 static boolean
1411 elf_s390_size_dynamic_sections (output_bfd, info)
1412 bfd *output_bfd ATTRIBUTE_UNUSED;
1413 struct bfd_link_info *info;
1415 struct elf_s390_link_hash_table *htab;
1416 bfd *dynobj;
1417 asection *s;
1418 boolean relocs;
1419 bfd *ibfd;
1421 htab = elf_s390_hash_table (info);
1422 dynobj = htab->elf.dynobj;
1423 if (dynobj == NULL)
1424 abort ();
1426 if (htab->elf.dynamic_sections_created)
1428 /* Set the contents of the .interp section to the interpreter. */
1429 if (! info->shared)
1431 s = bfd_get_section_by_name (dynobj, ".interp");
1432 if (s == NULL)
1433 abort ();
1434 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1435 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1439 /* Set up .got offsets for local syms, and space for local dynamic
1440 relocs. */
1441 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1443 bfd_signed_vma *local_got;
1444 bfd_signed_vma *end_local_got;
1445 bfd_size_type locsymcount;
1446 Elf_Internal_Shdr *symtab_hdr;
1447 asection *srela;
1449 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1450 continue;
1452 for (s = ibfd->sections; s != NULL; s = s->next)
1454 struct elf_s390_dyn_relocs *p;
1456 for (p = *((struct elf_s390_dyn_relocs **)
1457 &elf_section_data (s)->local_dynrel);
1458 p != NULL;
1459 p = p->next)
1461 if (!bfd_is_abs_section (p->sec)
1462 && bfd_is_abs_section (p->sec->output_section))
1464 /* Input section has been discarded, either because
1465 it is a copy of a linkonce section or due to
1466 linker script /DISCARD/, so we'll be discarding
1467 the relocs too. */
1469 else if (p->count != 0)
1471 srela = elf_section_data (p->sec)->sreloc;
1472 srela->_raw_size += p->count * sizeof (Elf32_External_Rela);
1473 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1474 info->flags |= DF_TEXTREL;
1479 local_got = elf_local_got_refcounts (ibfd);
1480 if (!local_got)
1481 continue;
1483 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1484 locsymcount = symtab_hdr->sh_info;
1485 end_local_got = local_got + locsymcount;
1486 s = htab->sgot;
1487 srela = htab->srelgot;
1488 for (; local_got < end_local_got; ++local_got)
1490 if (*local_got > 0)
1492 *local_got = s->_raw_size;
1493 s->_raw_size += GOT_ENTRY_SIZE;
1494 if (info->shared)
1495 srela->_raw_size += sizeof (Elf32_External_Rela);
1497 else
1498 *local_got = (bfd_vma) -1;
1502 /* Allocate global sym .plt and .got entries, and space for global
1503 sym dynamic relocs. */
1504 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1506 /* We now have determined the sizes of the various dynamic sections.
1507 Allocate memory for them. */
1508 relocs = false;
1509 for (s = dynobj->sections; s != NULL; s = s->next)
1511 if ((s->flags & SEC_LINKER_CREATED) == 0)
1512 continue;
1514 if (s == htab->splt
1515 || s == htab->sgot
1516 || s == htab->sgotplt)
1518 /* Strip this section if we don't need it; see the
1519 comment below. */
1521 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1523 if (s->_raw_size != 0 && s != htab->srelplt)
1524 relocs = true;
1526 /* We use the reloc_count field as a counter if we need
1527 to copy relocs into the output file. */
1528 s->reloc_count = 0;
1530 else
1532 /* It's not one of our sections, so don't allocate space. */
1533 continue;
1536 if (s->_raw_size == 0)
1538 /* If we don't need this section, strip it from the
1539 output file. This is to handle .rela.bss and
1540 .rela.plt. We must create it in
1541 create_dynamic_sections, because it must be created
1542 before the linker maps input sections to output
1543 sections. The linker does that before
1544 adjust_dynamic_symbol is called, and it is that
1545 function which decides whether anything needs to go
1546 into these sections. */
1548 _bfd_strip_section_from_output (info, s);
1549 continue;
1552 /* Allocate memory for the section contents. We use bfd_zalloc
1553 here in case unused entries are not reclaimed before the
1554 section's contents are written out. This should not happen,
1555 but this way if it does, we get a R_390_NONE reloc instead
1556 of garbage. */
1557 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1558 if (s->contents == NULL)
1559 return false;
1562 if (htab->elf.dynamic_sections_created)
1564 /* Add some entries to the .dynamic section. We fill in the
1565 values later, in elf_s390_finish_dynamic_sections, but we
1566 must add the entries now so that we get the correct size for
1567 the .dynamic section. The DT_DEBUG entry is filled in by the
1568 dynamic linker and used by the debugger. */
1569 #define add_dynamic_entry(TAG, VAL) \
1570 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1572 if (! info->shared)
1574 if (!add_dynamic_entry (DT_DEBUG, 0))
1575 return false;
1578 if (htab->splt->_raw_size != 0)
1580 if (!add_dynamic_entry (DT_PLTGOT, 0)
1581 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1582 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1583 || !add_dynamic_entry (DT_JMPREL, 0))
1584 return false;
1587 if (relocs)
1589 if (!add_dynamic_entry (DT_RELA, 0)
1590 || !add_dynamic_entry (DT_RELASZ, 0)
1591 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1592 return false;
1594 /* If any dynamic relocs apply to a read-only section,
1595 then we need a DT_TEXTREL entry. */
1596 if ((info->flags & DF_TEXTREL) == 0)
1597 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1598 (PTR) info);
1600 if ((info->flags & DF_TEXTREL) != 0)
1602 if (!add_dynamic_entry (DT_TEXTREL, 0))
1603 return false;
1607 #undef add_dynamic_entry
1609 return true;
1612 /* Relocate a 390 ELF section. */
1614 static boolean
1615 elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
1616 contents, relocs, local_syms, local_sections)
1617 bfd *output_bfd;
1618 struct bfd_link_info *info;
1619 bfd *input_bfd;
1620 asection *input_section;
1621 bfd_byte *contents;
1622 Elf_Internal_Rela *relocs;
1623 Elf_Internal_Sym *local_syms;
1624 asection **local_sections;
1626 struct elf_s390_link_hash_table *htab;
1627 Elf_Internal_Shdr *symtab_hdr;
1628 struct elf_link_hash_entry **sym_hashes;
1629 bfd_vma *local_got_offsets;
1630 Elf_Internal_Rela *rel;
1631 Elf_Internal_Rela *relend;
1633 if (info->relocateable)
1634 return true;
1636 htab = elf_s390_hash_table (info);
1637 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1638 sym_hashes = elf_sym_hashes (input_bfd);
1639 local_got_offsets = elf_local_got_offsets (input_bfd);
1641 rel = relocs;
1642 relend = relocs + input_section->reloc_count;
1643 for (; rel < relend; rel++)
1645 int r_type;
1646 reloc_howto_type *howto;
1647 unsigned long r_symndx;
1648 struct elf_link_hash_entry *h;
1649 Elf_Internal_Sym *sym;
1650 asection *sec;
1651 bfd_vma off;
1652 bfd_vma relocation;
1653 boolean unresolved_reloc;
1654 bfd_reloc_status_type r;
1656 r_type = ELF32_R_TYPE (rel->r_info);
1657 if (r_type == (int) R_390_GNU_VTINHERIT
1658 || r_type == (int) R_390_GNU_VTENTRY)
1659 continue;
1660 if (r_type < 0 || r_type >= (int) R_390_max)
1662 bfd_set_error (bfd_error_bad_value);
1663 return false;
1666 howto = elf_howto_table + r_type;
1667 r_symndx = ELF32_R_SYM (rel->r_info);
1668 h = NULL;
1669 sym = NULL;
1670 sec = NULL;
1671 unresolved_reloc = false;
1672 if (r_symndx < symtab_hdr->sh_info)
1674 sym = local_syms + r_symndx;
1675 sec = local_sections[r_symndx];
1676 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1678 else
1680 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1681 while (h->root.type == bfd_link_hash_indirect
1682 || h->root.type == bfd_link_hash_warning)
1683 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1685 if (h->root.type == bfd_link_hash_defined
1686 || h->root.type == bfd_link_hash_defweak)
1688 sec = h->root.u.def.section;
1689 if (sec->output_section == NULL)
1691 /* Set a flag that will be cleared later if we find a
1692 relocation value for this symbol. output_section
1693 is typically NULL for symbols satisfied by a shared
1694 library. */
1695 unresolved_reloc = true;
1696 relocation = 0;
1698 else
1699 relocation = (h->root.u.def.value
1700 + sec->output_section->vma
1701 + sec->output_offset);
1703 else if (h->root.type == bfd_link_hash_undefweak)
1704 relocation = 0;
1705 else if (info->shared
1706 && (!info->symbolic || info->allow_shlib_undefined)
1707 && !info->no_undefined
1708 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1709 relocation = 0;
1710 else
1712 if (! ((*info->callbacks->undefined_symbol)
1713 (info, h->root.root.string, input_bfd,
1714 input_section, rel->r_offset,
1715 (!info->shared || info->no_undefined
1716 || ELF_ST_VISIBILITY (h->other)))))
1717 return false;
1718 relocation = 0;
1722 switch (r_type)
1724 case R_390_GOT12:
1725 case R_390_GOT16:
1726 case R_390_GOT32:
1727 case R_390_GOTENT:
1728 /* Relocation is to the entry for this symbol in the global
1729 offset table. */
1730 if (htab->sgot == NULL)
1731 abort ();
1733 if (h != NULL)
1735 boolean dyn;
1737 off = h->got.offset;
1738 dyn = htab->elf.dynamic_sections_created;
1739 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
1740 || (info->shared
1741 && (info->symbolic
1742 || h->dynindx == -1
1743 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
1744 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1746 /* This is actually a static link, or it is a
1747 -Bsymbolic link and the symbol is defined
1748 locally, or the symbol was forced to be local
1749 because of a version file. We must initialize
1750 this entry in the global offset table. Since the
1751 offset must always be a multiple of 2, we use the
1752 least significant bit to record whether we have
1753 initialized it already.
1755 When doing a dynamic link, we create a .rel.got
1756 relocation entry to initialize the value. This
1757 is done in the finish_dynamic_symbol routine. */
1758 if ((off & 1) != 0)
1759 off &= ~1;
1760 else
1762 bfd_put_32 (output_bfd, relocation,
1763 htab->sgot->contents + off);
1764 h->got.offset |= 1;
1767 else
1768 unresolved_reloc = false;
1770 else
1772 if (local_got_offsets == NULL)
1773 abort ();
1775 off = local_got_offsets[r_symndx];
1777 /* The offset must always be a multiple of 4. We use
1778 the least significant bit to record whether we have
1779 already generated the necessary reloc. */
1780 if ((off & 1) != 0)
1781 off &= ~1;
1782 else
1784 bfd_put_32 (output_bfd, relocation,
1785 htab->sgot->contents + off);
1787 if (info->shared)
1789 asection *srelgot;
1790 Elf_Internal_Rela outrel;
1791 Elf32_External_Rela *loc;
1793 srelgot = htab->srelgot;
1794 if (srelgot == NULL)
1795 abort ();
1797 outrel.r_offset = (htab->sgot->output_section->vma
1798 + htab->sgot->output_offset
1799 + off);
1800 outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
1801 outrel.r_addend = relocation;
1802 loc = (Elf32_External_Rela *) srelgot->contents;
1803 loc += srelgot->reloc_count++;
1804 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1807 local_got_offsets[r_symndx] |= 1;
1811 if (off >= (bfd_vma) -2)
1812 abort ();
1814 relocation = htab->sgot->output_offset + off;
1817 * For @GOTENT the relocation is against the offset between
1818 * the instruction and the symbols entry in the GOT and not
1819 * between the start of the GOT and the symbols entry. We
1820 * add the vma of the GOT to get the correct value.
1822 if (r_type == R_390_GOTENT)
1823 relocation += htab->sgot->output_section->vma;
1825 break;
1827 case R_390_GOTOFF:
1828 /* Relocation is relative to the start of the global offset
1829 table. */
1831 /* Note that sgot->output_offset is not involved in this
1832 calculation. We always want the start of .got. If we
1833 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1834 permitted by the ABI, we might have to change this
1835 calculation. */
1836 relocation -= htab->sgot->output_section->vma;
1837 break;
1839 case R_390_GOTPC:
1840 case R_390_GOTPCDBL:
1841 /* Use global offset table as symbol value. */
1842 relocation = htab->sgot->output_section->vma;
1843 unresolved_reloc = false;
1844 break;
1846 case R_390_PLT16DBL:
1847 case R_390_PLT32DBL:
1848 case R_390_PLT32:
1849 /* Relocation is to the entry for this symbol in the
1850 procedure linkage table. */
1852 /* Resolve a PLT32 reloc against a local symbol directly,
1853 without using the procedure linkage table. */
1854 if (h == NULL)
1855 break;
1857 if (h->plt.offset == (bfd_vma) -1
1858 || htab->splt == NULL)
1860 /* We didn't make a PLT entry for this symbol. This
1861 happens when statically linking PIC code, or when
1862 using -Bsymbolic. */
1863 break;
1866 relocation = (htab->splt->output_section->vma
1867 + htab->splt->output_offset
1868 + h->plt.offset);
1869 unresolved_reloc = false;
1870 break;
1872 case R_390_8:
1873 case R_390_16:
1874 case R_390_32:
1875 case R_390_PC16:
1876 case R_390_PC16DBL:
1877 case R_390_PC32DBL:
1878 case R_390_PC32:
1879 /* r_symndx will be zero only for relocs against symbols
1880 from removed linkonce sections, or sections discarded by
1881 a linker script. */
1882 if (r_symndx == 0
1883 || (input_section->flags & SEC_ALLOC) == 0)
1884 break;
1886 if ((info->shared
1887 && ((r_type != R_390_PC16
1888 && r_type != R_390_PC16DBL
1889 && r_type != R_390_PC32DBL
1890 && r_type != R_390_PC32)
1891 || (h != NULL
1892 && h->dynindx != -1
1893 && (! info->symbolic
1894 || (h->elf_link_hash_flags
1895 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1896 || (!info->shared
1897 && h != NULL
1898 && h->dynindx != -1
1899 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1900 && (((h->elf_link_hash_flags
1901 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1902 && (h->elf_link_hash_flags
1903 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1904 || h->root.type == bfd_link_hash_undefweak
1905 || h->root.type == bfd_link_hash_undefined)))
1907 Elf_Internal_Rela outrel;
1908 boolean skip, relocate;
1909 asection *sreloc;
1910 Elf32_External_Rela *loc;
1912 /* When generating a shared object, these relocations
1913 are copied into the output file to be resolved at run
1914 time. */
1916 skip = false;
1917 relocate = false;
1919 outrel.r_offset =
1920 _bfd_elf_section_offset (output_bfd, info, input_section,
1921 rel->r_offset);
1922 if (outrel.r_offset == (bfd_vma) -1)
1923 skip = true;
1924 else if (outrel.r_offset == (bfd_vma) -2)
1925 skip = true, relocate = true;
1926 outrel.r_offset += (input_section->output_section->vma
1927 + input_section->output_offset);
1929 if (skip)
1930 memset (&outrel, 0, sizeof outrel);
1931 else if (h != NULL
1932 && h->dynindx != -1
1933 && (r_type == R_390_PC16
1934 || r_type == R_390_PC16DBL
1935 || r_type == R_390_PC32DBL
1936 || r_type == R_390_PC32
1937 || !info->shared
1938 || !info->symbolic
1939 || (h->elf_link_hash_flags
1940 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1942 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1943 outrel.r_addend = rel->r_addend;
1945 else
1947 /* This symbol is local, or marked to become local. */
1948 relocate = true;
1949 outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
1950 outrel.r_addend = relocation + rel->r_addend;
1953 sreloc = elf_section_data (input_section)->sreloc;
1954 if (sreloc == NULL)
1955 abort ();
1957 loc = (Elf32_External_Rela *) sreloc->contents;
1958 loc += sreloc->reloc_count++;
1959 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1961 /* If this reloc is against an external symbol, we do
1962 not want to fiddle with the addend. Otherwise, we
1963 need to include the symbol value so that it becomes
1964 an addend for the dynamic reloc. */
1965 if (! relocate)
1966 continue;
1968 break;
1970 default:
1971 break;
1974 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1975 because such sections are not SEC_ALLOC and thus ld.so will
1976 not process them. */
1977 if (unresolved_reloc
1978 && !((input_section->flags & SEC_DEBUGGING) != 0
1979 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1980 (*_bfd_error_handler)
1981 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1982 bfd_archive_filename (input_bfd),
1983 bfd_get_section_name (input_bfd, input_section),
1984 (long) rel->r_offset,
1985 h->root.root.string);
1987 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1988 contents, rel->r_offset,
1989 relocation, rel->r_addend);
1991 if (r != bfd_reloc_ok)
1993 const char *name;
1995 if (h != NULL)
1996 name = h->root.root.string;
1997 else
1999 name = bfd_elf_string_from_elf_section (input_bfd,
2000 symtab_hdr->sh_link,
2001 sym->st_name);
2002 if (name == NULL)
2003 return false;
2004 if (*name == '\0')
2005 name = bfd_section_name (input_bfd, sec);
2008 if (r == bfd_reloc_overflow)
2011 if (! ((*info->callbacks->reloc_overflow)
2012 (info, name, howto->name, (bfd_vma) 0,
2013 input_bfd, input_section, rel->r_offset)))
2014 return false;
2016 else
2018 (*_bfd_error_handler)
2019 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
2020 bfd_archive_filename (input_bfd),
2021 bfd_get_section_name (input_bfd, input_section),
2022 (long) rel->r_offset, name, (int) r);
2023 return false;
2028 return true;
2031 /* Finish up dynamic symbol handling. We set the contents of various
2032 dynamic sections here. */
2034 static boolean
2035 elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym)
2036 bfd *output_bfd;
2037 struct bfd_link_info *info;
2038 struct elf_link_hash_entry *h;
2039 Elf_Internal_Sym *sym;
2041 struct elf_s390_link_hash_table *htab;
2043 htab = elf_s390_hash_table (info);
2045 if (h->plt.offset != (bfd_vma) -1)
2047 bfd_vma plt_index;
2048 bfd_vma got_offset;
2049 Elf_Internal_Rela rela;
2050 Elf32_External_Rela *loc;
2051 bfd_vma relative_offset;
2053 /* This symbol has an entry in the procedure linkage table. Set
2054 it up. */
2056 if (h->dynindx == -1
2057 || htab->splt == NULL
2058 || htab->sgotplt == NULL
2059 || htab->srelplt == NULL)
2060 abort ();
2062 /* Calc. index no.
2063 Current offset - size first entry / entry size. */
2064 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
2066 /* Offset in GOT is PLT index plus GOT headers(3) times 4,
2067 addr & GOT addr. */
2068 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2070 /* S390 uses halfwords for relative branch calc! */
2071 relative_offset = - ((PLT_FIRST_ENTRY_SIZE +
2072 (PLT_ENTRY_SIZE * plt_index) + 18) / 2);
2073 /* If offset is > 32768, branch to a previous branch
2074 390 can only handle +-64 K jumps. */
2075 if ( -32768 > (int) relative_offset )
2076 relative_offset =
2077 -(unsigned) (((65536 / PLT_ENTRY_SIZE - 1) * PLT_ENTRY_SIZE) / 2);
2079 /* Fill in the entry in the procedure linkage table. */
2080 if (!info->shared)
2082 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD0,
2083 htab->splt->contents + h->plt.offset);
2084 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD1,
2085 htab->splt->contents + h->plt.offset + 4);
2086 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
2087 htab->splt->contents + h->plt.offset + 8);
2088 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
2089 htab->splt->contents + h->plt.offset + 12);
2090 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD4,
2091 htab->splt->contents + h->plt.offset + 16);
2092 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2093 htab->splt->contents + h->plt.offset + 20);
2094 bfd_put_32 (output_bfd,
2095 (htab->sgotplt->output_section->vma
2096 + htab->sgotplt->output_offset
2097 + got_offset),
2098 htab->splt->contents + h->plt.offset + 24);
2100 else if (got_offset < 4096)
2102 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD0 + got_offset,
2103 htab->splt->contents + h->plt.offset);
2104 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD1,
2105 htab->splt->contents + h->plt.offset + 4);
2106 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD2,
2107 htab->splt->contents + h->plt.offset + 8);
2108 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD3,
2109 htab->splt->contents + h->plt.offset + 12);
2110 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD4,
2111 htab->splt->contents + h->plt.offset + 16);
2112 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2113 htab->splt->contents + h->plt.offset + 20);
2114 bfd_put_32 (output_bfd, (bfd_vma) 0,
2115 htab->splt->contents + h->plt.offset + 24);
2117 else if (got_offset < 32768)
2119 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD0 + got_offset,
2120 htab->splt->contents + h->plt.offset);
2121 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD1,
2122 htab->splt->contents + h->plt.offset + 4);
2123 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD2,
2124 htab->splt->contents + h->plt.offset + 8);
2125 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD3,
2126 htab->splt->contents + h->plt.offset + 12);
2127 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD4,
2128 htab->splt->contents + h->plt.offset + 16);
2129 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2130 htab->splt->contents + h->plt.offset + 20);
2131 bfd_put_32 (output_bfd, (bfd_vma) 0,
2132 htab->splt->contents + h->plt.offset + 24);
2134 else
2136 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD0,
2137 htab->splt->contents + h->plt.offset);
2138 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD1,
2139 htab->splt->contents + h->plt.offset + 4);
2140 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
2141 htab->splt->contents + h->plt.offset + 8);
2142 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
2143 htab->splt->contents + h->plt.offset + 12);
2144 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
2145 htab->splt->contents + h->plt.offset + 16);
2146 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2147 htab->splt->contents + h->plt.offset + 20);
2148 bfd_put_32 (output_bfd, got_offset,
2149 htab->splt->contents + h->plt.offset + 24);
2151 /* Insert offset into reloc. table here. */
2152 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
2153 htab->splt->contents + h->plt.offset + 28);
2155 /* Fill in the entry in the global offset table.
2156 Points to instruction after GOT offset. */
2157 bfd_put_32 (output_bfd,
2158 (htab->splt->output_section->vma
2159 + htab->splt->output_offset
2160 + h->plt.offset
2161 + 12),
2162 htab->sgotplt->contents + got_offset);
2164 /* Fill in the entry in the .rela.plt section. */
2165 rela.r_offset = (htab->sgotplt->output_section->vma
2166 + htab->sgotplt->output_offset
2167 + got_offset);
2168 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_JMP_SLOT);
2169 rela.r_addend = 0;
2170 loc = (Elf32_External_Rela *) htab->srelplt->contents + plt_index;
2171 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2173 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2175 /* Mark the symbol as undefined, rather than as defined in
2176 the .plt section. Leave the value alone. This is a clue
2177 for the dynamic linker, to make function pointer
2178 comparisons work between an application and shared
2179 library. */
2180 sym->st_shndx = SHN_UNDEF;
2184 if (h->got.offset != (bfd_vma) -1)
2186 Elf_Internal_Rela rela;
2187 Elf32_External_Rela *loc;
2189 /* This symbol has an entry in the global offset table. Set it
2190 up. */
2192 if (htab->sgot == NULL || htab->srelgot == NULL)
2193 abort ();
2195 rela.r_offset = (htab->sgot->output_section->vma
2196 + htab->sgot->output_offset
2197 + (h->got.offset &~ (bfd_vma) 1));
2199 /* If this is a static link, or it is a -Bsymbolic link and the
2200 symbol is defined locally or was forced to be local because
2201 of a version file, we just want to emit a RELATIVE reloc.
2202 The entry in the global offset table will already have been
2203 initialized in the relocate_section function. */
2204 if (info->shared
2205 && (info->symbolic
2206 || h->dynindx == -1
2207 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2208 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2210 BFD_ASSERT((h->got.offset & 1) != 0);
2211 rela.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2212 rela.r_addend = (h->root.u.def.value
2213 + h->root.u.def.section->output_section->vma
2214 + h->root.u.def.section->output_offset);
2216 else
2218 BFD_ASSERT((h->got.offset & 1) == 0);
2219 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgot->contents + h->got.offset);
2220 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_GLOB_DAT);
2221 rela.r_addend = 0;
2224 loc = (Elf32_External_Rela *) htab->srelgot->contents;
2225 loc += htab->srelgot->reloc_count++;
2226 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2229 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2231 Elf_Internal_Rela rela;
2232 Elf32_External_Rela *loc;
2234 /* This symbols needs a copy reloc. Set it up. */
2236 if (h->dynindx == -1
2237 || (h->root.type != bfd_link_hash_defined
2238 && h->root.type != bfd_link_hash_defweak)
2239 || htab->srelbss == NULL)
2240 abort ();
2242 rela.r_offset = (h->root.u.def.value
2243 + h->root.u.def.section->output_section->vma
2244 + h->root.u.def.section->output_offset);
2245 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_COPY);
2246 rela.r_addend = 0;
2247 loc = (Elf32_External_Rela *) htab->srelbss->contents;
2248 loc += htab->srelbss->reloc_count++;
2249 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2252 /* Mark some specially defined symbols as absolute. */
2253 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2254 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2255 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2256 sym->st_shndx = SHN_ABS;
2258 return true;
2261 /* Used to decide how to sort relocs in an optimal manner for the
2262 dynamic linker, before writing them out. */
2264 static enum elf_reloc_type_class
2265 elf_s390_reloc_type_class (rela)
2266 const Elf_Internal_Rela *rela;
2268 switch ((int) ELF32_R_TYPE (rela->r_info))
2270 case R_390_RELATIVE:
2271 return reloc_class_relative;
2272 case R_390_JMP_SLOT:
2273 return reloc_class_plt;
2274 case R_390_COPY:
2275 return reloc_class_copy;
2276 default:
2277 return reloc_class_normal;
2281 /* Finish up the dynamic sections. */
2283 static boolean
2284 elf_s390_finish_dynamic_sections (output_bfd, info)
2285 bfd *output_bfd;
2286 struct bfd_link_info *info;
2288 struct elf_s390_link_hash_table *htab;
2289 bfd *dynobj;
2290 asection *sdyn;
2292 htab = elf_s390_hash_table (info);
2293 dynobj = htab->elf.dynobj;
2294 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2296 if (htab->elf.dynamic_sections_created)
2298 Elf32_External_Dyn *dyncon, *dynconend;
2300 if (sdyn == NULL || htab->sgot == NULL)
2301 abort ();
2303 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2304 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2305 for (; dyncon < dynconend; dyncon++)
2307 Elf_Internal_Dyn dyn;
2308 asection *s;
2310 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2312 switch (dyn.d_tag)
2314 default:
2315 continue;
2317 case DT_PLTGOT:
2318 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
2319 break;
2321 case DT_JMPREL:
2322 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2323 break;
2325 case DT_PLTRELSZ:
2326 s = htab->srelplt->output_section;
2327 if (s->_cooked_size != 0)
2328 dyn.d_un.d_val = s->_cooked_size;
2329 else
2330 dyn.d_un.d_val = s->_raw_size;
2331 break;
2334 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2337 /* Fill in the special first entry in the procedure linkage table. */
2338 if (htab->splt && htab->splt->_raw_size > 0)
2340 memset (htab->splt->contents, 0, PLT_FIRST_ENTRY_SIZE);
2341 if (info->shared)
2343 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD0,
2344 htab->splt->contents );
2345 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD1,
2346 htab->splt->contents +4 );
2347 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD2,
2348 htab->splt->contents +8 );
2349 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD3,
2350 htab->splt->contents +12 );
2351 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD4,
2352 htab->splt->contents +16 );
2354 else
2356 bfd_put_32 (output_bfd, (bfd_vma)PLT_FIRST_ENTRY_WORD0,
2357 htab->splt->contents );
2358 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD1,
2359 htab->splt->contents +4 );
2360 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD2,
2361 htab->splt->contents +8 );
2362 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD3,
2363 htab->splt->contents +12 );
2364 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD4,
2365 htab->splt->contents +16 );
2366 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD5,
2367 htab->splt->contents +20 );
2368 bfd_put_32 (output_bfd,
2369 htab->sgotplt->output_section->vma
2370 + htab->sgotplt->output_offset,
2371 htab->splt->contents + 24);
2373 elf_section_data (htab->splt->output_section)
2374 ->this_hdr.sh_entsize = 4;
2379 if (htab->sgotplt)
2381 /* Fill in the first three entries in the global offset table. */
2382 if (htab->sgotplt->_raw_size > 0)
2384 bfd_put_32 (output_bfd,
2385 (sdyn == NULL ? (bfd_vma) 0
2386 : sdyn->output_section->vma + sdyn->output_offset),
2387 htab->sgotplt->contents);
2388 /* One entry for shared object struct ptr. */
2389 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 4);
2390 /* One entry for _dl_runtime_resolve. */
2391 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8);
2394 elf_section_data (htab->sgotplt->output_section)
2395 ->this_hdr.sh_entsize = 4;
2397 return true;
2400 static boolean
2401 elf_s390_object_p (abfd)
2402 bfd *abfd;
2404 return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_31);
2407 static boolean
2408 elf_s390_grok_prstatus (abfd, note)
2409 bfd * abfd;
2410 Elf_Internal_Note * note;
2412 int offset;
2413 unsigned int raw_size;
2415 switch (note->descsz)
2417 default:
2418 return false;
2420 case 224: /* S/390 Linux. */
2421 /* pr_cursig */
2422 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2424 /* pr_pid */
2425 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
2427 /* pr_reg */
2428 offset = 72;
2429 raw_size = 144;
2430 break;
2433 /* Make a ".reg/999" section. */
2434 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2435 raw_size, note->descpos + offset);
2438 #define TARGET_BIG_SYM bfd_elf32_s390_vec
2439 #define TARGET_BIG_NAME "elf32-s390"
2440 #define ELF_ARCH bfd_arch_s390
2441 #define ELF_MACHINE_CODE EM_S390
2442 #define ELF_MACHINE_ALT1 EM_S390_OLD
2443 #define ELF_MAXPAGESIZE 0x1000
2445 #define elf_backend_can_gc_sections 1
2446 #define elf_backend_can_refcount 1
2447 #define elf_backend_want_got_plt 1
2448 #define elf_backend_plt_readonly 1
2449 #define elf_backend_want_plt_sym 0
2450 #define elf_backend_got_header_size 12
2451 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2452 #define elf_backend_rela_normal 1
2454 #define elf_info_to_howto elf_s390_info_to_howto
2456 #define bfd_elf32_bfd_is_local_label_name elf_s390_is_local_label_name
2457 #define bfd_elf32_bfd_link_hash_table_create elf_s390_link_hash_table_create
2458 #define bfd_elf32_bfd_reloc_type_lookup elf_s390_reloc_type_lookup
2460 #define elf_backend_adjust_dynamic_symbol elf_s390_adjust_dynamic_symbol
2461 #define elf_backend_check_relocs elf_s390_check_relocs
2462 #define elf_backend_copy_indirect_symbol elf_s390_copy_indirect_symbol
2463 #define elf_backend_create_dynamic_sections elf_s390_create_dynamic_sections
2464 #define elf_backend_finish_dynamic_sections elf_s390_finish_dynamic_sections
2465 #define elf_backend_finish_dynamic_symbol elf_s390_finish_dynamic_symbol
2466 #define elf_backend_gc_mark_hook elf_s390_gc_mark_hook
2467 #define elf_backend_gc_sweep_hook elf_s390_gc_sweep_hook
2468 #define elf_backend_reloc_type_class elf_s390_reloc_type_class
2469 #define elf_backend_relocate_section elf_s390_relocate_section
2470 #define elf_backend_size_dynamic_sections elf_s390_size_dynamic_sections
2471 #define elf_backend_reloc_type_class elf_s390_reloc_type_class
2472 #define elf_backend_grok_prstatus elf_s390_grok_prstatus
2474 #define elf_backend_object_p elf_s390_object_p
2476 #include "elf32-target.h"