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