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