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