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