[gdb/build] Fix gdbserver build with -fsanitize=address
[deliverable/binutils-gdb.git] / bfd / elf32-ppc.c
CommitLineData
252b5132 1/* PowerPC-specific support for 32-bit ELF
250d07de 2 Copyright (C) 1994-2021 Free Software Foundation, Inc.
252b5132
RH
3 Written by Ian Lance Taylor, Cygnus Support.
4
ae9a127f 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
ae9a127f
NC
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
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
ae9a127f 10 (at your option) any later version.
252b5132 11
ae9a127f
NC
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.
252b5132 16
ae9a127f 17 You should have received a copy of the GNU General Public License
fc0bffd6 18 along with this program; if not, write to the
3e110533 19 Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
53e09e0a 20 Boston, MA 02110-1301, USA. */
252b5132 21
cd123cb7 22
252b5132
RH
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
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
252b5132 30#include "bfd.h"
252b5132
RH
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
34#include "elf/ppc.h"
7619e7c7 35#include "elf32-ppc.h"
9d8504b1 36#include "elf-vxworks.h"
6177242a 37#include "dwarf2.h"
08dc996f 38#include "opcode/ppc.h"
252b5132 39
bb294208
AM
40/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
41#define OCTETS_PER_BYTE(ABFD, SEC) 1
42
b9c361e0
JL
43typedef enum split16_format_type
44{
45 split16a_type = 0,
46 split16d_type
47}
48split16_format_type;
49
f0fe0e16 50/* RELA relocations are used here. */
252b5132 51
252b5132 52static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
55fd94b0 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
7619e7c7 54static bfd_reloc_status_type ppc_elf_unhandled_reloc
55fd94b0 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
252b5132 56
70bccea4
AM
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
252b5132
RH
63
64/* The name of the dynamic interpreter. This is put in the .interp
65 section. */
252b5132
RH
66#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
67
d7128ce4 68/* For old-style PLT. */
252b5132
RH
69/* The number of single-slot PLT entries (the rest use two slots). */
70#define PLT_NUM_SINGLE_ENTRIES 8192
71
d7128ce4 72/* For new-style .glink and .plt. */
a6aa5195 73#define GLINK_PLTRESOLVE 16*4
9e390558 74#define GLINK_ENTRY_SIZE(htab, h) \
407aa07c 75 ((4*4 \
9e390558
AM
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))
d7128ce4 81
9d8504b1
PB
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
86static const bfd_vma ppc_elf_vxworks_plt_entry
87 [VXWORKS_PLT_ENTRY_SIZE / 4] =
88 {
07d6d2b8
AM
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 */
9d8504b1
PB
97 };
98static 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
113static const bfd_vma ppc_elf_vxworks_plt0_entry
114 [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
115 {
07d6d2b8
AM
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 */
9d8504b1
PB
124 };
125static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
126 [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
127 {
128 0x819e0008, /* lwz r12,8(r30) */
07d6d2b8 129 0x7d8903a6, /* mtctr r12 */
9d8504b1 130 0x819e0004, /* lwz r12,4(r30) */
07d6d2b8
AM
131 0x4e800420, /* bctr */
132 0x60000000, /* nop */
133 0x60000000, /* nop */
134 0x60000000, /* nop */
135 0x60000000, /* nop */
9d8504b1
PB
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
de194d85 145/* The number of relocations in the PLTResolve slot when creating
9d8504b1
PB
146 a shared library. */
147#define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
148
d7128ce4 149/* Some instructions. */
d7128ce4 150#define ADDIS_11_11 0x3d6b0000
a6aa5195
AM
151#define ADDIS_11_30 0x3d7e0000
152#define ADDIS_12_12 0x3d8c0000
d7128ce4 153#define ADDI_11_11 0x396b0000
d7128ce4 154#define ADD_0_11_11 0x7c0b5a14
a7f2871e 155#define ADD_3_12_2 0x7c6c1214
d7128ce4 156#define ADD_11_0_11 0x7d605a14
a6aa5195 157#define B 0x48000000
da3a2088 158#define BA 0x48000002
a6aa5195 159#define BCL_20_31 0x429f0005
d7128ce4 160#define BCTR 0x4e800420
a7f2871e
AM
161#define BEQLR 0x4d820020
162#define CMPWI_11_0 0x2c0b0000
7e8aeb9a
AM
163#define LIS_11 0x3d600000
164#define LIS_12 0x3d800000
a6aa5195
AM
165#define LWZU_0_12 0x840c0000
166#define LWZ_0_12 0x800c0000
a7f2871e 167#define LWZ_11_3 0x81630000
a6aa5195
AM
168#define LWZ_11_11 0x816b0000
169#define LWZ_11_30 0x817e0000
a7f2871e 170#define LWZ_12_3 0x81830000
a6aa5195 171#define LWZ_12_12 0x818c0000
a7f2871e
AM
172#define MR_0_3 0x7c601b78
173#define MR_3_0 0x7c030378
a6aa5195
AM
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
7619e7c7
AM
181
182/* Offset of tp and dtp pointers from start of TLS block. */
183#define TP_OFFSET 0x7000
184#define DTP_OFFSET 0x8000
e87d4038
AM
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)
7fce784e 191\f
46807bf4
AM
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
e47cd125 206static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
252b5132 207
8da6118f 208static reloc_howto_type ppc_elf_howto_raw[] = {
252b5132 209 /* This reloc does nothing. */
46807bf4
AM
210 HOW (R_PPC_NONE, 3, 0, 0, 0, FALSE, dont,
211 bfd_elf_generic_reloc),
252b5132
RH
212
213 /* A standard 32 bit relocation. */
46807bf4
AM
214 HOW (R_PPC_ADDR32, 2, 32, 0xffffffff, 0, FALSE, dont,
215 bfd_elf_generic_reloc),
252b5132
RH
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. */
46807bf4
AM
219 HOW (R_PPC_ADDR24, 2, 26, 0x3fffffc, 0, FALSE, signed,
220 bfd_elf_generic_reloc),
252b5132
RH
221
222 /* A standard 16 bit relocation. */
46807bf4
AM
223 HOW (R_PPC_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
224 bfd_elf_generic_reloc),
252b5132
RH
225
226 /* A 16 bit relocation without overflow. */
46807bf4
AM
227 HOW (R_PPC_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
228 bfd_elf_generic_reloc),
252b5132
RH
229
230 /* The high order 16 bits of an address. */
46807bf4
AM
231 HOW (R_PPC_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, dont,
232 bfd_elf_generic_reloc),
252b5132
RH
233
234 /* The high order 16 bits of an address, plus 1 if the contents of
8da6118f 235 the low 16 bits, treated as a signed number, is negative. */
46807bf4
AM
236 HOW (R_PPC_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, dont,
237 ppc_elf_addr16_ha_reloc),
252b5132
RH
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. */
46807bf4
AM
241 HOW (R_PPC_ADDR14, 2, 16, 0xfffc, 0, FALSE, signed,
242 bfd_elf_generic_reloc),
252b5132
RH
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
8da6118f 246 bits must be zero. */
46807bf4
AM
247 HOW (R_PPC_ADDR14_BRTAKEN, 2, 16, 0xfffc, 0, FALSE, signed,
248 bfd_elf_generic_reloc),
252b5132
RH
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. */
46807bf4
AM
253 HOW (R_PPC_ADDR14_BRNTAKEN, 2, 16, 0xfffc, 0, FALSE, signed,
254 bfd_elf_generic_reloc),
252b5132 255
8da6118f 256 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
257 HOW (R_PPC_REL24, 2, 26, 0x3fffffc, 0, TRUE, signed,
258 bfd_elf_generic_reloc),
252b5132 259
8da6118f 260 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
261 HOW (R_PPC_REL14, 2, 16, 0xfffc, 0, TRUE, signed,
262 bfd_elf_generic_reloc),
252b5132 263
8da6118f 264 /* A relative 16 bit branch. Bit 10 should be set to indicate that
252b5132
RH
265 the branch is expected to be taken. The lower two bits must be
266 zero. */
46807bf4
AM
267 HOW (R_PPC_REL14_BRTAKEN, 2, 16, 0xfffc, 0, TRUE, signed,
268 bfd_elf_generic_reloc),
252b5132 269
8da6118f 270 /* A relative 16 bit branch. Bit 10 should be set to indicate that
252b5132
RH
271 the branch is not expected to be taken. The lower two bits must
272 be zero. */
46807bf4
AM
273 HOW (R_PPC_REL14_BRNTAKEN, 2, 16, 0xfffc, 0, TRUE, signed,
274 bfd_elf_generic_reloc),
252b5132
RH
275
276 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
277 symbol. */
46807bf4
AM
278 HOW (R_PPC_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
279 ppc_elf_unhandled_reloc),
252b5132
RH
280
281 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
282 the symbol. */
46807bf4
AM
283 HOW (R_PPC_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
284 ppc_elf_unhandled_reloc),
252b5132
RH
285
286 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
287 the symbol. */
46807bf4
AM
288 HOW (R_PPC_GOT16_HI, 1, 16, 0xffff, 16, FALSE, dont,
289 ppc_elf_unhandled_reloc),
252b5132
RH
290
291 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
292 the symbol. */
46807bf4
AM
293 HOW (R_PPC_GOT16_HA, 1, 16, 0xffff, 16, FALSE, dont,
294 ppc_elf_unhandled_reloc),
252b5132
RH
295
296 /* Like R_PPC_REL24, but referring to the procedure linkage table
297 entry for the symbol. */
46807bf4
AM
298 HOW (R_PPC_PLTREL24, 2, 26, 0x3fffffc, 0, TRUE, signed,
299 ppc_elf_unhandled_reloc),
252b5132
RH
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. */
46807bf4
AM
306 HOW (R_PPC_COPY, 2, 32, 0, 0, FALSE, dont,
307 ppc_elf_unhandled_reloc),
252b5132
RH
308
309 /* Like R_PPC_ADDR32, but used when setting global offset table
310 entries. */
46807bf4
AM
311 HOW (R_PPC_GLOB_DAT, 2, 32, 0xffffffff, 0, FALSE, dont,
312 ppc_elf_unhandled_reloc),
252b5132
RH
313
314 /* Marks a procedure linkage table entry for a symbol. */
46807bf4
AM
315 HOW (R_PPC_JMP_SLOT, 2, 32, 0, 0, FALSE, dont,
316 ppc_elf_unhandled_reloc),
252b5132
RH
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. */
46807bf4
AM
321 HOW (R_PPC_RELATIVE, 2, 32, 0xffffffff, 0, FALSE, dont,
322 bfd_elf_generic_reloc),
252b5132
RH
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_. */
46807bf4
AM
327 HOW (R_PPC_LOCAL24PC, 2, 26, 0x3fffffc, 0, TRUE, signed,
328 bfd_elf_generic_reloc),
252b5132
RH
329
330 /* Like R_PPC_ADDR32, but may be unaligned. */
46807bf4
AM
331 HOW (R_PPC_UADDR32, 2, 32, 0xffffffff, 0, FALSE, dont,
332 bfd_elf_generic_reloc),
252b5132
RH
333
334 /* Like R_PPC_ADDR16, but may be unaligned. */
46807bf4
AM
335 HOW (R_PPC_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
336 bfd_elf_generic_reloc),
252b5132
RH
337
338 /* 32-bit PC relative */
46807bf4
AM
339 HOW (R_PPC_REL32, 2, 32, 0xffffffff, 0, TRUE, dont,
340 bfd_elf_generic_reloc),
252b5132
RH
341
342 /* 32-bit relocation to the symbol's procedure linkage table.
c3668558 343 FIXME: not supported. */
46807bf4
AM
344 HOW (R_PPC_PLT32, 2, 32, 0, 0, FALSE, dont,
345 ppc_elf_unhandled_reloc),
252b5132
RH
346
347 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
c3668558 348 FIXME: not supported. */
46807bf4
AM
349 HOW (R_PPC_PLTREL32, 2, 32, 0, 0, TRUE, dont,
350 ppc_elf_unhandled_reloc),
252b5132
RH
351
352 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
353 the symbol. */
46807bf4
AM
354 HOW (R_PPC_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
355 ppc_elf_unhandled_reloc),
252b5132
RH
356
357 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
358 the symbol. */
46807bf4
AM
359 HOW (R_PPC_PLT16_HI, 1, 16, 0xffff, 16, FALSE, dont,
360 ppc_elf_unhandled_reloc),
252b5132
RH
361
362 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
363 the symbol. */
46807bf4
AM
364 HOW (R_PPC_PLT16_HA, 1, 16, 0xffff, 16, FALSE, dont,
365 ppc_elf_unhandled_reloc),
252b5132
RH
366
367 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
368 small data items. */
46807bf4
AM
369 HOW (R_PPC_SDAREL16, 1, 16, 0xffff, 0, FALSE, signed,
370 ppc_elf_unhandled_reloc),
252b5132 371
c061c2d8 372 /* 16-bit section relative relocation. */
46807bf4
AM
373 HOW (R_PPC_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
374 ppc_elf_unhandled_reloc),
252b5132 375
c3668558 376 /* 16-bit lower half section relative relocation. */
46807bf4
AM
377 HOW (R_PPC_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
378 ppc_elf_unhandled_reloc),
252b5132 379
c3668558 380 /* 16-bit upper half section relative relocation. */
46807bf4
AM
381 HOW (R_PPC_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, dont,
382 ppc_elf_unhandled_reloc),
252b5132 383
c3668558 384 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
385 HOW (R_PPC_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, dont,
386 ppc_elf_unhandled_reloc),
252b5132 387
727fc41e 388 /* Marker relocs for TLS. */
46807bf4
AM
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),
727fc41e 397
23cedd1d 398 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
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),
23cedd1d 404
7619e7c7
AM
405 /* Computes the load module index of the load module that contains the
406 definition of its TLS sym. */
46807bf4
AM
407 HOW (R_PPC_DTPMOD32, 2, 32, 0xffffffff, 0, FALSE, dont,
408 ppc_elf_unhandled_reloc),
252b5132 409
7619e7c7
AM
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. */
46807bf4
AM
413 HOW (R_PPC_DTPREL32, 2, 32, 0xffffffff, 0, FALSE, dont,
414 ppc_elf_unhandled_reloc),
7619e7c7
AM
415
416 /* A 16 bit dtprel reloc. */
46807bf4
AM
417 HOW (R_PPC_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
418 ppc_elf_unhandled_reloc),
252b5132 419
7619e7c7 420 /* Like DTPREL16, but no overflow. */
46807bf4
AM
421 HOW (R_PPC_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
422 ppc_elf_unhandled_reloc),
252b5132 423
7619e7c7 424 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
425 HOW (R_PPC_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, dont,
426 ppc_elf_unhandled_reloc),
252b5132 427
7619e7c7 428 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
429 HOW (R_PPC_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, dont,
430 ppc_elf_unhandled_reloc),
252b5132 431
7619e7c7
AM
432 /* Computes a tp-relative displacement, the difference between the value of
433 sym+add and the value of the thread pointer (r13). */
46807bf4
AM
434 HOW (R_PPC_TPREL32, 2, 32, 0xffffffff, 0, FALSE, dont,
435 ppc_elf_unhandled_reloc),
7619e7c7
AM
436
437 /* A 16 bit tprel reloc. */
46807bf4
AM
438 HOW (R_PPC_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
439 ppc_elf_unhandled_reloc),
252b5132 440
7619e7c7 441 /* Like TPREL16, but no overflow. */
46807bf4
AM
442 HOW (R_PPC_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
443 ppc_elf_unhandled_reloc),
252b5132 444
7619e7c7 445 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
446 HOW (R_PPC_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, dont,
447 ppc_elf_unhandled_reloc),
7619e7c7
AM
448
449 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
450 HOW (R_PPC_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, dont,
451 ppc_elf_unhandled_reloc),
7619e7c7
AM
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. */
46807bf4
AM
456 HOW (R_PPC_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
457 ppc_elf_unhandled_reloc),
252b5132 458
7619e7c7 459 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
460 HOW (R_PPC_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
461 ppc_elf_unhandled_reloc),
252b5132 462
7619e7c7 463 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
464 HOW (R_PPC_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, dont,
465 ppc_elf_unhandled_reloc),
252b5132 466
7619e7c7 467 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
468 HOW (R_PPC_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, dont,
469 ppc_elf_unhandled_reloc),
7619e7c7
AM
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. */
46807bf4
AM
474 HOW (R_PPC_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
475 ppc_elf_unhandled_reloc),
252b5132 476
7619e7c7 477 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
478 HOW (R_PPC_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
479 ppc_elf_unhandled_reloc),
252b5132 480
7619e7c7 481 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
482 HOW (R_PPC_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, dont,
483 ppc_elf_unhandled_reloc),
252b5132 484
7619e7c7 485 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
486 HOW (R_PPC_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, dont,
487 ppc_elf_unhandled_reloc),
7619e7c7
AM
488
489 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
490 the offset to the entry. */
46807bf4
AM
491 HOW (R_PPC_GOT_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
492 ppc_elf_unhandled_reloc),
252b5132 493
7619e7c7 494 /* Like GOT_DTPREL16, but no overflow. */
46807bf4
AM
495 HOW (R_PPC_GOT_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
496 ppc_elf_unhandled_reloc),
252b5132 497
7619e7c7 498 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
499 HOW (R_PPC_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, dont,
500 ppc_elf_unhandled_reloc),
252b5132 501
7619e7c7 502 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
503 HOW (R_PPC_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, dont,
504 ppc_elf_unhandled_reloc),
c3668558 505
7619e7c7
AM
506 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
507 offset to the entry. */
46807bf4
AM
508 HOW (R_PPC_GOT_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
509 ppc_elf_unhandled_reloc),
7619e7c7
AM
510
511 /* Like GOT_TPREL16, but no overflow. */
46807bf4
AM
512 HOW (R_PPC_GOT_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
513 ppc_elf_unhandled_reloc),
7619e7c7
AM
514
515 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
516 HOW (R_PPC_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, dont,
517 ppc_elf_unhandled_reloc),
7619e7c7
AM
518
519 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
520 HOW (R_PPC_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, dont,
521 ppc_elf_unhandled_reloc),
7619e7c7
AM
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. */
46807bf4
AM
527 HOW (R_PPC_EMB_NADDR32, 2, 32, 0xffffffff, 0, FALSE, dont,
528 ppc_elf_unhandled_reloc),
7619e7c7
AM
529
530 /* 16 bit value resulting from the addend minus the symbol. */
46807bf4
AM
531 HOW (R_PPC_EMB_NADDR16, 1, 16, 0xffff, 0, FALSE, signed,
532 ppc_elf_unhandled_reloc),
7619e7c7
AM
533
534 /* 16 bit value resulting from the addend minus the symbol. */
46807bf4
AM
535 HOW (R_PPC_EMB_NADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
536 ppc_elf_unhandled_reloc),
7619e7c7
AM
537
538 /* The high order 16 bits of the addend minus the symbol. */
46807bf4
AM
539 HOW (R_PPC_EMB_NADDR16_HI, 1, 16, 0xffff, 16, FALSE, dont,
540 ppc_elf_unhandled_reloc),
7619e7c7
AM
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. */
46807bf4
AM
545 HOW (R_PPC_EMB_NADDR16_HA, 1, 16, 0xffff, 16, FALSE, dont,
546 ppc_elf_unhandled_reloc),
25dbc73a
AM
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. */
46807bf4
AM
551 HOW (R_PPC_EMB_SDAI16, 1, 16, 0xffff, 0, FALSE, signed,
552 ppc_elf_unhandled_reloc),
25dbc73a
AM
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. */
46807bf4
AM
557 HOW (R_PPC_EMB_SDA2I16, 1, 16, 0xffff, 0, FALSE, signed,
558 ppc_elf_unhandled_reloc),
25dbc73a
AM
559
560 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
561 small data items. */
46807bf4
AM
562 HOW (R_PPC_EMB_SDA2REL, 1, 16, 0xffff, 0, FALSE, signed,
563 ppc_elf_unhandled_reloc),
25dbc73a
AM
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). */
46807bf4
AM
568 HOW (R_PPC_EMB_SDA21, 2, 16, 0xffff, 0, FALSE, signed,
569 ppc_elf_unhandled_reloc),
25dbc73a
AM
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). */
46807bf4
AM
581 HOW (R_PPC_EMB_RELSDA, 1, 16, 0xffff, 0, FALSE, signed,
582 ppc_elf_unhandled_reloc),
25dbc73a 583
b9c361e0 584 /* A relative 8 bit branch. */
46807bf4
AM
585 HOW (R_PPC_VLE_REL8, 1, 8, 0xff, 1, TRUE, signed,
586 bfd_elf_generic_reloc),
68ffbac6 587
b9c361e0 588 /* A relative 15 bit branch. */
46807bf4
AM
589 HOW (R_PPC_VLE_REL15, 2, 16, 0xfffe, 0, TRUE, signed,
590 bfd_elf_generic_reloc),
b9c361e0 591
68ffbac6 592 /* A relative 24 bit branch. */
46807bf4
AM
593 HOW (R_PPC_VLE_REL24, 2, 25, 0x1fffffe, 0, TRUE, signed,
594 bfd_elf_generic_reloc),
b9c361e0
JL
595
596 /* The 16 LSBS in split16a format. */
46807bf4
AM
597 HOW (R_PPC_VLE_LO16A, 2, 16, 0x1f07ff, 0, FALSE, dont,
598 ppc_elf_unhandled_reloc),
b9c361e0
JL
599
600 /* The 16 LSBS in split16d format. */
46807bf4
AM
601 HOW (R_PPC_VLE_LO16D, 2, 16, 0x3e007ff, 0, FALSE, dont,
602 ppc_elf_unhandled_reloc),
b9c361e0
JL
603
604 /* Bits 16-31 split16a format. */
46807bf4
AM
605 HOW (R_PPC_VLE_HI16A, 2, 16, 0x1f07ff, 16, FALSE, dont,
606 ppc_elf_unhandled_reloc),
b9c361e0
JL
607
608 /* Bits 16-31 split16d format. */
46807bf4
AM
609 HOW (R_PPC_VLE_HI16D, 2, 16, 0x3e007ff, 16, FALSE, dont,
610 ppc_elf_unhandled_reloc),
b9c361e0
JL
611
612 /* Bits 16-31 (High Adjusted) in split16a format. */
46807bf4
AM
613 HOW (R_PPC_VLE_HA16A, 2, 16, 0x1f07ff, 16, FALSE, dont,
614 ppc_elf_unhandled_reloc),
b9c361e0
JL
615
616 /* Bits 16-31 (High Adjusted) in split16d format. */
46807bf4
AM
617 HOW (R_PPC_VLE_HA16D, 2, 16, 0x3e007ff, 16, FALSE, dont,
618 ppc_elf_unhandled_reloc),
b9c361e0 619
86c95733
AM
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. */
46807bf4
AM
623 HOW (R_PPC_VLE_SDA21, 2, 16, 0xffff, 0, FALSE, signed,
624 ppc_elf_unhandled_reloc),
b9c361e0 625
86c95733 626 /* Like R_PPC_VLE_SDA21 but ignore overflow. */
46807bf4
AM
627 HOW (R_PPC_VLE_SDA21_LO, 2, 16, 0xffff, 0, FALSE, dont,
628 ppc_elf_unhandled_reloc),
b9c361e0
JL
629
630 /* The 16 LSBS relative to _SDA_BASE_ in split16a format. */
46807bf4
AM
631 HOW (R_PPC_VLE_SDAREL_LO16A, 2, 16, 0x1f07ff, 0, FALSE, dont,
632 ppc_elf_unhandled_reloc),
b9c361e0
JL
633
634 /* The 16 LSBS relative to _SDA_BASE_ in split16d format. */
46807bf4
AM
635 HOW (R_PPC_VLE_SDAREL_LO16D, 2, 16, 0x3e007ff, 0, FALSE, dont,
636 ppc_elf_unhandled_reloc),
b9c361e0
JL
637
638 /* Bits 16-31 relative to _SDA_BASE_ in split16a format. */
46807bf4
AM
639 HOW (R_PPC_VLE_SDAREL_HI16A, 2, 16, 0x1f07ff, 16, FALSE, dont,
640 ppc_elf_unhandled_reloc),
b9c361e0
JL
641
642 /* Bits 16-31 relative to _SDA_BASE_ in split16d format. */
46807bf4
AM
643 HOW (R_PPC_VLE_SDAREL_HI16D, 2, 16, 0x3e007ff, 16, FALSE, dont,
644 ppc_elf_unhandled_reloc),
b9c361e0
JL
645
646 /* Bits 16-31 (HA) relative to _SDA_BASE split16a format. */
46807bf4
AM
647 HOW (R_PPC_VLE_SDAREL_HA16A, 2, 16, 0x1f07ff, 16, FALSE, dont,
648 ppc_elf_unhandled_reloc),
b9c361e0
JL
649
650 /* Bits 16-31 (HA) relative to _SDA_BASE split16d format. */
46807bf4
AM
651 HOW (R_PPC_VLE_SDAREL_HA16D, 2, 16, 0x3e007ff, 16, FALSE, dont,
652 ppc_elf_unhandled_reloc),
b9c361e0 653
83eef883 654 /* e_li split20 format. */
bb6bf75e
AM
655 HOW (R_PPC_VLE_ADDR20, 2, 20, 0x1f7fff, 0, FALSE, dont,
656 ppc_elf_unhandled_reloc),
46807bf4
AM
657
658 HOW (R_PPC_IRELATIVE, 2, 32, 0xffffffff, 0, FALSE, dont,
659 ppc_elf_unhandled_reloc),
e054468f 660
d7128ce4 661 /* A 16 bit relative relocation. */
46807bf4
AM
662 HOW (R_PPC_REL16, 1, 16, 0xffff, 0, TRUE, signed,
663 bfd_elf_generic_reloc),
d7128ce4
AM
664
665 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
666 HOW (R_PPC_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
667 bfd_elf_generic_reloc),
d7128ce4
AM
668
669 /* The high order 16 bits of a relative address. */
46807bf4
AM
670 HOW (R_PPC_REL16_HI, 1, 16, 0xffff, 16, TRUE, dont,
671 bfd_elf_generic_reloc),
d7128ce4
AM
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. */
46807bf4
AM
675 HOW (R_PPC_REL16_HA, 1, 16, 0xffff, 16, TRUE, dont,
676 ppc_elf_addr16_ha_reloc),
d7128ce4 677
a680de9a 678 /* Like R_PPC_REL16_HA but for split field in addpcis. */
46807bf4
AM
679 HOW (R_PPC_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
680 ppc_elf_addr16_ha_reloc),
a680de9a 681
7ba71655 682 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
683 HOW (R_PPC_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
684 ppc_elf_addr16_ha_reloc),
7ba71655 685
25dbc73a 686 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
687 HOW (R_PPC_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
688 NULL),
25dbc73a
AM
689
690 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
691 HOW (R_PPC_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
692 NULL),
7619e7c7 693
25dbc73a 694 /* Phony reloc to handle AIX style TOC entries. */
46807bf4
AM
695 HOW (R_PPC_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
696 ppc_elf_unhandled_reloc),
25dbc73a
AM
697};
698\f
699/* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
700
701static void
702ppc_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
718static reloc_howto_type *
719ppc_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;
1fe532cf 736 case BFD_RELOC_PPC64_ADDR16_DS:
25dbc73a 737 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1fe532cf 738 case BFD_RELOC_PPC64_ADDR16_LO_DS:
25dbc73a
AM
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;
1fe532cf 749 case BFD_RELOC_PPC64_GOT16_DS:
25dbc73a 750 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1fe532cf 751 case BFD_RELOC_PPC64_GOT16_LO_DS:
25dbc73a
AM
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;
1fe532cf 762 case BFD_RELOC_PPC64_PLT16_LO_DS:
25dbc73a
AM
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;
1fe532cf 767 case BFD_RELOC_PPC64_SECTOFF_DS:
25dbc73a 768 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1fe532cf 769 case BFD_RELOC_PPC64_SECTOFF_LO_DS:
25dbc73a
AM
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;
1fe532cf 774 case BFD_RELOC_PPC64_TOC16_DS:
25dbc73a
AM
775 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
776 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
727fc41e
AM
777 case BFD_RELOC_PPC_TLSGD: r = R_PPC_TLSGD; break;
778 case BFD_RELOC_PPC_TLSLD: r = R_PPC_TLSLD; break;
25dbc73a 779 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1fe532cf 780 case BFD_RELOC_PPC64_TPREL16_DS:
25dbc73a 781 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1fe532cf 782 case BFD_RELOC_PPC64_TPREL16_LO_DS:
25dbc73a
AM
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;
1fe532cf 787 case BFD_RELOC_PPC64_DTPREL16_DS:
25dbc73a 788 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1fe532cf 789 case BFD_RELOC_PPC64_DTPREL16_LO_DS:
25dbc73a
AM
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;
b9c361e0
JL
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;
d7128ce4
AM
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;
7ba71655 859 case BFD_RELOC_PPC_16DX_HA: r = R_PPC_16DX_HA; break;
a680de9a 860 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC_REL16DX_HA; break;
25dbc73a
AM
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
157090f7
AM
868static reloc_howto_type *
869ppc_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
25dbc73a
AM
884/* Set the howto pointer for a PowerPC ELF reloc. */
885
f3185997 886static bfd_boolean
4aef7643 887ppc_elf_info_to_howto (bfd *abfd,
25dbc73a
AM
888 arelent *cache_ptr,
889 Elf_Internal_Rela *dst)
890{
cd21f5da
NC
891 unsigned int r_type;
892
25dbc73a
AM
893 /* Initialize howto table if not already done. */
894 if (!ppc_elf_howto_table[R_PPC_ADDR32])
895 ppc_elf_howto_init ();
896
cd21f5da
NC
897 r_type = ELF32_R_TYPE (dst->r_info);
898 if (r_type >= R_PPC_max)
899 {
695344c0 900 /* xgettext:c-format */
0aa13fee 901 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4eca0228 902 abfd, r_type);
cd21f5da 903 bfd_set_error (bfd_error_bad_value);
f3185997 904 return FALSE;
cd21f5da 905 }
f3185997 906
cd21f5da 907 cache_ptr->howto = ppc_elf_howto_table[r_type];
375de94a
AM
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. */
f3185997 911 if (cache_ptr->howto == NULL)
375de94a 912 {
695344c0 913 /* xgettext:c-format */
0aa13fee 914 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4eca0228 915 abfd, r_type);
375de94a
AM
916 bfd_set_error (bfd_error_bad_value);
917
f3185997 918 return FALSE;
375de94a 919 }
f3185997
NC
920
921 return TRUE;
25dbc73a
AM
922}
923
d7128ce4 924/* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs. */
25dbc73a
AM
925
926static bfd_reloc_status_type
4aef7643 927ppc_elf_addr16_ha_reloc (bfd *abfd,
25dbc73a
AM
928 arelent *reloc_entry,
929 asymbol *symbol,
4aef7643 930 void *data,
25dbc73a
AM
931 asection *input_section,
932 bfd *output_bfd,
933 char **error_message ATTRIBUTE_UNUSED)
934{
a680de9a
PB
935 enum elf_ppc_reloc_type r_type;
936 long insn;
937 bfd_size_type octets;
938 bfd_vma value;
25dbc73a
AM
939
940 if (output_bfd != NULL)
941 {
942 reloc_entry->address += input_section->output_offset;
943 return bfd_reloc_ok;
944 }
945
a680de9a
PB
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
bb294208 962 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
a680de9a
PB
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;
25dbc73a
AM
968}
969
970static bfd_reloc_status_type
971ppc_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 {
7fbd5f4e
AM
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;
25dbc73a
AM
994 }
995 return bfd_reloc_dangerous;
996}
997\f
998/* Sections created by the linker. */
999
1000typedef struct elf_linker_section
1001{
c9a2f333 1002 /* Pointer to the bfd section. */
25dbc73a 1003 asection *section;
c9a2f333
AM
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;
046183de
AM
1010 /* Associated symbol. */
1011 struct elf_link_hash_entry *sym;
25dbc73a
AM
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
1018typedef 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;
25dbc73a
AM
1028} elf_linker_section_pointers_t;
1029
1030struct 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;
016687f8
AM
1037
1038 /* Flags used to auto-detect plt type. */
1039 unsigned int makes_plt_call : 1;
1040 unsigned int has_rel16 : 1;
25dbc73a
AM
1041};
1042
1043#define ppc_elf_tdata(bfd) \
1044 ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
7619e7c7 1045
25dbc73a
AM
1046#define elf_local_ptr_offsets(bfd) \
1047 (ppc_elf_tdata (bfd)->linker_section_pointers)
7619e7c7 1048
0c8d6e5c
AM
1049#define is_ppc_elf(bfd) \
1050 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1051 && elf_object_id (bfd) == PPC32_ELF_DATA)
0c8d6e5c 1052
25dbc73a 1053/* Override the generic function because we store some extras. */
7619e7c7 1054
25dbc73a
AM
1055static bfd_boolean
1056ppc_elf_mkobject (bfd *abfd)
1057{
0ffa91dd 1058 return bfd_elf_allocate_object (abfd, sizeof (struct ppc_elf_obj_tdata),
4dfe6ac6 1059 PPC32_ELF_DATA);
25dbc73a 1060}
7619e7c7 1061
14b57c7c
AM
1062/* When defaulting arch/mach, decode apuinfo to find a better match. */
1063
1064bfd_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);
62a47958
AM
1085 if (s != NULL
1086 && s->size >= 24
1087 && bfd_malloc_and_get_section (abfd, s, &contents))
14b57c7c
AM
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;
8941017b 1114 break;
14b57c7c
AM
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
25dbc73a 1142/* Fix bad default arch selected for a 32 bit input bfd when the
14b57c7c 1143 default is 64 bit. Also select arch based on apuinfo. */
7619e7c7 1144
25dbc73a
AM
1145static bfd_boolean
1146ppc_elf_object_p (bfd *abfd)
1147{
14b57c7c
AM
1148 if (!abfd->arch_info->the_default)
1149 return TRUE;
1150
1151 if (abfd->arch_info->bits_per_word == 64)
25dbc73a
AM
1152 {
1153 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
7619e7c7 1154
25dbc73a
AM
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 }
14b57c7c 1162 return _bfd_elf_ppc_set_arch (abfd);
25dbc73a 1163}
7619e7c7 1164
25dbc73a 1165/* Function to set whether a module needs the -mrelocatable bit set. */
7619e7c7 1166
25dbc73a
AM
1167static bfd_boolean
1168ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1169{
1170 BFD_ASSERT (!elf_flags_init (abfd)
1171 || elf_elfheader (abfd)->e_flags == flags);
7619e7c7 1172
25dbc73a
AM
1173 elf_elfheader (abfd)->e_flags = flags;
1174 elf_flags_init (abfd) = TRUE;
1175 return TRUE;
1176}
1177
25dbc73a 1178/* Support for core dump NOTE sections. */
deaaf2f3 1179
b34976b6 1180static bfd_boolean
25dbc73a 1181ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 1182{
25dbc73a
AM
1183 int offset;
1184 unsigned int size;
252b5132 1185
25dbc73a
AM
1186 switch (note->descsz)
1187 {
1188 default:
1189 return FALSE;
252b5132 1190
25dbc73a
AM
1191 case 268: /* Linux/PPC. */
1192 /* pr_cursig */
228e534f 1193 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
deaaf2f3 1194
25dbc73a 1195 /* pr_pid */
228e534f 1196 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
9abc968f 1197
25dbc73a
AM
1198 /* pr_reg */
1199 offset = 72;
1200 size = 192;
deaaf2f3 1201
25dbc73a
AM
1202 break;
1203 }
deaaf2f3 1204
25dbc73a
AM
1205 /* Make a ".reg/999" section. */
1206 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1207 size, note->descpos + offset);
1208}
252b5132 1209
25dbc73a
AM
1210static bfd_boolean
1211ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1212{
1213 switch (note->descsz)
deaaf2f3 1214 {
25dbc73a
AM
1215 default:
1216 return FALSE;
deaaf2f3 1217
25dbc73a 1218 case 128: /* Linux/PPC elf_prpsinfo. */
228e534f 1219 elf_tdata (abfd)->core->pid
bc989cdc 1220 = bfd_get_32 (abfd, note->descdata + 16);
228e534f 1221 elf_tdata (abfd)->core->program
25dbc73a 1222 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
228e534f 1223 elf_tdata (abfd)->core->command
25dbc73a
AM
1224 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1225 }
9abc968f 1226
25dbc73a
AM
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. */
70bccea4 1230
25dbc73a 1231 {
228e534f 1232 char *command = elf_tdata (abfd)->core->command;
25dbc73a 1233 int n = strlen (command);
70bccea4 1234
25dbc73a
AM
1235 if (0 < n && command[n - 1] == ' ')
1236 command[n - 1] = '\0';
1237 }
deaaf2f3 1238
25dbc73a
AM
1239 return TRUE;
1240}
deaaf2f3 1241
183e98be
AM
1242static char *
1243ppc_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 {
602f1657 1252 char data[128] ATTRIBUTE_NONSTRING;
183e98be
AM
1253 va_list ap;
1254
1255 va_start (ap, note_type);
75cd47ed 1256 memset (data, 0, sizeof (data));
183e98be 1257 strncpy (data + 32, va_arg (ap, const char *), 16);
be3e27bb 1258#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1259 DIAGNOSTIC_PUSH;
be3e27bb 1260 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1261 -Wstringop-truncation:
1262 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1263 */
95da9854
L
1264 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1265#endif
183e98be 1266 strncpy (data + 48, va_arg (ap, const char *), 80);
be3e27bb 1267#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1268 DIAGNOSTIC_POP;
fe75810f 1269#endif
183e98be
AM
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
b9c361e0 1299static flagword
68ffbac6 1300ppc_elf_lookup_section_flags (char *flag_name)
b9c361e0
JL
1301{
1302
1303 if (!strcmp (flag_name, "SHF_PPC_VLE"))
1304 return SHF_PPC_VLE;
1305
1306 return 0;
1307}
1308
25dbc73a
AM
1309/* Return address for Ith PLT stub in section PLT, for relocation REL
1310 or (bfd_vma) -1 if it should not be included. */
deaaf2f3 1311
25dbc73a
AM
1312static bfd_vma
1313ppc_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}
deaaf2f3 1319
25dbc73a 1320/* Handle a PowerPC specific section when reading an object file. This
6dc132d9
L
1321 is called when bfd_section_from_shdr finds a section with an unknown
1322 type. */
deaaf2f3 1323
25dbc73a 1324static bfd_boolean
6dc132d9
L
1325ppc_elf_section_from_shdr (bfd *abfd,
1326 Elf_Internal_Shdr *hdr,
1327 const char *name,
1328 int shindex)
25dbc73a
AM
1329{
1330 asection *newsect;
1331 flagword flags;
d1c6de6f 1332
6dc132d9 1333 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
25dbc73a 1334 return FALSE;
deaaf2f3 1335
25dbc73a 1336 newsect = hdr->bfd_section;
bf577467 1337 flags = 0;
25dbc73a
AM
1338 if (hdr->sh_flags & SHF_EXCLUDE)
1339 flags |= SEC_EXCLUDE;
9abc968f 1340
25dbc73a
AM
1341 if (hdr->sh_type == SHT_ORDERED)
1342 flags |= SEC_SORT_ENTRIES;
d1c6de6f 1343
bf577467
AM
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));
25dbc73a
AM
1352}
1353
1354/* Set up any other section flags and such that may be necessary. */
1355
1356static bfd_boolean
1357ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1358 Elf_Internal_Shdr *shdr,
1359 asection *asect)
1360{
25dbc73a
AM
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
1370static int
a6b96beb
AM
1371ppc_elf_additional_program_headers (bfd *abfd,
1372 struct bfd_link_info *info ATTRIBUTE_UNUSED)
25dbc73a
AM
1373{
1374 asection *s;
1375 int ret = 0;
d1c6de6f 1376
25dbc73a
AM
1377 s = bfd_get_section_by_name (abfd, ".sbss2");
1378 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1379 ++ret;
d1c6de6f 1380
25dbc73a
AM
1381 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1382 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1383 ++ret;
deaaf2f3 1384
25dbc73a
AM
1385 return ret;
1386}
deaaf2f3 1387
68ffbac6 1388/* Modify the segment map for VLE executables. */
b9c361e0
JL
1389
1390bfd_boolean
1391ppc_elf_modify_segment_map (bfd *abfd,
1392 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1393{
f7d69005 1394 struct elf_segment_map *m;
b9c361e0
JL
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
12bd6957 1402 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b9c361e0 1403 {
f7d69005 1404 struct elf_segment_map *n;
986f0783 1405 size_t amt;
f7d69005
AM
1406 unsigned int j, k;
1407 unsigned int p_flags;
1408
1409 if (m->p_type != PT_LOAD || m->count == 0)
b9c361e0
JL
1410 continue;
1411
f7d69005 1412 for (p_flags = PF_R, j = 0; j != m->count; ++j)
b9c361e0 1413 {
f7d69005
AM
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;
94caa966 1428
f7d69005
AM
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)
b9c361e0
JL
1451 continue;
1452
f7d69005 1453 /* Sections 0..j-1 stay in this (current) segment,
b9c361e0
JL
1454 the remainder are put in a new segment.
1455 The scan resumes with the new segment. */
1456
b9c361e0
JL
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)
954b63d4 1461 return FALSE;
b9c361e0
JL
1462
1463 n->p_type = PT_LOAD;
b9c361e0
JL
1464 n->count = m->count - j;
1465 for (k = 0; k < n->count; ++k)
f7d69005
AM
1466 n->sections[k] = m->sections[j + k];
1467 m->count = j;
1468 m->p_size_valid = 0;
b9c361e0
JL
1469 n->next = m->next;
1470 m->next = n;
b9c361e0
JL
1471 }
1472
1473 return TRUE;
1474}
1475
25dbc73a
AM
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. */
e656e369 1480
b35d266b 1481static const struct bfd_elf_special_section ppc_elf_special_sections[] =
7f4d3958 1482{
14b57c7c
AM
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 }
7f4d3958
L
1493};
1494
551b43fd 1495/* This is what we want for new plt/got. */
bd38246a 1496static const struct bfd_elf_special_section ppc_alt_plt =
07d6d2b8 1497 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC };
551b43fd
AM
1498
1499static const struct bfd_elf_special_section *
4aef7643 1500ppc_elf_get_sec_type_attr (bfd *abfd, asection *sec)
7f4d3958 1501{
b35d266b 1502 const struct bfd_elf_special_section *ssect;
551b43fd
AM
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}
25dbc73a
AM
1519\f
1520/* Very simple linked list structure for recording apuinfo values. */
1521typedef struct apuinfo_list
1522{
1523 struct apuinfo_list *next;
1524 unsigned long value;
1525}
1526apuinfo_list;
e656e369 1527
25dbc73a 1528static apuinfo_list *head;
f2faa800 1529static bfd_boolean apuinfo_set;
deaaf2f3 1530
25dbc73a
AM
1531static void
1532apuinfo_list_init (void)
1533{
1534 head = NULL;
f2faa800 1535 apuinfo_set = FALSE;
25dbc73a 1536}
9abc968f 1537
25dbc73a
AM
1538static void
1539apuinfo_list_add (unsigned long value)
1540{
1541 apuinfo_list *entry = head;
deaaf2f3 1542
25dbc73a
AM
1543 while (entry != NULL)
1544 {
1545 if (entry->value == value)
1546 return;
1547 entry = entry->next;
1548 }
b4a38de6 1549
25dbc73a
AM
1550 entry = bfd_malloc (sizeof (* entry));
1551 if (entry == NULL)
1552 return;
e656e369 1553
25dbc73a
AM
1554 entry->value = value;
1555 entry->next = head;
1556 head = entry;
1557}
9abc968f 1558
25dbc73a
AM
1559static unsigned
1560apuinfo_list_length (void)
1561{
1562 apuinfo_list *entry;
1563 unsigned long count;
9abc968f 1564
25dbc73a
AM
1565 for (entry = head, count = 0;
1566 entry;
1567 entry = entry->next)
1568 ++ count;
e656e369 1569
25dbc73a
AM
1570 return count;
1571}
eea6121a 1572
25dbc73a
AM
1573static inline unsigned long
1574apuinfo_list_element (unsigned long number)
1575{
1576 apuinfo_list * entry;
e656e369 1577
25dbc73a
AM
1578 for (entry = head;
1579 entry && number --;
1580 entry = entry->next)
1581 ;
252b5132 1582
25dbc73a
AM
1583 return entry ? entry->value : 0;
1584}
1585
1586static void
1587apuinfo_list_finish (void)
1588{
1589 apuinfo_list *entry;
1590
1591 for (entry = head; entry;)
252b5132 1592 {
25dbc73a
AM
1593 apuinfo_list *next = entry->next;
1594 free (entry);
1595 entry = next;
1596 }
9abc968f 1597
25dbc73a
AM
1598 head = NULL;
1599}
9abc968f 1600
25dbc73a
AM
1601/* Scan the input BFDs and create a linked list of
1602 the APUinfo values that will need to be emitted. */
9abc968f 1603
25dbc73a
AM
1604static void
1605ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
1606{
1607 bfd *ibfd;
1608 asection *asec;
deddc40b
NS
1609 char *buffer = NULL;
1610 bfd_size_type largest_input_size = 0;
25dbc73a 1611 unsigned i;
25dbc73a
AM
1612 unsigned long length;
1613 const char *error_message = NULL;
9abc968f 1614
25dbc73a
AM
1615 if (link_info == NULL)
1616 return;
9abc968f 1617
25dbc73a 1618 apuinfo_list_init ();
252b5132 1619
25dbc73a 1620 /* Read in the input sections contents. */
c72f2fb2 1621 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link.next)
252b5132 1622 {
25dbc73a 1623 unsigned long datum;
252b5132 1624
25dbc73a
AM
1625 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1626 if (asec == NULL)
1627 continue;
252b5132 1628
695344c0 1629 /* xgettext:c-format */
871b3ab2 1630 error_message = _("corrupt %s section in %pB");
25dbc73a 1631 length = asec->size;
deddc40b
NS
1632 if (length < 20)
1633 goto fail;
1634
f2faa800 1635 apuinfo_set = TRUE;
deddc40b 1636 if (largest_input_size < asec->size)
25dbc73a 1637 {
c9594989 1638 free (buffer);
deddc40b
NS
1639 largest_input_size = asec->size;
1640 buffer = bfd_malloc (largest_input_size);
1641 if (!buffer)
1642 return;
25dbc73a 1643 }
5b914448 1644
25dbc73a 1645 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
deddc40b 1646 || (bfd_bread (buffer, length, ibfd) != length))
25dbc73a 1647 {
695344c0 1648 /* xgettext:c-format */
871b3ab2 1649 error_message = _("unable to read in %s section from %pB");
25dbc73a
AM
1650 goto fail;
1651 }
252b5132 1652
25dbc73a
AM
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. */
deddc40b 1656 datum = bfd_get_32 (ibfd, buffer);
25dbc73a
AM
1657 if (datum != sizeof APUINFO_LABEL)
1658 goto fail;
252b5132 1659
deddc40b 1660 datum = bfd_get_32 (ibfd, buffer + 8);
25dbc73a
AM
1661 if (datum != 0x2)
1662 goto fail;
252b5132 1663
deddc40b 1664 if (strcmp (buffer + 12, APUINFO_LABEL) != 0)
25dbc73a 1665 goto fail;
252b5132 1666
25dbc73a 1667 /* Get the number of bytes used for apuinfo entries. */
deddc40b 1668 datum = bfd_get_32 (ibfd, buffer + 4);
25dbc73a
AM
1669 if (datum + 20 != length)
1670 goto fail;
1671
25dbc73a
AM
1672 /* Scan the apuinfo section, building a list of apuinfo numbers. */
1673 for (i = 0; i < datum; i += 4)
deddc40b 1674 apuinfo_list_add (bfd_get_32 (ibfd, buffer + 20 + i));
252b5132
RH
1675 }
1676
25dbc73a 1677 error_message = NULL;
252b5132 1678
f2faa800 1679 if (apuinfo_set)
deddc40b 1680 {
f2faa800
NS
1681 /* Compute the size of the output section. */
1682 unsigned num_entries = apuinfo_list_length ();
5b914448 1683
deddc40b
NS
1684 /* Set the output section size, if it exists. */
1685 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
f2faa800 1686
fd361982 1687 if (asec && !bfd_set_section_size (asec, 20 + num_entries * 4))
deddc40b
NS
1688 {
1689 ibfd = abfd;
695344c0 1690 /* xgettext:c-format */
871b3ab2 1691 error_message = _("warning: unable to set size of %s section in %pB");
deddc40b
NS
1692 }
1693 }
25dbc73a
AM
1694
1695 fail:
c9594989 1696 free (buffer);
25dbc73a
AM
1697
1698 if (error_message)
cd9af601 1699 _bfd_error_handler (error_message, APUINFO_SECTION_NAME, ibfd);
25dbc73a 1700}
252b5132 1701
25dbc73a
AM
1702/* Prevent the output section from accumulating the input sections'
1703 contents. We have already stored this in our linked list structure. */
252b5132 1704
25dbc73a
AM
1705static bfd_boolean
1706ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
c7b8f16e 1707 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
25dbc73a
AM
1708 asection *asec,
1709 bfd_byte *contents ATTRIBUTE_UNUSED)
1710{
f2faa800 1711 return apuinfo_set && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
252b5132
RH
1712}
1713
25dbc73a
AM
1714/* Finally we can generate the output section. */
1715
1716static void
cc364be6 1717ppc_final_write_processing (bfd *abfd)
7619e7c7 1718{
25dbc73a
AM
1719 bfd_byte *buffer;
1720 asection *asec;
1721 unsigned i;
1722 unsigned num_entries;
1723 bfd_size_type length;
7619e7c7 1724
25dbc73a
AM
1725 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1726 if (asec == NULL)
1727 return;
1728
f2faa800 1729 if (!apuinfo_set)
25dbc73a
AM
1730 return;
1731
1732 length = asec->size;
1733 if (length < 20)
1734 return;
1735
1736 buffer = bfd_malloc (length);
1737 if (buffer == NULL)
7619e7c7 1738 {
4eca0228 1739 _bfd_error_handler
cf97bcb0 1740 (_("failed to allocate space for new APUinfo section"));
25dbc73a 1741 return;
7619e7c7 1742 }
7619e7c7 1743
25dbc73a
AM
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);
feee612b 1750
25dbc73a
AM
1751 length = 20;
1752 for (i = 0; i < num_entries; i++)
feee612b 1753 {
25dbc73a
AM
1754 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
1755 length += 4;
feee612b 1756 }
feee612b 1757
25dbc73a 1758 if (length != asec->size)
cf97bcb0 1759 _bfd_error_handler (_("failed to compute new APUinfo section"));
252b5132 1760
25dbc73a 1761 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
cf97bcb0 1762 _bfd_error_handler (_("failed to install new APUinfo section"));
252b5132 1763
25dbc73a
AM
1764 free (buffer);
1765
1766 apuinfo_list_finish ();
252b5132 1767}
06f44071 1768
cc364be6
AM
1769static bfd_boolean
1770ppc_elf_final_write_processing (bfd *abfd)
06f44071 1771{
cc364be6
AM
1772 ppc_final_write_processing (abfd);
1773 return _bfd_elf_final_write_processing (abfd);
06f44071 1774}
25dbc73a 1775\f
7382d32a 1776static bfd_boolean
c6dd29ce 1777is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
7382d32a 1778{
407aa07c 1779 bfd_byte buf[4 * 4];
7382d32a 1780
9e390558 1781 if (!bfd_get_section_contents (abfd, glink, buf, off, sizeof buf))
7382d32a
AM
1782 return FALSE;
1783
c6dd29ce
AM
1784 return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
1785 && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
407aa07c
AM
1786 && bfd_get_32 (abfd, buf + 8) == MTCTR_11
1787 && bfd_get_32 (abfd, buf + 12) == BCTR);
7382d32a
AM
1788}
1789
468392fb
AM
1790static bfd_boolean
1791section_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
1799static long
1800ppc_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;
9e390558 1808 bfd_vma stub_off;
468392fb
AM
1809 asymbol *s;
1810 arelent *p;
9ad9b810 1811 size_t count, i, stub_delta;
468392fb
AM
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
7382d32a 1918 count = relplt->size / sizeof (Elf32_External_Rela);
7382d32a
AM
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
9e390558
AM
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)
7382d32a
AM
1930 return 0;
1931
468392fb
AM
1932 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
1933 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
1934 return -1;
1935
468392fb
AM
1936 size = count * sizeof (asymbol);
1937 p = relplt->relocation;
1938 for (i = 0; i < count; i++, p++)
e054468f
AM
1939 {
1940 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
1941 if (p->addend != 0)
1942 size += sizeof ("+0x") - 1 + 8;
1943 }
468392fb
AM
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
9e390558 1954 stub_off = glink_vma - glink->vma;
468392fb 1955 names = (char *) (s + count + 1 + (resolv_vma != 0));
1079403c
AM
1956 p = relplt->relocation + count - 1;
1957 for (i = 0; i < count; i++)
468392fb
AM
1958 {
1959 size_t len;
1960
9e390558
AM
1961 stub_off -= stub_delta;
1962 if (strcmp ((*p->sym_ptr_ptr)->name, "__tls_get_addr_opt") == 0)
1963 stub_off -= 32;
468392fb
AM
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;
6ba2a415 1969 s->flags |= BSF_SYNTHETIC;
468392fb 1970 s->section = glink;
9e390558 1971 s->value = stub_off;
468392fb
AM
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;
e054468f
AM
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 }
468392fb
AM
1984 memcpy (names, "@plt", sizeof ("@plt"));
1985 names += sizeof ("@plt");
1986 ++s;
1079403c 1987 --p;
468392fb
AM
1988 }
1989
1990 /* Add a symbol at the start of the glink branch table. */
86a4952b 1991 memset (s, 0, sizeof *s);
468392fb 1992 s->the_bfd = abfd;
6ba2a415 1993 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
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. */
86a4952b 2005 memset (s, 0, sizeof *s);
468392fb 2006 s->the_bfd = abfd;
6ba2a415 2007 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
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
25dbc73a
AM
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. */
252b5132 2026
a6aa5195 2027/* Track PLT entries needed for a given symbol. We might need more
a877a2b6 2028 than one glink entry per symbol when generating a pic binary. */
a6aa5195
AM
2029struct 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
a877a2b6
AM
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). */
a6aa5195
AM
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
7c8bbca5
AM
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. */
252b5132 2057
1d483afe
AM
2058static int
2059must_be_dyn_reloc (struct bfd_link_info *info,
2060 enum elf_ppc_reloc_type r_type)
2061{
2062 switch (r_type)
2063 {
2064 default:
7c8bbca5
AM
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. */
1d483afe
AM
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:
7c8bbca5
AM
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);
1d483afe
AM
2086 }
2087}
252b5132 2088
25dbc73a
AM
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
252b5132 2094
f95f8542
AM
2095/* Used to track dynamic relocations for local symbols. */
2096struct 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
25dbc73a 2110/* PPC ELF linker hash entry. */
252b5132 2111
25dbc73a
AM
2112struct ppc_elf_link_hash_entry
2113{
2114 struct elf_link_hash_entry elf;
252b5132 2115
25dbc73a
AM
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;
252b5132 2120
37da22e5
AM
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
b00a0a86 2127 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
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. */
b00a0a86 2135#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
bac3c8c5 2136 unsigned char tls_mask;
4dc4a9a5 2137
37da22e5
AM
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. */
2d7ad24e 2141#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5
AM
2142#define NON_GOT 256 /* local symbol plt, not stored. */
2143
4dc4a9a5
DJ
2144 /* Nonzero if we have seen a small data relocation referring to this
2145 symbol. */
d3e454b9
AM
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;
25dbc73a 2151};
252b5132 2152
25dbc73a
AM
2153#define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2154
2155/* PPC ELF linker hash table. */
2156
2157struct ppc_elf_link_hash_table
2158{
2159 struct elf_link_hash_table elf;
2160
5446cbdf
AM
2161 /* Various options passed from the linker. */
2162 struct ppc_elf_params *params;
2163
25dbc73a 2164 /* Short-cuts to get to dynamic linker sections. */
d7128ce4 2165 asection *glink;
25dbc73a
AM
2166 asection *dynsbss;
2167 asection *relsbss;
c9a2f333 2168 elf_linker_section_t sdata[2];
25dbc73a 2169 asection *sbss;
6177242a 2170 asection *glink_eh_frame;
2d7ad24e
AM
2171 asection *pltlocal;
2172 asection *relpltlocal;
25dbc73a 2173
016687f8
AM
2174 /* The (unloaded but important) .rela.plt.unloaded on VxWorks. */
2175 asection *srelplt2;
2176
1d483afe 2177 /* Shortcut to __tls_get_addr. */
25dbc73a 2178 struct elf_link_hash_entry *tls_get_addr;
252b5132 2179
016687f8
AM
2180 /* The bfd that forced an old-style PLT. */
2181 bfd *old_bfd;
5b914448 2182
25dbc73a
AM
2183 /* TLS local dynamic got entry handling. */
2184 union {
2185 bfd_signed_vma refcount;
2186 bfd_vma offset;
2187 } tlsld_got;
252b5132 2188
c6dd29ce 2189 /* Offset of branch table to PltResolve function in glink. */
d7128ce4
AM
2190 bfd_vma glink_pltresolve;
2191
3b36f7e6
AM
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
4a3dc543
RS
2197 /* The type of PLT we have chosen to use. */
2198 enum ppc_elf_plt_type plt_type;
2199
82e66161
AM
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
9a23f96e
AM
2205 /* Set if tls optimization is enabled. */
2206 unsigned int do_tls_opt:1;
2207
3e04d765
AM
2208 /* Set if inline plt calls should be converted to direct calls. */
2209 unsigned int can_convert_all_inline_plt:1;
2210
9d8504b1
PB
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;
25dbc73a 2217};
252b5132 2218
94caa966
AM
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
9737e8af
AM
2225/* Nonzero if this section has a call to __tls_get_addr lacking marker
2226 relocs. */
2227#define nomark_tls_get_addr sec_flg1
94caa966 2228
3e04d765
AM
2229 /* Flag set when PLTCALL relocs are detected. */
2230#define has_pltcall sec_flg2
2231
25dbc73a 2232/* Get the PPC ELF linker hash table from a link_info structure. */
252b5132 2233
25dbc73a 2234#define ppc_elf_hash_table(p) \
a8d549c0
AM
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)
252b5132 2238
25dbc73a 2239/* Create an entry in a PPC ELF linker hash table. */
252b5132 2240
25dbc73a
AM
2241static struct bfd_hash_entry *
2242ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2243 struct bfd_hash_table *table,
2244 const char *string)
252b5132 2245{
25dbc73a
AM
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 }
252b5132 2255
25dbc73a
AM
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;
25dbc73a 2261 ppc_elf_hash_entry (entry)->tls_mask = 0;
5240d94d 2262 ppc_elf_hash_entry (entry)->has_sda_refs = 0;
25dbc73a 2263 }
252b5132 2264
25dbc73a 2265 return entry;
252b5132 2266}
3dab13f6 2267
25dbc73a 2268/* Create a PPC ELF linker hash table. */
3dab13f6 2269
25dbc73a
AM
2270static struct bfd_link_hash_table *
2271ppc_elf_link_hash_table_create (bfd *abfd)
3dab13f6 2272{
25dbc73a 2273 struct ppc_elf_link_hash_table *ret;
76e7a751 2274 static struct ppc_elf_params default_params
407aa07c 2275 = { PLT_OLD, 0, 0, 1, 0, 0, 12, 0, 0, 0 };
3dab13f6 2276
25dbc73a
AM
2277 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2278 if (ret == NULL)
2279 return NULL;
3dab13f6 2280
66eb6687
AM
2281 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
2282 ppc_elf_link_hash_newfunc,
4dfe6ac6
NC
2283 sizeof (struct ppc_elf_link_hash_entry),
2284 PPC32_ELF_DATA))
3dab13f6 2285 {
25dbc73a
AM
2286 free (ret);
2287 return NULL;
2288 }
58111eb7 2289
a6aa5195
AM
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
db434ba0
AM
2295 ret->params = &default_params;
2296
c9a2f333
AM
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
9d8504b1
PB
2305 ret->plt_entry_size = 12;
2306 ret->plt_slot_size = 8;
2307 ret->plt_initial_entry_size = 72;
9d8504b1 2308
25dbc73a
AM
2309 return &ret->elf.root;
2310}
3dab13f6 2311
5446cbdf
AM
2312/* Hook linker params into hash table. */
2313
2314void
2315ppc_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;
76e7a751 2321 params->pagesize_p2 = bfd_log2 (params->pagesize);
5446cbdf
AM
2322}
2323
9d8504b1 2324/* Create .got and the related sections. */
3dab13f6 2325
25dbc73a
AM
2326static bfd_boolean
2327ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2328{
2329 struct ppc_elf_link_hash_table *htab;
3dab13f6 2330
25dbc73a
AM
2331 if (!_bfd_elf_create_got_section (abfd, info))
2332 return FALSE;
3dab13f6 2333
25dbc73a 2334 htab = ppc_elf_hash_table (info);
90c14f0c 2335 if (htab->elf.target_os != is_vxworks)
9d8504b1
PB
2336 {
2337 /* The powerpc .got has a blrl instruction in it. Mark it
2338 executable. */
ce558b89
AM
2339 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
2340 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
fd361982 2341 if (!bfd_set_section_flags (htab->elf.sgot, flags))
9d8504b1
PB
2342 return FALSE;
2343 }
3dab13f6 2344
25dbc73a
AM
2345 return TRUE;
2346}
3dab13f6 2347
93d1b056
AM
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
2352static bfd_boolean
2353ppc_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
e054468f
AM
2378static bfd_boolean
2379ppc_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;
691d2e9a 2384 int p2align;
e054468f
AM
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;
691d2e9a
AM
2390 p2align = htab->params->ppc476_workaround ? 6 : 4;
2391 if (p2align < htab->params->plt_stub_align)
2392 p2align = htab->params->plt_stub_align;
e054468f 2393 if (s == NULL
fd361982 2394 || !bfd_set_section_alignment (s, p2align))
e054468f
AM
2395 return FALSE;
2396
6177242a
AM
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
fd361982 2404 || !bfd_set_section_alignment (s, 2))
6177242a
AM
2405 return FALSE;
2406 }
2407
e054468f
AM
2408 flags = SEC_ALLOC | SEC_LINKER_CREATED;
2409 s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
ce558b89 2410 htab->elf.iplt = s;
e054468f 2411 if (s == NULL
fd361982 2412 || !bfd_set_section_alignment (s, 4))
e054468f
AM
2413 return FALSE;
2414
2415 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2416 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14b2f831 2417 s = bfd_make_section_anyway_with_flags (abfd, ".rela.iplt", flags);
ce558b89 2418 htab->elf.irelplt = s;
e054468f 2419 if (s == NULL
fd361982 2420 || ! bfd_set_section_alignment (s, 2))
e054468f 2421 return FALSE;
93d1b056 2422
2d7ad24e
AM
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
fd361982 2429 || !bfd_set_section_alignment (htab->pltlocal, 2))
2d7ad24e
AM
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
fd361982 2439 || !bfd_set_section_alignment (htab->relpltlocal, 2))
2d7ad24e
AM
2440 return FALSE;
2441 }
2442
93d1b056
AM
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
e054468f
AM
2451 return TRUE;
2452}
2453
25dbc73a
AM
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). */
3dab13f6 2457
25dbc73a
AM
2458static bfd_boolean
2459ppc_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;
3dab13f6 2464
25dbc73a 2465 htab = ppc_elf_hash_table (info);
3dab13f6 2466
ce558b89 2467 if (htab->elf.sgot == NULL
25dbc73a 2468 && !ppc_elf_create_got (abfd, info))
3dab13f6
AM
2469 return FALSE;
2470
25dbc73a
AM
2471 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2472 return FALSE;
3dab13f6 2473
e054468f
AM
2474 if (htab->glink == NULL
2475 && !ppc_elf_create_glink (abfd, info))
d7128ce4 2476 return FALSE;
3dab13f6 2477
14b2f831
AM
2478 s = bfd_make_section_anyway_with_flags (abfd, ".dynsbss",
2479 SEC_ALLOC | SEC_LINKER_CREATED);
3b36f7e6 2480 htab->dynsbss = s;
3496cb2a 2481 if (s == NULL)
25dbc73a 2482 return FALSE;
3dab13f6 2483
0e1862bb 2484 if (! bfd_link_pic (info))
25dbc73a 2485 {
e054468f
AM
2486 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2487 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14b2f831 2488 s = bfd_make_section_anyway_with_flags (abfd, ".rela.sbss", flags);
3b36f7e6 2489 htab->relsbss = s;
25dbc73a 2490 if (s == NULL
fd361982 2491 || !bfd_set_section_alignment (s, 2))
25dbc73a
AM
2492 return FALSE;
2493 }
3dab13f6 2494
90c14f0c 2495 if (htab->elf.target_os == is_vxworks
711de32c
RS
2496 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2497 return FALSE;
9d8504b1 2498
ce558b89 2499 s = htab->elf.splt;
3b36f7e6 2500 flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
4a3dc543
RS
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;
fd361982 2504 return bfd_set_section_flags (s, flags);
25dbc73a 2505}
3dab13f6 2506
25dbc73a 2507/* Copy the extra info we tack onto an elf_link_hash_entry. */
58111eb7 2508
25dbc73a 2509static void
fcfa13d2 2510ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
25dbc73a
AM
2511 struct elf_link_hash_entry *dir,
2512 struct elf_link_hash_entry *ind)
2513{
2514 struct ppc_elf_link_hash_entry *edir, *eind;
3dab13f6 2515
25dbc73a
AM
2516 edir = (struct ppc_elf_link_hash_entry *) dir;
2517 eind = (struct ppc_elf_link_hash_entry *) ind;
3dab13f6 2518
c79d6685
AM
2519 edir->tls_mask |= eind->tls_mask;
2520 edir->has_sda_refs |= eind->has_sda_refs;
2521
e81830c5
AM
2522 if (edir->elf.versioned != versioned_hidden)
2523 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
c79d6685
AM
2524 edir->elf.ref_regular |= eind->elf.ref_regular;
2525 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 2526 edir->elf.non_got_ref |= eind->elf.non_got_ref;
c79d6685
AM
2527 edir->elf.needs_plt |= eind->elf.needs_plt;
2528 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
2529
287c7eaf
AM
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
190eb1dd 2534 if (ind->dyn_relocs != NULL)
25dbc73a 2535 {
190eb1dd 2536 if (dir->dyn_relocs != NULL)
3dab13f6 2537 {
6061a67d
AM
2538 struct elf_dyn_relocs **pp;
2539 struct elf_dyn_relocs *p;
3dab13f6 2540
fcfa13d2 2541 /* Add reloc counts against the indirect sym to the direct sym
25dbc73a 2542 list. Merge any entries against the same section. */
190eb1dd 2543 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
3dab13f6 2544 {
6061a67d 2545 struct elf_dyn_relocs *q;
25dbc73a 2546
190eb1dd 2547 for (q = dir->dyn_relocs; q != NULL; q = q->next)
25dbc73a
AM
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;
3dab13f6 2557 }
190eb1dd 2558 *pp = dir->dyn_relocs;
3dab13f6 2559 }
25dbc73a 2560
190eb1dd
L
2561 dir->dyn_relocs = ind->dyn_relocs;
2562 ind->dyn_relocs = NULL;
3dab13f6 2563 }
3dab13f6 2564
a6aa5195
AM
2565 /* Copy over the GOT refcount entries that we may have already seen to
2566 the symbol which just became indirect. */
fcfa13d2
AM
2567 edir->elf.got.refcount += eind->elf.got.refcount;
2568 eind->elf.got.refcount = 0;
a6aa5195
AM
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
fcfa13d2 2599 if (eind->elf.dynindx != -1)
25dbc73a 2600 {
fcfa13d2
AM
2601 if (edir->elf.dynindx != -1)
2602 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2603 edir->elf.dynstr_index);
a6aa5195
AM
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;
25dbc73a 2608 }
25dbc73a 2609}
3dab13f6 2610
25dbc73a
AM
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. */
58111eb7 2613
25dbc73a
AM
2614static bfd_boolean
2615ppc_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
0e1862bb 2624 && !bfd_link_relocatable (info)
0c8d6e5c
AM
2625 && is_ppc_elf (info->output_bfd)
2626 && sym->st_size <= elf_gp_size (abfd))
25dbc73a
AM
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;
3dab13f6 2631
25dbc73a
AM
2632 htab = ppc_elf_hash_table (info);
2633 if (htab->sbss == NULL)
2634 {
10885e24 2635 flagword flags = SEC_IS_COMMON | SEC_SMALL_DATA | SEC_LINKER_CREATED;
3dab13f6 2636
644285ef
AM
2637 if (!htab->elf.dynobj)
2638 htab->elf.dynobj = abfd;
2639
3496cb2a
L
2640 htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2641 ".sbss",
2642 flags);
2643 if (htab->sbss == NULL)
25dbc73a 2644 return FALSE;
3dab13f6 2645 }
3dab13f6 2646
25dbc73a
AM
2647 *secp = htab->sbss;
2648 *valp = sym->st_size;
2649 }
3dab13f6 2650
25dbc73a 2651 return TRUE;
3dab13f6
AM
2652}
2653\f
25dbc73a
AM
2654/* Find a linker generated pointer with a given addend and type. */
2655
2656static elf_linker_section_pointers_t *
2657elf_find_pointer_linker_section
2658 (elf_linker_section_pointers_t *linker_pointers,
2659 bfd_vma addend,
2660 elf_linker_section_t *lsect)
252b5132 2661{
25dbc73a
AM
2662 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2663 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2664 return linker_pointers;
252b5132 2665
25dbc73a
AM
2666 return NULL;
2667}
2668
2669/* Allocate a pointer to live in a linker created section. */
2670
2671static bfd_boolean
93d1b056
AM
2672elf_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)
25dbc73a
AM
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;
25dbc73a
AM
2697 }
2698 else
2699 {
0c8d6e5c 2700 BFD_ASSERT (is_ppc_elf (abfd));
0ffa91dd 2701
25dbc73a
AM
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 {
0ffa91dd 2708 unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
25dbc73a
AM
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;
252b5132 2725
25dbc73a 2726 ptr_linker_section_ptr = &ptr[r_symndx];
25dbc73a 2727 }
41fcb14e 2728
25dbc73a
AM
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);
41fcb14e 2734
25dbc73a 2735 if (!linker_section_ptr)
7619e7c7 2736 return FALSE;
41fcb14e 2737
25dbc73a
AM
2738 linker_section_ptr->next = *ptr_linker_section_ptr;
2739 linker_section_ptr->addend = rel->r_addend;
2740 linker_section_ptr->lsect = lsect;
25dbc73a 2741 *ptr_linker_section_ptr = linker_section_ptr;
41fcb14e 2742
fd361982 2743 if (!bfd_set_section_alignment (lsect->section, 2))
93d1b056 2744 return FALSE;
25dbc73a
AM
2745 linker_section_ptr->offset = lsect->section->size;
2746 lsect->section->size += 4;
7619e7c7 2747
25dbc73a
AM
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
7619e7c7
AM
2754
2755 return TRUE;
41fcb14e
AM
2756}
2757
e054468f 2758static struct plt_entry **
25dbc73a
AM
2759update_local_sym_info (bfd *abfd,
2760 Elf_Internal_Shdr *symtab_hdr,
2761 unsigned long r_symndx,
2762 int tls_type)
252b5132 2763{
25dbc73a 2764 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
e054468f 2765 struct plt_entry **local_plt;
bac3c8c5 2766 unsigned char *local_got_tls_masks;
252b5132 2767
25dbc73a 2768 if (local_got_refcounts == NULL)
252b5132 2769 {
25dbc73a
AM
2770 bfd_size_type size = symtab_hdr->sh_info;
2771
e054468f
AM
2772 size *= (sizeof (*local_got_refcounts)
2773 + sizeof (*local_plt)
2774 + sizeof (*local_got_tls_masks));
25dbc73a
AM
2775 local_got_refcounts = bfd_zalloc (abfd, size);
2776 if (local_got_refcounts == NULL)
e054468f 2777 return NULL;
25dbc73a 2778 elf_local_got_refcounts (abfd) = local_got_refcounts;
252b5132 2779 }
41fcb14e 2780
e054468f 2781 local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
bac3c8c5 2782 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5
AM
2783 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
2784 if ((tls_type & NON_GOT) == 0)
e054468f
AM
2785 local_got_refcounts[r_symndx] += 1;
2786 return local_plt + r_symndx;
25dbc73a 2787}
41fcb14e 2788
a6aa5195 2789static bfd_boolean
3ec01793 2790update_plt_info (bfd *abfd, struct plt_entry **plist,
a6aa5195
AM
2791 asection *sec, bfd_vma addend)
2792{
2793 struct plt_entry *ent;
2794
32af9f6e
AM
2795 if (addend < 32768)
2796 sec = NULL;
3ec01793 2797 for (ent = *plist; ent != NULL; ent = ent->next)
a6aa5195
AM
2798 if (ent->sec == sec && ent->addend == addend)
2799 break;
2800 if (ent == NULL)
2801 {
986f0783 2802 size_t amt = sizeof (*ent);
a6aa5195
AM
2803 ent = bfd_alloc (abfd, amt);
2804 if (ent == NULL)
2805 return FALSE;
3ec01793 2806 ent->next = *plist;
a6aa5195
AM
2807 ent->sec = sec;
2808 ent->addend = addend;
2809 ent->plt.refcount = 0;
3ec01793 2810 *plist = ent;
a6aa5195
AM
2811 }
2812 ent->plt.refcount += 1;
2813 return TRUE;
2814}
2815
2816static struct plt_entry *
3ec01793 2817find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
a6aa5195
AM
2818{
2819 struct plt_entry *ent;
2820
2821 if (addend < 32768)
2822 sec = NULL;
3ec01793 2823 for (ent = *plist; ent != NULL; ent = ent->next)
a6aa5195
AM
2824 if (ent->sec == sec && ent->addend == addend)
2825 break;
2826 return ent;
2827}
2828
e054468f
AM
2829static bfd_boolean
2830is_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
dfdaec14
AJ
2841 || r_type == R_PPC_ADDR14_BRNTAKEN
2842 || r_type == R_PPC_VLE_REL24);
e054468f
AM
2843}
2844
23cedd1d
AM
2845/* Relocs on inline plt call sequence insns prior to the call. */
2846
2847static bfd_boolean
2848is_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
25dbc73a
AM
2856static void
2857bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
2858{
4eca0228 2859 _bfd_error_handler
695344c0 2860 /* xgettext:c-format */
871b3ab2 2861 (_("%pB: relocation %s cannot be used when making a shared object"),
25dbc73a
AM
2862 abfd,
2863 ppc_elf_howto_table[r_type]->name);
2864 bfd_set_error (bfd_error_bad_value);
252b5132
RH
2865}
2866
25dbc73a
AM
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. */
252b5132 2870
b34976b6 2871static bfd_boolean
25dbc73a
AM
2872ppc_elf_check_relocs (bfd *abfd,
2873 struct bfd_link_info *info,
2874 asection *sec,
2875 const Elf_Internal_Rela *relocs)
252b5132 2876{
7619e7c7 2877 struct ppc_elf_link_hash_table *htab;
25dbc73a
AM
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;
a6aa5195 2882 asection *got2, *sreloc;
727fc41e 2883 struct elf_link_hash_entry *tga;
25dbc73a 2884
0e1862bb 2885 if (bfd_link_relocatable (info))
25dbc73a 2886 return TRUE;
252b5132
RH
2887
2888#ifdef DEBUG
871b3ab2 2889 _bfd_error_handler ("ppc_elf_check_relocs called for section %pA in %pB",
25dbc73a 2890 sec, abfd);
252b5132
RH
2891#endif
2892
0c8d6e5c 2893 BFD_ASSERT (is_ppc_elf (abfd));
0ffa91dd 2894
25dbc73a
AM
2895 /* Initialize howto table if not already done. */
2896 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2897 ppc_elf_howto_init ();
2898
7619e7c7 2899 htab = ppc_elf_hash_table (info);
25f23106
AM
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 }
727fc41e
AM
2907 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
2908 FALSE, FALSE, TRUE);
0ffa91dd 2909 symtab_hdr = &elf_symtab_hdr (abfd);
25dbc73a 2910 sym_hashes = elf_sym_hashes (abfd);
a6aa5195 2911 got2 = bfd_get_section_by_name (abfd, ".got2");
25dbc73a
AM
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;
727fc41e 2920 int tls_type;
cbf95972 2921 struct plt_entry **ifunc;
2d7ad24e
AM
2922 struct plt_entry **pltent;
2923 bfd_vma addend;
25dbc73a
AM
2924
2925 r_symndx = ELF32_R_SYM (rel->r_info);
2926 if (r_symndx < symtab_hdr->sh_info)
2927 h = NULL;
2928 else
973a3492
L
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 }
25dbc73a
AM
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. */
3b36f7e6 2939 if (h != NULL
ce558b89 2940 && htab->elf.sgot == NULL
3b36f7e6 2941 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
252b5132 2942 {
3b36f7e6
AM
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);
25dbc73a 2948 }
252b5132 2949
727fc41e 2950 tls_type = 0;
de972ffa 2951 r_type = ELF32_R_TYPE (rel->r_info);
cbf95972 2952 ifunc = NULL;
90c14f0c 2953 if (h == NULL && htab->elf.target_os != is_vxworks)
e054468f 2954 {
f1dfbfdb 2955 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
06a162cf
AM
2956 abfd, r_symndx);
2957 if (isym == NULL)
2958 return FALSE;
2959
0bed072f 2960 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
e054468f 2961 {
0bed072f 2962 /* Set PLT_IFUNC flag for this sym, no GOT entry yet. */
06a162cf 2963 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
37da22e5 2964 NON_GOT | PLT_IFUNC);
06a162cf 2965 if (ifunc == NULL)
e054468f
AM
2966 return FALSE;
2967
06a162cf
AM
2968 /* STT_GNU_IFUNC symbols must have a PLT entry;
2969 In a non-pie executable even when there are
2970 no plt calls. */
0e1862bb 2971 if (!bfd_link_pic (info)
08be3224
AM
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)
e054468f 2976 {
2d7ad24e 2977 addend = 0;
0bed072f 2978 if (r_type == R_PPC_PLTREL24)
08be3224
AM
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;
0bed072f
AM
2986 if (!update_plt_info (abfd, ifunc, got2, addend))
2987 return FALSE;
e054468f
AM
2988 }
2989 }
25f23106
AM
2990 }
2991
90c14f0c 2992 if (htab->elf.target_os != is_vxworks
de972ffa
AM
2993 && is_branch_reloc (r_type)
2994 && h != NULL
2995 && h == tga)
25f23106 2996 {
de972ffa
AM
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. */
9737e8af 3005 sec->nomark_tls_get_addr = 1;
e054468f 3006 }
727fc41e 3007
25dbc73a
AM
3008 switch (r_type)
3009 {
727fc41e
AM
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. */
37da22e5
AM
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;
727fc41e
AM
3020 break;
3021
23cedd1d
AM
3022 case R_PPC_PLTSEQ:
3023 break;
3024
25dbc73a
AM
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:
25dbc73a
AM
3029 tls_type = TLS_TLS | TLS_LD;
3030 goto dogottls;
252b5132 3031
25dbc73a
AM
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:
7c8bbca5 3043 if (bfd_link_dll (info))
25dbc73a
AM
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;
1a0670f3 3055 /* Fall through. */
252b5132 3056
25dbc73a
AM
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. */
ce558b89 3063 if (htab->elf.sgot == NULL)
25dbc73a
AM
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;
91e21fb7
AM
3079
3080 /* We may also need a plt entry if the symbol turns out to be
3081 an ifunc. */
0e1862bb 3082 if (h != NULL && !bfd_link_pic (info))
91e21fb7
AM
3083 {
3084 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3085 return FALSE;
3086 }
25dbc73a 3087 break;
252b5132 3088
25dbc73a
AM
3089 /* Indirect .sdata relocation. */
3090 case R_PPC_EMB_SDAI16:
93d1b056
AM
3091 htab->sdata[0].sym->ref_regular = 1;
3092 if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[0],
3093 h, rel))
25dbc73a 3094 return FALSE;
046183de
AM
3095 if (h != NULL)
3096 {
3097 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3098 h->non_got_ref = TRUE;
3099 }
25dbc73a 3100 break;
252b5132 3101
25dbc73a
AM
3102 /* Indirect .sdata2 relocation. */
3103 case R_PPC_EMB_SDA2I16:
c0d9f31d 3104 if (!bfd_link_executable (info))
25dbc73a
AM
3105 {
3106 bad_shared_reloc (abfd, r_type);
3107 return FALSE;
3108 }
93d1b056
AM
3109 htab->sdata[1].sym->ref_regular = 1;
3110 if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[1],
3111 h, rel))
25dbc73a 3112 return FALSE;
046183de
AM
3113 if (h != NULL)
3114 {
3115 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3116 h->non_got_ref = TRUE;
3117 }
25dbc73a 3118 break;
252b5132 3119
93d1b056
AM
3120 case R_PPC_SDAREL16:
3121 htab->sdata[0].sym->ref_regular = 1;
1a0670f3 3122 /* Fall through. */
93d1b056 3123
b9c361e0
JL
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:
046183de
AM
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
b9c361e0
JL
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:
83eef883 3146 case R_PPC_VLE_ADDR20:
b9c361e0
JL
3147 break;
3148
25dbc73a 3149 case R_PPC_EMB_SDA2REL:
c0d9f31d 3150 if (!bfd_link_executable (info))
046183de
AM
3151 {
3152 bad_shared_reloc (abfd, r_type);
3153 return FALSE;
3154 }
93d1b056 3155 htab->sdata[1].sym->ref_regular = 1;
046183de
AM
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
b9c361e0
JL
3163 case R_PPC_VLE_SDA21_LO:
3164 case R_PPC_VLE_SDA21:
25dbc73a
AM
3165 case R_PPC_EMB_SDA21:
3166 case R_PPC_EMB_RELSDA:
046183de
AM
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
25dbc73a
AM
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:
4dc4a9a5 3179 if (h != NULL)
046183de 3180 h->non_got_ref = TRUE;
25dbc73a 3181 break;
252b5132 3182
25dbc73a 3183 case R_PPC_PLTREL24:
de972ffa 3184 if (h == NULL)
6d78d0b9 3185 break;
08be3224 3186 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3e04d765 3187 goto pltentry;
08be3224 3188
23cedd1d 3189 case R_PPC_PLTCALL:
3e04d765
AM
3190 sec->has_pltcall = 1;
3191 /* Fall through. */
3192
6d78d0b9 3193 case R_PPC_PLT32:
25dbc73a
AM
3194 case R_PPC_PLTREL32:
3195 case R_PPC_PLT16_LO:
3196 case R_PPC_PLT16_HI:
3197 case R_PPC_PLT16_HA:
3e04d765 3198 pltentry:
25dbc73a
AM
3199#ifdef DEBUG
3200 fprintf (stderr, "Reloc requires a PLT entry\n");
3201#endif
cbf95972 3202 /* This symbol requires a procedure linkage table entry. */
25dbc73a
AM
3203 if (h == NULL)
3204 {
2d7ad24e
AM
3205 pltent = update_local_sym_info (abfd, symtab_hdr, r_symndx,
3206 NON_GOT | PLT_KEEP);
3207 if (pltent == NULL)
3208 return FALSE;
25dbc73a 3209 }
a6aa5195
AM
3210 else
3211 {
2d7ad24e
AM
3212 if (r_type != R_PPC_PLTREL24)
3213 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
a6aa5195 3214 h->needs_plt = 1;
2d7ad24e 3215 pltent = &h->plt.plist;
a6aa5195 3216 }
2d7ad24e
AM
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;
25dbc73a 3226 break;
ee05f2fe 3227
25dbc73a
AM
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;
252b5132 3241
d7128ce4
AM
3242 case R_PPC_REL16:
3243 case R_PPC_REL16_LO:
3244 case R_PPC_REL16_HI:
3245 case R_PPC_REL16_HA:
a680de9a 3246 case R_PPC_REL16DX_HA:
016687f8 3247 ppc_elf_tdata (abfd)->has_rel16 = 1;
d7128ce4
AM
3248 break;
3249
a6aa5195 3250 /* These are just markers. */
25dbc73a
AM
3251 case R_PPC_TLS:
3252 case R_PPC_EMB_MRKREF:
3253 case R_PPC_NONE:
3254 case R_PPC_max:
32af9f6e
AM
3255 case R_PPC_RELAX:
3256 case R_PPC_RELAX_PLT:
3257 case R_PPC_RELAX_PLTREL24:
7ba71655 3258 case R_PPC_16DX_HA:
25dbc73a 3259 break;
252b5132 3260
25dbc73a
AM
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:
e054468f 3266 case R_PPC_IRELATIVE:
25dbc73a 3267 break;
252b5132 3268
25dbc73a
AM
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;
252b5132 3277
25dbc73a
AM
3278 /* This refers only to functions defined in the shared library. */
3279 case R_PPC_LOCAL24PC:
727fc41e 3280 if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
016687f8
AM
3281 {
3282 htab->plt_type = PLT_OLD;
3283 htab->old_bfd = abfd;
3284 }
c7e17e05
AM
3285 if (h != NULL && h->type == STT_GNU_IFUNC)
3286 {
c7e17e05
AM
3287 h->needs_plt = 1;
3288 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3289 return FALSE;
3290 }
25dbc73a 3291 break;
252b5132 3292
25dbc73a
AM
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;
252b5132 3299
25dbc73a
AM
3300 /* This relocation describes which C++ vtable entries are actually
3301 used. Record for later use during GC. */
3302 case R_PPC_GNU_VTENTRY:
a0ea3a14 3303 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
25dbc73a
AM
3304 return FALSE;
3305 break;
252b5132 3306
252dcdf4
AM
3307 case R_PPC_TPREL16_HI:
3308 case R_PPC_TPREL16_HA:
3309 sec->has_tls_reloc = 1;
3310 /* Fall through. */
7c8bbca5 3311 /* We shouldn't really be seeing TPREL32. */
25dbc73a 3312 case R_PPC_TPREL32:
1d483afe
AM
3313 case R_PPC_TPREL16:
3314 case R_PPC_TPREL16_LO:
7c8bbca5 3315 if (bfd_link_dll (info))
25dbc73a
AM
3316 info->flags |= DF_STATIC_TLS;
3317 goto dodyn;
252b5132 3318
25dbc73a
AM
3319 /* Nor these. */
3320 case R_PPC_DTPMOD32:
3321 case R_PPC_DTPREL32:
3322 goto dodyn;
252b5132 3323
9edfd1af
AM
3324 case R_PPC_REL32:
3325 if (h == NULL
3326 && got2 != NULL
3327 && (sec->flags & SEC_CODE) != 0
0e1862bb 3328 && bfd_link_pic (info)
4a3dc543 3329 && htab->plt_type == PLT_UNSET)
9edfd1af
AM
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;
87d72d41 3338 Elf_Internal_Sym *isym;
9edfd1af 3339
f1dfbfdb 3340 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
3341 abfd, r_symndx);
3342 if (isym == NULL)
3343 return FALSE;
3344
3345 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
9edfd1af 3346 if (s == got2)
016687f8
AM
3347 {
3348 htab->plt_type = PLT_OLD;
3349 htab->old_bfd = abfd;
3350 }
9edfd1af 3351 }
7123df0e 3352 if (h == NULL || h == htab->elf.hgot)
fd38b44c 3353 break;
625af618
AM
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:
0e1862bb 3363 if (h != NULL && !bfd_link_pic (info))
625af618
AM
3364 {
3365 /* We may need a plt entry if the symbol turns out to be
3366 a function defined in a dynamic object. */
3ec01793 3367 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
625af618
AM
3368 return FALSE;
3369
3370 /* We may need a copy reloc too. */
3371 h->non_got_ref = 1;
3372 h->pointer_equality_needed = 1;
d3e454b9
AM
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;
625af618
AM
3377 }
3378 goto dodyn;
9edfd1af 3379
25dbc73a
AM
3380 case R_PPC_REL24:
3381 case R_PPC_REL14:
3382 case R_PPC_REL14_BRTAKEN:
3383 case R_PPC_REL14_BRNTAKEN:
d7128ce4 3384 if (h == NULL)
25dbc73a 3385 break;
7123df0e 3386 if (h == htab->elf.hgot)
d7128ce4 3387 {
7123df0e 3388 if (htab->plt_type == PLT_UNSET)
016687f8
AM
3389 {
3390 htab->plt_type = PLT_OLD;
3391 htab->old_bfd = abfd;
3392 }
d7128ce4
AM
3393 break;
3394 }
25dbc73a 3395 /* fall through */
ee05f2fe 3396
25dbc73a 3397 case R_PPC_ADDR24:
25dbc73a
AM
3398 case R_PPC_ADDR14:
3399 case R_PPC_ADDR14_BRTAKEN:
3400 case R_PPC_ADDR14_BRNTAKEN:
0e1862bb 3401 if (h != NULL && !bfd_link_pic (info))
25dbc73a
AM
3402 {
3403 /* We may need a plt entry if the symbol turns out to be
3404 a function defined in a dynamic object. */
e054468f 3405 h->needs_plt = 1;
de972ffa 3406 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
a6aa5195 3407 return FALSE;
625af618 3408 break;
25dbc73a 3409 }
7619e7c7 3410
25dbc73a
AM
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.
7619e7c7 3428
25dbc73a
AM
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. */
0e1862bb 3433 if ((bfd_link_pic (info)
1d483afe 3434 && (must_be_dyn_reloc (info, r_type)
25dbc73a 3435 || (h != NULL
198f1157 3436 && (!SYMBOLIC_BIND (info, h)
25dbc73a
AM
3437 || h->root.type == bfd_link_hash_defweak
3438 || !h->def_regular))))
3439 || (ELIMINATE_COPY_RELOCS
0e1862bb 3440 && !bfd_link_pic (info)
25dbc73a
AM
3441 && h != NULL
3442 && (h->root.type == bfd_link_hash_defweak
de972ffa 3443 || !h->def_regular)))
25dbc73a 3444 {
25dbc73a
AM
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 {
c9a2f333
AM
3454 if (htab->elf.dynobj == NULL)
3455 htab->elf.dynobj = abfd;
83bac4b0
NC
3456
3457 sreloc = _bfd_elf_make_dynamic_reloc_section
3458 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ TRUE);
3459
25dbc73a 3460 if (sreloc == NULL)
83bac4b0 3461 return FALSE;
25dbc73a 3462 }
7619e7c7 3463
25dbc73a
AM
3464 /* If this is a global symbol, we count the number of
3465 relocations we need for this symbol. */
3466 if (h != NULL)
3467 {
f95f8542
AM
3468 struct elf_dyn_relocs *p;
3469 struct elf_dyn_relocs **rel_head;
3470
190eb1dd 3471 rel_head = &h->dyn_relocs;
f95f8542
AM
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;
25dbc73a
AM
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. */
f95f8542
AM
3493 struct ppc_dyn_relocs *p;
3494 struct ppc_dyn_relocs **rel_head;
3495 bfd_boolean is_ifunc;
25dbc73a 3496 asection *s;
6edfbbad 3497 void *vpp;
87d72d41 3498 Elf_Internal_Sym *isym;
6edfbbad 3499
f1dfbfdb 3500 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
3501 abfd, r_symndx);
3502 if (isym == NULL)
25dbc73a 3503 return FALSE;
7fce784e 3504
87d72d41
AM
3505 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3506 if (s == NULL)
3507 s = sec;
3508
6edfbbad 3509 vpp = &elf_section_data (s)->local_dynrel;
f95f8542
AM
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;
25dbc73a 3527 }
7619e7c7 3528 }
25dbc73a
AM
3529
3530 break;
7619e7c7
AM
3531 }
3532 }
ee05f2fe 3533
25dbc73a
AM
3534 return TRUE;
3535}
3536\f
005d79fd
AM
3537/* Warn for conflicting Tag_GNU_Power_ABI_FP attributes between IBFD
3538 and OBFD, and merge non-conflicting ones. */
4a91d0ba 3539bfd_boolean
50e03d47 3540_bfd_elf_ppc_merge_fp_attributes (bfd *ibfd, struct bfd_link_info *info)
34c8bcba 3541{
50e03d47 3542 bfd *obfd = info->output_bfd;
c6e65352
DJ
3543 obj_attribute *in_attr, *in_attrs;
3544 obj_attribute *out_attr, *out_attrs;
4a91d0ba 3545 bfd_boolean ret = TRUE;
6f3fe02b
AM
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;
34c8bcba 3557
c6e65352
DJ
3558 in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
3559 out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
3560
c6e65352
DJ
3561 in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
3562 out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
005d79fd 3563
c6e65352 3564 if (in_attr->i != out_attr->i)
34c8bcba 3565 {
005d79fd
AM
3566 int in_fp = in_attr->i & 3;
3567 int out_fp = out_attr->i & 3;
4a91d0ba 3568 static bfd *last_fp, *last_ld;
005d79fd
AM
3569
3570 if (in_fp == 0)
34c8bcba 3571 ;
005d79fd
AM
3572 else if (out_fp == 0)
3573 {
6f3fe02b
AM
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 }
005d79fd
AM
3580 }
3581 else if (out_fp != 2 && in_fp == 2)
8d2c8c3d
AM
3582 {
3583 _bfd_error_handler
3584 /* xgettext:c-format */
4a91d0ba
AM
3585 (_("%pB uses hard float, %pB uses soft float"),
3586 last_fp, ibfd);
6f3fe02b 3587 ret = warn_only;
8d2c8c3d 3588 }
005d79fd 3589 else if (out_fp == 2 && in_fp != 2)
8d2c8c3d
AM
3590 {
3591 _bfd_error_handler
3592 /* xgettext:c-format */
4a91d0ba
AM
3593 (_("%pB uses hard float, %pB uses soft float"),
3594 ibfd, last_fp);
6f3fe02b 3595 ret = warn_only;
8d2c8c3d 3596 }
005d79fd 3597 else if (out_fp == 1 && in_fp == 3)
8d2c8c3d
AM
3598 {
3599 _bfd_error_handler
3600 /* xgettext:c-format */
4a91d0ba
AM
3601 (_("%pB uses double-precision hard float, "
3602 "%pB uses single-precision hard float"), last_fp, ibfd);
6f3fe02b 3603 ret = warn_only;
8d2c8c3d 3604 }
005d79fd 3605 else if (out_fp == 3 && in_fp == 1)
8d2c8c3d
AM
3606 {
3607 _bfd_error_handler
3608 /* xgettext:c-format */
4a91d0ba
AM
3609 (_("%pB uses double-precision hard float, "
3610 "%pB uses single-precision hard float"), ibfd, last_fp);
6f3fe02b 3611 ret = warn_only;
8d2c8c3d 3612 }
005d79fd
AM
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 {
6f3fe02b
AM
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 }
005d79fd
AM
3626 }
3627 else if (out_fp != 2 * 4 && in_fp == 2 * 4)
8d2c8c3d
AM
3628 {
3629 _bfd_error_handler
3630 /* xgettext:c-format */
4a91d0ba
AM
3631 (_("%pB uses 64-bit long double, "
3632 "%pB uses 128-bit long double"), ibfd, last_ld);
6f3fe02b 3633 ret = warn_only;
8d2c8c3d 3634 }
005d79fd 3635 else if (in_fp != 2 * 4 && out_fp == 2 * 4)
8d2c8c3d
AM
3636 {
3637 _bfd_error_handler
3638 /* xgettext:c-format */
4a91d0ba
AM
3639 (_("%pB uses 64-bit long double, "
3640 "%pB uses 128-bit long double"), last_ld, ibfd);
6f3fe02b 3641 ret = warn_only;
8d2c8c3d 3642 }
005d79fd 3643 else if (out_fp == 1 * 4 && in_fp == 3 * 4)
8d2c8c3d
AM
3644 {
3645 _bfd_error_handler
3646 /* xgettext:c-format */
4a91d0ba
AM
3647 (_("%pB uses IBM long double, "
3648 "%pB uses IEEE long double"), last_ld, ibfd);
6f3fe02b 3649 ret = warn_only;
8d2c8c3d 3650 }
005d79fd 3651 else if (out_fp == 3 * 4 && in_fp == 1 * 4)
8d2c8c3d
AM
3652 {
3653 _bfd_error_handler
3654 /* xgettext:c-format */
4a91d0ba
AM
3655 (_("%pB uses IBM long double, "
3656 "%pB uses IEEE long double"), ibfd, last_ld);
6f3fe02b 3657 ret = warn_only;
8d2c8c3d 3658 }
c6e65352 3659 }
4a91d0ba
AM
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;
005d79fd
AM
3667}
3668
3669/* Merge object attributes from IBFD into OBFD. Warn if
3670 there are conflicting attributes. */
3671static bfd_boolean
50e03d47 3672ppc_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
005d79fd 3673{
50e03d47 3674 bfd *obfd;
005d79fd
AM
3675 obj_attribute *in_attr, *in_attrs;
3676 obj_attribute *out_attr, *out_attrs;
4a91d0ba 3677 bfd_boolean ret;
005d79fd 3678
4a91d0ba
AM
3679 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
3680 return FALSE;
005d79fd 3681
50e03d47 3682 obfd = info->output_bfd;
005d79fd
AM
3683 in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
3684 out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
c6e65352
DJ
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];
4a91d0ba 3690 ret = TRUE;
c6e65352
DJ
3691 if (in_attr->i != out_attr->i)
3692 {
005d79fd
AM
3693 int in_vec = in_attr->i & 3;
3694 int out_vec = out_attr->i & 3;
4a91d0ba 3695 static bfd *last_vec;
c6e65352 3696
005d79fd
AM
3697 if (in_vec == 0)
3698 ;
3699 else if (out_vec == 0)
c6e65352 3700 {
8d2c8c3d 3701 out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
005d79fd 3702 out_attr->i = in_vec;
4a91d0ba 3703 last_vec = ibfd;
c6e65352 3704 }
c6e65352
DJ
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. */
005d79fd 3710 else if (in_vec == 1)
c6e65352 3711 ;
005d79fd
AM
3712 else if (out_vec == 1)
3713 {
8d2c8c3d 3714 out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
005d79fd 3715 out_attr->i = in_vec;
4a91d0ba 3716 last_vec = ibfd;
005d79fd
AM
3717 }
3718 else if (out_vec < in_vec)
8d2c8c3d
AM
3719 {
3720 _bfd_error_handler
3721 /* xgettext:c-format */
4a91d0ba
AM
3722 (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
3723 last_vec, ibfd);
8d2c8c3d 3724 out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
4a91d0ba 3725 ret = FALSE;
8d2c8c3d 3726 }
005d79fd 3727 else if (out_vec > in_vec)
8d2c8c3d
AM
3728 {
3729 _bfd_error_handler
3730 /* xgettext:c-format */
4a91d0ba
AM
3731 (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
3732 ibfd, last_vec);
8d2c8c3d 3733 out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
4a91d0ba 3734 ret = FALSE;
8d2c8c3d 3735 }
34c8bcba
JM
3736 }
3737
f82e0623
NF
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 {
005d79fd
AM
3744 int in_struct = in_attr->i & 3;
3745 int out_struct = out_attr->i & 3;
4a91d0ba 3746 static bfd *last_struct;
005d79fd
AM
3747
3748 if (in_struct == 0 || in_struct == 3)
f82e0623 3749 ;
005d79fd
AM
3750 else if (out_struct == 0)
3751 {
8d2c8c3d 3752 out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
005d79fd 3753 out_attr->i = in_struct;
4a91d0ba 3754 last_struct = ibfd;
005d79fd
AM
3755 }
3756 else if (out_struct < in_struct)
8d2c8c3d
AM
3757 {
3758 _bfd_error_handler
3759 /* xgettext:c-format */
4a91d0ba
AM
3760 (_("%pB uses r3/r4 for small structure returns, "
3761 "%pB uses memory"), last_struct, ibfd);
8d2c8c3d 3762 out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
4a91d0ba 3763 ret = FALSE;
8d2c8c3d 3764 }
005d79fd 3765 else if (out_struct > in_struct)
8d2c8c3d
AM
3766 {
3767 _bfd_error_handler
3768 /* xgettext:c-format */
4a91d0ba
AM
3769 (_("%pB uses r3/r4 for small structure returns, "
3770 "%pB uses memory"), ibfd, last_struct);
8d2c8c3d 3771 out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
4a91d0ba 3772 ret = FALSE;
8d2c8c3d 3773 }
f82e0623 3774 }
4a91d0ba
AM
3775 if (!ret)
3776 {
3777 bfd_set_error (bfd_error_bad_value);
3778 return FALSE;
3779 }
f82e0623 3780
34c8bcba 3781 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 3782 return _bfd_elf_merge_object_attributes (ibfd, info);
34c8bcba
JM
3783}
3784
8853c3d3
AM
3785/* Merge backend specific data from an object file to the output
3786 object file when linking. */
3787
3788static bfd_boolean
50e03d47 3789ppc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
8853c3d3 3790{
50e03d47 3791 bfd *obfd = info->output_bfd;
8853c3d3
AM
3792 flagword old_flags;
3793 flagword new_flags;
3794 bfd_boolean error;
3795
0c8d6e5c 3796 if (!is_ppc_elf (ibfd) || !is_ppc_elf (obfd))
8853c3d3
AM
3797 return TRUE;
3798
cc643b88 3799 /* Check if we have the same endianness. */
50e03d47 3800 if (! _bfd_generic_verify_endian_match (ibfd, info))
8853c3d3
AM
3801 return FALSE;
3802
50e03d47 3803 if (!ppc_elf_merge_obj_attributes (ibfd, info))
34c8bcba
JM
3804 return FALSE;
3805
91ed9b71
AM
3806 if ((ibfd->flags & DYNAMIC) != 0)
3807 return TRUE;
3808
8853c3d3
AM
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;
4eca0228 3832 _bfd_error_handler
871b3ab2 3833 (_("%pB: compiled with -mrelocatable and linked with "
8853c3d3
AM
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;
4eca0228 3840 _bfd_error_handler
871b3ab2 3841 (_("%pB: compiled normally and linked with "
8853c3d3
AM
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;
4eca0228 3867 _bfd_error_handler
695344c0 3868 /* xgettext:c-format */
871b3ab2 3869 (_("%pB: uses different e_flags (%#x) fields "
cd9af601
AM
3870 "than previous modules (%#x)"),
3871 ibfd, new_flags, old_flags);
8853c3d3
AM
3872 }
3873
3874 if (error)
3875 {
3876 bfd_set_error (bfd_error_bad_value);
3877 return FALSE;
3878 }
3879 }
3880
3881 return TRUE;
3882}
b9c361e0
JL
3883
3884static void
95f0d0d2 3885ppc_elf_vle_split16 (bfd *input_bfd,
08dc996f
AM
3886 asection *input_section,
3887 unsigned long offset,
95f0d0d2 3888 bfd_byte *loc,
86c95733 3889 bfd_vma value,
08dc996f
AM
3890 split16_format_type split16_format,
3891 bfd_boolean fixup)
b9c361e0 3892{
bb6bf75e 3893 unsigned int insn, opcode;
b9c361e0 3894
95f0d0d2 3895 insn = bfd_get_32 (input_bfd, loc);
bb6bf75e 3896 opcode = insn & E_OPCODE_MASK;
08dc996f
AM
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 */
871b3ab2 3910 (_("%pB(%pA+0x%lx): expected 16A style relocation on 0x%08x insn"),
08dc996f
AM
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 */
871b3ab2 3929 (_("%pB(%pA+0x%lx): expected 16D style relocation on 0x%08x insn"),
08dc996f
AM
3930 input_bfd, input_section, offset, opcode);
3931 }
3932 }
bb6bf75e
AM
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 }
86c95733 3949 insn |= value & 0x7ff;
95f0d0d2 3950 bfd_put_32 (input_bfd, insn, loc);
b9c361e0 3951}
83eef883
AFB
3952
3953static void
3954ppc_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
8853c3d3 3969\f
d7128ce4
AM
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. */
3972int
3973ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
5446cbdf 3974 struct bfd_link_info *info)
d7128ce4
AM
3975{
3976 struct ppc_elf_link_hash_table *htab;
9d8504b1 3977 flagword flags;
d7128ce4
AM
3978
3979 htab = ppc_elf_hash_table (info);
4a3dc543
RS
3980
3981 if (htab->plt_type == PLT_UNSET)
016687f8 3982 {
b3874e1a
AM
3983 struct elf_link_hash_entry *h;
3984
5446cbdf 3985 if (htab->params->plt_style == PLT_OLD)
016687f8 3986 htab->plt_type = PLT_OLD;
0e1862bb 3987 else if (bfd_link_pic (info)
b3874e1a
AM
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)
21d68fcd 3995 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
b3874e1a
AM
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 }
016687f8
AM
4003 else
4004 {
4005 bfd *ibfd;
5446cbdf 4006 enum ppc_elf_plt_type plt_type = htab->params->plt_style;
016687f8
AM
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;
c72f2fb2 4014 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
0c8d6e5c 4015 if (is_ppc_elf (ibfd))
016687f8
AM
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 }
5446cbdf 4029 if (htab->plt_type == PLT_OLD && htab->params->plt_style == PLT_NEW)
b3874e1a
AM
4030 {
4031 if (htab->old_bfd != NULL)
cf97bcb0 4032 _bfd_error_handler (_("bss-plt forced due to %pB"), htab->old_bfd);
b3874e1a 4033 else
cf97bcb0 4034 _bfd_error_handler (_("bss-plt forced by profiling"));
b3874e1a 4035 }
d7128ce4 4036
4a3dc543 4037 BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
9d8504b1 4038
4a3dc543 4039 if (htab->plt_type == PLT_NEW)
9d8504b1
PB
4040 {
4041 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4042 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
86b9da88
AM
4043
4044 /* The new PLT is a loaded section. */
ce558b89 4045 if (htab->elf.splt != NULL
fd361982 4046 && !bfd_set_section_flags (htab->elf.splt, flags))
86b9da88
AM
4047 return -1;
4048
4049 /* The new GOT is not executable. */
ce558b89 4050 if (htab->elf.sgot != NULL
fd361982 4051 && !bfd_set_section_flags (htab->elf.sgot, flags))
86b9da88 4052 return -1;
d7128ce4
AM
4053 }
4054 else
4055 {
4056 /* Stop an unused .glink section from affecting .text alignment. */
86b9da88 4057 if (htab->glink != NULL
fd361982 4058 && !bfd_set_section_alignment (htab->glink, 0))
86b9da88 4059 return -1;
d7128ce4 4060 }
4a3dc543 4061 return htab->plt_type == PLT_NEW;
d7128ce4
AM
4062}
4063\f
25dbc73a
AM
4064/* Return the section that should be marked against GC for a given
4065 relocation. */
586119b3 4066
25dbc73a
AM
4067static asection *
4068ppc_elf_gc_mark_hook (asection *sec,
07adf181 4069 struct bfd_link_info *info,
25dbc73a
AM
4070 Elf_Internal_Rela *rel,
4071 struct elf_link_hash_entry *h,
4072 Elf_Internal_Sym *sym)
4073{
4074 if (h != NULL)
07adf181
AM
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);
25dbc73a 4083}
49c09209
AM
4084
4085static bfd_boolean
4086get_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}
25dbc73a 4171\f
3e04d765
AM
4172/* Analyze inline PLT call relocations to see whether calls to locally
4173 defined functions can be converted to direct calls. */
4174
4175bfd_boolean
4176ppc_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);
c9594989 4267 if (symtab_hdr->contents != (unsigned char *) local_syms)
3e04d765
AM
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
3a71aa26
AM
4306/* Set plt output section type, htab->tls_get_addr, and call the
4307 generic ELF tls_setup function. */
7fce784e 4308
25dbc73a 4309asection *
5446cbdf 4310ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
7fce784e 4311{
25dbc73a 4312 struct ppc_elf_link_hash_table *htab;
7fce784e 4313
25dbc73a 4314 htab = ppc_elf_hash_table (info);
a7f2871e
AM
4315 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4316 FALSE, FALSE, TRUE);
f01f1741
AM
4317 if (htab->plt_type != PLT_NEW)
4318 htab->params->no_tls_get_addr_opt = TRUE;
4319
5446cbdf 4320 if (!htab->params->no_tls_get_addr_opt)
a7f2871e
AM
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)
21d68fcd 4339 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga)))
a7f2871e
AM
4340 {
4341 struct plt_entry *ent;
32af9f6e
AM
4342 for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
4343 if (ent->plt.refcount > 0)
4344 break;
4345 if (ent != NULL)
a7f2871e
AM
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);
b531344c 4350 opt->mark = 1;
a7f2871e
AM
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
5446cbdf 4365 htab->params->no_tls_get_addr_opt = TRUE;
a7f2871e 4366 }
4a3dc543 4367 if (htab->plt_type == PLT_NEW
ce558b89
AM
4368 && htab->elf.splt != NULL
4369 && htab->elf.splt->output_section != NULL)
d7128ce4 4370 {
ce558b89
AM
4371 elf_section_type (htab->elf.splt->output_section) = SHT_PROGBITS;
4372 elf_section_flags (htab->elf.splt->output_section) = SHF_ALLOC + SHF_WRITE;
d7128ce4
AM
4373 }
4374
25dbc73a 4375 return _bfd_elf_tls_setup (obfd, info);
7fce784e
AS
4376}
4377
3a71aa26
AM
4378/* Return TRUE iff REL is a branch reloc with a global symbol matching
4379 HASH. */
4380
4381static bfd_boolean
4382branch_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
e054468f 4390 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
3a71aa26
AM
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
25dbc73a
AM
4405/* Run through all the TLS relocs looking for optimization
4406 opportunities. */
252b5132 4407
25dbc73a
AM
4408bfd_boolean
4409ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4410 struct bfd_link_info *info)
252b5132 4411{
7fce784e 4412 bfd *ibfd;
25dbc73a
AM
4413 asection *sec;
4414 struct ppc_elf_link_hash_table *htab;
b7fcf6f6 4415 int pass;
252b5132 4416
3cbc1e5e 4417 if (!bfd_link_executable (info))
25dbc73a 4418 return TRUE;
252b5132 4419
7619e7c7 4420 htab = ppc_elf_hash_table (info);
663a1470
AM
4421 if (htab == NULL)
4422 return FALSE;
4423
252dcdf4
AM
4424 htab->do_tls_opt = 1;
4425
b7fcf6f6
AM
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
663a1470
AM
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. */
b7fcf6f6 4432 for (pass = 0; pass < 2; ++pass)
c72f2fb2 4433 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
b7fcf6f6 4434 {
0ffa91dd 4435 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
32af9f6e 4436 asection *got2 = bfd_get_section_by_name (ibfd, ".got2");
7619e7c7 4437
b7fcf6f6
AM
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;
663a1470 4442 int expecting_tls_get_addr = 0;
252b5132 4443
b7fcf6f6
AM
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;
7619e7c7 4449
b7fcf6f6
AM
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;
bac3c8c5 4456 unsigned char *tls_mask;
37da22e5 4457 unsigned char tls_set, tls_clear;
b7fcf6f6 4458 bfd_boolean is_local;
b7fcf6f6
AM
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;
7fce784e 4465
b7fcf6f6
AM
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 }
7619e7c7 4472
f749f26e 4473 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
b7fcf6f6 4474 r_type = ELF32_R_TYPE (rel->r_info);
663a1470
AM
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
9737e8af 4482 && sec->nomark_tls_get_addr
663a1470
AM
4483 && h != NULL
4484 && h == htab->tls_get_addr
4485 && !expecting_tls_get_addr
4486 && is_branch_reloc (r_type))
4487 {
25f53a85 4488 info->callbacks->minfo ("%H __tls_get_addr lost arg, "
663a1470
AM
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;
b7fcf6f6
AM
4497 switch (r_type)
4498 {
4499 case R_PPC_GOT_TLSLD16:
4500 case R_PPC_GOT_TLSLD16_LO:
4501 expecting_tls_get_addr = 1;
1a0670f3 4502 /* Fall through. */
b7fcf6f6
AM
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;
1a0670f3 4520 /* Fall through. */
b7fcf6f6
AM
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 */
b00a0a86 4529 tls_set = TLS_TLS | TLS_GDIE;
b7fcf6f6
AM
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
32af9f6e 4547 case R_PPC_TLSLD:
7d04a20a
AM
4548 if (!is_local)
4549 continue;
4550 /* Fall through. */
4551 case R_PPC_TLSGD:
23cedd1d
AM
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 }
32af9f6e
AM
4586 expecting_tls_get_addr = 2;
4587 tls_set = 0;
4588 tls_clear = 0;
4589 break;
4590
252dcdf4
AM
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
b7fcf6f6 4622 default:
25dbc73a 4623 continue;
b7fcf6f6 4624 }
25dbc73a 4625
b7fcf6f6
AM
4626 if (pass == 0)
4627 {
727fc41e 4628 if (!expecting_tls_get_addr
9737e8af 4629 || !sec->nomark_tls_get_addr)
b7fcf6f6 4630 continue;
25dbc73a 4631
3a71aa26
AM
4632 if (rel + 1 < relend
4633 && branch_reloc_hash_match (ibfd, rel + 1,
4634 htab->tls_get_addr))
4635 continue;
25dbc73a 4636
b7fcf6f6
AM
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
663a1470 4640 the entire optimization. */
25f53a85 4641 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
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;
b7fcf6f6 4647 }
25dbc73a 4648
b7fcf6f6
AM
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 {
b7fcf6f6 4656 bfd_signed_vma *lgot_refs;
e054468f 4657 struct plt_entry **local_plt;
bac3c8c5 4658 unsigned char *lgot_masks;
25dbc73a 4659
b7fcf6f6
AM
4660 lgot_refs = elf_local_got_refcounts (ibfd);
4661 if (lgot_refs == NULL)
4662 abort ();
e054468f
AM
4663 local_plt = (struct plt_entry **)
4664 (lgot_refs + symtab_hdr->sh_info);
bac3c8c5
AM
4665 lgot_masks = (unsigned char *)
4666 (local_plt + symtab_hdr->sh_info);
b7fcf6f6
AM
4667 tls_mask = &lgot_masks[r_symndx];
4668 got_count = &lgot_refs[r_symndx];
4669 }
25dbc73a 4670
37da22e5
AM
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
9737e8af 4679 && !sec->nomark_tls_get_addr
37da22e5
AM
4680 && ((*tls_mask & (TLS_TLS | TLS_MARK))
4681 != (TLS_TLS | TLS_MARK)))
4682 continue;
4683
7d04a20a 4684 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
37da22e5
AM
4685 {
4686 struct plt_entry *ent;
4687 bfd_vma addend = 0;
4688
4689 if (bfd_link_pic (info)
23cedd1d
AM
4690 && (ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTREL24
4691 || ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTCALL))
37da22e5
AM
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;
37da22e5 4697 }
7d04a20a
AM
4698 if (tls_clear == 0)
4699 continue;
37da22e5 4700
b7fcf6f6
AM
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 }
25dbc73a 4707
b7fcf6f6
AM
4708 *tls_mask |= tls_set;
4709 *tls_mask &= ~tls_clear;
4710 }
25dbc73a 4711
b7fcf6f6
AM
4712 if (elf_section_data (sec)->relocs != relstart)
4713 free (relstart);
4714 }
b7fcf6f6 4715 }
25dbc73a
AM
4716 return TRUE;
4717}
4718\f
287c7eaf
AM
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
4723static bfd_boolean
4724alias_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 {
5dbc8b37 4729 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
287c7eaf
AM
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
4739static bfd_boolean
4740pc_dynrelocs (struct elf_link_hash_entry *h)
4741{
4742 struct elf_dyn_relocs *p;
4743
190eb1dd 4744 for (p = h->dyn_relocs; p != NULL; p = p->next)
287c7eaf
AM
4745 if (p->pc_count != 0)
4746 return TRUE;
4747 return FALSE;
4748}
4749
25dbc73a
AM
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. */
7fce784e 4755
25dbc73a
AM
4756static bfd_boolean
4757ppc_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;
7fce784e 4762
25dbc73a
AM
4763#ifdef DEBUG
4764 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
4765 h->root.root.string);
4766#endif
252b5132 4767
25dbc73a
AM
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
91e21fb7 4772 || h->type == STT_GNU_IFUNC
60d67dc8 4773 || h->is_weakalias
25dbc73a
AM
4774 || (h->def_dynamic
4775 && h->ref_regular
4776 && !h->def_regular)));
252b5132 4777
25dbc73a
AM
4778 /* Deal with function syms. */
4779 if (h->type == STT_FUNC
e054468f 4780 || h->type == STT_GNU_IFUNC
25dbc73a
AM
4781 || h->needs_plt)
4782 {
529fe20e
AM
4783 bfd_boolean local = (SYMBOL_CALLS_LOCAL (info, h)
4784 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
287c7eaf
AM
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)
190eb1dd 4788 h->dyn_relocs = NULL;
529fe20e 4789
25dbc73a
AM
4790 /* Clear procedure linkage table information for any symbol that
4791 won't need a .plt entry. */
a6aa5195
AM
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
2d7ad24e
AM
4797 || (h->type != STT_GNU_IFUNC
4798 && local
3e04d765
AM
4799 && (htab->can_convert_all_inline_plt
4800 || (ppc_elf_hash_entry (h)->tls_mask
4801 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
252b5132 4802 {
25dbc73a 4803 /* A PLT entry is not required/allowed when:
252b5132 4804
25dbc73a
AM
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.
252b5132 4808
25dbc73a
AM
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. */
a6aa5195 4813 h->plt.plist = NULL;
25dbc73a 4814 h->needs_plt = 0;
d1eca1e4 4815 h->pointer_equality_needed = 0;
25dbc73a 4816 }
625af618
AM
4817 else
4818 {
d1eca1e4
AM
4819 /* Taking a function's address in a read/write section
4820 doesn't require us to define the function symbol in the
1009ef28 4821 executable on a plt call stub. A dynamic reloc can
f0158f44
AM
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
3988aed5
AM
4829 || (h->non_got_ref
4830 && !h->ref_regular_nonweak
4831 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
90c14f0c 4832 && htab->elf.target_os != is_vxworks
1009ef28 4833 && !ppc_elf_hash_entry (h)->has_sda_refs
5dbc8b37 4834 && !_bfd_elf_readonly_dynrelocs (h))
d1eca1e4
AM
4835 {
4836 h->pointer_equality_needed = 0;
04383fd1
AM
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)
3988aed5 4840 h->plt.plist = NULL;
d1eca1e4 4841 }
287c7eaf 4842 else if (!bfd_link_pic (info))
529fe20e
AM
4843 /* We are going to be defining the function symbol on the
4844 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 4845 h->dyn_relocs = NULL;
625af618 4846 }
d3e454b9 4847 h->protected_def = 0;
3988aed5 4848 /* Function symbols can't have copy relocs. */
25dbc73a 4849 return TRUE;
252b5132 4850 }
25dbc73a 4851 else
a6aa5195 4852 h->plt.plist = NULL;
252b5132 4853
25dbc73a
AM
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. */
60d67dc8 4857 if (h->is_weakalias)
252b5132 4858 {
60d67dc8
AM
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;
4a7e5234
AM
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)
190eb1dd 4866 h->dyn_relocs = NULL;
25dbc73a
AM
4867 return TRUE;
4868 }
dc810e39 4869
25dbc73a
AM
4870 /* This is a reference to a symbol defined by a dynamic object which
4871 is not a function. */
252b5132 4872
25dbc73a
AM
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. */
0e1862bb 4877 if (bfd_link_pic (info))
d3e454b9
AM
4878 {
4879 h->protected_def = 0;
4880 return TRUE;
4881 }
252b5132 4882
25dbc73a
AM
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)
d3e454b9
AM
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;
d3e454b9
AM
4903 return TRUE;
4904 }
25dbc73a 4905
a127494f
AM
4906 /* If -z nocopyreloc was given, we won't generate them either. */
4907 if (info->nocopyreloc)
529fe20e 4908 return TRUE;
a127494f 4909
dce2246a 4910 /* If we don't find any dynamic relocs in read-only sections, then
cfd2c773
NS
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. */
4dc4a9a5 4916 if (ELIMINATE_COPY_RELOCS
cfd2c773 4917 && !ppc_elf_hash_entry (h)->has_sda_refs
90c14f0c 4918 && htab->elf.target_os != is_vxworks
625af618 4919 && !h->def_regular
287c7eaf 4920 && !alias_readonly_dynrelocs (h))
529fe20e 4921 return TRUE;
252b5132 4922
25dbc73a
AM
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
4dc4a9a5
DJ
4933 Of course, if the symbol is referenced using SDAREL relocs, we
4934 must instead allocate it in .sbss. */
4dc4a9a5 4935 if (ppc_elf_hash_entry (h)->has_sda_refs)
25dbc73a 4936 s = htab->dynsbss;
5474d94f
AM
4937 else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
4938 s = htab->elf.sdynrelro;
25dbc73a 4939 else
9d19e4fd 4940 s = htab->elf.sdynbss;
25dbc73a
AM
4941 BFD_ASSERT (s != NULL);
4942
1d7e9d18 4943 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
25dbc73a
AM
4944 {
4945 asection *srel;
4946
4a7e5234
AM
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. */
4dc4a9a5 4950 if (ppc_elf_hash_entry (h)->has_sda_refs)
25dbc73a 4951 srel = htab->relsbss;
5474d94f
AM
4952 else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
4953 srel = htab->elf.sreldynrelro;
25dbc73a 4954 else
9d19e4fd 4955 srel = htab->elf.srelbss;
25dbc73a
AM
4956 BFD_ASSERT (srel != NULL);
4957 srel->size += sizeof (Elf32_External_Rela);
4958 h->needs_copy = 1;
4959 }
7619e7c7 4960
529fe20e 4961 /* We no longer want dyn_relocs. */
190eb1dd 4962 h->dyn_relocs = NULL;
6cabe1ea 4963 return _bfd_elf_adjust_dynamic_copy (info, h, s);
25dbc73a
AM
4964}
4965\f
ac39eb42
AM
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>. */
0ba07910
AM
4971
4972static bfd_boolean
4973add_stub_sym (struct plt_entry *ent,
4974 struct elf_link_hash_entry *h,
ac39eb42 4975 struct bfd_link_info *info)
0ba07910
AM
4976{
4977 struct elf_link_hash_entry *sh;
4978 size_t len1, len2, len3;
4979 char *name;
ac39eb42
AM
4980 const char *stub;
4981 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
4982
0e1862bb 4983 if (bfd_link_pic (info))
ac39eb42
AM
4984 stub = ".plt_pic32.";
4985 else
4986 stub = ".plt_call32.";
0ba07910
AM
4987
4988 len1 = strlen (h->root.root.string);
ac39eb42 4989 len2 = strlen (stub);
0ba07910
AM
4990 len3 = 0;
4991 if (ent->sec)
4992 len3 = strlen (ent->sec->name);
ac39eb42 4993 name = bfd_malloc (len1 + len2 + len3 + 9);
0ba07910
AM
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);
ac39eb42
AM
4999 memcpy (name + 8 + len3, stub, len2);
5000 memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
0ba07910
AM
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;
2ec55de3 5014 sh->root.linker_def = 1;
0ba07910
AM
5015 }
5016 return TRUE;
5017}
5018
3b36f7e6
AM
5019/* Allocate NEED contiguous space in .got, and return the offset.
5020 Handles allocation of the got header when crossing 32k. */
5021
5022static bfd_vma
5023allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
5024{
5025 bfd_vma where;
4a3dc543 5026 unsigned int max_before_header;
3b36f7e6 5027
4a3dc543 5028 if (htab->plt_type == PLT_VXWORKS)
9d8504b1 5029 {
ce558b89
AM
5030 where = htab->elf.sgot->size;
5031 htab->elf.sgot->size += need;
9d8504b1 5032 }
3b36f7e6
AM
5033 else
5034 {
4a3dc543
RS
5035 max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
5036 if (need <= htab->got_gap)
3b36f7e6 5037 {
4a3dc543
RS
5038 where = max_before_header - htab->got_gap;
5039 htab->got_gap -= need;
5040 }
5041 else
5042 {
ce558b89
AM
5043 if (htab->elf.sgot->size + need > max_before_header
5044 && htab->elf.sgot->size <= max_before_header)
4a3dc543 5045 {
ce558b89
AM
5046 htab->got_gap = max_before_header - htab->elf.sgot->size;
5047 htab->elf.sgot->size = max_before_header + htab->got_header_size;
4a3dc543 5048 }
ce558b89
AM
5049 where = htab->elf.sgot->size;
5050 htab->elf.sgot->size += need;
3b36f7e6 5051 }
3b36f7e6
AM
5052 }
5053 return where;
5054}
5055
f15d0b54
AM
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
5059static inline unsigned int
5060got_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;
b00a0a86 5070 if ((tls_mask & (TLS_TPREL | TLS_GDIE)) != 0)
f15d0b54
AM
5071 need += 4;
5072 if ((tls_mask & TLS_DTPREL) != 0)
5073 need += 4;
5074 }
5075 return need;
5076}
5077
46434633 5078/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
5079
5080static bfd_boolean
46434633
AM
5081ensure_undef_dynamic (struct bfd_link_info *info,
5082 struct elf_link_hash_entry *h)
f0158f44
AM
5083{
5084 struct elf_link_hash_table *htab = elf_hash_table (info);
5085
5086 if (htab->dynamic_sections_created
46434633
AM
5087 && ((info->dynamic_undefined_weak != 0
5088 && h->root.type == bfd_link_hash_undefweak)
5089 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
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
25dbc73a 5097/* Allocate space in associated reloc sections for dynamic relocs. */
252b5132 5098
b34976b6 5099static bfd_boolean
25dbc73a 5100allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
252b5132 5101{
25dbc73a
AM
5102 struct bfd_link_info *info = inf;
5103 struct ppc_elf_link_hash_entry *eh;
7619e7c7 5104 struct ppc_elf_link_hash_table *htab;
6061a67d 5105 struct elf_dyn_relocs *p;
f0158f44 5106 bfd_boolean dyn;
252b5132 5107
25dbc73a 5108 if (h->root.type == bfd_link_hash_indirect)
b34976b6 5109 return TRUE;
252b5132 5110
7619e7c7 5111 htab = ppc_elf_hash_table (info);
25dbc73a 5112 eh = (struct ppc_elf_link_hash_entry *) h;
d3e454b9
AM
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))
25dbc73a 5120 {
d6e14abc
AM
5121 unsigned int need;
5122
46434633
AM
5123 /* Make sure this symbol is output as a dynamic symbol. */
5124 if (!ensure_undef_dynamic (info, &eh->elf))
f0158f44 5125 return FALSE;
252b5132 5126
d6e14abc 5127 need = 0;
37da22e5 5128 if ((eh->tls_mask & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
25dbc73a 5129 {
f749f26e 5130 if (SYMBOL_REFERENCES_LOCAL (info, &eh->elf))
f15d0b54
AM
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
d6e14abc 5136 need += 8;
d6e14abc 5137 }
f15d0b54 5138 need += got_entries_needed (eh->tls_mask);
d6e14abc
AM
5139 if (need == 0)
5140 eh->elf.got.offset = (bfd_vma) -1;
5141 else
5142 {
3b36f7e6 5143 eh->elf.got.offset = allocate_got (htab, need);
f749f26e
AM
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)))
f0158f44
AM
5148 || (htab->elf.dynamic_sections_created
5149 && eh->elf.dynindx != -1
5150 && !SYMBOL_REFERENCES_LOCAL (info, &eh->elf)))
21d68fcd 5151 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &eh->elf))
252b5132 5152 {
f15d0b54 5153 asection *rsec;
f15d0b54 5154
f749f26e
AM
5155 need *= sizeof (Elf32_External_Rela) / 4;
5156 if ((eh->tls_mask & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
f15d0b54
AM
5157 need -= sizeof (Elf32_External_Rela);
5158 rsec = htab->elf.srelgot;
0bed072f 5159 if (eh->elf.type == STT_GNU_IFUNC)
ce558b89 5160 rsec = htab->elf.irelplt;
f15d0b54 5161 rsec->size += need;
252b5132 5162 }
25dbc73a
AM
5163 }
5164 }
5165 else
5166 eh->elf.got.offset = (bfd_vma) -1;
252b5132 5167
954b63d4
AM
5168 /* If no dynamic sections we can't have dynamic relocs, except for
5169 IFUNCs which are handled even in static executables. */
f0158f44
AM
5170 if (!htab->elf.dynamic_sections_created
5171 && h->type != STT_GNU_IFUNC)
190eb1dd 5172 h->dyn_relocs = NULL;
f0158f44 5173
529fe20e
AM
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)
190eb1dd 5177 h->dyn_relocs = NULL;
529fe20e
AM
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))
190eb1dd 5182 h->dyn_relocs = NULL;
529fe20e 5183
190eb1dd 5184 if (h->dyn_relocs == NULL)
287c7eaf 5185 ;
252b5132 5186
25dbc73a
AM
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. */
f0158f44 5192 else if (bfd_link_pic (info))
25dbc73a
AM
5193 {
5194 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 5195 or certain REL relocs (see must_be_dyn_reloc) that can be
25dbc73a
AM
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. */
529fe20e 5200 if (SYMBOL_CALLS_LOCAL (info, h))
25dbc73a 5201 {
6061a67d 5202 struct elf_dyn_relocs **pp;
25dbc73a 5203
190eb1dd 5204 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
252b5132 5205 {
25dbc73a
AM
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;
252b5132 5212 }
25dbc73a 5213 }
252b5132 5214
90c14f0c 5215 if (htab->elf.target_os == is_vxworks)
3348747a 5216 {
6061a67d 5217 struct elf_dyn_relocs **pp;
3348747a 5218
190eb1dd 5219 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
3348747a
NS
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
190eb1dd 5228 if (h->dyn_relocs != NULL)
25dbc73a 5229 {
46434633
AM
5230 /* Make sure this symbol is output as a dynamic symbol. */
5231 if (!ensure_undef_dynamic (info, h))
f0158f44 5232 return FALSE;
25dbc73a
AM
5233 }
5234 }
5235 else if (ELIMINATE_COPY_RELOCS)
5236 {
f0158f44 5237 /* For the non-pic case, discard space for relocs against
25dbc73a
AM
5238 symbols which turn out to need copy relocs or are not
5239 dynamic. */
529fe20e 5240 if (h->dynamic_adjusted
d3e454b9 5241 && !h->def_regular
529fe20e 5242 && !ELF_COMMON_DEF_P (h)
d3e454b9
AM
5243 && !(h->protected_def
5244 && eh->has_addr16_ha
5245 && eh->has_addr16_lo
5246 && htab->params->pic_fixup > 0))
25dbc73a 5247 {
46434633
AM
5248 /* Make sure this symbol is output as a dynamic symbol. */
5249 if (!ensure_undef_dynamic (info, h))
f0158f44 5250 return FALSE;
7619e7c7 5251
f0158f44 5252 if (h->dynindx == -1)
190eb1dd 5253 h->dyn_relocs = NULL;
25dbc73a 5254 }
f0158f44 5255 else
190eb1dd 5256 h->dyn_relocs = NULL;
25dbc73a 5257 }
252b5132 5258
f0158f44 5259 /* Allocate space. */
190eb1dd 5260 for (p = h->dyn_relocs; p != NULL; p = p->next)
25dbc73a
AM
5261 {
5262 asection *sreloc = elf_section_data (p->sec)->sreloc;
f95f8542 5263 if (eh->elf.type == STT_GNU_IFUNC)
ce558b89 5264 sreloc = htab->elf.irelplt;
25dbc73a
AM
5265 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5266 }
252b5132 5267
2d7ad24e
AM
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. */
f0158f44 5274 dyn = htab->elf.dynamic_sections_created && h->dynindx != -1;
2d7ad24e
AM
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
3e04d765 5281 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
5282 && (ppc_elf_hash_entry (h)->tls_mask
5283 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
f0158f44
AM
5284 {
5285 struct plt_entry *ent;
5286 bfd_boolean doneone = FALSE;
2d7ad24e 5287 bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
f0158f44
AM
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)
2d7ad24e
AM
5295 {
5296 if (h->type == STT_GNU_IFUNC)
5297 s = htab->elf.iplt;
5298 else
5299 s = htab->pltlocal;
5300 }
f0158f44
AM
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
2d7ad24e
AM
5311 if (s == htab->pltlocal)
5312 ent->glink_offset = glink_offset;
5313 else
f0158f44 5314 {
2d7ad24e
AM
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;
f0158f44 5330
2d7ad24e
AM
5331 if (htab->params->emit_stub_syms
5332 && !add_stub_sym (ent, h, info))
5333 return FALSE;
5334 }
f0158f44
AM
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)
2d7ad24e
AM
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 }
f0158f44
AM
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
25dbc73a
AM
5445 return TRUE;
5446}
7619e7c7 5447
6177242a
AM
5448static 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
25dbc73a
AM
5462/* Set the sizes of the dynamic sections. */
5463
5464static bfd_boolean
93d1b056 5465ppc_elf_size_dynamic_sections (bfd *output_bfd,
25dbc73a
AM
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;
7fce784e 5472
252b5132 5473#ifdef DEBUG
25dbc73a 5474 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
252b5132 5475#endif
252b5132 5476
25dbc73a
AM
5477 htab = ppc_elf_hash_table (info);
5478 BFD_ASSERT (htab->elf.dynobj != NULL);
252b5132 5479
25dbc73a
AM
5480 if (elf_hash_table (info)->dynamic_sections_created)
5481 {
5482 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 5483 if (bfd_link_executable (info) && !info->nointerp)
25dbc73a 5484 {
3d4d4302 5485 s = bfd_get_linker_section (htab->elf.dynobj, ".interp");
25dbc73a
AM
5486 BFD_ASSERT (s != NULL);
5487 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5488 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5489 }
5490 }
252b5132 5491
4a3dc543 5492 if (htab->plt_type == PLT_OLD)
d7128ce4 5493 htab->got_header_size = 16;
4a3dc543 5494 else if (htab->plt_type == PLT_NEW)
d7128ce4 5495 htab->got_header_size = 12;
252b5132 5496
25dbc73a
AM
5497 /* Set up .got offsets for local syms, and space for local dynamic
5498 relocs. */
c72f2fb2 5499 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
25dbc73a
AM
5500 {
5501 bfd_signed_vma *local_got;
5502 bfd_signed_vma *end_local_got;
e054468f
AM
5503 struct plt_entry **local_plt;
5504 struct plt_entry **end_local_plt;
25dbc73a
AM
5505 char *lgot_masks;
5506 bfd_size_type locsymcount;
5507 Elf_Internal_Shdr *symtab_hdr;
7fce784e 5508
0c8d6e5c 5509 if (!is_ppc_elf (ibfd))
25dbc73a 5510 continue;
7fce784e 5511
25dbc73a
AM
5512 for (s = ibfd->sections; s != NULL; s = s->next)
5513 {
f95f8542 5514 struct ppc_dyn_relocs *p;
252b5132 5515
f95f8542 5516 for (p = ((struct ppc_dyn_relocs *)
25dbc73a
AM
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. */
7fce784e 5528 }
90c14f0c 5529 else if (htab->elf.target_os == is_vxworks
3348747a
NS
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 }
25dbc73a 5536 else if (p->count != 0)
7fce784e 5537 {
25f23106 5538 asection *sreloc = elf_section_data (p->sec)->sreloc;
f95f8542 5539 if (p->ifunc)
ce558b89 5540 sreloc = htab->elf.irelplt;
25f23106 5541 sreloc->size += p->count * sizeof (Elf32_External_Rela);
25dbc73a
AM
5542 if ((p->sec->output_section->flags
5543 & (SEC_READONLY | SEC_ALLOC))
5544 == (SEC_READONLY | SEC_ALLOC))
0caf6c82
L
5545 {
5546 info->flags |= DF_TEXTREL;
871b3ab2 5547 info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
0caf6c82
L
5548 p->sec->owner, p->sec);
5549 }
7fce784e 5550 }
252b5132 5551 }
252b5132 5552 }
252b5132 5553
25dbc73a
AM
5554 local_got = elf_local_got_refcounts (ibfd);
5555 if (!local_got)
5556 continue;
252b5132 5557
0ffa91dd 5558 symtab_hdr = &elf_symtab_hdr (ibfd);
25dbc73a
AM
5559 locsymcount = symtab_hdr->sh_info;
5560 end_local_got = local_got + locsymcount;
e054468f
AM
5561 local_plt = (struct plt_entry **) end_local_got;
5562 end_local_plt = local_plt + locsymcount;
5563 lgot_masks = (char *) end_local_plt;
91d6fa6a 5564
25dbc73a
AM
5565 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
5566 if (*local_got > 0)
5567 {
f15d0b54 5568 unsigned int need;
37da22e5 5569 if ((*lgot_masks & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
f15d0b54
AM
5570 htab->tlsld_got.refcount += 1;
5571 need = got_entries_needed (*lgot_masks);
d6e14abc
AM
5572 if (need == 0)
5573 *local_got = (bfd_vma) -1;
25dbc73a
AM
5574 else
5575 {
3b36f7e6 5576 *local_got = allocate_got (htab, need);
f749f26e
AM
5577 if (bfd_link_pic (info)
5578 && !((*lgot_masks & TLS_TLS) != 0
5579 && bfd_link_executable (info)))
0bed072f 5580 {
f15d0b54 5581 asection *srel;
f15d0b54 5582
f749f26e 5583 need *= sizeof (Elf32_External_Rela) / 4;
f15d0b54 5584 srel = htab->elf.srelgot;
37da22e5 5585 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
ce558b89 5586 srel = htab->elf.irelplt;
f15d0b54 5587 srel->size += need;
0bed072f 5588 }
25dbc73a
AM
5589 }
5590 }
5591 else
5592 *local_got = (bfd_vma) -1;
e054468f 5593
90c14f0c 5594 if (htab->elf.target_os == is_vxworks)
e054468f
AM
5595 continue;
5596
5597 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
2d7ad24e
AM
5598 lgot_masks = (char *) end_local_plt;
5599 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
5600 {
5601 struct plt_entry *ent;
5602 bfd_boolean doneone = FALSE;
2d7ad24e 5603 bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
e054468f
AM
5604
5605 for (ent = *local_plt; ent != NULL; ent = ent->next)
5606 if (ent->plt.refcount > 0)
5607 {
2d7ad24e
AM
5608 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
5609 s = htab->elf.iplt;
3e04d765
AM
5610 else if (htab->can_convert_all_inline_plt
5611 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
5612 {
5613 ent->plt.offset = (bfd_vma) -1;
5614 continue;
5615 }
5616 else
5617 s = htab->pltlocal;
e054468f
AM
5618
5619 if (!doneone)
5620 {
5621 plt_offset = s->size;
5622 s->size += 4;
5623 }
5624 ent->plt.offset = plt_offset;
5625
2d7ad24e 5626 if (s != htab->pltlocal && (!doneone || bfd_link_pic (info)))
e054468f 5627 {
2d7ad24e 5628 s = htab->glink;
e054468f 5629 glink_offset = s->size;
9e390558 5630 s->size += GLINK_ENTRY_SIZE (htab, NULL);
e054468f
AM
5631 }
5632 ent->glink_offset = glink_offset;
5633
5634 if (!doneone)
5635 {
2d7ad24e
AM
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 }
e054468f
AM
5646 doneone = TRUE;
5647 }
5648 }
5649 else
5650 ent->plt.offset = (bfd_vma) -1;
5651 }
25dbc73a 5652 }
252b5132 5653
b7fcf6f6
AM
5654 /* Allocate space for global sym dynamic relocs. */
5655 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
5656
3b36f7e6
AM
5657 if (htab->tlsld_got.refcount > 0)
5658 {
5659 htab->tlsld_got.offset = allocate_got (htab, 8);
f749f26e 5660 if (bfd_link_dll (info))
ce558b89 5661 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
3b36f7e6
AM
5662 }
5663 else
5664 htab->tlsld_got.offset = (bfd_vma) -1;
5665
ce558b89 5666 if (htab->elf.sgot != NULL && htab->plt_type != PLT_VXWORKS)
3b36f7e6
AM
5667 {
5668 unsigned int g_o_t = 32768;
5669
4d962050
AM
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. */
ce558b89 5674 if (htab->elf.sgot->size <= 32768)
3b36f7e6 5675 {
ce558b89 5676 g_o_t = htab->elf.sgot->size;
4a3dc543 5677 if (htab->plt_type == PLT_OLD)
4d962050 5678 g_o_t += 4;
ce558b89 5679 htab->elf.sgot->size += htab->got_header_size;
3b36f7e6 5680 }
3b36f7e6
AM
5681
5682 htab->elf.hgot->root.u.def.value = g_o_t;
5683 }
0e1862bb 5684 if (bfd_link_pic (info))
bd6c6e2b
AM
5685 {
5686 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
93d1b056
AM
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;
bd6c6e2b 5690 }
15bfcc77
AM
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 }
3b36f7e6 5701
e054468f
AM
5702 if (htab->glink != NULL
5703 && htab->glink->size != 0
5704 && htab->elf.dynamic_sections_created)
d7128ce4
AM
5705 {
5706 htab->glink_pltresolve = htab->glink->size;
9e390558
AM
5707 /* Space for the branch table. */
5708 htab->glink->size
5709 += htab->elf.srelplt->size / (sizeof (Elf32_External_Rela) / 4) - 4;
86b9da88 5710 /* Pad out to align the start of PLTresolve. */
5446cbdf
AM
5711 htab->glink->size += -htab->glink->size & (htab->params->ppc476_workaround
5712 ? 63 : 15);
d7128ce4 5713 htab->glink->size += GLINK_PLTRESOLVE;
0ba07910 5714
5446cbdf 5715 if (htab->params->emit_stub_syms)
0ba07910
AM
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;
2ec55de3 5732 sh->root.linker_def = 1;
0ba07910
AM
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;
2ec55de3 5748 sh->root.linker_def = 1;
0ba07910
AM
5749 }
5750 }
d7128ce4
AM
5751 }
5752
6177242a
AM
5753 if (htab->glink != NULL
5754 && htab->glink->size != 0
5755 && htab->glink_eh_frame != NULL
9a2a56cc
AM
5756 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
5757 && _bfd_elf_eh_frame_present (info))
6177242a
AM
5758 {
5759 s = htab->glink_eh_frame;
5760 s->size = sizeof (glink_eh_frame_cie) + 20;
0e1862bb 5761 if (bfd_link_pic (info))
6177242a
AM
5762 {
5763 s->size += 4;
5764 if (htab->glink->size - GLINK_PLTRESOLVE + 8 >= 256)
5765 s->size += 4;
5766 }
5767 }
5768
25dbc73a
AM
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)
252b5132 5773 {
9d8504b1
PB
5774 bfd_boolean strip_section = TRUE;
5775
25dbc73a
AM
5776 if ((s->flags & SEC_LINKER_CREATED) == 0)
5777 continue;
252b5132 5778
ce558b89
AM
5779 if (s == htab->elf.splt
5780 || s == htab->elf.sgot)
25dbc73a 5781 {
9d8504b1
PB
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. */
e054468f 5785 if (htab->elf.hplt != NULL)
9d8504b1 5786 strip_section = FALSE;
25dbc73a
AM
5787 /* Strip this section if we don't need it; see the
5788 comment below. */
5789 }
ce558b89 5790 else if (s == htab->elf.iplt
2d7ad24e 5791 || s == htab->pltlocal
e054468f 5792 || s == htab->glink
6177242a 5793 || s == htab->glink_eh_frame
ce558b89 5794 || s == htab->elf.sgotplt
e054468f 5795 || s == htab->sbss
9d19e4fd 5796 || s == htab->elf.sdynbss
5474d94f 5797 || s == htab->elf.sdynrelro
15bfcc77 5798 || s == htab->dynsbss)
25dbc73a 5799 {
c9a2f333 5800 /* Strip these too. */
25dbc73a 5801 }
15bfcc77
AM
5802 else if (s == htab->sdata[0].section
5803 || s == htab->sdata[1].section)
5804 {
5805 strip_section = (s->flags & SEC_KEEP) == 0;
5806 }
fd361982 5807 else if (CONST_STRNEQ (bfd_section_name (s), ".rela"))
25dbc73a 5808 {
c456f082 5809 if (s->size != 0)
25dbc73a
AM
5810 {
5811 /* Remember whether there are any relocation sections. */
5812 relocs = TRUE;
252b5132 5813
25dbc73a
AM
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;
252b5132
RH
5817 }
5818 }
25dbc73a
AM
5819 else
5820 {
5821 /* It's not one of our sections, so don't allocate space. */
5822 continue;
5823 }
252b5132 5824
9d8504b1 5825 if (s->size == 0 && strip_section)
25dbc73a 5826 {
c456f082
AM
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. */
8423293d 5836 s->flags |= SEC_EXCLUDE;
25dbc73a
AM
5837 continue;
5838 }
7fce784e 5839
d7128ce4 5840 if ((s->flags & SEC_HAS_CONTENTS) == 0)
644285ef
AM
5841 continue;
5842
25dbc73a
AM
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 }
252b5132 5848
25dbc73a 5849 if (htab->elf.dynamic_sections_created)
7619e7c7 5850 {
25dbc73a
AM
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)
252b5132 5858
3084d7a2
L
5859 if (!_bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
5860 relocs))
5861 return FALSE;
7619e7c7 5862
c7e17e05
AM
5863 if (htab->plt_type == PLT_NEW
5864 && htab->glink != NULL
5865 && htab->glink->size != 0)
d7128ce4 5866 {
1fe44d79 5867 if (!add_dynamic_entry (DT_PPC_GOT, 0))
d7128ce4 5868 return FALSE;
5446cbdf 5869 if (!htab->params->no_tls_get_addr_opt
a7f2871e
AM
5870 && htab->tls_get_addr != NULL
5871 && htab->tls_get_addr->plt.plist != NULL
e8910a83 5872 && !add_dynamic_entry (DT_PPC_OPT, PPC_OPT_TLS))
a7f2871e 5873 return FALSE;
d7128ce4 5874 }
7a2b07ff 5875 }
25dbc73a
AM
5876#undef add_dynamic_entry
5877
7e01508c
AM
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
0e1862bb 5904 if (bfd_link_pic (info)
7e01508c
AM
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
7619e7c7
AM
5938 return TRUE;
5939}
0eb4a168 5940
93d1b056
AM
5941/* Arrange to have _SDA_BASE_ or _SDA2_BASE_ stripped from the output
5942 if it looks like nothing is using them. */
5943
5944static void
5945maybe_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
5968void
5969ppc_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
0eb4a168
AM
5981/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5982
5983static bfd_boolean
5984ppc_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}
25dbc73a
AM
5994\f
5995#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
5996
01017ef8
NS
5997/* Relaxation trampolines. r12 is available for clobbering (r11, is
5998 used for some functions that are allowed to break the ABI). */
25dbc73a
AM
5999static const int shared_stub_entry[] =
6000 {
6001 0x7c0802a6, /* mflr 0 */
6002 0x429f0005, /* bcl 20, 31, .Lxxx */
01017ef8
NS
6003 0x7d8802a6, /* mflr 12 */
6004 0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
53291d1f 6005 0x398c0000, /* addi 12, 12, (xxx-.Lxxx)@l */
25dbc73a 6006 0x7c0803a6, /* mtlr 0 */
01017ef8 6007 0x7d8903a6, /* mtctr 12 */
25dbc73a
AM
6008 0x4e800420, /* bctr */
6009 };
6010
6011static const int stub_entry[] =
6012 {
01017ef8
NS
6013 0x3d800000, /* lis 12,xxx@ha */
6014 0x398c0000, /* addi 12,12,xxx@l */
6015 0x7d8903a6, /* mtctr 12 */
25dbc73a
AM
6016 0x4e800420, /* bctr */
6017 };
6018
5446cbdf
AM
6019struct ppc_elf_relax_info
6020{
6021 unsigned int workaround_size;
d3e454b9 6022 unsigned int picfixup_size;
5446cbdf
AM
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
53291d1f 6028 add trampolines at the end of the section. */
5446cbdf 6029
25dbc73a
AM
6030static bfd_boolean
6031ppc_elf_relax_section (bfd *abfd,
6032 asection *isec,
6033 struct bfd_link_info *link_info,
6034 bfd_boolean *again)
6035{
d3e454b9 6036 struct one_branch_fixup
25dbc73a 6037 {
d3e454b9 6038 struct one_branch_fixup *next;
25dbc73a 6039 asection *tsec;
01017ef8
NS
6040 /* Final link, can use the symbol offset. For a
6041 relocatable link we use the symbol's index. */
25dbc73a
AM
6042 bfd_vma toff;
6043 bfd_vma trampoff;
6044 };
7619e7c7 6045
25dbc73a
AM
6046 Elf_Internal_Shdr *symtab_hdr;
6047 bfd_byte *contents = NULL;
6048 Elf_Internal_Sym *isymbuf = NULL;
6049 Elf_Internal_Rela *internal_relocs = NULL;
5446cbdf 6050 Elf_Internal_Rela *irel, *irelend = NULL;
d3e454b9 6051 struct one_branch_fixup *branch_fixups = NULL;
5446cbdf 6052 struct ppc_elf_relax_info *relax_info = NULL;
9bc4e62b 6053 unsigned changes = 0;
5446cbdf 6054 bfd_boolean workaround_change;
d7128ce4 6055 struct ppc_elf_link_hash_table *htab;
d3e454b9 6056 bfd_size_type trampbase, trampoff, newsize, picfixup_size;
a6aa5195 6057 asection *got2;
50248c89 6058 bfd_boolean maybe_pasted;
7619e7c7 6059
25dbc73a 6060 *again = FALSE;
7619e7c7 6061
5446cbdf 6062 /* No need to do anything with non-alloc or non-code sections. */
c87b5a93 6063 if ((isec->flags & SEC_ALLOC) == 0
a8ad78a7 6064 || (isec->flags & SEC_CODE) == 0
5446cbdf
AM
6065 || (isec->flags & SEC_LINKER_CREATED) != 0
6066 || isec->size < 4)
25dbc73a 6067 return TRUE;
7619e7c7 6068
c8a1f254
NS
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. */
0e1862bb 6072 if (bfd_link_relocatable (link_info) && bfd_link_pic (link_info))
795bc6b3 6073 return TRUE;
5b914448 6074
5446cbdf
AM
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
53291d1f
AM
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
d3e454b9
AM
6088 if (htab->params->ppc476_workaround
6089 || htab->params->pic_fixup > 0)
5446cbdf
AM
6090 {
6091 if (elf_section_data (isec)->sec_info == NULL)
6092 {
5446cbdf
AM
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
50248c89
AM
6102 maybe_pasted = (strcmp (isec->output_section->name, ".init") == 0
6103 || strcmp (isec->output_section->name, ".fini") == 0);
25dbc73a 6104 /* Space for a branch around any trampolines. */
5446cbdf
AM
6105 trampoff = trampbase;
6106 if (maybe_pasted && trampbase == isec->rawsize)
50248c89 6107 trampoff += 4;
7619e7c7 6108
0ffa91dd 6109 symtab_hdr = &elf_symtab_hdr (abfd);
d3e454b9
AM
6110 picfixup_size = 0;
6111 if (htab->params->branch_trampolines
6112 || htab->params->pic_fixup > 0)
7619e7c7 6113 {
5446cbdf
AM
6114 /* Get a copy of the native relocations. */
6115 if (isec->reloc_count != 0)
25dbc73a 6116 {
5446cbdf
AM
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;
25dbc73a 6121 }
7619e7c7 6122
5446cbdf
AM
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++)
25dbc73a 6127 {
5446cbdf
AM
6128 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
6129 bfd_vma toff, roff;
6130 asection *tsec;
d3e454b9 6131 struct one_branch_fixup *f;
5446cbdf 6132 size_t insn_offset = 0;
d3e454b9 6133 bfd_vma max_branch_offset = 0, val;
5446cbdf
AM
6134 bfd_byte *hit_addr;
6135 unsigned long t0;
6136 struct elf_link_hash_entry *h;
49c09209 6137 Elf_Internal_Sym *isym;
5446cbdf
AM
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:
23cedd1d 6146 case R_PPC_PLTCALL:
5446cbdf
AM
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;
7fce784e 6155
d3e454b9
AM
6156 case R_PPC_ADDR16_HA:
6157 if (htab->params->pic_fixup > 0)
6158 break;
6159 continue;
6160
5446cbdf
AM
6161 default:
6162 continue;
6163 }
6164
6165 /* Get the value of the symbol referred to by the reloc. */
49c09209
AM
6166 if (!get_sym_h (&h, &isym, &tsec, NULL, &isymbuf,
6167 ELF32_R_SYM (irel->r_info), abfd))
6168 goto error_return;
5446cbdf 6169
49c09209
AM
6170 if (isym != NULL)
6171 {
6172 if (tsec != NULL)
6173 ;
5446cbdf
AM
6174 else if (isym->st_shndx == SHN_ABS)
6175 tsec = bfd_abs_section_ptr;
49199304
AM
6176 else
6177 continue;
5446cbdf
AM
6178
6179 toff = isym->st_value;
6180 sym_type = ELF_ST_TYPE (isym->st_info);
25dbc73a 6181 }
25dbc73a 6182 else
5446cbdf 6183 {
49c09209
AM
6184 if (tsec != NULL)
6185 toff = h->root.u.def.value;
5446cbdf
AM
6186 else if (h->root.type == bfd_link_hash_undefined
6187 || h->root.type == bfd_link_hash_undefweak)
6188 {
49c09209
AM
6189 unsigned long indx;
6190
6191 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
5446cbdf 6192 tsec = bfd_und_section_ptr;
0e1862bb 6193 toff = bfd_link_relocatable (link_info) ? indx : 0;
5446cbdf
AM
6194 }
6195 else
6196 continue;
6197
9f7552cf
AM
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. */
0e1862bb 6201 if (bfd_link_executable (link_info)
9f7552cf
AM
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
5446cbdf
AM
6253 sym_type = h->type;
6254 }
7619e7c7 6255
d3e454b9
AM
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
5446cbdf
AM
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)
25dbc73a 6273 {
5446cbdf
AM
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;
25dbc73a 6278 }
5446cbdf
AM
6279 else if (sym_type == STT_GNU_IFUNC
6280 && elf_local_got_offsets (abfd) != NULL)
c8a1f254 6281 {
5446cbdf
AM
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);
c8a1f254 6286 }
5446cbdf 6287 if (plist != NULL)
a9585d22 6288 {
5446cbdf
AM
6289 bfd_vma addend = 0;
6290 struct plt_entry *ent;
6291
0e1862bb 6292 if (r_type == R_PPC_PLTREL24 && bfd_link_pic (link_info))
5446cbdf
AM
6293 addend = irel->r_addend;
6294 ent = find_plt_ent (plist, got2, addend);
6295 if (ent != NULL)
a9585d22 6296 {
5446cbdf
AM
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 {
ce558b89 6307 tsec = htab->elf.splt;
5446cbdf
AM
6308 toff = ent->plt.offset;
6309 }
de972ffa
AM
6310 }
6311 }
de972ffa 6312
5446cbdf
AM
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;
25dbc73a 6318
49199304
AM
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
5446cbdf
AM
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". */
49199304
AM
6353 if (sym_type == STT_SECTION
6354 && r_type != R_PPC_PLTREL24)
5446cbdf
AM
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
49199304
AM
6362 if (sym_type != STT_SECTION
6363 && r_type != R_PPC_PLTREL24)
5446cbdf
AM
6364 toff += irel->r_addend;
6365 }
6366 /* PLTREL24 addends are special. */
6367 else if (r_type != R_PPC_PLTREL24)
25dbc73a 6368 toff += irel->r_addend;
7619e7c7 6369
5446cbdf 6370 /* Attempted -shared link of non-pic code loses. */
0e1862bb 6371 if ((!bfd_link_relocatable (link_info)
5446cbdf
AM
6372 && tsec == bfd_und_section_ptr)
6373 || tsec->output_section == NULL
6374 || (tsec->owner != NULL
6375 && (tsec->owner->flags & BFD_PLUGIN) != 0))
6376 continue;
7619e7c7 6377
5446cbdf 6378 roff = irel->r_offset;
7de713b9 6379
49199304
AM
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
5446cbdf
AM
6390 /* If the branch is in range, no need to do anything. */
6391 if (tsec != bfd_und_section_ptr
0e1862bb 6392 && (!bfd_link_relocatable (link_info)
5446cbdf
AM
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;
7619e7c7 6398
5446cbdf
AM
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 }
01017ef8 6405
5446cbdf 6406 /* Look for an existing fixup to this address. */
d3e454b9 6407 for (f = branch_fixups; f ; f = f->next)
5446cbdf
AM
6408 if (f->tsec == tsec && f->toff == toff)
6409 break;
252b5132 6410
5446cbdf
AM
6411 if (f == NULL)
6412 {
6413 size_t size;
6414 unsigned long stub_rtype;
25dbc73a 6415
5446cbdf
AM
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;
252b5132 6421
0e1862bb 6422 if (bfd_link_pic (link_info))
5446cbdf
AM
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;
ce558b89 6433 if (tsec == htab->elf.splt
5446cbdf
AM
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 }
252b5132 6440
5446cbdf
AM
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));
d3e454b9 6452 f->next = branch_fixups;
5446cbdf
AM
6453 f->tsec = tsec;
6454 f->toff = toff;
6455 f->trampoff = trampoff;
d3e454b9 6456 branch_fixups = f;
5446cbdf
AM
6457
6458 trampoff += size;
6459 changes++;
25dbc73a
AM
6460 }
6461 else
6462 {
5446cbdf
AM
6463 val = f->trampoff - roff;
6464 if (val >= max_branch_offset)
6465 continue;
252b5132 6466
5446cbdf
AM
6467 /* Nop out the reloc, since we're finalizing things here. */
6468 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6469 }
252b5132 6470
5446cbdf
AM
6471 /* Get the section contents. */
6472 if (contents == NULL)
25dbc73a 6473 {
5446cbdf
AM
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;
25dbc73a 6477 /* Go get them off disk. */
5446cbdf 6478 else if (!bfd_malloc_and_get_section (abfd, isec, &contents))
25dbc73a
AM
6479 goto error_return;
6480 }
252b5132 6481
5446cbdf
AM
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;
252b5132 6494
5446cbdf
AM
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 }
25dbc73a 6504 }
252b5132 6505
d3e454b9 6506 while (branch_fixups != NULL)
25dbc73a 6507 {
d3e454b9
AM
6508 struct one_branch_fixup *f = branch_fixups;
6509 branch_fixups = branch_fixups->next;
25dbc73a
AM
6510 free (f);
6511 }
5446cbdf 6512 }
252b5132 6513
5446cbdf
AM
6514 workaround_change = FALSE;
6515 newsize = trampoff;
795bc6b3 6516 if (htab->params->ppc476_workaround
0e1862bb 6517 && (!bfd_link_relocatable (link_info)
795bc6b3 6518 || isec->output_section->alignment_power >= htab->params->pagesize_p2))
5446cbdf
AM
6519 {
6520 bfd_vma addr, end_addr;
6521 unsigned int crossings;
795bc6b3 6522 bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
5446cbdf
AM
6523
6524 addr = isec->output_section->vma + isec->output_offset;
6128f9cf 6525 end_addr = addr + trampoff;
5446cbdf 6526 addr &= -pagesize;
795bc6b3 6527 crossings = ((end_addr & -pagesize) - addr) >> htab->params->pagesize_p2;
5446cbdf
AM
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. */
1e269e9b 6533 newsize = 15 - ((end_addr - 1) & 15);
5446cbdf
AM
6534 newsize += crossings * 16;
6535 if (relax_info->workaround_size < newsize)
6536 {
6537 relax_info->workaround_size = newsize;
6538 workaround_change = TRUE;
5446cbdf
AM
6539 }
6540 /* Ensure relocate_section is called. */
6541 isec->flags |= SEC_RELOC;
6542 }
6543 newsize = trampoff + relax_info->workaround_size;
6544 }
6545
d3e454b9
AM
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)
5446cbdf
AM
6555 isec->size = newsize;
6556
25dbc73a
AM
6557 if (isymbuf != NULL
6558 && symtab_hdr->contents != (unsigned char *) isymbuf)
252b5132 6559 {
25dbc73a
AM
6560 if (! link_info->keep_memory)
6561 free (isymbuf);
252b5132 6562 else
25dbc73a
AM
6563 {
6564 /* Cache the symbols for elf_link_input_bfd. */
6565 symtab_hdr->contents = (unsigned char *) isymbuf;
6566 }
252b5132
RH
6567 }
6568
25dbc73a
AM
6569 if (contents != NULL
6570 && elf_section_data (isec)->this_hdr.contents != contents)
6571 {
53291d1f 6572 if (!changes && !link_info->keep_memory)
25dbc73a
AM
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 }
252b5132 6580
d3e454b9 6581 changes += picfixup_size;
9bc4e62b 6582 if (changes != 0)
25dbc73a 6583 {
9bc4e62b
NS
6584 /* Append sufficient NOP relocs so we can write out relocation
6585 information for the trampolines. */
d4730f92 6586 Elf_Internal_Shdr *rel_hdr;
9bc4e62b
NS
6587 Elf_Internal_Rela *new_relocs = bfd_malloc ((changes + isec->reloc_count)
6588 * sizeof (*new_relocs));
6589 unsigned ix;
5b914448 6590
9bc4e62b
NS
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)
25dbc73a 6602 free (internal_relocs);
9bc4e62b
NS
6603 elf_section_data (isec)->relocs = new_relocs;
6604 isec->reloc_count += changes;
d4730f92
BS
6605 rel_hdr = _bfd_elf_single_rel_hdr (isec);
6606 rel_hdr->sh_size += changes * rel_hdr->sh_entsize;
25dbc73a 6607 }
c9594989 6608 else if (elf_section_data (isec)->relocs != internal_relocs)
9bc4e62b 6609 free (internal_relocs);
252b5132 6610
5446cbdf 6611 *again = changes != 0 || workaround_change;
b34976b6 6612 return TRUE;
25dbc73a
AM
6613
6614 error_return:
d3e454b9 6615 while (branch_fixups != NULL)
5446cbdf 6616 {
d3e454b9
AM
6617 struct one_branch_fixup *f = branch_fixups;
6618 branch_fixups = branch_fixups->next;
5446cbdf
AM
6619 free (f);
6620 }
c9594989 6621 if ((unsigned char *) isymbuf != symtab_hdr->contents)
25dbc73a 6622 free (isymbuf);
c9594989 6623 if (elf_section_data (isec)->this_hdr.contents != contents)
25dbc73a 6624 free (contents);
c9594989 6625 if (elf_section_data (isec)->relocs != internal_relocs)
25dbc73a
AM
6626 free (internal_relocs);
6627 return FALSE;
252b5132 6628}
252b5132 6629\f
8a696751
AM
6630/* What to do when ld finds relocations against symbols defined in
6631 discarded sections. */
6632
6633static unsigned int
6634ppc_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}
c9a2f333 6644\f
25dbc73a 6645/* Fill in the address for a pointer generated in a linker section. */
252b5132 6646
25dbc73a 6647static bfd_vma
76750a2f 6648elf_finish_pointer_linker_section (bfd *input_bfd,
25dbc73a
AM
6649 elf_linker_section_t *lsect,
6650 struct elf_link_hash_entry *h,
6651 bfd_vma relocation,
2bb04cf2 6652 const Elf_Internal_Rela *rel)
25dbc73a
AM
6653{
6654 elf_linker_section_pointers_t *linker_section_ptr;
252b5132 6655
25dbc73a 6656 BFD_ASSERT (lsect != NULL);
252b5132 6657
25dbc73a 6658 if (h != NULL)
252b5132 6659 {
25dbc73a
AM
6660 /* Handle global symbol. */
6661 struct ppc_elf_link_hash_entry *eh;
252b5132 6662
25dbc73a 6663 eh = (struct ppc_elf_link_hash_entry *) h;
76750a2f
AM
6664 BFD_ASSERT (eh->elf.def_regular);
6665 linker_section_ptr = eh->linker_section_pointer;
25dbc73a
AM
6666 }
6667 else
6668 {
6669 /* Handle local symbol. */
6670 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
76750a2f 6671
0c8d6e5c 6672 BFD_ASSERT (is_ppc_elf (input_bfd));
25dbc73a 6673 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
76750a2f
AM
6674 linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
6675 }
252b5132 6676
76750a2f
AM
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);
25dbc73a 6681
76750a2f
AM
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;
252b5132
RH
6690 }
6691
bd6c6e2b
AM
6692 relocation = (lsect->section->output_section->vma
6693 + lsect->section->output_offset
76750a2f 6694 + linker_section_ptr->offset - 1
bd6c6e2b 6695 - SYM_VAL (lsect->sym));
252b5132 6696
25dbc73a
AM
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
252b5132 6702
bd6c6e2b 6703 return relocation;
252b5132 6704}
25dbc73a 6705
e054468f
AM
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
6710static void
9e390558
AM
6711write_glink_stub (struct elf_link_hash_entry *h, struct plt_entry *ent,
6712 asection *plt_sec, unsigned char *p,
e054468f
AM
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;
9e390558
AM
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 }
e054468f
AM
6741
6742 plt = ((ent->plt.offset & ~1)
6743 + plt_sec->output_section->vma
6744 + plt_sec->output_offset);
e054468f 6745
0e1862bb 6746 if (bfd_link_pic (info))
e054468f
AM
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)
9e390558 6760 bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
e054468f
AM
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);
e054468f
AM
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);
9e390558
AM
6773 }
6774 p += 4;
6775 bfd_put_32 (output_bfd, MTCTR_11, p);
6776 p += 4;
407aa07c
AM
6777 bfd_put_32 (output_bfd, BCTR, p);
6778 p += 4;
9e390558
AM
6779 while (p < end)
6780 {
6781 bfd_put_32 (output_bfd, htab->params->ppc476_workaround ? BA : NOP, p);
e054468f
AM
6782 p += 4;
6783 }
6784}
6785
bd6c6e2b
AM
6786/* Return true if symbol is defined statically. */
6787
6788static bfd_boolean
6789is_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
2d0f3896
AM
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
6801unsigned int
6802_bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
6803{
6804 unsigned int rtra;
6805
2365f8d7 6806 if ((insn & (0x3fu << 26)) != 31 << 26)
2d0f3896
AM
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. */
2365f8d7 6824 insn = (32u | ((insn >> 6) & 0x1f)) << 26;
2d0f3896
AM
6825 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
6826 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
2365f8d7 6827 insn = ((58u | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
2d0f3896
AM
6828 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
6829 /* lwax -> lwa. */
2365f8d7 6830 insn = (58u << 26) | 2;
2d0f3896
AM
6831 else
6832 return 0;
6833 insn |= rtra;
6834 return insn;
6835}
6836
766bc656
AM
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
6841unsigned int
6842_bfd_elf_ppc_at_tprel_transform (unsigned int insn, unsigned int reg)
6843{
6844 if ((insn & (0x1f << 16)) == reg << 16
2365f8d7
AM
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 */
766bc656 6861 && (insn & 3) != 1)
2365f8d7 6862 || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
766bc656
AM
6863 && ((insn & 3) == 0 || (insn & 3) == 3))))
6864 {
6865 insn &= ~(0x1f << 16);
6866 }
6867 else if ((insn & (0x1f << 21)) == reg << 21
2365f8d7
AM
6868 && ((insn & (0x3eu << 26)) == 24u << 26 /* ori, oris */
6869 || (insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */
6870 || (insn & (0x3eu << 26)) == 28u << 26 /* andi,andis */))
766bc656
AM
6871 {
6872 insn &= ~(0x1f << 21);
6873 insn |= (insn & (0x1f << 16)) << 5;
2365f8d7 6874 if ((insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */)
766bc656
AM
6875 insn -= 2 >> 26; /* convert to ori,oris */
6876 }
6877 else
6878 insn = 0;
6879 return insn;
6880}
6881
1fe532cf
AM
6882static bfd_boolean
6883is_insn_ds_form (unsigned int insn)
6884{
2365f8d7
AM
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 */);
1fe532cf
AM
6889}
6890
6891static bfd_boolean
6892is_insn_dq_form (unsigned int insn)
6893{
2365f8d7
AM
6894 return ((insn & (0x3fu << 26)) == 56u << 26 /* lq */
6895 || ((insn & (0x3fu << 26)) == (61u << 26) /* lxv, stxv */
a680de9a 6896 && (insn & 3) == 1));
1fe532cf
AM
6897}
6898
252b5132
RH
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
1049f94e 6908 relocatable output file) adjusting the reloc addend as
252b5132
RH
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
1049f94e 6922 When generating relocatable output, this function must handle
252b5132
RH
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
b34976b6 6928static bfd_boolean
55fd94b0
AM
6929ppc_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)
252b5132 6937{
7619e7c7
AM
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;
c316a17c 6942 Elf_Internal_Rela *wrel;
7619e7c7
AM
6943 Elf_Internal_Rela *relend;
6944 Elf_Internal_Rela outrel;
f95f8542 6945 asection *got2;
252b5132 6946 bfd_vma *local_got_offsets;
b34976b6 6947 bfd_boolean ret = TRUE;
95f0d0d2 6948 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
3348747a 6949 bfd_boolean is_vxworks_tls;
d3e454b9
AM
6950 unsigned int picfixup_size = 0;
6951 struct ppc_elf_relax_info *relax_info = NULL;
b34976b6 6952
252b5132 6953#ifdef DEBUG
871b3ab2 6954 _bfd_error_handler ("ppc_elf_relocate_section called for %pB section %pA, "
d003868e
AM
6955 "%ld relocations%s",
6956 input_bfd, input_section,
6957 (long) input_section->reloc_count,
0e1862bb 6958 (bfd_link_relocatable (info)) ? " (relocatable)" : "");
252b5132
RH
6959#endif
6960
7af5d5c4
AM
6961 if (!is_ppc_elf (input_bfd))
6962 {
6963 bfd_set_error (bfd_error_wrong_format);
6964 return FALSE;
6965 }
6966
a6aa5195
AM
6967 got2 = bfd_get_section_by_name (input_bfd, ".got2");
6968
e47cd125 6969 /* Initialize howto table if not already done. */
8da6118f 6970 if (!ppc_elf_howto_table[R_PPC_ADDR32])
252b5132
RH
6971 ppc_elf_howto_init ();
6972
7619e7c7 6973 htab = ppc_elf_hash_table (info);
252b5132 6974 local_got_offsets = elf_local_got_offsets (input_bfd);
0ffa91dd 6975 symtab_hdr = &elf_symtab_hdr (input_bfd);
7619e7c7 6976 sym_hashes = elf_sym_hashes (input_bfd);
3348747a
NS
6977 /* We have to handle relocations in vxworks .tls_vars sections
6978 specially, because the dynamic loader is 'weird'. */
90c14f0c 6979 is_vxworks_tls = (htab->elf.target_os == is_vxworks && bfd_link_pic (info)
3348747a
NS
6980 && !strcmp (input_section->output_section->name,
6981 ".tls_vars"));
d3e454b9
AM
6982 if (input_section->sec_info_type == SEC_INFO_TYPE_TARGET)
6983 relax_info = elf_section_data (input_section)->sec_info;
c316a17c 6984 rel = wrel = relocs;
7619e7c7 6985 relend = relocs + input_section->reloc_count;
c316a17c 6986 for (; rel < relend; wrel++, rel++)
252b5132 6987 {
7619e7c7
AM
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;
252b5132
RH
6995 reloc_howto_type *howto;
6996 unsigned long r_symndx;
6997 bfd_vma relocation;
91d6fa6a 6998 bfd_vma branch_bit, from;
23cedd1d 6999 bfd_boolean unresolved_reloc, save_unresolved_reloc;
7619e7c7
AM
7000 bfd_boolean warned;
7001 unsigned int tls_type, tls_mask, tls_gd;
08be3224 7002 struct plt_entry **ifunc, **plt_list;
86c95733 7003 struct reloc_howto_struct alt_howto;
7619e7c7 7004
c316a17c 7005 again:
55fd94b0
AM
7006 r_type = ELF32_R_TYPE (rel->r_info);
7007 sym = NULL;
7008 sec = NULL;
7009 h = NULL;
7619e7c7
AM
7010 unresolved_reloc = FALSE;
7011 warned = FALSE;
252b5132 7012 r_symndx = ELF32_R_SYM (rel->r_info);
560e09e9 7013
252b5132
RH
7014 if (r_symndx < symtab_hdr->sh_info)
7015 {
7016 sym = local_syms + r_symndx;
7017 sec = local_sections[r_symndx];
26c61ae5 7018 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
252b5132 7019
8517fae7 7020 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
252b5132
RH
7021 }
7022 else
7023 {
62d887d4
L
7024 bfd_boolean ignored;
7025
b2a8e766
AM
7026 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
7027 r_symndx, symtab_hdr, sym_hashes,
7028 h, sec, relocation,
62d887d4 7029 unresolved_reloc, warned, ignored);
9abc968f 7030
252b5132 7031 sym_name = h->root.root.string;
7619e7c7
AM
7032 }
7033
dbaa2011 7034 if (sec != NULL && discarded_section (sec))
ab96bf03
AM
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];
c316a17c
AM
7042
7043 _bfd_clear_contents (howto, input_bfd, input_section,
0930cb30 7044 contents, rel->r_offset);
c316a17c
AM
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
dcd2b8a0 7050 symbols defined in discarded sections. Not done for
c316a17c
AM
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;
ab96bf03
AM
7058 }
7059
0e1862bb 7060 if (bfd_link_relocatable (info))
ab96bf03
AM
7061 {
7062 if (got2 != NULL
7063 && r_type == R_PPC_PLTREL24
f75e0e33 7064 && rel->r_addend != 0)
ab96bf03
AM
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 }
53291d1f
AM
7070 if (r_type != R_PPC_RELAX_PLT
7071 && r_type != R_PPC_RELAX_PLTREL24
7072 && r_type != R_PPC_RELAX)
c316a17c 7073 goto copy_reloc;
ab96bf03
AM
7074 }
7075
7619e7c7
AM
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;
727fc41e
AM
7082 if (h != NULL)
7083 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
7084 else if (local_got_offsets != NULL)
7619e7c7 7085 {
e054468f 7086 struct plt_entry **local_plt;
727fc41e 7087 char *lgot_masks;
e054468f
AM
7088 local_plt
7089 = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
7090 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
727fc41e 7091 tls_mask = lgot_masks[r_symndx];
7619e7c7
AM
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:
e054468f 7111 if ((tls_mask & TLS_TLS) != 0
7619e7c7
AM
7112 && (tls_mask & TLS_TPREL) == 0)
7113 {
7114 bfd_vma insn;
91d6fa6a 7115
95f0d0d2 7116 insn = bfd_get_32 (input_bfd,
c316a17c 7117 contents + rel->r_offset - d_offset);
7619e7c7
AM
7118 insn &= 31 << 21;
7119 insn |= 0x3c020000; /* addis 0,2,0 */
95f0d0d2 7120 bfd_put_32 (input_bfd, insn,
c316a17c 7121 contents + rel->r_offset - d_offset);
7619e7c7
AM
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:
e054468f 7128 if ((tls_mask & TLS_TLS) != 0
7619e7c7
AM
7129 && (tls_mask & TLS_TPREL) == 0)
7130 {
2d0f3896
AM
7131 bfd_vma insn;
7132
95f0d0d2 7133 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
7134 insn = _bfd_elf_ppc_at_tls_transform (insn, 2);
7135 if (insn == 0)
7619e7c7 7136 abort ();
95f0d0d2 7137 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
7619e7c7
AM
7138 r_type = R_PPC_TPREL16_LO;
7139 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4fe5ca5b 7140
7619e7c7 7141 /* Was PPC_TLS which sits on insn boundary, now
4fe5ca5b
GM
7142 PPC_TPREL16_LO which is at low-order half-word. */
7143 rel->r_offset += d_offset;
7619e7c7
AM
7144 }
7145 break;
7146
7147 case R_PPC_GOT_TLSGD16_HI:
7148 case R_PPC_GOT_TLSGD16_HA:
b00a0a86 7149 tls_gd = TLS_GDIE;
e054468f 7150 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
7619e7c7
AM
7151 goto tls_gdld_hi;
7152 break;
7153
7154 case R_PPC_GOT_TLSLD16_HI:
7155 case R_PPC_GOT_TLSLD16_HA:
e054468f 7156 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
7619e7c7
AM
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 {
4fe5ca5b 7164 rel->r_offset -= d_offset;
95f0d0d2 7165 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
7619e7c7
AM
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:
b00a0a86 7174 tls_gd = TLS_GDIE;
e054468f 7175 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
b7fcf6f6 7176 goto tls_ldgd_opt;
7619e7c7
AM
7177 break;
7178
7179 case R_PPC_GOT_TLSLD16:
7180 case R_PPC_GOT_TLSLD16_LO:
e054468f 7181 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
7619e7c7 7182 {
3a71aa26 7183 unsigned int insn1, insn2;
b7fcf6f6 7184 bfd_vma offset;
7619e7c7 7185
b7fcf6f6 7186 tls_ldgd_opt:
727fc41e
AM
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. */
9737e8af 7193 if (input_section->nomark_tls_get_addr
727fc41e
AM
7194 && rel + 1 < relend
7195 && branch_reloc_hash_match (input_bfd, rel + 1,
7196 htab->tls_get_addr))
7197 offset = rel[1].r_offset;
b86ac8e3
AM
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. */
95f0d0d2 7202 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 7203 contents + rel->r_offset - d_offset);
b7fcf6f6
AM
7204 if ((tls_mask & tls_gd) != 0)
7205 {
7206 /* IE */
b86ac8e3 7207 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 7208 insn1 |= 32u << 26; /* lwz */
727fc41e
AM
7209 if (offset != (bfd_vma) -1)
7210 {
f58d5a2d 7211 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
727fc41e 7212 insn2 = 0x7c631214; /* add 3,3,2 */
95f0d0d2 7213 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 7214 }
b7fcf6f6
AM
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 */
b86ac8e3
AM
7222 insn1 &= 0x1f << 21;
7223 insn1 |= 0x3c020000; /* addis r,2,0 */
7619e7c7
AM
7224 if (tls_gd == 0)
7225 {
b7fcf6f6 7226 /* Was an LD reloc. */
1d483afe
AM
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)
cf35638d 7233 r_symndx = STN_UNDEF;
b7fcf6f6 7234 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 7235 if (r_symndx != STN_UNDEF)
1d483afe
AM
7236 rel->r_addend -= (local_syms[r_symndx].st_value
7237 + sec->output_offset
7238 + sec->output_section->vma);
7619e7c7 7239 }
b7fcf6f6
AM
7240 r_type = R_PPC_TPREL16_HA;
7241 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
727fc41e
AM
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 */
95f0d0d2 7248 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 7249 }
b7fcf6f6 7250 }
95f0d0d2 7251 bfd_put_32 (input_bfd, insn1,
b7fcf6f6 7252 contents + rel->r_offset - d_offset);
b7fcf6f6
AM
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. */
c316a17c 7257 goto again;
7619e7c7 7258 }
252b5132 7259 }
7619e7c7 7260 break;
727fc41e
AM
7261
7262 case R_PPC_TLSGD:
675e2809
AM
7263 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
7264 && rel + 1 < relend)
727fc41e
AM
7265 {
7266 unsigned int insn2;
7267 bfd_vma offset = rel->r_offset;
7268
23cedd1d
AM
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
b00a0a86 7276 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
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);
95f0d0d2 7290 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
7291 /* Zap the reloc on the _tls_get_addr call too. */
7292 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 7293 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
727fc41e
AM
7294 }
7295 break;
7296
7297 case R_PPC_TLSLD:
675e2809
AM
7298 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
7299 && rel + 1 < relend)
727fc41e
AM
7300 {
7301 unsigned int insn2;
7302
23cedd1d
AM
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
727fc41e
AM
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)
cf35638d 7316 r_symndx = STN_UNDEF;
727fc41e 7317 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 7318 if (r_symndx != STN_UNDEF)
727fc41e
AM
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 */
95f0d0d2 7326 bfd_put_32 (input_bfd, insn2,
727fc41e
AM
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);
f58d5a2d 7330 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
c316a17c 7331 goto again;
727fc41e
AM
7332 }
7333 break;
7619e7c7
AM
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;
1a0670f3 7347 /* Fall through. */
7619e7c7 7348
4cc11e76 7349 /* Branch not taken prediction relocations. */
7619e7c7
AM
7350 case R_PPC_ADDR14_BRNTAKEN:
7351 case R_PPC_REL14_BRNTAKEN:
91d6fa6a 7352 {
23cedd1d 7353 unsigned int insn;
7619e7c7 7354
95f0d0d2 7355 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
91d6fa6a
NC
7356 insn &= ~BRANCH_PREDICT_BIT;
7357 insn |= branch_bit;
7619e7c7 7358
91d6fa6a
NC
7359 from = (rel->r_offset
7360 + input_section->output_offset
7361 + input_section->output_section->vma);
7619e7c7 7362
91d6fa6a
NC
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
95f0d0d2 7367 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
91d6fa6a 7368 }
08be3224
AM
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);
2365f8d7 7377 if ((insn & (0x3fu << 26)) == 15u << 26
08be3224
AM
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;
252b5132
RH
7395 }
7396
d3e454b9
AM
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 {
95f0d0d2 7411 insn = bfd_get_32 (input_bfd,
d3e454b9 7412 contents + rel->r_offset - d_offset);
2365f8d7 7413 if ((insn & (0x3fu << 26)) == (15u << 26)
d3e454b9
AM
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
174d0a74 7427 (_("%H: fixup branch overflow\n"),
d3e454b9
AM
7428 input_bfd, input_section, rel->r_offset);
7429
95f0d0d2 7430 bfd_put_32 (input_bfd, B | off,
d3e454b9 7431 contents + rel->r_offset - d_offset);
ce558b89
AM
7432 got_addr = (htab->elf.sgot->output_section->vma
7433 + htab->elf.sgot->output_offset
d3e454b9 7434 + (h->got.offset & ~1));
c316a17c
AM
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;
d3e454b9 7438 insn &= ~0xffff;
4aef7643 7439 insn |= ((unsigned int) (got_addr + 0x8000) >> 16) & 0xffff;
95f0d0d2 7440 bfd_put_32 (input_bfd, insn, p);
d3e454b9
AM
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;
95f0d0d2 7447 bfd_put_32 (input_bfd, insn, p + 4);
d3e454b9 7448
95f0d0d2 7449 bfd_put_32 (input_bfd, B | ((-4 - off) & 0x3ffffff), p + 8);
d3e454b9
AM
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));
c316a17c
AM
7455 wrel++, rel++;
7456 rel->r_offset = wrel[-1].r_offset + 4;
d3e454b9 7457 rel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_LO);
c316a17c 7458 rel->r_addend = wrel[-1].r_addend;
d3e454b9
AM
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
2c4d9cbe 7465 _bfd_error_handler
695344c0 7466 /* xgettext:c-format */
2dcf00ce
AM
7467 (_("%pB(%pA+%#" PRIx64 "): error: "
7468 "%s with unexpected instruction %#x"),
7469 input_bfd, input_section, (uint64_t) rel->r_offset,
d3e454b9
AM
7470 "R_PPC_ADDR16_HA", insn);
7471 }
7472 else if (r_type == R_PPC_ADDR16_LO)
7473 {
95f0d0d2 7474 insn = bfd_get_32 (input_bfd,
d3e454b9 7475 contents + rel->r_offset - d_offset);
2365f8d7
AM
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 */
d3e454b9 7491 && (insn & 3) != 1)
2365f8d7 7492 || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
d3e454b9
AM
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
2c4d9cbe 7501 _bfd_error_handler
695344c0 7502 /* xgettext:c-format */
2dcf00ce
AM
7503 (_("%pB(%pA+%#" PRIx64 "): error: "
7504 "%s with unexpected instruction %#x"),
7505 input_bfd, input_section, (uint64_t) rel->r_offset,
d3e454b9
AM
7506 "R_PPC_ADDR16_LO", insn);
7507 }
7508 }
7509
e054468f 7510 ifunc = NULL;
90c14f0c 7511 if (htab->elf.target_os != is_vxworks)
e054468f 7512 {
de972ffa
AM
7513 struct plt_entry *ent;
7514
e054468f
AM
7515 if (h != NULL)
7516 {
7517 if (h->type == STT_GNU_IFUNC)
7518 ifunc = &h->plt.plist;
7519 }
de972ffa
AM
7520 else if (local_got_offsets != NULL
7521 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
e054468f 7522 {
de972ffa 7523 struct plt_entry **local_plt;
e054468f 7524
de972ffa
AM
7525 local_plt = (struct plt_entry **) (local_got_offsets
7526 + symtab_hdr->sh_info);
7527 ifunc = local_plt + r_symndx;
e054468f 7528 }
e054468f 7529
de972ffa
AM
7530 ent = NULL;
7531 if (ifunc != NULL
0e1862bb 7532 && (!bfd_link_pic (info)
08be3224
AM
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))
de972ffa
AM
7537 {
7538 addend = 0;
08be3224
AM
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))
de972ffa
AM
7544 addend = rel->r_addend;
7545 ent = find_plt_ent (ifunc, got2, addend);
7546 }
7547 if (ent != NULL)
7548 {
888a7fc3
AM
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
695344c0 7566 /* xgettext:c-format */
174d0a74 7567 (_("%X%H: unsupported bss-plt -fPIC ifunc %s\n"),
888a7fc3
AM
7568 input_bfd, input_section, rel->r_offset, sym_name);
7569 }
e054468f
AM
7570
7571 unresolved_reloc = FALSE;
7572 if (htab->plt_type == PLT_NEW
7573 || !htab->elf.dynamic_sections_created
c7e17e05
AM
7574 || h == NULL
7575 || h->dynindx == -1)
e054468f
AM
7576 relocation = (htab->glink->output_section->vma
7577 + htab->glink->output_offset
7578 + (ent->glink_offset & ~1));
7579 else
ce558b89
AM
7580 relocation = (htab->elf.splt->output_section->vma
7581 + htab->elf.splt->output_offset
e054468f
AM
7582 + ent->plt.offset);
7583 }
7584 }
7585
7619e7c7 7586 addend = rel->r_addend;
23cedd1d 7587 save_unresolved_reloc = unresolved_reloc;
7619e7c7 7588 howto = NULL;
55fd94b0
AM
7589 if (r_type < R_PPC_max)
7590 howto = ppc_elf_howto_table[r_type];
9a23f96e 7591
9a23f96e 7592 tls_type = 0;
7619e7c7 7593 switch (r_type)
252b5132
RH
7594 {
7595 default:
cf97bcb0
AM
7596 /* xgettext:c-format */
7597 _bfd_error_handler (_("%pB: %s unsupported"),
7598 input_bfd, howto->name);
252b5132
RH
7599
7600 bfd_set_error (bfd_error_bad_value);
b34976b6 7601 ret = FALSE;
c316a17c 7602 goto copy_reloc;
252b5132 7603
7619e7c7
AM
7604 case R_PPC_NONE:
7605 case R_PPC_TLS:
727fc41e
AM
7606 case R_PPC_TLSGD:
7607 case R_PPC_TLSLD:
7619e7c7
AM
7608 case R_PPC_EMB_MRKREF:
7609 case R_PPC_GNU_VTINHERIT:
7610 case R_PPC_GNU_VTENTRY:
c316a17c 7611 goto copy_reloc;
7595d193 7612
7619e7c7
AM
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:
727fc41e 7649 tls_mask = 0;
7619e7c7
AM
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
ce558b89 7658 if (htab->elf.sgot == NULL)
7619e7c7
AM
7659 abort ();
7660
7661 indx = 0;
7662 if (tls_type == (TLS_TLS | TLS_LD)
f749f26e 7663 && SYMBOL_REFERENCES_LOCAL (info, h))
7619e7c7
AM
7664 offp = &htab->tlsld_got.offset;
7665 else if (h != NULL)
7666 {
f0158f44
AM
7667 if (!htab->elf.dynamic_sections_created
7668 || h->dynindx == -1
7669 || SYMBOL_REFERENCES_LOCAL (info, h)
21d68fcd 7670 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
7619e7c7
AM
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 {
37da22e5
AM
7698 unsigned int tls_m = ((tls_mask & TLS_TLS) != 0
7699 ? tls_mask & (TLS_LD | TLS_GD | TLS_DTPREL
b00a0a86 7700 | TLS_TPREL | TLS_GDIE)
37da22e5 7701 : 0);
70bccea4
AM
7702
7703 if (offp == &htab->tlsld_got.offset)
7704 tls_m = TLS_LD;
f749f26e
AM
7705 else if ((tls_m & TLS_LD) != 0
7706 && SYMBOL_REFERENCES_LOCAL (info, h))
70bccea4
AM
7707 tls_m &= ~TLS_LD;
7708
7709 /* We might have multiple got entries for this sym.
7710 Initialize them all. */
7711 do
7619e7c7 7712 {
70bccea4
AM
7713 int tls_ty = 0;
7714
7715 if ((tls_m & TLS_LD) != 0)
7619e7c7 7716 {
70bccea4
AM
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 }
b00a0a86 7730 else if ((tls_m & (TLS_TPREL | TLS_GDIE)) != 0)
70bccea4
AM
7731 {
7732 tls_ty = TLS_TLS | TLS_TPREL;
7733 tls_m = 0;
7619e7c7 7734 }
7619e7c7 7735
70bccea4 7736 /* Generate relocs for the dynamic linker. */
f0158f44
AM
7737 if (indx != 0
7738 || (bfd_link_pic (info)
7739 && (h == NULL
f749f26e
AM
7740 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
7741 && !(tls_ty != 0
f15d0b54
AM
7742 && bfd_link_executable (info)
7743 && SYMBOL_REFERENCES_LOCAL (info, h))))
7619e7c7 7744 {
ce558b89 7745 asection *rsec = htab->elf.srelgot;
91d6fa6a 7746 bfd_byte * loc;
91e21fb7 7747
0bed072f 7748 if (ifunc != NULL)
82e66161
AM
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 }
ce558b89
AM
7756 outrel.r_offset = (htab->elf.sgot->output_section->vma
7757 + htab->elf.sgot->output_offset
70bccea4 7758 + off);
e515b051 7759 outrel.r_addend = 0;
70bccea4
AM
7760 if (tls_ty & (TLS_LD | TLS_GD))
7761 {
7762 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
70bccea4
AM
7763 if (tls_ty == (TLS_TLS | TLS_GD))
7764 {
25f23106
AM
7765 loc = rsec->contents;
7766 loc += (rsec->reloc_count++
70bccea4
AM
7767 * sizeof (Elf32_External_Rela));
7768 bfd_elf32_swap_reloca_out (output_bfd,
7769 &outrel, loc);
e515b051 7770 outrel.r_offset += 4;
70bccea4
AM
7771 outrel.r_info
7772 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
70bccea4
AM
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);
25f23106 7779 else if (indx != 0)
70bccea4 7780 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
25f23106
AM
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);
d6e14abc 7785 if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
e515b051
AM
7786 {
7787 outrel.r_addend += relocation;
7788 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
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 }
e515b051 7795 }
25f23106
AM
7796 loc = rsec->contents;
7797 loc += (rsec->reloc_count++
70bccea4
AM
7798 * sizeof (Elf32_External_Rela));
7799 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7619e7c7
AM
7800 }
7801
70bccea4
AM
7802 /* Init the .got section contents if we're not
7803 emitting a reloc. */
7804 else
7619e7c7 7805 {
70bccea4
AM
7806 bfd_vma value = relocation;
7807
f0158f44 7808 if (tls_ty != 0)
70bccea4 7809 {
989f9879
AM
7810 if (htab->elf.tls_sec == NULL)
7811 value = 0;
7812 else
7813 {
f0158f44
AM
7814 if (tls_ty & TLS_LD)
7815 value = 0;
7816 else
7817 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 7818 if (tls_ty & TLS_TPREL)
989f9879
AM
7819 value += DTP_OFFSET - TP_OFFSET;
7820 }
70bccea4 7821
f0158f44 7822 if (tls_ty & (TLS_LD | TLS_GD))
7b609f53 7823 {
95f0d0d2 7824 bfd_put_32 (input_bfd, value,
ce558b89 7825 htab->elf.sgot->contents + off + 4);
676ee2b5 7826 value = 1;
7b609f53 7827 }
70bccea4 7828 }
95f0d0d2 7829 bfd_put_32 (input_bfd, value,
ce558b89 7830 htab->elf.sgot->contents + off);
7619e7c7 7831 }
70bccea4
AM
7832
7833 off += 4;
7834 if (tls_ty & (TLS_LD | TLS_GD))
7835 off += 4;
7619e7c7 7836 }
70bccea4
AM
7837 while (tls_m != 0);
7838
7839 off = *offp;
7840 *offp = off | 1;
7619e7c7
AM
7841 }
7842
7843 if (off >= (bfd_vma) -2)
7844 abort ();
7845
70bccea4
AM
7846 if ((tls_type & TLS_TLS) != 0)
7847 {
7848 if (tls_type != (TLS_TLS | TLS_LD))
7849 {
7850 if ((tls_mask & TLS_LD) != 0
f749f26e 7851 && !SYMBOL_REFERENCES_LOCAL (info, h))
70bccea4
AM
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
d3e454b9
AM
7866 /* If here for a picfixup, we're done. */
7867 if (r_type != ELF32_R_TYPE (rel->r_info))
c316a17c 7868 goto copy_reloc;
d3e454b9 7869
ce558b89
AM
7870 relocation = (htab->elf.sgot->output_section->vma
7871 + htab->elf.sgot->output_offset
e87d4038
AM
7872 + off
7873 - SYM_VAL (htab->elf.hgot));
7619e7c7
AM
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)
25f53a85 7880 info->callbacks->einfo
695344c0 7881 /* xgettext:c-format */
174d0a74 7882 (_("%H: non-zero addend on %s reloc against `%s'\n"),
25f53a85 7883 input_bfd, input_section, rel->r_offset,
7b609f53 7884 howto->name,
7619e7c7
AM
7885 sym_name);
7886 }
86c95733 7887 break;
7619e7c7 7888
86c95733 7889 /* Relocations that need no special processing. */
7619e7c7 7890 case R_PPC_LOCAL24PC:
252b5132
RH
7891 /* It makes no sense to point a local relocation
7892 at a symbol not in this object. */
7b609f53 7893 if (unresolved_reloc)
252b5132 7894 {
1a72702b
AM
7895 (*info->callbacks->undefined_symbol) (info,
7896 h->root.root.string,
7897 input_bfd,
7898 input_section,
7899 rel->r_offset,
7900 TRUE);
c316a17c 7901 goto copy_reloc;
252b5132 7902 }
888a7fc3
AM
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. */
695344c0 7913 /* xgettext:c-format */
174d0a74 7914 info->callbacks->einfo (_("%X%H: @local call to ifunc %s\n"),
888a7fc3
AM
7915 input_bfd, input_section, rel->r_offset,
7916 h->root.root.string);
7917 }
252b5132
RH
7918 break;
7919
7619e7c7
AM
7920 case R_PPC_DTPREL16:
7921 case R_PPC_DTPREL16_LO:
7922 case R_PPC_DTPREL16_HI:
7923 case R_PPC_DTPREL16_HA:
989f9879
AM
7924 if (htab->elf.tls_sec != NULL)
7925 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
7926 break;
7927
70bccea4
AM
7928 /* Relocations that may need to be propagated if this is a shared
7929 object. */
7619e7c7
AM
7930 case R_PPC_TPREL16:
7931 case R_PPC_TPREL16_LO:
7932 case R_PPC_TPREL16_HI:
7933 case R_PPC_TPREL16_HA:
766bc656
AM
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;
95f0d0d2 7943 unsigned int insn = bfd_get_32 (input_bfd, p);
766bc656
AM
7944 insn = _bfd_elf_ppc_at_tprel_transform (insn, 2);
7945 if (insn != 0)
95f0d0d2 7946 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
7947 break;
7948 }
989f9879
AM
7949 if (htab->elf.tls_sec != NULL)
7950 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7619e7c7 7951 /* The TPREL16 relocs shouldn't really be used in shared
7c8bbca5
AM
7952 libs or with non-local symbols as that will result in
7953 DT_TEXTREL being set, but support them anyway. */
7619e7c7
AM
7954 goto dodyn;
7955
7956 case R_PPC_TPREL32:
989f9879
AM
7957 if (htab->elf.tls_sec != NULL)
7958 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7619e7c7
AM
7959 goto dodyn;
7960
7961 case R_PPC_DTPREL32:
989f9879
AM
7962 if (htab->elf.tls_sec != NULL)
7963 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
7964 goto dodyn;
7965
e515b051
AM
7966 case R_PPC_DTPMOD32:
7967 relocation = 1;
7968 addend = 0;
7969 goto dodyn;
7970
d7128ce4
AM
7971 case R_PPC_REL16:
7972 case R_PPC_REL16_LO:
7973 case R_PPC_REL16_HI:
7974 case R_PPC_REL16_HA:
a680de9a 7975 case R_PPC_REL16DX_HA:
d7128ce4
AM
7976 break;
7977
7619e7c7 7978 case R_PPC_REL32:
625af618
AM
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
b9c361e0
JL
7992 case R_PPC_VLE_REL8:
7993 case R_PPC_VLE_REL15:
7994 case R_PPC_VLE_REL24:
625af618 7995 case R_PPC_REL24:
7619e7c7
AM
7996 case R_PPC_REL14:
7997 case R_PPC_REL14_BRTAKEN:
7998 case R_PPC_REL14_BRNTAKEN:
252b5132
RH
7999 /* If these relocations are not to a named symbol, they can be
8000 handled right here, no need to bother the dynamic linker. */
89200bf8 8001 if (SYMBOL_CALLS_LOCAL (info, h)
3b36f7e6 8002 || h == htab->elf.hgot)
252b5132 8003 break;
70bccea4 8004 /* fall through */
252b5132 8005
7619e7c7 8006 case R_PPC_ADDR24:
7619e7c7
AM
8007 case R_PPC_ADDR14:
8008 case R_PPC_ADDR14_BRTAKEN:
8009 case R_PPC_ADDR14_BRNTAKEN:
0e1862bb 8010 if (h != NULL && !bfd_link_pic (info))
625af618
AM
8011 break;
8012 /* fall through */
8013
7619e7c7 8014 dodyn:
3348747a
NS
8015 if ((input_section->flags & SEC_ALLOC) == 0
8016 || is_vxworks_tls)
c87b5a93 8017 break;
c87b5a93 8018
287c7eaf
AM
8019 if (bfd_link_pic (info)
8020 ? ((h == NULL
190eb1dd 8021 || h->dyn_relocs != NULL)
287c7eaf
AM
8022 && ((h != NULL && pc_dynrelocs (h))
8023 || must_be_dyn_reloc (info, r_type)))
8024 : (h != NULL
190eb1dd 8025 && h->dyn_relocs != NULL))
252b5132 8026 {
0bb2d96a 8027 int skip;
f95f8542
AM
8028 bfd_byte *loc;
8029 asection *sreloc;
82e66161
AM
8030 long indx = 0;
8031
252b5132 8032#ifdef DEBUG
55fd94b0
AM
8033 fprintf (stderr, "ppc_elf_relocate_section needs to "
8034 "create relocation for %s\n",
70bccea4
AM
8035 (h && h->root.root.string
8036 ? h->root.root.string : "<unknown>"));
252b5132
RH
8037#endif
8038
8039 /* When generating a shared object, these relocations
70bccea4
AM
8040 are copied into the output file to be resolved at run
8041 time. */
0bb2d96a 8042 skip = 0;
5b914448
AM
8043 outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
8044 input_section,
8045 rel->r_offset);
0bb2d96a
JJ
8046 if (outrel.r_offset == (bfd_vma) -1
8047 || outrel.r_offset == (bfd_vma) -2)
8048 skip = (int) outrel.r_offset;
252b5132
RH
8049 outrel.r_offset += (input_section->output_section->vma
8050 + input_section->output_offset);
8051
63f6e94f
AM
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
252b5132
RH
8060 if (skip)
8061 memset (&outrel, 0, sizeof outrel);
21d68fcd 8062 else if (!SYMBOL_REFERENCES_LOCAL (info, h))
252b5132 8063 {
82e66161
AM
8064 indx = h->dynindx;
8065 BFD_ASSERT (indx != -1);
7619e7c7 8066 unresolved_reloc = FALSE;
82e66161 8067 outrel.r_info = ELF32_R_INFO (indx, r_type);
252b5132
RH
8068 outrel.r_addend = rel->r_addend;
8069 }
8070 else
8071 {
47388f4c
AM
8072 outrel.r_addend = relocation + rel->r_addend;
8073
25f23106 8074 if (r_type != R_PPC_ADDR32)
252b5132 8075 {
25f23106
AM
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. */
25f53a85 8090 info->callbacks->einfo
695344c0 8091 /* xgettext:c-format */
174d0a74 8092 (_("%H: relocation %s for indirect "
25f53a85
AM
8093 "function %s unsupported\n"),
8094 input_bfd, input_section, rel->r_offset,
25f23106
AM
8095 howto->name,
8096 sym_name);
8097 ret = FALSE;
8098 }
cf35638d 8099 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
89200bf8 8100 ;
252b5132
RH
8101 else if (sec == NULL || sec->owner == NULL)
8102 {
8103 bfd_set_error (bfd_error_bad_value);
89200bf8 8104 ret = FALSE;
252b5132
RH
8105 }
8106 else
8107 {
8108 asection *osec;
8109
47388f4c
AM
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,
25f23106
AM
8114 but ld.so expects buggy relocs.
8115 FIXME: Why not always use a zero index? */
252b5132 8116 osec = sec->output_section;
f26a3287
AM
8117 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
8118 {
8119 osec = htab->elf.tls_sec;
8120 indx = 0;
8121 }
8122 else
74541ad4 8123 {
74541ad4 8124 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
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);
74541ad4 8131 }
f26a3287
AM
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;
252b5132
RH
8138 }
8139
8140 outrel.r_info = ELF32_R_INFO (indx, r_type);
252b5132 8141 }
25f23106
AM
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);
252b5132
RH
8146 }
8147
82e66161
AM
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
947216bf
AM
8160 loc = sreloc->contents;
8161 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
8162 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132 8163
2243deae 8164 if (skip == -1)
c316a17c 8165 goto copy_reloc;
2243deae 8166
f0158f44
AM
8167 /* This reloc will be computed at runtime. Clear the memory
8168 so that it contains a predictable value for prelink. */
8169 if (!skip)
2243deae
L
8170 {
8171 relocation = howto->pc_relative ? outrel.r_offset : 0;
8172 addend = 0;
8173 break;
8174 }
252b5132 8175 }
252b5132
RH
8176 break;
8177
32af9f6e
AM
8178 case R_PPC_RELAX_PLT:
8179 case R_PPC_RELAX_PLTREL24:
de972ffa
AM
8180 if (h != NULL)
8181 {
32af9f6e
AM
8182 struct plt_entry *ent;
8183 bfd_vma got2_addend = 0;
8184
8185 if (r_type == R_PPC_RELAX_PLTREL24)
8186 {
0e1862bb 8187 if (bfd_link_pic (info))
32af9f6e
AM
8188 got2_addend = addend;
8189 addend = 0;
8190 }
8191 ent = find_plt_ent (&h->plt.plist, got2, got2_addend);
de972ffa
AM
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
ce558b89
AM
8197 relocation = (htab->elf.splt->output_section->vma
8198 + htab->elf.splt->output_offset
de972ffa
AM
8199 + ent->plt.offset);
8200 }
1a0670f3 8201 /* Fall through. */
b4a38de6 8202
32af9f6e 8203 case R_PPC_RELAX:
9abc968f 8204 {
53291d1f
AM
8205 const int *stub;
8206 size_t size;
8207 size_t insn_offset = rel->r_offset;
8208 unsigned int insn;
9abc968f 8209
0e1862bb 8210 if (bfd_link_pic (info))
53291d1f
AM
8211 {
8212 relocation -= (input_section->output_section->vma
8213 + input_section->output_offset
8214 + rel->r_offset - 4);
8215 stub = shared_stub_entry;
95f0d0d2
AM
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);
53291d1f
AM
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 }
9abc968f 8227
9abc968f 8228 relocation += addend;
0e1862bb 8229 if (bfd_link_relocatable (info))
53291d1f
AM
8230 relocation = 0;
8231
8232 /* First insn is HA, second is LO. */
8233 insn = *stub++;
8234 insn |= ((relocation + 0x8000) >> 16) & 0xffff;
95f0d0d2 8235 bfd_put_32 (input_bfd, insn, contents + insn_offset);
53291d1f
AM
8236 insn_offset += 4;
8237
8238 insn = *stub++;
8239 insn |= relocation & 0xffff;
95f0d0d2 8240 bfd_put_32 (input_bfd, insn, contents + insn_offset);
53291d1f
AM
8241 insn_offset += 4;
8242 size -= 2;
8243
8244 while (size != 0)
8245 {
8246 insn = *stub++;
8247 --size;
95f0d0d2 8248 bfd_put_32 (input_bfd, insn, contents + insn_offset);
53291d1f
AM
8249 insn_offset += 4;
8250 }
9bc4e62b
NS
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 */
c316a17c
AM
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);
9abc968f 8263 }
8517fae7 8264 continue;
deaaf2f3 8265
70bccea4 8266 /* Indirect .sdata relocation. */
7619e7c7 8267 case R_PPC_EMB_SDAI16:
c9a2f333 8268 BFD_ASSERT (htab->sdata[0].section != NULL);
bd6c6e2b
AM
8269 if (!is_static_defined (htab->sdata[0].sym))
8270 {
8271 unresolved_reloc = TRUE;
8272 break;
8273 }
7619e7c7 8274 relocation
76750a2f
AM
8275 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
8276 h, relocation, rel);
bd6c6e2b 8277 addend = 0;
252b5132
RH
8278 break;
8279
70bccea4 8280 /* Indirect .sdata2 relocation. */
7619e7c7 8281 case R_PPC_EMB_SDA2I16:
c9a2f333 8282 BFD_ASSERT (htab->sdata[1].section != NULL);
bd6c6e2b
AM
8283 if (!is_static_defined (htab->sdata[1].sym))
8284 {
8285 unresolved_reloc = TRUE;
8286 break;
8287 }
7619e7c7 8288 relocation
76750a2f
AM
8289 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
8290 h, relocation, rel);
bd6c6e2b 8291 addend = 0;
252b5132
RH
8292 break;
8293
70bccea4
AM
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. */
7619e7c7 8298 case R_PPC_TOC16: /* phony GOT16 relocations */
3b4f3a11
AM
8299 if (sec == NULL || sec->output_section == NULL)
8300 {
8301 unresolved_reloc = TRUE;
8302 break;
8303 }
fd361982
AM
8304 BFD_ASSERT (strcmp (bfd_section_name (sec), ".got") == 0
8305 || strcmp (bfd_section_name (sec), ".cgot") == 0);
252b5132 8306
3b4f3a11 8307 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
252b5132
RH
8308 break;
8309
7619e7c7 8310 case R_PPC_PLTREL24:
f75e0e33
AM
8311 if (h != NULL && ifunc == NULL)
8312 {
cbf95972
AM
8313 struct plt_entry *ent;
8314
8315 ent = find_plt_ent (&h->plt.plist, got2,
8316 bfd_link_pic (info) ? addend : 0);
f75e0e33 8317 if (ent == NULL
ce558b89 8318 || htab->elf.splt == NULL)
f75e0e33
AM
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
ce558b89
AM
8334 relocation = (htab->elf.splt->output_section->vma
8335 + htab->elf.splt->output_offset
f75e0e33
AM
8336 + ent->plt.offset);
8337 }
8338 }
252b5132 8339
f75e0e33
AM
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;
8da6118f 8344 break;
252b5132 8345
23cedd1d
AM
8346 case R_PPC_PLTSEQ:
8347 case R_PPC_PLTCALL:
08be3224
AM
8348 case R_PPC_PLT16_LO:
8349 case R_PPC_PLT16_HI:
8350 case R_PPC_PLT16_HA:
2d7ad24e 8351 plt_list = NULL;
08be3224
AM
8352 if (h != NULL)
8353 plt_list = &h->plt.plist;
2d7ad24e
AM
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 }
08be3224
AM
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);
2d7ad24e 8370 if (ent != NULL && ent->plt.offset != (bfd_vma) -1)
08be3224 8371 {
2d7ad24e
AM
8372 asection *plt;
8373
08be3224 8374 unresolved_reloc = FALSE;
2d7ad24e
AM
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
08be3224
AM
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
70bccea4 8405 /* Relocate against _SDA_BASE_. */
7619e7c7 8406 case R_PPC_SDAREL16:
252b5132
RH
8407 {
8408 const char *name;
bd6c6e2b 8409 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
252b5132 8410
bd6c6e2b
AM
8411 if (sec == NULL
8412 || sec->output_section == NULL
8413 || !is_static_defined (sda))
3b4f3a11
AM
8414 {
8415 unresolved_reloc = TRUE;
8416 break;
8417 }
bd6c6e2b 8418 addend -= SYM_VAL (sda);
3b4f3a11 8419
fd361982 8420 name = bfd_section_name (sec->output_section);
93d1b056
AM
8421 if (!(strcmp (name, ".sdata") == 0
8422 || strcmp (name, ".sbss") == 0))
252b5132 8423 {
cf97bcb0 8424 _bfd_error_handler
695344c0 8425 /* xgettext:c-format */
cf97bcb0
AM
8426 (_("%pB: the target (%s) of a %s relocation is "
8427 "in the wrong output section (%s)"),
d003868e 8428 input_bfd,
55fd94b0
AM
8429 sym_name,
8430 howto->name,
8431 name);
252b5132 8432 }
252b5132
RH
8433 }
8434 break;
8435
70bccea4 8436 /* Relocate against _SDA2_BASE_. */
7619e7c7 8437 case R_PPC_EMB_SDA2REL:
252b5132
RH
8438 {
8439 const char *name;
bd6c6e2b 8440 struct elf_link_hash_entry *sda = htab->sdata[1].sym;
252b5132 8441
bd6c6e2b
AM
8442 if (sec == NULL
8443 || sec->output_section == NULL
8444 || !is_static_defined (sda))
3b4f3a11
AM
8445 {
8446 unresolved_reloc = TRUE;
8447 break;
8448 }
bd6c6e2b 8449 addend -= SYM_VAL (sda);
3b4f3a11 8450
fd361982 8451 name = bfd_section_name (sec->output_section);
93d1b056
AM
8452 if (!(strcmp (name, ".sdata2") == 0
8453 || strcmp (name, ".sbss2") == 0))
252b5132 8454 {
cf97bcb0 8455 _bfd_error_handler
695344c0 8456 /* xgettext:c-format */
cf97bcb0
AM
8457 (_("%pB: the target (%s) of a %s relocation is "
8458 "in the wrong output section (%s)"),
d003868e 8459 input_bfd,
55fd94b0
AM
8460 sym_name,
8461 howto->name,
8462 name);
252b5132 8463 }
252b5132
RH
8464 }
8465 break;
8466
b9c361e0 8467 case R_PPC_VLE_LO16A:
86c95733 8468 relocation = relocation + addend;
08dc996f
AM
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);
c316a17c 8472 goto copy_reloc;
b9c361e0
JL
8473
8474 case R_PPC_VLE_LO16D:
86c95733 8475 relocation = relocation + addend;
08dc996f
AM
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);
c316a17c 8479 goto copy_reloc;
b9c361e0
JL
8480
8481 case R_PPC_VLE_HI16A:
86c95733 8482 relocation = (relocation + addend) >> 16;
08dc996f
AM
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);
c316a17c 8486 goto copy_reloc;
b9c361e0
JL
8487
8488 case R_PPC_VLE_HI16D:
86c95733 8489 relocation = (relocation + addend) >> 16;
08dc996f
AM
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);
c316a17c 8493 goto copy_reloc;
b9c361e0
JL
8494
8495 case R_PPC_VLE_HA16A:
86c95733 8496 relocation = (relocation + addend + 0x8000) >> 16;
08dc996f
AM
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);
c316a17c 8500 goto copy_reloc;
b9c361e0
JL
8501
8502 case R_PPC_VLE_HA16D:
86c95733 8503 relocation = (relocation + addend + 0x8000) >> 16;
08dc996f
AM
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);
c316a17c 8507 goto copy_reloc;
b9c361e0 8508
70bccea4 8509 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
7619e7c7 8510 case R_PPC_EMB_SDA21:
b9c361e0 8511 case R_PPC_VLE_SDA21:
7619e7c7 8512 case R_PPC_EMB_RELSDA:
b9c361e0 8513 case R_PPC_VLE_SDA21_LO:
252b5132
RH
8514 {
8515 const char *name;
8516 int reg;
86c95733 8517 unsigned int insn;
bd6c6e2b 8518 struct elf_link_hash_entry *sda = NULL;
252b5132 8519
3b4f3a11
AM
8520 if (sec == NULL || sec->output_section == NULL)
8521 {
8522 unresolved_reloc = TRUE;
8523 break;
8524 }
8525
fd361982 8526 name = bfd_section_name (sec->output_section);
93d1b056
AM
8527 if (strcmp (name, ".sdata") == 0
8528 || strcmp (name, ".sbss") == 0)
252b5132
RH
8529 {
8530 reg = 13;
bd6c6e2b 8531 sda = htab->sdata[0].sym;
252b5132 8532 }
93d1b056
AM
8533 else if (strcmp (name, ".sdata2") == 0
8534 || strcmp (name, ".sbss2") == 0)
252b5132
RH
8535 {
8536 reg = 2;
bd6c6e2b 8537 sda = htab->sdata[1].sym;
252b5132 8538 }
8da6118f
KH
8539 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
8540 || strcmp (name, ".PPC.EMB.sbss0") == 0)
252b5132
RH
8541 {
8542 reg = 0;
8543 }
252b5132
RH
8544 else
8545 {
cf97bcb0 8546 _bfd_error_handler
695344c0 8547 /* xgettext:c-format */
cf97bcb0
AM
8548 (_("%pB: the target (%s) of a %s relocation is "
8549 "in the wrong output section (%s)"),
d003868e 8550 input_bfd,
55fd94b0
AM
8551 sym_name,
8552 howto->name,
8553 name);
252b5132
RH
8554
8555 bfd_set_error (bfd_error_bad_value);
b34976b6 8556 ret = FALSE;
c316a17c 8557 goto copy_reloc;
252b5132
RH
8558 }
8559
bd6c6e2b
AM
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
6f5601c4
AM
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
95f0d0d2 8583 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
b9c361e0
JL
8584 if (reg == 0
8585 && (r_type == R_PPC_VLE_SDA21
8586 || r_type == R_PPC_VLE_SDA21_LO))
8587 {
86c95733
AM
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
95f0d0d2 8603 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
c3301df1
AM
8604
8605 if (r_type == R_PPC_VLE_SDA21
8606 && ((relocation + 0x80000) & 0xffffffff) > 0x100000)
8607 goto overflow;
c316a17c 8608 goto copy_reloc;
b9c361e0 8609 }
6f5601c4
AM
8610 /* Fill in register field. */
8611 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
95f0d0d2 8612 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
252b5132
RH
8613 }
8614 break;
8615
b9c361e0
JL
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;
b9c361e0
JL
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
fd361982 8633 name = bfd_section_name (sec->output_section);
93d1b056
AM
8634 if (strcmp (name, ".sdata") == 0
8635 || strcmp (name, ".sbss") == 0)
5499c7c7 8636 sda = htab->sdata[0].sym;
93d1b056
AM
8637 else if (strcmp (name, ".sdata2") == 0
8638 || strcmp (name, ".sbss2") == 0)
5499c7c7 8639 sda = htab->sdata[1].sym;
b9c361e0
JL
8640 else
8641 {
4eca0228 8642 _bfd_error_handler
695344c0 8643 /* xgettext:c-format */
871b3ab2 8644 (_("%pB: the target (%s) of a %s relocation is "
b9c361e0
JL
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;
c316a17c 8653 goto copy_reloc;
b9c361e0
JL
8654 }
8655
5499c7c7 8656 if (sda == NULL || !is_static_defined (sda))
b9c361e0 8657 {
5499c7c7
AM
8658 unresolved_reloc = TRUE;
8659 break;
b9c361e0 8660 }
5499c7c7 8661 value = relocation + addend - SYM_VAL (sda);
86c95733
AM
8662
8663 if (r_type == R_PPC_VLE_SDAREL_LO16A)
08dc996f
AM
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);
86c95733 8668 else if (r_type == R_PPC_VLE_SDAREL_LO16D)
08dc996f
AM
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);
86c95733 8673 else if (r_type == R_PPC_VLE_SDAREL_HI16A)
b9c361e0 8674 {
86c95733 8675 value = value >> 16;
08dc996f
AM
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);
b9c361e0 8680 }
86c95733 8681 else if (r_type == R_PPC_VLE_SDAREL_HI16D)
b9c361e0 8682 {
86c95733 8683 value = value >> 16;
08dc996f
AM
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);
b9c361e0 8688 }
86c95733 8689 else if (r_type == R_PPC_VLE_SDAREL_HA16A)
b9c361e0 8690 {
86c95733 8691 value = (value + 0x8000) >> 16;
08dc996f
AM
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);
b9c361e0 8696 }
86c95733 8697 else if (r_type == R_PPC_VLE_SDAREL_HA16D)
b9c361e0 8698 {
86c95733 8699 value = (value + 0x8000) >> 16;
08dc996f
AM
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);
b9c361e0
JL
8704 }
8705 }
c316a17c 8706 goto copy_reloc;
b9c361e0 8707
83eef883
AFB
8708 case R_PPC_VLE_ADDR20:
8709 ppc_elf_vle_split20 (output_bfd, contents + rel->r_offset, relocation);
24872cb3 8710 goto copy_reloc;
83eef883 8711
70bccea4 8712 /* Relocate against the beginning of the section. */
7619e7c7
AM
8713 case R_PPC_SECTOFF:
8714 case R_PPC_SECTOFF_LO:
8715 case R_PPC_SECTOFF_HI:
8716 case R_PPC_SECTOFF_HA:
3b4f3a11
AM
8717 if (sec == NULL || sec->output_section == NULL)
8718 {
8719 unresolved_reloc = TRUE;
8720 break;
8721 }
252b5132 8722 addend -= sec->output_section->vma;
252b5132
RH
8723 break;
8724
70bccea4 8725 /* Negative relocations. */
7619e7c7
AM
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:
8da6118f 8731 addend -= 2 * relocation;
252b5132
RH
8732 break;
8733
7619e7c7
AM
8734 case R_PPC_COPY:
8735 case R_PPC_GLOB_DAT:
8736 case R_PPC_JMP_SLOT:
8737 case R_PPC_RELATIVE:
e054468f 8738 case R_PPC_IRELATIVE:
7619e7c7
AM
8739 case R_PPC_PLT32:
8740 case R_PPC_PLTREL32:
7619e7c7
AM
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:
cf97bcb0
AM
8747 /* xgettext:c-format */
8748 _bfd_error_handler (_("%pB: %s unsupported"),
8749 input_bfd, howto->name);
252b5132
RH
8750
8751 bfd_set_error (bfd_error_invalid_operation);
b34976b6 8752 ret = FALSE;
c316a17c 8753 goto copy_reloc;
7619e7c7 8754 }
252b5132 8755
252dcdf4
AM
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
23cedd1d
AM
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
482f3505 8799 (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
23cedd1d
AM
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
482f3505 8817 (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
23cedd1d
AM
8818 input_bfd, input_section, rel->r_offset,
8819 howto->name);
8820 break;
8821 }
8822
7619e7c7
AM
8823 /* Do any further special processing. */
8824 switch (r_type)
8825 {
8826 default:
8827 break;
8828
8829 case R_PPC_ADDR16_HA:
d7128ce4 8830 case R_PPC_REL16_HA:
a680de9a 8831 case R_PPC_REL16DX_HA:
7619e7c7
AM
8832 case R_PPC_SECTOFF_HA:
8833 case R_PPC_TPREL16_HA:
8834 case R_PPC_DTPREL16_HA:
7619e7c7
AM
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
7b609f53 8838 that's not actually defined anywhere. In that case,
7619e7c7
AM
8839 'sec' would be NULL, and we should leave the symbol
8840 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
8841 if (sec == NULL)
8842 break;
1a0670f3 8843 /* Fall through. */
5c5f6e17
AM
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;
7619e7c7 8854 break;
1fe532cf
AM
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
95f0d0d2 8881 insn = bfd_get_32 (input_bfd,
c316a17c 8882 contents + rel->r_offset - d_offset);
1fe532cf
AM
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;
a680de9a
PB
8890 relocation += addend;
8891 addend = insn & mask;
8892 lobit = mask & relocation;
1fe532cf
AM
8893 if (lobit != 0)
8894 {
a680de9a 8895 relocation ^= lobit;
1fe532cf 8896 info->callbacks->einfo
695344c0 8897 /* xgettext:c-format */
174d0a74 8898 (_("%H: error: %s against `%s' not a multiple of %u\n"),
1fe532cf
AM
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 }
1fe532cf
AM
8904 }
8905 break;
252b5132
RH
8906 }
8907
252b5132 8908#ifdef DEBUG
55fd94b0
AM
8909 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
8910 "offset = %ld, addend = %ld\n",
252b5132 8911 howto->name,
8da6118f 8912 (int) r_type,
252b5132
RH
8913 sym_name,
8914 r_symndx,
7619e7c7 8915 (long) rel->r_offset,
8da6118f 8916 (long) addend);
252b5132
RH
8917#endif
8918
7619e7c7
AM
8919 if (unresolved_reloc
8920 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
8921 && h->def_dynamic)
8922 && _bfd_elf_section_offset (output_bfd, info, input_section,
8923 rel->r_offset) != (bfd_vma) -1)
7619e7c7 8924 {
25f53a85 8925 info->callbacks->einfo
695344c0 8926 /* xgettext:c-format */
174d0a74 8927 (_("%H: unresolvable %s relocation against symbol `%s'\n"),
25f53a85 8928 input_bfd, input_section, rel->r_offset,
7b609f53 8929 howto->name,
7619e7c7
AM
8930 sym_name);
8931 ret = FALSE;
8932 }
8933
86c95733
AM
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));
2365f8d7 8948 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac 8949 complain = complain_overflow_bitfield;
2365f8d7
AM
8950 else if ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
8951 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
8952 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
86c95733
AM
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
a680de9a
PB
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);
252b5132 8987
7619e7c7 8988 if (r != bfd_reloc_ok)
252b5132 8989 {
7619e7c7 8990 if (r == bfd_reloc_overflow)
252b5132 8991 {
c3301df1 8992 overflow:
8131c122
AM
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)))
1a72702b
AM
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);
dc1bc0c9
RH
9003 }
9004 else
9005 {
25f53a85 9006 info->callbacks->einfo
695344c0 9007 /* xgettext:c-format */
174d0a74 9008 (_("%H: %s reloc against `%s': error %d\n"),
25f53a85
AM
9009 input_bfd, input_section, rel->r_offset,
9010 howto->name, sym_name, (int) r);
7619e7c7 9011 ret = FALSE;
252b5132
RH
9012 }
9013 }
c316a17c
AM
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;
252b5132
RH
9039 }
9040
252b5132
RH
9041#ifdef DEBUG
9042 fprintf (stderr, "\n");
9043#endif
9044
53291d1f
AM
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
5446cbdf 9055 if (htab->params->ppc476_workaround
795bc6b3 9056 && input_section->sec_info_type == SEC_INFO_TYPE_TARGET
0e1862bb 9057 && (!bfd_link_relocatable (info)
795bc6b3
AM
9058 || (input_section->output_section->alignment_power
9059 >= htab->params->pagesize_p2)))
5446cbdf 9060 {
5446cbdf 9061 bfd_vma start_addr, end_addr, addr;
795bc6b3 9062 bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
5446cbdf 9063
5446cbdf 9064 if (relax_info->workaround_size != 0)
40fd06d1
AM
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 }
5446cbdf
AM
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;
668e22e5
AM
9104 hi = relend;
9105 rel = NULL;
5446cbdf
AM
9106 while (lo < hi)
9107 {
9108 rel = lo + (hi - lo) / 2;
9109 if (rel->r_offset < offset)
9110 lo = rel + 1;
668e22e5 9111 else if (rel->r_offset > offset + 3)
5446cbdf
AM
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
b4ab4364
AM
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 .
1b786873
L
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
b4ab4364 9171 . nop b somewhere_else
1b786873
L
9172 . b somewhere_else nop
9173 . new_page: new_page:
b4ab4364 9174 . */
5446cbdf 9175 insn = bfd_get_32 (input_bfd, contents + offset);
2365f8d7
AM
9176 if ((insn & (0x3fu << 26)) == (18u << 26) /* b,bl,ba,bla */
9177 || ((insn & (0x3fu << 26)) == (16u << 26) /* bc,bcl,bca,bcla*/
07d6d2b8 9178 && (insn & (0x14 << 21)) == (0x14 << 21)) /* with BO=0x14 */
2365f8d7 9179 || ((insn & (0x3fu << 26)) == (19u << 26)
b4ab4364 9180 && (insn & (0x3ff << 1)) == (16u << 1) /* bclr,bclrl */
07d6d2b8 9181 && (insn & (0x14 << 21)) == (0x14 << 21)))/* with BO=0x14 */
5446cbdf
AM
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);
668e22e5
AM
9189
9190 if (rel != NULL
9191 && rel->r_offset >= offset
9192 && rel->r_offset < offset + 4)
9193 {
f8b44781
AM
9194 asection *sreloc;
9195
668e22e5
AM
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;
f8b44781
AM
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 {
1d5c94cb 9228 Elf32_External_Rela *slo, *shi, *srelend;
f8b44781
AM
9229 bfd_vma soffset;
9230
1d5c94cb
AM
9231 slo = (Elf32_External_Rela *) sreloc->contents;
9232 shi = srelend = slo + sreloc->reloc_count;
f8b44781
AM
9233 soffset = (offset + input_section->output_section->vma
9234 + input_section->output_offset);
9235 while (slo < shi)
9236 {
1d5c94cb
AM
9237 Elf32_External_Rela *srel = slo + (shi - slo) / 2;
9238 bfd_elf32_swap_reloca_in (output_bfd, (bfd_byte *) srel,
9239 &outrel);
f8b44781
AM
9240 if (outrel.r_offset < soffset)
9241 slo = srel + 1;
9242 else if (outrel.r_offset > soffset + 3)
9243 shi = srel;
9244 else
9245 {
1d5c94cb 9246 if (srel + 1 != srelend)
f8b44781 9247 {
1d5c94cb
AM
9248 memmove (srel, srel + 1,
9249 (srelend - (srel + 1)) * sizeof (*srel));
9250 srel = srelend - 1;
f8b44781
AM
9251 }
9252 outrel.r_offset += patch_off - offset;
1d5c94cb
AM
9253 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
9254 (bfd_byte *) srel);
f8b44781
AM
9255 break;
9256 }
9257 }
9258 }
668e22e5
AM
9259 }
9260 else
9261 rel = NULL;
9262
2365f8d7 9263 if ((insn & (0x3fu << 26)) == (16u << 26) /* bc */
5446cbdf
AM
9264 && (insn & 2) == 0 /* relative */)
9265 {
9266 bfd_vma delta = ((insn & 0xfffc) ^ 0x8000) - 0x8000;
9267
9268 delta += offset - patch_off;
0e1862bb 9269 if (bfd_link_relocatable (info) && rel != NULL)
668e22e5 9270 delta = 0;
0e1862bb 9271 if (!bfd_link_relocatable (info) && rel != NULL)
668e22e5
AM
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 }
5446cbdf
AM
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 {
668e22e5
AM
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 }
5446cbdf
AM
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
252b5132
RH
9337 return ret;
9338}
252b5132 9339\f
49c09209 9340/* Write out the PLT relocs and entries for H. */
c5fccbec 9341
b34976b6 9342static bfd_boolean
49c09209 9343write_global_sym_plt (struct elf_link_hash_entry *h, void *inf)
e1a9cb8e 9344{
49c09209
AM
9345 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9346 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
a6aa5195
AM
9347 struct plt_entry *ent;
9348 bfd_boolean doneone;
e1a9cb8e 9349
a6aa5195
AM
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;
2d7ad24e
AM
9359 asection *plt = htab->elf.splt;
9360 asection *relplt = htab->elf.srelplt;
e1a9cb8e 9361
e054468f 9362 if (htab->plt_type == PLT_NEW
25f23106
AM
9363 || !htab->elf.dynamic_sections_created
9364 || h->dynindx == -1)
9d8504b1
PB
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
4a3dc543 9371 && htab->plt_type == PLT_OLD)
9d8504b1
PB
9372 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
9373 }
9374
a6aa5195
AM
9375 /* This symbol has an entry in the procedure linkage table.
9376 Set it up. */
e054468f 9377 if (htab->plt_type == PLT_VXWORKS
25f23106
AM
9378 && htab->elf.dynamic_sections_created
9379 && h->dynindx != -1)
a6aa5195 9380 {
9d8504b1
PB
9381 bfd_vma got_offset;
9382 const bfd_vma *plt_entry;
5b914448 9383
9d8504b1
PB
9384 /* The first three entries in .got.plt are reserved. */
9385 got_offset = (reloc_index + 3) * 4;
9386
9387 /* Use the right PLT. */
0e1862bb 9388 plt_entry = bfd_link_pic (info) ? ppc_elf_vxworks_pic_plt_entry
9d8504b1
PB
9389 : ppc_elf_vxworks_plt_entry;
9390
9391 /* Fill in the .plt on VxWorks. */
0e1862bb 9392 if (bfd_link_pic (info))
9d8504b1 9393 {
49c09209 9394 bfd_put_32 (info->output_bfd,
e87d4038 9395 plt_entry[0] | PPC_HA (got_offset),
2d7ad24e 9396 plt->contents + ent->plt.offset + 0);
49c09209 9397 bfd_put_32 (info->output_bfd,
e87d4038 9398 plt_entry[1] | PPC_LO (got_offset),
2d7ad24e 9399 plt->contents + ent->plt.offset + 4);
9d8504b1
PB
9400 }
9401 else
9402 {
e87d4038 9403 bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
9d8504b1 9404
49c09209 9405 bfd_put_32 (info->output_bfd,
e87d4038 9406 plt_entry[0] | PPC_HA (got_loc),
2d7ad24e 9407 plt->contents + ent->plt.offset + 0);
49c09209 9408 bfd_put_32 (info->output_bfd,
e87d4038 9409 plt_entry[1] | PPC_LO (got_loc),
2d7ad24e 9410 plt->contents + ent->plt.offset + 4);
9d8504b1
PB
9411 }
9412
49c09209 9413 bfd_put_32 (info->output_bfd, plt_entry[2],
2d7ad24e 9414 plt->contents + ent->plt.offset + 8);
49c09209 9415 bfd_put_32 (info->output_bfd, plt_entry[3],
2d7ad24e 9416 plt->contents + ent->plt.offset + 12);
9d8504b1
PB
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. */
49c09209 9424 bfd_put_32 (info->output_bfd,
9d8504b1 9425 plt_entry[4] | reloc_index,
2d7ad24e 9426 plt->contents + ent->plt.offset + 16);
9d8504b1
PB
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. */
49c09209 9433 bfd_put_32 (info->output_bfd,
5b914448 9434 (plt_entry[5]
9d8504b1 9435 | (-(ent->plt.offset + 20) & 0x03fffffc)),
2d7ad24e 9436 plt->contents + ent->plt.offset + 20);
49c09209 9437 bfd_put_32 (info->output_bfd, plt_entry[6],
2d7ad24e 9438 plt->contents + ent->plt.offset + 24);
49c09209 9439 bfd_put_32 (info->output_bfd, plt_entry[7],
2d7ad24e 9440 plt->contents + ent->plt.offset + 28);
9d8504b1
PB
9441
9442 /* Fill in the GOT entry corresponding to this PLT slot with
a8685210 9443 the address immediately after the "bctr" instruction
9d8504b1 9444 in this PLT entry. */
2d7ad24e
AM
9445 bfd_put_32 (info->output_bfd, (plt->output_section->vma
9446 + plt->output_offset
9447 + ent->plt.offset + 16),
ce558b89 9448 htab->elf.sgotplt->contents + got_offset);
9d8504b1 9449
0e1862bb 9450 if (!bfd_link_pic (info))
9d8504b1
PB
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. */
2d7ad24e
AM
9459 rela.r_offset = (plt->output_section->vma
9460 + plt->output_offset
9d8504b1 9461 + ent->plt.offset + 2);
636ce3f5 9462 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
9d8504b1
PB
9463 R_PPC_ADDR16_HA);
9464 rela.r_addend = got_offset;
49c09209 9465 bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
9d8504b1
PB
9466 loc += sizeof (Elf32_External_Rela);
9467
9468 /* Provide the @l relocation for the second instruction. */
2d7ad24e
AM
9469 rela.r_offset = (plt->output_section->vma
9470 + plt->output_offset
9d8504b1 9471 + ent->plt.offset + 6);
636ce3f5 9472 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
9d8504b1
PB
9473 R_PPC_ADDR16_LO);
9474 rela.r_addend = got_offset;
49c09209 9475 bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
9d8504b1
PB
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. */
ce558b89
AM
9480 rela.r_offset = (htab->elf.sgotplt->output_section->vma
9481 + htab->elf.sgotplt->output_offset
9d8504b1 9482 + got_offset);
7325306f 9483 rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
9d8504b1
PB
9484 R_PPC_ADDR32);
9485 rela.r_addend = ent->plt.offset + 16;
49c09209 9486 bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
9d8504b1
PB
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. */
ce558b89
AM
9494 rela.r_offset = (htab->elf.sgotplt->output_section->vma
9495 + htab->elf.sgotplt->output_offset
9d8504b1 9496 + got_offset);
2d7ad24e 9497 rela.r_addend = 0;
a6aa5195
AM
9498 }
9499 else
9500 {
2d7ad24e 9501 rela.r_addend = 0;
25f23106
AM
9502 if (!htab->elf.dynamic_sections_created
9503 || h->dynindx == -1)
2d7ad24e
AM
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 }
e054468f 9520
2d7ad24e 9521 if (relplt == NULL)
9d8504b1 9522 {
2d7ad24e
AM
9523 loc = plt->contents + ent->plt.offset;
9524 bfd_put_32 (info->output_bfd, rela.r_addend, loc);
9d8504b1
PB
9525 }
9526 else
9527 {
2d7ad24e
AM
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 }
9d8504b1 9547 }
a6aa5195 9548 }
e1a9cb8e 9549
2d7ad24e 9550 if (relplt != NULL)
e054468f 9551 {
2d7ad24e
AM
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);
82e66161 9573 }
a6aa5195
AM
9574 doneone = TRUE;
9575 }
e1a9cb8e 9576
e054468f 9577 if (htab->plt_type == PLT_NEW
25f23106
AM
9578 || !htab->elf.dynamic_sections_created
9579 || h->dynindx == -1)
a6aa5195 9580 {
a7f2871e 9581 unsigned char *p;
49c09209 9582 asection *plt = htab->elf.splt;
9e390558 9583
25f23106
AM
9584 if (!htab->elf.dynamic_sections_created
9585 || h->dynindx == -1)
2d7ad24e
AM
9586 {
9587 if (h->type == STT_GNU_IFUNC)
9588 plt = htab->elf.iplt;
9589 else
9590 break;
9591 }
e1a9cb8e 9592
a7f2871e 9593 p = (unsigned char *) htab->glink->contents + ent->glink_offset;
49c09209 9594 write_glink_stub (h, ent, plt, p, info);
e1a9cb8e 9595
0e1862bb 9596 if (!bfd_link_pic (info))
e054468f
AM
9597 /* We only need one non-PIC glink stub. */
9598 break;
a6aa5195
AM
9599 }
9600 else
9601 break;
9602 }
49c09209
AM
9603 return TRUE;
9604}
9605
9606/* Finish up PLT handling. */
9607
9608bfd_boolean
9609ppc_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;
8cd1fe1b 9651 unsigned char *p;
49c09209
AM
9652
9653 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
9654 lplt - local_plt, ibfd))
9655 {
c9594989 9656 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209
AM
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
2d7ad24e
AM
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 }
49c09209
AM
9687
9688 rela.r_offset = (ent->plt.offset
9689 + plt->output_offset
9690 + plt->output_section->vma);
49c09209
AM
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);
49c09209 9695
8cd1fe1b 9696 p = (unsigned char *) htab->glink->contents + ent->glink_offset;
49c09209 9697 write_glink_stub (NULL, ent, htab->elf.iplt, p, info);
49c09209
AM
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
9716static bfd_boolean
9717ppc_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 }
e1a9cb8e 9773
25dbc73a
AM
9774 if (h->needs_copy)
9775 {
9776 asection *s;
9777 Elf_Internal_Rela rela;
9778 bfd_byte *loc;
e1a9cb8e 9779
25dbc73a 9780 /* This symbols needs a copy reloc. Set it up. */
e1a9cb8e 9781
25dbc73a
AM
9782#ifdef DEBUG
9783 fprintf (stderr, ", copy");
9784#endif
e1a9cb8e 9785
25dbc73a 9786 BFD_ASSERT (h->dynindx != -1);
86bbe32f 9787
4dc4a9a5 9788 if (ppc_elf_hash_entry (h)->has_sda_refs)
25dbc73a 9789 s = htab->relsbss;
afbf7e8e 9790 else if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f 9791 s = htab->elf.sreldynrelro;
25dbc73a 9792 else
9d19e4fd 9793 s = htab->elf.srelbss;
25dbc73a 9794 BFD_ASSERT (s != NULL);
e1a9cb8e 9795
e87d4038 9796 rela.r_offset = SYM_VAL (h);
25dbc73a
AM
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 }
e1a9cb8e 9802
25dbc73a
AM
9803#ifdef DEBUG
9804 fprintf (stderr, "\n");
9805#endif
e1a9cb8e 9806
25dbc73a
AM
9807 return TRUE;
9808}
9809\f
9810static enum elf_reloc_type_class
7e612e98
AM
9811ppc_elf_reloc_type_class (const struct bfd_link_info *info,
9812 const asection *rel_sec,
9813 const Elf_Internal_Rela *rela)
e1a9cb8e 9814{
7e612e98
AM
9815 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
9816
ce558b89 9817 if (rel_sec == htab->elf.irelplt)
7e612e98
AM
9818 return reloc_class_ifunc;
9819
25dbc73a
AM
9820 switch (ELF32_R_TYPE (rela->r_info))
9821 {
9822 case R_PPC_RELATIVE:
9823 return reloc_class_relative;
25dbc73a
AM
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 }
e1a9cb8e 9831}
25dbc73a
AM
9832\f
9833/* Finish up the dynamic sections. */
e1a9cb8e 9834
25dbc73a
AM
9835static bfd_boolean
9836ppc_elf_finish_dynamic_sections (bfd *output_bfd,
9837 struct bfd_link_info *info)
e1a9cb8e 9838{
25dbc73a
AM
9839 asection *sdyn;
9840 struct ppc_elf_link_hash_table *htab;
1fe44d79 9841 bfd_vma got;
e87d4038
AM
9842 bfd *dynobj;
9843 bfd_boolean ret = TRUE;
e1a9cb8e 9844
25dbc73a
AM
9845#ifdef DEBUG
9846 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
9847#endif
6b0817e5 9848
25dbc73a 9849 htab = ppc_elf_hash_table (info);
ce558b89 9850 dynobj = htab->elf.dynobj;
3d4d4302 9851 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
e1a9cb8e 9852
1fe44d79
AM
9853 got = 0;
9854 if (htab->elf.hgot != NULL)
e87d4038 9855 got = SYM_VAL (htab->elf.hgot);
1fe44d79 9856
25dbc73a 9857 if (htab->elf.dynamic_sections_created)
e1a9cb8e 9858 {
25dbc73a 9859 Elf32_External_Dyn *dyncon, *dynconend;
e1a9cb8e 9860
ce558b89 9861 BFD_ASSERT (htab->elf.splt != NULL && sdyn != NULL);
86bbe32f 9862
25dbc73a
AM
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;
e1a9cb8e 9869
9d8504b1 9870 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
86bbe32f 9871
25dbc73a
AM
9872 switch (dyn.d_tag)
9873 {
9874 case DT_PLTGOT:
90c14f0c 9875 if (htab->elf.target_os == is_vxworks)
ce558b89 9876 s = htab->elf.sgotplt;
9d8504b1 9877 else
ce558b89 9878 s = htab->elf.splt;
25dbc73a
AM
9879 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9880 break;
86bbe32f 9881
25dbc73a 9882 case DT_PLTRELSZ:
ce558b89 9883 dyn.d_un.d_val = htab->elf.srelplt->size;
25dbc73a 9884 break;
e1a9cb8e 9885
25dbc73a 9886 case DT_JMPREL:
ce558b89 9887 s = htab->elf.srelplt;
25dbc73a
AM
9888 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9889 break;
2f89ff8d 9890
1fe44d79
AM
9891 case DT_PPC_GOT:
9892 dyn.d_un.d_ptr = got;
d7128ce4
AM
9893 break;
9894
82e66161
AM
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
25dbc73a 9906 default:
90c14f0c 9907 if (htab->elf.target_os == is_vxworks
7a2b07ff
NS
9908 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
9909 break;
25dbc73a
AM
9910 continue;
9911 }
4c45e5c9 9912
25dbc73a
AM
9913 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
9914 }
9915 }
4c45e5c9 9916
6528b6eb
AM
9917 if (htab->elf.sgot != NULL
9918 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
25dbc73a 9919 {
ce558b89
AM
9920 if (htab->elf.hgot->root.u.def.section == htab->elf.sgot
9921 || htab->elf.hgot->root.u.def.section == htab->elf.sgotplt)
e87d4038 9922 {
e79f5955 9923 unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
2f89ff8d 9924
e87d4038
AM
9925 p += htab->elf.hgot->root.u.def.value;
9926 if (htab->plt_type == PLT_OLD)
e79f5955
AM
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 }
3b36f7e6 9935
e87d4038 9936 if (sdyn != NULL)
e79f5955
AM
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 }
e87d4038
AM
9943 }
9944 else
9945 {
695344c0 9946 /* xgettext:c-format */
cf97bcb0
AM
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));
e87d4038
AM
9951 bfd_set_error (bfd_error_bad_value);
9952 ret = FALSE;
9953 }
25dbc73a 9954
ce558b89 9955 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
25dbc73a
AM
9956 }
9957
9d8504b1 9958 /* Fill in the first entry in the VxWorks procedure linkage table. */
90c14f0c 9959 if (htab->elf.target_os == is_vxworks
6528b6eb
AM
9960 && htab->elf.splt != NULL
9961 && htab->elf.splt->size != 0
9962 && htab->elf.splt->output_section != bfd_abs_section_ptr)
9d8504b1 9963 {
6528b6eb 9964 asection *splt = htab->elf.splt;
9d8504b1 9965 /* Use the right PLT. */
0e1862bb 9966 const bfd_vma *plt_entry = (bfd_link_pic (info)
5b914448
AM
9967 ? ppc_elf_vxworks_pic_plt0_entry
9968 : ppc_elf_vxworks_plt0_entry);
9d8504b1 9969
0e1862bb 9970 if (!bfd_link_pic (info))
9d8504b1 9971 {
e87d4038 9972 bfd_vma got_value = SYM_VAL (htab->elf.hgot);
9d8504b1 9973
e87d4038 9974 bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
9d8504b1 9975 splt->contents + 0);
e87d4038 9976 bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
9d8504b1
PB
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
0e1862bb 9991 if (! bfd_link_pic (info))
9d8504b1
PB
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. */
ce558b89
AM
9999 rela.r_offset = (htab->elf.splt->output_section->vma
10000 + htab->elf.splt->output_offset
9d8504b1 10001 + 2);
636ce3f5 10002 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
9d8504b1
PB
10003 rela.r_addend = 0;
10004 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10005 loc += sizeof (Elf32_External_Rela);
5b914448 10006
9d8504b1 10007 /* Output the @l relocation for the second instruction. */
ce558b89
AM
10008 rela.r_offset = (htab->elf.splt->output_section->vma
10009 + htab->elf.splt->output_offset
9d8504b1 10010 + 6);
636ce3f5 10011 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
9d8504b1
PB
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);
636ce3f5 10024 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
9d8504b1
PB
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);
636ce3f5 10029 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
9d8504b1
PB
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);
7325306f 10034 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
9d8504b1
PB
10035 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
10036 loc += sizeof (Elf32_External_Rela);
10037 }
10038 }
10039 }
10040
e054468f
AM
10041 if (htab->glink != NULL
10042 && htab->glink->contents != NULL
10043 && htab->elf.dynamic_sections_created)
d7128ce4
AM
10044 {
10045 unsigned char *p;
10046 unsigned char *endp;
a6aa5195 10047 bfd_vma res0;
a6aa5195
AM
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.
176a0d42 10059 * # The idea is that the plt call stub loads ctr and r11 with these
a6aa5195
AM
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
07d6d2b8 10076 * sub 11,11,12 # r11 = index * 4
a6aa5195 10077 * addis 12,12,(got+4-1b)@ha
07d6d2b8
AM
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
a6aa5195
AM
10080 * mtctr 0
10081 * add 0,11,11
07d6d2b8 10082 * add 11,0,11 # r11 = index * 12 = reloc offset.
a6aa5195 10083 * bctr
9e390558 10084 *
176a0d42
AM
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
07d6d2b8
AM
10098 * lwz 0,(got+4)@l(12) # got[1] address of dl_runtime_resolve
10099 * addi 11,11,(-res_0)@l # r11 = index * 4
176a0d42
AM
10100 * mtctr 0
10101 * add 0,11,11
07d6d2b8
AM
10102 * lwz 12,(got+8)@l(12) # got[2] contains the map address
10103 * add 11,0,11 # r11 = index * 12 = reloc offset.
176a0d42
AM
10104 * bctr
10105 */
7e8aeb9a 10106
a6aa5195 10107 /* Build the branch table, one for each plt entry (less one),
86b9da88 10108 and perhaps some padding. */
a6aa5195
AM
10109 p = htab->glink->contents;
10110 p += htab->glink_pltresolve;
86b9da88
AM
10111 endp = htab->glink->contents;
10112 endp += htab->glink->size - GLINK_PLTRESOLVE;
da3a2088 10113 while (p < endp - (htab->params->ppc476_workaround ? 0 : 8 * 4))
86b9da88
AM
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
7e8aeb9a 10124 res0 = (htab->glink_pltresolve
86b9da88
AM
10125 + htab->glink->output_section->vma
10126 + htab->glink->output_offset);
10127
da3a2088
AM
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
86b9da88 10161 /* Last comes the PLTresolve stub. */
9e390558 10162 endp = p + GLINK_PLTRESOLVE;
0e1862bb 10163 if (bfd_link_pic (info))
7e8aeb9a 10164 {
a6aa5195 10165 bfd_vma bcl;
d7128ce4 10166
a6aa5195
AM
10167 bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
10168 + htab->glink->output_section->vma
10169 + htab->glink->output_offset);
10170
9e390558
AM
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;
a6aa5195
AM
10187 if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
10188 {
9e390558
AM
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;
a6aa5195
AM
10193 }
10194 else
10195 {
9e390558
AM
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;
a6aa5195 10200 }
9e390558
AM
10201 bfd_put_32 (output_bfd, MTCTR_0, p);
10202 p += 4;
10203 bfd_put_32 (output_bfd, ADD_0_11_11, p);
7e8aeb9a
AM
10204 }
10205 else
d7128ce4 10206 {
9e390558
AM
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;
a6aa5195 10211 if (PPC_HA (got + 4) == PPC_HA (got + 8))
9e390558 10212 bfd_put_32 (output_bfd, LWZ_0_12 + PPC_LO (got + 4), p);
a6aa5195 10213 else
9e390558
AM
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;
407aa07c
AM
10230 bfd_put_32 (output_bfd, BCTR, p);
10231 p += 4;
9e390558
AM
10232 while (p < endp)
10233 {
10234 bfd_put_32 (output_bfd,
10235 htab->params->ppc476_workaround ? BA : NOP, p);
10236 p += 4;
d7128ce4 10237 }
9e390558 10238 BFD_ASSERT (p == endp);
d7128ce4
AM
10239 }
10240
6177242a
AM
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
6177242a
AM
10247 p += sizeof (glink_eh_frame_cie);
10248 /* FDE length. */
6177242a
AM
10249 p += 4;
10250 /* CIE pointer. */
6177242a
AM
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);
6177242a 10259
dbaa2011 10260 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
6177242a
AM
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
e87d4038 10267 return ret;
25dbc73a 10268}
e1a9cb8e 10269\f
6d00b590 10270#define TARGET_LITTLE_SYM powerpc_elf32_le_vec
252b5132 10271#define TARGET_LITTLE_NAME "elf32-powerpcle"
6d00b590 10272#define TARGET_BIG_SYM powerpc_elf32_vec
252b5132
RH
10273#define TARGET_BIG_NAME "elf32-powerpc"
10274#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 10275#define ELF_TARGET_ID PPC32_ELF_DATA
252b5132
RH
10276#define ELF_MACHINE_CODE EM_PPC
10277#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
10278#define ELF_COMMONPAGESIZE 0x1000
10279#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
252b5132
RH
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
5474d94f 10291#define elf_backend_want_dynrelro 1
252b5132 10292#define elf_backend_can_gc_sections 1
51b64d56 10293#define elf_backend_can_refcount 1
b491616a 10294#define elf_backend_rela_normal 1
53291d1f 10295#define elf_backend_caches_rawsize 1
252b5132 10296
43c40ab2 10297#define bfd_elf32_mkobject ppc_elf_mkobject
252b5132 10298#define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
70bccea4 10299#define bfd_elf32_bfd_relax_section ppc_elf_relax_section
252b5132 10300#define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
b9c361e0 10301#define bfd_elf32_bfd_reloc_name_lookup ppc_elf_reloc_name_lookup
252b5132 10302#define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
70bccea4 10303#define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
468392fb 10304#define bfd_elf32_get_synthetic_symtab ppc_elf_get_synthetic_symtab
252b5132 10305
feee612b 10306#define elf_backend_object_p ppc_elf_object_p
252b5132 10307#define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
252b5132
RH
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
c0e331c7 10312#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
7fce784e 10313#define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
252b5132
RH
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
0eb4a168 10317#define elf_backend_hash_symbol ppc_elf_hash_symbol
252b5132
RH
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
954b63d4 10322#define elf_backend_modify_segment_map ppc_elf_modify_segment_map
c5fccbec
DJ
10323#define elf_backend_grok_prstatus ppc_elf_grok_prstatus
10324#define elf_backend_grok_psinfo ppc_elf_grok_psinfo
183e98be 10325#define elf_backend_write_core_note ppc_elf_write_core_note
29c2fb7c 10326#define elf_backend_reloc_type_class ppc_elf_reloc_type_class
70bccea4
AM
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
551b43fd 10330#define elf_backend_get_sec_type_attr ppc_elf_get_sec_type_attr
4c45e5c9 10331#define elf_backend_plt_sym_val ppc_elf_plt_sym_val
8a696751 10332#define elf_backend_action_discarded ppc_elf_action_discarded
74541ad4 10333#define elf_backend_init_index_section _bfd_elf_init_1_index_section
b9c361e0 10334#define elf_backend_lookup_section_flags_hook ppc_elf_lookup_section_flags
252b5132
RH
10335
10336#include "elf32-target.h"
9d8504b1 10337
7b8e7dad
AM
10338/* FreeBSD Target */
10339
10340#undef TARGET_LITTLE_SYM
10341#undef TARGET_LITTLE_NAME
10342
10343#undef TARGET_BIG_SYM
6d00b590 10344#define TARGET_BIG_SYM powerpc_elf32_fbsd_vec
7b8e7dad
AM
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
9d8504b1
PB
10356/* VxWorks Target */
10357
10358#undef TARGET_LITTLE_SYM
10359#undef TARGET_LITTLE_NAME
10360
10361#undef TARGET_BIG_SYM
6d00b590 10362#define TARGET_BIG_SYM powerpc_elf32_vxworks_vec
9d8504b1
PB
10363#undef TARGET_BIG_NAME
10364#define TARGET_BIG_NAME "elf32-powerpc-vxworks"
10365
7b8e7dad
AM
10366#undef ELF_OSABI
10367
90c14f0c
L
10368#undef ELF_TARGET_OS
10369#define ELF_TARGET_OS is_vxworks
10370
d5e3d971
PB
10371/* VxWorks uses the elf default section flags for .plt. */
10372static const struct bfd_elf_special_section *
4aef7643 10373ppc_elf_vxworks_get_sec_type_attr (bfd *abfd, asection *sec)
9d8504b1 10374{
d5e3d971
PB
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}
9d8504b1
PB
10383
10384/* Like ppc_elf_link_hash_table_create, but overrides
10385 appropriately for VxWorks. */
10386static struct bfd_link_hash_table *
10387ppc_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
954b63d4 10395 = (struct ppc_elf_link_hash_table *)ret;
4a3dc543 10396 htab->plt_type = PLT_VXWORKS;
9d8504b1
PB
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. */
10405static bfd_boolean
10406ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
10407 struct bfd_link_info *info,
10408 Elf_Internal_Sym *sym,
4aef7643
AM
10409 const char **namep,
10410 flagword *flagsp,
9d8504b1
PB
10411 asection **secp,
10412 bfd_vma *valp)
10413{
4aef7643
AM
10414 if (!elf_vxworks_add_symbol_hook (abfd, info, sym, namep, flagsp, secp,
10415 valp))
9d8504b1
PB
10416 return FALSE;
10417
4aef7643 10418 return ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp);
9d8504b1
PB
10419}
10420
cc364be6
AM
10421static bfd_boolean
10422ppc_elf_vxworks_final_write_processing (bfd *abfd)
9d8504b1 10423{
cc364be6
AM
10424 ppc_final_write_processing (abfd);
10425 return elf_vxworks_final_write_processing (abfd);
9d8504b1
PB
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
64f52338
AM
10442#undef elf_backend_dtrel_excludes_plt
10443#define elf_backend_dtrel_excludes_plt 1
9d8504b1 10444
468392fb
AM
10445#undef bfd_elf32_get_synthetic_symtab
10446
9d8504b1
PB
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
9d8504b1
PB
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 \
9c72ff84 10455 elf_vxworks_link_output_symbol_hook
9d8504b1
PB
10456#undef elf_backend_final_write_processing
10457#define elf_backend_final_write_processing \
10458 ppc_elf_vxworks_final_write_processing
d5e3d971
PB
10459#undef elf_backend_get_sec_type_attr
10460#define elf_backend_get_sec_type_attr \
10461 ppc_elf_vxworks_get_sec_type_attr
9d8504b1
PB
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"
This page took 2.879325 seconds and 4 git commands to generate.