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