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