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