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