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