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