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