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