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