elf32-ppc.c revision 1.1.1.8 1 /* PowerPC-specific support for 32-bit ELF
2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
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 3 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
19 Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 /* The assembler should generate a full set of section symbols even
23 when they appear unused. The linux kernel build tool recordmcount
24 needs them. */
25 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
26
27 #include "sysdep.h"
28 #include <stdarg.h>
29 #include "bfd.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/ppc.h"
34 #include "elf32-ppc.h"
35 #include "elf-vxworks.h"
36 #include "dwarf2.h"
37 #include "opcode/ppc.h"
38
39 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
40 #define OCTETS_PER_BYTE(ABFD, SEC) 1
41
42 typedef enum split16_format_type
43 {
44 split16a_type = 0,
45 split16d_type
46 }
47 split16_format_type;
48
49 /* RELA relocations are used here. */
50
51 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
53 static bfd_reloc_status_type ppc_elf_unhandled_reloc
54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
55
56 /* Branch prediction bit for branch taken relocs. */
57 #define BRANCH_PREDICT_BIT 0x200000
58 /* Mask to set RA in memory instructions. */
59 #define RA_REGISTER_MASK 0x001f0000
60 /* Value to shift register by to insert RA. */
61 #define RA_REGISTER_SHIFT 16
62
63 /* The name of the dynamic interpreter. This is put in the .interp
64 section. */
65 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
66
67 /* For old-style PLT. */
68 /* The number of single-slot PLT entries (the rest use two slots). */
69 #define PLT_NUM_SINGLE_ENTRIES 8192
70
71 /* For new-style .glink and .plt. */
72 #define GLINK_PLTRESOLVE 16*4
73 #define GLINK_ENTRY_SIZE(htab, h) \
74 ((4*4 \
75 + (h != NULL \
76 && h == htab->tls_get_addr \
77 && !htab->params->no_tls_get_addr_opt ? 8*4 : 0) \
78 + (1u << htab->params->plt_stub_align) - 1) \
79 & -(1u << htab->params->plt_stub_align))
80
81 /* VxWorks uses its own plt layout, filled in by the static linker. */
82
83 /* The standard VxWorks PLT entry. */
84 #define VXWORKS_PLT_ENTRY_SIZE 32
85 static const bfd_vma ppc_elf_vxworks_plt_entry
86 [VXWORKS_PLT_ENTRY_SIZE / 4] =
87 {
88 0x3d800000, /* lis r12,0 */
89 0x818c0000, /* lwz r12,0(r12) */
90 0x7d8903a6, /* mtctr r12 */
91 0x4e800420, /* bctr */
92 0x39600000, /* li r11,0 */
93 0x48000000, /* b 14 <.PLT0resolve+0x4> */
94 0x60000000, /* nop */
95 0x60000000, /* nop */
96 };
97 static const bfd_vma ppc_elf_vxworks_pic_plt_entry
98 [VXWORKS_PLT_ENTRY_SIZE / 4] =
99 {
100 0x3d9e0000, /* addis r12,r30,0 */
101 0x818c0000, /* lwz r12,0(r12) */
102 0x7d8903a6, /* mtctr r12 */
103 0x4e800420, /* bctr */
104 0x39600000, /* li r11,0 */
105 0x48000000, /* b 14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
106 0x60000000, /* nop */
107 0x60000000, /* nop */
108 };
109
110 /* The initial VxWorks PLT entry. */
111 #define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
112 static const bfd_vma ppc_elf_vxworks_plt0_entry
113 [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
114 {
115 0x3d800000, /* lis r12,0 */
116 0x398c0000, /* addi r12,r12,0 */
117 0x800c0008, /* lwz r0,8(r12) */
118 0x7c0903a6, /* mtctr r0 */
119 0x818c0004, /* lwz r12,4(r12) */
120 0x4e800420, /* bctr */
121 0x60000000, /* nop */
122 0x60000000, /* nop */
123 };
124 static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
125 [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
126 {
127 0x819e0008, /* lwz r12,8(r30) */
128 0x7d8903a6, /* mtctr r12 */
129 0x819e0004, /* lwz r12,4(r30) */
130 0x4e800420, /* bctr */
131 0x60000000, /* nop */
132 0x60000000, /* nop */
133 0x60000000, /* nop */
134 0x60000000, /* nop */
135 };
136
137 /* For executables, we have some additional relocations in
138 .rela.plt.unloaded, for the kernel loader. */
139
140 /* The number of non-JMP_SLOT relocations per PLT0 slot. */
141 #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
142 /* The number of relocations in the PLTResolve slot. */
143 #define VXWORKS_PLTRESOLVE_RELOCS 2
144 /* The number of relocations in the PLTResolve slot when creating
145 a shared library. */
146 #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
147
148 /* Some instructions. */
149 #define ADDIS_11_11 0x3d6b0000
150 #define ADDIS_11_30 0x3d7e0000
151 #define ADDIS_12_12 0x3d8c0000
152 #define ADDI_11_11 0x396b0000
153 #define ADD_0_11_11 0x7c0b5a14
154 #define ADD_3_12_2 0x7c6c1214
155 #define ADD_11_0_11 0x7d605a14
156 #define B 0x48000000
157 #define BA 0x48000002
158 #define BCL_20_31 0x429f0005
159 #define BCTR 0x4e800420
160 #define BEQLR 0x4d820020
161 #define CMPWI_11_0 0x2c0b0000
162 #define LIS_11 0x3d600000
163 #define LIS_12 0x3d800000
164 #define LWZU_0_12 0x840c0000
165 #define LWZ_0_12 0x800c0000
166 #define LWZ_11_3 0x81630000
167 #define LWZ_11_11 0x816b0000
168 #define LWZ_11_30 0x817e0000
169 #define LWZ_12_3 0x81830000
170 #define LWZ_12_12 0x818c0000
171 #define MR_0_3 0x7c601b78
172 #define MR_3_0 0x7c030378
173 #define MFLR_0 0x7c0802a6
174 #define MFLR_12 0x7d8802a6
175 #define MTCTR_0 0x7c0903a6
176 #define MTCTR_11 0x7d6903a6
177 #define MTLR_0 0x7c0803a6
178 #define NOP 0x60000000
179 #define SUB_11_11_12 0x7d6c5850
180
181 /* Offset of tp and dtp pointers from start of TLS block. */
182 #define TP_OFFSET 0x7000
183 #define DTP_OFFSET 0x8000
184
185 /* The value of a defined global symbol. */
186 #define SYM_VAL(SYM) \
187 ((SYM)->root.u.def.section->output_section->vma \
188 + (SYM)->root.u.def.section->output_offset \
189 + (SYM)->root.u.def.value)
190
191 /* Relocation HOWTO's. */
193 /* Like other ELF RELA targets that don't apply multiple
194 field-altering relocations to the same localation, src_mask is
195 always zero and pcrel_offset is the same as pc_relative.
196 PowerPC can always use a zero bitpos, even when the field is not at
197 the LSB. For example, a REL24 could use rightshift=2, bisize=24
198 and bitpos=2 which matches the ABI description, or as we do here,
199 rightshift=0, bitsize=26 and bitpos=0. */
200 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
201 complain, special_func) \
202 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
203 complain_overflow_ ## complain, special_func, \
204 #type, false, 0, mask, pc_relative)
205
206 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
207
208 static reloc_howto_type ppc_elf_howto_raw[] = {
209 /* This reloc does nothing. */
210 HOW (R_PPC_NONE, 0, 0, 0, 0, false, dont,
211 bfd_elf_generic_reloc),
212
213 /* A standard 32 bit relocation. */
214 HOW (R_PPC_ADDR32, 4, 32, 0xffffffff, 0, false, dont,
215 bfd_elf_generic_reloc),
216
217 /* An absolute 26 bit branch; the lower two bits must be zero.
218 FIXME: we don't check that, we just clear them. */
219 HOW (R_PPC_ADDR24, 4, 26, 0x3fffffc, 0, false, signed,
220 bfd_elf_generic_reloc),
221
222 /* A standard 16 bit relocation. */
223 HOW (R_PPC_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
224 bfd_elf_generic_reloc),
225
226 /* A 16 bit relocation without overflow. */
227 HOW (R_PPC_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
228 bfd_elf_generic_reloc),
229
230 /* The high order 16 bits of an address. */
231 HOW (R_PPC_ADDR16_HI, 2, 16, 0xffff, 16, false, dont,
232 bfd_elf_generic_reloc),
233
234 /* The high order 16 bits of an address, plus 1 if the contents of
235 the low 16 bits, treated as a signed number, is negative. */
236 HOW (R_PPC_ADDR16_HA, 2, 16, 0xffff, 16, false, dont,
237 ppc_elf_addr16_ha_reloc),
238
239 /* An absolute 16 bit branch; the lower two bits must be zero.
240 FIXME: we don't check that, we just clear them. */
241 HOW (R_PPC_ADDR14, 4, 16, 0xfffc, 0, false, signed,
242 bfd_elf_generic_reloc),
243
244 /* An absolute 16 bit branch, for which bit 10 should be set to
245 indicate that the branch is expected to be taken. The lower two
246 bits must be zero. */
247 HOW (R_PPC_ADDR14_BRTAKEN, 4, 16, 0xfffc, 0, false, signed,
248 bfd_elf_generic_reloc),
249
250 /* An absolute 16 bit branch, for which bit 10 should be set to
251 indicate that the branch is not expected to be taken. The lower
252 two bits must be zero. */
253 HOW (R_PPC_ADDR14_BRNTAKEN, 4, 16, 0xfffc, 0, false, signed,
254 bfd_elf_generic_reloc),
255
256 /* A relative 26 bit branch; the lower two bits must be zero. */
257 HOW (R_PPC_REL24, 4, 26, 0x3fffffc, 0, true, signed,
258 bfd_elf_generic_reloc),
259
260 /* A relative 16 bit branch; the lower two bits must be zero. */
261 HOW (R_PPC_REL14, 4, 16, 0xfffc, 0, true, signed,
262 bfd_elf_generic_reloc),
263
264 /* A relative 16 bit branch. Bit 10 should be set to indicate that
265 the branch is expected to be taken. The lower two bits must be
266 zero. */
267 HOW (R_PPC_REL14_BRTAKEN, 4, 16, 0xfffc, 0, true, signed,
268 bfd_elf_generic_reloc),
269
270 /* A relative 16 bit branch. Bit 10 should be set to indicate that
271 the branch is not expected to be taken. The lower two bits must
272 be zero. */
273 HOW (R_PPC_REL14_BRNTAKEN, 4, 16, 0xfffc, 0, true, signed,
274 bfd_elf_generic_reloc),
275
276 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
277 symbol. */
278 HOW (R_PPC_GOT16, 2, 16, 0xffff, 0, false, signed,
279 ppc_elf_unhandled_reloc),
280
281 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
282 the symbol. */
283 HOW (R_PPC_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
284 ppc_elf_unhandled_reloc),
285
286 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
287 the symbol. */
288 HOW (R_PPC_GOT16_HI, 2, 16, 0xffff, 16, false, dont,
289 ppc_elf_unhandled_reloc),
290
291 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
292 the symbol. */
293 HOW (R_PPC_GOT16_HA, 2, 16, 0xffff, 16, false, dont,
294 ppc_elf_unhandled_reloc),
295
296 /* Like R_PPC_REL24, but referring to the procedure linkage table
297 entry for the symbol. */
298 HOW (R_PPC_PLTREL24, 4, 26, 0x3fffffc, 0, true, signed,
299 ppc_elf_unhandled_reloc),
300
301 /* This is used only by the dynamic linker. The symbol should exist
302 both in the object being run and in some shared library. The
303 dynamic linker copies the data addressed by the symbol from the
304 shared library into the object, because the object being
305 run has to have the data at some particular address. */
306 HOW (R_PPC_COPY, 4, 32, 0, 0, false, dont,
307 ppc_elf_unhandled_reloc),
308
309 /* Like R_PPC_ADDR32, but used when setting global offset table
310 entries. */
311 HOW (R_PPC_GLOB_DAT, 4, 32, 0xffffffff, 0, false, dont,
312 ppc_elf_unhandled_reloc),
313
314 /* Marks a procedure linkage table entry for a symbol. */
315 HOW (R_PPC_JMP_SLOT, 4, 32, 0, 0, false, dont,
316 ppc_elf_unhandled_reloc),
317
318 /* Used only by the dynamic linker. When the object is run, this
319 longword is set to the load address of the object, plus the
320 addend. */
321 HOW (R_PPC_RELATIVE, 4, 32, 0xffffffff, 0, false, dont,
322 bfd_elf_generic_reloc),
323
324 /* Like R_PPC_REL24, but uses the value of the symbol within the
325 object rather than the final value. Normally used for
326 _GLOBAL_OFFSET_TABLE_. */
327 HOW (R_PPC_LOCAL24PC, 4, 26, 0x3fffffc, 0, true, signed,
328 bfd_elf_generic_reloc),
329
330 /* Like R_PPC_ADDR32, but may be unaligned. */
331 HOW (R_PPC_UADDR32, 4, 32, 0xffffffff, 0, false, dont,
332 bfd_elf_generic_reloc),
333
334 /* Like R_PPC_ADDR16, but may be unaligned. */
335 HOW (R_PPC_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
336 bfd_elf_generic_reloc),
337
338 /* 32-bit PC relative */
339 HOW (R_PPC_REL32, 4, 32, 0xffffffff, 0, true, dont,
340 bfd_elf_generic_reloc),
341
342 /* 32-bit relocation to the symbol's procedure linkage table.
343 FIXME: not supported. */
344 HOW (R_PPC_PLT32, 4, 32, 0, 0, false, dont,
345 ppc_elf_unhandled_reloc),
346
347 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
348 FIXME: not supported. */
349 HOW (R_PPC_PLTREL32, 4, 32, 0, 0, true, dont,
350 ppc_elf_unhandled_reloc),
351
352 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
353 the symbol. */
354 HOW (R_PPC_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
355 ppc_elf_unhandled_reloc),
356
357 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
358 the symbol. */
359 HOW (R_PPC_PLT16_HI, 2, 16, 0xffff, 16, false, dont,
360 ppc_elf_unhandled_reloc),
361
362 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
363 the symbol. */
364 HOW (R_PPC_PLT16_HA, 2, 16, 0xffff, 16, false, dont,
365 ppc_elf_unhandled_reloc),
366
367 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
368 small data items. */
369 HOW (R_PPC_SDAREL16, 2, 16, 0xffff, 0, false, signed,
370 ppc_elf_unhandled_reloc),
371
372 /* 16-bit section relative relocation. */
373 HOW (R_PPC_SECTOFF, 2, 16, 0xffff, 0, false, signed,
374 ppc_elf_unhandled_reloc),
375
376 /* 16-bit lower half section relative relocation. */
377 HOW (R_PPC_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
378 ppc_elf_unhandled_reloc),
379
380 /* 16-bit upper half section relative relocation. */
381 HOW (R_PPC_SECTOFF_HI, 2, 16, 0xffff, 16, false, dont,
382 ppc_elf_unhandled_reloc),
383
384 /* 16-bit upper half adjusted section relative relocation. */
385 HOW (R_PPC_SECTOFF_HA, 2, 16, 0xffff, 16, false, dont,
386 ppc_elf_unhandled_reloc),
387
388 /* Marker relocs for TLS. */
389 HOW (R_PPC_TLS, 4, 32, 0, 0, false, dont,
390 bfd_elf_generic_reloc),
391
392 HOW (R_PPC_TLSGD, 4, 32, 0, 0, false, dont,
393 bfd_elf_generic_reloc),
394
395 HOW (R_PPC_TLSLD, 4, 32, 0, 0, false, dont,
396 bfd_elf_generic_reloc),
397
398 /* Marker relocs on inline plt call instructions. */
399 HOW (R_PPC_PLTSEQ, 4, 32, 0, 0, false, dont,
400 bfd_elf_generic_reloc),
401
402 HOW (R_PPC_PLTCALL, 4, 32, 0, 0, false, dont,
403 bfd_elf_generic_reloc),
404
405 /* Computes the load module index of the load module that contains the
406 definition of its TLS sym. */
407 HOW (R_PPC_DTPMOD32, 4, 32, 0xffffffff, 0, false, dont,
408 ppc_elf_unhandled_reloc),
409
410 /* Computes a dtv-relative displacement, the difference between the value
411 of sym+add and the base address of the thread-local storage block that
412 contains the definition of sym, minus 0x8000. */
413 HOW (R_PPC_DTPREL32, 4, 32, 0xffffffff, 0, false, dont,
414 ppc_elf_unhandled_reloc),
415
416 /* A 16 bit dtprel reloc. */
417 HOW (R_PPC_DTPREL16, 2, 16, 0xffff, 0, false, signed,
418 ppc_elf_unhandled_reloc),
419
420 /* Like DTPREL16, but no overflow. */
421 HOW (R_PPC_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
422 ppc_elf_unhandled_reloc),
423
424 /* Like DTPREL16_LO, but next higher group of 16 bits. */
425 HOW (R_PPC_DTPREL16_HI, 2, 16, 0xffff, 16, false, dont,
426 ppc_elf_unhandled_reloc),
427
428 /* Like DTPREL16_HI, but adjust for low 16 bits. */
429 HOW (R_PPC_DTPREL16_HA, 2, 16, 0xffff, 16, false, dont,
430 ppc_elf_unhandled_reloc),
431
432 /* Computes a tp-relative displacement, the difference between the value of
433 sym+add and the value of the thread pointer (r13). */
434 HOW (R_PPC_TPREL32, 4, 32, 0xffffffff, 0, false, dont,
435 ppc_elf_unhandled_reloc),
436
437 /* A 16 bit tprel reloc. */
438 HOW (R_PPC_TPREL16, 2, 16, 0xffff, 0, false, signed,
439 ppc_elf_unhandled_reloc),
440
441 /* Like TPREL16, but no overflow. */
442 HOW (R_PPC_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
443 ppc_elf_unhandled_reloc),
444
445 /* Like TPREL16_LO, but next higher group of 16 bits. */
446 HOW (R_PPC_TPREL16_HI, 2, 16, 0xffff, 16, false, dont,
447 ppc_elf_unhandled_reloc),
448
449 /* Like TPREL16_HI, but adjust for low 16 bits. */
450 HOW (R_PPC_TPREL16_HA, 2, 16, 0xffff, 16, false, dont,
451 ppc_elf_unhandled_reloc),
452
453 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
454 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
455 to the first entry. */
456 HOW (R_PPC_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
457 ppc_elf_unhandled_reloc),
458
459 /* Like GOT_TLSGD16, but no overflow. */
460 HOW (R_PPC_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
461 ppc_elf_unhandled_reloc),
462
463 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
464 HOW (R_PPC_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, dont,
465 ppc_elf_unhandled_reloc),
466
467 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
468 HOW (R_PPC_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, dont,
469 ppc_elf_unhandled_reloc),
470
471 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
472 with values (sym+add)@dtpmod and zero, and computes the offset to the
473 first entry. */
474 HOW (R_PPC_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
475 ppc_elf_unhandled_reloc),
476
477 /* Like GOT_TLSLD16, but no overflow. */
478 HOW (R_PPC_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
479 ppc_elf_unhandled_reloc),
480
481 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
482 HOW (R_PPC_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, dont,
483 ppc_elf_unhandled_reloc),
484
485 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
486 HOW (R_PPC_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, dont,
487 ppc_elf_unhandled_reloc),
488
489 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
490 the offset to the entry. */
491 HOW (R_PPC_GOT_DTPREL16, 2, 16, 0xffff, 0, false, signed,
492 ppc_elf_unhandled_reloc),
493
494 /* Like GOT_DTPREL16, but no overflow. */
495 HOW (R_PPC_GOT_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
496 ppc_elf_unhandled_reloc),
497
498 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
499 HOW (R_PPC_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, dont,
500 ppc_elf_unhandled_reloc),
501
502 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
503 HOW (R_PPC_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, dont,
504 ppc_elf_unhandled_reloc),
505
506 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
507 offset to the entry. */
508 HOW (R_PPC_GOT_TPREL16, 2, 16, 0xffff, 0, false, signed,
509 ppc_elf_unhandled_reloc),
510
511 /* Like GOT_TPREL16, but no overflow. */
512 HOW (R_PPC_GOT_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
513 ppc_elf_unhandled_reloc),
514
515 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
516 HOW (R_PPC_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, dont,
517 ppc_elf_unhandled_reloc),
518
519 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
520 HOW (R_PPC_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, dont,
521 ppc_elf_unhandled_reloc),
522
523 /* The remaining relocs are from the Embedded ELF ABI, and are not
524 in the SVR4 ELF ABI. */
525
526 /* 32 bit value resulting from the addend minus the symbol. */
527 HOW (R_PPC_EMB_NADDR32, 4, 32, 0xffffffff, 0, false, dont,
528 ppc_elf_unhandled_reloc),
529
530 /* 16 bit value resulting from the addend minus the symbol. */
531 HOW (R_PPC_EMB_NADDR16, 2, 16, 0xffff, 0, false, signed,
532 ppc_elf_unhandled_reloc),
533
534 /* 16 bit value resulting from the addend minus the symbol. */
535 HOW (R_PPC_EMB_NADDR16_LO, 2, 16, 0xffff, 0, false, dont,
536 ppc_elf_unhandled_reloc),
537
538 /* The high order 16 bits of the addend minus the symbol. */
539 HOW (R_PPC_EMB_NADDR16_HI, 2, 16, 0xffff, 16, false, dont,
540 ppc_elf_unhandled_reloc),
541
542 /* The high order 16 bits of the result of the addend minus the address,
543 plus 1 if the contents of the low 16 bits, treated as a signed number,
544 is negative. */
545 HOW (R_PPC_EMB_NADDR16_HA, 2, 16, 0xffff, 16, false, dont,
546 ppc_elf_unhandled_reloc),
547
548 /* 16 bit value resulting from allocating a 4 byte word to hold an
549 address in the .sdata section, and returning the offset from
550 _SDA_BASE_ for that relocation. */
551 HOW (R_PPC_EMB_SDAI16, 2, 16, 0xffff, 0, false, signed,
552 ppc_elf_unhandled_reloc),
553
554 /* 16 bit value resulting from allocating a 4 byte word to hold an
555 address in the .sdata2 section, and returning the offset from
556 _SDA2_BASE_ for that relocation. */
557 HOW (R_PPC_EMB_SDA2I16, 2, 16, 0xffff, 0, false, signed,
558 ppc_elf_unhandled_reloc),
559
560 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
561 small data items. */
562 HOW (R_PPC_EMB_SDA2REL, 2, 16, 0xffff, 0, false, signed,
563 ppc_elf_unhandled_reloc),
564
565 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
566 signed offset from the appropriate base, and filling in the register
567 field with the appropriate register (0, 2, or 13). */
568 HOW (R_PPC_EMB_SDA21, 4, 16, 0xffff, 0, false, signed,
569 ppc_elf_unhandled_reloc),
570
571 /* Relocation not handled: R_PPC_EMB_MRKREF */
572 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
573 /* Relocation not handled: R_PPC_EMB_RELST_LO */
574 /* Relocation not handled: R_PPC_EMB_RELST_HI */
575 /* Relocation not handled: R_PPC_EMB_RELST_HA */
576 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
577
578 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
579 in the 16 bit signed offset from the appropriate base, and filling in the
580 register field with the appropriate register (0, 2, or 13). */
581 HOW (R_PPC_EMB_RELSDA, 2, 16, 0xffff, 0, false, signed,
582 ppc_elf_unhandled_reloc),
583
584 /* A relative 8 bit branch. */
585 HOW (R_PPC_VLE_REL8, 2, 8, 0xff, 1, true, signed,
586 bfd_elf_generic_reloc),
587
588 /* A relative 15 bit branch. */
589 HOW (R_PPC_VLE_REL15, 4, 16, 0xfffe, 0, true, signed,
590 bfd_elf_generic_reloc),
591
592 /* A relative 24 bit branch. */
593 HOW (R_PPC_VLE_REL24, 4, 25, 0x1fffffe, 0, true, signed,
594 bfd_elf_generic_reloc),
595
596 /* The 16 LSBS in split16a format. */
597 HOW (R_PPC_VLE_LO16A, 4, 16, 0x1f07ff, 0, false, dont,
598 ppc_elf_unhandled_reloc),
599
600 /* The 16 LSBS in split16d format. */
601 HOW (R_PPC_VLE_LO16D, 4, 16, 0x3e007ff, 0, false, dont,
602 ppc_elf_unhandled_reloc),
603
604 /* Bits 16-31 split16a format. */
605 HOW (R_PPC_VLE_HI16A, 4, 16, 0x1f07ff, 16, false, dont,
606 ppc_elf_unhandled_reloc),
607
608 /* Bits 16-31 split16d format. */
609 HOW (R_PPC_VLE_HI16D, 4, 16, 0x3e007ff, 16, false, dont,
610 ppc_elf_unhandled_reloc),
611
612 /* Bits 16-31 (High Adjusted) in split16a format. */
613 HOW (R_PPC_VLE_HA16A, 4, 16, 0x1f07ff, 16, false, dont,
614 ppc_elf_unhandled_reloc),
615
616 /* Bits 16-31 (High Adjusted) in split16d format. */
617 HOW (R_PPC_VLE_HA16D, 4, 16, 0x3e007ff, 16, false, dont,
618 ppc_elf_unhandled_reloc),
619
620 /* This reloc is like R_PPC_EMB_SDA21 but only applies to e_add16i
621 instructions. If the register base is 0 then the linker changes
622 the e_add16i to an e_li instruction. */
623 HOW (R_PPC_VLE_SDA21, 4, 16, 0xffff, 0, false, signed,
624 ppc_elf_unhandled_reloc),
625
626 /* Like R_PPC_VLE_SDA21 but ignore overflow. */
627 HOW (R_PPC_VLE_SDA21_LO, 4, 16, 0xffff, 0, false, dont,
628 ppc_elf_unhandled_reloc),
629
630 /* The 16 LSBS relative to _SDA_BASE_ in split16a format. */
631 HOW (R_PPC_VLE_SDAREL_LO16A, 4, 16, 0x1f07ff, 0, false, dont,
632 ppc_elf_unhandled_reloc),
633
634 /* The 16 LSBS relative to _SDA_BASE_ in split16d format. */
635 HOW (R_PPC_VLE_SDAREL_LO16D, 4, 16, 0x3e007ff, 0, false, dont,
636 ppc_elf_unhandled_reloc),
637
638 /* Bits 16-31 relative to _SDA_BASE_ in split16a format. */
639 HOW (R_PPC_VLE_SDAREL_HI16A, 4, 16, 0x1f07ff, 16, false, dont,
640 ppc_elf_unhandled_reloc),
641
642 /* Bits 16-31 relative to _SDA_BASE_ in split16d format. */
643 HOW (R_PPC_VLE_SDAREL_HI16D, 4, 16, 0x3e007ff, 16, false, dont,
644 ppc_elf_unhandled_reloc),
645
646 /* Bits 16-31 (HA) relative to _SDA_BASE split16a format. */
647 HOW (R_PPC_VLE_SDAREL_HA16A, 4, 16, 0x1f07ff, 16, false, dont,
648 ppc_elf_unhandled_reloc),
649
650 /* Bits 16-31 (HA) relative to _SDA_BASE split16d format. */
651 HOW (R_PPC_VLE_SDAREL_HA16D, 4, 16, 0x3e007ff, 16, false, dont,
652 ppc_elf_unhandled_reloc),
653
654 /* e_li split20 format. */
655 HOW (R_PPC_VLE_ADDR20, 4, 20, 0x1f7fff, 0, false, dont,
656 ppc_elf_unhandled_reloc),
657
658 HOW (R_PPC_IRELATIVE, 4, 32, 0xffffffff, 0, false, dont,
659 ppc_elf_unhandled_reloc),
660
661 /* A 16 bit relative relocation. */
662 HOW (R_PPC_REL16, 2, 16, 0xffff, 0, true, signed,
663 bfd_elf_generic_reloc),
664
665 /* A 16 bit relative relocation without overflow. */
666 HOW (R_PPC_REL16_LO, 2, 16, 0xffff, 0, true, dont,
667 bfd_elf_generic_reloc),
668
669 /* The high order 16 bits of a relative address. */
670 HOW (R_PPC_REL16_HI, 2, 16, 0xffff, 16, true, dont,
671 bfd_elf_generic_reloc),
672
673 /* The high order 16 bits of a relative address, plus 1 if the contents of
674 the low 16 bits, treated as a signed number, is negative. */
675 HOW (R_PPC_REL16_HA, 2, 16, 0xffff, 16, true, dont,
676 ppc_elf_addr16_ha_reloc),
677
678 /* Like R_PPC_REL16_HA but for split field in addpcis. */
679 HOW (R_PPC_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
680 ppc_elf_addr16_ha_reloc),
681
682 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
683 HOW (R_PPC_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
684 ppc_elf_addr16_ha_reloc),
685
686 /* GNU extension to record C++ vtable hierarchy. */
687 HOW (R_PPC_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
688 NULL),
689
690 /* GNU extension to record C++ vtable member usage. */
691 HOW (R_PPC_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
692 NULL),
693
694 /* Phony reloc to handle AIX style TOC entries. */
695 HOW (R_PPC_TOC16, 2, 16, 0xffff, 0, false, signed,
696 ppc_elf_unhandled_reloc),
697 };
698
699 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
701
702 static void
703 ppc_elf_howto_init (void)
704 {
705 unsigned int i, type;
706
707 for (i = 0;
708 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
709 i++)
710 {
711 type = ppc_elf_howto_raw[i].type;
712 if (type >= (sizeof (ppc_elf_howto_table)
713 / sizeof (ppc_elf_howto_table[0])))
714 abort ();
715 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
716 }
717 }
718
719 static reloc_howto_type *
720 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
721 bfd_reloc_code_real_type code)
722 {
723 enum elf_ppc_reloc_type r;
724
725 /* Initialize howto table if not already done. */
726 if (!ppc_elf_howto_table[R_PPC_ADDR32])
727 ppc_elf_howto_init ();
728
729 switch (code)
730 {
731 default:
732 return NULL;
733
734 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
735 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
736 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
737 case BFD_RELOC_PPC64_ADDR16_DS:
738 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
739 case BFD_RELOC_PPC64_ADDR16_LO_DS:
740 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
741 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
742 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
743 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
744 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
745 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
746 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
747 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
748 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
749 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
750 case BFD_RELOC_PPC64_GOT16_DS:
751 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
752 case BFD_RELOC_PPC64_GOT16_LO_DS:
753 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
754 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
755 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
756 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
757 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
758 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
759 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
760 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
761 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
762 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
763 case BFD_RELOC_PPC64_PLT16_LO_DS:
764 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
765 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
766 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
767 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
768 case BFD_RELOC_PPC64_SECTOFF_DS:
769 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
770 case BFD_RELOC_PPC64_SECTOFF_LO_DS:
771 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
772 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
773 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
774 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
775 case BFD_RELOC_PPC64_TOC16_DS:
776 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
777 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
778 case BFD_RELOC_PPC_TLSGD: r = R_PPC_TLSGD; break;
779 case BFD_RELOC_PPC_TLSLD: r = R_PPC_TLSLD; break;
780 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
781 case BFD_RELOC_PPC64_TPREL16_DS:
782 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
783 case BFD_RELOC_PPC64_TPREL16_LO_DS:
784 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
785 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
786 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
787 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
788 case BFD_RELOC_PPC64_DTPREL16_DS:
789 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
790 case BFD_RELOC_PPC64_DTPREL16_LO_DS:
791 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
792 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
793 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
794 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
795 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
796 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
797 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
798 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
799 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
800 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
801 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
802 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
803 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
804 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
805 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
806 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
807 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
808 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
809 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
810 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
811 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
812 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
813 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
814 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
815 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
816 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
817 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
818 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
819 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
820 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
821 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
822 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
823 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
824 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
825 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
826 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
827 case BFD_RELOC_PPC_VLE_REL8: r = R_PPC_VLE_REL8; break;
828 case BFD_RELOC_PPC_VLE_REL15: r = R_PPC_VLE_REL15; break;
829 case BFD_RELOC_PPC_VLE_REL24: r = R_PPC_VLE_REL24; break;
830 case BFD_RELOC_PPC_VLE_LO16A: r = R_PPC_VLE_LO16A; break;
831 case BFD_RELOC_PPC_VLE_LO16D: r = R_PPC_VLE_LO16D; break;
832 case BFD_RELOC_PPC_VLE_HI16A: r = R_PPC_VLE_HI16A; break;
833 case BFD_RELOC_PPC_VLE_HI16D: r = R_PPC_VLE_HI16D; break;
834 case BFD_RELOC_PPC_VLE_HA16A: r = R_PPC_VLE_HA16A; break;
835 case BFD_RELOC_PPC_VLE_HA16D: r = R_PPC_VLE_HA16D; break;
836 case BFD_RELOC_PPC_VLE_SDA21: r = R_PPC_VLE_SDA21; break;
837 case BFD_RELOC_PPC_VLE_SDA21_LO: r = R_PPC_VLE_SDA21_LO; break;
838 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
839 r = R_PPC_VLE_SDAREL_LO16A;
840 break;
841 case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
842 r = R_PPC_VLE_SDAREL_LO16D;
843 break;
844 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
845 r = R_PPC_VLE_SDAREL_HI16A;
846 break;
847 case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
848 r = R_PPC_VLE_SDAREL_HI16D;
849 break;
850 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
851 r = R_PPC_VLE_SDAREL_HA16A;
852 break;
853 case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
854 r = R_PPC_VLE_SDAREL_HA16D;
855 break;
856 case BFD_RELOC_16_PCREL: r = R_PPC_REL16; break;
857 case BFD_RELOC_LO16_PCREL: r = R_PPC_REL16_LO; break;
858 case BFD_RELOC_HI16_PCREL: r = R_PPC_REL16_HI; break;
859 case BFD_RELOC_HI16_S_PCREL: r = R_PPC_REL16_HA; break;
860 case BFD_RELOC_PPC_16DX_HA: r = R_PPC_16DX_HA; break;
861 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC_REL16DX_HA; break;
862 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
863 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
864 }
865
866 return ppc_elf_howto_table[r];
867 };
868
869 static reloc_howto_type *
870 ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
871 const char *r_name)
872 {
873 unsigned int i;
874
875 for (i = 0;
876 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
877 i++)
878 if (ppc_elf_howto_raw[i].name != NULL
879 && strcasecmp (ppc_elf_howto_raw[i].name, r_name) == 0)
880 return &ppc_elf_howto_raw[i];
881
882 return NULL;
883 }
884
885 /* Set the howto pointer for a PowerPC ELF reloc. */
886
887 static bool
888 ppc_elf_info_to_howto (bfd *abfd,
889 arelent *cache_ptr,
890 Elf_Internal_Rela *dst)
891 {
892 unsigned int r_type;
893
894 /* Initialize howto table if not already done. */
895 if (!ppc_elf_howto_table[R_PPC_ADDR32])
896 ppc_elf_howto_init ();
897
898 r_type = ELF32_R_TYPE (dst->r_info);
899 if (r_type >= R_PPC_max)
900 {
901 /* xgettext:c-format */
902 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
903 abfd, r_type);
904 bfd_set_error (bfd_error_bad_value);
905 return false;
906 }
907
908 cache_ptr->howto = ppc_elf_howto_table[r_type];
909
910 /* Just because the above assert didn't trigger doesn't mean that
911 ELF32_R_TYPE (dst->r_info) is necessarily a valid relocation. */
912 if (cache_ptr->howto == NULL)
913 {
914 /* xgettext:c-format */
915 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
916 abfd, r_type);
917 bfd_set_error (bfd_error_bad_value);
918
919 return false;
920 }
921
922 return true;
923 }
924
925 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs. */
926
927 static bfd_reloc_status_type
928 ppc_elf_addr16_ha_reloc (bfd *abfd,
929 arelent *reloc_entry,
930 asymbol *symbol,
931 void *data,
932 asection *input_section,
933 bfd *output_bfd,
934 char **error_message ATTRIBUTE_UNUSED)
935 {
936 enum elf_ppc_reloc_type r_type;
937 long insn;
938 bfd_size_type octets;
939 bfd_vma value;
940
941 if (output_bfd != NULL)
942 {
943 reloc_entry->address += input_section->output_offset;
944 return bfd_reloc_ok;
945 }
946
947 reloc_entry->addend += 0x8000;
948 r_type = reloc_entry->howto->type;
949 if (r_type != R_PPC_REL16DX_HA)
950 return bfd_reloc_continue;
951
952 value = 0;
953 if (!bfd_is_com_section (symbol->section))
954 value = symbol->value;
955 value += (reloc_entry->addend
956 + symbol->section->output_offset
957 + symbol->section->output_section->vma);
958 value -= (reloc_entry->address
959 + input_section->output_offset
960 + input_section->output_section->vma);
961 value >>= 16;
962
963 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
964 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
965 input_section, octets))
966 return bfd_reloc_outofrange;
967
968 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
969 insn &= ~0x1fffc1;
970 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
971 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
972 return bfd_reloc_ok;
973 }
974
975 static bfd_reloc_status_type
976 ppc_elf_unhandled_reloc (bfd *abfd,
977 arelent *reloc_entry,
978 asymbol *symbol,
979 void *data,
980 asection *input_section,
981 bfd *output_bfd,
982 char **error_message)
983 {
984 /* If this is a relocatable link (output_bfd test tells us), just
985 call the generic function. Any adjustment will be done at final
986 link time. */
987 if (output_bfd != NULL)
988 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
989 input_section, output_bfd, error_message);
990
991 if (error_message != NULL)
992 {
993 static char *message;
994 free (message);
995 if (asprintf (&message, _("generic linker can't handle %s"),
996 reloc_entry->howto->name) < 0)
997 message = NULL;
998 *error_message = message;
999 }
1000 return bfd_reloc_dangerous;
1001 }
1002
1003 /* Sections created by the linker. */
1005
1006 typedef struct elf_linker_section
1007 {
1008 /* Pointer to the bfd section. */
1009 asection *section;
1010 /* Section name. */
1011 const char *name;
1012 /* Associated bss section name. */
1013 const char *bss_name;
1014 /* Associated symbol name. */
1015 const char *sym_name;
1016 /* Associated symbol. */
1017 struct elf_link_hash_entry *sym;
1018 } elf_linker_section_t;
1019
1020 /* Linked list of allocated pointer entries. This hangs off of the
1021 symbol lists, and provides allows us to return different pointers,
1022 based on different addend's. */
1023
1024 typedef struct elf_linker_section_pointers
1025 {
1026 /* next allocated pointer for this symbol */
1027 struct elf_linker_section_pointers *next;
1028 /* offset of pointer from beginning of section */
1029 bfd_vma offset;
1030 /* addend used */
1031 bfd_vma addend;
1032 /* which linker section this is */
1033 elf_linker_section_t *lsect;
1034 } elf_linker_section_pointers_t;
1035
1036 struct ppc_elf_obj_tdata
1037 {
1038 struct elf_obj_tdata elf;
1039
1040 /* A mapping from local symbols to offsets into the various linker
1041 sections added. This is index by the symbol index. */
1042 elf_linker_section_pointers_t **linker_section_pointers;
1043
1044 /* Flags used to auto-detect plt type. */
1045 unsigned int makes_plt_call : 1;
1046 unsigned int has_rel16 : 1;
1047 };
1048
1049 #define ppc_elf_tdata(bfd) \
1050 ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1051
1052 #define elf_local_ptr_offsets(bfd) \
1053 (ppc_elf_tdata (bfd)->linker_section_pointers)
1054
1055 #define is_ppc_elf(bfd) \
1056 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1057 && elf_object_id (bfd) == PPC32_ELF_DATA)
1058
1059 /* Override the generic function because we store some extras. */
1060
1061 static bool
1062 ppc_elf_mkobject (bfd *abfd)
1063 {
1064 return bfd_elf_allocate_object (abfd, sizeof (struct ppc_elf_obj_tdata),
1065 PPC32_ELF_DATA);
1066 }
1067
1068 /* When defaulting arch/mach, decode apuinfo to find a better match. */
1069
1070 bool
1071 _bfd_elf_ppc_set_arch (bfd *abfd)
1072 {
1073 unsigned long mach = 0;
1074 asection *s;
1075 unsigned char *contents;
1076
1077 if (abfd->arch_info->bits_per_word == 32
1078 && bfd_big_endian (abfd))
1079 {
1080
1081 for (s = abfd->sections; s != NULL; s = s->next)
1082 if ((elf_section_data (s)->this_hdr.sh_flags & SHF_PPC_VLE) != 0)
1083 break;
1084 if (s != NULL)
1085 mach = bfd_mach_ppc_vle;
1086 }
1087
1088 if (mach == 0)
1089 {
1090 s = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1091 if (s != NULL
1092 && s->size >= 24
1093 && bfd_malloc_and_get_section (abfd, s, &contents))
1094 {
1095 unsigned int apuinfo_size = bfd_get_32 (abfd, contents + 4);
1096 unsigned int i;
1097
1098 for (i = 20; i < apuinfo_size + 20 && i + 4 <= s->size; i += 4)
1099 {
1100 unsigned int val = bfd_get_32 (abfd, contents + i);
1101 switch (val >> 16)
1102 {
1103 case PPC_APUINFO_PMR:
1104 case PPC_APUINFO_RFMCI:
1105 if (mach == 0)
1106 mach = bfd_mach_ppc_titan;
1107 break;
1108
1109 case PPC_APUINFO_ISEL:
1110 case PPC_APUINFO_CACHELCK:
1111 if (mach == bfd_mach_ppc_titan)
1112 mach = bfd_mach_ppc_e500mc;
1113 break;
1114
1115 case PPC_APUINFO_SPE:
1116 case PPC_APUINFO_EFS:
1117 case PPC_APUINFO_BRLOCK:
1118 if (mach != bfd_mach_ppc_vle)
1119 mach = bfd_mach_ppc_e500;
1120 break;
1121
1122 case PPC_APUINFO_VLE:
1123 mach = bfd_mach_ppc_vle;
1124 break;
1125
1126 default:
1127 mach = -1ul;
1128 }
1129 }
1130 free (contents);
1131 }
1132 }
1133
1134 if (mach != 0 && mach != -1ul)
1135 {
1136 const bfd_arch_info_type *arch;
1137
1138 for (arch = abfd->arch_info->next; arch; arch = arch->next)
1139 if (arch->mach == mach)
1140 {
1141 abfd->arch_info = arch;
1142 break;
1143 }
1144 }
1145 return true;
1146 }
1147
1148 /* Fix bad default arch selected for a 32 bit input bfd when the
1149 default is 64 bit. Also select arch based on apuinfo. */
1150
1151 static bool
1152 ppc_elf_object_p (bfd *abfd)
1153 {
1154 if (!abfd->arch_info->the_default)
1155 return true;
1156
1157 if (abfd->arch_info->bits_per_word == 64)
1158 {
1159 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1160
1161 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1162 {
1163 /* Relies on arch after 64 bit default being 32 bit default. */
1164 abfd->arch_info = abfd->arch_info->next;
1165 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1166 }
1167 }
1168 return _bfd_elf_ppc_set_arch (abfd);
1169 }
1170
1171 /* Function to set whether a module needs the -mrelocatable bit set. */
1172
1173 static bool
1174 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1175 {
1176 BFD_ASSERT (!elf_flags_init (abfd)
1177 || elf_elfheader (abfd)->e_flags == flags);
1178
1179 elf_elfheader (abfd)->e_flags = flags;
1180 elf_flags_init (abfd) = true;
1181 return true;
1182 }
1183
1184 /* Support for core dump NOTE sections. */
1185
1186 static bool
1187 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1188 {
1189 int offset;
1190 unsigned int size;
1191
1192 switch (note->descsz)
1193 {
1194 default:
1195 return false;
1196
1197 case 268: /* Linux/PPC. */
1198 /* pr_cursig */
1199 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1200
1201 /* pr_pid */
1202 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
1203
1204 /* pr_reg */
1205 offset = 72;
1206 size = 192;
1207
1208 break;
1209 }
1210
1211 /* Make a ".reg/999" section. */
1212 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1213 size, note->descpos + offset);
1214 }
1215
1216 static bool
1217 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1218 {
1219 switch (note->descsz)
1220 {
1221 default:
1222 return false;
1223
1224 case 128: /* Linux/PPC elf_prpsinfo. */
1225 elf_tdata (abfd)->core->pid
1226 = bfd_get_32 (abfd, note->descdata + 16);
1227 elf_tdata (abfd)->core->program
1228 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1229 elf_tdata (abfd)->core->command
1230 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1231 }
1232
1233 /* Note that for some reason, a spurious space is tacked
1234 onto the end of the args in some (at least one anyway)
1235 implementations, so strip it off if it exists. */
1236
1237 {
1238 char *command = elf_tdata (abfd)->core->command;
1239 int n = strlen (command);
1240
1241 if (0 < n && command[n - 1] == ' ')
1242 command[n - 1] = '\0';
1243 }
1244
1245 return true;
1246 }
1247
1248 static char *
1249 ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
1250 {
1251 switch (note_type)
1252 {
1253 default:
1254 return NULL;
1255
1256 case NT_PRPSINFO:
1257 {
1258 char data[128] ATTRIBUTE_NONSTRING;
1259 va_list ap;
1260
1261 va_start (ap, note_type);
1262 memset (data, 0, sizeof (data));
1263 strncpy (data + 32, va_arg (ap, const char *), 16);
1264 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1265 DIAGNOSTIC_PUSH;
1266 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1267 -Wstringop-truncation:
1268 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1269 */
1270 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1271 #endif
1272 strncpy (data + 48, va_arg (ap, const char *), 80);
1273 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1274 DIAGNOSTIC_POP;
1275 #endif
1276 va_end (ap);
1277 return elfcore_write_note (abfd, buf, bufsiz,
1278 "CORE", note_type, data, sizeof (data));
1279 }
1280
1281 case NT_PRSTATUS:
1282 {
1283 char data[268];
1284 va_list ap;
1285 long pid;
1286 int cursig;
1287 const void *greg;
1288
1289 va_start (ap, note_type);
1290 memset (data, 0, 72);
1291 pid = va_arg (ap, long);
1292 bfd_put_32 (abfd, pid, data + 24);
1293 cursig = va_arg (ap, int);
1294 bfd_put_16 (abfd, cursig, data + 12);
1295 greg = va_arg (ap, const void *);
1296 memcpy (data + 72, greg, 192);
1297 memset (data + 264, 0, 4);
1298 va_end (ap);
1299 return elfcore_write_note (abfd, buf, bufsiz,
1300 "CORE", note_type, data, sizeof (data));
1301 }
1302 }
1303 }
1304
1305 static flagword
1306 ppc_elf_lookup_section_flags (char *flag_name)
1307 {
1308
1309 if (!strcmp (flag_name, "SHF_PPC_VLE"))
1310 return SHF_PPC_VLE;
1311
1312 return 0;
1313 }
1314
1315 /* Return address for Ith PLT stub in section PLT, for relocation REL
1316 or (bfd_vma) -1 if it should not be included. */
1317
1318 static bfd_vma
1319 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1320 const asection *plt ATTRIBUTE_UNUSED,
1321 const arelent *rel)
1322 {
1323 return rel->address;
1324 }
1325
1326 /* Handle a PowerPC specific section when reading an object file. This
1327 is called when bfd_section_from_shdr finds a section with an unknown
1328 type. */
1329
1330 static bool
1331 ppc_elf_section_from_shdr (bfd *abfd,
1332 Elf_Internal_Shdr *hdr,
1333 const char *name,
1334 int shindex)
1335 {
1336 asection *newsect;
1337 flagword flags;
1338
1339 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1340 return false;
1341
1342 newsect = hdr->bfd_section;
1343 flags = 0;
1344 if (hdr->sh_flags & SHF_EXCLUDE)
1345 flags |= SEC_EXCLUDE;
1346
1347 if (hdr->sh_type == SHT_ORDERED)
1348 flags |= SEC_SORT_ENTRIES;
1349
1350 if (startswith (name, ".PPC.EMB"))
1351 name += 8;
1352 if (startswith (name, ".sbss")
1353 || startswith (name, ".sdata"))
1354 flags |= SEC_SMALL_DATA;
1355
1356 return (flags == 0
1357 || bfd_set_section_flags (newsect, newsect->flags | flags));
1358 }
1359
1360 /* Set up any other section flags and such that may be necessary. */
1361
1362 static bool
1363 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1364 Elf_Internal_Shdr *shdr,
1365 asection *asect)
1366 {
1367 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1368 shdr->sh_type = SHT_ORDERED;
1369
1370 return true;
1371 }
1372
1373 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
1374 need to bump up the number of section headers. */
1375
1376 static int
1377 ppc_elf_additional_program_headers (bfd *abfd,
1378 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1379 {
1380 asection *s;
1381 int ret = 0;
1382
1383 s = bfd_get_section_by_name (abfd, ".sbss2");
1384 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1385 ++ret;
1386
1387 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1388 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1389 ++ret;
1390
1391 return ret;
1392 }
1393
1394 /* Modify the segment map for VLE executables. */
1395
1396 bool
1397 ppc_elf_modify_segment_map (bfd *abfd,
1398 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1399 {
1400 struct elf_segment_map *m;
1401
1402 /* At this point in the link, output sections have already been sorted by
1403 LMA and assigned to segments. All that is left to do is to ensure
1404 there is no mixing of VLE & non-VLE sections in a text segment.
1405 If we find that case, we split the segment.
1406 We maintain the original output section order. */
1407
1408 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
1409 {
1410 struct elf_segment_map *n;
1411 size_t amt;
1412 unsigned int j, k;
1413 unsigned int p_flags;
1414
1415 if (m->p_type != PT_LOAD || m->count == 0)
1416 continue;
1417
1418 for (p_flags = PF_R, j = 0; j != m->count; ++j)
1419 {
1420 if ((m->sections[j]->flags & SEC_READONLY) == 0)
1421 p_flags |= PF_W;
1422 if ((m->sections[j]->flags & SEC_CODE) != 0)
1423 {
1424 p_flags |= PF_X;
1425 if ((elf_section_flags (m->sections[j]) & SHF_PPC_VLE) != 0)
1426 p_flags |= PF_PPC_VLE;
1427 break;
1428 }
1429 }
1430 if (j != m->count)
1431 while (++j != m->count)
1432 {
1433 unsigned int p_flags1 = PF_R;
1434
1435 if ((m->sections[j]->flags & SEC_READONLY) == 0)
1436 p_flags1 |= PF_W;
1437 if ((m->sections[j]->flags & SEC_CODE) != 0)
1438 {
1439 p_flags1 |= PF_X;
1440 if ((elf_section_flags (m->sections[j]) & SHF_PPC_VLE) != 0)
1441 p_flags1 |= PF_PPC_VLE;
1442 if (((p_flags1 ^ p_flags) & PF_PPC_VLE) != 0)
1443 break;
1444 }
1445 p_flags |= p_flags1;
1446 }
1447 /* If we're splitting a segment which originally contained rw
1448 sections then those sections might now only be in one of the
1449 two parts. So always set p_flags if splitting, even if we
1450 are being called for objcopy with p_flags_valid set. */
1451 if (j != m->count || !m->p_flags_valid)
1452 {
1453 m->p_flags_valid = 1;
1454 m->p_flags = p_flags;
1455 }
1456 if (j == m->count)
1457 continue;
1458
1459 /* Sections 0..j-1 stay in this (current) segment,
1460 the remainder are put in a new segment.
1461 The scan resumes with the new segment. */
1462
1463 amt = sizeof (struct elf_segment_map);
1464 amt += (m->count - j - 1) * sizeof (asection *);
1465 n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
1466 if (n == NULL)
1467 return false;
1468
1469 n->p_type = PT_LOAD;
1470 n->count = m->count - j;
1471 for (k = 0; k < n->count; ++k)
1472 n->sections[k] = m->sections[j + k];
1473 m->count = j;
1474 m->p_size_valid = 0;
1475 n->next = m->next;
1476 m->next = n;
1477 }
1478
1479 return true;
1480 }
1481
1482 /* Add extra PPC sections -- Note, for now, make .sbss2 and
1483 .PPC.EMB.sbss0 a normal section, and not a bss section so
1484 that the linker doesn't crater when trying to make more than
1485 2 sections. */
1486
1487 static const struct bfd_elf_special_section ppc_elf_special_sections[] =
1488 {
1489 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
1490 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1491 { STRING_COMMA_LEN (".sbss2"), -2, SHT_PROGBITS, SHF_ALLOC },
1492 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1493 { STRING_COMMA_LEN (".sdata2"), -2, SHT_PROGBITS, SHF_ALLOC },
1494 { STRING_COMMA_LEN (".tags"), 0, SHT_ORDERED, SHF_ALLOC },
1495 { STRING_COMMA_LEN (APUINFO_SECTION_NAME), 0, SHT_NOTE, 0 },
1496 { STRING_COMMA_LEN (".PPC.EMB.sbss0"), 0, SHT_PROGBITS, SHF_ALLOC },
1497 { STRING_COMMA_LEN (".PPC.EMB.sdata0"), 0, SHT_PROGBITS, SHF_ALLOC },
1498 { NULL, 0, 0, 0, 0 }
1499 };
1500
1501 /* This is what we want for new plt/got. */
1502 static const struct bfd_elf_special_section ppc_alt_plt =
1503 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC };
1504
1505 static const struct bfd_elf_special_section *
1506 ppc_elf_get_sec_type_attr (bfd *abfd, asection *sec)
1507 {
1508 const struct bfd_elf_special_section *ssect;
1509
1510 /* See if this is one of the special sections. */
1511 if (sec->name == NULL)
1512 return NULL;
1513
1514 ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
1515 sec->use_rela_p);
1516 if (ssect != NULL)
1517 {
1518 if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
1519 ssect = &ppc_alt_plt;
1520 return ssect;
1521 }
1522
1523 return _bfd_elf_get_sec_type_attr (abfd, sec);
1524 }
1525
1526 /* Very simple linked list structure for recording apuinfo values. */
1528 typedef struct apuinfo_list
1529 {
1530 struct apuinfo_list *next;
1531 unsigned long value;
1532 }
1533 apuinfo_list;
1534
1535 static apuinfo_list *head;
1536 static bool apuinfo_set;
1537
1538 static void
1539 apuinfo_list_init (void)
1540 {
1541 head = NULL;
1542 apuinfo_set = false;
1543 }
1544
1545 static void
1546 apuinfo_list_add (unsigned long value)
1547 {
1548 apuinfo_list *entry = head;
1549
1550 while (entry != NULL)
1551 {
1552 if (entry->value == value)
1553 return;
1554 entry = entry->next;
1555 }
1556
1557 entry = bfd_malloc (sizeof (* entry));
1558 if (entry == NULL)
1559 return;
1560
1561 entry->value = value;
1562 entry->next = head;
1563 head = entry;
1564 }
1565
1566 static unsigned
1567 apuinfo_list_length (void)
1568 {
1569 apuinfo_list *entry;
1570 unsigned long count;
1571
1572 for (entry = head, count = 0;
1573 entry;
1574 entry = entry->next)
1575 ++ count;
1576
1577 return count;
1578 }
1579
1580 static inline unsigned long
1581 apuinfo_list_element (unsigned long number)
1582 {
1583 apuinfo_list * entry;
1584
1585 for (entry = head;
1586 entry && number --;
1587 entry = entry->next)
1588 ;
1589
1590 return entry ? entry->value : 0;
1591 }
1592
1593 static void
1594 apuinfo_list_finish (void)
1595 {
1596 apuinfo_list *entry;
1597
1598 for (entry = head; entry;)
1599 {
1600 apuinfo_list *next = entry->next;
1601 free (entry);
1602 entry = next;
1603 }
1604
1605 head = NULL;
1606 }
1607
1608 /* Scan the input BFDs and create a linked list of
1609 the APUinfo values that will need to be emitted. */
1610
1611 static void
1612 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
1613 {
1614 bfd *ibfd;
1615 asection *asec;
1616 char *buffer = NULL;
1617 bfd_size_type largest_input_size = 0;
1618 unsigned i;
1619 unsigned long length;
1620 const char *error_message = NULL;
1621
1622 if (link_info == NULL)
1623 return;
1624
1625 apuinfo_list_init ();
1626
1627 /* Read in the input sections contents. */
1628 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link.next)
1629 {
1630 unsigned long datum;
1631
1632 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1633 if (asec == NULL)
1634 continue;
1635
1636 /* xgettext:c-format */
1637 error_message = _("corrupt %s section in %pB");
1638 length = asec->size;
1639 if (length < 20)
1640 goto fail;
1641
1642 apuinfo_set = true;
1643 if (largest_input_size < asec->size)
1644 {
1645 free (buffer);
1646 largest_input_size = asec->size;
1647 buffer = bfd_malloc (largest_input_size);
1648 if (!buffer)
1649 return;
1650 }
1651
1652 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
1653 || (bfd_bread (buffer, length, ibfd) != length))
1654 {
1655 /* xgettext:c-format */
1656 error_message = _("unable to read in %s section from %pB");
1657 goto fail;
1658 }
1659
1660 /* Verify the contents of the header. Note - we have to
1661 extract the values this way in order to allow for a
1662 host whose endian-ness is different from the target. */
1663 datum = bfd_get_32 (ibfd, buffer);
1664 if (datum != sizeof APUINFO_LABEL)
1665 goto fail;
1666
1667 datum = bfd_get_32 (ibfd, buffer + 8);
1668 if (datum != 0x2)
1669 goto fail;
1670
1671 if (strcmp (buffer + 12, APUINFO_LABEL) != 0)
1672 goto fail;
1673
1674 /* Get the number of bytes used for apuinfo entries. */
1675 datum = bfd_get_32 (ibfd, buffer + 4);
1676 if (datum + 20 != length)
1677 goto fail;
1678
1679 /* Scan the apuinfo section, building a list of apuinfo numbers. */
1680 for (i = 0; i < datum; i += 4)
1681 apuinfo_list_add (bfd_get_32 (ibfd, buffer + 20 + i));
1682 }
1683
1684 error_message = NULL;
1685
1686 if (apuinfo_set)
1687 {
1688 /* Compute the size of the output section. */
1689 unsigned num_entries = apuinfo_list_length ();
1690
1691 /* Set the output section size, if it exists. */
1692 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1693
1694 if (asec && !bfd_set_section_size (asec, 20 + num_entries * 4))
1695 {
1696 ibfd = abfd;
1697 /* xgettext:c-format */
1698 error_message = _("warning: unable to set size of %s section in %pB");
1699 }
1700 }
1701
1702 fail:
1703 free (buffer);
1704
1705 if (error_message)
1706 _bfd_error_handler (error_message, APUINFO_SECTION_NAME, ibfd);
1707 }
1708
1709 /* Prevent the output section from accumulating the input sections'
1710 contents. We have already stored this in our linked list structure. */
1711
1712 static bool
1713 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
1714 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
1715 asection *asec,
1716 bfd_byte *contents ATTRIBUTE_UNUSED)
1717 {
1718 return apuinfo_set && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
1719 }
1720
1721 /* Finally we can generate the output section. */
1722
1723 static void
1724 ppc_final_write_processing (bfd *abfd)
1725 {
1726 bfd_byte *buffer;
1727 asection *asec;
1728 unsigned i;
1729 unsigned num_entries;
1730 bfd_size_type length;
1731
1732 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1733 if (asec == NULL)
1734 return;
1735
1736 if (!apuinfo_set)
1737 return;
1738
1739 length = asec->size;
1740 if (length < 20)
1741 return;
1742
1743 buffer = bfd_malloc (length);
1744 if (buffer == NULL)
1745 {
1746 _bfd_error_handler
1747 (_("failed to allocate space for new APUinfo section"));
1748 return;
1749 }
1750
1751 /* Create the apuinfo header. */
1752 num_entries = apuinfo_list_length ();
1753 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
1754 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
1755 bfd_put_32 (abfd, 0x2, buffer + 8);
1756 strcpy ((char *) buffer + 12, APUINFO_LABEL);
1757
1758 length = 20;
1759 for (i = 0; i < num_entries; i++)
1760 {
1761 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
1762 length += 4;
1763 }
1764
1765 if (length != asec->size)
1766 _bfd_error_handler (_("failed to compute new APUinfo section"));
1767
1768 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
1769 _bfd_error_handler (_("failed to install new APUinfo section"));
1770
1771 free (buffer);
1772
1773 apuinfo_list_finish ();
1774 }
1775
1776 static bool
1777 ppc_elf_final_write_processing (bfd *abfd)
1778 {
1779 ppc_final_write_processing (abfd);
1780 return _bfd_elf_final_write_processing (abfd);
1781 }
1782
1783 static bool
1785 is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
1786 {
1787 bfd_byte buf[4 * 4];
1788
1789 if (!bfd_get_section_contents (abfd, glink, buf, off, sizeof buf))
1790 return false;
1791
1792 return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
1793 && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
1794 && bfd_get_32 (abfd, buf + 8) == MTCTR_11
1795 && bfd_get_32 (abfd, buf + 12) == BCTR);
1796 }
1797
1798 static bool
1799 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
1800 {
1801 bfd_vma vma = *(bfd_vma *) ptr;
1802 return ((section->flags & SEC_ALLOC) != 0
1803 && section->vma <= vma
1804 && vma < section->vma + section->size);
1805 }
1806
1807 static long
1808 ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms,
1809 long dynsymcount, asymbol **dynsyms,
1810 asymbol **ret)
1811 {
1812 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
1813 asection *plt, *relplt, *dynamic, *glink;
1814 bfd_vma glink_vma = 0;
1815 bfd_vma resolv_vma = 0;
1816 bfd_vma stub_off;
1817 asymbol *s;
1818 arelent *p;
1819 size_t count, i, stub_delta;
1820 size_t size;
1821 char *names;
1822 bfd_byte buf[4];
1823
1824 *ret = NULL;
1825
1826 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
1827 return 0;
1828
1829 if (dynsymcount <= 0)
1830 return 0;
1831
1832 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
1833 if (relplt == NULL)
1834 return 0;
1835
1836 plt = bfd_get_section_by_name (abfd, ".plt");
1837 if (plt == NULL)
1838 return 0;
1839
1840 /* Call common code to handle old-style executable PLTs. */
1841 if (elf_section_flags (plt) & SHF_EXECINSTR)
1842 return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
1843 dynsymcount, dynsyms, ret);
1844
1845 /* If this object was prelinked, the prelinker stored the address
1846 of .glink at got[1]. If it wasn't prelinked, got[1] will be zero. */
1847 dynamic = bfd_get_section_by_name (abfd, ".dynamic");
1848 if (dynamic != NULL)
1849 {
1850 bfd_byte *dynbuf, *extdyn, *extdynend;
1851 size_t extdynsize;
1852 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1853
1854 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
1855 return -1;
1856
1857 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1858 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1859
1860 for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
1861 (size_t) (extdynend - extdyn) >= extdynsize;
1862 extdyn += extdynsize)
1863 {
1864 Elf_Internal_Dyn dyn;
1865 (*swap_dyn_in) (abfd, extdyn, &dyn);
1866
1867 if (dyn.d_tag == DT_NULL)
1868 break;
1869
1870 if (dyn.d_tag == DT_PPC_GOT)
1871 {
1872 unsigned int g_o_t = dyn.d_un.d_val;
1873 asection *got = bfd_get_section_by_name (abfd, ".got");
1874 if (got != NULL
1875 && bfd_get_section_contents (abfd, got, buf,
1876 g_o_t - got->vma + 4, 4))
1877 glink_vma = bfd_get_32 (abfd, buf);
1878 break;
1879 }
1880 }
1881 free (dynbuf);
1882 }
1883
1884 /* Otherwise we read the first plt entry. */
1885 if (glink_vma == 0)
1886 {
1887 if (bfd_get_section_contents (abfd, plt, buf, 0, 4))
1888 glink_vma = bfd_get_32 (abfd, buf);
1889 }
1890
1891 if (glink_vma == 0)
1892 return 0;
1893
1894 /* The .glink section usually does not survive the final
1895 link; search for the section (usually .text) where the
1896 glink stubs now reside. */
1897 glink = bfd_sections_find_if (abfd, section_covers_vma, &glink_vma);
1898 if (glink == NULL)
1899 return 0;
1900
1901 /* Determine glink PLT resolver by reading the relative branch
1902 from the first glink stub. */
1903 if (bfd_get_section_contents (abfd, glink, buf,
1904 glink_vma - glink->vma, 4))
1905 {
1906 unsigned int insn = bfd_get_32 (abfd, buf);
1907
1908 /* The first glink stub may either branch to the resolver ... */
1909 insn ^= B;
1910 if ((insn & ~0x3fffffc) == 0)
1911 resolv_vma = glink_vma + (insn ^ 0x2000000) - 0x2000000;
1912
1913 /* ... or fall through a bunch of NOPs. */
1914 else if ((insn ^ B ^ NOP) == 0)
1915 for (i = 4;
1916 bfd_get_section_contents (abfd, glink, buf,
1917 glink_vma - glink->vma + i, 4);
1918 i += 4)
1919 if (bfd_get_32 (abfd, buf) != NOP)
1920 {
1921 resolv_vma = glink_vma + i;
1922 break;
1923 }
1924 }
1925
1926 count = relplt->size / sizeof (Elf32_External_Rela);
1927 /* If the stubs are those for -shared/-pie then we might have
1928 multiple stubs for each plt entry. If that is the case then
1929 there is no way to associate stubs with their plt entries short
1930 of figuring out the GOT pointer value used in the stub.
1931 The offsets tested here need to cover all possible values of
1932 GLINK_ENTRY_SIZE for other than __tls_get_addr_opt. */
1933 stub_off = glink_vma - glink->vma;
1934 for (stub_delta = 16; stub_delta <= 32; stub_delta += 8)
1935 if (is_nonpic_glink_stub (abfd, glink, stub_off - stub_delta))
1936 break;
1937 if (stub_delta > 32)
1938 return 0;
1939
1940 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
1941 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
1942 return -1;
1943
1944 size = count * sizeof (asymbol);
1945 p = relplt->relocation;
1946 for (i = 0; i < count; i++, p++)
1947 {
1948 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
1949 if (p->addend != 0)
1950 size += sizeof ("+0x") - 1 + 8;
1951 }
1952
1953 size += sizeof (asymbol) + sizeof ("__glink");
1954
1955 if (resolv_vma)
1956 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
1957
1958 s = *ret = bfd_malloc (size);
1959 if (s == NULL)
1960 return -1;
1961
1962 stub_off = glink_vma - glink->vma;
1963 names = (char *) (s + count + 1 + (resolv_vma != 0));
1964 p = relplt->relocation + count - 1;
1965 for (i = 0; i < count; i++)
1966 {
1967 size_t len;
1968
1969 stub_off -= stub_delta;
1970 if (strcmp ((*p->sym_ptr_ptr)->name, "__tls_get_addr_opt") == 0)
1971 stub_off -= 32;
1972 *s = **p->sym_ptr_ptr;
1973 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
1974 we are defining a symbol, ensure one of them is set. */
1975 if ((s->flags & BSF_LOCAL) == 0)
1976 s->flags |= BSF_GLOBAL;
1977 s->flags |= BSF_SYNTHETIC;
1978 s->section = glink;
1979 s->value = stub_off;
1980 s->name = names;
1981 s->udata.p = NULL;
1982 len = strlen ((*p->sym_ptr_ptr)->name);
1983 memcpy (names, (*p->sym_ptr_ptr)->name, len);
1984 names += len;
1985 if (p->addend != 0)
1986 {
1987 memcpy (names, "+0x", sizeof ("+0x") - 1);
1988 names += sizeof ("+0x") - 1;
1989 bfd_sprintf_vma (abfd, names, p->addend);
1990 names += strlen (names);
1991 }
1992 memcpy (names, "@plt", sizeof ("@plt"));
1993 names += sizeof ("@plt");
1994 ++s;
1995 --p;
1996 }
1997
1998 /* Add a symbol at the start of the glink branch table. */
1999 memset (s, 0, sizeof *s);
2000 s->the_bfd = abfd;
2001 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2002 s->section = glink;
2003 s->value = glink_vma - glink->vma;
2004 s->name = names;
2005 memcpy (names, "__glink", sizeof ("__glink"));
2006 names += sizeof ("__glink");
2007 s++;
2008 count++;
2009
2010 if (resolv_vma)
2011 {
2012 /* Add a symbol for the glink PLT resolver. */
2013 memset (s, 0, sizeof *s);
2014 s->the_bfd = abfd;
2015 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2016 s->section = glink;
2017 s->value = resolv_vma - glink->vma;
2018 s->name = names;
2019 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
2020 names += sizeof ("__glink_PLTresolve");
2021 s++;
2022 count++;
2023 }
2024
2025 return count;
2026 }
2027
2028 /* The following functions are specific to the ELF linker, while
2030 functions above are used generally. They appear in this file more
2031 or less in the order in which they are called. eg.
2032 ppc_elf_check_relocs is called early in the link process,
2033 ppc_elf_finish_dynamic_sections is one of the last functions
2034 called. */
2035
2036 /* Track PLT entries needed for a given symbol. We might need more
2037 than one glink entry per symbol when generating a pic binary. */
2038 struct plt_entry
2039 {
2040 struct plt_entry *next;
2041
2042 /* -fPIC uses multiple GOT sections, one per file, called ".got2".
2043 This field stores the offset into .got2 used to initialise the
2044 GOT pointer reg. It will always be at least 32768. (Current
2045 gcc always uses an offset of 32768, but ld -r will pack .got2
2046 sections together resulting in larger offsets). */
2047 bfd_vma addend;
2048
2049 /* The .got2 section. */
2050 asection *sec;
2051
2052 /* PLT refcount or offset. */
2053 union
2054 {
2055 bfd_signed_vma refcount;
2056 bfd_vma offset;
2057 } plt;
2058
2059 /* .glink stub offset. */
2060 bfd_vma glink_offset;
2061 };
2062
2063 /* Of those relocs that might be copied as dynamic relocs, this
2064 function selects those that must be copied when linking a shared
2065 library or PIE, even when the symbol is local. */
2066
2067 static int
2068 must_be_dyn_reloc (struct bfd_link_info *info,
2069 enum elf_ppc_reloc_type r_type)
2070 {
2071 switch (r_type)
2072 {
2073 default:
2074 /* Only relative relocs can be resolved when the object load
2075 address isn't fixed. DTPREL32 is excluded because the
2076 dynamic linker needs to differentiate global dynamic from
2077 local dynamic __tls_index pairs when PPC_OPT_TLS is set. */
2078 return 1;
2079
2080 case R_PPC_REL24:
2081 case R_PPC_REL14:
2082 case R_PPC_REL14_BRTAKEN:
2083 case R_PPC_REL14_BRNTAKEN:
2084 case R_PPC_REL32:
2085 return 0;
2086
2087 case R_PPC_TPREL32:
2088 case R_PPC_TPREL16:
2089 case R_PPC_TPREL16_LO:
2090 case R_PPC_TPREL16_HI:
2091 case R_PPC_TPREL16_HA:
2092 /* These relocations are relative but in a shared library the
2093 linker doesn't know the thread pointer base. */
2094 return bfd_link_dll (info);
2095 }
2096 }
2097
2098 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2099 copying dynamic variables from a shared lib into an app's dynbss
2100 section, and instead use a dynamic relocation to point into the
2101 shared lib. */
2102 #define ELIMINATE_COPY_RELOCS 1
2103
2104 /* Used to track dynamic relocations for local symbols. */
2105 struct ppc_dyn_relocs
2106 {
2107 struct ppc_dyn_relocs *next;
2108
2109 /* The input section of the reloc. */
2110 asection *sec;
2111
2112 /* Total number of relocs copied for the input section. */
2113 unsigned int count : 31;
2114
2115 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2116 unsigned int ifunc : 1;
2117 };
2118
2119 /* PPC ELF linker hash entry. */
2120
2121 struct ppc_elf_link_hash_entry
2122 {
2123 struct elf_link_hash_entry elf;
2124
2125 /* If this symbol is used in the linker created sections, the processor
2126 specific backend uses this field to map the field into the offset
2127 from the beginning of the section. */
2128 elf_linker_section_pointers_t *linker_section_pointer;
2129
2130 /* Contexts in which symbol is used in the GOT.
2131 Bits are or'd into the mask as the corresponding relocs are
2132 encountered during check_relocs, with TLS_TLS being set when any
2133 of the other TLS bits are set. tls_optimize clears bits when
2134 optimizing to indicate the corresponding GOT entry type is not
2135 needed. If set, TLS_TLS is never cleared. tls_optimize may also
2136 set TLS_GDIE when a GD reloc turns into an IE one.
2137 These flags are also kept for local symbols. */
2138 #define TLS_TLS 1 /* Any TLS reloc. */
2139 #define TLS_GD 2 /* GD reloc. */
2140 #define TLS_LD 4 /* LD reloc. */
2141 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
2142 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2143 #define TLS_MARK 32 /* __tls_get_addr call marked. */
2144 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
2145 unsigned char tls_mask;
2146
2147 /* The above field is also used to mark function symbols. In which
2148 case TLS_TLS will be 0. */
2149 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2150 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
2151 #define NON_GOT 256 /* local symbol plt, not stored. */
2152
2153 /* Nonzero if we have seen a small data relocation referring to this
2154 symbol. */
2155 unsigned char has_sda_refs : 1;
2156
2157 /* Flag use of given relocations. */
2158 unsigned char has_addr16_ha : 1;
2159 unsigned char has_addr16_lo : 1;
2160 };
2161
2162 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2163
2164 /* PPC ELF linker hash table. */
2165
2166 struct ppc_elf_link_hash_table
2167 {
2168 struct elf_link_hash_table elf;
2169
2170 /* Various options passed from the linker. */
2171 struct ppc_elf_params *params;
2172
2173 /* Short-cuts to get to dynamic linker sections. */
2174 asection *glink;
2175 asection *dynsbss;
2176 asection *relsbss;
2177 elf_linker_section_t sdata[2];
2178 asection *sbss;
2179 asection *glink_eh_frame;
2180 asection *pltlocal;
2181 asection *relpltlocal;
2182
2183 /* The (unloaded but important) .rela.plt.unloaded on VxWorks. */
2184 asection *srelplt2;
2185
2186 /* Shortcut to __tls_get_addr. */
2187 struct elf_link_hash_entry *tls_get_addr;
2188
2189 /* The bfd that forced an old-style PLT. */
2190 bfd *old_bfd;
2191
2192 /* TLS local dynamic got entry handling. */
2193 union {
2194 bfd_signed_vma refcount;
2195 bfd_vma offset;
2196 } tlsld_got;
2197
2198 /* Offset of branch table to PltResolve function in glink. */
2199 bfd_vma glink_pltresolve;
2200
2201 /* Size of reserved GOT entries. */
2202 unsigned int got_header_size;
2203 /* Non-zero if allocating the header left a gap. */
2204 unsigned int got_gap;
2205
2206 /* The type of PLT we have chosen to use. */
2207 enum ppc_elf_plt_type plt_type;
2208
2209 /* Whether there exist local gnu indirect function resolvers,
2210 referenced by dynamic relocations. */
2211 unsigned int local_ifunc_resolver:1;
2212 unsigned int maybe_local_ifunc_resolver:1;
2213
2214 /* Set if tls optimization is enabled. */
2215 unsigned int do_tls_opt:1;
2216
2217 /* Set if inline plt calls should be converted to direct calls. */
2218 unsigned int can_convert_all_inline_plt:1;
2219
2220 /* The size of PLT entries. */
2221 int plt_entry_size;
2222 /* The distance between adjacent PLT slots. */
2223 int plt_slot_size;
2224 /* The size of the first PLT entry. */
2225 int plt_initial_entry_size;
2226 };
2227
2228 /* Rename some of the generic section flags to better document how they
2229 are used for ppc32. The flags are only valid for ppc32 elf objects. */
2230
2231 /* Nonzero if this section has TLS related relocations. */
2232 #define has_tls_reloc sec_flg0
2233
2234 /* Nonzero if this section has a call to __tls_get_addr lacking marker
2235 relocs. */
2236 #define nomark_tls_get_addr sec_flg1
2237
2238 /* Flag set when PLTCALL relocs are detected. */
2239 #define has_pltcall sec_flg2
2240
2241 /* Get the PPC ELF linker hash table from a link_info structure. */
2242
2243 #define ppc_elf_hash_table(p) \
2244 ((is_elf_hash_table ((p)->hash) \
2245 && elf_hash_table_id (elf_hash_table (p)) == PPC32_ELF_DATA) \
2246 ? (struct ppc_elf_link_hash_table *) (p)->hash : NULL)
2247
2248 /* Create an entry in a PPC ELF linker hash table. */
2249
2250 static struct bfd_hash_entry *
2251 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2252 struct bfd_hash_table *table,
2253 const char *string)
2254 {
2255 /* Allocate the structure if it has not already been allocated by a
2256 subclass. */
2257 if (entry == NULL)
2258 {
2259 entry = bfd_hash_allocate (table,
2260 sizeof (struct ppc_elf_link_hash_entry));
2261 if (entry == NULL)
2262 return entry;
2263 }
2264
2265 /* Call the allocation method of the superclass. */
2266 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2267 if (entry != NULL)
2268 {
2269 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2270 ppc_elf_hash_entry (entry)->tls_mask = 0;
2271 ppc_elf_hash_entry (entry)->has_sda_refs = 0;
2272 }
2273
2274 return entry;
2275 }
2276
2277 /* Create a PPC ELF linker hash table. */
2278
2279 static struct bfd_link_hash_table *
2280 ppc_elf_link_hash_table_create (bfd *abfd)
2281 {
2282 struct ppc_elf_link_hash_table *ret;
2283 static struct ppc_elf_params default_params
2284 = { PLT_OLD, 0, 0, 1, 0, 0, 12, 0, 0, 0 };
2285
2286 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2287 if (ret == NULL)
2288 return NULL;
2289
2290 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
2291 ppc_elf_link_hash_newfunc,
2292 sizeof (struct ppc_elf_link_hash_entry),
2293 PPC32_ELF_DATA))
2294 {
2295 free (ret);
2296 return NULL;
2297 }
2298
2299 ret->elf.init_plt_refcount.refcount = 0;
2300 ret->elf.init_plt_refcount.glist = NULL;
2301 ret->elf.init_plt_offset.offset = 0;
2302 ret->elf.init_plt_offset.glist = NULL;
2303
2304 ret->params = &default_params;
2305
2306 ret->sdata[0].name = ".sdata";
2307 ret->sdata[0].sym_name = "_SDA_BASE_";
2308 ret->sdata[0].bss_name = ".sbss";
2309
2310 ret->sdata[1].name = ".sdata2";
2311 ret->sdata[1].sym_name = "_SDA2_BASE_";
2312 ret->sdata[1].bss_name = ".sbss2";
2313
2314 ret->plt_entry_size = 12;
2315 ret->plt_slot_size = 8;
2316 ret->plt_initial_entry_size = 72;
2317
2318 return &ret->elf.root;
2319 }
2320
2321 /* Hook linker params into hash table. */
2322
2323 void
2324 ppc_elf_link_params (struct bfd_link_info *info, struct ppc_elf_params *params)
2325 {
2326 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2327
2328 if (htab)
2329 htab->params = params;
2330 params->pagesize_p2 = bfd_log2 (params->pagesize);
2331 }
2332
2333 /* Create .got and the related sections. */
2334
2335 static bool
2336 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2337 {
2338 struct ppc_elf_link_hash_table *htab;
2339
2340 if (!_bfd_elf_create_got_section (abfd, info))
2341 return false;
2342
2343 htab = ppc_elf_hash_table (info);
2344 if (htab->elf.target_os != is_vxworks)
2345 {
2346 /* The powerpc .got has a blrl instruction in it. Mark it
2347 executable. */
2348 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
2349 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2350 if (!bfd_set_section_flags (htab->elf.sgot, flags))
2351 return false;
2352 }
2353
2354 return true;
2355 }
2356
2357 /* Create a special linker section, used for R_PPC_EMB_SDAI16 and
2358 R_PPC_EMB_SDA2I16 pointers. These sections become part of .sdata
2359 and .sdata2. Create _SDA_BASE_ and _SDA2_BASE too. */
2360
2361 static bool
2362 ppc_elf_create_linker_section (bfd *abfd,
2363 struct bfd_link_info *info,
2364 flagword flags,
2365 elf_linker_section_t *lsect)
2366 {
2367 asection *s;
2368
2369 flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2370 | SEC_LINKER_CREATED);
2371
2372 s = bfd_make_section_anyway_with_flags (abfd, lsect->name, flags);
2373 if (s == NULL)
2374 return false;
2375 lsect->section = s;
2376
2377 /* Define the sym on the first section of this name. */
2378 s = bfd_get_section_by_name (abfd, lsect->name);
2379
2380 lsect->sym = _bfd_elf_define_linkage_sym (abfd, info, s, lsect->sym_name);
2381 if (lsect->sym == NULL)
2382 return false;
2383 lsect->sym->root.u.def.value = 0x8000;
2384 return true;
2385 }
2386
2387 static bool
2388 ppc_elf_create_glink (bfd *abfd, struct bfd_link_info *info)
2389 {
2390 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2391 asection *s;
2392 flagword flags;
2393 int p2align;
2394
2395 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS
2396 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2397 s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags);
2398 htab->glink = s;
2399 p2align = htab->params->ppc476_workaround ? 6 : 4;
2400 if (p2align < htab->params->plt_stub_align)
2401 p2align = htab->params->plt_stub_align;
2402 if (s == NULL
2403 || !bfd_set_section_alignment (s, p2align))
2404 return false;
2405
2406 if (!info->no_ld_generated_unwind_info)
2407 {
2408 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2409 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2410 s = bfd_make_section_anyway_with_flags (abfd, ".eh_frame", flags);
2411 htab->glink_eh_frame = s;
2412 if (s == NULL
2413 || !bfd_set_section_alignment (s, 2))
2414 return false;
2415 }
2416
2417 flags = SEC_ALLOC | SEC_LINKER_CREATED;
2418 s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
2419 htab->elf.iplt = s;
2420 if (s == NULL
2421 || !bfd_set_section_alignment (s, 4))
2422 return false;
2423
2424 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2425 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2426 s = bfd_make_section_anyway_with_flags (abfd, ".rela.iplt", flags);
2427 htab->elf.irelplt = s;
2428 if (s == NULL
2429 || ! bfd_set_section_alignment (s, 2))
2430 return false;
2431
2432 /* Local plt entries. */
2433 flags = (SEC_ALLOC | SEC_LOAD
2434 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2435 htab->pltlocal = bfd_make_section_anyway_with_flags (abfd, ".branch_lt",
2436 flags);
2437 if (htab->pltlocal == NULL
2438 || !bfd_set_section_alignment (htab->pltlocal, 2))
2439 return false;
2440
2441 if (bfd_link_pic (info))
2442 {
2443 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2444 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2445 htab->relpltlocal
2446 = bfd_make_section_anyway_with_flags (abfd, ".rela.branch_lt", flags);
2447 if (htab->relpltlocal == NULL
2448 || !bfd_set_section_alignment (htab->relpltlocal, 2))
2449 return false;
2450 }
2451
2452 if (!ppc_elf_create_linker_section (abfd, info, 0,
2453 &htab->sdata[0]))
2454 return false;
2455
2456 if (!ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
2457 &htab->sdata[1]))
2458 return false;
2459
2460 return true;
2461 }
2462
2463 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2464 to output sections (just like _bfd_elf_create_dynamic_sections has
2465 to create .dynbss and .rela.bss). */
2466
2467 static bool
2468 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2469 {
2470 struct ppc_elf_link_hash_table *htab;
2471 asection *s;
2472 flagword flags;
2473
2474 htab = ppc_elf_hash_table (info);
2475
2476 if (htab->elf.sgot == NULL
2477 && !ppc_elf_create_got (abfd, info))
2478 return false;
2479
2480 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2481 return false;
2482
2483 if (htab->glink == NULL
2484 && !ppc_elf_create_glink (abfd, info))
2485 return false;
2486
2487 s = bfd_make_section_anyway_with_flags (abfd, ".dynsbss",
2488 SEC_ALLOC | SEC_LINKER_CREATED);
2489 htab->dynsbss = s;
2490 if (s == NULL)
2491 return false;
2492
2493 if (! bfd_link_pic (info))
2494 {
2495 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2496 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2497 s = bfd_make_section_anyway_with_flags (abfd, ".rela.sbss", flags);
2498 htab->relsbss = s;
2499 if (s == NULL
2500 || !bfd_set_section_alignment (s, 2))
2501 return false;
2502 }
2503
2504 if (htab->elf.target_os == is_vxworks
2505 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2506 return false;
2507
2508 s = htab->elf.splt;
2509 flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
2510 if (htab->plt_type == PLT_VXWORKS)
2511 /* The VxWorks PLT is a loaded section with contents. */
2512 flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
2513 return bfd_set_section_flags (s, flags);
2514 }
2515
2516 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2517
2518 static void
2519 ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
2520 struct elf_link_hash_entry *dir,
2521 struct elf_link_hash_entry *ind)
2522 {
2523 struct ppc_elf_link_hash_entry *edir, *eind;
2524
2525 edir = (struct ppc_elf_link_hash_entry *) dir;
2526 eind = (struct ppc_elf_link_hash_entry *) ind;
2527
2528 edir->tls_mask |= eind->tls_mask;
2529 edir->has_sda_refs |= eind->has_sda_refs;
2530
2531 if (edir->elf.versioned != versioned_hidden)
2532 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
2533 edir->elf.ref_regular |= eind->elf.ref_regular;
2534 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
2535 edir->elf.non_got_ref |= eind->elf.non_got_ref;
2536 edir->elf.needs_plt |= eind->elf.needs_plt;
2537 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
2538
2539 /* If we were called to copy over info for a weak sym, that's all. */
2540 if (eind->elf.root.type != bfd_link_hash_indirect)
2541 return;
2542
2543 if (ind->dyn_relocs != NULL)
2544 {
2545 if (dir->dyn_relocs != NULL)
2546 {
2547 struct elf_dyn_relocs **pp;
2548 struct elf_dyn_relocs *p;
2549
2550 /* Add reloc counts against the indirect sym to the direct sym
2551 list. Merge any entries against the same section. */
2552 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
2553 {
2554 struct elf_dyn_relocs *q;
2555
2556 for (q = dir->dyn_relocs; q != NULL; q = q->next)
2557 if (q->sec == p->sec)
2558 {
2559 q->pc_count += p->pc_count;
2560 q->count += p->count;
2561 *pp = p->next;
2562 break;
2563 }
2564 if (q == NULL)
2565 pp = &p->next;
2566 }
2567 *pp = dir->dyn_relocs;
2568 }
2569
2570 dir->dyn_relocs = ind->dyn_relocs;
2571 ind->dyn_relocs = NULL;
2572 }
2573
2574 /* Copy over the GOT refcount entries that we may have already seen to
2575 the symbol which just became indirect. */
2576 edir->elf.got.refcount += eind->elf.got.refcount;
2577 eind->elf.got.refcount = 0;
2578
2579 /* And plt entries. */
2580 if (eind->elf.plt.plist != NULL)
2581 {
2582 if (edir->elf.plt.plist != NULL)
2583 {
2584 struct plt_entry **entp;
2585 struct plt_entry *ent;
2586
2587 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
2588 {
2589 struct plt_entry *dent;
2590
2591 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
2592 if (dent->sec == ent->sec && dent->addend == ent->addend)
2593 {
2594 dent->plt.refcount += ent->plt.refcount;
2595 *entp = ent->next;
2596 break;
2597 }
2598 if (dent == NULL)
2599 entp = &ent->next;
2600 }
2601 *entp = edir->elf.plt.plist;
2602 }
2603
2604 edir->elf.plt.plist = eind->elf.plt.plist;
2605 eind->elf.plt.plist = NULL;
2606 }
2607
2608 if (eind->elf.dynindx != -1)
2609 {
2610 if (edir->elf.dynindx != -1)
2611 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2612 edir->elf.dynstr_index);
2613 edir->elf.dynindx = eind->elf.dynindx;
2614 edir->elf.dynstr_index = eind->elf.dynstr_index;
2615 eind->elf.dynindx = -1;
2616 eind->elf.dynstr_index = 0;
2617 }
2618 }
2619
2620 /* Hook called by the linker routine which adds symbols from an object
2621 file. We use it to put .comm items in .sbss, and not .bss. */
2622
2623 static bool
2624 ppc_elf_add_symbol_hook (bfd *abfd,
2625 struct bfd_link_info *info,
2626 Elf_Internal_Sym *sym,
2627 const char **namep ATTRIBUTE_UNUSED,
2628 flagword *flagsp ATTRIBUTE_UNUSED,
2629 asection **secp,
2630 bfd_vma *valp)
2631 {
2632 if (sym->st_shndx == SHN_COMMON
2633 && !bfd_link_relocatable (info)
2634 && is_ppc_elf (info->output_bfd)
2635 && sym->st_size <= elf_gp_size (abfd))
2636 {
2637 /* Common symbols less than or equal to -G nn bytes are automatically
2638 put into .sbss. */
2639 struct ppc_elf_link_hash_table *htab;
2640
2641 htab = ppc_elf_hash_table (info);
2642 if (htab->sbss == NULL)
2643 {
2644 flagword flags = SEC_IS_COMMON | SEC_SMALL_DATA | SEC_LINKER_CREATED;
2645
2646 if (!htab->elf.dynobj)
2647 htab->elf.dynobj = abfd;
2648
2649 htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2650 ".sbss",
2651 flags);
2652 if (htab->sbss == NULL)
2653 return false;
2654 }
2655
2656 *secp = htab->sbss;
2657 *valp = sym->st_size;
2658 }
2659
2660 return true;
2661 }
2662
2663 /* Find a linker generated pointer with a given addend and type. */
2665
2666 static elf_linker_section_pointers_t *
2667 elf_find_pointer_linker_section
2668 (elf_linker_section_pointers_t *linker_pointers,
2669 bfd_vma addend,
2670 elf_linker_section_t *lsect)
2671 {
2672 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2673 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2674 return linker_pointers;
2675
2676 return NULL;
2677 }
2678
2679 /* Allocate a pointer to live in a linker created section. */
2680
2681 static bool
2682 elf_allocate_pointer_linker_section (bfd *abfd,
2683 elf_linker_section_t *lsect,
2684 struct elf_link_hash_entry *h,
2685 const Elf_Internal_Rela *rel)
2686 {
2687 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2688 elf_linker_section_pointers_t *linker_section_ptr;
2689 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2690 bfd_size_type amt;
2691
2692 BFD_ASSERT (lsect != NULL);
2693
2694 /* Is this a global symbol? */
2695 if (h != NULL)
2696 {
2697 struct ppc_elf_link_hash_entry *eh;
2698
2699 /* Has this symbol already been allocated? If so, our work is done. */
2700 eh = (struct ppc_elf_link_hash_entry *) h;
2701 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2702 rel->r_addend,
2703 lsect))
2704 return true;
2705
2706 ptr_linker_section_ptr = &eh->linker_section_pointer;
2707 }
2708 else
2709 {
2710 BFD_ASSERT (is_ppc_elf (abfd));
2711
2712 /* Allocation of a pointer to a local symbol. */
2713 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2714
2715 /* Allocate a table to hold the local symbols if first time. */
2716 if (!ptr)
2717 {
2718 unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
2719
2720 amt = num_symbols;
2721 amt *= sizeof (elf_linker_section_pointers_t *);
2722 ptr = bfd_zalloc (abfd, amt);
2723
2724 if (!ptr)
2725 return false;
2726
2727 elf_local_ptr_offsets (abfd) = ptr;
2728 }
2729
2730 /* Has this symbol already been allocated? If so, our work is done. */
2731 if (elf_find_pointer_linker_section (ptr[r_symndx],
2732 rel->r_addend,
2733 lsect))
2734 return true;
2735
2736 ptr_linker_section_ptr = &ptr[r_symndx];
2737 }
2738
2739 /* Allocate space for a pointer in the linker section, and allocate
2740 a new pointer record from internal memory. */
2741 BFD_ASSERT (ptr_linker_section_ptr != NULL);
2742 amt = sizeof (elf_linker_section_pointers_t);
2743 linker_section_ptr = bfd_alloc (abfd, amt);
2744
2745 if (!linker_section_ptr)
2746 return false;
2747
2748 linker_section_ptr->next = *ptr_linker_section_ptr;
2749 linker_section_ptr->addend = rel->r_addend;
2750 linker_section_ptr->lsect = lsect;
2751 *ptr_linker_section_ptr = linker_section_ptr;
2752
2753 if (!bfd_set_section_alignment (lsect->section, 2))
2754 return false;
2755 linker_section_ptr->offset = lsect->section->size;
2756 lsect->section->size += 4;
2757
2758 #ifdef DEBUG
2759 fprintf (stderr,
2760 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2761 lsect->name, (long) linker_section_ptr->offset,
2762 (long) lsect->section->size);
2763 #endif
2764
2765 return true;
2766 }
2767
2768 static struct plt_entry **
2769 update_local_sym_info (bfd *abfd,
2770 Elf_Internal_Shdr *symtab_hdr,
2771 unsigned long r_symndx,
2772 int tls_type)
2773 {
2774 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
2775 struct plt_entry **local_plt;
2776 unsigned char *local_got_tls_masks;
2777
2778 if (local_got_refcounts == NULL)
2779 {
2780 bfd_size_type size = symtab_hdr->sh_info;
2781
2782 size *= (sizeof (*local_got_refcounts)
2783 + sizeof (*local_plt)
2784 + sizeof (*local_got_tls_masks));
2785 local_got_refcounts = bfd_zalloc (abfd, size);
2786 if (local_got_refcounts == NULL)
2787 return NULL;
2788 elf_local_got_refcounts (abfd) = local_got_refcounts;
2789 }
2790
2791 local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
2792 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
2793 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
2794 if ((tls_type & NON_GOT) == 0)
2795 local_got_refcounts[r_symndx] += 1;
2796 return local_plt + r_symndx;
2797 }
2798
2799 static bool
2800 update_plt_info (bfd *abfd, struct plt_entry **plist,
2801 asection *sec, bfd_vma addend)
2802 {
2803 struct plt_entry *ent;
2804
2805 if (addend < 32768)
2806 sec = NULL;
2807 for (ent = *plist; ent != NULL; ent = ent->next)
2808 if (ent->sec == sec && ent->addend == addend)
2809 break;
2810 if (ent == NULL)
2811 {
2812 size_t amt = sizeof (*ent);
2813 ent = bfd_alloc (abfd, amt);
2814 if (ent == NULL)
2815 return false;
2816 ent->next = *plist;
2817 ent->sec = sec;
2818 ent->addend = addend;
2819 ent->plt.refcount = 0;
2820 *plist = ent;
2821 }
2822 ent->plt.refcount += 1;
2823 return true;
2824 }
2825
2826 static struct plt_entry *
2827 find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
2828 {
2829 struct plt_entry *ent;
2830
2831 if (addend < 32768)
2832 sec = NULL;
2833 for (ent = *plist; ent != NULL; ent = ent->next)
2834 if (ent->sec == sec && ent->addend == addend)
2835 break;
2836 return ent;
2837 }
2838
2839 static bool
2840 is_branch_reloc (enum elf_ppc_reloc_type r_type)
2841 {
2842 return (r_type == R_PPC_PLTREL24
2843 || r_type == R_PPC_LOCAL24PC
2844 || r_type == R_PPC_REL24
2845 || r_type == R_PPC_REL14
2846 || r_type == R_PPC_REL14_BRTAKEN
2847 || r_type == R_PPC_REL14_BRNTAKEN
2848 || r_type == R_PPC_ADDR24
2849 || r_type == R_PPC_ADDR14
2850 || r_type == R_PPC_ADDR14_BRTAKEN
2851 || r_type == R_PPC_ADDR14_BRNTAKEN
2852 || r_type == R_PPC_VLE_REL24);
2853 }
2854
2855 /* Relocs on inline plt call sequence insns prior to the call. */
2856
2857 static bool
2858 is_plt_seq_reloc (enum elf_ppc_reloc_type r_type)
2859 {
2860 return (r_type == R_PPC_PLT16_HA
2861 || r_type == R_PPC_PLT16_HI
2862 || r_type == R_PPC_PLT16_LO
2863 || r_type == R_PPC_PLTSEQ);
2864 }
2865
2866 /* Like bfd_reloc_offset_in_range but without a howto. Return true
2867 iff a field of SIZE bytes at OFFSET is within SEC limits. */
2868
2869 static bool
2870 offset_in_range (asection *sec, bfd_vma offset, size_t size)
2871 {
2872 return offset <= sec->size && size <= sec->size - offset;
2873 }
2874
2875 static void
2876 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
2877 {
2878 _bfd_error_handler
2879 /* xgettext:c-format */
2880 (_("%pB: relocation %s cannot be used when making a shared object"),
2881 abfd,
2882 ppc_elf_howto_table[r_type]->name);
2883 bfd_set_error (bfd_error_bad_value);
2884 }
2885
2886 /* Look through the relocs for a section during the first phase, and
2887 allocate space in the global offset table or procedure linkage
2888 table. */
2889
2890 static bool
2891 ppc_elf_check_relocs (bfd *abfd,
2892 struct bfd_link_info *info,
2893 asection *sec,
2894 const Elf_Internal_Rela *relocs)
2895 {
2896 struct ppc_elf_link_hash_table *htab;
2897 Elf_Internal_Shdr *symtab_hdr;
2898 struct elf_link_hash_entry **sym_hashes;
2899 const Elf_Internal_Rela *rel;
2900 const Elf_Internal_Rela *rel_end;
2901 asection *got2, *sreloc;
2902 struct elf_link_hash_entry *tga;
2903
2904 if (bfd_link_relocatable (info))
2905 return true;
2906
2907 #ifdef DEBUG
2908 _bfd_error_handler ("ppc_elf_check_relocs called for section %pA in %pB",
2909 sec, abfd);
2910 #endif
2911
2912 BFD_ASSERT (is_ppc_elf (abfd));
2913
2914 /* Initialize howto table if not already done. */
2915 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2916 ppc_elf_howto_init ();
2917
2918 htab = ppc_elf_hash_table (info);
2919 if (htab->glink == NULL)
2920 {
2921 if (htab->elf.dynobj == NULL)
2922 htab->elf.dynobj = abfd;
2923 if (!ppc_elf_create_glink (htab->elf.dynobj, info))
2924 return false;
2925 }
2926 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
2927 false, false, true);
2928 symtab_hdr = &elf_symtab_hdr (abfd);
2929 sym_hashes = elf_sym_hashes (abfd);
2930 got2 = bfd_get_section_by_name (abfd, ".got2");
2931 sreloc = NULL;
2932
2933 rel_end = relocs + sec->reloc_count;
2934 for (rel = relocs; rel < rel_end; rel++)
2935 {
2936 unsigned long r_symndx;
2937 enum elf_ppc_reloc_type r_type;
2938 struct elf_link_hash_entry *h;
2939 Elf_Internal_Sym *isym;
2940 int tls_type;
2941 struct plt_entry **ifunc;
2942 struct plt_entry **pltent;
2943 bfd_vma addend;
2944
2945 r_symndx = ELF32_R_SYM (rel->r_info);
2946 if (r_symndx < symtab_hdr->sh_info)
2947 {
2948 h = NULL;
2949 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
2950 if (isym == NULL)
2951 return false;
2952 }
2953 else
2954 {
2955 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2956 while (h->root.type == bfd_link_hash_indirect
2957 || h->root.type == bfd_link_hash_warning)
2958 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2959 isym = NULL;
2960 }
2961
2962 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2963 This shows up in particular in an R_PPC_ADDR32 in the eabi
2964 startup code. */
2965 if (h != NULL
2966 && htab->elf.sgot == NULL
2967 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2968 {
2969 if (htab->elf.dynobj == NULL)
2970 htab->elf.dynobj = abfd;
2971 if (!ppc_elf_create_got (htab->elf.dynobj, info))
2972 return false;
2973 BFD_ASSERT (h == htab->elf.hgot);
2974 }
2975
2976 tls_type = 0;
2977 r_type = ELF32_R_TYPE (rel->r_info);
2978 ifunc = NULL;
2979 if (h != NULL)
2980 {
2981 if (h->type == STT_GNU_IFUNC)
2982 {
2983 h->needs_plt = 1;
2984 ifunc = &h->plt.plist;
2985 }
2986 }
2987 else if (htab->elf.target_os != is_vxworks)
2988 {
2989 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
2990 {
2991 /* Set PLT_IFUNC flag for this sym, no GOT entry yet. */
2992 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
2993 NON_GOT | PLT_IFUNC);
2994 if (ifunc == NULL)
2995 return false;
2996
2997 /* STT_GNU_IFUNC symbols must have a PLT entry;
2998 In a non-pie executable even when there are
2999 no plt calls. */
3000 if (!bfd_link_pic (info)
3001 || is_branch_reloc (r_type)
3002 || r_type == R_PPC_PLT16_LO
3003 || r_type == R_PPC_PLT16_HI
3004 || r_type == R_PPC_PLT16_HA)
3005 {
3006 addend = 0;
3007 if (r_type == R_PPC_PLTREL24)
3008 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3009 if (bfd_link_pic (info)
3010 && (r_type == R_PPC_PLTREL24
3011 || r_type == R_PPC_PLT16_LO
3012 || r_type == R_PPC_PLT16_HI
3013 || r_type == R_PPC_PLT16_HA))
3014 addend = rel->r_addend;
3015 if (!update_plt_info (abfd, ifunc, got2, addend))
3016 return false;
3017 }
3018 }
3019 }
3020
3021 if (htab->elf.target_os != is_vxworks
3022 && is_branch_reloc (r_type)
3023 && h != NULL
3024 && h == tga)
3025 {
3026 if (rel != relocs
3027 && (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
3028 || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
3029 /* We have a new-style __tls_get_addr call with a marker
3030 reloc. */
3031 ;
3032 else
3033 /* Mark this section as having an old-style call. */
3034 sec->nomark_tls_get_addr = 1;
3035 }
3036
3037 switch (r_type)
3038 {
3039 case R_PPC_TLSGD:
3040 case R_PPC_TLSLD:
3041 /* These special tls relocs tie a call to __tls_get_addr with
3042 its parameter symbol. */
3043 if (h != NULL)
3044 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
3045 else
3046 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3047 NON_GOT | TLS_TLS | TLS_MARK))
3048 return false;
3049 break;
3050
3051 case R_PPC_PLTSEQ:
3052 break;
3053
3054 case R_PPC_GOT_TLSLD16:
3055 case R_PPC_GOT_TLSLD16_LO:
3056 case R_PPC_GOT_TLSLD16_HI:
3057 case R_PPC_GOT_TLSLD16_HA:
3058 tls_type = TLS_TLS | TLS_LD;
3059 goto dogottls;
3060
3061 case R_PPC_GOT_TLSGD16:
3062 case R_PPC_GOT_TLSGD16_LO:
3063 case R_PPC_GOT_TLSGD16_HI:
3064 case R_PPC_GOT_TLSGD16_HA:
3065 tls_type = TLS_TLS | TLS_GD;
3066 goto dogottls;
3067
3068 case R_PPC_GOT_TPREL16:
3069 case R_PPC_GOT_TPREL16_LO:
3070 case R_PPC_GOT_TPREL16_HI:
3071 case R_PPC_GOT_TPREL16_HA:
3072 if (bfd_link_dll (info))
3073 info->flags |= DF_STATIC_TLS;
3074 tls_type = TLS_TLS | TLS_TPREL;
3075 goto dogottls;
3076
3077 case R_PPC_GOT_DTPREL16:
3078 case R_PPC_GOT_DTPREL16_LO:
3079 case R_PPC_GOT_DTPREL16_HI:
3080 case R_PPC_GOT_DTPREL16_HA:
3081 tls_type = TLS_TLS | TLS_DTPREL;
3082 dogottls:
3083 sec->has_tls_reloc = 1;
3084 /* Fall through. */
3085
3086 /* GOT16 relocations */
3087 case R_PPC_GOT16:
3088 case R_PPC_GOT16_LO:
3089 case R_PPC_GOT16_HI:
3090 case R_PPC_GOT16_HA:
3091 /* This symbol requires a global offset table entry. */
3092 if (htab->elf.sgot == NULL)
3093 {
3094 if (htab->elf.dynobj == NULL)
3095 htab->elf.dynobj = abfd;
3096 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3097 return false;
3098 }
3099 if (h != NULL)
3100 {
3101 h->got.refcount += 1;
3102 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3103 }
3104 else
3105 /* This is a global offset table entry for a local symbol. */
3106 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3107 return false;
3108
3109 /* We may also need a plt entry if the symbol turns out to be
3110 an ifunc. */
3111 if (h != NULL && !bfd_link_pic (info))
3112 {
3113 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3114 return false;
3115 }
3116 break;
3117
3118 /* Indirect .sdata relocation. */
3119 case R_PPC_EMB_SDAI16:
3120 htab->sdata[0].sym->ref_regular = 1;
3121 if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[0],
3122 h, rel))
3123 return false;
3124 if (h != NULL)
3125 {
3126 ppc_elf_hash_entry (h)->has_sda_refs = true;
3127 h->non_got_ref = true;
3128 }
3129 break;
3130
3131 /* Indirect .sdata2 relocation. */
3132 case R_PPC_EMB_SDA2I16:
3133 if (!bfd_link_executable (info))
3134 {
3135 bad_shared_reloc (abfd, r_type);
3136 return false;
3137 }
3138 htab->sdata[1].sym->ref_regular = 1;
3139 if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[1],
3140 h, rel))
3141 return false;
3142 if (h != NULL)
3143 {
3144 ppc_elf_hash_entry (h)->has_sda_refs = true;
3145 h->non_got_ref = true;
3146 }
3147 break;
3148
3149 case R_PPC_SDAREL16:
3150 htab->sdata[0].sym->ref_regular = 1;
3151 /* Fall through. */
3152
3153 case R_PPC_VLE_SDAREL_LO16A:
3154 case R_PPC_VLE_SDAREL_LO16D:
3155 case R_PPC_VLE_SDAREL_HI16A:
3156 case R_PPC_VLE_SDAREL_HI16D:
3157 case R_PPC_VLE_SDAREL_HA16A:
3158 case R_PPC_VLE_SDAREL_HA16D:
3159 if (h != NULL)
3160 {
3161 ppc_elf_hash_entry (h)->has_sda_refs = true;
3162 h->non_got_ref = true;
3163 }
3164 break;
3165
3166 case R_PPC_VLE_REL8:
3167 case R_PPC_VLE_REL15:
3168 case R_PPC_VLE_REL24:
3169 case R_PPC_VLE_LO16A:
3170 case R_PPC_VLE_LO16D:
3171 case R_PPC_VLE_HI16A:
3172 case R_PPC_VLE_HI16D:
3173 case R_PPC_VLE_HA16A:
3174 case R_PPC_VLE_HA16D:
3175 case R_PPC_VLE_ADDR20:
3176 break;
3177
3178 case R_PPC_EMB_SDA2REL:
3179 if (!bfd_link_executable (info))
3180 {
3181 bad_shared_reloc (abfd, r_type);
3182 return false;
3183 }
3184 htab->sdata[1].sym->ref_regular = 1;
3185 if (h != NULL)
3186 {
3187 ppc_elf_hash_entry (h)->has_sda_refs = true;
3188 h->non_got_ref = true;
3189 }
3190 break;
3191
3192 case R_PPC_VLE_SDA21_LO:
3193 case R_PPC_VLE_SDA21:
3194 case R_PPC_EMB_SDA21:
3195 case R_PPC_EMB_RELSDA:
3196 if (h != NULL)
3197 {
3198 ppc_elf_hash_entry (h)->has_sda_refs = true;
3199 h->non_got_ref = true;
3200 }
3201 break;
3202
3203 case R_PPC_EMB_NADDR32:
3204 case R_PPC_EMB_NADDR16:
3205 case R_PPC_EMB_NADDR16_LO:
3206 case R_PPC_EMB_NADDR16_HI:
3207 case R_PPC_EMB_NADDR16_HA:
3208 if (h != NULL)
3209 h->non_got_ref = true;
3210 break;
3211
3212 case R_PPC_PLTREL24:
3213 if (h == NULL)
3214 break;
3215 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3216 goto pltentry;
3217
3218 case R_PPC_PLTCALL:
3219 sec->has_pltcall = 1;
3220 /* Fall through. */
3221
3222 case R_PPC_PLT32:
3223 case R_PPC_PLTREL32:
3224 case R_PPC_PLT16_LO:
3225 case R_PPC_PLT16_HI:
3226 case R_PPC_PLT16_HA:
3227 pltentry:
3228 #ifdef DEBUG
3229 fprintf (stderr, "Reloc requires a PLT entry\n");
3230 #endif
3231 /* This symbol requires a procedure linkage table entry. */
3232 if (h == NULL)
3233 {
3234 pltent = update_local_sym_info (abfd, symtab_hdr, r_symndx,
3235 NON_GOT | PLT_KEEP);
3236 if (pltent == NULL)
3237 return false;
3238 }
3239 else
3240 {
3241 if (r_type != R_PPC_PLTREL24)
3242 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
3243 h->needs_plt = 1;
3244 pltent = &h->plt.plist;
3245 }
3246 addend = 0;
3247 if (bfd_link_pic (info)
3248 && (r_type == R_PPC_PLTREL24
3249 || r_type == R_PPC_PLT16_LO
3250 || r_type == R_PPC_PLT16_HI
3251 || r_type == R_PPC_PLT16_HA))
3252 addend = rel->r_addend;
3253 if (!update_plt_info (abfd, pltent, got2, addend))
3254 return false;
3255 break;
3256
3257 /* The following relocations don't need to propagate the
3258 relocation if linking a shared object since they are
3259 section relative. */
3260 case R_PPC_SECTOFF:
3261 case R_PPC_SECTOFF_LO:
3262 case R_PPC_SECTOFF_HI:
3263 case R_PPC_SECTOFF_HA:
3264 case R_PPC_DTPREL16:
3265 case R_PPC_DTPREL16_LO:
3266 case R_PPC_DTPREL16_HI:
3267 case R_PPC_DTPREL16_HA:
3268 case R_PPC_TOC16:
3269 break;
3270
3271 case R_PPC_REL16:
3272 case R_PPC_REL16_LO:
3273 case R_PPC_REL16_HI:
3274 case R_PPC_REL16_HA:
3275 case R_PPC_REL16DX_HA:
3276 ppc_elf_tdata (abfd)->has_rel16 = 1;
3277 break;
3278
3279 /* These are just markers. */
3280 case R_PPC_TLS:
3281 case R_PPC_EMB_MRKREF:
3282 case R_PPC_NONE:
3283 case R_PPC_max:
3284 case R_PPC_RELAX:
3285 case R_PPC_RELAX_PLT:
3286 case R_PPC_RELAX_PLTREL24:
3287 case R_PPC_16DX_HA:
3288 break;
3289
3290 /* These should only appear in dynamic objects. */
3291 case R_PPC_COPY:
3292 case R_PPC_GLOB_DAT:
3293 case R_PPC_JMP_SLOT:
3294 case R_PPC_RELATIVE:
3295 case R_PPC_IRELATIVE:
3296 break;
3297
3298 /* These aren't handled yet. We'll report an error later. */
3299 case R_PPC_ADDR30:
3300 case R_PPC_EMB_RELSEC16:
3301 case R_PPC_EMB_RELST_LO:
3302 case R_PPC_EMB_RELST_HI:
3303 case R_PPC_EMB_RELST_HA:
3304 case R_PPC_EMB_BIT_FLD:
3305 break;
3306
3307 /* This refers only to functions defined in the shared library. */
3308 case R_PPC_LOCAL24PC:
3309 if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
3310 {
3311 htab->plt_type = PLT_OLD;
3312 htab->old_bfd = abfd;
3313 }
3314 if (h != NULL
3315 && ifunc != NULL
3316 && !update_plt_info (abfd, ifunc, NULL, 0))
3317 return false;
3318 break;
3319
3320 /* This relocation describes the C++ object vtable hierarchy.
3321 Reconstruct it for later use during GC. */
3322 case R_PPC_GNU_VTINHERIT:
3323 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3324 return false;
3325 break;
3326
3327 /* This relocation describes which C++ vtable entries are actually
3328 used. Record for later use during GC. */
3329 case R_PPC_GNU_VTENTRY:
3330 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3331 return false;
3332 break;
3333
3334 case R_PPC_TPREL16_HI:
3335 case R_PPC_TPREL16_HA:
3336 sec->has_tls_reloc = 1;
3337 /* Fall through. */
3338 /* We shouldn't really be seeing TPREL32. */
3339 case R_PPC_TPREL32:
3340 case R_PPC_TPREL16:
3341 case R_PPC_TPREL16_LO:
3342 if (bfd_link_dll (info))
3343 info->flags |= DF_STATIC_TLS;
3344 goto dodyn;
3345
3346 /* Nor these. */
3347 case R_PPC_DTPMOD32:
3348 case R_PPC_DTPREL32:
3349 goto dodyn;
3350
3351 case R_PPC_REL32:
3352 if (h == NULL
3353 && got2 != NULL
3354 && (sec->flags & SEC_CODE) != 0
3355 && bfd_link_pic (info)
3356 && htab->plt_type == PLT_UNSET)
3357 {
3358 /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
3359 the start of a function, which assembles to a REL32
3360 reference to .got2. If we detect one of these, then
3361 force the old PLT layout because the linker cannot
3362 reliably deduce the GOT pointer value needed for
3363 PLT call stubs. */
3364 asection *s;
3365
3366 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3367 if (s == got2)
3368 {
3369 htab->plt_type = PLT_OLD;
3370 htab->old_bfd = abfd;
3371 }
3372 }
3373 if (h == NULL || h == htab->elf.hgot)
3374 break;
3375 /* fall through */
3376
3377 case R_PPC_ADDR32:
3378 case R_PPC_ADDR16:
3379 case R_PPC_ADDR16_LO:
3380 case R_PPC_ADDR16_HI:
3381 case R_PPC_ADDR16_HA:
3382 case R_PPC_UADDR32:
3383 case R_PPC_UADDR16:
3384 if (h != NULL && !bfd_link_pic (info))
3385 {
3386 /* We may need a plt entry if the symbol turns out to be
3387 a function defined in a dynamic object. */
3388 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3389 return false;
3390
3391 /* We may need a copy reloc too. */
3392 h->non_got_ref = 1;
3393 h->pointer_equality_needed = 1;
3394 if (r_type == R_PPC_ADDR16_HA)
3395 ppc_elf_hash_entry (h)->has_addr16_ha = 1;
3396 if (r_type == R_PPC_ADDR16_LO)
3397 ppc_elf_hash_entry (h)->has_addr16_lo = 1;
3398 }
3399 goto dodyn;
3400
3401 case R_PPC_REL24:
3402 case R_PPC_REL14:
3403 case R_PPC_REL14_BRTAKEN:
3404 case R_PPC_REL14_BRNTAKEN:
3405 if (h == NULL)
3406 break;
3407 if (h == htab->elf.hgot)
3408 {
3409 if (htab->plt_type == PLT_UNSET)
3410 {
3411 htab->plt_type = PLT_OLD;
3412 htab->old_bfd = abfd;
3413 }
3414 break;
3415 }
3416 /* fall through */
3417
3418 case R_PPC_ADDR24:
3419 case R_PPC_ADDR14:
3420 case R_PPC_ADDR14_BRTAKEN:
3421 case R_PPC_ADDR14_BRNTAKEN:
3422 if (h != NULL && !bfd_link_pic (info))
3423 {
3424 /* We may need a plt entry if the symbol turns out to be
3425 a function defined in a dynamic object. */
3426 h->needs_plt = 1;
3427 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3428 return false;
3429 break;
3430 }
3431
3432 dodyn:
3433 /* Set up information for symbols that might need dynamic
3434 relocations. At this point in linking we have read all
3435 the input files and resolved most symbols, but have not
3436 yet decided whether symbols are dynamic or finalized
3437 symbol flags. In some cases we might be setting dynamic
3438 reloc info for symbols that do not end up needing such.
3439 That's OK, adjust_dynamic_symbol and allocate_dynrelocs
3440 work together with this code. */
3441 if ((h != NULL
3442 && !SYMBOL_REFERENCES_LOCAL (info, h))
3443 || (bfd_link_pic (info)
3444 && (h != NULL
3445 ? !bfd_is_abs_symbol (&h->root)
3446 : isym->st_shndx != SHN_ABS)
3447 && must_be_dyn_reloc (info, r_type)))
3448 {
3449 #ifdef DEBUG
3450 fprintf (stderr,
3451 "ppc_elf_check_relocs needs to "
3452 "create relocation for %s\n",
3453 (h && h->root.root.string
3454 ? h->root.root.string : "<unknown>"));
3455 #endif
3456 if (sreloc == NULL)
3457 {
3458 if (htab->elf.dynobj == NULL)
3459 htab->elf.dynobj = abfd;
3460
3461 sreloc = _bfd_elf_make_dynamic_reloc_section
3462 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ true);
3463
3464 if (sreloc == NULL)
3465 return false;
3466 }
3467
3468 /* If this is a global symbol, we count the number of
3469 relocations we need for this symbol. */
3470 if (h != NULL)
3471 {
3472 struct elf_dyn_relocs *p;
3473 struct elf_dyn_relocs **rel_head;
3474
3475 rel_head = &h->dyn_relocs;
3476 p = *rel_head;
3477 if (p == NULL || p->sec != sec)
3478 {
3479 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3480 if (p == NULL)
3481 return false;
3482 p->next = *rel_head;
3483 *rel_head = p;
3484 p->sec = sec;
3485 p->count = 0;
3486 p->pc_count = 0;
3487 }
3488 p->count += 1;
3489 if (!must_be_dyn_reloc (info, r_type))
3490 p->pc_count += 1;
3491 }
3492 else
3493 {
3494 /* Track dynamic relocs needed for local syms too.
3495 We really need local syms available to do this
3496 easily. Oh well. */
3497 struct ppc_dyn_relocs *p;
3498 struct ppc_dyn_relocs **rel_head;
3499 bool is_ifunc;
3500 asection *s;
3501 void *vpp;
3502
3503 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3504 if (s == NULL)
3505 s = sec;
3506
3507 vpp = &elf_section_data (s)->local_dynrel;
3508 rel_head = (struct ppc_dyn_relocs **) vpp;
3509 is_ifunc = ifunc != NULL;
3510 p = *rel_head;
3511 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
3512 p = p->next;
3513 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
3514 {
3515 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3516 if (p == NULL)
3517 return false;
3518 p->next = *rel_head;
3519 *rel_head = p;
3520 p->sec = sec;
3521 p->ifunc = is_ifunc;
3522 p->count = 0;
3523 }
3524 p->count += 1;
3525 }
3526 }
3527
3528 break;
3529 }
3530 }
3531
3532 return true;
3533 }
3534
3535 /* Warn for conflicting Tag_GNU_Power_ABI_FP attributes between IBFD
3537 and OBFD, and merge non-conflicting ones. */
3538 bool
3539 _bfd_elf_ppc_merge_fp_attributes (bfd *ibfd, struct bfd_link_info *info)
3540 {
3541 bfd *obfd = info->output_bfd;
3542 obj_attribute *in_attr, *in_attrs;
3543 obj_attribute *out_attr, *out_attrs;
3544 bool ret = true;
3545 bool warn_only;
3546
3547 /* We only warn about shared library mismatches, because common
3548 libraries advertise support for a particular long double variant
3549 but actually support more than one variant. For example, glibc
3550 typically supports 128-bit IBM long double in the shared library
3551 but has a compatibility static archive for 64-bit long double.
3552 The linker doesn't have the smarts to see that an app using
3553 object files marked as 64-bit long double call the compatibility
3554 layer objects and only from there call into the shared library. */
3555 warn_only = (ibfd->flags & DYNAMIC) != 0;
3556
3557 in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
3558 out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
3559
3560 in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
3561 out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
3562
3563 if (in_attr->i != out_attr->i)
3564 {
3565 int in_fp = in_attr->i & 3;
3566 int out_fp = out_attr->i & 3;
3567 static bfd *last_fp, *last_ld;
3568
3569 if (in_fp == 0)
3570 ;
3571 else if (out_fp == 0)
3572 {
3573 if (!warn_only)
3574 {
3575 out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
3576 out_attr->i ^= in_fp;
3577 last_fp = ibfd;
3578 }
3579 }
3580 else if (out_fp != 2 && in_fp == 2)
3581 {
3582 _bfd_error_handler
3583 /* xgettext:c-format */
3584 (_("%pB uses hard float, %pB uses soft float"),
3585 last_fp, ibfd);
3586 ret = warn_only;
3587 }
3588 else if (out_fp == 2 && in_fp != 2)
3589 {
3590 _bfd_error_handler
3591 /* xgettext:c-format */
3592 (_("%pB uses hard float, %pB uses soft float"),
3593 ibfd, last_fp);
3594 ret = warn_only;
3595 }
3596 else if (out_fp == 1 && in_fp == 3)
3597 {
3598 _bfd_error_handler
3599 /* xgettext:c-format */
3600 (_("%pB uses double-precision hard float, "
3601 "%pB uses single-precision hard float"), last_fp, ibfd);
3602 ret = warn_only;
3603 }
3604 else if (out_fp == 3 && in_fp == 1)
3605 {
3606 _bfd_error_handler
3607 /* xgettext:c-format */
3608 (_("%pB uses double-precision hard float, "
3609 "%pB uses single-precision hard float"), ibfd, last_fp);
3610 ret = warn_only;
3611 }
3612
3613 in_fp = in_attr->i & 0xc;
3614 out_fp = out_attr->i & 0xc;
3615 if (in_fp == 0)
3616 ;
3617 else if (out_fp == 0)
3618 {
3619 if (!warn_only)
3620 {
3621 out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
3622 out_attr->i ^= in_fp;
3623 last_ld = ibfd;
3624 }
3625 }
3626 else if (out_fp != 2 * 4 && in_fp == 2 * 4)
3627 {
3628 _bfd_error_handler
3629 /* xgettext:c-format */
3630 (_("%pB uses 64-bit long double, "
3631 "%pB uses 128-bit long double"), ibfd, last_ld);
3632 ret = warn_only;
3633 }
3634 else if (in_fp != 2 * 4 && out_fp == 2 * 4)
3635 {
3636 _bfd_error_handler
3637 /* xgettext:c-format */
3638 (_("%pB uses 64-bit long double, "
3639 "%pB uses 128-bit long double"), last_ld, ibfd);
3640 ret = warn_only;
3641 }
3642 else if (out_fp == 1 * 4 && in_fp == 3 * 4)
3643 {
3644 _bfd_error_handler
3645 /* xgettext:c-format */
3646 (_("%pB uses IBM long double, "
3647 "%pB uses IEEE long double"), last_ld, ibfd);
3648 ret = warn_only;
3649 }
3650 else if (out_fp == 3 * 4 && in_fp == 1 * 4)
3651 {
3652 _bfd_error_handler
3653 /* xgettext:c-format */
3654 (_("%pB uses IBM long double, "
3655 "%pB uses IEEE long double"), ibfd, last_ld);
3656 ret = warn_only;
3657 }
3658 }
3659
3660 if (!ret)
3661 {
3662 out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
3663 bfd_set_error (bfd_error_bad_value);
3664 }
3665 return ret;
3666 }
3667
3668 /* Merge object attributes from IBFD into OBFD. Warn if
3669 there are conflicting attributes. */
3670 static bool
3671 ppc_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
3672 {
3673 bfd *obfd;
3674 obj_attribute *in_attr, *in_attrs;
3675 obj_attribute *out_attr, *out_attrs;
3676 bool ret;
3677
3678 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
3679 return false;
3680
3681 obfd = info->output_bfd;
3682 in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
3683 out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
3684
3685 /* Check for conflicting Tag_GNU_Power_ABI_Vector attributes and
3686 merge non-conflicting ones. */
3687 in_attr = &in_attrs[Tag_GNU_Power_ABI_Vector];
3688 out_attr = &out_attrs[Tag_GNU_Power_ABI_Vector];
3689 ret = true;
3690 if (in_attr->i != out_attr->i)
3691 {
3692 int in_vec = in_attr->i & 3;
3693 int out_vec = out_attr->i & 3;
3694 static bfd *last_vec;
3695
3696 if (in_vec == 0)
3697 ;
3698 else if (out_vec == 0)
3699 {
3700 out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
3701 out_attr->i = in_vec;
3702 last_vec = ibfd;
3703 }
3704 /* For now, allow generic to transition to AltiVec or SPE
3705 without a warning. If GCC marked files with their stack
3706 alignment and used don't-care markings for files which are
3707 not affected by the vector ABI, we could warn about this
3708 case too. */
3709 else if (in_vec == 1)
3710 ;
3711 else if (out_vec == 1)
3712 {
3713 out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
3714 out_attr->i = in_vec;
3715 last_vec = ibfd;
3716 }
3717 else if (out_vec < in_vec)
3718 {
3719 _bfd_error_handler
3720 /* xgettext:c-format */
3721 (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
3722 last_vec, ibfd);
3723 out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
3724 ret = false;
3725 }
3726 else if (out_vec > in_vec)
3727 {
3728 _bfd_error_handler
3729 /* xgettext:c-format */
3730 (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
3731 ibfd, last_vec);
3732 out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
3733 ret = false;
3734 }
3735 }
3736
3737 /* Check for conflicting Tag_GNU_Power_ABI_Struct_Return attributes
3738 and merge non-conflicting ones. */
3739 in_attr = &in_attrs[Tag_GNU_Power_ABI_Struct_Return];
3740 out_attr = &out_attrs[Tag_GNU_Power_ABI_Struct_Return];
3741 if (in_attr->i != out_attr->i)
3742 {
3743 int in_struct = in_attr->i & 3;
3744 int out_struct = out_attr->i & 3;
3745 static bfd *last_struct;
3746
3747 if (in_struct == 0 || in_struct == 3)
3748 ;
3749 else if (out_struct == 0)
3750 {
3751 out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
3752 out_attr->i = in_struct;
3753 last_struct = ibfd;
3754 }
3755 else if (out_struct < in_struct)
3756 {
3757 _bfd_error_handler
3758 /* xgettext:c-format */
3759 (_("%pB uses r3/r4 for small structure returns, "
3760 "%pB uses memory"), last_struct, ibfd);
3761 out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
3762 ret = false;
3763 }
3764 else if (out_struct > in_struct)
3765 {
3766 _bfd_error_handler
3767 /* xgettext:c-format */
3768 (_("%pB uses r3/r4 for small structure returns, "
3769 "%pB uses memory"), ibfd, last_struct);
3770 out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
3771 ret = false;
3772 }
3773 }
3774 if (!ret)
3775 {
3776 bfd_set_error (bfd_error_bad_value);
3777 return false;
3778 }
3779
3780 /* Merge Tag_compatibility attributes and any common GNU ones. */
3781 return _bfd_elf_merge_object_attributes (ibfd, info);
3782 }
3783
3784 /* Merge backend specific data from an object file to the output
3785 object file when linking. */
3786
3787 static bool
3788 ppc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3789 {
3790 bfd *obfd = info->output_bfd;
3791 flagword old_flags;
3792 flagword new_flags;
3793 bool error;
3794
3795 if (!is_ppc_elf (ibfd) || !is_ppc_elf (obfd))
3796 return true;
3797
3798 /* Check if we have the same endianness. */
3799 if (! _bfd_generic_verify_endian_match (ibfd, info))
3800 return false;
3801
3802 if (!ppc_elf_merge_obj_attributes (ibfd, info))
3803 return false;
3804
3805 if ((ibfd->flags & DYNAMIC) != 0)
3806 return true;
3807
3808 new_flags = elf_elfheader (ibfd)->e_flags;
3809 old_flags = elf_elfheader (obfd)->e_flags;
3810 if (!elf_flags_init (obfd))
3811 {
3812 /* First call, no flags set. */
3813 elf_flags_init (obfd) = true;
3814 elf_elfheader (obfd)->e_flags = new_flags;
3815 }
3816
3817 /* Compatible flags are ok. */
3818 else if (new_flags == old_flags)
3819 ;
3820
3821 /* Incompatible flags. */
3822 else
3823 {
3824 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
3825 to be linked with either. */
3826 error = false;
3827 if ((new_flags & EF_PPC_RELOCATABLE) != 0
3828 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
3829 {
3830 error = true;
3831 _bfd_error_handler
3832 (_("%pB: compiled with -mrelocatable and linked with "
3833 "modules compiled normally"), ibfd);
3834 }
3835 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
3836 && (old_flags & EF_PPC_RELOCATABLE) != 0)
3837 {
3838 error = true;
3839 _bfd_error_handler
3840 (_("%pB: compiled normally and linked with "
3841 "modules compiled with -mrelocatable"), ibfd);
3842 }
3843
3844 /* The output is -mrelocatable-lib iff both the input files are. */
3845 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
3846 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
3847
3848 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
3849 but each input file is either -mrelocatable or -mrelocatable-lib. */
3850 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
3851 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
3852 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
3853 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
3854
3855 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
3856 any module uses it. */
3857 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
3858
3859 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3860 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3861
3862 /* Warn about any other mismatches. */
3863 if (new_flags != old_flags)
3864 {
3865 error = true;
3866 _bfd_error_handler
3867 /* xgettext:c-format */
3868 (_("%pB: uses different e_flags (%#x) fields "
3869 "than previous modules (%#x)"),
3870 ibfd, new_flags, old_flags);
3871 }
3872
3873 if (error)
3874 {
3875 bfd_set_error (bfd_error_bad_value);
3876 return false;
3877 }
3878 }
3879
3880 return true;
3881 }
3882
3883 static bfd_reloc_status_type
3884 ppc_elf_vle_split16 (bfd *input_bfd,
3885 asection *input_section,
3886 unsigned long offset,
3887 bfd_byte *loc,
3888 bfd_vma value,
3889 split16_format_type split16_format,
3890 bool fixup)
3891 {
3892 unsigned int insn, opcode;
3893
3894 if (!offset_in_range (input_section, offset, 4))
3895 return bfd_reloc_outofrange;
3896 insn = bfd_get_32 (input_bfd, loc);
3897 opcode = insn & E_OPCODE_MASK;
3898 if (opcode == E_OR2I_INSN
3899 || opcode == E_AND2I_DOT_INSN
3900 || opcode == E_OR2IS_INSN
3901 || opcode == E_LIS_INSN
3902 || opcode == E_AND2IS_DOT_INSN)
3903 {
3904 if (split16_format != split16a_type)
3905 {
3906 if (fixup)
3907 split16_format = split16a_type;
3908 else
3909 _bfd_error_handler
3910 /* xgettext:c-format */
3911 (_("%pB(%pA+0x%lx): expected 16A style relocation on 0x%08x insn"),
3912 input_bfd, input_section, offset, opcode);
3913 }
3914 }
3915 else if (opcode == E_ADD2I_DOT_INSN
3916 || opcode == E_ADD2IS_INSN
3917 || opcode == E_CMP16I_INSN
3918 || opcode == E_MULL2I_INSN
3919 || opcode == E_CMPL16I_INSN
3920 || opcode == E_CMPH16I_INSN
3921 || opcode == E_CMPHL16I_INSN)
3922 {
3923 if (split16_format != split16d_type)
3924 {
3925 if (fixup)
3926 split16_format = split16d_type;
3927 else
3928 _bfd_error_handler
3929 /* xgettext:c-format */
3930 (_("%pB(%pA+0x%lx): expected 16D style relocation on 0x%08x insn"),
3931 input_bfd, input_section, offset, opcode);
3932 }
3933 }
3934 if (split16_format == split16a_type)
3935 {
3936 insn &= ~((0xf800 << 5) | 0x7ff);
3937 insn |= (value & 0xf800) << 5;
3938 if ((insn & E_LI_MASK) == E_LI_INSN)
3939 {
3940 /* Hack for e_li. Extend sign. */
3941 insn &= ~(0xf0000 >> 5);
3942 insn |= (-(value & 0x8000) & 0xf0000) >> 5;
3943 }
3944 }
3945 else
3946 {
3947 insn &= ~((0xf800 << 10) | 0x7ff);
3948 insn |= (value & 0xf800) << 10;
3949 }
3950 insn |= value & 0x7ff;
3951 bfd_put_32 (input_bfd, insn, loc);
3952 return bfd_reloc_ok;
3953 }
3954
3955 static void
3956 ppc_elf_vle_split20 (bfd *output_bfd, bfd_byte *loc, bfd_vma value)
3957 {
3958 unsigned int insn;
3959
3960 insn = bfd_get_32 (output_bfd, loc);
3961 /* We have an li20 field, bits 17..20, 11..15, 21..31. */
3962 /* Top 4 bits of value to 17..20. */
3963 insn |= (value & 0xf0000) >> 5;
3964 /* Next 5 bits of the value to 11..15. */
3965 insn |= (value & 0xf800) << 5;
3966 /* And the final 11 bits of the value to bits 21 to 31. */
3967 insn |= value & 0x7ff;
3968 bfd_put_32 (output_bfd, insn, loc);
3969 }
3970
3971
3972 /* Choose which PLT scheme to use, and set .plt flags appropriately.
3974 Returns -1 on error, 0 for old PLT, 1 for new PLT. */
3975 int
3976 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
3977 struct bfd_link_info *info)
3978 {
3979 struct ppc_elf_link_hash_table *htab;
3980 flagword flags;
3981
3982 htab = ppc_elf_hash_table (info);
3983
3984 if (htab->plt_type == PLT_UNSET)
3985 {
3986 struct elf_link_hash_entry *h;
3987
3988 if (htab->params->plt_style == PLT_OLD)
3989 htab->plt_type = PLT_OLD;
3990 else if (bfd_link_pic (info)
3991 && htab->elf.dynamic_sections_created
3992 && (h = elf_link_hash_lookup (&htab->elf, "_mcount",
3993 false, false, true)) != NULL
3994 && (h->type == STT_FUNC
3995 || h->needs_plt)
3996 && h->ref_regular
3997 && !(SYMBOL_CALLS_LOCAL (info, h)
3998 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
3999 {
4000 /* Profiling of shared libs (and pies) is not supported with
4001 secure plt, because ppc32 does profiling before a
4002 function prologue and a secure plt pic call stubs needs
4003 r30 to be set up. */
4004 htab->plt_type = PLT_OLD;
4005 }
4006 else
4007 {
4008 bfd *ibfd;
4009 enum ppc_elf_plt_type plt_type = htab->params->plt_style;
4010
4011 /* Look through the reloc flags left by ppc_elf_check_relocs.
4012 Use the old style bss plt if a file makes plt calls
4013 without using the new relocs, and if ld isn't given
4014 --secure-plt and we never see REL16 relocs. */
4015 if (plt_type == PLT_UNSET)
4016 plt_type = PLT_OLD;
4017 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4018 if (is_ppc_elf (ibfd))
4019 {
4020 if (ppc_elf_tdata (ibfd)->has_rel16)
4021 plt_type = PLT_NEW;
4022 else if (ppc_elf_tdata (ibfd)->makes_plt_call)
4023 {
4024 plt_type = PLT_OLD;
4025 htab->old_bfd = ibfd;
4026 break;
4027 }
4028 }
4029 htab->plt_type = plt_type;
4030 }
4031 }
4032 if (htab->plt_type == PLT_OLD && htab->params->plt_style == PLT_NEW)
4033 {
4034 if (htab->old_bfd != NULL)
4035 _bfd_error_handler (_("bss-plt forced due to %pB"), htab->old_bfd);
4036 else
4037 _bfd_error_handler (_("bss-plt forced by profiling"));
4038 }
4039
4040 BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
4041
4042 if (htab->plt_type == PLT_NEW)
4043 {
4044 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4045 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4046
4047 /* The new PLT is a loaded section. */
4048 if (htab->elf.splt != NULL
4049 && !bfd_set_section_flags (htab->elf.splt, flags))
4050 return -1;
4051
4052 /* The new GOT is not executable. */
4053 if (htab->elf.sgot != NULL
4054 && !bfd_set_section_flags (htab->elf.sgot, flags))
4055 return -1;
4056 }
4057 else
4058 {
4059 /* Stop an unused .glink section from affecting .text alignment. */
4060 if (htab->glink != NULL
4061 && !bfd_set_section_alignment (htab->glink, 0))
4062 return -1;
4063 }
4064 return htab->plt_type == PLT_NEW;
4065 }
4066
4067 /* Return the section that should be marked against GC for a given
4069 relocation. */
4070
4071 static asection *
4072 ppc_elf_gc_mark_hook (asection *sec,
4073 struct bfd_link_info *info,
4074 Elf_Internal_Rela *rel,
4075 struct elf_link_hash_entry *h,
4076 Elf_Internal_Sym *sym)
4077 {
4078 if (h != NULL)
4079 switch (ELF32_R_TYPE (rel->r_info))
4080 {
4081 case R_PPC_GNU_VTINHERIT:
4082 case R_PPC_GNU_VTENTRY:
4083 return NULL;
4084 }
4085
4086 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4087 }
4088
4089 static bool
4090 get_sym_h (struct elf_link_hash_entry **hp,
4091 Elf_Internal_Sym **symp,
4092 asection **symsecp,
4093 unsigned char **tls_maskp,
4094 Elf_Internal_Sym **locsymsp,
4095 unsigned long r_symndx,
4096 bfd *ibfd)
4097 {
4098 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4099
4100 if (r_symndx >= symtab_hdr->sh_info)
4101 {
4102 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4103 struct elf_link_hash_entry *h;
4104
4105 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4106 while (h->root.type == bfd_link_hash_indirect
4107 || h->root.type == bfd_link_hash_warning)
4108 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4109
4110 if (hp != NULL)
4111 *hp = h;
4112
4113 if (symp != NULL)
4114 *symp = NULL;
4115
4116 if (symsecp != NULL)
4117 {
4118 asection *symsec = NULL;
4119 if (h->root.type == bfd_link_hash_defined
4120 || h->root.type == bfd_link_hash_defweak)
4121 symsec = h->root.u.def.section;
4122 *symsecp = symsec;
4123 }
4124
4125 if (tls_maskp != NULL)
4126 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
4127 }
4128 else
4129 {
4130 Elf_Internal_Sym *sym;
4131 Elf_Internal_Sym *locsyms = *locsymsp;
4132
4133 if (locsyms == NULL)
4134 {
4135 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4136 if (locsyms == NULL)
4137 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4138 symtab_hdr->sh_info,
4139 0, NULL, NULL, NULL);
4140 if (locsyms == NULL)
4141 return false;
4142 *locsymsp = locsyms;
4143 }
4144 sym = locsyms + r_symndx;
4145
4146 if (hp != NULL)
4147 *hp = NULL;
4148
4149 if (symp != NULL)
4150 *symp = sym;
4151
4152 if (symsecp != NULL)
4153 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
4154
4155 if (tls_maskp != NULL)
4156 {
4157 bfd_signed_vma *local_got;
4158 unsigned char *tls_mask;
4159
4160 tls_mask = NULL;
4161 local_got = elf_local_got_refcounts (ibfd);
4162 if (local_got != NULL)
4163 {
4164 struct plt_entry **local_plt = (struct plt_entry **)
4165 (local_got + symtab_hdr->sh_info);
4166 unsigned char *lgot_masks = (unsigned char *)
4167 (local_plt + symtab_hdr->sh_info);
4168 tls_mask = &lgot_masks[r_symndx];
4169 }
4170 *tls_maskp = tls_mask;
4171 }
4172 }
4173 return true;
4174 }
4175
4176 /* Analyze inline PLT call relocations to see whether calls to locally
4178 defined functions can be converted to direct calls. */
4179
4180 bool
4181 ppc_elf_inline_plt (struct bfd_link_info *info)
4182 {
4183 struct ppc_elf_link_hash_table *htab;
4184 bfd *ibfd;
4185 asection *sec;
4186 bfd_vma low_vma, high_vma, limit;
4187
4188 htab = ppc_elf_hash_table (info);
4189 if (htab == NULL)
4190 return false;
4191
4192 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
4193 reduced somewhat to cater for possible stubs that might be added
4194 between the call and its destination. */
4195 limit = 0x1e00000;
4196 low_vma = -1;
4197 high_vma = 0;
4198 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
4199 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
4200 {
4201 if (low_vma > sec->vma)
4202 low_vma = sec->vma;
4203 if (high_vma < sec->vma + sec->size)
4204 high_vma = sec->vma + sec->size;
4205 }
4206
4207 /* If a "bl" can reach anywhere in local code sections, then we can
4208 convert all inline PLT sequences to direct calls when the symbol
4209 is local. */
4210 if (high_vma - low_vma < limit)
4211 {
4212 htab->can_convert_all_inline_plt = 1;
4213 return true;
4214 }
4215
4216 /* Otherwise, go looking through relocs for cases where a direct
4217 call won't reach. Mark the symbol on any such reloc to disable
4218 the optimization and keep the PLT entry as it seems likely that
4219 this will be better than creating trampolines. Note that this
4220 will disable the optimization for all inline PLT calls to a
4221 particular symbol, not just those that won't reach. The
4222 difficulty in doing a more precise optimization is that the
4223 linker needs to make a decision depending on whether a
4224 particular R_PPC_PLTCALL insn can be turned into a direct
4225 call, for each of the R_PPC_PLTSEQ and R_PPC_PLT16* insns in
4226 the sequence, and there is nothing that ties those relocs
4227 together except their symbol. */
4228
4229 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4230 {
4231 Elf_Internal_Shdr *symtab_hdr;
4232 Elf_Internal_Sym *local_syms;
4233
4234 if (!is_ppc_elf (ibfd))
4235 continue;
4236
4237 local_syms = NULL;
4238 symtab_hdr = &elf_symtab_hdr (ibfd);
4239
4240 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4241 if (sec->has_pltcall
4242 && !bfd_is_abs_section (sec->output_section))
4243 {
4244 Elf_Internal_Rela *relstart, *rel, *relend;
4245
4246 /* Read the relocations. */
4247 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4248 info->keep_memory);
4249 if (relstart == NULL)
4250 return false;
4251
4252 relend = relstart + sec->reloc_count;
4253 for (rel = relstart; rel < relend; rel++)
4254 {
4255 enum elf_ppc_reloc_type r_type;
4256 unsigned long r_symndx;
4257 asection *sym_sec;
4258 struct elf_link_hash_entry *h;
4259 Elf_Internal_Sym *sym;
4260 unsigned char *tls_maskp;
4261
4262 r_type = ELF32_R_TYPE (rel->r_info);
4263 if (r_type != R_PPC_PLTCALL)
4264 continue;
4265
4266 r_symndx = ELF32_R_SYM (rel->r_info);
4267 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
4268 r_symndx, ibfd))
4269 {
4270 if (elf_section_data (sec)->relocs != relstart)
4271 free (relstart);
4272 if (symtab_hdr->contents != (unsigned char *) local_syms)
4273 free (local_syms);
4274 return false;
4275 }
4276
4277 if (sym_sec != NULL && sym_sec->output_section != NULL)
4278 {
4279 bfd_vma from, to;
4280 if (h != NULL)
4281 to = h->root.u.def.value;
4282 else
4283 to = sym->st_value;
4284 to += (rel->r_addend
4285 + sym_sec->output_offset
4286 + sym_sec->output_section->vma);
4287 from = (rel->r_offset
4288 + sec->output_offset
4289 + sec->output_section->vma);
4290 if (to - from + limit < 2 * limit)
4291 *tls_maskp &= ~PLT_KEEP;
4292 }
4293 }
4294 if (elf_section_data (sec)->relocs != relstart)
4295 free (relstart);
4296 }
4297
4298 if (local_syms != NULL
4299 && symtab_hdr->contents != (unsigned char *) local_syms)
4300 {
4301 if (!info->keep_memory)
4302 free (local_syms);
4303 else
4304 symtab_hdr->contents = (unsigned char *) local_syms;
4305 }
4306 }
4307
4308 return true;
4309 }
4310
4311 /* Set plt output section type, htab->tls_get_addr, and call the
4312 generic ELF tls_setup function. */
4313
4314 asection *
4315 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
4316 {
4317 struct ppc_elf_link_hash_table *htab;
4318
4319 htab = ppc_elf_hash_table (info);
4320 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4321 false, false, true);
4322 if (htab->plt_type != PLT_NEW)
4323 htab->params->no_tls_get_addr_opt = true;
4324
4325 if (!htab->params->no_tls_get_addr_opt)
4326 {
4327 struct elf_link_hash_entry *opt, *tga;
4328 opt = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
4329 false, false, true);
4330 if (opt != NULL
4331 && (opt->root.type == bfd_link_hash_defined
4332 || opt->root.type == bfd_link_hash_defweak))
4333 {
4334 /* If glibc supports an optimized __tls_get_addr call stub,
4335 signalled by the presence of __tls_get_addr_opt, and we'll
4336 be calling __tls_get_addr via a plt call stub, then
4337 make __tls_get_addr point to __tls_get_addr_opt. */
4338 tga = htab->tls_get_addr;
4339 if (htab->elf.dynamic_sections_created
4340 && tga != NULL
4341 && (tga->type == STT_FUNC
4342 || tga->needs_plt)
4343 && !(SYMBOL_CALLS_LOCAL (info, tga)
4344 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga)))
4345 {
4346 struct plt_entry *ent;
4347 for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
4348 if (ent->plt.refcount > 0)
4349 break;
4350 if (ent != NULL)
4351 {
4352 tga->root.type = bfd_link_hash_indirect;
4353 tga->root.u.i.link = &opt->root;
4354 ppc_elf_copy_indirect_symbol (info, opt, tga);
4355 opt->mark = 1;
4356 if (opt->dynindx != -1)
4357 {
4358 /* Use __tls_get_addr_opt in dynamic relocations. */
4359 opt->dynindx = -1;
4360 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4361 opt->dynstr_index);
4362 if (!bfd_elf_link_record_dynamic_symbol (info, opt))
4363 return false;
4364 }
4365 htab->tls_get_addr = opt;
4366 }
4367 }
4368 }
4369 else
4370 htab->params->no_tls_get_addr_opt = true;
4371 }
4372 if (htab->plt_type == PLT_NEW
4373 && htab->elf.splt != NULL
4374 && htab->elf.splt->output_section != NULL)
4375 {
4376 elf_section_type (htab->elf.splt->output_section) = SHT_PROGBITS;
4377 elf_section_flags (htab->elf.splt->output_section) = SHF_ALLOC + SHF_WRITE;
4378 }
4379
4380 return _bfd_elf_tls_setup (obfd, info);
4381 }
4382
4383 /* Return TRUE iff REL is a branch reloc with a global symbol matching
4384 HASH. */
4385
4386 static bool
4387 branch_reloc_hash_match (const bfd *ibfd,
4388 const Elf_Internal_Rela *rel,
4389 const struct elf_link_hash_entry *hash)
4390 {
4391 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4392 enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
4393 unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
4394
4395 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
4396 {
4397 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4398 struct elf_link_hash_entry *h;
4399
4400 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4401 while (h->root.type == bfd_link_hash_indirect
4402 || h->root.type == bfd_link_hash_warning)
4403 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4404 if (h == hash)
4405 return true;
4406 }
4407 return false;
4408 }
4409
4410 /* Run through all the TLS relocs looking for optimization
4411 opportunities. */
4412
4413 bool
4414 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4415 struct bfd_link_info *info)
4416 {
4417 bfd *ibfd;
4418 asection *sec;
4419 struct ppc_elf_link_hash_table *htab;
4420 int pass;
4421
4422 if (!bfd_link_executable (info))
4423 return true;
4424
4425 htab = ppc_elf_hash_table (info);
4426 if (htab == NULL)
4427 return false;
4428
4429 htab->do_tls_opt = 1;
4430
4431 /* Make two passes through the relocs. First time check that tls
4432 relocs involved in setting up a tls_get_addr call are indeed
4433 followed by such a call. If they are not, don't do any tls
4434 optimization. On the second pass twiddle tls_mask flags to
4435 notify relocate_section that optimization can be done, and
4436 adjust got and plt refcounts. */
4437 for (pass = 0; pass < 2; ++pass)
4438 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4439 {
4440 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4441 asection *got2 = bfd_get_section_by_name (ibfd, ".got2");
4442
4443 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4444 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4445 {
4446 Elf_Internal_Rela *relstart, *rel, *relend;
4447 int expecting_tls_get_addr = 0;
4448
4449 /* Read the relocations. */
4450 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4451 info->keep_memory);
4452 if (relstart == NULL)
4453 return false;
4454
4455 relend = relstart + sec->reloc_count;
4456 for (rel = relstart; rel < relend; rel++)
4457 {
4458 enum elf_ppc_reloc_type r_type;
4459 unsigned long r_symndx;
4460 struct elf_link_hash_entry *h = NULL;
4461 unsigned char *tls_mask;
4462 unsigned char tls_set, tls_clear;
4463 bool is_local;
4464 bfd_signed_vma *got_count;
4465
4466 r_symndx = ELF32_R_SYM (rel->r_info);
4467 if (r_symndx >= symtab_hdr->sh_info)
4468 {
4469 struct elf_link_hash_entry **sym_hashes;
4470
4471 sym_hashes = elf_sym_hashes (ibfd);
4472 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4473 while (h->root.type == bfd_link_hash_indirect
4474 || h->root.type == bfd_link_hash_warning)
4475 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4476 }
4477
4478 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
4479 r_type = ELF32_R_TYPE (rel->r_info);
4480 /* If this section has old-style __tls_get_addr calls
4481 without marker relocs, then check that each
4482 __tls_get_addr call reloc is preceded by a reloc
4483 that conceivably belongs to the __tls_get_addr arg
4484 setup insn. If we don't find matching arg setup
4485 relocs, don't do any tls optimization. */
4486 if (pass == 0
4487 && sec->nomark_tls_get_addr
4488 && h != NULL
4489 && h == htab->tls_get_addr
4490 && !expecting_tls_get_addr
4491 && is_branch_reloc (r_type))
4492 {
4493 info->callbacks->minfo ("%H __tls_get_addr lost arg, "
4494 "TLS optimization disabled\n",
4495 ibfd, sec, rel->r_offset);
4496 if (elf_section_data (sec)->relocs != relstart)
4497 free (relstart);
4498 return true;
4499 }
4500
4501 expecting_tls_get_addr = 0;
4502 switch (r_type)
4503 {
4504 case R_PPC_GOT_TLSLD16:
4505 case R_PPC_GOT_TLSLD16_LO:
4506 expecting_tls_get_addr = 1;
4507 /* Fall through. */
4508
4509 case R_PPC_GOT_TLSLD16_HI:
4510 case R_PPC_GOT_TLSLD16_HA:
4511 /* These relocs should never be against a symbol
4512 defined in a shared lib. Leave them alone if
4513 that turns out to be the case. */
4514 if (!is_local)
4515 continue;
4516
4517 /* LD -> LE */
4518 tls_set = 0;
4519 tls_clear = TLS_LD;
4520 break;
4521
4522 case R_PPC_GOT_TLSGD16:
4523 case R_PPC_GOT_TLSGD16_LO:
4524 expecting_tls_get_addr = 1;
4525 /* Fall through. */
4526
4527 case R_PPC_GOT_TLSGD16_HI:
4528 case R_PPC_GOT_TLSGD16_HA:
4529 if (is_local)
4530 /* GD -> LE */
4531 tls_set = 0;
4532 else
4533 /* GD -> IE */
4534 tls_set = TLS_TLS | TLS_GDIE;
4535 tls_clear = TLS_GD;
4536 break;
4537
4538 case R_PPC_GOT_TPREL16:
4539 case R_PPC_GOT_TPREL16_LO:
4540 case R_PPC_GOT_TPREL16_HI:
4541 case R_PPC_GOT_TPREL16_HA:
4542 if (is_local)
4543 {
4544 /* IE -> LE */
4545 tls_set = 0;
4546 tls_clear = TLS_TPREL;
4547 break;
4548 }
4549 else
4550 continue;
4551
4552 case R_PPC_TLSLD:
4553 if (!is_local)
4554 continue;
4555 /* Fall through. */
4556 case R_PPC_TLSGD:
4557 if (rel + 1 < relend
4558 && is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
4559 {
4560 if (pass != 0
4561 && ELF32_R_TYPE (rel[1].r_info) != R_PPC_PLTSEQ)
4562 {
4563 r_type = ELF32_R_TYPE (rel[1].r_info);
4564 r_symndx = ELF32_R_SYM (rel[1].r_info);
4565 if (r_symndx >= symtab_hdr->sh_info)
4566 {
4567 struct elf_link_hash_entry **sym_hashes;
4568
4569 sym_hashes = elf_sym_hashes (ibfd);
4570 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4571 while (h->root.type == bfd_link_hash_indirect
4572 || h->root.type == bfd_link_hash_warning)
4573 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4574 if (h != NULL)
4575 {
4576 struct plt_entry *ent = NULL;
4577 bfd_vma addend = 0;
4578
4579 if (bfd_link_pic (info))
4580 addend = rel->r_addend;
4581 ent = find_plt_ent (&h->plt.plist,
4582 got2, addend);
4583 if (ent != NULL
4584 && ent->plt.refcount > 0)
4585 ent->plt.refcount -= 1;
4586 }
4587 }
4588 }
4589 continue;
4590 }
4591 expecting_tls_get_addr = 2;
4592 tls_set = 0;
4593 tls_clear = 0;
4594 break;
4595
4596 case R_PPC_TPREL16_HA:
4597 if (pass == 0)
4598 {
4599 unsigned char buf[4];
4600 unsigned int insn;
4601 bfd_vma off = rel->r_offset & ~3;
4602 if (!bfd_get_section_contents (ibfd, sec, buf,
4603 off, 4))
4604 {
4605 if (elf_section_data (sec)->relocs != relstart)
4606 free (relstart);
4607 return false;
4608 }
4609 insn = bfd_get_32 (ibfd, buf);
4610 /* addis rt,2,imm */
4611 if ((insn & ((0x3fu << 26) | 0x1f << 16))
4612 != ((15u << 26) | (2 << 16)))
4613 {
4614 /* xgettext:c-format */
4615 info->callbacks->minfo
4616 (_("%H: warning: %s unexpected insn %#x.\n"),
4617 ibfd, sec, off, "R_PPC_TPREL16_HA", insn);
4618 htab->do_tls_opt = 0;
4619 }
4620 }
4621 continue;
4622
4623 case R_PPC_TPREL16_HI:
4624 htab->do_tls_opt = 0;
4625 continue;
4626
4627 default:
4628 continue;
4629 }
4630
4631 if (pass == 0)
4632 {
4633 if (!expecting_tls_get_addr
4634 || !sec->nomark_tls_get_addr)
4635 continue;
4636
4637 if (rel + 1 < relend
4638 && branch_reloc_hash_match (ibfd, rel + 1,
4639 htab->tls_get_addr))
4640 continue;
4641
4642 /* Uh oh, we didn't find the expected call. We
4643 could just mark this symbol to exclude it
4644 from tls optimization but it's safer to skip
4645 the entire optimization. */
4646 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
4647 "TLS optimization disabled\n"),
4648 ibfd, sec, rel->r_offset);
4649 if (elf_section_data (sec)->relocs != relstart)
4650 free (relstart);
4651 return true;
4652 }
4653
4654 if (h != NULL)
4655 {
4656 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4657 got_count = &h->got.refcount;
4658 }
4659 else
4660 {
4661 bfd_signed_vma *lgot_refs;
4662 struct plt_entry **local_plt;
4663 unsigned char *lgot_masks;
4664
4665 lgot_refs = elf_local_got_refcounts (ibfd);
4666 if (lgot_refs == NULL)
4667 abort ();
4668 local_plt = (struct plt_entry **)
4669 (lgot_refs + symtab_hdr->sh_info);
4670 lgot_masks = (unsigned char *)
4671 (local_plt + symtab_hdr->sh_info);
4672 tls_mask = &lgot_masks[r_symndx];
4673 got_count = &lgot_refs[r_symndx];
4674 }
4675
4676 /* If we don't have old-style __tls_get_addr calls
4677 without TLSGD/TLSLD marker relocs, and we haven't
4678 found a new-style __tls_get_addr call with a
4679 marker for this symbol, then we either have a
4680 broken object file or an -mlongcall style
4681 indirect call to __tls_get_addr without a marker.
4682 Disable optimization in this case. */
4683 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
4684 && !sec->nomark_tls_get_addr
4685 && ((*tls_mask & (TLS_TLS | TLS_MARK))
4686 != (TLS_TLS | TLS_MARK)))
4687 continue;
4688
4689 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
4690 {
4691 struct plt_entry *ent;
4692 bfd_vma addend = 0;
4693
4694 if (bfd_link_pic (info)
4695 && (ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTREL24
4696 || ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTCALL))
4697 addend = rel[1].r_addend;
4698 ent = find_plt_ent (&htab->tls_get_addr->plt.plist,
4699 got2, addend);
4700 if (ent != NULL && ent->plt.refcount > 0)
4701 ent->plt.refcount -= 1;
4702 }
4703 if (tls_clear == 0)
4704 continue;
4705
4706 if (tls_set == 0)
4707 {
4708 /* We managed to get rid of a got entry. */
4709 if (*got_count > 0)
4710 *got_count -= 1;
4711 }
4712
4713 *tls_mask |= tls_set;
4714 *tls_mask &= ~tls_clear;
4715 }
4716
4717 if (elf_section_data (sec)->relocs != relstart)
4718 free (relstart);
4719 }
4720 }
4721 return true;
4722 }
4723
4724 /* Return true if we have dynamic relocs against H or any of its weak
4726 aliases, that apply to read-only sections. Cannot be used after
4727 size_dynamic_sections. */
4728
4729 static bool
4730 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
4731 {
4732 struct ppc_elf_link_hash_entry *eh = ppc_elf_hash_entry (h);
4733 do
4734 {
4735 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
4736 return true;
4737 eh = ppc_elf_hash_entry (eh->elf.u.alias);
4738 } while (eh != NULL && &eh->elf != h);
4739
4740 return false;
4741 }
4742
4743 /* Return whether H has pc-relative dynamic relocs. */
4744
4745 static bool
4746 pc_dynrelocs (struct elf_link_hash_entry *h)
4747 {
4748 struct elf_dyn_relocs *p;
4749
4750 for (p = h->dyn_relocs; p != NULL; p = p->next)
4751 if (p->pc_count != 0)
4752 return true;
4753 return false;
4754 }
4755
4756 /* Adjust a symbol defined by a dynamic object and referenced by a
4757 regular object. The current definition is in some section of the
4758 dynamic object, but we're not including those sections. We have to
4759 change the definition to something the rest of the link can
4760 understand. */
4761
4762 static bool
4763 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4764 struct elf_link_hash_entry *h)
4765 {
4766 struct ppc_elf_link_hash_table *htab;
4767 asection *s;
4768
4769 #ifdef DEBUG
4770 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
4771 h->root.root.string);
4772 #endif
4773
4774 /* Make sure we know what is going on here. */
4775 htab = ppc_elf_hash_table (info);
4776 BFD_ASSERT (htab->elf.dynobj != NULL
4777 && (h->needs_plt
4778 || h->type == STT_GNU_IFUNC
4779 || h->is_weakalias
4780 || (h->def_dynamic
4781 && h->ref_regular
4782 && !h->def_regular)));
4783
4784 /* Deal with function syms. */
4785 if (h->type == STT_FUNC
4786 || h->type == STT_GNU_IFUNC
4787 || h->needs_plt)
4788 {
4789 bool local = (SYMBOL_CALLS_LOCAL (info, h)
4790 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
4791 /* Discard dyn_relocs when non-pic if we've decided that a
4792 function symbol is local. */
4793 if (!bfd_link_pic (info) && local)
4794 h->dyn_relocs = NULL;
4795
4796 /* Clear procedure linkage table information for any symbol that
4797 won't need a .plt entry. */
4798 struct plt_entry *ent;
4799 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4800 if (ent->plt.refcount > 0)
4801 break;
4802 if (ent == NULL
4803 || (h->type != STT_GNU_IFUNC
4804 && local
4805 && (htab->can_convert_all_inline_plt
4806 || (ppc_elf_hash_entry (h)->tls_mask
4807 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
4808 {
4809 /* A PLT entry is not required/allowed when:
4810
4811 1. We are not using ld.so; because then the PLT entry
4812 can't be set up, so we can't use one. In this case,
4813 ppc_elf_adjust_dynamic_symbol won't even be called.
4814
4815 2. GC has rendered the entry unused.
4816
4817 3. We know for certain that a call to this symbol
4818 will go to this object, or will remain undefined. */
4819 h->plt.plist = NULL;
4820 h->needs_plt = 0;
4821 h->pointer_equality_needed = 0;
4822 }
4823 else
4824 {
4825 /* Taking a function's address in a read/write section
4826 doesn't require us to define the function symbol in the
4827 executable on a plt call stub. A dynamic reloc can
4828 be used instead, giving better runtime performance.
4829 (Calls via that function pointer don't need to bounce
4830 through the plt call stub.) Similarly, use a dynamic
4831 reloc for a weak reference when possible, allowing the
4832 resolution of the symbol to be set at load time rather
4833 than link time. */
4834 if ((h->pointer_equality_needed
4835 || (h->non_got_ref
4836 && !h->ref_regular_nonweak
4837 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
4838 && htab->elf.target_os != is_vxworks
4839 && !ppc_elf_hash_entry (h)->has_sda_refs
4840 && !_bfd_elf_readonly_dynrelocs (h))
4841 {
4842 h->pointer_equality_needed = 0;
4843 /* If we haven't seen a branch reloc and the symbol
4844 isn't an ifunc then we don't need a plt entry. */
4845 if (!h->needs_plt && h->type != STT_GNU_IFUNC)
4846 h->plt.plist = NULL;
4847 }
4848 else if (!bfd_link_pic (info))
4849 /* We are going to be defining the function symbol on the
4850 plt stub, so no dyn_relocs needed when non-pic. */
4851 h->dyn_relocs = NULL;
4852 }
4853 h->protected_def = 0;
4854 /* Function symbols can't have copy relocs. */
4855 return true;
4856 }
4857 else
4858 h->plt.plist = NULL;
4859
4860 /* If this is a weak symbol, and there is a real definition, the
4861 processor independent code will have arranged for us to see the
4862 real definition first, and we can just use the same value. */
4863 if (h->is_weakalias)
4864 {
4865 struct elf_link_hash_entry *def = weakdef (h);
4866 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4867 h->root.u.def.section = def->root.u.def.section;
4868 h->root.u.def.value = def->root.u.def.value;
4869 if (def->root.u.def.section == htab->elf.sdynbss
4870 || def->root.u.def.section == htab->elf.sdynrelro
4871 || def->root.u.def.section == htab->dynsbss)
4872 h->dyn_relocs = NULL;
4873 return true;
4874 }
4875
4876 /* This is a reference to a symbol defined by a dynamic object which
4877 is not a function. */
4878
4879 /* If we are creating a shared library, we must presume that the
4880 only references to the symbol are via the global offset table.
4881 For such cases we need not do anything here; the relocations will
4882 be handled correctly by relocate_section. */
4883 if (bfd_link_pic (info))
4884 {
4885 h->protected_def = 0;
4886 return true;
4887 }
4888
4889 /* If there are no references to this symbol that do not use the
4890 GOT, we don't need to generate a copy reloc. */
4891 if (!h->non_got_ref)
4892 {
4893 h->protected_def = 0;
4894 return true;
4895 }
4896
4897 /* Protected variables do not work with .dynbss. The copy in
4898 .dynbss won't be used by the shared library with the protected
4899 definition for the variable. Editing to PIC, or text relocations
4900 are preferable to an incorrect program. */
4901 if (h->protected_def)
4902 {
4903 if (ELIMINATE_COPY_RELOCS
4904 && ppc_elf_hash_entry (h)->has_addr16_ha
4905 && ppc_elf_hash_entry (h)->has_addr16_lo
4906 && htab->params->pic_fixup == 0
4907 && info->disable_target_specific_optimizations <= 1)
4908 htab->params->pic_fixup = 1;
4909 return true;
4910 }
4911
4912 /* If -z nocopyreloc was given, we won't generate them either. */
4913 if (info->nocopyreloc)
4914 return true;
4915
4916 /* If we don't find any dynamic relocs in read-only sections, then
4917 we'll be keeping the dynamic relocs and avoiding the copy reloc.
4918 We can't do this if there are any small data relocations. This
4919 doesn't work on VxWorks, where we can not have dynamic
4920 relocations (other than copy and jump slot relocations) in an
4921 executable. */
4922 if (ELIMINATE_COPY_RELOCS
4923 && !ppc_elf_hash_entry (h)->has_sda_refs
4924 && htab->elf.target_os != is_vxworks
4925 && !h->def_regular
4926 && !alias_readonly_dynrelocs (h))
4927 return true;
4928
4929 /* We must allocate the symbol in our .dynbss section, which will
4930 become part of the .bss section of the executable. There will be
4931 an entry for this symbol in the .dynsym section. The dynamic
4932 object will contain position independent code, so all references
4933 from the dynamic object to this symbol will go through the global
4934 offset table. The dynamic linker will use the .dynsym entry to
4935 determine the address it must put in the global offset table, so
4936 both the dynamic object and the regular object will refer to the
4937 same memory location for the variable.
4938
4939 Of course, if the symbol is referenced using SDAREL relocs, we
4940 must instead allocate it in .sbss. */
4941 if (ppc_elf_hash_entry (h)->has_sda_refs)
4942 s = htab->dynsbss;
4943 else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
4944 s = htab->elf.sdynrelro;
4945 else
4946 s = htab->elf.sdynbss;
4947 BFD_ASSERT (s != NULL);
4948
4949 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
4950 {
4951 asection *srel;
4952
4953 /* We must generate a R_PPC_COPY reloc to tell the dynamic
4954 linker to copy the initial value out of the dynamic object
4955 and into the runtime process image. */
4956 if (ppc_elf_hash_entry (h)->has_sda_refs)
4957 srel = htab->relsbss;
4958 else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
4959 srel = htab->elf.sreldynrelro;
4960 else
4961 srel = htab->elf.srelbss;
4962 BFD_ASSERT (srel != NULL);
4963 srel->size += sizeof (Elf32_External_Rela);
4964 h->needs_copy = 1;
4965 }
4966
4967 /* We no longer want dyn_relocs. */
4968 h->dyn_relocs = NULL;
4969 return _bfd_elf_adjust_dynamic_copy (info, h, s);
4970 }
4971
4972 /* Generate a symbol to mark plt call stubs. For non-PIC code the sym is
4974 xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
4975 specifying the addend on the plt relocation. For -fpic code, the sym
4976 is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
4977 xxxxxxxx.got2.plt_pic32.<callee>. */
4978
4979 static bool
4980 add_stub_sym (struct plt_entry *ent,
4981 struct elf_link_hash_entry *h,
4982 struct bfd_link_info *info)
4983 {
4984 struct elf_link_hash_entry *sh;
4985 size_t len1, len2, len3;
4986 char *name;
4987 const char *stub;
4988 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
4989
4990 if (bfd_link_pic (info))
4991 stub = ".plt_pic32.";
4992 else
4993 stub = ".plt_call32.";
4994
4995 len1 = strlen (h->root.root.string);
4996 len2 = strlen (stub);
4997 len3 = 0;
4998 if (ent->sec)
4999 len3 = strlen (ent->sec->name);
5000 name = bfd_malloc (len1 + len2 + len3 + 9);
5001 if (name == NULL)
5002 return false;
5003 sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
5004 if (ent->sec)
5005 memcpy (name + 8, ent->sec->name, len3);
5006 memcpy (name + 8 + len3, stub, len2);
5007 memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
5008 sh = elf_link_hash_lookup (&htab->elf, name, true, false, false);
5009 if (sh == NULL)
5010 return false;
5011 if (sh->root.type == bfd_link_hash_new)
5012 {
5013 sh->root.type = bfd_link_hash_defined;
5014 sh->root.u.def.section = htab->glink;
5015 sh->root.u.def.value = ent->glink_offset;
5016 sh->ref_regular = 1;
5017 sh->def_regular = 1;
5018 sh->ref_regular_nonweak = 1;
5019 sh->forced_local = 1;
5020 sh->non_elf = 0;
5021 sh->root.linker_def = 1;
5022 }
5023 return true;
5024 }
5025
5026 /* Allocate NEED contiguous space in .got, and return the offset.
5027 Handles allocation of the got header when crossing 32k. */
5028
5029 static bfd_vma
5030 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
5031 {
5032 bfd_vma where;
5033 unsigned int max_before_header;
5034
5035 if (htab->plt_type == PLT_VXWORKS)
5036 {
5037 where = htab->elf.sgot->size;
5038 htab->elf.sgot->size += need;
5039 }
5040 else
5041 {
5042 max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
5043 if (need <= htab->got_gap)
5044 {
5045 where = max_before_header - htab->got_gap;
5046 htab->got_gap -= need;
5047 }
5048 else
5049 {
5050 if (htab->elf.sgot->size + need > max_before_header
5051 && htab->elf.sgot->size <= max_before_header)
5052 {
5053 htab->got_gap = max_before_header - htab->elf.sgot->size;
5054 htab->elf.sgot->size = max_before_header + htab->got_header_size;
5055 }
5056 where = htab->elf.sgot->size;
5057 htab->elf.sgot->size += need;
5058 }
5059 }
5060 return where;
5061 }
5062
5063 /* Calculate size of GOT entries for symbol given its TLS_MASK.
5064 TLS_LD is excluded because those go in a special GOT slot. */
5065
5066 static inline unsigned int
5067 got_entries_needed (int tls_mask)
5068 {
5069 unsigned int need;
5070 if ((tls_mask & TLS_TLS) == 0)
5071 need = 4;
5072 else
5073 {
5074 need = 0;
5075 if ((tls_mask & TLS_GD) != 0)
5076 need += 8;
5077 if ((tls_mask & (TLS_TPREL | TLS_GDIE)) != 0)
5078 need += 4;
5079 if ((tls_mask & TLS_DTPREL) != 0)
5080 need += 4;
5081 }
5082 return need;
5083 }
5084
5085 /* If H is undefined, make it dynamic if that makes sense. */
5086
5087 static bool
5088 ensure_undef_dynamic (struct bfd_link_info *info,
5089 struct elf_link_hash_entry *h)
5090 {
5091 struct elf_link_hash_table *htab = elf_hash_table (info);
5092
5093 if (htab->dynamic_sections_created
5094 && ((info->dynamic_undefined_weak != 0
5095 && h->root.type == bfd_link_hash_undefweak)
5096 || h->root.type == bfd_link_hash_undefined)
5097 && h->dynindx == -1
5098 && !h->forced_local
5099 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
5100 return bfd_elf_link_record_dynamic_symbol (info, h);
5101 return true;
5102 }
5103
5104 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
5105 usual htab->elf.splt section for a PLT entry. */
5106
5107 static inline
5108 bool use_local_plt (struct bfd_link_info *info,
5109 struct elf_link_hash_entry *h)
5110 {
5111 return (h == NULL
5112 || h->dynindx == -1
5113 || !elf_hash_table (info)->dynamic_sections_created);
5114 }
5115
5116 /* Allocate space in associated reloc sections for dynamic relocs. */
5117
5118 static bool
5119 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5120 {
5121 struct bfd_link_info *info = inf;
5122 struct ppc_elf_link_hash_entry *eh;
5123 struct ppc_elf_link_hash_table *htab;
5124 struct elf_dyn_relocs *p;
5125
5126 if (h->root.type == bfd_link_hash_indirect)
5127 return true;
5128
5129 htab = ppc_elf_hash_table (info);
5130 eh = (struct ppc_elf_link_hash_entry *) h;
5131 if (eh->elf.got.refcount > 0
5132 || (ELIMINATE_COPY_RELOCS
5133 && !eh->elf.def_regular
5134 && eh->elf.protected_def
5135 && eh->has_addr16_ha
5136 && eh->has_addr16_lo
5137 && htab->params->pic_fixup > 0))
5138 {
5139 unsigned int need;
5140
5141 /* Make sure this symbol is output as a dynamic symbol. */
5142 if (!ensure_undef_dynamic (info, &eh->elf))
5143 return false;
5144
5145 need = 0;
5146 if ((eh->tls_mask & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
5147 {
5148 if (SYMBOL_REFERENCES_LOCAL (info, &eh->elf))
5149 /* We'll just use htab->tlsld_got.offset. This should
5150 always be the case. It's a little odd if we have
5151 a local dynamic reloc against a non-local symbol. */
5152 htab->tlsld_got.refcount += 1;
5153 else
5154 need += 8;
5155 }
5156 need += got_entries_needed (eh->tls_mask);
5157 if (need == 0)
5158 eh->elf.got.offset = (bfd_vma) -1;
5159 else
5160 {
5161 eh->elf.got.offset = allocate_got (htab, need);
5162 if (((bfd_link_pic (info)
5163 && !((eh->tls_mask & TLS_TLS) != 0
5164 && bfd_link_executable (info)
5165 && SYMBOL_REFERENCES_LOCAL (info, &eh->elf))
5166 && !bfd_is_abs_symbol (&h->root))
5167 || (htab->elf.dynamic_sections_created
5168 && eh->elf.dynindx != -1
5169 && !SYMBOL_REFERENCES_LOCAL (info, &eh->elf)))
5170 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &eh->elf))
5171 {
5172 asection *rsec;
5173
5174 need *= sizeof (Elf32_External_Rela) / 4;
5175 if ((eh->tls_mask & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
5176 need -= sizeof (Elf32_External_Rela);
5177 rsec = htab->elf.srelgot;
5178 if (eh->elf.type == STT_GNU_IFUNC)
5179 rsec = htab->elf.irelplt;
5180 rsec->size += need;
5181 }
5182 }
5183 }
5184 else
5185 eh->elf.got.offset = (bfd_vma) -1;
5186
5187 /* If no dynamic sections we can't have dynamic relocs, except for
5188 IFUNCs which are handled even in static executables. */
5189 if (!htab->elf.dynamic_sections_created
5190 && h->type != STT_GNU_IFUNC)
5191 h->dyn_relocs = NULL;
5192
5193 /* Discard relocs on undefined symbols that must be local. */
5194 else if (h->root.type == bfd_link_hash_undefined
5195 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5196 h->dyn_relocs = NULL;
5197
5198 /* Also discard relocs on undefined weak syms with non-default
5199 visibility, or when dynamic_undefined_weak says so. */
5200 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
5201 h->dyn_relocs = NULL;
5202
5203 if (h->dyn_relocs == NULL)
5204 ;
5205
5206 /* In the shared -Bsymbolic case, discard space allocated for
5207 dynamic pc-relative relocs against symbols which turn out to be
5208 defined in regular objects. For the normal shared case, discard
5209 space for relocs that have become local due to symbol visibility
5210 changes. */
5211 else if (bfd_link_pic (info))
5212 {
5213 /* Relocs that use pc_count are those that appear on a call insn,
5214 or certain REL relocs (see must_be_dyn_reloc) that can be
5215 generated via assembly. We want calls to protected symbols to
5216 resolve directly to the function rather than going via the plt.
5217 If people want function pointer comparisons to work as expected
5218 then they should avoid writing weird assembly. */
5219 if (SYMBOL_CALLS_LOCAL (info, h))
5220 {
5221 struct elf_dyn_relocs **pp;
5222
5223 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
5224 {
5225 p->count -= p->pc_count;
5226 p->pc_count = 0;
5227 if (p->count == 0)
5228 *pp = p->next;
5229 else
5230 pp = &p->next;
5231 }
5232 }
5233
5234 if (htab->elf.target_os == is_vxworks)
5235 {
5236 struct elf_dyn_relocs **pp;
5237
5238 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
5239 {
5240 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
5241 *pp = p->next;
5242 else
5243 pp = &p->next;
5244 }
5245 }
5246
5247 if (h->dyn_relocs != NULL)
5248 {
5249 /* Make sure this symbol is output as a dynamic symbol. */
5250 if (!ensure_undef_dynamic (info, h))
5251 return false;
5252 }
5253 }
5254 else if (ELIMINATE_COPY_RELOCS)
5255 {
5256 /* For the non-pic case, discard space for relocs against
5257 symbols which turn out to need copy relocs or are not
5258 dynamic. */
5259 if ((h->dynamic_adjusted
5260 || (h->ref_regular
5261 && h->root.type == bfd_link_hash_undefweak
5262 && (info->dynamic_undefined_weak > 0
5263 || !_bfd_elf_readonly_dynrelocs (h))))
5264 && !h->def_regular
5265 && !ELF_COMMON_DEF_P (h)
5266 && !(h->protected_def
5267 && eh->has_addr16_ha
5268 && eh->has_addr16_lo
5269 && htab->params->pic_fixup > 0))
5270 {
5271 /* Make sure this symbol is output as a dynamic symbol. */
5272 if (!ensure_undef_dynamic (info, h))
5273 return false;
5274
5275 if (h->dynindx == -1)
5276 h->dyn_relocs = NULL;
5277 }
5278 else
5279 h->dyn_relocs = NULL;
5280 }
5281
5282 /* Allocate space. */
5283 for (p = h->dyn_relocs; p != NULL; p = p->next)
5284 if (!discarded_section (p->sec))
5285 {
5286 asection *sreloc = elf_section_data (p->sec)->sreloc;
5287 if (eh->elf.type == STT_GNU_IFUNC)
5288 sreloc = htab->elf.irelplt;
5289 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5290 }
5291
5292 /* Handle PLT relocs. Done last, after dynindx has settled.
5293 We might need a PLT entry when the symbol
5294 a) is dynamic, or
5295 b) is an ifunc, or
5296 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
5297 d) has plt16 relocs and we are linking statically. */
5298 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
5299 || h->type == STT_GNU_IFUNC
5300 || (h->needs_plt && h->dynamic_adjusted)
5301 || (h->needs_plt
5302 && h->def_regular
5303 && !htab->elf.dynamic_sections_created
5304 && !htab->can_convert_all_inline_plt
5305 && (ppc_elf_hash_entry (h)->tls_mask
5306 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
5307 {
5308 struct plt_entry *ent;
5309 bool doneone = false;
5310 bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
5311
5312 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5313 if (ent->plt.refcount > 0)
5314 {
5315 asection *s;
5316 bool dyn;
5317
5318 if (!ensure_undef_dynamic (info, h))
5319 return false;
5320
5321 dyn = !use_local_plt (info, h);
5322 s = htab->elf.splt;
5323 if (!dyn)
5324 {
5325 if (h->type == STT_GNU_IFUNC)
5326 s = htab->elf.iplt;
5327 else
5328 s = htab->pltlocal;
5329 }
5330
5331 if (htab->plt_type == PLT_NEW || !dyn)
5332 {
5333 if (!doneone)
5334 {
5335 plt_offset = s->size;
5336 s->size += 4;
5337 }
5338 ent->plt.offset = plt_offset;
5339
5340 if (s == htab->pltlocal)
5341 ent->glink_offset = glink_offset;
5342 else
5343 {
5344 s = htab->glink;
5345 if (!doneone || bfd_link_pic (info))
5346 {
5347 glink_offset = s->size;
5348 s->size += GLINK_ENTRY_SIZE (htab, h);
5349 }
5350 if (!doneone
5351 && !bfd_link_pic (info)
5352 && h->def_dynamic
5353 && !h->def_regular)
5354 {
5355 h->root.u.def.section = s;
5356 h->root.u.def.value = glink_offset;
5357 }
5358 ent->glink_offset = glink_offset;
5359
5360 if (htab->params->emit_stub_syms
5361 && !add_stub_sym (ent, h, info))
5362 return false;
5363 }
5364 }
5365 else
5366 {
5367 if (!doneone)
5368 {
5369 /* If this is the first .plt entry, make room
5370 for the special first entry. */
5371 if (s->size == 0)
5372 s->size += htab->plt_initial_entry_size;
5373
5374 /* The PowerPC PLT is actually composed of two
5375 parts, the first part is 2 words (for a load
5376 and a jump), and then there is a remaining
5377 word available at the end. */
5378 plt_offset = (htab->plt_initial_entry_size
5379 + (htab->plt_slot_size
5380 * ((s->size
5381 - htab->plt_initial_entry_size)
5382 / htab->plt_entry_size)));
5383
5384 /* If this symbol is not defined in a regular
5385 file, and we are not generating a shared
5386 library, then set the symbol to this location
5387 in the .plt. This is to avoid text
5388 relocations, and is required to make
5389 function pointers compare as equal between
5390 the normal executable and the shared library. */
5391 if (! bfd_link_pic (info)
5392 && h->def_dynamic
5393 && !h->def_regular)
5394 {
5395 h->root.u.def.section = s;
5396 h->root.u.def.value = plt_offset;
5397 }
5398
5399 /* Make room for this entry. */
5400 s->size += htab->plt_entry_size;
5401 /* After the 8192nd entry, room for two entries
5402 is allocated. */
5403 if (htab->plt_type == PLT_OLD
5404 && (s->size - htab->plt_initial_entry_size)
5405 / htab->plt_entry_size
5406 > PLT_NUM_SINGLE_ENTRIES)
5407 s->size += htab->plt_entry_size;
5408 }
5409 ent->plt.offset = plt_offset;
5410 }
5411
5412 /* We also need to make an entry in the .rela.plt section. */
5413 if (!doneone)
5414 {
5415 if (!dyn)
5416 {
5417 if (h->type == STT_GNU_IFUNC)
5418 {
5419 s = htab->elf.irelplt;
5420 s->size += sizeof (Elf32_External_Rela);
5421 }
5422 else if (bfd_link_pic (info))
5423 {
5424 s = htab->relpltlocal;
5425 s->size += sizeof (Elf32_External_Rela);
5426 }
5427 }
5428 else
5429 {
5430 htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
5431
5432 if (htab->plt_type == PLT_VXWORKS)
5433 {
5434 /* Allocate space for the unloaded relocations. */
5435 if (!bfd_link_pic (info)
5436 && htab->elf.dynamic_sections_created)
5437 {
5438 if (ent->plt.offset
5439 == (bfd_vma) htab->plt_initial_entry_size)
5440 {
5441 htab->srelplt2->size
5442 += (sizeof (Elf32_External_Rela)
5443 * VXWORKS_PLTRESOLVE_RELOCS);
5444 }
5445
5446 htab->srelplt2->size
5447 += (sizeof (Elf32_External_Rela)
5448 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
5449 }
5450
5451 /* Every PLT entry has an associated GOT entry in
5452 .got.plt. */
5453 htab->elf.sgotplt->size += 4;
5454 }
5455 }
5456 doneone = true;
5457 }
5458 }
5459 else
5460 ent->plt.offset = (bfd_vma) -1;
5461
5462 if (!doneone)
5463 {
5464 h->plt.plist = NULL;
5465 h->needs_plt = 0;
5466 }
5467 }
5468 else
5469 {
5470 h->plt.plist = NULL;
5471 h->needs_plt = 0;
5472 }
5473
5474 return true;
5475 }
5476
5477 static const unsigned char glink_eh_frame_cie[] =
5478 {
5479 0, 0, 0, 16, /* length. */
5480 0, 0, 0, 0, /* id. */
5481 1, /* CIE version. */
5482 'z', 'R', 0, /* Augmentation string. */
5483 4, /* Code alignment. */
5484 0x7c, /* Data alignment. */
5485 65, /* RA reg. */
5486 1, /* Augmentation size. */
5487 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
5488 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
5489 };
5490
5491 /* Set the sizes of the dynamic sections. */
5492
5493 static bool
5494 ppc_elf_size_dynamic_sections (bfd *output_bfd,
5495 struct bfd_link_info *info)
5496 {
5497 struct ppc_elf_link_hash_table *htab;
5498 asection *s;
5499 bool relocs;
5500 bfd *ibfd;
5501
5502 #ifdef DEBUG
5503 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
5504 #endif
5505
5506 htab = ppc_elf_hash_table (info);
5507 BFD_ASSERT (htab->elf.dynobj != NULL);
5508
5509 if (elf_hash_table (info)->dynamic_sections_created)
5510 {
5511 /* Set the contents of the .interp section to the interpreter. */
5512 if (bfd_link_executable (info) && !info->nointerp)
5513 {
5514 s = bfd_get_linker_section (htab->elf.dynobj, ".interp");
5515 BFD_ASSERT (s != NULL);
5516 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5517 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5518 }
5519 }
5520
5521 if (htab->plt_type == PLT_OLD)
5522 htab->got_header_size = 16;
5523 else if (htab->plt_type == PLT_NEW)
5524 htab->got_header_size = 12;
5525
5526 /* Set up .got offsets for local syms, and space for local dynamic
5527 relocs. */
5528 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5529 {
5530 bfd_signed_vma *local_got;
5531 bfd_signed_vma *end_local_got;
5532 struct plt_entry **local_plt;
5533 struct plt_entry **end_local_plt;
5534 char *lgot_masks;
5535 bfd_size_type locsymcount;
5536 Elf_Internal_Shdr *symtab_hdr;
5537 Elf_Internal_Sym *local_syms;
5538 Elf_Internal_Sym *isym;
5539
5540 if (!is_ppc_elf (ibfd))
5541 continue;
5542
5543 for (s = ibfd->sections; s != NULL; s = s->next)
5544 {
5545 struct ppc_dyn_relocs *p;
5546
5547 for (p = ((struct ppc_dyn_relocs *)
5548 elf_section_data (s)->local_dynrel);
5549 p != NULL;
5550 p = p->next)
5551 {
5552 if (discarded_section (p->sec))
5553 {
5554 /* Input section has been discarded, either because
5555 it is a copy of a linkonce section or due to
5556 linker script /DISCARD/, so we'll be discarding
5557 the relocs too. */
5558 }
5559 else if (htab->elf.target_os == is_vxworks
5560 && strcmp (p->sec->output_section->name,
5561 ".tls_vars") == 0)
5562 {
5563 /* Relocations in vxworks .tls_vars sections are
5564 handled specially by the loader. */
5565 }
5566 else if (p->count != 0)
5567 {
5568 asection *sreloc = elf_section_data (p->sec)->sreloc;
5569 if (p->ifunc)
5570 sreloc = htab->elf.irelplt;
5571 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5572 if ((p->sec->output_section->flags
5573 & (SEC_READONLY | SEC_ALLOC))
5574 == (SEC_READONLY | SEC_ALLOC))
5575 {
5576 info->flags |= DF_TEXTREL;
5577 info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
5578 p->sec->owner, p->sec);
5579 }
5580 }
5581 }
5582 }
5583
5584 local_got = elf_local_got_refcounts (ibfd);
5585 if (!local_got)
5586 continue;
5587
5588 symtab_hdr = &elf_symtab_hdr (ibfd);
5589 locsymcount = symtab_hdr->sh_info;
5590 end_local_got = local_got + locsymcount;
5591 local_plt = (struct plt_entry **) end_local_got;
5592 end_local_plt = local_plt + locsymcount;
5593 lgot_masks = (char *) end_local_plt;
5594 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
5595 if (local_syms == NULL && locsymcount != 0)
5596 {
5597 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
5598 0, NULL, NULL, NULL);
5599 if (local_syms == NULL)
5600 return false;
5601 }
5602
5603 for (isym = local_syms;
5604 local_got < end_local_got;
5605 ++local_got, ++lgot_masks, ++isym)
5606 if (*local_got > 0)
5607 {
5608 unsigned int need;
5609 if ((*lgot_masks & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
5610 htab->tlsld_got.refcount += 1;
5611 need = got_entries_needed (*lgot_masks);
5612 if (need == 0)
5613 *local_got = (bfd_vma) -1;
5614 else
5615 {
5616 *local_got = allocate_got (htab, need);
5617 if (bfd_link_pic (info)
5618 && !((*lgot_masks & TLS_TLS) != 0
5619 && bfd_link_executable (info))
5620 && isym->st_shndx != SHN_ABS)
5621 {
5622 asection *srel;
5623
5624 need *= sizeof (Elf32_External_Rela) / 4;
5625 srel = htab->elf.srelgot;
5626 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
5627 srel = htab->elf.irelplt;
5628 srel->size += need;
5629 }
5630 }
5631 }
5632 else
5633 *local_got = (bfd_vma) -1;
5634
5635 if (htab->elf.target_os == is_vxworks)
5636 continue;
5637
5638 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
5639 lgot_masks = (char *) end_local_plt;
5640 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
5641 {
5642 struct plt_entry *ent;
5643 bool doneone = false;
5644 bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
5645
5646 for (ent = *local_plt; ent != NULL; ent = ent->next)
5647 if (ent->plt.refcount > 0)
5648 {
5649 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
5650 s = htab->elf.iplt;
5651 else if (htab->can_convert_all_inline_plt
5652 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
5653 {
5654 ent->plt.offset = (bfd_vma) -1;
5655 continue;
5656 }
5657 else
5658 s = htab->pltlocal;
5659
5660 if (!doneone)
5661 {
5662 plt_offset = s->size;
5663 s->size += 4;
5664 }
5665 ent->plt.offset = plt_offset;
5666
5667 if (s != htab->pltlocal && (!doneone || bfd_link_pic (info)))
5668 {
5669 s = htab->glink;
5670 glink_offset = s->size;
5671 s->size += GLINK_ENTRY_SIZE (htab, NULL);
5672 }
5673 ent->glink_offset = glink_offset;
5674
5675 if (!doneone)
5676 {
5677 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
5678 {
5679 s = htab->elf.irelplt;
5680 s->size += sizeof (Elf32_External_Rela);
5681 }
5682 else if (bfd_link_pic (info))
5683 {
5684 s = htab->relpltlocal;
5685 s->size += sizeof (Elf32_External_Rela);
5686 }
5687 doneone = true;
5688 }
5689 }
5690 else
5691 ent->plt.offset = (bfd_vma) -1;
5692 }
5693
5694 if (local_syms != NULL
5695 && symtab_hdr->contents != (unsigned char *) local_syms)
5696 {
5697 if (!info->keep_memory)
5698 free (local_syms);
5699 else
5700 symtab_hdr->contents = (unsigned char *) local_syms;
5701 }
5702 }
5703
5704 /* Allocate space for global sym dynamic relocs. */
5705 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
5706
5707 if (htab->tlsld_got.refcount > 0)
5708 {
5709 htab->tlsld_got.offset = allocate_got (htab, 8);
5710 if (bfd_link_dll (info))
5711 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
5712 }
5713 else
5714 htab->tlsld_got.offset = (bfd_vma) -1;
5715
5716 if (htab->elf.sgot != NULL && htab->plt_type != PLT_VXWORKS)
5717 {
5718 unsigned int g_o_t = 32768;
5719
5720 /* If we haven't allocated the header, do so now. When we get here,
5721 for old plt/got the got size will be 0 to 32764 (not allocated),
5722 or 32780 to 65536 (header allocated). For new plt/got, the
5723 corresponding ranges are 0 to 32768 and 32780 to 65536. */
5724 if (htab->elf.sgot->size <= 32768)
5725 {
5726 g_o_t = htab->elf.sgot->size;
5727 if (htab->plt_type == PLT_OLD)
5728 g_o_t += 4;
5729 htab->elf.sgot->size += htab->got_header_size;
5730 }
5731
5732 htab->elf.hgot->root.u.def.value = g_o_t;
5733 }
5734 if (bfd_link_pic (info))
5735 {
5736 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
5737
5738 sda->root.u.def.section = htab->elf.hgot->root.u.def.section;
5739 sda->root.u.def.value = htab->elf.hgot->root.u.def.value;
5740 }
5741 if (info->emitrelocations)
5742 {
5743 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
5744
5745 if (sda != NULL && sda->ref_regular)
5746 sda->root.u.def.section->flags |= SEC_KEEP;
5747 sda = htab->sdata[1].sym;
5748 if (sda != NULL && sda->ref_regular)
5749 sda->root.u.def.section->flags |= SEC_KEEP;
5750 }
5751
5752 if (htab->glink != NULL
5753 && htab->glink->size != 0
5754 && htab->elf.dynamic_sections_created)
5755 {
5756 htab->glink_pltresolve = htab->glink->size;
5757 /* Space for the branch table. */
5758 htab->glink->size
5759 += htab->elf.srelplt->size / (sizeof (Elf32_External_Rela) / 4) - 4;
5760 /* Pad out to align the start of PLTresolve. */
5761 htab->glink->size += -htab->glink->size & (htab->params->ppc476_workaround
5762 ? 63 : 15);
5763 htab->glink->size += GLINK_PLTRESOLVE;
5764
5765 if (htab->params->emit_stub_syms)
5766 {
5767 struct elf_link_hash_entry *sh;
5768 sh = elf_link_hash_lookup (&htab->elf, "__glink",
5769 true, false, false);
5770 if (sh == NULL)
5771 return false;
5772 if (sh->root.type == bfd_link_hash_new)
5773 {
5774 sh->root.type = bfd_link_hash_defined;
5775 sh->root.u.def.section = htab->glink;
5776 sh->root.u.def.value = htab->glink_pltresolve;
5777 sh->ref_regular = 1;
5778 sh->def_regular = 1;
5779 sh->ref_regular_nonweak = 1;
5780 sh->forced_local = 1;
5781 sh->non_elf = 0;
5782 sh->root.linker_def = 1;
5783 }
5784 sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
5785 true, false, false);
5786 if (sh == NULL)
5787 return false;
5788 if (sh->root.type == bfd_link_hash_new)
5789 {
5790 sh->root.type = bfd_link_hash_defined;
5791 sh->root.u.def.section = htab->glink;
5792 sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
5793 sh->ref_regular = 1;
5794 sh->def_regular = 1;
5795 sh->ref_regular_nonweak = 1;
5796 sh->forced_local = 1;
5797 sh->non_elf = 0;
5798 sh->root.linker_def = 1;
5799 }
5800 }
5801 }
5802
5803 if (htab->glink != NULL
5804 && htab->glink->size != 0
5805 && htab->glink_eh_frame != NULL
5806 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
5807 && _bfd_elf_eh_frame_present (info))
5808 {
5809 s = htab->glink_eh_frame;
5810 s->size = sizeof (glink_eh_frame_cie) + 20;
5811 if (bfd_link_pic (info))
5812 {
5813 s->size += 4;
5814 if (htab->glink->size - GLINK_PLTRESOLVE + 8 >= 256)
5815 s->size += 4;
5816 }
5817 }
5818
5819 /* We've now determined the sizes of the various dynamic sections.
5820 Allocate memory for them. */
5821 relocs = false;
5822 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
5823 {
5824 bool strip_section = true;
5825
5826 if ((s->flags & SEC_LINKER_CREATED) == 0)
5827 continue;
5828
5829 if (s == htab->elf.splt
5830 || s == htab->elf.sgot)
5831 {
5832 /* We'd like to strip these sections if they aren't needed, but if
5833 we've exported dynamic symbols from them we must leave them.
5834 It's too late to tell BFD to get rid of the symbols. */
5835 if (htab->elf.hplt != NULL)
5836 strip_section = false;
5837 /* Strip this section if we don't need it; see the
5838 comment below. */
5839 }
5840 else if (s == htab->elf.iplt
5841 || s == htab->pltlocal
5842 || s == htab->glink
5843 || s == htab->glink_eh_frame
5844 || s == htab->elf.sgotplt
5845 || s == htab->sbss
5846 || s == htab->elf.sdynbss
5847 || s == htab->elf.sdynrelro
5848 || s == htab->dynsbss)
5849 {
5850 /* Strip these too. */
5851 }
5852 else if (s == htab->sdata[0].section
5853 || s == htab->sdata[1].section)
5854 {
5855 strip_section = (s->flags & SEC_KEEP) == 0;
5856 }
5857 else if (startswith (bfd_section_name (s), ".rela"))
5858 {
5859 if (s->size != 0)
5860 {
5861 /* Remember whether there are any relocation sections. */
5862 relocs = true;
5863
5864 /* We use the reloc_count field as a counter if we need
5865 to copy relocs into the output file. */
5866 s->reloc_count = 0;
5867 }
5868 }
5869 else
5870 {
5871 /* It's not one of our sections, so don't allocate space. */
5872 continue;
5873 }
5874
5875 if (s->size == 0 && strip_section)
5876 {
5877 /* If we don't need this section, strip it from the
5878 output file. This is mostly to handle .rela.bss and
5879 .rela.plt. We must create both sections in
5880 create_dynamic_sections, because they must be created
5881 before the linker maps input sections to output
5882 sections. The linker does that before
5883 adjust_dynamic_symbol is called, and it is that
5884 function which decides whether anything needs to go
5885 into these sections. */
5886 s->flags |= SEC_EXCLUDE;
5887 continue;
5888 }
5889
5890 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5891 continue;
5892
5893 /* Allocate memory for the section contents. */
5894 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
5895 if (s->contents == NULL)
5896 return false;
5897 }
5898
5899 if (htab->elf.dynamic_sections_created)
5900 {
5901 /* Add some entries to the .dynamic section. We fill in the
5902 values later, in ppc_elf_finish_dynamic_sections, but we
5903 must add the entries now so that we get the correct size for
5904 the .dynamic section. The DT_DEBUG entry is filled in by the
5905 dynamic linker and used by the debugger. */
5906 #define add_dynamic_entry(TAG, VAL) \
5907 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5908
5909 if (!_bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
5910 relocs))
5911 return false;
5912
5913 if (htab->plt_type == PLT_NEW
5914 && htab->glink != NULL
5915 && htab->glink->size != 0)
5916 {
5917 if (!add_dynamic_entry (DT_PPC_GOT, 0))
5918 return false;
5919 if (!htab->params->no_tls_get_addr_opt
5920 && htab->tls_get_addr != NULL
5921 && htab->tls_get_addr->plt.plist != NULL
5922 && !add_dynamic_entry (DT_PPC_OPT, PPC_OPT_TLS))
5923 return false;
5924 }
5925 }
5926 #undef add_dynamic_entry
5927
5928 if (htab->glink_eh_frame != NULL
5929 && htab->glink_eh_frame->contents != NULL)
5930 {
5931 unsigned char *p = htab->glink_eh_frame->contents;
5932 bfd_vma val;
5933
5934 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
5935 /* CIE length (rewrite in case little-endian). */
5936 bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
5937 p += sizeof (glink_eh_frame_cie);
5938 /* FDE length. */
5939 val = htab->glink_eh_frame->size - 4 - sizeof (glink_eh_frame_cie);
5940 bfd_put_32 (htab->elf.dynobj, val, p);
5941 p += 4;
5942 /* CIE pointer. */
5943 val = p - htab->glink_eh_frame->contents;
5944 bfd_put_32 (htab->elf.dynobj, val, p);
5945 p += 4;
5946 /* Offset to .glink. Set later. */
5947 p += 4;
5948 /* .glink size. */
5949 bfd_put_32 (htab->elf.dynobj, htab->glink->size, p);
5950 p += 4;
5951 /* Augmentation. */
5952 p += 1;
5953
5954 if (bfd_link_pic (info)
5955 && htab->elf.dynamic_sections_created)
5956 {
5957 bfd_vma adv = (htab->glink->size - GLINK_PLTRESOLVE + 8) >> 2;
5958 if (adv < 64)
5959 *p++ = DW_CFA_advance_loc + adv;
5960 else if (adv < 256)
5961 {
5962 *p++ = DW_CFA_advance_loc1;
5963 *p++ = adv;
5964 }
5965 else if (adv < 65536)
5966 {
5967 *p++ = DW_CFA_advance_loc2;
5968 bfd_put_16 (htab->elf.dynobj, adv, p);
5969 p += 2;
5970 }
5971 else
5972 {
5973 *p++ = DW_CFA_advance_loc4;
5974 bfd_put_32 (htab->elf.dynobj, adv, p);
5975 p += 4;
5976 }
5977 *p++ = DW_CFA_register;
5978 *p++ = 65;
5979 p++;
5980 *p++ = DW_CFA_advance_loc + 4;
5981 *p++ = DW_CFA_restore_extended;
5982 *p++ = 65;
5983 }
5984 BFD_ASSERT ((bfd_vma) ((p + 3 - htab->glink_eh_frame->contents) & -4)
5985 == htab->glink_eh_frame->size);
5986 }
5987
5988 return true;
5989 }
5990
5991 /* Arrange to have _SDA_BASE_ or _SDA2_BASE_ stripped from the output
5992 if it looks like nothing is using them. */
5993
5994 static void
5995 maybe_strip_sdasym (bfd *output_bfd, elf_linker_section_t *lsect)
5996 {
5997 struct elf_link_hash_entry *sda = lsect->sym;
5998
5999 if (sda != NULL && !sda->ref_regular && sda->dynindx == -1)
6000 {
6001 asection *s;
6002
6003 s = bfd_get_section_by_name (output_bfd, lsect->name);
6004 if (s == NULL || bfd_section_removed_from_list (output_bfd, s))
6005 {
6006 s = bfd_get_section_by_name (output_bfd, lsect->bss_name);
6007 if (s == NULL || bfd_section_removed_from_list (output_bfd, s))
6008 {
6009 sda->def_regular = 0;
6010 /* This is somewhat magic. See elf_link_output_extsym. */
6011 sda->ref_dynamic = 1;
6012 sda->forced_local = 0;
6013 }
6014 }
6015 }
6016 }
6017
6018 void
6019 ppc_elf_maybe_strip_sdata_syms (struct bfd_link_info *info)
6020 {
6021 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
6022
6023 if (htab != NULL)
6024 {
6025 maybe_strip_sdasym (info->output_bfd, &htab->sdata[0]);
6026 maybe_strip_sdasym (info->output_bfd, &htab->sdata[1]);
6027 }
6028 }
6029
6030
6031 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6032
6033 static bool
6034 ppc_elf_hash_symbol (struct elf_link_hash_entry *h)
6035 {
6036 if (h->plt.plist != NULL
6037 && !h->def_regular
6038 && (!h->pointer_equality_needed
6039 || !h->ref_regular_nonweak))
6040 return false;
6041
6042 return _bfd_elf_hash_symbol (h);
6043 }
6044
6045 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
6047
6048 /* Relaxation trampolines. r12 is available for clobbering (r11, is
6049 used for some functions that are allowed to break the ABI). */
6050 static const int shared_stub_entry[] =
6051 {
6052 0x7c0802a6, /* mflr 0 */
6053 0x429f0005, /* bcl 20, 31, .Lxxx */
6054 0x7d8802a6, /* mflr 12 */
6055 0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
6056 0x398c0000, /* addi 12, 12, (xxx-.Lxxx)@l */
6057 0x7c0803a6, /* mtlr 0 */
6058 0x7d8903a6, /* mtctr 12 */
6059 0x4e800420, /* bctr */
6060 };
6061
6062 static const int stub_entry[] =
6063 {
6064 0x3d800000, /* lis 12,xxx@ha */
6065 0x398c0000, /* addi 12,12,xxx@l */
6066 0x7d8903a6, /* mtctr 12 */
6067 0x4e800420, /* bctr */
6068 };
6069
6070 struct ppc_elf_relax_info
6071 {
6072 unsigned int workaround_size;
6073 unsigned int picfixup_size;
6074 };
6075
6076 /* This function implements long branch trampolines, and the ppc476
6077 icache bug workaround. Any section needing trampolines or patch
6078 space for the workaround has its size extended so that we can
6079 add trampolines at the end of the section. */
6080
6081 static bool
6082 ppc_elf_relax_section (bfd *abfd,
6083 asection *isec,
6084 struct bfd_link_info *link_info,
6085 bool *again)
6086 {
6087 struct one_branch_fixup
6088 {
6089 struct one_branch_fixup *next;
6090 asection *tsec;
6091 /* Final link, can use the symbol offset. For a
6092 relocatable link we use the symbol's index. */
6093 bfd_vma toff;
6094 bfd_vma trampoff;
6095 };
6096
6097 Elf_Internal_Shdr *symtab_hdr;
6098 bfd_byte *contents = NULL;
6099 Elf_Internal_Sym *isymbuf = NULL;
6100 Elf_Internal_Rela *internal_relocs = NULL;
6101 Elf_Internal_Rela *irel, *irelend = NULL;
6102 struct one_branch_fixup *branch_fixups = NULL;
6103 struct ppc_elf_relax_info *relax_info = NULL;
6104 unsigned changes = 0;
6105 bool workaround_change;
6106 struct ppc_elf_link_hash_table *htab;
6107 bfd_size_type trampbase, trampoff, newsize, picfixup_size;
6108 asection *got2;
6109 bool maybe_pasted;
6110
6111 *again = false;
6112
6113 /* No need to do anything with non-alloc or non-code sections. */
6114 if ((isec->flags & SEC_ALLOC) == 0
6115 || (isec->flags & SEC_CODE) == 0
6116 || (isec->flags & SEC_LINKER_CREATED) != 0
6117 || isec->size < 4)
6118 return true;
6119
6120 /* We cannot represent the required PIC relocs in the output, so don't
6121 do anything. The linker doesn't support mixing -shared and -r
6122 anyway. */
6123 if (bfd_link_relocatable (link_info) && bfd_link_pic (link_info))
6124 return true;
6125
6126 htab = ppc_elf_hash_table (link_info);
6127 if (htab == NULL)
6128 return true;
6129
6130 isec->size = (isec->size + 3) & -4;
6131 if (isec->rawsize == 0)
6132 isec->rawsize = isec->size;
6133 trampbase = isec->size;
6134
6135 BFD_ASSERT (isec->sec_info_type == SEC_INFO_TYPE_NONE
6136 || isec->sec_info_type == SEC_INFO_TYPE_TARGET);
6137 isec->sec_info_type = SEC_INFO_TYPE_TARGET;
6138
6139 if (htab->params->ppc476_workaround
6140 || htab->params->pic_fixup > 0)
6141 {
6142 if (elf_section_data (isec)->sec_info == NULL)
6143 {
6144 elf_section_data (isec)->sec_info
6145 = bfd_zalloc (abfd, sizeof (struct ppc_elf_relax_info));
6146 if (elf_section_data (isec)->sec_info == NULL)
6147 return false;
6148 }
6149 relax_info = elf_section_data (isec)->sec_info;
6150 trampbase -= relax_info->workaround_size;
6151 }
6152
6153 maybe_pasted = (strcmp (isec->output_section->name, ".init") == 0
6154 || strcmp (isec->output_section->name, ".fini") == 0);
6155 /* Space for a branch around any trampolines. */
6156 trampoff = trampbase;
6157 if (maybe_pasted && trampbase == isec->rawsize)
6158 trampoff += 4;
6159
6160 symtab_hdr = &elf_symtab_hdr (abfd);
6161 picfixup_size = 0;
6162 if (htab->params->branch_trampolines
6163 || htab->params->pic_fixup > 0)
6164 {
6165 /* Get a copy of the native relocations. */
6166 if (isec->reloc_count != 0)
6167 {
6168 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
6169 link_info->keep_memory);
6170 if (internal_relocs == NULL)
6171 goto error_return;
6172 }
6173
6174 got2 = bfd_get_section_by_name (abfd, ".got2");
6175
6176 irelend = internal_relocs + isec->reloc_count;
6177 for (irel = internal_relocs; irel < irelend; irel++)
6178 {
6179 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
6180 bfd_vma toff, roff;
6181 asection *tsec;
6182 struct one_branch_fixup *f;
6183 size_t insn_offset = 0;
6184 bfd_vma max_branch_offset = 0, val;
6185 bfd_byte *hit_addr;
6186 unsigned long t0;
6187 struct elf_link_hash_entry *h;
6188 Elf_Internal_Sym *isym;
6189 struct plt_entry **plist;
6190 unsigned char sym_type;
6191
6192 switch (r_type)
6193 {
6194 case R_PPC_REL24:
6195 case R_PPC_LOCAL24PC:
6196 case R_PPC_PLTREL24:
6197 case R_PPC_PLTCALL:
6198 max_branch_offset = 1 << 25;
6199 break;
6200
6201 case R_PPC_REL14:
6202 case R_PPC_REL14_BRTAKEN:
6203 case R_PPC_REL14_BRNTAKEN:
6204 max_branch_offset = 1 << 15;
6205 break;
6206
6207 case R_PPC_ADDR16_HA:
6208 if (htab->params->pic_fixup > 0)
6209 break;
6210 continue;
6211
6212 default:
6213 continue;
6214 }
6215
6216 /* Get the value of the symbol referred to by the reloc. */
6217 if (!get_sym_h (&h, &isym, &tsec, NULL, &isymbuf,
6218 ELF32_R_SYM (irel->r_info), abfd))
6219 goto error_return;
6220
6221 if (isym != NULL)
6222 {
6223 if (tsec != NULL)
6224 ;
6225 else if (isym->st_shndx == SHN_ABS)
6226 tsec = bfd_abs_section_ptr;
6227 else
6228 continue;
6229
6230 toff = isym->st_value;
6231 sym_type = ELF_ST_TYPE (isym->st_info);
6232 }
6233 else
6234 {
6235 if (tsec != NULL)
6236 toff = h->root.u.def.value;
6237 else if (h->root.type == bfd_link_hash_undefined
6238 || h->root.type == bfd_link_hash_undefweak)
6239 {
6240 unsigned long indx;
6241
6242 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6243 tsec = bfd_und_section_ptr;
6244 toff = bfd_link_relocatable (link_info) ? indx : 0;
6245 }
6246 else
6247 continue;
6248
6249 /* If this branch is to __tls_get_addr then we may later
6250 optimise away the call. We won't be needing a long-
6251 branch stub in that case. */
6252 if (bfd_link_executable (link_info)
6253 && h == htab->tls_get_addr
6254 && irel != internal_relocs)
6255 {
6256 unsigned long t_symndx = ELF32_R_SYM (irel[-1].r_info);
6257 unsigned long t_rtype = ELF32_R_TYPE (irel[-1].r_info);
6258 unsigned int tls_mask = 0;
6259
6260 /* The previous reloc should be one of R_PPC_TLSGD or
6261 R_PPC_TLSLD, or for older object files, a reloc
6262 on the __tls_get_addr arg setup insn. Get tls
6263 mask bits from the symbol on that reloc. */
6264 if (t_symndx < symtab_hdr->sh_info)
6265 {
6266 bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
6267
6268 if (local_got_offsets != NULL)
6269 {
6270 struct plt_entry **local_plt = (struct plt_entry **)
6271 (local_got_offsets + symtab_hdr->sh_info);
6272 char *lgot_masks = (char *)
6273 (local_plt + symtab_hdr->sh_info);
6274 tls_mask = lgot_masks[t_symndx];
6275 }
6276 }
6277 else
6278 {
6279 struct elf_link_hash_entry *th
6280 = elf_sym_hashes (abfd)[t_symndx - symtab_hdr->sh_info];
6281
6282 while (th->root.type == bfd_link_hash_indirect
6283 || th->root.type == bfd_link_hash_warning)
6284 th = (struct elf_link_hash_entry *) th->root.u.i.link;
6285
6286 tls_mask
6287 = ((struct ppc_elf_link_hash_entry *) th)->tls_mask;
6288 }
6289
6290 /* The mask bits tell us if the call will be
6291 optimised away. */
6292 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
6293 && (t_rtype == R_PPC_TLSGD
6294 || t_rtype == R_PPC_GOT_TLSGD16
6295 || t_rtype == R_PPC_GOT_TLSGD16_LO))
6296 continue;
6297 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
6298 && (t_rtype == R_PPC_TLSLD
6299 || t_rtype == R_PPC_GOT_TLSLD16
6300 || t_rtype == R_PPC_GOT_TLSLD16_LO))
6301 continue;
6302 }
6303
6304 sym_type = h->type;
6305 }
6306
6307 if (r_type == R_PPC_ADDR16_HA)
6308 {
6309 if (h != NULL
6310 && !h->def_regular
6311 && h->protected_def
6312 && ppc_elf_hash_entry (h)->has_addr16_ha
6313 && ppc_elf_hash_entry (h)->has_addr16_lo)
6314 picfixup_size += 12;
6315 continue;
6316 }
6317
6318 /* The condition here under which we call find_plt_ent must
6319 match that in relocate_section. If we call find_plt_ent here
6320 but not in relocate_section, or vice versa, then the branch
6321 destination used here may be incorrect. */
6322 plist = NULL;
6323 if (h != NULL)
6324 {
6325 /* We know is_branch_reloc (r_type) is true. */
6326 if (h->type == STT_GNU_IFUNC
6327 || r_type == R_PPC_PLTREL24)
6328 plist = &h->plt.plist;
6329 }
6330 else if (sym_type == STT_GNU_IFUNC
6331 && elf_local_got_offsets (abfd) != NULL)
6332 {
6333 bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
6334 struct plt_entry **local_plt = (struct plt_entry **)
6335 (local_got_offsets + symtab_hdr->sh_info);
6336 plist = local_plt + ELF32_R_SYM (irel->r_info);
6337 }
6338 if (plist != NULL)
6339 {
6340 bfd_vma addend = 0;
6341 struct plt_entry *ent;
6342
6343 if (r_type == R_PPC_PLTREL24 && bfd_link_pic (link_info))
6344 addend = irel->r_addend;
6345 ent = find_plt_ent (plist, got2, addend);
6346 if (ent != NULL)
6347 {
6348 if (htab->plt_type == PLT_NEW
6349 || h == NULL
6350 || !htab->elf.dynamic_sections_created
6351 || h->dynindx == -1)
6352 {
6353 tsec = htab->glink;
6354 toff = ent->glink_offset;
6355 }
6356 else
6357 {
6358 tsec = htab->elf.splt;
6359 toff = ent->plt.offset;
6360 }
6361 }
6362 }
6363
6364 /* If the branch and target are in the same section, you have
6365 no hope of adding stubs. We'll error out later should the
6366 branch overflow. */
6367 if (tsec == isec)
6368 continue;
6369
6370 /* toff is used for the symbol index when the symbol is
6371 undefined and we're doing a relocatable link, so we can't
6372 support addends. It would be possible to do so by
6373 putting the addend in one_branch_fixup but addends on
6374 branches are rare so it hardly seems worth supporting. */
6375 if (bfd_link_relocatable (link_info)
6376 && tsec == bfd_und_section_ptr
6377 && r_type != R_PPC_PLTREL24
6378 && irel->r_addend != 0)
6379 continue;
6380
6381 /* There probably isn't any reason to handle symbols in
6382 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
6383 attribute for a code section, and we are only looking at
6384 branches. However, implement it correctly here as a
6385 reference for other target relax_section functions. */
6386 if (0 && tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
6387 {
6388 /* At this stage in linking, no SEC_MERGE symbol has been
6389 adjusted, so all references to such symbols need to be
6390 passed through _bfd_merged_section_offset. (Later, in
6391 relocate_section, all SEC_MERGE symbols *except* for
6392 section symbols have been adjusted.)
6393
6394 gas may reduce relocations against symbols in SEC_MERGE
6395 sections to a relocation against the section symbol when
6396 the original addend was zero. When the reloc is against
6397 a section symbol we should include the addend in the
6398 offset passed to _bfd_merged_section_offset, since the
6399 location of interest is the original symbol. On the
6400 other hand, an access to "sym+addend" where "sym" is not
6401 a section symbol should not include the addend; Such an
6402 access is presumed to be an offset from "sym"; The
6403 location of interest is just "sym". */
6404 if (sym_type == STT_SECTION
6405 && r_type != R_PPC_PLTREL24)
6406 toff += irel->r_addend;
6407
6408 toff
6409 = _bfd_merged_section_offset (abfd, &tsec,
6410 elf_section_data (tsec)->sec_info,
6411 toff);
6412
6413 if (sym_type != STT_SECTION
6414 && r_type != R_PPC_PLTREL24)
6415 toff += irel->r_addend;
6416 }
6417 /* PLTREL24 addends are special. */
6418 else if (r_type != R_PPC_PLTREL24)
6419 toff += irel->r_addend;
6420
6421 /* Attempted -shared link of non-pic code loses. */
6422 if ((!bfd_link_relocatable (link_info)
6423 && tsec == bfd_und_section_ptr)
6424 || tsec->output_section == NULL
6425 || (tsec->owner != NULL
6426 && (tsec->owner->flags & BFD_PLUGIN) != 0))
6427 continue;
6428
6429 roff = irel->r_offset;
6430
6431 /* Avoid creating a lot of unnecessary fixups when
6432 relocatable if the output section size is such that a
6433 fixup can be created at final link.
6434 The max_branch_offset adjustment allows for some number
6435 of other fixups being needed at final link. */
6436 if (bfd_link_relocatable (link_info)
6437 && (isec->output_section->rawsize - (isec->output_offset + roff)
6438 < max_branch_offset - (max_branch_offset >> 4)))
6439 continue;
6440
6441 /* If the branch is in range, no need to do anything. */
6442 if (tsec != bfd_und_section_ptr
6443 && (!bfd_link_relocatable (link_info)
6444 /* A relocatable link may have sections moved during
6445 final link, so do not presume they remain in range. */
6446 || tsec->output_section == isec->output_section))
6447 {
6448 bfd_vma symaddr, reladdr;
6449
6450 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
6451 reladdr = isec->output_section->vma + isec->output_offset + roff;
6452 if (symaddr - reladdr + max_branch_offset
6453 < 2 * max_branch_offset)
6454 continue;
6455 }
6456
6457 /* Look for an existing fixup to this address. */
6458 for (f = branch_fixups; f ; f = f->next)
6459 if (f->tsec == tsec && f->toff == toff)
6460 break;
6461
6462 if (f == NULL)
6463 {
6464 size_t size;
6465 unsigned long stub_rtype;
6466
6467 val = trampoff - roff;
6468 if (val >= max_branch_offset)
6469 /* Oh dear, we can't reach a trampoline. Don't try to add
6470 one. We'll report an error later. */
6471 continue;
6472
6473 if (bfd_link_pic (link_info))
6474 {
6475 size = 4 * ARRAY_SIZE (shared_stub_entry);
6476 insn_offset = 12;
6477 }
6478 else
6479 {
6480 size = 4 * ARRAY_SIZE (stub_entry);
6481 insn_offset = 0;
6482 }
6483 stub_rtype = R_PPC_RELAX;
6484 if (tsec == htab->elf.splt
6485 || tsec == htab->glink)
6486 {
6487 stub_rtype = R_PPC_RELAX_PLT;
6488 if (r_type == R_PPC_PLTREL24)
6489 stub_rtype = R_PPC_RELAX_PLTREL24;
6490 }
6491
6492 /* Hijack the old relocation. Since we need two
6493 relocations for this use a "composite" reloc. */
6494 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
6495 stub_rtype);
6496 irel->r_offset = trampoff + insn_offset;
6497 if (r_type == R_PPC_PLTREL24
6498 && stub_rtype != R_PPC_RELAX_PLTREL24)
6499 irel->r_addend = 0;
6500
6501 /* Record the fixup so we don't do it again this section. */
6502 f = bfd_malloc (sizeof (*f));
6503 f->next = branch_fixups;
6504 f->tsec = tsec;
6505 f->toff = toff;
6506 f->trampoff = trampoff;
6507 branch_fixups = f;
6508
6509 trampoff += size;
6510 changes++;
6511 }
6512 else
6513 {
6514 val = f->trampoff - roff;
6515 if (val >= max_branch_offset)
6516 continue;
6517
6518 /* Nop out the reloc, since we're finalizing things here. */
6519 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6520 }
6521
6522 /* Get the section contents. */
6523 if (contents == NULL)
6524 {
6525 /* Get cached copy if it exists. */
6526 if (elf_section_data (isec)->this_hdr.contents != NULL)
6527 contents = elf_section_data (isec)->this_hdr.contents;
6528 /* Go get them off disk. */
6529 else if (!bfd_malloc_and_get_section (abfd, isec, &contents))
6530 goto error_return;
6531 }
6532
6533 /* Fix up the existing branch to hit the trampoline. */
6534 hit_addr = contents + roff;
6535 switch (r_type)
6536 {
6537 case R_PPC_REL24:
6538 case R_PPC_LOCAL24PC:
6539 case R_PPC_PLTREL24:
6540 t0 = bfd_get_32 (abfd, hit_addr);
6541 t0 &= ~0x3fffffc;
6542 t0 |= val & 0x3fffffc;
6543 bfd_put_32 (abfd, t0, hit_addr);
6544 break;
6545
6546 case R_PPC_REL14:
6547 case R_PPC_REL14_BRTAKEN:
6548 case R_PPC_REL14_BRNTAKEN:
6549 t0 = bfd_get_32 (abfd, hit_addr);
6550 t0 &= ~0xfffc;
6551 t0 |= val & 0xfffc;
6552 bfd_put_32 (abfd, t0, hit_addr);
6553 break;
6554 }
6555 }
6556
6557 while (branch_fixups != NULL)
6558 {
6559 struct one_branch_fixup *f = branch_fixups;
6560 branch_fixups = branch_fixups->next;
6561 free (f);
6562 }
6563 }
6564
6565 workaround_change = false;
6566 newsize = trampoff;
6567 if (htab->params->ppc476_workaround
6568 && (!bfd_link_relocatable (link_info)
6569 || isec->output_section->alignment_power >= htab->params->pagesize_p2))
6570 {
6571 bfd_vma addr, end_addr;
6572 unsigned int crossings;
6573 bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
6574
6575 addr = isec->output_section->vma + isec->output_offset;
6576 end_addr = addr + trampoff;
6577 addr &= -pagesize;
6578 crossings = ((end_addr & -pagesize) - addr) >> htab->params->pagesize_p2;
6579 if (crossings != 0)
6580 {
6581 /* Keep space aligned, to ensure the patch code itself does
6582 not cross a page. Don't decrease size calculated on a
6583 previous pass as otherwise we might never settle on a layout. */
6584 newsize = 15 - ((end_addr - 1) & 15);
6585 newsize += crossings * 16;
6586 if (relax_info->workaround_size < newsize)
6587 {
6588 relax_info->workaround_size = newsize;
6589 workaround_change = true;
6590 }
6591 /* Ensure relocate_section is called. */
6592 isec->flags |= SEC_RELOC;
6593 }
6594 newsize = trampoff + relax_info->workaround_size;
6595 }
6596
6597 if (htab->params->pic_fixup > 0)
6598 {
6599 picfixup_size -= relax_info->picfixup_size;
6600 if (picfixup_size != 0)
6601 relax_info->picfixup_size += picfixup_size;
6602 newsize += relax_info->picfixup_size;
6603 }
6604
6605 if (changes != 0 || picfixup_size != 0 || workaround_change)
6606 isec->size = newsize;
6607
6608 if (isymbuf != NULL
6609 && symtab_hdr->contents != (unsigned char *) isymbuf)
6610 {
6611 if (! link_info->keep_memory)
6612 free (isymbuf);
6613 else
6614 {
6615 /* Cache the symbols for elf_link_input_bfd. */
6616 symtab_hdr->contents = (unsigned char *) isymbuf;
6617 }
6618 }
6619
6620 if (contents != NULL
6621 && elf_section_data (isec)->this_hdr.contents != contents)
6622 {
6623 if (!changes && !link_info->keep_memory)
6624 free (contents);
6625 else
6626 {
6627 /* Cache the section contents for elf_link_input_bfd. */
6628 elf_section_data (isec)->this_hdr.contents = contents;
6629 }
6630 }
6631
6632 changes += picfixup_size;
6633 if (changes != 0)
6634 {
6635 /* Append sufficient NOP relocs so we can write out relocation
6636 information for the trampolines. */
6637 Elf_Internal_Shdr *rel_hdr;
6638 Elf_Internal_Rela *new_relocs = bfd_malloc ((changes + isec->reloc_count)
6639 * sizeof (*new_relocs));
6640 unsigned ix;
6641
6642 if (!new_relocs)
6643 goto error_return;
6644 memcpy (new_relocs, internal_relocs,
6645 isec->reloc_count * sizeof (*new_relocs));
6646 for (ix = changes; ix--;)
6647 {
6648 irel = new_relocs + ix + isec->reloc_count;
6649
6650 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6651 }
6652 if (internal_relocs != elf_section_data (isec)->relocs)
6653 free (internal_relocs);
6654 elf_section_data (isec)->relocs = new_relocs;
6655 isec->reloc_count += changes;
6656 rel_hdr = _bfd_elf_single_rel_hdr (isec);
6657 rel_hdr->sh_size += changes * rel_hdr->sh_entsize;
6658 }
6659 else if (elf_section_data (isec)->relocs != internal_relocs)
6660 free (internal_relocs);
6661
6662 *again = changes != 0 || workaround_change;
6663 return true;
6664
6665 error_return:
6666 while (branch_fixups != NULL)
6667 {
6668 struct one_branch_fixup *f = branch_fixups;
6669 branch_fixups = branch_fixups->next;
6670 free (f);
6671 }
6672 if ((unsigned char *) isymbuf != symtab_hdr->contents)
6673 free (isymbuf);
6674 if (elf_section_data (isec)->this_hdr.contents != contents)
6675 free (contents);
6676 if (elf_section_data (isec)->relocs != internal_relocs)
6677 free (internal_relocs);
6678 return false;
6679 }
6680
6681 /* What to do when ld finds relocations against symbols defined in
6683 discarded sections. */
6684
6685 static unsigned int
6686 ppc_elf_action_discarded (asection *sec)
6687 {
6688 if (strcmp (".fixup", sec->name) == 0)
6689 return 0;
6690
6691 if (strcmp (".got2", sec->name) == 0)
6692 return 0;
6693
6694 return _bfd_elf_default_action_discarded (sec);
6695 }
6696
6697 /* Fill in the address for a pointer generated in a linker section. */
6699
6700 static bfd_vma
6701 elf_finish_pointer_linker_section (bfd *input_bfd,
6702 elf_linker_section_t *lsect,
6703 struct elf_link_hash_entry *h,
6704 bfd_vma relocation,
6705 const Elf_Internal_Rela *rel)
6706 {
6707 elf_linker_section_pointers_t *linker_section_ptr;
6708
6709 BFD_ASSERT (lsect != NULL);
6710
6711 if (h != NULL)
6712 {
6713 /* Handle global symbol. */
6714 struct ppc_elf_link_hash_entry *eh;
6715
6716 eh = (struct ppc_elf_link_hash_entry *) h;
6717 BFD_ASSERT (eh->elf.def_regular);
6718 linker_section_ptr = eh->linker_section_pointer;
6719 }
6720 else
6721 {
6722 /* Handle local symbol. */
6723 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
6724
6725 BFD_ASSERT (is_ppc_elf (input_bfd));
6726 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
6727 linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
6728 }
6729
6730 linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
6731 rel->r_addend,
6732 lsect);
6733 BFD_ASSERT (linker_section_ptr != NULL);
6734
6735 /* Offset will always be a multiple of four, so use the bottom bit
6736 as a "written" flag. */
6737 if ((linker_section_ptr->offset & 1) == 0)
6738 {
6739 bfd_put_32 (lsect->section->owner,
6740 relocation + linker_section_ptr->addend,
6741 lsect->section->contents + linker_section_ptr->offset);
6742 linker_section_ptr->offset += 1;
6743 }
6744
6745 relocation = (lsect->section->output_section->vma
6746 + lsect->section->output_offset
6747 + linker_section_ptr->offset - 1
6748 - SYM_VAL (lsect->sym));
6749
6750 #ifdef DEBUG
6751 fprintf (stderr,
6752 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6753 lsect->name, (long) relocation, (long) relocation);
6754 #endif
6755
6756 return relocation;
6757 }
6758
6759 #define PPC_LO(v) ((v) & 0xffff)
6760 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6761 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6762
6763 static void
6764 write_glink_stub (struct elf_link_hash_entry *h, struct plt_entry *ent,
6765 asection *plt_sec, unsigned char *p,
6766 struct bfd_link_info *info)
6767 {
6768 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
6769 bfd *output_bfd = info->output_bfd;
6770 bfd_vma plt;
6771 unsigned char *end = p + GLINK_ENTRY_SIZE (htab, h);
6772
6773 if (h != NULL
6774 && h == htab->tls_get_addr
6775 && !htab->params->no_tls_get_addr_opt)
6776 {
6777 bfd_put_32 (output_bfd, LWZ_11_3, p);
6778 p += 4;
6779 bfd_put_32 (output_bfd, LWZ_12_3 + 4, p);
6780 p += 4;
6781 bfd_put_32 (output_bfd, MR_0_3, p);
6782 p += 4;
6783 bfd_put_32 (output_bfd, CMPWI_11_0, p);
6784 p += 4;
6785 bfd_put_32 (output_bfd, ADD_3_12_2, p);
6786 p += 4;
6787 bfd_put_32 (output_bfd, BEQLR, p);
6788 p += 4;
6789 bfd_put_32 (output_bfd, MR_3_0, p);
6790 p += 4;
6791 bfd_put_32 (output_bfd, NOP, p);
6792 p += 4;
6793 }
6794
6795 plt = ((ent->plt.offset & ~1)
6796 + plt_sec->output_section->vma
6797 + plt_sec->output_offset);
6798
6799 if (bfd_link_pic (info))
6800 {
6801 bfd_vma got = 0;
6802
6803 if (ent->addend >= 32768)
6804 got = (ent->addend
6805 + ent->sec->output_section->vma
6806 + ent->sec->output_offset);
6807 else if (htab->elf.hgot != NULL)
6808 got = SYM_VAL (htab->elf.hgot);
6809
6810 plt -= got;
6811
6812 if (plt + 0x8000 < 0x10000)
6813 bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
6814 else
6815 {
6816 bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
6817 p += 4;
6818 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6819 }
6820 }
6821 else
6822 {
6823 bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
6824 p += 4;
6825 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6826 }
6827 p += 4;
6828 bfd_put_32 (output_bfd, MTCTR_11, p);
6829 p += 4;
6830 bfd_put_32 (output_bfd, BCTR, p);
6831 p += 4;
6832 while (p < end)
6833 {
6834 bfd_put_32 (output_bfd, htab->params->ppc476_workaround ? BA : NOP, p);
6835 p += 4;
6836 }
6837 }
6838
6839 /* Return true if symbol is defined statically. */
6840
6841 static bool
6842 is_static_defined (struct elf_link_hash_entry *h)
6843 {
6844 return ((h->root.type == bfd_link_hash_defined
6845 || h->root.type == bfd_link_hash_defweak)
6846 && h->root.u.def.section != NULL
6847 && h->root.u.def.section->output_section != NULL);
6848 }
6849
6850 /* If INSN is an opcode that may be used with an @tls operand, return
6851 the transformed insn for TLS optimisation, otherwise return 0. If
6852 REG is non-zero only match an insn with RB or RA equal to REG. */
6853
6854 unsigned int
6855 _bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
6856 {
6857 unsigned int rtra;
6858
6859 if ((insn & (0x3fu << 26)) != 31 << 26)
6860 return 0;
6861
6862 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
6863 rtra = insn & ((1 << 26) - (1 << 16));
6864 else if (((insn >> 16) & 0x1f) == reg)
6865 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
6866 else
6867 return 0;
6868
6869 if ((insn & (0x3ff << 1)) == 266 << 1)
6870 /* add -> addi. */
6871 insn = 14 << 26;
6872 else if ((insn & (0x1f << 1)) == 23 << 1
6873 && ((insn & (0x1f << 6)) < 14 << 6
6874 || ((insn & (0x1f << 6)) >= 16 << 6
6875 && (insn & (0x1f << 6)) < 24 << 6)))
6876 /* load and store indexed -> dform. */
6877 insn = (32u | ((insn >> 6) & 0x1f)) << 26;
6878 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
6879 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
6880 insn = ((58u | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
6881 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
6882 /* lwax -> lwa. */
6883 insn = (58u << 26) | 2;
6884 else
6885 return 0;
6886 insn |= rtra;
6887 return insn;
6888 }
6889
6890 /* If INSN is an opcode that may be used with an @tprel operand, return
6891 the transformed insn for an undefined weak symbol, ie. with the
6892 thread pointer REG operand removed. Otherwise return 0. */
6893
6894 unsigned int
6895 _bfd_elf_ppc_at_tprel_transform (unsigned int insn, unsigned int reg)
6896 {
6897 if ((insn & (0x1f << 16)) == reg << 16
6898 && ((insn & (0x3fu << 26)) == 14u << 26 /* addi */
6899 || (insn & (0x3fu << 26)) == 15u << 26 /* addis */
6900 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
6901 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
6902 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
6903 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
6904 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
6905 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
6906 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
6907 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
6908 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
6909 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
6910 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
6911 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
6912 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
6913 || ((insn & (0x3fu << 26)) == 58u << 26 /* lwa,ld,lmd */
6914 && (insn & 3) != 1)
6915 || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
6916 && ((insn & 3) == 0 || (insn & 3) == 3))))
6917 {
6918 insn &= ~(0x1f << 16);
6919 }
6920 else if ((insn & (0x1f << 21)) == reg << 21
6921 && ((insn & (0x3eu << 26)) == 24u << 26 /* ori, oris */
6922 || (insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */
6923 || (insn & (0x3eu << 26)) == 28u << 26 /* andi,andis */))
6924 {
6925 insn &= ~(0x1f << 21);
6926 insn |= (insn & (0x1f << 16)) << 5;
6927 if ((insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */)
6928 insn -= 2 >> 26; /* convert to ori,oris */
6929 }
6930 else
6931 insn = 0;
6932 return insn;
6933 }
6934
6935 static bool
6936 is_insn_ds_form (unsigned int insn)
6937 {
6938 return ((insn & (0x3fu << 26)) == 58u << 26 /* ld,ldu,lwa */
6939 || (insn & (0x3fu << 26)) == 62u << 26 /* std,stdu,stq */
6940 || (insn & (0x3fu << 26)) == 57u << 26 /* lfdp */
6941 || (insn & (0x3fu << 26)) == 61u << 26 /* stfdp */);
6942 }
6943
6944 static bool
6945 is_insn_dq_form (unsigned int insn)
6946 {
6947 return ((insn & (0x3fu << 26)) == 56u << 26 /* lq */
6948 || ((insn & (0x3fu << 26)) == (61u << 26) /* lxv, stxv */
6949 && (insn & 3) == 1));
6950 }
6951
6952 /* The RELOCATE_SECTION function is called by the ELF backend linker
6953 to handle the relocations for a section.
6954
6955 The relocs are always passed as Rela structures; if the section
6956 actually uses Rel structures, the r_addend field will always be
6957 zero.
6958
6959 This function is responsible for adjust the section contents as
6960 necessary, and (if using Rela relocs and generating a
6961 relocatable output file) adjusting the reloc addend as
6962 necessary.
6963
6964 This function does not have to worry about setting the reloc
6965 address or the reloc symbol index.
6966
6967 LOCAL_SYMS is a pointer to the swapped in local symbols.
6968
6969 LOCAL_SECTIONS is an array giving the section in the input file
6970 corresponding to the st_shndx field of each local symbol.
6971
6972 The global hash table entry for the global symbols can be found
6973 via elf_sym_hashes (input_bfd).
6974
6975 When generating relocatable output, this function must handle
6976 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
6977 going to be the section symbol corresponding to the output
6978 section, which means that the addend must be adjusted
6979 accordingly. */
6980
6981 static int
6982 ppc_elf_relocate_section (bfd *output_bfd,
6983 struct bfd_link_info *info,
6984 bfd *input_bfd,
6985 asection *input_section,
6986 bfd_byte *contents,
6987 Elf_Internal_Rela *relocs,
6988 Elf_Internal_Sym *local_syms,
6989 asection **local_sections)
6990 {
6991 Elf_Internal_Shdr *symtab_hdr;
6992 struct elf_link_hash_entry **sym_hashes;
6993 struct ppc_elf_link_hash_table *htab;
6994 Elf_Internal_Rela *rel;
6995 Elf_Internal_Rela *wrel;
6996 Elf_Internal_Rela *relend;
6997 Elf_Internal_Rela outrel;
6998 asection *got2;
6999 bfd_vma *local_got_offsets;
7000 bool ret = true;
7001 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
7002 bool is_vxworks_tls;
7003 unsigned int picfixup_size = 0;
7004 struct ppc_elf_relax_info *relax_info = NULL;
7005
7006 #ifdef DEBUG
7007 _bfd_error_handler ("ppc_elf_relocate_section called for %pB section %pA, "
7008 "%ld relocations%s",
7009 input_bfd, input_section,
7010 (long) input_section->reloc_count,
7011 (bfd_link_relocatable (info)) ? " (relocatable)" : "");
7012 #endif
7013
7014 if (!is_ppc_elf (input_bfd))
7015 {
7016 bfd_set_error (bfd_error_wrong_format);
7017 return false;
7018 }
7019
7020 got2 = bfd_get_section_by_name (input_bfd, ".got2");
7021
7022 /* Initialize howto table if not already done. */
7023 if (!ppc_elf_howto_table[R_PPC_ADDR32])
7024 ppc_elf_howto_init ();
7025
7026 htab = ppc_elf_hash_table (info);
7027 local_got_offsets = elf_local_got_offsets (input_bfd);
7028 symtab_hdr = &elf_symtab_hdr (input_bfd);
7029 sym_hashes = elf_sym_hashes (input_bfd);
7030 /* We have to handle relocations in vxworks .tls_vars sections
7031 specially, because the dynamic loader is 'weird'. */
7032 is_vxworks_tls = (htab->elf.target_os == is_vxworks && bfd_link_pic (info)
7033 && !strcmp (input_section->output_section->name,
7034 ".tls_vars"));
7035 if (input_section->sec_info_type == SEC_INFO_TYPE_TARGET)
7036 relax_info = elf_section_data (input_section)->sec_info;
7037 rel = wrel = relocs;
7038 relend = relocs + input_section->reloc_count;
7039 for (; rel < relend; wrel++, rel++)
7040 {
7041 enum elf_ppc_reloc_type r_type;
7042 bfd_vma addend;
7043 bfd_reloc_status_type r;
7044 Elf_Internal_Sym *sym;
7045 asection *sec;
7046 struct elf_link_hash_entry *h;
7047 const char *sym_name;
7048 reloc_howto_type *howto;
7049 unsigned long r_symndx;
7050 bfd_vma relocation;
7051 bfd_vma branch_bit, from;
7052 bool unresolved_reloc, save_unresolved_reloc;
7053 bool warned;
7054 unsigned int tls_type, tls_mask, tls_gd;
7055 struct plt_entry **ifunc, **plt_list;
7056 struct reloc_howto_struct alt_howto;
7057
7058 again:
7059 r_type = ELF32_R_TYPE (rel->r_info);
7060 sym = NULL;
7061 sec = NULL;
7062 h = NULL;
7063 unresolved_reloc = false;
7064 warned = false;
7065 r_symndx = ELF32_R_SYM (rel->r_info);
7066
7067 if (r_symndx < symtab_hdr->sh_info)
7068 {
7069 sym = local_syms + r_symndx;
7070 sec = local_sections[r_symndx];
7071 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
7072
7073 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
7074 }
7075 else
7076 {
7077 bool ignored;
7078
7079 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
7080 r_symndx, symtab_hdr, sym_hashes,
7081 h, sec, relocation,
7082 unresolved_reloc, warned, ignored);
7083
7084 sym_name = h->root.root.string;
7085 }
7086
7087 if (sec != NULL && discarded_section (sec))
7088 {
7089 /* For relocs against symbols from removed linkonce sections,
7090 or sections discarded by a linker script, we just want the
7091 section contents zeroed. Avoid any special processing. */
7092 howto = NULL;
7093 if (r_type < R_PPC_max)
7094 howto = ppc_elf_howto_table[r_type];
7095
7096 _bfd_clear_contents (howto, input_bfd, input_section,
7097 contents, rel->r_offset);
7098 wrel->r_offset = rel->r_offset;
7099 wrel->r_info = 0;
7100 wrel->r_addend = 0;
7101
7102 /* For ld -r, remove relocations in debug sections against
7103 symbols defined in discarded sections. Not done for
7104 non-debug to preserve relocs in .eh_frame which the
7105 eh_frame editing code expects to be present. */
7106 if (bfd_link_relocatable (info)
7107 && (input_section->flags & SEC_DEBUGGING))
7108 wrel--;
7109
7110 continue;
7111 }
7112
7113 if (bfd_link_relocatable (info))
7114 {
7115 if (got2 != NULL
7116 && r_type == R_PPC_PLTREL24
7117 && rel->r_addend != 0)
7118 {
7119 /* R_PPC_PLTREL24 is rather special. If non-zero, the
7120 addend specifies the GOT pointer offset within .got2. */
7121 rel->r_addend += got2->output_offset;
7122 }
7123 if (r_type != R_PPC_RELAX_PLT
7124 && r_type != R_PPC_RELAX_PLTREL24
7125 && r_type != R_PPC_RELAX)
7126 goto copy_reloc;
7127 }
7128
7129 /* TLS optimizations. Replace instruction sequences and relocs
7130 based on information we collected in tls_optimize. We edit
7131 RELOCS so that --emit-relocs will output something sensible
7132 for the final instruction stream. */
7133 tls_mask = 0;
7134 tls_gd = 0;
7135 if (h != NULL)
7136 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
7137 else if (local_got_offsets != NULL)
7138 {
7139 struct plt_entry **local_plt;
7140 char *lgot_masks;
7141 local_plt
7142 = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
7143 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
7144 tls_mask = lgot_masks[r_symndx];
7145 }
7146
7147 /* Ensure reloc mapping code below stays sane. */
7148 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
7149 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
7150 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
7151 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
7152 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
7153 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
7154 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
7155 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
7156 abort ();
7157 switch (r_type)
7158 {
7159 default:
7160 break;
7161
7162 case R_PPC_GOT_TPREL16:
7163 case R_PPC_GOT_TPREL16_LO:
7164 if ((tls_mask & TLS_TLS) != 0
7165 && (tls_mask & TLS_TPREL) == 0
7166 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
7167 {
7168 bfd_vma insn;
7169
7170 insn = bfd_get_32 (input_bfd,
7171 contents + rel->r_offset - d_offset);
7172 insn &= 31 << 21;
7173 insn |= 0x3c020000; /* addis 0,2,0 */
7174 bfd_put_32 (input_bfd, insn,
7175 contents + rel->r_offset - d_offset);
7176 r_type = R_PPC_TPREL16_HA;
7177 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7178 }
7179 break;
7180
7181 case R_PPC_TLS:
7182 if ((tls_mask & TLS_TLS) != 0
7183 && (tls_mask & TLS_TPREL) == 0
7184 && offset_in_range (input_section, rel->r_offset, 4))
7185 {
7186 bfd_vma insn;
7187
7188 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
7189 insn = _bfd_elf_ppc_at_tls_transform (insn, 2);
7190 if (insn == 0)
7191 abort ();
7192 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
7193 r_type = R_PPC_TPREL16_LO;
7194 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7195
7196 /* Was PPC_TLS which sits on insn boundary, now
7197 PPC_TPREL16_LO which is at low-order half-word. */
7198 rel->r_offset += d_offset;
7199 }
7200 break;
7201
7202 case R_PPC_GOT_TLSGD16_HI:
7203 case R_PPC_GOT_TLSGD16_HA:
7204 tls_gd = TLS_GDIE;
7205 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
7206 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
7207 goto tls_gdld_hi;
7208 break;
7209
7210 case R_PPC_GOT_TLSLD16_HI:
7211 case R_PPC_GOT_TLSLD16_HA:
7212 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
7213 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
7214 {
7215 tls_gdld_hi:
7216 if ((tls_mask & tls_gd) != 0)
7217 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
7218 + R_PPC_GOT_TPREL16);
7219 else
7220 {
7221 rel->r_offset -= d_offset;
7222 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
7223 r_type = R_PPC_NONE;
7224 }
7225 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7226 }
7227 break;
7228
7229 case R_PPC_GOT_TLSGD16:
7230 case R_PPC_GOT_TLSGD16_LO:
7231 tls_gd = TLS_GDIE;
7232 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
7233 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
7234 goto tls_ldgd_opt;
7235 break;
7236
7237 case R_PPC_GOT_TLSLD16:
7238 case R_PPC_GOT_TLSLD16_LO:
7239 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
7240 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
7241 {
7242 unsigned int insn1, insn2;
7243 bfd_vma offset;
7244
7245 tls_ldgd_opt:
7246 offset = (bfd_vma) -1;
7247 /* If not using the newer R_PPC_TLSGD/LD to mark
7248 __tls_get_addr calls, we must trust that the call
7249 stays with its arg setup insns, ie. that the next
7250 reloc is the __tls_get_addr call associated with
7251 the current reloc. Edit both insns. */
7252 if (input_section->nomark_tls_get_addr
7253 && rel + 1 < relend
7254 && branch_reloc_hash_match (input_bfd, rel + 1,
7255 htab->tls_get_addr))
7256 offset = rel[1].r_offset;
7257 /* We read the low GOT_TLS insn because we need to keep
7258 the destination reg. It may be something other than
7259 the usual r3, and moved to r3 before the call by
7260 intervening code. */
7261 insn1 = bfd_get_32 (input_bfd,
7262 contents + rel->r_offset - d_offset);
7263 if ((tls_mask & tls_gd) != 0)
7264 {
7265 /* IE */
7266 insn1 &= (0x1f << 21) | (0x1f << 16);
7267 insn1 |= 32u << 26; /* lwz */
7268 if (offset != (bfd_vma) -1
7269 && offset_in_range (input_section, offset, 4))
7270 {
7271 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7272 insn2 = 0x7c631214; /* add 3,3,2 */
7273 bfd_put_32 (input_bfd, insn2, contents + offset);
7274 }
7275 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
7276 + R_PPC_GOT_TPREL16);
7277 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7278 }
7279 else
7280 {
7281 /* LE */
7282 insn1 &= 0x1f << 21;
7283 insn1 |= 0x3c020000; /* addis r,2,0 */
7284 if (tls_gd == 0)
7285 {
7286 /* Was an LD reloc. */
7287 for (r_symndx = 0;
7288 r_symndx < symtab_hdr->sh_info;
7289 r_symndx++)
7290 if (local_sections[r_symndx] == sec)
7291 break;
7292 if (r_symndx >= symtab_hdr->sh_info)
7293 r_symndx = STN_UNDEF;
7294 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7295 if (r_symndx != STN_UNDEF)
7296 rel->r_addend -= (local_syms[r_symndx].st_value
7297 + sec->output_offset
7298 + sec->output_section->vma);
7299 }
7300 r_type = R_PPC_TPREL16_HA;
7301 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7302 if (offset != (bfd_vma) -1
7303 && offset_in_range (input_section, offset, 4))
7304 {
7305 rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
7306 rel[1].r_offset = offset + d_offset;
7307 rel[1].r_addend = rel->r_addend;
7308 insn2 = 0x38630000; /* addi 3,3,0 */
7309 bfd_put_32 (input_bfd, insn2, contents + offset);
7310 }
7311 }
7312 bfd_put_32 (input_bfd, insn1,
7313 contents + rel->r_offset - d_offset);
7314 if (tls_gd == 0)
7315 {
7316 /* We changed the symbol on an LD reloc. Start over
7317 in order to get h, sym, sec etc. right. */
7318 goto again;
7319 }
7320 }
7321 break;
7322
7323 case R_PPC_TLSGD:
7324 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
7325 && rel + 1 < relend
7326 && offset_in_range (input_section, rel->r_offset, 4))
7327 {
7328 unsigned int insn2;
7329 bfd_vma offset = rel->r_offset;
7330
7331 if (is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
7332 {
7333 bfd_put_32 (input_bfd, NOP, contents + offset);
7334 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7335 break;
7336 }
7337
7338 if ((tls_mask & TLS_GDIE) != 0)
7339 {
7340 /* IE */
7341 r_type = R_PPC_NONE;
7342 insn2 = 0x7c631214; /* add 3,3,2 */
7343 }
7344 else
7345 {
7346 /* LE */
7347 r_type = R_PPC_TPREL16_LO;
7348 rel->r_offset += d_offset;
7349 insn2 = 0x38630000; /* addi 3,3,0 */
7350 }
7351 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7352 bfd_put_32 (input_bfd, insn2, contents + offset);
7353 /* Zap the reloc on the _tls_get_addr call too. */
7354 BFD_ASSERT (offset == rel[1].r_offset);
7355 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7356 }
7357 break;
7358
7359 case R_PPC_TLSLD:
7360 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
7361 && rel + 1 < relend
7362 && offset_in_range (input_section, rel->r_offset, 4))
7363 {
7364 unsigned int insn2;
7365
7366 if (is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
7367 {
7368 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
7369 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7370 break;
7371 }
7372
7373 for (r_symndx = 0;
7374 r_symndx < symtab_hdr->sh_info;
7375 r_symndx++)
7376 if (local_sections[r_symndx] == sec)
7377 break;
7378 if (r_symndx >= symtab_hdr->sh_info)
7379 r_symndx = STN_UNDEF;
7380 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7381 if (r_symndx != STN_UNDEF)
7382 rel->r_addend -= (local_syms[r_symndx].st_value
7383 + sec->output_offset
7384 + sec->output_section->vma);
7385
7386 rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
7387 rel->r_offset += d_offset;
7388 insn2 = 0x38630000; /* addi 3,3,0 */
7389 bfd_put_32 (input_bfd, insn2,
7390 contents + rel->r_offset - d_offset);
7391 /* Zap the reloc on the _tls_get_addr call too. */
7392 BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
7393 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7394 goto again;
7395 }
7396 break;
7397 }
7398
7399 /* Handle other relocations that tweak non-addend part of insn. */
7400 branch_bit = 0;
7401 switch (r_type)
7402 {
7403 default:
7404 break;
7405
7406 /* Branch taken prediction relocations. */
7407 case R_PPC_ADDR14_BRTAKEN:
7408 case R_PPC_REL14_BRTAKEN:
7409 branch_bit = BRANCH_PREDICT_BIT;
7410 /* Fall through. */
7411
7412 /* Branch not taken prediction relocations. */
7413 case R_PPC_ADDR14_BRNTAKEN:
7414 case R_PPC_REL14_BRNTAKEN:
7415 if (offset_in_range (input_section, rel->r_offset, 4))
7416 {
7417 unsigned int insn;
7418
7419 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
7420 insn &= ~BRANCH_PREDICT_BIT;
7421 insn |= branch_bit;
7422
7423 from = (rel->r_offset
7424 + input_section->output_offset
7425 + input_section->output_section->vma);
7426
7427 /* Invert 'y' bit if not the default. */
7428 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7429 insn ^= BRANCH_PREDICT_BIT;
7430
7431 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
7432 }
7433 break;
7434
7435 case R_PPC_PLT16_HA:
7436 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
7437 {
7438 unsigned int insn;
7439
7440 insn = bfd_get_32 (input_bfd,
7441 contents + rel->r_offset - d_offset);
7442 if ((insn & (0x3fu << 26)) == 15u << 26
7443 && (insn & (0x1f << 16)) != 0)
7444 {
7445 if (!bfd_link_pic (info))
7446 {
7447 /* Convert addis to lis. */
7448 insn &= ~(0x1f << 16);
7449 bfd_put_32 (input_bfd, insn,
7450 contents + rel->r_offset - d_offset);
7451 }
7452 }
7453 else if (bfd_link_pic (info))
7454 info->callbacks->einfo
7455 (_("%P: %H: error: %s with unexpected instruction %x\n"),
7456 input_bfd, input_section, rel->r_offset,
7457 "R_PPC_PLT16_HA", insn);
7458 }
7459 break;
7460 }
7461
7462 if (ELIMINATE_COPY_RELOCS
7463 && h != NULL
7464 && !h->def_regular
7465 && h->protected_def
7466 && ppc_elf_hash_entry (h)->has_addr16_ha
7467 && ppc_elf_hash_entry (h)->has_addr16_lo
7468 && htab->params->pic_fixup > 0)
7469 {
7470 /* Convert lis;addi or lis;load/store accessing a protected
7471 variable defined in a shared library to PIC. */
7472 unsigned int insn;
7473
7474 if (r_type == R_PPC_ADDR16_HA
7475 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
7476 {
7477 insn = bfd_get_32 (input_bfd,
7478 contents + rel->r_offset - d_offset);
7479 if ((insn & (0x3fu << 26)) == (15u << 26)
7480 && (insn & (0x1f << 16)) == 0 /* lis */)
7481 {
7482 bfd_byte *p;
7483 bfd_vma off;
7484 bfd_vma got_addr;
7485
7486 p = (contents + input_section->size
7487 - relax_info->workaround_size
7488 - relax_info->picfixup_size
7489 + picfixup_size);
7490 off = (p - contents) - (rel->r_offset - d_offset);
7491 if (off > 0x1fffffc || (off & 3) != 0)
7492 info->callbacks->einfo
7493 (_("%H: fixup branch overflow\n"),
7494 input_bfd, input_section, rel->r_offset);
7495
7496 bfd_put_32 (input_bfd, B | off,
7497 contents + rel->r_offset - d_offset);
7498 got_addr = (htab->elf.sgot->output_section->vma
7499 + htab->elf.sgot->output_offset
7500 + (h->got.offset & ~1));
7501 wrel->r_offset = (p - contents) + d_offset;
7502 wrel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_HA);
7503 wrel->r_addend = got_addr;
7504 insn &= ~0xffff;
7505 insn |= ((unsigned int) (got_addr + 0x8000) >> 16) & 0xffff;
7506 bfd_put_32 (input_bfd, insn, p);
7507
7508 /* Convert lis to lwz, loading address from GOT. */
7509 insn &= ~0xffff;
7510 insn ^= (32u ^ 15u) << 26;
7511 insn |= (insn & (0x1f << 21)) >> 5;
7512 insn |= got_addr & 0xffff;
7513 bfd_put_32 (input_bfd, insn, p + 4);
7514
7515 bfd_put_32 (input_bfd, B | ((-4 - off) & 0x3ffffff), p + 8);
7516 picfixup_size += 12;
7517
7518 /* Use one of the spare relocs, so --emit-relocs
7519 output is reasonable. */
7520 memmove (rel + 1, rel, (relend - rel - 1) * sizeof (*rel));
7521 wrel++, rel++;
7522 rel->r_offset = wrel[-1].r_offset + 4;
7523 rel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_LO);
7524 rel->r_addend = wrel[-1].r_addend;
7525
7526 /* Continue on as if we had a got reloc, to output
7527 dynamic reloc. */
7528 r_type = R_PPC_GOT16_LO;
7529 }
7530 else
7531 _bfd_error_handler
7532 /* xgettext:c-format */
7533 (_("%pB(%pA+%#" PRIx64 "): error: "
7534 "%s with unexpected instruction %#x"),
7535 input_bfd, input_section, (uint64_t) rel->r_offset,
7536 "R_PPC_ADDR16_HA", insn);
7537 }
7538 else if (r_type == R_PPC_ADDR16_LO
7539 && offset_in_range (input_section,
7540 rel->r_offset - d_offset, 4))
7541 {
7542 insn = bfd_get_32 (input_bfd,
7543 contents + rel->r_offset - d_offset);
7544 if ((insn & (0x3fu << 26)) == 14u << 26 /* addi */
7545 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
7546 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
7547 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
7548 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
7549 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
7550 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
7551 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
7552 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
7553 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
7554 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
7555 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
7556 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
7557 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
7558 || ((insn & (0x3fu << 26)) == 58u << 26 /* lwa,ld,lmd */
7559 && (insn & 3) != 1)
7560 || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
7561 && ((insn & 3) == 0 || (insn & 3) == 3)))
7562 {
7563 /* Arrange to apply the reloc addend, if any. */
7564 relocation = 0;
7565 unresolved_reloc = false;
7566 rel->r_info = ELF32_R_INFO (0, r_type);
7567 }
7568 else
7569 _bfd_error_handler
7570 /* xgettext:c-format */
7571 (_("%pB(%pA+%#" PRIx64 "): error: "
7572 "%s with unexpected instruction %#x"),
7573 input_bfd, input_section, (uint64_t) rel->r_offset,
7574 "R_PPC_ADDR16_LO", insn);
7575 }
7576 }
7577
7578 ifunc = NULL;
7579 if (htab->elf.target_os != is_vxworks)
7580 {
7581 struct plt_entry *ent;
7582
7583 if (h != NULL)
7584 {
7585 if (h->type == STT_GNU_IFUNC)
7586 ifunc = &h->plt.plist;
7587 }
7588 else if (local_got_offsets != NULL
7589 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
7590 {
7591 struct plt_entry **local_plt;
7592
7593 local_plt = (struct plt_entry **) (local_got_offsets
7594 + symtab_hdr->sh_info);
7595 ifunc = local_plt + r_symndx;
7596 }
7597
7598 ent = NULL;
7599 if (ifunc != NULL
7600 && (!bfd_link_pic (info)
7601 || is_branch_reloc (r_type)
7602 || r_type == R_PPC_PLT16_LO
7603 || r_type == R_PPC_PLT16_HI
7604 || r_type == R_PPC_PLT16_HA))
7605 {
7606 addend = 0;
7607 if (bfd_link_pic (info)
7608 && (r_type == R_PPC_PLTREL24
7609 || r_type == R_PPC_PLT16_LO
7610 || r_type == R_PPC_PLT16_HI
7611 || r_type == R_PPC_PLT16_HA))
7612 addend = rel->r_addend;
7613 ent = find_plt_ent (ifunc, got2, addend);
7614 }
7615 if (ent != NULL)
7616 {
7617 if (bfd_link_pic (info)
7618 && ent->sec != got2
7619 && htab->plt_type != PLT_NEW
7620 && (!htab->elf.dynamic_sections_created
7621 || h == NULL
7622 || h->dynindx == -1))
7623 {
7624 /* Uh oh, we are going to create a pic glink stub
7625 for an ifunc (here for h == NULL and later in
7626 finish_dynamic_symbol for h != NULL), and
7627 apparently are using code compiled with
7628 -mbss-plt. The difficulty is that -mbss-plt code
7629 gives no indication via a magic PLTREL24 addend
7630 whether r30 is equal to _GLOBAL_OFFSET_TABLE_ or
7631 is pointing into a .got2 section (and how far
7632 into .got2). */
7633 info->callbacks->einfo
7634 /* xgettext:c-format */
7635 (_("%X%H: unsupported bss-plt -fPIC ifunc %s\n"),
7636 input_bfd, input_section, rel->r_offset, sym_name);
7637 }
7638
7639 unresolved_reloc = false;
7640 if (htab->plt_type == PLT_NEW
7641 || !htab->elf.dynamic_sections_created
7642 || h == NULL
7643 || h->dynindx == -1)
7644 relocation = (htab->glink->output_section->vma
7645 + htab->glink->output_offset
7646 + (ent->glink_offset & ~1));
7647 else
7648 relocation = (htab->elf.splt->output_section->vma
7649 + htab->elf.splt->output_offset
7650 + ent->plt.offset);
7651 }
7652 }
7653
7654 addend = rel->r_addend;
7655 save_unresolved_reloc = unresolved_reloc;
7656 howto = NULL;
7657 if (r_type < R_PPC_max)
7658 howto = ppc_elf_howto_table[r_type];
7659
7660 tls_type = 0;
7661 switch (r_type)
7662 {
7663 default:
7664 de_fault:
7665 if (howto)
7666 /* xgettext:c-format */
7667 _bfd_error_handler (_("%pB: %s unsupported"),
7668 input_bfd, howto->name);
7669 else
7670 /* xgettext:c-format */
7671 _bfd_error_handler (_("%pB: reloc %#x unsupported"),
7672 input_bfd, r_type);
7673
7674 bfd_set_error (bfd_error_bad_value);
7675 ret = false;
7676 goto copy_reloc;
7677
7678 case R_PPC_NONE:
7679 case R_PPC_TLS:
7680 case R_PPC_TLSGD:
7681 case R_PPC_TLSLD:
7682 case R_PPC_EMB_MRKREF:
7683 case R_PPC_GNU_VTINHERIT:
7684 case R_PPC_GNU_VTENTRY:
7685 goto copy_reloc;
7686
7687 /* GOT16 relocations. Like an ADDR16 using the symbol's
7688 address in the GOT as relocation value instead of the
7689 symbol's value itself. Also, create a GOT entry for the
7690 symbol and put the symbol value there. */
7691 case R_PPC_GOT_TLSGD16:
7692 case R_PPC_GOT_TLSGD16_LO:
7693 case R_PPC_GOT_TLSGD16_HI:
7694 case R_PPC_GOT_TLSGD16_HA:
7695 tls_type = TLS_TLS | TLS_GD;
7696 goto dogot;
7697
7698 case R_PPC_GOT_TLSLD16:
7699 case R_PPC_GOT_TLSLD16_LO:
7700 case R_PPC_GOT_TLSLD16_HI:
7701 case R_PPC_GOT_TLSLD16_HA:
7702 tls_type = TLS_TLS | TLS_LD;
7703 goto dogot;
7704
7705 case R_PPC_GOT_TPREL16:
7706 case R_PPC_GOT_TPREL16_LO:
7707 case R_PPC_GOT_TPREL16_HI:
7708 case R_PPC_GOT_TPREL16_HA:
7709 tls_type = TLS_TLS | TLS_TPREL;
7710 goto dogot;
7711
7712 case R_PPC_GOT_DTPREL16:
7713 case R_PPC_GOT_DTPREL16_LO:
7714 case R_PPC_GOT_DTPREL16_HI:
7715 case R_PPC_GOT_DTPREL16_HA:
7716 tls_type = TLS_TLS | TLS_DTPREL;
7717 goto dogot;
7718
7719 case R_PPC_GOT16:
7720 case R_PPC_GOT16_LO:
7721 case R_PPC_GOT16_HI:
7722 case R_PPC_GOT16_HA:
7723 tls_mask = 0;
7724 dogot:
7725 {
7726 /* Relocation is to the entry for this symbol in the global
7727 offset table. */
7728 bfd_vma off;
7729 bfd_vma *offp;
7730 unsigned long indx;
7731
7732 if (htab->elf.sgot == NULL)
7733 abort ();
7734
7735 indx = 0;
7736 if (tls_type == (TLS_TLS | TLS_LD)
7737 && SYMBOL_REFERENCES_LOCAL (info, h))
7738 offp = &htab->tlsld_got.offset;
7739 else if (h != NULL)
7740 {
7741 if (!htab->elf.dynamic_sections_created
7742 || h->dynindx == -1
7743 || SYMBOL_REFERENCES_LOCAL (info, h)
7744 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
7745 /* This is actually a static link, or it is a
7746 -Bsymbolic link and the symbol is defined
7747 locally, or the symbol was forced to be local
7748 because of a version file. */
7749 ;
7750 else
7751 {
7752 indx = h->dynindx;
7753 unresolved_reloc = false;
7754 }
7755 offp = &h->got.offset;
7756 }
7757 else
7758 {
7759 if (local_got_offsets == NULL)
7760 abort ();
7761 offp = &local_got_offsets[r_symndx];
7762 }
7763
7764 /* The offset must always be a multiple of 4. We use the
7765 least significant bit to record whether we have already
7766 processed this entry. */
7767 off = *offp;
7768 if ((off & 1) != 0)
7769 off &= ~1;
7770 else
7771 {
7772 unsigned int tls_m = ((tls_mask & TLS_TLS) != 0
7773 ? tls_mask & (TLS_LD | TLS_GD | TLS_DTPREL
7774 | TLS_TPREL | TLS_GDIE)
7775 : 0);
7776
7777 if (offp == &htab->tlsld_got.offset)
7778 tls_m = TLS_LD;
7779 else if ((tls_m & TLS_LD) != 0
7780 && SYMBOL_REFERENCES_LOCAL (info, h))
7781 tls_m &= ~TLS_LD;
7782
7783 /* We might have multiple got entries for this sym.
7784 Initialize them all. */
7785 do
7786 {
7787 int tls_ty = 0;
7788
7789 if ((tls_m & TLS_LD) != 0)
7790 {
7791 tls_ty = TLS_TLS | TLS_LD;
7792 tls_m &= ~TLS_LD;
7793 }
7794 else if ((tls_m & TLS_GD) != 0)
7795 {
7796 tls_ty = TLS_TLS | TLS_GD;
7797 tls_m &= ~TLS_GD;
7798 }
7799 else if ((tls_m & TLS_DTPREL) != 0)
7800 {
7801 tls_ty = TLS_TLS | TLS_DTPREL;
7802 tls_m &= ~TLS_DTPREL;
7803 }
7804 else if ((tls_m & (TLS_TPREL | TLS_GDIE)) != 0)
7805 {
7806 tls_ty = TLS_TLS | TLS_TPREL;
7807 tls_m = 0;
7808 }
7809
7810 /* Generate relocs for the dynamic linker. */
7811 if (indx != 0
7812 || (bfd_link_pic (info)
7813 && (h == NULL
7814 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
7815 && !(tls_ty != 0
7816 && bfd_link_executable (info)
7817 && SYMBOL_REFERENCES_LOCAL (info, h))
7818 && (h != NULL
7819 ? !bfd_is_abs_symbol (&h->root)
7820 : sym->st_shndx != SHN_ABS)))
7821 {
7822 asection *rsec = htab->elf.srelgot;
7823 bfd_byte * loc;
7824
7825 if (ifunc != NULL)
7826 {
7827 rsec = htab->elf.irelplt;
7828 if (indx == 0)
7829 htab->local_ifunc_resolver = 1;
7830 else if (is_static_defined (h))
7831 htab->maybe_local_ifunc_resolver = 1;
7832 }
7833 outrel.r_offset = (htab->elf.sgot->output_section->vma
7834 + htab->elf.sgot->output_offset
7835 + off);
7836 outrel.r_addend = 0;
7837 if (tls_ty & (TLS_LD | TLS_GD))
7838 {
7839 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
7840 if (tls_ty == (TLS_TLS | TLS_GD))
7841 {
7842 loc = rsec->contents;
7843 loc += (rsec->reloc_count++
7844 * sizeof (Elf32_External_Rela));
7845 bfd_elf32_swap_reloca_out (output_bfd,
7846 &outrel, loc);
7847 outrel.r_offset += 4;
7848 outrel.r_info
7849 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7850 }
7851 }
7852 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
7853 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7854 else if (tls_ty == (TLS_TLS | TLS_TPREL))
7855 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
7856 else if (indx != 0)
7857 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
7858 else if (ifunc != NULL)
7859 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7860 else
7861 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
7862 if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
7863 {
7864 outrel.r_addend += relocation;
7865 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
7866 {
7867 if (htab->elf.tls_sec == NULL)
7868 outrel.r_addend = 0;
7869 else
7870 outrel.r_addend -= htab->elf.tls_sec->vma;
7871 }
7872 }
7873 loc = rsec->contents;
7874 loc += (rsec->reloc_count++
7875 * sizeof (Elf32_External_Rela));
7876 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7877 }
7878
7879 /* Init the .got section contents if we're not
7880 emitting a reloc. */
7881 else
7882 {
7883 bfd_vma value = relocation;
7884
7885 if (tls_ty != 0)
7886 {
7887 if (htab->elf.tls_sec == NULL)
7888 value = 0;
7889 else
7890 {
7891 if (tls_ty & TLS_LD)
7892 value = 0;
7893 else
7894 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
7895 if (tls_ty & TLS_TPREL)
7896 value += DTP_OFFSET - TP_OFFSET;
7897 }
7898
7899 if (tls_ty & (TLS_LD | TLS_GD))
7900 {
7901 bfd_put_32 (input_bfd, value,
7902 htab->elf.sgot->contents + off + 4);
7903 value = 1;
7904 }
7905 }
7906 bfd_put_32 (input_bfd, value,
7907 htab->elf.sgot->contents + off);
7908 }
7909
7910 off += 4;
7911 if (tls_ty & (TLS_LD | TLS_GD))
7912 off += 4;
7913 }
7914 while (tls_m != 0);
7915
7916 off = *offp;
7917 *offp = off | 1;
7918 }
7919
7920 if (off >= (bfd_vma) -2)
7921 abort ();
7922
7923 if ((tls_type & TLS_TLS) != 0)
7924 {
7925 if (tls_type != (TLS_TLS | TLS_LD))
7926 {
7927 if ((tls_mask & TLS_LD) != 0
7928 && !SYMBOL_REFERENCES_LOCAL (info, h))
7929 off += 8;
7930 if (tls_type != (TLS_TLS | TLS_GD))
7931 {
7932 if ((tls_mask & TLS_GD) != 0)
7933 off += 8;
7934 if (tls_type != (TLS_TLS | TLS_DTPREL))
7935 {
7936 if ((tls_mask & TLS_DTPREL) != 0)
7937 off += 4;
7938 }
7939 }
7940 }
7941 }
7942
7943 /* If here for a picfixup, we're done. */
7944 if (r_type != ELF32_R_TYPE (rel->r_info))
7945 goto copy_reloc;
7946
7947 relocation = (htab->elf.sgot->output_section->vma
7948 + htab->elf.sgot->output_offset
7949 + off
7950 - SYM_VAL (htab->elf.hgot));
7951
7952 /* Addends on got relocations don't make much sense.
7953 x+off@got is actually x@got+off, and since the got is
7954 generated by a hash table traversal, the value in the
7955 got at entry m+n bears little relation to the entry m. */
7956 if (addend != 0)
7957 info->callbacks->einfo
7958 /* xgettext:c-format */
7959 (_("%H: non-zero addend on %s reloc against `%s'\n"),
7960 input_bfd, input_section, rel->r_offset,
7961 howto->name,
7962 sym_name);
7963 }
7964 break;
7965
7966 /* Relocations that need no special processing. */
7967 case R_PPC_LOCAL24PC:
7968 /* It makes no sense to point a local relocation
7969 at a symbol not in this object. */
7970 if (unresolved_reloc)
7971 {
7972 (*info->callbacks->undefined_symbol) (info,
7973 h->root.root.string,
7974 input_bfd,
7975 input_section,
7976 rel->r_offset,
7977 true);
7978 goto copy_reloc;
7979 }
7980 if (h != NULL && h->type == STT_GNU_IFUNC && bfd_link_pic (info))
7981 {
7982 /* @local on an ifunc does not really make sense since
7983 the ifunc resolver can take you anywhere. More
7984 seriously, calls to ifuncs must go through a plt call
7985 stub, and for pic the plt call stubs uses r30 to
7986 access the PLT. The problem is that a call that is
7987 local won't have the +32k reloc addend trick marking
7988 -fPIC code, so the linker won't know whether r30 is
7989 _GLOBAL_OFFSET_TABLE_ or pointing into a .got2 section. */
7990 /* xgettext:c-format */
7991 info->callbacks->einfo (_("%X%H: @local call to ifunc %s\n"),
7992 input_bfd, input_section, rel->r_offset,
7993 h->root.root.string);
7994 }
7995 break;
7996
7997 case R_PPC_DTPREL16:
7998 case R_PPC_DTPREL16_LO:
7999 case R_PPC_DTPREL16_HI:
8000 case R_PPC_DTPREL16_HA:
8001 if (htab->elf.tls_sec != NULL)
8002 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
8003 break;
8004
8005 /* Relocations that may need to be propagated if this is a shared
8006 object. */
8007 case R_PPC_TPREL16:
8008 case R_PPC_TPREL16_LO:
8009 case R_PPC_TPREL16_HI:
8010 case R_PPC_TPREL16_HA:
8011 if (h != NULL
8012 && h->root.type == bfd_link_hash_undefweak
8013 && h->dynindx == -1
8014 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
8015 {
8016 /* Make this relocation against an undefined weak symbol
8017 resolve to zero. This is really just a tweak, since
8018 code using weak externs ought to check that they are
8019 defined before using them. */
8020 bfd_byte *p = contents + rel->r_offset - d_offset;
8021 unsigned int insn = bfd_get_32 (input_bfd, p);
8022 insn = _bfd_elf_ppc_at_tprel_transform (insn, 2);
8023 if (insn != 0)
8024 bfd_put_32 (input_bfd, insn, p);
8025 break;
8026 }
8027 if (htab->elf.tls_sec != NULL)
8028 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
8029 /* The TPREL16 relocs shouldn't really be used in shared
8030 libs or with non-local symbols as that will result in
8031 DT_TEXTREL being set, but support them anyway. */
8032 goto dodyn;
8033
8034 case R_PPC_TPREL32:
8035 if (htab->elf.tls_sec != NULL)
8036 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
8037 goto dodyn;
8038
8039 case R_PPC_DTPREL32:
8040 if (htab->elf.tls_sec != NULL)
8041 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
8042 goto dodyn;
8043
8044 case R_PPC_DTPMOD32:
8045 relocation = 1;
8046 addend = 0;
8047 goto dodyn;
8048
8049 case R_PPC_REL16:
8050 case R_PPC_REL16_LO:
8051 case R_PPC_REL16_HI:
8052 case R_PPC_REL16_HA:
8053 case R_PPC_REL16DX_HA:
8054 break;
8055
8056 case R_PPC_REL32:
8057 if (h == NULL || h == htab->elf.hgot)
8058 break;
8059 /* fall through */
8060
8061 case R_PPC_ADDR32:
8062 case R_PPC_ADDR16:
8063 case R_PPC_ADDR16_LO:
8064 case R_PPC_ADDR16_HI:
8065 case R_PPC_ADDR16_HA:
8066 case R_PPC_UADDR32:
8067 case R_PPC_UADDR16:
8068 goto dodyn;
8069
8070 case R_PPC_VLE_REL8:
8071 case R_PPC_VLE_REL15:
8072 case R_PPC_VLE_REL24:
8073 case R_PPC_REL24:
8074 case R_PPC_REL14:
8075 case R_PPC_REL14_BRTAKEN:
8076 case R_PPC_REL14_BRNTAKEN:
8077 /* If these relocations are not to a named symbol, they can be
8078 handled right here, no need to bother the dynamic linker. */
8079 if (SYMBOL_CALLS_LOCAL (info, h)
8080 || h == htab->elf.hgot)
8081 break;
8082 /* fall through */
8083
8084 case R_PPC_ADDR24:
8085 case R_PPC_ADDR14:
8086 case R_PPC_ADDR14_BRTAKEN:
8087 case R_PPC_ADDR14_BRNTAKEN:
8088 if (h != NULL && !bfd_link_pic (info))
8089 break;
8090 /* fall through */
8091
8092 dodyn:
8093 if ((input_section->flags & SEC_ALLOC) == 0
8094 || is_vxworks_tls)
8095 break;
8096
8097 if (bfd_link_pic (info)
8098 ? ((h == NULL
8099 || h->dyn_relocs != NULL)
8100 && ((h != NULL && pc_dynrelocs (h))
8101 || must_be_dyn_reloc (info, r_type)))
8102 : (h != NULL
8103 && h->dyn_relocs != NULL))
8104 {
8105 int skip;
8106 bfd_byte *loc;
8107 asection *sreloc;
8108 long indx = 0;
8109
8110 #ifdef DEBUG
8111 fprintf (stderr, "ppc_elf_relocate_section needs to "
8112 "create relocation for %s\n",
8113 (h && h->root.root.string
8114 ? h->root.root.string : "<unknown>"));
8115 #endif
8116
8117 /* When generating a shared object, these relocations
8118 are copied into the output file to be resolved at run
8119 time. */
8120 skip = 0;
8121 outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
8122 input_section,
8123 rel->r_offset);
8124 if (outrel.r_offset == (bfd_vma) -1
8125 || outrel.r_offset == (bfd_vma) -2)
8126 skip = (int) outrel.r_offset;
8127 outrel.r_offset += (input_section->output_section->vma
8128 + input_section->output_offset);
8129
8130 /* Optimize unaligned reloc use. */
8131 if ((r_type == R_PPC_ADDR32 && (outrel.r_offset & 3) != 0)
8132 || (r_type == R_PPC_UADDR32 && (outrel.r_offset & 3) == 0))
8133 r_type ^= R_PPC_ADDR32 ^ R_PPC_UADDR32;
8134 if ((r_type == R_PPC_ADDR16 && (outrel.r_offset & 1) != 0)
8135 || (r_type == R_PPC_UADDR16 && (outrel.r_offset & 1) == 0))
8136 r_type ^= R_PPC_ADDR16 ^ R_PPC_UADDR16;
8137
8138 if (skip)
8139 memset (&outrel, 0, sizeof outrel);
8140 else if (!SYMBOL_REFERENCES_LOCAL (info, h))
8141 {
8142 indx = h->dynindx;
8143 BFD_ASSERT (indx != -1);
8144 unresolved_reloc = false;
8145 outrel.r_info = ELF32_R_INFO (indx, r_type);
8146 outrel.r_addend = rel->r_addend;
8147 }
8148 else
8149 {
8150 outrel.r_addend = relocation + rel->r_addend;
8151
8152 if (r_type != R_PPC_ADDR32)
8153 {
8154 if (ifunc != NULL)
8155 {
8156 /* If we get here when building a static
8157 executable, then the libc startup function
8158 responsible for applying indirect function
8159 relocations is going to complain about
8160 the reloc type.
8161 If we get here when building a dynamic
8162 executable, it will be because we have
8163 a text relocation. The dynamic loader
8164 will set the text segment writable and
8165 non-executable to apply text relocations.
8166 So we'll segfault when trying to run the
8167 indirection function to resolve the reloc. */
8168 info->callbacks->einfo
8169 /* xgettext:c-format */
8170 (_("%H: relocation %s for indirect "
8171 "function %s unsupported\n"),
8172 input_bfd, input_section, rel->r_offset,
8173 howto->name,
8174 sym_name);
8175 ret = false;
8176 }
8177 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
8178 ;
8179 else if (sec == NULL || sec->owner == NULL)
8180 {
8181 bfd_set_error (bfd_error_bad_value);
8182 ret = false;
8183 }
8184 else
8185 {
8186 asection *osec;
8187
8188 /* We are turning this relocation into one
8189 against a section symbol. It would be
8190 proper to subtract the symbol's value,
8191 osec->vma, from the emitted reloc addend,
8192 but ld.so expects buggy relocs.
8193 FIXME: Why not always use a zero index? */
8194 osec = sec->output_section;
8195 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
8196 {
8197 osec = htab->elf.tls_sec;
8198 indx = 0;
8199 }
8200 else
8201 {
8202 indx = elf_section_data (osec)->dynindx;
8203 if (indx == 0)
8204 {
8205 osec = htab->elf.text_index_section;
8206 indx = elf_section_data (osec)->dynindx;
8207 }
8208 BFD_ASSERT (indx != 0);
8209 }
8210
8211 /* ld.so doesn't expect buggy TLS relocs.
8212 Don't leave the symbol value in the
8213 addend for them. */
8214 if (IS_PPC_TLS_RELOC (r_type))
8215 outrel.r_addend -= osec->vma;
8216 }
8217
8218 outrel.r_info = ELF32_R_INFO (indx, r_type);
8219 }
8220 else if (ifunc != NULL)
8221 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
8222 else
8223 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
8224 }
8225
8226 sreloc = elf_section_data (input_section)->sreloc;
8227 if (ifunc)
8228 {
8229 sreloc = htab->elf.irelplt;
8230 if (indx == 0)
8231 htab->local_ifunc_resolver = 1;
8232 else if (is_static_defined (h))
8233 htab->maybe_local_ifunc_resolver = 1;
8234 }
8235 if (sreloc == NULL)
8236 return false;
8237
8238 loc = sreloc->contents;
8239 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
8240 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
8241
8242 if (skip == -1)
8243 goto copy_reloc;
8244
8245 /* This reloc will be computed at runtime. Clear the memory
8246 so that it contains a predictable value for prelink. */
8247 if (!skip)
8248 {
8249 relocation = howto->pc_relative ? outrel.r_offset : 0;
8250 addend = 0;
8251 break;
8252 }
8253 }
8254 break;
8255
8256 case R_PPC_RELAX_PLT:
8257 case R_PPC_RELAX_PLTREL24:
8258 if (h != NULL)
8259 {
8260 struct plt_entry *ent;
8261 bfd_vma got2_addend = 0;
8262
8263 if (r_type == R_PPC_RELAX_PLTREL24)
8264 {
8265 if (bfd_link_pic (info))
8266 got2_addend = addend;
8267 addend = 0;
8268 }
8269 ent = find_plt_ent (&h->plt.plist, got2, got2_addend);
8270 if (htab->plt_type == PLT_NEW)
8271 relocation = (htab->glink->output_section->vma
8272 + htab->glink->output_offset
8273 + ent->glink_offset);
8274 else
8275 relocation = (htab->elf.splt->output_section->vma
8276 + htab->elf.splt->output_offset
8277 + ent->plt.offset);
8278 }
8279 /* Fall through. */
8280
8281 case R_PPC_RELAX:
8282 if (bfd_link_pic (info)
8283 ? offset_in_range (input_section, rel->r_offset - 12,
8284 ARRAY_SIZE (shared_stub_entry) * 4)
8285 : offset_in_range (input_section, rel->r_offset,
8286 ARRAY_SIZE (stub_entry) * 4))
8287 {
8288 const int *stub;
8289 size_t size;
8290 size_t insn_offset = rel->r_offset;
8291 unsigned int insn;
8292
8293 if (bfd_link_pic (info))
8294 {
8295 relocation -= (input_section->output_section->vma
8296 + input_section->output_offset
8297 + rel->r_offset - 4);
8298 stub = shared_stub_entry;
8299 bfd_put_32 (input_bfd, stub[0], contents + insn_offset - 12);
8300 bfd_put_32 (input_bfd, stub[1], contents + insn_offset - 8);
8301 bfd_put_32 (input_bfd, stub[2], contents + insn_offset - 4);
8302 stub += 3;
8303 size = ARRAY_SIZE (shared_stub_entry) - 3;
8304 }
8305 else
8306 {
8307 stub = stub_entry;
8308 size = ARRAY_SIZE (stub_entry);
8309 }
8310
8311 relocation += addend;
8312 if (bfd_link_relocatable (info))
8313 relocation = 0;
8314
8315 /* First insn is HA, second is LO. */
8316 insn = *stub++;
8317 insn |= ((relocation + 0x8000) >> 16) & 0xffff;
8318 bfd_put_32 (input_bfd, insn, contents + insn_offset);
8319 insn_offset += 4;
8320
8321 insn = *stub++;
8322 insn |= relocation & 0xffff;
8323 bfd_put_32 (input_bfd, insn, contents + insn_offset);
8324 insn_offset += 4;
8325 size -= 2;
8326
8327 while (size != 0)
8328 {
8329 insn = *stub++;
8330 --size;
8331 bfd_put_32 (input_bfd, insn, contents + insn_offset);
8332 insn_offset += 4;
8333 }
8334
8335 /* Rewrite the reloc and convert one of the trailing nop
8336 relocs to describe this relocation. */
8337 BFD_ASSERT (ELF32_R_TYPE (relend[-1].r_info) == R_PPC_NONE);
8338 /* The relocs are at the bottom 2 bytes */
8339 wrel->r_offset = rel->r_offset + d_offset;
8340 wrel->r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
8341 wrel->r_addend = rel->r_addend;
8342 memmove (wrel + 1, wrel, (relend - wrel - 1) * sizeof (*wrel));
8343 wrel++, rel++;
8344 wrel->r_offset += 4;
8345 wrel->r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
8346 }
8347 else
8348 goto de_fault;
8349 continue;
8350
8351 /* Indirect .sdata relocation. */
8352 case R_PPC_EMB_SDAI16:
8353 BFD_ASSERT (htab->sdata[0].section != NULL);
8354 if (!is_static_defined (htab->sdata[0].sym))
8355 {
8356 unresolved_reloc = true;
8357 break;
8358 }
8359 relocation
8360 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
8361 h, relocation, rel);
8362 addend = 0;
8363 break;
8364
8365 /* Indirect .sdata2 relocation. */
8366 case R_PPC_EMB_SDA2I16:
8367 BFD_ASSERT (htab->sdata[1].section != NULL);
8368 if (!is_static_defined (htab->sdata[1].sym))
8369 {
8370 unresolved_reloc = true;
8371 break;
8372 }
8373 relocation
8374 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
8375 h, relocation, rel);
8376 addend = 0;
8377 break;
8378
8379 /* Handle the TOC16 reloc. We want to use the offset within the .got
8380 section, not the actual VMA. This is appropriate when generating
8381 an embedded ELF object, for which the .got section acts like the
8382 AIX .toc section. */
8383 case R_PPC_TOC16: /* phony GOT16 relocations */
8384 if (sec == NULL || sec->output_section == NULL)
8385 {
8386 unresolved_reloc = true;
8387 break;
8388 }
8389 BFD_ASSERT (strcmp (bfd_section_name (sec), ".got") == 0
8390 || strcmp (bfd_section_name (sec), ".cgot") == 0);
8391
8392 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
8393 break;
8394
8395 case R_PPC_PLTREL24:
8396 if (h != NULL && ifunc == NULL)
8397 {
8398 struct plt_entry *ent;
8399
8400 ent = find_plt_ent (&h->plt.plist, got2,
8401 bfd_link_pic (info) ? addend : 0);
8402 if (ent == NULL
8403 || htab->elf.splt == NULL)
8404 {
8405 /* We didn't make a PLT entry for this symbol. This
8406 happens when statically linking PIC code, or when
8407 using -Bsymbolic. */
8408 }
8409 else
8410 {
8411 /* Relocation is to the entry for this symbol in the
8412 procedure linkage table. */
8413 unresolved_reloc = false;
8414 if (htab->plt_type == PLT_NEW)
8415 relocation = (htab->glink->output_section->vma
8416 + htab->glink->output_offset
8417 + ent->glink_offset);
8418 else
8419 relocation = (htab->elf.splt->output_section->vma
8420 + htab->elf.splt->output_offset
8421 + ent->plt.offset);
8422 }
8423 }
8424
8425 /* R_PPC_PLTREL24 is rather special. If non-zero, the
8426 addend specifies the GOT pointer offset within .got2.
8427 Don't apply it to the relocation field. */
8428 addend = 0;
8429 break;
8430
8431 case R_PPC_PLTSEQ:
8432 case R_PPC_PLTCALL:
8433 case R_PPC_PLT16_LO:
8434 case R_PPC_PLT16_HI:
8435 case R_PPC_PLT16_HA:
8436 plt_list = NULL;
8437 if (h != NULL)
8438 plt_list = &h->plt.plist;
8439 else if (ifunc != NULL)
8440 plt_list = ifunc;
8441 else if (local_got_offsets != NULL)
8442 {
8443 struct plt_entry **local_plt;
8444 local_plt = (struct plt_entry **) (local_got_offsets
8445 + symtab_hdr->sh_info);
8446 plt_list = local_plt + r_symndx;
8447 }
8448 unresolved_reloc = true;
8449 if (plt_list != NULL)
8450 {
8451 struct plt_entry *ent;
8452
8453 ent = find_plt_ent (plt_list, got2,
8454 bfd_link_pic (info) ? addend : 0);
8455 if (ent != NULL && ent->plt.offset != (bfd_vma) -1)
8456 {
8457 asection *plt;
8458
8459 unresolved_reloc = false;
8460 plt = htab->elf.splt;
8461 if (use_local_plt (info, h))
8462 {
8463 if (ifunc != NULL)
8464 plt = htab->elf.iplt;
8465 else
8466 plt = htab->pltlocal;
8467 }
8468 relocation = (plt->output_section->vma
8469 + plt->output_offset
8470 + ent->plt.offset);
8471 if (bfd_link_pic (info))
8472 {
8473 bfd_vma got = 0;
8474
8475 if (ent->addend >= 32768)
8476 got = (ent->addend
8477 + ent->sec->output_section->vma
8478 + ent->sec->output_offset);
8479 else
8480 got = SYM_VAL (htab->elf.hgot);
8481 relocation -= got;
8482 }
8483 }
8484 }
8485 addend = 0;
8486 break;
8487
8488 /* Relocate against _SDA_BASE_. */
8489 case R_PPC_SDAREL16:
8490 {
8491 const char *name;
8492 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
8493
8494 if (sec == NULL
8495 || sec->output_section == NULL
8496 || !is_static_defined (sda))
8497 {
8498 unresolved_reloc = true;
8499 break;
8500 }
8501 addend -= SYM_VAL (sda);
8502
8503 name = bfd_section_name (sec->output_section);
8504 if (!(strcmp (name, ".sdata") == 0
8505 || strcmp (name, ".sbss") == 0))
8506 {
8507 _bfd_error_handler
8508 /* xgettext:c-format */
8509 (_("%pB: the target (%s) of a %s relocation is "
8510 "in the wrong output section (%s)"),
8511 input_bfd,
8512 sym_name,
8513 howto->name,
8514 name);
8515 }
8516 }
8517 break;
8518
8519 /* Relocate against _SDA2_BASE_. */
8520 case R_PPC_EMB_SDA2REL:
8521 {
8522 const char *name;
8523 struct elf_link_hash_entry *sda = htab->sdata[1].sym;
8524
8525 if (sec == NULL
8526 || sec->output_section == NULL
8527 || !is_static_defined (sda))
8528 {
8529 unresolved_reloc = true;
8530 break;
8531 }
8532 addend -= SYM_VAL (sda);
8533
8534 name = bfd_section_name (sec->output_section);
8535 if (!(strcmp (name, ".sdata2") == 0
8536 || strcmp (name, ".sbss2") == 0))
8537 {
8538 _bfd_error_handler
8539 /* xgettext:c-format */
8540 (_("%pB: the target (%s) of a %s relocation is "
8541 "in the wrong output section (%s)"),
8542 input_bfd,
8543 sym_name,
8544 howto->name,
8545 name);
8546 }
8547 }
8548 break;
8549
8550 case R_PPC_VLE_LO16A:
8551 relocation = relocation + addend;
8552 r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
8553 contents + rel->r_offset, relocation,
8554 split16a_type,
8555 htab->params->vle_reloc_fixup);
8556 goto report_reloc;
8557
8558 case R_PPC_VLE_LO16D:
8559 relocation = relocation + addend;
8560 r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
8561 contents + rel->r_offset, relocation,
8562 split16d_type,
8563 htab->params->vle_reloc_fixup);
8564 goto report_reloc;
8565
8566 case R_PPC_VLE_HI16A:
8567 relocation = (relocation + addend) >> 16;
8568 r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
8569 contents + rel->r_offset, relocation,
8570 split16a_type,
8571 htab->params->vle_reloc_fixup);
8572 goto report_reloc;
8573
8574 case R_PPC_VLE_HI16D:
8575 relocation = (relocation + addend) >> 16;
8576 r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
8577 contents + rel->r_offset, relocation,
8578 split16d_type,
8579 htab->params->vle_reloc_fixup);
8580 goto report_reloc;
8581
8582 case R_PPC_VLE_HA16A:
8583 relocation = (relocation + addend + 0x8000) >> 16;
8584 r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
8585 contents + rel->r_offset, relocation,
8586 split16a_type,
8587 htab->params->vle_reloc_fixup);
8588 goto report_reloc;
8589
8590 case R_PPC_VLE_HA16D:
8591 relocation = (relocation + addend + 0x8000) >> 16;
8592 r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
8593 contents + rel->r_offset, relocation,
8594 split16d_type,
8595 htab->params->vle_reloc_fixup);
8596 goto report_reloc;
8597
8598 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
8599 case R_PPC_EMB_SDA21:
8600 case R_PPC_VLE_SDA21:
8601 case R_PPC_EMB_RELSDA:
8602 case R_PPC_VLE_SDA21_LO:
8603 if (!offset_in_range (input_section, rel->r_offset, 4))
8604 {
8605 r = bfd_reloc_outofrange;
8606 goto report_reloc;
8607 }
8608 else
8609 {
8610 const char *name;
8611 int reg;
8612 unsigned int insn;
8613 struct elf_link_hash_entry *sda = NULL;
8614
8615 if (sec == NULL || sec->output_section == NULL)
8616 {
8617 unresolved_reloc = true;
8618 break;
8619 }
8620
8621 name = bfd_section_name (sec->output_section);
8622 if (strcmp (name, ".sdata") == 0
8623 || strcmp (name, ".sbss") == 0)
8624 {
8625 reg = 13;
8626 sda = htab->sdata[0].sym;
8627 }
8628 else if (strcmp (name, ".sdata2") == 0
8629 || strcmp (name, ".sbss2") == 0)
8630 {
8631 reg = 2;
8632 sda = htab->sdata[1].sym;
8633 }
8634 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
8635 || strcmp (name, ".PPC.EMB.sbss0") == 0)
8636 {
8637 reg = 0;
8638 }
8639 else
8640 {
8641 _bfd_error_handler
8642 /* xgettext:c-format */
8643 (_("%pB: the target (%s) of a %s relocation is "
8644 "in the wrong output section (%s)"),
8645 input_bfd,
8646 sym_name,
8647 howto->name,
8648 name);
8649
8650 bfd_set_error (bfd_error_bad_value);
8651 ret = false;
8652 goto copy_reloc;
8653 }
8654
8655 if (sda != NULL)
8656 {
8657 if (!is_static_defined (sda))
8658 {
8659 unresolved_reloc = true;
8660 break;
8661 }
8662 addend -= SYM_VAL (sda);
8663 }
8664
8665 if (r_type == R_PPC_EMB_RELSDA)
8666 break;
8667
8668 /* The PowerPC Embedded Application Binary Interface
8669 version 1.0 insanely chose to specify R_PPC_EMB_SDA21
8670 operating on a 24-bit field at r_offset. GNU as and
8671 GNU ld have always assumed R_PPC_EMB_SDA21 operates on
8672 a 32-bit bit insn at r_offset. Cope with object file
8673 producers that possibly comply with the EABI in
8674 generating an odd r_offset for big-endian objects. */
8675 if (r_type == R_PPC_EMB_SDA21)
8676 rel->r_offset &= ~1;
8677
8678 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
8679 if (reg == 0
8680 && (r_type == R_PPC_VLE_SDA21
8681 || r_type == R_PPC_VLE_SDA21_LO))
8682 {
8683 relocation = relocation + addend;
8684 addend = 0;
8685
8686 /* Force e_li insn, keeping RT from original insn. */
8687 insn &= 0x1f << 21;
8688 insn |= 28u << 26;
8689
8690 /* We have an li20 field, bits 17..20, 11..15, 21..31. */
8691 /* Top 4 bits of value to 17..20. */
8692 insn |= (relocation & 0xf0000) >> 5;
8693 /* Next 5 bits of the value to 11..15. */
8694 insn |= (relocation & 0xf800) << 5;
8695 /* And the final 11 bits of the value to bits 21 to 31. */
8696 insn |= relocation & 0x7ff;
8697
8698 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
8699
8700 r = bfd_reloc_ok;
8701 if (r_type == R_PPC_VLE_SDA21
8702 && ((relocation + 0x80000) & 0xffffffff) > 0x100000)
8703 r = bfd_reloc_overflow;
8704 goto report_reloc;
8705 }
8706 /* Fill in register field. */
8707 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
8708 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
8709 }
8710 break;
8711
8712 case R_PPC_VLE_SDAREL_LO16A:
8713 case R_PPC_VLE_SDAREL_LO16D:
8714 case R_PPC_VLE_SDAREL_HI16A:
8715 case R_PPC_VLE_SDAREL_HI16D:
8716 case R_PPC_VLE_SDAREL_HA16A:
8717 case R_PPC_VLE_SDAREL_HA16D:
8718 if (!offset_in_range (input_section, rel->r_offset, 4))
8719 r = bfd_reloc_outofrange;
8720 else
8721 {
8722 bfd_vma value;
8723 const char *name;
8724 struct elf_link_hash_entry *sda = NULL;
8725
8726 if (sec == NULL || sec->output_section == NULL)
8727 {
8728 unresolved_reloc = true;
8729 break;
8730 }
8731
8732 name = bfd_section_name (sec->output_section);
8733 if (strcmp (name, ".sdata") == 0
8734 || strcmp (name, ".sbss") == 0)
8735 sda = htab->sdata[0].sym;
8736 else if (strcmp (name, ".sdata2") == 0
8737 || strcmp (name, ".sbss2") == 0)
8738 sda = htab->sdata[1].sym;
8739 else
8740 {
8741 _bfd_error_handler
8742 /* xgettext:c-format */
8743 (_("%pB: the target (%s) of a %s relocation is "
8744 "in the wrong output section (%s)"),
8745 input_bfd,
8746 sym_name,
8747 howto->name,
8748 name);
8749
8750 bfd_set_error (bfd_error_bad_value);
8751 ret = false;
8752 goto copy_reloc;
8753 }
8754
8755 if (sda == NULL || !is_static_defined (sda))
8756 {
8757 unresolved_reloc = true;
8758 break;
8759 }
8760 value = relocation + addend - SYM_VAL (sda);
8761
8762 if (r_type == R_PPC_VLE_SDAREL_LO16A)
8763 r = ppc_elf_vle_split16 (input_bfd, input_section,
8764 rel->r_offset,
8765 contents + rel->r_offset, value,
8766 split16a_type,
8767 htab->params->vle_reloc_fixup);
8768 else if (r_type == R_PPC_VLE_SDAREL_LO16D)
8769 r = ppc_elf_vle_split16 (input_bfd, input_section,
8770 rel->r_offset,
8771 contents + rel->r_offset, value,
8772 split16d_type,
8773 htab->params->vle_reloc_fixup);
8774 else if (r_type == R_PPC_VLE_SDAREL_HI16A)
8775 {
8776 value = value >> 16;
8777 r = ppc_elf_vle_split16 (input_bfd, input_section,
8778 rel->r_offset,
8779 contents + rel->r_offset, value,
8780 split16a_type,
8781 htab->params->vle_reloc_fixup);
8782 }
8783 else if (r_type == R_PPC_VLE_SDAREL_HI16D)
8784 {
8785 value = value >> 16;
8786 r = ppc_elf_vle_split16 (input_bfd, input_section,
8787 rel->r_offset,
8788 contents + rel->r_offset, value,
8789 split16d_type,
8790 htab->params->vle_reloc_fixup);
8791 }
8792 else if (r_type == R_PPC_VLE_SDAREL_HA16A)
8793 {
8794 value = (value + 0x8000) >> 16;
8795 r = ppc_elf_vle_split16 (input_bfd, input_section,
8796 rel->r_offset,
8797 contents + rel->r_offset, value,
8798 split16a_type,
8799 htab->params->vle_reloc_fixup);
8800 }
8801 else if (r_type == R_PPC_VLE_SDAREL_HA16D)
8802 {
8803 value = (value + 0x8000) >> 16;
8804 r = ppc_elf_vle_split16 (input_bfd, input_section,
8805 rel->r_offset,
8806 contents + rel->r_offset, value,
8807 split16d_type,
8808 htab->params->vle_reloc_fixup);
8809 }
8810 else
8811 abort ();
8812 }
8813 goto report_reloc;
8814
8815 case R_PPC_VLE_ADDR20:
8816 if (!offset_in_range (input_section, rel->r_offset, 4))
8817 r = bfd_reloc_outofrange;
8818 else
8819 {
8820 ppc_elf_vle_split20 (output_bfd, contents + rel->r_offset,
8821 relocation);
8822 r = bfd_reloc_ok;
8823 }
8824 goto report_reloc;
8825
8826 /* Relocate against the beginning of the section. */
8827 case R_PPC_SECTOFF:
8828 case R_PPC_SECTOFF_LO:
8829 case R_PPC_SECTOFF_HI:
8830 case R_PPC_SECTOFF_HA:
8831 if (sec == NULL || sec->output_section == NULL)
8832 {
8833 unresolved_reloc = true;
8834 break;
8835 }
8836 addend -= sec->output_section->vma;
8837 break;
8838
8839 /* Negative relocations. */
8840 case R_PPC_EMB_NADDR32:
8841 case R_PPC_EMB_NADDR16:
8842 case R_PPC_EMB_NADDR16_LO:
8843 case R_PPC_EMB_NADDR16_HI:
8844 case R_PPC_EMB_NADDR16_HA:
8845 addend -= 2 * relocation;
8846 break;
8847
8848 case R_PPC_COPY:
8849 case R_PPC_GLOB_DAT:
8850 case R_PPC_JMP_SLOT:
8851 case R_PPC_RELATIVE:
8852 case R_PPC_IRELATIVE:
8853 case R_PPC_PLT32:
8854 case R_PPC_PLTREL32:
8855 case R_PPC_ADDR30:
8856 case R_PPC_EMB_RELSEC16:
8857 case R_PPC_EMB_RELST_LO:
8858 case R_PPC_EMB_RELST_HI:
8859 case R_PPC_EMB_RELST_HA:
8860 case R_PPC_EMB_BIT_FLD:
8861 /* xgettext:c-format */
8862 _bfd_error_handler (_("%pB: %s unsupported"),
8863 input_bfd, howto->name);
8864
8865 bfd_set_error (bfd_error_invalid_operation);
8866 ret = false;
8867 goto copy_reloc;
8868 }
8869
8870 switch (r_type)
8871 {
8872 default:
8873 break;
8874
8875 case R_PPC_TPREL16_HA:
8876 if (htab->do_tls_opt
8877 && relocation + addend + 0x8000 < 0x10000
8878 && offset_in_range (input_section, rel->r_offset & ~3, 4))
8879
8880 {
8881 bfd_byte *p = contents + (rel->r_offset & ~3);
8882 bfd_put_32 (input_bfd, NOP, p);
8883 }
8884 break;
8885
8886 case R_PPC_TPREL16_LO:
8887 if (htab->do_tls_opt
8888 && relocation + addend + 0x8000 < 0x10000
8889 && offset_in_range (input_section, rel->r_offset & ~3, 4))
8890 {
8891 bfd_byte *p = contents + (rel->r_offset & ~3);
8892 unsigned int insn = bfd_get_32 (input_bfd, p);
8893 insn &= ~(0x1f << 16);
8894 insn |= 2 << 16;
8895 bfd_put_32 (input_bfd, insn, p);
8896 }
8897 break;
8898 }
8899
8900 switch (r_type)
8901 {
8902 default:
8903 break;
8904
8905 case R_PPC_PLTCALL:
8906 if (unresolved_reloc)
8907 {
8908 if (offset_in_range (input_section, rel->r_offset, 4))
8909 {
8910 bfd_byte *p = contents + rel->r_offset;
8911 unsigned int insn = bfd_get_32 (input_bfd, p);
8912 insn &= 1;
8913 bfd_put_32 (input_bfd, B | insn, p);
8914 unresolved_reloc = save_unresolved_reloc;
8915 r_type = R_PPC_REL24;
8916 howto = ppc_elf_howto_table[r_type];
8917 }
8918 }
8919 else if (htab->plt_type != PLT_NEW)
8920 info->callbacks->einfo
8921 (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
8922 input_bfd, input_section, rel->r_offset,
8923 howto->name);
8924 break;
8925
8926 case R_PPC_PLTSEQ:
8927 case R_PPC_PLT16_HA:
8928 case R_PPC_PLT16_LO:
8929 if (unresolved_reloc)
8930 {
8931 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
8932 {
8933 bfd_byte *p = contents + (rel->r_offset & ~3);
8934 bfd_put_32 (input_bfd, NOP, p);
8935 unresolved_reloc = false;
8936 r_type = R_PPC_NONE;
8937 howto = ppc_elf_howto_table[r_type];
8938 }
8939 }
8940 else if (htab->plt_type != PLT_NEW)
8941 info->callbacks->einfo
8942 (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
8943 input_bfd, input_section, rel->r_offset,
8944 howto->name);
8945 break;
8946 }
8947
8948 /* Do any further special processing. */
8949 switch (r_type)
8950 {
8951 default:
8952 break;
8953
8954 case R_PPC_ADDR16_HA:
8955 case R_PPC_REL16_HA:
8956 case R_PPC_REL16DX_HA:
8957 case R_PPC_SECTOFF_HA:
8958 case R_PPC_TPREL16_HA:
8959 case R_PPC_DTPREL16_HA:
8960 case R_PPC_EMB_NADDR16_HA:
8961 case R_PPC_EMB_RELST_HA:
8962 /* It's just possible that this symbol is a weak symbol
8963 that's not actually defined anywhere. In that case,
8964 'sec' would be NULL, and we should leave the symbol
8965 alone (it will be set to zero elsewhere in the link). */
8966 if (sec == NULL)
8967 break;
8968 /* Fall through. */
8969
8970 case R_PPC_PLT16_HA:
8971 case R_PPC_GOT16_HA:
8972 case R_PPC_GOT_TLSGD16_HA:
8973 case R_PPC_GOT_TLSLD16_HA:
8974 case R_PPC_GOT_TPREL16_HA:
8975 case R_PPC_GOT_DTPREL16_HA:
8976 /* Add 0x10000 if sign bit in 0:15 is set.
8977 Bits 0:15 are not used. */
8978 addend += 0x8000;
8979 break;
8980
8981 case R_PPC_ADDR16:
8982 case R_PPC_ADDR16_LO:
8983 case R_PPC_GOT16:
8984 case R_PPC_GOT16_LO:
8985 case R_PPC_SDAREL16:
8986 case R_PPC_SECTOFF:
8987 case R_PPC_SECTOFF_LO:
8988 case R_PPC_DTPREL16:
8989 case R_PPC_DTPREL16_LO:
8990 case R_PPC_TPREL16:
8991 case R_PPC_TPREL16_LO:
8992 case R_PPC_GOT_TLSGD16:
8993 case R_PPC_GOT_TLSGD16_LO:
8994 case R_PPC_GOT_TLSLD16:
8995 case R_PPC_GOT_TLSLD16_LO:
8996 case R_PPC_GOT_DTPREL16:
8997 case R_PPC_GOT_DTPREL16_LO:
8998 case R_PPC_GOT_TPREL16:
8999 case R_PPC_GOT_TPREL16_LO:
9000 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
9001 {
9002 /* The 32-bit ABI lacks proper relocations to deal with
9003 certain 64-bit instructions. Prevent damage to bits
9004 that make up part of the insn opcode. */
9005 unsigned int insn, mask, lobit;
9006
9007 insn = bfd_get_32 (input_bfd,
9008 contents + rel->r_offset - d_offset);
9009 mask = 0;
9010 if (is_insn_ds_form (insn))
9011 mask = 3;
9012 else if (is_insn_dq_form (insn))
9013 mask = 15;
9014 else
9015 break;
9016 relocation += addend;
9017 addend = insn & mask;
9018 lobit = mask & relocation;
9019 if (lobit != 0)
9020 {
9021 relocation ^= lobit;
9022 info->callbacks->einfo
9023 /* xgettext:c-format */
9024 (_("%H: error: %s against `%s' not a multiple of %u\n"),
9025 input_bfd, input_section, rel->r_offset,
9026 howto->name, sym_name, mask + 1);
9027 bfd_set_error (bfd_error_bad_value);
9028 ret = false;
9029 }
9030 }
9031 break;
9032 }
9033
9034 #ifdef DEBUG
9035 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
9036 "offset = %ld, addend = %ld\n",
9037 howto->name,
9038 (int) r_type,
9039 sym_name,
9040 r_symndx,
9041 (long) rel->r_offset,
9042 (long) addend);
9043 #endif
9044
9045 if (unresolved_reloc
9046 && !((input_section->flags & SEC_DEBUGGING) != 0
9047 && h->def_dynamic)
9048 && _bfd_elf_section_offset (output_bfd, info, input_section,
9049 rel->r_offset) != (bfd_vma) -1)
9050 {
9051 info->callbacks->einfo
9052 /* xgettext:c-format */
9053 (_("%H: unresolvable %s relocation against symbol `%s'\n"),
9054 input_bfd, input_section, rel->r_offset,
9055 howto->name,
9056 sym_name);
9057 ret = false;
9058 }
9059
9060 /* 16-bit fields in insns mostly have signed values, but a
9061 few insns have 16-bit unsigned values. Really, we should
9062 have different reloc types. */
9063 if (howto->complain_on_overflow != complain_overflow_dont
9064 && howto->dst_mask == 0xffff
9065 && (input_section->flags & SEC_CODE) != 0
9066 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9067 {
9068 enum complain_overflow complain = complain_overflow_signed;
9069
9070 if ((elf_section_flags (input_section) & SHF_PPC_VLE) == 0)
9071 {
9072 unsigned int insn;
9073
9074 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
9075 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
9076 complain = complain_overflow_bitfield;
9077 else if ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
9078 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
9079 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
9080 complain = complain_overflow_unsigned;
9081 }
9082 if (howto->complain_on_overflow != complain)
9083 {
9084 alt_howto = *howto;
9085 alt_howto.complain_on_overflow = complain;
9086 howto = &alt_howto;
9087 }
9088 }
9089
9090 if (r_type == R_PPC_REL16DX_HA)
9091 {
9092 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
9093 if (offset_in_range (input_section, rel->r_offset, 4))
9094 r = bfd_reloc_outofrange;
9095 else
9096 {
9097 unsigned int insn;
9098
9099 relocation += addend;
9100 relocation -= (rel->r_offset
9101 + input_section->output_offset
9102 + input_section->output_section->vma);
9103 relocation >>= 16;
9104 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
9105 insn &= ~0x1fffc1;
9106 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
9107 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
9108 r = bfd_reloc_ok;
9109 }
9110 }
9111 else
9112 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
9113 rel->r_offset, relocation, addend);
9114
9115 report_reloc:
9116 if (r != bfd_reloc_ok)
9117 {
9118 if (r == bfd_reloc_overflow)
9119 {
9120 /* On code like "if (foo) foo();" don't report overflow
9121 on a branch to zero when foo is undefined. */
9122 if (!warned
9123 && !(h != NULL
9124 && (h->root.type == bfd_link_hash_undefweak
9125 || h->root.type == bfd_link_hash_undefined)
9126 && is_branch_reloc (r_type)))
9127 info->callbacks->reloc_overflow
9128 (info, (h ? &h->root : NULL), sym_name, howto->name,
9129 rel->r_addend, input_bfd, input_section, rel->r_offset);
9130 }
9131 else
9132 {
9133 info->callbacks->einfo
9134 /* xgettext:c-format */
9135 (_("%H: %s reloc against `%s': error %d\n"),
9136 input_bfd, input_section, rel->r_offset,
9137 howto->name, sym_name, (int) r);
9138 ret = false;
9139 }
9140 }
9141 copy_reloc:
9142 if (wrel != rel)
9143 *wrel = *rel;
9144 }
9145
9146 if (wrel != rel)
9147 {
9148 Elf_Internal_Shdr *rel_hdr;
9149 size_t deleted = rel - wrel;
9150
9151 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
9152 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
9153 if (rel_hdr->sh_size == 0)
9154 {
9155 /* It is too late to remove an empty reloc section. Leave
9156 one NONE reloc.
9157 ??? What is wrong with an empty section??? */
9158 rel_hdr->sh_size = rel_hdr->sh_entsize;
9159 deleted -= 1;
9160 wrel++;
9161 }
9162 relend = wrel;
9163 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
9164 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
9165 input_section->reloc_count -= deleted;
9166 }
9167
9168 #ifdef DEBUG
9169 fprintf (stderr, "\n");
9170 #endif
9171
9172 if (input_section->sec_info_type == SEC_INFO_TYPE_TARGET
9173 && input_section->size != input_section->rawsize
9174 && (strcmp (input_section->output_section->name, ".init") == 0
9175 || strcmp (input_section->output_section->name, ".fini") == 0))
9176 {
9177 /* Branch around the trampolines. */
9178 unsigned int insn = B + input_section->size - input_section->rawsize;
9179 bfd_put_32 (input_bfd, insn, contents + input_section->rawsize);
9180 }
9181
9182 if (htab->params->ppc476_workaround
9183 && input_section->sec_info_type == SEC_INFO_TYPE_TARGET
9184 && (!bfd_link_relocatable (info)
9185 || (input_section->output_section->alignment_power
9186 >= htab->params->pagesize_p2)))
9187 {
9188 bfd_vma start_addr, end_addr, addr;
9189 bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
9190
9191 if (relax_info->workaround_size != 0)
9192 {
9193 bfd_byte *p;
9194 unsigned int n;
9195 bfd_byte fill[4];
9196
9197 bfd_put_32 (input_bfd, BA, fill);
9198 p = contents + input_section->size - relax_info->workaround_size;
9199 n = relax_info->workaround_size >> 2;
9200 while (n--)
9201 {
9202 memcpy (p, fill, 4);
9203 p += 4;
9204 }
9205 }
9206
9207 /* The idea is: Replace the last instruction on a page with a
9208 branch to a patch area. Put the insn there followed by a
9209 branch back to the next page. Complicated a little by
9210 needing to handle moved conditional branches, and by not
9211 wanting to touch data-in-text. */
9212
9213 start_addr = (input_section->output_section->vma
9214 + input_section->output_offset);
9215 end_addr = (start_addr + input_section->size
9216 - relax_info->workaround_size);
9217 for (addr = ((start_addr & -pagesize) + pagesize - 4);
9218 addr < end_addr;
9219 addr += pagesize)
9220 {
9221 bfd_vma offset = addr - start_addr;
9222 Elf_Internal_Rela *lo, *hi;
9223 bool is_data;
9224 bfd_vma patch_off, patch_addr;
9225 unsigned int insn;
9226
9227 /* Do we have a data reloc at this offset? If so, leave
9228 the word alone. */
9229 is_data = false;
9230 lo = relocs;
9231 hi = relend;
9232 rel = NULL;
9233 while (lo < hi)
9234 {
9235 rel = lo + (hi - lo) / 2;
9236 if (rel->r_offset < offset)
9237 lo = rel + 1;
9238 else if (rel->r_offset > offset + 3)
9239 hi = rel;
9240 else
9241 {
9242 switch (ELF32_R_TYPE (rel->r_info))
9243 {
9244 case R_PPC_ADDR32:
9245 case R_PPC_UADDR32:
9246 case R_PPC_REL32:
9247 case R_PPC_ADDR30:
9248 is_data = true;
9249 break;
9250 default:
9251 break;
9252 }
9253 break;
9254 }
9255 }
9256 if (is_data)
9257 continue;
9258
9259 /* Some instructions can be left alone too. Unconditional
9260 branches, except for bcctr with BO=0x14 (bctr, bctrl),
9261 avoid the icache failure.
9262
9263 The problem occurs due to prefetch across a page boundary
9264 where stale instructions can be fetched from the next
9265 page, and the mechanism for flushing these bad
9266 instructions fails under certain circumstances. The
9267 unconditional branches:
9268 1) Branch: b, bl, ba, bla,
9269 2) Branch Conditional: bc, bca, bcl, bcla,
9270 3) Branch Conditional to Link Register: bclr, bclrl,
9271 where (2) and (3) have BO=0x14 making them unconditional,
9272 prevent the bad prefetch because the prefetch itself is
9273 affected by these instructions. This happens even if the
9274 instruction is not executed.
9275
9276 A bctr example:
9277 .
9278 . lis 9,new_page@ha
9279 . addi 9,9,new_page@l
9280 . mtctr 9
9281 . bctr
9282 . nop
9283 . nop
9284 . new_page:
9285 .
9286 The bctr is not predicted taken due to ctr not being
9287 ready, so prefetch continues on past the bctr into the
9288 new page which might have stale instructions. If they
9289 fail to be flushed, then they will be executed after the
9290 bctr executes. Either of the following modifications
9291 prevent the bad prefetch from happening in the first
9292 place:
9293 .
9294 . lis 9,new_page@ha lis 9,new_page@ha
9295 . addi 9,9,new_page@l addi 9,9,new_page@l
9296 . mtctr 9 mtctr 9
9297 . bctr bctr
9298 . nop b somewhere_else
9299 . b somewhere_else nop
9300 . new_page: new_page:
9301 . */
9302 insn = bfd_get_32 (input_bfd, contents + offset);
9303 if ((insn & (0x3fu << 26)) == (18u << 26) /* b,bl,ba,bla */
9304 || ((insn & (0x3fu << 26)) == (16u << 26) /* bc,bcl,bca,bcla*/
9305 && (insn & (0x14 << 21)) == (0x14 << 21)) /* with BO=0x14 */
9306 || ((insn & (0x3fu << 26)) == (19u << 26)
9307 && (insn & (0x3ff << 1)) == (16u << 1) /* bclr,bclrl */
9308 && (insn & (0x14 << 21)) == (0x14 << 21)))/* with BO=0x14 */
9309 continue;
9310
9311 patch_addr = (start_addr + input_section->size
9312 - relax_info->workaround_size);
9313 patch_addr = (patch_addr + 15) & -16;
9314 patch_off = patch_addr - start_addr;
9315 bfd_put_32 (input_bfd, B + patch_off - offset, contents + offset);
9316
9317 if (rel != NULL
9318 && rel->r_offset >= offset
9319 && rel->r_offset < offset + 4)
9320 {
9321 asection *sreloc;
9322
9323 /* If the insn we are patching had a reloc, adjust the
9324 reloc r_offset so that the reloc applies to the moved
9325 location. This matters for -r and --emit-relocs. */
9326 if (rel + 1 != relend)
9327 {
9328 Elf_Internal_Rela tmp = *rel;
9329
9330 /* Keep the relocs sorted by r_offset. */
9331 memmove (rel, rel + 1, (relend - (rel + 1)) * sizeof (*rel));
9332 relend[-1] = tmp;
9333 }
9334 relend[-1].r_offset += patch_off - offset;
9335
9336 /* Adjust REL16 addends too. */
9337 switch (ELF32_R_TYPE (relend[-1].r_info))
9338 {
9339 case R_PPC_REL16:
9340 case R_PPC_REL16_LO:
9341 case R_PPC_REL16_HI:
9342 case R_PPC_REL16_HA:
9343 relend[-1].r_addend += patch_off - offset;
9344 break;
9345 default:
9346 break;
9347 }
9348
9349 /* If we are building a PIE or shared library with
9350 non-PIC objects, perhaps we had a dynamic reloc too?
9351 If so, the dynamic reloc must move with the insn. */
9352 sreloc = elf_section_data (input_section)->sreloc;
9353 if (sreloc != NULL)
9354 {
9355 Elf32_External_Rela *slo, *shi, *srelend;
9356 bfd_vma soffset;
9357
9358 slo = (Elf32_External_Rela *) sreloc->contents;
9359 shi = srelend = slo + sreloc->reloc_count;
9360 soffset = (offset + input_section->output_section->vma
9361 + input_section->output_offset);
9362 while (slo < shi)
9363 {
9364 Elf32_External_Rela *srel = slo + (shi - slo) / 2;
9365 bfd_elf32_swap_reloca_in (output_bfd, (bfd_byte *) srel,
9366 &outrel);
9367 if (outrel.r_offset < soffset)
9368 slo = srel + 1;
9369 else if (outrel.r_offset > soffset + 3)
9370 shi = srel;
9371 else
9372 {
9373 if (srel + 1 != srelend)
9374 {
9375 memmove (srel, srel + 1,
9376 (srelend - (srel + 1)) * sizeof (*srel));
9377 srel = srelend - 1;
9378 }
9379 outrel.r_offset += patch_off - offset;
9380 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
9381 (bfd_byte *) srel);
9382 break;
9383 }
9384 }
9385 }
9386 }
9387 else
9388 rel = NULL;
9389
9390 if ((insn & (0x3fu << 26)) == (16u << 26) /* bc */
9391 && (insn & 2) == 0 /* relative */)
9392 {
9393 bfd_vma delta = ((insn & 0xfffc) ^ 0x8000) - 0x8000;
9394
9395 delta += offset - patch_off;
9396 if (bfd_link_relocatable (info) && rel != NULL)
9397 delta = 0;
9398 if (!bfd_link_relocatable (info) && rel != NULL)
9399 {
9400 enum elf_ppc_reloc_type r_type;
9401
9402 r_type = ELF32_R_TYPE (relend[-1].r_info);
9403 if (r_type == R_PPC_REL14_BRTAKEN)
9404 insn |= BRANCH_PREDICT_BIT;
9405 else if (r_type == R_PPC_REL14_BRNTAKEN)
9406 insn &= ~BRANCH_PREDICT_BIT;
9407 else
9408 BFD_ASSERT (r_type == R_PPC_REL14);
9409
9410 if ((r_type == R_PPC_REL14_BRTAKEN
9411 || r_type == R_PPC_REL14_BRNTAKEN)
9412 && delta + 0x8000 < 0x10000
9413 && (bfd_signed_vma) delta < 0)
9414 insn ^= BRANCH_PREDICT_BIT;
9415 }
9416 if (delta + 0x8000 < 0x10000)
9417 {
9418 bfd_put_32 (input_bfd,
9419 (insn & ~0xfffc) | (delta & 0xfffc),
9420 contents + patch_off);
9421 patch_off += 4;
9422 bfd_put_32 (input_bfd,
9423 B | ((offset + 4 - patch_off) & 0x3fffffc),
9424 contents + patch_off);
9425 patch_off += 4;
9426 }
9427 else
9428 {
9429 if (rel != NULL)
9430 {
9431 unsigned int r_sym = ELF32_R_SYM (relend[-1].r_info);
9432
9433 relend[-1].r_offset += 8;
9434 relend[-1].r_info = ELF32_R_INFO (r_sym, R_PPC_REL24);
9435 }
9436 bfd_put_32 (input_bfd,
9437 (insn & ~0xfffc) | 8,
9438 contents + patch_off);
9439 patch_off += 4;
9440 bfd_put_32 (input_bfd,
9441 B | ((offset + 4 - patch_off) & 0x3fffffc),
9442 contents + patch_off);
9443 patch_off += 4;
9444 bfd_put_32 (input_bfd,
9445 B | ((delta - 8) & 0x3fffffc),
9446 contents + patch_off);
9447 patch_off += 4;
9448 }
9449 }
9450 else
9451 {
9452 bfd_put_32 (input_bfd, insn, contents + patch_off);
9453 patch_off += 4;
9454 bfd_put_32 (input_bfd,
9455 B | ((offset + 4 - patch_off) & 0x3fffffc),
9456 contents + patch_off);
9457 patch_off += 4;
9458 }
9459 BFD_ASSERT (patch_off <= input_section->size);
9460 relax_info->workaround_size = input_section->size - patch_off;
9461 }
9462 }
9463
9464 return ret;
9465 }
9466
9467 /* Write out the PLT relocs and entries for H. */
9469
9470 static bool
9471 write_global_sym_plt (struct elf_link_hash_entry *h, void *inf)
9472 {
9473 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9474 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
9475 struct plt_entry *ent;
9476 bool doneone;
9477
9478 doneone = false;
9479 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
9480 if (ent->plt.offset != (bfd_vma) -1)
9481 {
9482 bool dyn = !use_local_plt (info, h);
9483
9484 if (!doneone)
9485 {
9486 Elf_Internal_Rela rela;
9487 bfd_byte *loc;
9488 bfd_vma reloc_index;
9489 asection *plt = htab->elf.splt;
9490 asection *relplt = htab->elf.srelplt;
9491
9492 if (htab->plt_type == PLT_NEW || !dyn)
9493 reloc_index = ent->plt.offset / 4;
9494 else
9495 {
9496 reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
9497 / htab->plt_slot_size);
9498 if (reloc_index > PLT_NUM_SINGLE_ENTRIES
9499 && htab->plt_type == PLT_OLD)
9500 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
9501 }
9502
9503 /* This symbol has an entry in the procedure linkage table.
9504 Set it up. */
9505 if (htab->plt_type == PLT_VXWORKS && dyn)
9506 {
9507 bfd_vma got_offset;
9508 const bfd_vma *plt_entry;
9509
9510 /* The first three entries in .got.plt are reserved. */
9511 got_offset = (reloc_index + 3) * 4;
9512
9513 /* Use the right PLT. */
9514 plt_entry = bfd_link_pic (info) ? ppc_elf_vxworks_pic_plt_entry
9515 : ppc_elf_vxworks_plt_entry;
9516
9517 /* Fill in the .plt on VxWorks. */
9518 if (bfd_link_pic (info))
9519 {
9520 bfd_put_32 (info->output_bfd,
9521 plt_entry[0] | PPC_HA (got_offset),
9522 plt->contents + ent->plt.offset + 0);
9523 bfd_put_32 (info->output_bfd,
9524 plt_entry[1] | PPC_LO (got_offset),
9525 plt->contents + ent->plt.offset + 4);
9526 }
9527 else
9528 {
9529 bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
9530
9531 bfd_put_32 (info->output_bfd,
9532 plt_entry[0] | PPC_HA (got_loc),
9533 plt->contents + ent->plt.offset + 0);
9534 bfd_put_32 (info->output_bfd,
9535 plt_entry[1] | PPC_LO (got_loc),
9536 plt->contents + ent->plt.offset + 4);
9537 }
9538
9539 bfd_put_32 (info->output_bfd, plt_entry[2],
9540 plt->contents + ent->plt.offset + 8);
9541 bfd_put_32 (info->output_bfd, plt_entry[3],
9542 plt->contents + ent->plt.offset + 12);
9543
9544 /* This instruction is an immediate load. The value loaded is
9545 the byte offset of the R_PPC_JMP_SLOT relocation from the
9546 start of the .rela.plt section. The value is stored in the
9547 low-order 16 bits of the load instruction. */
9548 /* NOTE: It appears that this is now an index rather than a
9549 prescaled offset. */
9550 bfd_put_32 (info->output_bfd,
9551 plt_entry[4] | reloc_index,
9552 plt->contents + ent->plt.offset + 16);
9553 /* This instruction is a PC-relative branch whose target is
9554 the start of the PLT section. The address of this branch
9555 instruction is 20 bytes beyond the start of this PLT entry.
9556 The address is encoded in bits 6-29, inclusive. The value
9557 stored is right-shifted by two bits, permitting a 26-bit
9558 offset. */
9559 bfd_put_32 (info->output_bfd,
9560 (plt_entry[5]
9561 | (-(ent->plt.offset + 20) & 0x03fffffc)),
9562 plt->contents + ent->plt.offset + 20);
9563 bfd_put_32 (info->output_bfd, plt_entry[6],
9564 plt->contents + ent->plt.offset + 24);
9565 bfd_put_32 (info->output_bfd, plt_entry[7],
9566 plt->contents + ent->plt.offset + 28);
9567
9568 /* Fill in the GOT entry corresponding to this PLT slot with
9569 the address immediately after the "bctr" instruction
9570 in this PLT entry. */
9571 bfd_put_32 (info->output_bfd, (plt->output_section->vma
9572 + plt->output_offset
9573 + ent->plt.offset + 16),
9574 htab->elf.sgotplt->contents + got_offset);
9575
9576 if (!bfd_link_pic (info))
9577 {
9578 /* Fill in a couple of entries in .rela.plt.unloaded. */
9579 loc = htab->srelplt2->contents
9580 + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
9581 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
9582 * sizeof (Elf32_External_Rela));
9583
9584 /* Provide the @ha relocation for the first instruction. */
9585 rela.r_offset = (plt->output_section->vma
9586 + plt->output_offset
9587 + ent->plt.offset + 2);
9588 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
9589 R_PPC_ADDR16_HA);
9590 rela.r_addend = got_offset;
9591 bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
9592 loc += sizeof (Elf32_External_Rela);
9593
9594 /* Provide the @l relocation for the second instruction. */
9595 rela.r_offset = (plt->output_section->vma
9596 + plt->output_offset
9597 + ent->plt.offset + 6);
9598 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
9599 R_PPC_ADDR16_LO);
9600 rela.r_addend = got_offset;
9601 bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
9602 loc += sizeof (Elf32_External_Rela);
9603
9604 /* Provide a relocation for the GOT entry corresponding to this
9605 PLT slot. Point it at the middle of the .plt entry. */
9606 rela.r_offset = (htab->elf.sgotplt->output_section->vma
9607 + htab->elf.sgotplt->output_offset
9608 + got_offset);
9609 rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
9610 R_PPC_ADDR32);
9611 rela.r_addend = ent->plt.offset + 16;
9612 bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
9613 }
9614
9615 /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
9616 In particular, the offset for the relocation is not the
9617 address of the PLT entry for this function, as specified
9618 by the ABI. Instead, the offset is set to the address of
9619 the GOT slot for this function. See EABI 4.4.4.1. */
9620 rela.r_offset = (htab->elf.sgotplt->output_section->vma
9621 + htab->elf.sgotplt->output_offset
9622 + got_offset);
9623 rela.r_addend = 0;
9624 }
9625 else
9626 {
9627 rela.r_addend = 0;
9628 if (!dyn)
9629 {
9630 if (h->type == STT_GNU_IFUNC)
9631 {
9632 plt = htab->elf.iplt;
9633 relplt = htab->elf.irelplt;
9634 }
9635 else
9636 {
9637 plt = htab->pltlocal;
9638 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9639 }
9640 if (h->def_regular
9641 && (h->root.type == bfd_link_hash_defined
9642 || h->root.type == bfd_link_hash_defweak))
9643 rela.r_addend = SYM_VAL (h);
9644 }
9645
9646 if (relplt == NULL)
9647 {
9648 loc = plt->contents + ent->plt.offset;
9649 bfd_put_32 (info->output_bfd, rela.r_addend, loc);
9650 }
9651 else
9652 {
9653 rela.r_offset = (plt->output_section->vma
9654 + plt->output_offset
9655 + ent->plt.offset);
9656
9657 if (htab->plt_type == PLT_OLD || !dyn)
9658 {
9659 /* We don't need to fill in the .plt. The ppc dynamic
9660 linker will fill it in. */
9661 }
9662 else
9663 {
9664 bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
9665 + htab->glink->output_section->vma
9666 + htab->glink->output_offset);
9667 bfd_put_32 (info->output_bfd, val,
9668 plt->contents + ent->plt.offset);
9669 }
9670 }
9671 }
9672
9673 if (relplt != NULL)
9674 {
9675 /* Fill in the entry in the .rela.plt section. */
9676 if (!dyn)
9677 {
9678 if (h->type == STT_GNU_IFUNC)
9679 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
9680 else
9681 rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
9682 loc = relplt->contents + (relplt->reloc_count++
9683 * sizeof (Elf32_External_Rela));
9684 htab->local_ifunc_resolver = 1;
9685 }
9686 else
9687 {
9688 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
9689 loc = relplt->contents + (reloc_index
9690 * sizeof (Elf32_External_Rela));
9691 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
9692 htab->maybe_local_ifunc_resolver = 1;
9693 }
9694 bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
9695 }
9696 doneone = true;
9697 }
9698
9699 if (htab->plt_type == PLT_NEW || !dyn)
9700 {
9701 unsigned char *p;
9702 asection *plt = htab->elf.splt;
9703
9704 if (!dyn)
9705 {
9706 if (h->type == STT_GNU_IFUNC)
9707 plt = htab->elf.iplt;
9708 else
9709 break;
9710 }
9711
9712 p = (unsigned char *) htab->glink->contents + ent->glink_offset;
9713 write_glink_stub (h, ent, plt, p, info);
9714
9715 if (!bfd_link_pic (info))
9716 /* We only need one non-PIC glink stub. */
9717 break;
9718 }
9719 else
9720 break;
9721 }
9722 return true;
9723 }
9724
9725 /* Finish up PLT handling. */
9726
9727 bool
9728 ppc_finish_symbols (struct bfd_link_info *info)
9729 {
9730 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
9731 bfd *ibfd;
9732
9733 if (!htab)
9734 return true;
9735
9736 elf_link_hash_traverse (&htab->elf, write_global_sym_plt, info);
9737
9738 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9739 {
9740 bfd_vma *local_got, *end_local_got;
9741 struct plt_entry **local_plt, **lplt, **end_local_plt;
9742 Elf_Internal_Shdr *symtab_hdr;
9743 bfd_size_type locsymcount;
9744 Elf_Internal_Sym *local_syms = NULL;
9745 struct plt_entry *ent;
9746
9747 if (!is_ppc_elf (ibfd))
9748 continue;
9749
9750 local_got = elf_local_got_offsets (ibfd);
9751 if (!local_got)
9752 continue;
9753
9754 symtab_hdr = &elf_symtab_hdr (ibfd);
9755 locsymcount = symtab_hdr->sh_info;
9756 end_local_got = local_got + locsymcount;
9757 local_plt = (struct plt_entry **) end_local_got;
9758 end_local_plt = local_plt + locsymcount;
9759 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
9760 for (ent = *lplt; ent != NULL; ent = ent->next)
9761 {
9762 if (ent->plt.offset != (bfd_vma) -1)
9763 {
9764 Elf_Internal_Sym *sym;
9765 asection *sym_sec;
9766 asection *plt, *relplt;
9767 bfd_byte *loc;
9768 bfd_vma val;
9769 Elf_Internal_Rela rela;
9770 unsigned char *p;
9771
9772 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
9773 lplt - local_plt, ibfd))
9774 {
9775 if (symtab_hdr->contents != (unsigned char *) local_syms)
9776 free (local_syms);
9777 return false;
9778 }
9779
9780 val = sym->st_value;
9781 if (sym_sec != NULL && sym_sec->output_section != NULL)
9782 val += sym_sec->output_offset + sym_sec->output_section->vma;
9783
9784 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9785 {
9786 htab->local_ifunc_resolver = 1;
9787 plt = htab->elf.iplt;
9788 relplt = htab->elf.irelplt;
9789 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
9790 }
9791 else
9792 {
9793 plt = htab->pltlocal;
9794 if (bfd_link_pic (info))
9795 {
9796 relplt = htab->relpltlocal;
9797 rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
9798 }
9799 else
9800 {
9801 loc = plt->contents + ent->plt.offset;
9802 bfd_put_32 (info->output_bfd, val, loc);
9803 continue;
9804 }
9805 }
9806
9807 rela.r_offset = (ent->plt.offset
9808 + plt->output_offset
9809 + plt->output_section->vma);
9810 rela.r_addend = val;
9811 loc = relplt->contents + (relplt->reloc_count++
9812 * sizeof (Elf32_External_Rela));
9813 bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
9814
9815 p = (unsigned char *) htab->glink->contents + ent->glink_offset;
9816 write_glink_stub (NULL, ent, htab->elf.iplt, p, info);
9817 }
9818 }
9819
9820 if (local_syms != NULL
9821 && symtab_hdr->contents != (unsigned char *) local_syms)
9822 {
9823 if (!info->keep_memory)
9824 free (local_syms);
9825 else
9826 symtab_hdr->contents = (unsigned char *) local_syms;
9827 }
9828 }
9829 return true;
9830 }
9831
9832 /* Finish up dynamic symbol handling. We set the contents of various
9833 dynamic sections here. */
9834
9835 static bool
9836 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
9837 struct bfd_link_info *info,
9838 struct elf_link_hash_entry *h,
9839 Elf_Internal_Sym *sym)
9840 {
9841 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
9842 struct plt_entry *ent;
9843
9844 #ifdef DEBUG
9845 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
9846 h->root.root.string);
9847 #endif
9848
9849 if (!h->def_regular
9850 || (h->type == STT_GNU_IFUNC && !bfd_link_pic (info)))
9851 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
9852 if (ent->plt.offset != (bfd_vma) -1)
9853 {
9854 if (!h->def_regular)
9855 {
9856 /* Mark the symbol as undefined, rather than as
9857 defined in the .plt section. Leave the value if
9858 there were any relocations where pointer equality
9859 matters (this is a clue for the dynamic linker, to
9860 make function pointer comparisons work between an
9861 application and shared library), otherwise set it
9862 to zero. */
9863 sym->st_shndx = SHN_UNDEF;
9864 if (!h->pointer_equality_needed)
9865 sym->st_value = 0;
9866 else if (!h->ref_regular_nonweak)
9867 {
9868 /* This breaks function pointer comparisons, but
9869 that is better than breaking tests for a NULL
9870 function pointer. */
9871 sym->st_value = 0;
9872 }
9873 }
9874 else
9875 {
9876 /* Set the value of ifunc symbols in a non-pie
9877 executable to the glink entry. This is to avoid
9878 text relocations. We can't do this for ifunc in
9879 allocate_dynrelocs, as we do for normal dynamic
9880 function symbols with plt entries, because we need
9881 to keep the original value around for the ifunc
9882 relocation. */
9883 sym->st_shndx
9884 = (_bfd_elf_section_from_bfd_section
9885 (info->output_bfd, htab->glink->output_section));
9886 sym->st_value = (ent->glink_offset
9887 + htab->glink->output_offset
9888 + htab->glink->output_section->vma);
9889 }
9890 break;
9891 }
9892
9893 if (h->needs_copy)
9894 {
9895 asection *s;
9896 Elf_Internal_Rela rela;
9897 bfd_byte *loc;
9898
9899 /* This symbols needs a copy reloc. Set it up. */
9900
9901 #ifdef DEBUG
9902 fprintf (stderr, ", copy");
9903 #endif
9904
9905 BFD_ASSERT (h->dynindx != -1);
9906
9907 if (ppc_elf_hash_entry (h)->has_sda_refs)
9908 s = htab->relsbss;
9909 else if (h->root.u.def.section == htab->elf.sdynrelro)
9910 s = htab->elf.sreldynrelro;
9911 else
9912 s = htab->elf.srelbss;
9913 BFD_ASSERT (s != NULL);
9914
9915 rela.r_offset = SYM_VAL (h);
9916 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
9917 rela.r_addend = 0;
9918 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
9919 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9920 }
9921
9922 #ifdef DEBUG
9923 fprintf (stderr, "\n");
9924 #endif
9925
9926 return true;
9927 }
9928
9929 static enum elf_reloc_type_class
9931 ppc_elf_reloc_type_class (const struct bfd_link_info *info,
9932 const asection *rel_sec,
9933 const Elf_Internal_Rela *rela)
9934 {
9935 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
9936
9937 if (rel_sec == htab->elf.irelplt)
9938 return reloc_class_ifunc;
9939
9940 switch (ELF32_R_TYPE (rela->r_info))
9941 {
9942 case R_PPC_RELATIVE:
9943 return reloc_class_relative;
9944 case R_PPC_JMP_SLOT:
9945 return reloc_class_plt;
9946 case R_PPC_COPY:
9947 return reloc_class_copy;
9948 default:
9949 return reloc_class_normal;
9950 }
9951 }
9952
9953 /* Finish up the dynamic sections. */
9955
9956 static bool
9957 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
9958 struct bfd_link_info *info)
9959 {
9960 asection *sdyn;
9961 struct ppc_elf_link_hash_table *htab;
9962 bfd_vma got;
9963 bfd *dynobj;
9964 bool ret = true;
9965
9966 #ifdef DEBUG
9967 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
9968 #endif
9969
9970 htab = ppc_elf_hash_table (info);
9971 dynobj = htab->elf.dynobj;
9972 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
9973
9974 got = 0;
9975 if (htab->elf.hgot != NULL)
9976 got = SYM_VAL (htab->elf.hgot);
9977
9978 if (htab->elf.dynamic_sections_created)
9979 {
9980 Elf32_External_Dyn *dyncon, *dynconend;
9981
9982 BFD_ASSERT (htab->elf.splt != NULL && sdyn != NULL);
9983
9984 dyncon = (Elf32_External_Dyn *) sdyn->contents;
9985 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
9986 for (; dyncon < dynconend; dyncon++)
9987 {
9988 Elf_Internal_Dyn dyn;
9989 asection *s;
9990
9991 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
9992
9993 switch (dyn.d_tag)
9994 {
9995 case DT_PLTGOT:
9996 if (htab->elf.target_os == is_vxworks)
9997 s = htab->elf.sgotplt;
9998 else
9999 s = htab->elf.splt;
10000 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10001 break;
10002
10003 case DT_PLTRELSZ:
10004 dyn.d_un.d_val = htab->elf.srelplt->size;
10005 break;
10006
10007 case DT_JMPREL:
10008 s = htab->elf.srelplt;
10009 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10010 break;
10011
10012 case DT_PPC_GOT:
10013 dyn.d_un.d_ptr = got;
10014 break;
10015
10016 case DT_TEXTREL:
10017 if (htab->local_ifunc_resolver)
10018 info->callbacks->einfo
10019 (_("%X%P: text relocations and GNU indirect "
10020 "functions will result in a segfault at runtime\n"));
10021 else if (htab->maybe_local_ifunc_resolver)
10022 info->callbacks->einfo
10023 (_("%P: warning: text relocations and GNU indirect "
10024 "functions may result in a segfault at runtime\n"));
10025 continue;
10026
10027 default:
10028 if (htab->elf.target_os == is_vxworks
10029 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10030 break;
10031 continue;
10032 }
10033
10034 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
10035 }
10036 }
10037
10038 if (htab->elf.sgot != NULL
10039 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
10040 {
10041 if (htab->elf.hgot->root.u.def.section == htab->elf.sgot
10042 || htab->elf.hgot->root.u.def.section == htab->elf.sgotplt)
10043 {
10044 unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
10045
10046 p += htab->elf.hgot->root.u.def.value;
10047 if (htab->plt_type == PLT_OLD)
10048 {
10049 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4
10050 so that a function can easily find the address of
10051 _GLOBAL_OFFSET_TABLE_. */
10052 BFD_ASSERT (htab->elf.hgot->root.u.def.value - 4
10053 < htab->elf.hgot->root.u.def.section->size);
10054 bfd_put_32 (output_bfd, 0x4e800021, p - 4);
10055 }
10056
10057 if (sdyn != NULL)
10058 {
10059 bfd_vma val = sdyn->output_section->vma + sdyn->output_offset;
10060 BFD_ASSERT (htab->elf.hgot->root.u.def.value
10061 < htab->elf.hgot->root.u.def.section->size);
10062 bfd_put_32 (output_bfd, val, p);
10063 }
10064 }
10065 else
10066 {
10067 /* xgettext:c-format */
10068 _bfd_error_handler (_("%s not defined in linker created %pA"),
10069 htab->elf.hgot->root.root.string,
10070 (htab->elf.sgotplt != NULL
10071 ? htab->elf.sgotplt : htab->elf.sgot));
10072 bfd_set_error (bfd_error_bad_value);
10073 ret = false;
10074 }
10075
10076 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
10077 }
10078
10079 /* Fill in the first entry in the VxWorks procedure linkage table. */
10080 if (htab->elf.target_os == is_vxworks
10081 && htab->elf.splt != NULL
10082 && htab->elf.splt->size != 0
10083 && htab->elf.splt->output_section != bfd_abs_section_ptr)
10084 {
10085 asection *splt = htab->elf.splt;
10086 /* Use the right PLT. */
10087 const bfd_vma *plt_entry = (bfd_link_pic (info)
10088 ? ppc_elf_vxworks_pic_plt0_entry
10089 : ppc_elf_vxworks_plt0_entry);
10090
10091 if (!bfd_link_pic (info))
10092 {
10093 bfd_vma got_value = SYM_VAL (htab->elf.hgot);
10094
10095 bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
10096 splt->contents + 0);
10097 bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
10098 splt->contents + 4);
10099 }
10100 else
10101 {
10102 bfd_put_32 (output_bfd, plt_entry[0], splt->contents + 0);
10103 bfd_put_32 (output_bfd, plt_entry[1], splt->contents + 4);
10104 }
10105 bfd_put_32 (output_bfd, plt_entry[2], splt->contents + 8);
10106 bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
10107 bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
10108 bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
10109 bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
10110 bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
10111
10112 if (! bfd_link_pic (info))
10113 {
10114 Elf_Internal_Rela rela;
10115 bfd_byte *loc;
10116
10117 loc = htab->srelplt2->contents;
10118
10119 /* Output the @ha relocation for the first instruction. */
10120 rela.r_offset = (htab->elf.splt->output_section->vma
10121 + htab->elf.splt->output_offset
10122 + 2);
10123 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
10124 rela.r_addend = 0;
10125 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10126 loc += sizeof (Elf32_External_Rela);
10127
10128 /* Output the @l relocation for the second instruction. */
10129 rela.r_offset = (htab->elf.splt->output_section->vma
10130 + htab->elf.splt->output_offset
10131 + 6);
10132 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
10133 rela.r_addend = 0;
10134 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10135 loc += sizeof (Elf32_External_Rela);
10136
10137 /* Fix up the remaining relocations. They may have the wrong
10138 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10139 in which symbols were output. */
10140 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10141 {
10142 Elf_Internal_Rela rel;
10143
10144 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
10145 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
10146 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
10147 loc += sizeof (Elf32_External_Rela);
10148
10149 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
10150 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
10151 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
10152 loc += sizeof (Elf32_External_Rela);
10153
10154 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
10155 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
10156 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
10157 loc += sizeof (Elf32_External_Rela);
10158 }
10159 }
10160 }
10161
10162 if (htab->glink != NULL
10163 && htab->glink->contents != NULL
10164 && htab->elf.dynamic_sections_created)
10165 {
10166 unsigned char *p;
10167 unsigned char *endp;
10168 bfd_vma res0;
10169
10170 /*
10171 * PIC glink code is the following:
10172 *
10173 * # ith PLT code stub.
10174 * addis 11,30,(plt+(i-1)*4-got)@ha
10175 * lwz 11,(plt+(i-1)*4-got)@l(11)
10176 * mtctr 11
10177 * bctr
10178 *
10179 * # A table of branches, one for each plt entry.
10180 * # The idea is that the plt call stub loads ctr and r11 with these
10181 * # addresses, so (r11 - res_0) gives the plt index * 4.
10182 * res_0: b PLTresolve
10183 * res_1: b PLTresolve
10184 * .
10185 * # Some number of entries towards the end can be nops
10186 * res_n_m3: nop
10187 * res_n_m2: nop
10188 * res_n_m1:
10189 *
10190 * PLTresolve:
10191 * addis 11,11,(1f-res_0)@ha
10192 * mflr 0
10193 * bcl 20,31,1f
10194 * 1: addi 11,11,(1b-res_0)@l
10195 * mflr 12
10196 * mtlr 0
10197 * sub 11,11,12 # r11 = index * 4
10198 * addis 12,12,(got+4-1b)@ha
10199 * lwz 0,(got+4-1b)@l(12) # got[1] address of dl_runtime_resolve
10200 * lwz 12,(got+8-1b)@l(12) # got[2] contains the map address
10201 * mtctr 0
10202 * add 0,11,11
10203 * add 11,0,11 # r11 = index * 12 = reloc offset.
10204 * bctr
10205 *
10206 * Non-PIC glink code is a little simpler.
10207 *
10208 * # ith PLT code stub.
10209 * lis 11,(plt+(i-1)*4)@ha
10210 * lwz 11,(plt+(i-1)*4)@l(11)
10211 * mtctr 11
10212 * bctr
10213 *
10214 * The branch table is the same, then comes
10215 *
10216 * PLTresolve:
10217 * lis 12,(got+4)@ha
10218 * addis 11,11,(-res_0)@ha
10219 * lwz 0,(got+4)@l(12) # got[1] address of dl_runtime_resolve
10220 * addi 11,11,(-res_0)@l # r11 = index * 4
10221 * mtctr 0
10222 * add 0,11,11
10223 * lwz 12,(got+8)@l(12) # got[2] contains the map address
10224 * add 11,0,11 # r11 = index * 12 = reloc offset.
10225 * bctr
10226 */
10227
10228 /* Build the branch table, one for each plt entry (less one),
10229 and perhaps some padding. */
10230 p = htab->glink->contents;
10231 p += htab->glink_pltresolve;
10232 endp = htab->glink->contents;
10233 endp += htab->glink->size - GLINK_PLTRESOLVE;
10234 while (p < endp - (htab->params->ppc476_workaround ? 0 : 8 * 4))
10235 {
10236 bfd_put_32 (output_bfd, B + endp - p, p);
10237 p += 4;
10238 }
10239 while (p < endp)
10240 {
10241 bfd_put_32 (output_bfd, NOP, p);
10242 p += 4;
10243 }
10244
10245 res0 = (htab->glink_pltresolve
10246 + htab->glink->output_section->vma
10247 + htab->glink->output_offset);
10248
10249 if (htab->params->ppc476_workaround)
10250 {
10251 /* Ensure that a call stub at the end of a page doesn't
10252 result in prefetch over the end of the page into the
10253 glink branch table. */
10254 bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
10255 bfd_vma page_addr;
10256 bfd_vma glink_start = (htab->glink->output_section->vma
10257 + htab->glink->output_offset);
10258
10259 for (page_addr = res0 & -pagesize;
10260 page_addr > glink_start;
10261 page_addr -= pagesize)
10262 {
10263 /* We have a plt call stub that may need fixing. */
10264 bfd_byte *loc;
10265 unsigned int insn;
10266
10267 loc = htab->glink->contents + page_addr - 4 - glink_start;
10268 insn = bfd_get_32 (output_bfd, loc);
10269 if (insn == BCTR)
10270 {
10271 /* By alignment, we know that there must be at least
10272 one other call stub before this one. */
10273 insn = bfd_get_32 (output_bfd, loc - 16);
10274 if (insn == BCTR)
10275 bfd_put_32 (output_bfd, B | (-16 & 0x3fffffc), loc);
10276 else
10277 bfd_put_32 (output_bfd, B | (-20 & 0x3fffffc), loc);
10278 }
10279 }
10280 }
10281
10282 /* Last comes the PLTresolve stub. */
10283 endp = p + GLINK_PLTRESOLVE;
10284 if (bfd_link_pic (info))
10285 {
10286 bfd_vma bcl;
10287
10288 bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
10289 + htab->glink->output_section->vma
10290 + htab->glink->output_offset);
10291
10292 bfd_put_32 (output_bfd, ADDIS_11_11 + PPC_HA (bcl - res0), p);
10293 p += 4;
10294 bfd_put_32 (output_bfd, MFLR_0, p);
10295 p += 4;
10296 bfd_put_32 (output_bfd, BCL_20_31, p);
10297 p += 4;
10298 bfd_put_32 (output_bfd, ADDI_11_11 + PPC_LO (bcl - res0), p);
10299 p += 4;
10300 bfd_put_32 (output_bfd, MFLR_12, p);
10301 p += 4;
10302 bfd_put_32 (output_bfd, MTLR_0, p);
10303 p += 4;
10304 bfd_put_32 (output_bfd, SUB_11_11_12, p);
10305 p += 4;
10306 bfd_put_32 (output_bfd, ADDIS_12_12 + PPC_HA (got + 4 - bcl), p);
10307 p += 4;
10308 if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
10309 {
10310 bfd_put_32 (output_bfd, LWZ_0_12 + PPC_LO (got + 4 - bcl), p);
10311 p += 4;
10312 bfd_put_32 (output_bfd, LWZ_12_12 + PPC_LO (got + 8 - bcl), p);
10313 p += 4;
10314 }
10315 else
10316 {
10317 bfd_put_32 (output_bfd, LWZU_0_12 + PPC_LO (got + 4 - bcl), p);
10318 p += 4;
10319 bfd_put_32 (output_bfd, LWZ_12_12 + 4, p);
10320 p += 4;
10321 }
10322 bfd_put_32 (output_bfd, MTCTR_0, p);
10323 p += 4;
10324 bfd_put_32 (output_bfd, ADD_0_11_11, p);
10325 }
10326 else
10327 {
10328 bfd_put_32 (output_bfd, LIS_12 + PPC_HA (got + 4), p);
10329 p += 4;
10330 bfd_put_32 (output_bfd, ADDIS_11_11 + PPC_HA (-res0), p);
10331 p += 4;
10332 if (PPC_HA (got + 4) == PPC_HA (got + 8))
10333 bfd_put_32 (output_bfd, LWZ_0_12 + PPC_LO (got + 4), p);
10334 else
10335 bfd_put_32 (output_bfd, LWZU_0_12 + PPC_LO (got + 4), p);
10336 p += 4;
10337 bfd_put_32 (output_bfd, ADDI_11_11 + PPC_LO (-res0), p);
10338 p += 4;
10339 bfd_put_32 (output_bfd, MTCTR_0, p);
10340 p += 4;
10341 bfd_put_32 (output_bfd, ADD_0_11_11, p);
10342 p += 4;
10343 if (PPC_HA (got + 4) == PPC_HA (got + 8))
10344 bfd_put_32 (output_bfd, LWZ_12_12 + PPC_LO (got + 8), p);
10345 else
10346 bfd_put_32 (output_bfd, LWZ_12_12 + 4, p);
10347 }
10348 p += 4;
10349 bfd_put_32 (output_bfd, ADD_11_0_11, p);
10350 p += 4;
10351 bfd_put_32 (output_bfd, BCTR, p);
10352 p += 4;
10353 while (p < endp)
10354 {
10355 bfd_put_32 (output_bfd,
10356 htab->params->ppc476_workaround ? BA : NOP, p);
10357 p += 4;
10358 }
10359 BFD_ASSERT (p == endp);
10360 }
10361
10362 if (htab->glink_eh_frame != NULL
10363 && htab->glink_eh_frame->contents != NULL)
10364 {
10365 unsigned char *p = htab->glink_eh_frame->contents;
10366 bfd_vma val;
10367
10368 p += sizeof (glink_eh_frame_cie);
10369 /* FDE length. */
10370 p += 4;
10371 /* CIE pointer. */
10372 p += 4;
10373 /* Offset to .glink. */
10374 val = (htab->glink->output_section->vma
10375 + htab->glink->output_offset);
10376 val -= (htab->glink_eh_frame->output_section->vma
10377 + htab->glink_eh_frame->output_offset);
10378 val -= p - htab->glink_eh_frame->contents;
10379 bfd_put_32 (htab->elf.dynobj, val, p);
10380
10381 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
10382 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
10383 htab->glink_eh_frame,
10384 htab->glink_eh_frame->contents))
10385 return false;
10386 }
10387
10388 return ret;
10389 }
10390
10391 #define TARGET_LITTLE_SYM powerpc_elf32_le_vec
10393 #define TARGET_LITTLE_NAME "elf32-powerpcle"
10394 #define TARGET_BIG_SYM powerpc_elf32_vec
10395 #define TARGET_BIG_NAME "elf32-powerpc"
10396 #define ELF_ARCH bfd_arch_powerpc
10397 #define ELF_TARGET_ID PPC32_ELF_DATA
10398 #define ELF_MACHINE_CODE EM_PPC
10399 #define ELF_MAXPAGESIZE 0x10000
10400 #define ELF_COMMONPAGESIZE 0x1000
10401 #define elf_info_to_howto ppc_elf_info_to_howto
10402
10403 #ifdef EM_CYGNUS_POWERPC
10404 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
10405 #endif
10406
10407 #ifdef EM_PPC_OLD
10408 #define ELF_MACHINE_ALT2 EM_PPC_OLD
10409 #endif
10410
10411 #define elf_backend_plt_not_loaded 1
10412 #define elf_backend_want_dynrelro 1
10413 #define elf_backend_can_gc_sections 1
10414 #define elf_backend_can_refcount 1
10415 #define elf_backend_rela_normal 1
10416 #define elf_backend_caches_rawsize 1
10417
10418 #define bfd_elf32_mkobject ppc_elf_mkobject
10419 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
10420 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
10421 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
10422 #define bfd_elf32_bfd_reloc_name_lookup ppc_elf_reloc_name_lookup
10423 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
10424 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
10425 #define bfd_elf32_get_synthetic_symtab ppc_elf_get_synthetic_symtab
10426
10427 #define elf_backend_object_p ppc_elf_object_p
10428 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
10429 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
10430 #define elf_backend_relocate_section ppc_elf_relocate_section
10431 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
10432 #define elf_backend_check_relocs ppc_elf_check_relocs
10433 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
10434 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
10435 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
10436 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
10437 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
10438 #define elf_backend_hash_symbol ppc_elf_hash_symbol
10439 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
10440 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
10441 #define elf_backend_fake_sections ppc_elf_fake_sections
10442 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
10443 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
10444 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
10445 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
10446 #define elf_backend_write_core_note ppc_elf_write_core_note
10447 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
10448 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
10449 #define elf_backend_final_write_processing ppc_elf_final_write_processing
10450 #define elf_backend_write_section ppc_elf_write_section
10451 #define elf_backend_get_sec_type_attr ppc_elf_get_sec_type_attr
10452 #define elf_backend_plt_sym_val ppc_elf_plt_sym_val
10453 #define elf_backend_action_discarded ppc_elf_action_discarded
10454 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
10455 #define elf_backend_lookup_section_flags_hook ppc_elf_lookup_section_flags
10456
10457 #include "elf32-target.h"
10458
10459 /* FreeBSD Target */
10460
10461 #undef TARGET_LITTLE_SYM
10462 #undef TARGET_LITTLE_NAME
10463
10464 #undef TARGET_BIG_SYM
10465 #define TARGET_BIG_SYM powerpc_elf32_fbsd_vec
10466 #undef TARGET_BIG_NAME
10467 #define TARGET_BIG_NAME "elf32-powerpc-freebsd"
10468
10469 #undef ELF_OSABI
10470 #define ELF_OSABI ELFOSABI_FREEBSD
10471
10472 #undef elf32_bed
10473 #define elf32_bed elf32_powerpc_fbsd_bed
10474
10475 #include "elf32-target.h"
10476
10477 /* VxWorks Target */
10478
10479 #undef TARGET_LITTLE_SYM
10480 #undef TARGET_LITTLE_NAME
10481
10482 #undef TARGET_BIG_SYM
10483 #define TARGET_BIG_SYM powerpc_elf32_vxworks_vec
10484 #undef TARGET_BIG_NAME
10485 #define TARGET_BIG_NAME "elf32-powerpc-vxworks"
10486
10487 #undef ELF_OSABI
10488
10489 #undef ELF_TARGET_OS
10490 #define ELF_TARGET_OS is_vxworks
10491
10492 /* VxWorks uses the elf default section flags for .plt. */
10493 static const struct bfd_elf_special_section *
10494 ppc_elf_vxworks_get_sec_type_attr (bfd *abfd, asection *sec)
10495 {
10496 if (sec->name == NULL)
10497 return NULL;
10498
10499 if (strcmp (sec->name, ".plt") == 0)
10500 return _bfd_elf_get_sec_type_attr (abfd, sec);
10501
10502 return ppc_elf_get_sec_type_attr (abfd, sec);
10503 }
10504
10505 /* Like ppc_elf_link_hash_table_create, but overrides
10506 appropriately for VxWorks. */
10507 static struct bfd_link_hash_table *
10508 ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
10509 {
10510 struct bfd_link_hash_table *ret;
10511
10512 ret = ppc_elf_link_hash_table_create (abfd);
10513 if (ret)
10514 {
10515 struct ppc_elf_link_hash_table *htab
10516 = (struct ppc_elf_link_hash_table *)ret;
10517 htab->plt_type = PLT_VXWORKS;
10518 htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
10519 htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
10520 htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
10521 }
10522 return ret;
10523 }
10524
10525 /* Tweak magic VxWorks symbols as they are loaded. */
10526 static bool
10527 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
10528 struct bfd_link_info *info,
10529 Elf_Internal_Sym *sym,
10530 const char **namep,
10531 flagword *flagsp,
10532 asection **secp,
10533 bfd_vma *valp)
10534 {
10535 if (!elf_vxworks_add_symbol_hook (abfd, info, sym, namep, flagsp, secp,
10536 valp))
10537 return false;
10538
10539 return ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp);
10540 }
10541
10542 static bool
10543 ppc_elf_vxworks_final_write_processing (bfd *abfd)
10544 {
10545 ppc_final_write_processing (abfd);
10546 return elf_vxworks_final_write_processing (abfd);
10547 }
10548
10549 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
10550 define it. */
10551 #undef elf_backend_want_plt_sym
10552 #define elf_backend_want_plt_sym 1
10553 #undef elf_backend_want_got_plt
10554 #define elf_backend_want_got_plt 1
10555 #undef elf_backend_got_symbol_offset
10556 #define elf_backend_got_symbol_offset 0
10557 #undef elf_backend_plt_not_loaded
10558 #define elf_backend_plt_not_loaded 0
10559 #undef elf_backend_plt_readonly
10560 #define elf_backend_plt_readonly 1
10561 #undef elf_backend_got_header_size
10562 #define elf_backend_got_header_size 12
10563 #undef elf_backend_dtrel_excludes_plt
10564 #define elf_backend_dtrel_excludes_plt 1
10565
10566 #undef bfd_elf32_get_synthetic_symtab
10567
10568 #undef bfd_elf32_bfd_link_hash_table_create
10569 #define bfd_elf32_bfd_link_hash_table_create \
10570 ppc_elf_vxworks_link_hash_table_create
10571 #undef elf_backend_add_symbol_hook
10572 #define elf_backend_add_symbol_hook \
10573 ppc_elf_vxworks_add_symbol_hook
10574 #undef elf_backend_link_output_symbol_hook
10575 #define elf_backend_link_output_symbol_hook \
10576 elf_vxworks_link_output_symbol_hook
10577 #undef elf_backend_final_write_processing
10578 #define elf_backend_final_write_processing \
10579 ppc_elf_vxworks_final_write_processing
10580 #undef elf_backend_get_sec_type_attr
10581 #define elf_backend_get_sec_type_attr \
10582 ppc_elf_vxworks_get_sec_type_attr
10583 #undef elf_backend_emit_relocs
10584 #define elf_backend_emit_relocs \
10585 elf_vxworks_emit_relocs
10586
10587 #undef elf32_bed
10588 #define elf32_bed ppc_elf_vxworks_bed
10589
10590 #include "elf32-target.h"
10591