bfd/
[deliverable/binutils-gdb.git] / bfd / elf32-ppc.c
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
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 3 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23
24 /* This file is based on a preliminary PowerPC ELF ABI. The
25 information may not match the final PowerPC ELF ABI. It includes
26 suggestions from the in-progress Embedded PowerPC ABI, and that
27 information may also not match. */
28
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc.h"
36 #include "elf32-ppc.h"
37 #include "elf-vxworks.h"
38
39 /* RELA relocations are used here. */
40
41 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type ppc_elf_unhandled_reloc
44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45
46 /* Branch prediction bit for branch taken relocs. */
47 #define BRANCH_PREDICT_BIT 0x200000
48 /* Mask to set RA in memory instructions. */
49 #define RA_REGISTER_MASK 0x001f0000
50 /* Value to shift register by to insert RA. */
51 #define RA_REGISTER_SHIFT 16
52
53 /* The name of the dynamic interpreter. This is put in the .interp
54 section. */
55 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
56
57 /* For old-style PLT. */
58 /* The number of single-slot PLT entries (the rest use two slots). */
59 #define PLT_NUM_SINGLE_ENTRIES 8192
60
61 /* For new-style .glink and .plt. */
62 #define GLINK_PLTRESOLVE 16*4
63 #define GLINK_ENTRY_SIZE 4*4
64 #define TLS_GET_ADDR_GLINK_SIZE 12*4
65
66 /* VxWorks uses its own plt layout, filled in by the static linker. */
67
68 /* The standard VxWorks PLT entry. */
69 #define VXWORKS_PLT_ENTRY_SIZE 32
70 static const bfd_vma ppc_elf_vxworks_plt_entry
71 [VXWORKS_PLT_ENTRY_SIZE / 4] =
72 {
73 0x3d800000, /* lis r12,0 */
74 0x818c0000, /* lwz r12,0(r12) */
75 0x7d8903a6, /* mtctr r12 */
76 0x4e800420, /* bctr */
77 0x39600000, /* li r11,0 */
78 0x48000000, /* b 14 <.PLT0resolve+0x4> */
79 0x60000000, /* nop */
80 0x60000000, /* nop */
81 };
82 static const bfd_vma ppc_elf_vxworks_pic_plt_entry
83 [VXWORKS_PLT_ENTRY_SIZE / 4] =
84 {
85 0x3d9e0000, /* addis r12,r30,0 */
86 0x818c0000, /* lwz r12,0(r12) */
87 0x7d8903a6, /* mtctr r12 */
88 0x4e800420, /* bctr */
89 0x39600000, /* li r11,0 */
90 0x48000000, /* b 14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
91 0x60000000, /* nop */
92 0x60000000, /* nop */
93 };
94
95 /* The initial VxWorks PLT entry. */
96 #define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
97 static const bfd_vma ppc_elf_vxworks_plt0_entry
98 [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
99 {
100 0x3d800000, /* lis r12,0 */
101 0x398c0000, /* addi r12,r12,0 */
102 0x800c0008, /* lwz r0,8(r12) */
103 0x7c0903a6, /* mtctr r0 */
104 0x818c0004, /* lwz r12,4(r12) */
105 0x4e800420, /* bctr */
106 0x60000000, /* nop */
107 0x60000000, /* nop */
108 };
109 static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
110 [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
111 {
112 0x819e0008, /* lwz r12,8(r30) */
113 0x7d8903a6, /* mtctr r12 */
114 0x819e0004, /* lwz r12,4(r30) */
115 0x4e800420, /* bctr */
116 0x60000000, /* nop */
117 0x60000000, /* nop */
118 0x60000000, /* nop */
119 0x60000000, /* nop */
120 };
121
122 /* For executables, we have some additional relocations in
123 .rela.plt.unloaded, for the kernel loader. */
124
125 /* The number of non-JMP_SLOT relocations per PLT0 slot. */
126 #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
127 /* The number of relocations in the PLTResolve slot. */
128 #define VXWORKS_PLTRESOLVE_RELOCS 2
129 /* The number of relocations in the PLTResolve slot when when creating
130 a shared library. */
131 #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
132
133 /* Some instructions. */
134 #define ADDIS_11_11 0x3d6b0000
135 #define ADDIS_11_30 0x3d7e0000
136 #define ADDIS_12_12 0x3d8c0000
137 #define ADDI_11_11 0x396b0000
138 #define ADD_0_11_11 0x7c0b5a14
139 #define ADD_3_12_2 0x7c6c1214
140 #define ADD_11_0_11 0x7d605a14
141 #define B 0x48000000
142 #define BCL_20_31 0x429f0005
143 #define BCTR 0x4e800420
144 #define BEQLR 0x4d820020
145 #define CMPWI_11_0 0x2c0b0000
146 #define LIS_11 0x3d600000
147 #define LIS_12 0x3d800000
148 #define LWZU_0_12 0x840c0000
149 #define LWZ_0_12 0x800c0000
150 #define LWZ_11_3 0x81630000
151 #define LWZ_11_11 0x816b0000
152 #define LWZ_11_30 0x817e0000
153 #define LWZ_12_3 0x81830000
154 #define LWZ_12_12 0x818c0000
155 #define MR_0_3 0x7c601b78
156 #define MR_3_0 0x7c030378
157 #define MFLR_0 0x7c0802a6
158 #define MFLR_12 0x7d8802a6
159 #define MTCTR_0 0x7c0903a6
160 #define MTCTR_11 0x7d6903a6
161 #define MTLR_0 0x7c0803a6
162 #define NOP 0x60000000
163 #define SUB_11_11_12 0x7d6c5850
164
165 /* Offset of tp and dtp pointers from start of TLS block. */
166 #define TP_OFFSET 0x7000
167 #define DTP_OFFSET 0x8000
168
169 /* The value of a defined global symbol. */
170 #define SYM_VAL(SYM) \
171 ((SYM)->root.u.def.section->output_section->vma \
172 + (SYM)->root.u.def.section->output_offset \
173 + (SYM)->root.u.def.value)
174 \f
175 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
176
177 static reloc_howto_type ppc_elf_howto_raw[] = {
178 /* This reloc does nothing. */
179 HOWTO (R_PPC_NONE, /* type */
180 0, /* rightshift */
181 2, /* size (0 = byte, 1 = short, 2 = long) */
182 32, /* bitsize */
183 FALSE, /* pc_relative */
184 0, /* bitpos */
185 complain_overflow_bitfield, /* complain_on_overflow */
186 bfd_elf_generic_reloc, /* special_function */
187 "R_PPC_NONE", /* name */
188 FALSE, /* partial_inplace */
189 0, /* src_mask */
190 0, /* dst_mask */
191 FALSE), /* pcrel_offset */
192
193 /* A standard 32 bit relocation. */
194 HOWTO (R_PPC_ADDR32, /* type */
195 0, /* rightshift */
196 2, /* size (0 = byte, 1 = short, 2 = long) */
197 32, /* bitsize */
198 FALSE, /* pc_relative */
199 0, /* bitpos */
200 complain_overflow_bitfield, /* complain_on_overflow */
201 bfd_elf_generic_reloc, /* special_function */
202 "R_PPC_ADDR32", /* name */
203 FALSE, /* partial_inplace */
204 0, /* src_mask */
205 0xffffffff, /* dst_mask */
206 FALSE), /* pcrel_offset */
207
208 /* An absolute 26 bit branch; the lower two bits must be zero.
209 FIXME: we don't check that, we just clear them. */
210 HOWTO (R_PPC_ADDR24, /* type */
211 0, /* rightshift */
212 2, /* size (0 = byte, 1 = short, 2 = long) */
213 26, /* bitsize */
214 FALSE, /* pc_relative */
215 0, /* bitpos */
216 complain_overflow_bitfield, /* complain_on_overflow */
217 bfd_elf_generic_reloc, /* special_function */
218 "R_PPC_ADDR24", /* name */
219 FALSE, /* partial_inplace */
220 0, /* src_mask */
221 0x3fffffc, /* dst_mask */
222 FALSE), /* pcrel_offset */
223
224 /* A standard 16 bit relocation. */
225 HOWTO (R_PPC_ADDR16, /* type */
226 0, /* rightshift */
227 1, /* size (0 = byte, 1 = short, 2 = long) */
228 16, /* bitsize */
229 FALSE, /* pc_relative */
230 0, /* bitpos */
231 complain_overflow_bitfield, /* complain_on_overflow */
232 bfd_elf_generic_reloc, /* special_function */
233 "R_PPC_ADDR16", /* name */
234 FALSE, /* partial_inplace */
235 0, /* src_mask */
236 0xffff, /* dst_mask */
237 FALSE), /* pcrel_offset */
238
239 /* A 16 bit relocation without overflow. */
240 HOWTO (R_PPC_ADDR16_LO, /* type */
241 0, /* rightshift */
242 1, /* size (0 = byte, 1 = short, 2 = long) */
243 16, /* bitsize */
244 FALSE, /* pc_relative */
245 0, /* bitpos */
246 complain_overflow_dont,/* complain_on_overflow */
247 bfd_elf_generic_reloc, /* special_function */
248 "R_PPC_ADDR16_LO", /* name */
249 FALSE, /* partial_inplace */
250 0, /* src_mask */
251 0xffff, /* dst_mask */
252 FALSE), /* pcrel_offset */
253
254 /* The high order 16 bits of an address. */
255 HOWTO (R_PPC_ADDR16_HI, /* type */
256 16, /* rightshift */
257 1, /* size (0 = byte, 1 = short, 2 = long) */
258 16, /* bitsize */
259 FALSE, /* pc_relative */
260 0, /* bitpos */
261 complain_overflow_dont, /* complain_on_overflow */
262 bfd_elf_generic_reloc, /* special_function */
263 "R_PPC_ADDR16_HI", /* name */
264 FALSE, /* partial_inplace */
265 0, /* src_mask */
266 0xffff, /* dst_mask */
267 FALSE), /* pcrel_offset */
268
269 /* The high order 16 bits of an address, plus 1 if the contents of
270 the low 16 bits, treated as a signed number, is negative. */
271 HOWTO (R_PPC_ADDR16_HA, /* type */
272 16, /* rightshift */
273 1, /* size (0 = byte, 1 = short, 2 = long) */
274 16, /* bitsize */
275 FALSE, /* pc_relative */
276 0, /* bitpos */
277 complain_overflow_dont, /* complain_on_overflow */
278 ppc_elf_addr16_ha_reloc, /* special_function */
279 "R_PPC_ADDR16_HA", /* name */
280 FALSE, /* partial_inplace */
281 0, /* src_mask */
282 0xffff, /* dst_mask */
283 FALSE), /* pcrel_offset */
284
285 /* An absolute 16 bit branch; the lower two bits must be zero.
286 FIXME: we don't check that, we just clear them. */
287 HOWTO (R_PPC_ADDR14, /* type */
288 0, /* rightshift */
289 2, /* size (0 = byte, 1 = short, 2 = long) */
290 16, /* bitsize */
291 FALSE, /* pc_relative */
292 0, /* bitpos */
293 complain_overflow_bitfield, /* complain_on_overflow */
294 bfd_elf_generic_reloc, /* special_function */
295 "R_PPC_ADDR14", /* name */
296 FALSE, /* partial_inplace */
297 0, /* src_mask */
298 0xfffc, /* dst_mask */
299 FALSE), /* pcrel_offset */
300
301 /* An absolute 16 bit branch, for which bit 10 should be set to
302 indicate that the branch is expected to be taken. The lower two
303 bits must be zero. */
304 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 16, /* bitsize */
308 FALSE, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_bitfield, /* complain_on_overflow */
311 bfd_elf_generic_reloc, /* special_function */
312 "R_PPC_ADDR14_BRTAKEN",/* name */
313 FALSE, /* partial_inplace */
314 0, /* src_mask */
315 0xfffc, /* dst_mask */
316 FALSE), /* pcrel_offset */
317
318 /* An absolute 16 bit branch, for which bit 10 should be set to
319 indicate that the branch is not expected to be taken. The lower
320 two bits must be zero. */
321 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
322 0, /* rightshift */
323 2, /* size (0 = byte, 1 = short, 2 = long) */
324 16, /* bitsize */
325 FALSE, /* pc_relative */
326 0, /* bitpos */
327 complain_overflow_bitfield, /* complain_on_overflow */
328 bfd_elf_generic_reloc, /* special_function */
329 "R_PPC_ADDR14_BRNTAKEN",/* name */
330 FALSE, /* partial_inplace */
331 0, /* src_mask */
332 0xfffc, /* dst_mask */
333 FALSE), /* pcrel_offset */
334
335 /* A relative 26 bit branch; the lower two bits must be zero. */
336 HOWTO (R_PPC_REL24, /* type */
337 0, /* rightshift */
338 2, /* size (0 = byte, 1 = short, 2 = long) */
339 26, /* bitsize */
340 TRUE, /* pc_relative */
341 0, /* bitpos */
342 complain_overflow_signed, /* complain_on_overflow */
343 bfd_elf_generic_reloc, /* special_function */
344 "R_PPC_REL24", /* name */
345 FALSE, /* partial_inplace */
346 0, /* src_mask */
347 0x3fffffc, /* dst_mask */
348 TRUE), /* pcrel_offset */
349
350 /* A relative 16 bit branch; the lower two bits must be zero. */
351 HOWTO (R_PPC_REL14, /* type */
352 0, /* rightshift */
353 2, /* size (0 = byte, 1 = short, 2 = long) */
354 16, /* bitsize */
355 TRUE, /* pc_relative */
356 0, /* bitpos */
357 complain_overflow_signed, /* complain_on_overflow */
358 bfd_elf_generic_reloc, /* special_function */
359 "R_PPC_REL14", /* name */
360 FALSE, /* partial_inplace */
361 0, /* src_mask */
362 0xfffc, /* dst_mask */
363 TRUE), /* pcrel_offset */
364
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is expected to be taken. The lower two bits must be
367 zero. */
368 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
369 0, /* rightshift */
370 2, /* size (0 = byte, 1 = short, 2 = long) */
371 16, /* bitsize */
372 TRUE, /* pc_relative */
373 0, /* bitpos */
374 complain_overflow_signed, /* complain_on_overflow */
375 bfd_elf_generic_reloc, /* special_function */
376 "R_PPC_REL14_BRTAKEN", /* name */
377 FALSE, /* partial_inplace */
378 0, /* src_mask */
379 0xfffc, /* dst_mask */
380 TRUE), /* pcrel_offset */
381
382 /* A relative 16 bit branch. Bit 10 should be set to indicate that
383 the branch is not expected to be taken. The lower two bits must
384 be zero. */
385 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
386 0, /* rightshift */
387 2, /* size (0 = byte, 1 = short, 2 = long) */
388 16, /* bitsize */
389 TRUE, /* pc_relative */
390 0, /* bitpos */
391 complain_overflow_signed, /* complain_on_overflow */
392 bfd_elf_generic_reloc, /* special_function */
393 "R_PPC_REL14_BRNTAKEN",/* name */
394 FALSE, /* partial_inplace */
395 0, /* src_mask */
396 0xfffc, /* dst_mask */
397 TRUE), /* pcrel_offset */
398
399 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
400 symbol. */
401 HOWTO (R_PPC_GOT16, /* type */
402 0, /* rightshift */
403 1, /* size (0 = byte, 1 = short, 2 = long) */
404 16, /* bitsize */
405 FALSE, /* pc_relative */
406 0, /* bitpos */
407 complain_overflow_signed, /* complain_on_overflow */
408 bfd_elf_generic_reloc, /* special_function */
409 "R_PPC_GOT16", /* name */
410 FALSE, /* partial_inplace */
411 0, /* src_mask */
412 0xffff, /* dst_mask */
413 FALSE), /* pcrel_offset */
414
415 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
416 the symbol. */
417 HOWTO (R_PPC_GOT16_LO, /* type */
418 0, /* rightshift */
419 1, /* size (0 = byte, 1 = short, 2 = long) */
420 16, /* bitsize */
421 FALSE, /* pc_relative */
422 0, /* bitpos */
423 complain_overflow_dont, /* complain_on_overflow */
424 bfd_elf_generic_reloc, /* special_function */
425 "R_PPC_GOT16_LO", /* name */
426 FALSE, /* partial_inplace */
427 0, /* src_mask */
428 0xffff, /* dst_mask */
429 FALSE), /* pcrel_offset */
430
431 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
432 the symbol. */
433 HOWTO (R_PPC_GOT16_HI, /* type */
434 16, /* rightshift */
435 1, /* size (0 = byte, 1 = short, 2 = long) */
436 16, /* bitsize */
437 FALSE, /* pc_relative */
438 0, /* bitpos */
439 complain_overflow_bitfield, /* complain_on_overflow */
440 bfd_elf_generic_reloc, /* special_function */
441 "R_PPC_GOT16_HI", /* name */
442 FALSE, /* partial_inplace */
443 0, /* src_mask */
444 0xffff, /* dst_mask */
445 FALSE), /* pcrel_offset */
446
447 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
448 the symbol. */
449 HOWTO (R_PPC_GOT16_HA, /* type */
450 16, /* rightshift */
451 1, /* size (0 = byte, 1 = short, 2 = long) */
452 16, /* bitsize */
453 FALSE, /* pc_relative */
454 0, /* bitpos */
455 complain_overflow_bitfield, /* complain_on_overflow */
456 ppc_elf_addr16_ha_reloc, /* special_function */
457 "R_PPC_GOT16_HA", /* name */
458 FALSE, /* partial_inplace */
459 0, /* src_mask */
460 0xffff, /* dst_mask */
461 FALSE), /* pcrel_offset */
462
463 /* Like R_PPC_REL24, but referring to the procedure linkage table
464 entry for the symbol. */
465 HOWTO (R_PPC_PLTREL24, /* type */
466 0, /* rightshift */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
468 26, /* bitsize */
469 TRUE, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_signed, /* complain_on_overflow */
472 bfd_elf_generic_reloc, /* special_function */
473 "R_PPC_PLTREL24", /* name */
474 FALSE, /* partial_inplace */
475 0, /* src_mask */
476 0x3fffffc, /* dst_mask */
477 TRUE), /* pcrel_offset */
478
479 /* This is used only by the dynamic linker. The symbol should exist
480 both in the object being run and in some shared library. The
481 dynamic linker copies the data addressed by the symbol from the
482 shared library into the object, because the object being
483 run has to have the data at some particular address. */
484 HOWTO (R_PPC_COPY, /* type */
485 0, /* rightshift */
486 2, /* size (0 = byte, 1 = short, 2 = long) */
487 32, /* bitsize */
488 FALSE, /* pc_relative */
489 0, /* bitpos */
490 complain_overflow_bitfield, /* complain_on_overflow */
491 bfd_elf_generic_reloc, /* special_function */
492 "R_PPC_COPY", /* name */
493 FALSE, /* partial_inplace */
494 0, /* src_mask */
495 0, /* dst_mask */
496 FALSE), /* pcrel_offset */
497
498 /* Like R_PPC_ADDR32, but used when setting global offset table
499 entries. */
500 HOWTO (R_PPC_GLOB_DAT, /* type */
501 0, /* rightshift */
502 2, /* size (0 = byte, 1 = short, 2 = long) */
503 32, /* bitsize */
504 FALSE, /* pc_relative */
505 0, /* bitpos */
506 complain_overflow_bitfield, /* complain_on_overflow */
507 bfd_elf_generic_reloc, /* special_function */
508 "R_PPC_GLOB_DAT", /* name */
509 FALSE, /* partial_inplace */
510 0, /* src_mask */
511 0xffffffff, /* dst_mask */
512 FALSE), /* pcrel_offset */
513
514 /* Marks a procedure linkage table entry for a symbol. */
515 HOWTO (R_PPC_JMP_SLOT, /* type */
516 0, /* rightshift */
517 2, /* size (0 = byte, 1 = short, 2 = long) */
518 32, /* bitsize */
519 FALSE, /* pc_relative */
520 0, /* bitpos */
521 complain_overflow_bitfield, /* complain_on_overflow */
522 bfd_elf_generic_reloc, /* special_function */
523 "R_PPC_JMP_SLOT", /* name */
524 FALSE, /* partial_inplace */
525 0, /* src_mask */
526 0, /* dst_mask */
527 FALSE), /* pcrel_offset */
528
529 /* Used only by the dynamic linker. When the object is run, this
530 longword is set to the load address of the object, plus the
531 addend. */
532 HOWTO (R_PPC_RELATIVE, /* type */
533 0, /* rightshift */
534 2, /* size (0 = byte, 1 = short, 2 = long) */
535 32, /* bitsize */
536 FALSE, /* pc_relative */
537 0, /* bitpos */
538 complain_overflow_bitfield, /* complain_on_overflow */
539 bfd_elf_generic_reloc, /* special_function */
540 "R_PPC_RELATIVE", /* name */
541 FALSE, /* partial_inplace */
542 0, /* src_mask */
543 0xffffffff, /* dst_mask */
544 FALSE), /* pcrel_offset */
545
546 /* Like R_PPC_REL24, but uses the value of the symbol within the
547 object rather than the final value. Normally used for
548 _GLOBAL_OFFSET_TABLE_. */
549 HOWTO (R_PPC_LOCAL24PC, /* type */
550 0, /* rightshift */
551 2, /* size (0 = byte, 1 = short, 2 = long) */
552 26, /* bitsize */
553 TRUE, /* pc_relative */
554 0, /* bitpos */
555 complain_overflow_signed, /* complain_on_overflow */
556 bfd_elf_generic_reloc, /* special_function */
557 "R_PPC_LOCAL24PC", /* name */
558 FALSE, /* partial_inplace */
559 0, /* src_mask */
560 0x3fffffc, /* dst_mask */
561 TRUE), /* pcrel_offset */
562
563 /* Like R_PPC_ADDR32, but may be unaligned. */
564 HOWTO (R_PPC_UADDR32, /* type */
565 0, /* rightshift */
566 2, /* size (0 = byte, 1 = short, 2 = long) */
567 32, /* bitsize */
568 FALSE, /* pc_relative */
569 0, /* bitpos */
570 complain_overflow_bitfield, /* complain_on_overflow */
571 bfd_elf_generic_reloc, /* special_function */
572 "R_PPC_UADDR32", /* name */
573 FALSE, /* partial_inplace */
574 0, /* src_mask */
575 0xffffffff, /* dst_mask */
576 FALSE), /* pcrel_offset */
577
578 /* Like R_PPC_ADDR16, but may be unaligned. */
579 HOWTO (R_PPC_UADDR16, /* type */
580 0, /* rightshift */
581 1, /* size (0 = byte, 1 = short, 2 = long) */
582 16, /* bitsize */
583 FALSE, /* pc_relative */
584 0, /* bitpos */
585 complain_overflow_bitfield, /* complain_on_overflow */
586 bfd_elf_generic_reloc, /* special_function */
587 "R_PPC_UADDR16", /* name */
588 FALSE, /* partial_inplace */
589 0, /* src_mask */
590 0xffff, /* dst_mask */
591 FALSE), /* pcrel_offset */
592
593 /* 32-bit PC relative */
594 HOWTO (R_PPC_REL32, /* type */
595 0, /* rightshift */
596 2, /* size (0 = byte, 1 = short, 2 = long) */
597 32, /* bitsize */
598 TRUE, /* pc_relative */
599 0, /* bitpos */
600 complain_overflow_bitfield, /* complain_on_overflow */
601 bfd_elf_generic_reloc, /* special_function */
602 "R_PPC_REL32", /* name */
603 FALSE, /* partial_inplace */
604 0, /* src_mask */
605 0xffffffff, /* dst_mask */
606 TRUE), /* pcrel_offset */
607
608 /* 32-bit relocation to the symbol's procedure linkage table.
609 FIXME: not supported. */
610 HOWTO (R_PPC_PLT32, /* type */
611 0, /* rightshift */
612 2, /* size (0 = byte, 1 = short, 2 = long) */
613 32, /* bitsize */
614 FALSE, /* pc_relative */
615 0, /* bitpos */
616 complain_overflow_bitfield, /* complain_on_overflow */
617 bfd_elf_generic_reloc, /* special_function */
618 "R_PPC_PLT32", /* name */
619 FALSE, /* partial_inplace */
620 0, /* src_mask */
621 0, /* dst_mask */
622 FALSE), /* pcrel_offset */
623
624 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
625 FIXME: not supported. */
626 HOWTO (R_PPC_PLTREL32, /* type */
627 0, /* rightshift */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
629 32, /* bitsize */
630 TRUE, /* pc_relative */
631 0, /* bitpos */
632 complain_overflow_bitfield, /* complain_on_overflow */
633 bfd_elf_generic_reloc, /* special_function */
634 "R_PPC_PLTREL32", /* name */
635 FALSE, /* partial_inplace */
636 0, /* src_mask */
637 0, /* dst_mask */
638 TRUE), /* pcrel_offset */
639
640 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
641 the symbol. */
642 HOWTO (R_PPC_PLT16_LO, /* type */
643 0, /* rightshift */
644 1, /* size (0 = byte, 1 = short, 2 = long) */
645 16, /* bitsize */
646 FALSE, /* pc_relative */
647 0, /* bitpos */
648 complain_overflow_dont, /* complain_on_overflow */
649 bfd_elf_generic_reloc, /* special_function */
650 "R_PPC_PLT16_LO", /* name */
651 FALSE, /* partial_inplace */
652 0, /* src_mask */
653 0xffff, /* dst_mask */
654 FALSE), /* pcrel_offset */
655
656 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
657 the symbol. */
658 HOWTO (R_PPC_PLT16_HI, /* type */
659 16, /* rightshift */
660 1, /* size (0 = byte, 1 = short, 2 = long) */
661 16, /* bitsize */
662 FALSE, /* pc_relative */
663 0, /* bitpos */
664 complain_overflow_bitfield, /* complain_on_overflow */
665 bfd_elf_generic_reloc, /* special_function */
666 "R_PPC_PLT16_HI", /* name */
667 FALSE, /* partial_inplace */
668 0, /* src_mask */
669 0xffff, /* dst_mask */
670 FALSE), /* pcrel_offset */
671
672 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
673 the symbol. */
674 HOWTO (R_PPC_PLT16_HA, /* type */
675 16, /* rightshift */
676 1, /* size (0 = byte, 1 = short, 2 = long) */
677 16, /* bitsize */
678 FALSE, /* pc_relative */
679 0, /* bitpos */
680 complain_overflow_bitfield, /* complain_on_overflow */
681 ppc_elf_addr16_ha_reloc, /* special_function */
682 "R_PPC_PLT16_HA", /* name */
683 FALSE, /* partial_inplace */
684 0, /* src_mask */
685 0xffff, /* dst_mask */
686 FALSE), /* pcrel_offset */
687
688 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
689 small data items. */
690 HOWTO (R_PPC_SDAREL16, /* type */
691 0, /* rightshift */
692 1, /* size (0 = byte, 1 = short, 2 = long) */
693 16, /* bitsize */
694 FALSE, /* pc_relative */
695 0, /* bitpos */
696 complain_overflow_signed, /* complain_on_overflow */
697 bfd_elf_generic_reloc, /* special_function */
698 "R_PPC_SDAREL16", /* name */
699 FALSE, /* partial_inplace */
700 0, /* src_mask */
701 0xffff, /* dst_mask */
702 FALSE), /* pcrel_offset */
703
704 /* 16-bit section relative relocation. */
705 HOWTO (R_PPC_SECTOFF, /* type */
706 0, /* rightshift */
707 1, /* size (0 = byte, 1 = short, 2 = long) */
708 16, /* bitsize */
709 FALSE, /* pc_relative */
710 0, /* bitpos */
711 complain_overflow_bitfield, /* complain_on_overflow */
712 bfd_elf_generic_reloc, /* special_function */
713 "R_PPC_SECTOFF", /* name */
714 FALSE, /* partial_inplace */
715 0, /* src_mask */
716 0xffff, /* dst_mask */
717 FALSE), /* pcrel_offset */
718
719 /* 16-bit lower half section relative relocation. */
720 HOWTO (R_PPC_SECTOFF_LO, /* type */
721 0, /* rightshift */
722 1, /* size (0 = byte, 1 = short, 2 = long) */
723 16, /* bitsize */
724 FALSE, /* pc_relative */
725 0, /* bitpos */
726 complain_overflow_dont, /* complain_on_overflow */
727 bfd_elf_generic_reloc, /* special_function */
728 "R_PPC_SECTOFF_LO", /* name */
729 FALSE, /* partial_inplace */
730 0, /* src_mask */
731 0xffff, /* dst_mask */
732 FALSE), /* pcrel_offset */
733
734 /* 16-bit upper half section relative relocation. */
735 HOWTO (R_PPC_SECTOFF_HI, /* type */
736 16, /* rightshift */
737 1, /* size (0 = byte, 1 = short, 2 = long) */
738 16, /* bitsize */
739 FALSE, /* pc_relative */
740 0, /* bitpos */
741 complain_overflow_bitfield, /* complain_on_overflow */
742 bfd_elf_generic_reloc, /* special_function */
743 "R_PPC_SECTOFF_HI", /* name */
744 FALSE, /* partial_inplace */
745 0, /* src_mask */
746 0xffff, /* dst_mask */
747 FALSE), /* pcrel_offset */
748
749 /* 16-bit upper half adjusted section relative relocation. */
750 HOWTO (R_PPC_SECTOFF_HA, /* type */
751 16, /* rightshift */
752 1, /* size (0 = byte, 1 = short, 2 = long) */
753 16, /* bitsize */
754 FALSE, /* pc_relative */
755 0, /* bitpos */
756 complain_overflow_bitfield, /* complain_on_overflow */
757 ppc_elf_addr16_ha_reloc, /* special_function */
758 "R_PPC_SECTOFF_HA", /* name */
759 FALSE, /* partial_inplace */
760 0, /* src_mask */
761 0xffff, /* dst_mask */
762 FALSE), /* pcrel_offset */
763
764 /* Marker relocs for TLS. */
765 HOWTO (R_PPC_TLS,
766 0, /* rightshift */
767 2, /* size (0 = byte, 1 = short, 2 = long) */
768 32, /* bitsize */
769 FALSE, /* pc_relative */
770 0, /* bitpos */
771 complain_overflow_dont, /* complain_on_overflow */
772 bfd_elf_generic_reloc, /* special_function */
773 "R_PPC_TLS", /* name */
774 FALSE, /* partial_inplace */
775 0, /* src_mask */
776 0, /* dst_mask */
777 FALSE), /* pcrel_offset */
778
779 HOWTO (R_PPC_TLSGD,
780 0, /* rightshift */
781 2, /* size (0 = byte, 1 = short, 2 = long) */
782 32, /* bitsize */
783 FALSE, /* pc_relative */
784 0, /* bitpos */
785 complain_overflow_dont, /* complain_on_overflow */
786 bfd_elf_generic_reloc, /* special_function */
787 "R_PPC_TLSGD", /* name */
788 FALSE, /* partial_inplace */
789 0, /* src_mask */
790 0, /* dst_mask */
791 FALSE), /* pcrel_offset */
792
793 HOWTO (R_PPC_TLSLD,
794 0, /* rightshift */
795 2, /* size (0 = byte, 1 = short, 2 = long) */
796 32, /* bitsize */
797 FALSE, /* pc_relative */
798 0, /* bitpos */
799 complain_overflow_dont, /* complain_on_overflow */
800 bfd_elf_generic_reloc, /* special_function */
801 "R_PPC_TLSLD", /* name */
802 FALSE, /* partial_inplace */
803 0, /* src_mask */
804 0, /* dst_mask */
805 FALSE), /* pcrel_offset */
806
807 /* Computes the load module index of the load module that contains the
808 definition of its TLS sym. */
809 HOWTO (R_PPC_DTPMOD32,
810 0, /* rightshift */
811 2, /* size (0 = byte, 1 = short, 2 = long) */
812 32, /* bitsize */
813 FALSE, /* pc_relative */
814 0, /* bitpos */
815 complain_overflow_dont, /* complain_on_overflow */
816 ppc_elf_unhandled_reloc, /* special_function */
817 "R_PPC_DTPMOD32", /* name */
818 FALSE, /* partial_inplace */
819 0, /* src_mask */
820 0xffffffff, /* dst_mask */
821 FALSE), /* pcrel_offset */
822
823 /* Computes a dtv-relative displacement, the difference between the value
824 of sym+add and the base address of the thread-local storage block that
825 contains the definition of sym, minus 0x8000. */
826 HOWTO (R_PPC_DTPREL32,
827 0, /* rightshift */
828 2, /* size (0 = byte, 1 = short, 2 = long) */
829 32, /* bitsize */
830 FALSE, /* pc_relative */
831 0, /* bitpos */
832 complain_overflow_dont, /* complain_on_overflow */
833 ppc_elf_unhandled_reloc, /* special_function */
834 "R_PPC_DTPREL32", /* name */
835 FALSE, /* partial_inplace */
836 0, /* src_mask */
837 0xffffffff, /* dst_mask */
838 FALSE), /* pcrel_offset */
839
840 /* A 16 bit dtprel reloc. */
841 HOWTO (R_PPC_DTPREL16,
842 0, /* rightshift */
843 1, /* size (0 = byte, 1 = short, 2 = long) */
844 16, /* bitsize */
845 FALSE, /* pc_relative */
846 0, /* bitpos */
847 complain_overflow_signed, /* complain_on_overflow */
848 ppc_elf_unhandled_reloc, /* special_function */
849 "R_PPC_DTPREL16", /* name */
850 FALSE, /* partial_inplace */
851 0, /* src_mask */
852 0xffff, /* dst_mask */
853 FALSE), /* pcrel_offset */
854
855 /* Like DTPREL16, but no overflow. */
856 HOWTO (R_PPC_DTPREL16_LO,
857 0, /* 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_DTPREL16_LO", /* name */
865 FALSE, /* partial_inplace */
866 0, /* src_mask */
867 0xffff, /* dst_mask */
868 FALSE), /* pcrel_offset */
869
870 /* Like DTPREL16_LO, but next higher group of 16 bits. */
871 HOWTO (R_PPC_DTPREL16_HI,
872 16, /* rightshift */
873 1, /* size (0 = byte, 1 = short, 2 = long) */
874 16, /* bitsize */
875 FALSE, /* pc_relative */
876 0, /* bitpos */
877 complain_overflow_dont, /* complain_on_overflow */
878 ppc_elf_unhandled_reloc, /* special_function */
879 "R_PPC_DTPREL16_HI", /* name */
880 FALSE, /* partial_inplace */
881 0, /* src_mask */
882 0xffff, /* dst_mask */
883 FALSE), /* pcrel_offset */
884
885 /* Like DTPREL16_HI, but adjust for low 16 bits. */
886 HOWTO (R_PPC_DTPREL16_HA,
887 16, /* rightshift */
888 1, /* size (0 = byte, 1 = short, 2 = long) */
889 16, /* bitsize */
890 FALSE, /* pc_relative */
891 0, /* bitpos */
892 complain_overflow_dont, /* complain_on_overflow */
893 ppc_elf_unhandled_reloc, /* special_function */
894 "R_PPC_DTPREL16_HA", /* name */
895 FALSE, /* partial_inplace */
896 0, /* src_mask */
897 0xffff, /* dst_mask */
898 FALSE), /* pcrel_offset */
899
900 /* Computes a tp-relative displacement, the difference between the value of
901 sym+add and the value of the thread pointer (r13). */
902 HOWTO (R_PPC_TPREL32,
903 0, /* rightshift */
904 2, /* size (0 = byte, 1 = short, 2 = long) */
905 32, /* bitsize */
906 FALSE, /* pc_relative */
907 0, /* bitpos */
908 complain_overflow_dont, /* complain_on_overflow */
909 ppc_elf_unhandled_reloc, /* special_function */
910 "R_PPC_TPREL32", /* name */
911 FALSE, /* partial_inplace */
912 0, /* src_mask */
913 0xffffffff, /* dst_mask */
914 FALSE), /* pcrel_offset */
915
916 /* A 16 bit tprel reloc. */
917 HOWTO (R_PPC_TPREL16,
918 0, /* rightshift */
919 1, /* size (0 = byte, 1 = short, 2 = long) */
920 16, /* bitsize */
921 FALSE, /* pc_relative */
922 0, /* bitpos */
923 complain_overflow_signed, /* complain_on_overflow */
924 ppc_elf_unhandled_reloc, /* special_function */
925 "R_PPC_TPREL16", /* name */
926 FALSE, /* partial_inplace */
927 0, /* src_mask */
928 0xffff, /* dst_mask */
929 FALSE), /* pcrel_offset */
930
931 /* Like TPREL16, but no overflow. */
932 HOWTO (R_PPC_TPREL16_LO,
933 0, /* rightshift */
934 1, /* size (0 = byte, 1 = short, 2 = long) */
935 16, /* bitsize */
936 FALSE, /* pc_relative */
937 0, /* bitpos */
938 complain_overflow_dont, /* complain_on_overflow */
939 ppc_elf_unhandled_reloc, /* special_function */
940 "R_PPC_TPREL16_LO", /* name */
941 FALSE, /* partial_inplace */
942 0, /* src_mask */
943 0xffff, /* dst_mask */
944 FALSE), /* pcrel_offset */
945
946 /* Like TPREL16_LO, but next higher group of 16 bits. */
947 HOWTO (R_PPC_TPREL16_HI,
948 16, /* rightshift */
949 1, /* size (0 = byte, 1 = short, 2 = long) */
950 16, /* bitsize */
951 FALSE, /* pc_relative */
952 0, /* bitpos */
953 complain_overflow_dont, /* complain_on_overflow */
954 ppc_elf_unhandled_reloc, /* special_function */
955 "R_PPC_TPREL16_HI", /* name */
956 FALSE, /* partial_inplace */
957 0, /* src_mask */
958 0xffff, /* dst_mask */
959 FALSE), /* pcrel_offset */
960
961 /* Like TPREL16_HI, but adjust for low 16 bits. */
962 HOWTO (R_PPC_TPREL16_HA,
963 16, /* rightshift */
964 1, /* size (0 = byte, 1 = short, 2 = long) */
965 16, /* bitsize */
966 FALSE, /* pc_relative */
967 0, /* bitpos */
968 complain_overflow_dont, /* complain_on_overflow */
969 ppc_elf_unhandled_reloc, /* special_function */
970 "R_PPC_TPREL16_HA", /* name */
971 FALSE, /* partial_inplace */
972 0, /* src_mask */
973 0xffff, /* dst_mask */
974 FALSE), /* pcrel_offset */
975
976 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
977 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
978 to the first entry. */
979 HOWTO (R_PPC_GOT_TLSGD16,
980 0, /* rightshift */
981 1, /* size (0 = byte, 1 = short, 2 = long) */
982 16, /* bitsize */
983 FALSE, /* pc_relative */
984 0, /* bitpos */
985 complain_overflow_signed, /* complain_on_overflow */
986 ppc_elf_unhandled_reloc, /* special_function */
987 "R_PPC_GOT_TLSGD16", /* name */
988 FALSE, /* partial_inplace */
989 0, /* src_mask */
990 0xffff, /* dst_mask */
991 FALSE), /* pcrel_offset */
992
993 /* Like GOT_TLSGD16, but no overflow. */
994 HOWTO (R_PPC_GOT_TLSGD16_LO,
995 0, /* rightshift */
996 1, /* size (0 = byte, 1 = short, 2 = long) */
997 16, /* bitsize */
998 FALSE, /* pc_relative */
999 0, /* bitpos */
1000 complain_overflow_dont, /* complain_on_overflow */
1001 ppc_elf_unhandled_reloc, /* special_function */
1002 "R_PPC_GOT_TLSGD16_LO", /* name */
1003 FALSE, /* partial_inplace */
1004 0, /* src_mask */
1005 0xffff, /* dst_mask */
1006 FALSE), /* pcrel_offset */
1007
1008 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1009 HOWTO (R_PPC_GOT_TLSGD16_HI,
1010 16, /* rightshift */
1011 1, /* size (0 = byte, 1 = short, 2 = long) */
1012 16, /* bitsize */
1013 FALSE, /* pc_relative */
1014 0, /* bitpos */
1015 complain_overflow_dont, /* complain_on_overflow */
1016 ppc_elf_unhandled_reloc, /* special_function */
1017 "R_PPC_GOT_TLSGD16_HI", /* name */
1018 FALSE, /* partial_inplace */
1019 0, /* src_mask */
1020 0xffff, /* dst_mask */
1021 FALSE), /* pcrel_offset */
1022
1023 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1024 HOWTO (R_PPC_GOT_TLSGD16_HA,
1025 16, /* rightshift */
1026 1, /* size (0 = byte, 1 = short, 2 = long) */
1027 16, /* bitsize */
1028 FALSE, /* pc_relative */
1029 0, /* bitpos */
1030 complain_overflow_dont, /* complain_on_overflow */
1031 ppc_elf_unhandled_reloc, /* special_function */
1032 "R_PPC_GOT_TLSGD16_HA", /* name */
1033 FALSE, /* partial_inplace */
1034 0, /* src_mask */
1035 0xffff, /* dst_mask */
1036 FALSE), /* pcrel_offset */
1037
1038 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1039 with values (sym+add)@dtpmod and zero, and computes the offset to the
1040 first entry. */
1041 HOWTO (R_PPC_GOT_TLSLD16,
1042 0, /* rightshift */
1043 1, /* size (0 = byte, 1 = short, 2 = long) */
1044 16, /* bitsize */
1045 FALSE, /* pc_relative */
1046 0, /* bitpos */
1047 complain_overflow_signed, /* complain_on_overflow */
1048 ppc_elf_unhandled_reloc, /* special_function */
1049 "R_PPC_GOT_TLSLD16", /* name */
1050 FALSE, /* partial_inplace */
1051 0, /* src_mask */
1052 0xffff, /* dst_mask */
1053 FALSE), /* pcrel_offset */
1054
1055 /* Like GOT_TLSLD16, but no overflow. */
1056 HOWTO (R_PPC_GOT_TLSLD16_LO,
1057 0, /* rightshift */
1058 1, /* size (0 = byte, 1 = short, 2 = long) */
1059 16, /* bitsize */
1060 FALSE, /* pc_relative */
1061 0, /* bitpos */
1062 complain_overflow_dont, /* complain_on_overflow */
1063 ppc_elf_unhandled_reloc, /* special_function */
1064 "R_PPC_GOT_TLSLD16_LO", /* name */
1065 FALSE, /* partial_inplace */
1066 0, /* src_mask */
1067 0xffff, /* dst_mask */
1068 FALSE), /* pcrel_offset */
1069
1070 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1071 HOWTO (R_PPC_GOT_TLSLD16_HI,
1072 16, /* rightshift */
1073 1, /* size (0 = byte, 1 = short, 2 = long) */
1074 16, /* bitsize */
1075 FALSE, /* pc_relative */
1076 0, /* bitpos */
1077 complain_overflow_dont, /* complain_on_overflow */
1078 ppc_elf_unhandled_reloc, /* special_function */
1079 "R_PPC_GOT_TLSLD16_HI", /* name */
1080 FALSE, /* partial_inplace */
1081 0, /* src_mask */
1082 0xffff, /* dst_mask */
1083 FALSE), /* pcrel_offset */
1084
1085 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1086 HOWTO (R_PPC_GOT_TLSLD16_HA,
1087 16, /* rightshift */
1088 1, /* size (0 = byte, 1 = short, 2 = long) */
1089 16, /* bitsize */
1090 FALSE, /* pc_relative */
1091 0, /* bitpos */
1092 complain_overflow_dont, /* complain_on_overflow */
1093 ppc_elf_unhandled_reloc, /* special_function */
1094 "R_PPC_GOT_TLSLD16_HA", /* name */
1095 FALSE, /* partial_inplace */
1096 0, /* src_mask */
1097 0xffff, /* dst_mask */
1098 FALSE), /* pcrel_offset */
1099
1100 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1101 the offset to the entry. */
1102 HOWTO (R_PPC_GOT_DTPREL16,
1103 0, /* rightshift */
1104 1, /* size (0 = byte, 1 = short, 2 = long) */
1105 16, /* bitsize */
1106 FALSE, /* pc_relative */
1107 0, /* bitpos */
1108 complain_overflow_signed, /* complain_on_overflow */
1109 ppc_elf_unhandled_reloc, /* special_function */
1110 "R_PPC_GOT_DTPREL16", /* name */
1111 FALSE, /* partial_inplace */
1112 0, /* src_mask */
1113 0xffff, /* dst_mask */
1114 FALSE), /* pcrel_offset */
1115
1116 /* Like GOT_DTPREL16, but no overflow. */
1117 HOWTO (R_PPC_GOT_DTPREL16_LO,
1118 0, /* rightshift */
1119 1, /* size (0 = byte, 1 = short, 2 = long) */
1120 16, /* bitsize */
1121 FALSE, /* pc_relative */
1122 0, /* bitpos */
1123 complain_overflow_dont, /* complain_on_overflow */
1124 ppc_elf_unhandled_reloc, /* special_function */
1125 "R_PPC_GOT_DTPREL16_LO", /* name */
1126 FALSE, /* partial_inplace */
1127 0, /* src_mask */
1128 0xffff, /* dst_mask */
1129 FALSE), /* pcrel_offset */
1130
1131 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1132 HOWTO (R_PPC_GOT_DTPREL16_HI,
1133 16, /* rightshift */
1134 1, /* size (0 = byte, 1 = short, 2 = long) */
1135 16, /* bitsize */
1136 FALSE, /* pc_relative */
1137 0, /* bitpos */
1138 complain_overflow_dont, /* complain_on_overflow */
1139 ppc_elf_unhandled_reloc, /* special_function */
1140 "R_PPC_GOT_DTPREL16_HI", /* name */
1141 FALSE, /* partial_inplace */
1142 0, /* src_mask */
1143 0xffff, /* dst_mask */
1144 FALSE), /* pcrel_offset */
1145
1146 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1147 HOWTO (R_PPC_GOT_DTPREL16_HA,
1148 16, /* rightshift */
1149 1, /* size (0 = byte, 1 = short, 2 = long) */
1150 16, /* bitsize */
1151 FALSE, /* pc_relative */
1152 0, /* bitpos */
1153 complain_overflow_dont, /* complain_on_overflow */
1154 ppc_elf_unhandled_reloc, /* special_function */
1155 "R_PPC_GOT_DTPREL16_HA", /* name */
1156 FALSE, /* partial_inplace */
1157 0, /* src_mask */
1158 0xffff, /* dst_mask */
1159 FALSE), /* pcrel_offset */
1160
1161 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1162 offset to the entry. */
1163 HOWTO (R_PPC_GOT_TPREL16,
1164 0, /* rightshift */
1165 1, /* size (0 = byte, 1 = short, 2 = long) */
1166 16, /* bitsize */
1167 FALSE, /* pc_relative */
1168 0, /* bitpos */
1169 complain_overflow_signed, /* complain_on_overflow */
1170 ppc_elf_unhandled_reloc, /* special_function */
1171 "R_PPC_GOT_TPREL16", /* name */
1172 FALSE, /* partial_inplace */
1173 0, /* src_mask */
1174 0xffff, /* dst_mask */
1175 FALSE), /* pcrel_offset */
1176
1177 /* Like GOT_TPREL16, but no overflow. */
1178 HOWTO (R_PPC_GOT_TPREL16_LO,
1179 0, /* rightshift */
1180 1, /* size (0 = byte, 1 = short, 2 = long) */
1181 16, /* bitsize */
1182 FALSE, /* pc_relative */
1183 0, /* bitpos */
1184 complain_overflow_dont, /* complain_on_overflow */
1185 ppc_elf_unhandled_reloc, /* special_function */
1186 "R_PPC_GOT_TPREL16_LO", /* name */
1187 FALSE, /* partial_inplace */
1188 0, /* src_mask */
1189 0xffff, /* dst_mask */
1190 FALSE), /* pcrel_offset */
1191
1192 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1193 HOWTO (R_PPC_GOT_TPREL16_HI,
1194 16, /* rightshift */
1195 1, /* size (0 = byte, 1 = short, 2 = long) */
1196 16, /* bitsize */
1197 FALSE, /* pc_relative */
1198 0, /* bitpos */
1199 complain_overflow_dont, /* complain_on_overflow */
1200 ppc_elf_unhandled_reloc, /* special_function */
1201 "R_PPC_GOT_TPREL16_HI", /* name */
1202 FALSE, /* partial_inplace */
1203 0, /* src_mask */
1204 0xffff, /* dst_mask */
1205 FALSE), /* pcrel_offset */
1206
1207 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1208 HOWTO (R_PPC_GOT_TPREL16_HA,
1209 16, /* rightshift */
1210 1, /* size (0 = byte, 1 = short, 2 = long) */
1211 16, /* bitsize */
1212 FALSE, /* pc_relative */
1213 0, /* bitpos */
1214 complain_overflow_dont, /* complain_on_overflow */
1215 ppc_elf_unhandled_reloc, /* special_function */
1216 "R_PPC_GOT_TPREL16_HA", /* name */
1217 FALSE, /* partial_inplace */
1218 0, /* src_mask */
1219 0xffff, /* dst_mask */
1220 FALSE), /* pcrel_offset */
1221
1222 /* The remaining relocs are from the Embedded ELF ABI, and are not
1223 in the SVR4 ELF ABI. */
1224
1225 /* 32 bit value resulting from the addend minus the symbol. */
1226 HOWTO (R_PPC_EMB_NADDR32, /* type */
1227 0, /* rightshift */
1228 2, /* size (0 = byte, 1 = short, 2 = long) */
1229 32, /* bitsize */
1230 FALSE, /* pc_relative */
1231 0, /* bitpos */
1232 complain_overflow_bitfield, /* complain_on_overflow */
1233 bfd_elf_generic_reloc, /* special_function */
1234 "R_PPC_EMB_NADDR32", /* name */
1235 FALSE, /* partial_inplace */
1236 0, /* src_mask */
1237 0xffffffff, /* dst_mask */
1238 FALSE), /* pcrel_offset */
1239
1240 /* 16 bit value resulting from the addend minus the symbol. */
1241 HOWTO (R_PPC_EMB_NADDR16, /* type */
1242 0, /* rightshift */
1243 1, /* size (0 = byte, 1 = short, 2 = long) */
1244 16, /* bitsize */
1245 FALSE, /* pc_relative */
1246 0, /* bitpos */
1247 complain_overflow_bitfield, /* complain_on_overflow */
1248 bfd_elf_generic_reloc, /* special_function */
1249 "R_PPC_EMB_NADDR16", /* name */
1250 FALSE, /* partial_inplace */
1251 0, /* src_mask */
1252 0xffff, /* dst_mask */
1253 FALSE), /* pcrel_offset */
1254
1255 /* 16 bit value resulting from the addend minus the symbol. */
1256 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1257 0, /* rightshift */
1258 1, /* size (0 = byte, 1 = short, 2 = long) */
1259 16, /* bitsize */
1260 FALSE, /* pc_relative */
1261 0, /* bitpos */
1262 complain_overflow_dont,/* complain_on_overflow */
1263 bfd_elf_generic_reloc, /* special_function */
1264 "R_PPC_EMB_ADDR16_LO", /* name */
1265 FALSE, /* partial_inplace */
1266 0, /* src_mask */
1267 0xffff, /* dst_mask */
1268 FALSE), /* pcrel_offset */
1269
1270 /* The high order 16 bits of the addend minus the symbol. */
1271 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1272 16, /* rightshift */
1273 1, /* size (0 = byte, 1 = short, 2 = long) */
1274 16, /* bitsize */
1275 FALSE, /* pc_relative */
1276 0, /* bitpos */
1277 complain_overflow_dont, /* complain_on_overflow */
1278 bfd_elf_generic_reloc, /* special_function */
1279 "R_PPC_EMB_NADDR16_HI", /* name */
1280 FALSE, /* partial_inplace */
1281 0, /* src_mask */
1282 0xffff, /* dst_mask */
1283 FALSE), /* pcrel_offset */
1284
1285 /* The high order 16 bits of the result of the addend minus the address,
1286 plus 1 if the contents of the low 16 bits, treated as a signed number,
1287 is negative. */
1288 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1289 16, /* rightshift */
1290 1, /* size (0 = byte, 1 = short, 2 = long) */
1291 16, /* bitsize */
1292 FALSE, /* pc_relative */
1293 0, /* bitpos */
1294 complain_overflow_dont, /* complain_on_overflow */
1295 ppc_elf_addr16_ha_reloc, /* special_function */
1296 "R_PPC_EMB_NADDR16_HA", /* name */
1297 FALSE, /* partial_inplace */
1298 0, /* src_mask */
1299 0xffff, /* dst_mask */
1300 FALSE), /* pcrel_offset */
1301
1302 /* 16 bit value resulting from allocating a 4 byte word to hold an
1303 address in the .sdata section, and returning the offset from
1304 _SDA_BASE_ for that relocation. */
1305 HOWTO (R_PPC_EMB_SDAI16, /* type */
1306 0, /* rightshift */
1307 1, /* size (0 = byte, 1 = short, 2 = long) */
1308 16, /* bitsize */
1309 FALSE, /* pc_relative */
1310 0, /* bitpos */
1311 complain_overflow_signed, /* complain_on_overflow */
1312 bfd_elf_generic_reloc, /* special_function */
1313 "R_PPC_EMB_SDAI16", /* name */
1314 FALSE, /* partial_inplace */
1315 0, /* src_mask */
1316 0xffff, /* dst_mask */
1317 FALSE), /* pcrel_offset */
1318
1319 /* 16 bit value resulting from allocating a 4 byte word to hold an
1320 address in the .sdata2 section, and returning the offset from
1321 _SDA2_BASE_ for that relocation. */
1322 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1323 0, /* rightshift */
1324 1, /* size (0 = byte, 1 = short, 2 = long) */
1325 16, /* bitsize */
1326 FALSE, /* pc_relative */
1327 0, /* bitpos */
1328 complain_overflow_signed, /* complain_on_overflow */
1329 bfd_elf_generic_reloc, /* special_function */
1330 "R_PPC_EMB_SDA2I16", /* name */
1331 FALSE, /* partial_inplace */
1332 0, /* src_mask */
1333 0xffff, /* dst_mask */
1334 FALSE), /* pcrel_offset */
1335
1336 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1337 small data items. */
1338 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1339 0, /* rightshift */
1340 1, /* size (0 = byte, 1 = short, 2 = long) */
1341 16, /* bitsize */
1342 FALSE, /* pc_relative */
1343 0, /* bitpos */
1344 complain_overflow_signed, /* complain_on_overflow */
1345 bfd_elf_generic_reloc, /* special_function */
1346 "R_PPC_EMB_SDA2REL", /* name */
1347 FALSE, /* partial_inplace */
1348 0, /* src_mask */
1349 0xffff, /* dst_mask */
1350 FALSE), /* pcrel_offset */
1351
1352 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1353 signed offset from the appropriate base, and filling in the register
1354 field with the appropriate register (0, 2, or 13). */
1355 HOWTO (R_PPC_EMB_SDA21, /* type */
1356 0, /* rightshift */
1357 2, /* size (0 = byte, 1 = short, 2 = long) */
1358 16, /* bitsize */
1359 FALSE, /* pc_relative */
1360 0, /* bitpos */
1361 complain_overflow_signed, /* complain_on_overflow */
1362 bfd_elf_generic_reloc, /* special_function */
1363 "R_PPC_EMB_SDA21", /* name */
1364 FALSE, /* partial_inplace */
1365 0, /* src_mask */
1366 0xffff, /* dst_mask */
1367 FALSE), /* pcrel_offset */
1368
1369 /* Relocation not handled: R_PPC_EMB_MRKREF */
1370 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1371 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1372 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1373 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1374 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1375
1376 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1377 in the 16 bit signed offset from the appropriate base, and filling in the
1378 register field with the appropriate register (0, 2, or 13). */
1379 HOWTO (R_PPC_EMB_RELSDA, /* type */
1380 0, /* rightshift */
1381 1, /* size (0 = byte, 1 = short, 2 = long) */
1382 16, /* bitsize */
1383 TRUE, /* pc_relative */
1384 0, /* bitpos */
1385 complain_overflow_signed, /* complain_on_overflow */
1386 bfd_elf_generic_reloc, /* special_function */
1387 "R_PPC_EMB_RELSDA", /* name */
1388 FALSE, /* partial_inplace */
1389 0, /* src_mask */
1390 0xffff, /* dst_mask */
1391 FALSE), /* pcrel_offset */
1392
1393 HOWTO (R_PPC_IRELATIVE, /* type */
1394 0, /* rightshift */
1395 2, /* size (0 = byte, 1 = short, 2 = long) */
1396 32, /* bitsize */
1397 FALSE, /* pc_relative */
1398 0, /* bitpos */
1399 complain_overflow_bitfield, /* complain_on_overflow */
1400 bfd_elf_generic_reloc, /* special_function */
1401 "R_PPC_IRELATIVE", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffffffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1406
1407 /* A 16 bit relative relocation. */
1408 HOWTO (R_PPC_REL16, /* type */
1409 0, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 TRUE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_bitfield, /* complain_on_overflow */
1415 bfd_elf_generic_reloc, /* special_function */
1416 "R_PPC_REL16", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 TRUE), /* pcrel_offset */
1421
1422 /* A 16 bit relative relocation without overflow. */
1423 HOWTO (R_PPC_REL16_LO, /* type */
1424 0, /* rightshift */
1425 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 16, /* bitsize */
1427 TRUE, /* pc_relative */
1428 0, /* bitpos */
1429 complain_overflow_dont,/* complain_on_overflow */
1430 bfd_elf_generic_reloc, /* special_function */
1431 "R_PPC_REL16_LO", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 TRUE), /* pcrel_offset */
1436
1437 /* The high order 16 bits of a relative address. */
1438 HOWTO (R_PPC_REL16_HI, /* type */
1439 16, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 TRUE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_dont, /* complain_on_overflow */
1445 bfd_elf_generic_reloc, /* special_function */
1446 "R_PPC_REL16_HI", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xffff, /* dst_mask */
1450 TRUE), /* pcrel_offset */
1451
1452 /* The high order 16 bits of a relative address, plus 1 if the contents of
1453 the low 16 bits, treated as a signed number, is negative. */
1454 HOWTO (R_PPC_REL16_HA, /* type */
1455 16, /* rightshift */
1456 1, /* size (0 = byte, 1 = short, 2 = long) */
1457 16, /* bitsize */
1458 TRUE, /* pc_relative */
1459 0, /* bitpos */
1460 complain_overflow_dont, /* complain_on_overflow */
1461 ppc_elf_addr16_ha_reloc, /* special_function */
1462 "R_PPC_REL16_HA", /* name */
1463 FALSE, /* partial_inplace */
1464 0, /* src_mask */
1465 0xffff, /* dst_mask */
1466 TRUE), /* pcrel_offset */
1467
1468 /* GNU extension to record C++ vtable hierarchy. */
1469 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1470 0, /* rightshift */
1471 0, /* size (0 = byte, 1 = short, 2 = long) */
1472 0, /* bitsize */
1473 FALSE, /* pc_relative */
1474 0, /* bitpos */
1475 complain_overflow_dont, /* complain_on_overflow */
1476 NULL, /* special_function */
1477 "R_PPC_GNU_VTINHERIT", /* name */
1478 FALSE, /* partial_inplace */
1479 0, /* src_mask */
1480 0, /* dst_mask */
1481 FALSE), /* pcrel_offset */
1482
1483 /* GNU extension to record C++ vtable member usage. */
1484 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1485 0, /* rightshift */
1486 0, /* size (0 = byte, 1 = short, 2 = long) */
1487 0, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_dont, /* complain_on_overflow */
1491 NULL, /* special_function */
1492 "R_PPC_GNU_VTENTRY", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 0, /* dst_mask */
1496 FALSE), /* pcrel_offset */
1497
1498 /* Phony reloc to handle AIX style TOC entries. */
1499 HOWTO (R_PPC_TOC16, /* type */
1500 0, /* rightshift */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 16, /* bitsize */
1503 FALSE, /* pc_relative */
1504 0, /* bitpos */
1505 complain_overflow_signed, /* complain_on_overflow */
1506 bfd_elf_generic_reloc, /* special_function */
1507 "R_PPC_TOC16", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1512 };
1513 \f
1514 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1515
1516 static void
1517 ppc_elf_howto_init (void)
1518 {
1519 unsigned int i, type;
1520
1521 for (i = 0;
1522 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1523 i++)
1524 {
1525 type = ppc_elf_howto_raw[i].type;
1526 if (type >= (sizeof (ppc_elf_howto_table)
1527 / sizeof (ppc_elf_howto_table[0])))
1528 abort ();
1529 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1530 }
1531 }
1532
1533 static reloc_howto_type *
1534 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1535 bfd_reloc_code_real_type code)
1536 {
1537 enum elf_ppc_reloc_type r;
1538
1539 /* Initialize howto table if not already done. */
1540 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1541 ppc_elf_howto_init ();
1542
1543 switch (code)
1544 {
1545 default:
1546 return NULL;
1547
1548 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
1549 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
1550 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
1551 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1552 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
1553 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
1554 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
1555 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
1556 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
1557 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
1558 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
1559 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
1560 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
1561 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
1562 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1563 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
1564 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
1565 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
1566 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
1567 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
1568 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
1569 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
1570 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
1571 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
1572 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
1573 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
1574 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
1575 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
1576 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
1577 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1578 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
1579 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
1580 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
1581 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
1582 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
1583 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
1584 case BFD_RELOC_PPC_TLSGD: r = R_PPC_TLSGD; break;
1585 case BFD_RELOC_PPC_TLSLD: r = R_PPC_TLSLD; break;
1586 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1587 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1588 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
1589 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
1590 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
1591 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
1592 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1593 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
1594 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
1595 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
1596 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
1597 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
1598 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
1599 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
1600 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
1601 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
1602 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
1603 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
1604 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
1605 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
1606 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
1607 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
1608 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
1609 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
1610 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
1611 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
1612 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
1613 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
1614 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
1615 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
1616 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
1617 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
1618 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
1619 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
1620 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
1621 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
1622 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
1623 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
1624 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
1625 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
1626 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
1627 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
1628 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
1629 case BFD_RELOC_16_PCREL: r = R_PPC_REL16; break;
1630 case BFD_RELOC_LO16_PCREL: r = R_PPC_REL16_LO; break;
1631 case BFD_RELOC_HI16_PCREL: r = R_PPC_REL16_HI; break;
1632 case BFD_RELOC_HI16_S_PCREL: r = R_PPC_REL16_HA; break;
1633 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
1634 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
1635 }
1636
1637 return ppc_elf_howto_table[r];
1638 };
1639
1640 static reloc_howto_type *
1641 ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1642 const char *r_name)
1643 {
1644 unsigned int i;
1645
1646 for (i = 0;
1647 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1648 i++)
1649 if (ppc_elf_howto_raw[i].name != NULL
1650 && strcasecmp (ppc_elf_howto_raw[i].name, r_name) == 0)
1651 return &ppc_elf_howto_raw[i];
1652
1653 return NULL;
1654 }
1655
1656 /* Set the howto pointer for a PowerPC ELF reloc. */
1657
1658 static void
1659 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1660 arelent *cache_ptr,
1661 Elf_Internal_Rela *dst)
1662 {
1663 /* Initialize howto table if not already done. */
1664 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1665 ppc_elf_howto_init ();
1666
1667 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1668 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1669
1670 /* Just because the above assert didn't trigger doesn't mean that
1671 ELF32_R_TYPE (dst->r_info) is necessarily a valid relocation. */
1672 if (!cache_ptr->howto)
1673 {
1674 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
1675 abfd, ELF32_R_TYPE (dst->r_info));
1676 bfd_set_error (bfd_error_bad_value);
1677
1678 cache_ptr->howto = ppc_elf_howto_table[R_PPC_NONE];
1679 }
1680 }
1681
1682 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs. */
1683
1684 static bfd_reloc_status_type
1685 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1686 arelent *reloc_entry,
1687 asymbol *symbol,
1688 void *data ATTRIBUTE_UNUSED,
1689 asection *input_section,
1690 bfd *output_bfd,
1691 char **error_message ATTRIBUTE_UNUSED)
1692 {
1693 bfd_vma relocation;
1694
1695 if (output_bfd != NULL)
1696 {
1697 reloc_entry->address += input_section->output_offset;
1698 return bfd_reloc_ok;
1699 }
1700
1701 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1702 return bfd_reloc_outofrange;
1703
1704 if (bfd_is_com_section (symbol->section))
1705 relocation = 0;
1706 else
1707 relocation = symbol->value;
1708
1709 relocation += symbol->section->output_section->vma;
1710 relocation += symbol->section->output_offset;
1711 relocation += reloc_entry->addend;
1712 if (reloc_entry->howto->pc_relative)
1713 relocation -= reloc_entry->address;
1714
1715 reloc_entry->addend += (relocation & 0x8000) << 1;
1716
1717 return bfd_reloc_continue;
1718 }
1719
1720 static bfd_reloc_status_type
1721 ppc_elf_unhandled_reloc (bfd *abfd,
1722 arelent *reloc_entry,
1723 asymbol *symbol,
1724 void *data,
1725 asection *input_section,
1726 bfd *output_bfd,
1727 char **error_message)
1728 {
1729 /* If this is a relocatable link (output_bfd test tells us), just
1730 call the generic function. Any adjustment will be done at final
1731 link time. */
1732 if (output_bfd != NULL)
1733 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1734 input_section, output_bfd, error_message);
1735
1736 if (error_message != NULL)
1737 {
1738 static char buf[60];
1739 sprintf (buf, _("generic linker can't handle %s"),
1740 reloc_entry->howto->name);
1741 *error_message = buf;
1742 }
1743 return bfd_reloc_dangerous;
1744 }
1745 \f
1746 /* Sections created by the linker. */
1747
1748 typedef struct elf_linker_section
1749 {
1750 /* Pointer to the bfd section. */
1751 asection *section;
1752 /* Section name. */
1753 const char *name;
1754 /* Associated bss section name. */
1755 const char *bss_name;
1756 /* Associated symbol name. */
1757 const char *sym_name;
1758 /* Associated symbol. */
1759 struct elf_link_hash_entry *sym;
1760 } elf_linker_section_t;
1761
1762 /* Linked list of allocated pointer entries. This hangs off of the
1763 symbol lists, and provides allows us to return different pointers,
1764 based on different addend's. */
1765
1766 typedef struct elf_linker_section_pointers
1767 {
1768 /* next allocated pointer for this symbol */
1769 struct elf_linker_section_pointers *next;
1770 /* offset of pointer from beginning of section */
1771 bfd_vma offset;
1772 /* addend used */
1773 bfd_vma addend;
1774 /* which linker section this is */
1775 elf_linker_section_t *lsect;
1776 } elf_linker_section_pointers_t;
1777
1778 struct ppc_elf_obj_tdata
1779 {
1780 struct elf_obj_tdata elf;
1781
1782 /* A mapping from local symbols to offsets into the various linker
1783 sections added. This is index by the symbol index. */
1784 elf_linker_section_pointers_t **linker_section_pointers;
1785
1786 /* Flags used to auto-detect plt type. */
1787 unsigned int makes_plt_call : 1;
1788 unsigned int has_rel16 : 1;
1789 };
1790
1791 #define ppc_elf_tdata(bfd) \
1792 ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1793
1794 #define elf_local_ptr_offsets(bfd) \
1795 (ppc_elf_tdata (bfd)->linker_section_pointers)
1796
1797 #define is_ppc_elf(bfd) \
1798 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1799 && elf_object_id (bfd) == PPC32_ELF_DATA)
1800
1801 /* Override the generic function because we store some extras. */
1802
1803 static bfd_boolean
1804 ppc_elf_mkobject (bfd *abfd)
1805 {
1806 return bfd_elf_allocate_object (abfd, sizeof (struct ppc_elf_obj_tdata),
1807 PPC32_ELF_DATA);
1808 }
1809
1810 /* Fix bad default arch selected for a 32 bit input bfd when the
1811 default is 64 bit. */
1812
1813 static bfd_boolean
1814 ppc_elf_object_p (bfd *abfd)
1815 {
1816 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1817 {
1818 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1819
1820 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1821 {
1822 /* Relies on arch after 64 bit default being 32 bit default. */
1823 abfd->arch_info = abfd->arch_info->next;
1824 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1825 }
1826 }
1827 return TRUE;
1828 }
1829
1830 /* Function to set whether a module needs the -mrelocatable bit set. */
1831
1832 static bfd_boolean
1833 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1834 {
1835 BFD_ASSERT (!elf_flags_init (abfd)
1836 || elf_elfheader (abfd)->e_flags == flags);
1837
1838 elf_elfheader (abfd)->e_flags = flags;
1839 elf_flags_init (abfd) = TRUE;
1840 return TRUE;
1841 }
1842
1843 /* Support for core dump NOTE sections. */
1844
1845 static bfd_boolean
1846 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1847 {
1848 int offset;
1849 unsigned int size;
1850
1851 switch (note->descsz)
1852 {
1853 default:
1854 return FALSE;
1855
1856 case 268: /* Linux/PPC. */
1857 /* pr_cursig */
1858 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1859
1860 /* pr_pid */
1861 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1862
1863 /* pr_reg */
1864 offset = 72;
1865 size = 192;
1866
1867 break;
1868 }
1869
1870 /* Make a ".reg/999" section. */
1871 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1872 size, note->descpos + offset);
1873 }
1874
1875 static bfd_boolean
1876 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1877 {
1878 switch (note->descsz)
1879 {
1880 default:
1881 return FALSE;
1882
1883 case 128: /* Linux/PPC elf_prpsinfo. */
1884 elf_tdata (abfd)->core_program
1885 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1886 elf_tdata (abfd)->core_command
1887 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1888 }
1889
1890 /* Note that for some reason, a spurious space is tacked
1891 onto the end of the args in some (at least one anyway)
1892 implementations, so strip it off if it exists. */
1893
1894 {
1895 char *command = elf_tdata (abfd)->core_command;
1896 int n = strlen (command);
1897
1898 if (0 < n && command[n - 1] == ' ')
1899 command[n - 1] = '\0';
1900 }
1901
1902 return TRUE;
1903 }
1904
1905 static char *
1906 ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
1907 {
1908 switch (note_type)
1909 {
1910 default:
1911 return NULL;
1912
1913 case NT_PRPSINFO:
1914 {
1915 char data[128];
1916 va_list ap;
1917
1918 va_start (ap, note_type);
1919 memset (data, 0, 32);
1920 strncpy (data + 32, va_arg (ap, const char *), 16);
1921 strncpy (data + 48, va_arg (ap, const char *), 80);
1922 va_end (ap);
1923 return elfcore_write_note (abfd, buf, bufsiz,
1924 "CORE", note_type, data, sizeof (data));
1925 }
1926
1927 case NT_PRSTATUS:
1928 {
1929 char data[268];
1930 va_list ap;
1931 long pid;
1932 int cursig;
1933 const void *greg;
1934
1935 va_start (ap, note_type);
1936 memset (data, 0, 72);
1937 pid = va_arg (ap, long);
1938 bfd_put_32 (abfd, pid, data + 24);
1939 cursig = va_arg (ap, int);
1940 bfd_put_16 (abfd, cursig, data + 12);
1941 greg = va_arg (ap, const void *);
1942 memcpy (data + 72, greg, 192);
1943 memset (data + 264, 0, 4);
1944 va_end (ap);
1945 return elfcore_write_note (abfd, buf, bufsiz,
1946 "CORE", note_type, data, sizeof (data));
1947 }
1948 }
1949 }
1950
1951 /* Return address for Ith PLT stub in section PLT, for relocation REL
1952 or (bfd_vma) -1 if it should not be included. */
1953
1954 static bfd_vma
1955 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1956 const asection *plt ATTRIBUTE_UNUSED,
1957 const arelent *rel)
1958 {
1959 return rel->address;
1960 }
1961
1962 /* Handle a PowerPC specific section when reading an object file. This
1963 is called when bfd_section_from_shdr finds a section with an unknown
1964 type. */
1965
1966 static bfd_boolean
1967 ppc_elf_section_from_shdr (bfd *abfd,
1968 Elf_Internal_Shdr *hdr,
1969 const char *name,
1970 int shindex)
1971 {
1972 asection *newsect;
1973 flagword flags;
1974
1975 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1976 return FALSE;
1977
1978 newsect = hdr->bfd_section;
1979 flags = bfd_get_section_flags (abfd, newsect);
1980 if (hdr->sh_flags & SHF_EXCLUDE)
1981 flags |= SEC_EXCLUDE;
1982
1983 if (hdr->sh_type == SHT_ORDERED)
1984 flags |= SEC_SORT_ENTRIES;
1985
1986 bfd_set_section_flags (abfd, newsect, flags);
1987 return TRUE;
1988 }
1989
1990 /* Set up any other section flags and such that may be necessary. */
1991
1992 static bfd_boolean
1993 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1994 Elf_Internal_Shdr *shdr,
1995 asection *asect)
1996 {
1997 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
1998 shdr->sh_flags |= SHF_EXCLUDE;
1999
2000 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2001 shdr->sh_type = SHT_ORDERED;
2002
2003 return TRUE;
2004 }
2005
2006 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
2007 need to bump up the number of section headers. */
2008
2009 static int
2010 ppc_elf_additional_program_headers (bfd *abfd,
2011 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2012 {
2013 asection *s;
2014 int ret = 0;
2015
2016 s = bfd_get_section_by_name (abfd, ".sbss2");
2017 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2018 ++ret;
2019
2020 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2021 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2022 ++ret;
2023
2024 return ret;
2025 }
2026
2027 /* Add extra PPC sections -- Note, for now, make .sbss2 and
2028 .PPC.EMB.sbss0 a normal section, and not a bss section so
2029 that the linker doesn't crater when trying to make more than
2030 2 sections. */
2031
2032 static const struct bfd_elf_special_section ppc_elf_special_sections[] =
2033 {
2034 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
2035 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2036 { STRING_COMMA_LEN (".sbss2"), -2, SHT_PROGBITS, SHF_ALLOC },
2037 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2038 { STRING_COMMA_LEN (".sdata2"), -2, SHT_PROGBITS, SHF_ALLOC },
2039 { STRING_COMMA_LEN (".tags"), 0, SHT_ORDERED, SHF_ALLOC },
2040 { STRING_COMMA_LEN (".PPC.EMB.apuinfo"), 0, SHT_NOTE, 0 },
2041 { STRING_COMMA_LEN (".PPC.EMB.sbss0"), 0, SHT_PROGBITS, SHF_ALLOC },
2042 { STRING_COMMA_LEN (".PPC.EMB.sdata0"), 0, SHT_PROGBITS, SHF_ALLOC },
2043 { NULL, 0, 0, 0, 0 }
2044 };
2045
2046 /* This is what we want for new plt/got. */
2047 static struct bfd_elf_special_section ppc_alt_plt =
2048 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC };
2049
2050 static const struct bfd_elf_special_section *
2051 ppc_elf_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
2052 {
2053 const struct bfd_elf_special_section *ssect;
2054
2055 /* See if this is one of the special sections. */
2056 if (sec->name == NULL)
2057 return NULL;
2058
2059 ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
2060 sec->use_rela_p);
2061 if (ssect != NULL)
2062 {
2063 if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
2064 ssect = &ppc_alt_plt;
2065 return ssect;
2066 }
2067
2068 return _bfd_elf_get_sec_type_attr (abfd, sec);
2069 }
2070 \f
2071 /* Very simple linked list structure for recording apuinfo values. */
2072 typedef struct apuinfo_list
2073 {
2074 struct apuinfo_list *next;
2075 unsigned long value;
2076 }
2077 apuinfo_list;
2078
2079 static apuinfo_list *head;
2080
2081
2082 static void
2083 apuinfo_list_init (void)
2084 {
2085 head = NULL;
2086 }
2087
2088 static void
2089 apuinfo_list_add (unsigned long value)
2090 {
2091 apuinfo_list *entry = head;
2092
2093 while (entry != NULL)
2094 {
2095 if (entry->value == value)
2096 return;
2097 entry = entry->next;
2098 }
2099
2100 entry = bfd_malloc (sizeof (* entry));
2101 if (entry == NULL)
2102 return;
2103
2104 entry->value = value;
2105 entry->next = head;
2106 head = entry;
2107 }
2108
2109 static unsigned
2110 apuinfo_list_length (void)
2111 {
2112 apuinfo_list *entry;
2113 unsigned long count;
2114
2115 for (entry = head, count = 0;
2116 entry;
2117 entry = entry->next)
2118 ++ count;
2119
2120 return count;
2121 }
2122
2123 static inline unsigned long
2124 apuinfo_list_element (unsigned long number)
2125 {
2126 apuinfo_list * entry;
2127
2128 for (entry = head;
2129 entry && number --;
2130 entry = entry->next)
2131 ;
2132
2133 return entry ? entry->value : 0;
2134 }
2135
2136 static void
2137 apuinfo_list_finish (void)
2138 {
2139 apuinfo_list *entry;
2140
2141 for (entry = head; entry;)
2142 {
2143 apuinfo_list *next = entry->next;
2144 free (entry);
2145 entry = next;
2146 }
2147
2148 head = NULL;
2149 }
2150
2151 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
2152 #define APUINFO_LABEL "APUinfo"
2153
2154 /* Scan the input BFDs and create a linked list of
2155 the APUinfo values that will need to be emitted. */
2156
2157 static void
2158 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
2159 {
2160 bfd *ibfd;
2161 asection *asec;
2162 char *buffer = NULL;
2163 bfd_size_type largest_input_size = 0;
2164 unsigned i;
2165 unsigned num_entries;
2166 unsigned long length;
2167 const char *error_message = NULL;
2168
2169 if (link_info == NULL)
2170 return;
2171
2172 apuinfo_list_init ();
2173
2174 /* Read in the input sections contents. */
2175 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2176 {
2177 unsigned long datum;
2178
2179 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2180 if (asec == NULL)
2181 continue;
2182
2183 error_message = _("corrupt %s section in %B");
2184 length = asec->size;
2185 if (length < 20)
2186 goto fail;
2187
2188 if (largest_input_size < asec->size)
2189 {
2190 if (buffer)
2191 free (buffer);
2192 largest_input_size = asec->size;
2193 buffer = bfd_malloc (largest_input_size);
2194 if (!buffer)
2195 return;
2196 }
2197
2198 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
2199 || (bfd_bread (buffer, length, ibfd) != length))
2200 {
2201 error_message = _("unable to read in %s section from %B");
2202 goto fail;
2203 }
2204
2205 /* Verify the contents of the header. Note - we have to
2206 extract the values this way in order to allow for a
2207 host whose endian-ness is different from the target. */
2208 datum = bfd_get_32 (ibfd, buffer);
2209 if (datum != sizeof APUINFO_LABEL)
2210 goto fail;
2211
2212 datum = bfd_get_32 (ibfd, buffer + 8);
2213 if (datum != 0x2)
2214 goto fail;
2215
2216 if (strcmp (buffer + 12, APUINFO_LABEL) != 0)
2217 goto fail;
2218
2219 /* Get the number of bytes used for apuinfo entries. */
2220 datum = bfd_get_32 (ibfd, buffer + 4);
2221 if (datum + 20 != length)
2222 goto fail;
2223
2224 /* Scan the apuinfo section, building a list of apuinfo numbers. */
2225 for (i = 0; i < datum; i += 4)
2226 apuinfo_list_add (bfd_get_32 (ibfd, buffer + 20 + i));
2227 }
2228
2229 error_message = NULL;
2230
2231 /* Compute the size of the output section. */
2232 num_entries = apuinfo_list_length ();
2233
2234 if (num_entries)
2235 {
2236 /* Set the output section size, if it exists. */
2237 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2238 if (asec && ! bfd_set_section_size (abfd, asec, 20 + num_entries * 4))
2239 {
2240 ibfd = abfd;
2241 error_message = _("warning: unable to set size of %s section in %B");
2242 }
2243 }
2244
2245 fail:
2246 if (buffer)
2247 free (buffer);
2248
2249 if (error_message)
2250 (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
2251 }
2252
2253 /* Prevent the output section from accumulating the input sections'
2254 contents. We have already stored this in our linked list structure. */
2255
2256 static bfd_boolean
2257 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
2258 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2259 asection *asec,
2260 bfd_byte *contents ATTRIBUTE_UNUSED)
2261 {
2262 return (apuinfo_list_length ()
2263 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
2264 }
2265
2266 /* Finally we can generate the output section. */
2267
2268 static void
2269 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
2270 {
2271 bfd_byte *buffer;
2272 asection *asec;
2273 unsigned i;
2274 unsigned num_entries;
2275 bfd_size_type length;
2276
2277 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2278 if (asec == NULL)
2279 return;
2280
2281 if (apuinfo_list_length () == 0)
2282 return;
2283
2284 length = asec->size;
2285 if (length < 20)
2286 return;
2287
2288 buffer = bfd_malloc (length);
2289 if (buffer == NULL)
2290 {
2291 (*_bfd_error_handler)
2292 (_("failed to allocate space for new APUinfo section."));
2293 return;
2294 }
2295
2296 /* Create the apuinfo header. */
2297 num_entries = apuinfo_list_length ();
2298 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2299 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2300 bfd_put_32 (abfd, 0x2, buffer + 8);
2301 strcpy ((char *) buffer + 12, APUINFO_LABEL);
2302
2303 length = 20;
2304 for (i = 0; i < num_entries; i++)
2305 {
2306 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2307 length += 4;
2308 }
2309
2310 if (length != asec->size)
2311 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2312
2313 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2314 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2315
2316 free (buffer);
2317
2318 apuinfo_list_finish ();
2319 }
2320 \f
2321 static bfd_boolean
2322 is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
2323 {
2324 bfd_byte buf[GLINK_ENTRY_SIZE];
2325
2326 if (!bfd_get_section_contents (abfd, glink, buf, off, GLINK_ENTRY_SIZE))
2327 return FALSE;
2328
2329 return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
2330 && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
2331 && bfd_get_32 (abfd, buf + 8) == MTCTR_11
2332 && bfd_get_32 (abfd, buf + 12) == BCTR);
2333 }
2334
2335 static bfd_boolean
2336 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2337 {
2338 bfd_vma vma = *(bfd_vma *) ptr;
2339 return ((section->flags & SEC_ALLOC) != 0
2340 && section->vma <= vma
2341 && vma < section->vma + section->size);
2342 }
2343
2344 static long
2345 ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms,
2346 long dynsymcount, asymbol **dynsyms,
2347 asymbol **ret)
2348 {
2349 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2350 asection *plt, *relplt, *dynamic, *glink;
2351 bfd_vma glink_vma = 0;
2352 bfd_vma resolv_vma = 0;
2353 bfd_vma stub_vma;
2354 asymbol *s;
2355 arelent *p;
2356 long count, i;
2357 size_t size;
2358 char *names;
2359 bfd_byte buf[4];
2360
2361 *ret = NULL;
2362
2363 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
2364 return 0;
2365
2366 if (dynsymcount <= 0)
2367 return 0;
2368
2369 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2370 if (relplt == NULL)
2371 return 0;
2372
2373 plt = bfd_get_section_by_name (abfd, ".plt");
2374 if (plt == NULL)
2375 return 0;
2376
2377 /* Call common code to handle old-style executable PLTs. */
2378 if (elf_section_flags (plt) & SHF_EXECINSTR)
2379 return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
2380 dynsymcount, dynsyms, ret);
2381
2382 /* If this object was prelinked, the prelinker stored the address
2383 of .glink at got[1]. If it wasn't prelinked, got[1] will be zero. */
2384 dynamic = bfd_get_section_by_name (abfd, ".dynamic");
2385 if (dynamic != NULL)
2386 {
2387 bfd_byte *dynbuf, *extdyn, *extdynend;
2388 size_t extdynsize;
2389 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2390
2391 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2392 return -1;
2393
2394 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2395 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2396
2397 extdyn = dynbuf;
2398 extdynend = extdyn + dynamic->size;
2399 for (; extdyn < extdynend; extdyn += extdynsize)
2400 {
2401 Elf_Internal_Dyn dyn;
2402 (*swap_dyn_in) (abfd, extdyn, &dyn);
2403
2404 if (dyn.d_tag == DT_NULL)
2405 break;
2406
2407 if (dyn.d_tag == DT_PPC_GOT)
2408 {
2409 unsigned int g_o_t = dyn.d_un.d_val;
2410 asection *got = bfd_get_section_by_name (abfd, ".got");
2411 if (got != NULL
2412 && bfd_get_section_contents (abfd, got, buf,
2413 g_o_t - got->vma + 4, 4))
2414 glink_vma = bfd_get_32 (abfd, buf);
2415 break;
2416 }
2417 }
2418 free (dynbuf);
2419 }
2420
2421 /* Otherwise we read the first plt entry. */
2422 if (glink_vma == 0)
2423 {
2424 if (bfd_get_section_contents (abfd, plt, buf, 0, 4))
2425 glink_vma = bfd_get_32 (abfd, buf);
2426 }
2427
2428 if (glink_vma == 0)
2429 return 0;
2430
2431 /* The .glink section usually does not survive the final
2432 link; search for the section (usually .text) where the
2433 glink stubs now reside. */
2434 glink = bfd_sections_find_if (abfd, section_covers_vma, &glink_vma);
2435 if (glink == NULL)
2436 return 0;
2437
2438 /* Determine glink PLT resolver by reading the relative branch
2439 from the first glink stub. */
2440 if (bfd_get_section_contents (abfd, glink, buf,
2441 glink_vma - glink->vma, 4))
2442 {
2443 unsigned int insn = bfd_get_32 (abfd, buf);
2444
2445 /* The first glink stub may either branch to the resolver ... */
2446 insn ^= B;
2447 if ((insn & ~0x3fffffc) == 0)
2448 resolv_vma = glink_vma + (insn ^ 0x2000000) - 0x2000000;
2449
2450 /* ... or fall through a bunch of NOPs. */
2451 else if ((insn ^ B ^ NOP) == 0)
2452 for (i = 4;
2453 bfd_get_section_contents (abfd, glink, buf,
2454 glink_vma - glink->vma + i, 4);
2455 i += 4)
2456 if (bfd_get_32 (abfd, buf) != NOP)
2457 {
2458 resolv_vma = glink_vma + i;
2459 break;
2460 }
2461 }
2462
2463 count = relplt->size / sizeof (Elf32_External_Rela);
2464 stub_vma = glink_vma - (bfd_vma) count * 16;
2465 /* If the stubs are those for -shared/-pie then we might have
2466 multiple stubs for each plt entry. If that is the case then
2467 there is no way to associate stubs with their plt entries short
2468 of figuring out the GOT pointer value used in the stub. */
2469 if (!is_nonpic_glink_stub (abfd, glink,
2470 glink_vma - GLINK_ENTRY_SIZE - glink->vma))
2471 return 0;
2472
2473 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2474 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
2475 return -1;
2476
2477 size = count * sizeof (asymbol);
2478 p = relplt->relocation;
2479 for (i = 0; i < count; i++, p++)
2480 {
2481 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2482 if (p->addend != 0)
2483 size += sizeof ("+0x") - 1 + 8;
2484 }
2485
2486 size += sizeof (asymbol) + sizeof ("__glink");
2487
2488 if (resolv_vma)
2489 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2490
2491 s = *ret = bfd_malloc (size);
2492 if (s == NULL)
2493 return -1;
2494
2495 names = (char *) (s + count + 1 + (resolv_vma != 0));
2496 p = relplt->relocation;
2497 for (i = 0; i < count; i++, p++)
2498 {
2499 size_t len;
2500
2501 *s = **p->sym_ptr_ptr;
2502 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2503 we are defining a symbol, ensure one of them is set. */
2504 if ((s->flags & BSF_LOCAL) == 0)
2505 s->flags |= BSF_GLOBAL;
2506 s->flags |= BSF_SYNTHETIC;
2507 s->section = glink;
2508 s->value = stub_vma - glink->vma;
2509 s->name = names;
2510 s->udata.p = NULL;
2511 len = strlen ((*p->sym_ptr_ptr)->name);
2512 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2513 names += len;
2514 if (p->addend != 0)
2515 {
2516 memcpy (names, "+0x", sizeof ("+0x") - 1);
2517 names += sizeof ("+0x") - 1;
2518 bfd_sprintf_vma (abfd, names, p->addend);
2519 names += strlen (names);
2520 }
2521 memcpy (names, "@plt", sizeof ("@plt"));
2522 names += sizeof ("@plt");
2523 ++s;
2524 stub_vma += 16;
2525 }
2526
2527 /* Add a symbol at the start of the glink branch table. */
2528 memset (s, 0, sizeof *s);
2529 s->the_bfd = abfd;
2530 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2531 s->section = glink;
2532 s->value = glink_vma - glink->vma;
2533 s->name = names;
2534 memcpy (names, "__glink", sizeof ("__glink"));
2535 names += sizeof ("__glink");
2536 s++;
2537 count++;
2538
2539 if (resolv_vma)
2540 {
2541 /* Add a symbol for the glink PLT resolver. */
2542 memset (s, 0, sizeof *s);
2543 s->the_bfd = abfd;
2544 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2545 s->section = glink;
2546 s->value = resolv_vma - glink->vma;
2547 s->name = names;
2548 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
2549 names += sizeof ("__glink_PLTresolve");
2550 s++;
2551 count++;
2552 }
2553
2554 return count;
2555 }
2556 \f
2557 /* The following functions are specific to the ELF linker, while
2558 functions above are used generally. They appear in this file more
2559 or less in the order in which they are called. eg.
2560 ppc_elf_check_relocs is called early in the link process,
2561 ppc_elf_finish_dynamic_sections is one of the last functions
2562 called. */
2563
2564 /* The PPC linker needs to keep track of the number of relocs that it
2565 decides to copy as dynamic relocs in check_relocs for each symbol.
2566 This is so that it can later discard them if they are found to be
2567 unnecessary. We store the information in a field extending the
2568 regular ELF linker hash table. */
2569
2570 struct ppc_elf_dyn_relocs
2571 {
2572 struct ppc_elf_dyn_relocs *next;
2573
2574 /* The input section of the reloc. */
2575 asection *sec;
2576
2577 /* Total number of relocs copied for the input section. */
2578 bfd_size_type count;
2579
2580 /* Number of pc-relative relocs copied for the input section. */
2581 bfd_size_type pc_count;
2582 };
2583
2584 /* Track PLT entries needed for a given symbol. We might need more
2585 than one glink entry per symbol when generating a pic binary. */
2586 struct plt_entry
2587 {
2588 struct plt_entry *next;
2589
2590 /* -fPIC uses multiple GOT sections, one per file, called ".got2".
2591 This field stores the offset into .got2 used to initialise the
2592 GOT pointer reg. It will always be at least 32768. (Current
2593 gcc always uses an offset of 32768, but ld -r will pack .got2
2594 sections together resulting in larger offsets). */
2595 bfd_vma addend;
2596
2597 /* The .got2 section. */
2598 asection *sec;
2599
2600 /* PLT refcount or offset. */
2601 union
2602 {
2603 bfd_signed_vma refcount;
2604 bfd_vma offset;
2605 } plt;
2606
2607 /* .glink stub offset. */
2608 bfd_vma glink_offset;
2609 };
2610
2611 /* Of those relocs that might be copied as dynamic relocs, this function
2612 selects those that must be copied when linking a shared library,
2613 even when the symbol is local. */
2614
2615 static int
2616 must_be_dyn_reloc (struct bfd_link_info *info,
2617 enum elf_ppc_reloc_type r_type)
2618 {
2619 switch (r_type)
2620 {
2621 default:
2622 return 1;
2623
2624 case R_PPC_REL24:
2625 case R_PPC_REL14:
2626 case R_PPC_REL14_BRTAKEN:
2627 case R_PPC_REL14_BRNTAKEN:
2628 case R_PPC_REL32:
2629 return 0;
2630
2631 case R_PPC_TPREL32:
2632 case R_PPC_TPREL16:
2633 case R_PPC_TPREL16_LO:
2634 case R_PPC_TPREL16_HI:
2635 case R_PPC_TPREL16_HA:
2636 return !info->executable;
2637 }
2638 }
2639
2640 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2641 copying dynamic variables from a shared lib into an app's dynbss
2642 section, and instead use a dynamic relocation to point into the
2643 shared lib. */
2644 #define ELIMINATE_COPY_RELOCS 1
2645
2646 /* PPC ELF linker hash entry. */
2647
2648 struct ppc_elf_link_hash_entry
2649 {
2650 struct elf_link_hash_entry elf;
2651
2652 /* If this symbol is used in the linker created sections, the processor
2653 specific backend uses this field to map the field into the offset
2654 from the beginning of the section. */
2655 elf_linker_section_pointers_t *linker_section_pointer;
2656
2657 /* Track dynamic relocs copied for this symbol. */
2658 struct ppc_elf_dyn_relocs *dyn_relocs;
2659
2660 /* Contexts in which symbol is used in the GOT (or TOC).
2661 TLS_GD .. TLS_TLS bits are or'd into the mask as the
2662 corresponding relocs are encountered during check_relocs.
2663 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2664 indicate the corresponding GOT entry type is not needed. */
2665 #define TLS_GD 1 /* GD reloc. */
2666 #define TLS_LD 2 /* LD reloc. */
2667 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
2668 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2669 #define TLS_TLS 16 /* Any TLS reloc. */
2670 #define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
2671 #define PLT_IFUNC 64 /* STT_GNU_IFUNC. */
2672 char tls_mask;
2673
2674 /* Nonzero if we have seen a small data relocation referring to this
2675 symbol. */
2676 unsigned char has_sda_refs;
2677 };
2678
2679 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2680
2681 /* PPC ELF linker hash table. */
2682
2683 struct ppc_elf_link_hash_table
2684 {
2685 struct elf_link_hash_table elf;
2686
2687 /* Short-cuts to get to dynamic linker sections. */
2688 asection *got;
2689 asection *relgot;
2690 asection *glink;
2691 asection *plt;
2692 asection *relplt;
2693 asection *iplt;
2694 asection *reliplt;
2695 asection *dynbss;
2696 asection *relbss;
2697 asection *dynsbss;
2698 asection *relsbss;
2699 elf_linker_section_t sdata[2];
2700 asection *sbss;
2701
2702 /* The (unloaded but important) .rela.plt.unloaded on VxWorks. */
2703 asection *srelplt2;
2704
2705 /* The .got.plt section (VxWorks only)*/
2706 asection *sgotplt;
2707
2708 /* Shortcut to __tls_get_addr. */
2709 struct elf_link_hash_entry *tls_get_addr;
2710
2711 /* The bfd that forced an old-style PLT. */
2712 bfd *old_bfd;
2713
2714 /* TLS local dynamic got entry handling. */
2715 union {
2716 bfd_signed_vma refcount;
2717 bfd_vma offset;
2718 } tlsld_got;
2719
2720 /* Offset of branch table to PltResolve function in glink. */
2721 bfd_vma glink_pltresolve;
2722
2723 /* Size of reserved GOT entries. */
2724 unsigned int got_header_size;
2725 /* Non-zero if allocating the header left a gap. */
2726 unsigned int got_gap;
2727
2728 /* The type of PLT we have chosen to use. */
2729 enum ppc_elf_plt_type plt_type;
2730
2731 /* Set if we should emit symbols for stubs. */
2732 unsigned int emit_stub_syms:1;
2733
2734 /* Set if __tls_get_addr optimization should not be done. */
2735 unsigned int no_tls_get_addr_opt:1;
2736
2737 /* True if the target system is VxWorks. */
2738 unsigned int is_vxworks:1;
2739
2740 /* The size of PLT entries. */
2741 int plt_entry_size;
2742 /* The distance between adjacent PLT slots. */
2743 int plt_slot_size;
2744 /* The size of the first PLT entry. */
2745 int plt_initial_entry_size;
2746
2747 /* Small local sym cache. */
2748 struct sym_cache sym_cache;
2749 };
2750
2751 /* Get the PPC ELF linker hash table from a link_info structure. */
2752
2753 #define ppc_elf_hash_table(p) \
2754 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2755 == PPC32_ELF_DATA ? ((struct ppc_elf_link_hash_table *) ((p)->hash)) : NULL)
2756
2757 /* Create an entry in a PPC ELF linker hash table. */
2758
2759 static struct bfd_hash_entry *
2760 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2761 struct bfd_hash_table *table,
2762 const char *string)
2763 {
2764 /* Allocate the structure if it has not already been allocated by a
2765 subclass. */
2766 if (entry == NULL)
2767 {
2768 entry = bfd_hash_allocate (table,
2769 sizeof (struct ppc_elf_link_hash_entry));
2770 if (entry == NULL)
2771 return entry;
2772 }
2773
2774 /* Call the allocation method of the superclass. */
2775 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2776 if (entry != NULL)
2777 {
2778 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2779 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2780 ppc_elf_hash_entry (entry)->tls_mask = 0;
2781 }
2782
2783 return entry;
2784 }
2785
2786 /* Create a PPC ELF linker hash table. */
2787
2788 static struct bfd_link_hash_table *
2789 ppc_elf_link_hash_table_create (bfd *abfd)
2790 {
2791 struct ppc_elf_link_hash_table *ret;
2792
2793 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2794 if (ret == NULL)
2795 return NULL;
2796
2797 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
2798 ppc_elf_link_hash_newfunc,
2799 sizeof (struct ppc_elf_link_hash_entry),
2800 PPC32_ELF_DATA))
2801 {
2802 free (ret);
2803 return NULL;
2804 }
2805
2806 ret->elf.init_plt_refcount.refcount = 0;
2807 ret->elf.init_plt_refcount.glist = NULL;
2808 ret->elf.init_plt_offset.offset = 0;
2809 ret->elf.init_plt_offset.glist = NULL;
2810
2811 ret->sdata[0].name = ".sdata";
2812 ret->sdata[0].sym_name = "_SDA_BASE_";
2813 ret->sdata[0].bss_name = ".sbss";
2814
2815 ret->sdata[1].name = ".sdata2";
2816 ret->sdata[1].sym_name = "_SDA2_BASE_";
2817 ret->sdata[1].bss_name = ".sbss2";
2818
2819 ret->plt_entry_size = 12;
2820 ret->plt_slot_size = 8;
2821 ret->plt_initial_entry_size = 72;
2822
2823 return &ret->elf.root;
2824 }
2825
2826 /* Create .got and the related sections. */
2827
2828 static bfd_boolean
2829 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2830 {
2831 struct ppc_elf_link_hash_table *htab;
2832 asection *s;
2833 flagword flags;
2834
2835 if (!_bfd_elf_create_got_section (abfd, info))
2836 return FALSE;
2837
2838 htab = ppc_elf_hash_table (info);
2839 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2840 if (s == NULL)
2841 abort ();
2842
2843 if (htab->is_vxworks)
2844 {
2845 htab->sgotplt = bfd_get_section_by_name (abfd, ".got.plt");
2846 if (!htab->sgotplt)
2847 abort ();
2848 }
2849 else
2850 {
2851 /* The powerpc .got has a blrl instruction in it. Mark it
2852 executable. */
2853 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
2854 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2855 if (!bfd_set_section_flags (abfd, s, flags))
2856 return FALSE;
2857 }
2858
2859 htab->relgot = bfd_get_section_by_name (abfd, ".rela.got");
2860 if (!htab->relgot)
2861 abort ();
2862
2863 return TRUE;
2864 }
2865
2866 static bfd_boolean
2867 ppc_elf_create_glink (bfd *abfd, struct bfd_link_info *info)
2868 {
2869 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2870 asection *s;
2871 flagword flags;
2872
2873 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS
2874 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2875 s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags);
2876 htab->glink = s;
2877 if (s == NULL
2878 || !bfd_set_section_alignment (abfd, s, 4))
2879 return FALSE;
2880
2881 flags = SEC_ALLOC | SEC_LINKER_CREATED;
2882 s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
2883 htab->iplt = s;
2884 if (s == NULL
2885 || !bfd_set_section_alignment (abfd, s, 4))
2886 return FALSE;
2887
2888 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2889 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2890 s = bfd_make_section_with_flags (abfd, ".rela.iplt", flags);
2891 htab->reliplt = s;
2892 if (s == NULL
2893 || ! bfd_set_section_alignment (abfd, s, 2))
2894 return FALSE;
2895 return TRUE;
2896 }
2897
2898 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2899 to output sections (just like _bfd_elf_create_dynamic_sections has
2900 to create .dynbss and .rela.bss). */
2901
2902 static bfd_boolean
2903 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2904 {
2905 struct ppc_elf_link_hash_table *htab;
2906 asection *s;
2907 flagword flags;
2908
2909 htab = ppc_elf_hash_table (info);
2910
2911 if (htab->got == NULL
2912 && !ppc_elf_create_got (abfd, info))
2913 return FALSE;
2914
2915 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2916 return FALSE;
2917
2918 if (htab->glink == NULL
2919 && !ppc_elf_create_glink (abfd, info))
2920 return FALSE;
2921
2922 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2923 s = bfd_make_section_with_flags (abfd, ".dynsbss",
2924 SEC_ALLOC | SEC_LINKER_CREATED);
2925 htab->dynsbss = s;
2926 if (s == NULL)
2927 return FALSE;
2928
2929 if (! info->shared)
2930 {
2931 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2932 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2933 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2934 s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
2935 htab->relsbss = s;
2936 if (s == NULL
2937 || ! bfd_set_section_alignment (abfd, s, 2))
2938 return FALSE;
2939 }
2940
2941 if (htab->is_vxworks
2942 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2943 return FALSE;
2944
2945 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2946 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2947 if (s == NULL)
2948 abort ();
2949
2950 flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
2951 if (htab->plt_type == PLT_VXWORKS)
2952 /* The VxWorks PLT is a loaded section with contents. */
2953 flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
2954 return bfd_set_section_flags (abfd, s, flags);
2955 }
2956
2957 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2958
2959 static void
2960 ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
2961 struct elf_link_hash_entry *dir,
2962 struct elf_link_hash_entry *ind)
2963 {
2964 struct ppc_elf_link_hash_entry *edir, *eind;
2965
2966 edir = (struct ppc_elf_link_hash_entry *) dir;
2967 eind = (struct ppc_elf_link_hash_entry *) ind;
2968
2969 if (eind->dyn_relocs != NULL)
2970 {
2971 if (edir->dyn_relocs != NULL)
2972 {
2973 struct ppc_elf_dyn_relocs **pp;
2974 struct ppc_elf_dyn_relocs *p;
2975
2976 /* Add reloc counts against the indirect sym to the direct sym
2977 list. Merge any entries against the same section. */
2978 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2979 {
2980 struct ppc_elf_dyn_relocs *q;
2981
2982 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2983 if (q->sec == p->sec)
2984 {
2985 q->pc_count += p->pc_count;
2986 q->count += p->count;
2987 *pp = p->next;
2988 break;
2989 }
2990 if (q == NULL)
2991 pp = &p->next;
2992 }
2993 *pp = edir->dyn_relocs;
2994 }
2995
2996 edir->dyn_relocs = eind->dyn_relocs;
2997 eind->dyn_relocs = NULL;
2998 }
2999
3000 edir->tls_mask |= eind->tls_mask;
3001 edir->has_sda_refs |= eind->has_sda_refs;
3002
3003 /* If called to transfer flags for a weakdef during processing
3004 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3005 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3006 if (!(ELIMINATE_COPY_RELOCS
3007 && eind->elf.root.type != bfd_link_hash_indirect
3008 && edir->elf.dynamic_adjusted))
3009 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3010
3011 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3012 edir->elf.ref_regular |= eind->elf.ref_regular;
3013 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3014 edir->elf.needs_plt |= eind->elf.needs_plt;
3015 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3016
3017 /* If we were called to copy over info for a weak sym, that's all. */
3018 if (eind->elf.root.type != bfd_link_hash_indirect)
3019 return;
3020
3021 /* Copy over the GOT refcount entries that we may have already seen to
3022 the symbol which just became indirect. */
3023 edir->elf.got.refcount += eind->elf.got.refcount;
3024 eind->elf.got.refcount = 0;
3025
3026 /* And plt entries. */
3027 if (eind->elf.plt.plist != NULL)
3028 {
3029 if (edir->elf.plt.plist != NULL)
3030 {
3031 struct plt_entry **entp;
3032 struct plt_entry *ent;
3033
3034 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3035 {
3036 struct plt_entry *dent;
3037
3038 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3039 if (dent->sec == ent->sec && dent->addend == ent->addend)
3040 {
3041 dent->plt.refcount += ent->plt.refcount;
3042 *entp = ent->next;
3043 break;
3044 }
3045 if (dent == NULL)
3046 entp = &ent->next;
3047 }
3048 *entp = edir->elf.plt.plist;
3049 }
3050
3051 edir->elf.plt.plist = eind->elf.plt.plist;
3052 eind->elf.plt.plist = NULL;
3053 }
3054
3055 if (eind->elf.dynindx != -1)
3056 {
3057 if (edir->elf.dynindx != -1)
3058 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3059 edir->elf.dynstr_index);
3060 edir->elf.dynindx = eind->elf.dynindx;
3061 edir->elf.dynstr_index = eind->elf.dynstr_index;
3062 eind->elf.dynindx = -1;
3063 eind->elf.dynstr_index = 0;
3064 }
3065 }
3066
3067 /* Hook called by the linker routine which adds symbols from an object
3068 file. We use it to put .comm items in .sbss, and not .bss. */
3069
3070 static bfd_boolean
3071 ppc_elf_add_symbol_hook (bfd *abfd,
3072 struct bfd_link_info *info,
3073 Elf_Internal_Sym *sym,
3074 const char **namep ATTRIBUTE_UNUSED,
3075 flagword *flagsp ATTRIBUTE_UNUSED,
3076 asection **secp,
3077 bfd_vma *valp)
3078 {
3079 if (sym->st_shndx == SHN_COMMON
3080 && !info->relocatable
3081 && is_ppc_elf (info->output_bfd)
3082 && sym->st_size <= elf_gp_size (abfd))
3083 {
3084 /* Common symbols less than or equal to -G nn bytes are automatically
3085 put into .sbss. */
3086 struct ppc_elf_link_hash_table *htab;
3087
3088 htab = ppc_elf_hash_table (info);
3089 if (htab->sbss == NULL)
3090 {
3091 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
3092
3093 if (!htab->elf.dynobj)
3094 htab->elf.dynobj = abfd;
3095
3096 htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3097 ".sbss",
3098 flags);
3099 if (htab->sbss == NULL)
3100 return FALSE;
3101 }
3102
3103 *secp = htab->sbss;
3104 *valp = sym->st_size;
3105 }
3106
3107 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3108 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
3109
3110 return TRUE;
3111 }
3112 \f
3113 static bfd_boolean
3114 create_sdata_sym (struct bfd_link_info *info, elf_linker_section_t *lsect)
3115 {
3116 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
3117
3118 lsect->sym = elf_link_hash_lookup (&htab->elf, lsect->sym_name,
3119 TRUE, FALSE, TRUE);
3120 if (lsect->sym == NULL)
3121 return FALSE;
3122 if (lsect->sym->root.type == bfd_link_hash_new)
3123 lsect->sym->non_elf = 0;
3124 lsect->sym->ref_regular = 1;
3125 _bfd_elf_link_hash_hide_symbol (info, lsect->sym, TRUE);
3126 return TRUE;
3127 }
3128
3129 /* Create a special linker section. */
3130
3131 static bfd_boolean
3132 ppc_elf_create_linker_section (bfd *abfd,
3133 struct bfd_link_info *info,
3134 flagword flags,
3135 elf_linker_section_t *lsect)
3136 {
3137 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
3138 asection *s;
3139
3140 flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3141 | SEC_LINKER_CREATED);
3142
3143 /* Record the first bfd that needs the special sections. */
3144 if (!htab->elf.dynobj)
3145 htab->elf.dynobj = abfd;
3146
3147 s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3148 lsect->name,
3149 flags);
3150 if (s == NULL
3151 || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
3152 return FALSE;
3153 lsect->section = s;
3154
3155 return create_sdata_sym (info, lsect);
3156 }
3157
3158 /* Find a linker generated pointer with a given addend and type. */
3159
3160 static elf_linker_section_pointers_t *
3161 elf_find_pointer_linker_section
3162 (elf_linker_section_pointers_t *linker_pointers,
3163 bfd_vma addend,
3164 elf_linker_section_t *lsect)
3165 {
3166 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
3167 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
3168 return linker_pointers;
3169
3170 return NULL;
3171 }
3172
3173 /* Allocate a pointer to live in a linker created section. */
3174
3175 static bfd_boolean
3176 elf_create_pointer_linker_section (bfd *abfd,
3177 elf_linker_section_t *lsect,
3178 struct elf_link_hash_entry *h,
3179 const Elf_Internal_Rela *rel)
3180 {
3181 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
3182 elf_linker_section_pointers_t *linker_section_ptr;
3183 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
3184 bfd_size_type amt;
3185
3186 BFD_ASSERT (lsect != NULL);
3187
3188 /* Is this a global symbol? */
3189 if (h != NULL)
3190 {
3191 struct ppc_elf_link_hash_entry *eh;
3192
3193 /* Has this symbol already been allocated? If so, our work is done. */
3194 eh = (struct ppc_elf_link_hash_entry *) h;
3195 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
3196 rel->r_addend,
3197 lsect))
3198 return TRUE;
3199
3200 ptr_linker_section_ptr = &eh->linker_section_pointer;
3201 }
3202 else
3203 {
3204 BFD_ASSERT (is_ppc_elf (abfd));
3205
3206 /* Allocation of a pointer to a local symbol. */
3207 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
3208
3209 /* Allocate a table to hold the local symbols if first time. */
3210 if (!ptr)
3211 {
3212 unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
3213
3214 amt = num_symbols;
3215 amt *= sizeof (elf_linker_section_pointers_t *);
3216 ptr = bfd_zalloc (abfd, amt);
3217
3218 if (!ptr)
3219 return FALSE;
3220
3221 elf_local_ptr_offsets (abfd) = ptr;
3222 }
3223
3224 /* Has this symbol already been allocated? If so, our work is done. */
3225 if (elf_find_pointer_linker_section (ptr[r_symndx],
3226 rel->r_addend,
3227 lsect))
3228 return TRUE;
3229
3230 ptr_linker_section_ptr = &ptr[r_symndx];
3231 }
3232
3233 /* Allocate space for a pointer in the linker section, and allocate
3234 a new pointer record from internal memory. */
3235 BFD_ASSERT (ptr_linker_section_ptr != NULL);
3236 amt = sizeof (elf_linker_section_pointers_t);
3237 linker_section_ptr = bfd_alloc (abfd, amt);
3238
3239 if (!linker_section_ptr)
3240 return FALSE;
3241
3242 linker_section_ptr->next = *ptr_linker_section_ptr;
3243 linker_section_ptr->addend = rel->r_addend;
3244 linker_section_ptr->lsect = lsect;
3245 *ptr_linker_section_ptr = linker_section_ptr;
3246
3247 linker_section_ptr->offset = lsect->section->size;
3248 lsect->section->size += 4;
3249
3250 #ifdef DEBUG
3251 fprintf (stderr,
3252 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
3253 lsect->name, (long) linker_section_ptr->offset,
3254 (long) lsect->section->size);
3255 #endif
3256
3257 return TRUE;
3258 }
3259
3260 static struct plt_entry **
3261 update_local_sym_info (bfd *abfd,
3262 Elf_Internal_Shdr *symtab_hdr,
3263 unsigned long r_symndx,
3264 int tls_type)
3265 {
3266 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3267 struct plt_entry **local_plt;
3268 char *local_got_tls_masks;
3269
3270 if (local_got_refcounts == NULL)
3271 {
3272 bfd_size_type size = symtab_hdr->sh_info;
3273
3274 size *= (sizeof (*local_got_refcounts)
3275 + sizeof (*local_plt)
3276 + sizeof (*local_got_tls_masks));
3277 local_got_refcounts = bfd_zalloc (abfd, size);
3278 if (local_got_refcounts == NULL)
3279 return NULL;
3280 elf_local_got_refcounts (abfd) = local_got_refcounts;
3281 }
3282
3283 local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
3284 local_got_tls_masks = (char *) (local_plt + symtab_hdr->sh_info);
3285 local_got_tls_masks[r_symndx] |= tls_type;
3286 if (tls_type != PLT_IFUNC)
3287 local_got_refcounts[r_symndx] += 1;
3288 return local_plt + r_symndx;
3289 }
3290
3291 static bfd_boolean
3292 update_plt_info (bfd *abfd, struct plt_entry **plist,
3293 asection *sec, bfd_vma addend)
3294 {
3295 struct plt_entry *ent;
3296
3297 if (addend < 32768)
3298 sec = NULL;
3299 for (ent = *plist; ent != NULL; ent = ent->next)
3300 if (ent->sec == sec && ent->addend == addend)
3301 break;
3302 if (ent == NULL)
3303 {
3304 bfd_size_type amt = sizeof (*ent);
3305 ent = bfd_alloc (abfd, amt);
3306 if (ent == NULL)
3307 return FALSE;
3308 ent->next = *plist;
3309 ent->sec = sec;
3310 ent->addend = addend;
3311 ent->plt.refcount = 0;
3312 *plist = ent;
3313 }
3314 ent->plt.refcount += 1;
3315 return TRUE;
3316 }
3317
3318 static struct plt_entry *
3319 find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
3320 {
3321 struct plt_entry *ent;
3322
3323 if (addend < 32768)
3324 sec = NULL;
3325 for (ent = *plist; ent != NULL; ent = ent->next)
3326 if (ent->sec == sec && ent->addend == addend)
3327 break;
3328 return ent;
3329 }
3330
3331 static bfd_boolean
3332 is_branch_reloc (enum elf_ppc_reloc_type r_type)
3333 {
3334 return (r_type == R_PPC_PLTREL24
3335 || r_type == R_PPC_LOCAL24PC
3336 || r_type == R_PPC_REL24
3337 || r_type == R_PPC_REL14
3338 || r_type == R_PPC_REL14_BRTAKEN
3339 || r_type == R_PPC_REL14_BRNTAKEN
3340 || r_type == R_PPC_ADDR24
3341 || r_type == R_PPC_ADDR14
3342 || r_type == R_PPC_ADDR14_BRTAKEN
3343 || r_type == R_PPC_ADDR14_BRNTAKEN);
3344 }
3345
3346 static void
3347 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3348 {
3349 (*_bfd_error_handler)
3350 (_("%B: relocation %s cannot be used when making a shared object"),
3351 abfd,
3352 ppc_elf_howto_table[r_type]->name);
3353 bfd_set_error (bfd_error_bad_value);
3354 }
3355
3356 /* Look through the relocs for a section during the first phase, and
3357 allocate space in the global offset table or procedure linkage
3358 table. */
3359
3360 static bfd_boolean
3361 ppc_elf_check_relocs (bfd *abfd,
3362 struct bfd_link_info *info,
3363 asection *sec,
3364 const Elf_Internal_Rela *relocs)
3365 {
3366 struct ppc_elf_link_hash_table *htab;
3367 Elf_Internal_Shdr *symtab_hdr;
3368 struct elf_link_hash_entry **sym_hashes;
3369 const Elf_Internal_Rela *rel;
3370 const Elf_Internal_Rela *rel_end;
3371 asection *got2, *sreloc;
3372 struct elf_link_hash_entry *tga;
3373
3374 if (info->relocatable)
3375 return TRUE;
3376
3377 /* Don't do anything special with non-loaded, non-alloced sections.
3378 In particular, any relocs in such sections should not affect GOT
3379 and PLT reference counting (ie. we don't allow them to create GOT
3380 or PLT entries), there's no possibility or desire to optimize TLS
3381 relocs, and there's not much point in propagating relocs to shared
3382 libs that the dynamic linker won't relocate. */
3383 if ((sec->flags & SEC_ALLOC) == 0)
3384 return TRUE;
3385
3386 #ifdef DEBUG
3387 _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
3388 sec, abfd);
3389 #endif
3390
3391 BFD_ASSERT (is_ppc_elf (abfd));
3392
3393 /* Initialize howto table if not already done. */
3394 if (!ppc_elf_howto_table[R_PPC_ADDR32])
3395 ppc_elf_howto_init ();
3396
3397 htab = ppc_elf_hash_table (info);
3398 if (htab->glink == NULL)
3399 {
3400 if (htab->elf.dynobj == NULL)
3401 htab->elf.dynobj = abfd;
3402 if (!ppc_elf_create_glink (htab->elf.dynobj, info))
3403 return FALSE;
3404 }
3405 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3406 FALSE, FALSE, TRUE);
3407 symtab_hdr = &elf_symtab_hdr (abfd);
3408 sym_hashes = elf_sym_hashes (abfd);
3409 got2 = bfd_get_section_by_name (abfd, ".got2");
3410 sreloc = NULL;
3411
3412 rel_end = relocs + sec->reloc_count;
3413 for (rel = relocs; rel < rel_end; rel++)
3414 {
3415 unsigned long r_symndx;
3416 enum elf_ppc_reloc_type r_type;
3417 struct elf_link_hash_entry *h;
3418 int tls_type;
3419
3420 r_symndx = ELF32_R_SYM (rel->r_info);
3421 if (r_symndx < symtab_hdr->sh_info)
3422 h = NULL;
3423 else
3424 {
3425 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3426 while (h->root.type == bfd_link_hash_indirect
3427 || h->root.type == bfd_link_hash_warning)
3428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3429 }
3430
3431 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3432 This shows up in particular in an R_PPC_ADDR32 in the eabi
3433 startup code. */
3434 if (h != NULL
3435 && htab->got == NULL
3436 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3437 {
3438 if (htab->elf.dynobj == NULL)
3439 htab->elf.dynobj = abfd;
3440 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3441 return FALSE;
3442 BFD_ASSERT (h == htab->elf.hgot);
3443 }
3444
3445 tls_type = 0;
3446 r_type = ELF32_R_TYPE (rel->r_info);
3447 if (h == NULL && !htab->is_vxworks)
3448 {
3449 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3450 abfd, r_symndx);
3451 if (isym == NULL)
3452 return FALSE;
3453
3454 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3455 && (!info->shared
3456 || is_branch_reloc (r_type)))
3457 {
3458 struct plt_entry **ifunc;
3459 bfd_vma addend;
3460
3461 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
3462 PLT_IFUNC);
3463 if (ifunc == NULL)
3464 return FALSE;
3465
3466 /* STT_GNU_IFUNC symbols must have a PLT entry;
3467 In a non-pie executable even when there are
3468 no plt calls. */
3469 addend = 0;
3470 if (r_type == R_PPC_PLTREL24)
3471 {
3472 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3473 if (info->shared)
3474 addend = rel->r_addend;
3475 }
3476 if (!update_plt_info (abfd, ifunc, got2, addend))
3477 return FALSE;
3478 }
3479 }
3480
3481 if (!htab->is_vxworks
3482 && is_branch_reloc (r_type)
3483 && h != NULL
3484 && h == tga)
3485 {
3486 if (rel != relocs
3487 && (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
3488 || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
3489 /* We have a new-style __tls_get_addr call with a marker
3490 reloc. */
3491 ;
3492 else
3493 /* Mark this section as having an old-style call. */
3494 sec->has_tls_get_addr_call = 1;
3495 }
3496
3497 switch (r_type)
3498 {
3499 case R_PPC_TLSGD:
3500 case R_PPC_TLSLD:
3501 /* These special tls relocs tie a call to __tls_get_addr with
3502 its parameter symbol. */
3503 break;
3504
3505 case R_PPC_GOT_TLSLD16:
3506 case R_PPC_GOT_TLSLD16_LO:
3507 case R_PPC_GOT_TLSLD16_HI:
3508 case R_PPC_GOT_TLSLD16_HA:
3509 tls_type = TLS_TLS | TLS_LD;
3510 goto dogottls;
3511
3512 case R_PPC_GOT_TLSGD16:
3513 case R_PPC_GOT_TLSGD16_LO:
3514 case R_PPC_GOT_TLSGD16_HI:
3515 case R_PPC_GOT_TLSGD16_HA:
3516 tls_type = TLS_TLS | TLS_GD;
3517 goto dogottls;
3518
3519 case R_PPC_GOT_TPREL16:
3520 case R_PPC_GOT_TPREL16_LO:
3521 case R_PPC_GOT_TPREL16_HI:
3522 case R_PPC_GOT_TPREL16_HA:
3523 if (!info->executable)
3524 info->flags |= DF_STATIC_TLS;
3525 tls_type = TLS_TLS | TLS_TPREL;
3526 goto dogottls;
3527
3528 case R_PPC_GOT_DTPREL16:
3529 case R_PPC_GOT_DTPREL16_LO:
3530 case R_PPC_GOT_DTPREL16_HI:
3531 case R_PPC_GOT_DTPREL16_HA:
3532 tls_type = TLS_TLS | TLS_DTPREL;
3533 dogottls:
3534 sec->has_tls_reloc = 1;
3535 /* Fall thru */
3536
3537 /* GOT16 relocations */
3538 case R_PPC_GOT16:
3539 case R_PPC_GOT16_LO:
3540 case R_PPC_GOT16_HI:
3541 case R_PPC_GOT16_HA:
3542 /* This symbol requires a global offset table entry. */
3543 if (htab->got == NULL)
3544 {
3545 if (htab->elf.dynobj == NULL)
3546 htab->elf.dynobj = abfd;
3547 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3548 return FALSE;
3549 }
3550 if (h != NULL)
3551 {
3552 h->got.refcount += 1;
3553 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3554 }
3555 else
3556 /* This is a global offset table entry for a local symbol. */
3557 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3558 return FALSE;
3559
3560 /* We may also need a plt entry if the symbol turns out to be
3561 an ifunc. */
3562 if (h != NULL && !info->shared)
3563 {
3564 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3565 return FALSE;
3566 }
3567 break;
3568
3569 /* Indirect .sdata relocation. */
3570 case R_PPC_EMB_SDAI16:
3571 if (info->shared)
3572 {
3573 bad_shared_reloc (abfd, r_type);
3574 return FALSE;
3575 }
3576 if (htab->sdata[0].section == NULL
3577 && !ppc_elf_create_linker_section (abfd, info, 0,
3578 &htab->sdata[0]))
3579 return FALSE;
3580 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
3581 h, rel))
3582 return FALSE;
3583 if (h != NULL)
3584 {
3585 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3586 h->non_got_ref = TRUE;
3587 }
3588 break;
3589
3590 /* Indirect .sdata2 relocation. */
3591 case R_PPC_EMB_SDA2I16:
3592 if (info->shared)
3593 {
3594 bad_shared_reloc (abfd, r_type);
3595 return FALSE;
3596 }
3597 if (htab->sdata[1].section == NULL
3598 && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
3599 &htab->sdata[1]))
3600 return FALSE;
3601 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
3602 h, rel))
3603 return FALSE;
3604 if (h != NULL)
3605 {
3606 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3607 h->non_got_ref = TRUE;
3608 }
3609 break;
3610
3611 case R_PPC_SDAREL16:
3612 if (htab->sdata[0].sym == NULL
3613 && !create_sdata_sym (info, &htab->sdata[0]))
3614 return FALSE;
3615 if (h != NULL)
3616 {
3617 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3618 h->non_got_ref = TRUE;
3619 }
3620 break;
3621
3622 case R_PPC_EMB_SDA2REL:
3623 if (info->shared)
3624 {
3625 bad_shared_reloc (abfd, r_type);
3626 return FALSE;
3627 }
3628 if (htab->sdata[1].sym == NULL
3629 && !create_sdata_sym (info, &htab->sdata[1]))
3630 return FALSE;
3631 if (h != NULL)
3632 {
3633 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3634 h->non_got_ref = TRUE;
3635 }
3636 break;
3637
3638 case R_PPC_EMB_SDA21:
3639 case R_PPC_EMB_RELSDA:
3640 if (info->shared)
3641 {
3642 bad_shared_reloc (abfd, r_type);
3643 return FALSE;
3644 }
3645 if (htab->sdata[0].sym == NULL
3646 && !create_sdata_sym (info, &htab->sdata[0]))
3647 return FALSE;
3648 if (htab->sdata[1].sym == NULL
3649 && !create_sdata_sym (info, &htab->sdata[1]))
3650 return FALSE;
3651 if (h != NULL)
3652 {
3653 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3654 h->non_got_ref = TRUE;
3655 }
3656 break;
3657
3658 case R_PPC_EMB_NADDR32:
3659 case R_PPC_EMB_NADDR16:
3660 case R_PPC_EMB_NADDR16_LO:
3661 case R_PPC_EMB_NADDR16_HI:
3662 case R_PPC_EMB_NADDR16_HA:
3663 if (info->shared)
3664 {
3665 bad_shared_reloc (abfd, r_type);
3666 return FALSE;
3667 }
3668 if (h != NULL)
3669 h->non_got_ref = TRUE;
3670 break;
3671
3672 case R_PPC_PLTREL24:
3673 if (h == NULL)
3674 break;
3675 /* Fall through */
3676 case R_PPC_PLT32:
3677 case R_PPC_PLTREL32:
3678 case R_PPC_PLT16_LO:
3679 case R_PPC_PLT16_HI:
3680 case R_PPC_PLT16_HA:
3681 #ifdef DEBUG
3682 fprintf (stderr, "Reloc requires a PLT entry\n");
3683 #endif
3684 /* This symbol requires a procedure linkage table entry. We
3685 actually build the entry in finish_dynamic_symbol,
3686 because this might be a case of linking PIC code without
3687 linking in any dynamic objects, in which case we don't
3688 need to generate a procedure linkage table after all. */
3689
3690 if (h == NULL)
3691 {
3692 /* It does not make sense to have a procedure linkage
3693 table entry for a local symbol. */
3694 (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
3695 "local symbol"),
3696 abfd,
3697 sec,
3698 (long) rel->r_offset,
3699 ppc_elf_howto_table[r_type]->name);
3700 bfd_set_error (bfd_error_bad_value);
3701 return FALSE;
3702 }
3703 else
3704 {
3705 bfd_vma addend = 0;
3706
3707 if (r_type == R_PPC_PLTREL24)
3708 {
3709 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3710 if (info->shared)
3711 addend = rel->r_addend;
3712 }
3713 h->needs_plt = 1;
3714 if (!update_plt_info (abfd, &h->plt.plist, got2, addend))
3715 return FALSE;
3716 }
3717 break;
3718
3719 /* The following relocations don't need to propagate the
3720 relocation if linking a shared object since they are
3721 section relative. */
3722 case R_PPC_SECTOFF:
3723 case R_PPC_SECTOFF_LO:
3724 case R_PPC_SECTOFF_HI:
3725 case R_PPC_SECTOFF_HA:
3726 case R_PPC_DTPREL16:
3727 case R_PPC_DTPREL16_LO:
3728 case R_PPC_DTPREL16_HI:
3729 case R_PPC_DTPREL16_HA:
3730 case R_PPC_TOC16:
3731 break;
3732
3733 case R_PPC_REL16:
3734 case R_PPC_REL16_LO:
3735 case R_PPC_REL16_HI:
3736 case R_PPC_REL16_HA:
3737 ppc_elf_tdata (abfd)->has_rel16 = 1;
3738 break;
3739
3740 /* These are just markers. */
3741 case R_PPC_TLS:
3742 case R_PPC_EMB_MRKREF:
3743 case R_PPC_NONE:
3744 case R_PPC_max:
3745 case R_PPC_RELAX:
3746 case R_PPC_RELAX_PLT:
3747 case R_PPC_RELAX_PLTREL24:
3748 break;
3749
3750 /* These should only appear in dynamic objects. */
3751 case R_PPC_COPY:
3752 case R_PPC_GLOB_DAT:
3753 case R_PPC_JMP_SLOT:
3754 case R_PPC_RELATIVE:
3755 case R_PPC_IRELATIVE:
3756 break;
3757
3758 /* These aren't handled yet. We'll report an error later. */
3759 case R_PPC_ADDR30:
3760 case R_PPC_EMB_RELSEC16:
3761 case R_PPC_EMB_RELST_LO:
3762 case R_PPC_EMB_RELST_HI:
3763 case R_PPC_EMB_RELST_HA:
3764 case R_PPC_EMB_BIT_FLD:
3765 break;
3766
3767 /* This refers only to functions defined in the shared library. */
3768 case R_PPC_LOCAL24PC:
3769 if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
3770 {
3771 htab->plt_type = PLT_OLD;
3772 htab->old_bfd = abfd;
3773 }
3774 break;
3775
3776 /* This relocation describes the C++ object vtable hierarchy.
3777 Reconstruct it for later use during GC. */
3778 case R_PPC_GNU_VTINHERIT:
3779 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3780 return FALSE;
3781 break;
3782
3783 /* This relocation describes which C++ vtable entries are actually
3784 used. Record for later use during GC. */
3785 case R_PPC_GNU_VTENTRY:
3786 BFD_ASSERT (h != NULL);
3787 if (h != NULL
3788 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3789 return FALSE;
3790 break;
3791
3792 /* We shouldn't really be seeing these. */
3793 case R_PPC_TPREL32:
3794 case R_PPC_TPREL16:
3795 case R_PPC_TPREL16_LO:
3796 case R_PPC_TPREL16_HI:
3797 case R_PPC_TPREL16_HA:
3798 if (!info->executable)
3799 info->flags |= DF_STATIC_TLS;
3800 goto dodyn;
3801
3802 /* Nor these. */
3803 case R_PPC_DTPMOD32:
3804 case R_PPC_DTPREL32:
3805 goto dodyn;
3806
3807 case R_PPC_REL32:
3808 if (h == NULL
3809 && got2 != NULL
3810 && (sec->flags & SEC_CODE) != 0
3811 && info->shared
3812 && htab->plt_type == PLT_UNSET)
3813 {
3814 /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
3815 the start of a function, which assembles to a REL32
3816 reference to .got2. If we detect one of these, then
3817 force the old PLT layout because the linker cannot
3818 reliably deduce the GOT pointer value needed for
3819 PLT call stubs. */
3820 asection *s;
3821 Elf_Internal_Sym *isym;
3822
3823 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3824 abfd, r_symndx);
3825 if (isym == NULL)
3826 return FALSE;
3827
3828 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3829 if (s == got2)
3830 {
3831 htab->plt_type = PLT_OLD;
3832 htab->old_bfd = abfd;
3833 }
3834 }
3835 if (h == NULL || h == htab->elf.hgot)
3836 break;
3837 /* fall through */
3838
3839 case R_PPC_ADDR32:
3840 case R_PPC_ADDR16:
3841 case R_PPC_ADDR16_LO:
3842 case R_PPC_ADDR16_HI:
3843 case R_PPC_ADDR16_HA:
3844 case R_PPC_UADDR32:
3845 case R_PPC_UADDR16:
3846 if (h != NULL && !info->shared)
3847 {
3848 /* We may need a plt entry if the symbol turns out to be
3849 a function defined in a dynamic object. */
3850 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3851 return FALSE;
3852
3853 /* We may need a copy reloc too. */
3854 h->non_got_ref = 1;
3855 h->pointer_equality_needed = 1;
3856 }
3857 goto dodyn;
3858
3859 case R_PPC_REL24:
3860 case R_PPC_REL14:
3861 case R_PPC_REL14_BRTAKEN:
3862 case R_PPC_REL14_BRNTAKEN:
3863 if (h == NULL)
3864 break;
3865 if (h == htab->elf.hgot)
3866 {
3867 if (htab->plt_type == PLT_UNSET)
3868 {
3869 htab->plt_type = PLT_OLD;
3870 htab->old_bfd = abfd;
3871 }
3872 break;
3873 }
3874 /* fall through */
3875
3876 case R_PPC_ADDR24:
3877 case R_PPC_ADDR14:
3878 case R_PPC_ADDR14_BRTAKEN:
3879 case R_PPC_ADDR14_BRNTAKEN:
3880 if (h != NULL && !info->shared)
3881 {
3882 /* We may need a plt entry if the symbol turns out to be
3883 a function defined in a dynamic object. */
3884 h->needs_plt = 1;
3885 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3886 return FALSE;
3887 break;
3888 }
3889
3890 dodyn:
3891 /* If we are creating a shared library, and this is a reloc
3892 against a global symbol, or a non PC relative reloc
3893 against a local symbol, then we need to copy the reloc
3894 into the shared library. However, if we are linking with
3895 -Bsymbolic, we do not need to copy a reloc against a
3896 global symbol which is defined in an object we are
3897 including in the link (i.e., DEF_REGULAR is set). At
3898 this point we have not seen all the input files, so it is
3899 possible that DEF_REGULAR is not set now but will be set
3900 later (it is never cleared). In case of a weak definition,
3901 DEF_REGULAR may be cleared later by a strong definition in
3902 a shared library. We account for that possibility below by
3903 storing information in the dyn_relocs field of the hash
3904 table entry. A similar situation occurs when creating
3905 shared libraries and symbol visibility changes render the
3906 symbol local.
3907
3908 If on the other hand, we are creating an executable, we
3909 may need to keep relocations for symbols satisfied by a
3910 dynamic library if we manage to avoid copy relocs for the
3911 symbol. */
3912 if ((info->shared
3913 && (must_be_dyn_reloc (info, r_type)
3914 || (h != NULL
3915 && (! info->symbolic
3916 || h->root.type == bfd_link_hash_defweak
3917 || !h->def_regular))))
3918 || (ELIMINATE_COPY_RELOCS
3919 && !info->shared
3920 && h != NULL
3921 && (h->root.type == bfd_link_hash_defweak
3922 || !h->def_regular)))
3923 {
3924 struct ppc_elf_dyn_relocs *p;
3925 struct ppc_elf_dyn_relocs **rel_head;
3926
3927 #ifdef DEBUG
3928 fprintf (stderr,
3929 "ppc_elf_check_relocs needs to "
3930 "create relocation for %s\n",
3931 (h && h->root.root.string
3932 ? h->root.root.string : "<unknown>"));
3933 #endif
3934 if (sreloc == NULL)
3935 {
3936 if (htab->elf.dynobj == NULL)
3937 htab->elf.dynobj = abfd;
3938
3939 sreloc = _bfd_elf_make_dynamic_reloc_section
3940 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ TRUE);
3941
3942 if (sreloc == NULL)
3943 return FALSE;
3944 }
3945
3946 /* If this is a global symbol, we count the number of
3947 relocations we need for this symbol. */
3948 if (h != NULL)
3949 {
3950 rel_head = &ppc_elf_hash_entry (h)->dyn_relocs;
3951 }
3952 else
3953 {
3954 /* Track dynamic relocs needed for local syms too.
3955 We really need local syms available to do this
3956 easily. Oh well. */
3957 asection *s;
3958 void *vpp;
3959 Elf_Internal_Sym *isym;
3960
3961 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3962 abfd, r_symndx);
3963 if (isym == NULL)
3964 return FALSE;
3965
3966 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3967 if (s == NULL)
3968 s = sec;
3969
3970 vpp = &elf_section_data (s)->local_dynrel;
3971 rel_head = (struct ppc_elf_dyn_relocs **) vpp;
3972 }
3973
3974 p = *rel_head;
3975 if (p == NULL || p->sec != sec)
3976 {
3977 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3978 if (p == NULL)
3979 return FALSE;
3980 p->next = *rel_head;
3981 *rel_head = p;
3982 p->sec = sec;
3983 p->count = 0;
3984 p->pc_count = 0;
3985 }
3986
3987 p->count += 1;
3988 if (!must_be_dyn_reloc (info, r_type))
3989 p->pc_count += 1;
3990 }
3991
3992 break;
3993 }
3994 }
3995
3996 return TRUE;
3997 }
3998 \f
3999
4000 /* Merge object attributes from IBFD into OBFD. Raise an error if
4001 there are conflicting attributes. */
4002 static bfd_boolean
4003 ppc_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
4004 {
4005 obj_attribute *in_attr, *in_attrs;
4006 obj_attribute *out_attr, *out_attrs;
4007
4008 if (!elf_known_obj_attributes_proc (obfd)[0].i)
4009 {
4010 /* This is the first object. Copy the attributes. */
4011 _bfd_elf_copy_obj_attributes (ibfd, obfd);
4012
4013 /* Use the Tag_null value to indicate the attributes have been
4014 initialized. */
4015 elf_known_obj_attributes_proc (obfd)[0].i = 1;
4016
4017 return TRUE;
4018 }
4019
4020 in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
4021 out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
4022
4023 /* Check for conflicting Tag_GNU_Power_ABI_FP attributes and merge
4024 non-conflicting ones. */
4025 in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
4026 out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
4027 if (in_attr->i != out_attr->i)
4028 {
4029 out_attr->type = 1;
4030 if (out_attr->i == 0)
4031 out_attr->i = in_attr->i;
4032 else if (in_attr->i == 0)
4033 ;
4034 else if (out_attr->i == 1 && in_attr->i == 2)
4035 _bfd_error_handler
4036 (_("Warning: %B uses hard float, %B uses soft float"), obfd, ibfd);
4037 else if (out_attr->i == 1 && in_attr->i == 3)
4038 _bfd_error_handler
4039 (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"),
4040 obfd, ibfd);
4041 else if (out_attr->i == 3 && in_attr->i == 1)
4042 _bfd_error_handler
4043 (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"),
4044 ibfd, obfd);
4045 else if (out_attr->i == 3 && in_attr->i == 2)
4046 _bfd_error_handler
4047 (_("Warning: %B uses soft float, %B uses single-precision hard float"),
4048 ibfd, obfd);
4049 else if (out_attr->i == 2 && (in_attr->i == 1 || in_attr->i == 3))
4050 _bfd_error_handler
4051 (_("Warning: %B uses hard float, %B uses soft float"), ibfd, obfd);
4052 else if (in_attr->i > 3)
4053 _bfd_error_handler
4054 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
4055 in_attr->i);
4056 else
4057 _bfd_error_handler
4058 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
4059 out_attr->i);
4060 }
4061
4062 /* Check for conflicting Tag_GNU_Power_ABI_Vector attributes and
4063 merge non-conflicting ones. */
4064 in_attr = &in_attrs[Tag_GNU_Power_ABI_Vector];
4065 out_attr = &out_attrs[Tag_GNU_Power_ABI_Vector];
4066 if (in_attr->i != out_attr->i)
4067 {
4068 const char *in_abi = NULL, *out_abi = NULL;
4069
4070 switch (in_attr->i)
4071 {
4072 case 1: in_abi = "generic"; break;
4073 case 2: in_abi = "AltiVec"; break;
4074 case 3: in_abi = "SPE"; break;
4075 }
4076
4077 switch (out_attr->i)
4078 {
4079 case 1: out_abi = "generic"; break;
4080 case 2: out_abi = "AltiVec"; break;
4081 case 3: out_abi = "SPE"; break;
4082 }
4083
4084 out_attr->type = 1;
4085 if (out_attr->i == 0)
4086 out_attr->i = in_attr->i;
4087 else if (in_attr->i == 0)
4088 ;
4089 /* For now, allow generic to transition to AltiVec or SPE
4090 without a warning. If GCC marked files with their stack
4091 alignment and used don't-care markings for files which are
4092 not affected by the vector ABI, we could warn about this
4093 case too. */
4094 else if (out_attr->i == 1)
4095 out_attr->i = in_attr->i;
4096 else if (in_attr->i == 1)
4097 ;
4098 else if (in_abi == NULL)
4099 _bfd_error_handler
4100 (_("Warning: %B uses unknown vector ABI %d"), ibfd,
4101 in_attr->i);
4102 else if (out_abi == NULL)
4103 _bfd_error_handler
4104 (_("Warning: %B uses unknown vector ABI %d"), obfd,
4105 in_attr->i);
4106 else
4107 _bfd_error_handler
4108 (_("Warning: %B uses vector ABI \"%s\", %B uses \"%s\""),
4109 ibfd, obfd, in_abi, out_abi);
4110 }
4111
4112 /* Check for conflicting Tag_GNU_Power_ABI_Struct_Return attributes
4113 and merge non-conflicting ones. */
4114 in_attr = &in_attrs[Tag_GNU_Power_ABI_Struct_Return];
4115 out_attr = &out_attrs[Tag_GNU_Power_ABI_Struct_Return];
4116 if (in_attr->i != out_attr->i)
4117 {
4118 out_attr->type = 1;
4119 if (out_attr->i == 0)
4120 out_attr->i = in_attr->i;
4121 else if (in_attr->i == 0)
4122 ;
4123 else if (out_attr->i == 1 && in_attr->i == 2)
4124 _bfd_error_handler
4125 (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), obfd, ibfd);
4126 else if (out_attr->i == 2 && in_attr->i == 1)
4127 _bfd_error_handler
4128 (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), ibfd, obfd);
4129 else if (in_attr->i > 2)
4130 _bfd_error_handler
4131 (_("Warning: %B uses unknown small structure return convention %d"), ibfd,
4132 in_attr->i);
4133 else
4134 _bfd_error_handler
4135 (_("Warning: %B uses unknown small structure return convention %d"), obfd,
4136 out_attr->i);
4137 }
4138
4139 /* Merge Tag_compatibility attributes and any common GNU ones. */
4140 _bfd_elf_merge_object_attributes (ibfd, obfd);
4141
4142 return TRUE;
4143 }
4144
4145 /* Merge backend specific data from an object file to the output
4146 object file when linking. */
4147
4148 static bfd_boolean
4149 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4150 {
4151 flagword old_flags;
4152 flagword new_flags;
4153 bfd_boolean error;
4154
4155 if (!is_ppc_elf (ibfd) || !is_ppc_elf (obfd))
4156 return TRUE;
4157
4158 /* Check if we have the same endianess. */
4159 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
4160 return FALSE;
4161
4162 if (!ppc_elf_merge_obj_attributes (ibfd, obfd))
4163 return FALSE;
4164
4165 new_flags = elf_elfheader (ibfd)->e_flags;
4166 old_flags = elf_elfheader (obfd)->e_flags;
4167 if (!elf_flags_init (obfd))
4168 {
4169 /* First call, no flags set. */
4170 elf_flags_init (obfd) = TRUE;
4171 elf_elfheader (obfd)->e_flags = new_flags;
4172 }
4173
4174 /* Compatible flags are ok. */
4175 else if (new_flags == old_flags)
4176 ;
4177
4178 /* Incompatible flags. */
4179 else
4180 {
4181 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
4182 to be linked with either. */
4183 error = FALSE;
4184 if ((new_flags & EF_PPC_RELOCATABLE) != 0
4185 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
4186 {
4187 error = TRUE;
4188 (*_bfd_error_handler)
4189 (_("%B: compiled with -mrelocatable and linked with "
4190 "modules compiled normally"), ibfd);
4191 }
4192 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
4193 && (old_flags & EF_PPC_RELOCATABLE) != 0)
4194 {
4195 error = TRUE;
4196 (*_bfd_error_handler)
4197 (_("%B: compiled normally and linked with "
4198 "modules compiled with -mrelocatable"), ibfd);
4199 }
4200
4201 /* The output is -mrelocatable-lib iff both the input files are. */
4202 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
4203 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
4204
4205 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
4206 but each input file is either -mrelocatable or -mrelocatable-lib. */
4207 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
4208 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
4209 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
4210 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
4211
4212 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
4213 any module uses it. */
4214 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
4215
4216 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4217 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4218
4219 /* Warn about any other mismatches. */
4220 if (new_flags != old_flags)
4221 {
4222 error = TRUE;
4223 (*_bfd_error_handler)
4224 (_("%B: uses different e_flags (0x%lx) fields "
4225 "than previous modules (0x%lx)"),
4226 ibfd, (long) new_flags, (long) old_flags);
4227 }
4228
4229 if (error)
4230 {
4231 bfd_set_error (bfd_error_bad_value);
4232 return FALSE;
4233 }
4234 }
4235
4236 return TRUE;
4237 }
4238 \f
4239 /* Choose which PLT scheme to use, and set .plt flags appropriately.
4240 Returns -1 on error, 0 for old PLT, 1 for new PLT. */
4241 int
4242 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
4243 struct bfd_link_info *info,
4244 enum ppc_elf_plt_type plt_style,
4245 int emit_stub_syms)
4246 {
4247 struct ppc_elf_link_hash_table *htab;
4248 flagword flags;
4249
4250 htab = ppc_elf_hash_table (info);
4251
4252 htab->emit_stub_syms = emit_stub_syms;
4253
4254 if (htab->plt_type == PLT_UNSET)
4255 {
4256 if (plt_style == PLT_OLD)
4257 htab->plt_type = PLT_OLD;
4258 else
4259 {
4260 bfd *ibfd;
4261 enum ppc_elf_plt_type plt_type = plt_style;
4262
4263 /* Look through the reloc flags left by ppc_elf_check_relocs.
4264 Use the old style bss plt if a file makes plt calls
4265 without using the new relocs, and if ld isn't given
4266 --secure-plt and we never see REL16 relocs. */
4267 if (plt_type == PLT_UNSET)
4268 plt_type = PLT_OLD;
4269 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4270 if (is_ppc_elf (ibfd))
4271 {
4272 if (ppc_elf_tdata (ibfd)->has_rel16)
4273 plt_type = PLT_NEW;
4274 else if (ppc_elf_tdata (ibfd)->makes_plt_call)
4275 {
4276 plt_type = PLT_OLD;
4277 htab->old_bfd = ibfd;
4278 break;
4279 }
4280 }
4281 htab->plt_type = plt_type;
4282 }
4283 }
4284 if (htab->plt_type == PLT_OLD && plt_style == PLT_NEW)
4285 info->callbacks->info (_("Using bss-plt due to %B"), htab->old_bfd);
4286
4287 BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
4288
4289 if (htab->plt_type == PLT_NEW)
4290 {
4291 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4292 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4293
4294 /* The new PLT is a loaded section. */
4295 if (htab->plt != NULL
4296 && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
4297 return -1;
4298
4299 /* The new GOT is not executable. */
4300 if (htab->got != NULL
4301 && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
4302 return -1;
4303 }
4304 else
4305 {
4306 /* Stop an unused .glink section from affecting .text alignment. */
4307 if (htab->glink != NULL
4308 && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
4309 return -1;
4310 }
4311 return htab->plt_type == PLT_NEW;
4312 }
4313 \f
4314 /* Return the section that should be marked against GC for a given
4315 relocation. */
4316
4317 static asection *
4318 ppc_elf_gc_mark_hook (asection *sec,
4319 struct bfd_link_info *info,
4320 Elf_Internal_Rela *rel,
4321 struct elf_link_hash_entry *h,
4322 Elf_Internal_Sym *sym)
4323 {
4324 if (h != NULL)
4325 switch (ELF32_R_TYPE (rel->r_info))
4326 {
4327 case R_PPC_GNU_VTINHERIT:
4328 case R_PPC_GNU_VTENTRY:
4329 return NULL;
4330 }
4331
4332 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4333 }
4334
4335 /* Update the got, plt and dynamic reloc reference counts for the
4336 section being removed. */
4337
4338 static bfd_boolean
4339 ppc_elf_gc_sweep_hook (bfd *abfd,
4340 struct bfd_link_info *info,
4341 asection *sec,
4342 const Elf_Internal_Rela *relocs)
4343 {
4344 struct ppc_elf_link_hash_table *htab;
4345 Elf_Internal_Shdr *symtab_hdr;
4346 struct elf_link_hash_entry **sym_hashes;
4347 bfd_signed_vma *local_got_refcounts;
4348 const Elf_Internal_Rela *rel, *relend;
4349 asection *got2;
4350
4351 if (info->relocatable)
4352 return TRUE;
4353
4354 if ((sec->flags & SEC_ALLOC) == 0)
4355 return TRUE;
4356
4357 elf_section_data (sec)->local_dynrel = NULL;
4358
4359 htab = ppc_elf_hash_table (info);
4360 symtab_hdr = &elf_symtab_hdr (abfd);
4361 sym_hashes = elf_sym_hashes (abfd);
4362 local_got_refcounts = elf_local_got_refcounts (abfd);
4363 got2 = bfd_get_section_by_name (abfd, ".got2");
4364
4365 relend = relocs + sec->reloc_count;
4366 for (rel = relocs; rel < relend; rel++)
4367 {
4368 unsigned long r_symndx;
4369 enum elf_ppc_reloc_type r_type;
4370 struct elf_link_hash_entry *h = NULL;
4371
4372 r_symndx = ELF32_R_SYM (rel->r_info);
4373 if (r_symndx >= symtab_hdr->sh_info)
4374 {
4375 struct ppc_elf_dyn_relocs **pp, *p;
4376 struct ppc_elf_link_hash_entry *eh;
4377
4378 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4379 while (h->root.type == bfd_link_hash_indirect
4380 || h->root.type == bfd_link_hash_warning)
4381 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4382 eh = (struct ppc_elf_link_hash_entry *) h;
4383
4384 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4385 if (p->sec == sec)
4386 {
4387 /* Everything must go for SEC. */
4388 *pp = p->next;
4389 break;
4390 }
4391 }
4392
4393 r_type = ELF32_R_TYPE (rel->r_info);
4394 if (!htab->is_vxworks
4395 && h == NULL
4396 && local_got_refcounts != NULL
4397 && (!info->shared
4398 || is_branch_reloc (r_type)))
4399 {
4400 struct plt_entry **local_plt = (struct plt_entry **)
4401 (local_got_refcounts + symtab_hdr->sh_info);
4402 char *local_got_tls_masks = (char *)
4403 (local_plt + symtab_hdr->sh_info);
4404 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
4405 {
4406 struct plt_entry **ifunc = local_plt + r_symndx;
4407 bfd_vma addend = 0;
4408 struct plt_entry *ent;
4409
4410 if (r_type == R_PPC_PLTREL24 && info->shared)
4411 addend = rel->r_addend;
4412 ent = find_plt_ent (ifunc, got2, addend);
4413 if (ent->plt.refcount > 0)
4414 ent->plt.refcount -= 1;
4415 continue;
4416 }
4417 }
4418
4419 switch (r_type)
4420 {
4421 case R_PPC_GOT_TLSLD16:
4422 case R_PPC_GOT_TLSLD16_LO:
4423 case R_PPC_GOT_TLSLD16_HI:
4424 case R_PPC_GOT_TLSLD16_HA:
4425 case R_PPC_GOT_TLSGD16:
4426 case R_PPC_GOT_TLSGD16_LO:
4427 case R_PPC_GOT_TLSGD16_HI:
4428 case R_PPC_GOT_TLSGD16_HA:
4429 case R_PPC_GOT_TPREL16:
4430 case R_PPC_GOT_TPREL16_LO:
4431 case R_PPC_GOT_TPREL16_HI:
4432 case R_PPC_GOT_TPREL16_HA:
4433 case R_PPC_GOT_DTPREL16:
4434 case R_PPC_GOT_DTPREL16_LO:
4435 case R_PPC_GOT_DTPREL16_HI:
4436 case R_PPC_GOT_DTPREL16_HA:
4437 case R_PPC_GOT16:
4438 case R_PPC_GOT16_LO:
4439 case R_PPC_GOT16_HI:
4440 case R_PPC_GOT16_HA:
4441 if (h != NULL)
4442 {
4443 if (h->got.refcount > 0)
4444 h->got.refcount--;
4445 if (!info->shared)
4446 {
4447 struct plt_entry *ent;
4448
4449 ent = find_plt_ent (&h->plt.plist, NULL, 0);
4450 if (ent != NULL && ent->plt.refcount > 0)
4451 ent->plt.refcount -= 1;
4452 }
4453 }
4454 else if (local_got_refcounts != NULL)
4455 {
4456 if (local_got_refcounts[r_symndx] > 0)
4457 local_got_refcounts[r_symndx]--;
4458 }
4459 break;
4460
4461 case R_PPC_REL24:
4462 case R_PPC_REL14:
4463 case R_PPC_REL14_BRTAKEN:
4464 case R_PPC_REL14_BRNTAKEN:
4465 case R_PPC_REL32:
4466 if (h == NULL || h == htab->elf.hgot)
4467 break;
4468 /* Fall thru */
4469
4470 case R_PPC_ADDR32:
4471 case R_PPC_ADDR24:
4472 case R_PPC_ADDR16:
4473 case R_PPC_ADDR16_LO:
4474 case R_PPC_ADDR16_HI:
4475 case R_PPC_ADDR16_HA:
4476 case R_PPC_ADDR14:
4477 case R_PPC_ADDR14_BRTAKEN:
4478 case R_PPC_ADDR14_BRNTAKEN:
4479 case R_PPC_UADDR32:
4480 case R_PPC_UADDR16:
4481 if (info->shared)
4482 break;
4483
4484 case R_PPC_PLT32:
4485 case R_PPC_PLTREL24:
4486 case R_PPC_PLTREL32:
4487 case R_PPC_PLT16_LO:
4488 case R_PPC_PLT16_HI:
4489 case R_PPC_PLT16_HA:
4490 if (h != NULL)
4491 {
4492 bfd_vma addend = 0;
4493 struct plt_entry *ent;
4494
4495 if (r_type == R_PPC_PLTREL24 && info->shared)
4496 addend = rel->r_addend;
4497 ent = find_plt_ent (&h->plt.plist, got2, addend);
4498 if (ent != NULL && ent->plt.refcount > 0)
4499 ent->plt.refcount -= 1;
4500 }
4501 break;
4502
4503 default:
4504 break;
4505 }
4506 }
4507 return TRUE;
4508 }
4509 \f
4510 /* Set plt output section type, htab->tls_get_addr, and call the
4511 generic ELF tls_setup function. */
4512
4513 asection *
4514 ppc_elf_tls_setup (bfd *obfd,
4515 struct bfd_link_info *info,
4516 int no_tls_get_addr_opt)
4517 {
4518 struct ppc_elf_link_hash_table *htab;
4519
4520 htab = ppc_elf_hash_table (info);
4521 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4522 FALSE, FALSE, TRUE);
4523 if (!no_tls_get_addr_opt)
4524 {
4525 struct elf_link_hash_entry *opt, *tga;
4526 opt = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
4527 FALSE, FALSE, TRUE);
4528 if (opt != NULL
4529 && (opt->root.type == bfd_link_hash_defined
4530 || opt->root.type == bfd_link_hash_defweak))
4531 {
4532 /* If glibc supports an optimized __tls_get_addr call stub,
4533 signalled by the presence of __tls_get_addr_opt, and we'll
4534 be calling __tls_get_addr via a plt call stub, then
4535 make __tls_get_addr point to __tls_get_addr_opt. */
4536 tga = htab->tls_get_addr;
4537 if (htab->elf.dynamic_sections_created
4538 && tga != NULL
4539 && (tga->type == STT_FUNC
4540 || tga->needs_plt)
4541 && !(SYMBOL_CALLS_LOCAL (info, tga)
4542 || (ELF_ST_VISIBILITY (tga->other) != STV_DEFAULT
4543 && tga->root.type == bfd_link_hash_undefweak)))
4544 {
4545 struct plt_entry *ent;
4546 for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
4547 if (ent->plt.refcount > 0)
4548 break;
4549 if (ent != NULL)
4550 {
4551 tga->root.type = bfd_link_hash_indirect;
4552 tga->root.u.i.link = &opt->root;
4553 ppc_elf_copy_indirect_symbol (info, opt, tga);
4554 if (opt->dynindx != -1)
4555 {
4556 /* Use __tls_get_addr_opt in dynamic relocations. */
4557 opt->dynindx = -1;
4558 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4559 opt->dynstr_index);
4560 if (!bfd_elf_link_record_dynamic_symbol (info, opt))
4561 return FALSE;
4562 }
4563 htab->tls_get_addr = opt;
4564 }
4565 }
4566 }
4567 else
4568 no_tls_get_addr_opt = TRUE;
4569 }
4570 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
4571 if (htab->plt_type == PLT_NEW
4572 && htab->plt != NULL
4573 && htab->plt->output_section != NULL)
4574 {
4575 elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
4576 elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
4577 }
4578
4579 return _bfd_elf_tls_setup (obfd, info);
4580 }
4581
4582 /* Return TRUE iff REL is a branch reloc with a global symbol matching
4583 HASH. */
4584
4585 static bfd_boolean
4586 branch_reloc_hash_match (const bfd *ibfd,
4587 const Elf_Internal_Rela *rel,
4588 const struct elf_link_hash_entry *hash)
4589 {
4590 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4591 enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
4592 unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
4593
4594 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
4595 {
4596 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4597 struct elf_link_hash_entry *h;
4598
4599 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4600 while (h->root.type == bfd_link_hash_indirect
4601 || h->root.type == bfd_link_hash_warning)
4602 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4603 if (h == hash)
4604 return TRUE;
4605 }
4606 return FALSE;
4607 }
4608
4609 /* Run through all the TLS relocs looking for optimization
4610 opportunities. */
4611
4612 bfd_boolean
4613 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4614 struct bfd_link_info *info)
4615 {
4616 bfd *ibfd;
4617 asection *sec;
4618 struct ppc_elf_link_hash_table *htab;
4619 int pass;
4620
4621 if (info->relocatable || !info->executable)
4622 return TRUE;
4623
4624 htab = ppc_elf_hash_table (info);
4625 /* Make two passes through the relocs. First time check that tls
4626 relocs involved in setting up a tls_get_addr call are indeed
4627 followed by such a call. If they are not, exclude them from
4628 the optimizations done on the second pass. */
4629 for (pass = 0; pass < 2; ++pass)
4630 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4631 {
4632 Elf_Internal_Sym *locsyms = NULL;
4633 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4634 asection *got2 = bfd_get_section_by_name (ibfd, ".got2");
4635
4636 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4637 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4638 {
4639 Elf_Internal_Rela *relstart, *rel, *relend;
4640
4641 /* Read the relocations. */
4642 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4643 info->keep_memory);
4644 if (relstart == NULL)
4645 return FALSE;
4646
4647 relend = relstart + sec->reloc_count;
4648 for (rel = relstart; rel < relend; rel++)
4649 {
4650 enum elf_ppc_reloc_type r_type;
4651 unsigned long r_symndx;
4652 struct elf_link_hash_entry *h = NULL;
4653 char *tls_mask;
4654 char tls_set, tls_clear;
4655 bfd_boolean is_local;
4656 int expecting_tls_get_addr;
4657 bfd_signed_vma *got_count;
4658
4659 r_symndx = ELF32_R_SYM (rel->r_info);
4660 if (r_symndx >= symtab_hdr->sh_info)
4661 {
4662 struct elf_link_hash_entry **sym_hashes;
4663
4664 sym_hashes = elf_sym_hashes (ibfd);
4665 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4666 while (h->root.type == bfd_link_hash_indirect
4667 || h->root.type == bfd_link_hash_warning)
4668 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4669 }
4670
4671 expecting_tls_get_addr = 0;
4672 is_local = FALSE;
4673 if (h == NULL
4674 || !h->def_dynamic)
4675 is_local = TRUE;
4676
4677 r_type = ELF32_R_TYPE (rel->r_info);
4678 switch (r_type)
4679 {
4680 case R_PPC_GOT_TLSLD16:
4681 case R_PPC_GOT_TLSLD16_LO:
4682 expecting_tls_get_addr = 1;
4683 /* Fall thru */
4684
4685 case R_PPC_GOT_TLSLD16_HI:
4686 case R_PPC_GOT_TLSLD16_HA:
4687 /* These relocs should never be against a symbol
4688 defined in a shared lib. Leave them alone if
4689 that turns out to be the case. */
4690 if (!is_local)
4691 continue;
4692
4693 /* LD -> LE */
4694 tls_set = 0;
4695 tls_clear = TLS_LD;
4696 break;
4697
4698 case R_PPC_GOT_TLSGD16:
4699 case R_PPC_GOT_TLSGD16_LO:
4700 expecting_tls_get_addr = 1;
4701 /* Fall thru */
4702
4703 case R_PPC_GOT_TLSGD16_HI:
4704 case R_PPC_GOT_TLSGD16_HA:
4705 if (is_local)
4706 /* GD -> LE */
4707 tls_set = 0;
4708 else
4709 /* GD -> IE */
4710 tls_set = TLS_TLS | TLS_TPRELGD;
4711 tls_clear = TLS_GD;
4712 break;
4713
4714 case R_PPC_GOT_TPREL16:
4715 case R_PPC_GOT_TPREL16_LO:
4716 case R_PPC_GOT_TPREL16_HI:
4717 case R_PPC_GOT_TPREL16_HA:
4718 if (is_local)
4719 {
4720 /* IE -> LE */
4721 tls_set = 0;
4722 tls_clear = TLS_TPREL;
4723 break;
4724 }
4725 else
4726 continue;
4727
4728 case R_PPC_TLSGD:
4729 case R_PPC_TLSLD:
4730 expecting_tls_get_addr = 2;
4731 tls_set = 0;
4732 tls_clear = 0;
4733 break;
4734
4735 default:
4736 continue;
4737 }
4738
4739 if (pass == 0)
4740 {
4741 if (!expecting_tls_get_addr
4742 || (expecting_tls_get_addr == 1
4743 && !sec->has_tls_get_addr_call))
4744 continue;
4745
4746 if (rel + 1 < relend
4747 && branch_reloc_hash_match (ibfd, rel + 1,
4748 htab->tls_get_addr))
4749 continue;
4750
4751 /* Uh oh, we didn't find the expected call. We
4752 could just mark this symbol to exclude it
4753 from tls optimization but it's safer to skip
4754 the entire section. */
4755 sec->has_tls_reloc = 0;
4756 break;
4757 }
4758
4759 if (expecting_tls_get_addr)
4760 {
4761 struct plt_entry *ent;
4762 bfd_vma addend = 0;
4763
4764 if (info->shared
4765 && ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTREL24)
4766 addend = rel[1].r_addend;
4767 ent = find_plt_ent (&htab->tls_get_addr->plt.plist,
4768 got2, addend);
4769 if (ent != NULL && ent->plt.refcount > 0)
4770 ent->plt.refcount -= 1;
4771
4772 if (expecting_tls_get_addr == 2)
4773 continue;
4774 }
4775
4776 if (h != NULL)
4777 {
4778 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4779 got_count = &h->got.refcount;
4780 }
4781 else
4782 {
4783 Elf_Internal_Sym *sym;
4784 bfd_signed_vma *lgot_refs;
4785 struct plt_entry **local_plt;
4786 char *lgot_masks;
4787
4788 if (locsyms == NULL)
4789 {
4790 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4791 if (locsyms == NULL)
4792 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4793 symtab_hdr->sh_info,
4794 0, NULL, NULL, NULL);
4795 if (locsyms == NULL)
4796 {
4797 if (elf_section_data (sec)->relocs != relstart)
4798 free (relstart);
4799 return FALSE;
4800 }
4801 }
4802 sym = locsyms + r_symndx;
4803 lgot_refs = elf_local_got_refcounts (ibfd);
4804 if (lgot_refs == NULL)
4805 abort ();
4806 local_plt = (struct plt_entry **)
4807 (lgot_refs + symtab_hdr->sh_info);
4808 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
4809 tls_mask = &lgot_masks[r_symndx];
4810 got_count = &lgot_refs[r_symndx];
4811 }
4812
4813 if (tls_set == 0)
4814 {
4815 /* We managed to get rid of a got entry. */
4816 if (*got_count > 0)
4817 *got_count -= 1;
4818 }
4819
4820 *tls_mask |= tls_set;
4821 *tls_mask &= ~tls_clear;
4822 }
4823
4824 if (elf_section_data (sec)->relocs != relstart)
4825 free (relstart);
4826 }
4827
4828 if (locsyms != NULL
4829 && (symtab_hdr->contents != (unsigned char *) locsyms))
4830 {
4831 if (!info->keep_memory)
4832 free (locsyms);
4833 else
4834 symtab_hdr->contents = (unsigned char *) locsyms;
4835 }
4836 }
4837 return TRUE;
4838 }
4839 \f
4840 /* Return true if we have dynamic relocs that apply to read-only sections. */
4841
4842 static bfd_boolean
4843 readonly_dynrelocs (struct elf_link_hash_entry *h)
4844 {
4845 struct ppc_elf_dyn_relocs *p;
4846
4847 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4848 {
4849 asection *s = p->sec->output_section;
4850
4851 if (s != NULL
4852 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
4853 == (SEC_READONLY | SEC_ALLOC)))
4854 return TRUE;
4855 }
4856 return FALSE;
4857 }
4858
4859 /* Adjust a symbol defined by a dynamic object and referenced by a
4860 regular object. The current definition is in some section of the
4861 dynamic object, but we're not including those sections. We have to
4862 change the definition to something the rest of the link can
4863 understand. */
4864
4865 static bfd_boolean
4866 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4867 struct elf_link_hash_entry *h)
4868 {
4869 struct ppc_elf_link_hash_table *htab;
4870 asection *s;
4871
4872 #ifdef DEBUG
4873 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
4874 h->root.root.string);
4875 #endif
4876
4877 /* Make sure we know what is going on here. */
4878 htab = ppc_elf_hash_table (info);
4879 BFD_ASSERT (htab->elf.dynobj != NULL
4880 && (h->needs_plt
4881 || h->type == STT_GNU_IFUNC
4882 || h->u.weakdef != NULL
4883 || (h->def_dynamic
4884 && h->ref_regular
4885 && !h->def_regular)));
4886
4887 /* Deal with function syms. */
4888 if (h->type == STT_FUNC
4889 || h->type == STT_GNU_IFUNC
4890 || h->needs_plt)
4891 {
4892 /* Clear procedure linkage table information for any symbol that
4893 won't need a .plt entry. */
4894 struct plt_entry *ent;
4895 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4896 if (ent->plt.refcount > 0)
4897 break;
4898 if (ent == NULL
4899 || (h->type != STT_GNU_IFUNC
4900 && (SYMBOL_CALLS_LOCAL (info, h)
4901 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4902 && h->root.type == bfd_link_hash_undefweak))))
4903 {
4904 /* A PLT entry is not required/allowed when:
4905
4906 1. We are not using ld.so; because then the PLT entry
4907 can't be set up, so we can't use one. In this case,
4908 ppc_elf_adjust_dynamic_symbol won't even be called.
4909
4910 2. GC has rendered the entry unused.
4911
4912 3. We know for certain that a call to this symbol
4913 will go to this object, or will remain undefined. */
4914 h->plt.plist = NULL;
4915 h->needs_plt = 0;
4916 }
4917 else
4918 {
4919 /* After adjust_dynamic_symbol, non_got_ref set in the
4920 non-shared case means that we have allocated space in
4921 .dynbss for the symbol and thus dyn_relocs for this
4922 symbol should be discarded.
4923 If we get here we know we are making a PLT entry for this
4924 symbol, and in an executable we'd normally resolve
4925 relocations against this symbol to the PLT entry. Allow
4926 dynamic relocs if the reference is weak, and the dynamic
4927 relocs will not cause text relocation. */
4928 if (!h->ref_regular_nonweak
4929 && h->non_got_ref
4930 && h->type != STT_GNU_IFUNC
4931 && !htab->is_vxworks
4932 && !ppc_elf_hash_entry (h)->has_sda_refs
4933 && !readonly_dynrelocs (h))
4934 h->non_got_ref = 0;
4935 }
4936 return TRUE;
4937 }
4938 else
4939 h->plt.plist = NULL;
4940
4941 /* If this is a weak symbol, and there is a real definition, the
4942 processor independent code will have arranged for us to see the
4943 real definition first, and we can just use the same value. */
4944 if (h->u.weakdef != NULL)
4945 {
4946 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4947 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4948 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4949 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4950 if (ELIMINATE_COPY_RELOCS)
4951 h->non_got_ref = h->u.weakdef->non_got_ref;
4952 return TRUE;
4953 }
4954
4955 /* This is a reference to a symbol defined by a dynamic object which
4956 is not a function. */
4957
4958 /* If we are creating a shared library, we must presume that the
4959 only references to the symbol are via the global offset table.
4960 For such cases we need not do anything here; the relocations will
4961 be handled correctly by relocate_section. */
4962 if (info->shared)
4963 return TRUE;
4964
4965 /* If there are no references to this symbol that do not use the
4966 GOT, we don't need to generate a copy reloc. */
4967 if (!h->non_got_ref)
4968 return TRUE;
4969
4970 /* If we didn't find any dynamic relocs in read-only sections, then
4971 we'll be keeping the dynamic relocs and avoiding the copy reloc.
4972 We can't do this if there are any small data relocations. This
4973 doesn't work on VxWorks, where we can not have dynamic
4974 relocations (other than copy and jump slot relocations) in an
4975 executable. */
4976 if (ELIMINATE_COPY_RELOCS
4977 && !ppc_elf_hash_entry (h)->has_sda_refs
4978 && !htab->is_vxworks
4979 && !h->def_regular
4980 && !readonly_dynrelocs (h))
4981 {
4982 h->non_got_ref = 0;
4983 return TRUE;
4984 }
4985
4986 if (h->size == 0)
4987 {
4988 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4989 h->root.root.string);
4990 return TRUE;
4991 }
4992
4993 /* We must allocate the symbol in our .dynbss section, which will
4994 become part of the .bss section of the executable. There will be
4995 an entry for this symbol in the .dynsym section. The dynamic
4996 object will contain position independent code, so all references
4997 from the dynamic object to this symbol will go through the global
4998 offset table. The dynamic linker will use the .dynsym entry to
4999 determine the address it must put in the global offset table, so
5000 both the dynamic object and the regular object will refer to the
5001 same memory location for the variable.
5002
5003 Of course, if the symbol is referenced using SDAREL relocs, we
5004 must instead allocate it in .sbss. */
5005
5006 if (ppc_elf_hash_entry (h)->has_sda_refs)
5007 s = htab->dynsbss;
5008 else
5009 s = htab->dynbss;
5010 BFD_ASSERT (s != NULL);
5011
5012 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
5013 copy the initial value out of the dynamic object and into the
5014 runtime process image. We need to remember the offset into the
5015 .rela.bss section we are going to use. */
5016 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5017 {
5018 asection *srel;
5019
5020 if (ppc_elf_hash_entry (h)->has_sda_refs)
5021 srel = htab->relsbss;
5022 else
5023 srel = htab->relbss;
5024 BFD_ASSERT (srel != NULL);
5025 srel->size += sizeof (Elf32_External_Rela);
5026 h->needs_copy = 1;
5027 }
5028
5029 return _bfd_elf_adjust_dynamic_copy (h, s);
5030 }
5031 \f
5032 /* Generate a symbol to mark plt call stubs. For non-PIC code the sym is
5033 xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
5034 specifying the addend on the plt relocation. For -fpic code, the sym
5035 is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
5036 xxxxxxxx.got2.plt_pic32.<callee>. */
5037
5038 static bfd_boolean
5039 add_stub_sym (struct plt_entry *ent,
5040 struct elf_link_hash_entry *h,
5041 struct bfd_link_info *info)
5042 {
5043 struct elf_link_hash_entry *sh;
5044 size_t len1, len2, len3;
5045 char *name;
5046 const char *stub;
5047 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
5048
5049 if (info->shared)
5050 stub = ".plt_pic32.";
5051 else
5052 stub = ".plt_call32.";
5053
5054 len1 = strlen (h->root.root.string);
5055 len2 = strlen (stub);
5056 len3 = 0;
5057 if (ent->sec)
5058 len3 = strlen (ent->sec->name);
5059 name = bfd_malloc (len1 + len2 + len3 + 9);
5060 if (name == NULL)
5061 return FALSE;
5062 sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
5063 if (ent->sec)
5064 memcpy (name + 8, ent->sec->name, len3);
5065 memcpy (name + 8 + len3, stub, len2);
5066 memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
5067 sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
5068 if (sh == NULL)
5069 return FALSE;
5070 if (sh->root.type == bfd_link_hash_new)
5071 {
5072 sh->root.type = bfd_link_hash_defined;
5073 sh->root.u.def.section = htab->glink;
5074 sh->root.u.def.value = ent->glink_offset;
5075 sh->ref_regular = 1;
5076 sh->def_regular = 1;
5077 sh->ref_regular_nonweak = 1;
5078 sh->forced_local = 1;
5079 sh->non_elf = 0;
5080 }
5081 return TRUE;
5082 }
5083
5084 /* Allocate NEED contiguous space in .got, and return the offset.
5085 Handles allocation of the got header when crossing 32k. */
5086
5087 static bfd_vma
5088 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
5089 {
5090 bfd_vma where;
5091 unsigned int max_before_header;
5092
5093 if (htab->plt_type == PLT_VXWORKS)
5094 {
5095 where = htab->got->size;
5096 htab->got->size += need;
5097 }
5098 else
5099 {
5100 max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
5101 if (need <= htab->got_gap)
5102 {
5103 where = max_before_header - htab->got_gap;
5104 htab->got_gap -= need;
5105 }
5106 else
5107 {
5108 if (htab->got->size + need > max_before_header
5109 && htab->got->size <= max_before_header)
5110 {
5111 htab->got_gap = max_before_header - htab->got->size;
5112 htab->got->size = max_before_header + htab->got_header_size;
5113 }
5114 where = htab->got->size;
5115 htab->got->size += need;
5116 }
5117 }
5118 return where;
5119 }
5120
5121 /* Allocate space in associated reloc sections for dynamic relocs. */
5122
5123 static bfd_boolean
5124 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5125 {
5126 struct bfd_link_info *info = inf;
5127 struct ppc_elf_link_hash_entry *eh;
5128 struct ppc_elf_link_hash_table *htab;
5129 struct ppc_elf_dyn_relocs *p;
5130
5131 if (h->root.type == bfd_link_hash_indirect)
5132 return TRUE;
5133
5134 if (h->root.type == bfd_link_hash_warning)
5135 /* When warning symbols are created, they **replace** the "real"
5136 entry in the hash table, thus we never get to see the real
5137 symbol in a hash traversal. So look at it now. */
5138 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5139
5140 htab = ppc_elf_hash_table (info);
5141 if (htab->elf.dynamic_sections_created
5142 || h->type == STT_GNU_IFUNC)
5143 {
5144 struct plt_entry *ent;
5145 bfd_boolean doneone = FALSE;
5146 bfd_vma plt_offset = 0, glink_offset = 0;
5147 bfd_boolean dyn;
5148
5149 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5150 if (ent->plt.refcount > 0)
5151 {
5152 /* Make sure this symbol is output as a dynamic symbol. */
5153 if (h->dynindx == -1
5154 && !h->forced_local
5155 && !h->def_regular
5156 && htab->elf.dynamic_sections_created)
5157 {
5158 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5159 return FALSE;
5160 }
5161
5162 dyn = htab->elf.dynamic_sections_created;
5163 if (info->shared
5164 || h->type == STT_GNU_IFUNC
5165 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
5166 {
5167 asection *s = htab->plt;
5168 if (!dyn || h->dynindx == -1)
5169 s = htab->iplt;
5170
5171 if (htab->plt_type == PLT_NEW || !dyn || h->dynindx == -1)
5172 {
5173 if (!doneone)
5174 {
5175 plt_offset = s->size;
5176 s->size += 4;
5177 }
5178 ent->plt.offset = plt_offset;
5179
5180 s = htab->glink;
5181 if (!doneone || info->shared)
5182 {
5183 glink_offset = s->size;
5184 s->size += GLINK_ENTRY_SIZE;
5185 if (h == htab->tls_get_addr
5186 && !htab->no_tls_get_addr_opt)
5187 s->size += TLS_GET_ADDR_GLINK_SIZE - GLINK_ENTRY_SIZE;
5188 }
5189 if (!doneone
5190 && !info->shared
5191 && h->def_dynamic
5192 && !h->def_regular)
5193 {
5194 h->root.u.def.section = s;
5195 h->root.u.def.value = glink_offset;
5196 }
5197 ent->glink_offset = glink_offset;
5198
5199 if (htab->emit_stub_syms
5200 && !add_stub_sym (ent, h, info))
5201 return FALSE;
5202 }
5203 else
5204 {
5205 if (!doneone)
5206 {
5207 /* If this is the first .plt entry, make room
5208 for the special first entry. */
5209 if (s->size == 0)
5210 s->size += htab->plt_initial_entry_size;
5211
5212 /* The PowerPC PLT is actually composed of two
5213 parts, the first part is 2 words (for a load
5214 and a jump), and then there is a remaining
5215 word available at the end. */
5216 plt_offset = (htab->plt_initial_entry_size
5217 + (htab->plt_slot_size
5218 * ((s->size
5219 - htab->plt_initial_entry_size)
5220 / htab->plt_entry_size)));
5221
5222 /* If this symbol is not defined in a regular
5223 file, and we are not generating a shared
5224 library, then set the symbol to this location
5225 in the .plt. This is to avoid text
5226 relocations, and is required to make
5227 function pointers compare as equal between
5228 the normal executable and the shared library. */
5229 if (! info->shared
5230 && h->def_dynamic
5231 && !h->def_regular)
5232 {
5233 h->root.u.def.section = s;
5234 h->root.u.def.value = plt_offset;
5235 }
5236
5237 /* Make room for this entry. */
5238 s->size += htab->plt_entry_size;
5239 /* After the 8192nd entry, room for two entries
5240 is allocated. */
5241 if (htab->plt_type == PLT_OLD
5242 && (s->size - htab->plt_initial_entry_size)
5243 / htab->plt_entry_size
5244 > PLT_NUM_SINGLE_ENTRIES)
5245 s->size += htab->plt_entry_size;
5246 }
5247 ent->plt.offset = plt_offset;
5248 }
5249
5250 /* We also need to make an entry in the .rela.plt section. */
5251 if (!doneone)
5252 {
5253 if (!htab->elf.dynamic_sections_created
5254 || h->dynindx == -1)
5255 htab->reliplt->size += sizeof (Elf32_External_Rela);
5256 else
5257 {
5258 htab->relplt->size += sizeof (Elf32_External_Rela);
5259
5260 if (htab->plt_type == PLT_VXWORKS)
5261 {
5262 /* Allocate space for the unloaded relocations. */
5263 if (!info->shared
5264 && htab->elf.dynamic_sections_created)
5265 {
5266 if (ent->plt.offset
5267 == (bfd_vma) htab->plt_initial_entry_size)
5268 {
5269 htab->srelplt2->size
5270 += (sizeof (Elf32_External_Rela)
5271 * VXWORKS_PLTRESOLVE_RELOCS);
5272 }
5273
5274 htab->srelplt2->size
5275 += (sizeof (Elf32_External_Rela)
5276 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
5277 }
5278
5279 /* Every PLT entry has an associated GOT entry in
5280 .got.plt. */
5281 htab->sgotplt->size += 4;
5282 }
5283 }
5284 doneone = TRUE;
5285 }
5286 }
5287 else
5288 ent->plt.offset = (bfd_vma) -1;
5289 }
5290 else
5291 ent->plt.offset = (bfd_vma) -1;
5292
5293 if (!doneone)
5294 {
5295 h->plt.plist = NULL;
5296 h->needs_plt = 0;
5297 }
5298 }
5299 else
5300 {
5301 h->plt.plist = NULL;
5302 h->needs_plt = 0;
5303 }
5304
5305 eh = (struct ppc_elf_link_hash_entry *) h;
5306 if (eh->elf.got.refcount > 0)
5307 {
5308 bfd_boolean dyn;
5309 unsigned int need;
5310
5311 /* Make sure this symbol is output as a dynamic symbol. */
5312 if (eh->elf.dynindx == -1
5313 && !eh->elf.forced_local
5314 && eh->elf.type != STT_GNU_IFUNC
5315 && htab->elf.dynamic_sections_created)
5316 {
5317 if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
5318 return FALSE;
5319 }
5320
5321 need = 0;
5322 if ((eh->tls_mask & TLS_TLS) != 0)
5323 {
5324 if ((eh->tls_mask & TLS_LD) != 0)
5325 {
5326 if (!eh->elf.def_dynamic)
5327 /* We'll just use htab->tlsld_got.offset. This should
5328 always be the case. It's a little odd if we have
5329 a local dynamic reloc against a non-local symbol. */
5330 htab->tlsld_got.refcount += 1;
5331 else
5332 need += 8;
5333 }
5334 if ((eh->tls_mask & TLS_GD) != 0)
5335 need += 8;
5336 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
5337 need += 4;
5338 if ((eh->tls_mask & TLS_DTPREL) != 0)
5339 need += 4;
5340 }
5341 else
5342 need += 4;
5343 if (need == 0)
5344 eh->elf.got.offset = (bfd_vma) -1;
5345 else
5346 {
5347 eh->elf.got.offset = allocate_got (htab, need);
5348 dyn = htab->elf.dynamic_sections_created;
5349 if ((info->shared
5350 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
5351 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
5352 || eh->elf.root.type != bfd_link_hash_undefweak))
5353 {
5354 asection *rsec = htab->relgot;
5355 /* All the entries we allocated need relocs.
5356 Except LD only needs one. */
5357 if ((eh->tls_mask & TLS_LD) != 0
5358 && eh->elf.def_dynamic)
5359 need -= 4;
5360 rsec->size += need * (sizeof (Elf32_External_Rela) / 4);
5361 }
5362 }
5363 }
5364 else
5365 eh->elf.got.offset = (bfd_vma) -1;
5366
5367 if (eh->dyn_relocs == NULL
5368 || !htab->elf.dynamic_sections_created)
5369 return TRUE;
5370
5371 /* In the shared -Bsymbolic case, discard space allocated for
5372 dynamic pc-relative relocs against symbols which turn out to be
5373 defined in regular objects. For the normal shared case, discard
5374 space for relocs that have become local due to symbol visibility
5375 changes. */
5376
5377 if (info->shared)
5378 {
5379 /* Relocs that use pc_count are those that appear on a call insn,
5380 or certain REL relocs (see must_be_dyn_reloc) that can be
5381 generated via assembly. We want calls to protected symbols to
5382 resolve directly to the function rather than going via the plt.
5383 If people want function pointer comparisons to work as expected
5384 then they should avoid writing weird assembly. */
5385 if (SYMBOL_CALLS_LOCAL (info, h))
5386 {
5387 struct ppc_elf_dyn_relocs **pp;
5388
5389 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5390 {
5391 p->count -= p->pc_count;
5392 p->pc_count = 0;
5393 if (p->count == 0)
5394 *pp = p->next;
5395 else
5396 pp = &p->next;
5397 }
5398 }
5399
5400 if (htab->is_vxworks)
5401 {
5402 struct ppc_elf_dyn_relocs **pp;
5403
5404 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5405 {
5406 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
5407 *pp = p->next;
5408 else
5409 pp = &p->next;
5410 }
5411 }
5412
5413 /* Discard relocs on undefined symbols that must be local. */
5414 if (eh->dyn_relocs != NULL
5415 && h->root.type == bfd_link_hash_undefined
5416 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
5417 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
5418 eh->dyn_relocs = NULL;
5419
5420 /* Also discard relocs on undefined weak syms with non-default
5421 visibility. */
5422 if (eh->dyn_relocs != NULL
5423 && h->root.type == bfd_link_hash_undefweak)
5424 {
5425 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5426 eh->dyn_relocs = NULL;
5427
5428 /* Make sure undefined weak symbols are output as a dynamic
5429 symbol in PIEs. */
5430 else if (h->dynindx == -1
5431 && !h->forced_local
5432 && !h->def_regular)
5433 {
5434 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5435 return FALSE;
5436 }
5437 }
5438 }
5439 else if (ELIMINATE_COPY_RELOCS)
5440 {
5441 /* For the non-shared case, discard space for relocs against
5442 symbols which turn out to need copy relocs or are not
5443 dynamic. */
5444
5445 if (!h->non_got_ref
5446 && !h->def_regular)
5447 {
5448 /* Make sure this symbol is output as a dynamic symbol.
5449 Undefined weak syms won't yet be marked as dynamic. */
5450 if (h->dynindx == -1
5451 && !h->forced_local)
5452 {
5453 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5454 return FALSE;
5455 }
5456
5457 /* If that succeeded, we know we'll be keeping all the
5458 relocs. */
5459 if (h->dynindx != -1)
5460 goto keep;
5461 }
5462
5463 eh->dyn_relocs = NULL;
5464
5465 keep: ;
5466 }
5467
5468 /* Finally, allocate space. */
5469 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5470 {
5471 asection *sreloc = elf_section_data (p->sec)->sreloc;
5472 if (!htab->elf.dynamic_sections_created)
5473 sreloc = htab->reliplt;
5474 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5475 }
5476
5477 return TRUE;
5478 }
5479
5480 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
5481 read-only sections. */
5482
5483 static bfd_boolean
5484 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
5485 {
5486 if (h->root.type == bfd_link_hash_indirect)
5487 return TRUE;
5488
5489 if (h->root.type == bfd_link_hash_warning)
5490 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5491
5492 if (readonly_dynrelocs (h))
5493 {
5494 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
5495
5496 /* Not an error, just cut short the traversal. */
5497 return FALSE;
5498 }
5499 return TRUE;
5500 }
5501
5502 /* Set the sizes of the dynamic sections. */
5503
5504 static bfd_boolean
5505 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5506 struct bfd_link_info *info)
5507 {
5508 struct ppc_elf_link_hash_table *htab;
5509 asection *s;
5510 bfd_boolean relocs;
5511 bfd *ibfd;
5512
5513 #ifdef DEBUG
5514 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
5515 #endif
5516
5517 htab = ppc_elf_hash_table (info);
5518 BFD_ASSERT (htab->elf.dynobj != NULL);
5519
5520 if (elf_hash_table (info)->dynamic_sections_created)
5521 {
5522 /* Set the contents of the .interp section to the interpreter. */
5523 if (info->executable)
5524 {
5525 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
5526 BFD_ASSERT (s != NULL);
5527 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5528 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5529 }
5530 }
5531
5532 if (htab->plt_type == PLT_OLD)
5533 htab->got_header_size = 16;
5534 else if (htab->plt_type == PLT_NEW)
5535 htab->got_header_size = 12;
5536
5537 /* Set up .got offsets for local syms, and space for local dynamic
5538 relocs. */
5539 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5540 {
5541 bfd_signed_vma *local_got;
5542 bfd_signed_vma *end_local_got;
5543 struct plt_entry **local_plt;
5544 struct plt_entry **end_local_plt;
5545 char *lgot_masks;
5546 bfd_size_type locsymcount;
5547 Elf_Internal_Shdr *symtab_hdr;
5548
5549 if (!is_ppc_elf (ibfd))
5550 continue;
5551
5552 for (s = ibfd->sections; s != NULL; s = s->next)
5553 {
5554 struct ppc_elf_dyn_relocs *p;
5555
5556 for (p = ((struct ppc_elf_dyn_relocs *)
5557 elf_section_data (s)->local_dynrel);
5558 p != NULL;
5559 p = p->next)
5560 {
5561 if (!bfd_is_abs_section (p->sec)
5562 && bfd_is_abs_section (p->sec->output_section))
5563 {
5564 /* Input section has been discarded, either because
5565 it is a copy of a linkonce section or due to
5566 linker script /DISCARD/, so we'll be discarding
5567 the relocs too. */
5568 }
5569 else if (htab->is_vxworks
5570 && strcmp (p->sec->output_section->name,
5571 ".tls_vars") == 0)
5572 {
5573 /* Relocations in vxworks .tls_vars sections are
5574 handled specially by the loader. */
5575 }
5576 else if (p->count != 0)
5577 {
5578 asection *sreloc = elf_section_data (p->sec)->sreloc;
5579 if (!htab->elf.dynamic_sections_created)
5580 sreloc = htab->reliplt;
5581 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5582 if ((p->sec->output_section->flags
5583 & (SEC_READONLY | SEC_ALLOC))
5584 == (SEC_READONLY | SEC_ALLOC))
5585 info->flags |= DF_TEXTREL;
5586 }
5587 }
5588 }
5589
5590 local_got = elf_local_got_refcounts (ibfd);
5591 if (!local_got)
5592 continue;
5593
5594 symtab_hdr = &elf_symtab_hdr (ibfd);
5595 locsymcount = symtab_hdr->sh_info;
5596 end_local_got = local_got + locsymcount;
5597 local_plt = (struct plt_entry **) end_local_got;
5598 end_local_plt = local_plt + locsymcount;
5599 lgot_masks = (char *) end_local_plt;
5600
5601 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
5602 if (*local_got > 0)
5603 {
5604 unsigned int need = 0;
5605 if ((*lgot_masks & TLS_TLS) != 0)
5606 {
5607 if ((*lgot_masks & TLS_GD) != 0)
5608 need += 8;
5609 if ((*lgot_masks & TLS_LD) != 0)
5610 htab->tlsld_got.refcount += 1;
5611 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
5612 need += 4;
5613 if ((*lgot_masks & TLS_DTPREL) != 0)
5614 need += 4;
5615 }
5616 else
5617 need += 4;
5618 if (need == 0)
5619 *local_got = (bfd_vma) -1;
5620 else
5621 {
5622 *local_got = allocate_got (htab, need);
5623 if (info->shared)
5624 htab->relgot->size += (need
5625 * (sizeof (Elf32_External_Rela) / 4));
5626 }
5627 }
5628 else
5629 *local_got = (bfd_vma) -1;
5630
5631 if (htab->is_vxworks)
5632 continue;
5633
5634 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
5635 for (; local_plt < end_local_plt; ++local_plt)
5636 {
5637 struct plt_entry *ent;
5638 bfd_boolean doneone = FALSE;
5639 bfd_vma plt_offset = 0, glink_offset = 0;
5640
5641 for (ent = *local_plt; ent != NULL; ent = ent->next)
5642 if (ent->plt.refcount > 0)
5643 {
5644 s = htab->iplt;
5645
5646 if (!doneone)
5647 {
5648 plt_offset = s->size;
5649 s->size += 4;
5650 }
5651 ent->plt.offset = plt_offset;
5652
5653 s = htab->glink;
5654 if (!doneone || info->shared)
5655 {
5656 glink_offset = s->size;
5657 s->size += GLINK_ENTRY_SIZE;
5658 }
5659 ent->glink_offset = glink_offset;
5660
5661 if (!doneone)
5662 {
5663 htab->reliplt->size += sizeof (Elf32_External_Rela);
5664 doneone = TRUE;
5665 }
5666 }
5667 else
5668 ent->plt.offset = (bfd_vma) -1;
5669 }
5670 }
5671
5672 /* Allocate space for global sym dynamic relocs. */
5673 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
5674
5675 if (htab->tlsld_got.refcount > 0)
5676 {
5677 htab->tlsld_got.offset = allocate_got (htab, 8);
5678 if (info->shared)
5679 htab->relgot->size += sizeof (Elf32_External_Rela);
5680 }
5681 else
5682 htab->tlsld_got.offset = (bfd_vma) -1;
5683
5684 if (htab->got != NULL && htab->plt_type != PLT_VXWORKS)
5685 {
5686 unsigned int g_o_t = 32768;
5687
5688 /* If we haven't allocated the header, do so now. When we get here,
5689 for old plt/got the got size will be 0 to 32764 (not allocated),
5690 or 32780 to 65536 (header allocated). For new plt/got, the
5691 corresponding ranges are 0 to 32768 and 32780 to 65536. */
5692 if (htab->got->size <= 32768)
5693 {
5694 g_o_t = htab->got->size;
5695 if (htab->plt_type == PLT_OLD)
5696 g_o_t += 4;
5697 htab->got->size += htab->got_header_size;
5698 }
5699
5700 htab->elf.hgot->root.u.def.value = g_o_t;
5701 }
5702 if (info->shared)
5703 {
5704 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
5705 if (sda != NULL
5706 && !(sda->root.type == bfd_link_hash_defined
5707 || sda->root.type == bfd_link_hash_defweak))
5708 {
5709 sda->root.type = bfd_link_hash_defined;
5710 sda->root.u.def.section = htab->elf.hgot->root.u.def.section;
5711 sda->root.u.def.value = htab->elf.hgot->root.u.def.value;
5712 }
5713 }
5714
5715 if (htab->glink != NULL
5716 && htab->glink->size != 0
5717 && htab->elf.dynamic_sections_created)
5718 {
5719 htab->glink_pltresolve = htab->glink->size;
5720 /* Space for the branch table. */
5721 htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
5722 /* Pad out to align the start of PLTresolve. */
5723 htab->glink->size += -htab->glink->size & 15;
5724 htab->glink->size += GLINK_PLTRESOLVE;
5725
5726 if (htab->emit_stub_syms)
5727 {
5728 struct elf_link_hash_entry *sh;
5729 sh = elf_link_hash_lookup (&htab->elf, "__glink",
5730 TRUE, FALSE, FALSE);
5731 if (sh == NULL)
5732 return FALSE;
5733 if (sh->root.type == bfd_link_hash_new)
5734 {
5735 sh->root.type = bfd_link_hash_defined;
5736 sh->root.u.def.section = htab->glink;
5737 sh->root.u.def.value = htab->glink_pltresolve;
5738 sh->ref_regular = 1;
5739 sh->def_regular = 1;
5740 sh->ref_regular_nonweak = 1;
5741 sh->forced_local = 1;
5742 sh->non_elf = 0;
5743 }
5744 sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
5745 TRUE, FALSE, FALSE);
5746 if (sh == NULL)
5747 return FALSE;
5748 if (sh->root.type == bfd_link_hash_new)
5749 {
5750 sh->root.type = bfd_link_hash_defined;
5751 sh->root.u.def.section = htab->glink;
5752 sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
5753 sh->ref_regular = 1;
5754 sh->def_regular = 1;
5755 sh->ref_regular_nonweak = 1;
5756 sh->forced_local = 1;
5757 sh->non_elf = 0;
5758 }
5759 }
5760 }
5761
5762 /* We've now determined the sizes of the various dynamic sections.
5763 Allocate memory for them. */
5764 relocs = FALSE;
5765 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
5766 {
5767 bfd_boolean strip_section = TRUE;
5768
5769 if ((s->flags & SEC_LINKER_CREATED) == 0)
5770 continue;
5771
5772 if (s == htab->plt
5773 || s == htab->got)
5774 {
5775 /* We'd like to strip these sections if they aren't needed, but if
5776 we've exported dynamic symbols from them we must leave them.
5777 It's too late to tell BFD to get rid of the symbols. */
5778 if (htab->elf.hplt != NULL)
5779 strip_section = FALSE;
5780 /* Strip this section if we don't need it; see the
5781 comment below. */
5782 }
5783 else if (s == htab->iplt
5784 || s == htab->glink
5785 || s == htab->sgotplt
5786 || s == htab->sbss
5787 || s == htab->dynbss
5788 || s == htab->dynsbss
5789 || s == htab->sdata[0].section
5790 || s == htab->sdata[1].section)
5791 {
5792 /* Strip these too. */
5793 }
5794 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
5795 {
5796 if (s->size != 0)
5797 {
5798 /* Remember whether there are any relocation sections. */
5799 relocs = TRUE;
5800
5801 /* We use the reloc_count field as a counter if we need
5802 to copy relocs into the output file. */
5803 s->reloc_count = 0;
5804 }
5805 }
5806 else
5807 {
5808 /* It's not one of our sections, so don't allocate space. */
5809 continue;
5810 }
5811
5812 if (s->size == 0 && strip_section)
5813 {
5814 /* If we don't need this section, strip it from the
5815 output file. This is mostly to handle .rela.bss and
5816 .rela.plt. We must create both sections in
5817 create_dynamic_sections, because they must be created
5818 before the linker maps input sections to output
5819 sections. The linker does that before
5820 adjust_dynamic_symbol is called, and it is that
5821 function which decides whether anything needs to go
5822 into these sections. */
5823 s->flags |= SEC_EXCLUDE;
5824 continue;
5825 }
5826
5827 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5828 continue;
5829
5830 /* Allocate memory for the section contents. */
5831 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
5832 if (s->contents == NULL)
5833 return FALSE;
5834 }
5835
5836 if (htab->elf.dynamic_sections_created)
5837 {
5838 /* Add some entries to the .dynamic section. We fill in the
5839 values later, in ppc_elf_finish_dynamic_sections, but we
5840 must add the entries now so that we get the correct size for
5841 the .dynamic section. The DT_DEBUG entry is filled in by the
5842 dynamic linker and used by the debugger. */
5843 #define add_dynamic_entry(TAG, VAL) \
5844 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5845
5846 if (info->executable)
5847 {
5848 if (!add_dynamic_entry (DT_DEBUG, 0))
5849 return FALSE;
5850 }
5851
5852 if (htab->plt != NULL && htab->plt->size != 0)
5853 {
5854 if (!add_dynamic_entry (DT_PLTGOT, 0)
5855 || !add_dynamic_entry (DT_PLTRELSZ, 0)
5856 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5857 || !add_dynamic_entry (DT_JMPREL, 0))
5858 return FALSE;
5859 }
5860
5861 if (htab->glink != NULL && htab->glink->size != 0)
5862 {
5863 if (!add_dynamic_entry (DT_PPC_GOT, 0))
5864 return FALSE;
5865 if (!htab->no_tls_get_addr_opt
5866 && htab->tls_get_addr != NULL
5867 && htab->tls_get_addr->plt.plist != NULL
5868 && !add_dynamic_entry (DT_PPC_TLSOPT, 0))
5869 return FALSE;
5870 }
5871
5872 if (relocs)
5873 {
5874 if (!add_dynamic_entry (DT_RELA, 0)
5875 || !add_dynamic_entry (DT_RELASZ, 0)
5876 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
5877 return FALSE;
5878 }
5879
5880 /* If any dynamic relocs apply to a read-only section, then we
5881 need a DT_TEXTREL entry. */
5882 if ((info->flags & DF_TEXTREL) == 0)
5883 elf_link_hash_traverse (elf_hash_table (info), maybe_set_textrel,
5884 info);
5885
5886 if ((info->flags & DF_TEXTREL) != 0)
5887 {
5888 if (!add_dynamic_entry (DT_TEXTREL, 0))
5889 return FALSE;
5890 }
5891 if (htab->is_vxworks
5892 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
5893 return FALSE;
5894 }
5895 #undef add_dynamic_entry
5896
5897 return TRUE;
5898 }
5899
5900 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5901
5902 static bfd_boolean
5903 ppc_elf_hash_symbol (struct elf_link_hash_entry *h)
5904 {
5905 if (h->plt.plist != NULL
5906 && !h->def_regular
5907 && (!h->pointer_equality_needed
5908 || !h->ref_regular_nonweak))
5909 return FALSE;
5910
5911 return _bfd_elf_hash_symbol (h);
5912 }
5913 \f
5914 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
5915
5916 /* Relaxation trampolines. r12 is available for clobbering (r11, is
5917 used for some functions that are allowed to break the ABI). */
5918 static const int shared_stub_entry[] =
5919 {
5920 0x7c0802a6, /* mflr 0 */
5921 0x429f0005, /* bcl 20, 31, .Lxxx */
5922 0x7d8802a6, /* mflr 12 */
5923 0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
5924 0x398c0008, /* addi 12, 12, (xxx-.Lxxx)@l */
5925 0x7c0803a6, /* mtlr 0 */
5926 0x7d8903a6, /* mtctr 12 */
5927 0x4e800420, /* bctr */
5928 };
5929
5930 static const int stub_entry[] =
5931 {
5932 0x3d800000, /* lis 12,xxx@ha */
5933 0x398c0000, /* addi 12,12,xxx@l */
5934 0x7d8903a6, /* mtctr 12 */
5935 0x4e800420, /* bctr */
5936 };
5937
5938 static bfd_boolean
5939 ppc_elf_relax_section (bfd *abfd,
5940 asection *isec,
5941 struct bfd_link_info *link_info,
5942 bfd_boolean *again)
5943 {
5944 struct one_fixup
5945 {
5946 struct one_fixup *next;
5947 asection *tsec;
5948 /* Final link, can use the symbol offset. For a
5949 relocatable link we use the symbol's index. */
5950 bfd_vma toff;
5951 bfd_vma trampoff;
5952 };
5953
5954 Elf_Internal_Shdr *symtab_hdr;
5955 bfd_byte *contents = NULL;
5956 Elf_Internal_Sym *isymbuf = NULL;
5957 Elf_Internal_Rela *internal_relocs = NULL;
5958 Elf_Internal_Rela *irel, *irelend;
5959 struct one_fixup *fixups = NULL;
5960 unsigned changes = 0;
5961 struct ppc_elf_link_hash_table *htab;
5962 bfd_size_type trampoff;
5963 asection *got2;
5964
5965 *again = FALSE;
5966
5967 /* Nothing to do if there are no relocations, and no need to do
5968 anything with non-alloc or non-code sections. */
5969 if ((isec->flags & SEC_ALLOC) == 0
5970 || (isec->flags & SEC_CODE) == 0
5971 || (isec->flags & SEC_RELOC) == 0
5972 || isec->reloc_count == 0)
5973 return TRUE;
5974
5975 /* We cannot represent the required PIC relocs in the output, so don't
5976 do anything. The linker doesn't support mixing -shared and -r
5977 anyway. */
5978 if (link_info->relocatable && link_info->shared)
5979 return TRUE;
5980
5981 trampoff = (isec->size + 3) & (bfd_vma) -4;
5982 /* Space for a branch around any trampolines. */
5983 trampoff += 4;
5984
5985 symtab_hdr = &elf_symtab_hdr (abfd);
5986
5987 /* Get a copy of the native relocations. */
5988 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
5989 link_info->keep_memory);
5990 if (internal_relocs == NULL)
5991 goto error_return;
5992
5993 htab = ppc_elf_hash_table (link_info);
5994 got2 = bfd_get_section_by_name (abfd, ".got2");
5995
5996 irelend = internal_relocs + isec->reloc_count;
5997 for (irel = internal_relocs; irel < irelend; irel++)
5998 {
5999 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
6000 bfd_vma toff, roff;
6001 asection *tsec;
6002 struct one_fixup *f;
6003 size_t insn_offset = 0;
6004 bfd_vma max_branch_offset, val;
6005 bfd_byte *hit_addr;
6006 unsigned long t0;
6007 struct elf_link_hash_entry *h;
6008 struct plt_entry **plist;
6009 unsigned char sym_type;
6010
6011 switch (r_type)
6012 {
6013 case R_PPC_REL24:
6014 case R_PPC_LOCAL24PC:
6015 case R_PPC_PLTREL24:
6016 max_branch_offset = 1 << 25;
6017 break;
6018
6019 case R_PPC_REL14:
6020 case R_PPC_REL14_BRTAKEN:
6021 case R_PPC_REL14_BRNTAKEN:
6022 max_branch_offset = 1 << 15;
6023 break;
6024
6025 default:
6026 continue;
6027 }
6028
6029 /* Get the value of the symbol referred to by the reloc. */
6030 h = NULL;
6031 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6032 {
6033 /* A local symbol. */
6034 Elf_Internal_Sym *isym;
6035
6036 /* Read this BFD's local symbols. */
6037 if (isymbuf == NULL)
6038 {
6039 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6040 if (isymbuf == NULL)
6041 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6042 symtab_hdr->sh_info, 0,
6043 NULL, NULL, NULL);
6044 if (isymbuf == 0)
6045 goto error_return;
6046 }
6047 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6048 if (isym->st_shndx == SHN_UNDEF)
6049 tsec = bfd_und_section_ptr;
6050 else if (isym->st_shndx == SHN_ABS)
6051 tsec = bfd_abs_section_ptr;
6052 else if (isym->st_shndx == SHN_COMMON)
6053 tsec = bfd_com_section_ptr;
6054 else
6055 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6056
6057 toff = isym->st_value;
6058 sym_type = ELF_ST_TYPE (isym->st_info);
6059 }
6060 else
6061 {
6062 /* Global symbol handling. */
6063 unsigned long indx;
6064
6065 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6066 h = elf_sym_hashes (abfd)[indx];
6067
6068 while (h->root.type == bfd_link_hash_indirect
6069 || h->root.type == bfd_link_hash_warning)
6070 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6071
6072 if (h->root.type == bfd_link_hash_defined
6073 || h->root.type == bfd_link_hash_defweak)
6074 {
6075 tsec = h->root.u.def.section;
6076 toff = h->root.u.def.value;
6077 }
6078 else if (h->root.type == bfd_link_hash_undefined
6079 || h->root.type == bfd_link_hash_undefweak)
6080 {
6081 tsec = bfd_und_section_ptr;
6082 toff = link_info->relocatable ? indx : 0;
6083 }
6084 else
6085 continue;
6086
6087 sym_type = h->type;
6088 }
6089
6090 /* The condition here under which we call find_plt_ent must
6091 match that in relocate_section. If we call find_plt_ent here
6092 but not in relocate_section, or vice versa, then the branch
6093 destination used here may be incorrect. */
6094 plist = NULL;
6095 if (h != NULL)
6096 {
6097 /* We know is_branch_reloc (r_type) is true. */
6098 if (h->type == STT_GNU_IFUNC
6099 || r_type == R_PPC_PLTREL24)
6100 plist = &h->plt.plist;
6101 }
6102 else if (sym_type == STT_GNU_IFUNC
6103 && elf_local_got_offsets (abfd) != NULL)
6104 {
6105 bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
6106 struct plt_entry **local_plt = (struct plt_entry **)
6107 (local_got_offsets + symtab_hdr->sh_info);
6108 plist = local_plt + ELF32_R_SYM (irel->r_info);
6109 }
6110 if (plist != NULL)
6111 {
6112 bfd_vma addend = 0;
6113 struct plt_entry *ent;
6114
6115 if (r_type == R_PPC_PLTREL24 && link_info->shared)
6116 addend = irel->r_addend;
6117 ent = find_plt_ent (plist, got2, addend);
6118 if (ent != NULL)
6119 {
6120 if (htab->plt_type == PLT_NEW
6121 || h == NULL
6122 || !htab->elf.dynamic_sections_created
6123 || h->dynindx == -1)
6124 {
6125 tsec = htab->glink;
6126 toff = ent->glink_offset;
6127 }
6128 else
6129 {
6130 tsec = htab->plt;
6131 toff = ent->plt.offset;
6132 }
6133 }
6134 }
6135
6136 /* If the branch and target are in the same section, you have
6137 no hope of adding stubs. We'll error out later should the
6138 branch overflow. */
6139 if (tsec == isec)
6140 continue;
6141
6142 /* There probably isn't any reason to handle symbols in
6143 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
6144 attribute for a code section, and we are only looking at
6145 branches. However, implement it correctly here as a
6146 reference for other target relax_section functions. */
6147 if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
6148 {
6149 /* At this stage in linking, no SEC_MERGE symbol has been
6150 adjusted, so all references to such symbols need to be
6151 passed through _bfd_merged_section_offset. (Later, in
6152 relocate_section, all SEC_MERGE symbols *except* for
6153 section symbols have been adjusted.)
6154
6155 gas may reduce relocations against symbols in SEC_MERGE
6156 sections to a relocation against the section symbol when
6157 the original addend was zero. When the reloc is against
6158 a section symbol we should include the addend in the
6159 offset passed to _bfd_merged_section_offset, since the
6160 location of interest is the original symbol. On the
6161 other hand, an access to "sym+addend" where "sym" is not
6162 a section symbol should not include the addend; Such an
6163 access is presumed to be an offset from "sym"; The
6164 location of interest is just "sym". */
6165 if (sym_type == STT_SECTION)
6166 toff += irel->r_addend;
6167
6168 toff = _bfd_merged_section_offset (abfd, &tsec,
6169 elf_section_data (tsec)->sec_info,
6170 toff);
6171
6172 if (sym_type != STT_SECTION)
6173 toff += irel->r_addend;
6174 }
6175 /* PLTREL24 addends are special. */
6176 else if (r_type != R_PPC_PLTREL24)
6177 toff += irel->r_addend;
6178
6179 /* Attempted -shared link of non-pic code loses. */
6180 if (tsec->output_section == NULL)
6181 continue;
6182
6183 roff = irel->r_offset;
6184
6185 /* If the branch is in range, no need to do anything. */
6186 if (tsec != bfd_und_section_ptr
6187 && (!link_info->relocatable
6188 /* A relocatable link may have sections moved during
6189 final link, so do not presume they remain in range. */
6190 || tsec->output_section == isec->output_section))
6191 {
6192 bfd_vma symaddr, reladdr;
6193
6194 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
6195 reladdr = isec->output_section->vma + isec->output_offset + roff;
6196 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
6197 continue;
6198 }
6199
6200 /* Look for an existing fixup to this address. */
6201 for (f = fixups; f ; f = f->next)
6202 if (f->tsec == tsec && f->toff == toff)
6203 break;
6204
6205 if (f == NULL)
6206 {
6207 size_t size;
6208 unsigned long stub_rtype;
6209
6210 val = trampoff - roff;
6211 if (val >= max_branch_offset)
6212 /* Oh dear, we can't reach a trampoline. Don't try to add
6213 one. We'll report an error later. */
6214 continue;
6215
6216 if (link_info->shared)
6217 {
6218 size = 4 * ARRAY_SIZE (shared_stub_entry);
6219 insn_offset = 12;
6220 }
6221 else
6222 {
6223 size = 4 * ARRAY_SIZE (stub_entry);
6224 insn_offset = 0;
6225 }
6226 stub_rtype = R_PPC_RELAX;
6227 if (tsec == htab->plt
6228 || tsec == htab->glink)
6229 {
6230 stub_rtype = R_PPC_RELAX_PLT;
6231 if (r_type == R_PPC_PLTREL24)
6232 stub_rtype = R_PPC_RELAX_PLTREL24;
6233 }
6234
6235 /* Hijack the old relocation. Since we need two
6236 relocations for this use a "composite" reloc. */
6237 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
6238 stub_rtype);
6239 irel->r_offset = trampoff + insn_offset;
6240 if (r_type == R_PPC_PLTREL24
6241 && stub_rtype != R_PPC_RELAX_PLTREL24)
6242 irel->r_addend = 0;
6243
6244 /* Record the fixup so we don't do it again this section. */
6245 f = bfd_malloc (sizeof (*f));
6246 f->next = fixups;
6247 f->tsec = tsec;
6248 f->toff = toff;
6249 f->trampoff = trampoff;
6250 fixups = f;
6251
6252 trampoff += size;
6253 changes++;
6254 }
6255 else
6256 {
6257 val = f->trampoff - roff;
6258 if (val >= max_branch_offset)
6259 continue;
6260
6261 /* Nop out the reloc, since we're finalizing things here. */
6262 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6263 }
6264
6265 /* Get the section contents. */
6266 if (contents == NULL)
6267 {
6268 /* Get cached copy if it exists. */
6269 if (elf_section_data (isec)->this_hdr.contents != NULL)
6270 contents = elf_section_data (isec)->this_hdr.contents;
6271 else
6272 {
6273 /* Go get them off disk. */
6274 if (!bfd_malloc_and_get_section (abfd, isec, &contents))
6275 goto error_return;
6276 }
6277 }
6278
6279 /* Fix up the existing branch to hit the trampoline. */
6280 hit_addr = contents + roff;
6281 switch (r_type)
6282 {
6283 case R_PPC_REL24:
6284 case R_PPC_LOCAL24PC:
6285 case R_PPC_PLTREL24:
6286 t0 = bfd_get_32 (abfd, hit_addr);
6287 t0 &= ~0x3fffffc;
6288 t0 |= val & 0x3fffffc;
6289 bfd_put_32 (abfd, t0, hit_addr);
6290 break;
6291
6292 case R_PPC_REL14:
6293 case R_PPC_REL14_BRTAKEN:
6294 case R_PPC_REL14_BRNTAKEN:
6295 t0 = bfd_get_32 (abfd, hit_addr);
6296 t0 &= ~0xfffc;
6297 t0 |= val & 0xfffc;
6298 bfd_put_32 (abfd, t0, hit_addr);
6299 break;
6300 }
6301 }
6302
6303 /* Write out the trampolines. */
6304 if (fixups != NULL)
6305 {
6306 const int *stub;
6307 bfd_byte *dest;
6308 bfd_vma val;
6309 int i, size;
6310
6311 do
6312 {
6313 struct one_fixup *f = fixups;
6314 fixups = fixups->next;
6315 free (f);
6316 }
6317 while (fixups);
6318
6319 contents = bfd_realloc_or_free (contents, trampoff);
6320 if (contents == NULL)
6321 goto error_return;
6322
6323 isec->size = (isec->size + 3) & (bfd_vma) -4;
6324 /* Branch around the trampolines. */
6325 val = B + trampoff - isec->size;
6326 dest = contents + isec->size;
6327 isec->size = trampoff;
6328 bfd_put_32 (abfd, val, dest);
6329 dest += 4;
6330
6331 if (link_info->shared)
6332 {
6333 stub = shared_stub_entry;
6334 size = ARRAY_SIZE (shared_stub_entry);
6335 }
6336 else
6337 {
6338 stub = stub_entry;
6339 size = ARRAY_SIZE (stub_entry);
6340 }
6341
6342 i = 0;
6343 while (dest < contents + trampoff)
6344 {
6345 bfd_put_32 (abfd, stub[i], dest);
6346 i++;
6347 if (i == size)
6348 i = 0;
6349 dest += 4;
6350 }
6351 BFD_ASSERT (i == 0);
6352 }
6353
6354 if (isymbuf != NULL
6355 && symtab_hdr->contents != (unsigned char *) isymbuf)
6356 {
6357 if (! link_info->keep_memory)
6358 free (isymbuf);
6359 else
6360 {
6361 /* Cache the symbols for elf_link_input_bfd. */
6362 symtab_hdr->contents = (unsigned char *) isymbuf;
6363 }
6364 }
6365
6366 if (contents != NULL
6367 && elf_section_data (isec)->this_hdr.contents != contents)
6368 {
6369 if (!changes && !link_info->keep_memory)
6370 free (contents);
6371 else
6372 {
6373 /* Cache the section contents for elf_link_input_bfd. */
6374 elf_section_data (isec)->this_hdr.contents = contents;
6375 }
6376 }
6377
6378 if (changes != 0)
6379 {
6380 /* Append sufficient NOP relocs so we can write out relocation
6381 information for the trampolines. */
6382 Elf_Internal_Rela *new_relocs = bfd_malloc ((changes + isec->reloc_count)
6383 * sizeof (*new_relocs));
6384 unsigned ix;
6385
6386 if (!new_relocs)
6387 goto error_return;
6388 memcpy (new_relocs, internal_relocs,
6389 isec->reloc_count * sizeof (*new_relocs));
6390 for (ix = changes; ix--;)
6391 {
6392 irel = new_relocs + ix + isec->reloc_count;
6393
6394 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6395 }
6396 if (internal_relocs != elf_section_data (isec)->relocs)
6397 free (internal_relocs);
6398 elf_section_data (isec)->relocs = new_relocs;
6399 isec->reloc_count += changes;
6400 elf_section_data (isec)->rel_hdr.sh_size
6401 += changes * elf_section_data (isec)->rel_hdr.sh_entsize;
6402 }
6403 else if (elf_section_data (isec)->relocs != internal_relocs)
6404 free (internal_relocs);
6405
6406 *again = changes != 0;
6407 if (!*again && link_info->relocatable)
6408 {
6409 /* Convert the internal relax relocs to external form. */
6410 for (irel = internal_relocs; irel < irelend; irel++)
6411 if (ELF32_R_TYPE (irel->r_info) == R_PPC_RELAX)
6412 {
6413 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
6414
6415 /* Rewrite the reloc and convert one of the trailing nop
6416 relocs to describe this relocation. */
6417 BFD_ASSERT (ELF32_R_TYPE (irelend[-1].r_info) == R_PPC_NONE);
6418 /* The relocs are at the bottom 2 bytes */
6419 irel[0].r_offset += 2;
6420 memmove (irel + 1, irel, (irelend - irel - 1) * sizeof (*irel));
6421 irel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
6422 irel[1].r_offset += 4;
6423 irel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
6424 irel++;
6425 }
6426 }
6427
6428 return TRUE;
6429
6430 error_return:
6431 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
6432 free (isymbuf);
6433 if (contents != NULL
6434 && elf_section_data (isec)->this_hdr.contents != contents)
6435 free (contents);
6436 if (internal_relocs != NULL
6437 && elf_section_data (isec)->relocs != internal_relocs)
6438 free (internal_relocs);
6439 return FALSE;
6440 }
6441 \f
6442 /* What to do when ld finds relocations against symbols defined in
6443 discarded sections. */
6444
6445 static unsigned int
6446 ppc_elf_action_discarded (asection *sec)
6447 {
6448 if (strcmp (".fixup", sec->name) == 0)
6449 return 0;
6450
6451 if (strcmp (".got2", sec->name) == 0)
6452 return 0;
6453
6454 return _bfd_elf_default_action_discarded (sec);
6455 }
6456 \f
6457 /* Fill in the address for a pointer generated in a linker section. */
6458
6459 static bfd_vma
6460 elf_finish_pointer_linker_section (bfd *input_bfd,
6461 elf_linker_section_t *lsect,
6462 struct elf_link_hash_entry *h,
6463 bfd_vma relocation,
6464 const Elf_Internal_Rela *rel)
6465 {
6466 elf_linker_section_pointers_t *linker_section_ptr;
6467
6468 BFD_ASSERT (lsect != NULL);
6469
6470 if (h != NULL)
6471 {
6472 /* Handle global symbol. */
6473 struct ppc_elf_link_hash_entry *eh;
6474
6475 eh = (struct ppc_elf_link_hash_entry *) h;
6476 BFD_ASSERT (eh->elf.def_regular);
6477 linker_section_ptr = eh->linker_section_pointer;
6478 }
6479 else
6480 {
6481 /* Handle local symbol. */
6482 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
6483
6484 BFD_ASSERT (is_ppc_elf (input_bfd));
6485 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
6486 linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
6487 }
6488
6489 linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
6490 rel->r_addend,
6491 lsect);
6492 BFD_ASSERT (linker_section_ptr != NULL);
6493
6494 /* Offset will always be a multiple of four, so use the bottom bit
6495 as a "written" flag. */
6496 if ((linker_section_ptr->offset & 1) == 0)
6497 {
6498 bfd_put_32 (lsect->section->owner,
6499 relocation + linker_section_ptr->addend,
6500 lsect->section->contents + linker_section_ptr->offset);
6501 linker_section_ptr->offset += 1;
6502 }
6503
6504 relocation = (lsect->section->output_section->vma
6505 + lsect->section->output_offset
6506 + linker_section_ptr->offset - 1
6507 - SYM_VAL (lsect->sym));
6508
6509 #ifdef DEBUG
6510 fprintf (stderr,
6511 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6512 lsect->name, (long) relocation, (long) relocation);
6513 #endif
6514
6515 return relocation;
6516 }
6517
6518 #define PPC_LO(v) ((v) & 0xffff)
6519 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6520 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6521
6522 static void
6523 write_glink_stub (struct plt_entry *ent, asection *plt_sec, unsigned char *p,
6524 struct bfd_link_info *info)
6525 {
6526 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
6527 bfd *output_bfd = info->output_bfd;
6528 bfd_vma plt;
6529
6530 plt = ((ent->plt.offset & ~1)
6531 + plt_sec->output_section->vma
6532 + plt_sec->output_offset);
6533
6534 if (info->shared)
6535 {
6536 bfd_vma got = 0;
6537
6538 if (ent->addend >= 32768)
6539 got = (ent->addend
6540 + ent->sec->output_section->vma
6541 + ent->sec->output_offset);
6542 else if (htab->elf.hgot != NULL)
6543 got = SYM_VAL (htab->elf.hgot);
6544
6545 plt -= got;
6546
6547 if (plt + 0x8000 < 0x10000)
6548 {
6549 bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
6550 p += 4;
6551 bfd_put_32 (output_bfd, MTCTR_11, p);
6552 p += 4;
6553 bfd_put_32 (output_bfd, BCTR, p);
6554 p += 4;
6555 bfd_put_32 (output_bfd, NOP, p);
6556 p += 4;
6557 }
6558 else
6559 {
6560 bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
6561 p += 4;
6562 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6563 p += 4;
6564 bfd_put_32 (output_bfd, MTCTR_11, p);
6565 p += 4;
6566 bfd_put_32 (output_bfd, BCTR, p);
6567 p += 4;
6568 }
6569 }
6570 else
6571 {
6572 bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
6573 p += 4;
6574 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6575 p += 4;
6576 bfd_put_32 (output_bfd, MTCTR_11, p);
6577 p += 4;
6578 bfd_put_32 (output_bfd, BCTR, p);
6579 p += 4;
6580 }
6581 }
6582
6583 /* Return true if symbol is defined statically. */
6584
6585 static bfd_boolean
6586 is_static_defined (struct elf_link_hash_entry *h)
6587 {
6588 return ((h->root.type == bfd_link_hash_defined
6589 || h->root.type == bfd_link_hash_defweak)
6590 && h->root.u.def.section != NULL
6591 && h->root.u.def.section->output_section != NULL);
6592 }
6593
6594 /* If INSN is an opcode that may be used with an @tls operand, return
6595 the transformed insn for TLS optimisation, otherwise return 0. If
6596 REG is non-zero only match an insn with RB or RA equal to REG. */
6597
6598 unsigned int
6599 _bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
6600 {
6601 unsigned int rtra;
6602
6603 if ((insn & (0x3f << 26)) != 31 << 26)
6604 return 0;
6605
6606 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
6607 rtra = insn & ((1 << 26) - (1 << 16));
6608 else if (((insn >> 16) & 0x1f) == reg)
6609 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
6610 else
6611 return 0;
6612
6613 if ((insn & (0x3ff << 1)) == 266 << 1)
6614 /* add -> addi. */
6615 insn = 14 << 26;
6616 else if ((insn & (0x1f << 1)) == 23 << 1
6617 && ((insn & (0x1f << 6)) < 14 << 6
6618 || ((insn & (0x1f << 6)) >= 16 << 6
6619 && (insn & (0x1f << 6)) < 24 << 6)))
6620 /* load and store indexed -> dform. */
6621 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
6622 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
6623 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
6624 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
6625 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
6626 /* lwax -> lwa. */
6627 insn = (58 << 26) | 2;
6628 else
6629 return 0;
6630 insn |= rtra;
6631 return insn;
6632 }
6633
6634 /* If INSN is an opcode that may be used with an @tprel operand, return
6635 the transformed insn for an undefined weak symbol, ie. with the
6636 thread pointer REG operand removed. Otherwise return 0. */
6637
6638 unsigned int
6639 _bfd_elf_ppc_at_tprel_transform (unsigned int insn, unsigned int reg)
6640 {
6641 if ((insn & (0x1f << 16)) == reg << 16
6642 && ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6643 || (insn & (0x3f << 26)) == 15u << 26 /* addis */
6644 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6645 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6646 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6647 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6648 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6649 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6650 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6651 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6652 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6653 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6654 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6655 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6656 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6657 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6658 && (insn & 3) != 1)
6659 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6660 && ((insn & 3) == 0 || (insn & 3) == 3))))
6661 {
6662 insn &= ~(0x1f << 16);
6663 }
6664 else if ((insn & (0x1f << 21)) == reg << 21
6665 && ((insn & (0x3e << 26)) == 24u << 26 /* ori, oris */
6666 || (insn & (0x3e << 26)) == 26u << 26 /* xori,xoris */
6667 || (insn & (0x3e << 26)) == 28u << 26 /* andi,andis */))
6668 {
6669 insn &= ~(0x1f << 21);
6670 insn |= (insn & (0x1f << 16)) << 5;
6671 if ((insn & (0x3e << 26)) == 26 << 26 /* xori,xoris */)
6672 insn -= 2 >> 26; /* convert to ori,oris */
6673 }
6674 else
6675 insn = 0;
6676 return insn;
6677 }
6678
6679 /* The RELOCATE_SECTION function is called by the ELF backend linker
6680 to handle the relocations for a section.
6681
6682 The relocs are always passed as Rela structures; if the section
6683 actually uses Rel structures, the r_addend field will always be
6684 zero.
6685
6686 This function is responsible for adjust the section contents as
6687 necessary, and (if using Rela relocs and generating a
6688 relocatable output file) adjusting the reloc addend as
6689 necessary.
6690
6691 This function does not have to worry about setting the reloc
6692 address or the reloc symbol index.
6693
6694 LOCAL_SYMS is a pointer to the swapped in local symbols.
6695
6696 LOCAL_SECTIONS is an array giving the section in the input file
6697 corresponding to the st_shndx field of each local symbol.
6698
6699 The global hash table entry for the global symbols can be found
6700 via elf_sym_hashes (input_bfd).
6701
6702 When generating relocatable output, this function must handle
6703 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
6704 going to be the section symbol corresponding to the output
6705 section, which means that the addend must be adjusted
6706 accordingly. */
6707
6708 static bfd_boolean
6709 ppc_elf_relocate_section (bfd *output_bfd,
6710 struct bfd_link_info *info,
6711 bfd *input_bfd,
6712 asection *input_section,
6713 bfd_byte *contents,
6714 Elf_Internal_Rela *relocs,
6715 Elf_Internal_Sym *local_syms,
6716 asection **local_sections)
6717 {
6718 Elf_Internal_Shdr *symtab_hdr;
6719 struct elf_link_hash_entry **sym_hashes;
6720 struct ppc_elf_link_hash_table *htab;
6721 Elf_Internal_Rela *rel;
6722 Elf_Internal_Rela *relend;
6723 Elf_Internal_Rela outrel;
6724 asection *got2, *sreloc = NULL;
6725 bfd_vma *local_got_offsets;
6726 bfd_boolean ret = TRUE;
6727 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
6728 bfd_boolean is_vxworks_tls;
6729
6730 #ifdef DEBUG
6731 _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
6732 "%ld relocations%s",
6733 input_bfd, input_section,
6734 (long) input_section->reloc_count,
6735 (info->relocatable) ? " (relocatable)" : "");
6736 #endif
6737
6738 got2 = bfd_get_section_by_name (input_bfd, ".got2");
6739
6740 /* Initialize howto table if not already done. */
6741 if (!ppc_elf_howto_table[R_PPC_ADDR32])
6742 ppc_elf_howto_init ();
6743
6744 htab = ppc_elf_hash_table (info);
6745 local_got_offsets = elf_local_got_offsets (input_bfd);
6746 symtab_hdr = &elf_symtab_hdr (input_bfd);
6747 sym_hashes = elf_sym_hashes (input_bfd);
6748 /* We have to handle relocations in vxworks .tls_vars sections
6749 specially, because the dynamic loader is 'weird'. */
6750 is_vxworks_tls = (htab->is_vxworks && info->shared
6751 && !strcmp (input_section->output_section->name,
6752 ".tls_vars"));
6753 rel = relocs;
6754 relend = relocs + input_section->reloc_count;
6755 for (; rel < relend; rel++)
6756 {
6757 enum elf_ppc_reloc_type r_type;
6758 bfd_vma addend;
6759 bfd_reloc_status_type r;
6760 Elf_Internal_Sym *sym;
6761 asection *sec;
6762 struct elf_link_hash_entry *h;
6763 const char *sym_name;
6764 reloc_howto_type *howto;
6765 unsigned long r_symndx;
6766 bfd_vma relocation;
6767 bfd_vma branch_bit, from;
6768 bfd_boolean unresolved_reloc;
6769 bfd_boolean warned;
6770 unsigned int tls_type, tls_mask, tls_gd;
6771 struct plt_entry **ifunc;
6772
6773 r_type = ELF32_R_TYPE (rel->r_info);
6774 sym = NULL;
6775 sec = NULL;
6776 h = NULL;
6777 unresolved_reloc = FALSE;
6778 warned = FALSE;
6779 r_symndx = ELF32_R_SYM (rel->r_info);
6780
6781 if (r_symndx < symtab_hdr->sh_info)
6782 {
6783 sym = local_syms + r_symndx;
6784 sec = local_sections[r_symndx];
6785 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
6786
6787 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
6788 }
6789 else
6790 {
6791 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
6792 r_symndx, symtab_hdr, sym_hashes,
6793 h, sec, relocation,
6794 unresolved_reloc, warned);
6795
6796 sym_name = h->root.root.string;
6797 }
6798
6799 if (sec != NULL && elf_discarded_section (sec))
6800 {
6801 /* For relocs against symbols from removed linkonce sections,
6802 or sections discarded by a linker script, we just want the
6803 section contents zeroed. Avoid any special processing. */
6804 howto = NULL;
6805 if (r_type < R_PPC_max)
6806 howto = ppc_elf_howto_table[r_type];
6807 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
6808 rel->r_info = 0;
6809 rel->r_addend = 0;
6810 continue;
6811 }
6812
6813 if (info->relocatable)
6814 {
6815 if (got2 != NULL
6816 && r_type == R_PPC_PLTREL24
6817 && rel->r_addend >= 32768)
6818 {
6819 /* R_PPC_PLTREL24 is rather special. If non-zero, the
6820 addend specifies the GOT pointer offset within .got2. */
6821 rel->r_addend += got2->output_offset;
6822 }
6823 continue;
6824 }
6825
6826 /* TLS optimizations. Replace instruction sequences and relocs
6827 based on information we collected in tls_optimize. We edit
6828 RELOCS so that --emit-relocs will output something sensible
6829 for the final instruction stream. */
6830 tls_mask = 0;
6831 tls_gd = 0;
6832 if (h != NULL)
6833 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
6834 else if (local_got_offsets != NULL)
6835 {
6836 struct plt_entry **local_plt;
6837 char *lgot_masks;
6838 local_plt
6839 = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
6840 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
6841 tls_mask = lgot_masks[r_symndx];
6842 }
6843
6844 /* Ensure reloc mapping code below stays sane. */
6845 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
6846 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
6847 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
6848 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
6849 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
6850 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
6851 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
6852 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
6853 abort ();
6854 switch (r_type)
6855 {
6856 default:
6857 break;
6858
6859 case R_PPC_GOT_TPREL16:
6860 case R_PPC_GOT_TPREL16_LO:
6861 if ((tls_mask & TLS_TLS) != 0
6862 && (tls_mask & TLS_TPREL) == 0)
6863 {
6864 bfd_vma insn;
6865
6866 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
6867 insn &= 31 << 21;
6868 insn |= 0x3c020000; /* addis 0,2,0 */
6869 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
6870 r_type = R_PPC_TPREL16_HA;
6871 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6872 }
6873 break;
6874
6875 case R_PPC_TLS:
6876 if ((tls_mask & TLS_TLS) != 0
6877 && (tls_mask & TLS_TPREL) == 0)
6878 {
6879 bfd_vma insn;
6880
6881 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
6882 insn = _bfd_elf_ppc_at_tls_transform (insn, 2);
6883 if (insn == 0)
6884 abort ();
6885 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
6886 r_type = R_PPC_TPREL16_LO;
6887 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6888
6889 /* Was PPC_TLS which sits on insn boundary, now
6890 PPC_TPREL16_LO which is at low-order half-word. */
6891 rel->r_offset += d_offset;
6892 }
6893 break;
6894
6895 case R_PPC_GOT_TLSGD16_HI:
6896 case R_PPC_GOT_TLSGD16_HA:
6897 tls_gd = TLS_TPRELGD;
6898 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
6899 goto tls_gdld_hi;
6900 break;
6901
6902 case R_PPC_GOT_TLSLD16_HI:
6903 case R_PPC_GOT_TLSLD16_HA:
6904 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
6905 {
6906 tls_gdld_hi:
6907 if ((tls_mask & tls_gd) != 0)
6908 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6909 + R_PPC_GOT_TPREL16);
6910 else
6911 {
6912 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
6913 rel->r_offset -= d_offset;
6914 r_type = R_PPC_NONE;
6915 }
6916 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6917 }
6918 break;
6919
6920 case R_PPC_GOT_TLSGD16:
6921 case R_PPC_GOT_TLSGD16_LO:
6922 tls_gd = TLS_TPRELGD;
6923 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
6924 goto tls_ldgd_opt;
6925 break;
6926
6927 case R_PPC_GOT_TLSLD16:
6928 case R_PPC_GOT_TLSLD16_LO:
6929 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
6930 {
6931 unsigned int insn1, insn2;
6932 bfd_vma offset;
6933
6934 tls_ldgd_opt:
6935 offset = (bfd_vma) -1;
6936 /* If not using the newer R_PPC_TLSGD/LD to mark
6937 __tls_get_addr calls, we must trust that the call
6938 stays with its arg setup insns, ie. that the next
6939 reloc is the __tls_get_addr call associated with
6940 the current reloc. Edit both insns. */
6941 if (input_section->has_tls_get_addr_call
6942 && rel + 1 < relend
6943 && branch_reloc_hash_match (input_bfd, rel + 1,
6944 htab->tls_get_addr))
6945 offset = rel[1].r_offset;
6946 if ((tls_mask & tls_gd) != 0)
6947 {
6948 /* IE */
6949 insn1 = bfd_get_32 (output_bfd,
6950 contents + rel->r_offset - d_offset);
6951 insn1 &= (1 << 26) - 1;
6952 insn1 |= 32 << 26; /* lwz */
6953 if (offset != (bfd_vma) -1)
6954 {
6955 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
6956 insn2 = 0x7c631214; /* add 3,3,2 */
6957 bfd_put_32 (output_bfd, insn2, contents + offset);
6958 }
6959 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6960 + R_PPC_GOT_TPREL16);
6961 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6962 }
6963 else
6964 {
6965 /* LE */
6966 insn1 = 0x3c620000; /* addis 3,2,0 */
6967 if (tls_gd == 0)
6968 {
6969 /* Was an LD reloc. */
6970 for (r_symndx = 0;
6971 r_symndx < symtab_hdr->sh_info;
6972 r_symndx++)
6973 if (local_sections[r_symndx] == sec)
6974 break;
6975 if (r_symndx >= symtab_hdr->sh_info)
6976 r_symndx = 0;
6977 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
6978 if (r_symndx != 0)
6979 rel->r_addend -= (local_syms[r_symndx].st_value
6980 + sec->output_offset
6981 + sec->output_section->vma);
6982 }
6983 r_type = R_PPC_TPREL16_HA;
6984 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6985 if (offset != (bfd_vma) -1)
6986 {
6987 rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
6988 rel[1].r_offset = offset + d_offset;
6989 rel[1].r_addend = rel->r_addend;
6990 insn2 = 0x38630000; /* addi 3,3,0 */
6991 bfd_put_32 (output_bfd, insn2, contents + offset);
6992 }
6993 }
6994 bfd_put_32 (output_bfd, insn1,
6995 contents + rel->r_offset - d_offset);
6996 if (tls_gd == 0)
6997 {
6998 /* We changed the symbol on an LD reloc. Start over
6999 in order to get h, sym, sec etc. right. */
7000 rel--;
7001 continue;
7002 }
7003 }
7004 break;
7005
7006 case R_PPC_TLSGD:
7007 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
7008 {
7009 unsigned int insn2;
7010 bfd_vma offset = rel->r_offset;
7011
7012 if ((tls_mask & TLS_TPRELGD) != 0)
7013 {
7014 /* IE */
7015 r_type = R_PPC_NONE;
7016 insn2 = 0x7c631214; /* add 3,3,2 */
7017 }
7018 else
7019 {
7020 /* LE */
7021 r_type = R_PPC_TPREL16_LO;
7022 rel->r_offset += d_offset;
7023 insn2 = 0x38630000; /* addi 3,3,0 */
7024 }
7025 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7026 bfd_put_32 (output_bfd, insn2, contents + offset);
7027 /* Zap the reloc on the _tls_get_addr call too. */
7028 BFD_ASSERT (offset == rel[1].r_offset);
7029 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7030 }
7031 break;
7032
7033 case R_PPC_TLSLD:
7034 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
7035 {
7036 unsigned int insn2;
7037
7038 for (r_symndx = 0;
7039 r_symndx < symtab_hdr->sh_info;
7040 r_symndx++)
7041 if (local_sections[r_symndx] == sec)
7042 break;
7043 if (r_symndx >= symtab_hdr->sh_info)
7044 r_symndx = 0;
7045 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7046 if (r_symndx != 0)
7047 rel->r_addend -= (local_syms[r_symndx].st_value
7048 + sec->output_offset
7049 + sec->output_section->vma);
7050
7051 rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
7052 rel->r_offset += d_offset;
7053 insn2 = 0x38630000; /* addi 3,3,0 */
7054 bfd_put_32 (output_bfd, insn2,
7055 contents + rel->r_offset - d_offset);
7056 /* Zap the reloc on the _tls_get_addr call too. */
7057 BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
7058 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7059 rel--;
7060 continue;
7061 }
7062 break;
7063 }
7064
7065 /* Handle other relocations that tweak non-addend part of insn. */
7066 branch_bit = 0;
7067 switch (r_type)
7068 {
7069 default:
7070 break;
7071
7072 /* Branch taken prediction relocations. */
7073 case R_PPC_ADDR14_BRTAKEN:
7074 case R_PPC_REL14_BRTAKEN:
7075 branch_bit = BRANCH_PREDICT_BIT;
7076 /* Fall thru */
7077
7078 /* Branch not taken prediction relocations. */
7079 case R_PPC_ADDR14_BRNTAKEN:
7080 case R_PPC_REL14_BRNTAKEN:
7081 {
7082 bfd_vma insn;
7083
7084 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7085 insn &= ~BRANCH_PREDICT_BIT;
7086 insn |= branch_bit;
7087
7088 from = (rel->r_offset
7089 + input_section->output_offset
7090 + input_section->output_section->vma);
7091
7092 /* Invert 'y' bit if not the default. */
7093 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7094 insn ^= BRANCH_PREDICT_BIT;
7095
7096 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7097 break;
7098 }
7099 }
7100
7101 ifunc = NULL;
7102 if (!htab->is_vxworks)
7103 {
7104 struct plt_entry *ent;
7105
7106 if (h != NULL)
7107 {
7108 if (h->type == STT_GNU_IFUNC)
7109 ifunc = &h->plt.plist;
7110 }
7111 else if (local_got_offsets != NULL
7112 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
7113 {
7114 struct plt_entry **local_plt;
7115
7116 local_plt = (struct plt_entry **) (local_got_offsets
7117 + symtab_hdr->sh_info);
7118 ifunc = local_plt + r_symndx;
7119 }
7120
7121 ent = NULL;
7122 if (ifunc != NULL
7123 && (!info->shared
7124 || is_branch_reloc (r_type)))
7125 {
7126 addend = 0;
7127 if (r_type == R_PPC_PLTREL24 && info->shared)
7128 addend = rel->r_addend;
7129 ent = find_plt_ent (ifunc, got2, addend);
7130 }
7131 if (ent != NULL)
7132 {
7133 if (h == NULL && (ent->plt.offset & 1) == 0)
7134 {
7135 Elf_Internal_Rela rela;
7136 bfd_byte *loc;
7137
7138 rela.r_offset = (htab->iplt->output_section->vma
7139 + htab->iplt->output_offset
7140 + ent->plt.offset);
7141 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7142 rela.r_addend = relocation;
7143 loc = htab->reliplt->contents;
7144 loc += (htab->reliplt->reloc_count++
7145 * sizeof (Elf32_External_Rela));
7146 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
7147
7148 ent->plt.offset |= 1;
7149 }
7150 if (h == NULL && (ent->glink_offset & 1) == 0)
7151 {
7152 unsigned char *p = ((unsigned char *) htab->glink->contents
7153 + ent->glink_offset);
7154 write_glink_stub (ent, htab->iplt, p, info);
7155 ent->glink_offset |= 1;
7156 }
7157
7158 unresolved_reloc = FALSE;
7159 if (htab->plt_type == PLT_NEW
7160 || !htab->elf.dynamic_sections_created
7161 || h == NULL)
7162 relocation = (htab->glink->output_section->vma
7163 + htab->glink->output_offset
7164 + (ent->glink_offset & ~1));
7165 else
7166 relocation = (htab->plt->output_section->vma
7167 + htab->plt->output_offset
7168 + ent->plt.offset);
7169 }
7170 }
7171
7172 addend = rel->r_addend;
7173 tls_type = 0;
7174 howto = NULL;
7175 if (r_type < R_PPC_max)
7176 howto = ppc_elf_howto_table[r_type];
7177 switch (r_type)
7178 {
7179 default:
7180 (*_bfd_error_handler)
7181 (_("%B: unknown relocation type %d for symbol %s"),
7182 input_bfd, (int) r_type, sym_name);
7183
7184 bfd_set_error (bfd_error_bad_value);
7185 ret = FALSE;
7186 continue;
7187
7188 case R_PPC_NONE:
7189 case R_PPC_TLS:
7190 case R_PPC_TLSGD:
7191 case R_PPC_TLSLD:
7192 case R_PPC_EMB_MRKREF:
7193 case R_PPC_GNU_VTINHERIT:
7194 case R_PPC_GNU_VTENTRY:
7195 continue;
7196
7197 /* GOT16 relocations. Like an ADDR16 using the symbol's
7198 address in the GOT as relocation value instead of the
7199 symbol's value itself. Also, create a GOT entry for the
7200 symbol and put the symbol value there. */
7201 case R_PPC_GOT_TLSGD16:
7202 case R_PPC_GOT_TLSGD16_LO:
7203 case R_PPC_GOT_TLSGD16_HI:
7204 case R_PPC_GOT_TLSGD16_HA:
7205 tls_type = TLS_TLS | TLS_GD;
7206 goto dogot;
7207
7208 case R_PPC_GOT_TLSLD16:
7209 case R_PPC_GOT_TLSLD16_LO:
7210 case R_PPC_GOT_TLSLD16_HI:
7211 case R_PPC_GOT_TLSLD16_HA:
7212 tls_type = TLS_TLS | TLS_LD;
7213 goto dogot;
7214
7215 case R_PPC_GOT_TPREL16:
7216 case R_PPC_GOT_TPREL16_LO:
7217 case R_PPC_GOT_TPREL16_HI:
7218 case R_PPC_GOT_TPREL16_HA:
7219 tls_type = TLS_TLS | TLS_TPREL;
7220 goto dogot;
7221
7222 case R_PPC_GOT_DTPREL16:
7223 case R_PPC_GOT_DTPREL16_LO:
7224 case R_PPC_GOT_DTPREL16_HI:
7225 case R_PPC_GOT_DTPREL16_HA:
7226 tls_type = TLS_TLS | TLS_DTPREL;
7227 goto dogot;
7228
7229 case R_PPC_GOT16:
7230 case R_PPC_GOT16_LO:
7231 case R_PPC_GOT16_HI:
7232 case R_PPC_GOT16_HA:
7233 tls_mask = 0;
7234 dogot:
7235 {
7236 /* Relocation is to the entry for this symbol in the global
7237 offset table. */
7238 bfd_vma off;
7239 bfd_vma *offp;
7240 unsigned long indx;
7241
7242 if (htab->got == NULL)
7243 abort ();
7244
7245 indx = 0;
7246 if (tls_type == (TLS_TLS | TLS_LD)
7247 && (h == NULL
7248 || !h->def_dynamic))
7249 offp = &htab->tlsld_got.offset;
7250 else if (h != NULL)
7251 {
7252 bfd_boolean dyn;
7253 dyn = htab->elf.dynamic_sections_created;
7254 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7255 || (info->shared
7256 && SYMBOL_REFERENCES_LOCAL (info, h)))
7257 /* This is actually a static link, or it is a
7258 -Bsymbolic link and the symbol is defined
7259 locally, or the symbol was forced to be local
7260 because of a version file. */
7261 ;
7262 else
7263 {
7264 indx = h->dynindx;
7265 unresolved_reloc = FALSE;
7266 }
7267 offp = &h->got.offset;
7268 }
7269 else
7270 {
7271 if (local_got_offsets == NULL)
7272 abort ();
7273 offp = &local_got_offsets[r_symndx];
7274 }
7275
7276 /* The offset must always be a multiple of 4. We use the
7277 least significant bit to record whether we have already
7278 processed this entry. */
7279 off = *offp;
7280 if ((off & 1) != 0)
7281 off &= ~1;
7282 else
7283 {
7284 unsigned int tls_m = (tls_mask
7285 & (TLS_LD | TLS_GD | TLS_DTPREL
7286 | TLS_TPREL | TLS_TPRELGD));
7287
7288 if (offp == &htab->tlsld_got.offset)
7289 tls_m = TLS_LD;
7290 else if (h == NULL
7291 || !h->def_dynamic)
7292 tls_m &= ~TLS_LD;
7293
7294 /* We might have multiple got entries for this sym.
7295 Initialize them all. */
7296 do
7297 {
7298 int tls_ty = 0;
7299
7300 if ((tls_m & TLS_LD) != 0)
7301 {
7302 tls_ty = TLS_TLS | TLS_LD;
7303 tls_m &= ~TLS_LD;
7304 }
7305 else if ((tls_m & TLS_GD) != 0)
7306 {
7307 tls_ty = TLS_TLS | TLS_GD;
7308 tls_m &= ~TLS_GD;
7309 }
7310 else if ((tls_m & TLS_DTPREL) != 0)
7311 {
7312 tls_ty = TLS_TLS | TLS_DTPREL;
7313 tls_m &= ~TLS_DTPREL;
7314 }
7315 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
7316 {
7317 tls_ty = TLS_TLS | TLS_TPREL;
7318 tls_m = 0;
7319 }
7320
7321 /* Generate relocs for the dynamic linker. */
7322 if ((info->shared || indx != 0)
7323 && (offp == &htab->tlsld_got.offset
7324 || h == NULL
7325 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7326 || h->root.type != bfd_link_hash_undefweak))
7327 {
7328 asection *rsec = htab->relgot;
7329 bfd_byte * loc;
7330
7331 outrel.r_offset = (htab->got->output_section->vma
7332 + htab->got->output_offset
7333 + off);
7334 outrel.r_addend = 0;
7335 if (tls_ty & (TLS_LD | TLS_GD))
7336 {
7337 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
7338 if (tls_ty == (TLS_TLS | TLS_GD))
7339 {
7340 loc = rsec->contents;
7341 loc += (rsec->reloc_count++
7342 * sizeof (Elf32_External_Rela));
7343 bfd_elf32_swap_reloca_out (output_bfd,
7344 &outrel, loc);
7345 outrel.r_offset += 4;
7346 outrel.r_info
7347 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7348 }
7349 }
7350 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
7351 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7352 else if (tls_ty == (TLS_TLS | TLS_TPREL))
7353 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
7354 else if (indx != 0)
7355 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
7356 else if (ifunc != NULL)
7357 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7358 else
7359 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
7360 if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
7361 {
7362 outrel.r_addend += relocation;
7363 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
7364 outrel.r_addend -= htab->elf.tls_sec->vma;
7365 }
7366 loc = rsec->contents;
7367 loc += (rsec->reloc_count++
7368 * sizeof (Elf32_External_Rela));
7369 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7370 }
7371
7372 /* Init the .got section contents if we're not
7373 emitting a reloc. */
7374 else
7375 {
7376 bfd_vma value = relocation;
7377
7378 if (tls_ty == (TLS_TLS | TLS_LD))
7379 value = 1;
7380 else if (tls_ty != 0)
7381 {
7382 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
7383 if (tls_ty == (TLS_TLS | TLS_TPREL))
7384 value += DTP_OFFSET - TP_OFFSET;
7385
7386 if (tls_ty == (TLS_TLS | TLS_GD))
7387 {
7388 bfd_put_32 (output_bfd, value,
7389 htab->got->contents + off + 4);
7390 value = 1;
7391 }
7392 }
7393 bfd_put_32 (output_bfd, value,
7394 htab->got->contents + off);
7395 }
7396
7397 off += 4;
7398 if (tls_ty & (TLS_LD | TLS_GD))
7399 off += 4;
7400 }
7401 while (tls_m != 0);
7402
7403 off = *offp;
7404 *offp = off | 1;
7405 }
7406
7407 if (off >= (bfd_vma) -2)
7408 abort ();
7409
7410 if ((tls_type & TLS_TLS) != 0)
7411 {
7412 if (tls_type != (TLS_TLS | TLS_LD))
7413 {
7414 if ((tls_mask & TLS_LD) != 0
7415 && !(h == NULL
7416 || !h->def_dynamic))
7417 off += 8;
7418 if (tls_type != (TLS_TLS | TLS_GD))
7419 {
7420 if ((tls_mask & TLS_GD) != 0)
7421 off += 8;
7422 if (tls_type != (TLS_TLS | TLS_DTPREL))
7423 {
7424 if ((tls_mask & TLS_DTPREL) != 0)
7425 off += 4;
7426 }
7427 }
7428 }
7429 }
7430
7431 relocation = (htab->got->output_section->vma
7432 + htab->got->output_offset
7433 + off
7434 - SYM_VAL (htab->elf.hgot));
7435
7436 /* Addends on got relocations don't make much sense.
7437 x+off@got is actually x@got+off, and since the got is
7438 generated by a hash table traversal, the value in the
7439 got at entry m+n bears little relation to the entry m. */
7440 if (addend != 0)
7441 (*_bfd_error_handler)
7442 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
7443 input_bfd,
7444 input_section,
7445 (long) rel->r_offset,
7446 howto->name,
7447 sym_name);
7448 }
7449 break;
7450
7451 /* Relocations that need no special processing. */
7452 case R_PPC_LOCAL24PC:
7453 /* It makes no sense to point a local relocation
7454 at a symbol not in this object. */
7455 if (unresolved_reloc)
7456 {
7457 if (! (*info->callbacks->undefined_symbol) (info,
7458 h->root.root.string,
7459 input_bfd,
7460 input_section,
7461 rel->r_offset,
7462 TRUE))
7463 return FALSE;
7464 continue;
7465 }
7466 break;
7467
7468 case R_PPC_DTPREL16:
7469 case R_PPC_DTPREL16_LO:
7470 case R_PPC_DTPREL16_HI:
7471 case R_PPC_DTPREL16_HA:
7472 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7473 break;
7474
7475 /* Relocations that may need to be propagated if this is a shared
7476 object. */
7477 case R_PPC_TPREL16:
7478 case R_PPC_TPREL16_LO:
7479 case R_PPC_TPREL16_HI:
7480 case R_PPC_TPREL16_HA:
7481 if (h != NULL
7482 && h->root.type == bfd_link_hash_undefweak
7483 && h->dynindx == -1)
7484 {
7485 /* Make this relocation against an undefined weak symbol
7486 resolve to zero. This is really just a tweak, since
7487 code using weak externs ought to check that they are
7488 defined before using them. */
7489 bfd_byte *p = contents + rel->r_offset - d_offset;
7490 unsigned int insn = bfd_get_32 (output_bfd, p);
7491 insn = _bfd_elf_ppc_at_tprel_transform (insn, 2);
7492 if (insn != 0)
7493 bfd_put_32 (output_bfd, insn, p);
7494 break;
7495 }
7496 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7497 /* The TPREL16 relocs shouldn't really be used in shared
7498 libs as they will result in DT_TEXTREL being set, but
7499 support them anyway. */
7500 goto dodyn;
7501
7502 case R_PPC_TPREL32:
7503 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7504 goto dodyn;
7505
7506 case R_PPC_DTPREL32:
7507 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7508 goto dodyn;
7509
7510 case R_PPC_DTPMOD32:
7511 relocation = 1;
7512 addend = 0;
7513 goto dodyn;
7514
7515 case R_PPC_REL16:
7516 case R_PPC_REL16_LO:
7517 case R_PPC_REL16_HI:
7518 case R_PPC_REL16_HA:
7519 break;
7520
7521 case R_PPC_REL32:
7522 if (h == NULL || h == htab->elf.hgot)
7523 break;
7524 /* fall through */
7525
7526 case R_PPC_ADDR32:
7527 case R_PPC_ADDR16:
7528 case R_PPC_ADDR16_LO:
7529 case R_PPC_ADDR16_HI:
7530 case R_PPC_ADDR16_HA:
7531 case R_PPC_UADDR32:
7532 case R_PPC_UADDR16:
7533 goto dodyn;
7534
7535 case R_PPC_REL24:
7536 case R_PPC_REL14:
7537 case R_PPC_REL14_BRTAKEN:
7538 case R_PPC_REL14_BRNTAKEN:
7539 /* If these relocations are not to a named symbol, they can be
7540 handled right here, no need to bother the dynamic linker. */
7541 if (SYMBOL_CALLS_LOCAL (info, h)
7542 || h == htab->elf.hgot)
7543 break;
7544 /* fall through */
7545
7546 case R_PPC_ADDR24:
7547 case R_PPC_ADDR14:
7548 case R_PPC_ADDR14_BRTAKEN:
7549 case R_PPC_ADDR14_BRNTAKEN:
7550 if (h != NULL && !info->shared)
7551 break;
7552 /* fall through */
7553
7554 dodyn:
7555 if ((input_section->flags & SEC_ALLOC) == 0
7556 || is_vxworks_tls)
7557 break;
7558
7559 if ((info->shared
7560 && !(h != NULL
7561 && ((h->root.type == bfd_link_hash_undefined
7562 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7563 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
7564 || (h->root.type == bfd_link_hash_undefweak
7565 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)))
7566 && (must_be_dyn_reloc (info, r_type)
7567 || !SYMBOL_CALLS_LOCAL (info, h)))
7568 || (ELIMINATE_COPY_RELOCS
7569 && !info->shared
7570 && h != NULL
7571 && h->dynindx != -1
7572 && !h->non_got_ref
7573 && !h->def_regular))
7574 {
7575 int skip;
7576 bfd_byte * loc;
7577 #ifdef DEBUG
7578 fprintf (stderr, "ppc_elf_relocate_section needs to "
7579 "create relocation for %s\n",
7580 (h && h->root.root.string
7581 ? h->root.root.string : "<unknown>"));
7582 #endif
7583
7584 /* When generating a shared object, these relocations
7585 are copied into the output file to be resolved at run
7586 time. */
7587 if (sreloc == NULL)
7588 {
7589 sreloc = elf_section_data (input_section)->sreloc;
7590 if (!htab->elf.dynamic_sections_created)
7591 sreloc = htab->reliplt;
7592 if (sreloc == NULL)
7593 return FALSE;
7594 }
7595
7596 skip = 0;
7597 outrel.r_offset =
7598 _bfd_elf_section_offset (output_bfd, info, input_section,
7599 rel->r_offset);
7600 if (outrel.r_offset == (bfd_vma) -1
7601 || outrel.r_offset == (bfd_vma) -2)
7602 skip = (int) outrel.r_offset;
7603 outrel.r_offset += (input_section->output_section->vma
7604 + input_section->output_offset);
7605
7606 if (skip)
7607 memset (&outrel, 0, sizeof outrel);
7608 else if ((h != NULL
7609 && (h->root.type == bfd_link_hash_undefined
7610 || h->root.type == bfd_link_hash_undefweak))
7611 || !SYMBOL_REFERENCES_LOCAL (info, h))
7612 {
7613 unresolved_reloc = FALSE;
7614 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
7615 outrel.r_addend = rel->r_addend;
7616 }
7617 else
7618 {
7619 outrel.r_addend = relocation + rel->r_addend;
7620
7621 if (r_type != R_PPC_ADDR32)
7622 {
7623 long indx = 0;
7624
7625 if (ifunc != NULL)
7626 {
7627 /* If we get here when building a static
7628 executable, then the libc startup function
7629 responsible for applying indirect function
7630 relocations is going to complain about
7631 the reloc type.
7632 If we get here when building a dynamic
7633 executable, it will be because we have
7634 a text relocation. The dynamic loader
7635 will set the text segment writable and
7636 non-executable to apply text relocations.
7637 So we'll segfault when trying to run the
7638 indirection function to resolve the reloc. */
7639 (*_bfd_error_handler)
7640 (_("%B(%A+0x%lx): relocation %s for indirect "
7641 "function %s unsupported"),
7642 input_bfd,
7643 input_section,
7644 (long) rel->r_offset,
7645 howto->name,
7646 sym_name);
7647 ret = FALSE;
7648 }
7649 else if (r_symndx == 0 || bfd_is_abs_section (sec))
7650 ;
7651 else if (sec == NULL || sec->owner == NULL)
7652 {
7653 bfd_set_error (bfd_error_bad_value);
7654 ret = FALSE;
7655 }
7656 else
7657 {
7658 asection *osec;
7659
7660 /* We are turning this relocation into one
7661 against a section symbol. It would be
7662 proper to subtract the symbol's value,
7663 osec->vma, from the emitted reloc addend,
7664 but ld.so expects buggy relocs.
7665 FIXME: Why not always use a zero index? */
7666 osec = sec->output_section;
7667 indx = elf_section_data (osec)->dynindx;
7668 if (indx == 0)
7669 {
7670 osec = htab->elf.text_index_section;
7671 indx = elf_section_data (osec)->dynindx;
7672 }
7673 BFD_ASSERT (indx != 0);
7674 #ifdef DEBUG
7675 if (indx == 0)
7676 printf ("indx=%ld section=%s flags=%08x name=%s\n",
7677 indx, osec->name, osec->flags,
7678 h->root.root.string);
7679 #endif
7680 }
7681
7682 outrel.r_info = ELF32_R_INFO (indx, r_type);
7683 }
7684 else if (ifunc != NULL)
7685 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7686 else
7687 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
7688 }
7689
7690 loc = sreloc->contents;
7691 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
7692 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7693
7694 if (skip == -1)
7695 continue;
7696
7697 /* This reloc will be computed at runtime. We clear the memory
7698 so that it contains predictable value. */
7699 if (! skip
7700 && ((input_section->flags & SEC_ALLOC) != 0
7701 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
7702 {
7703 relocation = howto->pc_relative ? outrel.r_offset : 0;
7704 addend = 0;
7705 break;
7706 }
7707 }
7708 break;
7709
7710 case R_PPC_RELAX_PLT:
7711 case R_PPC_RELAX_PLTREL24:
7712 if (h != NULL)
7713 {
7714 struct plt_entry *ent;
7715 bfd_vma got2_addend = 0;
7716
7717 if (r_type == R_PPC_RELAX_PLTREL24)
7718 {
7719 if (info->shared)
7720 got2_addend = addend;
7721 addend = 0;
7722 }
7723 ent = find_plt_ent (&h->plt.plist, got2, got2_addend);
7724 if (htab->plt_type == PLT_NEW)
7725 relocation = (htab->glink->output_section->vma
7726 + htab->glink->output_offset
7727 + ent->glink_offset);
7728 else
7729 relocation = (htab->plt->output_section->vma
7730 + htab->plt->output_offset
7731 + ent->plt.offset);
7732 }
7733 /* Fall thru */
7734
7735 case R_PPC_RELAX:
7736 if (info->shared)
7737 relocation -= (input_section->output_section->vma
7738 + input_section->output_offset
7739 + rel->r_offset - 4);
7740
7741 {
7742 unsigned long t0;
7743 unsigned long t1;
7744
7745 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
7746 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
7747
7748 /* We're clearing the bits for R_PPC_ADDR16_HA
7749 and R_PPC_ADDR16_LO here. */
7750 t0 &= ~0xffff;
7751 t1 &= ~0xffff;
7752
7753 /* t0 is HA, t1 is LO */
7754 relocation += addend;
7755 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
7756 t1 |= relocation & 0xffff;
7757
7758 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
7759 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
7760
7761 /* Rewrite the reloc and convert one of the trailing nop
7762 relocs to describe this relocation. */
7763 BFD_ASSERT (ELF32_R_TYPE (relend[-1].r_info) == R_PPC_NONE);
7764 /* The relocs are at the bottom 2 bytes */
7765 rel[0].r_offset += 2;
7766 memmove (rel + 1, rel, (relend - rel - 1) * sizeof (*rel));
7767 rel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
7768 rel[1].r_offset += 4;
7769 rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
7770 rel++;
7771 }
7772 continue;
7773
7774 /* Indirect .sdata relocation. */
7775 case R_PPC_EMB_SDAI16:
7776 BFD_ASSERT (htab->sdata[0].section != NULL);
7777 if (!is_static_defined (htab->sdata[0].sym))
7778 {
7779 unresolved_reloc = TRUE;
7780 break;
7781 }
7782 relocation
7783 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
7784 h, relocation, rel);
7785 addend = 0;
7786 break;
7787
7788 /* Indirect .sdata2 relocation. */
7789 case R_PPC_EMB_SDA2I16:
7790 BFD_ASSERT (htab->sdata[1].section != NULL);
7791 if (!is_static_defined (htab->sdata[1].sym))
7792 {
7793 unresolved_reloc = TRUE;
7794 break;
7795 }
7796 relocation
7797 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
7798 h, relocation, rel);
7799 addend = 0;
7800 break;
7801
7802 /* Handle the TOC16 reloc. We want to use the offset within the .got
7803 section, not the actual VMA. This is appropriate when generating
7804 an embedded ELF object, for which the .got section acts like the
7805 AIX .toc section. */
7806 case R_PPC_TOC16: /* phony GOT16 relocations */
7807 if (sec == NULL || sec->output_section == NULL)
7808 {
7809 unresolved_reloc = TRUE;
7810 break;
7811 }
7812 BFD_ASSERT (strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
7813 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
7814
7815 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
7816 break;
7817
7818 case R_PPC_PLTREL24:
7819 if (h == NULL || ifunc != NULL)
7820 break;
7821 /* Relocation is to the entry for this symbol in the
7822 procedure linkage table. */
7823 {
7824 struct plt_entry *ent = find_plt_ent (&h->plt.plist, got2,
7825 info->shared ? addend : 0);
7826 addend = 0;
7827 if (ent == NULL
7828 || htab->plt == NULL)
7829 {
7830 /* We didn't make a PLT entry for this symbol. This
7831 happens when statically linking PIC code, or when
7832 using -Bsymbolic. */
7833 break;
7834 }
7835
7836 unresolved_reloc = FALSE;
7837 if (htab->plt_type == PLT_NEW)
7838 relocation = (htab->glink->output_section->vma
7839 + htab->glink->output_offset
7840 + ent->glink_offset);
7841 else
7842 relocation = (htab->plt->output_section->vma
7843 + htab->plt->output_offset
7844 + ent->plt.offset);
7845 }
7846 break;
7847
7848 /* Relocate against _SDA_BASE_. */
7849 case R_PPC_SDAREL16:
7850 {
7851 const char *name;
7852 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
7853
7854 if (sec == NULL
7855 || sec->output_section == NULL
7856 || !is_static_defined (sda))
7857 {
7858 unresolved_reloc = TRUE;
7859 break;
7860 }
7861 addend -= SYM_VAL (sda);
7862
7863 name = bfd_get_section_name (abfd, sec->output_section);
7864 if (! ((CONST_STRNEQ (name, ".sdata")
7865 && (name[6] == 0 || name[6] == '.'))
7866 || (CONST_STRNEQ (name, ".sbss")
7867 && (name[5] == 0 || name[5] == '.'))))
7868 {
7869 (*_bfd_error_handler)
7870 (_("%B: the target (%s) of a %s relocation is "
7871 "in the wrong output section (%s)"),
7872 input_bfd,
7873 sym_name,
7874 howto->name,
7875 name);
7876 }
7877 }
7878 break;
7879
7880 /* Relocate against _SDA2_BASE_. */
7881 case R_PPC_EMB_SDA2REL:
7882 {
7883 const char *name;
7884 struct elf_link_hash_entry *sda = htab->sdata[1].sym;
7885
7886 if (sec == NULL
7887 || sec->output_section == NULL
7888 || !is_static_defined (sda))
7889 {
7890 unresolved_reloc = TRUE;
7891 break;
7892 }
7893 addend -= SYM_VAL (sda);
7894
7895 name = bfd_get_section_name (abfd, sec->output_section);
7896 if (! (CONST_STRNEQ (name, ".sdata2")
7897 || CONST_STRNEQ (name, ".sbss2")))
7898 {
7899 (*_bfd_error_handler)
7900 (_("%B: the target (%s) of a %s relocation is "
7901 "in the wrong output section (%s)"),
7902 input_bfd,
7903 sym_name,
7904 howto->name,
7905 name);
7906 }
7907 }
7908 break;
7909
7910 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
7911 case R_PPC_EMB_SDA21:
7912 case R_PPC_EMB_RELSDA:
7913 {
7914 const char *name;
7915 int reg;
7916 struct elf_link_hash_entry *sda = NULL;
7917
7918 if (sec == NULL || sec->output_section == NULL)
7919 {
7920 unresolved_reloc = TRUE;
7921 break;
7922 }
7923
7924 name = bfd_get_section_name (abfd, sec->output_section);
7925 if (((CONST_STRNEQ (name, ".sdata")
7926 && (name[6] == 0 || name[6] == '.'))
7927 || (CONST_STRNEQ (name, ".sbss")
7928 && (name[5] == 0 || name[5] == '.'))))
7929 {
7930 reg = 13;
7931 sda = htab->sdata[0].sym;
7932 }
7933 else if (CONST_STRNEQ (name, ".sdata2")
7934 || CONST_STRNEQ (name, ".sbss2"))
7935 {
7936 reg = 2;
7937 sda = htab->sdata[1].sym;
7938 }
7939 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
7940 || strcmp (name, ".PPC.EMB.sbss0") == 0)
7941 {
7942 reg = 0;
7943 }
7944 else
7945 {
7946 (*_bfd_error_handler)
7947 (_("%B: the target (%s) of a %s relocation is "
7948 "in the wrong output section (%s)"),
7949 input_bfd,
7950 sym_name,
7951 howto->name,
7952 name);
7953
7954 bfd_set_error (bfd_error_bad_value);
7955 ret = FALSE;
7956 continue;
7957 }
7958
7959 if (sda != NULL)
7960 {
7961 if (!is_static_defined (sda))
7962 {
7963 unresolved_reloc = TRUE;
7964 break;
7965 }
7966 addend -= SYM_VAL (sda);
7967 }
7968
7969 if (r_type == R_PPC_EMB_SDA21)
7970 {
7971 bfd_vma insn; /* Fill in register field. */
7972
7973 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7974 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
7975 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7976 }
7977 }
7978 break;
7979
7980 /* Relocate against the beginning of the section. */
7981 case R_PPC_SECTOFF:
7982 case R_PPC_SECTOFF_LO:
7983 case R_PPC_SECTOFF_HI:
7984 case R_PPC_SECTOFF_HA:
7985 if (sec == NULL || sec->output_section == NULL)
7986 {
7987 unresolved_reloc = TRUE;
7988 break;
7989 }
7990 addend -= sec->output_section->vma;
7991 break;
7992
7993 /* Negative relocations. */
7994 case R_PPC_EMB_NADDR32:
7995 case R_PPC_EMB_NADDR16:
7996 case R_PPC_EMB_NADDR16_LO:
7997 case R_PPC_EMB_NADDR16_HI:
7998 case R_PPC_EMB_NADDR16_HA:
7999 addend -= 2 * relocation;
8000 break;
8001
8002 case R_PPC_COPY:
8003 case R_PPC_GLOB_DAT:
8004 case R_PPC_JMP_SLOT:
8005 case R_PPC_RELATIVE:
8006 case R_PPC_IRELATIVE:
8007 case R_PPC_PLT32:
8008 case R_PPC_PLTREL32:
8009 case R_PPC_PLT16_LO:
8010 case R_PPC_PLT16_HI:
8011 case R_PPC_PLT16_HA:
8012 case R_PPC_ADDR30:
8013 case R_PPC_EMB_RELSEC16:
8014 case R_PPC_EMB_RELST_LO:
8015 case R_PPC_EMB_RELST_HI:
8016 case R_PPC_EMB_RELST_HA:
8017 case R_PPC_EMB_BIT_FLD:
8018 (*_bfd_error_handler)
8019 (_("%B: relocation %s is not yet supported for symbol %s."),
8020 input_bfd,
8021 howto->name,
8022 sym_name);
8023
8024 bfd_set_error (bfd_error_invalid_operation);
8025 ret = FALSE;
8026 continue;
8027 }
8028
8029 /* Do any further special processing. */
8030 switch (r_type)
8031 {
8032 default:
8033 break;
8034
8035 case R_PPC_ADDR16_HA:
8036 case R_PPC_REL16_HA:
8037 case R_PPC_SECTOFF_HA:
8038 case R_PPC_TPREL16_HA:
8039 case R_PPC_DTPREL16_HA:
8040 case R_PPC_EMB_NADDR16_HA:
8041 case R_PPC_EMB_RELST_HA:
8042 /* It's just possible that this symbol is a weak symbol
8043 that's not actually defined anywhere. In that case,
8044 'sec' would be NULL, and we should leave the symbol
8045 alone (it will be set to zero elsewhere in the link). */
8046 if (sec == NULL)
8047 break;
8048 /* Fall thru */
8049
8050 case R_PPC_PLT16_HA:
8051 case R_PPC_GOT16_HA:
8052 case R_PPC_GOT_TLSGD16_HA:
8053 case R_PPC_GOT_TLSLD16_HA:
8054 case R_PPC_GOT_TPREL16_HA:
8055 case R_PPC_GOT_DTPREL16_HA:
8056 /* Add 0x10000 if sign bit in 0:15 is set.
8057 Bits 0:15 are not used. */
8058 addend += 0x8000;
8059 break;
8060 }
8061
8062 #ifdef DEBUG
8063 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
8064 "offset = %ld, addend = %ld\n",
8065 howto->name,
8066 (int) r_type,
8067 sym_name,
8068 r_symndx,
8069 (long) rel->r_offset,
8070 (long) addend);
8071 #endif
8072
8073 if (unresolved_reloc
8074 && !((input_section->flags & SEC_DEBUGGING) != 0
8075 && h->def_dynamic))
8076 {
8077 (*_bfd_error_handler)
8078 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
8079 input_bfd,
8080 input_section,
8081 (long) rel->r_offset,
8082 howto->name,
8083 sym_name);
8084 ret = FALSE;
8085 }
8086
8087 r = _bfd_final_link_relocate (howto,
8088 input_bfd,
8089 input_section,
8090 contents,
8091 rel->r_offset,
8092 relocation,
8093 addend);
8094
8095 if (r != bfd_reloc_ok)
8096 {
8097 if (r == bfd_reloc_overflow)
8098 {
8099 if (warned)
8100 continue;
8101 if (h != NULL
8102 && h->root.type == bfd_link_hash_undefweak
8103 && howto->pc_relative)
8104 {
8105 /* Assume this is a call protected by other code that
8106 detect the symbol is undefined. If this is the case,
8107 we can safely ignore the overflow. If not, the
8108 program is hosed anyway, and a little warning isn't
8109 going to help. */
8110
8111 continue;
8112 }
8113
8114 if (! (*info->callbacks->reloc_overflow) (info,
8115 (h ? &h->root : NULL),
8116 sym_name,
8117 howto->name,
8118 rel->r_addend,
8119 input_bfd,
8120 input_section,
8121 rel->r_offset))
8122 return FALSE;
8123 }
8124 else
8125 {
8126 (*_bfd_error_handler)
8127 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
8128 input_bfd, input_section,
8129 (long) rel->r_offset, howto->name, sym_name, (int) r);
8130 ret = FALSE;
8131 }
8132 }
8133 }
8134
8135 #ifdef DEBUG
8136 fprintf (stderr, "\n");
8137 #endif
8138
8139 return ret;
8140 }
8141 \f
8142 /* Finish up dynamic symbol handling. We set the contents of various
8143 dynamic sections here. */
8144
8145 static bfd_boolean
8146 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
8147 struct bfd_link_info *info,
8148 struct elf_link_hash_entry *h,
8149 Elf_Internal_Sym *sym)
8150 {
8151 struct ppc_elf_link_hash_table *htab;
8152 struct plt_entry *ent;
8153 bfd_boolean doneone;
8154
8155 #ifdef DEBUG
8156 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
8157 h->root.root.string);
8158 #endif
8159
8160 htab = ppc_elf_hash_table (info);
8161 BFD_ASSERT (htab->elf.dynobj != NULL);
8162
8163 doneone = FALSE;
8164 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8165 if (ent->plt.offset != (bfd_vma) -1)
8166 {
8167 if (!doneone)
8168 {
8169 Elf_Internal_Rela rela;
8170 bfd_byte *loc;
8171 bfd_vma reloc_index;
8172
8173 if (htab->plt_type == PLT_NEW
8174 || !htab->elf.dynamic_sections_created
8175 || h->dynindx == -1)
8176 reloc_index = ent->plt.offset / 4;
8177 else
8178 {
8179 reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
8180 / htab->plt_slot_size);
8181 if (reloc_index > PLT_NUM_SINGLE_ENTRIES
8182 && htab->plt_type == PLT_OLD)
8183 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
8184 }
8185
8186 /* This symbol has an entry in the procedure linkage table.
8187 Set it up. */
8188 if (htab->plt_type == PLT_VXWORKS
8189 && htab->elf.dynamic_sections_created
8190 && h->dynindx != -1)
8191 {
8192 bfd_vma got_offset;
8193 const bfd_vma *plt_entry;
8194
8195 /* The first three entries in .got.plt are reserved. */
8196 got_offset = (reloc_index + 3) * 4;
8197
8198 /* Use the right PLT. */
8199 plt_entry = info->shared ? ppc_elf_vxworks_pic_plt_entry
8200 : ppc_elf_vxworks_plt_entry;
8201
8202 /* Fill in the .plt on VxWorks. */
8203 if (info->shared)
8204 {
8205 bfd_put_32 (output_bfd,
8206 plt_entry[0] | PPC_HA (got_offset),
8207 htab->plt->contents + ent->plt.offset + 0);
8208 bfd_put_32 (output_bfd,
8209 plt_entry[1] | PPC_LO (got_offset),
8210 htab->plt->contents + ent->plt.offset + 4);
8211 }
8212 else
8213 {
8214 bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
8215
8216 bfd_put_32 (output_bfd,
8217 plt_entry[0] | PPC_HA (got_loc),
8218 htab->plt->contents + ent->plt.offset + 0);
8219 bfd_put_32 (output_bfd,
8220 plt_entry[1] | PPC_LO (got_loc),
8221 htab->plt->contents + ent->plt.offset + 4);
8222 }
8223
8224 bfd_put_32 (output_bfd, plt_entry[2],
8225 htab->plt->contents + ent->plt.offset + 8);
8226 bfd_put_32 (output_bfd, plt_entry[3],
8227 htab->plt->contents + ent->plt.offset + 12);
8228
8229 /* This instruction is an immediate load. The value loaded is
8230 the byte offset of the R_PPC_JMP_SLOT relocation from the
8231 start of the .rela.plt section. The value is stored in the
8232 low-order 16 bits of the load instruction. */
8233 /* NOTE: It appears that this is now an index rather than a
8234 prescaled offset. */
8235 bfd_put_32 (output_bfd,
8236 plt_entry[4] | reloc_index,
8237 htab->plt->contents + ent->plt.offset + 16);
8238 /* This instruction is a PC-relative branch whose target is
8239 the start of the PLT section. The address of this branch
8240 instruction is 20 bytes beyond the start of this PLT entry.
8241 The address is encoded in bits 6-29, inclusive. The value
8242 stored is right-shifted by two bits, permitting a 26-bit
8243 offset. */
8244 bfd_put_32 (output_bfd,
8245 (plt_entry[5]
8246 | (-(ent->plt.offset + 20) & 0x03fffffc)),
8247 htab->plt->contents + ent->plt.offset + 20);
8248 bfd_put_32 (output_bfd, plt_entry[6],
8249 htab->plt->contents + ent->plt.offset + 24);
8250 bfd_put_32 (output_bfd, plt_entry[7],
8251 htab->plt->contents + ent->plt.offset + 28);
8252
8253 /* Fill in the GOT entry corresponding to this PLT slot with
8254 the address immediately after the the "bctr" instruction
8255 in this PLT entry. */
8256 bfd_put_32 (output_bfd, (htab->plt->output_section->vma
8257 + htab->plt->output_offset
8258 + ent->plt.offset + 16),
8259 htab->sgotplt->contents + got_offset);
8260
8261 if (!info->shared)
8262 {
8263 /* Fill in a couple of entries in .rela.plt.unloaded. */
8264 loc = htab->srelplt2->contents
8265 + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
8266 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
8267 * sizeof (Elf32_External_Rela));
8268
8269 /* Provide the @ha relocation for the first instruction. */
8270 rela.r_offset = (htab->plt->output_section->vma
8271 + htab->plt->output_offset
8272 + ent->plt.offset + 2);
8273 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
8274 R_PPC_ADDR16_HA);
8275 rela.r_addend = got_offset;
8276 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8277 loc += sizeof (Elf32_External_Rela);
8278
8279 /* Provide the @l relocation for the second instruction. */
8280 rela.r_offset = (htab->plt->output_section->vma
8281 + htab->plt->output_offset
8282 + ent->plt.offset + 6);
8283 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
8284 R_PPC_ADDR16_LO);
8285 rela.r_addend = got_offset;
8286 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8287 loc += sizeof (Elf32_External_Rela);
8288
8289 /* Provide a relocation for the GOT entry corresponding to this
8290 PLT slot. Point it at the middle of the .plt entry. */
8291 rela.r_offset = (htab->sgotplt->output_section->vma
8292 + htab->sgotplt->output_offset
8293 + got_offset);
8294 rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
8295 R_PPC_ADDR32);
8296 rela.r_addend = ent->plt.offset + 16;
8297 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8298 }
8299
8300 /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
8301 In particular, the offset for the relocation is not the
8302 address of the PLT entry for this function, as specified
8303 by the ABI. Instead, the offset is set to the address of
8304 the GOT slot for this function. See EABI 4.4.4.1. */
8305 rela.r_offset = (htab->sgotplt->output_section->vma
8306 + htab->sgotplt->output_offset
8307 + got_offset);
8308
8309 }
8310 else
8311 {
8312 asection *splt = htab->plt;
8313 if (!htab->elf.dynamic_sections_created
8314 || h->dynindx == -1)
8315 splt = htab->iplt;
8316
8317 rela.r_offset = (splt->output_section->vma
8318 + splt->output_offset
8319 + ent->plt.offset);
8320 if (htab->plt_type == PLT_OLD
8321 || !htab->elf.dynamic_sections_created
8322 || h->dynindx == -1)
8323 {
8324 /* We don't need to fill in the .plt. The ppc dynamic
8325 linker will fill it in. */
8326 }
8327 else
8328 {
8329 bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
8330 + htab->glink->output_section->vma
8331 + htab->glink->output_offset);
8332 bfd_put_32 (output_bfd, val,
8333 splt->contents + ent->plt.offset);
8334 }
8335 }
8336
8337 /* Fill in the entry in the .rela.plt section. */
8338 rela.r_addend = 0;
8339 if (!htab->elf.dynamic_sections_created
8340 || h->dynindx == -1)
8341 {
8342 BFD_ASSERT (h->type == STT_GNU_IFUNC
8343 && h->def_regular
8344 && (h->root.type == bfd_link_hash_defined
8345 || h->root.type == bfd_link_hash_defweak));
8346 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
8347 rela.r_addend = SYM_VAL (h);
8348 }
8349 else
8350 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
8351
8352 if (!htab->elf.dynamic_sections_created
8353 || h->dynindx == -1)
8354 loc = (htab->reliplt->contents
8355 + (htab->reliplt->reloc_count++
8356 * sizeof (Elf32_External_Rela)));
8357 else
8358 loc = (htab->relplt->contents
8359 + reloc_index * sizeof (Elf32_External_Rela));
8360 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8361
8362 if (!h->def_regular)
8363 {
8364 /* Mark the symbol as undefined, rather than as
8365 defined in the .plt section. Leave the value if
8366 there were any relocations where pointer equality
8367 matters (this is a clue for the dynamic linker, to
8368 make function pointer comparisons work between an
8369 application and shared library), otherwise set it
8370 to zero. */
8371 sym->st_shndx = SHN_UNDEF;
8372 if (!h->pointer_equality_needed)
8373 sym->st_value = 0;
8374 else if (!h->ref_regular_nonweak)
8375 {
8376 /* This breaks function pointer comparisons, but
8377 that is better than breaking tests for a NULL
8378 function pointer. */
8379 sym->st_value = 0;
8380 }
8381 }
8382 else if (h->type == STT_GNU_IFUNC
8383 && !info->shared)
8384 {
8385 /* Set the value of ifunc symbols in a non-pie
8386 executable to the glink entry. This is to avoid
8387 text relocations. We can't do this for ifunc in
8388 allocate_dynrelocs, as we do for normal dynamic
8389 function symbols with plt entries, because we need
8390 to keep the original value around for the ifunc
8391 relocation. */
8392 sym->st_shndx = (_bfd_elf_section_from_bfd_section
8393 (output_bfd, htab->glink->output_section));
8394 sym->st_value = (ent->glink_offset +
8395 htab->glink->output_offset
8396 + htab->glink->output_section->vma);
8397 }
8398 doneone = TRUE;
8399 }
8400
8401 if (htab->plt_type == PLT_NEW
8402 || !htab->elf.dynamic_sections_created
8403 || h->dynindx == -1)
8404 {
8405 unsigned char *p;
8406 asection *splt = htab->plt;
8407 if (!htab->elf.dynamic_sections_created
8408 || h->dynindx == -1)
8409 splt = htab->iplt;
8410
8411 p = (unsigned char *) htab->glink->contents + ent->glink_offset;
8412
8413 if (h == htab->tls_get_addr && !htab->no_tls_get_addr_opt)
8414 {
8415 bfd_put_32 (output_bfd, LWZ_11_3, p);
8416 p += 4;
8417 bfd_put_32 (output_bfd, LWZ_12_3 + 4, p);
8418 p += 4;
8419 bfd_put_32 (output_bfd, MR_0_3, p);
8420 p += 4;
8421 bfd_put_32 (output_bfd, CMPWI_11_0, p);
8422 p += 4;
8423 bfd_put_32 (output_bfd, ADD_3_12_2, p);
8424 p += 4;
8425 bfd_put_32 (output_bfd, BEQLR, p);
8426 p += 4;
8427 bfd_put_32 (output_bfd, MR_3_0, p);
8428 p += 4;
8429 bfd_put_32 (output_bfd, NOP, p);
8430 p += 4;
8431 }
8432
8433 write_glink_stub (ent, splt, p, info);
8434
8435 if (!info->shared)
8436 /* We only need one non-PIC glink stub. */
8437 break;
8438 }
8439 else
8440 break;
8441 }
8442
8443 if (h->needs_copy)
8444 {
8445 asection *s;
8446 Elf_Internal_Rela rela;
8447 bfd_byte *loc;
8448
8449 /* This symbols needs a copy reloc. Set it up. */
8450
8451 #ifdef DEBUG
8452 fprintf (stderr, ", copy");
8453 #endif
8454
8455 BFD_ASSERT (h->dynindx != -1);
8456
8457 if (ppc_elf_hash_entry (h)->has_sda_refs)
8458 s = htab->relsbss;
8459 else
8460 s = htab->relbss;
8461 BFD_ASSERT (s != NULL);
8462
8463 rela.r_offset = SYM_VAL (h);
8464 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
8465 rela.r_addend = 0;
8466 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
8467 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8468 }
8469
8470 #ifdef DEBUG
8471 fprintf (stderr, "\n");
8472 #endif
8473
8474 /* Mark some specially defined symbols as absolute. */
8475 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
8476 || (!htab->is_vxworks
8477 && (h == htab->elf.hgot
8478 || strcmp (h->root.root.string,
8479 "_PROCEDURE_LINKAGE_TABLE_") == 0)))
8480 sym->st_shndx = SHN_ABS;
8481
8482 return TRUE;
8483 }
8484 \f
8485 static enum elf_reloc_type_class
8486 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
8487 {
8488 switch (ELF32_R_TYPE (rela->r_info))
8489 {
8490 case R_PPC_RELATIVE:
8491 return reloc_class_relative;
8492 case R_PPC_REL24:
8493 case R_PPC_ADDR24:
8494 case R_PPC_JMP_SLOT:
8495 return reloc_class_plt;
8496 case R_PPC_COPY:
8497 return reloc_class_copy;
8498 default:
8499 return reloc_class_normal;
8500 }
8501 }
8502 \f
8503 /* Finish up the dynamic sections. */
8504
8505 static bfd_boolean
8506 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
8507 struct bfd_link_info *info)
8508 {
8509 asection *sdyn;
8510 asection *splt;
8511 struct ppc_elf_link_hash_table *htab;
8512 bfd_vma got;
8513 bfd *dynobj;
8514 bfd_boolean ret = TRUE;
8515
8516 #ifdef DEBUG
8517 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
8518 #endif
8519
8520 htab = ppc_elf_hash_table (info);
8521 dynobj = elf_hash_table (info)->dynobj;
8522 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8523 if (htab->is_vxworks)
8524 splt = bfd_get_section_by_name (dynobj, ".plt");
8525 else
8526 splt = NULL;
8527
8528 got = 0;
8529 if (htab->elf.hgot != NULL)
8530 got = SYM_VAL (htab->elf.hgot);
8531
8532 if (htab->elf.dynamic_sections_created)
8533 {
8534 Elf32_External_Dyn *dyncon, *dynconend;
8535
8536 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
8537
8538 dyncon = (Elf32_External_Dyn *) sdyn->contents;
8539 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
8540 for (; dyncon < dynconend; dyncon++)
8541 {
8542 Elf_Internal_Dyn dyn;
8543 asection *s;
8544
8545 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
8546
8547 switch (dyn.d_tag)
8548 {
8549 case DT_PLTGOT:
8550 if (htab->is_vxworks)
8551 s = htab->sgotplt;
8552 else
8553 s = htab->plt;
8554 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8555 break;
8556
8557 case DT_PLTRELSZ:
8558 dyn.d_un.d_val = htab->relplt->size;
8559 break;
8560
8561 case DT_JMPREL:
8562 s = htab->relplt;
8563 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8564 break;
8565
8566 case DT_PPC_GOT:
8567 dyn.d_un.d_ptr = got;
8568 break;
8569
8570 case DT_RELASZ:
8571 if (htab->is_vxworks)
8572 {
8573 if (htab->relplt)
8574 dyn.d_un.d_ptr -= htab->relplt->size;
8575 break;
8576 }
8577 continue;
8578
8579 default:
8580 if (htab->is_vxworks
8581 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
8582 break;
8583 continue;
8584 }
8585
8586 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8587 }
8588 }
8589
8590 if (htab->got != NULL)
8591 {
8592 if (htab->elf.hgot->root.u.def.section == htab->got
8593 || htab->elf.hgot->root.u.def.section == htab->sgotplt)
8594 {
8595 unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
8596
8597 p += htab->elf.hgot->root.u.def.value;
8598 if (htab->plt_type == PLT_OLD)
8599 {
8600 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4
8601 so that a function can easily find the address of
8602 _GLOBAL_OFFSET_TABLE_. */
8603 BFD_ASSERT (htab->elf.hgot->root.u.def.value - 4
8604 < htab->elf.hgot->root.u.def.section->size);
8605 bfd_put_32 (output_bfd, 0x4e800021, p - 4);
8606 }
8607
8608 if (sdyn != NULL)
8609 {
8610 bfd_vma val = sdyn->output_section->vma + sdyn->output_offset;
8611 BFD_ASSERT (htab->elf.hgot->root.u.def.value
8612 < htab->elf.hgot->root.u.def.section->size);
8613 bfd_put_32 (output_bfd, val, p);
8614 }
8615 }
8616 else
8617 {
8618 (*_bfd_error_handler) (_("%s not defined in linker created %s"),
8619 htab->elf.hgot->root.root.string,
8620 (htab->sgotplt != NULL
8621 ? htab->sgotplt->name : htab->got->name));
8622 bfd_set_error (bfd_error_bad_value);
8623 ret = FALSE;
8624 }
8625
8626 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
8627 }
8628
8629 /* Fill in the first entry in the VxWorks procedure linkage table. */
8630 if (splt && splt->size > 0)
8631 {
8632 /* Use the right PLT. */
8633 static const bfd_vma *plt_entry = NULL;
8634 plt_entry = info->shared ?
8635 ppc_elf_vxworks_pic_plt0_entry : ppc_elf_vxworks_plt0_entry;
8636
8637 if (!info->shared)
8638 {
8639 bfd_vma got_value = SYM_VAL (htab->elf.hgot);
8640
8641 bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
8642 splt->contents + 0);
8643 bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
8644 splt->contents + 4);
8645 }
8646 else
8647 {
8648 bfd_put_32 (output_bfd, plt_entry[0], splt->contents + 0);
8649 bfd_put_32 (output_bfd, plt_entry[1], splt->contents + 4);
8650 }
8651 bfd_put_32 (output_bfd, plt_entry[2], splt->contents + 8);
8652 bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
8653 bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
8654 bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
8655 bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
8656 bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
8657
8658 if (! info->shared)
8659 {
8660 Elf_Internal_Rela rela;
8661 bfd_byte *loc;
8662
8663 loc = htab->srelplt2->contents;
8664
8665 /* Output the @ha relocation for the first instruction. */
8666 rela.r_offset = (htab->plt->output_section->vma
8667 + htab->plt->output_offset
8668 + 2);
8669 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
8670 rela.r_addend = 0;
8671 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8672 loc += sizeof (Elf32_External_Rela);
8673
8674 /* Output the @l relocation for the second instruction. */
8675 rela.r_offset = (htab->plt->output_section->vma
8676 + htab->plt->output_offset
8677 + 6);
8678 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
8679 rela.r_addend = 0;
8680 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8681 loc += sizeof (Elf32_External_Rela);
8682
8683 /* Fix up the remaining relocations. They may have the wrong
8684 symbol index for _G_O_T_ or _P_L_T_ depending on the order
8685 in which symbols were output. */
8686 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
8687 {
8688 Elf_Internal_Rela rel;
8689
8690 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8691 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
8692 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8693 loc += sizeof (Elf32_External_Rela);
8694
8695 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8696 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
8697 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8698 loc += sizeof (Elf32_External_Rela);
8699
8700 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8701 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
8702 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8703 loc += sizeof (Elf32_External_Rela);
8704 }
8705 }
8706 }
8707
8708 if (htab->glink != NULL
8709 && htab->glink->contents != NULL
8710 && htab->elf.dynamic_sections_created)
8711 {
8712 unsigned char *p;
8713 unsigned char *endp;
8714 bfd_vma res0;
8715 unsigned int i;
8716
8717 /*
8718 * PIC glink code is the following:
8719 *
8720 * # ith PLT code stub.
8721 * addis 11,30,(plt+(i-1)*4-got)@ha
8722 * lwz 11,(plt+(i-1)*4-got)@l(11)
8723 * mtctr 11
8724 * bctr
8725 *
8726 * # A table of branches, one for each plt entry.
8727 * # The idea is that the plt call stub loads ctr and r11 with these
8728 * # addresses, so (r11 - res_0) gives the plt index * 4.
8729 * res_0: b PLTresolve
8730 * res_1: b PLTresolve
8731 * .
8732 * # Some number of entries towards the end can be nops
8733 * res_n_m3: nop
8734 * res_n_m2: nop
8735 * res_n_m1:
8736 *
8737 * PLTresolve:
8738 * addis 11,11,(1f-res_0)@ha
8739 * mflr 0
8740 * bcl 20,31,1f
8741 * 1: addi 11,11,(1b-res_0)@l
8742 * mflr 12
8743 * mtlr 0
8744 * sub 11,11,12 # r11 = index * 4
8745 * addis 12,12,(got+4-1b)@ha
8746 * lwz 0,(got+4-1b)@l(12) # got[1] address of dl_runtime_resolve
8747 * lwz 12,(got+8-1b)@l(12) # got[2] contains the map address
8748 * mtctr 0
8749 * add 0,11,11
8750 * add 11,0,11 # r11 = index * 12 = reloc offset.
8751 * bctr
8752 */
8753 static const unsigned int pic_plt_resolve[] =
8754 {
8755 ADDIS_11_11,
8756 MFLR_0,
8757 BCL_20_31,
8758 ADDI_11_11,
8759 MFLR_12,
8760 MTLR_0,
8761 SUB_11_11_12,
8762 ADDIS_12_12,
8763 LWZ_0_12,
8764 LWZ_12_12,
8765 MTCTR_0,
8766 ADD_0_11_11,
8767 ADD_11_0_11,
8768 BCTR,
8769 NOP,
8770 NOP
8771 };
8772
8773 /*
8774 * Non-PIC glink code is a little simpler.
8775 *
8776 * # ith PLT code stub.
8777 * lis 11,(plt+(i-1)*4)@ha
8778 * lwz 11,(plt+(i-1)*4)@l(11)
8779 * mtctr 11
8780 * bctr
8781 *
8782 * The branch table is the same, then comes
8783 *
8784 * PLTresolve:
8785 * lis 12,(got+4)@ha
8786 * addis 11,11,(-res_0)@ha
8787 * lwz 0,(got+4)@l(12) # got[1] address of dl_runtime_resolve
8788 * addi 11,11,(-res_0)@l # r11 = index * 4
8789 * mtctr 0
8790 * add 0,11,11
8791 * lwz 12,(got+8)@l(12) # got[2] contains the map address
8792 * add 11,0,11 # r11 = index * 12 = reloc offset.
8793 * bctr
8794 */
8795 static const unsigned int plt_resolve[] =
8796 {
8797 LIS_12,
8798 ADDIS_11_11,
8799 LWZ_0_12,
8800 ADDI_11_11,
8801 MTCTR_0,
8802 ADD_0_11_11,
8803 LWZ_12_12,
8804 ADD_11_0_11,
8805 BCTR,
8806 NOP,
8807 NOP,
8808 NOP,
8809 NOP,
8810 NOP,
8811 NOP,
8812 NOP
8813 };
8814
8815 if (ARRAY_SIZE (pic_plt_resolve) != GLINK_PLTRESOLVE / 4)
8816 abort ();
8817 if (ARRAY_SIZE (plt_resolve) != GLINK_PLTRESOLVE / 4)
8818 abort ();
8819
8820 /* Build the branch table, one for each plt entry (less one),
8821 and perhaps some padding. */
8822 p = htab->glink->contents;
8823 p += htab->glink_pltresolve;
8824 endp = htab->glink->contents;
8825 endp += htab->glink->size - GLINK_PLTRESOLVE;
8826 while (p < endp - 8 * 4)
8827 {
8828 bfd_put_32 (output_bfd, B + endp - p, p);
8829 p += 4;
8830 }
8831 while (p < endp)
8832 {
8833 bfd_put_32 (output_bfd, NOP, p);
8834 p += 4;
8835 }
8836
8837 res0 = (htab->glink_pltresolve
8838 + htab->glink->output_section->vma
8839 + htab->glink->output_offset);
8840
8841 /* Last comes the PLTresolve stub. */
8842 if (info->shared)
8843 {
8844 bfd_vma bcl;
8845
8846 for (i = 0; i < ARRAY_SIZE (pic_plt_resolve); i++)
8847 {
8848 bfd_put_32 (output_bfd, pic_plt_resolve[i], p);
8849 p += 4;
8850 }
8851 p -= 4 * ARRAY_SIZE (pic_plt_resolve);
8852
8853 bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
8854 + htab->glink->output_section->vma
8855 + htab->glink->output_offset);
8856
8857 bfd_put_32 (output_bfd,
8858 ADDIS_11_11 + PPC_HA (bcl - res0), p + 0*4);
8859 bfd_put_32 (output_bfd,
8860 ADDI_11_11 + PPC_LO (bcl - res0), p + 3*4);
8861 bfd_put_32 (output_bfd,
8862 ADDIS_12_12 + PPC_HA (got + 4 - bcl), p + 7*4);
8863 if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
8864 {
8865 bfd_put_32 (output_bfd,
8866 LWZ_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8867 bfd_put_32 (output_bfd,
8868 LWZ_12_12 + PPC_LO (got + 8 - bcl), p + 9*4);
8869 }
8870 else
8871 {
8872 bfd_put_32 (output_bfd,
8873 LWZU_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8874 bfd_put_32 (output_bfd,
8875 LWZ_12_12 + 4, p + 9*4);
8876 }
8877 }
8878 else
8879 {
8880 for (i = 0; i < ARRAY_SIZE (plt_resolve); i++)
8881 {
8882 bfd_put_32 (output_bfd, plt_resolve[i], p);
8883 p += 4;
8884 }
8885 p -= 4 * ARRAY_SIZE (plt_resolve);
8886
8887 bfd_put_32 (output_bfd,
8888 LIS_12 + PPC_HA (got + 4), p + 0*4);
8889 bfd_put_32 (output_bfd,
8890 ADDIS_11_11 + PPC_HA (-res0), p + 1*4);
8891 bfd_put_32 (output_bfd,
8892 ADDI_11_11 + PPC_LO (-res0), p + 3*4);
8893 if (PPC_HA (got + 4) == PPC_HA (got + 8))
8894 {
8895 bfd_put_32 (output_bfd,
8896 LWZ_0_12 + PPC_LO (got + 4), p + 2*4);
8897 bfd_put_32 (output_bfd,
8898 LWZ_12_12 + PPC_LO (got + 8), p + 6*4);
8899 }
8900 else
8901 {
8902 bfd_put_32 (output_bfd,
8903 LWZU_0_12 + PPC_LO (got + 4), p + 2*4);
8904 bfd_put_32 (output_bfd,
8905 LWZ_12_12 + 4, p + 6*4);
8906 }
8907 }
8908 }
8909
8910 return ret;
8911 }
8912 \f
8913 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
8914 #define TARGET_LITTLE_NAME "elf32-powerpcle"
8915 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
8916 #define TARGET_BIG_NAME "elf32-powerpc"
8917 #define ELF_ARCH bfd_arch_powerpc
8918 #define ELF_MACHINE_CODE EM_PPC
8919 #ifdef __QNXTARGET__
8920 #define ELF_MAXPAGESIZE 0x1000
8921 #else
8922 #define ELF_MAXPAGESIZE 0x10000
8923 #endif
8924 #define ELF_MINPAGESIZE 0x1000
8925 #define ELF_COMMONPAGESIZE 0x1000
8926 #define elf_info_to_howto ppc_elf_info_to_howto
8927
8928 #ifdef EM_CYGNUS_POWERPC
8929 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
8930 #endif
8931
8932 #ifdef EM_PPC_OLD
8933 #define ELF_MACHINE_ALT2 EM_PPC_OLD
8934 #endif
8935
8936 #define elf_backend_plt_not_loaded 1
8937 #define elf_backend_can_gc_sections 1
8938 #define elf_backend_can_refcount 1
8939 #define elf_backend_rela_normal 1
8940
8941 #define bfd_elf32_mkobject ppc_elf_mkobject
8942 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
8943 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
8944 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
8945 #define bfd_elf32_bfd_reloc_name_lookup ppc_elf_reloc_name_lookup
8946 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
8947 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
8948 #define bfd_elf32_get_synthetic_symtab ppc_elf_get_synthetic_symtab
8949
8950 #define elf_backend_object_p ppc_elf_object_p
8951 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
8952 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
8953 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
8954 #define elf_backend_relocate_section ppc_elf_relocate_section
8955 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
8956 #define elf_backend_check_relocs ppc_elf_check_relocs
8957 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
8958 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
8959 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
8960 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
8961 #define elf_backend_hash_symbol ppc_elf_hash_symbol
8962 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
8963 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
8964 #define elf_backend_fake_sections ppc_elf_fake_sections
8965 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
8966 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
8967 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
8968 #define elf_backend_write_core_note ppc_elf_write_core_note
8969 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
8970 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
8971 #define elf_backend_final_write_processing ppc_elf_final_write_processing
8972 #define elf_backend_write_section ppc_elf_write_section
8973 #define elf_backend_get_sec_type_attr ppc_elf_get_sec_type_attr
8974 #define elf_backend_plt_sym_val ppc_elf_plt_sym_val
8975 #define elf_backend_action_discarded ppc_elf_action_discarded
8976 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
8977 #define elf_backend_post_process_headers _bfd_elf_set_osabi
8978
8979 #include "elf32-target.h"
8980
8981 /* VxWorks Target */
8982
8983 #undef TARGET_LITTLE_SYM
8984 #undef TARGET_LITTLE_NAME
8985
8986 #undef TARGET_BIG_SYM
8987 #define TARGET_BIG_SYM bfd_elf32_powerpc_vxworks_vec
8988 #undef TARGET_BIG_NAME
8989 #define TARGET_BIG_NAME "elf32-powerpc-vxworks"
8990
8991 /* VxWorks uses the elf default section flags for .plt. */
8992 static const struct bfd_elf_special_section *
8993 ppc_elf_vxworks_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
8994 {
8995 if (sec->name == NULL)
8996 return NULL;
8997
8998 if (strcmp (sec->name, ".plt") == 0)
8999 return _bfd_elf_get_sec_type_attr (abfd, sec);
9000
9001 return ppc_elf_get_sec_type_attr (abfd, sec);
9002 }
9003
9004 /* Like ppc_elf_link_hash_table_create, but overrides
9005 appropriately for VxWorks. */
9006 static struct bfd_link_hash_table *
9007 ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
9008 {
9009 struct bfd_link_hash_table *ret;
9010
9011 ret = ppc_elf_link_hash_table_create (abfd);
9012 if (ret)
9013 {
9014 struct ppc_elf_link_hash_table *htab
9015 = (struct ppc_elf_link_hash_table *)ret;
9016 htab->is_vxworks = 1;
9017 htab->plt_type = PLT_VXWORKS;
9018 htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
9019 htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
9020 htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
9021 }
9022 return ret;
9023 }
9024
9025 /* Tweak magic VxWorks symbols as they are loaded. */
9026 static bfd_boolean
9027 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
9028 struct bfd_link_info *info,
9029 Elf_Internal_Sym *sym,
9030 const char **namep ATTRIBUTE_UNUSED,
9031 flagword *flagsp ATTRIBUTE_UNUSED,
9032 asection **secp,
9033 bfd_vma *valp)
9034 {
9035 if (!elf_vxworks_add_symbol_hook(abfd, info, sym,namep, flagsp, secp,
9036 valp))
9037 return FALSE;
9038
9039 return ppc_elf_add_symbol_hook(abfd, info, sym,namep, flagsp, secp, valp);
9040 }
9041
9042 static void
9043 ppc_elf_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
9044 {
9045 ppc_elf_final_write_processing(abfd, linker);
9046 elf_vxworks_final_write_processing(abfd, linker);
9047 }
9048
9049 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
9050 define it. */
9051 #undef elf_backend_want_plt_sym
9052 #define elf_backend_want_plt_sym 1
9053 #undef elf_backend_want_got_plt
9054 #define elf_backend_want_got_plt 1
9055 #undef elf_backend_got_symbol_offset
9056 #define elf_backend_got_symbol_offset 0
9057 #undef elf_backend_plt_not_loaded
9058 #define elf_backend_plt_not_loaded 0
9059 #undef elf_backend_plt_readonly
9060 #define elf_backend_plt_readonly 1
9061 #undef elf_backend_got_header_size
9062 #define elf_backend_got_header_size 12
9063
9064 #undef bfd_elf32_get_synthetic_symtab
9065
9066 #undef bfd_elf32_bfd_link_hash_table_create
9067 #define bfd_elf32_bfd_link_hash_table_create \
9068 ppc_elf_vxworks_link_hash_table_create
9069 #undef elf_backend_add_symbol_hook
9070 #define elf_backend_add_symbol_hook \
9071 ppc_elf_vxworks_add_symbol_hook
9072 #undef elf_backend_link_output_symbol_hook
9073 #define elf_backend_link_output_symbol_hook \
9074 elf_vxworks_link_output_symbol_hook
9075 #undef elf_backend_final_write_processing
9076 #define elf_backend_final_write_processing \
9077 ppc_elf_vxworks_final_write_processing
9078 #undef elf_backend_get_sec_type_attr
9079 #define elf_backend_get_sec_type_attr \
9080 ppc_elf_vxworks_get_sec_type_attr
9081 #undef elf_backend_emit_relocs
9082 #define elf_backend_emit_relocs \
9083 elf_vxworks_emit_relocs
9084
9085 #undef elf32_bed
9086 #define elf32_bed ppc_elf_vxworks_bed
9087 #undef elf_backend_post_process_headers
9088
9089 #include "elf32-target.h"
This page took 0.214716 seconds and 5 git commands to generate.