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