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