Update the FSF address in the copyright/GPL notice
[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,
264a1fe0 3 2004, 2005 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
10 the Free Software Foundation; either version 2 of the License, or
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
AM
19 along with this program; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
53e09e0a 21 Boston, MA 02110-1301, USA. */
252b5132
RH
22
23/* This file is based on a preliminary PowerPC ELF ABI. The
24 information may not match the final PowerPC ELF ABI. It includes
25 suggestions from the in-progress Embedded PowerPC ABI, and that
26 information may also not match. */
27
28#include "bfd.h"
29#include "sysdep.h"
30#include "bfdlink.h"
31#include "libbfd.h"
32#include "elf-bfd.h"
33#include "elf/ppc.h"
7619e7c7 34#include "elf32-ppc.h"
252b5132 35
f0fe0e16 36/* RELA relocations are used here. */
252b5132 37
252b5132 38static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
55fd94b0 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
7619e7c7 40static bfd_reloc_status_type ppc_elf_unhandled_reloc
55fd94b0 41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
252b5132 42
70bccea4
AM
43/* Branch prediction bit for branch taken relocs. */
44#define BRANCH_PREDICT_BIT 0x200000
45/* Mask to set RA in memory instructions. */
46#define RA_REGISTER_MASK 0x001f0000
47/* Value to shift register by to insert RA. */
48#define RA_REGISTER_SHIFT 16
252b5132
RH
49
50/* The name of the dynamic interpreter. This is put in the .interp
51 section. */
252b5132
RH
52#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
53
54/* The size in bytes of an entry in the procedure linkage table. */
55#define PLT_ENTRY_SIZE 12
56/* The initial size of the plt reserved for the dynamic linker. */
57#define PLT_INITIAL_ENTRY_SIZE 72
58/* The size of the gap between entries in the PLT. */
59#define PLT_SLOT_SIZE 8
60/* The number of single-slot PLT entries (the rest use two slots). */
61#define PLT_NUM_SINGLE_ENTRIES 8192
62
7619e7c7
AM
63/* Some nop instructions. */
64#define NOP 0x60000000
65#define CROR_151515 0x4def7b82
66#define CROR_313131 0x4ffffb82
67
68/* Offset of tp and dtp pointers from start of TLS block. */
69#define TP_OFFSET 0x7000
70#define DTP_OFFSET 0x8000
7fce784e 71\f
e47cd125 72static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
252b5132 73
8da6118f 74static reloc_howto_type ppc_elf_howto_raw[] = {
252b5132
RH
75 /* This reloc does nothing. */
76 HOWTO (R_PPC_NONE, /* type */
77 0, /* rightshift */
78 2, /* size (0 = byte, 1 = short, 2 = long) */
79 32, /* bitsize */
b34976b6 80 FALSE, /* pc_relative */
252b5132
RH
81 0, /* bitpos */
82 complain_overflow_bitfield, /* complain_on_overflow */
83 bfd_elf_generic_reloc, /* special_function */
84 "R_PPC_NONE", /* name */
b34976b6 85 FALSE, /* partial_inplace */
252b5132
RH
86 0, /* src_mask */
87 0, /* dst_mask */
b34976b6 88 FALSE), /* pcrel_offset */
252b5132
RH
89
90 /* A standard 32 bit relocation. */
91 HOWTO (R_PPC_ADDR32, /* type */
92 0, /* rightshift */
93 2, /* size (0 = byte, 1 = short, 2 = long) */
94 32, /* bitsize */
b34976b6 95 FALSE, /* pc_relative */
252b5132
RH
96 0, /* bitpos */
97 complain_overflow_bitfield, /* complain_on_overflow */
98 bfd_elf_generic_reloc, /* special_function */
99 "R_PPC_ADDR32", /* name */
b34976b6 100 FALSE, /* partial_inplace */
252b5132
RH
101 0, /* src_mask */
102 0xffffffff, /* dst_mask */
b34976b6 103 FALSE), /* pcrel_offset */
252b5132
RH
104
105 /* An absolute 26 bit branch; the lower two bits must be zero.
106 FIXME: we don't check that, we just clear them. */
107 HOWTO (R_PPC_ADDR24, /* type */
108 0, /* rightshift */
109 2, /* size (0 = byte, 1 = short, 2 = long) */
110 26, /* bitsize */
b34976b6 111 FALSE, /* pc_relative */
252b5132
RH
112 0, /* bitpos */
113 complain_overflow_bitfield, /* complain_on_overflow */
114 bfd_elf_generic_reloc, /* special_function */
115 "R_PPC_ADDR24", /* name */
b34976b6 116 FALSE, /* partial_inplace */
252b5132
RH
117 0, /* src_mask */
118 0x3fffffc, /* dst_mask */
b34976b6 119 FALSE), /* pcrel_offset */
252b5132
RH
120
121 /* A standard 16 bit relocation. */
122 HOWTO (R_PPC_ADDR16, /* type */
123 0, /* rightshift */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
125 16, /* bitsize */
b34976b6 126 FALSE, /* pc_relative */
252b5132
RH
127 0, /* bitpos */
128 complain_overflow_bitfield, /* complain_on_overflow */
129 bfd_elf_generic_reloc, /* special_function */
130 "R_PPC_ADDR16", /* name */
b34976b6 131 FALSE, /* partial_inplace */
252b5132
RH
132 0, /* src_mask */
133 0xffff, /* dst_mask */
b34976b6 134 FALSE), /* pcrel_offset */
252b5132
RH
135
136 /* A 16 bit relocation without overflow. */
137 HOWTO (R_PPC_ADDR16_LO, /* type */
138 0, /* rightshift */
139 1, /* size (0 = byte, 1 = short, 2 = long) */
140 16, /* bitsize */
b34976b6 141 FALSE, /* pc_relative */
252b5132
RH
142 0, /* bitpos */
143 complain_overflow_dont,/* complain_on_overflow */
144 bfd_elf_generic_reloc, /* special_function */
145 "R_PPC_ADDR16_LO", /* name */
b34976b6 146 FALSE, /* partial_inplace */
252b5132
RH
147 0, /* src_mask */
148 0xffff, /* dst_mask */
b34976b6 149 FALSE), /* pcrel_offset */
252b5132
RH
150
151 /* The high order 16 bits of an address. */
152 HOWTO (R_PPC_ADDR16_HI, /* type */
153 16, /* rightshift */
154 1, /* size (0 = byte, 1 = short, 2 = long) */
155 16, /* bitsize */
b34976b6 156 FALSE, /* pc_relative */
252b5132
RH
157 0, /* bitpos */
158 complain_overflow_dont, /* complain_on_overflow */
159 bfd_elf_generic_reloc, /* special_function */
160 "R_PPC_ADDR16_HI", /* name */
b34976b6 161 FALSE, /* partial_inplace */
252b5132
RH
162 0, /* src_mask */
163 0xffff, /* dst_mask */
b34976b6 164 FALSE), /* pcrel_offset */
252b5132
RH
165
166 /* The high order 16 bits of an address, plus 1 if the contents of
8da6118f 167 the low 16 bits, treated as a signed number, is negative. */
252b5132
RH
168 HOWTO (R_PPC_ADDR16_HA, /* type */
169 16, /* rightshift */
170 1, /* size (0 = byte, 1 = short, 2 = long) */
171 16, /* bitsize */
b34976b6 172 FALSE, /* pc_relative */
252b5132
RH
173 0, /* bitpos */
174 complain_overflow_dont, /* complain_on_overflow */
175 ppc_elf_addr16_ha_reloc, /* special_function */
176 "R_PPC_ADDR16_HA", /* name */
b34976b6 177 FALSE, /* partial_inplace */
252b5132
RH
178 0, /* src_mask */
179 0xffff, /* dst_mask */
b34976b6 180 FALSE), /* pcrel_offset */
252b5132
RH
181
182 /* An absolute 16 bit branch; the lower two bits must be zero.
183 FIXME: we don't check that, we just clear them. */
184 HOWTO (R_PPC_ADDR14, /* type */
185 0, /* rightshift */
186 2, /* size (0 = byte, 1 = short, 2 = long) */
187 16, /* bitsize */
b34976b6 188 FALSE, /* pc_relative */
252b5132
RH
189 0, /* bitpos */
190 complain_overflow_bitfield, /* complain_on_overflow */
191 bfd_elf_generic_reloc, /* special_function */
192 "R_PPC_ADDR14", /* name */
b34976b6 193 FALSE, /* partial_inplace */
252b5132
RH
194 0, /* src_mask */
195 0xfffc, /* dst_mask */
b34976b6 196 FALSE), /* pcrel_offset */
252b5132
RH
197
198 /* An absolute 16 bit branch, for which bit 10 should be set to
199 indicate that the branch is expected to be taken. The lower two
8da6118f 200 bits must be zero. */
252b5132
RH
201 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
202 0, /* rightshift */
203 2, /* size (0 = byte, 1 = short, 2 = long) */
204 16, /* bitsize */
b34976b6 205 FALSE, /* pc_relative */
252b5132
RH
206 0, /* bitpos */
207 complain_overflow_bitfield, /* complain_on_overflow */
208 bfd_elf_generic_reloc, /* special_function */
209 "R_PPC_ADDR14_BRTAKEN",/* name */
b34976b6 210 FALSE, /* partial_inplace */
252b5132
RH
211 0, /* src_mask */
212 0xfffc, /* dst_mask */
b34976b6 213 FALSE), /* pcrel_offset */
252b5132
RH
214
215 /* An absolute 16 bit branch, for which bit 10 should be set to
216 indicate that the branch is not expected to be taken. The lower
217 two bits must be zero. */
218 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
219 0, /* rightshift */
220 2, /* size (0 = byte, 1 = short, 2 = long) */
221 16, /* bitsize */
b34976b6 222 FALSE, /* pc_relative */
252b5132
RH
223 0, /* bitpos */
224 complain_overflow_bitfield, /* complain_on_overflow */
225 bfd_elf_generic_reloc, /* special_function */
226 "R_PPC_ADDR14_BRNTAKEN",/* name */
b34976b6 227 FALSE, /* partial_inplace */
252b5132
RH
228 0, /* src_mask */
229 0xfffc, /* dst_mask */
b34976b6 230 FALSE), /* pcrel_offset */
252b5132 231
8da6118f 232 /* A relative 26 bit branch; the lower two bits must be zero. */
252b5132
RH
233 HOWTO (R_PPC_REL24, /* type */
234 0, /* rightshift */
235 2, /* size (0 = byte, 1 = short, 2 = long) */
236 26, /* bitsize */
b34976b6 237 TRUE, /* pc_relative */
252b5132
RH
238 0, /* bitpos */
239 complain_overflow_signed, /* complain_on_overflow */
240 bfd_elf_generic_reloc, /* special_function */
241 "R_PPC_REL24", /* name */
b34976b6 242 FALSE, /* partial_inplace */
252b5132
RH
243 0, /* src_mask */
244 0x3fffffc, /* dst_mask */
b34976b6 245 TRUE), /* pcrel_offset */
252b5132 246
8da6118f 247 /* A relative 16 bit branch; the lower two bits must be zero. */
252b5132
RH
248 HOWTO (R_PPC_REL14, /* type */
249 0, /* rightshift */
250 2, /* size (0 = byte, 1 = short, 2 = long) */
251 16, /* bitsize */
b34976b6 252 TRUE, /* pc_relative */
252b5132
RH
253 0, /* bitpos */
254 complain_overflow_signed, /* complain_on_overflow */
255 bfd_elf_generic_reloc, /* special_function */
256 "R_PPC_REL14", /* name */
b34976b6 257 FALSE, /* partial_inplace */
252b5132
RH
258 0, /* src_mask */
259 0xfffc, /* dst_mask */
b34976b6 260 TRUE), /* pcrel_offset */
252b5132 261
8da6118f 262 /* A relative 16 bit branch. Bit 10 should be set to indicate that
252b5132
RH
263 the branch is expected to be taken. The lower two bits must be
264 zero. */
265 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
266 0, /* rightshift */
267 2, /* size (0 = byte, 1 = short, 2 = long) */
268 16, /* bitsize */
b34976b6 269 TRUE, /* pc_relative */
252b5132
RH
270 0, /* bitpos */
271 complain_overflow_signed, /* complain_on_overflow */
272 bfd_elf_generic_reloc, /* special_function */
273 "R_PPC_REL14_BRTAKEN", /* name */
b34976b6 274 FALSE, /* partial_inplace */
252b5132
RH
275 0, /* src_mask */
276 0xfffc, /* dst_mask */
b34976b6 277 TRUE), /* pcrel_offset */
252b5132 278
8da6118f 279 /* A relative 16 bit branch. Bit 10 should be set to indicate that
252b5132
RH
280 the branch is not expected to be taken. The lower two bits must
281 be zero. */
282 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
283 0, /* rightshift */
284 2, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
b34976b6 286 TRUE, /* pc_relative */
252b5132
RH
287 0, /* bitpos */
288 complain_overflow_signed, /* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC_REL14_BRNTAKEN",/* name */
b34976b6 291 FALSE, /* partial_inplace */
252b5132
RH
292 0, /* src_mask */
293 0xfffc, /* dst_mask */
b34976b6 294 TRUE), /* pcrel_offset */
252b5132
RH
295
296 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
297 symbol. */
298 HOWTO (R_PPC_GOT16, /* type */
299 0, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 16, /* bitsize */
b34976b6 302 FALSE, /* pc_relative */
252b5132
RH
303 0, /* bitpos */
304 complain_overflow_signed, /* complain_on_overflow */
305 bfd_elf_generic_reloc, /* special_function */
306 "R_PPC_GOT16", /* name */
b34976b6 307 FALSE, /* partial_inplace */
252b5132
RH
308 0, /* src_mask */
309 0xffff, /* dst_mask */
b34976b6 310 FALSE), /* pcrel_offset */
252b5132
RH
311
312 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
313 the symbol. */
314 HOWTO (R_PPC_GOT16_LO, /* type */
315 0, /* rightshift */
316 1, /* size (0 = byte, 1 = short, 2 = long) */
317 16, /* bitsize */
b34976b6 318 FALSE, /* pc_relative */
252b5132
RH
319 0, /* bitpos */
320 complain_overflow_dont, /* complain_on_overflow */
321 bfd_elf_generic_reloc, /* special_function */
322 "R_PPC_GOT16_LO", /* name */
b34976b6 323 FALSE, /* partial_inplace */
252b5132
RH
324 0, /* src_mask */
325 0xffff, /* dst_mask */
b34976b6 326 FALSE), /* pcrel_offset */
252b5132
RH
327
328 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
329 the symbol. */
330 HOWTO (R_PPC_GOT16_HI, /* type */
331 16, /* rightshift */
332 1, /* size (0 = byte, 1 = short, 2 = long) */
333 16, /* bitsize */
b34976b6 334 FALSE, /* pc_relative */
252b5132
RH
335 0, /* bitpos */
336 complain_overflow_bitfield, /* complain_on_overflow */
337 bfd_elf_generic_reloc, /* special_function */
338 "R_PPC_GOT16_HI", /* name */
b34976b6 339 FALSE, /* partial_inplace */
252b5132
RH
340 0, /* src_mask */
341 0xffff, /* dst_mask */
b34976b6 342 FALSE), /* pcrel_offset */
252b5132
RH
343
344 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
345 the symbol. */
346 HOWTO (R_PPC_GOT16_HA, /* type */
347 16, /* rightshift */
348 1, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
b34976b6 350 FALSE, /* pc_relative */
252b5132
RH
351 0, /* bitpos */
352 complain_overflow_bitfield, /* complain_on_overflow */
353 ppc_elf_addr16_ha_reloc, /* special_function */
354 "R_PPC_GOT16_HA", /* name */
b34976b6 355 FALSE, /* partial_inplace */
252b5132
RH
356 0, /* src_mask */
357 0xffff, /* dst_mask */
b34976b6 358 FALSE), /* pcrel_offset */
252b5132
RH
359
360 /* Like R_PPC_REL24, but referring to the procedure linkage table
361 entry for the symbol. */
362 HOWTO (R_PPC_PLTREL24, /* type */
363 0, /* rightshift */
364 2, /* size (0 = byte, 1 = short, 2 = long) */
365 26, /* bitsize */
b34976b6 366 TRUE, /* pc_relative */
252b5132
RH
367 0, /* bitpos */
368 complain_overflow_signed, /* complain_on_overflow */
369 bfd_elf_generic_reloc, /* special_function */
370 "R_PPC_PLTREL24", /* name */
b34976b6 371 FALSE, /* partial_inplace */
252b5132
RH
372 0, /* src_mask */
373 0x3fffffc, /* dst_mask */
b34976b6 374 TRUE), /* pcrel_offset */
252b5132
RH
375
376 /* This is used only by the dynamic linker. The symbol should exist
377 both in the object being run and in some shared library. The
378 dynamic linker copies the data addressed by the symbol from the
379 shared library into the object, because the object being
380 run has to have the data at some particular address. */
381 HOWTO (R_PPC_COPY, /* type */
382 0, /* rightshift */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
384 32, /* bitsize */
b34976b6 385 FALSE, /* pc_relative */
252b5132
RH
386 0, /* bitpos */
387 complain_overflow_bitfield, /* complain_on_overflow */
388 bfd_elf_generic_reloc, /* special_function */
389 "R_PPC_COPY", /* name */
b34976b6 390 FALSE, /* partial_inplace */
252b5132
RH
391 0, /* src_mask */
392 0, /* dst_mask */
b34976b6 393 FALSE), /* pcrel_offset */
252b5132
RH
394
395 /* Like R_PPC_ADDR32, but used when setting global offset table
396 entries. */
397 HOWTO (R_PPC_GLOB_DAT, /* type */
398 0, /* rightshift */
399 2, /* size (0 = byte, 1 = short, 2 = long) */
400 32, /* bitsize */
b34976b6 401 FALSE, /* pc_relative */
252b5132
RH
402 0, /* bitpos */
403 complain_overflow_bitfield, /* complain_on_overflow */
404 bfd_elf_generic_reloc, /* special_function */
405 "R_PPC_GLOB_DAT", /* name */
b34976b6 406 FALSE, /* partial_inplace */
252b5132
RH
407 0, /* src_mask */
408 0xffffffff, /* dst_mask */
b34976b6 409 FALSE), /* pcrel_offset */
252b5132
RH
410
411 /* Marks a procedure linkage table entry for a symbol. */
412 HOWTO (R_PPC_JMP_SLOT, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 32, /* bitsize */
b34976b6 416 FALSE, /* pc_relative */
252b5132
RH
417 0, /* bitpos */
418 complain_overflow_bitfield, /* complain_on_overflow */
419 bfd_elf_generic_reloc, /* special_function */
420 "R_PPC_JMP_SLOT", /* name */
b34976b6 421 FALSE, /* partial_inplace */
252b5132
RH
422 0, /* src_mask */
423 0, /* dst_mask */
b34976b6 424 FALSE), /* pcrel_offset */
252b5132
RH
425
426 /* Used only by the dynamic linker. When the object is run, this
427 longword is set to the load address of the object, plus the
428 addend. */
429 HOWTO (R_PPC_RELATIVE, /* type */
430 0, /* rightshift */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
432 32, /* bitsize */
b34976b6 433 FALSE, /* pc_relative */
252b5132
RH
434 0, /* bitpos */
435 complain_overflow_bitfield, /* complain_on_overflow */
436 bfd_elf_generic_reloc, /* special_function */
437 "R_PPC_RELATIVE", /* name */
b34976b6 438 FALSE, /* partial_inplace */
252b5132
RH
439 0, /* src_mask */
440 0xffffffff, /* dst_mask */
b34976b6 441 FALSE), /* pcrel_offset */
252b5132
RH
442
443 /* Like R_PPC_REL24, but uses the value of the symbol within the
444 object rather than the final value. Normally used for
445 _GLOBAL_OFFSET_TABLE_. */
446 HOWTO (R_PPC_LOCAL24PC, /* type */
447 0, /* rightshift */
448 2, /* size (0 = byte, 1 = short, 2 = long) */
449 26, /* bitsize */
b34976b6 450 TRUE, /* pc_relative */
252b5132
RH
451 0, /* bitpos */
452 complain_overflow_signed, /* complain_on_overflow */
453 bfd_elf_generic_reloc, /* special_function */
454 "R_PPC_LOCAL24PC", /* name */
b34976b6 455 FALSE, /* partial_inplace */
252b5132
RH
456 0, /* src_mask */
457 0x3fffffc, /* dst_mask */
b34976b6 458 TRUE), /* pcrel_offset */
252b5132
RH
459
460 /* Like R_PPC_ADDR32, but may be unaligned. */
461 HOWTO (R_PPC_UADDR32, /* type */
462 0, /* rightshift */
463 2, /* size (0 = byte, 1 = short, 2 = long) */
464 32, /* bitsize */
b34976b6 465 FALSE, /* pc_relative */
252b5132
RH
466 0, /* bitpos */
467 complain_overflow_bitfield, /* complain_on_overflow */
468 bfd_elf_generic_reloc, /* special_function */
469 "R_PPC_UADDR32", /* name */
b34976b6 470 FALSE, /* partial_inplace */
252b5132
RH
471 0, /* src_mask */
472 0xffffffff, /* dst_mask */
b34976b6 473 FALSE), /* pcrel_offset */
252b5132
RH
474
475 /* Like R_PPC_ADDR16, but may be unaligned. */
476 HOWTO (R_PPC_UADDR16, /* type */
477 0, /* rightshift */
478 1, /* size (0 = byte, 1 = short, 2 = long) */
479 16, /* 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_UADDR16", /* name */
b34976b6 485 FALSE, /* partial_inplace */
252b5132
RH
486 0, /* src_mask */
487 0xffff, /* dst_mask */
b34976b6 488 FALSE), /* pcrel_offset */
252b5132
RH
489
490 /* 32-bit PC relative */
491 HOWTO (R_PPC_REL32, /* type */
492 0, /* rightshift */
493 2, /* size (0 = byte, 1 = short, 2 = long) */
494 32, /* bitsize */
b34976b6 495 TRUE, /* pc_relative */
252b5132
RH
496 0, /* bitpos */
497 complain_overflow_bitfield, /* complain_on_overflow */
498 bfd_elf_generic_reloc, /* special_function */
499 "R_PPC_REL32", /* name */
b34976b6 500 FALSE, /* partial_inplace */
252b5132
RH
501 0, /* src_mask */
502 0xffffffff, /* dst_mask */
b34976b6 503 TRUE), /* pcrel_offset */
252b5132
RH
504
505 /* 32-bit relocation to the symbol's procedure linkage table.
c3668558 506 FIXME: not supported. */
252b5132
RH
507 HOWTO (R_PPC_PLT32, /* 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_PLT32", /* 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 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
c3668558 522 FIXME: not supported. */
252b5132
RH
523 HOWTO (R_PPC_PLTREL32, /* type */
524 0, /* rightshift */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
526 32, /* bitsize */
b34976b6 527 TRUE, /* pc_relative */
252b5132
RH
528 0, /* bitpos */
529 complain_overflow_bitfield, /* complain_on_overflow */
530 bfd_elf_generic_reloc, /* special_function */
531 "R_PPC_PLTREL32", /* name */
b34976b6 532 FALSE, /* partial_inplace */
252b5132
RH
533 0, /* src_mask */
534 0, /* dst_mask */
b34976b6 535 TRUE), /* pcrel_offset */
252b5132
RH
536
537 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
538 the symbol. */
539 HOWTO (R_PPC_PLT16_LO, /* type */
540 0, /* rightshift */
541 1, /* size (0 = byte, 1 = short, 2 = long) */
542 16, /* bitsize */
b34976b6 543 FALSE, /* pc_relative */
252b5132
RH
544 0, /* bitpos */
545 complain_overflow_dont, /* complain_on_overflow */
546 bfd_elf_generic_reloc, /* special_function */
547 "R_PPC_PLT16_LO", /* name */
b34976b6 548 FALSE, /* partial_inplace */
252b5132
RH
549 0, /* src_mask */
550 0xffff, /* dst_mask */
b34976b6 551 FALSE), /* pcrel_offset */
252b5132
RH
552
553 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
554 the symbol. */
555 HOWTO (R_PPC_PLT16_HI, /* type */
556 16, /* rightshift */
557 1, /* size (0 = byte, 1 = short, 2 = long) */
558 16, /* bitsize */
b34976b6 559 FALSE, /* pc_relative */
252b5132
RH
560 0, /* bitpos */
561 complain_overflow_bitfield, /* complain_on_overflow */
562 bfd_elf_generic_reloc, /* special_function */
563 "R_PPC_PLT16_HI", /* name */
b34976b6 564 FALSE, /* partial_inplace */
252b5132
RH
565 0, /* src_mask */
566 0xffff, /* dst_mask */
b34976b6 567 FALSE), /* pcrel_offset */
252b5132
RH
568
569 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
570 the symbol. */
571 HOWTO (R_PPC_PLT16_HA, /* type */
572 16, /* 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 ppc_elf_addr16_ha_reloc, /* special_function */
579 "R_PPC_PLT16_HA", /* 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 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
586 small data items. */
587 HOWTO (R_PPC_SDAREL16, /* type */
588 0, /* rightshift */
589 1, /* size (0 = byte, 1 = short, 2 = long) */
590 16, /* bitsize */
b34976b6 591 FALSE, /* pc_relative */
252b5132
RH
592 0, /* bitpos */
593 complain_overflow_signed, /* complain_on_overflow */
594 bfd_elf_generic_reloc, /* special_function */
595 "R_PPC_SDAREL16", /* name */
b34976b6 596 FALSE, /* partial_inplace */
252b5132
RH
597 0, /* src_mask */
598 0xffff, /* dst_mask */
b34976b6 599 FALSE), /* pcrel_offset */
252b5132 600
c061c2d8 601 /* 16-bit section relative relocation. */
252b5132
RH
602 HOWTO (R_PPC_SECTOFF, /* type */
603 0, /* rightshift */
c061c2d8
AM
604 1, /* size (0 = byte, 1 = short, 2 = long) */
605 16, /* 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_SECTOFF", /* name */
b34976b6 611 FALSE, /* partial_inplace */
252b5132 612 0, /* src_mask */
c061c2d8 613 0xffff, /* dst_mask */
b34976b6 614 FALSE), /* pcrel_offset */
252b5132 615
c3668558 616 /* 16-bit lower half section relative relocation. */
252b5132
RH
617 HOWTO (R_PPC_SECTOFF_LO, /* type */
618 0, /* rightshift */
619 1, /* size (0 = byte, 1 = short, 2 = long) */
620 16, /* bitsize */
b34976b6 621 FALSE, /* pc_relative */
252b5132
RH
622 0, /* bitpos */
623 complain_overflow_dont, /* complain_on_overflow */
624 bfd_elf_generic_reloc, /* special_function */
625 "R_PPC_SECTOFF_LO", /* name */
b34976b6 626 FALSE, /* partial_inplace */
252b5132
RH
627 0, /* src_mask */
628 0xffff, /* dst_mask */
b34976b6 629 FALSE), /* pcrel_offset */
252b5132 630
c3668558 631 /* 16-bit upper half section relative relocation. */
252b5132
RH
632 HOWTO (R_PPC_SECTOFF_HI, /* type */
633 16, /* rightshift */
634 1, /* size (0 = byte, 1 = short, 2 = long) */
635 16, /* bitsize */
b34976b6 636 FALSE, /* pc_relative */
252b5132
RH
637 0, /* bitpos */
638 complain_overflow_bitfield, /* complain_on_overflow */
639 bfd_elf_generic_reloc, /* special_function */
640 "R_PPC_SECTOFF_HI", /* name */
b34976b6 641 FALSE, /* partial_inplace */
252b5132
RH
642 0, /* src_mask */
643 0xffff, /* dst_mask */
b34976b6 644 FALSE), /* pcrel_offset */
252b5132 645
c3668558 646 /* 16-bit upper half adjusted section relative relocation. */
252b5132
RH
647 HOWTO (R_PPC_SECTOFF_HA, /* type */
648 16, /* rightshift */
649 1, /* size (0 = byte, 1 = short, 2 = long) */
650 16, /* bitsize */
b34976b6 651 FALSE, /* pc_relative */
252b5132
RH
652 0, /* bitpos */
653 complain_overflow_bitfield, /* complain_on_overflow */
654 ppc_elf_addr16_ha_reloc, /* special_function */
655 "R_PPC_SECTOFF_HA", /* name */
b34976b6 656 FALSE, /* partial_inplace */
252b5132
RH
657 0, /* src_mask */
658 0xffff, /* dst_mask */
b34976b6 659 FALSE), /* pcrel_offset */
252b5132 660
7619e7c7
AM
661 /* Marker reloc for TLS. */
662 HOWTO (R_PPC_TLS,
252b5132
RH
663 0, /* rightshift */
664 2, /* size (0 = byte, 1 = short, 2 = long) */
665 32, /* bitsize */
b34976b6 666 FALSE, /* pc_relative */
252b5132 667 0, /* bitpos */
7619e7c7 668 complain_overflow_dont, /* complain_on_overflow */
252b5132 669 bfd_elf_generic_reloc, /* special_function */
7619e7c7
AM
670 "R_PPC_TLS", /* name */
671 FALSE, /* partial_inplace */
672 0, /* src_mask */
673 0, /* dst_mask */
674 FALSE), /* pcrel_offset */
675
676 /* Computes the load module index of the load module that contains the
677 definition of its TLS sym. */
678 HOWTO (R_PPC_DTPMOD32,
679 0, /* rightshift */
680 2, /* size (0 = byte, 1 = short, 2 = long) */
681 32, /* bitsize */
682 FALSE, /* pc_relative */
683 0, /* bitpos */
684 complain_overflow_dont, /* complain_on_overflow */
685 ppc_elf_unhandled_reloc, /* special_function */
686 "R_PPC_DTPMOD32", /* name */
b34976b6 687 FALSE, /* partial_inplace */
252b5132
RH
688 0, /* src_mask */
689 0xffffffff, /* dst_mask */
b34976b6 690 FALSE), /* pcrel_offset */
252b5132 691
7619e7c7
AM
692 /* Computes a dtv-relative displacement, the difference between the value
693 of sym+add and the base address of the thread-local storage block that
694 contains the definition of sym, minus 0x8000. */
695 HOWTO (R_PPC_DTPREL32,
696 0, /* rightshift */
697 2, /* size (0 = byte, 1 = short, 2 = long) */
698 32, /* bitsize */
699 FALSE, /* pc_relative */
700 0, /* bitpos */
701 complain_overflow_dont, /* complain_on_overflow */
702 ppc_elf_unhandled_reloc, /* special_function */
703 "R_PPC_DTPREL32", /* name */
704 FALSE, /* partial_inplace */
705 0, /* src_mask */
706 0xffffffff, /* dst_mask */
707 FALSE), /* pcrel_offset */
708
709 /* A 16 bit dtprel reloc. */
710 HOWTO (R_PPC_DTPREL16,
252b5132
RH
711 0, /* rightshift */
712 1, /* size (0 = byte, 1 = short, 2 = long) */
713 16, /* bitsize */
b34976b6 714 FALSE, /* pc_relative */
252b5132 715 0, /* bitpos */
7619e7c7
AM
716 complain_overflow_signed, /* complain_on_overflow */
717 ppc_elf_unhandled_reloc, /* special_function */
718 "R_PPC_DTPREL16", /* name */
b34976b6 719 FALSE, /* partial_inplace */
252b5132
RH
720 0, /* src_mask */
721 0xffff, /* dst_mask */
b34976b6 722 FALSE), /* pcrel_offset */
252b5132 723
7619e7c7
AM
724 /* Like DTPREL16, but no overflow. */
725 HOWTO (R_PPC_DTPREL16_LO,
252b5132
RH
726 0, /* rightshift */
727 1, /* size (0 = byte, 1 = short, 2 = long) */
728 16, /* bitsize */
b34976b6 729 FALSE, /* pc_relative */
252b5132 730 0, /* bitpos */
7619e7c7
AM
731 complain_overflow_dont, /* complain_on_overflow */
732 ppc_elf_unhandled_reloc, /* special_function */
733 "R_PPC_DTPREL16_LO", /* name */
b34976b6 734 FALSE, /* partial_inplace */
252b5132
RH
735 0, /* src_mask */
736 0xffff, /* dst_mask */
b34976b6 737 FALSE), /* pcrel_offset */
252b5132 738
7619e7c7
AM
739 /* Like DTPREL16_LO, but next higher group of 16 bits. */
740 HOWTO (R_PPC_DTPREL16_HI,
252b5132
RH
741 16, /* rightshift */
742 1, /* size (0 = byte, 1 = short, 2 = long) */
743 16, /* bitsize */
b34976b6 744 FALSE, /* pc_relative */
252b5132
RH
745 0, /* bitpos */
746 complain_overflow_dont, /* complain_on_overflow */
7619e7c7
AM
747 ppc_elf_unhandled_reloc, /* special_function */
748 "R_PPC_DTPREL16_HI", /* name */
b34976b6 749 FALSE, /* partial_inplace */
252b5132
RH
750 0, /* src_mask */
751 0xffff, /* dst_mask */
b34976b6 752 FALSE), /* pcrel_offset */
252b5132 753
7619e7c7
AM
754 /* Like DTPREL16_HI, but adjust for low 16 bits. */
755 HOWTO (R_PPC_DTPREL16_HA,
252b5132
RH
756 16, /* rightshift */
757 1, /* size (0 = byte, 1 = short, 2 = long) */
758 16, /* bitsize */
b34976b6 759 FALSE, /* pc_relative */
252b5132
RH
760 0, /* bitpos */
761 complain_overflow_dont, /* complain_on_overflow */
7619e7c7
AM
762 ppc_elf_unhandled_reloc, /* special_function */
763 "R_PPC_DTPREL16_HA", /* name */
b34976b6 764 FALSE, /* partial_inplace */
252b5132
RH
765 0, /* src_mask */
766 0xffff, /* dst_mask */
b34976b6 767 FALSE), /* pcrel_offset */
252b5132 768
7619e7c7
AM
769 /* Computes a tp-relative displacement, the difference between the value of
770 sym+add and the value of the thread pointer (r13). */
771 HOWTO (R_PPC_TPREL32,
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 ppc_elf_unhandled_reloc, /* special_function */
779 "R_PPC_TPREL32", /* name */
780 FALSE, /* partial_inplace */
781 0, /* src_mask */
782 0xffffffff, /* dst_mask */
783 FALSE), /* pcrel_offset */
784
785 /* A 16 bit tprel reloc. */
786 HOWTO (R_PPC_TPREL16,
252b5132
RH
787 0, /* rightshift */
788 1, /* size (0 = byte, 1 = short, 2 = long) */
789 16, /* bitsize */
b34976b6 790 FALSE, /* pc_relative */
252b5132 791 0, /* bitpos */
7619e7c7
AM
792 complain_overflow_signed, /* complain_on_overflow */
793 ppc_elf_unhandled_reloc, /* special_function */
794 "R_PPC_TPREL16", /* name */
b34976b6 795 FALSE, /* partial_inplace */
252b5132
RH
796 0, /* src_mask */
797 0xffff, /* dst_mask */
b34976b6 798 FALSE), /* pcrel_offset */
252b5132 799
7619e7c7
AM
800 /* Like TPREL16, but no overflow. */
801 HOWTO (R_PPC_TPREL16_LO,
252b5132
RH
802 0, /* rightshift */
803 1, /* size (0 = byte, 1 = short, 2 = long) */
804 16, /* bitsize */
b34976b6 805 FALSE, /* pc_relative */
252b5132 806 0, /* bitpos */
7619e7c7
AM
807 complain_overflow_dont, /* complain_on_overflow */
808 ppc_elf_unhandled_reloc, /* special_function */
809 "R_PPC_TPREL16_LO", /* name */
b34976b6 810 FALSE, /* partial_inplace */
252b5132
RH
811 0, /* src_mask */
812 0xffff, /* dst_mask */
b34976b6 813 FALSE), /* pcrel_offset */
252b5132 814
7619e7c7
AM
815 /* Like TPREL16_LO, but next higher group of 16 bits. */
816 HOWTO (R_PPC_TPREL16_HI,
817 16, /* rightshift */
818 1, /* size (0 = byte, 1 = short, 2 = long) */
819 16, /* bitsize */
820 FALSE, /* pc_relative */
821 0, /* bitpos */
822 complain_overflow_dont, /* complain_on_overflow */
823 ppc_elf_unhandled_reloc, /* special_function */
824 "R_PPC_TPREL16_HI", /* name */
825 FALSE, /* partial_inplace */
826 0, /* src_mask */
827 0xffff, /* dst_mask */
828 FALSE), /* pcrel_offset */
829
830 /* Like TPREL16_HI, but adjust for low 16 bits. */
831 HOWTO (R_PPC_TPREL16_HA,
832 16, /* rightshift */
833 1, /* size (0 = byte, 1 = short, 2 = long) */
834 16, /* bitsize */
835 FALSE, /* pc_relative */
836 0, /* bitpos */
837 complain_overflow_dont, /* complain_on_overflow */
838 ppc_elf_unhandled_reloc, /* special_function */
839 "R_PPC_TPREL16_HA", /* name */
840 FALSE, /* partial_inplace */
841 0, /* src_mask */
842 0xffff, /* dst_mask */
843 FALSE), /* pcrel_offset */
844
845 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
846 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
847 to the first entry. */
848 HOWTO (R_PPC_GOT_TLSGD16,
252b5132
RH
849 0, /* rightshift */
850 1, /* size (0 = byte, 1 = short, 2 = long) */
851 16, /* bitsize */
b34976b6 852 FALSE, /* pc_relative */
252b5132
RH
853 0, /* bitpos */
854 complain_overflow_signed, /* complain_on_overflow */
7619e7c7
AM
855 ppc_elf_unhandled_reloc, /* special_function */
856 "R_PPC_GOT_TLSGD16", /* 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 GOT_TLSGD16, but no overflow. */
863 HOWTO (R_PPC_GOT_TLSGD16_LO,
252b5132 864 0, /* rightshift */
7619e7c7 865 1, /* size (0 = byte, 1 = short, 2 = long) */
252b5132 866 16, /* bitsize */
b34976b6 867 FALSE, /* pc_relative */
252b5132 868 0, /* bitpos */
7619e7c7
AM
869 complain_overflow_dont, /* complain_on_overflow */
870 ppc_elf_unhandled_reloc, /* special_function */
871 "R_PPC_GOT_TLSGD16_LO", /* 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 GOT_TLSGD16_LO, but next higher group of 16 bits. */
878 HOWTO (R_PPC_GOT_TLSGD16_HI,
879 16, /* rightshift */
880 1, /* size (0 = byte, 1 = short, 2 = long) */
881 16, /* bitsize */
882 FALSE, /* pc_relative */
883 0, /* bitpos */
884 complain_overflow_dont, /* complain_on_overflow */
885 ppc_elf_unhandled_reloc, /* special_function */
886 "R_PPC_GOT_TLSGD16_HI", /* name */
887 FALSE, /* partial_inplace */
888 0, /* src_mask */
889 0xffff, /* dst_mask */
890 FALSE), /* pcrel_offset */
252b5132 891
7619e7c7
AM
892 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
893 HOWTO (R_PPC_GOT_TLSGD16_HA,
894 16, /* rightshift */
895 1, /* size (0 = byte, 1 = short, 2 = long) */
896 16, /* bitsize */
897 FALSE, /* pc_relative */
898 0, /* bitpos */
899 complain_overflow_dont, /* complain_on_overflow */
900 ppc_elf_unhandled_reloc, /* special_function */
901 "R_PPC_GOT_TLSGD16_HA", /* name */
902 FALSE, /* partial_inplace */
903 0, /* src_mask */
904 0xffff, /* dst_mask */
905 FALSE), /* pcrel_offset */
906
907 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
908 with values (sym+add)@dtpmod and zero, and computes the offset to the
909 first entry. */
910 HOWTO (R_PPC_GOT_TLSLD16,
252b5132
RH
911 0, /* rightshift */
912 1, /* size (0 = byte, 1 = short, 2 = long) */
913 16, /* bitsize */
7619e7c7 914 FALSE, /* pc_relative */
252b5132
RH
915 0, /* bitpos */
916 complain_overflow_signed, /* complain_on_overflow */
7619e7c7
AM
917 ppc_elf_unhandled_reloc, /* special_function */
918 "R_PPC_GOT_TLSLD16", /* name */
b34976b6 919 FALSE, /* partial_inplace */
252b5132
RH
920 0, /* src_mask */
921 0xffff, /* dst_mask */
b34976b6 922 FALSE), /* pcrel_offset */
252b5132 923
7619e7c7
AM
924 /* Like GOT_TLSLD16, but no overflow. */
925 HOWTO (R_PPC_GOT_TLSLD16_LO,
252b5132 926 0, /* rightshift */
7619e7c7
AM
927 1, /* size (0 = byte, 1 = short, 2 = long) */
928 16, /* bitsize */
b34976b6 929 FALSE, /* pc_relative */
252b5132
RH
930 0, /* bitpos */
931 complain_overflow_dont, /* complain_on_overflow */
7619e7c7
AM
932 ppc_elf_unhandled_reloc, /* special_function */
933 "R_PPC_GOT_TLSLD16_LO", /* name */
b34976b6 934 FALSE, /* partial_inplace */
252b5132 935 0, /* src_mask */
7619e7c7 936 0xffff, /* dst_mask */
b34976b6 937 FALSE), /* pcrel_offset */
252b5132 938
7619e7c7
AM
939 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
940 HOWTO (R_PPC_GOT_TLSLD16_HI,
941 16, /* rightshift */
942 1, /* size (0 = byte, 1 = short, 2 = long) */
943 16, /* bitsize */
b34976b6 944 FALSE, /* pc_relative */
252b5132
RH
945 0, /* bitpos */
946 complain_overflow_dont, /* complain_on_overflow */
7619e7c7
AM
947 ppc_elf_unhandled_reloc, /* special_function */
948 "R_PPC_GOT_TLSLD16_HI", /* name */
b34976b6 949 FALSE, /* partial_inplace */
252b5132 950 0, /* src_mask */
7619e7c7 951 0xffff, /* dst_mask */
b34976b6 952 FALSE), /* pcrel_offset */
252b5132 953
7619e7c7
AM
954 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
955 HOWTO (R_PPC_GOT_TLSLD16_HA,
956 16, /* rightshift */
957 1, /* size (0 = byte, 1 = short, 2 = long) */
958 16, /* bitsize */
959 FALSE, /* pc_relative */
960 0, /* bitpos */
961 complain_overflow_dont, /* complain_on_overflow */
962 ppc_elf_unhandled_reloc, /* special_function */
963 "R_PPC_GOT_TLSLD16_HA", /* name */
964 FALSE, /* partial_inplace */
965 0, /* src_mask */
966 0xffff, /* dst_mask */
967 FALSE), /* pcrel_offset */
968
969 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
970 the offset to the entry. */
971 HOWTO (R_PPC_GOT_DTPREL16,
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_DTPREL16", /* 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_DTPREL16, but no overflow. */
986 HOWTO (R_PPC_GOT_DTPREL16_LO,
987 0, /* rightshift */
988 1, /* size (0 = byte, 1 = short, 2 = long) */
989 16, /* bitsize */
990 FALSE, /* pc_relative */
991 0, /* bitpos */
992 complain_overflow_dont, /* complain_on_overflow */
993 ppc_elf_unhandled_reloc, /* special_function */
994 "R_PPC_GOT_DTPREL16_LO", /* name */
995 FALSE, /* partial_inplace */
996 0, /* src_mask */
997 0xffff, /* dst_mask */
998 FALSE), /* pcrel_offset */
252b5132 999
7619e7c7
AM
1000 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1001 HOWTO (R_PPC_GOT_DTPREL16_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_DTPREL16_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_DTPREL16_HI, but adjust for low 16 bits. */
1016 HOWTO (R_PPC_GOT_DTPREL16_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_DTPREL16_HA", /* name */
1025 FALSE, /* partial_inplace */
1026 0, /* src_mask */
1027 0xffff, /* dst_mask */
1028 FALSE), /* pcrel_offset */
c3668558 1029
7619e7c7
AM
1030 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1031 offset to the entry. */
1032 HOWTO (R_PPC_GOT_TPREL16,
1033 0, /* rightshift */
1034 1, /* size (0 = byte, 1 = short, 2 = long) */
1035 16, /* bitsize */
1036 FALSE, /* pc_relative */
1037 0, /* bitpos */
1038 complain_overflow_signed, /* complain_on_overflow */
1039 ppc_elf_unhandled_reloc, /* special_function */
1040 "R_PPC_GOT_TPREL16", /* name */
1041 FALSE, /* partial_inplace */
1042 0, /* src_mask */
1043 0xffff, /* dst_mask */
1044 FALSE), /* pcrel_offset */
1045
1046 /* Like GOT_TPREL16, but no overflow. */
1047 HOWTO (R_PPC_GOT_TPREL16_LO,
1048 0, /* rightshift */
1049 1, /* size (0 = byte, 1 = short, 2 = long) */
1050 16, /* bitsize */
1051 FALSE, /* pc_relative */
1052 0, /* bitpos */
1053 complain_overflow_dont, /* complain_on_overflow */
1054 ppc_elf_unhandled_reloc, /* special_function */
1055 "R_PPC_GOT_TPREL16_LO", /* name */
1056 FALSE, /* partial_inplace */
1057 0, /* src_mask */
1058 0xffff, /* dst_mask */
1059 FALSE), /* pcrel_offset */
1060
1061 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1062 HOWTO (R_PPC_GOT_TPREL16_HI,
1063 16, /* rightshift */
1064 1, /* size (0 = byte, 1 = short, 2 = long) */
1065 16, /* bitsize */
1066 FALSE, /* pc_relative */
1067 0, /* bitpos */
1068 complain_overflow_dont, /* complain_on_overflow */
1069 ppc_elf_unhandled_reloc, /* special_function */
1070 "R_PPC_GOT_TPREL16_HI", /* name */
1071 FALSE, /* partial_inplace */
1072 0, /* src_mask */
1073 0xffff, /* dst_mask */
1074 FALSE), /* pcrel_offset */
1075
1076 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1077 HOWTO (R_PPC_GOT_TPREL16_HA,
1078 16, /* rightshift */
1079 1, /* size (0 = byte, 1 = short, 2 = long) */
1080 16, /* bitsize */
1081 FALSE, /* pc_relative */
1082 0, /* bitpos */
1083 complain_overflow_dont, /* complain_on_overflow */
1084 ppc_elf_unhandled_reloc, /* special_function */
1085 "R_PPC_GOT_TPREL16_HA", /* name */
1086 FALSE, /* partial_inplace */
1087 0, /* src_mask */
1088 0xffff, /* dst_mask */
1089 FALSE), /* pcrel_offset */
1090
1091 /* The remaining relocs are from the Embedded ELF ABI, and are not
1092 in the SVR4 ELF ABI. */
1093
1094 /* 32 bit value resulting from the addend minus the symbol. */
1095 HOWTO (R_PPC_EMB_NADDR32, /* type */
1096 0, /* rightshift */
1097 2, /* size (0 = byte, 1 = short, 2 = long) */
1098 32, /* bitsize */
1099 FALSE, /* pc_relative */
1100 0, /* bitpos */
1101 complain_overflow_bitfield, /* complain_on_overflow */
1102 bfd_elf_generic_reloc, /* special_function */
1103 "R_PPC_EMB_NADDR32", /* name */
1104 FALSE, /* partial_inplace */
1105 0, /* src_mask */
1106 0xffffffff, /* dst_mask */
1107 FALSE), /* pcrel_offset */
1108
1109 /* 16 bit value resulting from the addend minus the symbol. */
1110 HOWTO (R_PPC_EMB_NADDR16, /* type */
1111 0, /* rightshift */
1112 1, /* size (0 = byte, 1 = short, 2 = long) */
1113 16, /* bitsize */
1114 FALSE, /* pc_relative */
1115 0, /* bitpos */
1116 complain_overflow_bitfield, /* complain_on_overflow */
1117 bfd_elf_generic_reloc, /* special_function */
1118 "R_PPC_EMB_NADDR16", /* name */
1119 FALSE, /* partial_inplace */
1120 0, /* src_mask */
1121 0xffff, /* dst_mask */
1122 FALSE), /* pcrel_offset */
1123
1124 /* 16 bit value resulting from the addend minus the symbol. */
1125 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1126 0, /* rightshift */
1127 1, /* size (0 = byte, 1 = short, 2 = long) */
1128 16, /* bitsize */
1129 FALSE, /* pc_relative */
1130 0, /* bitpos */
1131 complain_overflow_dont,/* complain_on_overflow */
1132 bfd_elf_generic_reloc, /* special_function */
1133 "R_PPC_EMB_ADDR16_LO", /* name */
1134 FALSE, /* partial_inplace */
1135 0, /* src_mask */
1136 0xffff, /* dst_mask */
1137 FALSE), /* pcrel_offset */
1138
1139 /* The high order 16 bits of the addend minus the symbol. */
1140 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1141 16, /* rightshift */
1142 1, /* size (0 = byte, 1 = short, 2 = long) */
1143 16, /* bitsize */
1144 FALSE, /* pc_relative */
1145 0, /* bitpos */
1146 complain_overflow_dont, /* complain_on_overflow */
1147 bfd_elf_generic_reloc, /* special_function */
1148 "R_PPC_EMB_NADDR16_HI", /* name */
1149 FALSE, /* partial_inplace */
1150 0, /* src_mask */
1151 0xffff, /* dst_mask */
1152 FALSE), /* pcrel_offset */
1153
1154 /* The high order 16 bits of the result of the addend minus the address,
1155 plus 1 if the contents of the low 16 bits, treated as a signed number,
1156 is negative. */
1157 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1158 16, /* rightshift */
1159 1, /* size (0 = byte, 1 = short, 2 = long) */
1160 16, /* bitsize */
1161 FALSE, /* pc_relative */
1162 0, /* bitpos */
1163 complain_overflow_dont, /* complain_on_overflow */
25dbc73a
AM
1164 ppc_elf_addr16_ha_reloc, /* special_function */
1165 "R_PPC_EMB_NADDR16_HA", /* name */
1166 FALSE, /* partial_inplace */
1167 0, /* src_mask */
1168 0xffff, /* dst_mask */
1169 FALSE), /* pcrel_offset */
1170
1171 /* 16 bit value resulting from allocating a 4 byte word to hold an
1172 address in the .sdata section, and returning the offset from
1173 _SDA_BASE_ for that relocation. */
1174 HOWTO (R_PPC_EMB_SDAI16, /* type */
1175 0, /* rightshift */
1176 1, /* size (0 = byte, 1 = short, 2 = long) */
1177 16, /* bitsize */
1178 FALSE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_bitfield, /* complain_on_overflow */
1181 bfd_elf_generic_reloc, /* special_function */
1182 "R_PPC_EMB_SDAI16", /* name */
1183 FALSE, /* partial_inplace */
1184 0, /* src_mask */
1185 0xffff, /* dst_mask */
1186 FALSE), /* pcrel_offset */
1187
1188 /* 16 bit value resulting from allocating a 4 byte word to hold an
1189 address in the .sdata2 section, and returning the offset from
1190 _SDA2_BASE_ for that relocation. */
1191 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1192 0, /* rightshift */
1193 1, /* size (0 = byte, 1 = short, 2 = long) */
1194 16, /* bitsize */
1195 FALSE, /* pc_relative */
1196 0, /* bitpos */
1197 complain_overflow_bitfield, /* complain_on_overflow */
1198 bfd_elf_generic_reloc, /* special_function */
1199 "R_PPC_EMB_SDA2I16", /* name */
1200 FALSE, /* partial_inplace */
1201 0, /* src_mask */
1202 0xffff, /* dst_mask */
1203 FALSE), /* pcrel_offset */
1204
1205 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1206 small data items. */
1207 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1208 0, /* rightshift */
1209 1, /* size (0 = byte, 1 = short, 2 = long) */
1210 16, /* bitsize */
1211 FALSE, /* pc_relative */
1212 0, /* bitpos */
1213 complain_overflow_signed, /* complain_on_overflow */
1214 bfd_elf_generic_reloc, /* special_function */
1215 "R_PPC_EMB_SDA2REL", /* name */
1216 FALSE, /* partial_inplace */
1217 0, /* src_mask */
1218 0xffff, /* dst_mask */
1219 FALSE), /* pcrel_offset */
1220
1221 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1222 signed offset from the appropriate base, and filling in the register
1223 field with the appropriate register (0, 2, or 13). */
1224 HOWTO (R_PPC_EMB_SDA21, /* type */
1225 0, /* rightshift */
1226 2, /* size (0 = byte, 1 = short, 2 = long) */
1227 16, /* bitsize */
1228 FALSE, /* pc_relative */
1229 0, /* bitpos */
1230 complain_overflow_signed, /* complain_on_overflow */
1231 bfd_elf_generic_reloc, /* special_function */
1232 "R_PPC_EMB_SDA21", /* name */
1233 FALSE, /* partial_inplace */
1234 0, /* src_mask */
1235 0xffff, /* dst_mask */
1236 FALSE), /* pcrel_offset */
1237
1238 /* Relocation not handled: R_PPC_EMB_MRKREF */
1239 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1240 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1241 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1242 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1243 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1244
1245 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1246 in the 16 bit signed offset from the appropriate base, and filling in the
1247 register field with the appropriate register (0, 2, or 13). */
1248 HOWTO (R_PPC_EMB_RELSDA, /* type */
1249 0, /* rightshift */
1250 1, /* size (0 = byte, 1 = short, 2 = long) */
1251 16, /* bitsize */
1252 TRUE, /* pc_relative */
1253 0, /* bitpos */
1254 complain_overflow_signed, /* complain_on_overflow */
1255 bfd_elf_generic_reloc, /* special_function */
1256 "R_PPC_EMB_RELSDA", /* name */
1257 FALSE, /* partial_inplace */
1258 0, /* src_mask */
1259 0xffff, /* dst_mask */
1260 FALSE), /* pcrel_offset */
1261
1262 /* GNU extension to record C++ vtable hierarchy. */
1263 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1264 0, /* rightshift */
1265 0, /* size (0 = byte, 1 = short, 2 = long) */
1266 0, /* bitsize */
1267 FALSE, /* pc_relative */
1268 0, /* bitpos */
1269 complain_overflow_dont, /* complain_on_overflow */
1270 NULL, /* special_function */
1271 "R_PPC_GNU_VTINHERIT", /* name */
1272 FALSE, /* partial_inplace */
1273 0, /* src_mask */
1274 0, /* dst_mask */
1275 FALSE), /* pcrel_offset */
1276
1277 /* GNU extension to record C++ vtable member usage. */
1278 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1279 0, /* rightshift */
1280 0, /* size (0 = byte, 1 = short, 2 = long) */
1281 0, /* bitsize */
1282 FALSE, /* pc_relative */
1283 0, /* bitpos */
1284 complain_overflow_dont, /* complain_on_overflow */
1285 NULL, /* special_function */
1286 "R_PPC_GNU_VTENTRY", /* name */
7619e7c7
AM
1287 FALSE, /* partial_inplace */
1288 0, /* src_mask */
25dbc73a 1289 0, /* dst_mask */
7619e7c7
AM
1290 FALSE), /* pcrel_offset */
1291
25dbc73a
AM
1292 /* Phony reloc to handle AIX style TOC entries. */
1293 HOWTO (R_PPC_TOC16, /* type */
7619e7c7
AM
1294 0, /* rightshift */
1295 1, /* size (0 = byte, 1 = short, 2 = long) */
1296 16, /* bitsize */
1297 FALSE, /* pc_relative */
1298 0, /* bitpos */
25dbc73a 1299 complain_overflow_signed, /* complain_on_overflow */
7619e7c7 1300 bfd_elf_generic_reloc, /* special_function */
25dbc73a 1301 "R_PPC_TOC16", /* name */
7619e7c7
AM
1302 FALSE, /* partial_inplace */
1303 0, /* src_mask */
1304 0xffff, /* dst_mask */
1305 FALSE), /* pcrel_offset */
25dbc73a
AM
1306};
1307\f
1308/* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1309
1310static void
1311ppc_elf_howto_init (void)
1312{
1313 unsigned int i, type;
1314
1315 for (i = 0;
1316 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1317 i++)
1318 {
1319 type = ppc_elf_howto_raw[i].type;
1320 if (type >= (sizeof (ppc_elf_howto_table)
1321 / sizeof (ppc_elf_howto_table[0])))
1322 abort ();
1323 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1324 }
1325}
1326
1327static reloc_howto_type *
1328ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1329 bfd_reloc_code_real_type code)
1330{
1331 enum elf_ppc_reloc_type r;
1332
1333 /* Initialize howto table if not already done. */
1334 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1335 ppc_elf_howto_init ();
1336
1337 switch (code)
1338 {
1339 default:
1340 return NULL;
1341
1342 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
1343 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
1344 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
1345 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1346 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
1347 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
1348 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
1349 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
1350 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
1351 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
1352 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
1353 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
1354 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
1355 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
1356 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1357 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
1358 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
1359 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
1360 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
1361 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
1362 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
1363 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
1364 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
1365 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
1366 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
1367 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
1368 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
1369 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
1370 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
1371 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1372 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
1373 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
1374 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
1375 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
1376 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
1377 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
1378 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1379 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1380 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
1381 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
1382 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
1383 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
1384 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1385 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
1386 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
1387 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
1388 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
1389 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
1390 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
1391 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
1392 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
1393 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
1394 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
1395 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
1396 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
1397 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
1398 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
1399 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
1400 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
1401 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
1402 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
1403 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
1404 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
1405 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
1406 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
1407 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
1408 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
1409 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
1410 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
1411 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
1412 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
1413 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
1414 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
1415 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
1416 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
1417 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
1418 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
1419 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
1420 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
1421 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
1422 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
1423 }
1424
1425 return ppc_elf_howto_table[r];
1426};
1427
1428/* Set the howto pointer for a PowerPC ELF reloc. */
1429
1430static void
1431ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1432 arelent *cache_ptr,
1433 Elf_Internal_Rela *dst)
1434{
1435 /* Initialize howto table if not already done. */
1436 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1437 ppc_elf_howto_init ();
1438
1439 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1440 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1441}
1442
1443/* Handle the R_PPC_ADDR16_HA reloc. */
1444
1445static bfd_reloc_status_type
1446ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1447 arelent *reloc_entry,
1448 asymbol *symbol,
1449 void *data ATTRIBUTE_UNUSED,
1450 asection *input_section,
1451 bfd *output_bfd,
1452 char **error_message ATTRIBUTE_UNUSED)
1453{
1454 bfd_vma relocation;
1455
1456 if (output_bfd != NULL)
1457 {
1458 reloc_entry->address += input_section->output_offset;
1459 return bfd_reloc_ok;
1460 }
1461
1462 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1463 return bfd_reloc_outofrange;
1464
1465 if (bfd_is_com_section (symbol->section))
1466 relocation = 0;
1467 else
1468 relocation = symbol->value;
1469
1470 relocation += symbol->section->output_section->vma;
1471 relocation += symbol->section->output_offset;
1472 relocation += reloc_entry->addend;
1473
1474 reloc_entry->addend += (relocation & 0x8000) << 1;
1475
1476 return bfd_reloc_continue;
1477}
1478
1479static bfd_reloc_status_type
1480ppc_elf_unhandled_reloc (bfd *abfd,
1481 arelent *reloc_entry,
1482 asymbol *symbol,
1483 void *data,
1484 asection *input_section,
1485 bfd *output_bfd,
1486 char **error_message)
1487{
1488 /* If this is a relocatable link (output_bfd test tells us), just
1489 call the generic function. Any adjustment will be done at final
1490 link time. */
1491 if (output_bfd != NULL)
1492 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1493 input_section, output_bfd, error_message);
1494
1495 if (error_message != NULL)
1496 {
1497 static char buf[60];
1498 sprintf (buf, _("generic linker can't handle %s"),
1499 reloc_entry->howto->name);
1500 *error_message = buf;
1501 }
1502 return bfd_reloc_dangerous;
1503}
1504\f
1505/* Sections created by the linker. */
1506
1507typedef struct elf_linker_section
1508{
c9a2f333 1509 /* Pointer to the bfd section. */
25dbc73a 1510 asection *section;
c9a2f333
AM
1511 /* Section name. */
1512 const char *name;
1513 /* Associated bss section name. */
1514 const char *bss_name;
1515 /* Associated symbol name. */
1516 const char *sym_name;
1517 /* Value of symbol. */
1518 bfd_vma sym_val;
25dbc73a
AM
1519} elf_linker_section_t;
1520
1521/* Linked list of allocated pointer entries. This hangs off of the
1522 symbol lists, and provides allows us to return different pointers,
1523 based on different addend's. */
1524
1525typedef struct elf_linker_section_pointers
1526{
1527 /* next allocated pointer for this symbol */
1528 struct elf_linker_section_pointers *next;
1529 /* offset of pointer from beginning of section */
1530 bfd_vma offset;
1531 /* addend used */
1532 bfd_vma addend;
1533 /* which linker section this is */
1534 elf_linker_section_t *lsect;
25dbc73a
AM
1535} elf_linker_section_pointers_t;
1536
1537struct ppc_elf_obj_tdata
1538{
1539 struct elf_obj_tdata elf;
1540
1541 /* A mapping from local symbols to offsets into the various linker
1542 sections added. This is index by the symbol index. */
1543 elf_linker_section_pointers_t **linker_section_pointers;
1544};
1545
1546#define ppc_elf_tdata(bfd) \
1547 ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
7619e7c7 1548
25dbc73a
AM
1549#define elf_local_ptr_offsets(bfd) \
1550 (ppc_elf_tdata (bfd)->linker_section_pointers)
7619e7c7 1551
25dbc73a 1552/* Override the generic function because we store some extras. */
7619e7c7 1553
25dbc73a
AM
1554static bfd_boolean
1555ppc_elf_mkobject (bfd *abfd)
1556{
1557 bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
1558 abfd->tdata.any = bfd_zalloc (abfd, amt);
1559 if (abfd->tdata.any == NULL)
1560 return FALSE;
1561 return TRUE;
1562}
7619e7c7 1563
25dbc73a
AM
1564/* Fix bad default arch selected for a 32 bit input bfd when the
1565 default is 64 bit. */
7619e7c7 1566
25dbc73a
AM
1567static bfd_boolean
1568ppc_elf_object_p (bfd *abfd)
1569{
1570 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1571 {
1572 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
7619e7c7 1573
25dbc73a
AM
1574 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1575 {
1576 /* Relies on arch after 64 bit default being 32 bit default. */
1577 abfd->arch_info = abfd->arch_info->next;
1578 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1579 }
1580 }
1581 return TRUE;
1582}
7619e7c7 1583
25dbc73a 1584/* Function to set whether a module needs the -mrelocatable bit set. */
7619e7c7 1585
25dbc73a
AM
1586static bfd_boolean
1587ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1588{
1589 BFD_ASSERT (!elf_flags_init (abfd)
1590 || elf_elfheader (abfd)->e_flags == flags);
7619e7c7 1591
25dbc73a
AM
1592 elf_elfheader (abfd)->e_flags = flags;
1593 elf_flags_init (abfd) = TRUE;
1594 return TRUE;
1595}
1596
25dbc73a 1597/* Support for core dump NOTE sections. */
deaaf2f3 1598
b34976b6 1599static bfd_boolean
25dbc73a 1600ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 1601{
25dbc73a
AM
1602 int offset;
1603 unsigned int size;
252b5132 1604
25dbc73a
AM
1605 switch (note->descsz)
1606 {
1607 default:
1608 return FALSE;
252b5132 1609
25dbc73a
AM
1610 case 268: /* Linux/PPC. */
1611 /* pr_cursig */
1612 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
deaaf2f3 1613
25dbc73a
AM
1614 /* pr_pid */
1615 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
9abc968f 1616
25dbc73a
AM
1617 /* pr_reg */
1618 offset = 72;
1619 size = 192;
deaaf2f3 1620
25dbc73a
AM
1621 break;
1622 }
deaaf2f3 1623
25dbc73a
AM
1624 /* Make a ".reg/999" section. */
1625 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1626 size, note->descpos + offset);
1627}
252b5132 1628
25dbc73a
AM
1629static bfd_boolean
1630ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1631{
1632 switch (note->descsz)
deaaf2f3 1633 {
25dbc73a
AM
1634 default:
1635 return FALSE;
deaaf2f3 1636
25dbc73a
AM
1637 case 128: /* Linux/PPC elf_prpsinfo. */
1638 elf_tdata (abfd)->core_program
1639 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1640 elf_tdata (abfd)->core_command
1641 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1642 }
9abc968f 1643
25dbc73a
AM
1644 /* Note that for some reason, a spurious space is tacked
1645 onto the end of the args in some (at least one anyway)
1646 implementations, so strip it off if it exists. */
70bccea4 1647
25dbc73a
AM
1648 {
1649 char *command = elf_tdata (abfd)->core_command;
1650 int n = strlen (command);
70bccea4 1651
25dbc73a
AM
1652 if (0 < n && command[n - 1] == ' ')
1653 command[n - 1] = '\0';
1654 }
deaaf2f3 1655
25dbc73a
AM
1656 return TRUE;
1657}
deaaf2f3 1658
25dbc73a
AM
1659/* Return address for Ith PLT stub in section PLT, for relocation REL
1660 or (bfd_vma) -1 if it should not be included. */
deaaf2f3 1661
25dbc73a
AM
1662static bfd_vma
1663ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1664 const asection *plt ATTRIBUTE_UNUSED,
1665 const arelent *rel)
1666{
1667 return rel->address;
1668}
deaaf2f3 1669
25dbc73a 1670/* Handle a PowerPC specific section when reading an object file. This
6dc132d9
L
1671 is called when bfd_section_from_shdr finds a section with an unknown
1672 type. */
deaaf2f3 1673
25dbc73a 1674static bfd_boolean
6dc132d9
L
1675ppc_elf_section_from_shdr (bfd *abfd,
1676 Elf_Internal_Shdr *hdr,
1677 const char *name,
1678 int shindex)
25dbc73a
AM
1679{
1680 asection *newsect;
1681 flagword flags;
d1c6de6f 1682
6dc132d9 1683 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
25dbc73a 1684 return FALSE;
deaaf2f3 1685
25dbc73a
AM
1686 newsect = hdr->bfd_section;
1687 flags = bfd_get_section_flags (abfd, newsect);
1688 if (hdr->sh_flags & SHF_EXCLUDE)
1689 flags |= SEC_EXCLUDE;
9abc968f 1690
25dbc73a
AM
1691 if (hdr->sh_type == SHT_ORDERED)
1692 flags |= SEC_SORT_ENTRIES;
d1c6de6f 1693
25dbc73a
AM
1694 bfd_set_section_flags (abfd, newsect, flags);
1695 return TRUE;
1696}
1697
1698/* Set up any other section flags and such that may be necessary. */
1699
1700static bfd_boolean
1701ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1702 Elf_Internal_Shdr *shdr,
1703 asection *asect)
1704{
1705 if ((asect->flags & SEC_EXCLUDE) != 0)
1706 shdr->sh_flags |= SHF_EXCLUDE;
1707
1708 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1709 shdr->sh_type = SHT_ORDERED;
1710
1711 return TRUE;
1712}
1713
1714/* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
1715 need to bump up the number of section headers. */
1716
1717static int
1718ppc_elf_additional_program_headers (bfd *abfd)
1719{
1720 asection *s;
1721 int ret = 0;
d1c6de6f 1722
25dbc73a
AM
1723 s = bfd_get_section_by_name (abfd, ".sbss2");
1724 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1725 ++ret;
d1c6de6f 1726
25dbc73a
AM
1727 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1728 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1729 ++ret;
deaaf2f3 1730
25dbc73a
AM
1731 return ret;
1732}
deaaf2f3 1733
25dbc73a
AM
1734/* Add extra PPC sections -- Note, for now, make .sbss2 and
1735 .PPC.EMB.sbss0 a normal section, and not a bss section so
1736 that the linker doesn't crater when trying to make more than
1737 2 sections. */
e656e369 1738
25dbc73a
AM
1739static struct bfd_elf_special_section const ppc_elf_special_sections[]=
1740{
1741 { ".tags", 5, 0, SHT_ORDERED, SHF_ALLOC },
1742 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1743 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1744 { ".sdata2", 7, -2, SHT_PROGBITS, SHF_ALLOC },
1745 { ".sbss2", 6, -2, SHT_PROGBITS, SHF_ALLOC },
1746 { ".PPC.EMB.apuinfo", 16, 0, SHT_NOTE, 0 },
1747 { ".PPC.EMB.sdata0", 15, 0, SHT_PROGBITS, SHF_ALLOC },
1748 { ".PPC.EMB.sbss0", 14, 0, SHT_PROGBITS, SHF_ALLOC },
1749 { ".plt", 4, 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
1750 { NULL, 0, 0, 0, 0 }
1751};
1752\f
1753/* Very simple linked list structure for recording apuinfo values. */
1754typedef struct apuinfo_list
1755{
1756 struct apuinfo_list *next;
1757 unsigned long value;
1758}
1759apuinfo_list;
e656e369 1760
25dbc73a 1761static apuinfo_list *head;
deaaf2f3 1762
deaaf2f3 1763
25dbc73a
AM
1764static void
1765apuinfo_list_init (void)
1766{
1767 head = NULL;
1768}
9abc968f 1769
25dbc73a
AM
1770static void
1771apuinfo_list_add (unsigned long value)
1772{
1773 apuinfo_list *entry = head;
deaaf2f3 1774
25dbc73a
AM
1775 while (entry != NULL)
1776 {
1777 if (entry->value == value)
1778 return;
1779 entry = entry->next;
1780 }
b4a38de6 1781
25dbc73a
AM
1782 entry = bfd_malloc (sizeof (* entry));
1783 if (entry == NULL)
1784 return;
e656e369 1785
25dbc73a
AM
1786 entry->value = value;
1787 entry->next = head;
1788 head = entry;
1789}
9abc968f 1790
25dbc73a
AM
1791static unsigned
1792apuinfo_list_length (void)
1793{
1794 apuinfo_list *entry;
1795 unsigned long count;
9abc968f 1796
25dbc73a
AM
1797 for (entry = head, count = 0;
1798 entry;
1799 entry = entry->next)
1800 ++ count;
e656e369 1801
25dbc73a
AM
1802 return count;
1803}
eea6121a 1804
25dbc73a
AM
1805static inline unsigned long
1806apuinfo_list_element (unsigned long number)
1807{
1808 apuinfo_list * entry;
e656e369 1809
25dbc73a
AM
1810 for (entry = head;
1811 entry && number --;
1812 entry = entry->next)
1813 ;
252b5132 1814
25dbc73a
AM
1815 return entry ? entry->value : 0;
1816}
1817
1818static void
1819apuinfo_list_finish (void)
1820{
1821 apuinfo_list *entry;
1822
1823 for (entry = head; entry;)
252b5132 1824 {
25dbc73a
AM
1825 apuinfo_list *next = entry->next;
1826 free (entry);
1827 entry = next;
1828 }
9abc968f 1829
25dbc73a
AM
1830 head = NULL;
1831}
9abc968f 1832
25dbc73a
AM
1833#define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
1834#define APUINFO_LABEL "APUinfo"
9abc968f 1835
25dbc73a
AM
1836/* Scan the input BFDs and create a linked list of
1837 the APUinfo values that will need to be emitted. */
9abc968f 1838
25dbc73a
AM
1839static void
1840ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
1841{
1842 bfd *ibfd;
1843 asection *asec;
1844 char *buffer;
1845 unsigned num_input_sections;
1846 bfd_size_type output_section_size;
1847 unsigned i;
1848 unsigned num_entries;
1849 unsigned long offset;
1850 unsigned long length;
1851 const char *error_message = NULL;
9abc968f 1852
25dbc73a
AM
1853 if (link_info == NULL)
1854 return;
9abc968f 1855
25dbc73a
AM
1856 /* Scan the input bfds, looking for apuinfo sections. */
1857 num_input_sections = 0;
1858 output_section_size = 0;
252b5132 1859
25dbc73a 1860 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
252b5132 1861 {
25dbc73a
AM
1862 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1863 if (asec)
252b5132 1864 {
25dbc73a
AM
1865 ++ num_input_sections;
1866 output_section_size += asec->size;
252b5132 1867 }
252b5132
RH
1868 }
1869
25dbc73a
AM
1870 /* We need at least one input sections
1871 in order to make merging worthwhile. */
1872 if (num_input_sections < 1)
1873 return;
deaaf2f3 1874
25dbc73a
AM
1875 /* Just make sure that the output section exists as well. */
1876 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1877 if (asec == NULL)
1878 return;
252b5132 1879
25dbc73a
AM
1880 /* Allocate a buffer for the contents of the input sections. */
1881 buffer = bfd_malloc (output_section_size);
1882 if (buffer == NULL)
1883 return;
252b5132 1884
25dbc73a
AM
1885 offset = 0;
1886 apuinfo_list_init ();
252b5132 1887
25dbc73a
AM
1888 /* Read in the input sections contents. */
1889 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
252b5132 1890 {
25dbc73a
AM
1891 unsigned long datum;
1892 char *ptr;
252b5132 1893
25dbc73a
AM
1894 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1895 if (asec == NULL)
1896 continue;
252b5132 1897
25dbc73a
AM
1898 length = asec->size;
1899 if (length < 24)
1900 {
1901 error_message = _("corrupt or empty %s section in %B");
1902 goto fail;
1903 }
252b5132 1904
25dbc73a
AM
1905 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
1906 || (bfd_bread (buffer + offset, length, ibfd) != length))
1907 {
1908 error_message = _("unable to read in %s section from %B");
1909 goto fail;
1910 }
252b5132 1911
25dbc73a
AM
1912 /* Process the contents of the section. */
1913 ptr = buffer + offset;
1914 error_message = _("corrupt %s section in %B");
252b5132 1915
25dbc73a
AM
1916 /* Verify the contents of the header. Note - we have to
1917 extract the values this way in order to allow for a
1918 host whose endian-ness is different from the target. */
1919 datum = bfd_get_32 (ibfd, ptr);
1920 if (datum != sizeof APUINFO_LABEL)
1921 goto fail;
252b5132 1922
25dbc73a
AM
1923 datum = bfd_get_32 (ibfd, ptr + 8);
1924 if (datum != 0x2)
1925 goto fail;
252b5132 1926
25dbc73a
AM
1927 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
1928 goto fail;
252b5132 1929
25dbc73a
AM
1930 /* Get the number of bytes used for apuinfo entries. */
1931 datum = bfd_get_32 (ibfd, ptr + 4);
1932 if (datum + 20 != length)
1933 goto fail;
1934
1935 /* Make sure that we do not run off the end of the section. */
1936 if (offset + length > output_section_size)
1937 goto fail;
1938
1939 /* Scan the apuinfo section, building a list of apuinfo numbers. */
1940 for (i = 0; i < datum; i += 4)
1941 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
1942
1943 /* Update the offset. */
1944 offset += length;
252b5132
RH
1945 }
1946
25dbc73a 1947 error_message = NULL;
252b5132 1948
25dbc73a
AM
1949 /* Compute the size of the output section. */
1950 num_entries = apuinfo_list_length ();
1951 output_section_size = 20 + num_entries * 4;
252b5132 1952
25dbc73a
AM
1953 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1954
1955 if (! bfd_set_section_size (abfd, asec, output_section_size))
1956 ibfd = abfd,
1957 error_message = _("warning: unable to set size of %s section in %B");
1958
1959 fail:
1960 free (buffer);
1961
1962 if (error_message)
1963 (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
1964}
252b5132 1965
25dbc73a
AM
1966/* Prevent the output section from accumulating the input sections'
1967 contents. We have already stored this in our linked list structure. */
252b5132 1968
25dbc73a
AM
1969static bfd_boolean
1970ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
1971 asection *asec,
1972 bfd_byte *contents ATTRIBUTE_UNUSED)
1973{
1974 return (apuinfo_list_length ()
1975 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
252b5132
RH
1976}
1977
25dbc73a
AM
1978/* Finally we can generate the output section. */
1979
1980static void
1981ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
7619e7c7 1982{
25dbc73a
AM
1983 bfd_byte *buffer;
1984 asection *asec;
1985 unsigned i;
1986 unsigned num_entries;
1987 bfd_size_type length;
7619e7c7 1988
25dbc73a
AM
1989 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1990 if (asec == NULL)
1991 return;
1992
1993 if (apuinfo_list_length () == 0)
1994 return;
1995
1996 length = asec->size;
1997 if (length < 20)
1998 return;
1999
2000 buffer = bfd_malloc (length);
2001 if (buffer == NULL)
7619e7c7 2002 {
25dbc73a
AM
2003 (*_bfd_error_handler)
2004 (_("failed to allocate space for new APUinfo section."));
2005 return;
7619e7c7 2006 }
7619e7c7 2007
25dbc73a
AM
2008 /* Create the apuinfo header. */
2009 num_entries = apuinfo_list_length ();
2010 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2011 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2012 bfd_put_32 (abfd, 0x2, buffer + 8);
2013 strcpy ((char *) buffer + 12, APUINFO_LABEL);
feee612b 2014
25dbc73a
AM
2015 length = 20;
2016 for (i = 0; i < num_entries; i++)
feee612b 2017 {
25dbc73a
AM
2018 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2019 length += 4;
feee612b 2020 }
feee612b 2021
25dbc73a
AM
2022 if (length != asec->size)
2023 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
252b5132 2024
25dbc73a
AM
2025 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2026 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
252b5132 2027
25dbc73a
AM
2028 free (buffer);
2029
2030 apuinfo_list_finish ();
252b5132 2031}
25dbc73a
AM
2032\f
2033/* The following functions are specific to the ELF linker, while
2034 functions above are used generally. They appear in this file more
2035 or less in the order in which they are called. eg.
2036 ppc_elf_check_relocs is called early in the link process,
2037 ppc_elf_finish_dynamic_sections is one of the last functions
2038 called. */
252b5132 2039
25dbc73a
AM
2040/* The PPC linker needs to keep track of the number of relocs that it
2041 decides to copy as dynamic relocs in check_relocs for each symbol.
2042 This is so that it can later discard them if they are found to be
2043 unnecessary. We store the information in a field extending the
2044 regular ELF linker hash table. */
ae9a127f 2045
25dbc73a 2046struct ppc_elf_dyn_relocs
252b5132 2047{
25dbc73a 2048 struct ppc_elf_dyn_relocs *next;
252b5132 2049
25dbc73a
AM
2050 /* The input section of the reloc. */
2051 asection *sec;
252b5132 2052
25dbc73a
AM
2053 /* Total number of relocs copied for the input section. */
2054 bfd_size_type count;
252b5132 2055
25dbc73a
AM
2056 /* Number of pc-relative relocs copied for the input section. */
2057 bfd_size_type pc_count;
2058};
252b5132 2059
25dbc73a
AM
2060/* Of those relocs that might be copied as dynamic relocs, this macro
2061 selects those that must be copied when linking a shared library,
2062 even when the symbol is local. */
252b5132 2063
25dbc73a
AM
2064#define MUST_BE_DYN_RELOC(RTYPE) \
2065 ((RTYPE) != R_PPC_REL24 \
2066 && (RTYPE) != R_PPC_REL14 \
2067 && (RTYPE) != R_PPC_REL14_BRTAKEN \
2068 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
2069 && (RTYPE) != R_PPC_REL32)
252b5132 2070
25dbc73a
AM
2071/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2072 copying dynamic variables from a shared lib into an app's dynbss
2073 section, and instead use a dynamic relocation to point into the
2074 shared lib. */
2075#define ELIMINATE_COPY_RELOCS 1
252b5132 2076
25dbc73a 2077/* PPC ELF linker hash entry. */
252b5132 2078
25dbc73a
AM
2079struct ppc_elf_link_hash_entry
2080{
2081 struct elf_link_hash_entry elf;
252b5132 2082
25dbc73a
AM
2083 /* If this symbol is used in the linker created sections, the processor
2084 specific backend uses this field to map the field into the offset
2085 from the beginning of the section. */
2086 elf_linker_section_pointers_t *linker_section_pointer;
252b5132 2087
25dbc73a
AM
2088 /* Track dynamic relocs copied for this symbol. */
2089 struct ppc_elf_dyn_relocs *dyn_relocs;
252b5132 2090
25dbc73a
AM
2091 /* Contexts in which symbol is used in the GOT (or TOC).
2092 TLS_GD .. TLS_TLS bits are or'd into the mask as the
2093 corresponding relocs are encountered during check_relocs.
2094 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2095 indicate the corresponding GOT entry type is not needed. */
2096#define TLS_GD 1 /* GD reloc. */
2097#define TLS_LD 2 /* LD reloc. */
2098#define TLS_TPREL 4 /* TPREL reloc, => IE. */
2099#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2100#define TLS_TLS 16 /* Any TLS reloc. */
2101#define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
2102 char tls_mask;
2103};
252b5132 2104
25dbc73a
AM
2105#define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2106
2107/* PPC ELF linker hash table. */
2108
2109struct ppc_elf_link_hash_table
2110{
2111 struct elf_link_hash_table elf;
2112
2113 /* Short-cuts to get to dynamic linker sections. */
2114 asection *got;
2115 asection *relgot;
2116 asection *plt;
2117 asection *relplt;
2118 asection *dynbss;
2119 asection *relbss;
2120 asection *dynsbss;
2121 asection *relsbss;
c9a2f333 2122 elf_linker_section_t sdata[2];
25dbc73a
AM
2123 asection *sbss;
2124
2125 /* Shortcut to .__tls_get_addr. */
2126 struct elf_link_hash_entry *tls_get_addr;
252b5132 2127
25dbc73a
AM
2128 /* TLS local dynamic got entry handling. */
2129 union {
2130 bfd_signed_vma refcount;
2131 bfd_vma offset;
2132 } tlsld_got;
252b5132 2133
25dbc73a
AM
2134 /* Small local sym to section mapping cache. */
2135 struct sym_sec_cache sym_sec;
2136};
252b5132 2137
25dbc73a 2138/* Get the PPC ELF linker hash table from a link_info structure. */
252b5132 2139
25dbc73a
AM
2140#define ppc_elf_hash_table(p) \
2141 ((struct ppc_elf_link_hash_table *) (p)->hash)
252b5132 2142
25dbc73a 2143/* Create an entry in a PPC ELF linker hash table. */
252b5132 2144
25dbc73a
AM
2145static struct bfd_hash_entry *
2146ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2147 struct bfd_hash_table *table,
2148 const char *string)
252b5132 2149{
25dbc73a
AM
2150 /* Allocate the structure if it has not already been allocated by a
2151 subclass. */
2152 if (entry == NULL)
2153 {
2154 entry = bfd_hash_allocate (table,
2155 sizeof (struct ppc_elf_link_hash_entry));
2156 if (entry == NULL)
2157 return entry;
2158 }
252b5132 2159
25dbc73a
AM
2160 /* Call the allocation method of the superclass. */
2161 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2162 if (entry != NULL)
2163 {
2164 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2165 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2166 ppc_elf_hash_entry (entry)->tls_mask = 0;
2167 }
252b5132 2168
25dbc73a 2169 return entry;
252b5132 2170}
3dab13f6 2171
25dbc73a 2172/* Create a PPC ELF linker hash table. */
3dab13f6 2173
25dbc73a
AM
2174static struct bfd_link_hash_table *
2175ppc_elf_link_hash_table_create (bfd *abfd)
3dab13f6 2176{
25dbc73a 2177 struct ppc_elf_link_hash_table *ret;
3dab13f6 2178
25dbc73a
AM
2179 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2180 if (ret == NULL)
2181 return NULL;
3dab13f6 2182
25dbc73a
AM
2183 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
2184 ppc_elf_link_hash_newfunc))
3dab13f6 2185 {
25dbc73a
AM
2186 free (ret);
2187 return NULL;
2188 }
58111eb7 2189
c9a2f333
AM
2190 ret->sdata[0].name = ".sdata";
2191 ret->sdata[0].sym_name = "_SDA_BASE_";
2192 ret->sdata[0].bss_name = ".sbss";
2193
2194 ret->sdata[1].name = ".sdata2";
2195 ret->sdata[1].sym_name = "_SDA2_BASE_";
2196 ret->sdata[1].bss_name = ".sbss2";
2197
25dbc73a
AM
2198 return &ret->elf.root;
2199}
3dab13f6 2200
25dbc73a 2201/* The powerpc .got has a blrl instruction in it. Mark it executable. */
3dab13f6 2202
25dbc73a
AM
2203static bfd_boolean
2204ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2205{
2206 struct ppc_elf_link_hash_table *htab;
2207 asection *s;
2208 flagword flags;
3dab13f6 2209
25dbc73a
AM
2210 if (!_bfd_elf_create_got_section (abfd, info))
2211 return FALSE;
3dab13f6 2212
25dbc73a
AM
2213 htab = ppc_elf_hash_table (info);
2214 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2215 if (s == NULL)
2216 abort ();
3dab13f6 2217
25dbc73a
AM
2218 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2219 | SEC_LINKER_CREATED);
2220 if (!bfd_set_section_flags (abfd, s, flags))
2221 return FALSE;
3dab13f6 2222
25dbc73a
AM
2223 htab->relgot = bfd_make_section (abfd, ".rela.got");
2224 if (!htab->relgot
2225 || ! bfd_set_section_flags (abfd, htab->relgot,
2226 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2227 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2228 | SEC_READONLY))
2229 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2230 return FALSE;
3dab13f6 2231
25dbc73a
AM
2232 return TRUE;
2233}
3dab13f6 2234
25dbc73a
AM
2235/* We have to create .dynsbss and .rela.sbss here so that they get mapped
2236 to output sections (just like _bfd_elf_create_dynamic_sections has
2237 to create .dynbss and .rela.bss). */
3dab13f6 2238
25dbc73a
AM
2239static bfd_boolean
2240ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2241{
2242 struct ppc_elf_link_hash_table *htab;
2243 asection *s;
2244 flagword flags;
3dab13f6 2245
25dbc73a 2246 htab = ppc_elf_hash_table (info);
3dab13f6 2247
25dbc73a
AM
2248 if (htab->got == NULL
2249 && !ppc_elf_create_got (abfd, info))
3dab13f6
AM
2250 return FALSE;
2251
25dbc73a
AM
2252 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2253 return FALSE;
3dab13f6 2254
25dbc73a
AM
2255 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2256 | SEC_LINKER_CREATED);
3dab13f6 2257
25dbc73a
AM
2258 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2259 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2260 if (s == NULL
2261 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
2262 return FALSE;
3dab13f6 2263
25dbc73a
AM
2264 if (! info->shared)
2265 {
2266 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2267 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2268 if (s == NULL
2269 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2270 || ! bfd_set_section_alignment (abfd, s, 2))
2271 return FALSE;
2272 }
3dab13f6 2273
25dbc73a
AM
2274 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2275 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2276 if (s == NULL)
2277 abort ();
3dab13f6 2278
25dbc73a
AM
2279 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2280 return bfd_set_section_flags (abfd, s, flags);
2281}
3dab13f6 2282
25dbc73a 2283/* Copy the extra info we tack onto an elf_link_hash_entry. */
58111eb7 2284
25dbc73a
AM
2285static void
2286ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
2287 struct elf_link_hash_entry *dir,
2288 struct elf_link_hash_entry *ind)
2289{
2290 struct ppc_elf_link_hash_entry *edir, *eind;
3dab13f6 2291
25dbc73a
AM
2292 edir = (struct ppc_elf_link_hash_entry *) dir;
2293 eind = (struct ppc_elf_link_hash_entry *) ind;
3dab13f6 2294
25dbc73a
AM
2295 if (eind->dyn_relocs != NULL)
2296 {
2297 if (edir->dyn_relocs != NULL)
3dab13f6 2298 {
25dbc73a
AM
2299 struct ppc_elf_dyn_relocs **pp;
2300 struct ppc_elf_dyn_relocs *p;
3dab13f6 2301
25dbc73a
AM
2302 if (ind->root.type == bfd_link_hash_indirect)
2303 abort ();
2304
2305 /* Add reloc counts against the weak sym to the strong sym
2306 list. Merge any entries against the same section. */
2307 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3dab13f6 2308 {
25dbc73a
AM
2309 struct ppc_elf_dyn_relocs *q;
2310
2311 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2312 if (q->sec == p->sec)
2313 {
2314 q->pc_count += p->pc_count;
2315 q->count += p->count;
2316 *pp = p->next;
2317 break;
2318 }
2319 if (q == NULL)
2320 pp = &p->next;
3dab13f6 2321 }
25dbc73a 2322 *pp = edir->dyn_relocs;
3dab13f6 2323 }
25dbc73a
AM
2324
2325 edir->dyn_relocs = eind->dyn_relocs;
2326 eind->dyn_relocs = NULL;
3dab13f6 2327 }
3dab13f6 2328
25dbc73a 2329 edir->tls_mask |= eind->tls_mask;
3dab13f6 2330
25dbc73a
AM
2331 if (ELIMINATE_COPY_RELOCS
2332 && ind->root.type != bfd_link_hash_indirect
2333 && dir->dynamic_adjusted)
2334 {
2335 /* If called to transfer flags for a weakdef during processing
2336 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
2337 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
2338 dir->ref_dynamic |= ind->ref_dynamic;
2339 dir->ref_regular |= ind->ref_regular;
2340 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
2341 dir->needs_plt |= ind->needs_plt;
2342 }
2343 else
2344 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
2345}
3dab13f6 2346
8853c3d3
AM
2347/* Return 1 if target is one of ours. */
2348
2349static bfd_boolean
2350is_ppc_elf_target (const struct bfd_target *targ)
2351{
2352 extern const bfd_target bfd_elf32_powerpc_vec;
2353 extern const bfd_target bfd_elf32_powerpcle_vec;
2354
2355 return targ == &bfd_elf32_powerpc_vec || targ == &bfd_elf32_powerpcle_vec;
2356}
2357
25dbc73a
AM
2358/* Hook called by the linker routine which adds symbols from an object
2359 file. We use it to put .comm items in .sbss, and not .bss. */
58111eb7 2360
25dbc73a
AM
2361static bfd_boolean
2362ppc_elf_add_symbol_hook (bfd *abfd,
2363 struct bfd_link_info *info,
2364 Elf_Internal_Sym *sym,
2365 const char **namep ATTRIBUTE_UNUSED,
2366 flagword *flagsp ATTRIBUTE_UNUSED,
2367 asection **secp,
2368 bfd_vma *valp)
2369{
2370 if (sym->st_shndx == SHN_COMMON
2371 && !info->relocatable
2372 && sym->st_size <= elf_gp_size (abfd)
d2663f46 2373 && is_ppc_elf_target (info->hash->creator))
25dbc73a
AM
2374 {
2375 /* Common symbols less than or equal to -G nn bytes are automatically
2376 put into .sbss. */
2377 struct ppc_elf_link_hash_table *htab;
3dab13f6 2378
25dbc73a
AM
2379 htab = ppc_elf_hash_table (info);
2380 if (htab->sbss == NULL)
2381 {
644285ef 2382 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
3dab13f6 2383
644285ef
AM
2384 if (!htab->elf.dynobj)
2385 htab->elf.dynobj = abfd;
2386
2387 htab->sbss = bfd_make_section_anyway (htab->elf.dynobj, ".sbss");
25dbc73a 2388 if (htab->sbss == NULL
644285ef 2389 || ! bfd_set_section_flags (htab->elf.dynobj, htab->sbss, flags))
25dbc73a 2390 return FALSE;
3dab13f6 2391 }
3dab13f6 2392
25dbc73a
AM
2393 *secp = htab->sbss;
2394 *valp = sym->st_size;
2395 }
3dab13f6 2396
25dbc73a 2397 return TRUE;
3dab13f6
AM
2398}
2399\f
c9a2f333 2400/* Create a special linker section. */
25dbc73a 2401
c9a2f333 2402static bfd_boolean
55fd94b0
AM
2403ppc_elf_create_linker_section (bfd *abfd,
2404 struct bfd_link_info *info,
c9a2f333
AM
2405 flagword flags,
2406 elf_linker_section_t *lsect)
252b5132 2407{
58111eb7 2408 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
c9a2f333 2409 asection *s;
58111eb7 2410
c9a2f333
AM
2411 flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2412 | SEC_LINKER_CREATED);
58111eb7
AM
2413
2414 /* Record the first bfd that needs the special sections. */
2415 if (!htab->elf.dynobj)
2416 htab->elf.dynobj = abfd;
2417
76750a2f
AM
2418 s = bfd_make_section_anyway (htab->elf.dynobj, lsect->name);
2419 if (s == NULL
2420 || !bfd_set_section_flags (htab->elf.dynobj, s, flags)
2421 || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
c9a2f333 2422 return FALSE;
76750a2f 2423 lsect->section = s;
58111eb7 2424
c9a2f333 2425 return TRUE;
252b5132 2426}
252b5132 2427
25dbc73a
AM
2428/* Find a linker generated pointer with a given addend and type. */
2429
2430static elf_linker_section_pointers_t *
2431elf_find_pointer_linker_section
2432 (elf_linker_section_pointers_t *linker_pointers,
2433 bfd_vma addend,
2434 elf_linker_section_t *lsect)
252b5132 2435{
25dbc73a
AM
2436 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2437 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2438 return linker_pointers;
252b5132 2439
25dbc73a
AM
2440 return NULL;
2441}
2442
2443/* Allocate a pointer to live in a linker created section. */
2444
2445static bfd_boolean
2446elf_create_pointer_linker_section (bfd *abfd,
25dbc73a
AM
2447 elf_linker_section_t *lsect,
2448 struct elf_link_hash_entry *h,
2449 const Elf_Internal_Rela *rel)
2450{
2451 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2452 elf_linker_section_pointers_t *linker_section_ptr;
2453 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2454 bfd_size_type amt;
2455
2456 BFD_ASSERT (lsect != NULL);
2457
2458 /* Is this a global symbol? */
2459 if (h != NULL)
2460 {
2461 struct ppc_elf_link_hash_entry *eh;
2462
2463 /* Has this symbol already been allocated? If so, our work is done. */
2464 eh = (struct ppc_elf_link_hash_entry *) h;
2465 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2466 rel->r_addend,
2467 lsect))
2468 return TRUE;
2469
2470 ptr_linker_section_ptr = &eh->linker_section_pointer;
25dbc73a
AM
2471 }
2472 else
2473 {
2474 /* Allocation of a pointer to a local symbol. */
2475 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2476
2477 /* Allocate a table to hold the local symbols if first time. */
2478 if (!ptr)
2479 {
2480 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2481
2482 amt = num_symbols;
2483 amt *= sizeof (elf_linker_section_pointers_t *);
2484 ptr = bfd_zalloc (abfd, amt);
2485
2486 if (!ptr)
2487 return FALSE;
2488
2489 elf_local_ptr_offsets (abfd) = ptr;
2490 }
2491
2492 /* Has this symbol already been allocated? If so, our work is done. */
2493 if (elf_find_pointer_linker_section (ptr[r_symndx],
2494 rel->r_addend,
2495 lsect))
2496 return TRUE;
252b5132 2497
25dbc73a 2498 ptr_linker_section_ptr = &ptr[r_symndx];
25dbc73a 2499 }
41fcb14e 2500
25dbc73a
AM
2501 /* Allocate space for a pointer in the linker section, and allocate
2502 a new pointer record from internal memory. */
2503 BFD_ASSERT (ptr_linker_section_ptr != NULL);
2504 amt = sizeof (elf_linker_section_pointers_t);
2505 linker_section_ptr = bfd_alloc (abfd, amt);
41fcb14e 2506
25dbc73a 2507 if (!linker_section_ptr)
7619e7c7 2508 return FALSE;
41fcb14e 2509
25dbc73a
AM
2510 linker_section_ptr->next = *ptr_linker_section_ptr;
2511 linker_section_ptr->addend = rel->r_addend;
2512 linker_section_ptr->lsect = lsect;
25dbc73a 2513 *ptr_linker_section_ptr = linker_section_ptr;
41fcb14e 2514
25dbc73a
AM
2515 linker_section_ptr->offset = lsect->section->size;
2516 lsect->section->size += 4;
7619e7c7 2517
25dbc73a
AM
2518#ifdef DEBUG
2519 fprintf (stderr,
2520 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2521 lsect->name, (long) linker_section_ptr->offset,
2522 (long) lsect->section->size);
2523#endif
7619e7c7
AM
2524
2525 return TRUE;
41fcb14e
AM
2526}
2527
b34976b6 2528static bfd_boolean
25dbc73a
AM
2529update_local_sym_info (bfd *abfd,
2530 Elf_Internal_Shdr *symtab_hdr,
2531 unsigned long r_symndx,
2532 int tls_type)
252b5132 2533{
25dbc73a
AM
2534 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
2535 char *local_got_tls_masks;
252b5132 2536
25dbc73a 2537 if (local_got_refcounts == NULL)
252b5132 2538 {
25dbc73a
AM
2539 bfd_size_type size = symtab_hdr->sh_info;
2540
2541 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
2542 local_got_refcounts = bfd_zalloc (abfd, size);
2543 if (local_got_refcounts == NULL)
b34976b6 2544 return FALSE;
25dbc73a 2545 elf_local_got_refcounts (abfd) = local_got_refcounts;
252b5132 2546 }
41fcb14e 2547
25dbc73a
AM
2548 local_got_refcounts[r_symndx] += 1;
2549 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2550 local_got_tls_masks[r_symndx] |= tls_type;
2551 return TRUE;
2552}
41fcb14e 2553
25dbc73a
AM
2554static void
2555bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
2556{
2557 (*_bfd_error_handler)
2558 (_("%B: relocation %s cannot be used when making a shared object"),
2559 abfd,
2560 ppc_elf_howto_table[r_type]->name);
2561 bfd_set_error (bfd_error_bad_value);
252b5132
RH
2562}
2563
25dbc73a
AM
2564/* Look through the relocs for a section during the first phase, and
2565 allocate space in the global offset table or procedure linkage
2566 table. */
252b5132 2567
b34976b6 2568static bfd_boolean
25dbc73a
AM
2569ppc_elf_check_relocs (bfd *abfd,
2570 struct bfd_link_info *info,
2571 asection *sec,
2572 const Elf_Internal_Rela *relocs)
252b5132 2573{
7619e7c7 2574 struct ppc_elf_link_hash_table *htab;
25dbc73a
AM
2575 Elf_Internal_Shdr *symtab_hdr;
2576 struct elf_link_hash_entry **sym_hashes;
2577 const Elf_Internal_Rela *rel;
2578 const Elf_Internal_Rela *rel_end;
2579 asection *sreloc;
2580
2581 if (info->relocatable)
2582 return TRUE;
252b5132 2583
c87b5a93
AM
2584 /* Don't do anything special with non-loaded, non-alloced sections.
2585 In particular, any relocs in such sections should not affect GOT
2586 and PLT reference counting (ie. we don't allow them to create GOT
2587 or PLT entries), there's no possibility or desire to optimize TLS
2588 relocs, and there's not much point in propagating relocs to shared
2589 libs that the dynamic linker won't relocate. */
2590 if ((sec->flags & SEC_ALLOC) == 0)
2591 return TRUE;
2592
252b5132 2593#ifdef DEBUG
25dbc73a
AM
2594 _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
2595 sec, abfd);
252b5132
RH
2596#endif
2597
25dbc73a
AM
2598 /* Initialize howto table if not already done. */
2599 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2600 ppc_elf_howto_init ();
2601
7619e7c7 2602 htab = ppc_elf_hash_table (info);
25dbc73a
AM
2603 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2604 sym_hashes = elf_sym_hashes (abfd);
2605 sreloc = NULL;
2606
2607 rel_end = relocs + sec->reloc_count;
2608 for (rel = relocs; rel < rel_end; rel++)
2609 {
2610 unsigned long r_symndx;
2611 enum elf_ppc_reloc_type r_type;
2612 struct elf_link_hash_entry *h;
2613 int tls_type = 0;
2614
2615 r_symndx = ELF32_R_SYM (rel->r_info);
2616 if (r_symndx < symtab_hdr->sh_info)
2617 h = NULL;
2618 else
2619 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2620
2621 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2622 This shows up in particular in an R_PPC_ADDR32 in the eabi
2623 startup code. */
2624 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
252b5132 2625 {
25dbc73a
AM
2626 if (htab->got == NULL)
2627 {
2628 if (htab->elf.dynobj == NULL)
2629 htab->elf.dynobj = abfd;
2630 if (!ppc_elf_create_got (htab->elf.dynobj, info))
2631 return FALSE;
2632 }
2633 }
252b5132 2634
25dbc73a
AM
2635 r_type = ELF32_R_TYPE (rel->r_info);
2636 switch (r_type)
2637 {
2638 case R_PPC_GOT_TLSLD16:
2639 case R_PPC_GOT_TLSLD16_LO:
2640 case R_PPC_GOT_TLSLD16_HI:
2641 case R_PPC_GOT_TLSLD16_HA:
2642 htab->tlsld_got.refcount += 1;
2643 tls_type = TLS_TLS | TLS_LD;
2644 goto dogottls;
252b5132 2645
25dbc73a
AM
2646 case R_PPC_GOT_TLSGD16:
2647 case R_PPC_GOT_TLSGD16_LO:
2648 case R_PPC_GOT_TLSGD16_HI:
2649 case R_PPC_GOT_TLSGD16_HA:
2650 tls_type = TLS_TLS | TLS_GD;
2651 goto dogottls;
2652
2653 case R_PPC_GOT_TPREL16:
2654 case R_PPC_GOT_TPREL16_LO:
2655 case R_PPC_GOT_TPREL16_HI:
2656 case R_PPC_GOT_TPREL16_HA:
2657 if (info->shared)
2658 info->flags |= DF_STATIC_TLS;
2659 tls_type = TLS_TLS | TLS_TPREL;
2660 goto dogottls;
2661
2662 case R_PPC_GOT_DTPREL16:
2663 case R_PPC_GOT_DTPREL16_LO:
2664 case R_PPC_GOT_DTPREL16_HI:
2665 case R_PPC_GOT_DTPREL16_HA:
2666 tls_type = TLS_TLS | TLS_DTPREL;
2667 dogottls:
2668 sec->has_tls_reloc = 1;
2669 /* Fall thru */
252b5132 2670
25dbc73a
AM
2671 /* GOT16 relocations */
2672 case R_PPC_GOT16:
2673 case R_PPC_GOT16_LO:
2674 case R_PPC_GOT16_HI:
2675 case R_PPC_GOT16_HA:
2676 /* This symbol requires a global offset table entry. */
2677 if (htab->got == NULL)
2678 {
2679 if (htab->elf.dynobj == NULL)
2680 htab->elf.dynobj = abfd;
2681 if (!ppc_elf_create_got (htab->elf.dynobj, info))
2682 return FALSE;
2683 }
2684 if (h != NULL)
2685 {
2686 h->got.refcount += 1;
2687 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
2688 }
2689 else
2690 /* This is a global offset table entry for a local symbol. */
2691 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
2692 return FALSE;
2693 break;
252b5132 2694
25dbc73a
AM
2695 /* Indirect .sdata relocation. */
2696 case R_PPC_EMB_SDAI16:
2697 if (info->shared)
2698 {
2699 bad_shared_reloc (abfd, r_type);
2700 return FALSE;
2701 }
c9a2f333
AM
2702 if (htab->sdata[0].section == NULL
2703 && !ppc_elf_create_linker_section (abfd, info, 0,
2704 &htab->sdata[0]))
2705 return FALSE;
76750a2f
AM
2706 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
2707 h, rel))
25dbc73a
AM
2708 return FALSE;
2709 break;
252b5132 2710
25dbc73a
AM
2711 /* Indirect .sdata2 relocation. */
2712 case R_PPC_EMB_SDA2I16:
2713 if (info->shared)
2714 {
2715 bad_shared_reloc (abfd, r_type);
2716 return FALSE;
2717 }
c9a2f333
AM
2718 if (htab->sdata[1].section == NULL
2719 && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
2720 &htab->sdata[1]))
2721 return FALSE;
76750a2f
AM
2722 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
2723 h, rel))
25dbc73a
AM
2724 return FALSE;
2725 break;
252b5132 2726
25dbc73a
AM
2727 case R_PPC_SDAREL16:
2728 case R_PPC_EMB_SDA2REL:
2729 case R_PPC_EMB_SDA21:
2730 case R_PPC_EMB_RELSDA:
2731 case R_PPC_EMB_NADDR32:
2732 case R_PPC_EMB_NADDR16:
2733 case R_PPC_EMB_NADDR16_LO:
2734 case R_PPC_EMB_NADDR16_HI:
2735 case R_PPC_EMB_NADDR16_HA:
2736 if (info->shared)
2737 {
2738 bad_shared_reloc (abfd, r_type);
2739 return FALSE;
2740 }
2741 break;
252b5132 2742
25dbc73a
AM
2743 case R_PPC_PLT32:
2744 case R_PPC_PLTREL24:
2745 case R_PPC_PLTREL32:
2746 case R_PPC_PLT16_LO:
2747 case R_PPC_PLT16_HI:
2748 case R_PPC_PLT16_HA:
2749#ifdef DEBUG
2750 fprintf (stderr, "Reloc requires a PLT entry\n");
2751#endif
2752 /* This symbol requires a procedure linkage table entry. We
2753 actually build the entry in finish_dynamic_symbol,
2754 because this might be a case of linking PIC code without
2755 linking in any dynamic objects, in which case we don't
2756 need to generate a procedure linkage table after all. */
7619e7c7 2757
25dbc73a
AM
2758 if (h == NULL)
2759 {
2760 /* It does not make sense to have a procedure linkage
2761 table entry for a local symbol. */
2762 (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
2763 "local symbol"),
2764 abfd,
2765 sec,
2766 (long) rel->r_offset,
2767 ppc_elf_howto_table[r_type]->name);
2768 bfd_set_error (bfd_error_bad_value);
2769 return FALSE;
2770 }
ee05f2fe 2771
25dbc73a
AM
2772 h->needs_plt = 1;
2773 h->plt.refcount++;
2774 break;
ee05f2fe 2775
25dbc73a
AM
2776 /* The following relocations don't need to propagate the
2777 relocation if linking a shared object since they are
2778 section relative. */
2779 case R_PPC_SECTOFF:
2780 case R_PPC_SECTOFF_LO:
2781 case R_PPC_SECTOFF_HI:
2782 case R_PPC_SECTOFF_HA:
2783 case R_PPC_DTPREL16:
2784 case R_PPC_DTPREL16_LO:
2785 case R_PPC_DTPREL16_HI:
2786 case R_PPC_DTPREL16_HA:
2787 case R_PPC_TOC16:
2788 break;
252b5132 2789
25dbc73a
AM
2790 /* This are just markers. */
2791 case R_PPC_TLS:
2792 case R_PPC_EMB_MRKREF:
2793 case R_PPC_NONE:
2794 case R_PPC_max:
2795 break;
252b5132 2796
25dbc73a
AM
2797 /* These should only appear in dynamic objects. */
2798 case R_PPC_COPY:
2799 case R_PPC_GLOB_DAT:
2800 case R_PPC_JMP_SLOT:
2801 case R_PPC_RELATIVE:
2802 break;
252b5132 2803
25dbc73a
AM
2804 /* These aren't handled yet. We'll report an error later. */
2805 case R_PPC_ADDR30:
2806 case R_PPC_EMB_RELSEC16:
2807 case R_PPC_EMB_RELST_LO:
2808 case R_PPC_EMB_RELST_HI:
2809 case R_PPC_EMB_RELST_HA:
2810 case R_PPC_EMB_BIT_FLD:
2811 break;
252b5132 2812
25dbc73a
AM
2813 /* This refers only to functions defined in the shared library. */
2814 case R_PPC_LOCAL24PC:
2815 break;
252b5132 2816
25dbc73a
AM
2817 /* This relocation describes the C++ object vtable hierarchy.
2818 Reconstruct it for later use during GC. */
2819 case R_PPC_GNU_VTINHERIT:
2820 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2821 return FALSE;
2822 break;
252b5132 2823
25dbc73a
AM
2824 /* This relocation describes which C++ vtable entries are actually
2825 used. Record for later use during GC. */
2826 case R_PPC_GNU_VTENTRY:
2827 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2828 return FALSE;
2829 break;
252b5132 2830
25dbc73a
AM
2831 /* We shouldn't really be seeing these. */
2832 case R_PPC_TPREL32:
2833 if (info->shared)
2834 info->flags |= DF_STATIC_TLS;
2835 goto dodyn;
252b5132 2836
25dbc73a
AM
2837 /* Nor these. */
2838 case R_PPC_DTPMOD32:
2839 case R_PPC_DTPREL32:
2840 goto dodyn;
252b5132 2841
25dbc73a
AM
2842 case R_PPC_TPREL16:
2843 case R_PPC_TPREL16_LO:
2844 case R_PPC_TPREL16_HI:
2845 case R_PPC_TPREL16_HA:
2846 if (info->shared)
2847 info->flags |= DF_STATIC_TLS;
2848 goto dodyn;
ee05f2fe 2849
25dbc73a
AM
2850 /* When creating a shared object, we must copy these
2851 relocs into the output file. We create a reloc
2852 section in dynobj and make room for the reloc. */
2853 case R_PPC_REL24:
2854 case R_PPC_REL14:
2855 case R_PPC_REL14_BRTAKEN:
2856 case R_PPC_REL14_BRNTAKEN:
2857 case R_PPC_REL32:
2858 if (h == NULL
2859 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2860 break;
2861 /* fall through */
ee05f2fe 2862
25dbc73a
AM
2863 case R_PPC_ADDR32:
2864 case R_PPC_ADDR24:
2865 case R_PPC_ADDR16:
2866 case R_PPC_ADDR16_LO:
2867 case R_PPC_ADDR16_HI:
2868 case R_PPC_ADDR16_HA:
2869 case R_PPC_ADDR14:
2870 case R_PPC_ADDR14_BRTAKEN:
2871 case R_PPC_ADDR14_BRNTAKEN:
2872 case R_PPC_UADDR32:
2873 case R_PPC_UADDR16:
2874 if (h != NULL && !info->shared)
2875 {
2876 /* We may need a plt entry if the symbol turns out to be
2877 a function defined in a dynamic object. */
2878 h->plt.refcount++;
7fce784e 2879
25dbc73a
AM
2880 /* We may need a copy reloc too. */
2881 h->non_got_ref = 1;
2882 }
7619e7c7 2883
25dbc73a
AM
2884 dodyn:
2885 /* If we are creating a shared library, and this is a reloc
2886 against a global symbol, or a non PC relative reloc
2887 against a local symbol, then we need to copy the reloc
2888 into the shared library. However, if we are linking with
2889 -Bsymbolic, we do not need to copy a reloc against a
2890 global symbol which is defined in an object we are
2891 including in the link (i.e., DEF_REGULAR is set). At
2892 this point we have not seen all the input files, so it is
2893 possible that DEF_REGULAR is not set now but will be set
2894 later (it is never cleared). In case of a weak definition,
2895 DEF_REGULAR may be cleared later by a strong definition in
2896 a shared library. We account for that possibility below by
2897 storing information in the dyn_relocs field of the hash
2898 table entry. A similar situation occurs when creating
2899 shared libraries and symbol visibility changes render the
2900 symbol local.
7619e7c7 2901
25dbc73a
AM
2902 If on the other hand, we are creating an executable, we
2903 may need to keep relocations for symbols satisfied by a
2904 dynamic library if we manage to avoid copy relocs for the
2905 symbol. */
2906 if ((info->shared
2907 && (MUST_BE_DYN_RELOC (r_type)
2908 || (h != NULL
2909 && (! info->symbolic
2910 || h->root.type == bfd_link_hash_defweak
2911 || !h->def_regular))))
2912 || (ELIMINATE_COPY_RELOCS
2913 && !info->shared
25dbc73a
AM
2914 && h != NULL
2915 && (h->root.type == bfd_link_hash_defweak
2916 || !h->def_regular)))
2917 {
2918 struct ppc_elf_dyn_relocs *p;
2919 struct ppc_elf_dyn_relocs **head;
7619e7c7 2920
25dbc73a
AM
2921#ifdef DEBUG
2922 fprintf (stderr,
2923 "ppc_elf_check_relocs needs to "
2924 "create relocation for %s\n",
2925 (h && h->root.root.string
2926 ? h->root.root.string : "<unknown>"));
2927#endif
2928 if (sreloc == NULL)
2929 {
2930 const char *name;
7619e7c7 2931
25dbc73a
AM
2932 name = (bfd_elf_string_from_elf_section
2933 (abfd,
2934 elf_elfheader (abfd)->e_shstrndx,
2935 elf_section_data (sec)->rel_hdr.sh_name));
2936 if (name == NULL)
2937 return FALSE;
7619e7c7 2938
25dbc73a
AM
2939 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2940 && strcmp (bfd_get_section_name (abfd, sec),
2941 name + 5) == 0);
7619e7c7 2942
c9a2f333
AM
2943 if (htab->elf.dynobj == NULL)
2944 htab->elf.dynobj = abfd;
25dbc73a
AM
2945 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
2946 if (sreloc == NULL)
2947 {
2948 flagword flags;
7619e7c7 2949
25dbc73a
AM
2950 sreloc = bfd_make_section (htab->elf.dynobj, name);
2951 flags = (SEC_HAS_CONTENTS | SEC_READONLY
c87b5a93
AM
2952 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2953 | SEC_ALLOC | SEC_LOAD);
25dbc73a
AM
2954 if (sreloc == NULL
2955 || ! bfd_set_section_flags (htab->elf.dynobj,
2956 sreloc, flags)
2957 || ! bfd_set_section_alignment (htab->elf.dynobj,
2958 sreloc, 2))
2959 return FALSE;
2960 }
2961 elf_section_data (sec)->sreloc = sreloc;
2962 }
7619e7c7 2963
25dbc73a
AM
2964 /* If this is a global symbol, we count the number of
2965 relocations we need for this symbol. */
2966 if (h != NULL)
2967 {
2968 head = &ppc_elf_hash_entry (h)->dyn_relocs;
2969 }
2970 else
2971 {
2972 /* Track dynamic relocs needed for local syms too.
2973 We really need local syms available to do this
2974 easily. Oh well. */
7619e7c7 2975
25dbc73a
AM
2976 asection *s;
2977 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
2978 sec, r_symndx);
2979 if (s == NULL)
2980 return FALSE;
7fce784e 2981
25dbc73a
AM
2982 head = ((struct ppc_elf_dyn_relocs **)
2983 &elf_section_data (s)->local_dynrel);
2984 }
7fce784e 2985
25dbc73a
AM
2986 p = *head;
2987 if (p == NULL || p->sec != sec)
2988 {
2989 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
2990 if (p == NULL)
2991 return FALSE;
2992 p->next = *head;
2993 *head = p;
2994 p->sec = sec;
2995 p->count = 0;
2996 p->pc_count = 0;
2997 }
2998
2999 p->count += 1;
3000 if (!MUST_BE_DYN_RELOC (r_type))
3001 p->pc_count += 1;
7619e7c7 3002 }
25dbc73a
AM
3003
3004 break;
7619e7c7
AM
3005 }
3006 }
ee05f2fe 3007
25dbc73a
AM
3008 return TRUE;
3009}
3010\f
8853c3d3
AM
3011/* Merge backend specific data from an object file to the output
3012 object file when linking. */
3013
3014static bfd_boolean
3015ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3016{
3017 flagword old_flags;
3018 flagword new_flags;
3019 bfd_boolean error;
3020
3021 if (!is_ppc_elf_target (ibfd->xvec)
3022 || !is_ppc_elf_target (obfd->xvec))
3023 return TRUE;
3024
3025 /* Check if we have the same endianess. */
3026 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3027 return FALSE;
3028
3029 new_flags = elf_elfheader (ibfd)->e_flags;
3030 old_flags = elf_elfheader (obfd)->e_flags;
3031 if (!elf_flags_init (obfd))
3032 {
3033 /* First call, no flags set. */
3034 elf_flags_init (obfd) = TRUE;
3035 elf_elfheader (obfd)->e_flags = new_flags;
3036 }
3037
3038 /* Compatible flags are ok. */
3039 else if (new_flags == old_flags)
3040 ;
3041
3042 /* Incompatible flags. */
3043 else
3044 {
3045 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
3046 to be linked with either. */
3047 error = FALSE;
3048 if ((new_flags & EF_PPC_RELOCATABLE) != 0
3049 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
3050 {
3051 error = TRUE;
3052 (*_bfd_error_handler)
3053 (_("%B: compiled with -mrelocatable and linked with "
3054 "modules compiled normally"), ibfd);
3055 }
3056 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
3057 && (old_flags & EF_PPC_RELOCATABLE) != 0)
3058 {
3059 error = TRUE;
3060 (*_bfd_error_handler)
3061 (_("%B: compiled normally and linked with "
3062 "modules compiled with -mrelocatable"), ibfd);
3063 }
3064
3065 /* The output is -mrelocatable-lib iff both the input files are. */
3066 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
3067 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
3068
3069 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
3070 but each input file is either -mrelocatable or -mrelocatable-lib. */
3071 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
3072 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
3073 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
3074 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
3075
3076 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
3077 any module uses it. */
3078 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
3079
3080 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3081 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3082
3083 /* Warn about any other mismatches. */
3084 if (new_flags != old_flags)
3085 {
3086 error = TRUE;
3087 (*_bfd_error_handler)
3088 (_("%B: uses different e_flags (0x%lx) fields "
3089 "than previous modules (0x%lx)"),
3090 ibfd, (long) new_flags, (long) old_flags);
3091 }
3092
3093 if (error)
3094 {
3095 bfd_set_error (bfd_error_bad_value);
3096 return FALSE;
3097 }
3098 }
3099
3100 return TRUE;
3101}
3102\f
25dbc73a
AM
3103/* Return the section that should be marked against GC for a given
3104 relocation. */
586119b3 3105
25dbc73a
AM
3106static asection *
3107ppc_elf_gc_mark_hook (asection *sec,
3108 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3109 Elf_Internal_Rela *rel,
3110 struct elf_link_hash_entry *h,
3111 Elf_Internal_Sym *sym)
3112{
3113 if (h != NULL)
ee05f2fe 3114 {
25dbc73a 3115 switch (ELF32_R_TYPE (rel->r_info))
ee05f2fe 3116 {
25dbc73a
AM
3117 case R_PPC_GNU_VTINHERIT:
3118 case R_PPC_GNU_VTENTRY:
3119 break;
ee05f2fe 3120
25dbc73a
AM
3121 default:
3122 switch (h->root.type)
ee05f2fe 3123 {
25dbc73a
AM
3124 case bfd_link_hash_defined:
3125 case bfd_link_hash_defweak:
3126 return h->root.u.def.section;
3127
3128 case bfd_link_hash_common:
3129 return h->root.u.c.p->section;
3130
3131 default:
3132 break;
ee05f2fe
AM
3133 }
3134 }
25dbc73a
AM
3135 }
3136 else
3137 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4e795f50 3138
25dbc73a
AM
3139 return NULL;
3140}
a876ecf9 3141
25dbc73a
AM
3142/* Update the got, plt and dynamic reloc reference counts for the
3143 section being removed. */
3144
3145static bfd_boolean
3146ppc_elf_gc_sweep_hook (bfd *abfd,
3147 struct bfd_link_info *info,
3148 asection *sec,
3149 const Elf_Internal_Rela *relocs)
3150{
3151 struct ppc_elf_link_hash_table *htab;
3152 Elf_Internal_Shdr *symtab_hdr;
3153 struct elf_link_hash_entry **sym_hashes;
3154 bfd_signed_vma *local_got_refcounts;
3155 const Elf_Internal_Rela *rel, *relend;
3156
c87b5a93
AM
3157 if ((sec->flags & SEC_ALLOC) == 0)
3158 return TRUE;
3159
25dbc73a
AM
3160 elf_section_data (sec)->local_dynrel = NULL;
3161
3162 htab = ppc_elf_hash_table (info);
3163 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3164 sym_hashes = elf_sym_hashes (abfd);
3165 local_got_refcounts = elf_local_got_refcounts (abfd);
3166
3167 relend = relocs + sec->reloc_count;
3168 for (rel = relocs; rel < relend; rel++)
3169 {
3170 unsigned long r_symndx;
3171 enum elf_ppc_reloc_type r_type;
3172 struct elf_link_hash_entry *h = NULL;
3173
3174 r_symndx = ELF32_R_SYM (rel->r_info);
3175 if (r_symndx >= symtab_hdr->sh_info)
a876ecf9 3176 {
25dbc73a
AM
3177 struct ppc_elf_dyn_relocs **pp, *p;
3178 struct ppc_elf_link_hash_entry *eh;
3179
3180 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3181 while (h->root.type == bfd_link_hash_indirect
3182 || h->root.type == bfd_link_hash_warning)
3183 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3184 eh = (struct ppc_elf_link_hash_entry *) h;
3185
3186 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3187 if (p->sec == sec)
3188 {
3189 /* Everything must go for SEC. */
3190 *pp = p->next;
3191 break;
3192 }
a876ecf9 3193 }
ee05f2fe 3194
25dbc73a
AM
3195 r_type = ELF32_R_TYPE (rel->r_info);
3196 switch (r_type)
ee05f2fe 3197 {
25dbc73a
AM
3198 case R_PPC_GOT_TLSLD16:
3199 case R_PPC_GOT_TLSLD16_LO:
3200 case R_PPC_GOT_TLSLD16_HI:
3201 case R_PPC_GOT_TLSLD16_HA:
3202 htab->tlsld_got.refcount -= 1;
3203 /* Fall thru */
3204
3205 case R_PPC_GOT_TLSGD16:
3206 case R_PPC_GOT_TLSGD16_LO:
3207 case R_PPC_GOT_TLSGD16_HI:
3208 case R_PPC_GOT_TLSGD16_HA:
3209 case R_PPC_GOT_TPREL16:
3210 case R_PPC_GOT_TPREL16_LO:
3211 case R_PPC_GOT_TPREL16_HI:
3212 case R_PPC_GOT_TPREL16_HA:
3213 case R_PPC_GOT_DTPREL16:
3214 case R_PPC_GOT_DTPREL16_LO:
3215 case R_PPC_GOT_DTPREL16_HI:
3216 case R_PPC_GOT_DTPREL16_HA:
3217 case R_PPC_GOT16:
3218 case R_PPC_GOT16_LO:
3219 case R_PPC_GOT16_HI:
3220 case R_PPC_GOT16_HA:
3221 if (h != NULL)
ee05f2fe 3222 {
25dbc73a
AM
3223 if (h->got.refcount > 0)
3224 h->got.refcount--;
ee05f2fe 3225 }
25dbc73a
AM
3226 else if (local_got_refcounts != NULL)
3227 {
3228 if (local_got_refcounts[r_symndx] > 0)
3229 local_got_refcounts[r_symndx]--;
3230 }
3231 break;
ee05f2fe 3232
25dbc73a
AM
3233 case R_PPC_REL24:
3234 case R_PPC_REL14:
3235 case R_PPC_REL14_BRTAKEN:
3236 case R_PPC_REL14_BRNTAKEN:
3237 case R_PPC_REL32:
3238 if (h == NULL
3239 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3240 break;
3241 /* Fall thru */
ee05f2fe 3242
25dbc73a
AM
3243 case R_PPC_ADDR32:
3244 case R_PPC_ADDR24:
3245 case R_PPC_ADDR16:
3246 case R_PPC_ADDR16_LO:
3247 case R_PPC_ADDR16_HI:
3248 case R_PPC_ADDR16_HA:
3249 case R_PPC_ADDR14:
3250 case R_PPC_ADDR14_BRTAKEN:
3251 case R_PPC_ADDR14_BRNTAKEN:
3252 case R_PPC_UADDR32:
3253 case R_PPC_UADDR16:
3254 case R_PPC_PLT32:
3255 case R_PPC_PLTREL24:
3256 case R_PPC_PLT16_LO:
3257 case R_PPC_PLT16_HI:
3258 case R_PPC_PLT16_HA:
3259 if (h != NULL)
3260 {
3261 if (h->plt.refcount > 0)
3262 h->plt.refcount--;
3263 }
3264 break;
ee05f2fe 3265
25dbc73a
AM
3266 default:
3267 break;
3268 }
7fce784e 3269 }
7fce784e
AS
3270 return TRUE;
3271}
25dbc73a
AM
3272\f
3273/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7fce784e 3274
25dbc73a
AM
3275asection *
3276ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
7fce784e 3277{
25dbc73a 3278 struct ppc_elf_link_hash_table *htab;
7fce784e 3279
25dbc73a
AM
3280 htab = ppc_elf_hash_table (info);
3281 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3282 FALSE, FALSE, TRUE);
7fce784e 3283
25dbc73a 3284 return _bfd_elf_tls_setup (obfd, info);
7fce784e
AS
3285}
3286
25dbc73a
AM
3287/* Run through all the TLS relocs looking for optimization
3288 opportunities. */
252b5132 3289
25dbc73a
AM
3290bfd_boolean
3291ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
3292 struct bfd_link_info *info)
252b5132 3293{
7fce784e 3294 bfd *ibfd;
25dbc73a
AM
3295 asection *sec;
3296 struct ppc_elf_link_hash_table *htab;
252b5132 3297
25dbc73a
AM
3298 if (info->relocatable || info->shared)
3299 return TRUE;
252b5132 3300
7619e7c7 3301 htab = ppc_elf_hash_table (info);
25dbc73a 3302 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
252b5132 3303 {
25dbc73a
AM
3304 Elf_Internal_Sym *locsyms = NULL;
3305 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7619e7c7 3306
25dbc73a
AM
3307 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3308 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3309 {
3310 Elf_Internal_Rela *relstart, *rel, *relend;
3311 int expecting_tls_get_addr;
252b5132 3312
25dbc73a
AM
3313 /* Read the relocations. */
3314 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
3315 info->keep_memory);
3316 if (relstart == NULL)
3317 return FALSE;
7619e7c7 3318
25dbc73a
AM
3319 expecting_tls_get_addr = 0;
3320 relend = relstart + sec->reloc_count;
3321 for (rel = relstart; rel < relend; rel++)
3322 {
3323 enum elf_ppc_reloc_type r_type;
3324 unsigned long r_symndx;
3325 struct elf_link_hash_entry *h = NULL;
3326 char *tls_mask;
3327 char tls_set, tls_clear;
3328 bfd_boolean is_local;
7fce784e 3329
25dbc73a
AM
3330 r_symndx = ELF32_R_SYM (rel->r_info);
3331 if (r_symndx >= symtab_hdr->sh_info)
3332 {
3333 struct elf_link_hash_entry **sym_hashes;
7fce784e 3334
25dbc73a
AM
3335 sym_hashes = elf_sym_hashes (ibfd);
3336 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3337 while (h->root.type == bfd_link_hash_indirect
3338 || h->root.type == bfd_link_hash_warning)
3339 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3340 }
7619e7c7 3341
25dbc73a
AM
3342 is_local = FALSE;
3343 if (h == NULL
3344 || !h->def_dynamic)
3345 is_local = TRUE;
7619e7c7 3346
25dbc73a
AM
3347 r_type = ELF32_R_TYPE (rel->r_info);
3348 switch (r_type)
7619e7c7 3349 {
25dbc73a
AM
3350 case R_PPC_GOT_TLSLD16:
3351 case R_PPC_GOT_TLSLD16_LO:
3352 case R_PPC_GOT_TLSLD16_HI:
3353 case R_PPC_GOT_TLSLD16_HA:
3354 /* These relocs should never be against a symbol
3355 defined in a shared lib. Leave them alone if
3356 that turns out to be the case. */
3357 expecting_tls_get_addr = 0;
3358 htab->tlsld_got.refcount -= 1;
3359 if (!is_local)
3360 continue;
3361
3362 /* LD -> LE */
3363 tls_set = 0;
3364 tls_clear = TLS_LD;
3365 expecting_tls_get_addr = 1;
3366 break;
3367
3368 case R_PPC_GOT_TLSGD16:
3369 case R_PPC_GOT_TLSGD16_LO:
3370 case R_PPC_GOT_TLSGD16_HI:
3371 case R_PPC_GOT_TLSGD16_HA:
3372 if (is_local)
3373 /* GD -> LE */
3374 tls_set = 0;
3375 else
3376 /* GD -> IE */
3377 tls_set = TLS_TLS | TLS_TPRELGD;
3378 tls_clear = TLS_GD;
3379 expecting_tls_get_addr = 1;
3380 break;
3381
3382 case R_PPC_GOT_TPREL16:
3383 case R_PPC_GOT_TPREL16_LO:
3384 case R_PPC_GOT_TPREL16_HI:
3385 case R_PPC_GOT_TPREL16_HA:
3386 expecting_tls_get_addr = 0;
3387 if (is_local)
3388 {
3389 /* IE -> LE */
3390 tls_set = 0;
3391 tls_clear = TLS_TPREL;
3392 break;
3393 }
3394 else
3395 continue;
3396
3397 case R_PPC_REL14:
3398 case R_PPC_REL14_BRTAKEN:
3399 case R_PPC_REL14_BRNTAKEN:
3400 case R_PPC_REL24:
3401 if (expecting_tls_get_addr
3402 && h != NULL
3403 && h == htab->tls_get_addr)
3404 {
3405 if (h->plt.refcount > 0)
3406 h->plt.refcount -= 1;
3407 }
3408 expecting_tls_get_addr = 0;
3409 continue;
3410
3411 default:
3412 expecting_tls_get_addr = 0;
3413 continue;
7619e7c7 3414 }
25dbc73a
AM
3415
3416 if (h != NULL)
7619e7c7 3417 {
25dbc73a
AM
3418 if (tls_set == 0)
3419 {
3420 /* We managed to get rid of a got entry. */
3421 if (h->got.refcount > 0)
3422 h->got.refcount -= 1;
3423 }
3424 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3425 }
3426 else
3427 {
3428 Elf_Internal_Sym *sym;
3429 bfd_signed_vma *lgot_refs;
3430 char *lgot_masks;
3431
3432 if (locsyms == NULL)
3433 {
3434 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3435 if (locsyms == NULL)
3436 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3437 symtab_hdr->sh_info,
3438 0, NULL, NULL, NULL);
3439 if (locsyms == NULL)
3440 {
3441 if (elf_section_data (sec)->relocs != relstart)
3442 free (relstart);
3443 return FALSE;
3444 }
3445 }
3446 sym = locsyms + r_symndx;
3447 lgot_refs = elf_local_got_refcounts (ibfd);
3448 if (lgot_refs == NULL)
3449 abort ();
3450 if (tls_set == 0)
3451 {
3452 /* We managed to get rid of a got entry. */
3453 if (lgot_refs[r_symndx] > 0)
3454 lgot_refs[r_symndx] -= 1;
3455 }
3456 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
3457 tls_mask = &lgot_masks[r_symndx];
7619e7c7 3458 }
25dbc73a
AM
3459
3460 *tls_mask |= tls_set;
3461 *tls_mask &= ~tls_clear;
7619e7c7 3462 }
25dbc73a
AM
3463
3464 if (elf_section_data (sec)->relocs != relstart)
3465 free (relstart);
7619e7c7 3466 }
25dbc73a
AM
3467
3468 if (locsyms != NULL
3469 && (symtab_hdr->contents != (unsigned char *) locsyms))
3470 {
3471 if (!info->keep_memory)
3472 free (locsyms);
3473 else
3474 symtab_hdr->contents = (unsigned char *) locsyms;
3475 }
7fce784e 3476 }
25dbc73a
AM
3477 return TRUE;
3478}
3479\f
3480/* Adjust a symbol defined by a dynamic object and referenced by a
3481 regular object. The current definition is in some section of the
3482 dynamic object, but we're not including those sections. We have to
3483 change the definition to something the rest of the link can
3484 understand. */
7fce784e 3485
25dbc73a
AM
3486static bfd_boolean
3487ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3488 struct elf_link_hash_entry *h)
3489{
3490 struct ppc_elf_link_hash_table *htab;
3491 asection *s;
3492 unsigned int power_of_two;
7fce784e 3493
25dbc73a
AM
3494#ifdef DEBUG
3495 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
3496 h->root.root.string);
3497#endif
252b5132 3498
25dbc73a
AM
3499 /* Make sure we know what is going on here. */
3500 htab = ppc_elf_hash_table (info);
3501 BFD_ASSERT (htab->elf.dynobj != NULL
3502 && (h->needs_plt
3503 || h->u.weakdef != NULL
3504 || (h->def_dynamic
3505 && h->ref_regular
3506 && !h->def_regular)));
252b5132 3507
25dbc73a
AM
3508 /* Deal with function syms. */
3509 if (h->type == STT_FUNC
3510 || h->needs_plt)
3511 {
3512 /* Clear procedure linkage table information for any symbol that
3513 won't need a .plt entry. */
3514 if (h->plt.refcount <= 0
3515 || SYMBOL_CALLS_LOCAL (info, h)
3516 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3517 && h->root.type == bfd_link_hash_undefweak))
252b5132 3518 {
25dbc73a 3519 /* A PLT entry is not required/allowed when:
252b5132 3520
25dbc73a
AM
3521 1. We are not using ld.so; because then the PLT entry
3522 can't be set up, so we can't use one. In this case,
3523 ppc_elf_adjust_dynamic_symbol won't even be called.
252b5132 3524
25dbc73a
AM
3525 2. GC has rendered the entry unused.
3526
3527 3. We know for certain that a call to this symbol
3528 will go to this object, or will remain undefined. */
3529 h->plt.offset = (bfd_vma) -1;
3530 h->needs_plt = 0;
3531 }
3532 return TRUE;
252b5132 3533 }
25dbc73a
AM
3534 else
3535 h->plt.offset = (bfd_vma) -1;
252b5132 3536
25dbc73a
AM
3537 /* If this is a weak symbol, and there is a real definition, the
3538 processor independent code will have arranged for us to see the
3539 real definition first, and we can just use the same value. */
3540 if (h->u.weakdef != NULL)
252b5132 3541 {
25dbc73a
AM
3542 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3543 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3544 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3545 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3546 if (ELIMINATE_COPY_RELOCS)
3547 h->non_got_ref = h->u.weakdef->non_got_ref;
3548 return TRUE;
3549 }
dc810e39 3550
25dbc73a
AM
3551 /* This is a reference to a symbol defined by a dynamic object which
3552 is not a function. */
252b5132 3553
25dbc73a
AM
3554 /* If we are creating a shared library, we must presume that the
3555 only references to the symbol are via the global offset table.
3556 For such cases we need not do anything here; the relocations will
3557 be handled correctly by relocate_section. */
3558 if (info->shared)
3559 return TRUE;
252b5132 3560
25dbc73a
AM
3561 /* If there are no references to this symbol that do not use the
3562 GOT, we don't need to generate a copy reloc. */
3563 if (!h->non_got_ref)
3564 return TRUE;
3565
3566 if (ELIMINATE_COPY_RELOCS)
3567 {
3568 struct ppc_elf_dyn_relocs *p;
3569 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
252b5132 3570 {
25dbc73a
AM
3571 s = p->sec->output_section;
3572 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3573 break;
252b5132
RH
3574 }
3575
25dbc73a
AM
3576 /* If we didn't find any dynamic relocs in read-only sections, then
3577 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3578 if (p == NULL)
252b5132 3579 {
25dbc73a
AM
3580 h->non_got_ref = 0;
3581 return TRUE;
252b5132
RH
3582 }
3583 }
3584
25dbc73a
AM
3585 /* We must allocate the symbol in our .dynbss section, which will
3586 become part of the .bss section of the executable. There will be
3587 an entry for this symbol in the .dynsym section. The dynamic
3588 object will contain position independent code, so all references
3589 from the dynamic object to this symbol will go through the global
3590 offset table. The dynamic linker will use the .dynsym entry to
3591 determine the address it must put in the global offset table, so
3592 both the dynamic object and the regular object will refer to the
3593 same memory location for the variable.
3594
3595 Of course, if the symbol is sufficiently small, we must instead
3596 allocate it in .sbss. FIXME: It would be better to do this if and
3597 only if there were actually SDAREL relocs for that symbol. */
3598
3599 if (h->size <= elf_gp_size (htab->elf.dynobj))
3600 s = htab->dynsbss;
3601 else
3602 s = htab->dynbss;
3603 BFD_ASSERT (s != NULL);
3604
3605 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
3606 copy the initial value out of the dynamic object and into the
3607 runtime process image. We need to remember the offset into the
3608 .rela.bss section we are going to use. */
3609 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3610 {
3611 asection *srel;
3612
3613 if (h->size <= elf_gp_size (htab->elf.dynobj))
3614 srel = htab->relsbss;
3615 else
3616 srel = htab->relbss;
3617 BFD_ASSERT (srel != NULL);
3618 srel->size += sizeof (Elf32_External_Rela);
3619 h->needs_copy = 1;
3620 }
7619e7c7 3621
25dbc73a
AM
3622 /* We need to figure out the alignment required for this symbol. I
3623 have no idea how ELF linkers handle this. */
3624 power_of_two = bfd_log2 (h->size);
3625 if (power_of_two > 4)
3626 power_of_two = 4;
7619e7c7 3627
25dbc73a
AM
3628 /* Apply the required alignment. */
3629 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3630 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3631 {
3632 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
7619e7c7 3633 return FALSE;
7619e7c7
AM
3634 }
3635
25dbc73a
AM
3636 /* Define the symbol as being at this point in the section. */
3637 h->root.u.def.section = s;
3638 h->root.u.def.value = s->size;
7619e7c7 3639
25dbc73a
AM
3640 /* Increment the section size to make room for the symbol. */
3641 s->size += h->size;
7619e7c7 3642
25dbc73a
AM
3643 return TRUE;
3644}
3645\f
3646/* Allocate space in associated reloc sections for dynamic relocs. */
252b5132 3647
b34976b6 3648static bfd_boolean
25dbc73a 3649allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
252b5132 3650{
25dbc73a
AM
3651 struct bfd_link_info *info = inf;
3652 struct ppc_elf_link_hash_entry *eh;
7619e7c7 3653 struct ppc_elf_link_hash_table *htab;
25dbc73a 3654 struct ppc_elf_dyn_relocs *p;
252b5132 3655
25dbc73a 3656 if (h->root.type == bfd_link_hash_indirect)
b34976b6 3657 return TRUE;
252b5132 3658
25dbc73a
AM
3659 if (h->root.type == bfd_link_hash_warning)
3660 /* When warning symbols are created, they **replace** the "real"
3661 entry in the hash table, thus we never get to see the real
3662 symbol in a hash traversal. So look at it now. */
3663 h = (struct elf_link_hash_entry *) h->root.u.i.link;
e47cd125 3664
7619e7c7 3665 htab = ppc_elf_hash_table (info);
25dbc73a
AM
3666 if (htab->elf.dynamic_sections_created
3667 && h->plt.refcount > 0)
252b5132 3668 {
25dbc73a
AM
3669 /* Make sure this symbol is output as a dynamic symbol. */
3670 if (h->dynindx == -1
3671 && !h->forced_local)
252b5132 3672 {
25dbc73a
AM
3673 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3674 return FALSE;
252b5132
RH
3675 }
3676
25dbc73a
AM
3677 if (info->shared
3678 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
252b5132 3679 {
25dbc73a 3680 asection *s = htab->plt;
7619e7c7 3681
25dbc73a
AM
3682 /* If this is the first .plt entry, make room for the special
3683 first entry. */
3684 if (s->size == 0)
3685 s->size += PLT_INITIAL_ENTRY_SIZE;
7619e7c7 3686
25dbc73a
AM
3687 /* The PowerPC PLT is actually composed of two parts, the
3688 first part is 2 words (for a load and a jump), and then
3689 there is a remaining word available at the end. */
3690 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3691 + (PLT_SLOT_SIZE
3692 * ((s->size - PLT_INITIAL_ENTRY_SIZE)
3693 / PLT_ENTRY_SIZE)));
7619e7c7 3694
25dbc73a
AM
3695 /* If this symbol is not defined in a regular file, and we
3696 are not generating a shared library, then set the symbol
3697 to this location in the .plt. This is required to make
3698 function pointers compare as equal between the normal
3699 executable and the shared library. */
3700 if (! info->shared
3701 && !h->def_regular)
252b5132 3702 {
25dbc73a
AM
3703 h->root.u.def.section = s;
3704 h->root.u.def.value = h->plt.offset;
252b5132 3705 }
252b5132 3706
25dbc73a
AM
3707 /* Make room for this entry. After the 8192nd entry, room
3708 for two entries is allocated. */
3709 s->size += PLT_ENTRY_SIZE;
3710 if ((s->size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3711 > PLT_NUM_SINGLE_ENTRIES)
3712 s->size += PLT_ENTRY_SIZE;
3713
3714 /* We also need to make an entry in the .rela.plt section. */
3715 htab->relplt->size += sizeof (Elf32_External_Rela);
3716 }
3717 else
3718 {
3719 h->plt.offset = (bfd_vma) -1;
3720 h->needs_plt = 0;
3721 }
3722 }
3723 else
3724 {
3725 h->plt.offset = (bfd_vma) -1;
3726 h->needs_plt = 0;
3727 }
3728
3729 eh = (struct ppc_elf_link_hash_entry *) h;
3730 if (eh->elf.got.refcount > 0)
3731 {
3732 /* Make sure this symbol is output as a dynamic symbol. */
3733 if (eh->elf.dynindx == -1
3734 && !eh->elf.forced_local)
3735 {
3736 if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
b34976b6 3737 return FALSE;
25dbc73a 3738 }
252b5132 3739
25dbc73a
AM
3740 if (eh->tls_mask == (TLS_TLS | TLS_LD)
3741 && !eh->elf.def_dynamic)
3742 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
3743 eh->elf.got.offset = (bfd_vma) -1;
3744 else
3745 {
3746 bfd_boolean dyn;
3747 eh->elf.got.offset = htab->got->size;
3748 if ((eh->tls_mask & TLS_TLS) != 0)
252b5132 3749 {
25dbc73a
AM
3750 if ((eh->tls_mask & TLS_LD) != 0)
3751 htab->got->size += 8;
3752 if ((eh->tls_mask & TLS_GD) != 0)
3753 htab->got->size += 8;
3754 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
3755 htab->got->size += 4;
3756 if ((eh->tls_mask & TLS_DTPREL) != 0)
3757 htab->got->size += 4;
252b5132 3758 }
25dbc73a
AM
3759 else
3760 htab->got->size += 4;
3761 dyn = htab->elf.dynamic_sections_created;
3762 if ((info->shared
3763 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
3764 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
3765 || eh->elf.root.type != bfd_link_hash_undefweak))
252b5132 3766 {
25dbc73a
AM
3767 /* All the entries we allocated need relocs. */
3768 htab->relgot->size
3769 += ((htab->got->size - eh->elf.got.offset) / 4
3770 * sizeof (Elf32_External_Rela));
3771 /* Except LD only needs one. */
3772 if ((eh->tls_mask & TLS_LD) != 0)
3773 htab->relgot->size -= sizeof (Elf32_External_Rela);
252b5132 3774 }
25dbc73a
AM
3775 }
3776 }
3777 else
3778 eh->elf.got.offset = (bfd_vma) -1;
252b5132 3779
25dbc73a
AM
3780 if (eh->dyn_relocs == NULL)
3781 return TRUE;
252b5132 3782
25dbc73a
AM
3783 /* In the shared -Bsymbolic case, discard space allocated for
3784 dynamic pc-relative relocs against symbols which turn out to be
3785 defined in regular objects. For the normal shared case, discard
3786 space for relocs that have become local due to symbol visibility
3787 changes. */
3788
3789 if (info->shared)
3790 {
3791 /* Relocs that use pc_count are those that appear on a call insn,
3792 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
3793 generated via assembly. We want calls to protected symbols to
3794 resolve directly to the function rather than going via the plt.
3795 If people want function pointer comparisons to work as expected
3796 then they should avoid writing weird assembly. */
3797 if (SYMBOL_CALLS_LOCAL (info, h))
3798 {
3799 struct ppc_elf_dyn_relocs **pp;
3800
3801 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
252b5132 3802 {
25dbc73a
AM
3803 p->count -= p->pc_count;
3804 p->pc_count = 0;
3805 if (p->count == 0)
3806 *pp = p->next;
3807 else
3808 pp = &p->next;
252b5132 3809 }
25dbc73a 3810 }
252b5132 3811
25dbc73a
AM
3812 /* Also discard relocs on undefined weak syms with non-default
3813 visibility. */
3814 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3815 && h->root.type == bfd_link_hash_undefweak)
3816 eh->dyn_relocs = NULL;
7619e7c7 3817
25dbc73a
AM
3818 /* Make sure undefined weak symbols are output as a dynamic symbol
3819 in PIEs. */
3820 if (info->pie
3821 && eh->dyn_relocs != NULL
3822 && h->dynindx == -1
3823 && h->root.type == bfd_link_hash_undefweak
3824 && !h->forced_local)
3825 {
3826 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3827 return FALSE;
3828 }
3829 }
3830 else if (ELIMINATE_COPY_RELOCS)
3831 {
3832 /* For the non-shared case, discard space for relocs against
3833 symbols which turn out to need copy relocs or are not
3834 dynamic. */
7619e7c7 3835
25dbc73a
AM
3836 if (!h->non_got_ref
3837 && h->def_dynamic
3838 && !h->def_regular)
3839 {
3840 /* Make sure this symbol is output as a dynamic symbol.
3841 Undefined weak syms won't yet be marked as dynamic. */
3842 if (h->dynindx == -1
3843 && !h->forced_local)
3844 {
3845 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3846 return FALSE;
3847 }
7619e7c7 3848
25dbc73a
AM
3849 /* If that succeeded, we know we'll be keeping all the
3850 relocs. */
3851 if (h->dynindx != -1)
3852 goto keep;
3853 }
252b5132 3854
25dbc73a 3855 eh->dyn_relocs = NULL;
252b5132 3856
25dbc73a
AM
3857 keep: ;
3858 }
252b5132 3859
25dbc73a
AM
3860 /* Finally, allocate space. */
3861 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3862 {
3863 asection *sreloc = elf_section_data (p->sec)->sreloc;
3864 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3865 }
252b5132 3866
25dbc73a
AM
3867 return TRUE;
3868}
7619e7c7 3869
25dbc73a 3870/* Find any dynamic relocs that apply to read-only sections. */
7619e7c7 3871
25dbc73a
AM
3872static bfd_boolean
3873readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
3874{
3875 struct ppc_elf_dyn_relocs *p;
7619e7c7 3876
25dbc73a
AM
3877 if (h->root.type == bfd_link_hash_indirect)
3878 return TRUE;
252b5132 3879
25dbc73a
AM
3880 if (h->root.type == bfd_link_hash_warning)
3881 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7619e7c7 3882
25dbc73a
AM
3883 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3884 {
3885 asection *s = p->sec->output_section;
ee05f2fe 3886
25dbc73a
AM
3887 if (s != NULL
3888 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
3889 == (SEC_READONLY | SEC_ALLOC)))
3890 {
3891 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
ee05f2fe 3892
25dbc73a
AM
3893 /* Not an error, just cut short the traversal. */
3894 return FALSE;
3895 }
3896 }
3897 return TRUE;
3898}
3899
3900/* Set the sizes of the dynamic sections. */
3901
3902static bfd_boolean
3903ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3904 struct bfd_link_info *info)
3905{
3906 struct ppc_elf_link_hash_table *htab;
3907 asection *s;
3908 bfd_boolean relocs;
3909 bfd *ibfd;
7fce784e 3910
252b5132 3911#ifdef DEBUG
25dbc73a 3912 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
252b5132 3913#endif
252b5132 3914
25dbc73a
AM
3915 htab = ppc_elf_hash_table (info);
3916 BFD_ASSERT (htab->elf.dynobj != NULL);
252b5132 3917
25dbc73a
AM
3918 if (elf_hash_table (info)->dynamic_sections_created)
3919 {
3920 /* Set the contents of the .interp section to the interpreter. */
3921 if (info->executable)
3922 {
3923 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
3924 BFD_ASSERT (s != NULL);
3925 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3926 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3927 }
3928 }
252b5132 3929
25dbc73a
AM
3930 if (htab->tlsld_got.refcount > 0)
3931 {
3932 htab->tlsld_got.offset = htab->got->size;
3933 htab->got->size += 8;
3934 if (info->shared)
3935 htab->relgot->size += sizeof (Elf32_External_Rela);
3936 }
3937 else
3938 htab->tlsld_got.offset = (bfd_vma) -1;
252b5132 3939
25dbc73a
AM
3940 /* Set up .got offsets for local syms, and space for local dynamic
3941 relocs. */
3942 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3943 {
3944 bfd_signed_vma *local_got;
3945 bfd_signed_vma *end_local_got;
3946 char *lgot_masks;
3947 bfd_size_type locsymcount;
3948 Elf_Internal_Shdr *symtab_hdr;
3949 asection *srel;
7fce784e 3950
d2663f46 3951 if (!is_ppc_elf_target (ibfd->xvec))
25dbc73a 3952 continue;
7fce784e 3953
25dbc73a
AM
3954 for (s = ibfd->sections; s != NULL; s = s->next)
3955 {
3956 struct ppc_elf_dyn_relocs *p;
252b5132 3957
25dbc73a
AM
3958 for (p = ((struct ppc_elf_dyn_relocs *)
3959 elf_section_data (s)->local_dynrel);
3960 p != NULL;
3961 p = p->next)
3962 {
3963 if (!bfd_is_abs_section (p->sec)
3964 && bfd_is_abs_section (p->sec->output_section))
3965 {
3966 /* Input section has been discarded, either because
3967 it is a copy of a linkonce section or due to
3968 linker script /DISCARD/, so we'll be discarding
3969 the relocs too. */
7fce784e 3970 }
25dbc73a 3971 else if (p->count != 0)
7fce784e 3972 {
25dbc73a
AM
3973 elf_section_data (p->sec)->sreloc->size
3974 += p->count * sizeof (Elf32_External_Rela);
3975 if ((p->sec->output_section->flags
3976 & (SEC_READONLY | SEC_ALLOC))
3977 == (SEC_READONLY | SEC_ALLOC))
3978 info->flags |= DF_TEXTREL;
7fce784e 3979 }
252b5132 3980 }
252b5132 3981 }
252b5132 3982
25dbc73a
AM
3983 local_got = elf_local_got_refcounts (ibfd);
3984 if (!local_got)
3985 continue;
252b5132 3986
25dbc73a
AM
3987 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3988 locsymcount = symtab_hdr->sh_info;
3989 end_local_got = local_got + locsymcount;
3990 lgot_masks = (char *) end_local_got;
3991 s = htab->got;
3992 srel = htab->relgot;
3993 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
3994 if (*local_got > 0)
3995 {
3996 if (*lgot_masks == (TLS_TLS | TLS_LD))
3997 {
3998 /* If just an LD reloc, we'll just use
3999 htab->tlsld_got.offset. */
4000 if (htab->tlsld_got.offset == (bfd_vma) -1)
4001 {
4002 htab->tlsld_got.offset = s->size;
4003 s->size += 8;
4004 if (info->shared)
4005 srel->size += sizeof (Elf32_External_Rela);
4006 }
4007 *local_got = (bfd_vma) -1;
4008 }
4009 else
4010 {
4011 *local_got = s->size;
4012 if ((*lgot_masks & TLS_TLS) != 0)
4013 {
4014 if ((*lgot_masks & TLS_GD) != 0)
4015 s->size += 8;
4016 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
4017 s->size += 4;
4018 if ((*lgot_masks & TLS_DTPREL) != 0)
4019 s->size += 4;
4020 }
4021 else
4022 s->size += 4;
4023 if (info->shared)
4024 srel->size += ((s->size - *local_got) / 4
4025 * sizeof (Elf32_External_Rela));
4026 }
4027 }
4028 else
4029 *local_got = (bfd_vma) -1;
4030 }
252b5132 4031
25dbc73a
AM
4032 /* Allocate space for global sym dynamic relocs. */
4033 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
4034
4035 /* We've now determined the sizes of the various dynamic sections.
4036 Allocate memory for them. */
4037 relocs = FALSE;
4038 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
252b5132 4039 {
25dbc73a
AM
4040 if ((s->flags & SEC_LINKER_CREATED) == 0)
4041 continue;
252b5132 4042
25dbc73a 4043 if (s == htab->plt
644285ef
AM
4044 || s == htab->got
4045 || s == htab->sbss)
25dbc73a
AM
4046 {
4047 /* Strip this section if we don't need it; see the
4048 comment below. */
4049 }
c9a2f333
AM
4050 else if (s == htab->sdata[0].section
4051 || s == htab->sdata[1].section)
25dbc73a 4052 {
c9a2f333 4053 /* Strip these too. */
25dbc73a
AM
4054 }
4055 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4056 {
4057 if (s->size == 0)
252b5132 4058 {
25dbc73a
AM
4059 /* If we don't need this section, strip it from the
4060 output file. This is mostly to handle .rela.bss and
4061 .rela.plt. We must create both sections in
4062 create_dynamic_sections, because they must be created
4063 before the linker maps input sections to output
4064 sections. The linker does that before
4065 adjust_dynamic_symbol is called, and it is that
4066 function which decides whether anything needs to go
4067 into these sections. */
4068 }
4069 else
4070 {
4071 /* Remember whether there are any relocation sections. */
4072 relocs = TRUE;
252b5132 4073
25dbc73a
AM
4074 /* We use the reloc_count field as a counter if we need
4075 to copy relocs into the output file. */
4076 s->reloc_count = 0;
252b5132
RH
4077 }
4078 }
25dbc73a
AM
4079 else
4080 {
4081 /* It's not one of our sections, so don't allocate space. */
4082 continue;
4083 }
252b5132 4084
25dbc73a
AM
4085 if (s->size == 0)
4086 {
4087 _bfd_strip_section_from_output (info, s);
4088 continue;
4089 }
7fce784e 4090
644285ef
AM
4091 if (s == htab->sbss)
4092 continue;
4093
25dbc73a
AM
4094 /* Allocate memory for the section contents. */
4095 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
4096 if (s->contents == NULL)
4097 return FALSE;
4098 }
252b5132 4099
25dbc73a 4100 if (htab->elf.dynamic_sections_created)
7619e7c7 4101 {
25dbc73a
AM
4102 /* Add some entries to the .dynamic section. We fill in the
4103 values later, in ppc_elf_finish_dynamic_sections, but we
4104 must add the entries now so that we get the correct size for
4105 the .dynamic section. The DT_DEBUG entry is filled in by the
4106 dynamic linker and used by the debugger. */
4107#define add_dynamic_entry(TAG, VAL) \
4108 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
252b5132 4109
25dbc73a 4110 if (info->executable)
7619e7c7 4111 {
25dbc73a
AM
4112 if (!add_dynamic_entry (DT_DEBUG, 0))
4113 return FALSE;
7619e7c7
AM
4114 }
4115
25dbc73a 4116 if (htab->plt != NULL && htab->plt->size != 0)
7619e7c7 4117 {
25dbc73a
AM
4118 if (!add_dynamic_entry (DT_PLTGOT, 0)
4119 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4120 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4121 || !add_dynamic_entry (DT_JMPREL, 0))
4122 return FALSE;
4123 }
7619e7c7 4124
25dbc73a
AM
4125 if (relocs)
4126 {
4127 if (!add_dynamic_entry (DT_RELA, 0)
4128 || !add_dynamic_entry (DT_RELASZ, 0)
4129 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4130 return FALSE;
4131 }
7619e7c7 4132
25dbc73a
AM
4133 /* If any dynamic relocs apply to a read-only section, then we
4134 need a DT_TEXTREL entry. */
4135 if ((info->flags & DF_TEXTREL) == 0)
4136 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
4137 info);
7619e7c7 4138
25dbc73a
AM
4139 if ((info->flags & DF_TEXTREL) != 0)
4140 {
4141 if (!add_dynamic_entry (DT_TEXTREL, 0))
4142 return FALSE;
7619e7c7
AM
4143 }
4144 }
25dbc73a
AM
4145#undef add_dynamic_entry
4146
7619e7c7
AM
4147 return TRUE;
4148}
25dbc73a
AM
4149\f
4150#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
4151
4152static const int shared_stub_entry[] =
4153 {
4154 0x7c0802a6, /* mflr 0 */
4155 0x429f0005, /* bcl 20, 31, .Lxxx */
4156 0x7d6802a6, /* mflr 11 */
4157 0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
4158 0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
4159 0x7c0803a6, /* mtlr 0 */
4160 0x7d6903a6, /* mtctr 11 */
4161 0x4e800420, /* bctr */
4162 };
4163
4164static const int stub_entry[] =
4165 {
4166 0x3d600000, /* lis 11,xxx@ha */
4167 0x396b0000, /* addi 11,11,xxx@l */
4168 0x7d6903a6, /* mtctr 11 */
4169 0x4e800420, /* bctr */
4170 };
4171
4172static bfd_boolean
4173ppc_elf_relax_section (bfd *abfd,
4174 asection *isec,
4175 struct bfd_link_info *link_info,
4176 bfd_boolean *again)
4177{
4178 struct one_fixup
4179 {
4180 struct one_fixup *next;
4181 asection *tsec;
4182 bfd_vma toff;
4183 bfd_vma trampoff;
4184 };
7619e7c7 4185
25dbc73a
AM
4186 Elf_Internal_Shdr *symtab_hdr;
4187 bfd_byte *contents = NULL;
4188 Elf_Internal_Sym *isymbuf = NULL;
4189 Elf_Internal_Rela *internal_relocs = NULL;
4190 Elf_Internal_Rela *irel, *irelend;
4191 struct one_fixup *fixups = NULL;
4192 bfd_boolean changed;
4193 struct ppc_elf_link_hash_table *ppc_info;
4194 bfd_size_type trampoff;
7619e7c7 4195
25dbc73a 4196 *again = FALSE;
7619e7c7 4197
c87b5a93
AM
4198 /* Nothing to do if there are no relocations, and no need to do
4199 anything with non-alloc sections. */
4200 if ((isec->flags & SEC_ALLOC) == 0
4201 || (isec->flags & SEC_RELOC) == 0
4202 || isec->reloc_count == 0)
25dbc73a 4203 return TRUE;
7619e7c7 4204
25dbc73a
AM
4205 trampoff = (isec->size + 3) & (bfd_vma) -4;
4206 /* Space for a branch around any trampolines. */
4207 trampoff += 4;
7619e7c7 4208
25dbc73a 4209 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7619e7c7 4210
25dbc73a
AM
4211 /* Get a copy of the native relocations. */
4212 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
4213 link_info->keep_memory);
4214 if (internal_relocs == NULL)
4215 goto error_return;
7619e7c7 4216
25dbc73a
AM
4217 ppc_info = ppc_elf_hash_table (link_info);
4218 irelend = internal_relocs + isec->reloc_count;
7619e7c7 4219
25dbc73a 4220 for (irel = internal_relocs; irel < irelend; irel++)
7619e7c7 4221 {
25dbc73a
AM
4222 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
4223 bfd_vma symaddr, reladdr, toff, roff;
4224 asection *tsec;
4225 struct one_fixup *f;
4226 size_t insn_offset = 0;
4227 bfd_vma max_branch_offset, val;
4228 bfd_byte *hit_addr;
4229 unsigned long t0;
4230 unsigned char sym_type;
7619e7c7 4231
25dbc73a
AM
4232 switch (r_type)
4233 {
4234 case R_PPC_REL24:
4235 case R_PPC_LOCAL24PC:
4236 case R_PPC_PLTREL24:
4237 max_branch_offset = 1 << 25;
4238 break;
7619e7c7 4239
25dbc73a
AM
4240 case R_PPC_REL14:
4241 case R_PPC_REL14_BRTAKEN:
4242 case R_PPC_REL14_BRNTAKEN:
4243 max_branch_offset = 1 << 15;
4244 break;
7619e7c7 4245
25dbc73a
AM
4246 default:
4247 continue;
4248 }
7619e7c7 4249
25dbc73a
AM
4250 /* Get the value of the symbol referred to by the reloc. */
4251 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
4252 {
4253 /* A local symbol. */
4254 Elf_Internal_Sym *isym;
7fce784e 4255
25dbc73a
AM
4256 /* Read this BFD's local symbols. */
4257 if (isymbuf == NULL)
4258 {
4259 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4260 if (isymbuf == NULL)
4261 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4262 symtab_hdr->sh_info, 0,
4263 NULL, NULL, NULL);
4264 if (isymbuf == 0)
4265 goto error_return;
4266 }
4267 isym = isymbuf + ELF32_R_SYM (irel->r_info);
4268 if (isym->st_shndx == SHN_UNDEF)
4269 continue; /* We can't do anything with undefined symbols. */
4270 else if (isym->st_shndx == SHN_ABS)
4271 tsec = bfd_abs_section_ptr;
4272 else if (isym->st_shndx == SHN_COMMON)
4273 tsec = bfd_com_section_ptr;
4274 else
4275 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7fce784e 4276
25dbc73a
AM
4277 toff = isym->st_value;
4278 sym_type = ELF_ST_TYPE (isym->st_info);
4279 }
4280 else
4281 {
4282 /* Global symbol handling. */
4283 unsigned long indx;
4284 struct elf_link_hash_entry *h;
7619e7c7 4285
25dbc73a
AM
4286 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
4287 h = elf_sym_hashes (abfd)[indx];
7619e7c7 4288
25dbc73a
AM
4289 while (h->root.type == bfd_link_hash_indirect
4290 || h->root.type == bfd_link_hash_warning)
4291 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7619e7c7 4292
25dbc73a
AM
4293 if (r_type == R_PPC_PLTREL24
4294 && ppc_info->plt != NULL
4295 && h->plt.offset != (bfd_vma) -1)
4296 {
4297 tsec = ppc_info->plt;
4298 toff = h->plt.offset;
4299 }
4300 else if (h->root.type == bfd_link_hash_defined
4301 || h->root.type == bfd_link_hash_defweak)
4302 {
4303 tsec = h->root.u.def.section;
4304 toff = h->root.u.def.value;
4305 }
4306 else
4307 continue;
7619e7c7 4308
25dbc73a
AM
4309 sym_type = h->type;
4310 }
7619e7c7 4311
25dbc73a
AM
4312 /* If the branch and target are in the same section, you have
4313 no hope of adding stubs. We'll error out later should the
4314 branch overflow. */
4315 if (tsec == isec)
4316 continue;
4317
4318 /* There probably isn't any reason to handle symbols in
4319 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
4320 attribute for a code section, and we are only looking at
4321 branches. However, implement it correctly here as a
4322 reference for other target relax_section functions. */
4323 if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
4324 {
4325 /* At this stage in linking, no SEC_MERGE symbol has been
4326 adjusted, so all references to such symbols need to be
4327 passed through _bfd_merged_section_offset. (Later, in
4328 relocate_section, all SEC_MERGE symbols *except* for
4329 section symbols have been adjusted.)
4330
4331 gas may reduce relocations against symbols in SEC_MERGE
4332 sections to a relocation against the section symbol when
4333 the original addend was zero. When the reloc is against
4334 a section symbol we should include the addend in the
4335 offset passed to _bfd_merged_section_offset, since the
4336 location of interest is the original symbol. On the
4337 other hand, an access to "sym+addend" where "sym" is not
4338 a section symbol should not include the addend; Such an
4339 access is presumed to be an offset from "sym"; The
4340 location of interest is just "sym". */
4341 if (sym_type == STT_SECTION)
4342 toff += irel->r_addend;
7619e7c7 4343
25dbc73a
AM
4344 toff = _bfd_merged_section_offset (abfd, &tsec,
4345 elf_section_data (tsec)->sec_info,
4346 toff);
7619e7c7 4347
25dbc73a
AM
4348 if (sym_type != STT_SECTION)
4349 toff += irel->r_addend;
4350 }
4351 else
4352 toff += irel->r_addend;
7619e7c7 4353
25dbc73a 4354 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
7619e7c7 4355
25dbc73a
AM
4356 roff = irel->r_offset;
4357 reladdr = isec->output_section->vma + isec->output_offset + roff;
7619e7c7 4358
25dbc73a
AM
4359 /* If the branch is in range, no need to do anything. */
4360 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
4361 continue;
252b5132 4362
25dbc73a
AM
4363 /* Look for an existing fixup to this address. */
4364 for (f = fixups; f ; f = f->next)
4365 if (f->tsec == tsec && f->toff == toff)
4366 break;
4367
4368 if (f == NULL)
7619e7c7 4369 {
25dbc73a
AM
4370 size_t size;
4371 unsigned long stub_rtype;
252b5132 4372
25dbc73a
AM
4373 val = trampoff - roff;
4374 if (val >= max_branch_offset)
4375 /* Oh dear, we can't reach a trampoline. Don't try to add
4376 one. We'll report an error later. */
4377 continue;
252b5132 4378
25dbc73a
AM
4379 if (link_info->shared)
4380 {
4381 size = 4 * ARRAY_SIZE (shared_stub_entry);
4382 insn_offset = 12;
4383 stub_rtype = R_PPC_RELAX32PC;
4384 }
4385 else
4386 {
4387 size = 4 * ARRAY_SIZE (stub_entry);
4388 insn_offset = 0;
4389 stub_rtype = R_PPC_RELAX32;
4390 }
dc810e39 4391
25dbc73a
AM
4392 if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
4393 != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
4394 abort ();
4395 if (tsec == ppc_info->plt)
4396 stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
252b5132 4397
25dbc73a
AM
4398 /* Hijack the old relocation. Since we need two
4399 relocations for this use a "composite" reloc. */
4400 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4401 stub_rtype);
4402 irel->r_offset = trampoff + insn_offset;
252b5132 4403
25dbc73a
AM
4404 /* Record the fixup so we don't do it again this section. */
4405 f = bfd_malloc (sizeof (*f));
4406 f->next = fixups;
4407 f->tsec = tsec;
4408 f->toff = toff;
4409 f->trampoff = trampoff;
4410 fixups = f;
252b5132 4411
25dbc73a
AM
4412 trampoff += size;
4413 }
4414 else
4415 {
4416 val = f->trampoff - roff;
4417 if (val >= max_branch_offset)
4418 continue;
252b5132 4419
25dbc73a
AM
4420 /* Nop out the reloc, since we're finalizing things here. */
4421 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
4422 }
252b5132 4423
25dbc73a
AM
4424 /* Get the section contents. */
4425 if (contents == NULL)
4426 {
4427 /* Get cached copy if it exists. */
4428 if (elf_section_data (isec)->this_hdr.contents != NULL)
4429 contents = elf_section_data (isec)->this_hdr.contents;
4430 else
4431 {
4432 /* Go get them off disk. */
4433 if (!bfd_malloc_and_get_section (abfd, isec, &contents))
4434 goto error_return;
4435 }
4436 }
252b5132 4437
25dbc73a
AM
4438 /* Fix up the existing branch to hit the trampoline. */
4439 hit_addr = contents + roff;
4440 switch (r_type)
4441 {
4442 case R_PPC_REL24:
4443 case R_PPC_LOCAL24PC:
4444 case R_PPC_PLTREL24:
4445 t0 = bfd_get_32 (abfd, hit_addr);
4446 t0 &= ~0x3fffffc;
4447 t0 |= val & 0x3fffffc;
4448 bfd_put_32 (abfd, t0, hit_addr);
4449 break;
252b5132 4450
25dbc73a
AM
4451 case R_PPC_REL14:
4452 case R_PPC_REL14_BRTAKEN:
4453 case R_PPC_REL14_BRNTAKEN:
4454 t0 = bfd_get_32 (abfd, hit_addr);
4455 t0 &= ~0xfffc;
4456 t0 |= val & 0xfffc;
4457 bfd_put_32 (abfd, t0, hit_addr);
4458 break;
4459 }
4460 }
252b5132 4461
25dbc73a
AM
4462 /* Write out the trampolines. */
4463 changed = fixups != NULL;
4464 if (fixups != NULL)
4465 {
4466 const int *stub;
4467 bfd_byte *dest;
4468 bfd_vma val;
4469 int i, size;
252b5132 4470
25dbc73a
AM
4471 do
4472 {
4473 struct one_fixup *f = fixups;
4474 fixups = fixups->next;
4475 free (f);
4476 }
4477 while (fixups);
252b5132 4478
25dbc73a
AM
4479 contents = bfd_realloc (contents, trampoff);
4480 if (contents == NULL)
4481 goto error_return;
252b5132 4482
25dbc73a
AM
4483 isec->size = (isec->size + 3) & (bfd_vma) -4;
4484 /* Branch around the trampolines. */
4485 val = trampoff - isec->size + 0x48000000;
4486 dest = contents + isec->size;
4487 isec->size = trampoff;
4488 bfd_put_32 (abfd, val, dest);
4489 dest += 4;
252b5132 4490
25dbc73a
AM
4491 if (link_info->shared)
4492 {
4493 stub = shared_stub_entry;
4494 size = ARRAY_SIZE (shared_stub_entry);
4495 }
4496 else
4497 {
4498 stub = stub_entry;
4499 size = ARRAY_SIZE (stub_entry);
4500 }
252b5132 4501
25dbc73a
AM
4502 i = 0;
4503 while (dest < contents + trampoff)
252b5132 4504 {
25dbc73a
AM
4505 bfd_put_32 (abfd, stub[i], dest);
4506 i++;
4507 if (i == size)
4508 i = 0;
4509 dest += 4;
252b5132 4510 }
25dbc73a 4511 BFD_ASSERT (i == 0);
252b5132
RH
4512 }
4513
25dbc73a
AM
4514 if (isymbuf != NULL
4515 && symtab_hdr->contents != (unsigned char *) isymbuf)
252b5132 4516 {
25dbc73a
AM
4517 if (! link_info->keep_memory)
4518 free (isymbuf);
252b5132 4519 else
25dbc73a
AM
4520 {
4521 /* Cache the symbols for elf_link_input_bfd. */
4522 symtab_hdr->contents = (unsigned char *) isymbuf;
4523 }
252b5132
RH
4524 }
4525
25dbc73a
AM
4526 if (contents != NULL
4527 && elf_section_data (isec)->this_hdr.contents != contents)
4528 {
4529 if (!changed && !link_info->keep_memory)
4530 free (contents);
4531 else
4532 {
4533 /* Cache the section contents for elf_link_input_bfd. */
4534 elf_section_data (isec)->this_hdr.contents = contents;
4535 }
4536 }
252b5132 4537
25dbc73a
AM
4538 if (elf_section_data (isec)->relocs != internal_relocs)
4539 {
4540 if (!changed)
4541 free (internal_relocs);
4542 else
4543 elf_section_data (isec)->relocs = internal_relocs;
4544 }
252b5132 4545
25dbc73a 4546 *again = changed;
b34976b6 4547 return TRUE;
25dbc73a
AM
4548
4549 error_return:
4550 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
4551 free (isymbuf);
4552 if (contents != NULL
4553 && elf_section_data (isec)->this_hdr.contents != contents)
4554 free (contents);
4555 if (internal_relocs != NULL
4556 && elf_section_data (isec)->relocs != internal_relocs)
4557 free (internal_relocs);
4558 return FALSE;
252b5132 4559}
252b5132 4560\f
f85f57ad
AM
4561/* Set _SDA_BASE_, _SDA2_BASE, and sbss start and end syms. They are
4562 set here rather than via PROVIDE in the default linker script,
4563 because using PROVIDE inside an output section statement results in
4564 unnecessary output sections. Using PROVIDE outside an output section
4565 statement runs the risk of section alignment affecting where the
4566 section starts. */
c9a2f333
AM
4567
4568bfd_boolean
4569ppc_elf_set_sdata_syms (bfd *obfd, struct bfd_link_info *info)
4570{
4571 struct ppc_elf_link_hash_table *htab;
4572 unsigned i;
f85f57ad
AM
4573 asection *s;
4574 bfd_vma val;
c9a2f333
AM
4575
4576 htab = ppc_elf_hash_table (info);
4577
4578 for (i = 0; i < 2; i++)
4579 {
4580 elf_linker_section_t *lsect = &htab->sdata[i];
c9a2f333
AM
4581
4582 s = lsect->section;
4583 if (s != NULL)
4584 s = s->output_section;
4585 if (s == NULL)
4586 s = bfd_get_section_by_name (obfd, lsect->name);
4587 if (s == NULL)
4588 s = bfd_get_section_by_name (obfd, lsect->bss_name);
4589
4590 val = 0;
4591 if (s != NULL)
4592 val = s->vma + 32768;
4593 lsect->sym_val = val;
4594
81e1b023 4595 _bfd_elf_provide_symbol (info, lsect->sym_name, val);
c9a2f333 4596 }
f85f57ad
AM
4597
4598 s = bfd_get_section_by_name (obfd, ".sbss");
4599 val = 0;
4600 if (s != NULL)
4601 val = s->vma;
81e1b023
L
4602 _bfd_elf_provide_symbol (info, "__sbss_start", val);
4603 _bfd_elf_provide_symbol (info, "___sbss_start", val);
f85f57ad
AM
4604 if (s != NULL)
4605 val += s->size;
81e1b023
L
4606 _bfd_elf_provide_symbol (info, "__sbss_end", val);
4607 _bfd_elf_provide_symbol (info, "___sbss_end", val);
c9a2f333
AM
4608 return TRUE;
4609}
4610\f
25dbc73a 4611/* Fill in the address for a pointer generated in a linker section. */
252b5132 4612
25dbc73a 4613static bfd_vma
76750a2f 4614elf_finish_pointer_linker_section (bfd *input_bfd,
25dbc73a
AM
4615 elf_linker_section_t *lsect,
4616 struct elf_link_hash_entry *h,
4617 bfd_vma relocation,
2bb04cf2 4618 const Elf_Internal_Rela *rel)
25dbc73a
AM
4619{
4620 elf_linker_section_pointers_t *linker_section_ptr;
252b5132 4621
25dbc73a 4622 BFD_ASSERT (lsect != NULL);
252b5132 4623
25dbc73a 4624 if (h != NULL)
252b5132 4625 {
25dbc73a
AM
4626 /* Handle global symbol. */
4627 struct ppc_elf_link_hash_entry *eh;
252b5132 4628
25dbc73a 4629 eh = (struct ppc_elf_link_hash_entry *) h;
76750a2f
AM
4630 BFD_ASSERT (eh->elf.def_regular);
4631 linker_section_ptr = eh->linker_section_pointer;
25dbc73a
AM
4632 }
4633 else
4634 {
4635 /* Handle local symbol. */
4636 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
76750a2f 4637
25dbc73a 4638 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
76750a2f
AM
4639 linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
4640 }
252b5132 4641
76750a2f
AM
4642 linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
4643 rel->r_addend,
4644 lsect);
4645 BFD_ASSERT (linker_section_ptr != NULL);
25dbc73a 4646
76750a2f
AM
4647 /* Offset will always be a multiple of four, so use the bottom bit
4648 as a "written" flag. */
4649 if ((linker_section_ptr->offset & 1) == 0)
4650 {
4651 bfd_put_32 (lsect->section->owner,
4652 relocation + linker_section_ptr->addend,
4653 lsect->section->contents + linker_section_ptr->offset);
4654 linker_section_ptr->offset += 1;
252b5132
RH
4655 }
4656
25dbc73a 4657 relocation = (lsect->section->output_offset
76750a2f 4658 + linker_section_ptr->offset - 1
c9a2f333 4659 - 0x8000);
252b5132 4660
25dbc73a
AM
4661#ifdef DEBUG
4662 fprintf (stderr,
4663 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
4664 lsect->name, (long) relocation, (long) relocation);
4665#endif
252b5132 4666
25dbc73a
AM
4667 /* Subtract out the addend, because it will get added back in by the normal
4668 processing. */
4669 return relocation - linker_section_ptr->addend;
252b5132 4670}
25dbc73a 4671
252b5132
RH
4672/* The RELOCATE_SECTION function is called by the ELF backend linker
4673 to handle the relocations for a section.
4674
4675 The relocs are always passed as Rela structures; if the section
4676 actually uses Rel structures, the r_addend field will always be
4677 zero.
4678
4679 This function is responsible for adjust the section contents as
4680 necessary, and (if using Rela relocs and generating a
1049f94e 4681 relocatable output file) adjusting the reloc addend as
252b5132
RH
4682 necessary.
4683
4684 This function does not have to worry about setting the reloc
4685 address or the reloc symbol index.
4686
4687 LOCAL_SYMS is a pointer to the swapped in local symbols.
4688
4689 LOCAL_SECTIONS is an array giving the section in the input file
4690 corresponding to the st_shndx field of each local symbol.
4691
4692 The global hash table entry for the global symbols can be found
4693 via elf_sym_hashes (input_bfd).
4694
1049f94e 4695 When generating relocatable output, this function must handle
252b5132
RH
4696 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4697 going to be the section symbol corresponding to the output
4698 section, which means that the addend must be adjusted
4699 accordingly. */
4700
b34976b6 4701static bfd_boolean
55fd94b0
AM
4702ppc_elf_relocate_section (bfd *output_bfd,
4703 struct bfd_link_info *info,
4704 bfd *input_bfd,
4705 asection *input_section,
4706 bfd_byte *contents,
4707 Elf_Internal_Rela *relocs,
4708 Elf_Internal_Sym *local_syms,
4709 asection **local_sections)
252b5132 4710{
7619e7c7
AM
4711 Elf_Internal_Shdr *symtab_hdr;
4712 struct elf_link_hash_entry **sym_hashes;
4713 struct ppc_elf_link_hash_table *htab;
4714 Elf_Internal_Rela *rel;
4715 Elf_Internal_Rela *relend;
4716 Elf_Internal_Rela outrel;
4717 bfd_byte *loc;
b34976b6 4718 asection *sreloc = NULL;
252b5132 4719 bfd_vma *local_got_offsets;
b34976b6 4720 bfd_boolean ret = TRUE;
b34976b6 4721
252b5132 4722#ifdef DEBUG
d003868e
AM
4723 _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
4724 "%ld relocations%s",
4725 input_bfd, input_section,
4726 (long) input_section->reloc_count,
4727 (info->relocatable) ? " (relocatable)" : "");
252b5132
RH
4728#endif
4729
1049f94e 4730 if (info->relocatable)
b34976b6 4731 return TRUE;
b491616a 4732
e47cd125 4733 /* Initialize howto table if not already done. */
8da6118f 4734 if (!ppc_elf_howto_table[R_PPC_ADDR32])
252b5132
RH
4735 ppc_elf_howto_init ();
4736
7619e7c7 4737 htab = ppc_elf_hash_table (info);
252b5132 4738 local_got_offsets = elf_local_got_offsets (input_bfd);
7619e7c7
AM
4739 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4740 sym_hashes = elf_sym_hashes (input_bfd);
4741 rel = relocs;
4742 relend = relocs + input_section->reloc_count;
252b5132
RH
4743 for (; rel < relend; rel++)
4744 {
7619e7c7
AM
4745 enum elf_ppc_reloc_type r_type;
4746 bfd_vma addend;
4747 bfd_reloc_status_type r;
4748 Elf_Internal_Sym *sym;
4749 asection *sec;
4750 struct elf_link_hash_entry *h;
4751 const char *sym_name;
252b5132
RH
4752 reloc_howto_type *howto;
4753 unsigned long r_symndx;
4754 bfd_vma relocation;
7619e7c7 4755 bfd_vma branch_bit, insn, from;
7619e7c7
AM
4756 bfd_boolean unresolved_reloc;
4757 bfd_boolean warned;
4758 unsigned int tls_type, tls_mask, tls_gd;
4759
55fd94b0
AM
4760 r_type = ELF32_R_TYPE (rel->r_info);
4761 sym = NULL;
4762 sec = NULL;
4763 h = NULL;
7619e7c7
AM
4764 unresolved_reloc = FALSE;
4765 warned = FALSE;
252b5132 4766 r_symndx = ELF32_R_SYM (rel->r_info);
560e09e9 4767
252b5132
RH
4768 if (r_symndx < symtab_hdr->sh_info)
4769 {
4770 sym = local_syms + r_symndx;
4771 sec = local_sections[r_symndx];
26c61ae5 4772 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
252b5132 4773
8517fae7 4774 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
252b5132
RH
4775 }
4776 else
4777 {
b2a8e766
AM
4778 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4779 r_symndx, symtab_hdr, sym_hashes,
4780 h, sec, relocation,
4781 unresolved_reloc, warned);
9abc968f 4782
252b5132 4783 sym_name = h->root.root.string;
7619e7c7
AM
4784 }
4785
4786 /* TLS optimizations. Replace instruction sequences and relocs
4787 based on information we collected in tls_optimize. We edit
4788 RELOCS so that --emit-relocs will output something sensible
4789 for the final instruction stream. */
4790 tls_mask = 0;
4791 tls_gd = 0;
4792 if (IS_PPC_TLS_RELOC (r_type))
4793 {
4794 if (h != NULL)
4795 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4796 else if (local_got_offsets != NULL)
4797 {
4798 char *lgot_masks;
4799 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4800 tls_mask = lgot_masks[r_symndx];
4801 }
4802 }
4803
4804 /* Ensure reloc mapping code below stays sane. */
4805 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4806 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4807 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4808 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4809 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4810 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4811 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4812 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4813 abort ();
4814 switch (r_type)
4815 {
4816 default:
4817 break;
4818
4819 case R_PPC_GOT_TPREL16:
4820 case R_PPC_GOT_TPREL16_LO:
4821 if (tls_mask != 0
4822 && (tls_mask & TLS_TPREL) == 0)
4823 {
4824 bfd_vma insn;
4825 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4826 insn &= 31 << 21;
4827 insn |= 0x3c020000; /* addis 0,2,0 */
4828 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4829 r_type = R_PPC_TPREL16_HA;
4830 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4831 }
4832 break;
4833
4834 case R_PPC_TLS:
4835 if (tls_mask != 0
4836 && (tls_mask & TLS_TPREL) == 0)
4837 {
4838 bfd_vma insn, rtra;
4839 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4840 if ((insn & ((31 << 26) | (31 << 11)))
4841 == ((31 << 26) | (2 << 11)))
4842 rtra = insn & ((1 << 26) - (1 << 16));
4843 else if ((insn & ((31 << 26) | (31 << 16)))
4844 == ((31 << 26) | (2 << 16)))
4845 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4846 else
4847 abort ();
4848 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4849 /* add -> addi. */
4850 insn = 14 << 26;
4851 else if ((insn & (31 << 1)) == 23 << 1
4852 && ((insn & (31 << 6)) < 14 << 6
4853 || ((insn & (31 << 6)) >= 16 << 6
4854 && (insn & (31 << 6)) < 24 << 6)))
4855 /* load and store indexed -> dform. */
4856 insn = (32 | ((insn >> 6) & 31)) << 26;
4857 else if ((insn & (31 << 1)) == 21 << 1
4858 && (insn & (0x1a << 6)) == 0)
4859 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4860 insn = (((58 | ((insn >> 6) & 4)) << 26)
4861 | ((insn >> 6) & 1));
4862 else if ((insn & (31 << 1)) == 21 << 1
4863 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4864 /* lwax -> lwa. */
4865 insn = (58 << 26) | 2;
4866 else
4867 abort ();
4868 insn |= rtra;
4869 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4870 r_type = R_PPC_TPREL16_LO;
4871 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4872 /* Was PPC_TLS which sits on insn boundary, now
4873 PPC_TPREL16_LO which is at insn+2. */
4874 rel->r_offset += 2;
4875 }
4876 break;
4877
4878 case R_PPC_GOT_TLSGD16_HI:
4879 case R_PPC_GOT_TLSGD16_HA:
4880 tls_gd = TLS_TPRELGD;
4881 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4882 goto tls_gdld_hi;
4883 break;
4884
4885 case R_PPC_GOT_TLSLD16_HI:
4886 case R_PPC_GOT_TLSLD16_HA:
4887 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4888 {
4889 tls_gdld_hi:
4890 if ((tls_mask & tls_gd) != 0)
4891 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4892 + R_PPC_GOT_TPREL16);
4893 else
4894 {
4895 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4896 rel->r_offset -= 2;
4897 r_type = R_PPC_NONE;
4898 }
4899 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4900 }
4901 break;
4902
4903 case R_PPC_GOT_TLSGD16:
4904 case R_PPC_GOT_TLSGD16_LO:
4905 tls_gd = TLS_TPRELGD;
4906 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4907 goto tls_get_addr_check;
4908 break;
4909
4910 case R_PPC_GOT_TLSLD16:
4911 case R_PPC_GOT_TLSLD16_LO:
4912 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4913 {
4914 tls_get_addr_check:
4915 if (rel + 1 < relend)
4916 {
4917 enum elf_ppc_reloc_type r_type2;
4918 unsigned long r_symndx2;
4919 struct elf_link_hash_entry *h2;
87d243f1 4920 bfd_vma insn1, insn2;
7619e7c7
AM
4921 bfd_vma offset;
4922
4923 /* The next instruction should be a call to
4924 __tls_get_addr. Peek at the reloc to be sure. */
55fd94b0 4925 r_type2 = ELF32_R_TYPE (rel[1].r_info);
7619e7c7
AM
4926 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4927 if (r_symndx2 < symtab_hdr->sh_info
4928 || (r_type2 != R_PPC_REL14
4929 && r_type2 != R_PPC_REL14_BRTAKEN
4930 && r_type2 != R_PPC_REL14_BRNTAKEN
4931 && r_type2 != R_PPC_REL24
4932 && r_type2 != R_PPC_PLTREL24))
4933 break;
4934
4935 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4936 while (h2->root.type == bfd_link_hash_indirect
4937 || h2->root.type == bfd_link_hash_warning)
4938 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4939 if (h2 == NULL || h2 != htab->tls_get_addr)
4940 break;
4941
4942 /* OK, it checks out. Replace the call. */
4943 offset = rel[1].r_offset;
4944 insn1 = bfd_get_32 (output_bfd,
4945 contents + rel->r_offset - 2);
7619e7c7
AM
4946 if ((tls_mask & tls_gd) != 0)
4947 {
4948 /* IE */
4949 insn1 &= (1 << 26) - 1;
4950 insn1 |= 32 << 26; /* lwz */
4951 insn2 = 0x7c631214; /* add 3,3,2 */
4952 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4953 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4954 + R_PPC_GOT_TPREL16);
4955 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4956 }
4957 else
4958 {
4959 /* LE */
4960 insn1 = 0x3c620000; /* addis 3,2,0 */
4961 insn2 = 0x38630000; /* addi 3,3,0 */
4962 if (tls_gd == 0)
4963 {
4964 /* Was an LD reloc. */
4965 r_symndx = 0;
e1918d23
AM
4966 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4967 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
4968 }
4969 r_type = R_PPC_TPREL16_HA;
4970 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4971 rel[1].r_info = ELF32_R_INFO (r_symndx,
4972 R_PPC_TPREL16_LO);
4973 rel[1].r_offset += 2;
4974 }
7619e7c7
AM
4975 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4976 bfd_put_32 (output_bfd, insn2, contents + offset);
7619e7c7
AM
4977 if (tls_gd == 0)
4978 {
4979 /* We changed the symbol on an LD reloc. Start over
70bccea4 4980 in order to get h, sym, sec etc. right. */
7619e7c7
AM
4981 rel--;
4982 continue;
4983 }
4984 }
252b5132 4985 }
7619e7c7
AM
4986 break;
4987 }
4988
4989 /* Handle other relocations that tweak non-addend part of insn. */
4990 branch_bit = 0;
4991 switch (r_type)
4992 {
4993 default:
4994 break;
4995
4996 /* Branch taken prediction relocations. */
4997 case R_PPC_ADDR14_BRTAKEN:
4998 case R_PPC_REL14_BRTAKEN:
4999 branch_bit = BRANCH_PREDICT_BIT;
5000 /* Fall thru */
5001
4cc11e76 5002 /* Branch not taken prediction relocations. */
7619e7c7
AM
5003 case R_PPC_ADDR14_BRNTAKEN:
5004 case R_PPC_REL14_BRNTAKEN:
5005 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5006 insn &= ~BRANCH_PREDICT_BIT;
5007 insn |= branch_bit;
5008
5009 from = (rel->r_offset
5010 + input_section->output_offset
5011 + input_section->output_section->vma);
5012
5013 /* Invert 'y' bit if not the default. */
5014 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
5015 insn ^= BRANCH_PREDICT_BIT;
5016
5017 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5018 break;
252b5132
RH
5019 }
5020
7619e7c7
AM
5021 addend = rel->r_addend;
5022 tls_type = 0;
5023 howto = NULL;
55fd94b0
AM
5024 if (r_type < R_PPC_max)
5025 howto = ppc_elf_howto_table[r_type];
7619e7c7 5026 switch (r_type)
252b5132
RH
5027 {
5028 default:
7619e7c7 5029 (*_bfd_error_handler)
d003868e
AM
5030 (_("%B: unknown relocation type %d for symbol %s"),
5031 input_bfd, (int) r_type, sym_name);
252b5132
RH
5032
5033 bfd_set_error (bfd_error_bad_value);
b34976b6 5034 ret = FALSE;
252b5132
RH
5035 continue;
5036
7619e7c7
AM
5037 case R_PPC_NONE:
5038 case R_PPC_TLS:
5039 case R_PPC_EMB_MRKREF:
5040 case R_PPC_GNU_VTINHERIT:
5041 case R_PPC_GNU_VTENTRY:
7595d193
L
5042 continue;
5043
7619e7c7
AM
5044 /* GOT16 relocations. Like an ADDR16 using the symbol's
5045 address in the GOT as relocation value instead of the
5046 symbol's value itself. Also, create a GOT entry for the
5047 symbol and put the symbol value there. */
5048 case R_PPC_GOT_TLSGD16:
5049 case R_PPC_GOT_TLSGD16_LO:
5050 case R_PPC_GOT_TLSGD16_HI:
5051 case R_PPC_GOT_TLSGD16_HA:
5052 tls_type = TLS_TLS | TLS_GD;
5053 goto dogot;
5054
5055 case R_PPC_GOT_TLSLD16:
5056 case R_PPC_GOT_TLSLD16_LO:
5057 case R_PPC_GOT_TLSLD16_HI:
5058 case R_PPC_GOT_TLSLD16_HA:
5059 tls_type = TLS_TLS | TLS_LD;
5060 goto dogot;
5061
5062 case R_PPC_GOT_TPREL16:
5063 case R_PPC_GOT_TPREL16_LO:
5064 case R_PPC_GOT_TPREL16_HI:
5065 case R_PPC_GOT_TPREL16_HA:
5066 tls_type = TLS_TLS | TLS_TPREL;
5067 goto dogot;
5068
5069 case R_PPC_GOT_DTPREL16:
5070 case R_PPC_GOT_DTPREL16_LO:
5071 case R_PPC_GOT_DTPREL16_HI:
5072 case R_PPC_GOT_DTPREL16_HA:
5073 tls_type = TLS_TLS | TLS_DTPREL;
5074 goto dogot;
5075
5076 case R_PPC_GOT16:
5077 case R_PPC_GOT16_LO:
5078 case R_PPC_GOT16_HI:
5079 case R_PPC_GOT16_HA:
5080 dogot:
5081 {
5082 /* Relocation is to the entry for this symbol in the global
5083 offset table. */
5084 bfd_vma off;
5085 bfd_vma *offp;
5086 unsigned long indx;
5087
5088 if (htab->got == NULL)
5089 abort ();
5090
5091 indx = 0;
5092 if (tls_type == (TLS_TLS | TLS_LD)
d881513a 5093 && (h == NULL
f5385ebf 5094 || !h->def_dynamic))
7619e7c7
AM
5095 offp = &htab->tlsld_got.offset;
5096 else if (h != NULL)
5097 {
5098 bfd_boolean dyn;
5099 dyn = htab->elf.dynamic_sections_created;
ee05f2fe 5100 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7619e7c7
AM
5101 || (info->shared
5102 && SYMBOL_REFERENCES_LOCAL (info, h)))
5103 /* This is actually a static link, or it is a
5104 -Bsymbolic link and the symbol is defined
5105 locally, or the symbol was forced to be local
5106 because of a version file. */
5107 ;
5108 else
5109 {
5110 indx = h->dynindx;
5111 unresolved_reloc = FALSE;
5112 }
5113 offp = &h->got.offset;
5114 }
5115 else
5116 {
5117 if (local_got_offsets == NULL)
5118 abort ();
5119 offp = &local_got_offsets[r_symndx];
5120 }
5121
5122 /* The offset must always be a multiple of 4. We use the
5123 least significant bit to record whether we have already
5124 processed this entry. */
5125 off = *offp;
5126 if ((off & 1) != 0)
5127 off &= ~1;
5128 else
5129 {
70bccea4
AM
5130 unsigned int tls_m = (tls_mask
5131 & (TLS_LD | TLS_GD | TLS_DTPREL
5132 | TLS_TPREL | TLS_TPRELGD));
5133
5134 if (offp == &htab->tlsld_got.offset)
5135 tls_m = TLS_LD;
5136 else if (h == NULL
f5385ebf 5137 || !h->def_dynamic)
70bccea4
AM
5138 tls_m &= ~TLS_LD;
5139
5140 /* We might have multiple got entries for this sym.
5141 Initialize them all. */
5142 do
7619e7c7 5143 {
70bccea4
AM
5144 int tls_ty = 0;
5145
5146 if ((tls_m & TLS_LD) != 0)
7619e7c7 5147 {
70bccea4
AM
5148 tls_ty = TLS_TLS | TLS_LD;
5149 tls_m &= ~TLS_LD;
5150 }
5151 else if ((tls_m & TLS_GD) != 0)
5152 {
5153 tls_ty = TLS_TLS | TLS_GD;
5154 tls_m &= ~TLS_GD;
5155 }
5156 else if ((tls_m & TLS_DTPREL) != 0)
5157 {
5158 tls_ty = TLS_TLS | TLS_DTPREL;
5159 tls_m &= ~TLS_DTPREL;
5160 }
5161 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5162 {
5163 tls_ty = TLS_TLS | TLS_TPREL;
5164 tls_m = 0;
7619e7c7 5165 }
7619e7c7 5166
70bccea4 5167 /* Generate relocs for the dynamic linker. */
4e795f50
AM
5168 if ((info->shared || indx != 0)
5169 && (h == NULL
5170 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5171 || h->root.type != bfd_link_hash_undefweak))
7619e7c7 5172 {
70bccea4
AM
5173 outrel.r_offset = (htab->got->output_section->vma
5174 + htab->got->output_offset
5175 + off);
e515b051 5176 outrel.r_addend = 0;
70bccea4
AM
5177 if (tls_ty & (TLS_LD | TLS_GD))
5178 {
5179 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
70bccea4
AM
5180 if (tls_ty == (TLS_TLS | TLS_GD))
5181 {
5182 loc = htab->relgot->contents;
5183 loc += (htab->relgot->reloc_count++
5184 * sizeof (Elf32_External_Rela));
5185 bfd_elf32_swap_reloca_out (output_bfd,
5186 &outrel, loc);
e515b051 5187 outrel.r_offset += 4;
70bccea4
AM
5188 outrel.r_info
5189 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
70bccea4
AM
5190 }
5191 }
5192 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5193 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5194 else if (tls_ty == (TLS_TLS | TLS_TPREL))
5195 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5196 else if (indx == 0)
5197 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5198 else
5199 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
70bccea4 5200 if (indx == 0)
e515b051
AM
5201 {
5202 outrel.r_addend += relocation;
5203 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 5204 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 5205 }
70bccea4
AM
5206 loc = htab->relgot->contents;
5207 loc += (htab->relgot->reloc_count++
5208 * sizeof (Elf32_External_Rela));
5209 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7619e7c7
AM
5210 }
5211
70bccea4
AM
5212 /* Init the .got section contents if we're not
5213 emitting a reloc. */
5214 else
7619e7c7 5215 {
70bccea4
AM
5216 bfd_vma value = relocation;
5217
7b609f53
AM
5218 if (tls_ty == (TLS_TLS | TLS_LD))
5219 value = 1;
5220 else if (tls_ty != 0)
70bccea4 5221 {
e1918d23 5222 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 5223 if (tls_ty == (TLS_TLS | TLS_TPREL))
70bccea4 5224 value += DTP_OFFSET - TP_OFFSET;
70bccea4 5225
7b609f53
AM
5226 if (tls_ty == (TLS_TLS | TLS_GD))
5227 {
5228 bfd_put_32 (output_bfd, value,
5229 htab->got->contents + off + 4);
5230 value = 1;
5231 }
70bccea4 5232 }
70bccea4
AM
5233 bfd_put_32 (output_bfd, value,
5234 htab->got->contents + off);
7619e7c7 5235 }
70bccea4
AM
5236
5237 off += 4;
5238 if (tls_ty & (TLS_LD | TLS_GD))
5239 off += 4;
7619e7c7 5240 }
70bccea4
AM
5241 while (tls_m != 0);
5242
5243 off = *offp;
5244 *offp = off | 1;
7619e7c7
AM
5245 }
5246
5247 if (off >= (bfd_vma) -2)
5248 abort ();
5249
70bccea4
AM
5250 if ((tls_type & TLS_TLS) != 0)
5251 {
5252 if (tls_type != (TLS_TLS | TLS_LD))
5253 {
5254 if ((tls_mask & TLS_LD) != 0
5255 && !(h == NULL
f5385ebf 5256 || !h->def_dynamic))
70bccea4
AM
5257 off += 8;
5258 if (tls_type != (TLS_TLS | TLS_GD))
5259 {
5260 if ((tls_mask & TLS_GD) != 0)
5261 off += 8;
5262 if (tls_type != (TLS_TLS | TLS_DTPREL))
5263 {
5264 if ((tls_mask & TLS_DTPREL) != 0)
5265 off += 4;
5266 }
5267 }
5268 }
5269 }
5270
7619e7c7
AM
5271 relocation = htab->got->output_offset + off - 4;
5272
5273 /* Addends on got relocations don't make much sense.
5274 x+off@got is actually x@got+off, and since the got is
5275 generated by a hash table traversal, the value in the
5276 got at entry m+n bears little relation to the entry m. */
5277 if (addend != 0)
5278 (*_bfd_error_handler)
d003868e
AM
5279 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
5280 input_bfd,
5281 input_section,
7619e7c7 5282 (long) rel->r_offset,
7b609f53 5283 howto->name,
7619e7c7
AM
5284 sym_name);
5285 }
70bccea4 5286 break;
7619e7c7 5287
252b5132 5288 /* Relocations that need no special processing. */
7619e7c7 5289 case R_PPC_LOCAL24PC:
252b5132
RH
5290 /* It makes no sense to point a local relocation
5291 at a symbol not in this object. */
7b609f53 5292 if (unresolved_reloc)
252b5132 5293 {
464e1740
ILT
5294 if (! (*info->callbacks->undefined_symbol) (info,
5295 h->root.root.string,
5296 input_bfd,
5297 input_section,
5cc7c785 5298 rel->r_offset,
b34976b6
AM
5299 TRUE))
5300 return FALSE;
252b5132
RH
5301 continue;
5302 }
5303 break;
5304
7619e7c7
AM
5305 case R_PPC_DTPREL16:
5306 case R_PPC_DTPREL16_LO:
5307 case R_PPC_DTPREL16_HI:
5308 case R_PPC_DTPREL16_HA:
e1918d23 5309 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
5310 break;
5311
70bccea4
AM
5312 /* Relocations that may need to be propagated if this is a shared
5313 object. */
7619e7c7
AM
5314 case R_PPC_TPREL16:
5315 case R_PPC_TPREL16_LO:
5316 case R_PPC_TPREL16_HI:
5317 case R_PPC_TPREL16_HA:
e1918d23 5318 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7619e7c7
AM
5319 /* The TPREL16 relocs shouldn't really be used in shared
5320 libs as they will result in DT_TEXTREL being set, but
5321 support them anyway. */
5322 goto dodyn;
5323
5324 case R_PPC_TPREL32:
e1918d23 5325 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7619e7c7
AM
5326 goto dodyn;
5327
5328 case R_PPC_DTPREL32:
e1918d23 5329 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
5330 goto dodyn;
5331
e515b051
AM
5332 case R_PPC_DTPMOD32:
5333 relocation = 1;
5334 addend = 0;
5335 goto dodyn;
5336
7619e7c7
AM
5337 case R_PPC_REL24:
5338 case R_PPC_REL32:
5339 case R_PPC_REL14:
5340 case R_PPC_REL14_BRTAKEN:
5341 case R_PPC_REL14_BRNTAKEN:
252b5132
RH
5342 /* If these relocations are not to a named symbol, they can be
5343 handled right here, no need to bother the dynamic linker. */
f6c52c13
AM
5344 if (SYMBOL_REFERENCES_LOCAL (info, h)
5345 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
252b5132 5346 break;
70bccea4 5347 /* fall through */
252b5132 5348
70bccea4
AM
5349 /* Relocations that always need to be propagated if this is a shared
5350 object. */
7619e7c7
AM
5351 case R_PPC_ADDR32:
5352 case R_PPC_ADDR24:
5353 case R_PPC_ADDR16:
5354 case R_PPC_ADDR16_LO:
5355 case R_PPC_ADDR16_HI:
5356 case R_PPC_ADDR16_HA:
5357 case R_PPC_ADDR14:
5358 case R_PPC_ADDR14_BRTAKEN:
5359 case R_PPC_ADDR14_BRNTAKEN:
5360 case R_PPC_UADDR32:
5361 case R_PPC_UADDR16:
ee05f2fe
AM
5362 /* r_symndx will be zero only for relocs against symbols
5363 from removed linkonce sections, or sections discarded by
5364 a linker script. */
7619e7c7 5365 dodyn:
ee05f2fe
AM
5366 if (r_symndx == 0)
5367 break;
5368 /* Fall thru. */
5369
c87b5a93
AM
5370 if ((input_section->flags & SEC_ALLOC) == 0)
5371 break;
5372 /* Fall thru. */
5373
ee05f2fe 5374 if ((info->shared
4e795f50
AM
5375 && (h == NULL
5376 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5377 || h->root.type != bfd_link_hash_undefweak)
ee05f2fe 5378 && (MUST_BE_DYN_RELOC (r_type)
f6c52c13 5379 || !SYMBOL_CALLS_LOCAL (info, h)))
ee05f2fe
AM
5380 || (ELIMINATE_COPY_RELOCS
5381 && !info->shared
ee05f2fe
AM
5382 && h != NULL
5383 && h->dynindx != -1
f5385ebf
AM
5384 && !h->non_got_ref
5385 && h->def_dynamic
5386 && !h->def_regular))
252b5132 5387 {
0bb2d96a 5388 int skip;
252b5132
RH
5389
5390#ifdef DEBUG
55fd94b0
AM
5391 fprintf (stderr, "ppc_elf_relocate_section needs to "
5392 "create relocation for %s\n",
70bccea4
AM
5393 (h && h->root.root.string
5394 ? h->root.root.string : "<unknown>"));
252b5132
RH
5395#endif
5396
5397 /* When generating a shared object, these relocations
70bccea4
AM
5398 are copied into the output file to be resolved at run
5399 time. */
252b5132
RH
5400 if (sreloc == NULL)
5401 {
5402 const char *name;
5403
5404 name = (bfd_elf_string_from_elf_section
5405 (input_bfd,
5406 elf_elfheader (input_bfd)->e_shstrndx,
5407 elf_section_data (input_section)->rel_hdr.sh_name));
5408 if (name == NULL)
b34976b6 5409 return FALSE;
252b5132
RH
5410
5411 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5412 && strcmp (bfd_get_section_name (input_bfd,
5413 input_section),
5414 name + 5) == 0);
5415
7619e7c7 5416 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
252b5132
RH
5417 BFD_ASSERT (sreloc != NULL);
5418 }
5419
0bb2d96a 5420 skip = 0;
252b5132 5421
c629eae0
JJ
5422 outrel.r_offset =
5423 _bfd_elf_section_offset (output_bfd, info, input_section,
5424 rel->r_offset);
0bb2d96a
JJ
5425 if (outrel.r_offset == (bfd_vma) -1
5426 || outrel.r_offset == (bfd_vma) -2)
5427 skip = (int) outrel.r_offset;
252b5132
RH
5428 outrel.r_offset += (input_section->output_section->vma
5429 + input_section->output_offset);
5430
5431 if (skip)
5432 memset (&outrel, 0, sizeof outrel);
f6c52c13 5433 else if (!SYMBOL_REFERENCES_LOCAL (info, h))
252b5132 5434 {
7619e7c7 5435 unresolved_reloc = FALSE;
252b5132
RH
5436 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5437 outrel.r_addend = rel->r_addend;
5438 }
5439 else
5440 {
47388f4c
AM
5441 outrel.r_addend = relocation + rel->r_addend;
5442
252b5132 5443 if (r_type == R_PPC_ADDR32)
47388f4c 5444 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
252b5132
RH
5445 else
5446 {
5447 long indx;
5448
47388f4c 5449 if (bfd_is_abs_section (sec))
252b5132
RH
5450 indx = 0;
5451 else if (sec == NULL || sec->owner == NULL)
5452 {
5453 bfd_set_error (bfd_error_bad_value);
b34976b6 5454 return FALSE;
252b5132
RH
5455 }
5456 else
5457 {
5458 asection *osec;
5459
47388f4c
AM
5460 /* We are turning this relocation into one
5461 against a section symbol. It would be
5462 proper to subtract the symbol's value,
5463 osec->vma, from the emitted reloc addend,
5464 but ld.so expects buggy relocs. */
252b5132
RH
5465 osec = sec->output_section;
5466 indx = elf_section_data (osec)->dynindx;
8da6118f 5467 BFD_ASSERT (indx > 0);
252b5132
RH
5468#ifdef DEBUG
5469 if (indx <= 0)
55fd94b0
AM
5470 printf ("indx=%d section=%s flags=%08x name=%s\n",
5471 indx, osec->name, osec->flags,
5472 h->root.root.string);
252b5132
RH
5473#endif
5474 }
5475
5476 outrel.r_info = ELF32_R_INFO (indx, r_type);
252b5132
RH
5477 }
5478 }
5479
947216bf
AM
5480 loc = sreloc->contents;
5481 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5482 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132 5483
2243deae 5484 if (skip == -1)
252b5132 5485 continue;
2243deae
L
5486
5487 /* This reloc will be computed at runtime. We clear the memory
5488 so that it contains predictable value. */
5489 if (! skip
5490 && ((input_section->flags & SEC_ALLOC) != 0
5491 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5492 {
5493 relocation = howto->pc_relative ? outrel.r_offset : 0;
5494 addend = 0;
5495 break;
5496 }
252b5132 5497 }
252b5132
RH
5498 break;
5499
b4a38de6
AM
5500 case R_PPC_RELAX32PC_PLT:
5501 case R_PPC_RELAX32_PLT:
5502 BFD_ASSERT (h != NULL
5503 && h->plt.offset != (bfd_vma) -1
5504 && htab->plt != NULL);
5505
5506 relocation = (htab->plt->output_section->vma
5507 + htab->plt->output_offset
5508 + h->plt.offset);
5509 if (r_type == R_PPC_RELAX32_PLT)
5510 goto relax32;
5511 /* Fall thru */
5512
f31a141e
AM
5513 case R_PPC_RELAX32PC:
5514 relocation -= (input_section->output_section->vma
5515 + input_section->output_offset
5516 + rel->r_offset - 4);
5517 /* Fall thru */
b4a38de6 5518
deaaf2f3 5519 case R_PPC_RELAX32:
b4a38de6 5520 relax32:
9abc968f
AM
5521 {
5522 unsigned long t0;
5523 unsigned long t1;
5524
5525 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
5526 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
5527
5528 /* We're clearing the bits for R_PPC_ADDR16_HA
5529 and R_PPC_ADDR16_LO here. */
5530 t0 &= ~0xffff;
5531 t1 &= ~0xffff;
5532
5533 /* t0 is HA, t1 is LO */
5534 relocation += addend;
5535 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
5536 t1 |= relocation & 0xffff;
5537
5538 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
5539 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
5540 }
8517fae7 5541 continue;
deaaf2f3 5542
70bccea4 5543 /* Indirect .sdata relocation. */
7619e7c7 5544 case R_PPC_EMB_SDAI16:
c9a2f333 5545 BFD_ASSERT (htab->sdata[0].section != NULL);
7619e7c7 5546 relocation
76750a2f
AM
5547 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
5548 h, relocation, rel);
252b5132
RH
5549 break;
5550
70bccea4 5551 /* Indirect .sdata2 relocation. */
7619e7c7 5552 case R_PPC_EMB_SDA2I16:
c9a2f333 5553 BFD_ASSERT (htab->sdata[1].section != NULL);
7619e7c7 5554 relocation
76750a2f
AM
5555 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
5556 h, relocation, rel);
252b5132
RH
5557 break;
5558
70bccea4
AM
5559 /* Handle the TOC16 reloc. We want to use the offset within the .got
5560 section, not the actual VMA. This is appropriate when generating
5561 an embedded ELF object, for which the .got section acts like the
5562 AIX .toc section. */
7619e7c7 5563 case R_PPC_TOC16: /* phony GOT16 relocations */
55fd94b0 5564 BFD_ASSERT (sec != NULL);
252b5132
RH
5565 BFD_ASSERT (bfd_is_und_section (sec)
5566 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
f12123c0 5567 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
252b5132 5568
70bccea4 5569 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
252b5132
RH
5570 break;
5571
7619e7c7 5572 case R_PPC_PLTREL24:
252b5132 5573 /* Relocation is to the entry for this symbol in the
70bccea4 5574 procedure linkage table. */
252b5132
RH
5575 BFD_ASSERT (h != NULL);
5576
5577 if (h->plt.offset == (bfd_vma) -1
7619e7c7 5578 || htab->plt == NULL)
252b5132
RH
5579 {
5580 /* We didn't make a PLT entry for this symbol. This
70bccea4
AM
5581 happens when statically linking PIC code, or when
5582 using -Bsymbolic. */
252b5132
RH
5583 break;
5584 }
5585
7619e7c7
AM
5586 unresolved_reloc = FALSE;
5587 relocation = (htab->plt->output_section->vma
5588 + htab->plt->output_offset
252b5132 5589 + h->plt.offset);
8da6118f 5590 break;
252b5132 5591
70bccea4 5592 /* Relocate against _SDA_BASE_. */
7619e7c7 5593 case R_PPC_SDAREL16:
252b5132
RH
5594 {
5595 const char *name;
5596
55fd94b0 5597 BFD_ASSERT (sec != NULL);
252b5132 5598 name = bfd_get_section_name (abfd, sec->output_section);
5f819128
AM
5599 if (! ((strncmp (name, ".sdata", 6) == 0
5600 && (name[6] == 0 || name[6] == '.'))
5601 || (strncmp (name, ".sbss", 5) == 0
5602 && (name[5] == 0 || name[5] == '.'))))
252b5132 5603 {
55fd94b0 5604 (*_bfd_error_handler)
d003868e 5605 (_("%B: the target (%s) of a %s relocation is "
55fd94b0 5606 "in the wrong output section (%s)"),
d003868e 5607 input_bfd,
55fd94b0
AM
5608 sym_name,
5609 howto->name,
5610 name);
252b5132 5611 }
c9a2f333 5612 addend -= htab->sdata[0].sym_val;
252b5132
RH
5613 }
5614 break;
5615
70bccea4 5616 /* Relocate against _SDA2_BASE_. */
7619e7c7 5617 case R_PPC_EMB_SDA2REL:
252b5132
RH
5618 {
5619 const char *name;
5620
55fd94b0 5621 BFD_ASSERT (sec != NULL);
252b5132 5622 name = bfd_get_section_name (abfd, sec->output_section);
5f819128
AM
5623 if (! (strncmp (name, ".sdata2", 7) == 0
5624 || strncmp (name, ".sbss2", 6) == 0))
252b5132 5625 {
55fd94b0 5626 (*_bfd_error_handler)
d003868e 5627 (_("%B: the target (%s) of a %s relocation is "
55fd94b0 5628 "in the wrong output section (%s)"),
d003868e 5629 input_bfd,
55fd94b0
AM
5630 sym_name,
5631 howto->name,
5632 name);
c3668558 5633
252b5132 5634 bfd_set_error (bfd_error_bad_value);
b34976b6 5635 ret = FALSE;
252b5132
RH
5636 continue;
5637 }
c9a2f333 5638 addend -= htab->sdata[1].sym_val;
252b5132
RH
5639 }
5640 break;
5641
70bccea4 5642 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
7619e7c7
AM
5643 case R_PPC_EMB_SDA21:
5644 case R_PPC_EMB_RELSDA:
252b5132
RH
5645 {
5646 const char *name;
5647 int reg;
5648
55fd94b0 5649 BFD_ASSERT (sec != NULL);
252b5132 5650 name = bfd_get_section_name (abfd, sec->output_section);
b34976b6 5651 if (((strncmp (name, ".sdata", 6) == 0
5f819128
AM
5652 && (name[6] == 0 || name[6] == '.'))
5653 || (strncmp (name, ".sbss", 5) == 0
5654 && (name[5] == 0 || name[5] == '.'))))
252b5132
RH
5655 {
5656 reg = 13;
c9a2f333 5657 addend -= htab->sdata[0].sym_val;
252b5132
RH
5658 }
5659
5f819128
AM
5660 else if (strncmp (name, ".sdata2", 7) == 0
5661 || strncmp (name, ".sbss2", 6) == 0)
252b5132
RH
5662 {
5663 reg = 2;
c9a2f333 5664 addend -= htab->sdata[1].sym_val;
252b5132
RH
5665 }
5666
8da6118f
KH
5667 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5668 || strcmp (name, ".PPC.EMB.sbss0") == 0)
252b5132
RH
5669 {
5670 reg = 0;
5671 }
5672
5673 else
5674 {
55fd94b0 5675 (*_bfd_error_handler)
d003868e 5676 (_("%B: the target (%s) of a %s relocation is "
55fd94b0 5677 "in the wrong output section (%s)"),
d003868e 5678 input_bfd,
55fd94b0
AM
5679 sym_name,
5680 howto->name,
5681 name);
252b5132
RH
5682
5683 bfd_set_error (bfd_error_bad_value);
b34976b6 5684 ret = FALSE;
252b5132
RH
5685 continue;
5686 }
5687
5688 if (r_type == R_PPC_EMB_SDA21)
5689 { /* fill in register field */
7619e7c7 5690 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
252b5132 5691 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
7619e7c7 5692 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
252b5132
RH
5693 }
5694 }
5695 break;
5696
70bccea4 5697 /* Relocate against the beginning of the section. */
7619e7c7
AM
5698 case R_PPC_SECTOFF:
5699 case R_PPC_SECTOFF_LO:
5700 case R_PPC_SECTOFF_HI:
5701 case R_PPC_SECTOFF_HA:
55fd94b0 5702 BFD_ASSERT (sec != NULL);
252b5132 5703 addend -= sec->output_section->vma;
252b5132
RH
5704 break;
5705
70bccea4 5706 /* Negative relocations. */
7619e7c7
AM
5707 case R_PPC_EMB_NADDR32:
5708 case R_PPC_EMB_NADDR16:
5709 case R_PPC_EMB_NADDR16_LO:
5710 case R_PPC_EMB_NADDR16_HI:
5711 case R_PPC_EMB_NADDR16_HA:
8da6118f 5712 addend -= 2 * relocation;
252b5132
RH
5713 break;
5714
7619e7c7
AM
5715 case R_PPC_COPY:
5716 case R_PPC_GLOB_DAT:
5717 case R_PPC_JMP_SLOT:
5718 case R_PPC_RELATIVE:
5719 case R_PPC_PLT32:
5720 case R_PPC_PLTREL32:
5721 case R_PPC_PLT16_LO:
5722 case R_PPC_PLT16_HI:
5723 case R_PPC_PLT16_HA:
5724 case R_PPC_ADDR30:
5725 case R_PPC_EMB_RELSEC16:
5726 case R_PPC_EMB_RELST_LO:
5727 case R_PPC_EMB_RELST_HI:
5728 case R_PPC_EMB_RELST_HA:
5729 case R_PPC_EMB_BIT_FLD:
5730 (*_bfd_error_handler)
d003868e
AM
5731 (_("%B: relocation %s is not yet supported for symbol %s."),
5732 input_bfd,
7619e7c7
AM
5733 howto->name,
5734 sym_name);
252b5132
RH
5735
5736 bfd_set_error (bfd_error_invalid_operation);
b34976b6 5737 ret = FALSE;
252b5132 5738 continue;
7619e7c7 5739 }
252b5132 5740
7619e7c7
AM
5741 /* Do any further special processing. */
5742 switch (r_type)
5743 {
5744 default:
5745 break;
5746
5747 case R_PPC_ADDR16_HA:
5748 case R_PPC_GOT16_HA:
5749 case R_PPC_PLT16_HA:
5750 case R_PPC_SECTOFF_HA:
5751 case R_PPC_TPREL16_HA:
5752 case R_PPC_DTPREL16_HA:
5753 case R_PPC_GOT_TLSGD16_HA:
5754 case R_PPC_GOT_TLSLD16_HA:
5755 case R_PPC_GOT_TPREL16_HA:
5756 case R_PPC_GOT_DTPREL16_HA:
5757 case R_PPC_EMB_NADDR16_HA:
5758 case R_PPC_EMB_RELST_HA:
5759 /* It's just possible that this symbol is a weak symbol
7b609f53 5760 that's not actually defined anywhere. In that case,
7619e7c7
AM
5761 'sec' would be NULL, and we should leave the symbol
5762 alone (it will be set to zero elsewhere in the link). */
5763 if (sec != NULL)
e515b051
AM
5764 /* Add 0x10000 if sign bit in 0:15 is set.
5765 Bits 0:15 are not used. */
5766 addend += 0x8000;
7619e7c7 5767 break;
252b5132
RH
5768 }
5769
252b5132 5770#ifdef DEBUG
55fd94b0
AM
5771 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5772 "offset = %ld, addend = %ld\n",
252b5132 5773 howto->name,
8da6118f 5774 (int) r_type,
252b5132
RH
5775 sym_name,
5776 r_symndx,
7619e7c7 5777 (long) rel->r_offset,
8da6118f 5778 (long) addend);
252b5132
RH
5779#endif
5780
7619e7c7
AM
5781 if (unresolved_reloc
5782 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 5783 && h->def_dynamic))
7619e7c7
AM
5784 {
5785 (*_bfd_error_handler)
d003868e
AM
5786 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5787 input_bfd,
5788 input_section,
7619e7c7 5789 (long) rel->r_offset,
7b609f53 5790 howto->name,
7619e7c7
AM
5791 sym_name);
5792 ret = FALSE;
5793 }
5794
252b5132
RH
5795 r = _bfd_final_link_relocate (howto,
5796 input_bfd,
5797 input_section,
5798 contents,
7619e7c7 5799 rel->r_offset,
252b5132
RH
5800 relocation,
5801 addend);
5802
7619e7c7 5803 if (r != bfd_reloc_ok)
252b5132 5804 {
7619e7c7 5805 if (r == bfd_reloc_overflow)
252b5132 5806 {
7619e7c7
AM
5807 if (warned)
5808 continue;
5809 if (h != NULL
5810 && h->root.type == bfd_link_hash_undefweak
dc1bc0c9
RH
5811 && howto->pc_relative)
5812 {
5813 /* Assume this is a call protected by other code that
5814 detect the symbol is undefined. If this is the case,
5815 we can safely ignore the overflow. If not, the
5816 program is hosed anyway, and a little warning isn't
5817 going to help. */
252b5132 5818
dc1bc0c9
RH
5819 continue;
5820 }
252b5132 5821
7619e7c7 5822 if (! (*info->callbacks->reloc_overflow) (info,
dfeffb9f 5823 (h ? &h->root : NULL),
7619e7c7
AM
5824 sym_name,
5825 howto->name,
5826 rel->r_addend,
5827 input_bfd,
5828 input_section,
5829 rel->r_offset))
5830 return FALSE;
dc1bc0c9
RH
5831 }
5832 else
5833 {
7619e7c7 5834 (*_bfd_error_handler)
d003868e
AM
5835 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
5836 input_bfd, input_section,
7b609f53 5837 (long) rel->r_offset, howto->name, sym_name, (int) r);
7619e7c7 5838 ret = FALSE;
252b5132
RH
5839 }
5840 }
5841 }
5842
252b5132
RH
5843#ifdef DEBUG
5844 fprintf (stderr, "\n");
5845#endif
5846
5847 return ret;
5848}
252b5132 5849\f
25dbc73a
AM
5850/* Finish up dynamic symbol handling. We set the contents of various
5851 dynamic sections here. */
c5fccbec 5852
b34976b6 5853static bfd_boolean
25dbc73a
AM
5854ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
5855 struct bfd_link_info *info,
5856 struct elf_link_hash_entry *h,
5857 Elf_Internal_Sym *sym)
e1a9cb8e 5858{
25dbc73a 5859 struct ppc_elf_link_hash_table *htab;
e1a9cb8e 5860
25dbc73a
AM
5861#ifdef DEBUG
5862 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
5863 h->root.root.string);
5864#endif
e1a9cb8e 5865
25dbc73a
AM
5866 htab = ppc_elf_hash_table (info);
5867 BFD_ASSERT (htab->elf.dynobj != NULL);
e1a9cb8e 5868
25dbc73a 5869 if (h->plt.offset != (bfd_vma) -1)
e1a9cb8e 5870 {
25dbc73a
AM
5871 Elf_Internal_Rela rela;
5872 bfd_byte *loc;
5873 bfd_vma reloc_index;
e1a9cb8e 5874
25dbc73a
AM
5875#ifdef DEBUG
5876 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
5877#endif
e1a9cb8e 5878
25dbc73a
AM
5879 /* This symbol has an entry in the procedure linkage table. Set
5880 it up. */
e1a9cb8e 5881
25dbc73a
AM
5882 BFD_ASSERT (h->dynindx != -1);
5883 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
e1a9cb8e 5884
25dbc73a
AM
5885 /* We don't need to fill in the .plt. The ppc dynamic linker
5886 will fill it in. */
e1a9cb8e 5887
25dbc73a
AM
5888 /* Fill in the entry in the .rela.plt section. */
5889 rela.r_offset = (htab->plt->output_section->vma
5890 + htab->plt->output_offset
5891 + h->plt.offset);
5892 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
5893 rela.r_addend = 0;
e1a9cb8e 5894
25dbc73a
AM
5895 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
5896 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
5897 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
5898 loc = (htab->relplt->contents
5899 + reloc_index * sizeof (Elf32_External_Rela));
5900 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
e1a9cb8e 5901
25dbc73a
AM
5902 if (!h->def_regular)
5903 {
5904 /* Mark the symbol as undefined, rather than as defined in
5905 the .plt section. Leave the value alone. */
5906 sym->st_shndx = SHN_UNDEF;
5907 /* If the symbol is weak, we do need to clear the value.
5908 Otherwise, the PLT entry would provide a definition for
5909 the symbol even if the symbol wasn't defined anywhere,
5910 and so the symbol would never be NULL. */
5911 if (!h->ref_regular_nonweak)
5912 sym->st_value = 0;
5913 }
e1a9cb8e
NC
5914 }
5915
25dbc73a
AM
5916 if (h->needs_copy)
5917 {
5918 asection *s;
5919 Elf_Internal_Rela rela;
5920 bfd_byte *loc;
e1a9cb8e 5921
25dbc73a 5922 /* This symbols needs a copy reloc. Set it up. */
e1a9cb8e 5923
25dbc73a
AM
5924#ifdef DEBUG
5925 fprintf (stderr, ", copy");
5926#endif
e1a9cb8e 5927
25dbc73a 5928 BFD_ASSERT (h->dynindx != -1);
86bbe32f 5929
25dbc73a
AM
5930 if (h->size <= elf_gp_size (htab->elf.dynobj))
5931 s = htab->relsbss;
5932 else
5933 s = htab->relbss;
5934 BFD_ASSERT (s != NULL);
e1a9cb8e 5935
25dbc73a
AM
5936 rela.r_offset = (h->root.u.def.value
5937 + h->root.u.def.section->output_section->vma
5938 + h->root.u.def.section->output_offset);
5939 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
5940 rela.r_addend = 0;
5941 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5942 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5943 }
e1a9cb8e 5944
25dbc73a
AM
5945#ifdef DEBUG
5946 fprintf (stderr, "\n");
5947#endif
e1a9cb8e 5948
25dbc73a
AM
5949 /* Mark some specially defined symbols as absolute. */
5950 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5951 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
5952 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
5953 sym->st_shndx = SHN_ABS;
e1a9cb8e 5954
25dbc73a
AM
5955 return TRUE;
5956}
5957\f
5958static enum elf_reloc_type_class
5959ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
e1a9cb8e 5960{
25dbc73a
AM
5961 switch (ELF32_R_TYPE (rela->r_info))
5962 {
5963 case R_PPC_RELATIVE:
5964 return reloc_class_relative;
5965 case R_PPC_REL24:
5966 case R_PPC_ADDR24:
5967 case R_PPC_JMP_SLOT:
5968 return reloc_class_plt;
5969 case R_PPC_COPY:
5970 return reloc_class_copy;
5971 default:
5972 return reloc_class_normal;
5973 }
e1a9cb8e 5974}
25dbc73a
AM
5975\f
5976/* Finish up the dynamic sections. */
e1a9cb8e 5977
25dbc73a
AM
5978static bfd_boolean
5979ppc_elf_finish_dynamic_sections (bfd *output_bfd,
5980 struct bfd_link_info *info)
e1a9cb8e 5981{
25dbc73a
AM
5982 asection *sdyn;
5983 struct ppc_elf_link_hash_table *htab;
e1a9cb8e 5984
25dbc73a
AM
5985#ifdef DEBUG
5986 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
5987#endif
6b0817e5 5988
25dbc73a
AM
5989 htab = ppc_elf_hash_table (info);
5990 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
e1a9cb8e 5991
25dbc73a 5992 if (htab->elf.dynamic_sections_created)
e1a9cb8e 5993 {
25dbc73a 5994 Elf32_External_Dyn *dyncon, *dynconend;
e1a9cb8e 5995
25dbc73a 5996 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
86bbe32f 5997
25dbc73a
AM
5998 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5999 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6000 for (; dyncon < dynconend; dyncon++)
6001 {
6002 Elf_Internal_Dyn dyn;
6003 asection *s;
e1a9cb8e 6004
25dbc73a 6005 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
86bbe32f 6006
25dbc73a
AM
6007 switch (dyn.d_tag)
6008 {
6009 case DT_PLTGOT:
6010 s = htab->plt;
6011 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6012 break;
86bbe32f 6013
25dbc73a
AM
6014 case DT_PLTRELSZ:
6015 dyn.d_un.d_val = htab->relplt->size;
6016 break;
e1a9cb8e 6017
25dbc73a
AM
6018 case DT_JMPREL:
6019 s = htab->relplt;
6020 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6021 break;
2f89ff8d 6022
25dbc73a
AM
6023 default:
6024 continue;
6025 }
4c45e5c9 6026
25dbc73a
AM
6027 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6028 }
6029 }
4c45e5c9 6030
25dbc73a
AM
6031 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
6032 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
6033 if (htab->got)
6034 {
6035 unsigned char *contents = htab->got->contents;
6036 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
2f89ff8d 6037
25dbc73a
AM
6038 if (sdyn == NULL)
6039 bfd_put_32 (output_bfd, 0, contents + 4);
6040 else
6041 bfd_put_32 (output_bfd,
6042 sdyn->output_section->vma + sdyn->output_offset,
6043 contents + 4);
6044
6045 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
6046 }
6047
6048 return TRUE;
6049}
e1a9cb8e 6050\f
252b5132
RH
6051#define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
6052#define TARGET_LITTLE_NAME "elf32-powerpcle"
6053#define TARGET_BIG_SYM bfd_elf32_powerpc_vec
6054#define TARGET_BIG_NAME "elf32-powerpc"
6055#define ELF_ARCH bfd_arch_powerpc
6056#define ELF_MACHINE_CODE EM_PPC
d0facd1b
NC
6057#ifdef __QNXTARGET__
6058#define ELF_MAXPAGESIZE 0x1000
6059#else
252b5132 6060#define ELF_MAXPAGESIZE 0x10000
d0facd1b 6061#endif
b1342370 6062#define ELF_MINPAGESIZE 0x1000
252b5132
RH
6063#define elf_info_to_howto ppc_elf_info_to_howto
6064
6065#ifdef EM_CYGNUS_POWERPC
6066#define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6067#endif
6068
6069#ifdef EM_PPC_OLD
6070#define ELF_MACHINE_ALT2 EM_PPC_OLD
6071#endif
6072
6073#define elf_backend_plt_not_loaded 1
6074#define elf_backend_got_symbol_offset 4
6075#define elf_backend_can_gc_sections 1
51b64d56 6076#define elf_backend_can_refcount 1
252b5132 6077#define elf_backend_got_header_size 12
b491616a 6078#define elf_backend_rela_normal 1
252b5132 6079
43c40ab2 6080#define bfd_elf32_mkobject ppc_elf_mkobject
252b5132 6081#define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
70bccea4 6082#define bfd_elf32_bfd_relax_section ppc_elf_relax_section
252b5132
RH
6083#define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
6084#define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
70bccea4 6085#define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
252b5132 6086
feee612b 6087#define elf_backend_object_p ppc_elf_object_p
252b5132
RH
6088#define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
6089#define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
6090#define elf_backend_section_from_shdr ppc_elf_section_from_shdr
6091#define elf_backend_relocate_section ppc_elf_relocate_section
6092#define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
6093#define elf_backend_check_relocs ppc_elf_check_relocs
7fce784e 6094#define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
252b5132
RH
6095#define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
6096#define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
6097#define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
6098#define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
6099#define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
6100#define elf_backend_fake_sections ppc_elf_fake_sections
6101#define elf_backend_additional_program_headers ppc_elf_additional_program_headers
c5fccbec
DJ
6102#define elf_backend_grok_prstatus ppc_elf_grok_prstatus
6103#define elf_backend_grok_psinfo ppc_elf_grok_psinfo
29c2fb7c 6104#define elf_backend_reloc_type_class ppc_elf_reloc_type_class
70bccea4
AM
6105#define elf_backend_begin_write_processing ppc_elf_begin_write_processing
6106#define elf_backend_final_write_processing ppc_elf_final_write_processing
6107#define elf_backend_write_section ppc_elf_write_section
2f89ff8d 6108#define elf_backend_special_sections ppc_elf_special_sections
4c45e5c9 6109#define elf_backend_plt_sym_val ppc_elf_plt_sym_val
252b5132
RH
6110
6111#include "elf32-target.h"
This page took 0.964435 seconds and 4 git commands to generate.