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