]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-ppc.c
* elf32-ppc.c (is_ppc_elf_target): Move to linker part of file.
[thirdparty/binutils-gdb.git] / bfd / elf32-ppc.c
CommitLineData
252b5132 1/* PowerPC-specific support for 32-bit ELF
051d5130 2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
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,
21 Boston, MA 02111-1307, 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{
1509 /* pointer to the section */
1510 asection *section;
25dbc73a
AM
1511 /* pointer to the created symbol hash value */
1512 struct elf_link_hash_entry *sym_hash;
1513 /* offset of symbol from beginning of section */
1514 bfd_vma sym_offset;
1515} elf_linker_section_t;
1516
1517/* Linked list of allocated pointer entries. This hangs off of the
1518 symbol lists, and provides allows us to return different pointers,
1519 based on different addend's. */
1520
1521typedef struct elf_linker_section_pointers
1522{
1523 /* next allocated pointer for this symbol */
1524 struct elf_linker_section_pointers *next;
1525 /* offset of pointer from beginning of section */
1526 bfd_vma offset;
1527 /* addend used */
1528 bfd_vma addend;
1529 /* which linker section this is */
1530 elf_linker_section_t *lsect;
1531 /* whether address was written yet */
1532 bfd_boolean written_address_p;
1533} elf_linker_section_pointers_t;
1534
1535struct ppc_elf_obj_tdata
1536{
1537 struct elf_obj_tdata elf;
1538
1539 /* A mapping from local symbols to offsets into the various linker
1540 sections added. This is index by the symbol index. */
1541 elf_linker_section_pointers_t **linker_section_pointers;
1542};
1543
1544#define ppc_elf_tdata(bfd) \
1545 ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
7619e7c7 1546
25dbc73a
AM
1547#define elf_local_ptr_offsets(bfd) \
1548 (ppc_elf_tdata (bfd)->linker_section_pointers)
7619e7c7 1549
25dbc73a 1550/* Override the generic function because we store some extras. */
7619e7c7 1551
25dbc73a
AM
1552static bfd_boolean
1553ppc_elf_mkobject (bfd *abfd)
1554{
1555 bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
1556 abfd->tdata.any = bfd_zalloc (abfd, amt);
1557 if (abfd->tdata.any == NULL)
1558 return FALSE;
1559 return TRUE;
1560}
7619e7c7 1561
25dbc73a
AM
1562/* Fix bad default arch selected for a 32 bit input bfd when the
1563 default is 64 bit. */
7619e7c7 1564
25dbc73a
AM
1565static bfd_boolean
1566ppc_elf_object_p (bfd *abfd)
1567{
1568 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1569 {
1570 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
7619e7c7 1571
25dbc73a
AM
1572 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1573 {
1574 /* Relies on arch after 64 bit default being 32 bit default. */
1575 abfd->arch_info = abfd->arch_info->next;
1576 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1577 }
1578 }
1579 return TRUE;
1580}
7619e7c7 1581
25dbc73a 1582/* Function to set whether a module needs the -mrelocatable bit set. */
7619e7c7 1583
25dbc73a
AM
1584static bfd_boolean
1585ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1586{
1587 BFD_ASSERT (!elf_flags_init (abfd)
1588 || elf_elfheader (abfd)->e_flags == flags);
7619e7c7 1589
25dbc73a
AM
1590 elf_elfheader (abfd)->e_flags = flags;
1591 elf_flags_init (abfd) = TRUE;
1592 return TRUE;
1593}
1594
25dbc73a 1595/* Support for core dump NOTE sections. */
deaaf2f3 1596
b34976b6 1597static bfd_boolean
25dbc73a 1598ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 1599{
25dbc73a
AM
1600 int offset;
1601 unsigned int size;
252b5132 1602
25dbc73a
AM
1603 switch (note->descsz)
1604 {
1605 default:
1606 return FALSE;
252b5132 1607
25dbc73a
AM
1608 case 268: /* Linux/PPC. */
1609 /* pr_cursig */
1610 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
deaaf2f3 1611
25dbc73a
AM
1612 /* pr_pid */
1613 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
9abc968f 1614
25dbc73a
AM
1615 /* pr_reg */
1616 offset = 72;
1617 size = 192;
deaaf2f3 1618
25dbc73a
AM
1619 break;
1620 }
deaaf2f3 1621
25dbc73a
AM
1622 /* Make a ".reg/999" section. */
1623 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1624 size, note->descpos + offset);
1625}
252b5132 1626
25dbc73a
AM
1627static bfd_boolean
1628ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1629{
1630 switch (note->descsz)
deaaf2f3 1631 {
25dbc73a
AM
1632 default:
1633 return FALSE;
deaaf2f3 1634
25dbc73a
AM
1635 case 128: /* Linux/PPC elf_prpsinfo. */
1636 elf_tdata (abfd)->core_program
1637 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1638 elf_tdata (abfd)->core_command
1639 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1640 }
9abc968f 1641
25dbc73a
AM
1642 /* Note that for some reason, a spurious space is tacked
1643 onto the end of the args in some (at least one anyway)
1644 implementations, so strip it off if it exists. */
70bccea4 1645
25dbc73a
AM
1646 {
1647 char *command = elf_tdata (abfd)->core_command;
1648 int n = strlen (command);
70bccea4 1649
25dbc73a
AM
1650 if (0 < n && command[n - 1] == ' ')
1651 command[n - 1] = '\0';
1652 }
deaaf2f3 1653
25dbc73a
AM
1654 return TRUE;
1655}
deaaf2f3 1656
25dbc73a
AM
1657/* Return address for Ith PLT stub in section PLT, for relocation REL
1658 or (bfd_vma) -1 if it should not be included. */
deaaf2f3 1659
25dbc73a
AM
1660static bfd_vma
1661ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1662 const asection *plt ATTRIBUTE_UNUSED,
1663 const arelent *rel)
1664{
1665 return rel->address;
1666}
deaaf2f3 1667
25dbc73a 1668/* Handle a PowerPC specific section when reading an object file. This
6dc132d9
L
1669 is called when bfd_section_from_shdr finds a section with an unknown
1670 type. */
deaaf2f3 1671
25dbc73a 1672static bfd_boolean
6dc132d9
L
1673ppc_elf_section_from_shdr (bfd *abfd,
1674 Elf_Internal_Shdr *hdr,
1675 const char *name,
1676 int shindex)
25dbc73a
AM
1677{
1678 asection *newsect;
1679 flagword flags;
d1c6de6f 1680
6dc132d9 1681 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
25dbc73a 1682 return FALSE;
deaaf2f3 1683
25dbc73a
AM
1684 newsect = hdr->bfd_section;
1685 flags = bfd_get_section_flags (abfd, newsect);
1686 if (hdr->sh_flags & SHF_EXCLUDE)
1687 flags |= SEC_EXCLUDE;
9abc968f 1688
25dbc73a
AM
1689 if (hdr->sh_type == SHT_ORDERED)
1690 flags |= SEC_SORT_ENTRIES;
d1c6de6f 1691
25dbc73a
AM
1692 bfd_set_section_flags (abfd, newsect, flags);
1693 return TRUE;
1694}
1695
1696/* Set up any other section flags and such that may be necessary. */
1697
1698static bfd_boolean
1699ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1700 Elf_Internal_Shdr *shdr,
1701 asection *asect)
1702{
1703 if ((asect->flags & SEC_EXCLUDE) != 0)
1704 shdr->sh_flags |= SHF_EXCLUDE;
1705
1706 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1707 shdr->sh_type = SHT_ORDERED;
1708
1709 return TRUE;
1710}
1711
1712/* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
1713 need to bump up the number of section headers. */
1714
1715static int
1716ppc_elf_additional_program_headers (bfd *abfd)
1717{
1718 asection *s;
1719 int ret = 0;
d1c6de6f 1720
25dbc73a
AM
1721 s = bfd_get_section_by_name (abfd, ".sbss2");
1722 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1723 ++ret;
d1c6de6f 1724
25dbc73a
AM
1725 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1726 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1727 ++ret;
deaaf2f3 1728
25dbc73a
AM
1729 return ret;
1730}
deaaf2f3 1731
25dbc73a
AM
1732/* Add extra PPC sections -- Note, for now, make .sbss2 and
1733 .PPC.EMB.sbss0 a normal section, and not a bss section so
1734 that the linker doesn't crater when trying to make more than
1735 2 sections. */
e656e369 1736
25dbc73a
AM
1737static struct bfd_elf_special_section const ppc_elf_special_sections[]=
1738{
1739 { ".tags", 5, 0, SHT_ORDERED, SHF_ALLOC },
1740 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1741 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1742 { ".sdata2", 7, -2, SHT_PROGBITS, SHF_ALLOC },
1743 { ".sbss2", 6, -2, SHT_PROGBITS, SHF_ALLOC },
1744 { ".PPC.EMB.apuinfo", 16, 0, SHT_NOTE, 0 },
1745 { ".PPC.EMB.sdata0", 15, 0, SHT_PROGBITS, SHF_ALLOC },
1746 { ".PPC.EMB.sbss0", 14, 0, SHT_PROGBITS, SHF_ALLOC },
1747 { ".plt", 4, 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
1748 { NULL, 0, 0, 0, 0 }
1749};
1750\f
1751/* Very simple linked list structure for recording apuinfo values. */
1752typedef struct apuinfo_list
1753{
1754 struct apuinfo_list *next;
1755 unsigned long value;
1756}
1757apuinfo_list;
e656e369 1758
25dbc73a 1759static apuinfo_list *head;
deaaf2f3 1760
deaaf2f3 1761
25dbc73a
AM
1762static void
1763apuinfo_list_init (void)
1764{
1765 head = NULL;
1766}
9abc968f 1767
25dbc73a
AM
1768static void
1769apuinfo_list_add (unsigned long value)
1770{
1771 apuinfo_list *entry = head;
deaaf2f3 1772
25dbc73a
AM
1773 while (entry != NULL)
1774 {
1775 if (entry->value == value)
1776 return;
1777 entry = entry->next;
1778 }
b4a38de6 1779
25dbc73a
AM
1780 entry = bfd_malloc (sizeof (* entry));
1781 if (entry == NULL)
1782 return;
e656e369 1783
25dbc73a
AM
1784 entry->value = value;
1785 entry->next = head;
1786 head = entry;
1787}
9abc968f 1788
25dbc73a
AM
1789static unsigned
1790apuinfo_list_length (void)
1791{
1792 apuinfo_list *entry;
1793 unsigned long count;
9abc968f 1794
25dbc73a
AM
1795 for (entry = head, count = 0;
1796 entry;
1797 entry = entry->next)
1798 ++ count;
e656e369 1799
25dbc73a
AM
1800 return count;
1801}
eea6121a 1802
25dbc73a
AM
1803static inline unsigned long
1804apuinfo_list_element (unsigned long number)
1805{
1806 apuinfo_list * entry;
e656e369 1807
25dbc73a
AM
1808 for (entry = head;
1809 entry && number --;
1810 entry = entry->next)
1811 ;
252b5132 1812
25dbc73a
AM
1813 return entry ? entry->value : 0;
1814}
1815
1816static void
1817apuinfo_list_finish (void)
1818{
1819 apuinfo_list *entry;
1820
1821 for (entry = head; entry;)
252b5132 1822 {
25dbc73a
AM
1823 apuinfo_list *next = entry->next;
1824 free (entry);
1825 entry = next;
1826 }
9abc968f 1827
25dbc73a
AM
1828 head = NULL;
1829}
9abc968f 1830
25dbc73a
AM
1831#define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
1832#define APUINFO_LABEL "APUinfo"
9abc968f 1833
25dbc73a
AM
1834/* Scan the input BFDs and create a linked list of
1835 the APUinfo values that will need to be emitted. */
9abc968f 1836
25dbc73a
AM
1837static void
1838ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
1839{
1840 bfd *ibfd;
1841 asection *asec;
1842 char *buffer;
1843 unsigned num_input_sections;
1844 bfd_size_type output_section_size;
1845 unsigned i;
1846 unsigned num_entries;
1847 unsigned long offset;
1848 unsigned long length;
1849 const char *error_message = NULL;
9abc968f 1850
25dbc73a
AM
1851 if (link_info == NULL)
1852 return;
9abc968f 1853
25dbc73a
AM
1854 /* Scan the input bfds, looking for apuinfo sections. */
1855 num_input_sections = 0;
1856 output_section_size = 0;
252b5132 1857
25dbc73a 1858 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
252b5132 1859 {
25dbc73a
AM
1860 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1861 if (asec)
252b5132 1862 {
25dbc73a
AM
1863 ++ num_input_sections;
1864 output_section_size += asec->size;
252b5132 1865 }
252b5132
RH
1866 }
1867
25dbc73a
AM
1868 /* We need at least one input sections
1869 in order to make merging worthwhile. */
1870 if (num_input_sections < 1)
1871 return;
deaaf2f3 1872
25dbc73a
AM
1873 /* Just make sure that the output section exists as well. */
1874 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1875 if (asec == NULL)
1876 return;
252b5132 1877
25dbc73a
AM
1878 /* Allocate a buffer for the contents of the input sections. */
1879 buffer = bfd_malloc (output_section_size);
1880 if (buffer == NULL)
1881 return;
252b5132 1882
25dbc73a
AM
1883 offset = 0;
1884 apuinfo_list_init ();
252b5132 1885
25dbc73a
AM
1886 /* Read in the input sections contents. */
1887 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
252b5132 1888 {
25dbc73a
AM
1889 unsigned long datum;
1890 char *ptr;
252b5132 1891
25dbc73a
AM
1892 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1893 if (asec == NULL)
1894 continue;
252b5132 1895
25dbc73a
AM
1896 length = asec->size;
1897 if (length < 24)
1898 {
1899 error_message = _("corrupt or empty %s section in %B");
1900 goto fail;
1901 }
252b5132 1902
25dbc73a
AM
1903 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
1904 || (bfd_bread (buffer + offset, length, ibfd) != length))
1905 {
1906 error_message = _("unable to read in %s section from %B");
1907 goto fail;
1908 }
252b5132 1909
25dbc73a
AM
1910 /* Process the contents of the section. */
1911 ptr = buffer + offset;
1912 error_message = _("corrupt %s section in %B");
252b5132 1913
25dbc73a
AM
1914 /* Verify the contents of the header. Note - we have to
1915 extract the values this way in order to allow for a
1916 host whose endian-ness is different from the target. */
1917 datum = bfd_get_32 (ibfd, ptr);
1918 if (datum != sizeof APUINFO_LABEL)
1919 goto fail;
252b5132 1920
25dbc73a
AM
1921 datum = bfd_get_32 (ibfd, ptr + 8);
1922 if (datum != 0x2)
1923 goto fail;
252b5132 1924
25dbc73a
AM
1925 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
1926 goto fail;
252b5132 1927
25dbc73a
AM
1928 /* Get the number of bytes used for apuinfo entries. */
1929 datum = bfd_get_32 (ibfd, ptr + 4);
1930 if (datum + 20 != length)
1931 goto fail;
1932
1933 /* Make sure that we do not run off the end of the section. */
1934 if (offset + length > output_section_size)
1935 goto fail;
1936
1937 /* Scan the apuinfo section, building a list of apuinfo numbers. */
1938 for (i = 0; i < datum; i += 4)
1939 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
1940
1941 /* Update the offset. */
1942 offset += length;
252b5132
RH
1943 }
1944
25dbc73a 1945 error_message = NULL;
252b5132 1946
25dbc73a
AM
1947 /* Compute the size of the output section. */
1948 num_entries = apuinfo_list_length ();
1949 output_section_size = 20 + num_entries * 4;
252b5132 1950
25dbc73a
AM
1951 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1952
1953 if (! bfd_set_section_size (abfd, asec, output_section_size))
1954 ibfd = abfd,
1955 error_message = _("warning: unable to set size of %s section in %B");
1956
1957 fail:
1958 free (buffer);
1959
1960 if (error_message)
1961 (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
1962}
252b5132 1963
25dbc73a
AM
1964/* Prevent the output section from accumulating the input sections'
1965 contents. We have already stored this in our linked list structure. */
252b5132 1966
25dbc73a
AM
1967static bfd_boolean
1968ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
1969 asection *asec,
1970 bfd_byte *contents ATTRIBUTE_UNUSED)
1971{
1972 return (apuinfo_list_length ()
1973 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
252b5132
RH
1974}
1975
25dbc73a
AM
1976/* Finally we can generate the output section. */
1977
1978static void
1979ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
7619e7c7 1980{
25dbc73a
AM
1981 bfd_byte *buffer;
1982 asection *asec;
1983 unsigned i;
1984 unsigned num_entries;
1985 bfd_size_type length;
7619e7c7 1986
25dbc73a
AM
1987 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1988 if (asec == NULL)
1989 return;
1990
1991 if (apuinfo_list_length () == 0)
1992 return;
1993
1994 length = asec->size;
1995 if (length < 20)
1996 return;
1997
1998 buffer = bfd_malloc (length);
1999 if (buffer == NULL)
7619e7c7 2000 {
25dbc73a
AM
2001 (*_bfd_error_handler)
2002 (_("failed to allocate space for new APUinfo section."));
2003 return;
7619e7c7 2004 }
7619e7c7 2005
25dbc73a
AM
2006 /* Create the apuinfo header. */
2007 num_entries = apuinfo_list_length ();
2008 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2009 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2010 bfd_put_32 (abfd, 0x2, buffer + 8);
2011 strcpy ((char *) buffer + 12, APUINFO_LABEL);
feee612b 2012
25dbc73a
AM
2013 length = 20;
2014 for (i = 0; i < num_entries; i++)
feee612b 2015 {
25dbc73a
AM
2016 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2017 length += 4;
feee612b 2018 }
feee612b 2019
25dbc73a
AM
2020 if (length != asec->size)
2021 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
252b5132 2022
25dbc73a
AM
2023 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2024 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
252b5132 2025
25dbc73a
AM
2026 free (buffer);
2027
2028 apuinfo_list_finish ();
252b5132 2029}
25dbc73a
AM
2030\f
2031/* The following functions are specific to the ELF linker, while
2032 functions above are used generally. They appear in this file more
2033 or less in the order in which they are called. eg.
2034 ppc_elf_check_relocs is called early in the link process,
2035 ppc_elf_finish_dynamic_sections is one of the last functions
2036 called. */
252b5132 2037
25dbc73a
AM
2038/* The PPC linker needs to keep track of the number of relocs that it
2039 decides to copy as dynamic relocs in check_relocs for each symbol.
2040 This is so that it can later discard them if they are found to be
2041 unnecessary. We store the information in a field extending the
2042 regular ELF linker hash table. */
ae9a127f 2043
25dbc73a 2044struct ppc_elf_dyn_relocs
252b5132 2045{
25dbc73a 2046 struct ppc_elf_dyn_relocs *next;
252b5132 2047
25dbc73a
AM
2048 /* The input section of the reloc. */
2049 asection *sec;
252b5132 2050
25dbc73a
AM
2051 /* Total number of relocs copied for the input section. */
2052 bfd_size_type count;
252b5132 2053
25dbc73a
AM
2054 /* Number of pc-relative relocs copied for the input section. */
2055 bfd_size_type pc_count;
2056};
252b5132 2057
25dbc73a
AM
2058/* Of those relocs that might be copied as dynamic relocs, this macro
2059 selects those that must be copied when linking a shared library,
2060 even when the symbol is local. */
252b5132 2061
25dbc73a
AM
2062#define MUST_BE_DYN_RELOC(RTYPE) \
2063 ((RTYPE) != R_PPC_REL24 \
2064 && (RTYPE) != R_PPC_REL14 \
2065 && (RTYPE) != R_PPC_REL14_BRTAKEN \
2066 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
2067 && (RTYPE) != R_PPC_REL32)
252b5132 2068
25dbc73a
AM
2069/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2070 copying dynamic variables from a shared lib into an app's dynbss
2071 section, and instead use a dynamic relocation to point into the
2072 shared lib. */
2073#define ELIMINATE_COPY_RELOCS 1
252b5132 2074
25dbc73a 2075/* PPC ELF linker hash entry. */
252b5132 2076
25dbc73a
AM
2077struct ppc_elf_link_hash_entry
2078{
2079 struct elf_link_hash_entry elf;
252b5132 2080
25dbc73a
AM
2081 /* If this symbol is used in the linker created sections, the processor
2082 specific backend uses this field to map the field into the offset
2083 from the beginning of the section. */
2084 elf_linker_section_pointers_t *linker_section_pointer;
252b5132 2085
25dbc73a
AM
2086 /* Track dynamic relocs copied for this symbol. */
2087 struct ppc_elf_dyn_relocs *dyn_relocs;
252b5132 2088
25dbc73a
AM
2089 /* Contexts in which symbol is used in the GOT (or TOC).
2090 TLS_GD .. TLS_TLS bits are or'd into the mask as the
2091 corresponding relocs are encountered during check_relocs.
2092 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2093 indicate the corresponding GOT entry type is not needed. */
2094#define TLS_GD 1 /* GD reloc. */
2095#define TLS_LD 2 /* LD reloc. */
2096#define TLS_TPREL 4 /* TPREL reloc, => IE. */
2097#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2098#define TLS_TLS 16 /* Any TLS reloc. */
2099#define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
2100 char tls_mask;
2101};
252b5132 2102
25dbc73a
AM
2103#define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2104
2105/* PPC ELF linker hash table. */
2106
2107struct ppc_elf_link_hash_table
2108{
2109 struct elf_link_hash_table elf;
2110
2111 /* Short-cuts to get to dynamic linker sections. */
2112 asection *got;
2113 asection *relgot;
2114 asection *plt;
2115 asection *relplt;
2116 asection *dynbss;
2117 asection *relbss;
2118 asection *dynsbss;
2119 asection *relsbss;
2120 elf_linker_section_t *sdata;
2121 elf_linker_section_t *sdata2;
2122 asection *sbss;
2123
2124 /* Shortcut to .__tls_get_addr. */
2125 struct elf_link_hash_entry *tls_get_addr;
252b5132 2126
25dbc73a
AM
2127 /* TLS local dynamic got entry handling. */
2128 union {
2129 bfd_signed_vma refcount;
2130 bfd_vma offset;
2131 } tlsld_got;
252b5132 2132
25dbc73a
AM
2133 /* Small local sym to section mapping cache. */
2134 struct sym_sec_cache sym_sec;
2135};
252b5132 2136
25dbc73a 2137/* Get the PPC ELF linker hash table from a link_info structure. */
252b5132 2138
25dbc73a
AM
2139#define ppc_elf_hash_table(p) \
2140 ((struct ppc_elf_link_hash_table *) (p)->hash)
252b5132 2141
25dbc73a 2142/* Create an entry in a PPC ELF linker hash table. */
252b5132 2143
25dbc73a
AM
2144static struct bfd_hash_entry *
2145ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2146 struct bfd_hash_table *table,
2147 const char *string)
252b5132 2148{
25dbc73a
AM
2149 /* Allocate the structure if it has not already been allocated by a
2150 subclass. */
2151 if (entry == NULL)
2152 {
2153 entry = bfd_hash_allocate (table,
2154 sizeof (struct ppc_elf_link_hash_entry));
2155 if (entry == NULL)
2156 return entry;
2157 }
252b5132 2158
25dbc73a
AM
2159 /* Call the allocation method of the superclass. */
2160 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2161 if (entry != NULL)
2162 {
2163 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2164 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2165 ppc_elf_hash_entry (entry)->tls_mask = 0;
2166 }
252b5132 2167
25dbc73a 2168 return entry;
252b5132 2169}
3dab13f6 2170
25dbc73a 2171/* Create a PPC ELF linker hash table. */
3dab13f6 2172
25dbc73a
AM
2173static struct bfd_link_hash_table *
2174ppc_elf_link_hash_table_create (bfd *abfd)
3dab13f6 2175{
25dbc73a 2176 struct ppc_elf_link_hash_table *ret;
3dab13f6 2177
25dbc73a
AM
2178 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2179 if (ret == NULL)
2180 return NULL;
3dab13f6 2181
25dbc73a
AM
2182 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
2183 ppc_elf_link_hash_newfunc))
3dab13f6 2184 {
25dbc73a
AM
2185 free (ret);
2186 return NULL;
2187 }
58111eb7 2188
25dbc73a
AM
2189 return &ret->elf.root;
2190}
3dab13f6 2191
25dbc73a 2192/* The powerpc .got has a blrl instruction in it. Mark it executable. */
3dab13f6 2193
25dbc73a
AM
2194static bfd_boolean
2195ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2196{
2197 struct ppc_elf_link_hash_table *htab;
2198 asection *s;
2199 flagword flags;
3dab13f6 2200
25dbc73a
AM
2201 if (!_bfd_elf_create_got_section (abfd, info))
2202 return FALSE;
3dab13f6 2203
25dbc73a
AM
2204 htab = ppc_elf_hash_table (info);
2205 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2206 if (s == NULL)
2207 abort ();
3dab13f6 2208
25dbc73a
AM
2209 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2210 | SEC_LINKER_CREATED);
2211 if (!bfd_set_section_flags (abfd, s, flags))
2212 return FALSE;
3dab13f6 2213
25dbc73a
AM
2214 htab->relgot = bfd_make_section (abfd, ".rela.got");
2215 if (!htab->relgot
2216 || ! bfd_set_section_flags (abfd, htab->relgot,
2217 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2218 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2219 | SEC_READONLY))
2220 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2221 return FALSE;
3dab13f6 2222
25dbc73a
AM
2223 return TRUE;
2224}
3dab13f6 2225
25dbc73a
AM
2226/* We have to create .dynsbss and .rela.sbss here so that they get mapped
2227 to output sections (just like _bfd_elf_create_dynamic_sections has
2228 to create .dynbss and .rela.bss). */
3dab13f6 2229
25dbc73a
AM
2230static bfd_boolean
2231ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2232{
2233 struct ppc_elf_link_hash_table *htab;
2234 asection *s;
2235 flagword flags;
3dab13f6 2236
25dbc73a 2237 htab = ppc_elf_hash_table (info);
3dab13f6 2238
25dbc73a
AM
2239 if (htab->got == NULL
2240 && !ppc_elf_create_got (abfd, info))
3dab13f6
AM
2241 return FALSE;
2242
25dbc73a
AM
2243 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2244 return FALSE;
3dab13f6 2245
25dbc73a
AM
2246 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2247 | SEC_LINKER_CREATED);
3dab13f6 2248
25dbc73a
AM
2249 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2250 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2251 if (s == NULL
2252 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
2253 return FALSE;
3dab13f6 2254
25dbc73a
AM
2255 if (! info->shared)
2256 {
2257 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2258 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2259 if (s == NULL
2260 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2261 || ! bfd_set_section_alignment (abfd, s, 2))
2262 return FALSE;
2263 }
3dab13f6 2264
25dbc73a
AM
2265 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2266 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2267 if (s == NULL)
2268 abort ();
3dab13f6 2269
25dbc73a
AM
2270 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2271 return bfd_set_section_flags (abfd, s, flags);
2272}
3dab13f6 2273
25dbc73a 2274/* Copy the extra info we tack onto an elf_link_hash_entry. */
58111eb7 2275
25dbc73a
AM
2276static void
2277ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
2278 struct elf_link_hash_entry *dir,
2279 struct elf_link_hash_entry *ind)
2280{
2281 struct ppc_elf_link_hash_entry *edir, *eind;
3dab13f6 2282
25dbc73a
AM
2283 edir = (struct ppc_elf_link_hash_entry *) dir;
2284 eind = (struct ppc_elf_link_hash_entry *) ind;
3dab13f6 2285
25dbc73a
AM
2286 if (eind->dyn_relocs != NULL)
2287 {
2288 if (edir->dyn_relocs != NULL)
3dab13f6 2289 {
25dbc73a
AM
2290 struct ppc_elf_dyn_relocs **pp;
2291 struct ppc_elf_dyn_relocs *p;
3dab13f6 2292
25dbc73a
AM
2293 if (ind->root.type == bfd_link_hash_indirect)
2294 abort ();
2295
2296 /* Add reloc counts against the weak sym to the strong sym
2297 list. Merge any entries against the same section. */
2298 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3dab13f6 2299 {
25dbc73a
AM
2300 struct ppc_elf_dyn_relocs *q;
2301
2302 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2303 if (q->sec == p->sec)
2304 {
2305 q->pc_count += p->pc_count;
2306 q->count += p->count;
2307 *pp = p->next;
2308 break;
2309 }
2310 if (q == NULL)
2311 pp = &p->next;
3dab13f6 2312 }
25dbc73a 2313 *pp = edir->dyn_relocs;
3dab13f6 2314 }
25dbc73a
AM
2315
2316 edir->dyn_relocs = eind->dyn_relocs;
2317 eind->dyn_relocs = NULL;
3dab13f6 2318 }
3dab13f6 2319
25dbc73a 2320 edir->tls_mask |= eind->tls_mask;
3dab13f6 2321
25dbc73a
AM
2322 if (ELIMINATE_COPY_RELOCS
2323 && ind->root.type != bfd_link_hash_indirect
2324 && dir->dynamic_adjusted)
2325 {
2326 /* If called to transfer flags for a weakdef during processing
2327 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
2328 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
2329 dir->ref_dynamic |= ind->ref_dynamic;
2330 dir->ref_regular |= ind->ref_regular;
2331 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
2332 dir->needs_plt |= ind->needs_plt;
2333 }
2334 else
2335 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
2336}
3dab13f6 2337
8853c3d3
AM
2338/* Return 1 if target is one of ours. */
2339
2340static bfd_boolean
2341is_ppc_elf_target (const struct bfd_target *targ)
2342{
2343 extern const bfd_target bfd_elf32_powerpc_vec;
2344 extern const bfd_target bfd_elf32_powerpcle_vec;
2345
2346 return targ == &bfd_elf32_powerpc_vec || targ == &bfd_elf32_powerpcle_vec;
2347}
2348
25dbc73a
AM
2349/* Hook called by the linker routine which adds symbols from an object
2350 file. We use it to put .comm items in .sbss, and not .bss. */
58111eb7 2351
25dbc73a
AM
2352static bfd_boolean
2353ppc_elf_add_symbol_hook (bfd *abfd,
2354 struct bfd_link_info *info,
2355 Elf_Internal_Sym *sym,
2356 const char **namep ATTRIBUTE_UNUSED,
2357 flagword *flagsp ATTRIBUTE_UNUSED,
2358 asection **secp,
2359 bfd_vma *valp)
2360{
2361 if (sym->st_shndx == SHN_COMMON
2362 && !info->relocatable
2363 && sym->st_size <= elf_gp_size (abfd)
d2663f46 2364 && is_ppc_elf_target (info->hash->creator))
25dbc73a
AM
2365 {
2366 /* Common symbols less than or equal to -G nn bytes are automatically
2367 put into .sbss. */
2368 struct ppc_elf_link_hash_table *htab;
3dab13f6 2369
25dbc73a
AM
2370 htab = ppc_elf_hash_table (info);
2371 if (htab->sbss == NULL)
2372 {
2373 flagword flags = SEC_IS_COMMON;
3dab13f6 2374
25dbc73a
AM
2375 htab->sbss = bfd_make_section_anyway (abfd, ".sbss");
2376 if (htab->sbss == NULL
2377 || ! bfd_set_section_flags (abfd, htab->sbss, flags))
2378 return FALSE;
3dab13f6 2379 }
3dab13f6 2380
25dbc73a
AM
2381 *secp = htab->sbss;
2382 *valp = sym->st_size;
2383 }
3dab13f6 2384
25dbc73a 2385 return TRUE;
3dab13f6
AM
2386}
2387\f
25dbc73a
AM
2388/* Enumeration to specify the special section. */
2389enum elf_linker_section_enum
2390{
2391 LINKER_SECTION_SDATA,
2392 LINKER_SECTION_SDATA2
2393};
2394
252b5132
RH
2395/* Create a special linker section */
2396static elf_linker_section_t *
55fd94b0
AM
2397ppc_elf_create_linker_section (bfd *abfd,
2398 struct bfd_link_info *info,
2399 enum elf_linker_section_enum which)
252b5132 2400{
252b5132 2401 elf_linker_section_t *lsect;
58111eb7 2402 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
8528d62e 2403 asection *s, *sym_sec;
58111eb7
AM
2404 bfd_size_type amt;
2405 flagword flags;
2406 const char *name;
2407 const char *rel_name;
2408 const char *sym_name;
2409 bfd_vma sym_offset;
2410
cb2d1ef2
AM
2411 /* The linker creates these sections so it has somewhere to attach
2412 their respective symbols. Startup code (crt1.o) uses these symbols
2413 to initialize a register pointing to the section. If the output
2414 sections corresponding to these input sections were empty it would
2415 be OK to set the symbol to 0 (or any random number), because the
2416 associated register should never be used.
2417 FIXME: Setting a symbol this way is silly. The symbols ought to
2418 be set the same way other backends set gp. */
2419 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2420 | SEC_LINKER_CREATED);
58111eb7
AM
2421 sym_offset = 32768;
2422
2423 switch (which)
2424 {
2425 default:
2426 abort ();
2427 return NULL;
252b5132 2428
58111eb7
AM
2429 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
2430 name = ".sdata";
2431 rel_name = ".rela.sdata";
2432 sym_name = "_SDA_BASE_";
2433 break;
2434
2435 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2436 name = ".sdata2";
2437 rel_name = ".rela.sdata2";
2438 sym_name = "_SDA2_BASE_";
2439 flags |= SEC_READONLY;
2440 break;
2441 }
2442
2443 /* Record the first bfd that needs the special sections. */
2444 if (!htab->elf.dynobj)
2445 htab->elf.dynobj = abfd;
2446
2447 amt = sizeof (elf_linker_section_t);
2448 lsect = bfd_zalloc (htab->elf.dynobj, amt);
252b5132 2449
58111eb7
AM
2450 lsect->sym_offset = sym_offset;
2451
2452 /* See if the sections already exist. */
8528d62e 2453 sym_sec = s = bfd_get_section_by_name (htab->elf.dynobj, name);
58111eb7 2454 if (s == NULL || (s->flags & flags) != flags)
252b5132 2455 {
58111eb7
AM
2456 s = bfd_make_section_anyway (htab->elf.dynobj, name);
2457 if (s == NULL
2458 || !bfd_set_section_flags (htab->elf.dynobj, s, flags))
2459 return NULL;
8528d62e
DJ
2460 if (sym_sec == NULL)
2461 sym_sec = s;
58111eb7
AM
2462 }
2463 lsect->section = s;
252b5132 2464
58111eb7
AM
2465 if (bfd_get_section_alignment (htab->elf.dynobj, s) < 2
2466 && !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2467 return NULL;
252b5132 2468
eea6121a 2469 s->size = align_power (s->size, 2);
58111eb7
AM
2470
2471#ifdef DEBUG
2472 fprintf (stderr, "Creating section %s, current size = %ld\n",
eea6121a 2473 name, (long) s->size);
58111eb7
AM
2474#endif
2475
2476 if (sym_name)
2477 {
2478 struct elf_link_hash_entry *h;
2479 struct bfd_link_hash_entry *bh;
2480
2481#ifdef DEBUG
2482 fprintf (stderr, "Adding %s to section %s\n", sym_name, name);
2483#endif
2484 bh = bfd_link_hash_lookup (info->hash, sym_name,
2485 FALSE, FALSE, FALSE);
252b5132 2486
58111eb7
AM
2487 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
2488 && !(_bfd_generic_link_add_one_symbol
8528d62e 2489 (info, abfd, sym_name, BSF_GLOBAL, sym_sec, sym_offset, NULL,
58111eb7
AM
2490 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2491 return NULL;
2492 h = (struct elf_link_hash_entry *) bh;
2493
2494 h->type = STT_OBJECT;
2495 lsect->sym_hash = h;
2496
2497 if (info->shared
c152c796 2498 && ! bfd_elf_link_record_dynamic_symbol (info, h))
58111eb7
AM
2499 return NULL;
2500 }
2501
252b5132
RH
2502 return lsect;
2503}
252b5132 2504
25dbc73a
AM
2505/* Find a linker generated pointer with a given addend and type. */
2506
2507static elf_linker_section_pointers_t *
2508elf_find_pointer_linker_section
2509 (elf_linker_section_pointers_t *linker_pointers,
2510 bfd_vma addend,
2511 elf_linker_section_t *lsect)
252b5132 2512{
25dbc73a
AM
2513 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2514 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2515 return linker_pointers;
252b5132 2516
25dbc73a
AM
2517 return NULL;
2518}
2519
2520/* Allocate a pointer to live in a linker created section. */
2521
2522static bfd_boolean
2523elf_create_pointer_linker_section (bfd *abfd,
2524 struct bfd_link_info *info,
2525 elf_linker_section_t *lsect,
2526 struct elf_link_hash_entry *h,
2527 const Elf_Internal_Rela *rel)
2528{
2529 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2530 elf_linker_section_pointers_t *linker_section_ptr;
2531 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2532 bfd_size_type amt;
2533
2534 BFD_ASSERT (lsect != NULL);
2535
2536 /* Is this a global symbol? */
2537 if (h != NULL)
2538 {
2539 struct ppc_elf_link_hash_entry *eh;
2540
2541 /* Has this symbol already been allocated? If so, our work is done. */
2542 eh = (struct ppc_elf_link_hash_entry *) h;
2543 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2544 rel->r_addend,
2545 lsect))
2546 return TRUE;
2547
2548 ptr_linker_section_ptr = &eh->linker_section_pointer;
2549 /* Make sure this symbol is output as a dynamic symbol. */
2550 if (h->dynindx == -1)
2551 {
2552 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2553 return FALSE;
2554 }
25dbc73a
AM
2555 }
2556 else
2557 {
2558 /* Allocation of a pointer to a local symbol. */
2559 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2560
2561 /* Allocate a table to hold the local symbols if first time. */
2562 if (!ptr)
2563 {
2564 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2565
2566 amt = num_symbols;
2567 amt *= sizeof (elf_linker_section_pointers_t *);
2568 ptr = bfd_zalloc (abfd, amt);
2569
2570 if (!ptr)
2571 return FALSE;
2572
2573 elf_local_ptr_offsets (abfd) = ptr;
2574 }
2575
2576 /* Has this symbol already been allocated? If so, our work is done. */
2577 if (elf_find_pointer_linker_section (ptr[r_symndx],
2578 rel->r_addend,
2579 lsect))
2580 return TRUE;
252b5132 2581
25dbc73a 2582 ptr_linker_section_ptr = &ptr[r_symndx];
25dbc73a 2583 }
41fcb14e 2584
25dbc73a
AM
2585 /* Allocate space for a pointer in the linker section, and allocate
2586 a new pointer record from internal memory. */
2587 BFD_ASSERT (ptr_linker_section_ptr != NULL);
2588 amt = sizeof (elf_linker_section_pointers_t);
2589 linker_section_ptr = bfd_alloc (abfd, amt);
41fcb14e 2590
25dbc73a 2591 if (!linker_section_ptr)
7619e7c7 2592 return FALSE;
41fcb14e 2593
25dbc73a
AM
2594 linker_section_ptr->next = *ptr_linker_section_ptr;
2595 linker_section_ptr->addend = rel->r_addend;
2596 linker_section_ptr->lsect = lsect;
2597 linker_section_ptr->written_address_p = FALSE;
2598 *ptr_linker_section_ptr = linker_section_ptr;
41fcb14e 2599
25dbc73a
AM
2600 linker_section_ptr->offset = lsect->section->size;
2601 lsect->section->size += 4;
7619e7c7 2602
25dbc73a
AM
2603#ifdef DEBUG
2604 fprintf (stderr,
2605 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2606 lsect->name, (long) linker_section_ptr->offset,
2607 (long) lsect->section->size);
2608#endif
7619e7c7
AM
2609
2610 return TRUE;
41fcb14e
AM
2611}
2612
b34976b6 2613static bfd_boolean
25dbc73a
AM
2614update_local_sym_info (bfd *abfd,
2615 Elf_Internal_Shdr *symtab_hdr,
2616 unsigned long r_symndx,
2617 int tls_type)
252b5132 2618{
25dbc73a
AM
2619 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
2620 char *local_got_tls_masks;
252b5132 2621
25dbc73a 2622 if (local_got_refcounts == NULL)
252b5132 2623 {
25dbc73a
AM
2624 bfd_size_type size = symtab_hdr->sh_info;
2625
2626 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
2627 local_got_refcounts = bfd_zalloc (abfd, size);
2628 if (local_got_refcounts == NULL)
b34976b6 2629 return FALSE;
25dbc73a 2630 elf_local_got_refcounts (abfd) = local_got_refcounts;
252b5132 2631 }
41fcb14e 2632
25dbc73a
AM
2633 local_got_refcounts[r_symndx] += 1;
2634 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2635 local_got_tls_masks[r_symndx] |= tls_type;
2636 return TRUE;
2637}
41fcb14e 2638
25dbc73a
AM
2639static void
2640bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
2641{
2642 (*_bfd_error_handler)
2643 (_("%B: relocation %s cannot be used when making a shared object"),
2644 abfd,
2645 ppc_elf_howto_table[r_type]->name);
2646 bfd_set_error (bfd_error_bad_value);
252b5132
RH
2647}
2648
25dbc73a
AM
2649/* Look through the relocs for a section during the first phase, and
2650 allocate space in the global offset table or procedure linkage
2651 table. */
252b5132 2652
b34976b6 2653static bfd_boolean
25dbc73a
AM
2654ppc_elf_check_relocs (bfd *abfd,
2655 struct bfd_link_info *info,
2656 asection *sec,
2657 const Elf_Internal_Rela *relocs)
252b5132 2658{
7619e7c7 2659 struct ppc_elf_link_hash_table *htab;
25dbc73a
AM
2660 Elf_Internal_Shdr *symtab_hdr;
2661 struct elf_link_hash_entry **sym_hashes;
2662 const Elf_Internal_Rela *rel;
2663 const Elf_Internal_Rela *rel_end;
2664 asection *sreloc;
2665
2666 if (info->relocatable)
2667 return TRUE;
252b5132
RH
2668
2669#ifdef DEBUG
25dbc73a
AM
2670 _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
2671 sec, abfd);
252b5132
RH
2672#endif
2673
25dbc73a
AM
2674 /* Initialize howto table if not already done. */
2675 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2676 ppc_elf_howto_init ();
2677
2678 /* Create the linker generated sections all the time so that the
2679 special symbols are created. */
7619e7c7 2680 htab = ppc_elf_hash_table (info);
25dbc73a
AM
2681 if (htab->sdata == NULL)
2682 {
2683 htab->sdata = ppc_elf_create_linker_section (abfd, info,
2684 LINKER_SECTION_SDATA);
2685 if (htab->sdata == NULL)
2686 return FALSE;
2687 }
252b5132 2688
25dbc73a 2689 if (htab->sdata2 == NULL)
252b5132 2690 {
25dbc73a
AM
2691 htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
2692 LINKER_SECTION_SDATA2);
2693 if (htab->sdata2 == NULL)
2694 return FALSE;
2695 }
2696
2697 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2698 sym_hashes = elf_sym_hashes (abfd);
2699 sreloc = NULL;
2700
2701 rel_end = relocs + sec->reloc_count;
2702 for (rel = relocs; rel < rel_end; rel++)
2703 {
2704 unsigned long r_symndx;
2705 enum elf_ppc_reloc_type r_type;
2706 struct elf_link_hash_entry *h;
2707 int tls_type = 0;
2708
2709 r_symndx = ELF32_R_SYM (rel->r_info);
2710 if (r_symndx < symtab_hdr->sh_info)
2711 h = NULL;
2712 else
2713 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2714
2715 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2716 This shows up in particular in an R_PPC_ADDR32 in the eabi
2717 startup code. */
2718 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
252b5132 2719 {
25dbc73a
AM
2720 if (htab->got == NULL)
2721 {
2722 if (htab->elf.dynobj == NULL)
2723 htab->elf.dynobj = abfd;
2724 if (!ppc_elf_create_got (htab->elf.dynobj, info))
2725 return FALSE;
2726 }
2727 }
252b5132 2728
25dbc73a
AM
2729 r_type = ELF32_R_TYPE (rel->r_info);
2730 switch (r_type)
2731 {
2732 case R_PPC_GOT_TLSLD16:
2733 case R_PPC_GOT_TLSLD16_LO:
2734 case R_PPC_GOT_TLSLD16_HI:
2735 case R_PPC_GOT_TLSLD16_HA:
2736 htab->tlsld_got.refcount += 1;
2737 tls_type = TLS_TLS | TLS_LD;
2738 goto dogottls;
252b5132 2739
25dbc73a
AM
2740 case R_PPC_GOT_TLSGD16:
2741 case R_PPC_GOT_TLSGD16_LO:
2742 case R_PPC_GOT_TLSGD16_HI:
2743 case R_PPC_GOT_TLSGD16_HA:
2744 tls_type = TLS_TLS | TLS_GD;
2745 goto dogottls;
2746
2747 case R_PPC_GOT_TPREL16:
2748 case R_PPC_GOT_TPREL16_LO:
2749 case R_PPC_GOT_TPREL16_HI:
2750 case R_PPC_GOT_TPREL16_HA:
2751 if (info->shared)
2752 info->flags |= DF_STATIC_TLS;
2753 tls_type = TLS_TLS | TLS_TPREL;
2754 goto dogottls;
2755
2756 case R_PPC_GOT_DTPREL16:
2757 case R_PPC_GOT_DTPREL16_LO:
2758 case R_PPC_GOT_DTPREL16_HI:
2759 case R_PPC_GOT_DTPREL16_HA:
2760 tls_type = TLS_TLS | TLS_DTPREL;
2761 dogottls:
2762 sec->has_tls_reloc = 1;
2763 /* Fall thru */
252b5132 2764
25dbc73a
AM
2765 /* GOT16 relocations */
2766 case R_PPC_GOT16:
2767 case R_PPC_GOT16_LO:
2768 case R_PPC_GOT16_HI:
2769 case R_PPC_GOT16_HA:
2770 /* This symbol requires a global offset table entry. */
2771 if (htab->got == NULL)
2772 {
2773 if (htab->elf.dynobj == NULL)
2774 htab->elf.dynobj = abfd;
2775 if (!ppc_elf_create_got (htab->elf.dynobj, info))
2776 return FALSE;
2777 }
2778 if (h != NULL)
2779 {
2780 h->got.refcount += 1;
2781 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
2782 }
2783 else
2784 /* This is a global offset table entry for a local symbol. */
2785 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
2786 return FALSE;
2787 break;
252b5132 2788
25dbc73a
AM
2789 /* Indirect .sdata relocation. */
2790 case R_PPC_EMB_SDAI16:
2791 if (info->shared)
2792 {
2793 bad_shared_reloc (abfd, r_type);
2794 return FALSE;
2795 }
2796 if (!elf_create_pointer_linker_section (abfd, info,
2797 htab->sdata, h, rel))
2798 return FALSE;
2799 break;
252b5132 2800
25dbc73a
AM
2801 /* Indirect .sdata2 relocation. */
2802 case R_PPC_EMB_SDA2I16:
2803 if (info->shared)
2804 {
2805 bad_shared_reloc (abfd, r_type);
2806 return FALSE;
2807 }
2808 if (!elf_create_pointer_linker_section (abfd, info,
2809 htab->sdata2, h, rel))
2810 return FALSE;
2811 break;
252b5132 2812
25dbc73a
AM
2813 case R_PPC_SDAREL16:
2814 case R_PPC_EMB_SDA2REL:
2815 case R_PPC_EMB_SDA21:
2816 case R_PPC_EMB_RELSDA:
2817 case R_PPC_EMB_NADDR32:
2818 case R_PPC_EMB_NADDR16:
2819 case R_PPC_EMB_NADDR16_LO:
2820 case R_PPC_EMB_NADDR16_HI:
2821 case R_PPC_EMB_NADDR16_HA:
2822 if (info->shared)
2823 {
2824 bad_shared_reloc (abfd, r_type);
2825 return FALSE;
2826 }
2827 break;
252b5132 2828
25dbc73a
AM
2829 case R_PPC_PLT32:
2830 case R_PPC_PLTREL24:
2831 case R_PPC_PLTREL32:
2832 case R_PPC_PLT16_LO:
2833 case R_PPC_PLT16_HI:
2834 case R_PPC_PLT16_HA:
2835#ifdef DEBUG
2836 fprintf (stderr, "Reloc requires a PLT entry\n");
2837#endif
2838 /* This symbol requires a procedure linkage table entry. We
2839 actually build the entry in finish_dynamic_symbol,
2840 because this might be a case of linking PIC code without
2841 linking in any dynamic objects, in which case we don't
2842 need to generate a procedure linkage table after all. */
7619e7c7 2843
25dbc73a
AM
2844 if (h == NULL)
2845 {
2846 /* It does not make sense to have a procedure linkage
2847 table entry for a local symbol. */
2848 (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
2849 "local symbol"),
2850 abfd,
2851 sec,
2852 (long) rel->r_offset,
2853 ppc_elf_howto_table[r_type]->name);
2854 bfd_set_error (bfd_error_bad_value);
2855 return FALSE;
2856 }
ee05f2fe 2857
25dbc73a
AM
2858 h->needs_plt = 1;
2859 h->plt.refcount++;
2860 break;
ee05f2fe 2861
25dbc73a
AM
2862 /* The following relocations don't need to propagate the
2863 relocation if linking a shared object since they are
2864 section relative. */
2865 case R_PPC_SECTOFF:
2866 case R_PPC_SECTOFF_LO:
2867 case R_PPC_SECTOFF_HI:
2868 case R_PPC_SECTOFF_HA:
2869 case R_PPC_DTPREL16:
2870 case R_PPC_DTPREL16_LO:
2871 case R_PPC_DTPREL16_HI:
2872 case R_PPC_DTPREL16_HA:
2873 case R_PPC_TOC16:
2874 break;
252b5132 2875
25dbc73a
AM
2876 /* This are just markers. */
2877 case R_PPC_TLS:
2878 case R_PPC_EMB_MRKREF:
2879 case R_PPC_NONE:
2880 case R_PPC_max:
2881 break;
252b5132 2882
25dbc73a
AM
2883 /* These should only appear in dynamic objects. */
2884 case R_PPC_COPY:
2885 case R_PPC_GLOB_DAT:
2886 case R_PPC_JMP_SLOT:
2887 case R_PPC_RELATIVE:
2888 break;
252b5132 2889
25dbc73a
AM
2890 /* These aren't handled yet. We'll report an error later. */
2891 case R_PPC_ADDR30:
2892 case R_PPC_EMB_RELSEC16:
2893 case R_PPC_EMB_RELST_LO:
2894 case R_PPC_EMB_RELST_HI:
2895 case R_PPC_EMB_RELST_HA:
2896 case R_PPC_EMB_BIT_FLD:
2897 break;
252b5132 2898
25dbc73a
AM
2899 /* This refers only to functions defined in the shared library. */
2900 case R_PPC_LOCAL24PC:
2901 break;
252b5132 2902
25dbc73a
AM
2903 /* This relocation describes the C++ object vtable hierarchy.
2904 Reconstruct it for later use during GC. */
2905 case R_PPC_GNU_VTINHERIT:
2906 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2907 return FALSE;
2908 break;
252b5132 2909
25dbc73a
AM
2910 /* This relocation describes which C++ vtable entries are actually
2911 used. Record for later use during GC. */
2912 case R_PPC_GNU_VTENTRY:
2913 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2914 return FALSE;
2915 break;
252b5132 2916
25dbc73a
AM
2917 /* We shouldn't really be seeing these. */
2918 case R_PPC_TPREL32:
2919 if (info->shared)
2920 info->flags |= DF_STATIC_TLS;
2921 goto dodyn;
252b5132 2922
25dbc73a
AM
2923 /* Nor these. */
2924 case R_PPC_DTPMOD32:
2925 case R_PPC_DTPREL32:
2926 goto dodyn;
252b5132 2927
25dbc73a
AM
2928 case R_PPC_TPREL16:
2929 case R_PPC_TPREL16_LO:
2930 case R_PPC_TPREL16_HI:
2931 case R_PPC_TPREL16_HA:
2932 if (info->shared)
2933 info->flags |= DF_STATIC_TLS;
2934 goto dodyn;
ee05f2fe 2935
25dbc73a
AM
2936 /* When creating a shared object, we must copy these
2937 relocs into the output file. We create a reloc
2938 section in dynobj and make room for the reloc. */
2939 case R_PPC_REL24:
2940 case R_PPC_REL14:
2941 case R_PPC_REL14_BRTAKEN:
2942 case R_PPC_REL14_BRNTAKEN:
2943 case R_PPC_REL32:
2944 if (h == NULL
2945 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2946 break;
2947 /* fall through */
ee05f2fe 2948
25dbc73a
AM
2949 case R_PPC_ADDR32:
2950 case R_PPC_ADDR24:
2951 case R_PPC_ADDR16:
2952 case R_PPC_ADDR16_LO:
2953 case R_PPC_ADDR16_HI:
2954 case R_PPC_ADDR16_HA:
2955 case R_PPC_ADDR14:
2956 case R_PPC_ADDR14_BRTAKEN:
2957 case R_PPC_ADDR14_BRNTAKEN:
2958 case R_PPC_UADDR32:
2959 case R_PPC_UADDR16:
2960 if (h != NULL && !info->shared)
2961 {
2962 /* We may need a plt entry if the symbol turns out to be
2963 a function defined in a dynamic object. */
2964 h->plt.refcount++;
7fce784e 2965
25dbc73a
AM
2966 /* We may need a copy reloc too. */
2967 h->non_got_ref = 1;
2968 }
7619e7c7 2969
25dbc73a
AM
2970 dodyn:
2971 /* If we are creating a shared library, and this is a reloc
2972 against a global symbol, or a non PC relative reloc
2973 against a local symbol, then we need to copy the reloc
2974 into the shared library. However, if we are linking with
2975 -Bsymbolic, we do not need to copy a reloc against a
2976 global symbol which is defined in an object we are
2977 including in the link (i.e., DEF_REGULAR is set). At
2978 this point we have not seen all the input files, so it is
2979 possible that DEF_REGULAR is not set now but will be set
2980 later (it is never cleared). In case of a weak definition,
2981 DEF_REGULAR may be cleared later by a strong definition in
2982 a shared library. We account for that possibility below by
2983 storing information in the dyn_relocs field of the hash
2984 table entry. A similar situation occurs when creating
2985 shared libraries and symbol visibility changes render the
2986 symbol local.
7619e7c7 2987
25dbc73a
AM
2988 If on the other hand, we are creating an executable, we
2989 may need to keep relocations for symbols satisfied by a
2990 dynamic library if we manage to avoid copy relocs for the
2991 symbol. */
2992 if ((info->shared
2993 && (MUST_BE_DYN_RELOC (r_type)
2994 || (h != NULL
2995 && (! info->symbolic
2996 || h->root.type == bfd_link_hash_defweak
2997 || !h->def_regular))))
2998 || (ELIMINATE_COPY_RELOCS
2999 && !info->shared
3000 && (sec->flags & SEC_ALLOC) != 0
3001 && h != NULL
3002 && (h->root.type == bfd_link_hash_defweak
3003 || !h->def_regular)))
3004 {
3005 struct ppc_elf_dyn_relocs *p;
3006 struct ppc_elf_dyn_relocs **head;
7619e7c7 3007
25dbc73a
AM
3008#ifdef DEBUG
3009 fprintf (stderr,
3010 "ppc_elf_check_relocs needs to "
3011 "create relocation for %s\n",
3012 (h && h->root.root.string
3013 ? h->root.root.string : "<unknown>"));
3014#endif
3015 if (sreloc == NULL)
3016 {
3017 const char *name;
7619e7c7 3018
25dbc73a
AM
3019 name = (bfd_elf_string_from_elf_section
3020 (abfd,
3021 elf_elfheader (abfd)->e_shstrndx,
3022 elf_section_data (sec)->rel_hdr.sh_name));
3023 if (name == NULL)
3024 return FALSE;
7619e7c7 3025
25dbc73a
AM
3026 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3027 && strcmp (bfd_get_section_name (abfd, sec),
3028 name + 5) == 0);
7619e7c7 3029
25dbc73a
AM
3030 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3031 if (sreloc == NULL)
3032 {
3033 flagword flags;
7619e7c7 3034
25dbc73a
AM
3035 sreloc = bfd_make_section (htab->elf.dynobj, name);
3036 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3037 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3038 if ((sec->flags & SEC_ALLOC) != 0)
3039 flags |= SEC_ALLOC | SEC_LOAD;
3040 if (sreloc == NULL
3041 || ! bfd_set_section_flags (htab->elf.dynobj,
3042 sreloc, flags)
3043 || ! bfd_set_section_alignment (htab->elf.dynobj,
3044 sreloc, 2))
3045 return FALSE;
3046 }
3047 elf_section_data (sec)->sreloc = sreloc;
3048 }
7619e7c7 3049
25dbc73a
AM
3050 /* If this is a global symbol, we count the number of
3051 relocations we need for this symbol. */
3052 if (h != NULL)
3053 {
3054 head = &ppc_elf_hash_entry (h)->dyn_relocs;
3055 }
3056 else
3057 {
3058 /* Track dynamic relocs needed for local syms too.
3059 We really need local syms available to do this
3060 easily. Oh well. */
7619e7c7 3061
25dbc73a
AM
3062 asection *s;
3063 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3064 sec, r_symndx);
3065 if (s == NULL)
3066 return FALSE;
7fce784e 3067
25dbc73a
AM
3068 head = ((struct ppc_elf_dyn_relocs **)
3069 &elf_section_data (s)->local_dynrel);
3070 }
7fce784e 3071
25dbc73a
AM
3072 p = *head;
3073 if (p == NULL || p->sec != sec)
3074 {
3075 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3076 if (p == NULL)
3077 return FALSE;
3078 p->next = *head;
3079 *head = p;
3080 p->sec = sec;
3081 p->count = 0;
3082 p->pc_count = 0;
3083 }
3084
3085 p->count += 1;
3086 if (!MUST_BE_DYN_RELOC (r_type))
3087 p->pc_count += 1;
7619e7c7 3088 }
25dbc73a
AM
3089
3090 break;
7619e7c7
AM
3091 }
3092 }
ee05f2fe 3093
25dbc73a
AM
3094 return TRUE;
3095}
3096\f
8853c3d3
AM
3097/* Merge backend specific data from an object file to the output
3098 object file when linking. */
3099
3100static bfd_boolean
3101ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3102{
3103 flagword old_flags;
3104 flagword new_flags;
3105 bfd_boolean error;
3106
3107 if (!is_ppc_elf_target (ibfd->xvec)
3108 || !is_ppc_elf_target (obfd->xvec))
3109 return TRUE;
3110
3111 /* Check if we have the same endianess. */
3112 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3113 return FALSE;
3114
3115 new_flags = elf_elfheader (ibfd)->e_flags;
3116 old_flags = elf_elfheader (obfd)->e_flags;
3117 if (!elf_flags_init (obfd))
3118 {
3119 /* First call, no flags set. */
3120 elf_flags_init (obfd) = TRUE;
3121 elf_elfheader (obfd)->e_flags = new_flags;
3122 }
3123
3124 /* Compatible flags are ok. */
3125 else if (new_flags == old_flags)
3126 ;
3127
3128 /* Incompatible flags. */
3129 else
3130 {
3131 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
3132 to be linked with either. */
3133 error = FALSE;
3134 if ((new_flags & EF_PPC_RELOCATABLE) != 0
3135 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
3136 {
3137 error = TRUE;
3138 (*_bfd_error_handler)
3139 (_("%B: compiled with -mrelocatable and linked with "
3140 "modules compiled normally"), ibfd);
3141 }
3142 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
3143 && (old_flags & EF_PPC_RELOCATABLE) != 0)
3144 {
3145 error = TRUE;
3146 (*_bfd_error_handler)
3147 (_("%B: compiled normally and linked with "
3148 "modules compiled with -mrelocatable"), ibfd);
3149 }
3150
3151 /* The output is -mrelocatable-lib iff both the input files are. */
3152 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
3153 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
3154
3155 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
3156 but each input file is either -mrelocatable or -mrelocatable-lib. */
3157 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
3158 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
3159 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
3160 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
3161
3162 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
3163 any module uses it. */
3164 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
3165
3166 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3167 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3168
3169 /* Warn about any other mismatches. */
3170 if (new_flags != old_flags)
3171 {
3172 error = TRUE;
3173 (*_bfd_error_handler)
3174 (_("%B: uses different e_flags (0x%lx) fields "
3175 "than previous modules (0x%lx)"),
3176 ibfd, (long) new_flags, (long) old_flags);
3177 }
3178
3179 if (error)
3180 {
3181 bfd_set_error (bfd_error_bad_value);
3182 return FALSE;
3183 }
3184 }
3185
3186 return TRUE;
3187}
3188\f
25dbc73a
AM
3189/* Return the section that should be marked against GC for a given
3190 relocation. */
586119b3 3191
25dbc73a
AM
3192static asection *
3193ppc_elf_gc_mark_hook (asection *sec,
3194 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3195 Elf_Internal_Rela *rel,
3196 struct elf_link_hash_entry *h,
3197 Elf_Internal_Sym *sym)
3198{
3199 if (h != NULL)
ee05f2fe 3200 {
25dbc73a 3201 switch (ELF32_R_TYPE (rel->r_info))
ee05f2fe 3202 {
25dbc73a
AM
3203 case R_PPC_GNU_VTINHERIT:
3204 case R_PPC_GNU_VTENTRY:
3205 break;
ee05f2fe 3206
25dbc73a
AM
3207 default:
3208 switch (h->root.type)
ee05f2fe 3209 {
25dbc73a
AM
3210 case bfd_link_hash_defined:
3211 case bfd_link_hash_defweak:
3212 return h->root.u.def.section;
3213
3214 case bfd_link_hash_common:
3215 return h->root.u.c.p->section;
3216
3217 default:
3218 break;
ee05f2fe
AM
3219 }
3220 }
25dbc73a
AM
3221 }
3222 else
3223 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4e795f50 3224
25dbc73a
AM
3225 return NULL;
3226}
a876ecf9 3227
25dbc73a
AM
3228/* Update the got, plt and dynamic reloc reference counts for the
3229 section being removed. */
3230
3231static bfd_boolean
3232ppc_elf_gc_sweep_hook (bfd *abfd,
3233 struct bfd_link_info *info,
3234 asection *sec,
3235 const Elf_Internal_Rela *relocs)
3236{
3237 struct ppc_elf_link_hash_table *htab;
3238 Elf_Internal_Shdr *symtab_hdr;
3239 struct elf_link_hash_entry **sym_hashes;
3240 bfd_signed_vma *local_got_refcounts;
3241 const Elf_Internal_Rela *rel, *relend;
3242
3243 elf_section_data (sec)->local_dynrel = NULL;
3244
3245 htab = ppc_elf_hash_table (info);
3246 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3247 sym_hashes = elf_sym_hashes (abfd);
3248 local_got_refcounts = elf_local_got_refcounts (abfd);
3249
3250 relend = relocs + sec->reloc_count;
3251 for (rel = relocs; rel < relend; rel++)
3252 {
3253 unsigned long r_symndx;
3254 enum elf_ppc_reloc_type r_type;
3255 struct elf_link_hash_entry *h = NULL;
3256
3257 r_symndx = ELF32_R_SYM (rel->r_info);
3258 if (r_symndx >= symtab_hdr->sh_info)
a876ecf9 3259 {
25dbc73a
AM
3260 struct ppc_elf_dyn_relocs **pp, *p;
3261 struct ppc_elf_link_hash_entry *eh;
3262
3263 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3264 while (h->root.type == bfd_link_hash_indirect
3265 || h->root.type == bfd_link_hash_warning)
3266 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3267 eh = (struct ppc_elf_link_hash_entry *) h;
3268
3269 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3270 if (p->sec == sec)
3271 {
3272 /* Everything must go for SEC. */
3273 *pp = p->next;
3274 break;
3275 }
a876ecf9 3276 }
ee05f2fe 3277
25dbc73a
AM
3278 r_type = ELF32_R_TYPE (rel->r_info);
3279 switch (r_type)
ee05f2fe 3280 {
25dbc73a
AM
3281 case R_PPC_GOT_TLSLD16:
3282 case R_PPC_GOT_TLSLD16_LO:
3283 case R_PPC_GOT_TLSLD16_HI:
3284 case R_PPC_GOT_TLSLD16_HA:
3285 htab->tlsld_got.refcount -= 1;
3286 /* Fall thru */
3287
3288 case R_PPC_GOT_TLSGD16:
3289 case R_PPC_GOT_TLSGD16_LO:
3290 case R_PPC_GOT_TLSGD16_HI:
3291 case R_PPC_GOT_TLSGD16_HA:
3292 case R_PPC_GOT_TPREL16:
3293 case R_PPC_GOT_TPREL16_LO:
3294 case R_PPC_GOT_TPREL16_HI:
3295 case R_PPC_GOT_TPREL16_HA:
3296 case R_PPC_GOT_DTPREL16:
3297 case R_PPC_GOT_DTPREL16_LO:
3298 case R_PPC_GOT_DTPREL16_HI:
3299 case R_PPC_GOT_DTPREL16_HA:
3300 case R_PPC_GOT16:
3301 case R_PPC_GOT16_LO:
3302 case R_PPC_GOT16_HI:
3303 case R_PPC_GOT16_HA:
3304 if (h != NULL)
ee05f2fe 3305 {
25dbc73a
AM
3306 if (h->got.refcount > 0)
3307 h->got.refcount--;
ee05f2fe 3308 }
25dbc73a
AM
3309 else if (local_got_refcounts != NULL)
3310 {
3311 if (local_got_refcounts[r_symndx] > 0)
3312 local_got_refcounts[r_symndx]--;
3313 }
3314 break;
ee05f2fe 3315
25dbc73a
AM
3316 case R_PPC_REL24:
3317 case R_PPC_REL14:
3318 case R_PPC_REL14_BRTAKEN:
3319 case R_PPC_REL14_BRNTAKEN:
3320 case R_PPC_REL32:
3321 if (h == NULL
3322 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3323 break;
3324 /* Fall thru */
ee05f2fe 3325
25dbc73a
AM
3326 case R_PPC_ADDR32:
3327 case R_PPC_ADDR24:
3328 case R_PPC_ADDR16:
3329 case R_PPC_ADDR16_LO:
3330 case R_PPC_ADDR16_HI:
3331 case R_PPC_ADDR16_HA:
3332 case R_PPC_ADDR14:
3333 case R_PPC_ADDR14_BRTAKEN:
3334 case R_PPC_ADDR14_BRNTAKEN:
3335 case R_PPC_UADDR32:
3336 case R_PPC_UADDR16:
3337 case R_PPC_PLT32:
3338 case R_PPC_PLTREL24:
3339 case R_PPC_PLT16_LO:
3340 case R_PPC_PLT16_HI:
3341 case R_PPC_PLT16_HA:
3342 if (h != NULL)
3343 {
3344 if (h->plt.refcount > 0)
3345 h->plt.refcount--;
3346 }
3347 break;
ee05f2fe 3348
25dbc73a
AM
3349 default:
3350 break;
3351 }
7fce784e 3352 }
7fce784e
AS
3353 return TRUE;
3354}
25dbc73a
AM
3355\f
3356/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7fce784e 3357
25dbc73a
AM
3358asection *
3359ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
7fce784e 3360{
25dbc73a 3361 struct ppc_elf_link_hash_table *htab;
7fce784e 3362
25dbc73a
AM
3363 htab = ppc_elf_hash_table (info);
3364 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3365 FALSE, FALSE, TRUE);
7fce784e 3366
25dbc73a 3367 return _bfd_elf_tls_setup (obfd, info);
7fce784e
AS
3368}
3369
25dbc73a
AM
3370/* Run through all the TLS relocs looking for optimization
3371 opportunities. */
252b5132 3372
25dbc73a
AM
3373bfd_boolean
3374ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
3375 struct bfd_link_info *info)
252b5132 3376{
7fce784e 3377 bfd *ibfd;
25dbc73a
AM
3378 asection *sec;
3379 struct ppc_elf_link_hash_table *htab;
252b5132 3380
25dbc73a
AM
3381 if (info->relocatable || info->shared)
3382 return TRUE;
252b5132 3383
7619e7c7 3384 htab = ppc_elf_hash_table (info);
25dbc73a 3385 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
252b5132 3386 {
25dbc73a
AM
3387 Elf_Internal_Sym *locsyms = NULL;
3388 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7619e7c7 3389
25dbc73a
AM
3390 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3391 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3392 {
3393 Elf_Internal_Rela *relstart, *rel, *relend;
3394 int expecting_tls_get_addr;
252b5132 3395
25dbc73a
AM
3396 /* Read the relocations. */
3397 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
3398 info->keep_memory);
3399 if (relstart == NULL)
3400 return FALSE;
7619e7c7 3401
25dbc73a
AM
3402 expecting_tls_get_addr = 0;
3403 relend = relstart + sec->reloc_count;
3404 for (rel = relstart; rel < relend; rel++)
3405 {
3406 enum elf_ppc_reloc_type r_type;
3407 unsigned long r_symndx;
3408 struct elf_link_hash_entry *h = NULL;
3409 char *tls_mask;
3410 char tls_set, tls_clear;
3411 bfd_boolean is_local;
7fce784e 3412
25dbc73a
AM
3413 r_symndx = ELF32_R_SYM (rel->r_info);
3414 if (r_symndx >= symtab_hdr->sh_info)
3415 {
3416 struct elf_link_hash_entry **sym_hashes;
7fce784e 3417
25dbc73a
AM
3418 sym_hashes = elf_sym_hashes (ibfd);
3419 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3420 while (h->root.type == bfd_link_hash_indirect
3421 || h->root.type == bfd_link_hash_warning)
3422 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3423 }
7619e7c7 3424
25dbc73a
AM
3425 is_local = FALSE;
3426 if (h == NULL
3427 || !h->def_dynamic)
3428 is_local = TRUE;
7619e7c7 3429
25dbc73a
AM
3430 r_type = ELF32_R_TYPE (rel->r_info);
3431 switch (r_type)
7619e7c7 3432 {
25dbc73a
AM
3433 case R_PPC_GOT_TLSLD16:
3434 case R_PPC_GOT_TLSLD16_LO:
3435 case R_PPC_GOT_TLSLD16_HI:
3436 case R_PPC_GOT_TLSLD16_HA:
3437 /* These relocs should never be against a symbol
3438 defined in a shared lib. Leave them alone if
3439 that turns out to be the case. */
3440 expecting_tls_get_addr = 0;
3441 htab->tlsld_got.refcount -= 1;
3442 if (!is_local)
3443 continue;
3444
3445 /* LD -> LE */
3446 tls_set = 0;
3447 tls_clear = TLS_LD;
3448 expecting_tls_get_addr = 1;
3449 break;
3450
3451 case R_PPC_GOT_TLSGD16:
3452 case R_PPC_GOT_TLSGD16_LO:
3453 case R_PPC_GOT_TLSGD16_HI:
3454 case R_PPC_GOT_TLSGD16_HA:
3455 if (is_local)
3456 /* GD -> LE */
3457 tls_set = 0;
3458 else
3459 /* GD -> IE */
3460 tls_set = TLS_TLS | TLS_TPRELGD;
3461 tls_clear = TLS_GD;
3462 expecting_tls_get_addr = 1;
3463 break;
3464
3465 case R_PPC_GOT_TPREL16:
3466 case R_PPC_GOT_TPREL16_LO:
3467 case R_PPC_GOT_TPREL16_HI:
3468 case R_PPC_GOT_TPREL16_HA:
3469 expecting_tls_get_addr = 0;
3470 if (is_local)
3471 {
3472 /* IE -> LE */
3473 tls_set = 0;
3474 tls_clear = TLS_TPREL;
3475 break;
3476 }
3477 else
3478 continue;
3479
3480 case R_PPC_REL14:
3481 case R_PPC_REL14_BRTAKEN:
3482 case R_PPC_REL14_BRNTAKEN:
3483 case R_PPC_REL24:
3484 if (expecting_tls_get_addr
3485 && h != NULL
3486 && h == htab->tls_get_addr)
3487 {
3488 if (h->plt.refcount > 0)
3489 h->plt.refcount -= 1;
3490 }
3491 expecting_tls_get_addr = 0;
3492 continue;
3493
3494 default:
3495 expecting_tls_get_addr = 0;
3496 continue;
7619e7c7 3497 }
25dbc73a
AM
3498
3499 if (h != NULL)
7619e7c7 3500 {
25dbc73a
AM
3501 if (tls_set == 0)
3502 {
3503 /* We managed to get rid of a got entry. */
3504 if (h->got.refcount > 0)
3505 h->got.refcount -= 1;
3506 }
3507 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3508 }
3509 else
3510 {
3511 Elf_Internal_Sym *sym;
3512 bfd_signed_vma *lgot_refs;
3513 char *lgot_masks;
3514
3515 if (locsyms == NULL)
3516 {
3517 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3518 if (locsyms == NULL)
3519 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3520 symtab_hdr->sh_info,
3521 0, NULL, NULL, NULL);
3522 if (locsyms == NULL)
3523 {
3524 if (elf_section_data (sec)->relocs != relstart)
3525 free (relstart);
3526 return FALSE;
3527 }
3528 }
3529 sym = locsyms + r_symndx;
3530 lgot_refs = elf_local_got_refcounts (ibfd);
3531 if (lgot_refs == NULL)
3532 abort ();
3533 if (tls_set == 0)
3534 {
3535 /* We managed to get rid of a got entry. */
3536 if (lgot_refs[r_symndx] > 0)
3537 lgot_refs[r_symndx] -= 1;
3538 }
3539 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
3540 tls_mask = &lgot_masks[r_symndx];
7619e7c7 3541 }
25dbc73a
AM
3542
3543 *tls_mask |= tls_set;
3544 *tls_mask &= ~tls_clear;
7619e7c7 3545 }
25dbc73a
AM
3546
3547 if (elf_section_data (sec)->relocs != relstart)
3548 free (relstart);
7619e7c7 3549 }
25dbc73a
AM
3550
3551 if (locsyms != NULL
3552 && (symtab_hdr->contents != (unsigned char *) locsyms))
3553 {
3554 if (!info->keep_memory)
3555 free (locsyms);
3556 else
3557 symtab_hdr->contents = (unsigned char *) locsyms;
3558 }
7fce784e 3559 }
25dbc73a
AM
3560 return TRUE;
3561}
3562\f
3563/* Adjust a symbol defined by a dynamic object and referenced by a
3564 regular object. The current definition is in some section of the
3565 dynamic object, but we're not including those sections. We have to
3566 change the definition to something the rest of the link can
3567 understand. */
7fce784e 3568
25dbc73a
AM
3569static bfd_boolean
3570ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3571 struct elf_link_hash_entry *h)
3572{
3573 struct ppc_elf_link_hash_table *htab;
3574 asection *s;
3575 unsigned int power_of_two;
7fce784e 3576
25dbc73a
AM
3577#ifdef DEBUG
3578 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
3579 h->root.root.string);
3580#endif
252b5132 3581
25dbc73a
AM
3582 /* Make sure we know what is going on here. */
3583 htab = ppc_elf_hash_table (info);
3584 BFD_ASSERT (htab->elf.dynobj != NULL
3585 && (h->needs_plt
3586 || h->u.weakdef != NULL
3587 || (h->def_dynamic
3588 && h->ref_regular
3589 && !h->def_regular)));
252b5132 3590
25dbc73a
AM
3591 /* Deal with function syms. */
3592 if (h->type == STT_FUNC
3593 || h->needs_plt)
3594 {
3595 /* Clear procedure linkage table information for any symbol that
3596 won't need a .plt entry. */
3597 if (h->plt.refcount <= 0
3598 || SYMBOL_CALLS_LOCAL (info, h)
3599 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3600 && h->root.type == bfd_link_hash_undefweak))
252b5132 3601 {
25dbc73a 3602 /* A PLT entry is not required/allowed when:
252b5132 3603
25dbc73a
AM
3604 1. We are not using ld.so; because then the PLT entry
3605 can't be set up, so we can't use one. In this case,
3606 ppc_elf_adjust_dynamic_symbol won't even be called.
252b5132 3607
25dbc73a
AM
3608 2. GC has rendered the entry unused.
3609
3610 3. We know for certain that a call to this symbol
3611 will go to this object, or will remain undefined. */
3612 h->plt.offset = (bfd_vma) -1;
3613 h->needs_plt = 0;
3614 }
3615 return TRUE;
252b5132 3616 }
25dbc73a
AM
3617 else
3618 h->plt.offset = (bfd_vma) -1;
252b5132 3619
25dbc73a
AM
3620 /* If this is a weak symbol, and there is a real definition, the
3621 processor independent code will have arranged for us to see the
3622 real definition first, and we can just use the same value. */
3623 if (h->u.weakdef != NULL)
252b5132 3624 {
25dbc73a
AM
3625 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3626 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3627 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3628 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3629 if (ELIMINATE_COPY_RELOCS)
3630 h->non_got_ref = h->u.weakdef->non_got_ref;
3631 return TRUE;
3632 }
dc810e39 3633
25dbc73a
AM
3634 /* This is a reference to a symbol defined by a dynamic object which
3635 is not a function. */
252b5132 3636
25dbc73a
AM
3637 /* If we are creating a shared library, we must presume that the
3638 only references to the symbol are via the global offset table.
3639 For such cases we need not do anything here; the relocations will
3640 be handled correctly by relocate_section. */
3641 if (info->shared)
3642 return TRUE;
252b5132 3643
25dbc73a
AM
3644 /* If there are no references to this symbol that do not use the
3645 GOT, we don't need to generate a copy reloc. */
3646 if (!h->non_got_ref)
3647 return TRUE;
3648
3649 if (ELIMINATE_COPY_RELOCS)
3650 {
3651 struct ppc_elf_dyn_relocs *p;
3652 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
252b5132 3653 {
25dbc73a
AM
3654 s = p->sec->output_section;
3655 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3656 break;
252b5132
RH
3657 }
3658
25dbc73a
AM
3659 /* If we didn't find any dynamic relocs in read-only sections, then
3660 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3661 if (p == NULL)
252b5132 3662 {
25dbc73a
AM
3663 h->non_got_ref = 0;
3664 return TRUE;
252b5132
RH
3665 }
3666 }
3667
25dbc73a
AM
3668 /* We must allocate the symbol in our .dynbss section, which will
3669 become part of the .bss section of the executable. There will be
3670 an entry for this symbol in the .dynsym section. The dynamic
3671 object will contain position independent code, so all references
3672 from the dynamic object to this symbol will go through the global
3673 offset table. The dynamic linker will use the .dynsym entry to
3674 determine the address it must put in the global offset table, so
3675 both the dynamic object and the regular object will refer to the
3676 same memory location for the variable.
3677
3678 Of course, if the symbol is sufficiently small, we must instead
3679 allocate it in .sbss. FIXME: It would be better to do this if and
3680 only if there were actually SDAREL relocs for that symbol. */
3681
3682 if (h->size <= elf_gp_size (htab->elf.dynobj))
3683 s = htab->dynsbss;
3684 else
3685 s = htab->dynbss;
3686 BFD_ASSERT (s != NULL);
3687
3688 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
3689 copy the initial value out of the dynamic object and into the
3690 runtime process image. We need to remember the offset into the
3691 .rela.bss section we are going to use. */
3692 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3693 {
3694 asection *srel;
3695
3696 if (h->size <= elf_gp_size (htab->elf.dynobj))
3697 srel = htab->relsbss;
3698 else
3699 srel = htab->relbss;
3700 BFD_ASSERT (srel != NULL);
3701 srel->size += sizeof (Elf32_External_Rela);
3702 h->needs_copy = 1;
3703 }
7619e7c7 3704
25dbc73a
AM
3705 /* We need to figure out the alignment required for this symbol. I
3706 have no idea how ELF linkers handle this. */
3707 power_of_two = bfd_log2 (h->size);
3708 if (power_of_two > 4)
3709 power_of_two = 4;
7619e7c7 3710
25dbc73a
AM
3711 /* Apply the required alignment. */
3712 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3713 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3714 {
3715 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
7619e7c7 3716 return FALSE;
7619e7c7
AM
3717 }
3718
25dbc73a
AM
3719 /* Define the symbol as being at this point in the section. */
3720 h->root.u.def.section = s;
3721 h->root.u.def.value = s->size;
7619e7c7 3722
25dbc73a
AM
3723 /* Increment the section size to make room for the symbol. */
3724 s->size += h->size;
7619e7c7 3725
25dbc73a
AM
3726 return TRUE;
3727}
3728\f
3729/* Allocate space in associated reloc sections for dynamic relocs. */
252b5132 3730
b34976b6 3731static bfd_boolean
25dbc73a 3732allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
252b5132 3733{
25dbc73a
AM
3734 struct bfd_link_info *info = inf;
3735 struct ppc_elf_link_hash_entry *eh;
7619e7c7 3736 struct ppc_elf_link_hash_table *htab;
25dbc73a 3737 struct ppc_elf_dyn_relocs *p;
252b5132 3738
25dbc73a 3739 if (h->root.type == bfd_link_hash_indirect)
b34976b6 3740 return TRUE;
252b5132 3741
25dbc73a
AM
3742 if (h->root.type == bfd_link_hash_warning)
3743 /* When warning symbols are created, they **replace** the "real"
3744 entry in the hash table, thus we never get to see the real
3745 symbol in a hash traversal. So look at it now. */
3746 h = (struct elf_link_hash_entry *) h->root.u.i.link;
e47cd125 3747
7619e7c7 3748 htab = ppc_elf_hash_table (info);
25dbc73a
AM
3749 if (htab->elf.dynamic_sections_created
3750 && h->plt.refcount > 0)
252b5132 3751 {
25dbc73a
AM
3752 /* Make sure this symbol is output as a dynamic symbol. */
3753 if (h->dynindx == -1
3754 && !h->forced_local)
252b5132 3755 {
25dbc73a
AM
3756 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3757 return FALSE;
252b5132
RH
3758 }
3759
25dbc73a
AM
3760 if (info->shared
3761 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
252b5132 3762 {
25dbc73a 3763 asection *s = htab->plt;
7619e7c7 3764
25dbc73a
AM
3765 /* If this is the first .plt entry, make room for the special
3766 first entry. */
3767 if (s->size == 0)
3768 s->size += PLT_INITIAL_ENTRY_SIZE;
7619e7c7 3769
25dbc73a
AM
3770 /* The PowerPC PLT is actually composed of two parts, the
3771 first part is 2 words (for a load and a jump), and then
3772 there is a remaining word available at the end. */
3773 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3774 + (PLT_SLOT_SIZE
3775 * ((s->size - PLT_INITIAL_ENTRY_SIZE)
3776 / PLT_ENTRY_SIZE)));
7619e7c7 3777
25dbc73a
AM
3778 /* If this symbol is not defined in a regular file, and we
3779 are not generating a shared library, then set the symbol
3780 to this location in the .plt. This is required to make
3781 function pointers compare as equal between the normal
3782 executable and the shared library. */
3783 if (! info->shared
3784 && !h->def_regular)
252b5132 3785 {
25dbc73a
AM
3786 h->root.u.def.section = s;
3787 h->root.u.def.value = h->plt.offset;
252b5132 3788 }
252b5132 3789
25dbc73a
AM
3790 /* Make room for this entry. After the 8192nd entry, room
3791 for two entries is allocated. */
3792 s->size += PLT_ENTRY_SIZE;
3793 if ((s->size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3794 > PLT_NUM_SINGLE_ENTRIES)
3795 s->size += PLT_ENTRY_SIZE;
3796
3797 /* We also need to make an entry in the .rela.plt section. */
3798 htab->relplt->size += sizeof (Elf32_External_Rela);
3799 }
3800 else
3801 {
3802 h->plt.offset = (bfd_vma) -1;
3803 h->needs_plt = 0;
3804 }
3805 }
3806 else
3807 {
3808 h->plt.offset = (bfd_vma) -1;
3809 h->needs_plt = 0;
3810 }
3811
3812 eh = (struct ppc_elf_link_hash_entry *) h;
3813 if (eh->elf.got.refcount > 0)
3814 {
3815 /* Make sure this symbol is output as a dynamic symbol. */
3816 if (eh->elf.dynindx == -1
3817 && !eh->elf.forced_local)
3818 {
3819 if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
b34976b6 3820 return FALSE;
25dbc73a 3821 }
252b5132 3822
25dbc73a
AM
3823 if (eh->tls_mask == (TLS_TLS | TLS_LD)
3824 && !eh->elf.def_dynamic)
3825 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
3826 eh->elf.got.offset = (bfd_vma) -1;
3827 else
3828 {
3829 bfd_boolean dyn;
3830 eh->elf.got.offset = htab->got->size;
3831 if ((eh->tls_mask & TLS_TLS) != 0)
252b5132 3832 {
25dbc73a
AM
3833 if ((eh->tls_mask & TLS_LD) != 0)
3834 htab->got->size += 8;
3835 if ((eh->tls_mask & TLS_GD) != 0)
3836 htab->got->size += 8;
3837 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
3838 htab->got->size += 4;
3839 if ((eh->tls_mask & TLS_DTPREL) != 0)
3840 htab->got->size += 4;
252b5132 3841 }
25dbc73a
AM
3842 else
3843 htab->got->size += 4;
3844 dyn = htab->elf.dynamic_sections_created;
3845 if ((info->shared
3846 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
3847 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
3848 || eh->elf.root.type != bfd_link_hash_undefweak))
252b5132 3849 {
25dbc73a
AM
3850 /* All the entries we allocated need relocs. */
3851 htab->relgot->size
3852 += ((htab->got->size - eh->elf.got.offset) / 4
3853 * sizeof (Elf32_External_Rela));
3854 /* Except LD only needs one. */
3855 if ((eh->tls_mask & TLS_LD) != 0)
3856 htab->relgot->size -= sizeof (Elf32_External_Rela);
252b5132 3857 }
25dbc73a
AM
3858 }
3859 }
3860 else
3861 eh->elf.got.offset = (bfd_vma) -1;
252b5132 3862
25dbc73a
AM
3863 if (eh->dyn_relocs == NULL)
3864 return TRUE;
252b5132 3865
25dbc73a
AM
3866 /* In the shared -Bsymbolic case, discard space allocated for
3867 dynamic pc-relative relocs against symbols which turn out to be
3868 defined in regular objects. For the normal shared case, discard
3869 space for relocs that have become local due to symbol visibility
3870 changes. */
3871
3872 if (info->shared)
3873 {
3874 /* Relocs that use pc_count are those that appear on a call insn,
3875 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
3876 generated via assembly. We want calls to protected symbols to
3877 resolve directly to the function rather than going via the plt.
3878 If people want function pointer comparisons to work as expected
3879 then they should avoid writing weird assembly. */
3880 if (SYMBOL_CALLS_LOCAL (info, h))
3881 {
3882 struct ppc_elf_dyn_relocs **pp;
3883
3884 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
252b5132 3885 {
25dbc73a
AM
3886 p->count -= p->pc_count;
3887 p->pc_count = 0;
3888 if (p->count == 0)
3889 *pp = p->next;
3890 else
3891 pp = &p->next;
252b5132 3892 }
25dbc73a 3893 }
252b5132 3894
25dbc73a
AM
3895 /* Also discard relocs on undefined weak syms with non-default
3896 visibility. */
3897 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3898 && h->root.type == bfd_link_hash_undefweak)
3899 eh->dyn_relocs = NULL;
7619e7c7 3900
25dbc73a
AM
3901 /* Make sure undefined weak symbols are output as a dynamic symbol
3902 in PIEs. */
3903 if (info->pie
3904 && eh->dyn_relocs != NULL
3905 && h->dynindx == -1
3906 && h->root.type == bfd_link_hash_undefweak
3907 && !h->forced_local)
3908 {
3909 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3910 return FALSE;
3911 }
3912 }
3913 else if (ELIMINATE_COPY_RELOCS)
3914 {
3915 /* For the non-shared case, discard space for relocs against
3916 symbols which turn out to need copy relocs or are not
3917 dynamic. */
7619e7c7 3918
25dbc73a
AM
3919 if (!h->non_got_ref
3920 && h->def_dynamic
3921 && !h->def_regular)
3922 {
3923 /* Make sure this symbol is output as a dynamic symbol.
3924 Undefined weak syms won't yet be marked as dynamic. */
3925 if (h->dynindx == -1
3926 && !h->forced_local)
3927 {
3928 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3929 return FALSE;
3930 }
7619e7c7 3931
25dbc73a
AM
3932 /* If that succeeded, we know we'll be keeping all the
3933 relocs. */
3934 if (h->dynindx != -1)
3935 goto keep;
3936 }
252b5132 3937
25dbc73a 3938 eh->dyn_relocs = NULL;
252b5132 3939
25dbc73a
AM
3940 keep: ;
3941 }
252b5132 3942
25dbc73a
AM
3943 /* Finally, allocate space. */
3944 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3945 {
3946 asection *sreloc = elf_section_data (p->sec)->sreloc;
3947 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3948 }
252b5132 3949
25dbc73a
AM
3950 return TRUE;
3951}
7619e7c7 3952
25dbc73a 3953/* Find any dynamic relocs that apply to read-only sections. */
7619e7c7 3954
25dbc73a
AM
3955static bfd_boolean
3956readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
3957{
3958 struct ppc_elf_dyn_relocs *p;
7619e7c7 3959
25dbc73a
AM
3960 if (h->root.type == bfd_link_hash_indirect)
3961 return TRUE;
252b5132 3962
25dbc73a
AM
3963 if (h->root.type == bfd_link_hash_warning)
3964 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7619e7c7 3965
25dbc73a
AM
3966 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3967 {
3968 asection *s = p->sec->output_section;
ee05f2fe 3969
25dbc73a
AM
3970 if (s != NULL
3971 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
3972 == (SEC_READONLY | SEC_ALLOC)))
3973 {
3974 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
ee05f2fe 3975
25dbc73a
AM
3976 /* Not an error, just cut short the traversal. */
3977 return FALSE;
3978 }
3979 }
3980 return TRUE;
3981}
3982
3983/* Set the sizes of the dynamic sections. */
3984
3985static bfd_boolean
3986ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3987 struct bfd_link_info *info)
3988{
3989 struct ppc_elf_link_hash_table *htab;
3990 asection *s;
3991 bfd_boolean relocs;
3992 bfd *ibfd;
7fce784e 3993
252b5132 3994#ifdef DEBUG
25dbc73a 3995 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
252b5132 3996#endif
252b5132 3997
25dbc73a
AM
3998 htab = ppc_elf_hash_table (info);
3999 BFD_ASSERT (htab->elf.dynobj != NULL);
252b5132 4000
25dbc73a
AM
4001 if (elf_hash_table (info)->dynamic_sections_created)
4002 {
4003 /* Set the contents of the .interp section to the interpreter. */
4004 if (info->executable)
4005 {
4006 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
4007 BFD_ASSERT (s != NULL);
4008 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4009 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4010 }
4011 }
252b5132 4012
25dbc73a
AM
4013 if (htab->tlsld_got.refcount > 0)
4014 {
4015 htab->tlsld_got.offset = htab->got->size;
4016 htab->got->size += 8;
4017 if (info->shared)
4018 htab->relgot->size += sizeof (Elf32_External_Rela);
4019 }
4020 else
4021 htab->tlsld_got.offset = (bfd_vma) -1;
252b5132 4022
25dbc73a
AM
4023 /* Set up .got offsets for local syms, and space for local dynamic
4024 relocs. */
4025 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4026 {
4027 bfd_signed_vma *local_got;
4028 bfd_signed_vma *end_local_got;
4029 char *lgot_masks;
4030 bfd_size_type locsymcount;
4031 Elf_Internal_Shdr *symtab_hdr;
4032 asection *srel;
7fce784e 4033
d2663f46 4034 if (!is_ppc_elf_target (ibfd->xvec))
25dbc73a 4035 continue;
7fce784e 4036
25dbc73a
AM
4037 for (s = ibfd->sections; s != NULL; s = s->next)
4038 {
4039 struct ppc_elf_dyn_relocs *p;
252b5132 4040
25dbc73a
AM
4041 for (p = ((struct ppc_elf_dyn_relocs *)
4042 elf_section_data (s)->local_dynrel);
4043 p != NULL;
4044 p = p->next)
4045 {
4046 if (!bfd_is_abs_section (p->sec)
4047 && bfd_is_abs_section (p->sec->output_section))
4048 {
4049 /* Input section has been discarded, either because
4050 it is a copy of a linkonce section or due to
4051 linker script /DISCARD/, so we'll be discarding
4052 the relocs too. */
7fce784e 4053 }
25dbc73a 4054 else if (p->count != 0)
7fce784e 4055 {
25dbc73a
AM
4056 elf_section_data (p->sec)->sreloc->size
4057 += p->count * sizeof (Elf32_External_Rela);
4058 if ((p->sec->output_section->flags
4059 & (SEC_READONLY | SEC_ALLOC))
4060 == (SEC_READONLY | SEC_ALLOC))
4061 info->flags |= DF_TEXTREL;
7fce784e 4062 }
252b5132 4063 }
252b5132 4064 }
252b5132 4065
25dbc73a
AM
4066 local_got = elf_local_got_refcounts (ibfd);
4067 if (!local_got)
4068 continue;
252b5132 4069
25dbc73a
AM
4070 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4071 locsymcount = symtab_hdr->sh_info;
4072 end_local_got = local_got + locsymcount;
4073 lgot_masks = (char *) end_local_got;
4074 s = htab->got;
4075 srel = htab->relgot;
4076 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
4077 if (*local_got > 0)
4078 {
4079 if (*lgot_masks == (TLS_TLS | TLS_LD))
4080 {
4081 /* If just an LD reloc, we'll just use
4082 htab->tlsld_got.offset. */
4083 if (htab->tlsld_got.offset == (bfd_vma) -1)
4084 {
4085 htab->tlsld_got.offset = s->size;
4086 s->size += 8;
4087 if (info->shared)
4088 srel->size += sizeof (Elf32_External_Rela);
4089 }
4090 *local_got = (bfd_vma) -1;
4091 }
4092 else
4093 {
4094 *local_got = s->size;
4095 if ((*lgot_masks & TLS_TLS) != 0)
4096 {
4097 if ((*lgot_masks & TLS_GD) != 0)
4098 s->size += 8;
4099 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
4100 s->size += 4;
4101 if ((*lgot_masks & TLS_DTPREL) != 0)
4102 s->size += 4;
4103 }
4104 else
4105 s->size += 4;
4106 if (info->shared)
4107 srel->size += ((s->size - *local_got) / 4
4108 * sizeof (Elf32_External_Rela));
4109 }
4110 }
4111 else
4112 *local_got = (bfd_vma) -1;
4113 }
252b5132 4114
25dbc73a
AM
4115 /* Allocate space for global sym dynamic relocs. */
4116 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
4117
4118 /* We've now determined the sizes of the various dynamic sections.
4119 Allocate memory for them. */
4120 relocs = FALSE;
4121 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
252b5132 4122 {
25dbc73a
AM
4123 if ((s->flags & SEC_LINKER_CREATED) == 0)
4124 continue;
252b5132 4125
25dbc73a
AM
4126 if (s == htab->plt
4127 || s == htab->got)
4128 {
4129 /* Strip this section if we don't need it; see the
4130 comment below. */
4131 }
4132 else if ((htab->sdata != NULL && s == htab->sdata->section)
4133 || (htab->sdata2 != NULL && s == htab->sdata2->section))
4134 {
4135 if (s->size == 0)
4136 /* Don't strip these sections. We need them because they
4137 define _SDA_BASE_ and _SDA2_BASE_. crt1.o makes a
4138 reference to _SDA_BASE_ to set up r13. */
4139 continue;
4140 }
4141 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4142 {
4143 if (s->size == 0)
252b5132 4144 {
25dbc73a
AM
4145 /* If we don't need this section, strip it from the
4146 output file. This is mostly to handle .rela.bss and
4147 .rela.plt. We must create both sections in
4148 create_dynamic_sections, because they must be created
4149 before the linker maps input sections to output
4150 sections. The linker does that before
4151 adjust_dynamic_symbol is called, and it is that
4152 function which decides whether anything needs to go
4153 into these sections. */
4154 }
4155 else
4156 {
4157 /* Remember whether there are any relocation sections. */
4158 relocs = TRUE;
252b5132 4159
25dbc73a
AM
4160 /* We use the reloc_count field as a counter if we need
4161 to copy relocs into the output file. */
4162 s->reloc_count = 0;
252b5132
RH
4163 }
4164 }
25dbc73a
AM
4165 else
4166 {
4167 /* It's not one of our sections, so don't allocate space. */
4168 continue;
4169 }
252b5132 4170
25dbc73a
AM
4171 if (s->size == 0)
4172 {
4173 _bfd_strip_section_from_output (info, s);
4174 continue;
4175 }
7fce784e 4176
25dbc73a
AM
4177 /* Allocate memory for the section contents. */
4178 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
4179 if (s->contents == NULL)
4180 return FALSE;
4181 }
252b5132 4182
25dbc73a 4183 if (htab->elf.dynamic_sections_created)
7619e7c7 4184 {
25dbc73a
AM
4185 /* Add some entries to the .dynamic section. We fill in the
4186 values later, in ppc_elf_finish_dynamic_sections, but we
4187 must add the entries now so that we get the correct size for
4188 the .dynamic section. The DT_DEBUG entry is filled in by the
4189 dynamic linker and used by the debugger. */
4190#define add_dynamic_entry(TAG, VAL) \
4191 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
252b5132 4192
25dbc73a 4193 if (info->executable)
7619e7c7 4194 {
25dbc73a
AM
4195 if (!add_dynamic_entry (DT_DEBUG, 0))
4196 return FALSE;
7619e7c7
AM
4197 }
4198
25dbc73a 4199 if (htab->plt != NULL && htab->plt->size != 0)
7619e7c7 4200 {
25dbc73a
AM
4201 if (!add_dynamic_entry (DT_PLTGOT, 0)
4202 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4203 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4204 || !add_dynamic_entry (DT_JMPREL, 0))
4205 return FALSE;
4206 }
7619e7c7 4207
25dbc73a
AM
4208 if (relocs)
4209 {
4210 if (!add_dynamic_entry (DT_RELA, 0)
4211 || !add_dynamic_entry (DT_RELASZ, 0)
4212 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4213 return FALSE;
4214 }
7619e7c7 4215
25dbc73a
AM
4216 /* If any dynamic relocs apply to a read-only section, then we
4217 need a DT_TEXTREL entry. */
4218 if ((info->flags & DF_TEXTREL) == 0)
4219 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
4220 info);
7619e7c7 4221
25dbc73a
AM
4222 if ((info->flags & DF_TEXTREL) != 0)
4223 {
4224 if (!add_dynamic_entry (DT_TEXTREL, 0))
4225 return FALSE;
7619e7c7
AM
4226 }
4227 }
25dbc73a
AM
4228#undef add_dynamic_entry
4229
7619e7c7
AM
4230 return TRUE;
4231}
25dbc73a
AM
4232\f
4233#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
4234
4235static const int shared_stub_entry[] =
4236 {
4237 0x7c0802a6, /* mflr 0 */
4238 0x429f0005, /* bcl 20, 31, .Lxxx */
4239 0x7d6802a6, /* mflr 11 */
4240 0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
4241 0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
4242 0x7c0803a6, /* mtlr 0 */
4243 0x7d6903a6, /* mtctr 11 */
4244 0x4e800420, /* bctr */
4245 };
4246
4247static const int stub_entry[] =
4248 {
4249 0x3d600000, /* lis 11,xxx@ha */
4250 0x396b0000, /* addi 11,11,xxx@l */
4251 0x7d6903a6, /* mtctr 11 */
4252 0x4e800420, /* bctr */
4253 };
4254
4255static bfd_boolean
4256ppc_elf_relax_section (bfd *abfd,
4257 asection *isec,
4258 struct bfd_link_info *link_info,
4259 bfd_boolean *again)
4260{
4261 struct one_fixup
4262 {
4263 struct one_fixup *next;
4264 asection *tsec;
4265 bfd_vma toff;
4266 bfd_vma trampoff;
4267 };
7619e7c7 4268
25dbc73a
AM
4269 Elf_Internal_Shdr *symtab_hdr;
4270 bfd_byte *contents = NULL;
4271 Elf_Internal_Sym *isymbuf = NULL;
4272 Elf_Internal_Rela *internal_relocs = NULL;
4273 Elf_Internal_Rela *irel, *irelend;
4274 struct one_fixup *fixups = NULL;
4275 bfd_boolean changed;
4276 struct ppc_elf_link_hash_table *ppc_info;
4277 bfd_size_type trampoff;
7619e7c7 4278
25dbc73a 4279 *again = FALSE;
7619e7c7 4280
25dbc73a
AM
4281 /* Nothing to do if there are no relocations. */
4282 if ((isec->flags & SEC_RELOC) == 0 || isec->reloc_count == 0)
4283 return TRUE;
7619e7c7 4284
25dbc73a
AM
4285 trampoff = (isec->size + 3) & (bfd_vma) -4;
4286 /* Space for a branch around any trampolines. */
4287 trampoff += 4;
7619e7c7 4288
25dbc73a 4289 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7619e7c7 4290
25dbc73a
AM
4291 /* Get a copy of the native relocations. */
4292 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
4293 link_info->keep_memory);
4294 if (internal_relocs == NULL)
4295 goto error_return;
7619e7c7 4296
25dbc73a
AM
4297 ppc_info = ppc_elf_hash_table (link_info);
4298 irelend = internal_relocs + isec->reloc_count;
7619e7c7 4299
25dbc73a 4300 for (irel = internal_relocs; irel < irelend; irel++)
7619e7c7 4301 {
25dbc73a
AM
4302 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
4303 bfd_vma symaddr, reladdr, toff, roff;
4304 asection *tsec;
4305 struct one_fixup *f;
4306 size_t insn_offset = 0;
4307 bfd_vma max_branch_offset, val;
4308 bfd_byte *hit_addr;
4309 unsigned long t0;
4310 unsigned char sym_type;
7619e7c7 4311
25dbc73a
AM
4312 switch (r_type)
4313 {
4314 case R_PPC_REL24:
4315 case R_PPC_LOCAL24PC:
4316 case R_PPC_PLTREL24:
4317 max_branch_offset = 1 << 25;
4318 break;
7619e7c7 4319
25dbc73a
AM
4320 case R_PPC_REL14:
4321 case R_PPC_REL14_BRTAKEN:
4322 case R_PPC_REL14_BRNTAKEN:
4323 max_branch_offset = 1 << 15;
4324 break;
7619e7c7 4325
25dbc73a
AM
4326 default:
4327 continue;
4328 }
7619e7c7 4329
25dbc73a
AM
4330 /* Get the value of the symbol referred to by the reloc. */
4331 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
4332 {
4333 /* A local symbol. */
4334 Elf_Internal_Sym *isym;
7fce784e 4335
25dbc73a
AM
4336 /* Read this BFD's local symbols. */
4337 if (isymbuf == NULL)
4338 {
4339 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4340 if (isymbuf == NULL)
4341 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4342 symtab_hdr->sh_info, 0,
4343 NULL, NULL, NULL);
4344 if (isymbuf == 0)
4345 goto error_return;
4346 }
4347 isym = isymbuf + ELF32_R_SYM (irel->r_info);
4348 if (isym->st_shndx == SHN_UNDEF)
4349 continue; /* We can't do anything with undefined symbols. */
4350 else if (isym->st_shndx == SHN_ABS)
4351 tsec = bfd_abs_section_ptr;
4352 else if (isym->st_shndx == SHN_COMMON)
4353 tsec = bfd_com_section_ptr;
4354 else
4355 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7fce784e 4356
25dbc73a
AM
4357 toff = isym->st_value;
4358 sym_type = ELF_ST_TYPE (isym->st_info);
4359 }
4360 else
4361 {
4362 /* Global symbol handling. */
4363 unsigned long indx;
4364 struct elf_link_hash_entry *h;
7619e7c7 4365
25dbc73a
AM
4366 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
4367 h = elf_sym_hashes (abfd)[indx];
7619e7c7 4368
25dbc73a
AM
4369 while (h->root.type == bfd_link_hash_indirect
4370 || h->root.type == bfd_link_hash_warning)
4371 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7619e7c7 4372
25dbc73a
AM
4373 if (r_type == R_PPC_PLTREL24
4374 && ppc_info->plt != NULL
4375 && h->plt.offset != (bfd_vma) -1)
4376 {
4377 tsec = ppc_info->plt;
4378 toff = h->plt.offset;
4379 }
4380 else if (h->root.type == bfd_link_hash_defined
4381 || h->root.type == bfd_link_hash_defweak)
4382 {
4383 tsec = h->root.u.def.section;
4384 toff = h->root.u.def.value;
4385 }
4386 else
4387 continue;
7619e7c7 4388
25dbc73a
AM
4389 sym_type = h->type;
4390 }
7619e7c7 4391
25dbc73a
AM
4392 /* If the branch and target are in the same section, you have
4393 no hope of adding stubs. We'll error out later should the
4394 branch overflow. */
4395 if (tsec == isec)
4396 continue;
4397
4398 /* There probably isn't any reason to handle symbols in
4399 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
4400 attribute for a code section, and we are only looking at
4401 branches. However, implement it correctly here as a
4402 reference for other target relax_section functions. */
4403 if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
4404 {
4405 /* At this stage in linking, no SEC_MERGE symbol has been
4406 adjusted, so all references to such symbols need to be
4407 passed through _bfd_merged_section_offset. (Later, in
4408 relocate_section, all SEC_MERGE symbols *except* for
4409 section symbols have been adjusted.)
4410
4411 gas may reduce relocations against symbols in SEC_MERGE
4412 sections to a relocation against the section symbol when
4413 the original addend was zero. When the reloc is against
4414 a section symbol we should include the addend in the
4415 offset passed to _bfd_merged_section_offset, since the
4416 location of interest is the original symbol. On the
4417 other hand, an access to "sym+addend" where "sym" is not
4418 a section symbol should not include the addend; Such an
4419 access is presumed to be an offset from "sym"; The
4420 location of interest is just "sym". */
4421 if (sym_type == STT_SECTION)
4422 toff += irel->r_addend;
7619e7c7 4423
25dbc73a
AM
4424 toff = _bfd_merged_section_offset (abfd, &tsec,
4425 elf_section_data (tsec)->sec_info,
4426 toff);
7619e7c7 4427
25dbc73a
AM
4428 if (sym_type != STT_SECTION)
4429 toff += irel->r_addend;
4430 }
4431 else
4432 toff += irel->r_addend;
7619e7c7 4433
25dbc73a 4434 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
7619e7c7 4435
25dbc73a
AM
4436 roff = irel->r_offset;
4437 reladdr = isec->output_section->vma + isec->output_offset + roff;
7619e7c7 4438
25dbc73a
AM
4439 /* If the branch is in range, no need to do anything. */
4440 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
4441 continue;
252b5132 4442
25dbc73a
AM
4443 /* Look for an existing fixup to this address. */
4444 for (f = fixups; f ; f = f->next)
4445 if (f->tsec == tsec && f->toff == toff)
4446 break;
4447
4448 if (f == NULL)
7619e7c7 4449 {
25dbc73a
AM
4450 size_t size;
4451 unsigned long stub_rtype;
252b5132 4452
25dbc73a
AM
4453 val = trampoff - roff;
4454 if (val >= max_branch_offset)
4455 /* Oh dear, we can't reach a trampoline. Don't try to add
4456 one. We'll report an error later. */
4457 continue;
252b5132 4458
25dbc73a
AM
4459 if (link_info->shared)
4460 {
4461 size = 4 * ARRAY_SIZE (shared_stub_entry);
4462 insn_offset = 12;
4463 stub_rtype = R_PPC_RELAX32PC;
4464 }
4465 else
4466 {
4467 size = 4 * ARRAY_SIZE (stub_entry);
4468 insn_offset = 0;
4469 stub_rtype = R_PPC_RELAX32;
4470 }
dc810e39 4471
25dbc73a
AM
4472 if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
4473 != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
4474 abort ();
4475 if (tsec == ppc_info->plt)
4476 stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
252b5132 4477
25dbc73a
AM
4478 /* Hijack the old relocation. Since we need two
4479 relocations for this use a "composite" reloc. */
4480 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4481 stub_rtype);
4482 irel->r_offset = trampoff + insn_offset;
252b5132 4483
25dbc73a
AM
4484 /* Record the fixup so we don't do it again this section. */
4485 f = bfd_malloc (sizeof (*f));
4486 f->next = fixups;
4487 f->tsec = tsec;
4488 f->toff = toff;
4489 f->trampoff = trampoff;
4490 fixups = f;
252b5132 4491
25dbc73a
AM
4492 trampoff += size;
4493 }
4494 else
4495 {
4496 val = f->trampoff - roff;
4497 if (val >= max_branch_offset)
4498 continue;
252b5132 4499
25dbc73a
AM
4500 /* Nop out the reloc, since we're finalizing things here. */
4501 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
4502 }
252b5132 4503
25dbc73a
AM
4504 /* Get the section contents. */
4505 if (contents == NULL)
4506 {
4507 /* Get cached copy if it exists. */
4508 if (elf_section_data (isec)->this_hdr.contents != NULL)
4509 contents = elf_section_data (isec)->this_hdr.contents;
4510 else
4511 {
4512 /* Go get them off disk. */
4513 if (!bfd_malloc_and_get_section (abfd, isec, &contents))
4514 goto error_return;
4515 }
4516 }
252b5132 4517
25dbc73a
AM
4518 /* Fix up the existing branch to hit the trampoline. */
4519 hit_addr = contents + roff;
4520 switch (r_type)
4521 {
4522 case R_PPC_REL24:
4523 case R_PPC_LOCAL24PC:
4524 case R_PPC_PLTREL24:
4525 t0 = bfd_get_32 (abfd, hit_addr);
4526 t0 &= ~0x3fffffc;
4527 t0 |= val & 0x3fffffc;
4528 bfd_put_32 (abfd, t0, hit_addr);
4529 break;
252b5132 4530
25dbc73a
AM
4531 case R_PPC_REL14:
4532 case R_PPC_REL14_BRTAKEN:
4533 case R_PPC_REL14_BRNTAKEN:
4534 t0 = bfd_get_32 (abfd, hit_addr);
4535 t0 &= ~0xfffc;
4536 t0 |= val & 0xfffc;
4537 bfd_put_32 (abfd, t0, hit_addr);
4538 break;
4539 }
4540 }
252b5132 4541
25dbc73a
AM
4542 /* Write out the trampolines. */
4543 changed = fixups != NULL;
4544 if (fixups != NULL)
4545 {
4546 const int *stub;
4547 bfd_byte *dest;
4548 bfd_vma val;
4549 int i, size;
252b5132 4550
25dbc73a
AM
4551 do
4552 {
4553 struct one_fixup *f = fixups;
4554 fixups = fixups->next;
4555 free (f);
4556 }
4557 while (fixups);
252b5132 4558
25dbc73a
AM
4559 contents = bfd_realloc (contents, trampoff);
4560 if (contents == NULL)
4561 goto error_return;
252b5132 4562
25dbc73a
AM
4563 isec->size = (isec->size + 3) & (bfd_vma) -4;
4564 /* Branch around the trampolines. */
4565 val = trampoff - isec->size + 0x48000000;
4566 dest = contents + isec->size;
4567 isec->size = trampoff;
4568 bfd_put_32 (abfd, val, dest);
4569 dest += 4;
252b5132 4570
25dbc73a
AM
4571 if (link_info->shared)
4572 {
4573 stub = shared_stub_entry;
4574 size = ARRAY_SIZE (shared_stub_entry);
4575 }
4576 else
4577 {
4578 stub = stub_entry;
4579 size = ARRAY_SIZE (stub_entry);
4580 }
252b5132 4581
25dbc73a
AM
4582 i = 0;
4583 while (dest < contents + trampoff)
252b5132 4584 {
25dbc73a
AM
4585 bfd_put_32 (abfd, stub[i], dest);
4586 i++;
4587 if (i == size)
4588 i = 0;
4589 dest += 4;
252b5132 4590 }
25dbc73a 4591 BFD_ASSERT (i == 0);
252b5132
RH
4592 }
4593
25dbc73a
AM
4594 if (isymbuf != NULL
4595 && symtab_hdr->contents != (unsigned char *) isymbuf)
252b5132 4596 {
25dbc73a
AM
4597 if (! link_info->keep_memory)
4598 free (isymbuf);
252b5132 4599 else
25dbc73a
AM
4600 {
4601 /* Cache the symbols for elf_link_input_bfd. */
4602 symtab_hdr->contents = (unsigned char *) isymbuf;
4603 }
252b5132
RH
4604 }
4605
25dbc73a
AM
4606 if (contents != NULL
4607 && elf_section_data (isec)->this_hdr.contents != contents)
4608 {
4609 if (!changed && !link_info->keep_memory)
4610 free (contents);
4611 else
4612 {
4613 /* Cache the section contents for elf_link_input_bfd. */
4614 elf_section_data (isec)->this_hdr.contents = contents;
4615 }
4616 }
252b5132 4617
25dbc73a
AM
4618 if (elf_section_data (isec)->relocs != internal_relocs)
4619 {
4620 if (!changed)
4621 free (internal_relocs);
4622 else
4623 elf_section_data (isec)->relocs = internal_relocs;
4624 }
252b5132 4625
25dbc73a 4626 *again = changed;
b34976b6 4627 return TRUE;
25dbc73a
AM
4628
4629 error_return:
4630 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
4631 free (isymbuf);
4632 if (contents != NULL
4633 && elf_section_data (isec)->this_hdr.contents != contents)
4634 free (contents);
4635 if (internal_relocs != NULL
4636 && elf_section_data (isec)->relocs != internal_relocs)
4637 free (internal_relocs);
4638 return FALSE;
252b5132 4639}
252b5132 4640\f
25dbc73a 4641#define bfd_put_ptr(BFD, VAL, ADDR) bfd_put_32 (BFD, VAL, ADDR)
252b5132 4642
25dbc73a 4643/* Fill in the address for a pointer generated in a linker section. */
252b5132 4644
25dbc73a
AM
4645static bfd_vma
4646elf_finish_pointer_linker_section (bfd *output_bfd,
4647 bfd *input_bfd,
4648 struct bfd_link_info *info,
4649 elf_linker_section_t *lsect,
4650 struct elf_link_hash_entry *h,
4651 bfd_vma relocation,
2bb04cf2 4652 const Elf_Internal_Rela *rel)
25dbc73a
AM
4653{
4654 elf_linker_section_pointers_t *linker_section_ptr;
252b5132 4655
25dbc73a 4656 BFD_ASSERT (lsect != NULL);
252b5132 4657
25dbc73a 4658 if (h != NULL)
252b5132 4659 {
25dbc73a
AM
4660 /* Handle global symbol. */
4661 struct ppc_elf_link_hash_entry *eh;
252b5132 4662
25dbc73a
AM
4663 eh = (struct ppc_elf_link_hash_entry *) h;
4664 linker_section_ptr
4665 = elf_find_pointer_linker_section (eh->linker_section_pointer,
4666 rel->r_addend,
4667 lsect);
252b5132 4668
25dbc73a
AM
4669 BFD_ASSERT (linker_section_ptr != NULL);
4670
4671 if (! elf_hash_table (info)->dynamic_sections_created
4672 || (info->shared
4673 && info->symbolic
4674 && h->def_regular))
252b5132 4675 {
25dbc73a
AM
4676 /* This is actually a static link, or it is a
4677 -Bsymbolic link and the symbol is defined
4678 locally. We must initialize this entry in the
4679 global section.
252b5132 4680
25dbc73a
AM
4681 When doing a dynamic link, we create a .rela.<xxx>
4682 relocation entry to initialize the value. This
4683 is done in the finish_dynamic_symbol routine. */
4684 if (!linker_section_ptr->written_address_p)
4685 {
4686 linker_section_ptr->written_address_p = TRUE;
4687 bfd_put_ptr (output_bfd,
4688 relocation + linker_section_ptr->addend,
4689 (lsect->section->contents
4690 + linker_section_ptr->offset));
4691 }
4692 }
4693 }
4694 else
4695 {
4696 /* Handle local symbol. */
4697 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
4698 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
4699 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
4700 linker_section_ptr = (elf_find_pointer_linker_section
4701 (elf_local_ptr_offsets (input_bfd)[r_symndx],
4702 rel->r_addend,
4703 lsect));
252b5132 4704
25dbc73a
AM
4705 BFD_ASSERT (linker_section_ptr != NULL);
4706
4707 /* Write out pointer if it hasn't been rewritten out before. */
4708 if (!linker_section_ptr->written_address_p)
4709 {
4710 linker_section_ptr->written_address_p = TRUE;
4711 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
4712 lsect->section->contents + linker_section_ptr->offset);
252b5132
RH
4713 }
4714 }
4715
25dbc73a
AM
4716 relocation = (lsect->section->output_offset
4717 + linker_section_ptr->offset
4718 - lsect->sym_offset);
252b5132 4719
25dbc73a
AM
4720#ifdef DEBUG
4721 fprintf (stderr,
4722 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
4723 lsect->name, (long) relocation, (long) relocation);
4724#endif
252b5132 4725
25dbc73a
AM
4726 /* Subtract out the addend, because it will get added back in by the normal
4727 processing. */
4728 return relocation - linker_section_ptr->addend;
252b5132 4729}
25dbc73a 4730
252b5132
RH
4731/* The RELOCATE_SECTION function is called by the ELF backend linker
4732 to handle the relocations for a section.
4733
4734 The relocs are always passed as Rela structures; if the section
4735 actually uses Rel structures, the r_addend field will always be
4736 zero.
4737
4738 This function is responsible for adjust the section contents as
4739 necessary, and (if using Rela relocs and generating a
1049f94e 4740 relocatable output file) adjusting the reloc addend as
252b5132
RH
4741 necessary.
4742
4743 This function does not have to worry about setting the reloc
4744 address or the reloc symbol index.
4745
4746 LOCAL_SYMS is a pointer to the swapped in local symbols.
4747
4748 LOCAL_SECTIONS is an array giving the section in the input file
4749 corresponding to the st_shndx field of each local symbol.
4750
4751 The global hash table entry for the global symbols can be found
4752 via elf_sym_hashes (input_bfd).
4753
1049f94e 4754 When generating relocatable output, this function must handle
252b5132
RH
4755 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4756 going to be the section symbol corresponding to the output
4757 section, which means that the addend must be adjusted
4758 accordingly. */
4759
b34976b6 4760static bfd_boolean
55fd94b0
AM
4761ppc_elf_relocate_section (bfd *output_bfd,
4762 struct bfd_link_info *info,
4763 bfd *input_bfd,
4764 asection *input_section,
4765 bfd_byte *contents,
4766 Elf_Internal_Rela *relocs,
4767 Elf_Internal_Sym *local_syms,
4768 asection **local_sections)
252b5132 4769{
7619e7c7
AM
4770 Elf_Internal_Shdr *symtab_hdr;
4771 struct elf_link_hash_entry **sym_hashes;
4772 struct ppc_elf_link_hash_table *htab;
4773 Elf_Internal_Rela *rel;
4774 Elf_Internal_Rela *relend;
4775 Elf_Internal_Rela outrel;
4776 bfd_byte *loc;
b34976b6 4777 asection *sreloc = NULL;
252b5132 4778 bfd_vma *local_got_offsets;
b34976b6 4779 bfd_boolean ret = TRUE;
b34976b6 4780
252b5132 4781#ifdef DEBUG
d003868e
AM
4782 _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
4783 "%ld relocations%s",
4784 input_bfd, input_section,
4785 (long) input_section->reloc_count,
4786 (info->relocatable) ? " (relocatable)" : "");
252b5132
RH
4787#endif
4788
1049f94e 4789 if (info->relocatable)
b34976b6 4790 return TRUE;
b491616a 4791
e47cd125 4792 /* Initialize howto table if not already done. */
8da6118f 4793 if (!ppc_elf_howto_table[R_PPC_ADDR32])
252b5132
RH
4794 ppc_elf_howto_init ();
4795
7619e7c7 4796 htab = ppc_elf_hash_table (info);
252b5132 4797 local_got_offsets = elf_local_got_offsets (input_bfd);
7619e7c7
AM
4798 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4799 sym_hashes = elf_sym_hashes (input_bfd);
4800 rel = relocs;
4801 relend = relocs + input_section->reloc_count;
252b5132
RH
4802 for (; rel < relend; rel++)
4803 {
7619e7c7
AM
4804 enum elf_ppc_reloc_type r_type;
4805 bfd_vma addend;
4806 bfd_reloc_status_type r;
4807 Elf_Internal_Sym *sym;
4808 asection *sec;
4809 struct elf_link_hash_entry *h;
4810 const char *sym_name;
252b5132
RH
4811 reloc_howto_type *howto;
4812 unsigned long r_symndx;
4813 bfd_vma relocation;
7619e7c7 4814 bfd_vma branch_bit, insn, from;
7619e7c7
AM
4815 bfd_boolean unresolved_reloc;
4816 bfd_boolean warned;
4817 unsigned int tls_type, tls_mask, tls_gd;
4818
55fd94b0
AM
4819 r_type = ELF32_R_TYPE (rel->r_info);
4820 sym = NULL;
4821 sec = NULL;
4822 h = NULL;
7619e7c7
AM
4823 unresolved_reloc = FALSE;
4824 warned = FALSE;
252b5132 4825 r_symndx = ELF32_R_SYM (rel->r_info);
560e09e9 4826
252b5132
RH
4827 if (r_symndx < symtab_hdr->sh_info)
4828 {
4829 sym = local_syms + r_symndx;
4830 sec = local_sections[r_symndx];
be8dd2ca 4831 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym);
252b5132 4832
8517fae7 4833 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
252b5132
RH
4834 }
4835 else
4836 {
b2a8e766
AM
4837 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4838 r_symndx, symtab_hdr, sym_hashes,
4839 h, sec, relocation,
4840 unresolved_reloc, warned);
9abc968f 4841
252b5132 4842 sym_name = h->root.root.string;
7619e7c7
AM
4843 }
4844
4845 /* TLS optimizations. Replace instruction sequences and relocs
4846 based on information we collected in tls_optimize. We edit
4847 RELOCS so that --emit-relocs will output something sensible
4848 for the final instruction stream. */
4849 tls_mask = 0;
4850 tls_gd = 0;
4851 if (IS_PPC_TLS_RELOC (r_type))
4852 {
4853 if (h != NULL)
4854 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4855 else if (local_got_offsets != NULL)
4856 {
4857 char *lgot_masks;
4858 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4859 tls_mask = lgot_masks[r_symndx];
4860 }
4861 }
4862
4863 /* Ensure reloc mapping code below stays sane. */
4864 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4865 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4866 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4867 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4868 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4869 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4870 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4871 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4872 abort ();
4873 switch (r_type)
4874 {
4875 default:
4876 break;
4877
4878 case R_PPC_GOT_TPREL16:
4879 case R_PPC_GOT_TPREL16_LO:
4880 if (tls_mask != 0
4881 && (tls_mask & TLS_TPREL) == 0)
4882 {
4883 bfd_vma insn;
4884 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4885 insn &= 31 << 21;
4886 insn |= 0x3c020000; /* addis 0,2,0 */
4887 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4888 r_type = R_PPC_TPREL16_HA;
4889 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4890 }
4891 break;
4892
4893 case R_PPC_TLS:
4894 if (tls_mask != 0
4895 && (tls_mask & TLS_TPREL) == 0)
4896 {
4897 bfd_vma insn, rtra;
4898 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4899 if ((insn & ((31 << 26) | (31 << 11)))
4900 == ((31 << 26) | (2 << 11)))
4901 rtra = insn & ((1 << 26) - (1 << 16));
4902 else if ((insn & ((31 << 26) | (31 << 16)))
4903 == ((31 << 26) | (2 << 16)))
4904 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4905 else
4906 abort ();
4907 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4908 /* add -> addi. */
4909 insn = 14 << 26;
4910 else if ((insn & (31 << 1)) == 23 << 1
4911 && ((insn & (31 << 6)) < 14 << 6
4912 || ((insn & (31 << 6)) >= 16 << 6
4913 && (insn & (31 << 6)) < 24 << 6)))
4914 /* load and store indexed -> dform. */
4915 insn = (32 | ((insn >> 6) & 31)) << 26;
4916 else if ((insn & (31 << 1)) == 21 << 1
4917 && (insn & (0x1a << 6)) == 0)
4918 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4919 insn = (((58 | ((insn >> 6) & 4)) << 26)
4920 | ((insn >> 6) & 1));
4921 else if ((insn & (31 << 1)) == 21 << 1
4922 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4923 /* lwax -> lwa. */
4924 insn = (58 << 26) | 2;
4925 else
4926 abort ();
4927 insn |= rtra;
4928 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4929 r_type = R_PPC_TPREL16_LO;
4930 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4931 /* Was PPC_TLS which sits on insn boundary, now
4932 PPC_TPREL16_LO which is at insn+2. */
4933 rel->r_offset += 2;
4934 }
4935 break;
4936
4937 case R_PPC_GOT_TLSGD16_HI:
4938 case R_PPC_GOT_TLSGD16_HA:
4939 tls_gd = TLS_TPRELGD;
4940 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4941 goto tls_gdld_hi;
4942 break;
4943
4944 case R_PPC_GOT_TLSLD16_HI:
4945 case R_PPC_GOT_TLSLD16_HA:
4946 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4947 {
4948 tls_gdld_hi:
4949 if ((tls_mask & tls_gd) != 0)
4950 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4951 + R_PPC_GOT_TPREL16);
4952 else
4953 {
4954 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4955 rel->r_offset -= 2;
4956 r_type = R_PPC_NONE;
4957 }
4958 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4959 }
4960 break;
4961
4962 case R_PPC_GOT_TLSGD16:
4963 case R_PPC_GOT_TLSGD16_LO:
4964 tls_gd = TLS_TPRELGD;
4965 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4966 goto tls_get_addr_check;
4967 break;
4968
4969 case R_PPC_GOT_TLSLD16:
4970 case R_PPC_GOT_TLSLD16_LO:
4971 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4972 {
4973 tls_get_addr_check:
4974 if (rel + 1 < relend)
4975 {
4976 enum elf_ppc_reloc_type r_type2;
4977 unsigned long r_symndx2;
4978 struct elf_link_hash_entry *h2;
87d243f1 4979 bfd_vma insn1, insn2;
7619e7c7
AM
4980 bfd_vma offset;
4981
4982 /* The next instruction should be a call to
4983 __tls_get_addr. Peek at the reloc to be sure. */
55fd94b0 4984 r_type2 = ELF32_R_TYPE (rel[1].r_info);
7619e7c7
AM
4985 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4986 if (r_symndx2 < symtab_hdr->sh_info
4987 || (r_type2 != R_PPC_REL14
4988 && r_type2 != R_PPC_REL14_BRTAKEN
4989 && r_type2 != R_PPC_REL14_BRNTAKEN
4990 && r_type2 != R_PPC_REL24
4991 && r_type2 != R_PPC_PLTREL24))
4992 break;
4993
4994 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4995 while (h2->root.type == bfd_link_hash_indirect
4996 || h2->root.type == bfd_link_hash_warning)
4997 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4998 if (h2 == NULL || h2 != htab->tls_get_addr)
4999 break;
5000
5001 /* OK, it checks out. Replace the call. */
5002 offset = rel[1].r_offset;
5003 insn1 = bfd_get_32 (output_bfd,
5004 contents + rel->r_offset - 2);
7619e7c7
AM
5005 if ((tls_mask & tls_gd) != 0)
5006 {
5007 /* IE */
5008 insn1 &= (1 << 26) - 1;
5009 insn1 |= 32 << 26; /* lwz */
5010 insn2 = 0x7c631214; /* add 3,3,2 */
5011 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
5012 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
5013 + R_PPC_GOT_TPREL16);
5014 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5015 }
5016 else
5017 {
5018 /* LE */
5019 insn1 = 0x3c620000; /* addis 3,2,0 */
5020 insn2 = 0x38630000; /* addi 3,3,0 */
5021 if (tls_gd == 0)
5022 {
5023 /* Was an LD reloc. */
5024 r_symndx = 0;
e1918d23
AM
5025 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
5026 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
5027 }
5028 r_type = R_PPC_TPREL16_HA;
5029 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5030 rel[1].r_info = ELF32_R_INFO (r_symndx,
5031 R_PPC_TPREL16_LO);
5032 rel[1].r_offset += 2;
5033 }
7619e7c7
AM
5034 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
5035 bfd_put_32 (output_bfd, insn2, contents + offset);
7619e7c7
AM
5036 if (tls_gd == 0)
5037 {
5038 /* We changed the symbol on an LD reloc. Start over
70bccea4 5039 in order to get h, sym, sec etc. right. */
7619e7c7
AM
5040 rel--;
5041 continue;
5042 }
5043 }
252b5132 5044 }
7619e7c7
AM
5045 break;
5046 }
5047
5048 /* Handle other relocations that tweak non-addend part of insn. */
5049 branch_bit = 0;
5050 switch (r_type)
5051 {
5052 default:
5053 break;
5054
5055 /* Branch taken prediction relocations. */
5056 case R_PPC_ADDR14_BRTAKEN:
5057 case R_PPC_REL14_BRTAKEN:
5058 branch_bit = BRANCH_PREDICT_BIT;
5059 /* Fall thru */
5060
4cc11e76 5061 /* Branch not taken prediction relocations. */
7619e7c7
AM
5062 case R_PPC_ADDR14_BRNTAKEN:
5063 case R_PPC_REL14_BRNTAKEN:
5064 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5065 insn &= ~BRANCH_PREDICT_BIT;
5066 insn |= branch_bit;
5067
5068 from = (rel->r_offset
5069 + input_section->output_offset
5070 + input_section->output_section->vma);
5071
5072 /* Invert 'y' bit if not the default. */
5073 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
5074 insn ^= BRANCH_PREDICT_BIT;
5075
5076 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5077 break;
252b5132
RH
5078 }
5079
7619e7c7
AM
5080 addend = rel->r_addend;
5081 tls_type = 0;
5082 howto = NULL;
55fd94b0
AM
5083 if (r_type < R_PPC_max)
5084 howto = ppc_elf_howto_table[r_type];
7619e7c7 5085 switch (r_type)
252b5132
RH
5086 {
5087 default:
7619e7c7 5088 (*_bfd_error_handler)
d003868e
AM
5089 (_("%B: unknown relocation type %d for symbol %s"),
5090 input_bfd, (int) r_type, sym_name);
252b5132
RH
5091
5092 bfd_set_error (bfd_error_bad_value);
b34976b6 5093 ret = FALSE;
252b5132
RH
5094 continue;
5095
7619e7c7
AM
5096 case R_PPC_NONE:
5097 case R_PPC_TLS:
5098 case R_PPC_EMB_MRKREF:
5099 case R_PPC_GNU_VTINHERIT:
5100 case R_PPC_GNU_VTENTRY:
7595d193
L
5101 continue;
5102
7619e7c7
AM
5103 /* GOT16 relocations. Like an ADDR16 using the symbol's
5104 address in the GOT as relocation value instead of the
5105 symbol's value itself. Also, create a GOT entry for the
5106 symbol and put the symbol value there. */
5107 case R_PPC_GOT_TLSGD16:
5108 case R_PPC_GOT_TLSGD16_LO:
5109 case R_PPC_GOT_TLSGD16_HI:
5110 case R_PPC_GOT_TLSGD16_HA:
5111 tls_type = TLS_TLS | TLS_GD;
5112 goto dogot;
5113
5114 case R_PPC_GOT_TLSLD16:
5115 case R_PPC_GOT_TLSLD16_LO:
5116 case R_PPC_GOT_TLSLD16_HI:
5117 case R_PPC_GOT_TLSLD16_HA:
5118 tls_type = TLS_TLS | TLS_LD;
5119 goto dogot;
5120
5121 case R_PPC_GOT_TPREL16:
5122 case R_PPC_GOT_TPREL16_LO:
5123 case R_PPC_GOT_TPREL16_HI:
5124 case R_PPC_GOT_TPREL16_HA:
5125 tls_type = TLS_TLS | TLS_TPREL;
5126 goto dogot;
5127
5128 case R_PPC_GOT_DTPREL16:
5129 case R_PPC_GOT_DTPREL16_LO:
5130 case R_PPC_GOT_DTPREL16_HI:
5131 case R_PPC_GOT_DTPREL16_HA:
5132 tls_type = TLS_TLS | TLS_DTPREL;
5133 goto dogot;
5134
5135 case R_PPC_GOT16:
5136 case R_PPC_GOT16_LO:
5137 case R_PPC_GOT16_HI:
5138 case R_PPC_GOT16_HA:
5139 dogot:
5140 {
5141 /* Relocation is to the entry for this symbol in the global
5142 offset table. */
5143 bfd_vma off;
5144 bfd_vma *offp;
5145 unsigned long indx;
5146
5147 if (htab->got == NULL)
5148 abort ();
5149
5150 indx = 0;
5151 if (tls_type == (TLS_TLS | TLS_LD)
d881513a 5152 && (h == NULL
f5385ebf 5153 || !h->def_dynamic))
7619e7c7
AM
5154 offp = &htab->tlsld_got.offset;
5155 else if (h != NULL)
5156 {
5157 bfd_boolean dyn;
5158 dyn = htab->elf.dynamic_sections_created;
ee05f2fe 5159 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7619e7c7
AM
5160 || (info->shared
5161 && SYMBOL_REFERENCES_LOCAL (info, h)))
5162 /* This is actually a static link, or it is a
5163 -Bsymbolic link and the symbol is defined
5164 locally, or the symbol was forced to be local
5165 because of a version file. */
5166 ;
5167 else
5168 {
5169 indx = h->dynindx;
5170 unresolved_reloc = FALSE;
5171 }
5172 offp = &h->got.offset;
5173 }
5174 else
5175 {
5176 if (local_got_offsets == NULL)
5177 abort ();
5178 offp = &local_got_offsets[r_symndx];
5179 }
5180
5181 /* The offset must always be a multiple of 4. We use the
5182 least significant bit to record whether we have already
5183 processed this entry. */
5184 off = *offp;
5185 if ((off & 1) != 0)
5186 off &= ~1;
5187 else
5188 {
70bccea4
AM
5189 unsigned int tls_m = (tls_mask
5190 & (TLS_LD | TLS_GD | TLS_DTPREL
5191 | TLS_TPREL | TLS_TPRELGD));
5192
5193 if (offp == &htab->tlsld_got.offset)
5194 tls_m = TLS_LD;
5195 else if (h == NULL
f5385ebf 5196 || !h->def_dynamic)
70bccea4
AM
5197 tls_m &= ~TLS_LD;
5198
5199 /* We might have multiple got entries for this sym.
5200 Initialize them all. */
5201 do
7619e7c7 5202 {
70bccea4
AM
5203 int tls_ty = 0;
5204
5205 if ((tls_m & TLS_LD) != 0)
7619e7c7 5206 {
70bccea4
AM
5207 tls_ty = TLS_TLS | TLS_LD;
5208 tls_m &= ~TLS_LD;
5209 }
5210 else if ((tls_m & TLS_GD) != 0)
5211 {
5212 tls_ty = TLS_TLS | TLS_GD;
5213 tls_m &= ~TLS_GD;
5214 }
5215 else if ((tls_m & TLS_DTPREL) != 0)
5216 {
5217 tls_ty = TLS_TLS | TLS_DTPREL;
5218 tls_m &= ~TLS_DTPREL;
5219 }
5220 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5221 {
5222 tls_ty = TLS_TLS | TLS_TPREL;
5223 tls_m = 0;
7619e7c7 5224 }
7619e7c7 5225
70bccea4 5226 /* Generate relocs for the dynamic linker. */
4e795f50
AM
5227 if ((info->shared || indx != 0)
5228 && (h == NULL
5229 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5230 || h->root.type != bfd_link_hash_undefweak))
7619e7c7 5231 {
70bccea4
AM
5232 outrel.r_offset = (htab->got->output_section->vma
5233 + htab->got->output_offset
5234 + off);
e515b051 5235 outrel.r_addend = 0;
70bccea4
AM
5236 if (tls_ty & (TLS_LD | TLS_GD))
5237 {
5238 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
70bccea4
AM
5239 if (tls_ty == (TLS_TLS | TLS_GD))
5240 {
5241 loc = htab->relgot->contents;
5242 loc += (htab->relgot->reloc_count++
5243 * sizeof (Elf32_External_Rela));
5244 bfd_elf32_swap_reloca_out (output_bfd,
5245 &outrel, loc);
e515b051 5246 outrel.r_offset += 4;
70bccea4
AM
5247 outrel.r_info
5248 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
70bccea4
AM
5249 }
5250 }
5251 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5252 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5253 else if (tls_ty == (TLS_TLS | TLS_TPREL))
5254 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5255 else if (indx == 0)
5256 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5257 else
5258 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
70bccea4 5259 if (indx == 0)
e515b051
AM
5260 {
5261 outrel.r_addend += relocation;
5262 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 5263 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 5264 }
70bccea4
AM
5265 loc = htab->relgot->contents;
5266 loc += (htab->relgot->reloc_count++
5267 * sizeof (Elf32_External_Rela));
5268 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7619e7c7
AM
5269 }
5270
70bccea4
AM
5271 /* Init the .got section contents if we're not
5272 emitting a reloc. */
5273 else
7619e7c7 5274 {
70bccea4
AM
5275 bfd_vma value = relocation;
5276
7b609f53
AM
5277 if (tls_ty == (TLS_TLS | TLS_LD))
5278 value = 1;
5279 else if (tls_ty != 0)
70bccea4 5280 {
e1918d23 5281 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 5282 if (tls_ty == (TLS_TLS | TLS_TPREL))
70bccea4 5283 value += DTP_OFFSET - TP_OFFSET;
70bccea4 5284
7b609f53
AM
5285 if (tls_ty == (TLS_TLS | TLS_GD))
5286 {
5287 bfd_put_32 (output_bfd, value,
5288 htab->got->contents + off + 4);
5289 value = 1;
5290 }
70bccea4 5291 }
70bccea4
AM
5292 bfd_put_32 (output_bfd, value,
5293 htab->got->contents + off);
7619e7c7 5294 }
70bccea4
AM
5295
5296 off += 4;
5297 if (tls_ty & (TLS_LD | TLS_GD))
5298 off += 4;
7619e7c7 5299 }
70bccea4
AM
5300 while (tls_m != 0);
5301
5302 off = *offp;
5303 *offp = off | 1;
7619e7c7
AM
5304 }
5305
5306 if (off >= (bfd_vma) -2)
5307 abort ();
5308
70bccea4
AM
5309 if ((tls_type & TLS_TLS) != 0)
5310 {
5311 if (tls_type != (TLS_TLS | TLS_LD))
5312 {
5313 if ((tls_mask & TLS_LD) != 0
5314 && !(h == NULL
f5385ebf 5315 || !h->def_dynamic))
70bccea4
AM
5316 off += 8;
5317 if (tls_type != (TLS_TLS | TLS_GD))
5318 {
5319 if ((tls_mask & TLS_GD) != 0)
5320 off += 8;
5321 if (tls_type != (TLS_TLS | TLS_DTPREL))
5322 {
5323 if ((tls_mask & TLS_DTPREL) != 0)
5324 off += 4;
5325 }
5326 }
5327 }
5328 }
5329
7619e7c7
AM
5330 relocation = htab->got->output_offset + off - 4;
5331
5332 /* Addends on got relocations don't make much sense.
5333 x+off@got is actually x@got+off, and since the got is
5334 generated by a hash table traversal, the value in the
5335 got at entry m+n bears little relation to the entry m. */
5336 if (addend != 0)
5337 (*_bfd_error_handler)
d003868e
AM
5338 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
5339 input_bfd,
5340 input_section,
7619e7c7 5341 (long) rel->r_offset,
7b609f53 5342 howto->name,
7619e7c7
AM
5343 sym_name);
5344 }
70bccea4 5345 break;
7619e7c7 5346
252b5132 5347 /* Relocations that need no special processing. */
7619e7c7 5348 case R_PPC_LOCAL24PC:
252b5132
RH
5349 /* It makes no sense to point a local relocation
5350 at a symbol not in this object. */
7b609f53 5351 if (unresolved_reloc)
252b5132 5352 {
464e1740
ILT
5353 if (! (*info->callbacks->undefined_symbol) (info,
5354 h->root.root.string,
5355 input_bfd,
5356 input_section,
5cc7c785 5357 rel->r_offset,
b34976b6
AM
5358 TRUE))
5359 return FALSE;
252b5132
RH
5360 continue;
5361 }
5362 break;
5363
7619e7c7
AM
5364 case R_PPC_DTPREL16:
5365 case R_PPC_DTPREL16_LO:
5366 case R_PPC_DTPREL16_HI:
5367 case R_PPC_DTPREL16_HA:
e1918d23 5368 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
5369 break;
5370
70bccea4
AM
5371 /* Relocations that may need to be propagated if this is a shared
5372 object. */
7619e7c7
AM
5373 case R_PPC_TPREL16:
5374 case R_PPC_TPREL16_LO:
5375 case R_PPC_TPREL16_HI:
5376 case R_PPC_TPREL16_HA:
e1918d23 5377 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7619e7c7
AM
5378 /* The TPREL16 relocs shouldn't really be used in shared
5379 libs as they will result in DT_TEXTREL being set, but
5380 support them anyway. */
5381 goto dodyn;
5382
5383 case R_PPC_TPREL32:
e1918d23 5384 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7619e7c7
AM
5385 goto dodyn;
5386
5387 case R_PPC_DTPREL32:
e1918d23 5388 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7619e7c7
AM
5389 goto dodyn;
5390
e515b051
AM
5391 case R_PPC_DTPMOD32:
5392 relocation = 1;
5393 addend = 0;
5394 goto dodyn;
5395
7619e7c7
AM
5396 case R_PPC_REL24:
5397 case R_PPC_REL32:
5398 case R_PPC_REL14:
5399 case R_PPC_REL14_BRTAKEN:
5400 case R_PPC_REL14_BRNTAKEN:
252b5132
RH
5401 /* If these relocations are not to a named symbol, they can be
5402 handled right here, no need to bother the dynamic linker. */
f6c52c13
AM
5403 if (SYMBOL_REFERENCES_LOCAL (info, h)
5404 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
252b5132 5405 break;
70bccea4 5406 /* fall through */
252b5132 5407
70bccea4
AM
5408 /* Relocations that always need to be propagated if this is a shared
5409 object. */
7619e7c7
AM
5410 case R_PPC_ADDR32:
5411 case R_PPC_ADDR24:
5412 case R_PPC_ADDR16:
5413 case R_PPC_ADDR16_LO:
5414 case R_PPC_ADDR16_HI:
5415 case R_PPC_ADDR16_HA:
5416 case R_PPC_ADDR14:
5417 case R_PPC_ADDR14_BRTAKEN:
5418 case R_PPC_ADDR14_BRNTAKEN:
5419 case R_PPC_UADDR32:
5420 case R_PPC_UADDR16:
ee05f2fe
AM
5421 /* r_symndx will be zero only for relocs against symbols
5422 from removed linkonce sections, or sections discarded by
5423 a linker script. */
7619e7c7 5424 dodyn:
ee05f2fe
AM
5425 if (r_symndx == 0)
5426 break;
5427 /* Fall thru. */
5428
5429 if ((info->shared
4e795f50
AM
5430 && (h == NULL
5431 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5432 || h->root.type != bfd_link_hash_undefweak)
ee05f2fe 5433 && (MUST_BE_DYN_RELOC (r_type)
f6c52c13 5434 || !SYMBOL_CALLS_LOCAL (info, h)))
ee05f2fe
AM
5435 || (ELIMINATE_COPY_RELOCS
5436 && !info->shared
5437 && (input_section->flags & SEC_ALLOC) != 0
5438 && h != NULL
5439 && h->dynindx != -1
f5385ebf
AM
5440 && !h->non_got_ref
5441 && h->def_dynamic
5442 && !h->def_regular))
252b5132 5443 {
0bb2d96a 5444 int skip;
252b5132
RH
5445
5446#ifdef DEBUG
55fd94b0
AM
5447 fprintf (stderr, "ppc_elf_relocate_section needs to "
5448 "create relocation for %s\n",
70bccea4
AM
5449 (h && h->root.root.string
5450 ? h->root.root.string : "<unknown>"));
252b5132
RH
5451#endif
5452
5453 /* When generating a shared object, these relocations
70bccea4
AM
5454 are copied into the output file to be resolved at run
5455 time. */
252b5132
RH
5456 if (sreloc == NULL)
5457 {
5458 const char *name;
5459
5460 name = (bfd_elf_string_from_elf_section
5461 (input_bfd,
5462 elf_elfheader (input_bfd)->e_shstrndx,
5463 elf_section_data (input_section)->rel_hdr.sh_name));
5464 if (name == NULL)
b34976b6 5465 return FALSE;
252b5132
RH
5466
5467 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5468 && strcmp (bfd_get_section_name (input_bfd,
5469 input_section),
5470 name + 5) == 0);
5471
7619e7c7 5472 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
252b5132
RH
5473 BFD_ASSERT (sreloc != NULL);
5474 }
5475
0bb2d96a 5476 skip = 0;
252b5132 5477
c629eae0
JJ
5478 outrel.r_offset =
5479 _bfd_elf_section_offset (output_bfd, info, input_section,
5480 rel->r_offset);
0bb2d96a
JJ
5481 if (outrel.r_offset == (bfd_vma) -1
5482 || outrel.r_offset == (bfd_vma) -2)
5483 skip = (int) outrel.r_offset;
252b5132
RH
5484 outrel.r_offset += (input_section->output_section->vma
5485 + input_section->output_offset);
5486
5487 if (skip)
5488 memset (&outrel, 0, sizeof outrel);
f6c52c13 5489 else if (!SYMBOL_REFERENCES_LOCAL (info, h))
252b5132 5490 {
7619e7c7 5491 unresolved_reloc = FALSE;
252b5132
RH
5492 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5493 outrel.r_addend = rel->r_addend;
5494 }
5495 else
5496 {
47388f4c
AM
5497 outrel.r_addend = relocation + rel->r_addend;
5498
252b5132 5499 if (r_type == R_PPC_ADDR32)
47388f4c 5500 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
252b5132
RH
5501 else
5502 {
5503 long indx;
5504
47388f4c 5505 if (bfd_is_abs_section (sec))
252b5132
RH
5506 indx = 0;
5507 else if (sec == NULL || sec->owner == NULL)
5508 {
5509 bfd_set_error (bfd_error_bad_value);
b34976b6 5510 return FALSE;
252b5132
RH
5511 }
5512 else
5513 {
5514 asection *osec;
5515
47388f4c
AM
5516 /* We are turning this relocation into one
5517 against a section symbol. It would be
5518 proper to subtract the symbol's value,
5519 osec->vma, from the emitted reloc addend,
5520 but ld.so expects buggy relocs. */
252b5132
RH
5521 osec = sec->output_section;
5522 indx = elf_section_data (osec)->dynindx;
8da6118f 5523 BFD_ASSERT (indx > 0);
252b5132
RH
5524#ifdef DEBUG
5525 if (indx <= 0)
55fd94b0
AM
5526 printf ("indx=%d section=%s flags=%08x name=%s\n",
5527 indx, osec->name, osec->flags,
5528 h->root.root.string);
252b5132
RH
5529#endif
5530 }
5531
5532 outrel.r_info = ELF32_R_INFO (indx, r_type);
252b5132
RH
5533 }
5534 }
5535
947216bf
AM
5536 loc = sreloc->contents;
5537 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5538 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132 5539
2243deae 5540 if (skip == -1)
252b5132 5541 continue;
2243deae
L
5542
5543 /* This reloc will be computed at runtime. We clear the memory
5544 so that it contains predictable value. */
5545 if (! skip
5546 && ((input_section->flags & SEC_ALLOC) != 0
5547 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5548 {
5549 relocation = howto->pc_relative ? outrel.r_offset : 0;
5550 addend = 0;
5551 break;
5552 }
252b5132 5553 }
252b5132
RH
5554 break;
5555
b4a38de6
AM
5556 case R_PPC_RELAX32PC_PLT:
5557 case R_PPC_RELAX32_PLT:
5558 BFD_ASSERT (h != NULL
5559 && h->plt.offset != (bfd_vma) -1
5560 && htab->plt != NULL);
5561
5562 relocation = (htab->plt->output_section->vma
5563 + htab->plt->output_offset
5564 + h->plt.offset);
5565 if (r_type == R_PPC_RELAX32_PLT)
5566 goto relax32;
5567 /* Fall thru */
5568
f31a141e
AM
5569 case R_PPC_RELAX32PC:
5570 relocation -= (input_section->output_section->vma
5571 + input_section->output_offset
5572 + rel->r_offset - 4);
5573 /* Fall thru */
b4a38de6 5574
deaaf2f3 5575 case R_PPC_RELAX32:
b4a38de6 5576 relax32:
9abc968f
AM
5577 {
5578 unsigned long t0;
5579 unsigned long t1;
5580
5581 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
5582 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
5583
5584 /* We're clearing the bits for R_PPC_ADDR16_HA
5585 and R_PPC_ADDR16_LO here. */
5586 t0 &= ~0xffff;
5587 t1 &= ~0xffff;
5588
5589 /* t0 is HA, t1 is LO */
5590 relocation += addend;
5591 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
5592 t1 |= relocation & 0xffff;
5593
5594 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
5595 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
5596 }
8517fae7 5597 continue;
deaaf2f3 5598
70bccea4 5599 /* Indirect .sdata relocation. */
7619e7c7
AM
5600 case R_PPC_EMB_SDAI16:
5601 BFD_ASSERT (htab->sdata != NULL);
5602 relocation
3dab13f6
AM
5603 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5604 htab->sdata, h, relocation,
2bb04cf2 5605 rel);
252b5132
RH
5606 break;
5607
70bccea4 5608 /* Indirect .sdata2 relocation. */
7619e7c7
AM
5609 case R_PPC_EMB_SDA2I16:
5610 BFD_ASSERT (htab->sdata2 != NULL);
5611 relocation
3dab13f6
AM
5612 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5613 htab->sdata2, h, relocation,
2bb04cf2 5614 rel);
252b5132
RH
5615 break;
5616
70bccea4
AM
5617 /* Handle the TOC16 reloc. We want to use the offset within the .got
5618 section, not the actual VMA. This is appropriate when generating
5619 an embedded ELF object, for which the .got section acts like the
5620 AIX .toc section. */
7619e7c7 5621 case R_PPC_TOC16: /* phony GOT16 relocations */
55fd94b0 5622 BFD_ASSERT (sec != NULL);
252b5132
RH
5623 BFD_ASSERT (bfd_is_und_section (sec)
5624 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
f12123c0 5625 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
252b5132 5626
70bccea4 5627 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
252b5132
RH
5628 break;
5629
7619e7c7 5630 case R_PPC_PLTREL24:
252b5132 5631 /* Relocation is to the entry for this symbol in the
70bccea4 5632 procedure linkage table. */
252b5132
RH
5633 BFD_ASSERT (h != NULL);
5634
5635 if (h->plt.offset == (bfd_vma) -1
7619e7c7 5636 || htab->plt == NULL)
252b5132
RH
5637 {
5638 /* We didn't make a PLT entry for this symbol. This
70bccea4
AM
5639 happens when statically linking PIC code, or when
5640 using -Bsymbolic. */
252b5132
RH
5641 break;
5642 }
5643
7619e7c7
AM
5644 unresolved_reloc = FALSE;
5645 relocation = (htab->plt->output_section->vma
5646 + htab->plt->output_offset
252b5132 5647 + h->plt.offset);
8da6118f 5648 break;
252b5132 5649
70bccea4 5650 /* Relocate against _SDA_BASE_. */
7619e7c7 5651 case R_PPC_SDAREL16:
252b5132
RH
5652 {
5653 const char *name;
7619e7c7 5654 const struct elf_link_hash_entry *sh;
252b5132 5655
55fd94b0 5656 BFD_ASSERT (sec != NULL);
252b5132 5657 name = bfd_get_section_name (abfd, sec->output_section);
5f819128
AM
5658 if (! ((strncmp (name, ".sdata", 6) == 0
5659 && (name[6] == 0 || name[6] == '.'))
5660 || (strncmp (name, ".sbss", 5) == 0
5661 && (name[5] == 0 || name[5] == '.'))))
252b5132 5662 {
55fd94b0 5663 (*_bfd_error_handler)
d003868e 5664 (_("%B: the target (%s) of a %s relocation is "
55fd94b0 5665 "in the wrong output section (%s)"),
d003868e 5666 input_bfd,
55fd94b0
AM
5667 sym_name,
5668 howto->name,
5669 name);
252b5132 5670 }
7619e7c7
AM
5671 sh = htab->sdata->sym_hash;
5672 addend -= (sh->root.u.def.value
5673 + sh->root.u.def.section->output_section->vma
5674 + sh->root.u.def.section->output_offset);
252b5132
RH
5675 }
5676 break;
5677
70bccea4 5678 /* Relocate against _SDA2_BASE_. */
7619e7c7 5679 case R_PPC_EMB_SDA2REL:
252b5132
RH
5680 {
5681 const char *name;
7619e7c7 5682 const struct elf_link_hash_entry *sh;
252b5132 5683
55fd94b0 5684 BFD_ASSERT (sec != NULL);
252b5132 5685 name = bfd_get_section_name (abfd, sec->output_section);
5f819128
AM
5686 if (! (strncmp (name, ".sdata2", 7) == 0
5687 || strncmp (name, ".sbss2", 6) == 0))
252b5132 5688 {
55fd94b0 5689 (*_bfd_error_handler)
d003868e 5690 (_("%B: the target (%s) of a %s relocation is "
55fd94b0 5691 "in the wrong output section (%s)"),
d003868e 5692 input_bfd,
55fd94b0
AM
5693 sym_name,
5694 howto->name,
5695 name);
c3668558 5696
252b5132 5697 bfd_set_error (bfd_error_bad_value);
b34976b6 5698 ret = FALSE;
252b5132
RH
5699 continue;
5700 }
7619e7c7
AM
5701 sh = htab->sdata2->sym_hash;
5702 addend -= (sh->root.u.def.value
5703 + sh->root.u.def.section->output_section->vma
5704 + sh->root.u.def.section->output_offset);
252b5132
RH
5705 }
5706 break;
5707
70bccea4 5708 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
7619e7c7
AM
5709 case R_PPC_EMB_SDA21:
5710 case R_PPC_EMB_RELSDA:
252b5132
RH
5711 {
5712 const char *name;
7619e7c7 5713 const struct elf_link_hash_entry *sh;
252b5132
RH
5714 int reg;
5715
55fd94b0 5716 BFD_ASSERT (sec != NULL);
252b5132 5717 name = bfd_get_section_name (abfd, sec->output_section);
b34976b6 5718 if (((strncmp (name, ".sdata", 6) == 0
5f819128
AM
5719 && (name[6] == 0 || name[6] == '.'))
5720 || (strncmp (name, ".sbss", 5) == 0
5721 && (name[5] == 0 || name[5] == '.'))))
252b5132
RH
5722 {
5723 reg = 13;
7619e7c7
AM
5724 sh = htab->sdata->sym_hash;
5725 addend -= (sh->root.u.def.value
5726 + sh->root.u.def.section->output_section->vma
5727 + sh->root.u.def.section->output_offset);
252b5132
RH
5728 }
5729
5f819128
AM
5730 else if (strncmp (name, ".sdata2", 7) == 0
5731 || strncmp (name, ".sbss2", 6) == 0)
252b5132
RH
5732 {
5733 reg = 2;
7619e7c7
AM
5734 sh = htab->sdata2->sym_hash;
5735 addend -= (sh->root.u.def.value
5736 + sh->root.u.def.section->output_section->vma
5737 + sh->root.u.def.section->output_offset);
252b5132
RH
5738 }
5739
8da6118f
KH
5740 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5741 || strcmp (name, ".PPC.EMB.sbss0") == 0)
252b5132
RH
5742 {
5743 reg = 0;
5744 }
5745
5746 else
5747 {
55fd94b0 5748 (*_bfd_error_handler)
d003868e 5749 (_("%B: the target (%s) of a %s relocation is "
55fd94b0 5750 "in the wrong output section (%s)"),
d003868e 5751 input_bfd,
55fd94b0
AM
5752 sym_name,
5753 howto->name,
5754 name);
252b5132
RH
5755
5756 bfd_set_error (bfd_error_bad_value);
b34976b6 5757 ret = FALSE;
252b5132
RH
5758 continue;
5759 }
5760
5761 if (r_type == R_PPC_EMB_SDA21)
5762 { /* fill in register field */
7619e7c7 5763 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
252b5132 5764 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
7619e7c7 5765 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
252b5132
RH
5766 }
5767 }
5768 break;
5769
70bccea4 5770 /* Relocate against the beginning of the section. */
7619e7c7
AM
5771 case R_PPC_SECTOFF:
5772 case R_PPC_SECTOFF_LO:
5773 case R_PPC_SECTOFF_HI:
5774 case R_PPC_SECTOFF_HA:
55fd94b0 5775 BFD_ASSERT (sec != NULL);
252b5132 5776 addend -= sec->output_section->vma;
252b5132
RH
5777 break;
5778
70bccea4 5779 /* Negative relocations. */
7619e7c7
AM
5780 case R_PPC_EMB_NADDR32:
5781 case R_PPC_EMB_NADDR16:
5782 case R_PPC_EMB_NADDR16_LO:
5783 case R_PPC_EMB_NADDR16_HI:
5784 case R_PPC_EMB_NADDR16_HA:
8da6118f 5785 addend -= 2 * relocation;
252b5132
RH
5786 break;
5787
7619e7c7
AM
5788 case R_PPC_COPY:
5789 case R_PPC_GLOB_DAT:
5790 case R_PPC_JMP_SLOT:
5791 case R_PPC_RELATIVE:
5792 case R_PPC_PLT32:
5793 case R_PPC_PLTREL32:
5794 case R_PPC_PLT16_LO:
5795 case R_PPC_PLT16_HI:
5796 case R_PPC_PLT16_HA:
5797 case R_PPC_ADDR30:
5798 case R_PPC_EMB_RELSEC16:
5799 case R_PPC_EMB_RELST_LO:
5800 case R_PPC_EMB_RELST_HI:
5801 case R_PPC_EMB_RELST_HA:
5802 case R_PPC_EMB_BIT_FLD:
5803 (*_bfd_error_handler)
d003868e
AM
5804 (_("%B: relocation %s is not yet supported for symbol %s."),
5805 input_bfd,
7619e7c7
AM
5806 howto->name,
5807 sym_name);
252b5132
RH
5808
5809 bfd_set_error (bfd_error_invalid_operation);
b34976b6 5810 ret = FALSE;
252b5132 5811 continue;
7619e7c7 5812 }
252b5132 5813
7619e7c7
AM
5814 /* Do any further special processing. */
5815 switch (r_type)
5816 {
5817 default:
5818 break;
5819
5820 case R_PPC_ADDR16_HA:
5821 case R_PPC_GOT16_HA:
5822 case R_PPC_PLT16_HA:
5823 case R_PPC_SECTOFF_HA:
5824 case R_PPC_TPREL16_HA:
5825 case R_PPC_DTPREL16_HA:
5826 case R_PPC_GOT_TLSGD16_HA:
5827 case R_PPC_GOT_TLSLD16_HA:
5828 case R_PPC_GOT_TPREL16_HA:
5829 case R_PPC_GOT_DTPREL16_HA:
5830 case R_PPC_EMB_NADDR16_HA:
5831 case R_PPC_EMB_RELST_HA:
5832 /* It's just possible that this symbol is a weak symbol
7b609f53 5833 that's not actually defined anywhere. In that case,
7619e7c7
AM
5834 'sec' would be NULL, and we should leave the symbol
5835 alone (it will be set to zero elsewhere in the link). */
5836 if (sec != NULL)
e515b051
AM
5837 /* Add 0x10000 if sign bit in 0:15 is set.
5838 Bits 0:15 are not used. */
5839 addend += 0x8000;
7619e7c7 5840 break;
252b5132
RH
5841 }
5842
252b5132 5843#ifdef DEBUG
55fd94b0
AM
5844 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5845 "offset = %ld, addend = %ld\n",
252b5132 5846 howto->name,
8da6118f 5847 (int) r_type,
252b5132
RH
5848 sym_name,
5849 r_symndx,
7619e7c7 5850 (long) rel->r_offset,
8da6118f 5851 (long) addend);
252b5132
RH
5852#endif
5853
7619e7c7
AM
5854 if (unresolved_reloc
5855 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 5856 && h->def_dynamic))
7619e7c7
AM
5857 {
5858 (*_bfd_error_handler)
d003868e
AM
5859 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5860 input_bfd,
5861 input_section,
7619e7c7 5862 (long) rel->r_offset,
7b609f53 5863 howto->name,
7619e7c7
AM
5864 sym_name);
5865 ret = FALSE;
5866 }
5867
252b5132
RH
5868 r = _bfd_final_link_relocate (howto,
5869 input_bfd,
5870 input_section,
5871 contents,
7619e7c7 5872 rel->r_offset,
252b5132
RH
5873 relocation,
5874 addend);
5875
7619e7c7 5876 if (r != bfd_reloc_ok)
252b5132 5877 {
7619e7c7
AM
5878 if (sym_name == NULL)
5879 sym_name = "(null)";
5880 if (r == bfd_reloc_overflow)
252b5132 5881 {
7619e7c7
AM
5882 if (warned)
5883 continue;
5884 if (h != NULL
5885 && h->root.type == bfd_link_hash_undefweak
dc1bc0c9
RH
5886 && howto->pc_relative)
5887 {
5888 /* Assume this is a call protected by other code that
5889 detect the symbol is undefined. If this is the case,
5890 we can safely ignore the overflow. If not, the
5891 program is hosed anyway, and a little warning isn't
5892 going to help. */
252b5132 5893
dc1bc0c9
RH
5894 continue;
5895 }
252b5132 5896
7619e7c7 5897 if (! (*info->callbacks->reloc_overflow) (info,
dfeffb9f 5898 (h ? &h->root : NULL),
7619e7c7
AM
5899 sym_name,
5900 howto->name,
5901 rel->r_addend,
5902 input_bfd,
5903 input_section,
5904 rel->r_offset))
5905 return FALSE;
dc1bc0c9
RH
5906 }
5907 else
5908 {
7619e7c7 5909 (*_bfd_error_handler)
d003868e
AM
5910 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
5911 input_bfd, input_section,
7b609f53 5912 (long) rel->r_offset, howto->name, sym_name, (int) r);
7619e7c7 5913 ret = FALSE;
252b5132
RH
5914 }
5915 }
5916 }
5917
252b5132
RH
5918#ifdef DEBUG
5919 fprintf (stderr, "\n");
5920#endif
5921
5922 return ret;
5923}
252b5132 5924\f
25dbc73a
AM
5925/* Finish up dynamic symbol handling. We set the contents of various
5926 dynamic sections here. */
c5fccbec 5927
b34976b6 5928static bfd_boolean
25dbc73a
AM
5929ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
5930 struct bfd_link_info *info,
5931 struct elf_link_hash_entry *h,
5932 Elf_Internal_Sym *sym)
e1a9cb8e 5933{
25dbc73a 5934 struct ppc_elf_link_hash_table *htab;
e1a9cb8e 5935
25dbc73a
AM
5936#ifdef DEBUG
5937 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
5938 h->root.root.string);
5939#endif
e1a9cb8e 5940
25dbc73a
AM
5941 htab = ppc_elf_hash_table (info);
5942 BFD_ASSERT (htab->elf.dynobj != NULL);
e1a9cb8e 5943
25dbc73a 5944 if (h->plt.offset != (bfd_vma) -1)
e1a9cb8e 5945 {
25dbc73a
AM
5946 Elf_Internal_Rela rela;
5947 bfd_byte *loc;
5948 bfd_vma reloc_index;
e1a9cb8e 5949
25dbc73a
AM
5950#ifdef DEBUG
5951 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
5952#endif
e1a9cb8e 5953
25dbc73a
AM
5954 /* This symbol has an entry in the procedure linkage table. Set
5955 it up. */
e1a9cb8e 5956
25dbc73a
AM
5957 BFD_ASSERT (h->dynindx != -1);
5958 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
e1a9cb8e 5959
25dbc73a
AM
5960 /* We don't need to fill in the .plt. The ppc dynamic linker
5961 will fill it in. */
e1a9cb8e 5962
25dbc73a
AM
5963 /* Fill in the entry in the .rela.plt section. */
5964 rela.r_offset = (htab->plt->output_section->vma
5965 + htab->plt->output_offset
5966 + h->plt.offset);
5967 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
5968 rela.r_addend = 0;
e1a9cb8e 5969
25dbc73a
AM
5970 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
5971 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
5972 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
5973 loc = (htab->relplt->contents
5974 + reloc_index * sizeof (Elf32_External_Rela));
5975 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
e1a9cb8e 5976
25dbc73a
AM
5977 if (!h->def_regular)
5978 {
5979 /* Mark the symbol as undefined, rather than as defined in
5980 the .plt section. Leave the value alone. */
5981 sym->st_shndx = SHN_UNDEF;
5982 /* If the symbol is weak, we do need to clear the value.
5983 Otherwise, the PLT entry would provide a definition for
5984 the symbol even if the symbol wasn't defined anywhere,
5985 and so the symbol would never be NULL. */
5986 if (!h->ref_regular_nonweak)
5987 sym->st_value = 0;
5988 }
e1a9cb8e
NC
5989 }
5990
25dbc73a
AM
5991 if (h->needs_copy)
5992 {
5993 asection *s;
5994 Elf_Internal_Rela rela;
5995 bfd_byte *loc;
e1a9cb8e 5996
25dbc73a 5997 /* This symbols needs a copy reloc. Set it up. */
e1a9cb8e 5998
25dbc73a
AM
5999#ifdef DEBUG
6000 fprintf (stderr, ", copy");
6001#endif
e1a9cb8e 6002
25dbc73a 6003 BFD_ASSERT (h->dynindx != -1);
86bbe32f 6004
25dbc73a
AM
6005 if (h->size <= elf_gp_size (htab->elf.dynobj))
6006 s = htab->relsbss;
6007 else
6008 s = htab->relbss;
6009 BFD_ASSERT (s != NULL);
e1a9cb8e 6010
25dbc73a
AM
6011 rela.r_offset = (h->root.u.def.value
6012 + h->root.u.def.section->output_section->vma
6013 + h->root.u.def.section->output_offset);
6014 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
6015 rela.r_addend = 0;
6016 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6017 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6018 }
e1a9cb8e 6019
25dbc73a
AM
6020#ifdef DEBUG
6021 fprintf (stderr, "\n");
6022#endif
e1a9cb8e 6023
25dbc73a
AM
6024 /* Mark some specially defined symbols as absolute. */
6025 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6026 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
6027 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
6028 sym->st_shndx = SHN_ABS;
e1a9cb8e 6029
25dbc73a
AM
6030 return TRUE;
6031}
6032\f
6033static enum elf_reloc_type_class
6034ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
e1a9cb8e 6035{
25dbc73a
AM
6036 switch (ELF32_R_TYPE (rela->r_info))
6037 {
6038 case R_PPC_RELATIVE:
6039 return reloc_class_relative;
6040 case R_PPC_REL24:
6041 case R_PPC_ADDR24:
6042 case R_PPC_JMP_SLOT:
6043 return reloc_class_plt;
6044 case R_PPC_COPY:
6045 return reloc_class_copy;
6046 default:
6047 return reloc_class_normal;
6048 }
e1a9cb8e 6049}
25dbc73a
AM
6050\f
6051/* Finish up the dynamic sections. */
e1a9cb8e 6052
25dbc73a
AM
6053static bfd_boolean
6054ppc_elf_finish_dynamic_sections (bfd *output_bfd,
6055 struct bfd_link_info *info)
e1a9cb8e 6056{
25dbc73a
AM
6057 asection *sdyn;
6058 struct ppc_elf_link_hash_table *htab;
e1a9cb8e 6059
25dbc73a
AM
6060#ifdef DEBUG
6061 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
6062#endif
6b0817e5 6063
25dbc73a
AM
6064 htab = ppc_elf_hash_table (info);
6065 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
e1a9cb8e 6066
25dbc73a 6067 if (htab->elf.dynamic_sections_created)
e1a9cb8e 6068 {
25dbc73a 6069 Elf32_External_Dyn *dyncon, *dynconend;
e1a9cb8e 6070
25dbc73a 6071 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
86bbe32f 6072
25dbc73a
AM
6073 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6074 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6075 for (; dyncon < dynconend; dyncon++)
6076 {
6077 Elf_Internal_Dyn dyn;
6078 asection *s;
e1a9cb8e 6079
25dbc73a 6080 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
86bbe32f 6081
25dbc73a
AM
6082 switch (dyn.d_tag)
6083 {
6084 case DT_PLTGOT:
6085 s = htab->plt;
6086 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6087 break;
86bbe32f 6088
25dbc73a
AM
6089 case DT_PLTRELSZ:
6090 dyn.d_un.d_val = htab->relplt->size;
6091 break;
e1a9cb8e 6092
25dbc73a
AM
6093 case DT_JMPREL:
6094 s = htab->relplt;
6095 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6096 break;
2f89ff8d 6097
25dbc73a
AM
6098 default:
6099 continue;
6100 }
4c45e5c9 6101
25dbc73a
AM
6102 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6103 }
6104 }
4c45e5c9 6105
25dbc73a
AM
6106 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
6107 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
6108 if (htab->got)
6109 {
6110 unsigned char *contents = htab->got->contents;
6111 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
2f89ff8d 6112
25dbc73a
AM
6113 if (sdyn == NULL)
6114 bfd_put_32 (output_bfd, 0, contents + 4);
6115 else
6116 bfd_put_32 (output_bfd,
6117 sdyn->output_section->vma + sdyn->output_offset,
6118 contents + 4);
6119
6120 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
6121 }
6122
6123 return TRUE;
6124}
e1a9cb8e 6125\f
252b5132
RH
6126#define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
6127#define TARGET_LITTLE_NAME "elf32-powerpcle"
6128#define TARGET_BIG_SYM bfd_elf32_powerpc_vec
6129#define TARGET_BIG_NAME "elf32-powerpc"
6130#define ELF_ARCH bfd_arch_powerpc
6131#define ELF_MACHINE_CODE EM_PPC
d0facd1b
NC
6132#ifdef __QNXTARGET__
6133#define ELF_MAXPAGESIZE 0x1000
6134#else
252b5132 6135#define ELF_MAXPAGESIZE 0x10000
d0facd1b 6136#endif
b1342370 6137#define ELF_MINPAGESIZE 0x1000
252b5132
RH
6138#define elf_info_to_howto ppc_elf_info_to_howto
6139
6140#ifdef EM_CYGNUS_POWERPC
6141#define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6142#endif
6143
6144#ifdef EM_PPC_OLD
6145#define ELF_MACHINE_ALT2 EM_PPC_OLD
6146#endif
6147
6148#define elf_backend_plt_not_loaded 1
6149#define elf_backend_got_symbol_offset 4
6150#define elf_backend_can_gc_sections 1
51b64d56 6151#define elf_backend_can_refcount 1
252b5132 6152#define elf_backend_got_header_size 12
b491616a 6153#define elf_backend_rela_normal 1
252b5132 6154
43c40ab2 6155#define bfd_elf32_mkobject ppc_elf_mkobject
252b5132 6156#define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
70bccea4 6157#define bfd_elf32_bfd_relax_section ppc_elf_relax_section
252b5132
RH
6158#define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
6159#define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
70bccea4 6160#define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
252b5132 6161
feee612b 6162#define elf_backend_object_p ppc_elf_object_p
252b5132
RH
6163#define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
6164#define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
6165#define elf_backend_section_from_shdr ppc_elf_section_from_shdr
6166#define elf_backend_relocate_section ppc_elf_relocate_section
6167#define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
6168#define elf_backend_check_relocs ppc_elf_check_relocs
7fce784e 6169#define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
252b5132
RH
6170#define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
6171#define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
6172#define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
6173#define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
6174#define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
6175#define elf_backend_fake_sections ppc_elf_fake_sections
6176#define elf_backend_additional_program_headers ppc_elf_additional_program_headers
c5fccbec
DJ
6177#define elf_backend_grok_prstatus ppc_elf_grok_prstatus
6178#define elf_backend_grok_psinfo ppc_elf_grok_psinfo
29c2fb7c 6179#define elf_backend_reloc_type_class ppc_elf_reloc_type_class
70bccea4
AM
6180#define elf_backend_begin_write_processing ppc_elf_begin_write_processing
6181#define elf_backend_final_write_processing ppc_elf_final_write_processing
6182#define elf_backend_write_section ppc_elf_write_section
2f89ff8d 6183#define elf_backend_special_sections ppc_elf_special_sections
4c45e5c9 6184#define elf_backend_plt_sym_val ppc_elf_plt_sym_val
252b5132
RH
6185
6186#include "elf32-target.h"