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