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