* bfd-in2.h: Regenerate.
[binutils-gdb.git] / bfd / elf32-s390.c
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.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27
28 static reloc_howto_type *elf_s390_reloc_type_lookup
29 PARAMS ((bfd *, bfd_reloc_code_real_type));
30 static void elf_s390_info_to_howto
31 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
32 static bfd_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 bfd_boolean create_got_section
39 PARAMS((bfd *, struct bfd_link_info *));
40 static bfd_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 bfd_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 bfd_boolean elf_s390_gc_sweep_hook
52 PARAMS ((bfd *, struct bfd_link_info *, asection *,
53 const Elf_Internal_Rela *));
54 struct elf_s390_link_hash_entry;
55 static void elf_s390_adjust_gotplt
56 PARAMS ((struct elf_s390_link_hash_entry *));
57 static bfd_boolean elf_s390_adjust_dynamic_symbol
58 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
59 static bfd_boolean allocate_dynrelocs
60 PARAMS ((struct elf_link_hash_entry *, PTR));
61 static bfd_boolean readonly_dynrelocs
62 PARAMS ((struct elf_link_hash_entry *, PTR));
63 static bfd_boolean elf_s390_size_dynamic_sections
64 PARAMS ((bfd *, struct bfd_link_info *));
65 static bfd_boolean elf_s390_relocate_section
66 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
67 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
68 static bfd_boolean elf_s390_finish_dynamic_symbol
69 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
70 Elf_Internal_Sym *));
71 static enum elf_reloc_type_class elf_s390_reloc_type_class
72 PARAMS ((const Elf_Internal_Rela *));
73 static bfd_boolean elf_s390_finish_dynamic_sections
74 PARAMS ((bfd *, struct bfd_link_info *));
75 static bfd_boolean elf_s390_object_p
76 PARAMS ((bfd *));
77 static bfd_boolean elf_s390_grok_prstatus
78 PARAMS ((bfd *, Elf_Internal_Note *));
79
80 #include "elf/s390.h"
81
82 /* The relocation "howto" table. */
83
84 static reloc_howto_type elf_howto_table[] =
85 {
86 HOWTO (R_390_NONE, /* type */
87 0, /* rightshift */
88 0, /* size (0 = byte, 1 = short, 2 = long) */
89 0, /* bitsize */
90 FALSE, /* pc_relative */
91 0, /* bitpos */
92 complain_overflow_dont, /* complain_on_overflow */
93 bfd_elf_generic_reloc, /* special_function */
94 "R_390_NONE", /* name */
95 FALSE, /* partial_inplace */
96 0, /* src_mask */
97 0, /* dst_mask */
98 FALSE), /* pcrel_offset */
99
100 HOWTO(R_390_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
101 bfd_elf_generic_reloc, "R_390_8", FALSE, 0,0x000000ff, FALSE),
102 HOWTO(R_390_12, 0, 1, 12, FALSE, 0, complain_overflow_dont,
103 bfd_elf_generic_reloc, "R_390_12", FALSE, 0,0x00000fff, FALSE),
104 HOWTO(R_390_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
105 bfd_elf_generic_reloc, "R_390_16", FALSE, 0,0x0000ffff, FALSE),
106 HOWTO(R_390_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_390_32", FALSE, 0,0xffffffff, FALSE),
108 HOWTO(R_390_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_390_PC32", FALSE, 0,0xffffffff, TRUE),
110 HOWTO(R_390_GOT12, 0, 1, 12, FALSE, 0, complain_overflow_dont,
111 bfd_elf_generic_reloc, "R_390_GOT12", FALSE, 0,0x00000fff, FALSE),
112 HOWTO(R_390_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
113 bfd_elf_generic_reloc, "R_390_GOT32", FALSE, 0,0xffffffff, FALSE),
114 HOWTO(R_390_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
115 bfd_elf_generic_reloc, "R_390_PLT32", FALSE, 0,0xffffffff, TRUE),
116 HOWTO(R_390_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
117 bfd_elf_generic_reloc, "R_390_COPY", FALSE, 0,0xffffffff, FALSE),
118 HOWTO(R_390_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
119 bfd_elf_generic_reloc, "R_390_GLOB_DAT", FALSE, 0,0xffffffff, FALSE),
120 HOWTO(R_390_JMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
121 bfd_elf_generic_reloc, "R_390_JMP_SLOT", FALSE, 0,0xffffffff, FALSE),
122 HOWTO(R_390_RELATIVE, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
123 bfd_elf_generic_reloc, "R_390_RELATIVE", FALSE, 0,0xffffffff, FALSE),
124 HOWTO(R_390_GOTOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
125 bfd_elf_generic_reloc, "R_390_GOTOFF32", FALSE, 0,0xffffffff, FALSE),
126 HOWTO(R_390_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
127 bfd_elf_generic_reloc, "R_390_GOTPC", FALSE, 0,0xffffffff, TRUE),
128 HOWTO(R_390_GOT16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
129 bfd_elf_generic_reloc, "R_390_GOT16", FALSE, 0,0x0000ffff, FALSE),
130 HOWTO(R_390_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
131 bfd_elf_generic_reloc, "R_390_PC16", FALSE, 0,0x0000ffff, TRUE),
132 HOWTO(R_390_PC16DBL, 1, 1, 16, TRUE, 0, complain_overflow_bitfield,
133 bfd_elf_generic_reloc, "R_390_PC16DBL", FALSE, 0,0x0000ffff, TRUE),
134 HOWTO(R_390_PLT16DBL, 1, 1, 16, TRUE, 0, complain_overflow_bitfield,
135 bfd_elf_generic_reloc, "R_390_PLT16DBL", FALSE, 0,0x0000ffff, TRUE),
136 HOWTO(R_390_PC32DBL, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
137 bfd_elf_generic_reloc, "R_390_PC32DBL", FALSE, 0,0xffffffff, TRUE),
138 HOWTO(R_390_PLT32DBL, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
139 bfd_elf_generic_reloc, "R_390_PLT32DBL", FALSE, 0,0xffffffff, TRUE),
140 HOWTO(R_390_GOTPCDBL, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
141 bfd_elf_generic_reloc, "R_390_GOTPCDBL", FALSE, 0,0xffffffff, TRUE),
142 EMPTY_HOWTO (R_390_64), /* Empty entry for R_390_64. */
143 EMPTY_HOWTO (R_390_PC64), /* Empty entry for R_390_PC64. */
144 EMPTY_HOWTO (R_390_GOT64), /* Empty entry for R_390_GOT64. */
145 EMPTY_HOWTO (R_390_PLT64), /* Empty entry for R_390_PLT64. */
146 HOWTO(R_390_GOTENT, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
147 bfd_elf_generic_reloc, "R_390_GOTENT", FALSE, 0,0xffffffff, TRUE),
148 HOWTO(R_390_GOTOFF16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
149 bfd_elf_generic_reloc, "R_390_GOTOFF16", FALSE, 0,0x0000ffff, FALSE),
150 EMPTY_HOWTO (R_390_GOTOFF64), /* Empty entry for R_390_GOTOFF64. */
151 HOWTO(R_390_GOTPLT12, 0, 1, 12, FALSE, 0, complain_overflow_dont,
152 bfd_elf_generic_reloc, "R_390_GOTPLT12", FALSE, 0,0x00000fff, FALSE),
153 HOWTO(R_390_GOTPLT16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
154 bfd_elf_generic_reloc, "R_390_GOTPLT16", FALSE, 0,0x0000ffff, FALSE),
155 HOWTO(R_390_GOTPLT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
156 bfd_elf_generic_reloc, "R_390_GOTPLT32", FALSE, 0,0xffffffff, FALSE),
157 EMPTY_HOWTO (R_390_GOTPLT64), /* Empty entry for R_390_GOTPLT64. */
158 HOWTO(R_390_GOTPLTENT, 1, 2, 32, TRUE, 0, complain_overflow_bitfield,
159 bfd_elf_generic_reloc, "R_390_GOTPLTENT",FALSE, 0,0xffffffff, TRUE),
160 HOWTO(R_390_PLTOFF16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
161 bfd_elf_generic_reloc, "R_390_PLTOFF16", FALSE, 0,0x0000ffff, FALSE),
162 HOWTO(R_390_PLTOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
163 bfd_elf_generic_reloc, "R_390_PLTOFF32", FALSE, 0,0xffffffff, FALSE),
164 EMPTY_HOWTO (R_390_PLTOFF64), /* Empty entry for R_390_PLTOFF64. */
165 };
166
167 /* GNU extension to record C++ vtable hierarchy. */
168 static reloc_howto_type elf32_s390_vtinherit_howto =
169 HOWTO (R_390_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", FALSE,0, 0, FALSE);
170 static reloc_howto_type elf32_s390_vtentry_howto =
171 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);
172
173 static reloc_howto_type *
174 elf_s390_reloc_type_lookup (abfd, code)
175 bfd *abfd ATTRIBUTE_UNUSED;
176 bfd_reloc_code_real_type code;
177 {
178 switch (code)
179 {
180 case BFD_RELOC_NONE:
181 return &elf_howto_table[(int) R_390_NONE];
182 case BFD_RELOC_8:
183 return &elf_howto_table[(int) R_390_8];
184 case BFD_RELOC_390_12:
185 return &elf_howto_table[(int) R_390_12];
186 case BFD_RELOC_16:
187 return &elf_howto_table[(int) R_390_16];
188 case BFD_RELOC_32:
189 return &elf_howto_table[(int) R_390_32];
190 case BFD_RELOC_CTOR:
191 return &elf_howto_table[(int) R_390_32];
192 case BFD_RELOC_32_PCREL:
193 return &elf_howto_table[(int) R_390_PC32];
194 case BFD_RELOC_390_GOT12:
195 return &elf_howto_table[(int) R_390_GOT12];
196 case BFD_RELOC_32_GOT_PCREL:
197 return &elf_howto_table[(int) R_390_GOT32];
198 case BFD_RELOC_390_PLT32:
199 return &elf_howto_table[(int) R_390_PLT32];
200 case BFD_RELOC_390_COPY:
201 return &elf_howto_table[(int) R_390_COPY];
202 case BFD_RELOC_390_GLOB_DAT:
203 return &elf_howto_table[(int) R_390_GLOB_DAT];
204 case BFD_RELOC_390_JMP_SLOT:
205 return &elf_howto_table[(int) R_390_JMP_SLOT];
206 case BFD_RELOC_390_RELATIVE:
207 return &elf_howto_table[(int) R_390_RELATIVE];
208 case BFD_RELOC_32_GOTOFF:
209 return &elf_howto_table[(int) R_390_GOTOFF32];
210 case BFD_RELOC_390_GOTPC:
211 return &elf_howto_table[(int) R_390_GOTPC];
212 case BFD_RELOC_390_GOT16:
213 return &elf_howto_table[(int) R_390_GOT16];
214 case BFD_RELOC_16_PCREL:
215 return &elf_howto_table[(int) R_390_PC16];
216 case BFD_RELOC_390_PC16DBL:
217 return &elf_howto_table[(int) R_390_PC16DBL];
218 case BFD_RELOC_390_PLT16DBL:
219 return &elf_howto_table[(int) R_390_PLT16DBL];
220 case BFD_RELOC_390_PC32DBL:
221 return &elf_howto_table[(int) R_390_PC32DBL];
222 case BFD_RELOC_390_PLT32DBL:
223 return &elf_howto_table[(int) R_390_PLT32DBL];
224 case BFD_RELOC_390_GOTPCDBL:
225 return &elf_howto_table[(int) R_390_GOTPCDBL];
226 case BFD_RELOC_390_GOTENT:
227 return &elf_howto_table[(int) R_390_GOTENT];
228 case BFD_RELOC_16_GOTOFF:
229 return &elf_howto_table[(int) R_390_GOTOFF16];
230 case BFD_RELOC_390_GOTPLT12:
231 return &elf_howto_table[(int) R_390_GOTPLT12];
232 case BFD_RELOC_390_GOTPLT16:
233 return &elf_howto_table[(int) R_390_GOTPLT16];
234 case BFD_RELOC_390_GOTPLT32:
235 return &elf_howto_table[(int) R_390_GOTPLT32];
236 case BFD_RELOC_390_GOTPLTENT:
237 return &elf_howto_table[(int) R_390_GOTPLTENT];
238 case BFD_RELOC_390_PLTOFF16:
239 return &elf_howto_table[(int) R_390_PLTOFF16];
240 case BFD_RELOC_390_PLTOFF32:
241 return &elf_howto_table[(int) R_390_PLTOFF32];
242 case BFD_RELOC_VTABLE_INHERIT:
243 return &elf32_s390_vtinherit_howto;
244 case BFD_RELOC_VTABLE_ENTRY:
245 return &elf32_s390_vtentry_howto;
246 default:
247 break;
248 }
249 return 0;
250 }
251
252 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
253 and elf32-s390.c has its own copy. */
254
255 static void
256 elf_s390_info_to_howto (abfd, cache_ptr, dst)
257 bfd *abfd ATTRIBUTE_UNUSED;
258 arelent *cache_ptr;
259 Elf_Internal_Rela *dst;
260 {
261 switch (ELF32_R_TYPE(dst->r_info))
262 {
263 case R_390_GNU_VTINHERIT:
264 cache_ptr->howto = &elf32_s390_vtinherit_howto;
265 break;
266
267 case R_390_GNU_VTENTRY:
268 cache_ptr->howto = &elf32_s390_vtentry_howto;
269 break;
270
271 default:
272 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_390_max);
273 cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
274 }
275 }
276
277 static bfd_boolean
278 elf_s390_is_local_label_name (abfd, name)
279 bfd *abfd;
280 const char *name;
281 {
282 if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L'))
283 return TRUE;
284
285 return _bfd_elf_is_local_label_name (abfd, name);
286 }
287
288 /* Functions for the 390 ELF linker. */
289
290 /* The name of the dynamic interpreter. This is put in the .interp
291 section. */
292
293 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
294
295 /* The size in bytes of the first entry in the procedure linkage table. */
296 #define PLT_FIRST_ENTRY_SIZE 32
297 /* The size in bytes of an entry in the procedure linkage table. */
298 #define PLT_ENTRY_SIZE 32
299
300 #define GOT_ENTRY_SIZE 4
301
302 /* The first three entries in a procedure linkage table are reserved,
303 and the initial contents are unimportant (we zero them out).
304 Subsequent entries look like this. See the SVR4 ABI 386
305 supplement to see how this works. */
306
307 /* For the s390, simple addr offset can only be 0 - 4096.
308 To use the full 2 GB address space, several instructions
309 are needed to load an address in a register and execute
310 a branch( or just saving the address)
311
312 Furthermore, only r 0 and 1 are free to use!!! */
313
314 /* The first 3 words in the GOT are then reserved.
315 Word 0 is the address of the dynamic table.
316 Word 1 is a pointer to a structure describing the object
317 Word 2 is used to point to the loader entry address.
318
319 The code for position independand PLT entries looks like this:
320
321 r12 holds addr of the current GOT at entry to the PLT
322
323 The GOT holds the address in the PLT to be executed.
324 The loader then gets:
325 24(15) = Pointer to the structure describing the object.
326 28(15) = Offset in symbol table
327
328 The loader must then find the module where the function is
329 and insert the address in the GOT.
330
331 Note: 390 can only address +- 64 K relative.
332 We check if offset > 65536, then make a relative branch -64xxx
333 back to a previous defined branch
334
335 PLT1: BASR 1,0 # 2 bytes
336 L 1,22(1) # 4 bytes Load offset in GOT in r 1
337 L 1,(1,12) # 4 bytes Load address from GOT in r1
338 BCR 15,1 # 2 bytes Jump to address
339 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
340 L 1,14(1) # 4 bytes Load offset in symol table in r1
341 BRC 15,-x # 4 bytes Jump to start of PLT
342 .word 0 # 2 bytes filler
343 .long ? # 4 bytes offset in GOT
344 .long ? # 4 bytes offset into symbol table
345
346 This was the general case. There are two additional, optimizes PLT
347 definitions. One for GOT offsets < 4096 and one for GOT offsets < 32768.
348 First the one for GOT offsets < 4096:
349
350 PLT1: L 1,<offset>(12) # 4 bytes Load address from GOT in R1
351 BCR 15,1 # 2 bytes Jump to address
352 .word 0,0,0 # 6 bytes filler
353 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
354 L 1,14(1) # 4 bytes Load offset in symbol table in r1
355 BRC 15,-x # 4 bytes Jump to start of PLT
356 .word 0,0,0 # 6 bytes filler
357 .long ? # 4 bytes offset into symbol table
358
359 Second the one for GOT offsets < 32768:
360
361 PLT1: LHI 1,<offset> # 4 bytes Load offset in GOT to r1
362 L 1,(1,12) # 4 bytes Load address from GOT to r1
363 BCR 15,1 # 2 bytes Jump to address
364 .word 0 # 2 bytes filler
365 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
366 L 1,14(1) # 4 bytes Load offset in symbol table in r1
367 BRC 15,-x # 4 bytes Jump to start of PLT
368 .word 0,0,0 # 6 bytes filler
369 .long ? # 4 bytes offset into symbol table
370
371 Total = 32 bytes per PLT entry
372
373 The code for static build PLT entries looks like this:
374
375 PLT1: BASR 1,0 # 2 bytes
376 L 1,22(1) # 4 bytes Load address of GOT entry
377 L 1,0(0,1) # 4 bytes Load address from GOT in r1
378 BCR 15,1 # 2 bytes Jump to address
379 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time
380 L 1,14(1) # 4 bytes Load offset in symbol table in r1
381 BRC 15,-x # 4 bytes Jump to start of PLT
382 .word 0 # 2 bytes filler
383 .long ? # 4 bytes address of GOT entry
384 .long ? # 4 bytes offset into symbol table */
385
386 #define PLT_PIC_ENTRY_WORD0 0x0d105810
387 #define PLT_PIC_ENTRY_WORD1 0x10165811
388 #define PLT_PIC_ENTRY_WORD2 0xc00007f1
389 #define PLT_PIC_ENTRY_WORD3 0x0d105810
390 #define PLT_PIC_ENTRY_WORD4 0x100ea7f4
391
392 #define PLT_PIC12_ENTRY_WORD0 0x5810c000
393 #define PLT_PIC12_ENTRY_WORD1 0x07f10000
394 #define PLT_PIC12_ENTRY_WORD2 0x00000000
395 #define PLT_PIC12_ENTRY_WORD3 0x0d105810
396 #define PLT_PIC12_ENTRY_WORD4 0x100ea7f4
397
398 #define PLT_PIC16_ENTRY_WORD0 0xa7180000
399 #define PLT_PIC16_ENTRY_WORD1 0x5811c000
400 #define PLT_PIC16_ENTRY_WORD2 0x07f10000
401 #define PLT_PIC16_ENTRY_WORD3 0x0d105810
402 #define PLT_PIC16_ENTRY_WORD4 0x100ea7f4
403
404 #define PLT_ENTRY_WORD0 0x0d105810
405 #define PLT_ENTRY_WORD1 0x10165810
406 #define PLT_ENTRY_WORD2 0x100007f1
407 #define PLT_ENTRY_WORD3 0x0d105810
408 #define PLT_ENTRY_WORD4 0x100ea7f4
409
410 /* The first PLT entry pushes the offset into the symbol table
411 from R1 onto the stack at 8(15) and the loader object info
412 at 12(15), loads the loader address in R1 and jumps to it. */
413
414 /* The first entry in the PLT for PIC code:
415
416 PLT0:
417 ST 1,28(15) # R1 has offset into symbol table
418 L 1,4(12) # Get loader ino(object struct address)
419 ST 1,24(15) # Store address
420 L 1,8(12) # Entry address of loader in R1
421 BR 1 # Jump to loader
422
423 The first entry in the PLT for static code:
424
425 PLT0:
426 ST 1,28(15) # R1 has offset into symbol table
427 BASR 1,0
428 L 1,18(0,1) # Get address of GOT
429 MVC 24(4,15),4(1) # Move loader ino to stack
430 L 1,8(1) # Get address of loader
431 BR 1 # Jump to loader
432 .word 0 # filler
433 .long got # address of GOT */
434
435 #define PLT_PIC_FIRST_ENTRY_WORD0 0x5010f01c
436 #define PLT_PIC_FIRST_ENTRY_WORD1 0x5810c004
437 #define PLT_PIC_FIRST_ENTRY_WORD2 0x5010f018
438 #define PLT_PIC_FIRST_ENTRY_WORD3 0x5810c008
439 #define PLT_PIC_FIRST_ENTRY_WORD4 0x07f10000
440
441 #define PLT_FIRST_ENTRY_WORD0 0x5010f01c
442 #define PLT_FIRST_ENTRY_WORD1 0x0d105810
443 #define PLT_FIRST_ENTRY_WORD2 0x1012D203
444 #define PLT_FIRST_ENTRY_WORD3 0xf0181004
445 #define PLT_FIRST_ENTRY_WORD4 0x58101008
446 #define PLT_FIRST_ENTRY_WORD5 0x07f10000
447
448 /* The s390 linker needs to keep track of the number of relocs that it
449 decides to copy as dynamic relocs in check_relocs for each symbol.
450 This is so that it can later discard them if they are found to be
451 unnecessary. We store the information in a field extending the
452 regular ELF linker hash table. */
453
454 struct elf_s390_dyn_relocs
455 {
456 struct elf_s390_dyn_relocs *next;
457
458 /* The input section of the reloc. */
459 asection *sec;
460
461 /* Total number of relocs copied for the input section. */
462 bfd_size_type count;
463
464 /* Number of pc-relative relocs copied for the input section. */
465 bfd_size_type pc_count;
466 };
467
468 /* s390 ELF linker hash entry. */
469
470 struct elf_s390_link_hash_entry
471 {
472 struct elf_link_hash_entry elf;
473
474 /* Track dynamic relocs copied for this symbol. */
475 struct elf_s390_dyn_relocs *dyn_relocs;
476
477 /* Number of GOTPLT references for a function. */
478 bfd_signed_vma gotplt_refcount;
479 };
480
481 /* s390 ELF linker hash table. */
482
483 struct elf_s390_link_hash_table
484 {
485 struct elf_link_hash_table elf;
486
487 /* Short-cuts to get to dynamic linker sections. */
488 asection *sgot;
489 asection *sgotplt;
490 asection *srelgot;
491 asection *splt;
492 asection *srelplt;
493 asection *sdynbss;
494 asection *srelbss;
495
496 /* Small local sym to section mapping cache. */
497 struct sym_sec_cache sym_sec;
498 };
499
500 /* Get the s390 ELF linker hash table from a link_info structure. */
501
502 #define elf_s390_hash_table(p) \
503 ((struct elf_s390_link_hash_table *) ((p)->hash))
504
505 /* Create an entry in an s390 ELF linker hash table. */
506
507 static struct bfd_hash_entry *
508 link_hash_newfunc (entry, table, string)
509 struct bfd_hash_entry *entry;
510 struct bfd_hash_table *table;
511 const char *string;
512 {
513 /* Allocate the structure if it has not already been allocated by a
514 subclass. */
515 if (entry == NULL)
516 {
517 entry = bfd_hash_allocate (table,
518 sizeof (struct elf_s390_link_hash_entry));
519 if (entry == NULL)
520 return entry;
521 }
522
523 /* Call the allocation method of the superclass. */
524 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
525 if (entry != NULL)
526 {
527 struct elf_s390_link_hash_entry *eh;
528
529 eh = (struct elf_s390_link_hash_entry *) entry;
530 eh->dyn_relocs = NULL;
531 eh->gotplt_refcount = 0;
532 }
533
534 return entry;
535 }
536
537 /* Create an s390 ELF linker hash table. */
538
539 static struct bfd_link_hash_table *
540 elf_s390_link_hash_table_create (abfd)
541 bfd *abfd;
542 {
543 struct elf_s390_link_hash_table *ret;
544 bfd_size_type amt = sizeof (struct elf_s390_link_hash_table);
545
546 ret = (struct elf_s390_link_hash_table *) bfd_malloc (amt);
547 if (ret == NULL)
548 return NULL;
549
550 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
551 {
552 free (ret);
553 return NULL;
554 }
555
556 ret->sgot = NULL;
557 ret->sgotplt = NULL;
558 ret->srelgot = NULL;
559 ret->splt = NULL;
560 ret->srelplt = NULL;
561 ret->sdynbss = NULL;
562 ret->srelbss = NULL;
563 ret->sym_sec.abfd = NULL;
564
565 return &ret->elf.root;
566 }
567
568 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
569 shortcuts to them in our hash table. */
570
571 static bfd_boolean
572 create_got_section (dynobj, info)
573 bfd *dynobj;
574 struct bfd_link_info *info;
575 {
576 struct elf_s390_link_hash_table *htab;
577
578 if (! _bfd_elf_create_got_section (dynobj, info))
579 return FALSE;
580
581 htab = elf_s390_hash_table (info);
582 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
583 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
584 if (!htab->sgot || !htab->sgotplt)
585 abort ();
586
587 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
588 if (htab->srelgot == NULL
589 || ! bfd_set_section_flags (dynobj, htab->srelgot,
590 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
591 | SEC_IN_MEMORY | SEC_LINKER_CREATED
592 | SEC_READONLY))
593 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
594 return FALSE;
595 return TRUE;
596 }
597
598 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
599 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
600 hash table. */
601
602 static bfd_boolean
603 elf_s390_create_dynamic_sections (dynobj, info)
604 bfd *dynobj;
605 struct bfd_link_info *info;
606 {
607 struct elf_s390_link_hash_table *htab;
608
609 htab = elf_s390_hash_table (info);
610 if (!htab->sgot && !create_got_section (dynobj, info))
611 return FALSE;
612
613 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
614 return FALSE;
615
616 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
617 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
618 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
619 if (!info->shared)
620 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
621
622 if (!htab->splt || !htab->srelplt || !htab->sdynbss
623 || (!info->shared && !htab->srelbss))
624 abort ();
625
626 return TRUE;
627 }
628
629 /* Copy the extra info we tack onto an elf_link_hash_entry. */
630
631 static void
632 elf_s390_copy_indirect_symbol (bed, dir, ind)
633 struct elf_backend_data *bed;
634 struct elf_link_hash_entry *dir, *ind;
635 {
636 struct elf_s390_link_hash_entry *edir, *eind;
637
638 edir = (struct elf_s390_link_hash_entry *) dir;
639 eind = (struct elf_s390_link_hash_entry *) ind;
640
641 if (eind->dyn_relocs != NULL)
642 {
643 if (edir->dyn_relocs != NULL)
644 {
645 struct elf_s390_dyn_relocs **pp;
646 struct elf_s390_dyn_relocs *p;
647
648 if (ind->root.type == bfd_link_hash_indirect)
649 abort ();
650
651 /* Add reloc counts against the weak sym to the strong sym
652 list. Merge any entries against the same section. */
653 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
654 {
655 struct elf_s390_dyn_relocs *q;
656
657 for (q = edir->dyn_relocs; q != NULL; q = q->next)
658 if (q->sec == p->sec)
659 {
660 q->pc_count += p->pc_count;
661 q->count += p->count;
662 *pp = p->next;
663 break;
664 }
665 if (q == NULL)
666 pp = &p->next;
667 }
668 *pp = edir->dyn_relocs;
669 }
670
671 edir->dyn_relocs = eind->dyn_relocs;
672 eind->dyn_relocs = NULL;
673 }
674
675 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
676 }
677
678 /* Look through the relocs for a section during the first phase, and
679 allocate space in the global offset table or procedure linkage
680 table. */
681
682 static bfd_boolean
683 elf_s390_check_relocs (abfd, info, sec, relocs)
684 bfd *abfd;
685 struct bfd_link_info *info;
686 asection *sec;
687 const Elf_Internal_Rela *relocs;
688 {
689 struct elf_s390_link_hash_table *htab;
690 Elf_Internal_Shdr *symtab_hdr;
691 struct elf_link_hash_entry **sym_hashes;
692 const Elf_Internal_Rela *rel;
693 const Elf_Internal_Rela *rel_end;
694 asection *sreloc;
695 bfd_signed_vma *local_got_refcounts;
696
697 if (info->relocateable)
698 return TRUE;
699
700 htab = elf_s390_hash_table (info);
701 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
702 sym_hashes = elf_sym_hashes (abfd);
703 local_got_refcounts = elf_local_got_refcounts (abfd);
704
705 sreloc = NULL;
706
707 rel_end = relocs + sec->reloc_count;
708 for (rel = relocs; rel < rel_end; rel++)
709 {
710 unsigned long r_symndx;
711 struct elf_link_hash_entry *h;
712
713 r_symndx = ELF32_R_SYM (rel->r_info);
714
715 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
716 {
717 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
718 bfd_archive_filename (abfd),
719 r_symndx);
720 return FALSE;
721 }
722
723 if (r_symndx < symtab_hdr->sh_info)
724 h = NULL;
725 else
726 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
727
728 /* Create got section and local_got_refcounts array if they
729 are needed. */
730 switch (ELF32_R_TYPE (rel->r_info))
731 {
732 case R_390_GOT12:
733 case R_390_GOT16:
734 case R_390_GOT32:
735 case R_390_GOTENT:
736 case R_390_GOTPLT12:
737 case R_390_GOTPLT16:
738 case R_390_GOTPLT32:
739 case R_390_GOTPLTENT:
740 if (h == NULL
741 && local_got_refcounts == NULL)
742 {
743 bfd_size_type size;
744
745 size = symtab_hdr->sh_info;
746 size *= sizeof (bfd_signed_vma);
747 local_got_refcounts = ((bfd_signed_vma *)
748 bfd_zalloc (abfd, size));
749 if (local_got_refcounts == NULL)
750 return FALSE;
751 elf_local_got_refcounts (abfd) = local_got_refcounts;
752 }
753 /* Fall through. */
754 case R_390_GOTOFF16:
755 case R_390_GOTOFF32:
756 case R_390_GOTPC:
757 case R_390_GOTPCDBL:
758 if (htab->sgot == NULL)
759 {
760 if (htab->elf.dynobj == NULL)
761 htab->elf.dynobj = abfd;
762 if (!create_got_section (htab->elf.dynobj, info))
763 return FALSE;
764 }
765 }
766
767 switch (ELF32_R_TYPE (rel->r_info))
768 {
769 case R_390_GOT12:
770 case R_390_GOT16:
771 case R_390_GOT32:
772 case R_390_GOTENT:
773 /* This symbol requires a global offset table entry. */
774 if (h != NULL)
775 h->got.refcount += 1;
776 else
777 local_got_refcounts[r_symndx] += 1;
778 break;
779
780 case R_390_GOTOFF16:
781 case R_390_GOTOFF32:
782 case R_390_GOTPC:
783 case R_390_GOTPCDBL:
784 /* Got is created, nothing to be done. */
785 break;
786
787 case R_390_PLT16DBL:
788 case R_390_PLT32DBL:
789 case R_390_PLT32:
790 case R_390_PLTOFF16:
791 case R_390_PLTOFF32:
792 /* This symbol requires a procedure linkage table entry. We
793 actually build the entry in adjust_dynamic_symbol,
794 because this might be a case of linking PIC code which is
795 never referenced by a dynamic object, in which case we
796 don't need to generate a procedure linkage table entry
797 after all. */
798
799 /* If this is a local symbol, we resolve it directly without
800 creating a procedure linkage table entry. */
801 if (h != NULL)
802 {
803 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
804 h->plt.refcount += 1;
805 }
806 break;
807
808 case R_390_GOTPLT12:
809 case R_390_GOTPLT16:
810 case R_390_GOTPLT32:
811 case R_390_GOTPLTENT:
812 /* This symbol requires either a procedure linkage table entry
813 or an entry in the local got. We actually build the entry
814 in adjust_dynamic_symbol because whether this is really a
815 global reference can change and with it the fact if we have
816 to create a plt entry or a local got entry. To be able to
817 make a once global symbol a local one we have to keep track
818 of the number of gotplt references that exist for this
819 symbol. */
820 if (h != NULL)
821 {
822 ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount++;
823 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
824 h->plt.refcount += 1;
825 }
826 else
827 local_got_refcounts[r_symndx] += 1;
828 break;
829
830 case R_390_8:
831 case R_390_16:
832 case R_390_32:
833 case R_390_PC16:
834 case R_390_PC16DBL:
835 case R_390_PC32DBL:
836 case R_390_PC32:
837 if (h != NULL && !info->shared)
838 {
839 /* If this reloc is in a read-only section, we might
840 need a copy reloc. We can't check reliably at this
841 stage whether the section is read-only, as input
842 sections have not yet been mapped to output sections.
843 Tentatively set the flag for now, and correct in
844 adjust_dynamic_symbol. */
845 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
846
847 /* We may need a .plt entry if the function this reloc
848 refers to is in a shared lib. */
849 h->plt.refcount += 1;
850 }
851
852 /* If we are creating a shared library, and this is a reloc
853 against a global symbol, or a non PC relative reloc
854 against a local symbol, then we need to copy the reloc
855 into the shared library. However, if we are linking with
856 -Bsymbolic, we do not need to copy a reloc against a
857 global symbol which is defined in an object we are
858 including in the link (i.e., DEF_REGULAR is set). At
859 this point we have not seen all the input files, so it is
860 possible that DEF_REGULAR is not set now but will be set
861 later (it is never cleared). In case of a weak definition,
862 DEF_REGULAR may be cleared later by a strong definition in
863 a shared library. We account for that possibility below by
864 storing information in the relocs_copied field of the hash
865 table entry. A similar situation occurs when creating
866 shared libraries and symbol visibility changes render the
867 symbol local.
868
869 If on the other hand, we are creating an executable, we
870 may need to keep relocations for symbols satisfied by a
871 dynamic library if we manage to avoid copy relocs for the
872 symbol. */
873 if ((info->shared
874 && (sec->flags & SEC_ALLOC) != 0
875 && ((ELF32_R_TYPE (rel->r_info) != R_390_PC16
876 && ELF32_R_TYPE (rel->r_info) != R_390_PC16DBL
877 && ELF32_R_TYPE (rel->r_info) != R_390_PC32DBL
878 && ELF32_R_TYPE (rel->r_info) != R_390_PC32)
879 || (h != NULL
880 && (! info->symbolic
881 || h->root.type == bfd_link_hash_defweak
882 || (h->elf_link_hash_flags
883 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
884 || (!info->shared
885 && (sec->flags & SEC_ALLOC) != 0
886 && h != NULL
887 && (h->root.type == bfd_link_hash_defweak
888 || (h->elf_link_hash_flags
889 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
890 {
891 struct elf_s390_dyn_relocs *p;
892 struct elf_s390_dyn_relocs **head;
893
894 /* We must copy these reloc types into the output file.
895 Create a reloc section in dynobj and make room for
896 this reloc. */
897 if (sreloc == NULL)
898 {
899 const char *name;
900 bfd *dynobj;
901
902 name = (bfd_elf_string_from_elf_section
903 (abfd,
904 elf_elfheader (abfd)->e_shstrndx,
905 elf_section_data (sec)->rel_hdr.sh_name));
906 if (name == NULL)
907 return FALSE;
908
909 if (strncmp (name, ".rela", 5) != 0
910 || strcmp (bfd_get_section_name (abfd, sec),
911 name + 5) != 0)
912 {
913 (*_bfd_error_handler)
914 (_("%s: bad relocation section name `%s\'"),
915 bfd_archive_filename (abfd), name);
916 }
917
918 if (htab->elf.dynobj == NULL)
919 htab->elf.dynobj = abfd;
920
921 dynobj = htab->elf.dynobj;
922 sreloc = bfd_get_section_by_name (dynobj, name);
923 if (sreloc == NULL)
924 {
925 flagword flags;
926
927 sreloc = bfd_make_section (dynobj, name);
928 flags = (SEC_HAS_CONTENTS | SEC_READONLY
929 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
930 if ((sec->flags & SEC_ALLOC) != 0)
931 flags |= SEC_ALLOC | SEC_LOAD;
932 if (sreloc == NULL
933 || ! bfd_set_section_flags (dynobj, sreloc, flags)
934 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
935 return FALSE;
936 }
937 elf_section_data (sec)->sreloc = sreloc;
938 }
939
940 /* If this is a global symbol, we count the number of
941 relocations we need for this symbol. */
942 if (h != NULL)
943 {
944 head = &((struct elf_s390_link_hash_entry *) h)->dyn_relocs;
945 }
946 else
947 {
948 /* Track dynamic relocs needed for local syms too.
949 We really need local syms available to do this
950 easily. Oh well. */
951
952 asection *s;
953 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
954 sec, r_symndx);
955 if (s == NULL)
956 return FALSE;
957
958 head = ((struct elf_s390_dyn_relocs **)
959 &elf_section_data (s)->local_dynrel);
960 }
961
962 p = *head;
963 if (p == NULL || p->sec != sec)
964 {
965 bfd_size_type amt = sizeof *p;
966 p = ((struct elf_s390_dyn_relocs *)
967 bfd_alloc (htab->elf.dynobj, amt));
968 if (p == NULL)
969 return FALSE;
970 p->next = *head;
971 *head = p;
972 p->sec = sec;
973 p->count = 0;
974 p->pc_count = 0;
975 }
976
977 p->count += 1;
978 if (ELF32_R_TYPE (rel->r_info) == R_390_PC16
979 || ELF32_R_TYPE (rel->r_info) == R_390_PC16DBL
980 || ELF32_R_TYPE (rel->r_info) == R_390_PC32DBL
981 || ELF32_R_TYPE (rel->r_info) == R_390_PC32)
982 p->pc_count += 1;
983 }
984 break;
985
986 /* This relocation describes the C++ object vtable hierarchy.
987 Reconstruct it for later use during GC. */
988 case R_390_GNU_VTINHERIT:
989 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
990 return FALSE;
991 break;
992
993 /* This relocation describes which C++ vtable entries are actually
994 used. Record for later use during GC. */
995 case R_390_GNU_VTENTRY:
996 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
997 return FALSE;
998 break;
999
1000 default:
1001 break;
1002 }
1003 }
1004
1005 return TRUE;
1006 }
1007
1008 /* Return the section that should be marked against GC for a given
1009 relocation. */
1010
1011 static asection *
1012 elf_s390_gc_mark_hook (sec, info, rel, h, sym)
1013 asection *sec;
1014 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1015 Elf_Internal_Rela *rel;
1016 struct elf_link_hash_entry *h;
1017 Elf_Internal_Sym *sym;
1018 {
1019 if (h != NULL)
1020 {
1021 switch (ELF32_R_TYPE (rel->r_info))
1022 {
1023 case R_390_GNU_VTINHERIT:
1024 case R_390_GNU_VTENTRY:
1025 break;
1026
1027 default:
1028 switch (h->root.type)
1029 {
1030 case bfd_link_hash_defined:
1031 case bfd_link_hash_defweak:
1032 return h->root.u.def.section;
1033
1034 case bfd_link_hash_common:
1035 return h->root.u.c.p->section;
1036
1037 default:
1038 break;
1039 }
1040 }
1041 }
1042 else
1043 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1044
1045 return NULL;
1046 }
1047
1048 /* Update the got entry reference counts for the section being removed. */
1049
1050 static bfd_boolean
1051 elf_s390_gc_sweep_hook (abfd, info, sec, relocs)
1052 bfd *abfd;
1053 struct bfd_link_info *info;
1054 asection *sec;
1055 const Elf_Internal_Rela *relocs;
1056 {
1057 Elf_Internal_Shdr *symtab_hdr;
1058 struct elf_link_hash_entry **sym_hashes;
1059 bfd_signed_vma *local_got_refcounts;
1060 const Elf_Internal_Rela *rel, *relend;
1061 unsigned long r_symndx;
1062 struct elf_link_hash_entry *h;
1063
1064 elf_section_data (sec)->local_dynrel = NULL;
1065
1066 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1067 sym_hashes = elf_sym_hashes (abfd);
1068 local_got_refcounts = elf_local_got_refcounts (abfd);
1069
1070 relend = relocs + sec->reloc_count;
1071 for (rel = relocs; rel < relend; rel++)
1072 {
1073 r_symndx = ELF32_R_SYM (rel->r_info);
1074
1075 if (r_symndx < symtab_hdr->sh_info)
1076 h = NULL;
1077 else
1078 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1079
1080 switch (ELF32_R_TYPE (rel->r_info))
1081 {
1082 case R_390_GOT12:
1083 case R_390_GOT16:
1084 case R_390_GOT32:
1085 case R_390_GOTOFF16:
1086 case R_390_GOTOFF32:
1087 case R_390_GOTPC:
1088 case R_390_GOTPCDBL:
1089 case R_390_GOTENT:
1090 if (h != NULL)
1091 {
1092 if (h->got.refcount > 0)
1093 h->got.refcount -= 1;
1094 }
1095 else if (local_got_refcounts != NULL)
1096 {
1097 if (local_got_refcounts[r_symndx] > 0)
1098 local_got_refcounts[r_symndx] -= 1;
1099 }
1100 break;
1101
1102 case R_390_PLT16DBL:
1103 case R_390_PLT32DBL:
1104 case R_390_PLT32:
1105 case R_390_PLTOFF16:
1106 case R_390_PLTOFF32:
1107 if (h != NULL)
1108 {
1109 if (h->plt.refcount > 0)
1110 h->plt.refcount -= 1;
1111 }
1112 break;
1113
1114 case R_390_GOTPLT12:
1115 case R_390_GOTPLT16:
1116 case R_390_GOTPLT32:
1117 case R_390_GOTPLTENT:
1118 if (h != NULL)
1119 {
1120 if (h->plt.refcount > 0)
1121 {
1122 ((struct elf_s390_link_hash_entry *) h)->gotplt_refcount--;
1123 h->plt.refcount -= 1;
1124 }
1125 }
1126 else if (local_got_refcounts != NULL)
1127 {
1128 if (local_got_refcounts[r_symndx] > 0)
1129 local_got_refcounts[r_symndx] -= 1;
1130 }
1131 break;
1132
1133 case R_390_8:
1134 case R_390_12:
1135 case R_390_16:
1136 case R_390_32:
1137 case R_390_PC16:
1138 case R_390_PC16DBL:
1139 case R_390_PC32DBL:
1140 case R_390_PC32:
1141 if (h != NULL)
1142 {
1143 struct elf_s390_link_hash_entry *eh;
1144 struct elf_s390_dyn_relocs **pp;
1145 struct elf_s390_dyn_relocs *p;
1146
1147 if (!info->shared && h->plt.refcount > 0)
1148 h->plt.refcount -= 1;
1149
1150 eh = (struct elf_s390_link_hash_entry *) h;
1151
1152 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1153 if (p->sec == sec)
1154 {
1155 if (ELF32_R_TYPE (rel->r_info) == R_390_PC16
1156 || ELF32_R_TYPE (rel->r_info) == R_390_PC16DBL
1157 || ELF32_R_TYPE (rel->r_info) == R_390_PC32DBL
1158 || ELF32_R_TYPE (rel->r_info) == R_390_PC32)
1159 p->pc_count -= 1;
1160 p->count -= 1;
1161 if (p->count == 0)
1162 *pp = p->next;
1163 break;
1164 }
1165 }
1166 break;
1167
1168 default:
1169 break;
1170 }
1171 }
1172
1173 return TRUE;
1174 }
1175
1176 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1177 entry but we found we will not create any. Called when we find we will
1178 not have any PLT for this symbol, by for example
1179 elf_s390_adjust_dynamic_symbol when we're doing a proper dynamic link,
1180 or elf_s390_size_dynamic_sections if no dynamic sections will be
1181 created (we're only linking static objects). */
1182
1183 static void
1184 elf_s390_adjust_gotplt (h)
1185 struct elf_s390_link_hash_entry *h;
1186 {
1187 if (h->elf.root.type == bfd_link_hash_warning)
1188 h = (struct elf_s390_link_hash_entry *) h->elf.root.u.i.link;
1189
1190 if (h->gotplt_refcount <= 0)
1191 return;
1192
1193 /* We simply add the number of gotplt references to the number
1194 * of got references for this symbol. */
1195 h->elf.got.refcount += h->gotplt_refcount;
1196 h->gotplt_refcount = -1;
1197 }
1198
1199 /* Adjust a symbol defined by a dynamic object and referenced by a
1200 regular object. The current definition is in some section of the
1201 dynamic object, but we're not including those sections. We have to
1202 change the definition to something the rest of the link can
1203 understand. */
1204
1205 static bfd_boolean
1206 elf_s390_adjust_dynamic_symbol (info, h)
1207 struct bfd_link_info *info;
1208 struct elf_link_hash_entry *h;
1209 {
1210 struct elf_s390_link_hash_table *htab;
1211 struct elf_s390_link_hash_entry * eh;
1212 struct elf_s390_dyn_relocs *p;
1213 asection *s;
1214 unsigned int power_of_two;
1215
1216 /* If this is a function, put it in the procedure linkage table. We
1217 will fill in the contents of the procedure linkage table later
1218 (although we could actually do it here). */
1219 if (h->type == STT_FUNC
1220 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1221 {
1222 if (h->plt.refcount <= 0
1223 || (! info->shared
1224 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1225 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1226 && h->root.type != bfd_link_hash_undefweak
1227 && h->root.type != bfd_link_hash_undefined))
1228 {
1229 /* This case can occur if we saw a PLT32 reloc in an input
1230 file, but the symbol was never referred to by a dynamic
1231 object, or if all references were garbage collected. In
1232 such a case, we don't actually need to build a procedure
1233 linkage table, and we can just do a PC32 reloc instead. */
1234 h->plt.offset = (bfd_vma) -1;
1235 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1236 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1237 }
1238
1239 return TRUE;
1240 }
1241 else
1242 /* It's possible that we incorrectly decided a .plt reloc was
1243 needed for an R_390_PC32 reloc to a non-function sym in
1244 check_relocs. We can't decide accurately between function and
1245 non-function syms in check-relocs; Objects loaded later in
1246 the link may change h->type. So fix it now. */
1247 h->plt.offset = (bfd_vma) -1;
1248
1249 /* If this is a weak symbol, and there is a real definition, the
1250 processor independent code will have arranged for us to see the
1251 real definition first, and we can just use the same value. */
1252 if (h->weakdef != NULL)
1253 {
1254 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1255 || h->weakdef->root.type == bfd_link_hash_defweak);
1256 h->root.u.def.section = h->weakdef->root.u.def.section;
1257 h->root.u.def.value = h->weakdef->root.u.def.value;
1258 return TRUE;
1259 }
1260
1261 /* This is a reference to a symbol defined by a dynamic object which
1262 is not a function. */
1263
1264 /* If we are creating a shared library, we must presume that the
1265 only references to the symbol are via the global offset table.
1266 For such cases we need not do anything here; the relocations will
1267 be handled correctly by relocate_section. */
1268 if (info->shared)
1269 return TRUE;
1270
1271 /* If there are no references to this symbol that do not use the
1272 GOT, we don't need to generate a copy reloc. */
1273 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1274 return TRUE;
1275
1276 /* If -z nocopyreloc was given, we won't generate them either. */
1277 if (info->nocopyreloc)
1278 {
1279 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1280 return TRUE;
1281 }
1282
1283 eh = (struct elf_s390_link_hash_entry *) h;
1284 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1285 {
1286 s = p->sec->output_section;
1287 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1288 break;
1289 }
1290
1291 /* If we didn't find any dynamic relocs in read-only sections, then
1292 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1293 if (p == NULL)
1294 {
1295 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1296 return TRUE;
1297 }
1298
1299 /* We must allocate the symbol in our .dynbss section, which will
1300 become part of the .bss section of the executable. There will be
1301 an entry for this symbol in the .dynsym section. The dynamic
1302 object will contain position independent code, so all references
1303 from the dynamic object to this symbol will go through the global
1304 offset table. The dynamic linker will use the .dynsym entry to
1305 determine the address it must put in the global offset table, so
1306 both the dynamic object and the regular object will refer to the
1307 same memory location for the variable. */
1308
1309 htab = elf_s390_hash_table (info);
1310
1311 /* We must generate a R_390_COPY reloc to tell the dynamic linker to
1312 copy the initial value out of the dynamic object and into the
1313 runtime process image. */
1314 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1315 {
1316 htab->srelbss->_raw_size += sizeof (Elf32_External_Rela);
1317 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1318 }
1319
1320 /* We need to figure out the alignment required for this symbol. I
1321 have no idea how ELF linkers handle this. */
1322 power_of_two = bfd_log2 (h->size);
1323 if (power_of_two > 3)
1324 power_of_two = 3;
1325
1326 /* Apply the required alignment. */
1327 s = htab->sdynbss;
1328 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1329 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1330 {
1331 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1332 return FALSE;
1333 }
1334
1335 /* Define the symbol as being at this point in the section. */
1336 h->root.u.def.section = s;
1337 h->root.u.def.value = s->_raw_size;
1338
1339 /* Increment the section size to make room for the symbol. */
1340 s->_raw_size += h->size;
1341
1342 return TRUE;
1343 }
1344
1345 /* This is the condition under which elf_s390_finish_dynamic_symbol
1346 will be called from elflink.h. If elflink.h doesn't call our
1347 finish_dynamic_symbol routine, we'll need to do something about
1348 initializing any .plt and .got entries in elf_s390_relocate_section. */
1349 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1350 ((DYN) \
1351 && ((INFO)->shared \
1352 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1353 && ((H)->dynindx != -1 \
1354 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1355
1356 /* Allocate space in .plt, .got and associated reloc sections for
1357 dynamic relocs. */
1358
1359 static bfd_boolean
1360 allocate_dynrelocs (h, inf)
1361 struct elf_link_hash_entry *h;
1362 PTR inf;
1363 {
1364 struct bfd_link_info *info;
1365 struct elf_s390_link_hash_table *htab;
1366 struct elf_s390_link_hash_entry *eh;
1367 struct elf_s390_dyn_relocs *p;
1368
1369 if (h->root.type == bfd_link_hash_indirect)
1370 return TRUE;
1371
1372 if (h->root.type == bfd_link_hash_warning)
1373 /* When warning symbols are created, they **replace** the "real"
1374 entry in the hash table, thus we never get to see the real
1375 symbol in a hash traversal. So look at it now. */
1376 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1377
1378 info = (struct bfd_link_info *) inf;
1379 htab = elf_s390_hash_table (info);
1380
1381 if (htab->elf.dynamic_sections_created
1382 && h->plt.refcount > 0)
1383 {
1384 /* Make sure this symbol is output as a dynamic symbol.
1385 Undefined weak syms won't yet be marked as dynamic. */
1386 if (h->dynindx == -1
1387 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1388 {
1389 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1390 return FALSE;
1391 }
1392
1393 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1394 {
1395 asection *s = htab->splt;
1396
1397 /* If this is the first .plt entry, make room for the special
1398 first entry. */
1399 if (s->_raw_size == 0)
1400 s->_raw_size += PLT_FIRST_ENTRY_SIZE;
1401
1402 h->plt.offset = s->_raw_size;
1403
1404 /* If this symbol is not defined in a regular file, and we are
1405 not generating a shared library, then set the symbol to this
1406 location in the .plt. This is required to make function
1407 pointers compare as equal between the normal executable and
1408 the shared library. */
1409 if (! info->shared
1410 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1411 {
1412 h->root.u.def.section = s;
1413 h->root.u.def.value = h->plt.offset;
1414 }
1415
1416 /* Make room for this entry. */
1417 s->_raw_size += PLT_ENTRY_SIZE;
1418
1419 /* We also need to make an entry in the .got.plt section, which
1420 will be placed in the .got section by the linker script. */
1421 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1422
1423 /* We also need to make an entry in the .rela.plt section. */
1424 htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
1425 }
1426 else
1427 {
1428 h->plt.offset = (bfd_vma) -1;
1429 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1430 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1431 }
1432 }
1433 else
1434 {
1435 h->plt.offset = (bfd_vma) -1;
1436 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1437 elf_s390_adjust_gotplt((struct elf_s390_link_hash_entry *) h);
1438 }
1439
1440 if (h->got.refcount > 0)
1441 {
1442 asection *s;
1443 bfd_boolean dyn;
1444
1445 /* Make sure this symbol is output as a dynamic symbol.
1446 Undefined weak syms won't yet be marked as dynamic. */
1447 if (h->dynindx == -1
1448 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1449 {
1450 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1451 return FALSE;
1452 }
1453
1454 s = htab->sgot;
1455 h->got.offset = s->_raw_size;
1456 s->_raw_size += GOT_ENTRY_SIZE;
1457 dyn = htab->elf.dynamic_sections_created;
1458 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1459 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
1460 }
1461 else
1462 h->got.offset = (bfd_vma) -1;
1463
1464 eh = (struct elf_s390_link_hash_entry *) h;
1465 if (eh->dyn_relocs == NULL)
1466 return TRUE;
1467
1468 /* In the shared -Bsymbolic case, discard space allocated for
1469 dynamic pc-relative relocs against symbols which turn out to be
1470 defined in regular objects. For the normal shared case, discard
1471 space for pc-relative relocs that have become local due to symbol
1472 visibility changes. */
1473
1474 if (info->shared)
1475 {
1476 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1477 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1478 || info->symbolic))
1479 {
1480 struct elf_s390_dyn_relocs **pp;
1481
1482 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1483 {
1484 p->count -= p->pc_count;
1485 p->pc_count = 0;
1486 if (p->count == 0)
1487 *pp = p->next;
1488 else
1489 pp = &p->next;
1490 }
1491 }
1492 }
1493 else
1494 {
1495 /* For the non-shared case, discard space for relocs against
1496 symbols which turn out to need copy relocs or are not
1497 dynamic. */
1498
1499 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1500 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1501 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1502 || (htab->elf.dynamic_sections_created
1503 && (h->root.type == bfd_link_hash_undefweak
1504 || h->root.type == bfd_link_hash_undefined))))
1505 {
1506 /* Make sure this symbol is output as a dynamic symbol.
1507 Undefined weak syms won't yet be marked as dynamic. */
1508 if (h->dynindx == -1
1509 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1510 {
1511 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1512 return FALSE;
1513 }
1514
1515 /* If that succeeded, we know we'll be keeping all the
1516 relocs. */
1517 if (h->dynindx != -1)
1518 goto keep;
1519 }
1520
1521 eh->dyn_relocs = NULL;
1522
1523 keep: ;
1524 }
1525
1526 /* Finally, allocate space. */
1527 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1528 {
1529 asection *sreloc = elf_section_data (p->sec)->sreloc;
1530 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
1531 }
1532
1533 return TRUE;
1534 }
1535
1536 /* Find any dynamic relocs that apply to read-only sections. */
1537
1538 static bfd_boolean
1539 readonly_dynrelocs (h, inf)
1540 struct elf_link_hash_entry *h;
1541 PTR inf;
1542 {
1543 struct elf_s390_link_hash_entry *eh;
1544 struct elf_s390_dyn_relocs *p;
1545
1546 if (h->root.type == bfd_link_hash_warning)
1547 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1548
1549 eh = (struct elf_s390_link_hash_entry *) h;
1550 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1551 {
1552 asection *s = p->sec->output_section;
1553
1554 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1555 {
1556 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1557
1558 info->flags |= DF_TEXTREL;
1559
1560 /* Not an error, just cut short the traversal. */
1561 return FALSE;
1562 }
1563 }
1564 return TRUE;
1565 }
1566
1567 /* Set the sizes of the dynamic sections. */
1568
1569 static bfd_boolean
1570 elf_s390_size_dynamic_sections (output_bfd, info)
1571 bfd *output_bfd ATTRIBUTE_UNUSED;
1572 struct bfd_link_info *info;
1573 {
1574 struct elf_s390_link_hash_table *htab;
1575 bfd *dynobj;
1576 asection *s;
1577 bfd_boolean relocs;
1578 bfd *ibfd;
1579
1580 htab = elf_s390_hash_table (info);
1581 dynobj = htab->elf.dynobj;
1582 if (dynobj == NULL)
1583 abort ();
1584
1585 if (htab->elf.dynamic_sections_created)
1586 {
1587 /* Set the contents of the .interp section to the interpreter. */
1588 if (! info->shared)
1589 {
1590 s = bfd_get_section_by_name (dynobj, ".interp");
1591 if (s == NULL)
1592 abort ();
1593 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1594 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1595 }
1596 }
1597
1598 /* Set up .got offsets for local syms, and space for local dynamic
1599 relocs. */
1600 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1601 {
1602 bfd_signed_vma *local_got;
1603 bfd_signed_vma *end_local_got;
1604 bfd_size_type locsymcount;
1605 Elf_Internal_Shdr *symtab_hdr;
1606 asection *srela;
1607
1608 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1609 continue;
1610
1611 for (s = ibfd->sections; s != NULL; s = s->next)
1612 {
1613 struct elf_s390_dyn_relocs *p;
1614
1615 for (p = *((struct elf_s390_dyn_relocs **)
1616 &elf_section_data (s)->local_dynrel);
1617 p != NULL;
1618 p = p->next)
1619 {
1620 if (!bfd_is_abs_section (p->sec)
1621 && bfd_is_abs_section (p->sec->output_section))
1622 {
1623 /* Input section has been discarded, either because
1624 it is a copy of a linkonce section or due to
1625 linker script /DISCARD/, so we'll be discarding
1626 the relocs too. */
1627 }
1628 else if (p->count != 0)
1629 {
1630 srela = elf_section_data (p->sec)->sreloc;
1631 srela->_raw_size += p->count * sizeof (Elf32_External_Rela);
1632 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1633 info->flags |= DF_TEXTREL;
1634 }
1635 }
1636 }
1637
1638 local_got = elf_local_got_refcounts (ibfd);
1639 if (!local_got)
1640 continue;
1641
1642 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1643 locsymcount = symtab_hdr->sh_info;
1644 end_local_got = local_got + locsymcount;
1645 s = htab->sgot;
1646 srela = htab->srelgot;
1647 for (; local_got < end_local_got; ++local_got)
1648 {
1649 if (*local_got > 0)
1650 {
1651 *local_got = s->_raw_size;
1652 s->_raw_size += GOT_ENTRY_SIZE;
1653 if (info->shared)
1654 srela->_raw_size += sizeof (Elf32_External_Rela);
1655 }
1656 else
1657 *local_got = (bfd_vma) -1;
1658 }
1659 }
1660
1661 /* Allocate global sym .plt and .got entries, and space for global
1662 sym dynamic relocs. */
1663 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1664
1665 /* We now have determined the sizes of the various dynamic sections.
1666 Allocate memory for them. */
1667 relocs = FALSE;
1668 for (s = dynobj->sections; s != NULL; s = s->next)
1669 {
1670 if ((s->flags & SEC_LINKER_CREATED) == 0)
1671 continue;
1672
1673 if (s == htab->splt
1674 || s == htab->sgot
1675 || s == htab->sgotplt)
1676 {
1677 /* Strip this section if we don't need it; see the
1678 comment below. */
1679 }
1680 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1681 {
1682 if (s->_raw_size != 0 && s != htab->srelplt)
1683 relocs = TRUE;
1684
1685 /* We use the reloc_count field as a counter if we need
1686 to copy relocs into the output file. */
1687 s->reloc_count = 0;
1688 }
1689 else
1690 {
1691 /* It's not one of our sections, so don't allocate space. */
1692 continue;
1693 }
1694
1695 if (s->_raw_size == 0)
1696 {
1697 /* If we don't need this section, strip it from the
1698 output file. This is to handle .rela.bss and
1699 .rela.plt. We must create it in
1700 create_dynamic_sections, because it must be created
1701 before the linker maps input sections to output
1702 sections. The linker does that before
1703 adjust_dynamic_symbol is called, and it is that
1704 function which decides whether anything needs to go
1705 into these sections. */
1706
1707 _bfd_strip_section_from_output (info, s);
1708 continue;
1709 }
1710
1711 /* Allocate memory for the section contents. We use bfd_zalloc
1712 here in case unused entries are not reclaimed before the
1713 section's contents are written out. This should not happen,
1714 but this way if it does, we get a R_390_NONE reloc instead
1715 of garbage. */
1716 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1717 if (s->contents == NULL)
1718 return FALSE;
1719 }
1720
1721 if (htab->elf.dynamic_sections_created)
1722 {
1723 /* Add some entries to the .dynamic section. We fill in the
1724 values later, in elf_s390_finish_dynamic_sections, but we
1725 must add the entries now so that we get the correct size for
1726 the .dynamic section. The DT_DEBUG entry is filled in by the
1727 dynamic linker and used by the debugger. */
1728 #define add_dynamic_entry(TAG, VAL) \
1729 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1730
1731 if (! info->shared)
1732 {
1733 if (!add_dynamic_entry (DT_DEBUG, 0))
1734 return FALSE;
1735 }
1736
1737 if (htab->splt->_raw_size != 0)
1738 {
1739 if (!add_dynamic_entry (DT_PLTGOT, 0)
1740 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1741 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1742 || !add_dynamic_entry (DT_JMPREL, 0))
1743 return FALSE;
1744 }
1745
1746 if (relocs)
1747 {
1748 if (!add_dynamic_entry (DT_RELA, 0)
1749 || !add_dynamic_entry (DT_RELASZ, 0)
1750 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1751 return FALSE;
1752
1753 /* If any dynamic relocs apply to a read-only section,
1754 then we need a DT_TEXTREL entry. */
1755 if ((info->flags & DF_TEXTREL) == 0)
1756 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1757 (PTR) info);
1758
1759 if ((info->flags & DF_TEXTREL) != 0)
1760 {
1761 if (!add_dynamic_entry (DT_TEXTREL, 0))
1762 return FALSE;
1763 }
1764 }
1765 }
1766 #undef add_dynamic_entry
1767
1768 return TRUE;
1769 }
1770
1771 /* Relocate a 390 ELF section. */
1772
1773 static bfd_boolean
1774 elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
1775 contents, relocs, local_syms, local_sections)
1776 bfd *output_bfd;
1777 struct bfd_link_info *info;
1778 bfd *input_bfd;
1779 asection *input_section;
1780 bfd_byte *contents;
1781 Elf_Internal_Rela *relocs;
1782 Elf_Internal_Sym *local_syms;
1783 asection **local_sections;
1784 {
1785 struct elf_s390_link_hash_table *htab;
1786 Elf_Internal_Shdr *symtab_hdr;
1787 struct elf_link_hash_entry **sym_hashes;
1788 bfd_vma *local_got_offsets;
1789 Elf_Internal_Rela *rel;
1790 Elf_Internal_Rela *relend;
1791
1792 if (info->relocateable)
1793 return TRUE;
1794
1795 htab = elf_s390_hash_table (info);
1796 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1797 sym_hashes = elf_sym_hashes (input_bfd);
1798 local_got_offsets = elf_local_got_offsets (input_bfd);
1799
1800 rel = relocs;
1801 relend = relocs + input_section->reloc_count;
1802 for (; rel < relend; rel++)
1803 {
1804 unsigned int r_type;
1805 reloc_howto_type *howto;
1806 unsigned long r_symndx;
1807 struct elf_link_hash_entry *h;
1808 Elf_Internal_Sym *sym;
1809 asection *sec;
1810 bfd_vma off;
1811 bfd_vma relocation;
1812 bfd_boolean unresolved_reloc;
1813 bfd_reloc_status_type r;
1814
1815 r_type = ELF32_R_TYPE (rel->r_info);
1816 if (r_type == (int) R_390_GNU_VTINHERIT
1817 || r_type == (int) R_390_GNU_VTENTRY)
1818 continue;
1819 if (r_type >= (int) R_390_max)
1820 {
1821 bfd_set_error (bfd_error_bad_value);
1822 return FALSE;
1823 }
1824
1825 howto = elf_howto_table + r_type;
1826 r_symndx = ELF32_R_SYM (rel->r_info);
1827
1828 /* This is a final link. */
1829 h = NULL;
1830 sym = NULL;
1831 sec = NULL;
1832 unresolved_reloc = FALSE;
1833 if (r_symndx < symtab_hdr->sh_info)
1834 {
1835 sym = local_syms + r_symndx;
1836 sec = local_sections[r_symndx];
1837 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1838 }
1839 else
1840 {
1841 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1842 while (h->root.type == bfd_link_hash_indirect
1843 || h->root.type == bfd_link_hash_warning)
1844 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1845
1846 if (h->root.type == bfd_link_hash_defined
1847 || h->root.type == bfd_link_hash_defweak)
1848 {
1849 sec = h->root.u.def.section;
1850 if (sec->output_section == NULL)
1851 {
1852 /* Set a flag that will be cleared later if we find a
1853 relocation value for this symbol. output_section
1854 is typically NULL for symbols satisfied by a shared
1855 library. */
1856 unresolved_reloc = TRUE;
1857 relocation = 0;
1858 }
1859 else
1860 relocation = (h->root.u.def.value
1861 + sec->output_section->vma
1862 + sec->output_offset);
1863 }
1864 else if (h->root.type == bfd_link_hash_undefweak)
1865 relocation = 0;
1866 else if (info->shared
1867 && (!info->symbolic || info->allow_shlib_undefined)
1868 && !info->no_undefined
1869 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1870 relocation = 0;
1871 else
1872 {
1873 if (! ((*info->callbacks->undefined_symbol)
1874 (info, h->root.root.string, input_bfd,
1875 input_section, rel->r_offset,
1876 (!info->shared || info->no_undefined
1877 || ELF_ST_VISIBILITY (h->other)))))
1878 return FALSE;
1879 relocation = 0;
1880 }
1881 }
1882
1883 switch (r_type)
1884 {
1885 case R_390_GOTPLT12:
1886 case R_390_GOTPLT16:
1887 case R_390_GOTPLT32:
1888 case R_390_GOTPLTENT:
1889 /* There are three cases for a GOTPLT relocation. 1) The
1890 relocation is against the jump slot entry of a plt that
1891 will get emitted to the output file. 2) The relocation
1892 is against the jump slot of a plt entry that has been
1893 removed. elf_s390_adjust_gotplt has created a GOT entry
1894 as replacement. 3) The relocation is against a local symbol.
1895 Cases 2) and 3) are the same as the GOT relocation code
1896 so we just have to test for case 1 and fall through for
1897 the other two. */
1898 if (h != NULL && h->plt.offset != (bfd_vma) -1)
1899 {
1900 bfd_vma plt_index;
1901
1902 /* Calc. index no.
1903 Current offset - size first entry / entry size. */
1904 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) /
1905 PLT_ENTRY_SIZE;
1906
1907 /* Offset in GOT is PLT index plus GOT headers(3) times 4,
1908 addr & GOT addr. */
1909 relocation = (plt_index + 3) * GOT_ENTRY_SIZE;
1910 unresolved_reloc = FALSE;
1911
1912 if (r_type == R_390_GOTPLTENT)
1913 relocation += htab->sgot->output_section->vma;
1914 break;
1915 }
1916 /* Fall through. */
1917
1918 case R_390_GOT12:
1919 case R_390_GOT16:
1920 case R_390_GOT32:
1921 case R_390_GOTENT:
1922 /* Relocation is to the entry for this symbol in the global
1923 offset table. */
1924 if (htab->sgot == NULL)
1925 abort ();
1926
1927 if (h != NULL)
1928 {
1929 bfd_boolean dyn;
1930
1931 off = h->got.offset;
1932 dyn = htab->elf.dynamic_sections_created;
1933 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
1934 || (info->shared
1935 && (info->symbolic
1936 || h->dynindx == -1
1937 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
1938 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1939 {
1940 /* This is actually a static link, or it is a
1941 -Bsymbolic link and the symbol is defined
1942 locally, or the symbol was forced to be local
1943 because of a version file. We must initialize
1944 this entry in the global offset table. Since the
1945 offset must always be a multiple of 2, we use the
1946 least significant bit to record whether we have
1947 initialized it already.
1948
1949 When doing a dynamic link, we create a .rel.got
1950 relocation entry to initialize the value. This
1951 is done in the finish_dynamic_symbol routine. */
1952 if ((off & 1) != 0)
1953 off &= ~1;
1954 else
1955 {
1956 bfd_put_32 (output_bfd, relocation,
1957 htab->sgot->contents + off);
1958 h->got.offset |= 1;
1959 }
1960 }
1961 else
1962 unresolved_reloc = FALSE;
1963 }
1964 else
1965 {
1966 if (local_got_offsets == NULL)
1967 abort ();
1968
1969 off = local_got_offsets[r_symndx];
1970
1971 /* The offset must always be a multiple of 4. We use
1972 the least significant bit to record whether we have
1973 already generated the necessary reloc. */
1974 if ((off & 1) != 0)
1975 off &= ~1;
1976 else
1977 {
1978 bfd_put_32 (output_bfd, relocation,
1979 htab->sgot->contents + off);
1980
1981 if (info->shared)
1982 {
1983 asection *srelgot;
1984 Elf_Internal_Rela outrel;
1985 bfd_byte *loc;
1986
1987 srelgot = htab->srelgot;
1988 if (srelgot == NULL)
1989 abort ();
1990
1991 outrel.r_offset = (htab->sgot->output_section->vma
1992 + htab->sgot->output_offset
1993 + off);
1994 outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
1995 outrel.r_addend = relocation;
1996 loc = srelgot->contents;
1997 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1998 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1999 }
2000
2001 local_got_offsets[r_symndx] |= 1;
2002 }
2003 }
2004
2005 if (off >= (bfd_vma) -2)
2006 abort ();
2007
2008 relocation = htab->sgot->output_offset + off;
2009
2010 /*
2011 * For @GOTENT the relocation is against the offset between
2012 * the instruction and the symbols entry in the GOT and not
2013 * between the start of the GOT and the symbols entry. We
2014 * add the vma of the GOT to get the correct value.
2015 */
2016 if ( r_type == R_390_GOTENT
2017 || r_type == R_390_GOTPLTENT)
2018 relocation += htab->sgot->output_section->vma;
2019
2020 break;
2021
2022 case R_390_GOTOFF16:
2023 case R_390_GOTOFF32:
2024 /* Relocation is relative to the start of the global offset
2025 table. */
2026
2027 /* Note that sgot->output_offset is not involved in this
2028 calculation. We always want the start of .got. If we
2029 defined _GLOBAL_OFFSET_TABLE in a different way, as is
2030 permitted by the ABI, we might have to change this
2031 calculation. */
2032 relocation -= htab->sgot->output_section->vma;
2033 break;
2034
2035 case R_390_GOTPC:
2036 case R_390_GOTPCDBL:
2037 /* Use global offset table as symbol value. */
2038 relocation = htab->sgot->output_section->vma;
2039 unresolved_reloc = FALSE;
2040 break;
2041
2042 case R_390_PLT16DBL:
2043 case R_390_PLT32DBL:
2044 case R_390_PLT32:
2045 /* Relocation is to the entry for this symbol in the
2046 procedure linkage table. */
2047
2048 /* Resolve a PLT32 reloc against a local symbol directly,
2049 without using the procedure linkage table. */
2050 if (h == NULL)
2051 break;
2052
2053 if (h->plt.offset == (bfd_vma) -1
2054 || htab->splt == NULL)
2055 {
2056 /* We didn't make a PLT entry for this symbol. This
2057 happens when statically linking PIC code, or when
2058 using -Bsymbolic. */
2059 break;
2060 }
2061
2062 relocation = (htab->splt->output_section->vma
2063 + htab->splt->output_offset
2064 + h->plt.offset);
2065 unresolved_reloc = FALSE;
2066 break;
2067
2068 case R_390_PLTOFF16:
2069 case R_390_PLTOFF32:
2070 /* Relocation is to the entry for this symbol in the
2071 procedure linkage table relative to the start of the GOT. */
2072
2073 /* For local symbols or if we didn't make a PLT entry for
2074 this symbol resolve the symbol directly. */
2075 if ( h == NULL
2076 || h->plt.offset == (bfd_vma) -1
2077 || htab->splt == NULL)
2078 {
2079 relocation -= htab->sgot->output_section->vma;
2080 break;
2081 }
2082
2083 relocation = (htab->splt->output_section->vma
2084 + htab->splt->output_offset
2085 + h->plt.offset
2086 - htab->sgot->output_section->vma);
2087 unresolved_reloc = FALSE;
2088 break;
2089
2090 case R_390_8:
2091 case R_390_16:
2092 case R_390_32:
2093 case R_390_PC16:
2094 case R_390_PC16DBL:
2095 case R_390_PC32DBL:
2096 case R_390_PC32:
2097 /* r_symndx will be zero only for relocs against symbols
2098 from removed linkonce sections, or sections discarded by
2099 a linker script. */
2100 if (r_symndx == 0
2101 || (input_section->flags & SEC_ALLOC) == 0)
2102 break;
2103
2104 if ((info->shared
2105 && ((r_type != R_390_PC16
2106 && r_type != R_390_PC16DBL
2107 && r_type != R_390_PC32DBL
2108 && r_type != R_390_PC32)
2109 || (h != NULL
2110 && h->dynindx != -1
2111 && (! info->symbolic
2112 || (h->elf_link_hash_flags
2113 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2114 || (!info->shared
2115 && h != NULL
2116 && h->dynindx != -1
2117 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2118 && (((h->elf_link_hash_flags
2119 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2120 && (h->elf_link_hash_flags
2121 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2122 || h->root.type == bfd_link_hash_undefweak
2123 || h->root.type == bfd_link_hash_undefined)))
2124 {
2125 Elf_Internal_Rela outrel;
2126 bfd_boolean skip, relocate;
2127 asection *sreloc;
2128 bfd_byte *loc;
2129
2130 /* When generating a shared object, these relocations
2131 are copied into the output file to be resolved at run
2132 time. */
2133
2134 skip = FALSE;
2135 relocate = FALSE;
2136
2137 outrel.r_offset =
2138 _bfd_elf_section_offset (output_bfd, info, input_section,
2139 rel->r_offset);
2140 if (outrel.r_offset == (bfd_vma) -1)
2141 skip = TRUE;
2142 else if (outrel.r_offset == (bfd_vma) -2)
2143 skip = TRUE, relocate = TRUE;
2144 outrel.r_offset += (input_section->output_section->vma
2145 + input_section->output_offset);
2146
2147 if (skip)
2148 memset (&outrel, 0, sizeof outrel);
2149 else if (h != NULL
2150 && h->dynindx != -1
2151 && (r_type == R_390_PC16
2152 || r_type == R_390_PC16DBL
2153 || r_type == R_390_PC32DBL
2154 || r_type == R_390_PC32
2155 || !info->shared
2156 || !info->symbolic
2157 || (h->elf_link_hash_flags
2158 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2159 {
2160 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2161 outrel.r_addend = rel->r_addend;
2162 }
2163 else
2164 {
2165 /* This symbol is local, or marked to become local. */
2166 relocate = TRUE;
2167 outrel.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2168 outrel.r_addend = relocation + rel->r_addend;
2169 }
2170
2171 sreloc = elf_section_data (input_section)->sreloc;
2172 if (sreloc == NULL)
2173 abort ();
2174
2175 loc = sreloc->contents;
2176 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2177 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2178
2179 /* If this reloc is against an external symbol, we do
2180 not want to fiddle with the addend. Otherwise, we
2181 need to include the symbol value so that it becomes
2182 an addend for the dynamic reloc. */
2183 if (! relocate)
2184 continue;
2185 }
2186 break;
2187
2188 default:
2189 break;
2190 }
2191
2192 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2193 because such sections are not SEC_ALLOC and thus ld.so will
2194 not process them. */
2195 if (unresolved_reloc
2196 && !((input_section->flags & SEC_DEBUGGING) != 0
2197 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2198 (*_bfd_error_handler)
2199 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2200 bfd_archive_filename (input_bfd),
2201 bfd_get_section_name (input_bfd, input_section),
2202 (long) rel->r_offset,
2203 h->root.root.string);
2204
2205 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2206 contents, rel->r_offset,
2207 relocation, rel->r_addend);
2208
2209 if (r != bfd_reloc_ok)
2210 {
2211 const char *name;
2212
2213 if (h != NULL)
2214 name = h->root.root.string;
2215 else
2216 {
2217 name = bfd_elf_string_from_elf_section (input_bfd,
2218 symtab_hdr->sh_link,
2219 sym->st_name);
2220 if (name == NULL)
2221 return FALSE;
2222 if (*name == '\0')
2223 name = bfd_section_name (input_bfd, sec);
2224 }
2225
2226 if (r == bfd_reloc_overflow)
2227 {
2228
2229 if (! ((*info->callbacks->reloc_overflow)
2230 (info, name, howto->name, (bfd_vma) 0,
2231 input_bfd, input_section, rel->r_offset)))
2232 return FALSE;
2233 }
2234 else
2235 {
2236 (*_bfd_error_handler)
2237 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
2238 bfd_archive_filename (input_bfd),
2239 bfd_get_section_name (input_bfd, input_section),
2240 (long) rel->r_offset, name, (int) r);
2241 return FALSE;
2242 }
2243 }
2244 }
2245
2246 return TRUE;
2247 }
2248
2249 /* Finish up dynamic symbol handling. We set the contents of various
2250 dynamic sections here. */
2251
2252 static bfd_boolean
2253 elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym)
2254 bfd *output_bfd;
2255 struct bfd_link_info *info;
2256 struct elf_link_hash_entry *h;
2257 Elf_Internal_Sym *sym;
2258 {
2259 struct elf_s390_link_hash_table *htab;
2260
2261 htab = elf_s390_hash_table (info);
2262
2263 if (h->plt.offset != (bfd_vma) -1)
2264 {
2265 bfd_vma plt_index;
2266 bfd_vma got_offset;
2267 Elf_Internal_Rela rela;
2268 bfd_byte *loc;
2269 bfd_vma relative_offset;
2270
2271 /* This symbol has an entry in the procedure linkage table. Set
2272 it up. */
2273
2274 if (h->dynindx == -1
2275 || htab->splt == NULL
2276 || htab->sgotplt == NULL
2277 || htab->srelplt == NULL)
2278 abort ();
2279
2280 /* Calc. index no.
2281 Current offset - size first entry / entry size. */
2282 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE;
2283
2284 /* Offset in GOT is PLT index plus GOT headers(3) times 4,
2285 addr & GOT addr. */
2286 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2287
2288 /* S390 uses halfwords for relative branch calc! */
2289 relative_offset = - ((PLT_FIRST_ENTRY_SIZE +
2290 (PLT_ENTRY_SIZE * plt_index) + 18) / 2);
2291 /* If offset is > 32768, branch to a previous branch
2292 390 can only handle +-64 K jumps. */
2293 if ( -32768 > (int) relative_offset )
2294 relative_offset =
2295 -(unsigned) (((65536 / PLT_ENTRY_SIZE - 1) * PLT_ENTRY_SIZE) / 2);
2296
2297 /* Fill in the entry in the procedure linkage table. */
2298 if (!info->shared)
2299 {
2300 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD0,
2301 htab->splt->contents + h->plt.offset);
2302 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD1,
2303 htab->splt->contents + h->plt.offset + 4);
2304 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
2305 htab->splt->contents + h->plt.offset + 8);
2306 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
2307 htab->splt->contents + h->plt.offset + 12);
2308 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD4,
2309 htab->splt->contents + h->plt.offset + 16);
2310 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2311 htab->splt->contents + h->plt.offset + 20);
2312 bfd_put_32 (output_bfd,
2313 (htab->sgotplt->output_section->vma
2314 + htab->sgotplt->output_offset
2315 + got_offset),
2316 htab->splt->contents + h->plt.offset + 24);
2317 }
2318 else if (got_offset < 4096)
2319 {
2320 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD0 + got_offset,
2321 htab->splt->contents + h->plt.offset);
2322 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD1,
2323 htab->splt->contents + h->plt.offset + 4);
2324 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD2,
2325 htab->splt->contents + h->plt.offset + 8);
2326 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD3,
2327 htab->splt->contents + h->plt.offset + 12);
2328 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC12_ENTRY_WORD4,
2329 htab->splt->contents + h->plt.offset + 16);
2330 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2331 htab->splt->contents + h->plt.offset + 20);
2332 bfd_put_32 (output_bfd, (bfd_vma) 0,
2333 htab->splt->contents + h->plt.offset + 24);
2334 }
2335 else if (got_offset < 32768)
2336 {
2337 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD0 + got_offset,
2338 htab->splt->contents + h->plt.offset);
2339 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD1,
2340 htab->splt->contents + h->plt.offset + 4);
2341 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD2,
2342 htab->splt->contents + h->plt.offset + 8);
2343 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD3,
2344 htab->splt->contents + h->plt.offset + 12);
2345 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC16_ENTRY_WORD4,
2346 htab->splt->contents + h->plt.offset + 16);
2347 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2348 htab->splt->contents + h->plt.offset + 20);
2349 bfd_put_32 (output_bfd, (bfd_vma) 0,
2350 htab->splt->contents + h->plt.offset + 24);
2351 }
2352 else
2353 {
2354 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD0,
2355 htab->splt->contents + h->plt.offset);
2356 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD1,
2357 htab->splt->contents + h->plt.offset + 4);
2358 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
2359 htab->splt->contents + h->plt.offset + 8);
2360 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
2361 htab->splt->contents + h->plt.offset + 12);
2362 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
2363 htab->splt->contents + h->plt.offset + 16);
2364 bfd_put_32 (output_bfd, (bfd_vma) 0+(relative_offset << 16),
2365 htab->splt->contents + h->plt.offset + 20);
2366 bfd_put_32 (output_bfd, got_offset,
2367 htab->splt->contents + h->plt.offset + 24);
2368 }
2369 /* Insert offset into reloc. table here. */
2370 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
2371 htab->splt->contents + h->plt.offset + 28);
2372
2373 /* Fill in the entry in the global offset table.
2374 Points to instruction after GOT offset. */
2375 bfd_put_32 (output_bfd,
2376 (htab->splt->output_section->vma
2377 + htab->splt->output_offset
2378 + h->plt.offset
2379 + 12),
2380 htab->sgotplt->contents + got_offset);
2381
2382 /* Fill in the entry in the .rela.plt section. */
2383 rela.r_offset = (htab->sgotplt->output_section->vma
2384 + htab->sgotplt->output_offset
2385 + got_offset);
2386 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_JMP_SLOT);
2387 rela.r_addend = 0;
2388 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
2389 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2390
2391 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2392 {
2393 /* Mark the symbol as undefined, rather than as defined in
2394 the .plt section. Leave the value alone. This is a clue
2395 for the dynamic linker, to make function pointer
2396 comparisons work between an application and shared
2397 library. */
2398 sym->st_shndx = SHN_UNDEF;
2399 }
2400 }
2401
2402 if (h->got.offset != (bfd_vma) -1)
2403 {
2404 Elf_Internal_Rela rela;
2405 bfd_byte *loc;
2406
2407 /* This symbol has an entry in the global offset table. Set it
2408 up. */
2409
2410 if (htab->sgot == NULL || htab->srelgot == NULL)
2411 abort ();
2412
2413 rela.r_offset = (htab->sgot->output_section->vma
2414 + htab->sgot->output_offset
2415 + (h->got.offset &~ (bfd_vma) 1));
2416
2417 /* If this is a static link, or it is a -Bsymbolic link and the
2418 symbol is defined locally or was forced to be local because
2419 of a version file, we just want to emit a RELATIVE reloc.
2420 The entry in the global offset table will already have been
2421 initialized in the relocate_section function. */
2422 if (info->shared
2423 && (info->symbolic
2424 || h->dynindx == -1
2425 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2426 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2427 {
2428 BFD_ASSERT((h->got.offset & 1) != 0);
2429 rela.r_info = ELF32_R_INFO (0, R_390_RELATIVE);
2430 rela.r_addend = (h->root.u.def.value
2431 + h->root.u.def.section->output_section->vma
2432 + h->root.u.def.section->output_offset);
2433 }
2434 else
2435 {
2436 BFD_ASSERT((h->got.offset & 1) == 0);
2437 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgot->contents + h->got.offset);
2438 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_GLOB_DAT);
2439 rela.r_addend = 0;
2440 }
2441
2442 loc = htab->srelgot->contents;
2443 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
2444 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2445 }
2446
2447 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2448 {
2449 Elf_Internal_Rela rela;
2450 bfd_byte *loc;
2451
2452 /* This symbols needs a copy reloc. Set it up. */
2453
2454 if (h->dynindx == -1
2455 || (h->root.type != bfd_link_hash_defined
2456 && h->root.type != bfd_link_hash_defweak)
2457 || htab->srelbss == NULL)
2458 abort ();
2459
2460 rela.r_offset = (h->root.u.def.value
2461 + h->root.u.def.section->output_section->vma
2462 + h->root.u.def.section->output_offset);
2463 rela.r_info = ELF32_R_INFO (h->dynindx, R_390_COPY);
2464 rela.r_addend = 0;
2465 loc = htab->srelbss->contents;
2466 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rela);
2467 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2468 }
2469
2470 /* Mark some specially defined symbols as absolute. */
2471 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2472 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2473 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2474 sym->st_shndx = SHN_ABS;
2475
2476 return TRUE;
2477 }
2478
2479 /* Used to decide how to sort relocs in an optimal manner for the
2480 dynamic linker, before writing them out. */
2481
2482 static enum elf_reloc_type_class
2483 elf_s390_reloc_type_class (rela)
2484 const Elf_Internal_Rela *rela;
2485 {
2486 switch ((int) ELF32_R_TYPE (rela->r_info))
2487 {
2488 case R_390_RELATIVE:
2489 return reloc_class_relative;
2490 case R_390_JMP_SLOT:
2491 return reloc_class_plt;
2492 case R_390_COPY:
2493 return reloc_class_copy;
2494 default:
2495 return reloc_class_normal;
2496 }
2497 }
2498
2499 /* Finish up the dynamic sections. */
2500
2501 static bfd_boolean
2502 elf_s390_finish_dynamic_sections (output_bfd, info)
2503 bfd *output_bfd;
2504 struct bfd_link_info *info;
2505 {
2506 struct elf_s390_link_hash_table *htab;
2507 bfd *dynobj;
2508 asection *sdyn;
2509
2510 htab = elf_s390_hash_table (info);
2511 dynobj = htab->elf.dynobj;
2512 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2513
2514 if (htab->elf.dynamic_sections_created)
2515 {
2516 Elf32_External_Dyn *dyncon, *dynconend;
2517
2518 if (sdyn == NULL || htab->sgot == NULL)
2519 abort ();
2520
2521 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2522 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2523 for (; dyncon < dynconend; dyncon++)
2524 {
2525 Elf_Internal_Dyn dyn;
2526 asection *s;
2527
2528 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2529
2530 switch (dyn.d_tag)
2531 {
2532 default:
2533 continue;
2534
2535 case DT_PLTGOT:
2536 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
2537 break;
2538
2539 case DT_JMPREL:
2540 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2541 break;
2542
2543 case DT_PLTRELSZ:
2544 s = htab->srelplt->output_section;
2545 if (s->_cooked_size != 0)
2546 dyn.d_un.d_val = s->_cooked_size;
2547 else
2548 dyn.d_un.d_val = s->_raw_size;
2549 break;
2550 }
2551
2552 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2553 }
2554
2555 /* Fill in the special first entry in the procedure linkage table. */
2556 if (htab->splt && htab->splt->_raw_size > 0)
2557 {
2558 memset (htab->splt->contents, 0, PLT_FIRST_ENTRY_SIZE);
2559 if (info->shared)
2560 {
2561 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD0,
2562 htab->splt->contents );
2563 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD1,
2564 htab->splt->contents +4 );
2565 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD2,
2566 htab->splt->contents +8 );
2567 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD3,
2568 htab->splt->contents +12 );
2569 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_FIRST_ENTRY_WORD4,
2570 htab->splt->contents +16 );
2571 }
2572 else
2573 {
2574 bfd_put_32 (output_bfd, (bfd_vma)PLT_FIRST_ENTRY_WORD0,
2575 htab->splt->contents );
2576 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD1,
2577 htab->splt->contents +4 );
2578 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD2,
2579 htab->splt->contents +8 );
2580 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD3,
2581 htab->splt->contents +12 );
2582 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD4,
2583 htab->splt->contents +16 );
2584 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD5,
2585 htab->splt->contents +20 );
2586 bfd_put_32 (output_bfd,
2587 htab->sgotplt->output_section->vma
2588 + htab->sgotplt->output_offset,
2589 htab->splt->contents + 24);
2590 }
2591 elf_section_data (htab->splt->output_section)
2592 ->this_hdr.sh_entsize = 4;
2593 }
2594
2595 }
2596
2597 if (htab->sgotplt)
2598 {
2599 /* Fill in the first three entries in the global offset table. */
2600 if (htab->sgotplt->_raw_size > 0)
2601 {
2602 bfd_put_32 (output_bfd,
2603 (sdyn == NULL ? (bfd_vma) 0
2604 : sdyn->output_section->vma + sdyn->output_offset),
2605 htab->sgotplt->contents);
2606 /* One entry for shared object struct ptr. */
2607 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 4);
2608 /* One entry for _dl_runtime_resolve. */
2609 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8);
2610 }
2611
2612 elf_section_data (htab->sgotplt->output_section)
2613 ->this_hdr.sh_entsize = 4;
2614 }
2615 return TRUE;
2616 }
2617
2618 static bfd_boolean
2619 elf_s390_object_p (abfd)
2620 bfd *abfd;
2621 {
2622 return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_31);
2623 }
2624
2625 static bfd_boolean
2626 elf_s390_grok_prstatus (abfd, note)
2627 bfd * abfd;
2628 Elf_Internal_Note * note;
2629 {
2630 int offset;
2631 unsigned int raw_size;
2632
2633 switch (note->descsz)
2634 {
2635 default:
2636 return FALSE;
2637
2638 case 224: /* S/390 Linux. */
2639 /* pr_cursig */
2640 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2641
2642 /* pr_pid */
2643 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
2644
2645 /* pr_reg */
2646 offset = 72;
2647 raw_size = 144;
2648 break;
2649 }
2650
2651 /* Make a ".reg/999" section. */
2652 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2653 raw_size, note->descpos + offset);
2654 }
2655
2656 #define TARGET_BIG_SYM bfd_elf32_s390_vec
2657 #define TARGET_BIG_NAME "elf32-s390"
2658 #define ELF_ARCH bfd_arch_s390
2659 #define ELF_MACHINE_CODE EM_S390
2660 #define ELF_MACHINE_ALT1 EM_S390_OLD
2661 #define ELF_MAXPAGESIZE 0x1000
2662
2663 #define elf_backend_can_gc_sections 1
2664 #define elf_backend_can_refcount 1
2665 #define elf_backend_want_got_plt 1
2666 #define elf_backend_plt_readonly 1
2667 #define elf_backend_want_plt_sym 0
2668 #define elf_backend_got_header_size 12
2669 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2670 #define elf_backend_rela_normal 1
2671
2672 #define elf_info_to_howto elf_s390_info_to_howto
2673
2674 #define bfd_elf32_bfd_is_local_label_name elf_s390_is_local_label_name
2675 #define bfd_elf32_bfd_link_hash_table_create elf_s390_link_hash_table_create
2676 #define bfd_elf32_bfd_reloc_type_lookup elf_s390_reloc_type_lookup
2677
2678 #define elf_backend_adjust_dynamic_symbol elf_s390_adjust_dynamic_symbol
2679 #define elf_backend_check_relocs elf_s390_check_relocs
2680 #define elf_backend_copy_indirect_symbol elf_s390_copy_indirect_symbol
2681 #define elf_backend_create_dynamic_sections elf_s390_create_dynamic_sections
2682 #define elf_backend_finish_dynamic_sections elf_s390_finish_dynamic_sections
2683 #define elf_backend_finish_dynamic_symbol elf_s390_finish_dynamic_symbol
2684 #define elf_backend_gc_mark_hook elf_s390_gc_mark_hook
2685 #define elf_backend_gc_sweep_hook elf_s390_gc_sweep_hook
2686 #define elf_backend_reloc_type_class elf_s390_reloc_type_class
2687 #define elf_backend_relocate_section elf_s390_relocate_section
2688 #define elf_backend_size_dynamic_sections elf_s390_size_dynamic_sections
2689 #define elf_backend_reloc_type_class elf_s390_reloc_type_class
2690 #define elf_backend_grok_prstatus elf_s390_grok_prstatus
2691
2692 #define elf_backend_object_p elf_s390_object_p
2693
2694 #include "elf32-target.h"