* elf32-ppc.c (ppc_elf_check_relocs): Remove dead ifunc code.
[deliverable/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 */
1383 TRUE, /* pc_relative */
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{
d30e8e7c 1997 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
25dbc73a
AM
1998 shdr->sh_flags |= SHF_EXCLUDE;
1999
2000 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2001 shdr->sh_type = SHT_ORDERED;
2002
2003 return TRUE;
2004}
2005
2006/* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
2007 need to bump up the number of section headers. */
2008
2009static int
a6b96beb
AM
2010ppc_elf_additional_program_headers (bfd *abfd,
2011 struct bfd_link_info *info ATTRIBUTE_UNUSED)
25dbc73a
AM
2012{
2013 asection *s;
2014 int ret = 0;
d1c6de6f 2015
25dbc73a
AM
2016 s = bfd_get_section_by_name (abfd, ".sbss2");
2017 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2018 ++ret;
d1c6de6f 2019
25dbc73a
AM
2020 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2021 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2022 ++ret;
deaaf2f3 2023
25dbc73a
AM
2024 return ret;
2025}
deaaf2f3 2026
25dbc73a
AM
2027/* Add extra PPC sections -- Note, for now, make .sbss2 and
2028 .PPC.EMB.sbss0 a normal section, and not a bss section so
2029 that the linker doesn't crater when trying to make more than
2030 2 sections. */
e656e369 2031
b35d266b 2032static const struct bfd_elf_special_section ppc_elf_special_sections[] =
7f4d3958 2033{
0112cd26
NC
2034 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
2035 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2036 { STRING_COMMA_LEN (".sbss2"), -2, SHT_PROGBITS, SHF_ALLOC },
2037 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2038 { STRING_COMMA_LEN (".sdata2"), -2, SHT_PROGBITS, SHF_ALLOC },
2039 { STRING_COMMA_LEN (".tags"), 0, SHT_ORDERED, SHF_ALLOC },
2040 { STRING_COMMA_LEN (".PPC.EMB.apuinfo"), 0, SHT_NOTE, 0 },
2041 { STRING_COMMA_LEN (".PPC.EMB.sbss0"), 0, SHT_PROGBITS, SHF_ALLOC },
2042 { STRING_COMMA_LEN (".PPC.EMB.sdata0"), 0, SHT_PROGBITS, SHF_ALLOC },
2043 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2044};
2045
551b43fd
AM
2046/* This is what we want for new plt/got. */
2047static struct bfd_elf_special_section ppc_alt_plt =
0112cd26 2048 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC };
551b43fd
AM
2049
2050static const struct bfd_elf_special_section *
2051ppc_elf_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
7f4d3958 2052{
b35d266b 2053 const struct bfd_elf_special_section *ssect;
551b43fd
AM
2054
2055 /* See if this is one of the special sections. */
2056 if (sec->name == NULL)
2057 return NULL;
2058
2059 ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
2060 sec->use_rela_p);
2061 if (ssect != NULL)
2062 {
2063 if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
2064 ssect = &ppc_alt_plt;
2065 return ssect;
2066 }
2067
2068 return _bfd_elf_get_sec_type_attr (abfd, sec);
2069}
25dbc73a
AM
2070\f
2071/* Very simple linked list structure for recording apuinfo values. */
2072typedef struct apuinfo_list
2073{
2074 struct apuinfo_list *next;
2075 unsigned long value;
2076}
2077apuinfo_list;
e656e369 2078
25dbc73a 2079static apuinfo_list *head;
deaaf2f3 2080
deaaf2f3 2081
25dbc73a
AM
2082static void
2083apuinfo_list_init (void)
2084{
2085 head = NULL;
2086}
9abc968f 2087
25dbc73a
AM
2088static void
2089apuinfo_list_add (unsigned long value)
2090{
2091 apuinfo_list *entry = head;
deaaf2f3 2092
25dbc73a
AM
2093 while (entry != NULL)
2094 {
2095 if (entry->value == value)
2096 return;
2097 entry = entry->next;
2098 }
b4a38de6 2099
25dbc73a
AM
2100 entry = bfd_malloc (sizeof (* entry));
2101 if (entry == NULL)
2102 return;
e656e369 2103
25dbc73a
AM
2104 entry->value = value;
2105 entry->next = head;
2106 head = entry;
2107}
9abc968f 2108
25dbc73a
AM
2109static unsigned
2110apuinfo_list_length (void)
2111{
2112 apuinfo_list *entry;
2113 unsigned long count;
9abc968f 2114
25dbc73a
AM
2115 for (entry = head, count = 0;
2116 entry;
2117 entry = entry->next)
2118 ++ count;
e656e369 2119
25dbc73a
AM
2120 return count;
2121}
eea6121a 2122
25dbc73a
AM
2123static inline unsigned long
2124apuinfo_list_element (unsigned long number)
2125{
2126 apuinfo_list * entry;
e656e369 2127
25dbc73a
AM
2128 for (entry = head;
2129 entry && number --;
2130 entry = entry->next)
2131 ;
252b5132 2132
25dbc73a
AM
2133 return entry ? entry->value : 0;
2134}
2135
2136static void
2137apuinfo_list_finish (void)
2138{
2139 apuinfo_list *entry;
2140
2141 for (entry = head; entry;)
252b5132 2142 {
25dbc73a
AM
2143 apuinfo_list *next = entry->next;
2144 free (entry);
2145 entry = next;
2146 }
9abc968f 2147
25dbc73a
AM
2148 head = NULL;
2149}
9abc968f 2150
25dbc73a
AM
2151#define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
2152#define APUINFO_LABEL "APUinfo"
9abc968f 2153
25dbc73a
AM
2154/* Scan the input BFDs and create a linked list of
2155 the APUinfo values that will need to be emitted. */
9abc968f 2156
25dbc73a
AM
2157static void
2158ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
2159{
2160 bfd *ibfd;
2161 asection *asec;
2162 char *buffer;
2163 unsigned num_input_sections;
2164 bfd_size_type output_section_size;
2165 unsigned i;
2166 unsigned num_entries;
2167 unsigned long offset;
2168 unsigned long length;
2169 const char *error_message = NULL;
9abc968f 2170
25dbc73a
AM
2171 if (link_info == NULL)
2172 return;
9abc968f 2173
25dbc73a
AM
2174 /* Scan the input bfds, looking for apuinfo sections. */
2175 num_input_sections = 0;
2176 output_section_size = 0;
252b5132 2177
25dbc73a 2178 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
252b5132 2179 {
25dbc73a
AM
2180 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2181 if (asec)
252b5132 2182 {
25dbc73a
AM
2183 ++ num_input_sections;
2184 output_section_size += asec->size;
252b5132 2185 }
252b5132
RH
2186 }
2187
25dbc73a
AM
2188 /* We need at least one input sections
2189 in order to make merging worthwhile. */
2190 if (num_input_sections < 1)
2191 return;
deaaf2f3 2192
25dbc73a
AM
2193 /* Just make sure that the output section exists as well. */
2194 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2195 if (asec == NULL)
2196 return;
252b5132 2197
25dbc73a
AM
2198 /* Allocate a buffer for the contents of the input sections. */
2199 buffer = bfd_malloc (output_section_size);
2200 if (buffer == NULL)
2201 return;
252b5132 2202
25dbc73a
AM
2203 offset = 0;
2204 apuinfo_list_init ();
252b5132 2205
25dbc73a
AM
2206 /* Read in the input sections contents. */
2207 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
252b5132 2208 {
25dbc73a
AM
2209 unsigned long datum;
2210 char *ptr;
252b5132 2211
25dbc73a
AM
2212 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2213 if (asec == NULL)
2214 continue;
252b5132 2215
25dbc73a
AM
2216 length = asec->size;
2217 if (length < 24)
2218 {
2219 error_message = _("corrupt or empty %s section in %B");
2220 goto fail;
2221 }
252b5132 2222
25dbc73a
AM
2223 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
2224 || (bfd_bread (buffer + offset, length, ibfd) != length))
2225 {
2226 error_message = _("unable to read in %s section from %B");
2227 goto fail;
2228 }
252b5132 2229
25dbc73a
AM
2230 /* Process the contents of the section. */
2231 ptr = buffer + offset;
2232 error_message = _("corrupt %s section in %B");
252b5132 2233
25dbc73a
AM
2234 /* Verify the contents of the header. Note - we have to
2235 extract the values this way in order to allow for a
2236 host whose endian-ness is different from the target. */
2237 datum = bfd_get_32 (ibfd, ptr);
2238 if (datum != sizeof APUINFO_LABEL)
2239 goto fail;
252b5132 2240
25dbc73a
AM
2241 datum = bfd_get_32 (ibfd, ptr + 8);
2242 if (datum != 0x2)
2243 goto fail;
252b5132 2244
25dbc73a
AM
2245 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
2246 goto fail;
252b5132 2247
25dbc73a
AM
2248 /* Get the number of bytes used for apuinfo entries. */
2249 datum = bfd_get_32 (ibfd, ptr + 4);
2250 if (datum + 20 != length)
2251 goto fail;
2252
2253 /* Make sure that we do not run off the end of the section. */
2254 if (offset + length > output_section_size)
2255 goto fail;
2256
2257 /* Scan the apuinfo section, building a list of apuinfo numbers. */
2258 for (i = 0; i < datum; i += 4)
2259 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
2260
2261 /* Update the offset. */
2262 offset += length;
252b5132
RH
2263 }
2264
25dbc73a 2265 error_message = NULL;
252b5132 2266
25dbc73a
AM
2267 /* Compute the size of the output section. */
2268 num_entries = apuinfo_list_length ();
2269 output_section_size = 20 + num_entries * 4;
252b5132 2270
25dbc73a
AM
2271 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2272
2273 if (! bfd_set_section_size (abfd, asec, output_section_size))
2274 ibfd = abfd,
2275 error_message = _("warning: unable to set size of %s section in %B");
2276
2277 fail:
2278 free (buffer);
2279
2280 if (error_message)
2281 (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
2282}
252b5132 2283
25dbc73a
AM
2284/* Prevent the output section from accumulating the input sections'
2285 contents. We have already stored this in our linked list structure. */
252b5132 2286
25dbc73a
AM
2287static bfd_boolean
2288ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
c7b8f16e 2289 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
25dbc73a
AM
2290 asection *asec,
2291 bfd_byte *contents ATTRIBUTE_UNUSED)
2292{
2293 return (apuinfo_list_length ()
2294 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
252b5132
RH
2295}
2296
25dbc73a
AM
2297/* Finally we can generate the output section. */
2298
2299static void
2300ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
7619e7c7 2301{
25dbc73a
AM
2302 bfd_byte *buffer;
2303 asection *asec;
2304 unsigned i;
2305 unsigned num_entries;
2306 bfd_size_type length;
7619e7c7 2307
25dbc73a
AM
2308 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2309 if (asec == NULL)
2310 return;
2311
2312 if (apuinfo_list_length () == 0)
2313 return;
2314
2315 length = asec->size;
2316 if (length < 20)
2317 return;
2318
2319 buffer = bfd_malloc (length);
2320 if (buffer == NULL)
7619e7c7 2321 {
25dbc73a
AM
2322 (*_bfd_error_handler)
2323 (_("failed to allocate space for new APUinfo section."));
2324 return;
7619e7c7 2325 }
7619e7c7 2326
25dbc73a
AM
2327 /* Create the apuinfo header. */
2328 num_entries = apuinfo_list_length ();
2329 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2330 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2331 bfd_put_32 (abfd, 0x2, buffer + 8);
2332 strcpy ((char *) buffer + 12, APUINFO_LABEL);
feee612b 2333
25dbc73a
AM
2334 length = 20;
2335 for (i = 0; i < num_entries; i++)
feee612b 2336 {
25dbc73a
AM
2337 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2338 length += 4;
feee612b 2339 }
feee612b 2340
25dbc73a
AM
2341 if (length != asec->size)
2342 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
252b5132 2343
25dbc73a
AM
2344 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2345 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
252b5132 2346
25dbc73a
AM
2347 free (buffer);
2348
2349 apuinfo_list_finish ();
252b5132 2350}
25dbc73a 2351\f
7382d32a 2352static bfd_boolean
c6dd29ce 2353is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
7382d32a 2354{
c6dd29ce 2355 bfd_byte buf[GLINK_ENTRY_SIZE];
7382d32a 2356
c6dd29ce 2357 if (!bfd_get_section_contents (abfd, glink, buf, off, GLINK_ENTRY_SIZE))
7382d32a
AM
2358 return FALSE;
2359
c6dd29ce
AM
2360 return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
2361 && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
2362 && bfd_get_32 (abfd, buf + 8) == MTCTR_11
2363 && bfd_get_32 (abfd, buf + 12) == BCTR);
7382d32a
AM
2364}
2365
468392fb
AM
2366static bfd_boolean
2367section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2368{
2369 bfd_vma vma = *(bfd_vma *) ptr;
2370 return ((section->flags & SEC_ALLOC) != 0
2371 && section->vma <= vma
2372 && vma < section->vma + section->size);
2373}
2374
2375static long
2376ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms,
2377 long dynsymcount, asymbol **dynsyms,
2378 asymbol **ret)
2379{
2380 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2381 asection *plt, *relplt, *dynamic, *glink;
2382 bfd_vma glink_vma = 0;
2383 bfd_vma resolv_vma = 0;
2384 bfd_vma stub_vma;
2385 asymbol *s;
2386 arelent *p;
2387 long count, i;
2388 size_t size;
2389 char *names;
2390 bfd_byte buf[4];
2391
2392 *ret = NULL;
2393
2394 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
2395 return 0;
2396
2397 if (dynsymcount <= 0)
2398 return 0;
2399
2400 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2401 if (relplt == NULL)
2402 return 0;
2403
2404 plt = bfd_get_section_by_name (abfd, ".plt");
2405 if (plt == NULL)
2406 return 0;
2407
2408 /* Call common code to handle old-style executable PLTs. */
2409 if (elf_section_flags (plt) & SHF_EXECINSTR)
2410 return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
2411 dynsymcount, dynsyms, ret);
2412
2413 /* If this object was prelinked, the prelinker stored the address
2414 of .glink at got[1]. If it wasn't prelinked, got[1] will be zero. */
2415 dynamic = bfd_get_section_by_name (abfd, ".dynamic");
2416 if (dynamic != NULL)
2417 {
2418 bfd_byte *dynbuf, *extdyn, *extdynend;
2419 size_t extdynsize;
2420 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2421
2422 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2423 return -1;
2424
2425 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2426 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2427
2428 extdyn = dynbuf;
2429 extdynend = extdyn + dynamic->size;
2430 for (; extdyn < extdynend; extdyn += extdynsize)
2431 {
2432 Elf_Internal_Dyn dyn;
2433 (*swap_dyn_in) (abfd, extdyn, &dyn);
2434
2435 if (dyn.d_tag == DT_NULL)
2436 break;
2437
2438 if (dyn.d_tag == DT_PPC_GOT)
2439 {
2440 unsigned int g_o_t = dyn.d_un.d_val;
2441 asection *got = bfd_get_section_by_name (abfd, ".got");
2442 if (got != NULL
2443 && bfd_get_section_contents (abfd, got, buf,
2444 g_o_t - got->vma + 4, 4))
2445 glink_vma = bfd_get_32 (abfd, buf);
2446 break;
2447 }
2448 }
2449 free (dynbuf);
2450 }
2451
2452 /* Otherwise we read the first plt entry. */
2453 if (glink_vma == 0)
2454 {
2455 if (bfd_get_section_contents (abfd, plt, buf, 0, 4))
2456 glink_vma = bfd_get_32 (abfd, buf);
2457 }
2458
2459 if (glink_vma == 0)
2460 return 0;
2461
2462 /* The .glink section usually does not survive the final
2463 link; search for the section (usually .text) where the
2464 glink stubs now reside. */
2465 glink = bfd_sections_find_if (abfd, section_covers_vma, &glink_vma);
2466 if (glink == NULL)
2467 return 0;
2468
2469 /* Determine glink PLT resolver by reading the relative branch
2470 from the first glink stub. */
2471 if (bfd_get_section_contents (abfd, glink, buf,
2472 glink_vma - glink->vma, 4))
2473 {
2474 unsigned int insn = bfd_get_32 (abfd, buf);
2475
2476 /* The first glink stub may either branch to the resolver ... */
2477 insn ^= B;
2478 if ((insn & ~0x3fffffc) == 0)
2479 resolv_vma = glink_vma + (insn ^ 0x2000000) - 0x2000000;
2480
2481 /* ... or fall through a bunch of NOPs. */
2482 else if ((insn ^ B ^ NOP) == 0)
2483 for (i = 4;
2484 bfd_get_section_contents (abfd, glink, buf,
2485 glink_vma - glink->vma + i, 4);
2486 i += 4)
2487 if (bfd_get_32 (abfd, buf) != NOP)
2488 {
2489 resolv_vma = glink_vma + i;
2490 break;
2491 }
2492 }
2493
7382d32a
AM
2494 count = relplt->size / sizeof (Elf32_External_Rela);
2495 stub_vma = glink_vma - (bfd_vma) count * 16;
2496 /* If the stubs are those for -shared/-pie then we might have
2497 multiple stubs for each plt entry. If that is the case then
2498 there is no way to associate stubs with their plt entries short
2499 of figuring out the GOT pointer value used in the stub. */
c6dd29ce
AM
2500 if (!is_nonpic_glink_stub (abfd, glink,
2501 glink_vma - GLINK_ENTRY_SIZE - glink->vma))
7382d32a
AM
2502 return 0;
2503
468392fb
AM
2504 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2505 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
2506 return -1;
2507
468392fb
AM
2508 size = count * sizeof (asymbol);
2509 p = relplt->relocation;
2510 for (i = 0; i < count; i++, p++)
e054468f
AM
2511 {
2512 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2513 if (p->addend != 0)
2514 size += sizeof ("+0x") - 1 + 8;
2515 }
468392fb
AM
2516
2517 size += sizeof (asymbol) + sizeof ("__glink");
2518
2519 if (resolv_vma)
2520 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2521
2522 s = *ret = bfd_malloc (size);
2523 if (s == NULL)
2524 return -1;
2525
2526 names = (char *) (s + count + 1 + (resolv_vma != 0));
2527 p = relplt->relocation;
2528 for (i = 0; i < count; i++, p++)
2529 {
2530 size_t len;
2531
2532 *s = **p->sym_ptr_ptr;
2533 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2534 we are defining a symbol, ensure one of them is set. */
2535 if ((s->flags & BSF_LOCAL) == 0)
2536 s->flags |= BSF_GLOBAL;
6ba2a415 2537 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2538 s->section = glink;
2539 s->value = stub_vma - glink->vma;
2540 s->name = names;
2541 s->udata.p = NULL;
2542 len = strlen ((*p->sym_ptr_ptr)->name);
2543 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2544 names += len;
e054468f
AM
2545 if (p->addend != 0)
2546 {
2547 memcpy (names, "+0x", sizeof ("+0x") - 1);
2548 names += sizeof ("+0x") - 1;
2549 bfd_sprintf_vma (abfd, names, p->addend);
2550 names += strlen (names);
2551 }
468392fb
AM
2552 memcpy (names, "@plt", sizeof ("@plt"));
2553 names += sizeof ("@plt");
2554 ++s;
2555 stub_vma += 16;
2556 }
2557
2558 /* Add a symbol at the start of the glink branch table. */
86a4952b 2559 memset (s, 0, sizeof *s);
468392fb 2560 s->the_bfd = abfd;
6ba2a415 2561 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2562 s->section = glink;
2563 s->value = glink_vma - glink->vma;
2564 s->name = names;
2565 memcpy (names, "__glink", sizeof ("__glink"));
2566 names += sizeof ("__glink");
2567 s++;
2568 count++;
2569
2570 if (resolv_vma)
2571 {
2572 /* Add a symbol for the glink PLT resolver. */
86a4952b 2573 memset (s, 0, sizeof *s);
468392fb 2574 s->the_bfd = abfd;
6ba2a415 2575 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2576 s->section = glink;
2577 s->value = resolv_vma - glink->vma;
2578 s->name = names;
2579 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
2580 names += sizeof ("__glink_PLTresolve");
2581 s++;
2582 count++;
2583 }
2584
2585 return count;
2586}
2587\f
25dbc73a
AM
2588/* The following functions are specific to the ELF linker, while
2589 functions above are used generally. They appear in this file more
2590 or less in the order in which they are called. eg.
2591 ppc_elf_check_relocs is called early in the link process,
2592 ppc_elf_finish_dynamic_sections is one of the last functions
2593 called. */
252b5132 2594
25dbc73a
AM
2595/* The PPC linker needs to keep track of the number of relocs that it
2596 decides to copy as dynamic relocs in check_relocs for each symbol.
2597 This is so that it can later discard them if they are found to be
2598 unnecessary. We store the information in a field extending the
2599 regular ELF linker hash table. */
ae9a127f 2600
25dbc73a 2601struct ppc_elf_dyn_relocs
252b5132 2602{
25dbc73a 2603 struct ppc_elf_dyn_relocs *next;
252b5132 2604
25dbc73a
AM
2605 /* The input section of the reloc. */
2606 asection *sec;
252b5132 2607
25dbc73a
AM
2608 /* Total number of relocs copied for the input section. */
2609 bfd_size_type count;
252b5132 2610
25dbc73a
AM
2611 /* Number of pc-relative relocs copied for the input section. */
2612 bfd_size_type pc_count;
2613};
252b5132 2614
a6aa5195 2615/* Track PLT entries needed for a given symbol. We might need more
a877a2b6 2616 than one glink entry per symbol when generating a pic binary. */
a6aa5195
AM
2617struct plt_entry
2618{
2619 struct plt_entry *next;
2620
2621 /* -fPIC uses multiple GOT sections, one per file, called ".got2".
2622 This field stores the offset into .got2 used to initialise the
a877a2b6
AM
2623 GOT pointer reg. It will always be at least 32768. (Current
2624 gcc always uses an offset of 32768, but ld -r will pack .got2
2625 sections together resulting in larger offsets). */
a6aa5195
AM
2626 bfd_vma addend;
2627
2628 /* The .got2 section. */
2629 asection *sec;
2630
2631 /* PLT refcount or offset. */
2632 union
2633 {
2634 bfd_signed_vma refcount;
2635 bfd_vma offset;
2636 } plt;
2637
2638 /* .glink stub offset. */
2639 bfd_vma glink_offset;
2640};
2641
1d483afe 2642/* Of those relocs that might be copied as dynamic relocs, this function
25dbc73a
AM
2643 selects those that must be copied when linking a shared library,
2644 even when the symbol is local. */
252b5132 2645
1d483afe
AM
2646static int
2647must_be_dyn_reloc (struct bfd_link_info *info,
2648 enum elf_ppc_reloc_type r_type)
2649{
2650 switch (r_type)
2651 {
2652 default:
2653 return 1;
2654
2655 case R_PPC_REL24:
2656 case R_PPC_REL14:
2657 case R_PPC_REL14_BRTAKEN:
2658 case R_PPC_REL14_BRNTAKEN:
2659 case R_PPC_REL32:
2660 return 0;
2661
2662 case R_PPC_TPREL32:
2663 case R_PPC_TPREL16:
2664 case R_PPC_TPREL16_LO:
2665 case R_PPC_TPREL16_HI:
2666 case R_PPC_TPREL16_HA:
2667 return !info->executable;
2668 }
2669}
252b5132 2670
25dbc73a
AM
2671/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2672 copying dynamic variables from a shared lib into an app's dynbss
2673 section, and instead use a dynamic relocation to point into the
2674 shared lib. */
2675#define ELIMINATE_COPY_RELOCS 1
252b5132 2676
25dbc73a 2677/* PPC ELF linker hash entry. */
252b5132 2678
25dbc73a
AM
2679struct ppc_elf_link_hash_entry
2680{
2681 struct elf_link_hash_entry elf;
252b5132 2682
25dbc73a
AM
2683 /* If this symbol is used in the linker created sections, the processor
2684 specific backend uses this field to map the field into the offset
2685 from the beginning of the section. */
2686 elf_linker_section_pointers_t *linker_section_pointer;
252b5132 2687
25dbc73a
AM
2688 /* Track dynamic relocs copied for this symbol. */
2689 struct ppc_elf_dyn_relocs *dyn_relocs;
252b5132 2690
25dbc73a
AM
2691 /* Contexts in which symbol is used in the GOT (or TOC).
2692 TLS_GD .. TLS_TLS bits are or'd into the mask as the
2693 corresponding relocs are encountered during check_relocs.
2694 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2695 indicate the corresponding GOT entry type is not needed. */
2696#define TLS_GD 1 /* GD reloc. */
2697#define TLS_LD 2 /* LD reloc. */
2698#define TLS_TPREL 4 /* TPREL reloc, => IE. */
2699#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2700#define TLS_TLS 16 /* Any TLS reloc. */
2701#define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
e054468f 2702#define PLT_IFUNC 64 /* STT_GNU_IFUNC. */
25dbc73a 2703 char tls_mask;
4dc4a9a5
DJ
2704
2705 /* Nonzero if we have seen a small data relocation referring to this
2706 symbol. */
2707 unsigned char has_sda_refs;
25dbc73a 2708};
252b5132 2709
25dbc73a
AM
2710#define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2711
2712/* PPC ELF linker hash table. */
2713
2714struct ppc_elf_link_hash_table
2715{
2716 struct elf_link_hash_table elf;
2717
2718 /* Short-cuts to get to dynamic linker sections. */
2719 asection *got;
2720 asection *relgot;
d7128ce4 2721 asection *glink;
25dbc73a
AM
2722 asection *plt;
2723 asection *relplt;
e054468f
AM
2724 asection *iplt;
2725 asection *reliplt;
25dbc73a
AM
2726 asection *dynbss;
2727 asection *relbss;
2728 asection *dynsbss;
2729 asection *relsbss;
c9a2f333 2730 elf_linker_section_t sdata[2];
25dbc73a
AM
2731 asection *sbss;
2732
016687f8
AM
2733 /* The (unloaded but important) .rela.plt.unloaded on VxWorks. */
2734 asection *srelplt2;
2735
2736 /* The .got.plt section (VxWorks only)*/
2737 asection *sgotplt;
2738
1d483afe 2739 /* Shortcut to __tls_get_addr. */
25dbc73a 2740 struct elf_link_hash_entry *tls_get_addr;
252b5132 2741
016687f8
AM
2742 /* The bfd that forced an old-style PLT. */
2743 bfd *old_bfd;
2744
25dbc73a
AM
2745 /* TLS local dynamic got entry handling. */
2746 union {
2747 bfd_signed_vma refcount;
2748 bfd_vma offset;
2749 } tlsld_got;
252b5132 2750
c6dd29ce 2751 /* Offset of branch table to PltResolve function in glink. */
d7128ce4
AM
2752 bfd_vma glink_pltresolve;
2753
3b36f7e6
AM
2754 /* Size of reserved GOT entries. */
2755 unsigned int got_header_size;
2756 /* Non-zero if allocating the header left a gap. */
2757 unsigned int got_gap;
2758
4a3dc543
RS
2759 /* The type of PLT we have chosen to use. */
2760 enum ppc_elf_plt_type plt_type;
2761
0ba07910
AM
2762 /* Set if we should emit symbols for stubs. */
2763 unsigned int emit_stub_syms:1;
2764
a7f2871e
AM
2765 /* Set if __tls_get_addr optimization should not be done. */
2766 unsigned int no_tls_get_addr_opt:1;
2767
9d8504b1 2768 /* True if the target system is VxWorks. */
016687f8 2769 unsigned int is_vxworks:1;
9d8504b1
PB
2770
2771 /* The size of PLT entries. */
2772 int plt_entry_size;
2773 /* The distance between adjacent PLT slots. */
2774 int plt_slot_size;
2775 /* The size of the first PLT entry. */
2776 int plt_initial_entry_size;
016687f8 2777
87d72d41
AM
2778 /* Small local sym cache. */
2779 struct sym_cache sym_cache;
25dbc73a 2780};
252b5132 2781
25dbc73a 2782/* Get the PPC ELF linker hash table from a link_info structure. */
252b5132 2783
25dbc73a 2784#define ppc_elf_hash_table(p) \
4dfe6ac6
NC
2785 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2786 == PPC32_ELF_DATA ? ((struct ppc_elf_link_hash_table *) ((p)->hash)) : NULL)
252b5132 2787
25dbc73a 2788/* Create an entry in a PPC ELF linker hash table. */
252b5132 2789
25dbc73a
AM
2790static struct bfd_hash_entry *
2791ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2792 struct bfd_hash_table *table,
2793 const char *string)
252b5132 2794{
25dbc73a
AM
2795 /* Allocate the structure if it has not already been allocated by a
2796 subclass. */
2797 if (entry == NULL)
2798 {
2799 entry = bfd_hash_allocate (table,
2800 sizeof (struct ppc_elf_link_hash_entry));
2801 if (entry == NULL)
2802 return entry;
2803 }
252b5132 2804
25dbc73a
AM
2805 /* Call the allocation method of the superclass. */
2806 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2807 if (entry != NULL)
2808 {
2809 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2810 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2811 ppc_elf_hash_entry (entry)->tls_mask = 0;
2812 }
252b5132 2813
25dbc73a 2814 return entry;
252b5132 2815}
3dab13f6 2816
25dbc73a 2817/* Create a PPC ELF linker hash table. */
3dab13f6 2818
25dbc73a
AM
2819static struct bfd_link_hash_table *
2820ppc_elf_link_hash_table_create (bfd *abfd)
3dab13f6 2821{
25dbc73a 2822 struct ppc_elf_link_hash_table *ret;
3dab13f6 2823
25dbc73a
AM
2824 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2825 if (ret == NULL)
2826 return NULL;
3dab13f6 2827
66eb6687
AM
2828 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
2829 ppc_elf_link_hash_newfunc,
4dfe6ac6
NC
2830 sizeof (struct ppc_elf_link_hash_entry),
2831 PPC32_ELF_DATA))
3dab13f6 2832 {
25dbc73a
AM
2833 free (ret);
2834 return NULL;
2835 }
58111eb7 2836
a6aa5195
AM
2837 ret->elf.init_plt_refcount.refcount = 0;
2838 ret->elf.init_plt_refcount.glist = NULL;
2839 ret->elf.init_plt_offset.offset = 0;
2840 ret->elf.init_plt_offset.glist = NULL;
2841
c9a2f333
AM
2842 ret->sdata[0].name = ".sdata";
2843 ret->sdata[0].sym_name = "_SDA_BASE_";
2844 ret->sdata[0].bss_name = ".sbss";
2845
2846 ret->sdata[1].name = ".sdata2";
2847 ret->sdata[1].sym_name = "_SDA2_BASE_";
2848 ret->sdata[1].bss_name = ".sbss2";
2849
9d8504b1
PB
2850 ret->plt_entry_size = 12;
2851 ret->plt_slot_size = 8;
2852 ret->plt_initial_entry_size = 72;
9d8504b1 2853
25dbc73a
AM
2854 return &ret->elf.root;
2855}
3dab13f6 2856
9d8504b1 2857/* Create .got and the related sections. */
3dab13f6 2858
25dbc73a
AM
2859static bfd_boolean
2860ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2861{
2862 struct ppc_elf_link_hash_table *htab;
2863 asection *s;
2864 flagword flags;
3dab13f6 2865
25dbc73a
AM
2866 if (!_bfd_elf_create_got_section (abfd, info))
2867 return FALSE;
3dab13f6 2868
25dbc73a
AM
2869 htab = ppc_elf_hash_table (info);
2870 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2871 if (s == NULL)
2872 abort ();
3dab13f6 2873
9d8504b1
PB
2874 if (htab->is_vxworks)
2875 {
2876 htab->sgotplt = bfd_get_section_by_name (abfd, ".got.plt");
2877 if (!htab->sgotplt)
2878 abort ();
2879 }
2880 else
2881 {
2882 /* The powerpc .got has a blrl instruction in it. Mark it
2883 executable. */
2884 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
2885 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2886 if (!bfd_set_section_flags (abfd, s, flags))
2887 return FALSE;
2888 }
3dab13f6 2889
64e77c6d
L
2890 htab->relgot = bfd_get_section_by_name (abfd, ".rela.got");
2891 if (!htab->relgot)
2892 abort ();
3dab13f6 2893
25dbc73a
AM
2894 return TRUE;
2895}
3dab13f6 2896
e054468f
AM
2897static bfd_boolean
2898ppc_elf_create_glink (bfd *abfd, struct bfd_link_info *info)
2899{
2900 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2901 asection *s;
2902 flagword flags;
2903
2904 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS
2905 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2906 s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags);
2907 htab->glink = s;
2908 if (s == NULL
2909 || !bfd_set_section_alignment (abfd, s, 4))
2910 return FALSE;
2911
2912 flags = SEC_ALLOC | SEC_LINKER_CREATED;
2913 s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
2914 htab->iplt = s;
2915 if (s == NULL
2916 || !bfd_set_section_alignment (abfd, s, 4))
2917 return FALSE;
2918
2919 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2920 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2921 s = bfd_make_section_with_flags (abfd, ".rela.iplt", flags);
2922 htab->reliplt = s;
2923 if (s == NULL
2924 || ! bfd_set_section_alignment (abfd, s, 2))
2925 return FALSE;
2926 return TRUE;
2927}
2928
25dbc73a
AM
2929/* We have to create .dynsbss and .rela.sbss here so that they get mapped
2930 to output sections (just like _bfd_elf_create_dynamic_sections has
2931 to create .dynbss and .rela.bss). */
3dab13f6 2932
25dbc73a
AM
2933static bfd_boolean
2934ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2935{
2936 struct ppc_elf_link_hash_table *htab;
2937 asection *s;
2938 flagword flags;
3dab13f6 2939
25dbc73a 2940 htab = ppc_elf_hash_table (info);
3dab13f6 2941
25dbc73a
AM
2942 if (htab->got == NULL
2943 && !ppc_elf_create_got (abfd, info))
3dab13f6
AM
2944 return FALSE;
2945
25dbc73a
AM
2946 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2947 return FALSE;
3dab13f6 2948
e054468f
AM
2949 if (htab->glink == NULL
2950 && !ppc_elf_create_glink (abfd, info))
d7128ce4 2951 return FALSE;
3dab13f6 2952
25dbc73a 2953 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
3b36f7e6
AM
2954 s = bfd_make_section_with_flags (abfd, ".dynsbss",
2955 SEC_ALLOC | SEC_LINKER_CREATED);
2956 htab->dynsbss = s;
3496cb2a 2957 if (s == NULL)
25dbc73a 2958 return FALSE;
3dab13f6 2959
25dbc73a
AM
2960 if (! info->shared)
2961 {
2962 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
e054468f
AM
2963 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2964 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
d7128ce4 2965 s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
3b36f7e6 2966 htab->relsbss = s;
25dbc73a 2967 if (s == NULL
25dbc73a
AM
2968 || ! bfd_set_section_alignment (abfd, s, 2))
2969 return FALSE;
2970 }
3dab13f6 2971
711de32c
RS
2972 if (htab->is_vxworks
2973 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2974 return FALSE;
9d8504b1 2975
25dbc73a
AM
2976 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2977 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2978 if (s == NULL)
2979 abort ();
3dab13f6 2980
3b36f7e6 2981 flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
4a3dc543
RS
2982 if (htab->plt_type == PLT_VXWORKS)
2983 /* The VxWorks PLT is a loaded section with contents. */
2984 flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
25dbc73a
AM
2985 return bfd_set_section_flags (abfd, s, flags);
2986}
3dab13f6 2987
25dbc73a 2988/* Copy the extra info we tack onto an elf_link_hash_entry. */
58111eb7 2989
25dbc73a 2990static void
fcfa13d2 2991ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
25dbc73a
AM
2992 struct elf_link_hash_entry *dir,
2993 struct elf_link_hash_entry *ind)
2994{
2995 struct ppc_elf_link_hash_entry *edir, *eind;
3dab13f6 2996
25dbc73a
AM
2997 edir = (struct ppc_elf_link_hash_entry *) dir;
2998 eind = (struct ppc_elf_link_hash_entry *) ind;
3dab13f6 2999
25dbc73a
AM
3000 if (eind->dyn_relocs != NULL)
3001 {
3002 if (edir->dyn_relocs != NULL)
3dab13f6 3003 {
25dbc73a
AM
3004 struct ppc_elf_dyn_relocs **pp;
3005 struct ppc_elf_dyn_relocs *p;
3dab13f6 3006
fcfa13d2 3007 /* Add reloc counts against the indirect sym to the direct sym
25dbc73a
AM
3008 list. Merge any entries against the same section. */
3009 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3dab13f6 3010 {
25dbc73a
AM
3011 struct ppc_elf_dyn_relocs *q;
3012
3013 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3014 if (q->sec == p->sec)
3015 {
3016 q->pc_count += p->pc_count;
3017 q->count += p->count;
3018 *pp = p->next;
3019 break;
3020 }
3021 if (q == NULL)
3022 pp = &p->next;
3dab13f6 3023 }
25dbc73a 3024 *pp = edir->dyn_relocs;
3dab13f6 3025 }
25dbc73a
AM
3026
3027 edir->dyn_relocs = eind->dyn_relocs;
3028 eind->dyn_relocs = NULL;
3dab13f6 3029 }
3dab13f6 3030
25dbc73a 3031 edir->tls_mask |= eind->tls_mask;
4dc4a9a5 3032 edir->has_sda_refs |= eind->has_sda_refs;
3dab13f6 3033
a6aa5195
AM
3034 /* If called to transfer flags for a weakdef during processing
3035 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3036 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3037 if (!(ELIMINATE_COPY_RELOCS
3038 && eind->elf.root.type != bfd_link_hash_indirect
3039 && edir->elf.dynamic_adjusted))
3040 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3041
3042 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3043 edir->elf.ref_regular |= eind->elf.ref_regular;
3044 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3045 edir->elf.needs_plt |= eind->elf.needs_plt;
0eb4a168 3046 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
a6aa5195
AM
3047
3048 /* If we were called to copy over info for a weak sym, that's all. */
3049 if (eind->elf.root.type != bfd_link_hash_indirect)
3050 return;
3051
3052 /* Copy over the GOT refcount entries that we may have already seen to
3053 the symbol which just became indirect. */
fcfa13d2
AM
3054 edir->elf.got.refcount += eind->elf.got.refcount;
3055 eind->elf.got.refcount = 0;
a6aa5195
AM
3056
3057 /* And plt entries. */
3058 if (eind->elf.plt.plist != NULL)
3059 {
3060 if (edir->elf.plt.plist != NULL)
3061 {
3062 struct plt_entry **entp;
3063 struct plt_entry *ent;
3064
3065 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3066 {
3067 struct plt_entry *dent;
3068
3069 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3070 if (dent->sec == ent->sec && dent->addend == ent->addend)
3071 {
3072 dent->plt.refcount += ent->plt.refcount;
3073 *entp = ent->next;
3074 break;
3075 }
3076 if (dent == NULL)
3077 entp = &ent->next;
3078 }
3079 *entp = edir->elf.plt.plist;
3080 }
3081
3082 edir->elf.plt.plist = eind->elf.plt.plist;
3083 eind->elf.plt.plist = NULL;
3084 }
3085
fcfa13d2 3086 if (eind->elf.dynindx != -1)
25dbc73a 3087 {
fcfa13d2
AM
3088 if (edir->elf.dynindx != -1)
3089 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3090 edir->elf.dynstr_index);
a6aa5195
AM
3091 edir->elf.dynindx = eind->elf.dynindx;
3092 edir->elf.dynstr_index = eind->elf.dynstr_index;
3093 eind->elf.dynindx = -1;
3094 eind->elf.dynstr_index = 0;
25dbc73a 3095 }
25dbc73a 3096}
3dab13f6 3097
25dbc73a
AM
3098/* Hook called by the linker routine which adds symbols from an object
3099 file. We use it to put .comm items in .sbss, and not .bss. */
58111eb7 3100
25dbc73a
AM
3101static bfd_boolean
3102ppc_elf_add_symbol_hook (bfd *abfd,
3103 struct bfd_link_info *info,
3104 Elf_Internal_Sym *sym,
3105 const char **namep ATTRIBUTE_UNUSED,
3106 flagword *flagsp ATTRIBUTE_UNUSED,
3107 asection **secp,
3108 bfd_vma *valp)
3109{
3110 if (sym->st_shndx == SHN_COMMON
3111 && !info->relocatable
0c8d6e5c
AM
3112 && is_ppc_elf (info->output_bfd)
3113 && sym->st_size <= elf_gp_size (abfd))
25dbc73a
AM
3114 {
3115 /* Common symbols less than or equal to -G nn bytes are automatically
3116 put into .sbss. */
3117 struct ppc_elf_link_hash_table *htab;
3dab13f6 3118
25dbc73a
AM
3119 htab = ppc_elf_hash_table (info);
3120 if (htab->sbss == NULL)
3121 {
644285ef 3122 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
3dab13f6 3123
644285ef
AM
3124 if (!htab->elf.dynobj)
3125 htab->elf.dynobj = abfd;
3126
3496cb2a
L
3127 htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3128 ".sbss",
3129 flags);
3130 if (htab->sbss == NULL)
25dbc73a 3131 return FALSE;
3dab13f6 3132 }
3dab13f6 3133
25dbc73a
AM
3134 *secp = htab->sbss;
3135 *valp = sym->st_size;
3136 }
3dab13f6 3137
e054468f
AM
3138 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3139 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
3140
25dbc73a 3141 return TRUE;
3dab13f6
AM
3142}
3143\f
046183de 3144static bfd_boolean
bd6c6e2b 3145create_sdata_sym (struct bfd_link_info *info, elf_linker_section_t *lsect)
046183de 3146{
bd6c6e2b
AM
3147 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
3148
046183de
AM
3149 lsect->sym = elf_link_hash_lookup (&htab->elf, lsect->sym_name,
3150 TRUE, FALSE, TRUE);
3151 if (lsect->sym == NULL)
3152 return FALSE;
3153 if (lsect->sym->root.type == bfd_link_hash_new)
3154 lsect->sym->non_elf = 0;
3155 lsect->sym->ref_regular = 1;
bd6c6e2b 3156 _bfd_elf_link_hash_hide_symbol (info, lsect->sym, TRUE);
046183de
AM
3157 return TRUE;
3158}
3159
c9a2f333 3160/* Create a special linker section. */
25dbc73a 3161
c9a2f333 3162static bfd_boolean
55fd94b0
AM
3163ppc_elf_create_linker_section (bfd *abfd,
3164 struct bfd_link_info *info,
c9a2f333
AM
3165 flagword flags,
3166 elf_linker_section_t *lsect)
252b5132 3167{
58111eb7 3168 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
c9a2f333 3169 asection *s;
58111eb7 3170
c9a2f333
AM
3171 flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3172 | SEC_LINKER_CREATED);
58111eb7
AM
3173
3174 /* Record the first bfd that needs the special sections. */
3175 if (!htab->elf.dynobj)
3176 htab->elf.dynobj = abfd;
3177
3496cb2a
L
3178 s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3179 lsect->name,
3180 flags);
76750a2f 3181 if (s == NULL
76750a2f 3182 || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
c9a2f333 3183 return FALSE;
76750a2f 3184 lsect->section = s;
58111eb7 3185
bd6c6e2b 3186 return create_sdata_sym (info, lsect);
252b5132 3187}
252b5132 3188
25dbc73a
AM
3189/* Find a linker generated pointer with a given addend and type. */
3190
3191static elf_linker_section_pointers_t *
3192elf_find_pointer_linker_section
3193 (elf_linker_section_pointers_t *linker_pointers,
3194 bfd_vma addend,
3195 elf_linker_section_t *lsect)
252b5132 3196{
25dbc73a
AM
3197 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
3198 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
3199 return linker_pointers;
252b5132 3200
25dbc73a
AM
3201 return NULL;
3202}
3203
3204/* Allocate a pointer to live in a linker created section. */
3205
3206static bfd_boolean
3207elf_create_pointer_linker_section (bfd *abfd,
25dbc73a
AM
3208 elf_linker_section_t *lsect,
3209 struct elf_link_hash_entry *h,
3210 const Elf_Internal_Rela *rel)
3211{
3212 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
3213 elf_linker_section_pointers_t *linker_section_ptr;
3214 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
3215 bfd_size_type amt;
3216
3217 BFD_ASSERT (lsect != NULL);
3218
3219 /* Is this a global symbol? */
3220 if (h != NULL)
3221 {
3222 struct ppc_elf_link_hash_entry *eh;
3223
3224 /* Has this symbol already been allocated? If so, our work is done. */
3225 eh = (struct ppc_elf_link_hash_entry *) h;
3226 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
3227 rel->r_addend,
3228 lsect))
3229 return TRUE;
3230
3231 ptr_linker_section_ptr = &eh->linker_section_pointer;
25dbc73a
AM
3232 }
3233 else
3234 {
0c8d6e5c 3235 BFD_ASSERT (is_ppc_elf (abfd));
0ffa91dd 3236
25dbc73a
AM
3237 /* Allocation of a pointer to a local symbol. */
3238 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
3239
3240 /* Allocate a table to hold the local symbols if first time. */
3241 if (!ptr)
3242 {
0ffa91dd 3243 unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
25dbc73a
AM
3244
3245 amt = num_symbols;
3246 amt *= sizeof (elf_linker_section_pointers_t *);
3247 ptr = bfd_zalloc (abfd, amt);
3248
3249 if (!ptr)
3250 return FALSE;
3251
3252 elf_local_ptr_offsets (abfd) = ptr;
3253 }
3254
3255 /* Has this symbol already been allocated? If so, our work is done. */
3256 if (elf_find_pointer_linker_section (ptr[r_symndx],
3257 rel->r_addend,
3258 lsect))
3259 return TRUE;
252b5132 3260
25dbc73a 3261 ptr_linker_section_ptr = &ptr[r_symndx];
25dbc73a 3262 }
41fcb14e 3263
25dbc73a
AM
3264 /* Allocate space for a pointer in the linker section, and allocate
3265 a new pointer record from internal memory. */
3266 BFD_ASSERT (ptr_linker_section_ptr != NULL);
3267 amt = sizeof (elf_linker_section_pointers_t);
3268 linker_section_ptr = bfd_alloc (abfd, amt);
41fcb14e 3269
25dbc73a 3270 if (!linker_section_ptr)
7619e7c7 3271 return FALSE;
41fcb14e 3272
25dbc73a
AM
3273 linker_section_ptr->next = *ptr_linker_section_ptr;
3274 linker_section_ptr->addend = rel->r_addend;
3275 linker_section_ptr->lsect = lsect;
25dbc73a 3276 *ptr_linker_section_ptr = linker_section_ptr;
41fcb14e 3277
25dbc73a
AM
3278 linker_section_ptr->offset = lsect->section->size;
3279 lsect->section->size += 4;
7619e7c7 3280
25dbc73a
AM
3281#ifdef DEBUG
3282 fprintf (stderr,
3283 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
3284 lsect->name, (long) linker_section_ptr->offset,
3285 (long) lsect->section->size);
3286#endif
7619e7c7
AM
3287
3288 return TRUE;
41fcb14e
AM
3289}
3290
e054468f 3291static struct plt_entry **
25dbc73a
AM
3292update_local_sym_info (bfd *abfd,
3293 Elf_Internal_Shdr *symtab_hdr,
3294 unsigned long r_symndx,
3295 int tls_type)
252b5132 3296{
25dbc73a 3297 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
e054468f 3298 struct plt_entry **local_plt;
25dbc73a 3299 char *local_got_tls_masks;
252b5132 3300
25dbc73a 3301 if (local_got_refcounts == NULL)
252b5132 3302 {
25dbc73a
AM
3303 bfd_size_type size = symtab_hdr->sh_info;
3304
e054468f
AM
3305 size *= (sizeof (*local_got_refcounts)
3306 + sizeof (*local_plt)
3307 + sizeof (*local_got_tls_masks));
25dbc73a
AM
3308 local_got_refcounts = bfd_zalloc (abfd, size);
3309 if (local_got_refcounts == NULL)
e054468f 3310 return NULL;
25dbc73a 3311 elf_local_got_refcounts (abfd) = local_got_refcounts;
252b5132 3312 }
41fcb14e 3313
e054468f
AM
3314 local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
3315 local_got_tls_masks = (char *) (local_plt + symtab_hdr->sh_info);
25dbc73a 3316 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
3317 if (tls_type != PLT_IFUNC)
3318 local_got_refcounts[r_symndx] += 1;
3319 return local_plt + r_symndx;
25dbc73a 3320}
41fcb14e 3321
a6aa5195 3322static bfd_boolean
3ec01793 3323update_plt_info (bfd *abfd, struct plt_entry **plist,
a6aa5195
AM
3324 asection *sec, bfd_vma addend)
3325{
3326 struct plt_entry *ent;
3327
32af9f6e
AM
3328 if (addend < 32768)
3329 sec = NULL;
3ec01793 3330 for (ent = *plist; ent != NULL; ent = ent->next)
a6aa5195
AM
3331 if (ent->sec == sec && ent->addend == addend)
3332 break;
3333 if (ent == NULL)
3334 {
3335 bfd_size_type amt = sizeof (*ent);
3336 ent = bfd_alloc (abfd, amt);
3337 if (ent == NULL)
3338 return FALSE;
3ec01793 3339 ent->next = *plist;
a6aa5195
AM
3340 ent->sec = sec;
3341 ent->addend = addend;
3342 ent->plt.refcount = 0;
3ec01793 3343 *plist = ent;
a6aa5195
AM
3344 }
3345 ent->plt.refcount += 1;
3346 return TRUE;
3347}
3348
3349static struct plt_entry *
3ec01793 3350find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
a6aa5195
AM
3351{
3352 struct plt_entry *ent;
3353
3354 if (addend < 32768)
3355 sec = NULL;
3ec01793 3356 for (ent = *plist; ent != NULL; ent = ent->next)
a6aa5195
AM
3357 if (ent->sec == sec && ent->addend == addend)
3358 break;
3359 return ent;
3360}
3361
e054468f
AM
3362static bfd_boolean
3363is_branch_reloc (enum elf_ppc_reloc_type r_type)
3364{
3365 return (r_type == R_PPC_PLTREL24
3366 || r_type == R_PPC_LOCAL24PC
3367 || r_type == R_PPC_REL24
3368 || r_type == R_PPC_REL14
3369 || r_type == R_PPC_REL14_BRTAKEN
3370 || r_type == R_PPC_REL14_BRNTAKEN
3371 || r_type == R_PPC_ADDR24
3372 || r_type == R_PPC_ADDR14
3373 || r_type == R_PPC_ADDR14_BRTAKEN
3374 || r_type == R_PPC_ADDR14_BRNTAKEN);
3375}
3376
25dbc73a
AM
3377static void
3378bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3379{
3380 (*_bfd_error_handler)
3381 (_("%B: relocation %s cannot be used when making a shared object"),
3382 abfd,
3383 ppc_elf_howto_table[r_type]->name);
3384 bfd_set_error (bfd_error_bad_value);
252b5132
RH
3385}
3386
25dbc73a
AM
3387/* Look through the relocs for a section during the first phase, and
3388 allocate space in the global offset table or procedure linkage
3389 table. */
252b5132 3390
b34976b6 3391static bfd_boolean
25dbc73a
AM
3392ppc_elf_check_relocs (bfd *abfd,
3393 struct bfd_link_info *info,
3394 asection *sec,
3395 const Elf_Internal_Rela *relocs)
252b5132 3396{
7619e7c7 3397 struct ppc_elf_link_hash_table *htab;
25dbc73a
AM
3398 Elf_Internal_Shdr *symtab_hdr;
3399 struct elf_link_hash_entry **sym_hashes;
3400 const Elf_Internal_Rela *rel;
3401 const Elf_Internal_Rela *rel_end;
a6aa5195 3402 asection *got2, *sreloc;
727fc41e 3403 struct elf_link_hash_entry *tga;
25dbc73a
AM
3404
3405 if (info->relocatable)
3406 return TRUE;
252b5132 3407
c87b5a93
AM
3408 /* Don't do anything special with non-loaded, non-alloced sections.
3409 In particular, any relocs in such sections should not affect GOT
3410 and PLT reference counting (ie. we don't allow them to create GOT
3411 or PLT entries), there's no possibility or desire to optimize TLS
3412 relocs, and there's not much point in propagating relocs to shared
3413 libs that the dynamic linker won't relocate. */
3414 if ((sec->flags & SEC_ALLOC) == 0)
3415 return TRUE;
3416
252b5132 3417#ifdef DEBUG
25dbc73a
AM
3418 _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
3419 sec, abfd);
252b5132
RH
3420#endif
3421
0c8d6e5c 3422 BFD_ASSERT (is_ppc_elf (abfd));
0ffa91dd 3423
25dbc73a
AM
3424 /* Initialize howto table if not already done. */
3425 if (!ppc_elf_howto_table[R_PPC_ADDR32])
3426 ppc_elf_howto_init ();
3427
7619e7c7 3428 htab = ppc_elf_hash_table (info);
25f23106
AM
3429 if (htab->glink == NULL)
3430 {
3431 if (htab->elf.dynobj == NULL)
3432 htab->elf.dynobj = abfd;
3433 if (!ppc_elf_create_glink (htab->elf.dynobj, info))
3434 return FALSE;
3435 }
727fc41e
AM
3436 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3437 FALSE, FALSE, TRUE);
0ffa91dd 3438 symtab_hdr = &elf_symtab_hdr (abfd);
25dbc73a 3439 sym_hashes = elf_sym_hashes (abfd);
a6aa5195 3440 got2 = bfd_get_section_by_name (abfd, ".got2");
25dbc73a
AM
3441 sreloc = NULL;
3442
3443 rel_end = relocs + sec->reloc_count;
3444 for (rel = relocs; rel < rel_end; rel++)
3445 {
3446 unsigned long r_symndx;
3447 enum elf_ppc_reloc_type r_type;
3448 struct elf_link_hash_entry *h;
727fc41e 3449 int tls_type;
25dbc73a
AM
3450
3451 r_symndx = ELF32_R_SYM (rel->r_info);
3452 if (r_symndx < symtab_hdr->sh_info)
3453 h = NULL;
3454 else
973a3492
L
3455 {
3456 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3457 while (h->root.type == bfd_link_hash_indirect
3458 || h->root.type == bfd_link_hash_warning)
3459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3460 }
25dbc73a
AM
3461
3462 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3463 This shows up in particular in an R_PPC_ADDR32 in the eabi
3464 startup code. */
3b36f7e6
AM
3465 if (h != NULL
3466 && htab->got == NULL
3467 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
252b5132 3468 {
3b36f7e6
AM
3469 if (htab->elf.dynobj == NULL)
3470 htab->elf.dynobj = abfd;
3471 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3472 return FALSE;
3473 BFD_ASSERT (h == htab->elf.hgot);
25dbc73a 3474 }
252b5132 3475
727fc41e 3476 tls_type = 0;
de972ffa 3477 r_type = ELF32_R_TYPE (rel->r_info);
06a162cf 3478 if (h == NULL && !htab->is_vxworks)
e054468f 3479 {
06a162cf
AM
3480 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3481 abfd, r_symndx);
3482 if (isym == NULL)
3483 return FALSE;
3484
3485 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3486 && (!info->shared
3487 || is_branch_reloc (r_type)))
e054468f 3488 {
06a162cf
AM
3489 struct plt_entry **ifunc;
3490 bfd_vma addend;
3491
3492 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
3493 PLT_IFUNC);
3494 if (ifunc == NULL)
e054468f
AM
3495 return FALSE;
3496
06a162cf
AM
3497 /* STT_GNU_IFUNC symbols must have a PLT entry;
3498 In a non-pie executable even when there are
3499 no plt calls. */
3500 addend = 0;
3501 if (r_type == R_PPC_PLTREL24)
e054468f 3502 {
06a162cf
AM
3503 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3504 if (info->shared)
3505 addend = rel->r_addend;
e054468f 3506 }
06a162cf
AM
3507 if (!update_plt_info (abfd, ifunc, got2, addend))
3508 return FALSE;
e054468f 3509 }
25f23106
AM
3510 }
3511
de972ffa
AM
3512 if (!htab->is_vxworks
3513 && is_branch_reloc (r_type)
3514 && h != NULL
3515 && h == tga)
25f23106 3516 {
de972ffa
AM
3517 if (rel != relocs
3518 && (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
3519 || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
3520 /* We have a new-style __tls_get_addr call with a marker
3521 reloc. */
3522 ;
3523 else
3524 /* Mark this section as having an old-style call. */
3525 sec->has_tls_get_addr_call = 1;
e054468f 3526 }
727fc41e 3527
25dbc73a
AM
3528 switch (r_type)
3529 {
727fc41e
AM
3530 case R_PPC_TLSGD:
3531 case R_PPC_TLSLD:
3532 /* These special tls relocs tie a call to __tls_get_addr with
3533 its parameter symbol. */
3534 break;
3535
25dbc73a
AM
3536 case R_PPC_GOT_TLSLD16:
3537 case R_PPC_GOT_TLSLD16_LO:
3538 case R_PPC_GOT_TLSLD16_HI:
3539 case R_PPC_GOT_TLSLD16_HA:
25dbc73a
AM
3540 tls_type = TLS_TLS | TLS_LD;
3541 goto dogottls;
252b5132 3542
25dbc73a
AM
3543 case R_PPC_GOT_TLSGD16:
3544 case R_PPC_GOT_TLSGD16_LO:
3545 case R_PPC_GOT_TLSGD16_HI:
3546 case R_PPC_GOT_TLSGD16_HA:
3547 tls_type = TLS_TLS | TLS_GD;
3548 goto dogottls;
3549
3550 case R_PPC_GOT_TPREL16:
3551 case R_PPC_GOT_TPREL16_LO:
3552 case R_PPC_GOT_TPREL16_HI:
3553 case R_PPC_GOT_TPREL16_HA:
1d483afe 3554 if (!info->executable)
25dbc73a
AM
3555 info->flags |= DF_STATIC_TLS;
3556 tls_type = TLS_TLS | TLS_TPREL;
3557 goto dogottls;
3558
3559 case R_PPC_GOT_DTPREL16:
3560 case R_PPC_GOT_DTPREL16_LO:
3561 case R_PPC_GOT_DTPREL16_HI:
3562 case R_PPC_GOT_DTPREL16_HA:
3563 tls_type = TLS_TLS | TLS_DTPREL;
3564 dogottls:
3565 sec->has_tls_reloc = 1;
3566 /* Fall thru */
252b5132 3567
25dbc73a
AM
3568 /* GOT16 relocations */
3569 case R_PPC_GOT16:
3570 case R_PPC_GOT16_LO:
3571 case R_PPC_GOT16_HI:
3572 case R_PPC_GOT16_HA:
3573 /* This symbol requires a global offset table entry. */
3574 if (htab->got == NULL)
3575 {
3576 if (htab->elf.dynobj == NULL)
3577 htab->elf.dynobj = abfd;
3578 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3579 return FALSE;
3580 }
3581 if (h != NULL)
3582 {
3583 h->got.refcount += 1;
3584 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3585 }
3586 else
3587 /* This is a global offset table entry for a local symbol. */
3588 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3589 return FALSE;
91e21fb7
AM
3590
3591 /* We may also need a plt entry if the symbol turns out to be
3592 an ifunc. */
3593 if (h != NULL && !info->shared)
3594 {
3595 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3596 return FALSE;
3597 }
25dbc73a 3598 break;
252b5132 3599
25dbc73a
AM
3600 /* Indirect .sdata relocation. */
3601 case R_PPC_EMB_SDAI16:
3602 if (info->shared)
3603 {
3604 bad_shared_reloc (abfd, r_type);
3605 return FALSE;
3606 }
c9a2f333
AM
3607 if (htab->sdata[0].section == NULL
3608 && !ppc_elf_create_linker_section (abfd, info, 0,
3609 &htab->sdata[0]))
3610 return FALSE;
76750a2f
AM
3611 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
3612 h, rel))
25dbc73a 3613 return FALSE;
046183de
AM
3614 if (h != NULL)
3615 {
3616 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3617 h->non_got_ref = TRUE;
3618 }
25dbc73a 3619 break;
252b5132 3620
25dbc73a
AM
3621 /* Indirect .sdata2 relocation. */
3622 case R_PPC_EMB_SDA2I16:
3623 if (info->shared)
3624 {
3625 bad_shared_reloc (abfd, r_type);
3626 return FALSE;
3627 }
c9a2f333
AM
3628 if (htab->sdata[1].section == NULL
3629 && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
3630 &htab->sdata[1]))
3631 return FALSE;
76750a2f
AM
3632 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
3633 h, rel))
25dbc73a 3634 return FALSE;
046183de
AM
3635 if (h != NULL)
3636 {
3637 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3638 h->non_got_ref = TRUE;
3639 }
25dbc73a 3640 break;
252b5132 3641
25dbc73a 3642 case R_PPC_SDAREL16:
046183de 3643 if (htab->sdata[0].sym == NULL
bd6c6e2b 3644 && !create_sdata_sym (info, &htab->sdata[0]))
046183de
AM
3645 return FALSE;
3646 if (h != NULL)
3647 {
3648 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3649 h->non_got_ref = TRUE;
3650 }
3651 break;
3652
25dbc73a 3653 case R_PPC_EMB_SDA2REL:
046183de
AM
3654 if (info->shared)
3655 {
3656 bad_shared_reloc (abfd, r_type);
3657 return FALSE;
3658 }
3659 if (htab->sdata[1].sym == NULL
bd6c6e2b 3660 && !create_sdata_sym (info, &htab->sdata[1]))
046183de
AM
3661 return FALSE;
3662 if (h != NULL)
3663 {
3664 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3665 h->non_got_ref = TRUE;
3666 }
3667 break;
3668
25dbc73a
AM
3669 case R_PPC_EMB_SDA21:
3670 case R_PPC_EMB_RELSDA:
046183de
AM
3671 if (info->shared)
3672 {
3673 bad_shared_reloc (abfd, r_type);
3674 return FALSE;
3675 }
3676 if (htab->sdata[0].sym == NULL
bd6c6e2b 3677 && !create_sdata_sym (info, &htab->sdata[0]))
046183de
AM
3678 return FALSE;
3679 if (htab->sdata[1].sym == NULL
bd6c6e2b 3680 && !create_sdata_sym (info, &htab->sdata[1]))
046183de
AM
3681 return FALSE;
3682 if (h != NULL)
3683 {
3684 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3685 h->non_got_ref = TRUE;
3686 }
3687 break;
3688
25dbc73a
AM
3689 case R_PPC_EMB_NADDR32:
3690 case R_PPC_EMB_NADDR16:
3691 case R_PPC_EMB_NADDR16_LO:
3692 case R_PPC_EMB_NADDR16_HI:
3693 case R_PPC_EMB_NADDR16_HA:
3694 if (info->shared)
3695 {
3696 bad_shared_reloc (abfd, r_type);
3697 return FALSE;
3698 }
4dc4a9a5 3699 if (h != NULL)
046183de 3700 h->non_got_ref = TRUE;
25dbc73a 3701 break;
252b5132 3702
25dbc73a 3703 case R_PPC_PLTREL24:
de972ffa 3704 if (h == NULL)
6d78d0b9
AM
3705 break;
3706 /* Fall through */
3707 case R_PPC_PLT32:
25dbc73a
AM
3708 case R_PPC_PLTREL32:
3709 case R_PPC_PLT16_LO:
3710 case R_PPC_PLT16_HI:
3711 case R_PPC_PLT16_HA:
3712#ifdef DEBUG
3713 fprintf (stderr, "Reloc requires a PLT entry\n");
3714#endif
3715 /* This symbol requires a procedure linkage table entry. We
3716 actually build the entry in finish_dynamic_symbol,
3717 because this might be a case of linking PIC code without
3718 linking in any dynamic objects, in which case we don't
3719 need to generate a procedure linkage table after all. */
7619e7c7 3720
25dbc73a
AM
3721 if (h == NULL)
3722 {
3723 /* It does not make sense to have a procedure linkage
3724 table entry for a local symbol. */
3725 (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
3726 "local symbol"),
3727 abfd,
3728 sec,
3729 (long) rel->r_offset,
3730 ppc_elf_howto_table[r_type]->name);
3731 bfd_set_error (bfd_error_bad_value);
3732 return FALSE;
3733 }
a6aa5195
AM
3734 else
3735 {
016687f8 3736 bfd_vma addend = 0;
ee05f2fe 3737
016687f8
AM
3738 if (r_type == R_PPC_PLTREL24)
3739 {
3740 ppc_elf_tdata (abfd)->makes_plt_call = 1;
a877a2b6
AM
3741 if (info->shared)
3742 addend = rel->r_addend;
016687f8 3743 }
a6aa5195 3744 h->needs_plt = 1;
32af9f6e 3745 if (!update_plt_info (abfd, &h->plt.plist, got2, addend))
a6aa5195
AM
3746 return FALSE;
3747 }
25dbc73a 3748 break;
ee05f2fe 3749
25dbc73a
AM
3750 /* The following relocations don't need to propagate the
3751 relocation if linking a shared object since they are
3752 section relative. */
3753 case R_PPC_SECTOFF:
3754 case R_PPC_SECTOFF_LO:
3755 case R_PPC_SECTOFF_HI:
3756 case R_PPC_SECTOFF_HA:
3757 case R_PPC_DTPREL16:
3758 case R_PPC_DTPREL16_LO:
3759 case R_PPC_DTPREL16_HI:
3760 case R_PPC_DTPREL16_HA:
3761 case R_PPC_TOC16:
3762 break;
252b5132 3763
d7128ce4
AM
3764 case R_PPC_REL16:
3765 case R_PPC_REL16_LO:
3766 case R_PPC_REL16_HI:
3767 case R_PPC_REL16_HA:
016687f8 3768 ppc_elf_tdata (abfd)->has_rel16 = 1;
d7128ce4
AM
3769 break;
3770
a6aa5195 3771 /* These are just markers. */
25dbc73a
AM
3772 case R_PPC_TLS:
3773 case R_PPC_EMB_MRKREF:
3774 case R_PPC_NONE:
3775 case R_PPC_max:
32af9f6e
AM
3776 case R_PPC_RELAX:
3777 case R_PPC_RELAX_PLT:
3778 case R_PPC_RELAX_PLTREL24:
25dbc73a 3779 break;
252b5132 3780
25dbc73a
AM
3781 /* These should only appear in dynamic objects. */
3782 case R_PPC_COPY:
3783 case R_PPC_GLOB_DAT:
3784 case R_PPC_JMP_SLOT:
3785 case R_PPC_RELATIVE:
e054468f 3786 case R_PPC_IRELATIVE:
25dbc73a 3787 break;
252b5132 3788
25dbc73a
AM
3789 /* These aren't handled yet. We'll report an error later. */
3790 case R_PPC_ADDR30:
3791 case R_PPC_EMB_RELSEC16:
3792 case R_PPC_EMB_RELST_LO:
3793 case R_PPC_EMB_RELST_HI:
3794 case R_PPC_EMB_RELST_HA:
3795 case R_PPC_EMB_BIT_FLD:
3796 break;
252b5132 3797
25dbc73a
AM
3798 /* This refers only to functions defined in the shared library. */
3799 case R_PPC_LOCAL24PC:
727fc41e 3800 if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
016687f8
AM
3801 {
3802 htab->plt_type = PLT_OLD;
3803 htab->old_bfd = abfd;
3804 }
25dbc73a 3805 break;
252b5132 3806
25dbc73a
AM
3807 /* This relocation describes the C++ object vtable hierarchy.
3808 Reconstruct it for later use during GC. */
3809 case R_PPC_GNU_VTINHERIT:
3810 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3811 return FALSE;
3812 break;
252b5132 3813
25dbc73a
AM
3814 /* This relocation describes which C++ vtable entries are actually
3815 used. Record for later use during GC. */
3816 case R_PPC_GNU_VTENTRY:
d17e0c6e
JB
3817 BFD_ASSERT (h != NULL);
3818 if (h != NULL
3819 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
25dbc73a
AM
3820 return FALSE;
3821 break;
252b5132 3822
25dbc73a
AM
3823 /* We shouldn't really be seeing these. */
3824 case R_PPC_TPREL32:
1d483afe
AM
3825 case R_PPC_TPREL16:
3826 case R_PPC_TPREL16_LO:
3827 case R_PPC_TPREL16_HI:
3828 case R_PPC_TPREL16_HA:
3829 if (!info->executable)
25dbc73a
AM
3830 info->flags |= DF_STATIC_TLS;
3831 goto dodyn;
252b5132 3832
25dbc73a
AM
3833 /* Nor these. */
3834 case R_PPC_DTPMOD32:
3835 case R_PPC_DTPREL32:
3836 goto dodyn;
252b5132 3837
9edfd1af
AM
3838 case R_PPC_REL32:
3839 if (h == NULL
3840 && got2 != NULL
3841 && (sec->flags & SEC_CODE) != 0
a877a2b6 3842 && info->shared
4a3dc543 3843 && htab->plt_type == PLT_UNSET)
9edfd1af
AM
3844 {
3845 /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
3846 the start of a function, which assembles to a REL32
3847 reference to .got2. If we detect one of these, then
3848 force the old PLT layout because the linker cannot
3849 reliably deduce the GOT pointer value needed for
3850 PLT call stubs. */
3851 asection *s;
87d72d41 3852 Elf_Internal_Sym *isym;
9edfd1af 3853
87d72d41
AM
3854 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3855 abfd, r_symndx);
3856 if (isym == NULL)
3857 return FALSE;
3858
3859 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
9edfd1af 3860 if (s == got2)
016687f8
AM
3861 {
3862 htab->plt_type = PLT_OLD;
3863 htab->old_bfd = abfd;
3864 }
9edfd1af 3865 }
7123df0e 3866 if (h == NULL || h == htab->elf.hgot)
fd38b44c 3867 break;
625af618
AM
3868 /* fall through */
3869
3870 case R_PPC_ADDR32:
3871 case R_PPC_ADDR16:
3872 case R_PPC_ADDR16_LO:
3873 case R_PPC_ADDR16_HI:
3874 case R_PPC_ADDR16_HA:
3875 case R_PPC_UADDR32:
3876 case R_PPC_UADDR16:
3877 if (h != NULL && !info->shared)
3878 {
3879 /* We may need a plt entry if the symbol turns out to be
3880 a function defined in a dynamic object. */
3ec01793 3881 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
625af618
AM
3882 return FALSE;
3883
3884 /* We may need a copy reloc too. */
3885 h->non_got_ref = 1;
3886 h->pointer_equality_needed = 1;
3887 }
3888 goto dodyn;
9edfd1af 3889
25dbc73a
AM
3890 case R_PPC_REL24:
3891 case R_PPC_REL14:
3892 case R_PPC_REL14_BRTAKEN:
3893 case R_PPC_REL14_BRNTAKEN:
d7128ce4 3894 if (h == NULL)
25dbc73a 3895 break;
7123df0e 3896 if (h == htab->elf.hgot)
d7128ce4 3897 {
7123df0e 3898 if (htab->plt_type == PLT_UNSET)
016687f8
AM
3899 {
3900 htab->plt_type = PLT_OLD;
3901 htab->old_bfd = abfd;
3902 }
d7128ce4
AM
3903 break;
3904 }
25dbc73a 3905 /* fall through */
ee05f2fe 3906
25dbc73a 3907 case R_PPC_ADDR24:
25dbc73a
AM
3908 case R_PPC_ADDR14:
3909 case R_PPC_ADDR14_BRTAKEN:
3910 case R_PPC_ADDR14_BRNTAKEN:
25dbc73a
AM
3911 if (h != NULL && !info->shared)
3912 {
3913 /* We may need a plt entry if the symbol turns out to be
3914 a function defined in a dynamic object. */
e054468f 3915 h->needs_plt = 1;
de972ffa 3916 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
a6aa5195 3917 return FALSE;
625af618 3918 break;
25dbc73a 3919 }
7619e7c7 3920
25dbc73a
AM
3921 dodyn:
3922 /* If we are creating a shared library, and this is a reloc
3923 against a global symbol, or a non PC relative reloc
3924 against a local symbol, then we need to copy the reloc
3925 into the shared library. However, if we are linking with
3926 -Bsymbolic, we do not need to copy a reloc against a
3927 global symbol which is defined in an object we are
3928 including in the link (i.e., DEF_REGULAR is set). At
3929 this point we have not seen all the input files, so it is
3930 possible that DEF_REGULAR is not set now but will be set
3931 later (it is never cleared). In case of a weak definition,
3932 DEF_REGULAR may be cleared later by a strong definition in
3933 a shared library. We account for that possibility below by
3934 storing information in the dyn_relocs field of the hash
3935 table entry. A similar situation occurs when creating
3936 shared libraries and symbol visibility changes render the
3937 symbol local.
7619e7c7 3938
25dbc73a
AM
3939 If on the other hand, we are creating an executable, we
3940 may need to keep relocations for symbols satisfied by a
3941 dynamic library if we manage to avoid copy relocs for the
3942 symbol. */
3943 if ((info->shared
1d483afe 3944 && (must_be_dyn_reloc (info, r_type)
25dbc73a
AM
3945 || (h != NULL
3946 && (! info->symbolic
3947 || h->root.type == bfd_link_hash_defweak
3948 || !h->def_regular))))
3949 || (ELIMINATE_COPY_RELOCS
3950 && !info->shared
25dbc73a
AM
3951 && h != NULL
3952 && (h->root.type == bfd_link_hash_defweak
de972ffa 3953 || !h->def_regular)))
25dbc73a
AM
3954 {
3955 struct ppc_elf_dyn_relocs *p;
91d6fa6a 3956 struct ppc_elf_dyn_relocs **rel_head;
7619e7c7 3957
25dbc73a
AM
3958#ifdef DEBUG
3959 fprintf (stderr,
3960 "ppc_elf_check_relocs needs to "
3961 "create relocation for %s\n",
3962 (h && h->root.root.string
3963 ? h->root.root.string : "<unknown>"));
3964#endif
3965 if (sreloc == NULL)
3966 {
c9a2f333
AM
3967 if (htab->elf.dynobj == NULL)
3968 htab->elf.dynobj = abfd;
83bac4b0
NC
3969
3970 sreloc = _bfd_elf_make_dynamic_reloc_section
3971 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ TRUE);
3972
25dbc73a 3973 if (sreloc == NULL)
83bac4b0 3974 return FALSE;
25dbc73a 3975 }
7619e7c7 3976
25dbc73a
AM
3977 /* If this is a global symbol, we count the number of
3978 relocations we need for this symbol. */
3979 if (h != NULL)
3980 {
91d6fa6a 3981 rel_head = &ppc_elf_hash_entry (h)->dyn_relocs;
25dbc73a
AM
3982 }
3983 else
3984 {
3985 /* Track dynamic relocs needed for local syms too.
3986 We really need local syms available to do this
3987 easily. Oh well. */
25dbc73a 3988 asection *s;
6edfbbad 3989 void *vpp;
87d72d41 3990 Elf_Internal_Sym *isym;
6edfbbad 3991
87d72d41
AM
3992 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3993 abfd, r_symndx);
3994 if (isym == NULL)
25dbc73a 3995 return FALSE;
7fce784e 3996
87d72d41
AM
3997 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3998 if (s == NULL)
3999 s = sec;
4000
6edfbbad 4001 vpp = &elf_section_data (s)->local_dynrel;
91d6fa6a 4002 rel_head = (struct ppc_elf_dyn_relocs **) vpp;
25dbc73a 4003 }
7fce784e 4004
91d6fa6a 4005 p = *rel_head;
25dbc73a
AM
4006 if (p == NULL || p->sec != sec)
4007 {
4008 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4009 if (p == NULL)
4010 return FALSE;
91d6fa6a
NC
4011 p->next = *rel_head;
4012 *rel_head = p;
25dbc73a
AM
4013 p->sec = sec;
4014 p->count = 0;
4015 p->pc_count = 0;
4016 }
4017
4018 p->count += 1;
1d483afe 4019 if (!must_be_dyn_reloc (info, r_type))
25dbc73a 4020 p->pc_count += 1;
7619e7c7 4021 }
25dbc73a
AM
4022
4023 break;
7619e7c7
AM
4024 }
4025 }
ee05f2fe 4026
25dbc73a
AM
4027 return TRUE;
4028}
4029\f
34c8bcba
JM
4030
4031/* Merge object attributes from IBFD into OBFD. Raise an error if
4032 there are conflicting attributes. */
4033static bfd_boolean
4034ppc_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
4035{
c6e65352
DJ
4036 obj_attribute *in_attr, *in_attrs;
4037 obj_attribute *out_attr, *out_attrs;
34c8bcba
JM
4038
4039 if (!elf_known_obj_attributes_proc (obfd)[0].i)
4040 {
4041 /* This is the first object. Copy the attributes. */
4042 _bfd_elf_copy_obj_attributes (ibfd, obfd);
4043
4044 /* Use the Tag_null value to indicate the attributes have been
4045 initialized. */
4046 elf_known_obj_attributes_proc (obfd)[0].i = 1;
4047
4048 return TRUE;
4049 }
4050
c6e65352
DJ
4051 in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
4052 out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
4053
34c8bcba
JM
4054 /* Check for conflicting Tag_GNU_Power_ABI_FP attributes and merge
4055 non-conflicting ones. */
c6e65352
DJ
4056 in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
4057 out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
4058 if (in_attr->i != out_attr->i)
34c8bcba 4059 {
c6e65352
DJ
4060 out_attr->type = 1;
4061 if (out_attr->i == 0)
4062 out_attr->i = in_attr->i;
4063 else if (in_attr->i == 0)
34c8bcba 4064 ;
c6e65352 4065 else if (out_attr->i == 1 && in_attr->i == 2)
34c8bcba
JM
4066 _bfd_error_handler
4067 (_("Warning: %B uses hard float, %B uses soft float"), obfd, ibfd);
3c7b9897
AM
4068 else if (out_attr->i == 1 && in_attr->i == 3)
4069 _bfd_error_handler
4070 (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"),
4071 obfd, ibfd);
4072 else if (out_attr->i == 3 && in_attr->i == 1)
4073 _bfd_error_handler
4074 (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"),
4075 ibfd, obfd);
4076 else if (out_attr->i == 3 && in_attr->i == 2)
4077 _bfd_error_handler
4078 (_("Warning: %B uses soft float, %B uses single-precision hard float"),
4079 ibfd, obfd);
4080 else if (out_attr->i == 2 && (in_attr->i == 1 || in_attr->i == 3))
34c8bcba
JM
4081 _bfd_error_handler
4082 (_("Warning: %B uses hard float, %B uses soft float"), ibfd, obfd);
3c7b9897 4083 else if (in_attr->i > 3)
34c8bcba
JM
4084 _bfd_error_handler
4085 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
c6e65352 4086 in_attr->i);
34c8bcba
JM
4087 else
4088 _bfd_error_handler
4089 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
c6e65352
DJ
4090 out_attr->i);
4091 }
4092
4093 /* Check for conflicting Tag_GNU_Power_ABI_Vector attributes and
4094 merge non-conflicting ones. */
4095 in_attr = &in_attrs[Tag_GNU_Power_ABI_Vector];
4096 out_attr = &out_attrs[Tag_GNU_Power_ABI_Vector];
4097 if (in_attr->i != out_attr->i)
4098 {
4099 const char *in_abi = NULL, *out_abi = NULL;
4100
4101 switch (in_attr->i)
4102 {
4103 case 1: in_abi = "generic"; break;
4104 case 2: in_abi = "AltiVec"; break;
4105 case 3: in_abi = "SPE"; break;
4106 }
4107
4108 switch (out_attr->i)
4109 {
4110 case 1: out_abi = "generic"; break;
4111 case 2: out_abi = "AltiVec"; break;
4112 case 3: out_abi = "SPE"; break;
4113 }
4114
4115 out_attr->type = 1;
4116 if (out_attr->i == 0)
4117 out_attr->i = in_attr->i;
4118 else if (in_attr->i == 0)
4119 ;
4120 /* For now, allow generic to transition to AltiVec or SPE
4121 without a warning. If GCC marked files with their stack
4122 alignment and used don't-care markings for files which are
4123 not affected by the vector ABI, we could warn about this
4124 case too. */
4125 else if (out_attr->i == 1)
4126 out_attr->i = in_attr->i;
4127 else if (in_attr->i == 1)
4128 ;
4129 else if (in_abi == NULL)
4130 _bfd_error_handler
4131 (_("Warning: %B uses unknown vector ABI %d"), ibfd,
4132 in_attr->i);
4133 else if (out_abi == NULL)
4134 _bfd_error_handler
4135 (_("Warning: %B uses unknown vector ABI %d"), obfd,
4136 in_attr->i);
4137 else
4138 _bfd_error_handler
4139 (_("Warning: %B uses vector ABI \"%s\", %B uses \"%s\""),
4140 ibfd, obfd, in_abi, out_abi);
34c8bcba
JM
4141 }
4142
f82e0623
NF
4143 /* Check for conflicting Tag_GNU_Power_ABI_Struct_Return attributes
4144 and merge non-conflicting ones. */
4145 in_attr = &in_attrs[Tag_GNU_Power_ABI_Struct_Return];
4146 out_attr = &out_attrs[Tag_GNU_Power_ABI_Struct_Return];
4147 if (in_attr->i != out_attr->i)
4148 {
4149 out_attr->type = 1;
4150 if (out_attr->i == 0)
4151 out_attr->i = in_attr->i;
4152 else if (in_attr->i == 0)
4153 ;
4154 else if (out_attr->i == 1 && in_attr->i == 2)
4155 _bfd_error_handler
4156 (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), obfd, ibfd);
4157 else if (out_attr->i == 2 && in_attr->i == 1)
4158 _bfd_error_handler
4159 (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), ibfd, obfd);
4160 else if (in_attr->i > 2)
4161 _bfd_error_handler
4162 (_("Warning: %B uses unknown small structure return convention %d"), ibfd,
4163 in_attr->i);
4164 else
4165 _bfd_error_handler
4166 (_("Warning: %B uses unknown small structure return convention %d"), obfd,
4167 out_attr->i);
4168 }
4169
34c8bcba
JM
4170 /* Merge Tag_compatibility attributes and any common GNU ones. */
4171 _bfd_elf_merge_object_attributes (ibfd, obfd);
4172
4173 return TRUE;
4174}
4175
8853c3d3
AM
4176/* Merge backend specific data from an object file to the output
4177 object file when linking. */
4178
4179static bfd_boolean
4180ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4181{
4182 flagword old_flags;
4183 flagword new_flags;
4184 bfd_boolean error;
4185
0c8d6e5c 4186 if (!is_ppc_elf (ibfd) || !is_ppc_elf (obfd))
8853c3d3
AM
4187 return TRUE;
4188
4189 /* Check if we have the same endianess. */
4190 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
4191 return FALSE;
4192
34c8bcba
JM
4193 if (!ppc_elf_merge_obj_attributes (ibfd, obfd))
4194 return FALSE;
4195
8853c3d3
AM
4196 new_flags = elf_elfheader (ibfd)->e_flags;
4197 old_flags = elf_elfheader (obfd)->e_flags;
4198 if (!elf_flags_init (obfd))
4199 {
4200 /* First call, no flags set. */
4201 elf_flags_init (obfd) = TRUE;
4202 elf_elfheader (obfd)->e_flags = new_flags;
4203 }
4204
4205 /* Compatible flags are ok. */
4206 else if (new_flags == old_flags)
4207 ;
4208
4209 /* Incompatible flags. */
4210 else
4211 {
4212 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
4213 to be linked with either. */
4214 error = FALSE;
4215 if ((new_flags & EF_PPC_RELOCATABLE) != 0
4216 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
4217 {
4218 error = TRUE;
4219 (*_bfd_error_handler)
4220 (_("%B: compiled with -mrelocatable and linked with "
4221 "modules compiled normally"), ibfd);
4222 }
4223 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
4224 && (old_flags & EF_PPC_RELOCATABLE) != 0)
4225 {
4226 error = TRUE;
4227 (*_bfd_error_handler)
4228 (_("%B: compiled normally and linked with "
4229 "modules compiled with -mrelocatable"), ibfd);
4230 }
4231
4232 /* The output is -mrelocatable-lib iff both the input files are. */
4233 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
4234 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
4235
4236 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
4237 but each input file is either -mrelocatable or -mrelocatable-lib. */
4238 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
4239 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
4240 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
4241 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
4242
4243 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
4244 any module uses it. */
4245 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
4246
4247 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4248 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4249
4250 /* Warn about any other mismatches. */
4251 if (new_flags != old_flags)
4252 {
4253 error = TRUE;
4254 (*_bfd_error_handler)
4255 (_("%B: uses different e_flags (0x%lx) fields "
4256 "than previous modules (0x%lx)"),
4257 ibfd, (long) new_flags, (long) old_flags);
4258 }
4259
4260 if (error)
4261 {
4262 bfd_set_error (bfd_error_bad_value);
4263 return FALSE;
4264 }
4265 }
4266
4267 return TRUE;
4268}
4269\f
d7128ce4
AM
4270/* Choose which PLT scheme to use, and set .plt flags appropriately.
4271 Returns -1 on error, 0 for old PLT, 1 for new PLT. */
4272int
4273ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
4274 struct bfd_link_info *info,
016687f8 4275 enum ppc_elf_plt_type plt_style,
0ba07910 4276 int emit_stub_syms)
d7128ce4
AM
4277{
4278 struct ppc_elf_link_hash_table *htab;
9d8504b1 4279 flagword flags;
d7128ce4
AM
4280
4281 htab = ppc_elf_hash_table (info);
4a3dc543 4282
a7f2871e
AM
4283 htab->emit_stub_syms = emit_stub_syms;
4284
4a3dc543 4285 if (htab->plt_type == PLT_UNSET)
016687f8
AM
4286 {
4287 if (plt_style == PLT_OLD)
4288 htab->plt_type = PLT_OLD;
4289 else
4290 {
4291 bfd *ibfd;
4292 enum ppc_elf_plt_type plt_type = plt_style;
4293
4294 /* Look through the reloc flags left by ppc_elf_check_relocs.
4295 Use the old style bss plt if a file makes plt calls
4296 without using the new relocs, and if ld isn't given
4297 --secure-plt and we never see REL16 relocs. */
4298 if (plt_type == PLT_UNSET)
4299 plt_type = PLT_OLD;
60b31e8d 4300 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
0c8d6e5c 4301 if (is_ppc_elf (ibfd))
016687f8
AM
4302 {
4303 if (ppc_elf_tdata (ibfd)->has_rel16)
4304 plt_type = PLT_NEW;
4305 else if (ppc_elf_tdata (ibfd)->makes_plt_call)
4306 {
4307 plt_type = PLT_OLD;
4308 htab->old_bfd = ibfd;
4309 break;
4310 }
4311 }
4312 htab->plt_type = plt_type;
4313 }
4314 }
4315 if (htab->plt_type == PLT_OLD && plt_style == PLT_NEW)
4316 info->callbacks->info (_("Using bss-plt due to %B"), htab->old_bfd);
d7128ce4 4317
4a3dc543 4318 BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
9d8504b1 4319
4a3dc543 4320 if (htab->plt_type == PLT_NEW)
9d8504b1
PB
4321 {
4322 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4323 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
86b9da88
AM
4324
4325 /* The new PLT is a loaded section. */
4326 if (htab->plt != NULL
4327 && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
4328 return -1;
4329
4330 /* The new GOT is not executable. */
4331 if (htab->got != NULL
4332 && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
4333 return -1;
d7128ce4
AM
4334 }
4335 else
4336 {
4337 /* Stop an unused .glink section from affecting .text alignment. */
86b9da88
AM
4338 if (htab->glink != NULL
4339 && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
4340 return -1;
d7128ce4 4341 }
4a3dc543 4342 return htab->plt_type == PLT_NEW;
d7128ce4
AM
4343}
4344\f
25dbc73a
AM
4345/* Return the section that should be marked against GC for a given
4346 relocation. */
586119b3 4347
25dbc73a
AM
4348static asection *
4349ppc_elf_gc_mark_hook (asection *sec,
07adf181 4350 struct bfd_link_info *info,
25dbc73a
AM
4351 Elf_Internal_Rela *rel,
4352 struct elf_link_hash_entry *h,
4353 Elf_Internal_Sym *sym)
4354{
4355 if (h != NULL)
07adf181
AM
4356 switch (ELF32_R_TYPE (rel->r_info))
4357 {
4358 case R_PPC_GNU_VTINHERIT:
4359 case R_PPC_GNU_VTENTRY:
4360 return NULL;
4361 }
4362
4363 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
25dbc73a 4364}
a876ecf9 4365
25dbc73a
AM
4366/* Update the got, plt and dynamic reloc reference counts for the
4367 section being removed. */
4368
4369static bfd_boolean
4370ppc_elf_gc_sweep_hook (bfd *abfd,
4371 struct bfd_link_info *info,
4372 asection *sec,
4373 const Elf_Internal_Rela *relocs)
4374{
4375 struct ppc_elf_link_hash_table *htab;
4376 Elf_Internal_Shdr *symtab_hdr;
4377 struct elf_link_hash_entry **sym_hashes;
4378 bfd_signed_vma *local_got_refcounts;
4379 const Elf_Internal_Rela *rel, *relend;
a6aa5195 4380 asection *got2;
25dbc73a 4381
7dda2462
TG
4382 if (info->relocatable)
4383 return TRUE;
4384
c87b5a93
AM
4385 if ((sec->flags & SEC_ALLOC) == 0)
4386 return TRUE;
4387
25dbc73a
AM
4388 elf_section_data (sec)->local_dynrel = NULL;
4389
4390 htab = ppc_elf_hash_table (info);
0ffa91dd 4391 symtab_hdr = &elf_symtab_hdr (abfd);
25dbc73a
AM
4392 sym_hashes = elf_sym_hashes (abfd);
4393 local_got_refcounts = elf_local_got_refcounts (abfd);
a6aa5195 4394 got2 = bfd_get_section_by_name (abfd, ".got2");
25dbc73a
AM
4395
4396 relend = relocs + sec->reloc_count;
4397 for (rel = relocs; rel < relend; rel++)
4398 {
4399 unsigned long r_symndx;
4400 enum elf_ppc_reloc_type r_type;
4401 struct elf_link_hash_entry *h = NULL;
4402
4403 r_symndx = ELF32_R_SYM (rel->r_info);
4404 if (r_symndx >= symtab_hdr->sh_info)
a876ecf9 4405 {
25dbc73a
AM
4406 struct ppc_elf_dyn_relocs **pp, *p;
4407 struct ppc_elf_link_hash_entry *eh;
4408
4409 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4410 while (h->root.type == bfd_link_hash_indirect
4411 || h->root.type == bfd_link_hash_warning)
4412 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4413 eh = (struct ppc_elf_link_hash_entry *) h;
4414
4415 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4416 if (p->sec == sec)
4417 {
4418 /* Everything must go for SEC. */
4419 *pp = p->next;
4420 break;
4421 }
a876ecf9 4422 }
ee05f2fe 4423
25dbc73a 4424 r_type = ELF32_R_TYPE (rel->r_info);
de972ffa
AM
4425 if (!htab->is_vxworks
4426 && h == NULL
4427 && local_got_refcounts != NULL
4428 && (!info->shared
4429 || is_branch_reloc (r_type)))
e054468f 4430 {
de972ffa
AM
4431 struct plt_entry **local_plt = (struct plt_entry **)
4432 (local_got_refcounts + symtab_hdr->sh_info);
4433 char *local_got_tls_masks = (char *)
4434 (local_plt + symtab_hdr->sh_info);
4435 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
e054468f 4436 {
de972ffa 4437 struct plt_entry **ifunc = local_plt + r_symndx;
a877a2b6
AM
4438 bfd_vma addend = 0;
4439 struct plt_entry *ent;
4440
4441 if (r_type == R_PPC_PLTREL24 && info->shared)
4442 addend = rel->r_addend;
4443 ent = find_plt_ent (ifunc, got2, addend);
e054468f
AM
4444 if (ent->plt.refcount > 0)
4445 ent->plt.refcount -= 1;
4446 continue;
4447 }
4448 }
4449
25dbc73a 4450 switch (r_type)
ee05f2fe 4451 {
25dbc73a
AM
4452 case R_PPC_GOT_TLSLD16:
4453 case R_PPC_GOT_TLSLD16_LO:
4454 case R_PPC_GOT_TLSLD16_HI:
4455 case R_PPC_GOT_TLSLD16_HA:
25dbc73a
AM
4456 case R_PPC_GOT_TLSGD16:
4457 case R_PPC_GOT_TLSGD16_LO:
4458 case R_PPC_GOT_TLSGD16_HI:
4459 case R_PPC_GOT_TLSGD16_HA:
4460 case R_PPC_GOT_TPREL16:
4461 case R_PPC_GOT_TPREL16_LO:
4462 case R_PPC_GOT_TPREL16_HI:
4463 case R_PPC_GOT_TPREL16_HA:
4464 case R_PPC_GOT_DTPREL16:
4465 case R_PPC_GOT_DTPREL16_LO:
4466 case R_PPC_GOT_DTPREL16_HI:
4467 case R_PPC_GOT_DTPREL16_HA:
4468 case R_PPC_GOT16:
4469 case R_PPC_GOT16_LO:
4470 case R_PPC_GOT16_HI:
4471 case R_PPC_GOT16_HA:
4472 if (h != NULL)
ee05f2fe 4473 {
25dbc73a
AM
4474 if (h->got.refcount > 0)
4475 h->got.refcount--;
91e21fb7
AM
4476 if (!info->shared)
4477 {
4478 struct plt_entry *ent;
4479
4480 ent = find_plt_ent (&h->plt.plist, NULL, 0);
32af9f6e 4481 if (ent != NULL && ent->plt.refcount > 0)
91e21fb7
AM
4482 ent->plt.refcount -= 1;
4483 }
ee05f2fe 4484 }
25dbc73a
AM
4485 else if (local_got_refcounts != NULL)
4486 {
4487 if (local_got_refcounts[r_symndx] > 0)
4488 local_got_refcounts[r_symndx]--;
4489 }
4490 break;
ee05f2fe 4491
25dbc73a
AM
4492 case R_PPC_REL24:
4493 case R_PPC_REL14:
4494 case R_PPC_REL14_BRTAKEN:
4495 case R_PPC_REL14_BRNTAKEN:
4496 case R_PPC_REL32:
3b36f7e6 4497 if (h == NULL || h == htab->elf.hgot)
25dbc73a
AM
4498 break;
4499 /* Fall thru */
ee05f2fe 4500
25dbc73a
AM
4501 case R_PPC_ADDR32:
4502 case R_PPC_ADDR24:
4503 case R_PPC_ADDR16:
4504 case R_PPC_ADDR16_LO:
4505 case R_PPC_ADDR16_HI:
4506 case R_PPC_ADDR16_HA:
4507 case R_PPC_ADDR14:
4508 case R_PPC_ADDR14_BRTAKEN:
4509 case R_PPC_ADDR14_BRNTAKEN:
4510 case R_PPC_UADDR32:
4511 case R_PPC_UADDR16:
87538722
AM
4512 if (info->shared)
4513 break;
4514
25dbc73a
AM
4515 case R_PPC_PLT32:
4516 case R_PPC_PLTREL24:
87538722 4517 case R_PPC_PLTREL32:
25dbc73a
AM
4518 case R_PPC_PLT16_LO:
4519 case R_PPC_PLT16_HI:
4520 case R_PPC_PLT16_HA:
4521 if (h != NULL)
4522 {
a877a2b6
AM
4523 bfd_vma addend = 0;
4524 struct plt_entry *ent;
4525
4526 if (r_type == R_PPC_PLTREL24 && info->shared)
4527 addend = rel->r_addend;
4528 ent = find_plt_ent (&h->plt.plist, got2, addend);
32af9f6e 4529 if (ent != NULL && ent->plt.refcount > 0)
a6aa5195 4530 ent->plt.refcount -= 1;
25dbc73a
AM
4531 }
4532 break;
ee05f2fe 4533
25dbc73a
AM
4534 default:
4535 break;
4536 }
7fce784e 4537 }
7fce784e
AS
4538 return TRUE;
4539}
25dbc73a 4540\f
3a71aa26
AM
4541/* Set plt output section type, htab->tls_get_addr, and call the
4542 generic ELF tls_setup function. */
7fce784e 4543
25dbc73a 4544asection *
a7f2871e
AM
4545ppc_elf_tls_setup (bfd *obfd,
4546 struct bfd_link_info *info,
4547 int no_tls_get_addr_opt)
7fce784e 4548{
25dbc73a 4549 struct ppc_elf_link_hash_table *htab;
7fce784e 4550
25dbc73a 4551 htab = ppc_elf_hash_table (info);
a7f2871e
AM
4552 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4553 FALSE, FALSE, TRUE);
4554 if (!no_tls_get_addr_opt)
4555 {
4556 struct elf_link_hash_entry *opt, *tga;
4557 opt = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
4558 FALSE, FALSE, TRUE);
4559 if (opt != NULL
4560 && (opt->root.type == bfd_link_hash_defined
4561 || opt->root.type == bfd_link_hash_defweak))
4562 {
4563 /* If glibc supports an optimized __tls_get_addr call stub,
4564 signalled by the presence of __tls_get_addr_opt, and we'll
4565 be calling __tls_get_addr via a plt call stub, then
4566 make __tls_get_addr point to __tls_get_addr_opt. */
4567 tga = htab->tls_get_addr;
4568 if (htab->elf.dynamic_sections_created
4569 && tga != NULL
4570 && (tga->type == STT_FUNC
4571 || tga->needs_plt)
4572 && !(SYMBOL_CALLS_LOCAL (info, tga)
4573 || (ELF_ST_VISIBILITY (tga->other) != STV_DEFAULT
4574 && tga->root.type == bfd_link_hash_undefweak)))
4575 {
4576 struct plt_entry *ent;
32af9f6e
AM
4577 for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
4578 if (ent->plt.refcount > 0)
4579 break;
4580 if (ent != NULL)
a7f2871e
AM
4581 {
4582 tga->root.type = bfd_link_hash_indirect;
4583 tga->root.u.i.link = &opt->root;
4584 ppc_elf_copy_indirect_symbol (info, opt, tga);
4585 if (opt->dynindx != -1)
4586 {
4587 /* Use __tls_get_addr_opt in dynamic relocations. */
4588 opt->dynindx = -1;
4589 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4590 opt->dynstr_index);
4591 if (!bfd_elf_link_record_dynamic_symbol (info, opt))
4592 return FALSE;
4593 }
4594 htab->tls_get_addr = opt;
4595 }
4596 }
4597 }
4598 else
4599 no_tls_get_addr_opt = TRUE;
4600 }
4601 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
4a3dc543 4602 if (htab->plt_type == PLT_NEW
d7128ce4
AM
4603 && htab->plt != NULL
4604 && htab->plt->output_section != NULL)
4605 {
4606 elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
4607 elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
4608 }
4609
25dbc73a 4610 return _bfd_elf_tls_setup (obfd, info);
7fce784e
AS
4611}
4612
3a71aa26
AM
4613/* Return TRUE iff REL is a branch reloc with a global symbol matching
4614 HASH. */
4615
4616static bfd_boolean
4617branch_reloc_hash_match (const bfd *ibfd,
4618 const Elf_Internal_Rela *rel,
4619 const struct elf_link_hash_entry *hash)
4620{
4621 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4622 enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
4623 unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
4624
e054468f 4625 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
3a71aa26
AM
4626 {
4627 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4628 struct elf_link_hash_entry *h;
4629
4630 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4631 while (h->root.type == bfd_link_hash_indirect
4632 || h->root.type == bfd_link_hash_warning)
4633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4634 if (h == hash)
4635 return TRUE;
4636 }
4637 return FALSE;
4638}
4639
25dbc73a
AM
4640/* Run through all the TLS relocs looking for optimization
4641 opportunities. */
252b5132 4642
25dbc73a
AM
4643bfd_boolean
4644ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4645 struct bfd_link_info *info)
252b5132 4646{
7fce784e 4647 bfd *ibfd;
25dbc73a
AM
4648 asection *sec;
4649 struct ppc_elf_link_hash_table *htab;
b7fcf6f6 4650 int pass;
252b5132 4651
1d483afe 4652 if (info->relocatable || !info->executable)
25dbc73a 4653 return TRUE;
252b5132 4654
7619e7c7 4655 htab = ppc_elf_hash_table (info);
b7fcf6f6
AM
4656 /* Make two passes through the relocs. First time check that tls
4657 relocs involved in setting up a tls_get_addr call are indeed
4658 followed by such a call. If they are not, exclude them from
4659 the optimizations done on the second pass. */
4660 for (pass = 0; pass < 2; ++pass)
4661 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4662 {
4663 Elf_Internal_Sym *locsyms = NULL;
0ffa91dd 4664 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
32af9f6e 4665 asection *got2 = bfd_get_section_by_name (ibfd, ".got2");
7619e7c7 4666
b7fcf6f6
AM
4667 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4668 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4669 {
4670 Elf_Internal_Rela *relstart, *rel, *relend;
252b5132 4671
b7fcf6f6
AM
4672 /* Read the relocations. */
4673 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4674 info->keep_memory);
4675 if (relstart == NULL)
4676 return FALSE;
7619e7c7 4677
b7fcf6f6
AM
4678 relend = relstart + sec->reloc_count;
4679 for (rel = relstart; rel < relend; rel++)
4680 {
4681 enum elf_ppc_reloc_type r_type;
4682 unsigned long r_symndx;
4683 struct elf_link_hash_entry *h = NULL;
4684 char *tls_mask;
4685 char tls_set, tls_clear;
4686 bfd_boolean is_local;
4687 int expecting_tls_get_addr;
4688 bfd_signed_vma *got_count;
4689
4690 r_symndx = ELF32_R_SYM (rel->r_info);
4691 if (r_symndx >= symtab_hdr->sh_info)
4692 {
4693 struct elf_link_hash_entry **sym_hashes;
7fce784e 4694
b7fcf6f6
AM
4695 sym_hashes = elf_sym_hashes (ibfd);
4696 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4697 while (h->root.type == bfd_link_hash_indirect
4698 || h->root.type == bfd_link_hash_warning)
4699 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4700 }
7619e7c7 4701
b7fcf6f6
AM
4702 expecting_tls_get_addr = 0;
4703 is_local = FALSE;
4704 if (h == NULL
4705 || !h->def_dynamic)
4706 is_local = TRUE;
7619e7c7 4707
b7fcf6f6
AM
4708 r_type = ELF32_R_TYPE (rel->r_info);
4709 switch (r_type)
4710 {
4711 case R_PPC_GOT_TLSLD16:
4712 case R_PPC_GOT_TLSLD16_LO:
4713 expecting_tls_get_addr = 1;
4714 /* Fall thru */
4715
4716 case R_PPC_GOT_TLSLD16_HI:
4717 case R_PPC_GOT_TLSLD16_HA:
4718 /* These relocs should never be against a symbol
4719 defined in a shared lib. Leave them alone if
4720 that turns out to be the case. */
4721 if (!is_local)
4722 continue;
4723
4724 /* LD -> LE */
4725 tls_set = 0;
4726 tls_clear = TLS_LD;
4727 break;
4728
4729 case R_PPC_GOT_TLSGD16:
4730 case R_PPC_GOT_TLSGD16_LO:
4731 expecting_tls_get_addr = 1;
4732 /* Fall thru */
4733
4734 case R_PPC_GOT_TLSGD16_HI:
4735 case R_PPC_GOT_TLSGD16_HA:
4736 if (is_local)
4737 /* GD -> LE */
4738 tls_set = 0;
4739 else
4740 /* GD -> IE */
4741 tls_set = TLS_TLS | TLS_TPRELGD;
4742 tls_clear = TLS_GD;
4743 break;
4744
4745 case R_PPC_GOT_TPREL16:
4746 case R_PPC_GOT_TPREL16_LO:
4747 case R_PPC_GOT_TPREL16_HI:
4748 case R_PPC_GOT_TPREL16_HA:
4749 if (is_local)
4750 {
4751 /* IE -> LE */
4752 tls_set = 0;
4753 tls_clear = TLS_TPREL;
4754 break;
4755 }
4756 else
4757 continue;
4758
32af9f6e
AM
4759 case R_PPC_TLSGD:
4760 case R_PPC_TLSLD:
4761 expecting_tls_get_addr = 2;
4762 tls_set = 0;
4763 tls_clear = 0;
4764 break;
4765
b7fcf6f6 4766 default:
25dbc73a 4767 continue;
b7fcf6f6 4768 }
25dbc73a 4769
b7fcf6f6
AM
4770 if (pass == 0)
4771 {
727fc41e 4772 if (!expecting_tls_get_addr
32af9f6e
AM
4773 || (expecting_tls_get_addr == 1
4774 && !sec->has_tls_get_addr_call))
b7fcf6f6 4775 continue;
25dbc73a 4776
3a71aa26
AM
4777 if (rel + 1 < relend
4778 && branch_reloc_hash_match (ibfd, rel + 1,
4779 htab->tls_get_addr))
4780 continue;
25dbc73a 4781
b7fcf6f6
AM
4782 /* Uh oh, we didn't find the expected call. We
4783 could just mark this symbol to exclude it
4784 from tls optimization but it's safer to skip
4785 the entire section. */
4786 sec->has_tls_reloc = 0;
4787 break;
4788 }
25dbc73a 4789
32af9f6e
AM
4790 if (expecting_tls_get_addr)
4791 {
4792 struct plt_entry *ent;
4793 bfd_vma addend = 0;
4794
4795 if (info->shared
4796 && ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTREL24)
4797 addend = rel[1].r_addend;
4798 ent = find_plt_ent (&htab->tls_get_addr->plt.plist,
4799 got2, addend);
4800 if (ent != NULL && ent->plt.refcount > 0)
4801 ent->plt.refcount -= 1;
4802
4803 if (expecting_tls_get_addr == 2)
4804 continue;
4805 }
4806
b7fcf6f6
AM
4807 if (h != NULL)
4808 {
4809 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4810 got_count = &h->got.refcount;
4811 }
4812 else
4813 {
4814 Elf_Internal_Sym *sym;
4815 bfd_signed_vma *lgot_refs;
e054468f 4816 struct plt_entry **local_plt;
b7fcf6f6 4817 char *lgot_masks;
25dbc73a 4818
b7fcf6f6
AM
4819 if (locsyms == NULL)
4820 {
4821 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4822 if (locsyms == NULL)
4823 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4824 symtab_hdr->sh_info,
4825 0, NULL, NULL, NULL);
4826 if (locsyms == NULL)
4827 {
4828 if (elf_section_data (sec)->relocs != relstart)
4829 free (relstart);
4830 return FALSE;
4831 }
4832 }
4833 sym = locsyms + r_symndx;
4834 lgot_refs = elf_local_got_refcounts (ibfd);
4835 if (lgot_refs == NULL)
4836 abort ();
e054468f
AM
4837 local_plt = (struct plt_entry **)
4838 (lgot_refs + symtab_hdr->sh_info);
4839 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
b7fcf6f6
AM
4840 tls_mask = &lgot_masks[r_symndx];
4841 got_count = &lgot_refs[r_symndx];
4842 }
25dbc73a 4843
b7fcf6f6
AM
4844 if (tls_set == 0)
4845 {
4846 /* We managed to get rid of a got entry. */
4847 if (*got_count > 0)
4848 *got_count -= 1;
4849 }
25dbc73a 4850
b7fcf6f6
AM
4851 *tls_mask |= tls_set;
4852 *tls_mask &= ~tls_clear;
4853 }
25dbc73a 4854
b7fcf6f6
AM
4855 if (elf_section_data (sec)->relocs != relstart)
4856 free (relstart);
4857 }
4858
4859 if (locsyms != NULL
4860 && (symtab_hdr->contents != (unsigned char *) locsyms))
4861 {
4862 if (!info->keep_memory)
4863 free (locsyms);
4864 else
4865 symtab_hdr->contents = (unsigned char *) locsyms;
4866 }
4867 }
25dbc73a
AM
4868 return TRUE;
4869}
4870\f
625af618
AM
4871/* Return true if we have dynamic relocs that apply to read-only sections. */
4872
4873static bfd_boolean
4874readonly_dynrelocs (struct elf_link_hash_entry *h)
4875{
4876 struct ppc_elf_dyn_relocs *p;
4877
4878 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4879 {
4880 asection *s = p->sec->output_section;
4881
4882 if (s != NULL
4883 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
4884 == (SEC_READONLY | SEC_ALLOC)))
4885 return TRUE;
4886 }
4887 return FALSE;
4888}
4889
25dbc73a
AM
4890/* Adjust a symbol defined by a dynamic object and referenced by a
4891 regular object. The current definition is in some section of the
4892 dynamic object, but we're not including those sections. We have to
4893 change the definition to something the rest of the link can
4894 understand. */
7fce784e 4895
25dbc73a
AM
4896static bfd_boolean
4897ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4898 struct elf_link_hash_entry *h)
4899{
4900 struct ppc_elf_link_hash_table *htab;
4901 asection *s;
7fce784e 4902
25dbc73a
AM
4903#ifdef DEBUG
4904 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
4905 h->root.root.string);
4906#endif
252b5132 4907
25dbc73a
AM
4908 /* Make sure we know what is going on here. */
4909 htab = ppc_elf_hash_table (info);
4910 BFD_ASSERT (htab->elf.dynobj != NULL
4911 && (h->needs_plt
91e21fb7 4912 || h->type == STT_GNU_IFUNC
25dbc73a
AM
4913 || h->u.weakdef != NULL
4914 || (h->def_dynamic
4915 && h->ref_regular
4916 && !h->def_regular)));
252b5132 4917
25dbc73a
AM
4918 /* Deal with function syms. */
4919 if (h->type == STT_FUNC
e054468f 4920 || h->type == STT_GNU_IFUNC
25dbc73a
AM
4921 || h->needs_plt)
4922 {
4923 /* Clear procedure linkage table information for any symbol that
4924 won't need a .plt entry. */
a6aa5195
AM
4925 struct plt_entry *ent;
4926 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4927 if (ent->plt.refcount > 0)
4928 break;
4929 if (ent == NULL
e054468f
AM
4930 || (h->type != STT_GNU_IFUNC
4931 && (SYMBOL_CALLS_LOCAL (info, h)
4932 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4933 && h->root.type == bfd_link_hash_undefweak))))
252b5132 4934 {
25dbc73a 4935 /* A PLT entry is not required/allowed when:
252b5132 4936
25dbc73a
AM
4937 1. We are not using ld.so; because then the PLT entry
4938 can't be set up, so we can't use one. In this case,
4939 ppc_elf_adjust_dynamic_symbol won't even be called.
252b5132 4940
25dbc73a
AM
4941 2. GC has rendered the entry unused.
4942
4943 3. We know for certain that a call to this symbol
4944 will go to this object, or will remain undefined. */
a6aa5195 4945 h->plt.plist = NULL;
25dbc73a
AM
4946 h->needs_plt = 0;
4947 }
625af618
AM
4948 else
4949 {
25f23106
AM
4950 /* After adjust_dynamic_symbol, non_got_ref set in the
4951 non-shared case means that we have allocated space in
4952 .dynbss for the symbol and thus dyn_relocs for this
4953 symbol should be discarded.
625af618
AM
4954 If we get here we know we are making a PLT entry for this
4955 symbol, and in an executable we'd normally resolve
4956 relocations against this symbol to the PLT entry. Allow
4957 dynamic relocs if the reference is weak, and the dynamic
4958 relocs will not cause text relocation. */
4959 if (!h->ref_regular_nonweak
4960 && h->non_got_ref
25f23106 4961 && h->type != STT_GNU_IFUNC
625af618
AM
4962 && !htab->is_vxworks
4963 && !ppc_elf_hash_entry (h)->has_sda_refs
4964 && !readonly_dynrelocs (h))
4965 h->non_got_ref = 0;
4966 }
25dbc73a 4967 return TRUE;
252b5132 4968 }
25dbc73a 4969 else
a6aa5195 4970 h->plt.plist = NULL;
252b5132 4971
25dbc73a
AM
4972 /* If this is a weak symbol, and there is a real definition, the
4973 processor independent code will have arranged for us to see the
4974 real definition first, and we can just use the same value. */
4975 if (h->u.weakdef != NULL)
252b5132 4976 {
25dbc73a
AM
4977 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4978 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4979 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4980 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4981 if (ELIMINATE_COPY_RELOCS)
4982 h->non_got_ref = h->u.weakdef->non_got_ref;
4983 return TRUE;
4984 }
dc810e39 4985
25dbc73a
AM
4986 /* This is a reference to a symbol defined by a dynamic object which
4987 is not a function. */
252b5132 4988
25dbc73a
AM
4989 /* If we are creating a shared library, we must presume that the
4990 only references to the symbol are via the global offset table.
4991 For such cases we need not do anything here; the relocations will
4992 be handled correctly by relocate_section. */
4993 if (info->shared)
4994 return TRUE;
252b5132 4995
25dbc73a
AM
4996 /* If there are no references to this symbol that do not use the
4997 GOT, we don't need to generate a copy reloc. */
4998 if (!h->non_got_ref)
4999 return TRUE;
5000
cfd2c773
NS
5001 /* If we didn't find any dynamic relocs in read-only sections, then
5002 we'll be keeping the dynamic relocs and avoiding the copy reloc.
5003 We can't do this if there are any small data relocations. This
5004 doesn't work on VxWorks, where we can not have dynamic
5005 relocations (other than copy and jump slot relocations) in an
5006 executable. */
4dc4a9a5 5007 if (ELIMINATE_COPY_RELOCS
cfd2c773 5008 && !ppc_elf_hash_entry (h)->has_sda_refs
625af618
AM
5009 && !htab->is_vxworks
5010 && !h->def_regular
5011 && !readonly_dynrelocs (h))
25dbc73a 5012 {
625af618
AM
5013 h->non_got_ref = 0;
5014 return TRUE;
252b5132
RH
5015 }
5016
909272ee
AM
5017 if (h->size == 0)
5018 {
5019 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5020 h->root.root.string);
5021 return TRUE;
5022 }
5023
25dbc73a
AM
5024 /* We must allocate the symbol in our .dynbss section, which will
5025 become part of the .bss section of the executable. There will be
5026 an entry for this symbol in the .dynsym section. The dynamic
5027 object will contain position independent code, so all references
5028 from the dynamic object to this symbol will go through the global
5029 offset table. The dynamic linker will use the .dynsym entry to
5030 determine the address it must put in the global offset table, so
5031 both the dynamic object and the regular object will refer to the
5032 same memory location for the variable.
5033
4dc4a9a5
DJ
5034 Of course, if the symbol is referenced using SDAREL relocs, we
5035 must instead allocate it in .sbss. */
25dbc73a 5036
4dc4a9a5 5037 if (ppc_elf_hash_entry (h)->has_sda_refs)
25dbc73a
AM
5038 s = htab->dynsbss;
5039 else
5040 s = htab->dynbss;
5041 BFD_ASSERT (s != NULL);
5042
5043 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
5044 copy the initial value out of the dynamic object and into the
5045 runtime process image. We need to remember the offset into the
5046 .rela.bss section we are going to use. */
5047 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5048 {
5049 asection *srel;
5050
4dc4a9a5 5051 if (ppc_elf_hash_entry (h)->has_sda_refs)
25dbc73a
AM
5052 srel = htab->relsbss;
5053 else
5054 srel = htab->relbss;
5055 BFD_ASSERT (srel != NULL);
5056 srel->size += sizeof (Elf32_External_Rela);
5057 h->needs_copy = 1;
5058 }
7619e7c7 5059
027297b7 5060 return _bfd_elf_adjust_dynamic_copy (h, s);
25dbc73a
AM
5061}
5062\f
ac39eb42
AM
5063/* Generate a symbol to mark plt call stubs. For non-PIC code the sym is
5064 xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
5065 specifying the addend on the plt relocation. For -fpic code, the sym
5066 is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
5067 xxxxxxxx.got2.plt_pic32.<callee>. */
0ba07910
AM
5068
5069static bfd_boolean
5070add_stub_sym (struct plt_entry *ent,
5071 struct elf_link_hash_entry *h,
ac39eb42 5072 struct bfd_link_info *info)
0ba07910
AM
5073{
5074 struct elf_link_hash_entry *sh;
5075 size_t len1, len2, len3;
5076 char *name;
ac39eb42
AM
5077 const char *stub;
5078 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
5079
a877a2b6 5080 if (info->shared)
ac39eb42
AM
5081 stub = ".plt_pic32.";
5082 else
5083 stub = ".plt_call32.";
0ba07910
AM
5084
5085 len1 = strlen (h->root.root.string);
ac39eb42 5086 len2 = strlen (stub);
0ba07910
AM
5087 len3 = 0;
5088 if (ent->sec)
5089 len3 = strlen (ent->sec->name);
ac39eb42 5090 name = bfd_malloc (len1 + len2 + len3 + 9);
0ba07910
AM
5091 if (name == NULL)
5092 return FALSE;
5093 sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
5094 if (ent->sec)
5095 memcpy (name + 8, ent->sec->name, len3);
ac39eb42
AM
5096 memcpy (name + 8 + len3, stub, len2);
5097 memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
0ba07910
AM
5098 sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
5099 if (sh == NULL)
5100 return FALSE;
5101 if (sh->root.type == bfd_link_hash_new)
5102 {
5103 sh->root.type = bfd_link_hash_defined;
5104 sh->root.u.def.section = htab->glink;
5105 sh->root.u.def.value = ent->glink_offset;
5106 sh->ref_regular = 1;
5107 sh->def_regular = 1;
5108 sh->ref_regular_nonweak = 1;
5109 sh->forced_local = 1;
5110 sh->non_elf = 0;
5111 }
5112 return TRUE;
5113}
5114
3b36f7e6
AM
5115/* Allocate NEED contiguous space in .got, and return the offset.
5116 Handles allocation of the got header when crossing 32k. */
5117
5118static bfd_vma
5119allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
5120{
5121 bfd_vma where;
4a3dc543 5122 unsigned int max_before_header;
3b36f7e6 5123
4a3dc543 5124 if (htab->plt_type == PLT_VXWORKS)
9d8504b1
PB
5125 {
5126 where = htab->got->size;
5127 htab->got->size += need;
5128 }
3b36f7e6
AM
5129 else
5130 {
4a3dc543
RS
5131 max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
5132 if (need <= htab->got_gap)
3b36f7e6 5133 {
4a3dc543
RS
5134 where = max_before_header - htab->got_gap;
5135 htab->got_gap -= need;
5136 }
5137 else
5138 {
5139 if (htab->got->size + need > max_before_header
5140 && htab->got->size <= max_before_header)
5141 {
5142 htab->got_gap = max_before_header - htab->got->size;
5143 htab->got->size = max_before_header + htab->got_header_size;
5144 }
5145 where = htab->got->size;
5146 htab->got->size += need;
3b36f7e6 5147 }
3b36f7e6
AM
5148 }
5149 return where;
5150}
5151
25dbc73a 5152/* Allocate space in associated reloc sections for dynamic relocs. */
252b5132 5153
b34976b6 5154static bfd_boolean
25dbc73a 5155allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
252b5132 5156{
25dbc73a
AM
5157 struct bfd_link_info *info = inf;
5158 struct ppc_elf_link_hash_entry *eh;
7619e7c7 5159 struct ppc_elf_link_hash_table *htab;
25dbc73a 5160 struct ppc_elf_dyn_relocs *p;
252b5132 5161
25dbc73a 5162 if (h->root.type == bfd_link_hash_indirect)
b34976b6 5163 return TRUE;
252b5132 5164
25dbc73a
AM
5165 if (h->root.type == bfd_link_hash_warning)
5166 /* When warning symbols are created, they **replace** the "real"
5167 entry in the hash table, thus we never get to see the real
5168 symbol in a hash traversal. So look at it now. */
5169 h = (struct elf_link_hash_entry *) h->root.u.i.link;
e47cd125 5170
7619e7c7 5171 htab = ppc_elf_hash_table (info);
e054468f
AM
5172 if (htab->elf.dynamic_sections_created
5173 || h->type == STT_GNU_IFUNC)
252b5132 5174 {
a6aa5195
AM
5175 struct plt_entry *ent;
5176 bfd_boolean doneone = FALSE;
28e99bb6 5177 bfd_vma plt_offset = 0, glink_offset = 0;
e054468f 5178 bfd_boolean dyn;
28e99bb6 5179
a6aa5195
AM
5180 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5181 if (ent->plt.refcount > 0)
5182 {
5183 /* Make sure this symbol is output as a dynamic symbol. */
5184 if (h->dynindx == -1
e054468f
AM
5185 && !h->forced_local
5186 && !h->def_regular
5187 && htab->elf.dynamic_sections_created)
a6aa5195
AM
5188 {
5189 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5190 return FALSE;
5191 }
252b5132 5192
e054468f 5193 dyn = htab->elf.dynamic_sections_created;
a6aa5195 5194 if (info->shared
e054468f
AM
5195 || h->type == STT_GNU_IFUNC
5196 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
a6aa5195
AM
5197 {
5198 asection *s = htab->plt;
25f23106 5199 if (!dyn || h->dynindx == -1)
e054468f 5200 s = htab->iplt;
7619e7c7 5201
25f23106 5202 if (htab->plt_type == PLT_NEW || !dyn || h->dynindx == -1)
a6aa5195 5203 {
a6aa5195 5204 if (!doneone)
28e99bb6
AM
5205 {
5206 plt_offset = s->size;
5207 s->size += 4;
5208 }
5209 ent->plt.offset = plt_offset;
d7128ce4 5210
a6aa5195 5211 s = htab->glink;
a877a2b6 5212 if (!doneone || info->shared)
28e99bb6
AM
5213 {
5214 glink_offset = s->size;
5215 s->size += GLINK_ENTRY_SIZE;
a7f2871e
AM
5216 if (h == htab->tls_get_addr
5217 && !htab->no_tls_get_addr_opt)
5218 s->size += TLS_GET_ADDR_GLINK_SIZE - GLINK_ENTRY_SIZE;
28e99bb6
AM
5219 }
5220 if (!doneone
5221 && !info->shared
625af618 5222 && h->def_dynamic
a6aa5195
AM
5223 && !h->def_regular)
5224 {
5225 h->root.u.def.section = s;
28e99bb6 5226 h->root.u.def.value = glink_offset;
a6aa5195 5227 }
28e99bb6 5228 ent->glink_offset = glink_offset;
0ba07910
AM
5229
5230 if (htab->emit_stub_syms
ac39eb42 5231 && !add_stub_sym (ent, h, info))
0ba07910 5232 return FALSE;
a6aa5195
AM
5233 }
5234 else
5235 {
a6aa5195
AM
5236 if (!doneone)
5237 {
28e99bb6
AM
5238 /* If this is the first .plt entry, make room
5239 for the special first entry. */
5240 if (s->size == 0)
9d8504b1 5241 s->size += htab->plt_initial_entry_size;
28e99bb6
AM
5242
5243 /* The PowerPC PLT is actually composed of two
5244 parts, the first part is 2 words (for a load
5245 and a jump), and then there is a remaining
5246 word available at the end. */
9d8504b1
PB
5247 plt_offset = (htab->plt_initial_entry_size
5248 + (htab->plt_slot_size
5249 * ((s->size
5250 - htab->plt_initial_entry_size)
5251 / htab->plt_entry_size)));
28e99bb6
AM
5252
5253 /* If this symbol is not defined in a regular
5254 file, and we are not generating a shared
de972ffa
AM
5255 library, then set the symbol to this location
5256 in the .plt. This is to avoid text
5257 relocations, and is required to make
28e99bb6
AM
5258 function pointers compare as equal between
5259 the normal executable and the shared library. */
5260 if (! info->shared
625af618 5261 && h->def_dynamic
28e99bb6
AM
5262 && !h->def_regular)
5263 {
5264 h->root.u.def.section = s;
5265 h->root.u.def.value = plt_offset;
5266 }
5267
9d8504b1
PB
5268 /* Make room for this entry. */
5269 s->size += htab->plt_entry_size;
5270 /* After the 8192nd entry, room for two entries
5271 is allocated. */
4a3dc543 5272 if (htab->plt_type == PLT_OLD
9d8504b1
PB
5273 && (s->size - htab->plt_initial_entry_size)
5274 / htab->plt_entry_size
5275 > PLT_NUM_SINGLE_ENTRIES)
5276 s->size += htab->plt_entry_size;
a6aa5195 5277 }
28e99bb6 5278 ent->plt.offset = plt_offset;
a6aa5195 5279 }
25dbc73a 5280
a6aa5195
AM
5281 /* We also need to make an entry in the .rela.plt section. */
5282 if (!doneone)
5283 {
25f23106
AM
5284 if (!htab->elf.dynamic_sections_created
5285 || h->dynindx == -1)
e054468f
AM
5286 htab->reliplt->size += sizeof (Elf32_External_Rela);
5287 else
9d8504b1 5288 {
e054468f
AM
5289 htab->relplt->size += sizeof (Elf32_External_Rela);
5290
5291 if (htab->plt_type == PLT_VXWORKS)
9d8504b1 5292 {
e054468f
AM
5293 /* Allocate space for the unloaded relocations. */
5294 if (!info->shared
5295 && htab->elf.dynamic_sections_created)
9d8504b1 5296 {
e054468f
AM
5297 if (ent->plt.offset
5298 == (bfd_vma) htab->plt_initial_entry_size)
5299 {
5300 htab->srelplt2->size
5301 += (sizeof (Elf32_External_Rela)
5302 * VXWORKS_PLTRESOLVE_RELOCS);
5303 }
5304
9d8504b1 5305 htab->srelplt2->size
e054468f
AM
5306 += (sizeof (Elf32_External_Rela)
5307 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
9d8504b1
PB
5308 }
5309
e054468f
AM
5310 /* Every PLT entry has an associated GOT entry in
5311 .got.plt. */
5312 htab->sgotplt->size += 4;
9d8504b1 5313 }
9d8504b1 5314 }
a6aa5195
AM
5315 doneone = TRUE;
5316 }
5317 }
5318 else
5319 ent->plt.offset = (bfd_vma) -1;
a6aa5195 5320 }
2ce859a1
AM
5321 else
5322 ent->plt.offset = (bfd_vma) -1;
5323
5324 if (!doneone)
5325 {
5326 h->plt.plist = NULL;
5327 h->needs_plt = 0;
5328 }
25dbc73a
AM
5329 }
5330 else
5331 {
a6aa5195 5332 h->plt.plist = NULL;
25dbc73a
AM
5333 h->needs_plt = 0;
5334 }
5335
5336 eh = (struct ppc_elf_link_hash_entry *) h;
5337 if (eh->elf.got.refcount > 0)
5338 {
d6e14abc
AM
5339 bfd_boolean dyn;
5340 unsigned int need;
5341
25dbc73a
AM
5342 /* Make sure this symbol is output as a dynamic symbol. */
5343 if (eh->elf.dynindx == -1
b099ab9f 5344 && !eh->elf.forced_local
25f23106 5345 && eh->elf.type != STT_GNU_IFUNC
b099ab9f 5346 && htab->elf.dynamic_sections_created)
25dbc73a
AM
5347 {
5348 if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
b34976b6 5349 return FALSE;
25dbc73a 5350 }
252b5132 5351
d6e14abc
AM
5352 need = 0;
5353 if ((eh->tls_mask & TLS_TLS) != 0)
25dbc73a 5354 {
d6e14abc 5355 if ((eh->tls_mask & TLS_LD) != 0)
252b5132 5356 {
d6e14abc
AM
5357 if (!eh->elf.def_dynamic)
5358 /* We'll just use htab->tlsld_got.offset. This should
5359 always be the case. It's a little odd if we have
5360 a local dynamic reloc against a non-local symbol. */
5361 htab->tlsld_got.refcount += 1;
5362 else
3b36f7e6 5363 need += 8;
252b5132 5364 }
d6e14abc
AM
5365 if ((eh->tls_mask & TLS_GD) != 0)
5366 need += 8;
5367 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
5368 need += 4;
5369 if ((eh->tls_mask & TLS_DTPREL) != 0)
3b36f7e6 5370 need += 4;
d6e14abc
AM
5371 }
5372 else
5373 need += 4;
5374 if (need == 0)
5375 eh->elf.got.offset = (bfd_vma) -1;
5376 else
5377 {
3b36f7e6 5378 eh->elf.got.offset = allocate_got (htab, need);
25dbc73a
AM
5379 dyn = htab->elf.dynamic_sections_created;
5380 if ((info->shared
5381 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
5382 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
5383 || eh->elf.root.type != bfd_link_hash_undefweak))
252b5132 5384 {
91e21fb7 5385 asection *rsec = htab->relgot;
3b36f7e6
AM
5386 /* All the entries we allocated need relocs.
5387 Except LD only needs one. */
d6e14abc
AM
5388 if ((eh->tls_mask & TLS_LD) != 0
5389 && eh->elf.def_dynamic)
3b36f7e6 5390 need -= 4;
25f23106 5391 rsec->size += need * (sizeof (Elf32_External_Rela) / 4);
252b5132 5392 }
25dbc73a
AM
5393 }
5394 }
5395 else
5396 eh->elf.got.offset = (bfd_vma) -1;
252b5132 5397
b099ab9f 5398 if (eh->dyn_relocs == NULL
de972ffa 5399 || !htab->elf.dynamic_sections_created)
25dbc73a 5400 return TRUE;
252b5132 5401
25dbc73a
AM
5402 /* In the shared -Bsymbolic case, discard space allocated for
5403 dynamic pc-relative relocs against symbols which turn out to be
5404 defined in regular objects. For the normal shared case, discard
5405 space for relocs that have become local due to symbol visibility
5406 changes. */
5407
5408 if (info->shared)
5409 {
5410 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 5411 or certain REL relocs (see must_be_dyn_reloc) that can be
25dbc73a
AM
5412 generated via assembly. We want calls to protected symbols to
5413 resolve directly to the function rather than going via the plt.
5414 If people want function pointer comparisons to work as expected
5415 then they should avoid writing weird assembly. */
5416 if (SYMBOL_CALLS_LOCAL (info, h))
5417 {
5418 struct ppc_elf_dyn_relocs **pp;
5419
5420 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
252b5132 5421 {
25dbc73a
AM
5422 p->count -= p->pc_count;
5423 p->pc_count = 0;
5424 if (p->count == 0)
5425 *pp = p->next;
5426 else
5427 pp = &p->next;
252b5132 5428 }
25dbc73a 5429 }
252b5132 5430
3348747a
NS
5431 if (htab->is_vxworks)
5432 {
5433 struct ppc_elf_dyn_relocs **pp;
5434
5435 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5436 {
5437 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
5438 *pp = p->next;
5439 else
5440 pp = &p->next;
5441 }
5442 }
5443
89200bf8
AM
5444 /* Discard relocs on undefined symbols that must be local. */
5445 if (eh->dyn_relocs != NULL
5446 && h->root.type == bfd_link_hash_undefined
5447 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
5448 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
5449 eh->dyn_relocs = NULL;
5450
25dbc73a
AM
5451 /* Also discard relocs on undefined weak syms with non-default
5452 visibility. */
cab87ef9
AM
5453 if (eh->dyn_relocs != NULL
5454 && h->root.type == bfd_link_hash_undefweak)
25dbc73a 5455 {
dfbb6ac9
AM
5456 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5457 eh->dyn_relocs = NULL;
5458
5459 /* Make sure undefined weak symbols are output as a dynamic
5460 symbol in PIEs. */
5461 else if (h->dynindx == -1
e054468f
AM
5462 && !h->forced_local
5463 && !h->def_regular)
dfbb6ac9
AM
5464 {
5465 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5466 return FALSE;
5467 }
25dbc73a
AM
5468 }
5469 }
5470 else if (ELIMINATE_COPY_RELOCS)
5471 {
5472 /* For the non-shared case, discard space for relocs against
5473 symbols which turn out to need copy relocs or are not
5474 dynamic. */
7619e7c7 5475
25dbc73a 5476 if (!h->non_got_ref
25dbc73a
AM
5477 && !h->def_regular)
5478 {
5479 /* Make sure this symbol is output as a dynamic symbol.
5480 Undefined weak syms won't yet be marked as dynamic. */
5481 if (h->dynindx == -1
25f23106 5482 && !h->forced_local)
25dbc73a
AM
5483 {
5484 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5485 return FALSE;
5486 }
7619e7c7 5487
25dbc73a
AM
5488 /* If that succeeded, we know we'll be keeping all the
5489 relocs. */
5490 if (h->dynindx != -1)
5491 goto keep;
5492 }
252b5132 5493
25dbc73a 5494 eh->dyn_relocs = NULL;
252b5132 5495
25dbc73a
AM
5496 keep: ;
5497 }
252b5132 5498
25dbc73a
AM
5499 /* Finally, allocate space. */
5500 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5501 {
5502 asection *sreloc = elf_section_data (p->sec)->sreloc;
25f23106
AM
5503 if (!htab->elf.dynamic_sections_created)
5504 sreloc = htab->reliplt;
25dbc73a
AM
5505 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5506 }
252b5132 5507
25dbc73a
AM
5508 return TRUE;
5509}
7619e7c7 5510
625af618
AM
5511/* Set DF_TEXTREL if we find any dynamic relocs that apply to
5512 read-only sections. */
7619e7c7 5513
25dbc73a 5514static bfd_boolean
625af618 5515maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
25dbc73a 5516{
25dbc73a
AM
5517 if (h->root.type == bfd_link_hash_indirect)
5518 return TRUE;
252b5132 5519
25dbc73a
AM
5520 if (h->root.type == bfd_link_hash_warning)
5521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7619e7c7 5522
625af618 5523 if (readonly_dynrelocs (h))
25dbc73a 5524 {
625af618 5525 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
ee05f2fe 5526
625af618
AM
5527 /* Not an error, just cut short the traversal. */
5528 return FALSE;
25dbc73a
AM
5529 }
5530 return TRUE;
5531}
5532
5533/* Set the sizes of the dynamic sections. */
5534
5535static bfd_boolean
5536ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5537 struct bfd_link_info *info)
5538{
5539 struct ppc_elf_link_hash_table *htab;
5540 asection *s;
5541 bfd_boolean relocs;
5542 bfd *ibfd;
7fce784e 5543
252b5132 5544#ifdef DEBUG
25dbc73a 5545 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
252b5132 5546#endif
252b5132 5547
25dbc73a
AM
5548 htab = ppc_elf_hash_table (info);
5549 BFD_ASSERT (htab->elf.dynobj != NULL);
252b5132 5550
25dbc73a
AM
5551 if (elf_hash_table (info)->dynamic_sections_created)
5552 {
5553 /* Set the contents of the .interp section to the interpreter. */
5554 if (info->executable)
5555 {
5556 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
5557 BFD_ASSERT (s != NULL);
5558 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5559 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5560 }
5561 }
252b5132 5562
4a3dc543 5563 if (htab->plt_type == PLT_OLD)
d7128ce4 5564 htab->got_header_size = 16;
4a3dc543 5565 else if (htab->plt_type == PLT_NEW)
d7128ce4 5566 htab->got_header_size = 12;
252b5132 5567
25dbc73a
AM
5568 /* Set up .got offsets for local syms, and space for local dynamic
5569 relocs. */
5570 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5571 {
5572 bfd_signed_vma *local_got;
5573 bfd_signed_vma *end_local_got;
e054468f
AM
5574 struct plt_entry **local_plt;
5575 struct plt_entry **end_local_plt;
25dbc73a
AM
5576 char *lgot_masks;
5577 bfd_size_type locsymcount;
5578 Elf_Internal_Shdr *symtab_hdr;
7fce784e 5579
0c8d6e5c 5580 if (!is_ppc_elf (ibfd))
25dbc73a 5581 continue;
7fce784e 5582
25dbc73a
AM
5583 for (s = ibfd->sections; s != NULL; s = s->next)
5584 {
5585 struct ppc_elf_dyn_relocs *p;
252b5132 5586
25dbc73a
AM
5587 for (p = ((struct ppc_elf_dyn_relocs *)
5588 elf_section_data (s)->local_dynrel);
5589 p != NULL;
5590 p = p->next)
5591 {
5592 if (!bfd_is_abs_section (p->sec)
5593 && bfd_is_abs_section (p->sec->output_section))
5594 {
5595 /* Input section has been discarded, either because
5596 it is a copy of a linkonce section or due to
5597 linker script /DISCARD/, so we'll be discarding
5598 the relocs too. */
7fce784e 5599 }
3348747a
NS
5600 else if (htab->is_vxworks
5601 && strcmp (p->sec->output_section->name,
5602 ".tls_vars") == 0)
5603 {
5604 /* Relocations in vxworks .tls_vars sections are
5605 handled specially by the loader. */
5606 }
25dbc73a 5607 else if (p->count != 0)
7fce784e 5608 {
25f23106
AM
5609 asection *sreloc = elf_section_data (p->sec)->sreloc;
5610 if (!htab->elf.dynamic_sections_created)
5611 sreloc = htab->reliplt;
5612 sreloc->size += p->count * sizeof (Elf32_External_Rela);
25dbc73a
AM
5613 if ((p->sec->output_section->flags
5614 & (SEC_READONLY | SEC_ALLOC))
5615 == (SEC_READONLY | SEC_ALLOC))
5616 info->flags |= DF_TEXTREL;
7fce784e 5617 }
252b5132 5618 }
252b5132 5619 }
252b5132 5620
25dbc73a
AM
5621 local_got = elf_local_got_refcounts (ibfd);
5622 if (!local_got)
5623 continue;
252b5132 5624
0ffa91dd 5625 symtab_hdr = &elf_symtab_hdr (ibfd);
25dbc73a
AM
5626 locsymcount = symtab_hdr->sh_info;
5627 end_local_got = local_got + locsymcount;
e054468f
AM
5628 local_plt = (struct plt_entry **) end_local_got;
5629 end_local_plt = local_plt + locsymcount;
5630 lgot_masks = (char *) end_local_plt;
91d6fa6a 5631
25dbc73a
AM
5632 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
5633 if (*local_got > 0)
5634 {
d6e14abc
AM
5635 unsigned int need = 0;
5636 if ((*lgot_masks & TLS_TLS) != 0)
25dbc73a 5637 {
d6e14abc
AM
5638 if ((*lgot_masks & TLS_GD) != 0)
5639 need += 8;
5640 if ((*lgot_masks & TLS_LD) != 0)
5641 htab->tlsld_got.refcount += 1;
5642 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
5643 need += 4;
5644 if ((*lgot_masks & TLS_DTPREL) != 0)
5645 need += 4;
25dbc73a 5646 }
d6e14abc
AM
5647 else
5648 need += 4;
5649 if (need == 0)
5650 *local_got = (bfd_vma) -1;
25dbc73a
AM
5651 else
5652 {
3b36f7e6 5653 *local_got = allocate_got (htab, need);
25dbc73a 5654 if (info->shared)
3b36f7e6
AM
5655 htab->relgot->size += (need
5656 * (sizeof (Elf32_External_Rela) / 4));
25dbc73a
AM
5657 }
5658 }
5659 else
5660 *local_got = (bfd_vma) -1;
e054468f
AM
5661
5662 if (htab->is_vxworks)
5663 continue;
5664
5665 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
5666 for (; local_plt < end_local_plt; ++local_plt)
5667 {
5668 struct plt_entry *ent;
5669 bfd_boolean doneone = FALSE;
5670 bfd_vma plt_offset = 0, glink_offset = 0;
5671
5672 for (ent = *local_plt; ent != NULL; ent = ent->next)
5673 if (ent->plt.refcount > 0)
5674 {
91d6fa6a 5675 s = htab->iplt;
e054468f
AM
5676
5677 if (!doneone)
5678 {
5679 plt_offset = s->size;
5680 s->size += 4;
5681 }
5682 ent->plt.offset = plt_offset;
5683
5684 s = htab->glink;
a877a2b6 5685 if (!doneone || info->shared)
e054468f
AM
5686 {
5687 glink_offset = s->size;
5688 s->size += GLINK_ENTRY_SIZE;
5689 }
5690 ent->glink_offset = glink_offset;
5691
5692 if (!doneone)
5693 {
25f23106 5694 htab->reliplt->size += sizeof (Elf32_External_Rela);
e054468f
AM
5695 doneone = TRUE;
5696 }
5697 }
5698 else
5699 ent->plt.offset = (bfd_vma) -1;
5700 }
25dbc73a 5701 }
252b5132 5702
b7fcf6f6
AM
5703 /* Allocate space for global sym dynamic relocs. */
5704 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
5705
3b36f7e6
AM
5706 if (htab->tlsld_got.refcount > 0)
5707 {
5708 htab->tlsld_got.offset = allocate_got (htab, 8);
5709 if (info->shared)
5710 htab->relgot->size += sizeof (Elf32_External_Rela);
5711 }
5712 else
5713 htab->tlsld_got.offset = (bfd_vma) -1;
5714
4a3dc543 5715 if (htab->got != NULL && htab->plt_type != PLT_VXWORKS)
3b36f7e6
AM
5716 {
5717 unsigned int g_o_t = 32768;
5718
4d962050
AM
5719 /* If we haven't allocated the header, do so now. When we get here,
5720 for old plt/got the got size will be 0 to 32764 (not allocated),
5721 or 32780 to 65536 (header allocated). For new plt/got, the
5722 corresponding ranges are 0 to 32768 and 32780 to 65536. */
3b36f7e6
AM
5723 if (htab->got->size <= 32768)
5724 {
5725 g_o_t = htab->got->size;
4a3dc543 5726 if (htab->plt_type == PLT_OLD)
4d962050 5727 g_o_t += 4;
3b36f7e6
AM
5728 htab->got->size += htab->got_header_size;
5729 }
3b36f7e6
AM
5730
5731 htab->elf.hgot->root.u.def.value = g_o_t;
5732 }
bd6c6e2b
AM
5733 if (info->shared)
5734 {
5735 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
5736 if (sda != NULL
5737 && !(sda->root.type == bfd_link_hash_defined
5738 || sda->root.type == bfd_link_hash_defweak))
5739 {
5740 sda->root.type = bfd_link_hash_defined;
5741 sda->root.u.def.section = htab->elf.hgot->root.u.def.section;
5742 sda->root.u.def.value = htab->elf.hgot->root.u.def.value;
5743 }
5744 }
3b36f7e6 5745
e054468f
AM
5746 if (htab->glink != NULL
5747 && htab->glink->size != 0
5748 && htab->elf.dynamic_sections_created)
d7128ce4
AM
5749 {
5750 htab->glink_pltresolve = htab->glink->size;
86b9da88
AM
5751 /* Space for the branch table. */
5752 htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
5753 /* Pad out to align the start of PLTresolve. */
5754 htab->glink->size += -htab->glink->size & 15;
d7128ce4 5755 htab->glink->size += GLINK_PLTRESOLVE;
0ba07910
AM
5756
5757 if (htab->emit_stub_syms)
5758 {
5759 struct elf_link_hash_entry *sh;
5760 sh = elf_link_hash_lookup (&htab->elf, "__glink",
5761 TRUE, FALSE, FALSE);
5762 if (sh == NULL)
5763 return FALSE;
5764 if (sh->root.type == bfd_link_hash_new)
5765 {
5766 sh->root.type = bfd_link_hash_defined;
5767 sh->root.u.def.section = htab->glink;
5768 sh->root.u.def.value = htab->glink_pltresolve;
5769 sh->ref_regular = 1;
5770 sh->def_regular = 1;
5771 sh->ref_regular_nonweak = 1;
5772 sh->forced_local = 1;
5773 sh->non_elf = 0;
5774 }
5775 sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
5776 TRUE, FALSE, FALSE);
5777 if (sh == NULL)
5778 return FALSE;
5779 if (sh->root.type == bfd_link_hash_new)
5780 {
5781 sh->root.type = bfd_link_hash_defined;
5782 sh->root.u.def.section = htab->glink;
5783 sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
5784 sh->ref_regular = 1;
5785 sh->def_regular = 1;
5786 sh->ref_regular_nonweak = 1;
5787 sh->forced_local = 1;
5788 sh->non_elf = 0;
5789 }
5790 }
d7128ce4
AM
5791 }
5792
25dbc73a
AM
5793 /* We've now determined the sizes of the various dynamic sections.
5794 Allocate memory for them. */
5795 relocs = FALSE;
5796 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
252b5132 5797 {
9d8504b1
PB
5798 bfd_boolean strip_section = TRUE;
5799
25dbc73a
AM
5800 if ((s->flags & SEC_LINKER_CREATED) == 0)
5801 continue;
252b5132 5802
25dbc73a 5803 if (s == htab->plt
e054468f 5804 || s == htab->got)
25dbc73a 5805 {
9d8504b1
PB
5806 /* We'd like to strip these sections if they aren't needed, but if
5807 we've exported dynamic symbols from them we must leave them.
5808 It's too late to tell BFD to get rid of the symbols. */
e054468f 5809 if (htab->elf.hplt != NULL)
9d8504b1 5810 strip_section = FALSE;
25dbc73a
AM
5811 /* Strip this section if we don't need it; see the
5812 comment below. */
5813 }
e054468f
AM
5814 else if (s == htab->iplt
5815 || s == htab->glink
5816 || s == htab->sgotplt
5817 || s == htab->sbss
5818 || s == htab->dynbss
5819 || s == htab->dynsbss
5820 || s == htab->sdata[0].section
c9a2f333 5821 || s == htab->sdata[1].section)
25dbc73a 5822 {
c9a2f333 5823 /* Strip these too. */
25dbc73a 5824 }
0112cd26 5825 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
25dbc73a 5826 {
c456f082 5827 if (s->size != 0)
25dbc73a
AM
5828 {
5829 /* Remember whether there are any relocation sections. */
5830 relocs = TRUE;
252b5132 5831
25dbc73a
AM
5832 /* We use the reloc_count field as a counter if we need
5833 to copy relocs into the output file. */
5834 s->reloc_count = 0;
252b5132
RH
5835 }
5836 }
25dbc73a
AM
5837 else
5838 {
5839 /* It's not one of our sections, so don't allocate space. */
5840 continue;
5841 }
252b5132 5842
9d8504b1 5843 if (s->size == 0 && strip_section)
25dbc73a 5844 {
c456f082
AM
5845 /* If we don't need this section, strip it from the
5846 output file. This is mostly to handle .rela.bss and
5847 .rela.plt. We must create both sections in
5848 create_dynamic_sections, because they must be created
5849 before the linker maps input sections to output
5850 sections. The linker does that before
5851 adjust_dynamic_symbol is called, and it is that
5852 function which decides whether anything needs to go
5853 into these sections. */
8423293d 5854 s->flags |= SEC_EXCLUDE;
25dbc73a
AM
5855 continue;
5856 }
7fce784e 5857
d7128ce4 5858 if ((s->flags & SEC_HAS_CONTENTS) == 0)
644285ef
AM
5859 continue;
5860
25dbc73a
AM
5861 /* Allocate memory for the section contents. */
5862 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
5863 if (s->contents == NULL)
5864 return FALSE;
5865 }
252b5132 5866
25dbc73a 5867 if (htab->elf.dynamic_sections_created)
7619e7c7 5868 {
25dbc73a
AM
5869 /* Add some entries to the .dynamic section. We fill in the
5870 values later, in ppc_elf_finish_dynamic_sections, but we
5871 must add the entries now so that we get the correct size for
5872 the .dynamic section. The DT_DEBUG entry is filled in by the
5873 dynamic linker and used by the debugger. */
5874#define add_dynamic_entry(TAG, VAL) \
5875 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
252b5132 5876
25dbc73a 5877 if (info->executable)
7619e7c7 5878 {
25dbc73a
AM
5879 if (!add_dynamic_entry (DT_DEBUG, 0))
5880 return FALSE;
7619e7c7
AM
5881 }
5882
25dbc73a 5883 if (htab->plt != NULL && htab->plt->size != 0)
7619e7c7 5884 {
25dbc73a
AM
5885 if (!add_dynamic_entry (DT_PLTGOT, 0)
5886 || !add_dynamic_entry (DT_PLTRELSZ, 0)
5887 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5888 || !add_dynamic_entry (DT_JMPREL, 0))
5889 return FALSE;
5890 }
7619e7c7 5891
d7128ce4
AM
5892 if (htab->glink != NULL && htab->glink->size != 0)
5893 {
1fe44d79 5894 if (!add_dynamic_entry (DT_PPC_GOT, 0))
d7128ce4 5895 return FALSE;
a7f2871e
AM
5896 if (!htab->no_tls_get_addr_opt
5897 && htab->tls_get_addr != NULL
5898 && htab->tls_get_addr->plt.plist != NULL
5899 && !add_dynamic_entry (DT_PPC_TLSOPT, 0))
5900 return FALSE;
d7128ce4
AM
5901 }
5902
25dbc73a
AM
5903 if (relocs)
5904 {
5905 if (!add_dynamic_entry (DT_RELA, 0)
5906 || !add_dynamic_entry (DT_RELASZ, 0)
5907 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
5908 return FALSE;
5909 }
7619e7c7 5910
25dbc73a
AM
5911 /* If any dynamic relocs apply to a read-only section, then we
5912 need a DT_TEXTREL entry. */
5913 if ((info->flags & DF_TEXTREL) == 0)
625af618 5914 elf_link_hash_traverse (elf_hash_table (info), maybe_set_textrel,
25dbc73a 5915 info);
7619e7c7 5916
25dbc73a
AM
5917 if ((info->flags & DF_TEXTREL) != 0)
5918 {
5919 if (!add_dynamic_entry (DT_TEXTREL, 0))
5920 return FALSE;
7619e7c7 5921 }
7a2b07ff
NS
5922 if (htab->is_vxworks
5923 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
5924 return FALSE;
5925 }
25dbc73a
AM
5926#undef add_dynamic_entry
5927
7619e7c7
AM
5928 return TRUE;
5929}
0eb4a168
AM
5930
5931/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5932
5933static bfd_boolean
5934ppc_elf_hash_symbol (struct elf_link_hash_entry *h)
5935{
5936 if (h->plt.plist != NULL
5937 && !h->def_regular
5938 && (!h->pointer_equality_needed
5939 || !h->ref_regular_nonweak))
5940 return FALSE;
5941
5942 return _bfd_elf_hash_symbol (h);
5943}
25dbc73a
AM
5944\f
5945#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
5946
01017ef8
NS
5947/* Relaxation trampolines. r12 is available for clobbering (r11, is
5948 used for some functions that are allowed to break the ABI). */
25dbc73a
AM
5949static const int shared_stub_entry[] =
5950 {
5951 0x7c0802a6, /* mflr 0 */
5952 0x429f0005, /* bcl 20, 31, .Lxxx */
01017ef8
NS
5953 0x7d8802a6, /* mflr 12 */
5954 0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
5955 0x398c0008, /* addi 12, 12, (xxx-.Lxxx)@l */
25dbc73a 5956 0x7c0803a6, /* mtlr 0 */
01017ef8 5957 0x7d8903a6, /* mtctr 12 */
25dbc73a
AM
5958 0x4e800420, /* bctr */
5959 };
5960
5961static const int stub_entry[] =
5962 {
01017ef8
NS
5963 0x3d800000, /* lis 12,xxx@ha */
5964 0x398c0000, /* addi 12,12,xxx@l */
5965 0x7d8903a6, /* mtctr 12 */
25dbc73a
AM
5966 0x4e800420, /* bctr */
5967 };
5968
5969static bfd_boolean
5970ppc_elf_relax_section (bfd *abfd,
5971 asection *isec,
5972 struct bfd_link_info *link_info,
5973 bfd_boolean *again)
5974{
5975 struct one_fixup
5976 {
5977 struct one_fixup *next;
5978 asection *tsec;
01017ef8
NS
5979 /* Final link, can use the symbol offset. For a
5980 relocatable link we use the symbol's index. */
25dbc73a
AM
5981 bfd_vma toff;
5982 bfd_vma trampoff;
5983 };
7619e7c7 5984
25dbc73a
AM
5985 Elf_Internal_Shdr *symtab_hdr;
5986 bfd_byte *contents = NULL;
5987 Elf_Internal_Sym *isymbuf = NULL;
5988 Elf_Internal_Rela *internal_relocs = NULL;
5989 Elf_Internal_Rela *irel, *irelend;
5990 struct one_fixup *fixups = NULL;
9bc4e62b 5991 unsigned changes = 0;
d7128ce4 5992 struct ppc_elf_link_hash_table *htab;
25dbc73a 5993 bfd_size_type trampoff;
a6aa5195 5994 asection *got2;
7619e7c7 5995
25dbc73a 5996 *again = FALSE;
7619e7c7 5997
c87b5a93 5998 /* Nothing to do if there are no relocations, and no need to do
a8ad78a7 5999 anything with non-alloc or non-code sections. */
c87b5a93 6000 if ((isec->flags & SEC_ALLOC) == 0
a8ad78a7 6001 || (isec->flags & SEC_CODE) == 0
c87b5a93
AM
6002 || (isec->flags & SEC_RELOC) == 0
6003 || isec->reloc_count == 0)
25dbc73a 6004 return TRUE;
7619e7c7 6005
c8a1f254
NS
6006 /* We cannot represent the required PIC relocs in the output, so don't
6007 do anything. The linker doesn't support mixing -shared and -r
6008 anyway. */
6009 if (link_info->relocatable && link_info->shared)
6010 return TRUE;
6011
25dbc73a
AM
6012 trampoff = (isec->size + 3) & (bfd_vma) -4;
6013 /* Space for a branch around any trampolines. */
6014 trampoff += 4;
7619e7c7 6015
0ffa91dd 6016 symtab_hdr = &elf_symtab_hdr (abfd);
7619e7c7 6017
25dbc73a
AM
6018 /* Get a copy of the native relocations. */
6019 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
6020 link_info->keep_memory);
6021 if (internal_relocs == NULL)
6022 goto error_return;
7619e7c7 6023
d7128ce4 6024 htab = ppc_elf_hash_table (link_info);
a6aa5195 6025 got2 = bfd_get_section_by_name (abfd, ".got2");
7619e7c7 6026
a6aa5195 6027 irelend = internal_relocs + isec->reloc_count;
25dbc73a 6028 for (irel = internal_relocs; irel < irelend; irel++)
7619e7c7 6029 {
25dbc73a 6030 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
91d6fa6a 6031 bfd_vma toff, roff;
25dbc73a
AM
6032 asection *tsec;
6033 struct one_fixup *f;
6034 size_t insn_offset = 0;
6035 bfd_vma max_branch_offset, val;
6036 bfd_byte *hit_addr;
6037 unsigned long t0;
de972ffa 6038 struct elf_link_hash_entry *h;
a9585d22 6039 struct plt_entry **plist;
25dbc73a 6040 unsigned char sym_type;
7619e7c7 6041
25dbc73a
AM
6042 switch (r_type)
6043 {
6044 case R_PPC_REL24:
6045 case R_PPC_LOCAL24PC:
6046 case R_PPC_PLTREL24:
6047 max_branch_offset = 1 << 25;
6048 break;
7619e7c7 6049
25dbc73a
AM
6050 case R_PPC_REL14:
6051 case R_PPC_REL14_BRTAKEN:
6052 case R_PPC_REL14_BRNTAKEN:
6053 max_branch_offset = 1 << 15;
6054 break;
7619e7c7 6055
25dbc73a
AM
6056 default:
6057 continue;
6058 }
7619e7c7 6059
25dbc73a 6060 /* Get the value of the symbol referred to by the reloc. */
de972ffa 6061 h = NULL;
25dbc73a
AM
6062 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6063 {
6064 /* A local symbol. */
6065 Elf_Internal_Sym *isym;
7fce784e 6066
25dbc73a
AM
6067 /* Read this BFD's local symbols. */
6068 if (isymbuf == NULL)
6069 {
6070 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6071 if (isymbuf == NULL)
6072 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6073 symtab_hdr->sh_info, 0,
6074 NULL, NULL, NULL);
6075 if (isymbuf == 0)
6076 goto error_return;
6077 }
6078 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6079 if (isym->st_shndx == SHN_UNDEF)
c8a1f254 6080 tsec = bfd_und_section_ptr;
25dbc73a
AM
6081 else if (isym->st_shndx == SHN_ABS)
6082 tsec = bfd_abs_section_ptr;
6083 else if (isym->st_shndx == SHN_COMMON)
6084 tsec = bfd_com_section_ptr;
6085 else
6086 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7fce784e 6087
25dbc73a
AM
6088 toff = isym->st_value;
6089 sym_type = ELF_ST_TYPE (isym->st_info);
6090 }
6091 else
6092 {
6093 /* Global symbol handling. */
6094 unsigned long indx;
7619e7c7 6095
25dbc73a
AM
6096 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6097 h = elf_sym_hashes (abfd)[indx];
7619e7c7 6098
25dbc73a
AM
6099 while (h->root.type == bfd_link_hash_indirect
6100 || h->root.type == bfd_link_hash_warning)
6101 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7619e7c7 6102
a9585d22
AM
6103 if (h->root.type == bfd_link_hash_defined
6104 || h->root.type == bfd_link_hash_defweak)
25dbc73a
AM
6105 {
6106 tsec = h->root.u.def.section;
6107 toff = h->root.u.def.value;
6108 }
c8a1f254
NS
6109 else if (h->root.type == bfd_link_hash_undefined
6110 || h->root.type == bfd_link_hash_undefweak)
6111 {
6112 tsec = bfd_und_section_ptr;
01017ef8 6113 toff = link_info->relocatable ? indx : 0;
c8a1f254 6114 }
25dbc73a
AM
6115 else
6116 continue;
7619e7c7 6117
25dbc73a
AM
6118 sym_type = h->type;
6119 }
7619e7c7 6120
a9585d22
AM
6121 /* The condition here under which we call find_plt_ent must
6122 match that in relocate_section. If we call find_plt_ent here
6123 but not in relocate_section, or vice versa, then the branch
6124 destination used here may be incorrect. */
6125 plist = NULL;
6126 if (h != NULL)
de972ffa 6127 {
a9585d22
AM
6128 /* We know is_branch_reloc (r_type) is true. */
6129 if (h->type == STT_GNU_IFUNC
6130 || r_type == R_PPC_PLTREL24)
de972ffa 6131 plist = &h->plt.plist;
a9585d22
AM
6132 }
6133 else if (sym_type == STT_GNU_IFUNC
6134 && elf_local_got_offsets (abfd) != NULL)
6135 {
6136 bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
6137 struct plt_entry **local_plt = (struct plt_entry **)
6138 (local_got_offsets + symtab_hdr->sh_info);
6139 plist = local_plt + ELF32_R_SYM (irel->r_info);
6140 }
6141 if (plist != NULL)
6142 {
6143 bfd_vma addend = 0;
6144 struct plt_entry *ent;
de972ffa 6145
a9585d22
AM
6146 if (r_type == R_PPC_PLTREL24 && link_info->shared)
6147 addend = irel->r_addend;
6148 ent = find_plt_ent (plist, got2, addend);
6149 if (ent != NULL)
6150 {
6151 if (htab->plt_type == PLT_NEW
6152 || h == NULL
6153 || !htab->elf.dynamic_sections_created
6154 || h->dynindx == -1)
de972ffa 6155 {
a9585d22
AM
6156 tsec = htab->glink;
6157 toff = ent->glink_offset;
6158 }
6159 else
6160 {
6161 tsec = htab->plt;
6162 toff = ent->plt.offset;
de972ffa
AM
6163 }
6164 }
6165 }
6166
25dbc73a
AM
6167 /* If the branch and target are in the same section, you have
6168 no hope of adding stubs. We'll error out later should the
6169 branch overflow. */
6170 if (tsec == isec)
6171 continue;
6172
6173 /* There probably isn't any reason to handle symbols in
6174 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
6175 attribute for a code section, and we are only looking at
6176 branches. However, implement it correctly here as a
6177 reference for other target relax_section functions. */
6178 if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
6179 {
6180 /* At this stage in linking, no SEC_MERGE symbol has been
6181 adjusted, so all references to such symbols need to be
6182 passed through _bfd_merged_section_offset. (Later, in
6183 relocate_section, all SEC_MERGE symbols *except* for
6184 section symbols have been adjusted.)
6185
6186 gas may reduce relocations against symbols in SEC_MERGE
6187 sections to a relocation against the section symbol when
6188 the original addend was zero. When the reloc is against
6189 a section symbol we should include the addend in the
6190 offset passed to _bfd_merged_section_offset, since the
6191 location of interest is the original symbol. On the
6192 other hand, an access to "sym+addend" where "sym" is not
6193 a section symbol should not include the addend; Such an
6194 access is presumed to be an offset from "sym"; The
6195 location of interest is just "sym". */
6196 if (sym_type == STT_SECTION)
6197 toff += irel->r_addend;
7619e7c7 6198
25dbc73a
AM
6199 toff = _bfd_merged_section_offset (abfd, &tsec,
6200 elf_section_data (tsec)->sec_info,
6201 toff);
7619e7c7 6202
25dbc73a
AM
6203 if (sym_type != STT_SECTION)
6204 toff += irel->r_addend;
6205 }
a6aa5195
AM
6206 /* PLTREL24 addends are special. */
6207 else if (r_type != R_PPC_PLTREL24)
25dbc73a 6208 toff += irel->r_addend;
7619e7c7 6209
7de713b9
AM
6210 /* Attempted -shared link of non-pic code loses. */
6211 if (tsec->output_section == NULL)
6212 continue;
6213
25dbc73a 6214 roff = irel->r_offset;
7619e7c7 6215
25dbc73a 6216 /* If the branch is in range, no need to do anything. */
c8a1f254
NS
6217 if (tsec != bfd_und_section_ptr
6218 && (!link_info->relocatable
6219 /* A relocatable link may have sections moved during
6220 final link, so do not presume they remain in range. */
01017ef8
NS
6221 || tsec->output_section == isec->output_section))
6222 {
6223 bfd_vma symaddr, reladdr;
6224
6225 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
6226 reladdr = isec->output_section->vma + isec->output_offset + roff;
6227 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
6228 continue;
6229 }
252b5132 6230
25dbc73a
AM
6231 /* Look for an existing fixup to this address. */
6232 for (f = fixups; f ; f = f->next)
6233 if (f->tsec == tsec && f->toff == toff)
6234 break;
6235
6236 if (f == NULL)
7619e7c7 6237 {
25dbc73a
AM
6238 size_t size;
6239 unsigned long stub_rtype;
252b5132 6240
25dbc73a
AM
6241 val = trampoff - roff;
6242 if (val >= max_branch_offset)
6243 /* Oh dear, we can't reach a trampoline. Don't try to add
6244 one. We'll report an error later. */
6245 continue;
252b5132 6246
25dbc73a
AM
6247 if (link_info->shared)
6248 {
6249 size = 4 * ARRAY_SIZE (shared_stub_entry);
6250 insn_offset = 12;
25dbc73a
AM
6251 }
6252 else
6253 {
6254 size = 4 * ARRAY_SIZE (stub_entry);
6255 insn_offset = 0;
25dbc73a 6256 }
32af9f6e 6257 stub_rtype = R_PPC_RELAX;
d7128ce4
AM
6258 if (tsec == htab->plt
6259 || tsec == htab->glink)
32af9f6e
AM
6260 {
6261 stub_rtype = R_PPC_RELAX_PLT;
6262 if (r_type == R_PPC_PLTREL24)
6263 stub_rtype = R_PPC_RELAX_PLTREL24;
6264 }
252b5132 6265
25dbc73a
AM
6266 /* Hijack the old relocation. Since we need two
6267 relocations for this use a "composite" reloc. */
6268 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
6269 stub_rtype);
6270 irel->r_offset = trampoff + insn_offset;
32af9f6e
AM
6271 if (r_type == R_PPC_PLTREL24
6272 && stub_rtype != R_PPC_RELAX_PLTREL24)
e7d92e2e 6273 irel->r_addend = 0;
252b5132 6274
25dbc73a
AM
6275 /* Record the fixup so we don't do it again this section. */
6276 f = bfd_malloc (sizeof (*f));
6277 f->next = fixups;
6278 f->tsec = tsec;
6279 f->toff = toff;
6280 f->trampoff = trampoff;
6281 fixups = f;
252b5132 6282
25dbc73a 6283 trampoff += size;
9bc4e62b 6284 changes++;
25dbc73a
AM
6285 }
6286 else
6287 {
6288 val = f->trampoff - roff;
6289 if (val >= max_branch_offset)
6290 continue;
252b5132 6291
25dbc73a
AM
6292 /* Nop out the reloc, since we're finalizing things here. */
6293 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6294 }
252b5132 6295
25dbc73a
AM
6296 /* Get the section contents. */
6297 if (contents == NULL)
6298 {
6299 /* Get cached copy if it exists. */
6300 if (elf_section_data (isec)->this_hdr.contents != NULL)
6301 contents = elf_section_data (isec)->this_hdr.contents;
6302 else
6303 {
6304 /* Go get them off disk. */
6305 if (!bfd_malloc_and_get_section (abfd, isec, &contents))
6306 goto error_return;
6307 }
6308 }
252b5132 6309
25dbc73a
AM
6310 /* Fix up the existing branch to hit the trampoline. */
6311 hit_addr = contents + roff;
6312 switch (r_type)
6313 {
6314 case R_PPC_REL24:
6315 case R_PPC_LOCAL24PC:
6316 case R_PPC_PLTREL24:
6317 t0 = bfd_get_32 (abfd, hit_addr);
6318 t0 &= ~0x3fffffc;
6319 t0 |= val & 0x3fffffc;
6320 bfd_put_32 (abfd, t0, hit_addr);
6321 break;
252b5132 6322
25dbc73a
AM
6323 case R_PPC_REL14:
6324 case R_PPC_REL14_BRTAKEN:
6325 case R_PPC_REL14_BRNTAKEN:
6326 t0 = bfd_get_32 (abfd, hit_addr);
6327 t0 &= ~0xfffc;
6328 t0 |= val & 0xfffc;
6329 bfd_put_32 (abfd, t0, hit_addr);
6330 break;
6331 }
6332 }
252b5132 6333
25dbc73a 6334 /* Write out the trampolines. */
25dbc73a
AM
6335 if (fixups != NULL)
6336 {
6337 const int *stub;
6338 bfd_byte *dest;
6339 bfd_vma val;
6340 int i, size;
252b5132 6341
25dbc73a
AM
6342 do
6343 {
6344 struct one_fixup *f = fixups;
6345 fixups = fixups->next;
6346 free (f);
6347 }
6348 while (fixups);
252b5132 6349
515ef31d 6350 contents = bfd_realloc_or_free (contents, trampoff);
25dbc73a
AM
6351 if (contents == NULL)
6352 goto error_return;
252b5132 6353
25dbc73a
AM
6354 isec->size = (isec->size + 3) & (bfd_vma) -4;
6355 /* Branch around the trampolines. */
e7d92e2e 6356 val = B + trampoff - isec->size;
25dbc73a
AM
6357 dest = contents + isec->size;
6358 isec->size = trampoff;
6359 bfd_put_32 (abfd, val, dest);
6360 dest += 4;
252b5132 6361
25dbc73a
AM
6362 if (link_info->shared)
6363 {
6364 stub = shared_stub_entry;
6365 size = ARRAY_SIZE (shared_stub_entry);
6366 }
6367 else
6368 {
6369 stub = stub_entry;
6370 size = ARRAY_SIZE (stub_entry);
6371 }
252b5132 6372
25dbc73a
AM
6373 i = 0;
6374 while (dest < contents + trampoff)
252b5132 6375 {
25dbc73a
AM
6376 bfd_put_32 (abfd, stub[i], dest);
6377 i++;
6378 if (i == size)
6379 i = 0;
6380 dest += 4;
252b5132 6381 }
25dbc73a 6382 BFD_ASSERT (i == 0);
252b5132
RH
6383 }
6384
25dbc73a
AM
6385 if (isymbuf != NULL
6386 && symtab_hdr->contents != (unsigned char *) isymbuf)
252b5132 6387 {
25dbc73a
AM
6388 if (! link_info->keep_memory)
6389 free (isymbuf);
252b5132 6390 else
25dbc73a
AM
6391 {
6392 /* Cache the symbols for elf_link_input_bfd. */
6393 symtab_hdr->contents = (unsigned char *) isymbuf;
6394 }
252b5132
RH
6395 }
6396
25dbc73a
AM
6397 if (contents != NULL
6398 && elf_section_data (isec)->this_hdr.contents != contents)
6399 {
9bc4e62b 6400 if (!changes && !link_info->keep_memory)
25dbc73a
AM
6401 free (contents);
6402 else
6403 {
6404 /* Cache the section contents for elf_link_input_bfd. */
6405 elf_section_data (isec)->this_hdr.contents = contents;
6406 }
6407 }
252b5132 6408
9bc4e62b 6409 if (changes != 0)
25dbc73a 6410 {
9bc4e62b
NS
6411 /* Append sufficient NOP relocs so we can write out relocation
6412 information for the trampolines. */
6413 Elf_Internal_Rela *new_relocs = bfd_malloc ((changes + isec->reloc_count)
6414 * sizeof (*new_relocs));
6415 unsigned ix;
6416
6417 if (!new_relocs)
6418 goto error_return;
6419 memcpy (new_relocs, internal_relocs,
6420 isec->reloc_count * sizeof (*new_relocs));
6421 for (ix = changes; ix--;)
6422 {
6423 irel = new_relocs + ix + isec->reloc_count;
6424
6425 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6426 }
6427 if (internal_relocs != elf_section_data (isec)->relocs)
25dbc73a 6428 free (internal_relocs);
9bc4e62b
NS
6429 elf_section_data (isec)->relocs = new_relocs;
6430 isec->reloc_count += changes;
6431 elf_section_data (isec)->rel_hdr.sh_size
6432 += changes * elf_section_data (isec)->rel_hdr.sh_entsize;
25dbc73a 6433 }
9bc4e62b
NS
6434 else if (elf_section_data (isec)->relocs != internal_relocs)
6435 free (internal_relocs);
252b5132 6436
9bc4e62b 6437 *again = changes != 0;
c8a1f254
NS
6438 if (!*again && link_info->relocatable)
6439 {
6440 /* Convert the internal relax relocs to external form. */
6441 for (irel = internal_relocs; irel < irelend; irel++)
32af9f6e 6442 if (ELF32_R_TYPE (irel->r_info) == R_PPC_RELAX)
c8a1f254
NS
6443 {
6444 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
6445
6446 /* Rewrite the reloc and convert one of the trailing nop
6447 relocs to describe this relocation. */
6448 BFD_ASSERT (ELF32_R_TYPE (irelend[-1].r_info) == R_PPC_NONE);
6449 /* The relocs are at the bottom 2 bytes */
6450 irel[0].r_offset += 2;
6451 memmove (irel + 1, irel, (irelend - irel - 1) * sizeof (*irel));
6452 irel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
6453 irel[1].r_offset += 4;
6454 irel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
6455 irel++;
6456 }
6457 }
6458
b34976b6 6459 return TRUE;
25dbc73a
AM
6460
6461 error_return:
6462 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
6463 free (isymbuf);
6464 if (contents != NULL
6465 && elf_section_data (isec)->this_hdr.contents != contents)
6466 free (contents);
6467 if (internal_relocs != NULL
6468 && elf_section_data (isec)->relocs != internal_relocs)
6469 free (internal_relocs);
6470 return FALSE;
252b5132 6471}
252b5132 6472\f
8a696751
AM
6473/* What to do when ld finds relocations against symbols defined in
6474 discarded sections. */
6475
6476static unsigned int
6477ppc_elf_action_discarded (asection *sec)
6478{
6479 if (strcmp (".fixup", sec->name) == 0)
6480 return 0;
6481
6482 if (strcmp (".got2", sec->name) == 0)
6483 return 0;
6484
6485 return _bfd_elf_default_action_discarded (sec);
6486}
c9a2f333 6487\f
25dbc73a 6488/* Fill in the address for a pointer generated in a linker section. */
252b5132 6489
25dbc73a 6490static bfd_vma
76750a2f 6491elf_finish_pointer_linker_section (bfd *input_bfd,
25dbc73a
AM
6492 elf_linker_section_t *lsect,
6493 struct elf_link_hash_entry *h,
6494 bfd_vma relocation,
2bb04cf2 6495 const Elf_Internal_Rela *rel)
25dbc73a
AM
6496{
6497 elf_linker_section_pointers_t *linker_section_ptr;
252b5132 6498
25dbc73a 6499 BFD_ASSERT (lsect != NULL);
252b5132 6500
25dbc73a 6501 if (h != NULL)
252b5132 6502 {
25dbc73a
AM
6503 /* Handle global symbol. */
6504 struct ppc_elf_link_hash_entry *eh;
252b5132 6505
25dbc73a 6506 eh = (struct ppc_elf_link_hash_entry *) h;
76750a2f
AM
6507 BFD_ASSERT (eh->elf.def_regular);
6508 linker_section_ptr = eh->linker_section_pointer;
25dbc73a
AM
6509 }
6510 else
6511 {
6512 /* Handle local symbol. */
6513 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
76750a2f 6514
0c8d6e5c 6515 BFD_ASSERT (is_ppc_elf (input_bfd));
25dbc73a 6516 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
76750a2f
AM
6517 linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
6518 }
252b5132 6519
76750a2f
AM
6520 linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
6521 rel->r_addend,
6522 lsect);
6523 BFD_ASSERT (linker_section_ptr != NULL);
25dbc73a 6524
76750a2f
AM
6525 /* Offset will always be a multiple of four, so use the bottom bit
6526 as a "written" flag. */
6527 if ((linker_section_ptr->offset & 1) == 0)
6528 {
6529 bfd_put_32 (lsect->section->owner,
6530 relocation + linker_section_ptr->addend,
6531 lsect->section->contents + linker_section_ptr->offset);
6532 linker_section_ptr->offset += 1;
252b5132
RH
6533 }
6534
bd6c6e2b
AM
6535 relocation = (lsect->section->output_section->vma
6536 + lsect->section->output_offset
76750a2f 6537 + linker_section_ptr->offset - 1
bd6c6e2b 6538 - SYM_VAL (lsect->sym));
252b5132 6539
25dbc73a
AM
6540#ifdef DEBUG
6541 fprintf (stderr,
6542 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6543 lsect->name, (long) relocation, (long) relocation);
6544#endif
252b5132 6545
bd6c6e2b 6546 return relocation;
252b5132 6547}
25dbc73a 6548
e054468f
AM
6549#define PPC_LO(v) ((v) & 0xffff)
6550#define PPC_HI(v) (((v) >> 16) & 0xffff)
6551#define PPC_HA(v) PPC_HI ((v) + 0x8000)
6552
6553static void
a7f2871e 6554write_glink_stub (struct plt_entry *ent, asection *plt_sec, unsigned char *p,
e054468f
AM
6555 struct bfd_link_info *info)
6556{
6557 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
6558 bfd *output_bfd = info->output_bfd;
6559 bfd_vma plt;
e054468f
AM
6560
6561 plt = ((ent->plt.offset & ~1)
6562 + plt_sec->output_section->vma
6563 + plt_sec->output_offset);
e054468f 6564
a877a2b6 6565 if (info->shared)
e054468f
AM
6566 {
6567 bfd_vma got = 0;
6568
6569 if (ent->addend >= 32768)
6570 got = (ent->addend
6571 + ent->sec->output_section->vma
6572 + ent->sec->output_offset);
6573 else if (htab->elf.hgot != NULL)
6574 got = SYM_VAL (htab->elf.hgot);
6575
6576 plt -= got;
6577
6578 if (plt + 0x8000 < 0x10000)
6579 {
6580 bfd_put_32 (output_bfd, LWZ_11_30 + 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 bfd_put_32 (output_bfd, NOP, p);
6587 p += 4;
6588 }
6589 else
6590 {
6591 bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
6592 p += 4;
6593 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6594 p += 4;
6595 bfd_put_32 (output_bfd, MTCTR_11, p);
6596 p += 4;
6597 bfd_put_32 (output_bfd, BCTR, p);
6598 p += 4;
6599 }
6600 }
6601 else
6602 {
6603 bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
6604 p += 4;
6605 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6606 p += 4;
6607 bfd_put_32 (output_bfd, MTCTR_11, p);
6608 p += 4;
6609 bfd_put_32 (output_bfd, BCTR, p);
6610 p += 4;
6611 }
6612}
6613
bd6c6e2b
AM
6614/* Return true if symbol is defined statically. */
6615
6616static bfd_boolean
6617is_static_defined (struct elf_link_hash_entry *h)
6618{
6619 return ((h->root.type == bfd_link_hash_defined
6620 || h->root.type == bfd_link_hash_defweak)
6621 && h->root.u.def.section != NULL
6622 && h->root.u.def.section->output_section != NULL);
6623}
6624
2d0f3896
AM
6625/* If INSN is an opcode that may be used with an @tls operand, return
6626 the transformed insn for TLS optimisation, otherwise return 0. If
6627 REG is non-zero only match an insn with RB or RA equal to REG. */
6628
6629unsigned int
6630_bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
6631{
6632 unsigned int rtra;
6633
6634 if ((insn & (0x3f << 26)) != 31 << 26)
6635 return 0;
6636
6637 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
6638 rtra = insn & ((1 << 26) - (1 << 16));
6639 else if (((insn >> 16) & 0x1f) == reg)
6640 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
6641 else
6642 return 0;
6643
6644 if ((insn & (0x3ff << 1)) == 266 << 1)
6645 /* add -> addi. */
6646 insn = 14 << 26;
6647 else if ((insn & (0x1f << 1)) == 23 << 1
6648 && ((insn & (0x1f << 6)) < 14 << 6
6649 || ((insn & (0x1f << 6)) >= 16 << 6
6650 && (insn & (0x1f << 6)) < 24 << 6)))
6651 /* load and store indexed -> dform. */
6652 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
6653 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
6654 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
6655 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
6656 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
6657 /* lwax -> lwa. */
6658 insn = (58 << 26) | 2;
6659 else
6660 return 0;
6661 insn |= rtra;
6662 return insn;
6663}
6664
766bc656
AM
6665/* If INSN is an opcode that may be used with an @tprel operand, return
6666 the transformed insn for an undefined weak symbol, ie. with the
6667 thread pointer REG operand removed. Otherwise return 0. */
6668
6669unsigned int
6670_bfd_elf_ppc_at_tprel_transform (unsigned int insn, unsigned int reg)
6671{
6672 if ((insn & (0x1f << 16)) == reg << 16
6673 && ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6674 || (insn & (0x3f << 26)) == 15u << 26 /* addis */
6675 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6676 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6677 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6678 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6679 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6680 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6681 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6682 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6683 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6684 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6685 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6686 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6687 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6688 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6689 && (insn & 3) != 1)
6690 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6691 && ((insn & 3) == 0 || (insn & 3) == 3))))
6692 {
6693 insn &= ~(0x1f << 16);
6694 }
6695 else if ((insn & (0x1f << 21)) == reg << 21
6696 && ((insn & (0x3e << 26)) == 24u << 26 /* ori, oris */
6697 || (insn & (0x3e << 26)) == 26u << 26 /* xori,xoris */
6698 || (insn & (0x3e << 26)) == 28u << 26 /* andi,andis */))
6699 {
6700 insn &= ~(0x1f << 21);
6701 insn |= (insn & (0x1f << 16)) << 5;
6702 if ((insn & (0x3e << 26)) == 26 << 26 /* xori,xoris */)
6703 insn -= 2 >> 26; /* convert to ori,oris */
6704 }
6705 else
6706 insn = 0;
6707 return insn;
6708}
6709
252b5132
RH
6710/* The RELOCATE_SECTION function is called by the ELF backend linker
6711 to handle the relocations for a section.
6712
6713 The relocs are always passed as Rela structures; if the section
6714 actually uses Rel structures, the r_addend field will always be
6715 zero.
6716
6717 This function is responsible for adjust the section contents as
6718 necessary, and (if using Rela relocs and generating a
1049f94e 6719 relocatable output file) adjusting the reloc addend as
252b5132
RH
6720 necessary.
6721
6722 This function does not have to worry about setting the reloc
6723 address or the reloc symbol index.
6724
6725 LOCAL_SYMS is a pointer to the swapped in local symbols.
6726
6727 LOCAL_SECTIONS is an array giving the section in the input file
6728 corresponding to the st_shndx field of each local symbol.
6729
6730 The global hash table entry for the global symbols can be found
6731 via elf_sym_hashes (input_bfd).
6732
1049f94e 6733 When generating relocatable output, this function must handle
252b5132
RH
6734 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
6735 going to be the section symbol corresponding to the output
6736 section, which means that the addend must be adjusted
6737 accordingly. */
6738
b34976b6 6739static bfd_boolean
55fd94b0
AM
6740ppc_elf_relocate_section (bfd *output_bfd,
6741 struct bfd_link_info *info,
6742 bfd *input_bfd,
6743 asection *input_section,
6744 bfd_byte *contents,
6745 Elf_Internal_Rela *relocs,
6746 Elf_Internal_Sym *local_syms,
6747 asection **local_sections)
252b5132 6748{
7619e7c7
AM
6749 Elf_Internal_Shdr *symtab_hdr;
6750 struct elf_link_hash_entry **sym_hashes;
6751 struct ppc_elf_link_hash_table *htab;
6752 Elf_Internal_Rela *rel;
6753 Elf_Internal_Rela *relend;
6754 Elf_Internal_Rela outrel;
a6aa5195 6755 asection *got2, *sreloc = NULL;
252b5132 6756 bfd_vma *local_got_offsets;
b34976b6 6757 bfd_boolean ret = TRUE;
4fe5ca5b 6758 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
3348747a 6759 bfd_boolean is_vxworks_tls;
b34976b6 6760
252b5132 6761#ifdef DEBUG
d003868e
AM
6762 _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
6763 "%ld relocations%s",
6764 input_bfd, input_section,
6765 (long) input_section->reloc_count,
6766 (info->relocatable) ? " (relocatable)" : "");
252b5132
RH
6767#endif
6768
a6aa5195
AM
6769 got2 = bfd_get_section_by_name (input_bfd, ".got2");
6770
e47cd125 6771 /* Initialize howto table if not already done. */
8da6118f 6772 if (!ppc_elf_howto_table[R_PPC_ADDR32])
252b5132
RH
6773 ppc_elf_howto_init ();
6774
7619e7c7 6775 htab = ppc_elf_hash_table (info);
252b5132 6776 local_got_offsets = elf_local_got_offsets (input_bfd);
0ffa91dd 6777 symtab_hdr = &elf_symtab_hdr (input_bfd);
7619e7c7 6778 sym_hashes = elf_sym_hashes (input_bfd);
3348747a
NS
6779 /* We have to handle relocations in vxworks .tls_vars sections
6780 specially, because the dynamic loader is 'weird'. */
6781 is_vxworks_tls = (htab->is_vxworks && info->shared
6782 && !strcmp (input_section->output_section->name,
6783 ".tls_vars"));
7619e7c7
AM
6784 rel = relocs;
6785 relend = relocs + input_section->reloc_count;
252b5132
RH
6786 for (; rel < relend; rel++)
6787 {
7619e7c7
AM
6788 enum elf_ppc_reloc_type r_type;
6789 bfd_vma addend;
6790 bfd_reloc_status_type r;
6791 Elf_Internal_Sym *sym;
6792 asection *sec;
6793 struct elf_link_hash_entry *h;
6794 const char *sym_name;
252b5132
RH
6795 reloc_howto_type *howto;
6796 unsigned long r_symndx;
6797 bfd_vma relocation;
91d6fa6a 6798 bfd_vma branch_bit, from;
7619e7c7
AM
6799 bfd_boolean unresolved_reloc;
6800 bfd_boolean warned;
6801 unsigned int tls_type, tls_mask, tls_gd;
e054468f 6802 struct plt_entry **ifunc;
7619e7c7 6803
55fd94b0
AM
6804 r_type = ELF32_R_TYPE (rel->r_info);
6805 sym = NULL;
6806 sec = NULL;
6807 h = NULL;
7619e7c7
AM
6808 unresolved_reloc = FALSE;
6809 warned = FALSE;
252b5132 6810 r_symndx = ELF32_R_SYM (rel->r_info);
560e09e9 6811
252b5132
RH
6812 if (r_symndx < symtab_hdr->sh_info)
6813 {
6814 sym = local_syms + r_symndx;
6815 sec = local_sections[r_symndx];
26c61ae5 6816 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
252b5132 6817
8517fae7 6818 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
252b5132
RH
6819 }
6820 else
6821 {
b2a8e766
AM
6822 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
6823 r_symndx, symtab_hdr, sym_hashes,
6824 h, sec, relocation,
6825 unresolved_reloc, warned);
9abc968f 6826
252b5132 6827 sym_name = h->root.root.string;
7619e7c7
AM
6828 }
6829
ab96bf03
AM
6830 if (sec != NULL && elf_discarded_section (sec))
6831 {
6832 /* For relocs against symbols from removed linkonce sections,
6833 or sections discarded by a linker script, we just want the
6834 section contents zeroed. Avoid any special processing. */
6835 howto = NULL;
6836 if (r_type < R_PPC_max)
6837 howto = ppc_elf_howto_table[r_type];
6838 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
6839 rel->r_info = 0;
6840 rel->r_addend = 0;
6841 continue;
6842 }
6843
6844 if (info->relocatable)
6845 {
6846 if (got2 != NULL
6847 && r_type == R_PPC_PLTREL24
6848 && rel->r_addend >= 32768)
6849 {
6850 /* R_PPC_PLTREL24 is rather special. If non-zero, the
6851 addend specifies the GOT pointer offset within .got2. */
6852 rel->r_addend += got2->output_offset;
6853 }
6854 continue;
6855 }
6856
7619e7c7
AM
6857 /* TLS optimizations. Replace instruction sequences and relocs
6858 based on information we collected in tls_optimize. We edit
6859 RELOCS so that --emit-relocs will output something sensible
6860 for the final instruction stream. */
6861 tls_mask = 0;
6862 tls_gd = 0;
727fc41e
AM
6863 if (h != NULL)
6864 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
6865 else if (local_got_offsets != NULL)
7619e7c7 6866 {
e054468f 6867 struct plt_entry **local_plt;
727fc41e 6868 char *lgot_masks;
e054468f
AM
6869 local_plt
6870 = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
6871 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
727fc41e 6872 tls_mask = lgot_masks[r_symndx];
7619e7c7
AM
6873 }
6874
6875 /* Ensure reloc mapping code below stays sane. */
6876 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
6877 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
6878 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
6879 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
6880 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
6881 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
6882 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
6883 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
6884 abort ();
6885 switch (r_type)
6886 {
6887 default:
6888 break;
6889
6890 case R_PPC_GOT_TPREL16:
6891 case R_PPC_GOT_TPREL16_LO:
e054468f 6892 if ((tls_mask & TLS_TLS) != 0
7619e7c7
AM
6893 && (tls_mask & TLS_TPREL) == 0)
6894 {
6895 bfd_vma insn;
91d6fa6a 6896
4fe5ca5b 6897 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
7619e7c7
AM
6898 insn &= 31 << 21;
6899 insn |= 0x3c020000; /* addis 0,2,0 */
4fe5ca5b 6900 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
7619e7c7
AM
6901 r_type = R_PPC_TPREL16_HA;
6902 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6903 }
6904 break;
6905
6906 case R_PPC_TLS:
e054468f 6907 if ((tls_mask & TLS_TLS) != 0
7619e7c7
AM
6908 && (tls_mask & TLS_TPREL) == 0)
6909 {
2d0f3896
AM
6910 bfd_vma insn;
6911
7619e7c7 6912 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
6913 insn = _bfd_elf_ppc_at_tls_transform (insn, 2);
6914 if (insn == 0)
7619e7c7 6915 abort ();
7619e7c7
AM
6916 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
6917 r_type = R_PPC_TPREL16_LO;
6918 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4fe5ca5b 6919
7619e7c7 6920 /* Was PPC_TLS which sits on insn boundary, now
4fe5ca5b
GM
6921 PPC_TPREL16_LO which is at low-order half-word. */
6922 rel->r_offset += d_offset;
7619e7c7
AM
6923 }
6924 break;
6925
6926 case R_PPC_GOT_TLSGD16_HI:
6927 case R_PPC_GOT_TLSGD16_HA:
6928 tls_gd = TLS_TPRELGD;
e054468f 6929 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
7619e7c7
AM
6930 goto tls_gdld_hi;
6931 break;
6932
6933 case R_PPC_GOT_TLSLD16_HI:
6934 case R_PPC_GOT_TLSLD16_HA:
e054468f 6935 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
7619e7c7
AM
6936 {
6937 tls_gdld_hi:
6938 if ((tls_mask & tls_gd) != 0)
6939 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6940 + R_PPC_GOT_TPREL16);
6941 else
6942 {
6943 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4fe5ca5b 6944 rel->r_offset -= d_offset;
7619e7c7
AM
6945 r_type = R_PPC_NONE;
6946 }
6947 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6948 }
6949 break;
6950
6951 case R_PPC_GOT_TLSGD16:
6952 case R_PPC_GOT_TLSGD16_LO:
6953 tls_gd = TLS_TPRELGD;
e054468f 6954 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
b7fcf6f6 6955 goto tls_ldgd_opt;
7619e7c7
AM
6956 break;
6957
6958 case R_PPC_GOT_TLSLD16:
6959 case R_PPC_GOT_TLSLD16_LO:
e054468f 6960 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
7619e7c7 6961 {
3a71aa26 6962 unsigned int insn1, insn2;
b7fcf6f6 6963 bfd_vma offset;
7619e7c7 6964
b7fcf6f6 6965 tls_ldgd_opt:
727fc41e
AM
6966 offset = (bfd_vma) -1;
6967 /* If not using the newer R_PPC_TLSGD/LD to mark
6968 __tls_get_addr calls, we must trust that the call
6969 stays with its arg setup insns, ie. that the next
6970 reloc is the __tls_get_addr call associated with
6971 the current reloc. Edit both insns. */
6972 if (input_section->has_tls_get_addr_call
6973 && rel + 1 < relend
6974 && branch_reloc_hash_match (input_bfd, rel + 1,
6975 htab->tls_get_addr))
6976 offset = rel[1].r_offset;
b7fcf6f6
AM
6977 if ((tls_mask & tls_gd) != 0)
6978 {
6979 /* IE */
3a71aa26
AM
6980 insn1 = bfd_get_32 (output_bfd,
6981 contents + rel->r_offset - d_offset);
b7fcf6f6
AM
6982 insn1 &= (1 << 26) - 1;
6983 insn1 |= 32 << 26; /* lwz */
727fc41e
AM
6984 if (offset != (bfd_vma) -1)
6985 {
f58d5a2d 6986 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
727fc41e
AM
6987 insn2 = 0x7c631214; /* add 3,3,2 */
6988 bfd_put_32 (output_bfd, insn2, contents + offset);
6989 }
b7fcf6f6
AM
6990 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6991 + R_PPC_GOT_TPREL16);
6992 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6993 }
6994 else
6995 {
6996 /* LE */
6997 insn1 = 0x3c620000; /* addis 3,2,0 */
7619e7c7
AM
6998 if (tls_gd == 0)
6999 {
b7fcf6f6 7000 /* Was an LD reloc. */
1d483afe
AM
7001 for (r_symndx = 0;
7002 r_symndx < symtab_hdr->sh_info;
7003 r_symndx++)
7004 if (local_sections[r_symndx] == sec)
7005 break;
7006 if (r_symndx >= symtab_hdr->sh_info)
7007 r_symndx = 0;
b7fcf6f6 7008 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
1d483afe
AM
7009 if (r_symndx != 0)
7010 rel->r_addend -= (local_syms[r_symndx].st_value
7011 + sec->output_offset
7012 + sec->output_section->vma);
7619e7c7 7013 }
b7fcf6f6
AM
7014 r_type = R_PPC_TPREL16_HA;
7015 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
727fc41e
AM
7016 if (offset != (bfd_vma) -1)
7017 {
7018 rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
7019 rel[1].r_offset = offset + d_offset;
7020 rel[1].r_addend = rel->r_addend;
7021 insn2 = 0x38630000; /* addi 3,3,0 */
7022 bfd_put_32 (output_bfd, insn2, contents + offset);
7023 }
b7fcf6f6
AM
7024 }
7025 bfd_put_32 (output_bfd, insn1,
7026 contents + rel->r_offset - d_offset);
b7fcf6f6
AM
7027 if (tls_gd == 0)
7028 {
7029 /* We changed the symbol on an LD reloc. Start over
7030 in order to get h, sym, sec etc. right. */
7031 rel--;
7032 continue;
7619e7c7 7033 }
252b5132 7034 }
7619e7c7 7035 break;
727fc41e
AM
7036
7037 case R_PPC_TLSGD:
e054468f 7038 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
727fc41e
AM
7039 {
7040 unsigned int insn2;
7041 bfd_vma offset = rel->r_offset;
7042
7043 if ((tls_mask & TLS_TPRELGD) != 0)
7044 {
7045 /* IE */
7046 r_type = R_PPC_NONE;
7047 insn2 = 0x7c631214; /* add 3,3,2 */
7048 }
7049 else
7050 {
7051 /* LE */
7052 r_type = R_PPC_TPREL16_LO;
7053 rel->r_offset += d_offset;
7054 insn2 = 0x38630000; /* addi 3,3,0 */
7055 }
7056 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7057 bfd_put_32 (output_bfd, insn2, contents + offset);
7058 /* Zap the reloc on the _tls_get_addr call too. */
7059 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 7060 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
727fc41e
AM
7061 }
7062 break;
7063
7064 case R_PPC_TLSLD:
e054468f 7065 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
727fc41e
AM
7066 {
7067 unsigned int insn2;
7068
7069 for (r_symndx = 0;
7070 r_symndx < symtab_hdr->sh_info;
7071 r_symndx++)
7072 if (local_sections[r_symndx] == sec)
7073 break;
7074 if (r_symndx >= symtab_hdr->sh_info)
7075 r_symndx = 0;
7076 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7077 if (r_symndx != 0)
7078 rel->r_addend -= (local_syms[r_symndx].st_value
7079 + sec->output_offset
7080 + sec->output_section->vma);
7081
7082 rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
7083 rel->r_offset += d_offset;
7084 insn2 = 0x38630000; /* addi 3,3,0 */
7085 bfd_put_32 (output_bfd, insn2,
7086 contents + rel->r_offset - d_offset);
7087 /* Zap the reloc on the _tls_get_addr call too. */
7088 BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
f58d5a2d 7089 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
727fc41e
AM
7090 rel--;
7091 continue;
7092 }
7093 break;
7619e7c7
AM
7094 }
7095
7096 /* Handle other relocations that tweak non-addend part of insn. */
7097 branch_bit = 0;
7098 switch (r_type)
7099 {
7100 default:
7101 break;
7102
7103 /* Branch taken prediction relocations. */
7104 case R_PPC_ADDR14_BRTAKEN:
7105 case R_PPC_REL14_BRTAKEN:
7106 branch_bit = BRANCH_PREDICT_BIT;
7107 /* Fall thru */
7108
4cc11e76 7109 /* Branch not taken prediction relocations. */
7619e7c7
AM
7110 case R_PPC_ADDR14_BRNTAKEN:
7111 case R_PPC_REL14_BRNTAKEN:
91d6fa6a
NC
7112 {
7113 bfd_vma insn;
7619e7c7 7114
91d6fa6a
NC
7115 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7116 insn &= ~BRANCH_PREDICT_BIT;
7117 insn |= branch_bit;
7619e7c7 7118
91d6fa6a
NC
7119 from = (rel->r_offset
7120 + input_section->output_offset
7121 + input_section->output_section->vma);
7619e7c7 7122
91d6fa6a
NC
7123 /* Invert 'y' bit if not the default. */
7124 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7125 insn ^= BRANCH_PREDICT_BIT;
7126
7127 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7128 break;
7129 }
252b5132
RH
7130 }
7131
e054468f 7132 ifunc = NULL;
25f23106 7133 if (!htab->is_vxworks)
e054468f 7134 {
de972ffa
AM
7135 struct plt_entry *ent;
7136
e054468f
AM
7137 if (h != NULL)
7138 {
7139 if (h->type == STT_GNU_IFUNC)
7140 ifunc = &h->plt.plist;
7141 }
de972ffa
AM
7142 else if (local_got_offsets != NULL
7143 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
e054468f 7144 {
de972ffa 7145 struct plt_entry **local_plt;
e054468f 7146
de972ffa
AM
7147 local_plt = (struct plt_entry **) (local_got_offsets
7148 + symtab_hdr->sh_info);
7149 ifunc = local_plt + r_symndx;
e054468f 7150 }
e054468f 7151
de972ffa
AM
7152 ent = NULL;
7153 if (ifunc != NULL
7154 && (!info->shared
7155 || is_branch_reloc (r_type)))
7156 {
7157 addend = 0;
a877a2b6 7158 if (r_type == R_PPC_PLTREL24 && info->shared)
de972ffa
AM
7159 addend = rel->r_addend;
7160 ent = find_plt_ent (ifunc, got2, addend);
7161 }
7162 if (ent != NULL)
7163 {
e054468f
AM
7164 if (h == NULL && (ent->plt.offset & 1) == 0)
7165 {
7166 Elf_Internal_Rela rela;
7167 bfd_byte *loc;
7168
7169 rela.r_offset = (htab->iplt->output_section->vma
7170 + htab->iplt->output_offset
7171 + ent->plt.offset);
7172 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7173 rela.r_addend = relocation;
25f23106
AM
7174 loc = htab->reliplt->contents;
7175 loc += (htab->reliplt->reloc_count++
7176 * sizeof (Elf32_External_Rela));
e054468f
AM
7177 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
7178
7179 ent->plt.offset |= 1;
7180 }
7181 if (h == NULL && (ent->glink_offset & 1) == 0)
7182 {
a7f2871e
AM
7183 unsigned char *p = ((unsigned char *) htab->glink->contents
7184 + ent->glink_offset);
7185 write_glink_stub (ent, htab->iplt, p, info);
e054468f
AM
7186 ent->glink_offset |= 1;
7187 }
7188
7189 unresolved_reloc = FALSE;
7190 if (htab->plt_type == PLT_NEW
7191 || !htab->elf.dynamic_sections_created
7192 || h == NULL)
7193 relocation = (htab->glink->output_section->vma
7194 + htab->glink->output_offset
7195 + (ent->glink_offset & ~1));
7196 else
7197 relocation = (htab->plt->output_section->vma
7198 + htab->plt->output_offset
7199 + ent->plt.offset);
7200 }
7201 }
7202
7619e7c7
AM
7203 addend = rel->r_addend;
7204 tls_type = 0;
7205 howto = NULL;
55fd94b0
AM
7206 if (r_type < R_PPC_max)
7207 howto = ppc_elf_howto_table[r_type];
7619e7c7 7208 switch (r_type)
252b5132
RH
7209 {
7210 default:
7619e7c7 7211 (*_bfd_error_handler)
d003868e
AM
7212 (_("%B: unknown relocation type %d for symbol %s"),
7213 input_bfd, (int) r_type, sym_name);
252b5132
RH
7214
7215 bfd_set_error (bfd_error_bad_value);
b34976b6 7216 ret = FALSE;
252b5132
RH
7217 continue;
7218
7619e7c7
AM
7219 case R_PPC_NONE:
7220 case R_PPC_TLS:
727fc41e
AM
7221 case R_PPC_TLSGD:
7222 case R_PPC_TLSLD:
7619e7c7
AM
7223 case R_PPC_EMB_MRKREF:
7224 case R_PPC_GNU_VTINHERIT:
7225 case R_PPC_GNU_VTENTRY:
7595d193
L
7226 continue;
7227
7619e7c7
AM
7228 /* GOT16 relocations. Like an ADDR16 using the symbol's
7229 address in the GOT as relocation value instead of the
7230 symbol's value itself. Also, create a GOT entry for the
7231 symbol and put the symbol value there. */
7232 case R_PPC_GOT_TLSGD16:
7233 case R_PPC_GOT_TLSGD16_LO:
7234 case R_PPC_GOT_TLSGD16_HI:
7235 case R_PPC_GOT_TLSGD16_HA:
7236 tls_type = TLS_TLS | TLS_GD;
7237 goto dogot;
7238
7239 case R_PPC_GOT_TLSLD16:
7240 case R_PPC_GOT_TLSLD16_LO:
7241 case R_PPC_GOT_TLSLD16_HI:
7242 case R_PPC_GOT_TLSLD16_HA:
7243 tls_type = TLS_TLS | TLS_LD;
7244 goto dogot;
7245
7246 case R_PPC_GOT_TPREL16:
7247 case R_PPC_GOT_TPREL16_LO:
7248 case R_PPC_GOT_TPREL16_HI:
7249 case R_PPC_GOT_TPREL16_HA:
7250 tls_type = TLS_TLS | TLS_TPREL;
7251 goto dogot;
7252
7253 case R_PPC_GOT_DTPREL16:
7254 case R_PPC_GOT_DTPREL16_LO:
7255 case R_PPC_GOT_DTPREL16_HI:
7256 case R_PPC_GOT_DTPREL16_HA:
7257 tls_type = TLS_TLS | TLS_DTPREL;
7258 goto dogot;
7259
7260 case R_PPC_GOT16:
7261 case R_PPC_GOT16_LO:
7262 case R_PPC_GOT16_HI:
7263 case R_PPC_GOT16_HA:
727fc41e 7264 tls_mask = 0;
7619e7c7
AM
7265 dogot:
7266 {
7267 /* Relocation is to the entry for this symbol in the global
7268 offset table. */
7269 bfd_vma off;
7270 bfd_vma *offp;
7271 unsigned long indx;
7272
7273 if (htab->got == NULL)
7274 abort ();
7275
7276 indx = 0;
7277 if (tls_type == (TLS_TLS | TLS_LD)
d881513a 7278 && (h == NULL
f5385ebf 7279 || !h->def_dynamic))
7619e7c7
AM
7280 offp = &htab->tlsld_got.offset;
7281 else if (h != NULL)
7282 {
7283 bfd_boolean dyn;
7284 dyn = htab->elf.dynamic_sections_created;
ee05f2fe 7285 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7619e7c7
AM
7286 || (info->shared
7287 && SYMBOL_REFERENCES_LOCAL (info, h)))
7288 /* This is actually a static link, or it is a
7289 -Bsymbolic link and the symbol is defined
7290 locally, or the symbol was forced to be local
7291 because of a version file. */
7292 ;
7293 else
7294 {
7295 indx = h->dynindx;
7296 unresolved_reloc = FALSE;
7297 }
7298 offp = &h->got.offset;
7299 }
7300 else
7301 {
7302 if (local_got_offsets == NULL)
7303 abort ();
7304 offp = &local_got_offsets[r_symndx];
7305 }
7306
7307 /* The offset must always be a multiple of 4. We use the
7308 least significant bit to record whether we have already
7309 processed this entry. */
7310 off = *offp;
7311 if ((off & 1) != 0)
7312 off &= ~1;
7313 else
7314 {
70bccea4
AM
7315 unsigned int tls_m = (tls_mask
7316 & (TLS_LD | TLS_GD | TLS_DTPREL
7317 | TLS_TPREL | TLS_TPRELGD));
7318
7319 if (offp == &htab->tlsld_got.offset)
7320 tls_m = TLS_LD;
7321 else if (h == NULL
f5385ebf 7322 || !h->def_dynamic)
70bccea4
AM
7323 tls_m &= ~TLS_LD;
7324
7325 /* We might have multiple got entries for this sym.
7326 Initialize them all. */
7327 do
7619e7c7 7328 {
70bccea4
AM
7329 int tls_ty = 0;
7330
7331 if ((tls_m & TLS_LD) != 0)
7619e7c7 7332 {
70bccea4
AM
7333 tls_ty = TLS_TLS | TLS_LD;
7334 tls_m &= ~TLS_LD;
7335 }
7336 else if ((tls_m & TLS_GD) != 0)
7337 {
7338 tls_ty = TLS_TLS | TLS_GD;
7339 tls_m &= ~TLS_GD;
7340 }
7341 else if ((tls_m & TLS_DTPREL) != 0)
7342 {
7343 tls_ty = TLS_TLS | TLS_DTPREL;
7344 tls_m &= ~TLS_DTPREL;
7345 }
7346 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
7347 {
7348 tls_ty = TLS_TLS | TLS_TPREL;
7349 tls_m = 0;
7619e7c7 7350 }
7619e7c7 7351
70bccea4 7352 /* Generate relocs for the dynamic linker. */
4e795f50 7353 if ((info->shared || indx != 0)
d6e14abc
AM
7354 && (offp == &htab->tlsld_got.offset
7355 || h == NULL
4e795f50
AM
7356 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7357 || h->root.type != bfd_link_hash_undefweak))
7619e7c7 7358 {
91e21fb7 7359 asection *rsec = htab->relgot;
91d6fa6a 7360 bfd_byte * loc;
91e21fb7 7361
70bccea4
AM
7362 outrel.r_offset = (htab->got->output_section->vma
7363 + htab->got->output_offset
7364 + off);
e515b051 7365 outrel.r_addend = 0;
70bccea4
AM
7366 if (tls_ty & (TLS_LD | TLS_GD))
7367 {
7368 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
70bccea4
AM
7369 if (tls_ty == (TLS_TLS | TLS_GD))
7370 {
25f23106
AM
7371 loc = rsec->contents;
7372 loc += (rsec->reloc_count++
70bccea4
AM
7373 * sizeof (Elf32_External_Rela));
7374 bfd_elf32_swap_reloca_out (output_bfd,
7375 &outrel, loc);
e515b051 7376 outrel.r_offset += 4;
70bccea4
AM
7377 outrel.r_info
7378 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
70bccea4
AM
7379 }
7380 }
7381 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
7382 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7383 else if (tls_ty == (TLS_TLS | TLS_TPREL))
7384 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
25f23106 7385 else if (indx != 0)
70bccea4 7386 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
25f23106
AM
7387 else if (ifunc != NULL)
7388 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7389 else
7390 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
d6e14abc 7391 if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
e515b051
AM
7392 {
7393 outrel.r_addend += relocation;
7394 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 7395 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 7396 }
25f23106
AM
7397 loc = rsec->contents;
7398 loc += (rsec->reloc_count++
70bccea4
AM
7399 * sizeof (Elf32_External_Rela));
7400 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7619e7c7
AM
7401 }
7402
70bccea4
AM
7403 /* Init the .got section contents if we're not
7404 emitting a reloc. */
7405 else
7619e7c7 7406 {
70bccea4
AM
7407 bfd_vma value = relocation;
7408
7b609f53
AM
7409 if (tls_ty == (TLS_TLS | TLS_LD))
7410 value = 1;
7411 else if (tls_ty != 0)
70bccea4 7412 {
e1918d23 7413 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 7414 if (tls_ty == (TLS_TLS | TLS_TPREL))
70bccea4 7415 value += DTP_OFFSET - TP_OFFSET;
70bccea4 7416
7b609f53
AM
7417 if (tls_ty == (TLS_TLS | TLS_GD))
7418 {
7419 bfd_put_32 (output_bfd, value,
7420 htab->got->contents + off + 4);
7421 value = 1;
7422 }
70bccea4 7423 }
70bccea4
AM
7424 bfd_put_32 (output_bfd, value,
7425 htab->got->contents + off);
7619e7c7 7426 }
70bccea4
AM
7427
7428 off += 4;
7429 if (tls_ty & (TLS_LD | TLS_GD))
7430 off += 4;
7619e7c7 7431 }
70bccea4
AM
7432 while (tls_m != 0);
7433
7434 off = *offp;
7435 *offp = off | 1;
7619e7c7
AM
7436 }
7437
7438 if (off >= (bfd_vma) -2)
7439 abort ();
7440
70bccea4
AM
7441 if ((tls_type & TLS_TLS) != 0)
7442 {
7443 if (tls_type != (TLS_TLS | TLS_LD))
7444 {
7445 if ((tls_mask & TLS_LD) != 0
7446 && !(h == NULL
f5385ebf 7447 || !h->def_dynamic))
70bccea4
AM
7448 off += 8;
7449 if (tls_type != (TLS_TLS | TLS_GD))
7450 {
7451 if ((tls_mask & TLS_GD) != 0)
7452 off += 8;
7453 if (tls_type != (TLS_TLS | TLS_DTPREL))
7454 {
7455 if ((tls_mask & TLS_DTPREL) != 0)
7456 off += 4;
7457 }
7458 }
7459 }
7460 }
7461
e87d4038
AM
7462 relocation = (htab->got->output_section->vma
7463 + htab->got->output_offset
7464 + off
7465 - SYM_VAL (htab->elf.hgot));
7619e7c7
AM
7466
7467 /* Addends on got relocations don't make much sense.
7468 x+off@got is actually x@got+off, and since the got is
7469 generated by a hash table traversal, the value in the
7470 got at entry m+n bears little relation to the entry m. */
7471 if (addend != 0)
7472 (*_bfd_error_handler)
d003868e
AM
7473 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
7474 input_bfd,
7475 input_section,
7619e7c7 7476 (long) rel->r_offset,
7b609f53 7477 howto->name,
7619e7c7
AM
7478 sym_name);
7479 }
70bccea4 7480 break;
7619e7c7 7481
252b5132 7482 /* Relocations that need no special processing. */
7619e7c7 7483 case R_PPC_LOCAL24PC:
252b5132
RH
7484 /* It makes no sense to point a local relocation
7485 at a symbol not in this object. */
7b609f53 7486 if (unresolved_reloc)
252b5132 7487 {
464e1740
ILT
7488 if (! (*info->callbacks->undefined_symbol) (info,
7489 h->root.root.string,
7490 input_bfd,
7491 input_section,
5cc7c785 7492 rel->r_offset,
b34976b6
AM
7493 TRUE))
7494 return FALSE;
252b5132
RH
7495 continue;
7496 }
7497 break;
7498
7619e7c7
AM
7499 case R_PPC_DTPREL16:
7500 case R_PPC_DTPREL16_LO:
7501 case R_PPC_DTPREL16_HI:
7502 case R_PPC_DTPREL16_HA:
e1918d23 7503 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
7504 break;
7505
70bccea4
AM
7506 /* Relocations that may need to be propagated if this is a shared
7507 object. */
7619e7c7
AM
7508 case R_PPC_TPREL16:
7509 case R_PPC_TPREL16_LO:
7510 case R_PPC_TPREL16_HI:
7511 case R_PPC_TPREL16_HA:
766bc656
AM
7512 if (h != NULL
7513 && h->root.type == bfd_link_hash_undefweak
7514 && h->dynindx == -1)
7515 {
7516 /* Make this relocation against an undefined weak symbol
7517 resolve to zero. This is really just a tweak, since
7518 code using weak externs ought to check that they are
7519 defined before using them. */
7520 bfd_byte *p = contents + rel->r_offset - d_offset;
7521 unsigned int insn = bfd_get_32 (output_bfd, p);
7522 insn = _bfd_elf_ppc_at_tprel_transform (insn, 2);
7523 if (insn != 0)
7524 bfd_put_32 (output_bfd, insn, p);
7525 break;
7526 }
e1918d23 7527 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7619e7c7
AM
7528 /* The TPREL16 relocs shouldn't really be used in shared
7529 libs as they will result in DT_TEXTREL being set, but
7530 support them anyway. */
7531 goto dodyn;
7532
7533 case R_PPC_TPREL32:
e1918d23 7534 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7619e7c7
AM
7535 goto dodyn;
7536
7537 case R_PPC_DTPREL32:
e1918d23 7538 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
7539 goto dodyn;
7540
e515b051
AM
7541 case R_PPC_DTPMOD32:
7542 relocation = 1;
7543 addend = 0;
7544 goto dodyn;
7545
d7128ce4
AM
7546 case R_PPC_REL16:
7547 case R_PPC_REL16_LO:
7548 case R_PPC_REL16_HI:
7549 case R_PPC_REL16_HA:
7550 break;
7551
7619e7c7 7552 case R_PPC_REL32:
625af618
AM
7553 if (h == NULL || h == htab->elf.hgot)
7554 break;
7555 /* fall through */
7556
7557 case R_PPC_ADDR32:
7558 case R_PPC_ADDR16:
7559 case R_PPC_ADDR16_LO:
7560 case R_PPC_ADDR16_HI:
7561 case R_PPC_ADDR16_HA:
7562 case R_PPC_UADDR32:
7563 case R_PPC_UADDR16:
7564 goto dodyn;
7565
7566 case R_PPC_REL24:
7619e7c7
AM
7567 case R_PPC_REL14:
7568 case R_PPC_REL14_BRTAKEN:
7569 case R_PPC_REL14_BRNTAKEN:
252b5132
RH
7570 /* If these relocations are not to a named symbol, they can be
7571 handled right here, no need to bother the dynamic linker. */
89200bf8 7572 if (SYMBOL_CALLS_LOCAL (info, h)
3b36f7e6 7573 || h == htab->elf.hgot)
252b5132 7574 break;
70bccea4 7575 /* fall through */
252b5132 7576
7619e7c7 7577 case R_PPC_ADDR24:
7619e7c7
AM
7578 case R_PPC_ADDR14:
7579 case R_PPC_ADDR14_BRTAKEN:
7580 case R_PPC_ADDR14_BRNTAKEN:
625af618
AM
7581 if (h != NULL && !info->shared)
7582 break;
7583 /* fall through */
7584
7619e7c7 7585 dodyn:
3348747a
NS
7586 if ((input_section->flags & SEC_ALLOC) == 0
7587 || is_vxworks_tls)
c87b5a93 7588 break;
c87b5a93 7589
ee05f2fe 7590 if ((info->shared
89200bf8
AM
7591 && !(h != NULL
7592 && ((h->root.type == bfd_link_hash_undefined
7593 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7594 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
7595 || (h->root.type == bfd_link_hash_undefweak
7596 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)))
1d483afe 7597 && (must_be_dyn_reloc (info, r_type)
f6c52c13 7598 || !SYMBOL_CALLS_LOCAL (info, h)))
ee05f2fe
AM
7599 || (ELIMINATE_COPY_RELOCS
7600 && !info->shared
ee05f2fe
AM
7601 && h != NULL
7602 && h->dynindx != -1
f5385ebf 7603 && !h->non_got_ref
de972ffa 7604 && !h->def_regular))
252b5132 7605 {
0bb2d96a 7606 int skip;
91d6fa6a 7607 bfd_byte * loc;
252b5132 7608#ifdef DEBUG
55fd94b0
AM
7609 fprintf (stderr, "ppc_elf_relocate_section needs to "
7610 "create relocation for %s\n",
70bccea4
AM
7611 (h && h->root.root.string
7612 ? h->root.root.string : "<unknown>"));
252b5132
RH
7613#endif
7614
7615 /* When generating a shared object, these relocations
70bccea4
AM
7616 are copied into the output file to be resolved at run
7617 time. */
252b5132
RH
7618 if (sreloc == NULL)
7619 {
25f23106
AM
7620 sreloc = elf_section_data (input_section)->sreloc;
7621 if (!htab->elf.dynamic_sections_created)
7622 sreloc = htab->reliplt;
83bac4b0 7623 if (sreloc == NULL)
b34976b6 7624 return FALSE;
252b5132
RH
7625 }
7626
0bb2d96a 7627 skip = 0;
c629eae0
JJ
7628 outrel.r_offset =
7629 _bfd_elf_section_offset (output_bfd, info, input_section,
7630 rel->r_offset);
0bb2d96a
JJ
7631 if (outrel.r_offset == (bfd_vma) -1
7632 || outrel.r_offset == (bfd_vma) -2)
7633 skip = (int) outrel.r_offset;
252b5132
RH
7634 outrel.r_offset += (input_section->output_section->vma
7635 + input_section->output_offset);
7636
7637 if (skip)
7638 memset (&outrel, 0, sizeof outrel);
89200bf8
AM
7639 else if ((h != NULL
7640 && (h->root.type == bfd_link_hash_undefined
7641 || h->root.type == bfd_link_hash_undefweak))
7642 || !SYMBOL_REFERENCES_LOCAL (info, h))
252b5132 7643 {
7619e7c7 7644 unresolved_reloc = FALSE;
252b5132
RH
7645 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
7646 outrel.r_addend = rel->r_addend;
7647 }
7648 else
7649 {
47388f4c
AM
7650 outrel.r_addend = relocation + rel->r_addend;
7651
25f23106 7652 if (r_type != R_PPC_ADDR32)
252b5132 7653 {
89200bf8 7654 long indx = 0;
252b5132 7655
25f23106
AM
7656 if (ifunc != NULL)
7657 {
7658 /* If we get here when building a static
7659 executable, then the libc startup function
7660 responsible for applying indirect function
7661 relocations is going to complain about
7662 the reloc type.
7663 If we get here when building a dynamic
7664 executable, it will be because we have
7665 a text relocation. The dynamic loader
7666 will set the text segment writable and
7667 non-executable to apply text relocations.
7668 So we'll segfault when trying to run the
7669 indirection function to resolve the reloc. */
7670 (*_bfd_error_handler)
7671 (_("%B(%A+0x%lx): relocation %s for indirect "
7672 "function %s unsupported"),
7673 input_bfd,
7674 input_section,
7675 (long) rel->r_offset,
7676 howto->name,
7677 sym_name);
7678 ret = FALSE;
7679 }
7680 else if (r_symndx == 0 || bfd_is_abs_section (sec))
89200bf8 7681 ;
252b5132
RH
7682 else if (sec == NULL || sec->owner == NULL)
7683 {
7684 bfd_set_error (bfd_error_bad_value);
89200bf8 7685 ret = FALSE;
252b5132
RH
7686 }
7687 else
7688 {
7689 asection *osec;
7690
47388f4c
AM
7691 /* We are turning this relocation into one
7692 against a section symbol. It would be
7693 proper to subtract the symbol's value,
7694 osec->vma, from the emitted reloc addend,
25f23106
AM
7695 but ld.so expects buggy relocs.
7696 FIXME: Why not always use a zero index? */
252b5132
RH
7697 osec = sec->output_section;
7698 indx = elf_section_data (osec)->dynindx;
74541ad4
AM
7699 if (indx == 0)
7700 {
7701 osec = htab->elf.text_index_section;
7702 indx = elf_section_data (osec)->dynindx;
7703 }
7704 BFD_ASSERT (indx != 0);
252b5132 7705#ifdef DEBUG
74541ad4 7706 if (indx == 0)
e460dd0d 7707 printf ("indx=%ld section=%s flags=%08x name=%s\n",
55fd94b0
AM
7708 indx, osec->name, osec->flags,
7709 h->root.root.string);
252b5132
RH
7710#endif
7711 }
7712
7713 outrel.r_info = ELF32_R_INFO (indx, r_type);
252b5132 7714 }
25f23106
AM
7715 else if (ifunc != NULL)
7716 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7717 else
7718 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
252b5132
RH
7719 }
7720
947216bf
AM
7721 loc = sreloc->contents;
7722 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
7723 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132 7724
2243deae 7725 if (skip == -1)
252b5132 7726 continue;
2243deae
L
7727
7728 /* This reloc will be computed at runtime. We clear the memory
7729 so that it contains predictable value. */
7730 if (! skip
7731 && ((input_section->flags & SEC_ALLOC) != 0
7732 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
7733 {
7734 relocation = howto->pc_relative ? outrel.r_offset : 0;
7735 addend = 0;
7736 break;
7737 }
252b5132 7738 }
252b5132
RH
7739 break;
7740
32af9f6e
AM
7741 case R_PPC_RELAX_PLT:
7742 case R_PPC_RELAX_PLTREL24:
de972ffa
AM
7743 if (h != NULL)
7744 {
32af9f6e
AM
7745 struct plt_entry *ent;
7746 bfd_vma got2_addend = 0;
7747
7748 if (r_type == R_PPC_RELAX_PLTREL24)
7749 {
7750 if (info->shared)
7751 got2_addend = addend;
7752 addend = 0;
7753 }
7754 ent = find_plt_ent (&h->plt.plist, got2, got2_addend);
de972ffa
AM
7755 if (htab->plt_type == PLT_NEW)
7756 relocation = (htab->glink->output_section->vma
7757 + htab->glink->output_offset
7758 + ent->glink_offset);
7759 else
7760 relocation = (htab->plt->output_section->vma
7761 + htab->plt->output_offset
7762 + ent->plt.offset);
7763 }
b4a38de6
AM
7764 /* Fall thru */
7765
32af9f6e
AM
7766 case R_PPC_RELAX:
7767 if (info->shared)
7768 relocation -= (input_section->output_section->vma
7769 + input_section->output_offset
7770 + rel->r_offset - 4);
b4a38de6 7771
9abc968f
AM
7772 {
7773 unsigned long t0;
7774 unsigned long t1;
7775
7776 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
7777 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
7778
7779 /* We're clearing the bits for R_PPC_ADDR16_HA
7780 and R_PPC_ADDR16_LO here. */
7781 t0 &= ~0xffff;
7782 t1 &= ~0xffff;
7783
7784 /* t0 is HA, t1 is LO */
7785 relocation += addend;
7786 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
7787 t1 |= relocation & 0xffff;
7788
7789 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
7790 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
9bc4e62b
NS
7791
7792 /* Rewrite the reloc and convert one of the trailing nop
7793 relocs to describe this relocation. */
7794 BFD_ASSERT (ELF32_R_TYPE (relend[-1].r_info) == R_PPC_NONE);
7795 /* The relocs are at the bottom 2 bytes */
7796 rel[0].r_offset += 2;
7797 memmove (rel + 1, rel, (relend - rel - 1) * sizeof (*rel));
7798 rel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
7799 rel[1].r_offset += 4;
7800 rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
7801 rel++;
9abc968f 7802 }
8517fae7 7803 continue;
deaaf2f3 7804
70bccea4 7805 /* Indirect .sdata relocation. */
7619e7c7 7806 case R_PPC_EMB_SDAI16:
c9a2f333 7807 BFD_ASSERT (htab->sdata[0].section != NULL);
bd6c6e2b
AM
7808 if (!is_static_defined (htab->sdata[0].sym))
7809 {
7810 unresolved_reloc = TRUE;
7811 break;
7812 }
7619e7c7 7813 relocation
76750a2f
AM
7814 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
7815 h, relocation, rel);
bd6c6e2b 7816 addend = 0;
252b5132
RH
7817 break;
7818
70bccea4 7819 /* Indirect .sdata2 relocation. */
7619e7c7 7820 case R_PPC_EMB_SDA2I16:
c9a2f333 7821 BFD_ASSERT (htab->sdata[1].section != NULL);
bd6c6e2b
AM
7822 if (!is_static_defined (htab->sdata[1].sym))
7823 {
7824 unresolved_reloc = TRUE;
7825 break;
7826 }
7619e7c7 7827 relocation
76750a2f
AM
7828 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
7829 h, relocation, rel);
bd6c6e2b 7830 addend = 0;
252b5132
RH
7831 break;
7832
70bccea4
AM
7833 /* Handle the TOC16 reloc. We want to use the offset within the .got
7834 section, not the actual VMA. This is appropriate when generating
7835 an embedded ELF object, for which the .got section acts like the
7836 AIX .toc section. */
7619e7c7 7837 case R_PPC_TOC16: /* phony GOT16 relocations */
3b4f3a11
AM
7838 if (sec == NULL || sec->output_section == NULL)
7839 {
7840 unresolved_reloc = TRUE;
7841 break;
7842 }
7843 BFD_ASSERT (strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
f12123c0 7844 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
252b5132 7845
3b4f3a11 7846 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
252b5132
RH
7847 break;
7848
7619e7c7 7849 case R_PPC_PLTREL24:
e054468f 7850 if (h == NULL || ifunc != NULL)
6d78d0b9 7851 break;
252b5132 7852 /* Relocation is to the entry for this symbol in the
70bccea4 7853 procedure linkage table. */
a6aa5195 7854 {
a877a2b6
AM
7855 struct plt_entry *ent = find_plt_ent (&h->plt.plist, got2,
7856 info->shared ? addend : 0);
a6aa5195
AM
7857 addend = 0;
7858 if (ent == NULL
7859 || htab->plt == NULL)
7860 {
7861 /* We didn't make a PLT entry for this symbol. This
7862 happens when statically linking PIC code, or when
7863 using -Bsymbolic. */
7864 break;
7865 }
252b5132 7866
a6aa5195 7867 unresolved_reloc = FALSE;
4a3dc543 7868 if (htab->plt_type == PLT_NEW)
a6aa5195
AM
7869 relocation = (htab->glink->output_section->vma
7870 + htab->glink->output_offset
7871 + ent->glink_offset);
7872 else
7873 relocation = (htab->plt->output_section->vma
7874 + htab->plt->output_offset
7875 + ent->plt.offset);
7876 }
8da6118f 7877 break;
252b5132 7878
70bccea4 7879 /* Relocate against _SDA_BASE_. */
7619e7c7 7880 case R_PPC_SDAREL16:
252b5132
RH
7881 {
7882 const char *name;
bd6c6e2b 7883 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
252b5132 7884
bd6c6e2b
AM
7885 if (sec == NULL
7886 || sec->output_section == NULL
7887 || !is_static_defined (sda))
3b4f3a11
AM
7888 {
7889 unresolved_reloc = TRUE;
7890 break;
7891 }
bd6c6e2b 7892 addend -= SYM_VAL (sda);
3b4f3a11 7893
252b5132 7894 name = bfd_get_section_name (abfd, sec->output_section);
0112cd26 7895 if (! ((CONST_STRNEQ (name, ".sdata")
5f819128 7896 && (name[6] == 0 || name[6] == '.'))
0112cd26 7897 || (CONST_STRNEQ (name, ".sbss")
5f819128 7898 && (name[5] == 0 || name[5] == '.'))))
252b5132 7899 {
55fd94b0 7900 (*_bfd_error_handler)
d003868e 7901 (_("%B: the target (%s) of a %s relocation is "
55fd94b0 7902 "in the wrong output section (%s)"),
d003868e 7903 input_bfd,
55fd94b0
AM
7904 sym_name,
7905 howto->name,
7906 name);
252b5132 7907 }
252b5132
RH
7908 }
7909 break;
7910
70bccea4 7911 /* Relocate against _SDA2_BASE_. */
7619e7c7 7912 case R_PPC_EMB_SDA2REL:
252b5132
RH
7913 {
7914 const char *name;
bd6c6e2b 7915 struct elf_link_hash_entry *sda = htab->sdata[1].sym;
252b5132 7916
bd6c6e2b
AM
7917 if (sec == NULL
7918 || sec->output_section == NULL
7919 || !is_static_defined (sda))
3b4f3a11
AM
7920 {
7921 unresolved_reloc = TRUE;
7922 break;
7923 }
bd6c6e2b 7924 addend -= SYM_VAL (sda);
3b4f3a11 7925
252b5132 7926 name = bfd_get_section_name (abfd, sec->output_section);
0112cd26
NC
7927 if (! (CONST_STRNEQ (name, ".sdata2")
7928 || CONST_STRNEQ (name, ".sbss2")))
252b5132 7929 {
55fd94b0 7930 (*_bfd_error_handler)
d003868e 7931 (_("%B: the target (%s) of a %s relocation is "
55fd94b0 7932 "in the wrong output section (%s)"),
d003868e 7933 input_bfd,
55fd94b0
AM
7934 sym_name,
7935 howto->name,
7936 name);
252b5132 7937 }
252b5132
RH
7938 }
7939 break;
7940
70bccea4 7941 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
7619e7c7
AM
7942 case R_PPC_EMB_SDA21:
7943 case R_PPC_EMB_RELSDA:
252b5132
RH
7944 {
7945 const char *name;
7946 int reg;
bd6c6e2b 7947 struct elf_link_hash_entry *sda = NULL;
252b5132 7948
3b4f3a11
AM
7949 if (sec == NULL || sec->output_section == NULL)
7950 {
7951 unresolved_reloc = TRUE;
7952 break;
7953 }
7954
252b5132 7955 name = bfd_get_section_name (abfd, sec->output_section);
0112cd26 7956 if (((CONST_STRNEQ (name, ".sdata")
5f819128 7957 && (name[6] == 0 || name[6] == '.'))
0112cd26 7958 || (CONST_STRNEQ (name, ".sbss")
5f819128 7959 && (name[5] == 0 || name[5] == '.'))))
252b5132
RH
7960 {
7961 reg = 13;
bd6c6e2b 7962 sda = htab->sdata[0].sym;
252b5132 7963 }
0112cd26
NC
7964 else if (CONST_STRNEQ (name, ".sdata2")
7965 || CONST_STRNEQ (name, ".sbss2"))
252b5132
RH
7966 {
7967 reg = 2;
bd6c6e2b 7968 sda = htab->sdata[1].sym;
252b5132 7969 }
8da6118f
KH
7970 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
7971 || strcmp (name, ".PPC.EMB.sbss0") == 0)
252b5132
RH
7972 {
7973 reg = 0;
7974 }
252b5132
RH
7975 else
7976 {
55fd94b0 7977 (*_bfd_error_handler)
d003868e 7978 (_("%B: the target (%s) of a %s relocation is "
55fd94b0 7979 "in the wrong output section (%s)"),
d003868e 7980 input_bfd,
55fd94b0
AM
7981 sym_name,
7982 howto->name,
7983 name);
252b5132
RH
7984
7985 bfd_set_error (bfd_error_bad_value);
b34976b6 7986 ret = FALSE;
252b5132
RH
7987 continue;
7988 }
7989
bd6c6e2b
AM
7990 if (sda != NULL)
7991 {
7992 if (!is_static_defined (sda))
7993 {
7994 unresolved_reloc = TRUE;
7995 break;
7996 }
7997 addend -= SYM_VAL (sda);
7998 }
7999
252b5132 8000 if (r_type == R_PPC_EMB_SDA21)
91d6fa6a
NC
8001 {
8002 bfd_vma insn; /* Fill in register field. */
8003
7619e7c7 8004 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
252b5132 8005 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
7619e7c7 8006 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
252b5132
RH
8007 }
8008 }
8009 break;
8010
70bccea4 8011 /* Relocate against the beginning of the section. */
7619e7c7
AM
8012 case R_PPC_SECTOFF:
8013 case R_PPC_SECTOFF_LO:
8014 case R_PPC_SECTOFF_HI:
8015 case R_PPC_SECTOFF_HA:
3b4f3a11
AM
8016 if (sec == NULL || sec->output_section == NULL)
8017 {
8018 unresolved_reloc = TRUE;
8019 break;
8020 }
252b5132 8021 addend -= sec->output_section->vma;
252b5132
RH
8022 break;
8023
70bccea4 8024 /* Negative relocations. */
7619e7c7
AM
8025 case R_PPC_EMB_NADDR32:
8026 case R_PPC_EMB_NADDR16:
8027 case R_PPC_EMB_NADDR16_LO:
8028 case R_PPC_EMB_NADDR16_HI:
8029 case R_PPC_EMB_NADDR16_HA:
8da6118f 8030 addend -= 2 * relocation;
252b5132
RH
8031 break;
8032
7619e7c7
AM
8033 case R_PPC_COPY:
8034 case R_PPC_GLOB_DAT:
8035 case R_PPC_JMP_SLOT:
8036 case R_PPC_RELATIVE:
e054468f 8037 case R_PPC_IRELATIVE:
7619e7c7
AM
8038 case R_PPC_PLT32:
8039 case R_PPC_PLTREL32:
8040 case R_PPC_PLT16_LO:
8041 case R_PPC_PLT16_HI:
8042 case R_PPC_PLT16_HA:
8043 case R_PPC_ADDR30:
8044 case R_PPC_EMB_RELSEC16:
8045 case R_PPC_EMB_RELST_LO:
8046 case R_PPC_EMB_RELST_HI:
8047 case R_PPC_EMB_RELST_HA:
8048 case R_PPC_EMB_BIT_FLD:
8049 (*_bfd_error_handler)
d003868e
AM
8050 (_("%B: relocation %s is not yet supported for symbol %s."),
8051 input_bfd,
7619e7c7
AM
8052 howto->name,
8053 sym_name);
252b5132
RH
8054
8055 bfd_set_error (bfd_error_invalid_operation);
b34976b6 8056 ret = FALSE;
252b5132 8057 continue;
7619e7c7 8058 }
252b5132 8059
7619e7c7
AM
8060 /* Do any further special processing. */
8061 switch (r_type)
8062 {
8063 default:
8064 break;
8065
8066 case R_PPC_ADDR16_HA:
d7128ce4 8067 case R_PPC_REL16_HA:
7619e7c7
AM
8068 case R_PPC_SECTOFF_HA:
8069 case R_PPC_TPREL16_HA:
8070 case R_PPC_DTPREL16_HA:
7619e7c7
AM
8071 case R_PPC_EMB_NADDR16_HA:
8072 case R_PPC_EMB_RELST_HA:
8073 /* It's just possible that this symbol is a weak symbol
7b609f53 8074 that's not actually defined anywhere. In that case,
7619e7c7
AM
8075 'sec' would be NULL, and we should leave the symbol
8076 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
8077 if (sec == NULL)
8078 break;
8079 /* Fall thru */
8080
8081 case R_PPC_PLT16_HA:
8082 case R_PPC_GOT16_HA:
8083 case R_PPC_GOT_TLSGD16_HA:
8084 case R_PPC_GOT_TLSLD16_HA:
8085 case R_PPC_GOT_TPREL16_HA:
8086 case R_PPC_GOT_DTPREL16_HA:
8087 /* Add 0x10000 if sign bit in 0:15 is set.
8088 Bits 0:15 are not used. */
8089 addend += 0x8000;
7619e7c7 8090 break;
252b5132
RH
8091 }
8092
252b5132 8093#ifdef DEBUG
55fd94b0
AM
8094 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
8095 "offset = %ld, addend = %ld\n",
252b5132 8096 howto->name,
8da6118f 8097 (int) r_type,
252b5132
RH
8098 sym_name,
8099 r_symndx,
7619e7c7 8100 (long) rel->r_offset,
8da6118f 8101 (long) addend);
252b5132
RH
8102#endif
8103
7619e7c7
AM
8104 if (unresolved_reloc
8105 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 8106 && h->def_dynamic))
7619e7c7
AM
8107 {
8108 (*_bfd_error_handler)
d003868e
AM
8109 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
8110 input_bfd,
8111 input_section,
7619e7c7 8112 (long) rel->r_offset,
7b609f53 8113 howto->name,
7619e7c7
AM
8114 sym_name);
8115 ret = FALSE;
8116 }
8117
252b5132
RH
8118 r = _bfd_final_link_relocate (howto,
8119 input_bfd,
8120 input_section,
8121 contents,
7619e7c7 8122 rel->r_offset,
252b5132
RH
8123 relocation,
8124 addend);
8125
7619e7c7 8126 if (r != bfd_reloc_ok)
252b5132 8127 {
7619e7c7 8128 if (r == bfd_reloc_overflow)
252b5132 8129 {
7619e7c7
AM
8130 if (warned)
8131 continue;
8132 if (h != NULL
8133 && h->root.type == bfd_link_hash_undefweak
dc1bc0c9
RH
8134 && howto->pc_relative)
8135 {
8136 /* Assume this is a call protected by other code that
8137 detect the symbol is undefined. If this is the case,
8138 we can safely ignore the overflow. If not, the
8139 program is hosed anyway, and a little warning isn't
8140 going to help. */
252b5132 8141
dc1bc0c9
RH
8142 continue;
8143 }
252b5132 8144
7619e7c7 8145 if (! (*info->callbacks->reloc_overflow) (info,
dfeffb9f 8146 (h ? &h->root : NULL),
7619e7c7
AM
8147 sym_name,
8148 howto->name,
8149 rel->r_addend,
8150 input_bfd,
8151 input_section,
8152 rel->r_offset))
8153 return FALSE;
dc1bc0c9
RH
8154 }
8155 else
8156 {
7619e7c7 8157 (*_bfd_error_handler)
d003868e
AM
8158 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
8159 input_bfd, input_section,
7b609f53 8160 (long) rel->r_offset, howto->name, sym_name, (int) r);
7619e7c7 8161 ret = FALSE;
252b5132
RH
8162 }
8163 }
8164 }
8165
252b5132
RH
8166#ifdef DEBUG
8167 fprintf (stderr, "\n");
8168#endif
8169
8170 return ret;
8171}
252b5132 8172\f
25dbc73a
AM
8173/* Finish up dynamic symbol handling. We set the contents of various
8174 dynamic sections here. */
c5fccbec 8175
b34976b6 8176static bfd_boolean
25dbc73a
AM
8177ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
8178 struct bfd_link_info *info,
8179 struct elf_link_hash_entry *h,
8180 Elf_Internal_Sym *sym)
e1a9cb8e 8181{
25dbc73a 8182 struct ppc_elf_link_hash_table *htab;
a6aa5195
AM
8183 struct plt_entry *ent;
8184 bfd_boolean doneone;
e1a9cb8e 8185
25dbc73a
AM
8186#ifdef DEBUG
8187 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
8188 h->root.root.string);
8189#endif
e1a9cb8e 8190
25dbc73a
AM
8191 htab = ppc_elf_hash_table (info);
8192 BFD_ASSERT (htab->elf.dynobj != NULL);
e1a9cb8e 8193
a6aa5195
AM
8194 doneone = FALSE;
8195 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8196 if (ent->plt.offset != (bfd_vma) -1)
8197 {
8198 if (!doneone)
8199 {
8200 Elf_Internal_Rela rela;
8201 bfd_byte *loc;
8202 bfd_vma reloc_index;
e1a9cb8e 8203
e054468f 8204 if (htab->plt_type == PLT_NEW
25f23106
AM
8205 || !htab->elf.dynamic_sections_created
8206 || h->dynindx == -1)
9d8504b1
PB
8207 reloc_index = ent->plt.offset / 4;
8208 else
8209 {
8210 reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
8211 / htab->plt_slot_size);
8212 if (reloc_index > PLT_NUM_SINGLE_ENTRIES
4a3dc543 8213 && htab->plt_type == PLT_OLD)
9d8504b1
PB
8214 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
8215 }
8216
a6aa5195
AM
8217 /* This symbol has an entry in the procedure linkage table.
8218 Set it up. */
e054468f 8219 if (htab->plt_type == PLT_VXWORKS
25f23106
AM
8220 && htab->elf.dynamic_sections_created
8221 && h->dynindx != -1)
a6aa5195 8222 {
9d8504b1
PB
8223 bfd_vma got_offset;
8224 const bfd_vma *plt_entry;
8225
8226 /* The first three entries in .got.plt are reserved. */
8227 got_offset = (reloc_index + 3) * 4;
8228
8229 /* Use the right PLT. */
8230 plt_entry = info->shared ? ppc_elf_vxworks_pic_plt_entry
8231 : ppc_elf_vxworks_plt_entry;
8232
8233 /* Fill in the .plt on VxWorks. */
8234 if (info->shared)
8235 {
9d8504b1 8236 bfd_put_32 (output_bfd,
e87d4038 8237 plt_entry[0] | PPC_HA (got_offset),
9d8504b1
PB
8238 htab->plt->contents + ent->plt.offset + 0);
8239 bfd_put_32 (output_bfd,
e87d4038 8240 plt_entry[1] | PPC_LO (got_offset),
9d8504b1
PB
8241 htab->plt->contents + ent->plt.offset + 4);
8242 }
8243 else
8244 {
e87d4038 8245 bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
9d8504b1
PB
8246
8247 bfd_put_32 (output_bfd,
e87d4038 8248 plt_entry[0] | PPC_HA (got_loc),
9d8504b1
PB
8249 htab->plt->contents + ent->plt.offset + 0);
8250 bfd_put_32 (output_bfd,
e87d4038 8251 plt_entry[1] | PPC_LO (got_loc),
9d8504b1
PB
8252 htab->plt->contents + ent->plt.offset + 4);
8253 }
8254
8255 bfd_put_32 (output_bfd, plt_entry[2],
8256 htab->plt->contents + ent->plt.offset + 8);
8257 bfd_put_32 (output_bfd, plt_entry[3],
8258 htab->plt->contents + ent->plt.offset + 12);
8259
8260 /* This instruction is an immediate load. The value loaded is
8261 the byte offset of the R_PPC_JMP_SLOT relocation from the
8262 start of the .rela.plt section. The value is stored in the
8263 low-order 16 bits of the load instruction. */
8264 /* NOTE: It appears that this is now an index rather than a
8265 prescaled offset. */
8266 bfd_put_32 (output_bfd,
8267 plt_entry[4] | reloc_index,
8268 htab->plt->contents + ent->plt.offset + 16);
8269 /* This instruction is a PC-relative branch whose target is
8270 the start of the PLT section. The address of this branch
8271 instruction is 20 bytes beyond the start of this PLT entry.
8272 The address is encoded in bits 6-29, inclusive. The value
8273 stored is right-shifted by two bits, permitting a 26-bit
8274 offset. */
8275 bfd_put_32 (output_bfd,
8276 (plt_entry[5]
8277 | (-(ent->plt.offset + 20) & 0x03fffffc)),
8278 htab->plt->contents + ent->plt.offset + 20);
8279 bfd_put_32 (output_bfd, plt_entry[6],
8280 htab->plt->contents + ent->plt.offset + 24);
8281 bfd_put_32 (output_bfd, plt_entry[7],
8282 htab->plt->contents + ent->plt.offset + 28);
8283
8284 /* Fill in the GOT entry corresponding to this PLT slot with
8285 the address immediately after the the "bctr" instruction
8286 in this PLT entry. */
8287 bfd_put_32 (output_bfd, (htab->plt->output_section->vma
8288 + htab->plt->output_offset
8289 + ent->plt.offset + 16),
8290 htab->sgotplt->contents + got_offset);
8291
8292 if (!info->shared)
8293 {
8294 /* Fill in a couple of entries in .rela.plt.unloaded. */
8295 loc = htab->srelplt2->contents
8296 + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
8297 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
8298 * sizeof (Elf32_External_Rela));
8299
8300 /* Provide the @ha relocation for the first instruction. */
8301 rela.r_offset = (htab->plt->output_section->vma
8302 + htab->plt->output_offset
8303 + ent->plt.offset + 2);
636ce3f5 8304 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
9d8504b1
PB
8305 R_PPC_ADDR16_HA);
8306 rela.r_addend = got_offset;
8307 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8308 loc += sizeof (Elf32_External_Rela);
8309
8310 /* Provide the @l relocation for the second instruction. */
8311 rela.r_offset = (htab->plt->output_section->vma
8312 + htab->plt->output_offset
8313 + ent->plt.offset + 6);
636ce3f5 8314 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
9d8504b1
PB
8315 R_PPC_ADDR16_LO);
8316 rela.r_addend = got_offset;
8317 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8318 loc += sizeof (Elf32_External_Rela);
8319
8320 /* Provide a relocation for the GOT entry corresponding to this
8321 PLT slot. Point it at the middle of the .plt entry. */
8322 rela.r_offset = (htab->sgotplt->output_section->vma
8323 + htab->sgotplt->output_offset
8324 + got_offset);
7325306f 8325 rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
9d8504b1
PB
8326 R_PPC_ADDR32);
8327 rela.r_addend = ent->plt.offset + 16;
8328 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8329 }
8330
8331 /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
8332 In particular, the offset for the relocation is not the
8333 address of the PLT entry for this function, as specified
8334 by the ABI. Instead, the offset is set to the address of
8335 the GOT slot for this function. See EABI 4.4.4.1. */
8336 rela.r_offset = (htab->sgotplt->output_section->vma
8337 + htab->sgotplt->output_offset
8338 + got_offset);
8339
a6aa5195
AM
8340 }
8341 else
8342 {
e054468f 8343 asection *splt = htab->plt;
25f23106
AM
8344 if (!htab->elf.dynamic_sections_created
8345 || h->dynindx == -1)
e054468f
AM
8346 splt = htab->iplt;
8347
8348 rela.r_offset = (splt->output_section->vma
8349 + splt->output_offset
9d8504b1 8350 + ent->plt.offset);
e054468f 8351 if (htab->plt_type == PLT_OLD
25f23106
AM
8352 || !htab->elf.dynamic_sections_created
8353 || h->dynindx == -1)
9d8504b1
PB
8354 {
8355 /* We don't need to fill in the .plt. The ppc dynamic
8356 linker will fill it in. */
8357 }
8358 else
8359 {
8360 bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
8361 + htab->glink->output_section->vma
8362 + htab->glink->output_offset);
8363 bfd_put_32 (output_bfd, val,
e054468f 8364 splt->contents + ent->plt.offset);
9d8504b1 8365 }
a6aa5195 8366 }
e1a9cb8e 8367
a6aa5195 8368 /* Fill in the entry in the .rela.plt section. */
a6aa5195 8369 rela.r_addend = 0;
e054468f
AM
8370 if (!htab->elf.dynamic_sections_created
8371 || h->dynindx == -1)
8372 {
8373 BFD_ASSERT (h->type == STT_GNU_IFUNC
8374 && h->def_regular
8375 && (h->root.type == bfd_link_hash_defined
8376 || h->root.type == bfd_link_hash_defweak));
8377 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
8378 rela.r_addend = SYM_VAL (h);
8379 }
8380 else
8381 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
e1a9cb8e 8382
25f23106
AM
8383 if (!htab->elf.dynamic_sections_created
8384 || h->dynindx == -1)
8385 loc = (htab->reliplt->contents
8386 + (htab->reliplt->reloc_count++
8387 * sizeof (Elf32_External_Rela)));
e054468f 8388 else
25f23106
AM
8389 loc = (htab->relplt->contents
8390 + reloc_index * sizeof (Elf32_External_Rela));
a6aa5195 8391 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
e1a9cb8e 8392
a6aa5195
AM
8393 if (!h->def_regular)
8394 {
0eb4a168
AM
8395 /* Mark the symbol as undefined, rather than as
8396 defined in the .plt section. Leave the value if
8397 there were any relocations where pointer equality
8398 matters (this is a clue for the dynamic linker, to
8399 make function pointer comparisons work between an
8400 application and shared library), otherwise set it
8401 to zero. */
a6aa5195 8402 sym->st_shndx = SHN_UNDEF;
0eb4a168 8403 if (!h->pointer_equality_needed)
a6aa5195 8404 sym->st_value = 0;
0eb4a168
AM
8405 else if (!h->ref_regular_nonweak)
8406 {
625af618
AM
8407 /* This breaks function pointer comparisons, but
8408 that is better than breaking tests for a NULL
0eb4a168 8409 function pointer. */
0eb4a168
AM
8410 sym->st_value = 0;
8411 }
a6aa5195 8412 }
de972ffa
AM
8413 else if (h->type == STT_GNU_IFUNC
8414 && !info->shared)
8415 {
8416 /* Set the value of ifunc symbols in a non-pie
8417 executable to the glink entry. This is to avoid
8418 text relocations. We can't do this for ifunc in
8419 allocate_dynrelocs, as we do for normal dynamic
8420 function symbols with plt entries, because we need
8421 to keep the original value around for the ifunc
8422 relocation. */
8423 sym->st_shndx = (_bfd_elf_section_from_bfd_section
8424 (output_bfd, htab->glink->output_section));
8425 sym->st_value = (ent->glink_offset +
8426 htab->glink->output_offset
8427 + htab->glink->output_section->vma);
8428 }
a6aa5195
AM
8429 doneone = TRUE;
8430 }
e1a9cb8e 8431
e054468f 8432 if (htab->plt_type == PLT_NEW
25f23106
AM
8433 || !htab->elf.dynamic_sections_created
8434 || h->dynindx == -1)
a6aa5195 8435 {
a7f2871e 8436 unsigned char *p;
e054468f 8437 asection *splt = htab->plt;
25f23106
AM
8438 if (!htab->elf.dynamic_sections_created
8439 || h->dynindx == -1)
e054468f 8440 splt = htab->iplt;
e1a9cb8e 8441
a7f2871e
AM
8442 p = (unsigned char *) htab->glink->contents + ent->glink_offset;
8443
8444 if (h == htab->tls_get_addr && !htab->no_tls_get_addr_opt)
8445 {
8446 bfd_put_32 (output_bfd, LWZ_11_3, p);
8447 p += 4;
8448 bfd_put_32 (output_bfd, LWZ_12_3 + 4, p);
8449 p += 4;
8450 bfd_put_32 (output_bfd, MR_0_3, p);
8451 p += 4;
8452 bfd_put_32 (output_bfd, CMPWI_11_0, p);
8453 p += 4;
8454 bfd_put_32 (output_bfd, ADD_3_12_2, p);
8455 p += 4;
8456 bfd_put_32 (output_bfd, BEQLR, p);
8457 p += 4;
8458 bfd_put_32 (output_bfd, MR_3_0, p);
8459 p += 4;
8460 bfd_put_32 (output_bfd, NOP, p);
8461 p += 4;
8462 }
8463
8464 write_glink_stub (ent, splt, p, info);
e1a9cb8e 8465
a877a2b6 8466 if (!info->shared)
e054468f
AM
8467 /* We only need one non-PIC glink stub. */
8468 break;
a6aa5195
AM
8469 }
8470 else
8471 break;
8472 }
e1a9cb8e 8473
25dbc73a
AM
8474 if (h->needs_copy)
8475 {
8476 asection *s;
8477 Elf_Internal_Rela rela;
8478 bfd_byte *loc;
e1a9cb8e 8479
25dbc73a 8480 /* This symbols needs a copy reloc. Set it up. */
e1a9cb8e 8481
25dbc73a
AM
8482#ifdef DEBUG
8483 fprintf (stderr, ", copy");
8484#endif
e1a9cb8e 8485
25dbc73a 8486 BFD_ASSERT (h->dynindx != -1);
86bbe32f 8487
4dc4a9a5 8488 if (ppc_elf_hash_entry (h)->has_sda_refs)
25dbc73a
AM
8489 s = htab->relsbss;
8490 else
8491 s = htab->relbss;
8492 BFD_ASSERT (s != NULL);
e1a9cb8e 8493
e87d4038 8494 rela.r_offset = SYM_VAL (h);
25dbc73a
AM
8495 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
8496 rela.r_addend = 0;
8497 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
8498 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8499 }
e1a9cb8e 8500
25dbc73a
AM
8501#ifdef DEBUG
8502 fprintf (stderr, "\n");
8503#endif
e1a9cb8e 8504
25dbc73a 8505 /* Mark some specially defined symbols as absolute. */
9d8504b1
PB
8506 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
8507 || (!htab->is_vxworks
8508 && (h == htab->elf.hgot
8509 || strcmp (h->root.root.string,
8510 "_PROCEDURE_LINKAGE_TABLE_") == 0)))
25dbc73a 8511 sym->st_shndx = SHN_ABS;
e1a9cb8e 8512
25dbc73a
AM
8513 return TRUE;
8514}
8515\f
8516static enum elf_reloc_type_class
8517ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
e1a9cb8e 8518{
25dbc73a
AM
8519 switch (ELF32_R_TYPE (rela->r_info))
8520 {
8521 case R_PPC_RELATIVE:
8522 return reloc_class_relative;
8523 case R_PPC_REL24:
8524 case R_PPC_ADDR24:
8525 case R_PPC_JMP_SLOT:
8526 return reloc_class_plt;
8527 case R_PPC_COPY:
8528 return reloc_class_copy;
8529 default:
8530 return reloc_class_normal;
8531 }
e1a9cb8e 8532}
25dbc73a
AM
8533\f
8534/* Finish up the dynamic sections. */
e1a9cb8e 8535
25dbc73a
AM
8536static bfd_boolean
8537ppc_elf_finish_dynamic_sections (bfd *output_bfd,
8538 struct bfd_link_info *info)
e1a9cb8e 8539{
25dbc73a 8540 asection *sdyn;
9d8504b1 8541 asection *splt;
25dbc73a 8542 struct ppc_elf_link_hash_table *htab;
1fe44d79 8543 bfd_vma got;
e87d4038
AM
8544 bfd *dynobj;
8545 bfd_boolean ret = TRUE;
e1a9cb8e 8546
25dbc73a
AM
8547#ifdef DEBUG
8548 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
8549#endif
6b0817e5 8550
25dbc73a 8551 htab = ppc_elf_hash_table (info);
9d8504b1
PB
8552 dynobj = elf_hash_table (info)->dynobj;
8553 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8554 if (htab->is_vxworks)
8555 splt = bfd_get_section_by_name (dynobj, ".plt");
8556 else
8557 splt = NULL;
e1a9cb8e 8558
1fe44d79
AM
8559 got = 0;
8560 if (htab->elf.hgot != NULL)
e87d4038 8561 got = SYM_VAL (htab->elf.hgot);
1fe44d79 8562
25dbc73a 8563 if (htab->elf.dynamic_sections_created)
e1a9cb8e 8564 {
25dbc73a 8565 Elf32_External_Dyn *dyncon, *dynconend;
e1a9cb8e 8566
25dbc73a 8567 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
86bbe32f 8568
25dbc73a
AM
8569 dyncon = (Elf32_External_Dyn *) sdyn->contents;
8570 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
8571 for (; dyncon < dynconend; dyncon++)
8572 {
8573 Elf_Internal_Dyn dyn;
8574 asection *s;
e1a9cb8e 8575
9d8504b1 8576 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
86bbe32f 8577
25dbc73a
AM
8578 switch (dyn.d_tag)
8579 {
8580 case DT_PLTGOT:
9d8504b1
PB
8581 if (htab->is_vxworks)
8582 s = htab->sgotplt;
8583 else
8584 s = htab->plt;
25dbc73a
AM
8585 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8586 break;
86bbe32f 8587
25dbc73a
AM
8588 case DT_PLTRELSZ:
8589 dyn.d_un.d_val = htab->relplt->size;
8590 break;
e1a9cb8e 8591
25dbc73a
AM
8592 case DT_JMPREL:
8593 s = htab->relplt;
8594 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8595 break;
2f89ff8d 8596
1fe44d79
AM
8597 case DT_PPC_GOT:
8598 dyn.d_un.d_ptr = got;
d7128ce4
AM
8599 break;
8600
9d8504b1
PB
8601 case DT_RELASZ:
8602 if (htab->is_vxworks)
8603 {
8604 if (htab->relplt)
8605 dyn.d_un.d_ptr -= htab->relplt->size;
8606 break;
8607 }
8608 continue;
8609
25dbc73a 8610 default:
7a2b07ff
NS
8611 if (htab->is_vxworks
8612 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
8613 break;
25dbc73a
AM
8614 continue;
8615 }
4c45e5c9 8616
25dbc73a
AM
8617 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8618 }
8619 }
4c45e5c9 8620
3b36f7e6 8621 if (htab->got != NULL)
25dbc73a 8622 {
e79f5955
AM
8623 if (htab->elf.hgot->root.u.def.section == htab->got
8624 || htab->elf.hgot->root.u.def.section == htab->sgotplt)
e87d4038 8625 {
e79f5955 8626 unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
2f89ff8d 8627
e87d4038
AM
8628 p += htab->elf.hgot->root.u.def.value;
8629 if (htab->plt_type == PLT_OLD)
e79f5955
AM
8630 {
8631 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4
8632 so that a function can easily find the address of
8633 _GLOBAL_OFFSET_TABLE_. */
8634 BFD_ASSERT (htab->elf.hgot->root.u.def.value - 4
8635 < htab->elf.hgot->root.u.def.section->size);
8636 bfd_put_32 (output_bfd, 0x4e800021, p - 4);
8637 }
3b36f7e6 8638
e87d4038 8639 if (sdyn != NULL)
e79f5955
AM
8640 {
8641 bfd_vma val = sdyn->output_section->vma + sdyn->output_offset;
8642 BFD_ASSERT (htab->elf.hgot->root.u.def.value
8643 < htab->elf.hgot->root.u.def.section->size);
8644 bfd_put_32 (output_bfd, val, p);
8645 }
e87d4038
AM
8646 }
8647 else
8648 {
8649 (*_bfd_error_handler) (_("%s not defined in linker created %s"),
8650 htab->elf.hgot->root.root.string,
e79f5955
AM
8651 (htab->sgotplt != NULL
8652 ? htab->sgotplt->name : htab->got->name));
e87d4038
AM
8653 bfd_set_error (bfd_error_bad_value);
8654 ret = FALSE;
8655 }
25dbc73a
AM
8656
8657 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
8658 }
8659
9d8504b1
PB
8660 /* Fill in the first entry in the VxWorks procedure linkage table. */
8661 if (splt && splt->size > 0)
8662 {
8663 /* Use the right PLT. */
8664 static const bfd_vma *plt_entry = NULL;
8665 plt_entry = info->shared ?
8666 ppc_elf_vxworks_pic_plt0_entry : ppc_elf_vxworks_plt0_entry;
8667
8668 if (!info->shared)
8669 {
e87d4038 8670 bfd_vma got_value = SYM_VAL (htab->elf.hgot);
9d8504b1 8671
e87d4038 8672 bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
9d8504b1 8673 splt->contents + 0);
e87d4038 8674 bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
9d8504b1
PB
8675 splt->contents + 4);
8676 }
8677 else
8678 {
8679 bfd_put_32 (output_bfd, plt_entry[0], splt->contents + 0);
8680 bfd_put_32 (output_bfd, plt_entry[1], splt->contents + 4);
8681 }
8682 bfd_put_32 (output_bfd, plt_entry[2], splt->contents + 8);
8683 bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
8684 bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
8685 bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
8686 bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
8687 bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
8688
8689 if (! info->shared)
8690 {
8691 Elf_Internal_Rela rela;
8692 bfd_byte *loc;
8693
8694 loc = htab->srelplt2->contents;
8695
8696 /* Output the @ha relocation for the first instruction. */
8697 rela.r_offset = (htab->plt->output_section->vma
8698 + htab->plt->output_offset
8699 + 2);
636ce3f5 8700 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
9d8504b1
PB
8701 rela.r_addend = 0;
8702 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8703 loc += sizeof (Elf32_External_Rela);
8704
8705 /* Output the @l relocation for the second instruction. */
8706 rela.r_offset = (htab->plt->output_section->vma
8707 + htab->plt->output_offset
8708 + 6);
636ce3f5 8709 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
9d8504b1
PB
8710 rela.r_addend = 0;
8711 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8712 loc += sizeof (Elf32_External_Rela);
8713
8714 /* Fix up the remaining relocations. They may have the wrong
8715 symbol index for _G_O_T_ or _P_L_T_ depending on the order
8716 in which symbols were output. */
8717 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
8718 {
8719 Elf_Internal_Rela rel;
8720
8721 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
636ce3f5 8722 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
9d8504b1
PB
8723 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8724 loc += sizeof (Elf32_External_Rela);
8725
8726 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
636ce3f5 8727 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
9d8504b1
PB
8728 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8729 loc += sizeof (Elf32_External_Rela);
8730
8731 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7325306f 8732 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
9d8504b1
PB
8733 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8734 loc += sizeof (Elf32_External_Rela);
8735 }
8736 }
8737 }
8738
e054468f
AM
8739 if (htab->glink != NULL
8740 && htab->glink->contents != NULL
8741 && htab->elf.dynamic_sections_created)
d7128ce4
AM
8742 {
8743 unsigned char *p;
8744 unsigned char *endp;
a6aa5195 8745 bfd_vma res0;
d7128ce4 8746 unsigned int i;
a6aa5195
AM
8747
8748 /*
8749 * PIC glink code is the following:
8750 *
8751 * # ith PLT code stub.
8752 * addis 11,30,(plt+(i-1)*4-got)@ha
8753 * lwz 11,(plt+(i-1)*4-got)@l(11)
8754 * mtctr 11
8755 * bctr
8756 *
8757 * # A table of branches, one for each plt entry.
176a0d42 8758 * # The idea is that the plt call stub loads ctr and r11 with these
a6aa5195
AM
8759 * # addresses, so (r11 - res_0) gives the plt index * 4.
8760 * res_0: b PLTresolve
8761 * res_1: b PLTresolve
8762 * .
8763 * # Some number of entries towards the end can be nops
8764 * res_n_m3: nop
8765 * res_n_m2: nop
8766 * res_n_m1:
8767 *
8768 * PLTresolve:
8769 * addis 11,11,(1f-res_0)@ha
8770 * mflr 0
8771 * bcl 20,31,1f
8772 * 1: addi 11,11,(1b-res_0)@l
8773 * mflr 12
8774 * mtlr 0
8775 * sub 11,11,12 # r11 = index * 4
8776 * addis 12,12,(got+4-1b)@ha
8777 * lwz 0,(got+4-1b)@l(12) # got[1] address of dl_runtime_resolve
8778 * lwz 12,(got+8-1b)@l(12) # got[2] contains the map address
8779 * mtctr 0
8780 * add 0,11,11
8781 * add 11,0,11 # r11 = index * 12 = reloc offset.
8782 * bctr
8783 */
8784 static const unsigned int pic_plt_resolve[] =
8785 {
8786 ADDIS_11_11,
8787 MFLR_0,
8788 BCL_20_31,
8789 ADDI_11_11,
8790 MFLR_12,
8791 MTLR_0,
8792 SUB_11_11_12,
8793 ADDIS_12_12,
8794 LWZ_0_12,
8795 LWZ_12_12,
8796 MTCTR_0,
8797 ADD_0_11_11,
8798 ADD_11_0_11,
8799 BCTR,
8800 NOP,
8801 NOP
8802 };
8803
176a0d42
AM
8804 /*
8805 * Non-PIC glink code is a little simpler.
8806 *
8807 * # ith PLT code stub.
8808 * lis 11,(plt+(i-1)*4)@ha
8809 * lwz 11,(plt+(i-1)*4)@l(11)
8810 * mtctr 11
8811 * bctr
8812 *
8813 * The branch table is the same, then comes
8814 *
8815 * PLTresolve:
8816 * lis 12,(got+4)@ha
8817 * addis 11,11,(-res_0)@ha
8818 * lwz 0,(got+4)@l(12) # got[1] address of dl_runtime_resolve
8819 * addi 11,11,(-res_0)@l # r11 = index * 4
8820 * mtctr 0
8821 * add 0,11,11
8822 * lwz 12,(got+8)@l(12) # got[2] contains the map address
8823 * add 11,0,11 # r11 = index * 12 = reloc offset.
8824 * bctr
8825 */
d7128ce4
AM
8826 static const unsigned int plt_resolve[] =
8827 {
a6aa5195
AM
8828 LIS_12,
8829 ADDIS_11_11,
8830 LWZ_0_12,
8831 ADDI_11_11,
d7128ce4 8832 MTCTR_0,
7e8aeb9a 8833 ADD_0_11_11,
a6aa5195 8834 LWZ_12_12,
7e8aeb9a 8835 ADD_11_0_11,
d7128ce4
AM
8836 BCTR,
8837 NOP,
8838 NOP,
a6aa5195
AM
8839 NOP,
8840 NOP,
8841 NOP,
8842 NOP,
d7128ce4
AM
8843 NOP
8844 };
8845
a6aa5195
AM
8846 if (ARRAY_SIZE (pic_plt_resolve) != GLINK_PLTRESOLVE / 4)
8847 abort ();
8848 if (ARRAY_SIZE (plt_resolve) != GLINK_PLTRESOLVE / 4)
7e8aeb9a
AM
8849 abort ();
8850
a6aa5195 8851 /* Build the branch table, one for each plt entry (less one),
86b9da88 8852 and perhaps some padding. */
a6aa5195
AM
8853 p = htab->glink->contents;
8854 p += htab->glink_pltresolve;
86b9da88
AM
8855 endp = htab->glink->contents;
8856 endp += htab->glink->size - GLINK_PLTRESOLVE;
8857 while (p < endp - 8 * 4)
8858 {
8859 bfd_put_32 (output_bfd, B + endp - p, p);
8860 p += 4;
8861 }
8862 while (p < endp)
8863 {
8864 bfd_put_32 (output_bfd, NOP, p);
8865 p += 4;
8866 }
8867
7e8aeb9a 8868 res0 = (htab->glink_pltresolve
86b9da88
AM
8869 + htab->glink->output_section->vma
8870 + htab->glink->output_offset);
8871
8872 /* Last comes the PLTresolve stub. */
a877a2b6 8873 if (info->shared)
7e8aeb9a 8874 {
a6aa5195 8875 bfd_vma bcl;
d7128ce4 8876
a6aa5195 8877 for (i = 0; i < ARRAY_SIZE (pic_plt_resolve); i++)
7e8aeb9a 8878 {
a6aa5195 8879 bfd_put_32 (output_bfd, pic_plt_resolve[i], p);
7e8aeb9a
AM
8880 p += 4;
8881 }
a6aa5195
AM
8882 p -= 4 * ARRAY_SIZE (pic_plt_resolve);
8883
8884 bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
8885 + htab->glink->output_section->vma
8886 + htab->glink->output_offset);
8887
8888 bfd_put_32 (output_bfd,
8889 ADDIS_11_11 + PPC_HA (bcl - res0), p + 0*4);
8890 bfd_put_32 (output_bfd,
8891 ADDI_11_11 + PPC_LO (bcl - res0), p + 3*4);
8892 bfd_put_32 (output_bfd,
8893 ADDIS_12_12 + PPC_HA (got + 4 - bcl), p + 7*4);
8894 if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
8895 {
8896 bfd_put_32 (output_bfd,
8897 LWZ_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8898 bfd_put_32 (output_bfd,
8899 LWZ_12_12 + PPC_LO (got + 8 - bcl), p + 9*4);
8900 }
8901 else
8902 {
8903 bfd_put_32 (output_bfd,
8904 LWZU_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8905 bfd_put_32 (output_bfd,
8906 LWZ_12_12 + 4, p + 9*4);
8907 }
7e8aeb9a
AM
8908 }
8909 else
d7128ce4 8910 {
a6aa5195 8911 for (i = 0; i < ARRAY_SIZE (plt_resolve); i++)
7e8aeb9a
AM
8912 {
8913 bfd_put_32 (output_bfd, plt_resolve[i], p);
8914 p += 4;
8915 }
a6aa5195
AM
8916 p -= 4 * ARRAY_SIZE (plt_resolve);
8917
8918 bfd_put_32 (output_bfd,
8919 LIS_12 + PPC_HA (got + 4), p + 0*4);
8920 bfd_put_32 (output_bfd,
8921 ADDIS_11_11 + PPC_HA (-res0), p + 1*4);
8922 bfd_put_32 (output_bfd,
8923 ADDI_11_11 + PPC_LO (-res0), p + 3*4);
8924 if (PPC_HA (got + 4) == PPC_HA (got + 8))
8925 {
8926 bfd_put_32 (output_bfd,
8927 LWZ_0_12 + PPC_LO (got + 4), p + 2*4);
8928 bfd_put_32 (output_bfd,
8929 LWZ_12_12 + PPC_LO (got + 8), p + 6*4);
8930 }
8931 else
8932 {
8933 bfd_put_32 (output_bfd,
8934 LWZU_0_12 + PPC_LO (got + 4), p + 2*4);
8935 bfd_put_32 (output_bfd,
8936 LWZ_12_12 + 4, p + 6*4);
8937 }
d7128ce4 8938 }
d7128ce4
AM
8939 }
8940
e87d4038 8941 return ret;
25dbc73a 8942}
e1a9cb8e 8943\f
252b5132
RH
8944#define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
8945#define TARGET_LITTLE_NAME "elf32-powerpcle"
8946#define TARGET_BIG_SYM bfd_elf32_powerpc_vec
8947#define TARGET_BIG_NAME "elf32-powerpc"
8948#define ELF_ARCH bfd_arch_powerpc
8949#define ELF_MACHINE_CODE EM_PPC
d0facd1b
NC
8950#ifdef __QNXTARGET__
8951#define ELF_MAXPAGESIZE 0x1000
8952#else
252b5132 8953#define ELF_MAXPAGESIZE 0x10000
d0facd1b 8954#endif
b1342370 8955#define ELF_MINPAGESIZE 0x1000
24718e3b 8956#define ELF_COMMONPAGESIZE 0x1000
252b5132
RH
8957#define elf_info_to_howto ppc_elf_info_to_howto
8958
8959#ifdef EM_CYGNUS_POWERPC
8960#define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
8961#endif
8962
8963#ifdef EM_PPC_OLD
8964#define ELF_MACHINE_ALT2 EM_PPC_OLD
8965#endif
8966
8967#define elf_backend_plt_not_loaded 1
252b5132 8968#define elf_backend_can_gc_sections 1
51b64d56 8969#define elf_backend_can_refcount 1
b491616a 8970#define elf_backend_rela_normal 1
252b5132 8971
43c40ab2 8972#define bfd_elf32_mkobject ppc_elf_mkobject
252b5132 8973#define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
70bccea4 8974#define bfd_elf32_bfd_relax_section ppc_elf_relax_section
252b5132 8975#define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
157090f7 8976#define bfd_elf32_bfd_reloc_name_lookup ppc_elf_reloc_name_lookup
252b5132 8977#define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
70bccea4 8978#define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
468392fb 8979#define bfd_elf32_get_synthetic_symtab ppc_elf_get_synthetic_symtab
252b5132 8980
feee612b 8981#define elf_backend_object_p ppc_elf_object_p
252b5132
RH
8982#define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
8983#define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
8984#define elf_backend_section_from_shdr ppc_elf_section_from_shdr
8985#define elf_backend_relocate_section ppc_elf_relocate_section
8986#define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
8987#define elf_backend_check_relocs ppc_elf_check_relocs
7fce784e 8988#define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
252b5132
RH
8989#define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
8990#define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
8991#define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
0eb4a168 8992#define elf_backend_hash_symbol ppc_elf_hash_symbol
252b5132
RH
8993#define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
8994#define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
8995#define elf_backend_fake_sections ppc_elf_fake_sections
8996#define elf_backend_additional_program_headers ppc_elf_additional_program_headers
c5fccbec
DJ
8997#define elf_backend_grok_prstatus ppc_elf_grok_prstatus
8998#define elf_backend_grok_psinfo ppc_elf_grok_psinfo
183e98be 8999#define elf_backend_write_core_note ppc_elf_write_core_note
29c2fb7c 9000#define elf_backend_reloc_type_class ppc_elf_reloc_type_class
70bccea4
AM
9001#define elf_backend_begin_write_processing ppc_elf_begin_write_processing
9002#define elf_backend_final_write_processing ppc_elf_final_write_processing
9003#define elf_backend_write_section ppc_elf_write_section
551b43fd 9004#define elf_backend_get_sec_type_attr ppc_elf_get_sec_type_attr
4c45e5c9 9005#define elf_backend_plt_sym_val ppc_elf_plt_sym_val
8a696751 9006#define elf_backend_action_discarded ppc_elf_action_discarded
74541ad4 9007#define elf_backend_init_index_section _bfd_elf_init_1_index_section
e054468f 9008#define elf_backend_post_process_headers _bfd_elf_set_osabi
252b5132
RH
9009
9010#include "elf32-target.h"
9d8504b1
PB
9011
9012/* VxWorks Target */
9013
9014#undef TARGET_LITTLE_SYM
9015#undef TARGET_LITTLE_NAME
9016
9017#undef TARGET_BIG_SYM
9018#define TARGET_BIG_SYM bfd_elf32_powerpc_vxworks_vec
9019#undef TARGET_BIG_NAME
9020#define TARGET_BIG_NAME "elf32-powerpc-vxworks"
9021
d5e3d971
PB
9022/* VxWorks uses the elf default section flags for .plt. */
9023static const struct bfd_elf_special_section *
9024ppc_elf_vxworks_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
9d8504b1 9025{
d5e3d971
PB
9026 if (sec->name == NULL)
9027 return NULL;
9028
9029 if (strcmp (sec->name, ".plt") == 0)
9030 return _bfd_elf_get_sec_type_attr (abfd, sec);
9031
9032 return ppc_elf_get_sec_type_attr (abfd, sec);
9033}
9d8504b1
PB
9034
9035/* Like ppc_elf_link_hash_table_create, but overrides
9036 appropriately for VxWorks. */
9037static struct bfd_link_hash_table *
9038ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
9039{
9040 struct bfd_link_hash_table *ret;
9041
9042 ret = ppc_elf_link_hash_table_create (abfd);
9043 if (ret)
9044 {
9045 struct ppc_elf_link_hash_table *htab
9046 = (struct ppc_elf_link_hash_table *)ret;
9047 htab->is_vxworks = 1;
4a3dc543 9048 htab->plt_type = PLT_VXWORKS;
9d8504b1
PB
9049 htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
9050 htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
9051 htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
9052 }
9053 return ret;
9054}
9055
9056/* Tweak magic VxWorks symbols as they are loaded. */
9057static bfd_boolean
9058ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
9059 struct bfd_link_info *info,
9060 Elf_Internal_Sym *sym,
9061 const char **namep ATTRIBUTE_UNUSED,
9062 flagword *flagsp ATTRIBUTE_UNUSED,
9063 asection **secp,
9064 bfd_vma *valp)
9065{
9066 if (!elf_vxworks_add_symbol_hook(abfd, info, sym,namep, flagsp, secp,
9067 valp))
9068 return FALSE;
9069
9070 return ppc_elf_add_symbol_hook(abfd, info, sym,namep, flagsp, secp, valp);
9071}
9072
9d8504b1
PB
9073static void
9074ppc_elf_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
9075{
9076 ppc_elf_final_write_processing(abfd, linker);
9077 elf_vxworks_final_write_processing(abfd, linker);
9078}
9079
9080/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
9081 define it. */
9082#undef elf_backend_want_plt_sym
9083#define elf_backend_want_plt_sym 1
9084#undef elf_backend_want_got_plt
9085#define elf_backend_want_got_plt 1
9086#undef elf_backend_got_symbol_offset
9087#define elf_backend_got_symbol_offset 0
9088#undef elf_backend_plt_not_loaded
9089#define elf_backend_plt_not_loaded 0
9090#undef elf_backend_plt_readonly
9091#define elf_backend_plt_readonly 1
9092#undef elf_backend_got_header_size
9093#define elf_backend_got_header_size 12
9094
468392fb
AM
9095#undef bfd_elf32_get_synthetic_symtab
9096
9d8504b1
PB
9097#undef bfd_elf32_bfd_link_hash_table_create
9098#define bfd_elf32_bfd_link_hash_table_create \
9099 ppc_elf_vxworks_link_hash_table_create
9d8504b1
PB
9100#undef elf_backend_add_symbol_hook
9101#define elf_backend_add_symbol_hook \
9102 ppc_elf_vxworks_add_symbol_hook
9103#undef elf_backend_link_output_symbol_hook
9104#define elf_backend_link_output_symbol_hook \
9c72ff84 9105 elf_vxworks_link_output_symbol_hook
9d8504b1
PB
9106#undef elf_backend_final_write_processing
9107#define elf_backend_final_write_processing \
9108 ppc_elf_vxworks_final_write_processing
d5e3d971
PB
9109#undef elf_backend_get_sec_type_attr
9110#define elf_backend_get_sec_type_attr \
9111 ppc_elf_vxworks_get_sec_type_attr
9d8504b1
PB
9112#undef elf_backend_emit_relocs
9113#define elf_backend_emit_relocs \
9114 elf_vxworks_emit_relocs
9115
9116#undef elf32_bed
9117#define elf32_bed ppc_elf_vxworks_bed
e054468f 9118#undef elf_backend_post_process_headers
9d8504b1
PB
9119
9120#include "elf32-target.h"
This page took 1.247534 seconds and 4 git commands to generate.