include/elf/
[binutils-gdb.git] / bfd / elf32-ppc.c
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* 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 "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/ppc.h"
34 #include "elf32-ppc.h"
35
36 /* RELA relocations are used here. */
37
38 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc_elf_unhandled_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42
43 /* Branch prediction bit for branch taken relocs. */
44 #define BRANCH_PREDICT_BIT 0x200000
45 /* Mask to set RA in memory instructions. */
46 #define RA_REGISTER_MASK 0x001f0000
47 /* Value to shift register by to insert RA. */
48 #define RA_REGISTER_SHIFT 16
49
50 /* The name of the dynamic interpreter. This is put in the .interp
51 section. */
52 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
53
54 /* For old-style PLT. */
55 /* The size in bytes of an entry in the procedure linkage table. */
56 #define PLT_ENTRY_SIZE 12
57 /* The initial size of the plt reserved for the dynamic linker. */
58 #define PLT_INITIAL_ENTRY_SIZE 72
59 /* The size of the gap between entries in the PLT. */
60 #define PLT_SLOT_SIZE 8
61 /* The number of single-slot PLT entries (the rest use two slots). */
62 #define PLT_NUM_SINGLE_ENTRIES 8192
63
64 /* For new-style .glink and .plt. */
65 #define GLINK_PLTRESOLVE 12*4
66 #define GLINK_ENTRY_SIZE 4*4
67
68 /* Some instructions. */
69 #define NOP 0x60000000
70 #define B 0x48000000
71 #define ADDIS_11_11 0x3d6b0000
72 #define ADDI_11_11 0x396b0000
73 #define SUB_11_11_30 0x7d7e5850
74 #define ADD_0_11_11 0x7c0b5a14
75 #define ADD_11_0_11 0x7d605a14
76 #define LWZ_0_4_30 0x801e0004
77 #define MTCTR_0 0x7c0903a6
78 #define LWZ_12_8_30 0x819e0008
79 #define BCTR 0x4e800420
80 #define ADDIS_11_30 0x3d7e0000
81 #define LWZ_11_X_11 0x816b0000
82 #define LWZ_11_X_30 0x817e0000
83 #define MTCTR_11 0x7d6903a6
84
85 /* Offset of tp and dtp pointers from start of TLS block. */
86 #define TP_OFFSET 0x7000
87 #define DTP_OFFSET 0x8000
88 \f
89 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
90
91 static reloc_howto_type ppc_elf_howto_raw[] = {
92 /* This reloc does nothing. */
93 HOWTO (R_PPC_NONE, /* type */
94 0, /* rightshift */
95 2, /* size (0 = byte, 1 = short, 2 = long) */
96 32, /* bitsize */
97 FALSE, /* pc_relative */
98 0, /* bitpos */
99 complain_overflow_bitfield, /* complain_on_overflow */
100 bfd_elf_generic_reloc, /* special_function */
101 "R_PPC_NONE", /* name */
102 FALSE, /* partial_inplace */
103 0, /* src_mask */
104 0, /* dst_mask */
105 FALSE), /* pcrel_offset */
106
107 /* A standard 32 bit relocation. */
108 HOWTO (R_PPC_ADDR32, /* type */
109 0, /* rightshift */
110 2, /* size (0 = byte, 1 = short, 2 = long) */
111 32, /* bitsize */
112 FALSE, /* pc_relative */
113 0, /* bitpos */
114 complain_overflow_bitfield, /* complain_on_overflow */
115 bfd_elf_generic_reloc, /* special_function */
116 "R_PPC_ADDR32", /* name */
117 FALSE, /* partial_inplace */
118 0, /* src_mask */
119 0xffffffff, /* dst_mask */
120 FALSE), /* pcrel_offset */
121
122 /* An absolute 26 bit branch; the lower two bits must be zero.
123 FIXME: we don't check that, we just clear them. */
124 HOWTO (R_PPC_ADDR24, /* type */
125 0, /* rightshift */
126 2, /* size (0 = byte, 1 = short, 2 = long) */
127 26, /* bitsize */
128 FALSE, /* pc_relative */
129 0, /* bitpos */
130 complain_overflow_bitfield, /* complain_on_overflow */
131 bfd_elf_generic_reloc, /* special_function */
132 "R_PPC_ADDR24", /* name */
133 FALSE, /* partial_inplace */
134 0, /* src_mask */
135 0x3fffffc, /* dst_mask */
136 FALSE), /* pcrel_offset */
137
138 /* A standard 16 bit relocation. */
139 HOWTO (R_PPC_ADDR16, /* type */
140 0, /* rightshift */
141 1, /* size (0 = byte, 1 = short, 2 = long) */
142 16, /* bitsize */
143 FALSE, /* pc_relative */
144 0, /* bitpos */
145 complain_overflow_bitfield, /* complain_on_overflow */
146 bfd_elf_generic_reloc, /* special_function */
147 "R_PPC_ADDR16", /* name */
148 FALSE, /* partial_inplace */
149 0, /* src_mask */
150 0xffff, /* dst_mask */
151 FALSE), /* pcrel_offset */
152
153 /* A 16 bit relocation without overflow. */
154 HOWTO (R_PPC_ADDR16_LO, /* type */
155 0, /* rightshift */
156 1, /* size (0 = byte, 1 = short, 2 = long) */
157 16, /* bitsize */
158 FALSE, /* pc_relative */
159 0, /* bitpos */
160 complain_overflow_dont,/* complain_on_overflow */
161 bfd_elf_generic_reloc, /* special_function */
162 "R_PPC_ADDR16_LO", /* name */
163 FALSE, /* partial_inplace */
164 0, /* src_mask */
165 0xffff, /* dst_mask */
166 FALSE), /* pcrel_offset */
167
168 /* The high order 16 bits of an address. */
169 HOWTO (R_PPC_ADDR16_HI, /* type */
170 16, /* rightshift */
171 1, /* size (0 = byte, 1 = short, 2 = long) */
172 16, /* bitsize */
173 FALSE, /* pc_relative */
174 0, /* bitpos */
175 complain_overflow_dont, /* complain_on_overflow */
176 bfd_elf_generic_reloc, /* special_function */
177 "R_PPC_ADDR16_HI", /* name */
178 FALSE, /* partial_inplace */
179 0, /* src_mask */
180 0xffff, /* dst_mask */
181 FALSE), /* pcrel_offset */
182
183 /* The high order 16 bits of an address, plus 1 if the contents of
184 the low 16 bits, treated as a signed number, is negative. */
185 HOWTO (R_PPC_ADDR16_HA, /* type */
186 16, /* rightshift */
187 1, /* size (0 = byte, 1 = short, 2 = long) */
188 16, /* bitsize */
189 FALSE, /* pc_relative */
190 0, /* bitpos */
191 complain_overflow_dont, /* complain_on_overflow */
192 ppc_elf_addr16_ha_reloc, /* special_function */
193 "R_PPC_ADDR16_HA", /* name */
194 FALSE, /* partial_inplace */
195 0, /* src_mask */
196 0xffff, /* dst_mask */
197 FALSE), /* pcrel_offset */
198
199 /* An absolute 16 bit branch; the lower two bits must be zero.
200 FIXME: we don't check that, we just clear them. */
201 HOWTO (R_PPC_ADDR14, /* type */
202 0, /* rightshift */
203 2, /* size (0 = byte, 1 = short, 2 = long) */
204 16, /* bitsize */
205 FALSE, /* pc_relative */
206 0, /* bitpos */
207 complain_overflow_bitfield, /* complain_on_overflow */
208 bfd_elf_generic_reloc, /* special_function */
209 "R_PPC_ADDR14", /* name */
210 FALSE, /* partial_inplace */
211 0, /* src_mask */
212 0xfffc, /* dst_mask */
213 FALSE), /* pcrel_offset */
214
215 /* An absolute 16 bit branch, for which bit 10 should be set to
216 indicate that the branch is expected to be taken. The lower two
217 bits must be zero. */
218 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
219 0, /* rightshift */
220 2, /* size (0 = byte, 1 = short, 2 = long) */
221 16, /* bitsize */
222 FALSE, /* pc_relative */
223 0, /* bitpos */
224 complain_overflow_bitfield, /* complain_on_overflow */
225 bfd_elf_generic_reloc, /* special_function */
226 "R_PPC_ADDR14_BRTAKEN",/* name */
227 FALSE, /* partial_inplace */
228 0, /* src_mask */
229 0xfffc, /* dst_mask */
230 FALSE), /* pcrel_offset */
231
232 /* An absolute 16 bit branch, for which bit 10 should be set to
233 indicate that the branch is not expected to be taken. The lower
234 two bits must be zero. */
235 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
236 0, /* rightshift */
237 2, /* size (0 = byte, 1 = short, 2 = long) */
238 16, /* bitsize */
239 FALSE, /* pc_relative */
240 0, /* bitpos */
241 complain_overflow_bitfield, /* complain_on_overflow */
242 bfd_elf_generic_reloc, /* special_function */
243 "R_PPC_ADDR14_BRNTAKEN",/* name */
244 FALSE, /* partial_inplace */
245 0, /* src_mask */
246 0xfffc, /* dst_mask */
247 FALSE), /* pcrel_offset */
248
249 /* A relative 26 bit branch; the lower two bits must be zero. */
250 HOWTO (R_PPC_REL24, /* type */
251 0, /* rightshift */
252 2, /* size (0 = byte, 1 = short, 2 = long) */
253 26, /* bitsize */
254 TRUE, /* pc_relative */
255 0, /* bitpos */
256 complain_overflow_signed, /* complain_on_overflow */
257 bfd_elf_generic_reloc, /* special_function */
258 "R_PPC_REL24", /* name */
259 FALSE, /* partial_inplace */
260 0, /* src_mask */
261 0x3fffffc, /* dst_mask */
262 TRUE), /* pcrel_offset */
263
264 /* A relative 16 bit branch; the lower two bits must be zero. */
265 HOWTO (R_PPC_REL14, /* type */
266 0, /* rightshift */
267 2, /* size (0 = byte, 1 = short, 2 = long) */
268 16, /* bitsize */
269 TRUE, /* pc_relative */
270 0, /* bitpos */
271 complain_overflow_signed, /* complain_on_overflow */
272 bfd_elf_generic_reloc, /* special_function */
273 "R_PPC_REL14", /* name */
274 FALSE, /* partial_inplace */
275 0, /* src_mask */
276 0xfffc, /* dst_mask */
277 TRUE), /* pcrel_offset */
278
279 /* A relative 16 bit branch. Bit 10 should be set to indicate that
280 the branch is expected to be taken. The lower two bits must be
281 zero. */
282 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
283 0, /* rightshift */
284 2, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
286 TRUE, /* pc_relative */
287 0, /* bitpos */
288 complain_overflow_signed, /* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC_REL14_BRTAKEN", /* name */
291 FALSE, /* partial_inplace */
292 0, /* src_mask */
293 0xfffc, /* dst_mask */
294 TRUE), /* pcrel_offset */
295
296 /* A relative 16 bit branch. Bit 10 should be set to indicate that
297 the branch is not expected to be taken. The lower two bits must
298 be zero. */
299 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
300 0, /* rightshift */
301 2, /* size (0 = byte, 1 = short, 2 = long) */
302 16, /* bitsize */
303 TRUE, /* pc_relative */
304 0, /* bitpos */
305 complain_overflow_signed, /* complain_on_overflow */
306 bfd_elf_generic_reloc, /* special_function */
307 "R_PPC_REL14_BRNTAKEN",/* name */
308 FALSE, /* partial_inplace */
309 0, /* src_mask */
310 0xfffc, /* dst_mask */
311 TRUE), /* pcrel_offset */
312
313 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
314 symbol. */
315 HOWTO (R_PPC_GOT16, /* type */
316 0, /* rightshift */
317 1, /* size (0 = byte, 1 = short, 2 = long) */
318 16, /* bitsize */
319 FALSE, /* pc_relative */
320 0, /* bitpos */
321 complain_overflow_signed, /* complain_on_overflow */
322 bfd_elf_generic_reloc, /* special_function */
323 "R_PPC_GOT16", /* name */
324 FALSE, /* partial_inplace */
325 0, /* src_mask */
326 0xffff, /* dst_mask */
327 FALSE), /* pcrel_offset */
328
329 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
330 the symbol. */
331 HOWTO (R_PPC_GOT16_LO, /* type */
332 0, /* rightshift */
333 1, /* size (0 = byte, 1 = short, 2 = long) */
334 16, /* bitsize */
335 FALSE, /* pc_relative */
336 0, /* bitpos */
337 complain_overflow_dont, /* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_PPC_GOT16_LO", /* name */
340 FALSE, /* partial_inplace */
341 0, /* src_mask */
342 0xffff, /* dst_mask */
343 FALSE), /* pcrel_offset */
344
345 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
346 the symbol. */
347 HOWTO (R_PPC_GOT16_HI, /* type */
348 16, /* rightshift */
349 1, /* size (0 = byte, 1 = short, 2 = long) */
350 16, /* bitsize */
351 FALSE, /* pc_relative */
352 0, /* bitpos */
353 complain_overflow_bitfield, /* complain_on_overflow */
354 bfd_elf_generic_reloc, /* special_function */
355 "R_PPC_GOT16_HI", /* name */
356 FALSE, /* partial_inplace */
357 0, /* src_mask */
358 0xffff, /* dst_mask */
359 FALSE), /* pcrel_offset */
360
361 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
362 the symbol. */
363 HOWTO (R_PPC_GOT16_HA, /* type */
364 16, /* rightshift */
365 1, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
367 FALSE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_bitfield, /* complain_on_overflow */
370 ppc_elf_addr16_ha_reloc, /* special_function */
371 "R_PPC_GOT16_HA", /* name */
372 FALSE, /* partial_inplace */
373 0, /* src_mask */
374 0xffff, /* dst_mask */
375 FALSE), /* pcrel_offset */
376
377 /* Like R_PPC_REL24, but referring to the procedure linkage table
378 entry for the symbol. */
379 HOWTO (R_PPC_PLTREL24, /* type */
380 0, /* rightshift */
381 2, /* size (0 = byte, 1 = short, 2 = long) */
382 26, /* bitsize */
383 TRUE, /* pc_relative */
384 0, /* bitpos */
385 complain_overflow_signed, /* complain_on_overflow */
386 bfd_elf_generic_reloc, /* special_function */
387 "R_PPC_PLTREL24", /* name */
388 FALSE, /* partial_inplace */
389 0, /* src_mask */
390 0x3fffffc, /* dst_mask */
391 TRUE), /* pcrel_offset */
392
393 /* This is used only by the dynamic linker. The symbol should exist
394 both in the object being run and in some shared library. The
395 dynamic linker copies the data addressed by the symbol from the
396 shared library into the object, because the object being
397 run has to have the data at some particular address. */
398 HOWTO (R_PPC_COPY, /* type */
399 0, /* rightshift */
400 2, /* size (0 = byte, 1 = short, 2 = long) */
401 32, /* bitsize */
402 FALSE, /* pc_relative */
403 0, /* bitpos */
404 complain_overflow_bitfield, /* complain_on_overflow */
405 bfd_elf_generic_reloc, /* special_function */
406 "R_PPC_COPY", /* name */
407 FALSE, /* partial_inplace */
408 0, /* src_mask */
409 0, /* dst_mask */
410 FALSE), /* pcrel_offset */
411
412 /* Like R_PPC_ADDR32, but used when setting global offset table
413 entries. */
414 HOWTO (R_PPC_GLOB_DAT, /* type */
415 0, /* rightshift */
416 2, /* size (0 = byte, 1 = short, 2 = long) */
417 32, /* bitsize */
418 FALSE, /* pc_relative */
419 0, /* bitpos */
420 complain_overflow_bitfield, /* complain_on_overflow */
421 bfd_elf_generic_reloc, /* special_function */
422 "R_PPC_GLOB_DAT", /* name */
423 FALSE, /* partial_inplace */
424 0, /* src_mask */
425 0xffffffff, /* dst_mask */
426 FALSE), /* pcrel_offset */
427
428 /* Marks a procedure linkage table entry for a symbol. */
429 HOWTO (R_PPC_JMP_SLOT, /* type */
430 0, /* rightshift */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
432 32, /* bitsize */
433 FALSE, /* pc_relative */
434 0, /* bitpos */
435 complain_overflow_bitfield, /* complain_on_overflow */
436 bfd_elf_generic_reloc, /* special_function */
437 "R_PPC_JMP_SLOT", /* name */
438 FALSE, /* partial_inplace */
439 0, /* src_mask */
440 0, /* dst_mask */
441 FALSE), /* pcrel_offset */
442
443 /* Used only by the dynamic linker. When the object is run, this
444 longword is set to the load address of the object, plus the
445 addend. */
446 HOWTO (R_PPC_RELATIVE, /* type */
447 0, /* rightshift */
448 2, /* size (0 = byte, 1 = short, 2 = long) */
449 32, /* bitsize */
450 FALSE, /* pc_relative */
451 0, /* bitpos */
452 complain_overflow_bitfield, /* complain_on_overflow */
453 bfd_elf_generic_reloc, /* special_function */
454 "R_PPC_RELATIVE", /* name */
455 FALSE, /* partial_inplace */
456 0, /* src_mask */
457 0xffffffff, /* dst_mask */
458 FALSE), /* pcrel_offset */
459
460 /* Like R_PPC_REL24, but uses the value of the symbol within the
461 object rather than the final value. Normally used for
462 _GLOBAL_OFFSET_TABLE_. */
463 HOWTO (R_PPC_LOCAL24PC, /* type */
464 0, /* rightshift */
465 2, /* size (0 = byte, 1 = short, 2 = long) */
466 26, /* bitsize */
467 TRUE, /* pc_relative */
468 0, /* bitpos */
469 complain_overflow_signed, /* complain_on_overflow */
470 bfd_elf_generic_reloc, /* special_function */
471 "R_PPC_LOCAL24PC", /* name */
472 FALSE, /* partial_inplace */
473 0, /* src_mask */
474 0x3fffffc, /* dst_mask */
475 TRUE), /* pcrel_offset */
476
477 /* Like R_PPC_ADDR32, but may be unaligned. */
478 HOWTO (R_PPC_UADDR32, /* type */
479 0, /* rightshift */
480 2, /* size (0 = byte, 1 = short, 2 = long) */
481 32, /* bitsize */
482 FALSE, /* pc_relative */
483 0, /* bitpos */
484 complain_overflow_bitfield, /* complain_on_overflow */
485 bfd_elf_generic_reloc, /* special_function */
486 "R_PPC_UADDR32", /* name */
487 FALSE, /* partial_inplace */
488 0, /* src_mask */
489 0xffffffff, /* dst_mask */
490 FALSE), /* pcrel_offset */
491
492 /* Like R_PPC_ADDR16, but may be unaligned. */
493 HOWTO (R_PPC_UADDR16, /* type */
494 0, /* rightshift */
495 1, /* size (0 = byte, 1 = short, 2 = long) */
496 16, /* bitsize */
497 FALSE, /* pc_relative */
498 0, /* bitpos */
499 complain_overflow_bitfield, /* complain_on_overflow */
500 bfd_elf_generic_reloc, /* special_function */
501 "R_PPC_UADDR16", /* name */
502 FALSE, /* partial_inplace */
503 0, /* src_mask */
504 0xffff, /* dst_mask */
505 FALSE), /* pcrel_offset */
506
507 /* 32-bit PC relative */
508 HOWTO (R_PPC_REL32, /* type */
509 0, /* rightshift */
510 2, /* size (0 = byte, 1 = short, 2 = long) */
511 32, /* bitsize */
512 TRUE, /* pc_relative */
513 0, /* bitpos */
514 complain_overflow_bitfield, /* complain_on_overflow */
515 bfd_elf_generic_reloc, /* special_function */
516 "R_PPC_REL32", /* name */
517 FALSE, /* partial_inplace */
518 0, /* src_mask */
519 0xffffffff, /* dst_mask */
520 TRUE), /* pcrel_offset */
521
522 /* 32-bit relocation to the symbol's procedure linkage table.
523 FIXME: not supported. */
524 HOWTO (R_PPC_PLT32, /* type */
525 0, /* rightshift */
526 2, /* size (0 = byte, 1 = short, 2 = long) */
527 32, /* bitsize */
528 FALSE, /* pc_relative */
529 0, /* bitpos */
530 complain_overflow_bitfield, /* complain_on_overflow */
531 bfd_elf_generic_reloc, /* special_function */
532 "R_PPC_PLT32", /* name */
533 FALSE, /* partial_inplace */
534 0, /* src_mask */
535 0, /* dst_mask */
536 FALSE), /* pcrel_offset */
537
538 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
539 FIXME: not supported. */
540 HOWTO (R_PPC_PLTREL32, /* type */
541 0, /* rightshift */
542 2, /* size (0 = byte, 1 = short, 2 = long) */
543 32, /* bitsize */
544 TRUE, /* pc_relative */
545 0, /* bitpos */
546 complain_overflow_bitfield, /* complain_on_overflow */
547 bfd_elf_generic_reloc, /* special_function */
548 "R_PPC_PLTREL32", /* name */
549 FALSE, /* partial_inplace */
550 0, /* src_mask */
551 0, /* dst_mask */
552 TRUE), /* pcrel_offset */
553
554 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
555 the symbol. */
556 HOWTO (R_PPC_PLT16_LO, /* type */
557 0, /* rightshift */
558 1, /* size (0 = byte, 1 = short, 2 = long) */
559 16, /* bitsize */
560 FALSE, /* pc_relative */
561 0, /* bitpos */
562 complain_overflow_dont, /* complain_on_overflow */
563 bfd_elf_generic_reloc, /* special_function */
564 "R_PPC_PLT16_LO", /* name */
565 FALSE, /* partial_inplace */
566 0, /* src_mask */
567 0xffff, /* dst_mask */
568 FALSE), /* pcrel_offset */
569
570 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
571 the symbol. */
572 HOWTO (R_PPC_PLT16_HI, /* type */
573 16, /* rightshift */
574 1, /* size (0 = byte, 1 = short, 2 = long) */
575 16, /* bitsize */
576 FALSE, /* pc_relative */
577 0, /* bitpos */
578 complain_overflow_bitfield, /* complain_on_overflow */
579 bfd_elf_generic_reloc, /* special_function */
580 "R_PPC_PLT16_HI", /* name */
581 FALSE, /* partial_inplace */
582 0, /* src_mask */
583 0xffff, /* dst_mask */
584 FALSE), /* pcrel_offset */
585
586 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
587 the symbol. */
588 HOWTO (R_PPC_PLT16_HA, /* type */
589 16, /* 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 ppc_elf_addr16_ha_reloc, /* special_function */
596 "R_PPC_PLT16_HA", /* name */
597 FALSE, /* partial_inplace */
598 0, /* src_mask */
599 0xffff, /* dst_mask */
600 FALSE), /* pcrel_offset */
601
602 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
603 small data items. */
604 HOWTO (R_PPC_SDAREL16, /* type */
605 0, /* rightshift */
606 1, /* size (0 = byte, 1 = short, 2 = long) */
607 16, /* bitsize */
608 FALSE, /* pc_relative */
609 0, /* bitpos */
610 complain_overflow_signed, /* complain_on_overflow */
611 bfd_elf_generic_reloc, /* special_function */
612 "R_PPC_SDAREL16", /* name */
613 FALSE, /* partial_inplace */
614 0, /* src_mask */
615 0xffff, /* dst_mask */
616 FALSE), /* pcrel_offset */
617
618 /* 16-bit section relative relocation. */
619 HOWTO (R_PPC_SECTOFF, /* type */
620 0, /* rightshift */
621 1, /* size (0 = byte, 1 = short, 2 = long) */
622 16, /* bitsize */
623 FALSE, /* pc_relative */
624 0, /* bitpos */
625 complain_overflow_bitfield, /* complain_on_overflow */
626 bfd_elf_generic_reloc, /* special_function */
627 "R_PPC_SECTOFF", /* name */
628 FALSE, /* partial_inplace */
629 0, /* src_mask */
630 0xffff, /* dst_mask */
631 FALSE), /* pcrel_offset */
632
633 /* 16-bit lower half section relative relocation. */
634 HOWTO (R_PPC_SECTOFF_LO, /* type */
635 0, /* rightshift */
636 1, /* size (0 = byte, 1 = short, 2 = long) */
637 16, /* bitsize */
638 FALSE, /* pc_relative */
639 0, /* bitpos */
640 complain_overflow_dont, /* complain_on_overflow */
641 bfd_elf_generic_reloc, /* special_function */
642 "R_PPC_SECTOFF_LO", /* name */
643 FALSE, /* partial_inplace */
644 0, /* src_mask */
645 0xffff, /* dst_mask */
646 FALSE), /* pcrel_offset */
647
648 /* 16-bit upper half section relative relocation. */
649 HOWTO (R_PPC_SECTOFF_HI, /* type */
650 16, /* rightshift */
651 1, /* size (0 = byte, 1 = short, 2 = long) */
652 16, /* bitsize */
653 FALSE, /* pc_relative */
654 0, /* bitpos */
655 complain_overflow_bitfield, /* complain_on_overflow */
656 bfd_elf_generic_reloc, /* special_function */
657 "R_PPC_SECTOFF_HI", /* name */
658 FALSE, /* partial_inplace */
659 0, /* src_mask */
660 0xffff, /* dst_mask */
661 FALSE), /* pcrel_offset */
662
663 /* 16-bit upper half adjusted section relative relocation. */
664 HOWTO (R_PPC_SECTOFF_HA, /* type */
665 16, /* rightshift */
666 1, /* size (0 = byte, 1 = short, 2 = long) */
667 16, /* bitsize */
668 FALSE, /* pc_relative */
669 0, /* bitpos */
670 complain_overflow_bitfield, /* complain_on_overflow */
671 ppc_elf_addr16_ha_reloc, /* special_function */
672 "R_PPC_SECTOFF_HA", /* name */
673 FALSE, /* partial_inplace */
674 0, /* src_mask */
675 0xffff, /* dst_mask */
676 FALSE), /* pcrel_offset */
677
678 /* Marker reloc for TLS. */
679 HOWTO (R_PPC_TLS,
680 0, /* rightshift */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
682 32, /* bitsize */
683 FALSE, /* pc_relative */
684 0, /* bitpos */
685 complain_overflow_dont, /* complain_on_overflow */
686 bfd_elf_generic_reloc, /* special_function */
687 "R_PPC_TLS", /* name */
688 FALSE, /* partial_inplace */
689 0, /* src_mask */
690 0, /* dst_mask */
691 FALSE), /* pcrel_offset */
692
693 /* Computes the load module index of the load module that contains the
694 definition of its TLS sym. */
695 HOWTO (R_PPC_DTPMOD32,
696 0, /* rightshift */
697 2, /* size (0 = byte, 1 = short, 2 = long) */
698 32, /* bitsize */
699 FALSE, /* pc_relative */
700 0, /* bitpos */
701 complain_overflow_dont, /* complain_on_overflow */
702 ppc_elf_unhandled_reloc, /* special_function */
703 "R_PPC_DTPMOD32", /* name */
704 FALSE, /* partial_inplace */
705 0, /* src_mask */
706 0xffffffff, /* dst_mask */
707 FALSE), /* pcrel_offset */
708
709 /* Computes a dtv-relative displacement, the difference between the value
710 of sym+add and the base address of the thread-local storage block that
711 contains the definition of sym, minus 0x8000. */
712 HOWTO (R_PPC_DTPREL32,
713 0, /* rightshift */
714 2, /* size (0 = byte, 1 = short, 2 = long) */
715 32, /* bitsize */
716 FALSE, /* pc_relative */
717 0, /* bitpos */
718 complain_overflow_dont, /* complain_on_overflow */
719 ppc_elf_unhandled_reloc, /* special_function */
720 "R_PPC_DTPREL32", /* name */
721 FALSE, /* partial_inplace */
722 0, /* src_mask */
723 0xffffffff, /* dst_mask */
724 FALSE), /* pcrel_offset */
725
726 /* A 16 bit dtprel reloc. */
727 HOWTO (R_PPC_DTPREL16,
728 0, /* rightshift */
729 1, /* size (0 = byte, 1 = short, 2 = long) */
730 16, /* bitsize */
731 FALSE, /* pc_relative */
732 0, /* bitpos */
733 complain_overflow_signed, /* complain_on_overflow */
734 ppc_elf_unhandled_reloc, /* special_function */
735 "R_PPC_DTPREL16", /* name */
736 FALSE, /* partial_inplace */
737 0, /* src_mask */
738 0xffff, /* dst_mask */
739 FALSE), /* pcrel_offset */
740
741 /* Like DTPREL16, but no overflow. */
742 HOWTO (R_PPC_DTPREL16_LO,
743 0, /* rightshift */
744 1, /* size (0 = byte, 1 = short, 2 = long) */
745 16, /* bitsize */
746 FALSE, /* pc_relative */
747 0, /* bitpos */
748 complain_overflow_dont, /* complain_on_overflow */
749 ppc_elf_unhandled_reloc, /* special_function */
750 "R_PPC_DTPREL16_LO", /* name */
751 FALSE, /* partial_inplace */
752 0, /* src_mask */
753 0xffff, /* dst_mask */
754 FALSE), /* pcrel_offset */
755
756 /* Like DTPREL16_LO, but next higher group of 16 bits. */
757 HOWTO (R_PPC_DTPREL16_HI,
758 16, /* rightshift */
759 1, /* size (0 = byte, 1 = short, 2 = long) */
760 16, /* bitsize */
761 FALSE, /* pc_relative */
762 0, /* bitpos */
763 complain_overflow_dont, /* complain_on_overflow */
764 ppc_elf_unhandled_reloc, /* special_function */
765 "R_PPC_DTPREL16_HI", /* name */
766 FALSE, /* partial_inplace */
767 0, /* src_mask */
768 0xffff, /* dst_mask */
769 FALSE), /* pcrel_offset */
770
771 /* Like DTPREL16_HI, but adjust for low 16 bits. */
772 HOWTO (R_PPC_DTPREL16_HA,
773 16, /* rightshift */
774 1, /* size (0 = byte, 1 = short, 2 = long) */
775 16, /* bitsize */
776 FALSE, /* pc_relative */
777 0, /* bitpos */
778 complain_overflow_dont, /* complain_on_overflow */
779 ppc_elf_unhandled_reloc, /* special_function */
780 "R_PPC_DTPREL16_HA", /* name */
781 FALSE, /* partial_inplace */
782 0, /* src_mask */
783 0xffff, /* dst_mask */
784 FALSE), /* pcrel_offset */
785
786 /* Computes a tp-relative displacement, the difference between the value of
787 sym+add and the value of the thread pointer (r13). */
788 HOWTO (R_PPC_TPREL32,
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 ppc_elf_unhandled_reloc, /* special_function */
796 "R_PPC_TPREL32", /* name */
797 FALSE, /* partial_inplace */
798 0, /* src_mask */
799 0xffffffff, /* dst_mask */
800 FALSE), /* pcrel_offset */
801
802 /* A 16 bit tprel reloc. */
803 HOWTO (R_PPC_TPREL16,
804 0, /* rightshift */
805 1, /* size (0 = byte, 1 = short, 2 = long) */
806 16, /* bitsize */
807 FALSE, /* pc_relative */
808 0, /* bitpos */
809 complain_overflow_signed, /* complain_on_overflow */
810 ppc_elf_unhandled_reloc, /* special_function */
811 "R_PPC_TPREL16", /* name */
812 FALSE, /* partial_inplace */
813 0, /* src_mask */
814 0xffff, /* dst_mask */
815 FALSE), /* pcrel_offset */
816
817 /* Like TPREL16, but no overflow. */
818 HOWTO (R_PPC_TPREL16_LO,
819 0, /* rightshift */
820 1, /* size (0 = byte, 1 = short, 2 = long) */
821 16, /* 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_TPREL16_LO", /* name */
827 FALSE, /* partial_inplace */
828 0, /* src_mask */
829 0xffff, /* dst_mask */
830 FALSE), /* pcrel_offset */
831
832 /* Like TPREL16_LO, but next higher group of 16 bits. */
833 HOWTO (R_PPC_TPREL16_HI,
834 16, /* rightshift */
835 1, /* size (0 = byte, 1 = short, 2 = long) */
836 16, /* bitsize */
837 FALSE, /* pc_relative */
838 0, /* bitpos */
839 complain_overflow_dont, /* complain_on_overflow */
840 ppc_elf_unhandled_reloc, /* special_function */
841 "R_PPC_TPREL16_HI", /* name */
842 FALSE, /* partial_inplace */
843 0, /* src_mask */
844 0xffff, /* dst_mask */
845 FALSE), /* pcrel_offset */
846
847 /* Like TPREL16_HI, but adjust for low 16 bits. */
848 HOWTO (R_PPC_TPREL16_HA,
849 16, /* rightshift */
850 1, /* size (0 = byte, 1 = short, 2 = long) */
851 16, /* bitsize */
852 FALSE, /* pc_relative */
853 0, /* bitpos */
854 complain_overflow_dont, /* complain_on_overflow */
855 ppc_elf_unhandled_reloc, /* special_function */
856 "R_PPC_TPREL16_HA", /* name */
857 FALSE, /* partial_inplace */
858 0, /* src_mask */
859 0xffff, /* dst_mask */
860 FALSE), /* pcrel_offset */
861
862 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
863 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
864 to the first entry. */
865 HOWTO (R_PPC_GOT_TLSGD16,
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_signed, /* complain_on_overflow */
872 ppc_elf_unhandled_reloc, /* special_function */
873 "R_PPC_GOT_TLSGD16", /* name */
874 FALSE, /* partial_inplace */
875 0, /* src_mask */
876 0xffff, /* dst_mask */
877 FALSE), /* pcrel_offset */
878
879 /* Like GOT_TLSGD16, but no overflow. */
880 HOWTO (R_PPC_GOT_TLSGD16_LO,
881 0, /* 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_GOT_TLSGD16_LO", /* name */
889 FALSE, /* partial_inplace */
890 0, /* src_mask */
891 0xffff, /* dst_mask */
892 FALSE), /* pcrel_offset */
893
894 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
895 HOWTO (R_PPC_GOT_TLSGD16_HI,
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_GOT_TLSGD16_HI", /* name */
904 FALSE, /* partial_inplace */
905 0, /* src_mask */
906 0xffff, /* dst_mask */
907 FALSE), /* pcrel_offset */
908
909 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
910 HOWTO (R_PPC_GOT_TLSGD16_HA,
911 16, /* rightshift */
912 1, /* size (0 = byte, 1 = short, 2 = long) */
913 16, /* bitsize */
914 FALSE, /* pc_relative */
915 0, /* bitpos */
916 complain_overflow_dont, /* complain_on_overflow */
917 ppc_elf_unhandled_reloc, /* special_function */
918 "R_PPC_GOT_TLSGD16_HA", /* name */
919 FALSE, /* partial_inplace */
920 0, /* src_mask */
921 0xffff, /* dst_mask */
922 FALSE), /* pcrel_offset */
923
924 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
925 with values (sym+add)@dtpmod and zero, and computes the offset to the
926 first entry. */
927 HOWTO (R_PPC_GOT_TLSLD16,
928 0, /* rightshift */
929 1, /* size (0 = byte, 1 = short, 2 = long) */
930 16, /* bitsize */
931 FALSE, /* pc_relative */
932 0, /* bitpos */
933 complain_overflow_signed, /* complain_on_overflow */
934 ppc_elf_unhandled_reloc, /* special_function */
935 "R_PPC_GOT_TLSLD16", /* name */
936 FALSE, /* partial_inplace */
937 0, /* src_mask */
938 0xffff, /* dst_mask */
939 FALSE), /* pcrel_offset */
940
941 /* Like GOT_TLSLD16, but no overflow. */
942 HOWTO (R_PPC_GOT_TLSLD16_LO,
943 0, /* rightshift */
944 1, /* size (0 = byte, 1 = short, 2 = long) */
945 16, /* bitsize */
946 FALSE, /* pc_relative */
947 0, /* bitpos */
948 complain_overflow_dont, /* complain_on_overflow */
949 ppc_elf_unhandled_reloc, /* special_function */
950 "R_PPC_GOT_TLSLD16_LO", /* name */
951 FALSE, /* partial_inplace */
952 0, /* src_mask */
953 0xffff, /* dst_mask */
954 FALSE), /* pcrel_offset */
955
956 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
957 HOWTO (R_PPC_GOT_TLSLD16_HI,
958 16, /* rightshift */
959 1, /* size (0 = byte, 1 = short, 2 = long) */
960 16, /* bitsize */
961 FALSE, /* pc_relative */
962 0, /* bitpos */
963 complain_overflow_dont, /* complain_on_overflow */
964 ppc_elf_unhandled_reloc, /* special_function */
965 "R_PPC_GOT_TLSLD16_HI", /* name */
966 FALSE, /* partial_inplace */
967 0, /* src_mask */
968 0xffff, /* dst_mask */
969 FALSE), /* pcrel_offset */
970
971 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
972 HOWTO (R_PPC_GOT_TLSLD16_HA,
973 16, /* rightshift */
974 1, /* size (0 = byte, 1 = short, 2 = long) */
975 16, /* bitsize */
976 FALSE, /* pc_relative */
977 0, /* bitpos */
978 complain_overflow_dont, /* complain_on_overflow */
979 ppc_elf_unhandled_reloc, /* special_function */
980 "R_PPC_GOT_TLSLD16_HA", /* name */
981 FALSE, /* partial_inplace */
982 0, /* src_mask */
983 0xffff, /* dst_mask */
984 FALSE), /* pcrel_offset */
985
986 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
987 the offset to the entry. */
988 HOWTO (R_PPC_GOT_DTPREL16,
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_DTPREL16", /* name */
997 FALSE, /* partial_inplace */
998 0, /* src_mask */
999 0xffff, /* dst_mask */
1000 FALSE), /* pcrel_offset */
1001
1002 /* Like GOT_DTPREL16, but no overflow. */
1003 HOWTO (R_PPC_GOT_DTPREL16_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_DTPREL16_LO", /* name */
1012 FALSE, /* partial_inplace */
1013 0, /* src_mask */
1014 0xffff, /* dst_mask */
1015 FALSE), /* pcrel_offset */
1016
1017 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1018 HOWTO (R_PPC_GOT_DTPREL16_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_DTPREL16_HI", /* name */
1027 FALSE, /* partial_inplace */
1028 0, /* src_mask */
1029 0xffff, /* dst_mask */
1030 FALSE), /* pcrel_offset */
1031
1032 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1033 HOWTO (R_PPC_GOT_DTPREL16_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_DTPREL16_HA", /* name */
1042 FALSE, /* partial_inplace */
1043 0, /* src_mask */
1044 0xffff, /* dst_mask */
1045 FALSE), /* pcrel_offset */
1046
1047 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1048 offset to the entry. */
1049 HOWTO (R_PPC_GOT_TPREL16,
1050 0, /* rightshift */
1051 1, /* size (0 = byte, 1 = short, 2 = long) */
1052 16, /* bitsize */
1053 FALSE, /* pc_relative */
1054 0, /* bitpos */
1055 complain_overflow_signed, /* complain_on_overflow */
1056 ppc_elf_unhandled_reloc, /* special_function */
1057 "R_PPC_GOT_TPREL16", /* name */
1058 FALSE, /* partial_inplace */
1059 0, /* src_mask */
1060 0xffff, /* dst_mask */
1061 FALSE), /* pcrel_offset */
1062
1063 /* Like GOT_TPREL16, but no overflow. */
1064 HOWTO (R_PPC_GOT_TPREL16_LO,
1065 0, /* rightshift */
1066 1, /* size (0 = byte, 1 = short, 2 = long) */
1067 16, /* bitsize */
1068 FALSE, /* pc_relative */
1069 0, /* bitpos */
1070 complain_overflow_dont, /* complain_on_overflow */
1071 ppc_elf_unhandled_reloc, /* special_function */
1072 "R_PPC_GOT_TPREL16_LO", /* name */
1073 FALSE, /* partial_inplace */
1074 0, /* src_mask */
1075 0xffff, /* dst_mask */
1076 FALSE), /* pcrel_offset */
1077
1078 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1079 HOWTO (R_PPC_GOT_TPREL16_HI,
1080 16, /* rightshift */
1081 1, /* size (0 = byte, 1 = short, 2 = long) */
1082 16, /* bitsize */
1083 FALSE, /* pc_relative */
1084 0, /* bitpos */
1085 complain_overflow_dont, /* complain_on_overflow */
1086 ppc_elf_unhandled_reloc, /* special_function */
1087 "R_PPC_GOT_TPREL16_HI", /* name */
1088 FALSE, /* partial_inplace */
1089 0, /* src_mask */
1090 0xffff, /* dst_mask */
1091 FALSE), /* pcrel_offset */
1092
1093 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1094 HOWTO (R_PPC_GOT_TPREL16_HA,
1095 16, /* rightshift */
1096 1, /* size (0 = byte, 1 = short, 2 = long) */
1097 16, /* bitsize */
1098 FALSE, /* pc_relative */
1099 0, /* bitpos */
1100 complain_overflow_dont, /* complain_on_overflow */
1101 ppc_elf_unhandled_reloc, /* special_function */
1102 "R_PPC_GOT_TPREL16_HA", /* name */
1103 FALSE, /* partial_inplace */
1104 0, /* src_mask */
1105 0xffff, /* dst_mask */
1106 FALSE), /* pcrel_offset */
1107
1108 /* The remaining relocs are from the Embedded ELF ABI, and are not
1109 in the SVR4 ELF ABI. */
1110
1111 /* 32 bit value resulting from the addend minus the symbol. */
1112 HOWTO (R_PPC_EMB_NADDR32, /* type */
1113 0, /* rightshift */
1114 2, /* size (0 = byte, 1 = short, 2 = long) */
1115 32, /* bitsize */
1116 FALSE, /* pc_relative */
1117 0, /* bitpos */
1118 complain_overflow_bitfield, /* complain_on_overflow */
1119 bfd_elf_generic_reloc, /* special_function */
1120 "R_PPC_EMB_NADDR32", /* name */
1121 FALSE, /* partial_inplace */
1122 0, /* src_mask */
1123 0xffffffff, /* dst_mask */
1124 FALSE), /* pcrel_offset */
1125
1126 /* 16 bit value resulting from the addend minus the symbol. */
1127 HOWTO (R_PPC_EMB_NADDR16, /* type */
1128 0, /* rightshift */
1129 1, /* size (0 = byte, 1 = short, 2 = long) */
1130 16, /* bitsize */
1131 FALSE, /* pc_relative */
1132 0, /* bitpos */
1133 complain_overflow_bitfield, /* complain_on_overflow */
1134 bfd_elf_generic_reloc, /* special_function */
1135 "R_PPC_EMB_NADDR16", /* name */
1136 FALSE, /* partial_inplace */
1137 0, /* src_mask */
1138 0xffff, /* dst_mask */
1139 FALSE), /* pcrel_offset */
1140
1141 /* 16 bit value resulting from the addend minus the symbol. */
1142 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1143 0, /* rightshift */
1144 1, /* size (0 = byte, 1 = short, 2 = long) */
1145 16, /* bitsize */
1146 FALSE, /* pc_relative */
1147 0, /* bitpos */
1148 complain_overflow_dont,/* complain_on_overflow */
1149 bfd_elf_generic_reloc, /* special_function */
1150 "R_PPC_EMB_ADDR16_LO", /* name */
1151 FALSE, /* partial_inplace */
1152 0, /* src_mask */
1153 0xffff, /* dst_mask */
1154 FALSE), /* pcrel_offset */
1155
1156 /* The high order 16 bits of the addend minus the symbol. */
1157 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1158 16, /* rightshift */
1159 1, /* size (0 = byte, 1 = short, 2 = long) */
1160 16, /* bitsize */
1161 FALSE, /* pc_relative */
1162 0, /* bitpos */
1163 complain_overflow_dont, /* complain_on_overflow */
1164 bfd_elf_generic_reloc, /* special_function */
1165 "R_PPC_EMB_NADDR16_HI", /* name */
1166 FALSE, /* partial_inplace */
1167 0, /* src_mask */
1168 0xffff, /* dst_mask */
1169 FALSE), /* pcrel_offset */
1170
1171 /* The high order 16 bits of the result of the addend minus the address,
1172 plus 1 if the contents of the low 16 bits, treated as a signed number,
1173 is negative. */
1174 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1175 16, /* rightshift */
1176 1, /* size (0 = byte, 1 = short, 2 = long) */
1177 16, /* bitsize */
1178 FALSE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_dont, /* complain_on_overflow */
1181 ppc_elf_addr16_ha_reloc, /* special_function */
1182 "R_PPC_EMB_NADDR16_HA", /* name */
1183 FALSE, /* partial_inplace */
1184 0, /* src_mask */
1185 0xffff, /* dst_mask */
1186 FALSE), /* pcrel_offset */
1187
1188 /* 16 bit value resulting from allocating a 4 byte word to hold an
1189 address in the .sdata section, and returning the offset from
1190 _SDA_BASE_ for that relocation. */
1191 HOWTO (R_PPC_EMB_SDAI16, /* type */
1192 0, /* rightshift */
1193 1, /* size (0 = byte, 1 = short, 2 = long) */
1194 16, /* bitsize */
1195 FALSE, /* pc_relative */
1196 0, /* bitpos */
1197 complain_overflow_bitfield, /* complain_on_overflow */
1198 bfd_elf_generic_reloc, /* special_function */
1199 "R_PPC_EMB_SDAI16", /* name */
1200 FALSE, /* partial_inplace */
1201 0, /* src_mask */
1202 0xffff, /* dst_mask */
1203 FALSE), /* pcrel_offset */
1204
1205 /* 16 bit value resulting from allocating a 4 byte word to hold an
1206 address in the .sdata2 section, and returning the offset from
1207 _SDA2_BASE_ for that relocation. */
1208 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1209 0, /* rightshift */
1210 1, /* size (0 = byte, 1 = short, 2 = long) */
1211 16, /* bitsize */
1212 FALSE, /* pc_relative */
1213 0, /* bitpos */
1214 complain_overflow_bitfield, /* complain_on_overflow */
1215 bfd_elf_generic_reloc, /* special_function */
1216 "R_PPC_EMB_SDA2I16", /* name */
1217 FALSE, /* partial_inplace */
1218 0, /* src_mask */
1219 0xffff, /* dst_mask */
1220 FALSE), /* pcrel_offset */
1221
1222 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1223 small data items. */
1224 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1225 0, /* rightshift */
1226 1, /* size (0 = byte, 1 = short, 2 = long) */
1227 16, /* bitsize */
1228 FALSE, /* pc_relative */
1229 0, /* bitpos */
1230 complain_overflow_signed, /* complain_on_overflow */
1231 bfd_elf_generic_reloc, /* special_function */
1232 "R_PPC_EMB_SDA2REL", /* name */
1233 FALSE, /* partial_inplace */
1234 0, /* src_mask */
1235 0xffff, /* dst_mask */
1236 FALSE), /* pcrel_offset */
1237
1238 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1239 signed offset from the appropriate base, and filling in the register
1240 field with the appropriate register (0, 2, or 13). */
1241 HOWTO (R_PPC_EMB_SDA21, /* type */
1242 0, /* rightshift */
1243 2, /* size (0 = byte, 1 = short, 2 = long) */
1244 16, /* bitsize */
1245 FALSE, /* pc_relative */
1246 0, /* bitpos */
1247 complain_overflow_signed, /* complain_on_overflow */
1248 bfd_elf_generic_reloc, /* special_function */
1249 "R_PPC_EMB_SDA21", /* name */
1250 FALSE, /* partial_inplace */
1251 0, /* src_mask */
1252 0xffff, /* dst_mask */
1253 FALSE), /* pcrel_offset */
1254
1255 /* Relocation not handled: R_PPC_EMB_MRKREF */
1256 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1257 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1258 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1259 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1260 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1261
1262 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1263 in the 16 bit signed offset from the appropriate base, and filling in the
1264 register field with the appropriate register (0, 2, or 13). */
1265 HOWTO (R_PPC_EMB_RELSDA, /* type */
1266 0, /* rightshift */
1267 1, /* size (0 = byte, 1 = short, 2 = long) */
1268 16, /* bitsize */
1269 TRUE, /* pc_relative */
1270 0, /* bitpos */
1271 complain_overflow_signed, /* complain_on_overflow */
1272 bfd_elf_generic_reloc, /* special_function */
1273 "R_PPC_EMB_RELSDA", /* name */
1274 FALSE, /* partial_inplace */
1275 0, /* src_mask */
1276 0xffff, /* dst_mask */
1277 FALSE), /* pcrel_offset */
1278
1279 /* A 16 bit relative relocation. */
1280 HOWTO (R_PPC_REL16, /* type */
1281 0, /* rightshift */
1282 1, /* size (0 = byte, 1 = short, 2 = long) */
1283 16, /* bitsize */
1284 TRUE, /* pc_relative */
1285 0, /* bitpos */
1286 complain_overflow_bitfield, /* complain_on_overflow */
1287 bfd_elf_generic_reloc, /* special_function */
1288 "R_PPC_REL16", /* name */
1289 FALSE, /* partial_inplace */
1290 0, /* src_mask */
1291 0xffff, /* dst_mask */
1292 TRUE), /* pcrel_offset */
1293
1294 /* A 16 bit relative relocation without overflow. */
1295 HOWTO (R_PPC_REL16_LO, /* type */
1296 0, /* rightshift */
1297 1, /* size (0 = byte, 1 = short, 2 = long) */
1298 16, /* bitsize */
1299 TRUE, /* pc_relative */
1300 0, /* bitpos */
1301 complain_overflow_dont,/* complain_on_overflow */
1302 bfd_elf_generic_reloc, /* special_function */
1303 "R_PPC_REL16_LO", /* name */
1304 FALSE, /* partial_inplace */
1305 0, /* src_mask */
1306 0xffff, /* dst_mask */
1307 TRUE), /* pcrel_offset */
1308
1309 /* The high order 16 bits of a relative address. */
1310 HOWTO (R_PPC_REL16_HI, /* type */
1311 16, /* rightshift */
1312 1, /* size (0 = byte, 1 = short, 2 = long) */
1313 16, /* bitsize */
1314 TRUE, /* pc_relative */
1315 0, /* bitpos */
1316 complain_overflow_dont, /* complain_on_overflow */
1317 bfd_elf_generic_reloc, /* special_function */
1318 "R_PPC_REL16_HI", /* name */
1319 FALSE, /* partial_inplace */
1320 0, /* src_mask */
1321 0xffff, /* dst_mask */
1322 TRUE), /* pcrel_offset */
1323
1324 /* The high order 16 bits of a relative address, plus 1 if the contents of
1325 the low 16 bits, treated as a signed number, is negative. */
1326 HOWTO (R_PPC_REL16_HA, /* type */
1327 16, /* rightshift */
1328 1, /* size (0 = byte, 1 = short, 2 = long) */
1329 16, /* bitsize */
1330 TRUE, /* pc_relative */
1331 0, /* bitpos */
1332 complain_overflow_dont, /* complain_on_overflow */
1333 ppc_elf_addr16_ha_reloc, /* special_function */
1334 "R_PPC_REL16_HA", /* name */
1335 FALSE, /* partial_inplace */
1336 0, /* src_mask */
1337 0xffff, /* dst_mask */
1338 TRUE), /* pcrel_offset */
1339
1340 /* GNU extension to record C++ vtable hierarchy. */
1341 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1342 0, /* rightshift */
1343 0, /* size (0 = byte, 1 = short, 2 = long) */
1344 0, /* bitsize */
1345 FALSE, /* pc_relative */
1346 0, /* bitpos */
1347 complain_overflow_dont, /* complain_on_overflow */
1348 NULL, /* special_function */
1349 "R_PPC_GNU_VTINHERIT", /* name */
1350 FALSE, /* partial_inplace */
1351 0, /* src_mask */
1352 0, /* dst_mask */
1353 FALSE), /* pcrel_offset */
1354
1355 /* GNU extension to record C++ vtable member usage. */
1356 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1357 0, /* rightshift */
1358 0, /* size (0 = byte, 1 = short, 2 = long) */
1359 0, /* bitsize */
1360 FALSE, /* pc_relative */
1361 0, /* bitpos */
1362 complain_overflow_dont, /* complain_on_overflow */
1363 NULL, /* special_function */
1364 "R_PPC_GNU_VTENTRY", /* name */
1365 FALSE, /* partial_inplace */
1366 0, /* src_mask */
1367 0, /* dst_mask */
1368 FALSE), /* pcrel_offset */
1369
1370 /* Phony reloc to handle AIX style TOC entries. */
1371 HOWTO (R_PPC_TOC16, /* type */
1372 0, /* rightshift */
1373 1, /* size (0 = byte, 1 = short, 2 = long) */
1374 16, /* bitsize */
1375 FALSE, /* pc_relative */
1376 0, /* bitpos */
1377 complain_overflow_signed, /* complain_on_overflow */
1378 bfd_elf_generic_reloc, /* special_function */
1379 "R_PPC_TOC16", /* name */
1380 FALSE, /* partial_inplace */
1381 0, /* src_mask */
1382 0xffff, /* dst_mask */
1383 FALSE), /* pcrel_offset */
1384 };
1385 \f
1386 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1387
1388 static void
1389 ppc_elf_howto_init (void)
1390 {
1391 unsigned int i, type;
1392
1393 for (i = 0;
1394 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1395 i++)
1396 {
1397 type = ppc_elf_howto_raw[i].type;
1398 if (type >= (sizeof (ppc_elf_howto_table)
1399 / sizeof (ppc_elf_howto_table[0])))
1400 abort ();
1401 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1402 }
1403 }
1404
1405 static reloc_howto_type *
1406 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1407 bfd_reloc_code_real_type code)
1408 {
1409 enum elf_ppc_reloc_type r;
1410
1411 /* Initialize howto table if not already done. */
1412 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1413 ppc_elf_howto_init ();
1414
1415 switch (code)
1416 {
1417 default:
1418 return NULL;
1419
1420 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
1421 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
1422 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
1423 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1424 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
1425 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
1426 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
1427 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
1428 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
1429 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
1430 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
1431 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
1432 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
1433 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
1434 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1435 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
1436 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
1437 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
1438 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
1439 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
1440 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
1441 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
1442 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
1443 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
1444 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
1445 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
1446 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
1447 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
1448 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
1449 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1450 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
1451 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
1452 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
1453 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
1454 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
1455 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
1456 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1457 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1458 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
1459 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
1460 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
1461 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
1462 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1463 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
1464 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
1465 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
1466 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
1467 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
1468 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
1469 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
1470 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
1471 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
1472 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
1473 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
1474 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
1475 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
1476 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
1477 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
1478 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
1479 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
1480 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
1481 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
1482 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
1483 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
1484 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
1485 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
1486 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
1487 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
1488 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
1489 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
1490 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
1491 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
1492 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
1493 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
1494 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
1495 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
1496 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
1497 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
1498 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
1499 case BFD_RELOC_16_PCREL: r = R_PPC_REL16; break;
1500 case BFD_RELOC_LO16_PCREL: r = R_PPC_REL16_LO; break;
1501 case BFD_RELOC_HI16_PCREL: r = R_PPC_REL16_HI; break;
1502 case BFD_RELOC_HI16_S_PCREL: r = R_PPC_REL16_HA; break;
1503 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
1504 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
1505 }
1506
1507 return ppc_elf_howto_table[r];
1508 };
1509
1510 /* Set the howto pointer for a PowerPC ELF reloc. */
1511
1512 static void
1513 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1514 arelent *cache_ptr,
1515 Elf_Internal_Rela *dst)
1516 {
1517 /* Initialize howto table if not already done. */
1518 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1519 ppc_elf_howto_init ();
1520
1521 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1522 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1523 }
1524
1525 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs. */
1526
1527 static bfd_reloc_status_type
1528 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1529 arelent *reloc_entry,
1530 asymbol *symbol,
1531 void *data ATTRIBUTE_UNUSED,
1532 asection *input_section,
1533 bfd *output_bfd,
1534 char **error_message ATTRIBUTE_UNUSED)
1535 {
1536 bfd_vma relocation;
1537
1538 if (output_bfd != NULL)
1539 {
1540 reloc_entry->address += input_section->output_offset;
1541 return bfd_reloc_ok;
1542 }
1543
1544 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1545 return bfd_reloc_outofrange;
1546
1547 if (bfd_is_com_section (symbol->section))
1548 relocation = 0;
1549 else
1550 relocation = symbol->value;
1551
1552 relocation += symbol->section->output_section->vma;
1553 relocation += symbol->section->output_offset;
1554 relocation += reloc_entry->addend;
1555 if (reloc_entry->howto->pc_relative)
1556 relocation -= reloc_entry->address;
1557
1558 reloc_entry->addend += (relocation & 0x8000) << 1;
1559
1560 return bfd_reloc_continue;
1561 }
1562
1563 static bfd_reloc_status_type
1564 ppc_elf_unhandled_reloc (bfd *abfd,
1565 arelent *reloc_entry,
1566 asymbol *symbol,
1567 void *data,
1568 asection *input_section,
1569 bfd *output_bfd,
1570 char **error_message)
1571 {
1572 /* If this is a relocatable link (output_bfd test tells us), just
1573 call the generic function. Any adjustment will be done at final
1574 link time. */
1575 if (output_bfd != NULL)
1576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1577 input_section, output_bfd, error_message);
1578
1579 if (error_message != NULL)
1580 {
1581 static char buf[60];
1582 sprintf (buf, _("generic linker can't handle %s"),
1583 reloc_entry->howto->name);
1584 *error_message = buf;
1585 }
1586 return bfd_reloc_dangerous;
1587 }
1588 \f
1589 /* Sections created by the linker. */
1590
1591 typedef struct elf_linker_section
1592 {
1593 /* Pointer to the bfd section. */
1594 asection *section;
1595 /* Section name. */
1596 const char *name;
1597 /* Associated bss section name. */
1598 const char *bss_name;
1599 /* Associated symbol name. */
1600 const char *sym_name;
1601 /* Value of symbol. */
1602 bfd_vma sym_val;
1603 } elf_linker_section_t;
1604
1605 /* Linked list of allocated pointer entries. This hangs off of the
1606 symbol lists, and provides allows us to return different pointers,
1607 based on different addend's. */
1608
1609 typedef struct elf_linker_section_pointers
1610 {
1611 /* next allocated pointer for this symbol */
1612 struct elf_linker_section_pointers *next;
1613 /* offset of pointer from beginning of section */
1614 bfd_vma offset;
1615 /* addend used */
1616 bfd_vma addend;
1617 /* which linker section this is */
1618 elf_linker_section_t *lsect;
1619 } elf_linker_section_pointers_t;
1620
1621 struct ppc_elf_obj_tdata
1622 {
1623 struct elf_obj_tdata elf;
1624
1625 /* A mapping from local symbols to offsets into the various linker
1626 sections added. This is index by the symbol index. */
1627 elf_linker_section_pointers_t **linker_section_pointers;
1628 };
1629
1630 #define ppc_elf_tdata(bfd) \
1631 ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1632
1633 #define elf_local_ptr_offsets(bfd) \
1634 (ppc_elf_tdata (bfd)->linker_section_pointers)
1635
1636 /* Override the generic function because we store some extras. */
1637
1638 static bfd_boolean
1639 ppc_elf_mkobject (bfd *abfd)
1640 {
1641 bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
1642 abfd->tdata.any = bfd_zalloc (abfd, amt);
1643 if (abfd->tdata.any == NULL)
1644 return FALSE;
1645 return TRUE;
1646 }
1647
1648 /* Fix bad default arch selected for a 32 bit input bfd when the
1649 default is 64 bit. */
1650
1651 static bfd_boolean
1652 ppc_elf_object_p (bfd *abfd)
1653 {
1654 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1655 {
1656 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1657
1658 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1659 {
1660 /* Relies on arch after 64 bit default being 32 bit default. */
1661 abfd->arch_info = abfd->arch_info->next;
1662 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1663 }
1664 }
1665 return TRUE;
1666 }
1667
1668 /* Function to set whether a module needs the -mrelocatable bit set. */
1669
1670 static bfd_boolean
1671 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1672 {
1673 BFD_ASSERT (!elf_flags_init (abfd)
1674 || elf_elfheader (abfd)->e_flags == flags);
1675
1676 elf_elfheader (abfd)->e_flags = flags;
1677 elf_flags_init (abfd) = TRUE;
1678 return TRUE;
1679 }
1680
1681 /* Support for core dump NOTE sections. */
1682
1683 static bfd_boolean
1684 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1685 {
1686 int offset;
1687 unsigned int size;
1688
1689 switch (note->descsz)
1690 {
1691 default:
1692 return FALSE;
1693
1694 case 268: /* Linux/PPC. */
1695 /* pr_cursig */
1696 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1697
1698 /* pr_pid */
1699 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1700
1701 /* pr_reg */
1702 offset = 72;
1703 size = 192;
1704
1705 break;
1706 }
1707
1708 /* Make a ".reg/999" section. */
1709 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1710 size, note->descpos + offset);
1711 }
1712
1713 static bfd_boolean
1714 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1715 {
1716 switch (note->descsz)
1717 {
1718 default:
1719 return FALSE;
1720
1721 case 128: /* Linux/PPC elf_prpsinfo. */
1722 elf_tdata (abfd)->core_program
1723 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1724 elf_tdata (abfd)->core_command
1725 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1726 }
1727
1728 /* Note that for some reason, a spurious space is tacked
1729 onto the end of the args in some (at least one anyway)
1730 implementations, so strip it off if it exists. */
1731
1732 {
1733 char *command = elf_tdata (abfd)->core_command;
1734 int n = strlen (command);
1735
1736 if (0 < n && command[n - 1] == ' ')
1737 command[n - 1] = '\0';
1738 }
1739
1740 return TRUE;
1741 }
1742
1743 /* Return address for Ith PLT stub in section PLT, for relocation REL
1744 or (bfd_vma) -1 if it should not be included. */
1745
1746 static bfd_vma
1747 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1748 const asection *plt ATTRIBUTE_UNUSED,
1749 const arelent *rel)
1750 {
1751 return rel->address;
1752 }
1753
1754 /* Handle a PowerPC specific section when reading an object file. This
1755 is called when bfd_section_from_shdr finds a section with an unknown
1756 type. */
1757
1758 static bfd_boolean
1759 ppc_elf_section_from_shdr (bfd *abfd,
1760 Elf_Internal_Shdr *hdr,
1761 const char *name,
1762 int shindex)
1763 {
1764 asection *newsect;
1765 flagword flags;
1766
1767 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1768 return FALSE;
1769
1770 newsect = hdr->bfd_section;
1771 flags = bfd_get_section_flags (abfd, newsect);
1772 if (hdr->sh_flags & SHF_EXCLUDE)
1773 flags |= SEC_EXCLUDE;
1774
1775 if (hdr->sh_type == SHT_ORDERED)
1776 flags |= SEC_SORT_ENTRIES;
1777
1778 bfd_set_section_flags (abfd, newsect, flags);
1779 return TRUE;
1780 }
1781
1782 /* Set up any other section flags and such that may be necessary. */
1783
1784 static bfd_boolean
1785 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1786 Elf_Internal_Shdr *shdr,
1787 asection *asect)
1788 {
1789 if ((asect->flags & SEC_EXCLUDE) != 0)
1790 shdr->sh_flags |= SHF_EXCLUDE;
1791
1792 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1793 shdr->sh_type = SHT_ORDERED;
1794
1795 return TRUE;
1796 }
1797
1798 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
1799 need to bump up the number of section headers. */
1800
1801 static int
1802 ppc_elf_additional_program_headers (bfd *abfd)
1803 {
1804 asection *s;
1805 int ret = 0;
1806
1807 s = bfd_get_section_by_name (abfd, ".sbss2");
1808 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1809 ++ret;
1810
1811 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1812 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1813 ++ret;
1814
1815 return ret;
1816 }
1817
1818 /* Add extra PPC sections -- Note, for now, make .sbss2 and
1819 .PPC.EMB.sbss0 a normal section, and not a bss section so
1820 that the linker doesn't crater when trying to make more than
1821 2 sections. */
1822
1823 static struct bfd_elf_special_section const
1824 ppc_special_sections_p[] =
1825 {
1826 { ".plt", 4, 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
1827 { NULL, 0, 0, 0, 0 }
1828 };
1829
1830 static struct bfd_elf_special_section const
1831 ppc_special_sections_s[] =
1832 {
1833 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1834 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1835 { ".sdata2", 7, -2, SHT_PROGBITS, SHF_ALLOC },
1836 { ".sbss2", 6, -2, SHT_PROGBITS, SHF_ALLOC },
1837 { NULL, 0, 0, 0, 0 }
1838 };
1839
1840 static struct bfd_elf_special_section const
1841 ppc_special_sections_t[] =
1842 {
1843 { ".tags", 5, 0, SHT_ORDERED, SHF_ALLOC },
1844 { NULL, 0, 0, 0, 0 }
1845 };
1846
1847 static struct bfd_elf_special_section const
1848 ppc_special_sections_other[]=
1849 {
1850 { ".PPC.EMB.apuinfo", 16, 0, SHT_NOTE, 0 },
1851 { ".PPC.EMB.sdata0", 15, 0, SHT_PROGBITS, SHF_ALLOC },
1852 { ".PPC.EMB.sbss0", 14, 0, SHT_PROGBITS, SHF_ALLOC },
1853 { NULL, 0, 0, 0, 0 }
1854 };
1855
1856 static struct bfd_elf_special_section const *
1857 ppc_elf_special_sections[27]=
1858 {
1859 NULL, /* 'a' */
1860 NULL, /* 'b' */
1861 NULL, /* 'c' */
1862 NULL, /* 'd' */
1863 NULL, /* 'e' */
1864 NULL, /* 'f' */
1865 NULL, /* 'g' */
1866 NULL, /* 'h' */
1867 NULL, /* 'i' */
1868 NULL, /* 'j' */
1869 NULL, /* 'k' */
1870 NULL, /* 'l' */
1871 NULL, /* 'm' */
1872 NULL, /* 'n' */
1873 NULL, /* 'o' */
1874 ppc_special_sections_p, /* 'p' */
1875 NULL, /* 'q' */
1876 NULL, /* 'r' */
1877 ppc_special_sections_s, /* 's' */
1878 ppc_special_sections_t, /* 's' */
1879 NULL, /* 'u' */
1880 NULL, /* 'v' */
1881 NULL, /* 'w' */
1882 NULL, /* 'x' */
1883 NULL, /* 'y' */
1884 NULL, /* 'z' */
1885 ppc_special_sections_other, /* other */
1886 };
1887 \f
1888 /* Very simple linked list structure for recording apuinfo values. */
1889 typedef struct apuinfo_list
1890 {
1891 struct apuinfo_list *next;
1892 unsigned long value;
1893 }
1894 apuinfo_list;
1895
1896 static apuinfo_list *head;
1897
1898
1899 static void
1900 apuinfo_list_init (void)
1901 {
1902 head = NULL;
1903 }
1904
1905 static void
1906 apuinfo_list_add (unsigned long value)
1907 {
1908 apuinfo_list *entry = head;
1909
1910 while (entry != NULL)
1911 {
1912 if (entry->value == value)
1913 return;
1914 entry = entry->next;
1915 }
1916
1917 entry = bfd_malloc (sizeof (* entry));
1918 if (entry == NULL)
1919 return;
1920
1921 entry->value = value;
1922 entry->next = head;
1923 head = entry;
1924 }
1925
1926 static unsigned
1927 apuinfo_list_length (void)
1928 {
1929 apuinfo_list *entry;
1930 unsigned long count;
1931
1932 for (entry = head, count = 0;
1933 entry;
1934 entry = entry->next)
1935 ++ count;
1936
1937 return count;
1938 }
1939
1940 static inline unsigned long
1941 apuinfo_list_element (unsigned long number)
1942 {
1943 apuinfo_list * entry;
1944
1945 for (entry = head;
1946 entry && number --;
1947 entry = entry->next)
1948 ;
1949
1950 return entry ? entry->value : 0;
1951 }
1952
1953 static void
1954 apuinfo_list_finish (void)
1955 {
1956 apuinfo_list *entry;
1957
1958 for (entry = head; entry;)
1959 {
1960 apuinfo_list *next = entry->next;
1961 free (entry);
1962 entry = next;
1963 }
1964
1965 head = NULL;
1966 }
1967
1968 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
1969 #define APUINFO_LABEL "APUinfo"
1970
1971 /* Scan the input BFDs and create a linked list of
1972 the APUinfo values that will need to be emitted. */
1973
1974 static void
1975 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
1976 {
1977 bfd *ibfd;
1978 asection *asec;
1979 char *buffer;
1980 unsigned num_input_sections;
1981 bfd_size_type output_section_size;
1982 unsigned i;
1983 unsigned num_entries;
1984 unsigned long offset;
1985 unsigned long length;
1986 const char *error_message = NULL;
1987
1988 if (link_info == NULL)
1989 return;
1990
1991 /* Scan the input bfds, looking for apuinfo sections. */
1992 num_input_sections = 0;
1993 output_section_size = 0;
1994
1995 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
1996 {
1997 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1998 if (asec)
1999 {
2000 ++ num_input_sections;
2001 output_section_size += asec->size;
2002 }
2003 }
2004
2005 /* We need at least one input sections
2006 in order to make merging worthwhile. */
2007 if (num_input_sections < 1)
2008 return;
2009
2010 /* Just make sure that the output section exists as well. */
2011 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2012 if (asec == NULL)
2013 return;
2014
2015 /* Allocate a buffer for the contents of the input sections. */
2016 buffer = bfd_malloc (output_section_size);
2017 if (buffer == NULL)
2018 return;
2019
2020 offset = 0;
2021 apuinfo_list_init ();
2022
2023 /* Read in the input sections contents. */
2024 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2025 {
2026 unsigned long datum;
2027 char *ptr;
2028
2029 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2030 if (asec == NULL)
2031 continue;
2032
2033 length = asec->size;
2034 if (length < 24)
2035 {
2036 error_message = _("corrupt or empty %s section in %B");
2037 goto fail;
2038 }
2039
2040 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
2041 || (bfd_bread (buffer + offset, length, ibfd) != length))
2042 {
2043 error_message = _("unable to read in %s section from %B");
2044 goto fail;
2045 }
2046
2047 /* Process the contents of the section. */
2048 ptr = buffer + offset;
2049 error_message = _("corrupt %s section in %B");
2050
2051 /* Verify the contents of the header. Note - we have to
2052 extract the values this way in order to allow for a
2053 host whose endian-ness is different from the target. */
2054 datum = bfd_get_32 (ibfd, ptr);
2055 if (datum != sizeof APUINFO_LABEL)
2056 goto fail;
2057
2058 datum = bfd_get_32 (ibfd, ptr + 8);
2059 if (datum != 0x2)
2060 goto fail;
2061
2062 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
2063 goto fail;
2064
2065 /* Get the number of bytes used for apuinfo entries. */
2066 datum = bfd_get_32 (ibfd, ptr + 4);
2067 if (datum + 20 != length)
2068 goto fail;
2069
2070 /* Make sure that we do not run off the end of the section. */
2071 if (offset + length > output_section_size)
2072 goto fail;
2073
2074 /* Scan the apuinfo section, building a list of apuinfo numbers. */
2075 for (i = 0; i < datum; i += 4)
2076 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
2077
2078 /* Update the offset. */
2079 offset += length;
2080 }
2081
2082 error_message = NULL;
2083
2084 /* Compute the size of the output section. */
2085 num_entries = apuinfo_list_length ();
2086 output_section_size = 20 + num_entries * 4;
2087
2088 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2089
2090 if (! bfd_set_section_size (abfd, asec, output_section_size))
2091 ibfd = abfd,
2092 error_message = _("warning: unable to set size of %s section in %B");
2093
2094 fail:
2095 free (buffer);
2096
2097 if (error_message)
2098 (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
2099 }
2100
2101 /* Prevent the output section from accumulating the input sections'
2102 contents. We have already stored this in our linked list structure. */
2103
2104 static bfd_boolean
2105 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
2106 asection *asec,
2107 bfd_byte *contents ATTRIBUTE_UNUSED)
2108 {
2109 return (apuinfo_list_length ()
2110 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
2111 }
2112
2113 /* Finally we can generate the output section. */
2114
2115 static void
2116 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
2117 {
2118 bfd_byte *buffer;
2119 asection *asec;
2120 unsigned i;
2121 unsigned num_entries;
2122 bfd_size_type length;
2123
2124 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2125 if (asec == NULL)
2126 return;
2127
2128 if (apuinfo_list_length () == 0)
2129 return;
2130
2131 length = asec->size;
2132 if (length < 20)
2133 return;
2134
2135 buffer = bfd_malloc (length);
2136 if (buffer == NULL)
2137 {
2138 (*_bfd_error_handler)
2139 (_("failed to allocate space for new APUinfo section."));
2140 return;
2141 }
2142
2143 /* Create the apuinfo header. */
2144 num_entries = apuinfo_list_length ();
2145 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2146 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2147 bfd_put_32 (abfd, 0x2, buffer + 8);
2148 strcpy ((char *) buffer + 12, APUINFO_LABEL);
2149
2150 length = 20;
2151 for (i = 0; i < num_entries; i++)
2152 {
2153 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2154 length += 4;
2155 }
2156
2157 if (length != asec->size)
2158 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2159
2160 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2161 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2162
2163 free (buffer);
2164
2165 apuinfo_list_finish ();
2166 }
2167 \f
2168 /* The following functions are specific to the ELF linker, while
2169 functions above are used generally. They appear in this file more
2170 or less in the order in which they are called. eg.
2171 ppc_elf_check_relocs is called early in the link process,
2172 ppc_elf_finish_dynamic_sections is one of the last functions
2173 called. */
2174
2175 /* The PPC linker needs to keep track of the number of relocs that it
2176 decides to copy as dynamic relocs in check_relocs for each symbol.
2177 This is so that it can later discard them if they are found to be
2178 unnecessary. We store the information in a field extending the
2179 regular ELF linker hash table. */
2180
2181 struct ppc_elf_dyn_relocs
2182 {
2183 struct ppc_elf_dyn_relocs *next;
2184
2185 /* The input section of the reloc. */
2186 asection *sec;
2187
2188 /* Total number of relocs copied for the input section. */
2189 bfd_size_type count;
2190
2191 /* Number of pc-relative relocs copied for the input section. */
2192 bfd_size_type pc_count;
2193 };
2194
2195 /* Of those relocs that might be copied as dynamic relocs, this macro
2196 selects those that must be copied when linking a shared library,
2197 even when the symbol is local. */
2198
2199 #define MUST_BE_DYN_RELOC(RTYPE) \
2200 ((RTYPE) != R_PPC_REL24 \
2201 && (RTYPE) != R_PPC_REL14 \
2202 && (RTYPE) != R_PPC_REL14_BRTAKEN \
2203 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
2204 && (RTYPE) != R_PPC_REL32)
2205
2206 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2207 copying dynamic variables from a shared lib into an app's dynbss
2208 section, and instead use a dynamic relocation to point into the
2209 shared lib. */
2210 #define ELIMINATE_COPY_RELOCS 1
2211
2212 /* PPC ELF linker hash entry. */
2213
2214 struct ppc_elf_link_hash_entry
2215 {
2216 struct elf_link_hash_entry elf;
2217
2218 /* If this symbol is used in the linker created sections, the processor
2219 specific backend uses this field to map the field into the offset
2220 from the beginning of the section. */
2221 elf_linker_section_pointers_t *linker_section_pointer;
2222
2223 /* Track dynamic relocs copied for this symbol. */
2224 struct ppc_elf_dyn_relocs *dyn_relocs;
2225
2226 /* Contexts in which symbol is used in the GOT (or TOC).
2227 TLS_GD .. TLS_TLS bits are or'd into the mask as the
2228 corresponding relocs are encountered during check_relocs.
2229 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2230 indicate the corresponding GOT entry type is not needed. */
2231 #define TLS_GD 1 /* GD reloc. */
2232 #define TLS_LD 2 /* LD reloc. */
2233 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
2234 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2235 #define TLS_TLS 16 /* Any TLS reloc. */
2236 #define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
2237 char tls_mask;
2238 };
2239
2240 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2241
2242 /* PPC ELF linker hash table. */
2243
2244 struct ppc_elf_link_hash_table
2245 {
2246 struct elf_link_hash_table elf;
2247
2248 /* Short-cuts to get to dynamic linker sections. */
2249 asection *got;
2250 asection *relgot;
2251 asection *glink;
2252 asection *plt;
2253 asection *relplt;
2254 asection *dynbss;
2255 asection *relbss;
2256 asection *dynsbss;
2257 asection *relsbss;
2258 elf_linker_section_t sdata[2];
2259 asection *sbss;
2260
2261 /* Shortcut to .__tls_get_addr. */
2262 struct elf_link_hash_entry *tls_get_addr;
2263
2264 /* TLS local dynamic got entry handling. */
2265 union {
2266 bfd_signed_vma refcount;
2267 bfd_vma offset;
2268 } tlsld_got;
2269
2270 /* Offset of PltResolve function in glink. */
2271 bfd_vma glink_pltresolve;
2272
2273 /* Size of reserved GOT entries. */
2274 unsigned int got_header_size;
2275 /* Non-zero if allocating the header left a gap. */
2276 unsigned int got_gap;
2277
2278 /* Whether to use new plt/got layout or not. */
2279 unsigned int new_plt:1;
2280 unsigned int old_plt:1;
2281
2282 /* Small local sym to section mapping cache. */
2283 struct sym_sec_cache sym_sec;
2284 };
2285
2286 /* Get the PPC ELF linker hash table from a link_info structure. */
2287
2288 #define ppc_elf_hash_table(p) \
2289 ((struct ppc_elf_link_hash_table *) (p)->hash)
2290
2291 /* Create an entry in a PPC ELF linker hash table. */
2292
2293 static struct bfd_hash_entry *
2294 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2295 struct bfd_hash_table *table,
2296 const char *string)
2297 {
2298 /* Allocate the structure if it has not already been allocated by a
2299 subclass. */
2300 if (entry == NULL)
2301 {
2302 entry = bfd_hash_allocate (table,
2303 sizeof (struct ppc_elf_link_hash_entry));
2304 if (entry == NULL)
2305 return entry;
2306 }
2307
2308 /* Call the allocation method of the superclass. */
2309 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2310 if (entry != NULL)
2311 {
2312 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2313 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2314 ppc_elf_hash_entry (entry)->tls_mask = 0;
2315 }
2316
2317 return entry;
2318 }
2319
2320 /* Create a PPC ELF linker hash table. */
2321
2322 static struct bfd_link_hash_table *
2323 ppc_elf_link_hash_table_create (bfd *abfd)
2324 {
2325 struct ppc_elf_link_hash_table *ret;
2326
2327 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2328 if (ret == NULL)
2329 return NULL;
2330
2331 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
2332 ppc_elf_link_hash_newfunc))
2333 {
2334 free (ret);
2335 return NULL;
2336 }
2337
2338 ret->sdata[0].name = ".sdata";
2339 ret->sdata[0].sym_name = "_SDA_BASE_";
2340 ret->sdata[0].bss_name = ".sbss";
2341
2342 ret->sdata[1].name = ".sdata2";
2343 ret->sdata[1].sym_name = "_SDA2_BASE_";
2344 ret->sdata[1].bss_name = ".sbss2";
2345
2346 return &ret->elf.root;
2347 }
2348
2349 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
2350
2351 static bfd_boolean
2352 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2353 {
2354 struct ppc_elf_link_hash_table *htab;
2355 asection *s;
2356 flagword flags;
2357
2358 if (!_bfd_elf_create_got_section (abfd, info))
2359 return FALSE;
2360
2361 htab = ppc_elf_hash_table (info);
2362 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2363 if (s == NULL)
2364 abort ();
2365
2366 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2367 | SEC_LINKER_CREATED);
2368 if (!bfd_set_section_flags (abfd, s, flags))
2369 return FALSE;
2370
2371 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2372 | SEC_LINKER_CREATED | SEC_READONLY);
2373 htab->relgot = bfd_make_section_with_flags (abfd, ".rela.got", flags);
2374 if (!htab->relgot
2375 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2376 return FALSE;
2377
2378 return TRUE;
2379 }
2380
2381 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2382 to output sections (just like _bfd_elf_create_dynamic_sections has
2383 to create .dynbss and .rela.bss). */
2384
2385 static bfd_boolean
2386 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2387 {
2388 struct ppc_elf_link_hash_table *htab;
2389 asection *s;
2390 flagword flags;
2391
2392 htab = ppc_elf_hash_table (info);
2393
2394 if (htab->got == NULL
2395 && !ppc_elf_create_got (abfd, info))
2396 return FALSE;
2397
2398 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2399 return FALSE;
2400
2401 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2402 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2403
2404 s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags | SEC_CODE);
2405 htab->glink = s;
2406 if (s == NULL
2407 || !bfd_set_section_alignment (abfd, s, 4))
2408 return FALSE;
2409
2410 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2411 s = bfd_make_section_with_flags (abfd, ".dynsbss",
2412 SEC_ALLOC | SEC_LINKER_CREATED);
2413 htab->dynsbss = s;
2414 if (s == NULL)
2415 return FALSE;
2416
2417 if (! info->shared)
2418 {
2419 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2420 s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
2421 htab->relsbss = s;
2422 if (s == NULL
2423 || ! bfd_set_section_alignment (abfd, s, 2))
2424 return FALSE;
2425 }
2426
2427 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2428 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2429 if (s == NULL)
2430 abort ();
2431
2432 flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
2433 return bfd_set_section_flags (abfd, s, flags);
2434 }
2435
2436 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2437
2438 static void
2439 ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
2440 struct elf_link_hash_entry *dir,
2441 struct elf_link_hash_entry *ind)
2442 {
2443 struct ppc_elf_link_hash_entry *edir, *eind;
2444
2445 edir = (struct ppc_elf_link_hash_entry *) dir;
2446 eind = (struct ppc_elf_link_hash_entry *) ind;
2447
2448 if (eind->dyn_relocs != NULL)
2449 {
2450 if (edir->dyn_relocs != NULL)
2451 {
2452 struct ppc_elf_dyn_relocs **pp;
2453 struct ppc_elf_dyn_relocs *p;
2454
2455 if (ind->root.type == bfd_link_hash_indirect)
2456 abort ();
2457
2458 /* Add reloc counts against the weak sym to the strong sym
2459 list. Merge any entries against the same section. */
2460 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2461 {
2462 struct ppc_elf_dyn_relocs *q;
2463
2464 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2465 if (q->sec == p->sec)
2466 {
2467 q->pc_count += p->pc_count;
2468 q->count += p->count;
2469 *pp = p->next;
2470 break;
2471 }
2472 if (q == NULL)
2473 pp = &p->next;
2474 }
2475 *pp = edir->dyn_relocs;
2476 }
2477
2478 edir->dyn_relocs = eind->dyn_relocs;
2479 eind->dyn_relocs = NULL;
2480 }
2481
2482 edir->tls_mask |= eind->tls_mask;
2483
2484 if (ELIMINATE_COPY_RELOCS
2485 && ind->root.type != bfd_link_hash_indirect
2486 && dir->dynamic_adjusted)
2487 {
2488 /* If called to transfer flags for a weakdef during processing
2489 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
2490 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
2491 dir->ref_dynamic |= ind->ref_dynamic;
2492 dir->ref_regular |= ind->ref_regular;
2493 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
2494 dir->needs_plt |= ind->needs_plt;
2495 }
2496 else
2497 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
2498 }
2499
2500 /* Return 1 if target is one of ours. */
2501
2502 static bfd_boolean
2503 is_ppc_elf_target (const struct bfd_target *targ)
2504 {
2505 extern const bfd_target bfd_elf32_powerpc_vec;
2506 extern const bfd_target bfd_elf32_powerpcle_vec;
2507
2508 return targ == &bfd_elf32_powerpc_vec || targ == &bfd_elf32_powerpcle_vec;
2509 }
2510
2511 /* Hook called by the linker routine which adds symbols from an object
2512 file. We use it to put .comm items in .sbss, and not .bss. */
2513
2514 static bfd_boolean
2515 ppc_elf_add_symbol_hook (bfd *abfd,
2516 struct bfd_link_info *info,
2517 Elf_Internal_Sym *sym,
2518 const char **namep ATTRIBUTE_UNUSED,
2519 flagword *flagsp ATTRIBUTE_UNUSED,
2520 asection **secp,
2521 bfd_vma *valp)
2522 {
2523 if (sym->st_shndx == SHN_COMMON
2524 && !info->relocatable
2525 && sym->st_size <= elf_gp_size (abfd)
2526 && is_ppc_elf_target (info->hash->creator))
2527 {
2528 /* Common symbols less than or equal to -G nn bytes are automatically
2529 put into .sbss. */
2530 struct ppc_elf_link_hash_table *htab;
2531
2532 htab = ppc_elf_hash_table (info);
2533 if (htab->sbss == NULL)
2534 {
2535 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
2536
2537 if (!htab->elf.dynobj)
2538 htab->elf.dynobj = abfd;
2539
2540 htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2541 ".sbss",
2542 flags);
2543 if (htab->sbss == NULL)
2544 return FALSE;
2545 }
2546
2547 *secp = htab->sbss;
2548 *valp = sym->st_size;
2549 }
2550
2551 return TRUE;
2552 }
2553 \f
2554 /* Create a special linker section. */
2555
2556 static bfd_boolean
2557 ppc_elf_create_linker_section (bfd *abfd,
2558 struct bfd_link_info *info,
2559 flagword flags,
2560 elf_linker_section_t *lsect)
2561 {
2562 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2563 asection *s;
2564
2565 flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2566 | SEC_LINKER_CREATED);
2567
2568 /* Record the first bfd that needs the special sections. */
2569 if (!htab->elf.dynobj)
2570 htab->elf.dynobj = abfd;
2571
2572 s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2573 lsect->name,
2574 flags);
2575 if (s == NULL
2576 || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2577 return FALSE;
2578 lsect->section = s;
2579
2580 return TRUE;
2581 }
2582
2583 /* Find a linker generated pointer with a given addend and type. */
2584
2585 static elf_linker_section_pointers_t *
2586 elf_find_pointer_linker_section
2587 (elf_linker_section_pointers_t *linker_pointers,
2588 bfd_vma addend,
2589 elf_linker_section_t *lsect)
2590 {
2591 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2592 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2593 return linker_pointers;
2594
2595 return NULL;
2596 }
2597
2598 /* Allocate a pointer to live in a linker created section. */
2599
2600 static bfd_boolean
2601 elf_create_pointer_linker_section (bfd *abfd,
2602 elf_linker_section_t *lsect,
2603 struct elf_link_hash_entry *h,
2604 const Elf_Internal_Rela *rel)
2605 {
2606 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2607 elf_linker_section_pointers_t *linker_section_ptr;
2608 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2609 bfd_size_type amt;
2610
2611 BFD_ASSERT (lsect != NULL);
2612
2613 /* Is this a global symbol? */
2614 if (h != NULL)
2615 {
2616 struct ppc_elf_link_hash_entry *eh;
2617
2618 /* Has this symbol already been allocated? If so, our work is done. */
2619 eh = (struct ppc_elf_link_hash_entry *) h;
2620 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2621 rel->r_addend,
2622 lsect))
2623 return TRUE;
2624
2625 ptr_linker_section_ptr = &eh->linker_section_pointer;
2626 }
2627 else
2628 {
2629 /* Allocation of a pointer to a local symbol. */
2630 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2631
2632 /* Allocate a table to hold the local symbols if first time. */
2633 if (!ptr)
2634 {
2635 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2636
2637 amt = num_symbols;
2638 amt *= sizeof (elf_linker_section_pointers_t *);
2639 ptr = bfd_zalloc (abfd, amt);
2640
2641 if (!ptr)
2642 return FALSE;
2643
2644 elf_local_ptr_offsets (abfd) = ptr;
2645 }
2646
2647 /* Has this symbol already been allocated? If so, our work is done. */
2648 if (elf_find_pointer_linker_section (ptr[r_symndx],
2649 rel->r_addend,
2650 lsect))
2651 return TRUE;
2652
2653 ptr_linker_section_ptr = &ptr[r_symndx];
2654 }
2655
2656 /* Allocate space for a pointer in the linker section, and allocate
2657 a new pointer record from internal memory. */
2658 BFD_ASSERT (ptr_linker_section_ptr != NULL);
2659 amt = sizeof (elf_linker_section_pointers_t);
2660 linker_section_ptr = bfd_alloc (abfd, amt);
2661
2662 if (!linker_section_ptr)
2663 return FALSE;
2664
2665 linker_section_ptr->next = *ptr_linker_section_ptr;
2666 linker_section_ptr->addend = rel->r_addend;
2667 linker_section_ptr->lsect = lsect;
2668 *ptr_linker_section_ptr = linker_section_ptr;
2669
2670 linker_section_ptr->offset = lsect->section->size;
2671 lsect->section->size += 4;
2672
2673 #ifdef DEBUG
2674 fprintf (stderr,
2675 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2676 lsect->name, (long) linker_section_ptr->offset,
2677 (long) lsect->section->size);
2678 #endif
2679
2680 return TRUE;
2681 }
2682
2683 static bfd_boolean
2684 update_local_sym_info (bfd *abfd,
2685 Elf_Internal_Shdr *symtab_hdr,
2686 unsigned long r_symndx,
2687 int tls_type)
2688 {
2689 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
2690 char *local_got_tls_masks;
2691
2692 if (local_got_refcounts == NULL)
2693 {
2694 bfd_size_type size = symtab_hdr->sh_info;
2695
2696 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
2697 local_got_refcounts = bfd_zalloc (abfd, size);
2698 if (local_got_refcounts == NULL)
2699 return FALSE;
2700 elf_local_got_refcounts (abfd) = local_got_refcounts;
2701 }
2702
2703 local_got_refcounts[r_symndx] += 1;
2704 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2705 local_got_tls_masks[r_symndx] |= tls_type;
2706 return TRUE;
2707 }
2708
2709 static void
2710 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
2711 {
2712 (*_bfd_error_handler)
2713 (_("%B: relocation %s cannot be used when making a shared object"),
2714 abfd,
2715 ppc_elf_howto_table[r_type]->name);
2716 bfd_set_error (bfd_error_bad_value);
2717 }
2718
2719 /* Look through the relocs for a section during the first phase, and
2720 allocate space in the global offset table or procedure linkage
2721 table. */
2722
2723 static bfd_boolean
2724 ppc_elf_check_relocs (bfd *abfd,
2725 struct bfd_link_info *info,
2726 asection *sec,
2727 const Elf_Internal_Rela *relocs)
2728 {
2729 struct ppc_elf_link_hash_table *htab;
2730 Elf_Internal_Shdr *symtab_hdr;
2731 struct elf_link_hash_entry **sym_hashes;
2732 const Elf_Internal_Rela *rel;
2733 const Elf_Internal_Rela *rel_end;
2734 asection *sreloc;
2735
2736 if (info->relocatable)
2737 return TRUE;
2738
2739 /* Don't do anything special with non-loaded, non-alloced sections.
2740 In particular, any relocs in such sections should not affect GOT
2741 and PLT reference counting (ie. we don't allow them to create GOT
2742 or PLT entries), there's no possibility or desire to optimize TLS
2743 relocs, and there's not much point in propagating relocs to shared
2744 libs that the dynamic linker won't relocate. */
2745 if ((sec->flags & SEC_ALLOC) == 0)
2746 return TRUE;
2747
2748 #ifdef DEBUG
2749 _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
2750 sec, abfd);
2751 #endif
2752
2753 /* Initialize howto table if not already done. */
2754 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2755 ppc_elf_howto_init ();
2756
2757 htab = ppc_elf_hash_table (info);
2758 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2759 sym_hashes = elf_sym_hashes (abfd);
2760 sreloc = NULL;
2761
2762 rel_end = relocs + sec->reloc_count;
2763 for (rel = relocs; rel < rel_end; rel++)
2764 {
2765 unsigned long r_symndx;
2766 enum elf_ppc_reloc_type r_type;
2767 struct elf_link_hash_entry *h;
2768 int tls_type = 0;
2769
2770 r_symndx = ELF32_R_SYM (rel->r_info);
2771 if (r_symndx < symtab_hdr->sh_info)
2772 h = NULL;
2773 else
2774 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2775
2776 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2777 This shows up in particular in an R_PPC_ADDR32 in the eabi
2778 startup code. */
2779 if (h != NULL
2780 && htab->got == NULL
2781 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2782 {
2783 if (htab->elf.dynobj == NULL)
2784 htab->elf.dynobj = abfd;
2785 if (!ppc_elf_create_got (htab->elf.dynobj, info))
2786 return FALSE;
2787 BFD_ASSERT (h == htab->elf.hgot);
2788 }
2789
2790 r_type = ELF32_R_TYPE (rel->r_info);
2791 switch (r_type)
2792 {
2793 case R_PPC_GOT_TLSLD16:
2794 case R_PPC_GOT_TLSLD16_LO:
2795 case R_PPC_GOT_TLSLD16_HI:
2796 case R_PPC_GOT_TLSLD16_HA:
2797 htab->tlsld_got.refcount += 1;
2798 tls_type = TLS_TLS | TLS_LD;
2799 goto dogottls;
2800
2801 case R_PPC_GOT_TLSGD16:
2802 case R_PPC_GOT_TLSGD16_LO:
2803 case R_PPC_GOT_TLSGD16_HI:
2804 case R_PPC_GOT_TLSGD16_HA:
2805 tls_type = TLS_TLS | TLS_GD;
2806 goto dogottls;
2807
2808 case R_PPC_GOT_TPREL16:
2809 case R_PPC_GOT_TPREL16_LO:
2810 case R_PPC_GOT_TPREL16_HI:
2811 case R_PPC_GOT_TPREL16_HA:
2812 if (info->shared)
2813 info->flags |= DF_STATIC_TLS;
2814 tls_type = TLS_TLS | TLS_TPREL;
2815 goto dogottls;
2816
2817 case R_PPC_GOT_DTPREL16:
2818 case R_PPC_GOT_DTPREL16_LO:
2819 case R_PPC_GOT_DTPREL16_HI:
2820 case R_PPC_GOT_DTPREL16_HA:
2821 tls_type = TLS_TLS | TLS_DTPREL;
2822 dogottls:
2823 sec->has_tls_reloc = 1;
2824 /* Fall thru */
2825
2826 /* GOT16 relocations */
2827 case R_PPC_GOT16:
2828 case R_PPC_GOT16_LO:
2829 case R_PPC_GOT16_HI:
2830 case R_PPC_GOT16_HA:
2831 /* This symbol requires a global offset table entry. */
2832 if (htab->got == NULL)
2833 {
2834 if (htab->elf.dynobj == NULL)
2835 htab->elf.dynobj = abfd;
2836 if (!ppc_elf_create_got (htab->elf.dynobj, info))
2837 return FALSE;
2838 }
2839 if (h != NULL)
2840 {
2841 h->got.refcount += 1;
2842 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
2843 }
2844 else
2845 /* This is a global offset table entry for a local symbol. */
2846 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
2847 return FALSE;
2848 break;
2849
2850 /* Indirect .sdata relocation. */
2851 case R_PPC_EMB_SDAI16:
2852 if (info->shared)
2853 {
2854 bad_shared_reloc (abfd, r_type);
2855 return FALSE;
2856 }
2857 if (htab->sdata[0].section == NULL
2858 && !ppc_elf_create_linker_section (abfd, info, 0,
2859 &htab->sdata[0]))
2860 return FALSE;
2861 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
2862 h, rel))
2863 return FALSE;
2864 break;
2865
2866 /* Indirect .sdata2 relocation. */
2867 case R_PPC_EMB_SDA2I16:
2868 if (info->shared)
2869 {
2870 bad_shared_reloc (abfd, r_type);
2871 return FALSE;
2872 }
2873 if (htab->sdata[1].section == NULL
2874 && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
2875 &htab->sdata[1]))
2876 return FALSE;
2877 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
2878 h, rel))
2879 return FALSE;
2880 break;
2881
2882 case R_PPC_SDAREL16:
2883 case R_PPC_EMB_SDA2REL:
2884 case R_PPC_EMB_SDA21:
2885 case R_PPC_EMB_RELSDA:
2886 case R_PPC_EMB_NADDR32:
2887 case R_PPC_EMB_NADDR16:
2888 case R_PPC_EMB_NADDR16_LO:
2889 case R_PPC_EMB_NADDR16_HI:
2890 case R_PPC_EMB_NADDR16_HA:
2891 if (info->shared)
2892 {
2893 bad_shared_reloc (abfd, r_type);
2894 return FALSE;
2895 }
2896 break;
2897
2898 case R_PPC_PLT32:
2899 case R_PPC_PLTREL24:
2900 case R_PPC_PLTREL32:
2901 case R_PPC_PLT16_LO:
2902 case R_PPC_PLT16_HI:
2903 case R_PPC_PLT16_HA:
2904 #ifdef DEBUG
2905 fprintf (stderr, "Reloc requires a PLT entry\n");
2906 #endif
2907 /* This symbol requires a procedure linkage table entry. We
2908 actually build the entry in finish_dynamic_symbol,
2909 because this might be a case of linking PIC code without
2910 linking in any dynamic objects, in which case we don't
2911 need to generate a procedure linkage table after all. */
2912
2913 if (h == NULL)
2914 {
2915 /* It does not make sense to have a procedure linkage
2916 table entry for a local symbol. */
2917 (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
2918 "local symbol"),
2919 abfd,
2920 sec,
2921 (long) rel->r_offset,
2922 ppc_elf_howto_table[r_type]->name);
2923 bfd_set_error (bfd_error_bad_value);
2924 return FALSE;
2925 }
2926
2927 h->needs_plt = 1;
2928 h->plt.refcount++;
2929 break;
2930
2931 /* The following relocations don't need to propagate the
2932 relocation if linking a shared object since they are
2933 section relative. */
2934 case R_PPC_SECTOFF:
2935 case R_PPC_SECTOFF_LO:
2936 case R_PPC_SECTOFF_HI:
2937 case R_PPC_SECTOFF_HA:
2938 case R_PPC_DTPREL16:
2939 case R_PPC_DTPREL16_LO:
2940 case R_PPC_DTPREL16_HI:
2941 case R_PPC_DTPREL16_HA:
2942 case R_PPC_TOC16:
2943 break;
2944
2945 case R_PPC_REL16:
2946 case R_PPC_REL16_LO:
2947 case R_PPC_REL16_HI:
2948 case R_PPC_REL16_HA:
2949 htab->new_plt = 1;
2950 break;
2951
2952 /* This are just markers. */
2953 case R_PPC_TLS:
2954 case R_PPC_EMB_MRKREF:
2955 case R_PPC_NONE:
2956 case R_PPC_max:
2957 break;
2958
2959 /* These should only appear in dynamic objects. */
2960 case R_PPC_COPY:
2961 case R_PPC_GLOB_DAT:
2962 case R_PPC_JMP_SLOT:
2963 case R_PPC_RELATIVE:
2964 break;
2965
2966 /* These aren't handled yet. We'll report an error later. */
2967 case R_PPC_ADDR30:
2968 case R_PPC_EMB_RELSEC16:
2969 case R_PPC_EMB_RELST_LO:
2970 case R_PPC_EMB_RELST_HI:
2971 case R_PPC_EMB_RELST_HA:
2972 case R_PPC_EMB_BIT_FLD:
2973 break;
2974
2975 /* This refers only to functions defined in the shared library. */
2976 case R_PPC_LOCAL24PC:
2977 if (h && h == htab->elf.hgot)
2978 htab->old_plt = 1;
2979 break;
2980
2981 /* This relocation describes the C++ object vtable hierarchy.
2982 Reconstruct it for later use during GC. */
2983 case R_PPC_GNU_VTINHERIT:
2984 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2985 return FALSE;
2986 break;
2987
2988 /* This relocation describes which C++ vtable entries are actually
2989 used. Record for later use during GC. */
2990 case R_PPC_GNU_VTENTRY:
2991 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2992 return FALSE;
2993 break;
2994
2995 /* We shouldn't really be seeing these. */
2996 case R_PPC_TPREL32:
2997 if (info->shared)
2998 info->flags |= DF_STATIC_TLS;
2999 goto dodyn;
3000
3001 /* Nor these. */
3002 case R_PPC_DTPMOD32:
3003 case R_PPC_DTPREL32:
3004 goto dodyn;
3005
3006 case R_PPC_TPREL16:
3007 case R_PPC_TPREL16_LO:
3008 case R_PPC_TPREL16_HI:
3009 case R_PPC_TPREL16_HA:
3010 if (info->shared)
3011 info->flags |= DF_STATIC_TLS;
3012 goto dodyn;
3013
3014 /* When creating a shared object, we must copy these
3015 relocs into the output file. We create a reloc
3016 section in dynobj and make room for the reloc. */
3017 case R_PPC_REL24:
3018 case R_PPC_REL14:
3019 case R_PPC_REL14_BRTAKEN:
3020 case R_PPC_REL14_BRNTAKEN:
3021 case R_PPC_REL32:
3022 if (h == NULL)
3023 break;
3024 if (h == htab->elf.hgot)
3025 {
3026 htab->old_plt = 1;
3027 break;
3028 }
3029 /* fall through */
3030
3031 case R_PPC_ADDR32:
3032 case R_PPC_ADDR24:
3033 case R_PPC_ADDR16:
3034 case R_PPC_ADDR16_LO:
3035 case R_PPC_ADDR16_HI:
3036 case R_PPC_ADDR16_HA:
3037 case R_PPC_ADDR14:
3038 case R_PPC_ADDR14_BRTAKEN:
3039 case R_PPC_ADDR14_BRNTAKEN:
3040 case R_PPC_UADDR32:
3041 case R_PPC_UADDR16:
3042 if (h != NULL && !info->shared)
3043 {
3044 /* We may need a plt entry if the symbol turns out to be
3045 a function defined in a dynamic object. */
3046 h->plt.refcount++;
3047
3048 /* We may need a copy reloc too. */
3049 h->non_got_ref = 1;
3050 }
3051
3052 dodyn:
3053 /* If we are creating a shared library, and this is a reloc
3054 against a global symbol, or a non PC relative reloc
3055 against a local symbol, then we need to copy the reloc
3056 into the shared library. However, if we are linking with
3057 -Bsymbolic, we do not need to copy a reloc against a
3058 global symbol which is defined in an object we are
3059 including in the link (i.e., DEF_REGULAR is set). At
3060 this point we have not seen all the input files, so it is
3061 possible that DEF_REGULAR is not set now but will be set
3062 later (it is never cleared). In case of a weak definition,
3063 DEF_REGULAR may be cleared later by a strong definition in
3064 a shared library. We account for that possibility below by
3065 storing information in the dyn_relocs field of the hash
3066 table entry. A similar situation occurs when creating
3067 shared libraries and symbol visibility changes render the
3068 symbol local.
3069
3070 If on the other hand, we are creating an executable, we
3071 may need to keep relocations for symbols satisfied by a
3072 dynamic library if we manage to avoid copy relocs for the
3073 symbol. */
3074 if ((info->shared
3075 && (MUST_BE_DYN_RELOC (r_type)
3076 || (h != NULL
3077 && (! info->symbolic
3078 || h->root.type == bfd_link_hash_defweak
3079 || !h->def_regular))))
3080 || (ELIMINATE_COPY_RELOCS
3081 && !info->shared
3082 && h != NULL
3083 && (h->root.type == bfd_link_hash_defweak
3084 || !h->def_regular)))
3085 {
3086 struct ppc_elf_dyn_relocs *p;
3087 struct ppc_elf_dyn_relocs **head;
3088
3089 #ifdef DEBUG
3090 fprintf (stderr,
3091 "ppc_elf_check_relocs needs to "
3092 "create relocation for %s\n",
3093 (h && h->root.root.string
3094 ? h->root.root.string : "<unknown>"));
3095 #endif
3096 if (sreloc == NULL)
3097 {
3098 const char *name;
3099
3100 name = (bfd_elf_string_from_elf_section
3101 (abfd,
3102 elf_elfheader (abfd)->e_shstrndx,
3103 elf_section_data (sec)->rel_hdr.sh_name));
3104 if (name == NULL)
3105 return FALSE;
3106
3107 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3108 && strcmp (bfd_get_section_name (abfd, sec),
3109 name + 5) == 0);
3110
3111 if (htab->elf.dynobj == NULL)
3112 htab->elf.dynobj = abfd;
3113 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3114 if (sreloc == NULL)
3115 {
3116 flagword flags;
3117
3118 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3119 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3120 | SEC_ALLOC | SEC_LOAD);
3121 sreloc = bfd_make_section_with_flags (htab->elf.dynobj,
3122 name,
3123 flags);
3124 if (sreloc == NULL
3125 || ! bfd_set_section_alignment (htab->elf.dynobj,
3126 sreloc, 2))
3127 return FALSE;
3128 }
3129 elf_section_data (sec)->sreloc = sreloc;
3130 }
3131
3132 /* If this is a global symbol, we count the number of
3133 relocations we need for this symbol. */
3134 if (h != NULL)
3135 {
3136 head = &ppc_elf_hash_entry (h)->dyn_relocs;
3137 }
3138 else
3139 {
3140 /* Track dynamic relocs needed for local syms too.
3141 We really need local syms available to do this
3142 easily. Oh well. */
3143
3144 asection *s;
3145 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3146 sec, r_symndx);
3147 if (s == NULL)
3148 return FALSE;
3149
3150 head = ((struct ppc_elf_dyn_relocs **)
3151 &elf_section_data (s)->local_dynrel);
3152 }
3153
3154 p = *head;
3155 if (p == NULL || p->sec != sec)
3156 {
3157 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3158 if (p == NULL)
3159 return FALSE;
3160 p->next = *head;
3161 *head = p;
3162 p->sec = sec;
3163 p->count = 0;
3164 p->pc_count = 0;
3165 }
3166
3167 p->count += 1;
3168 if (!MUST_BE_DYN_RELOC (r_type))
3169 p->pc_count += 1;
3170 }
3171
3172 break;
3173 }
3174 }
3175
3176 return TRUE;
3177 }
3178 \f
3179 /* Merge backend specific data from an object file to the output
3180 object file when linking. */
3181
3182 static bfd_boolean
3183 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3184 {
3185 flagword old_flags;
3186 flagword new_flags;
3187 bfd_boolean error;
3188
3189 if (!is_ppc_elf_target (ibfd->xvec)
3190 || !is_ppc_elf_target (obfd->xvec))
3191 return TRUE;
3192
3193 /* Check if we have the same endianess. */
3194 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3195 return FALSE;
3196
3197 new_flags = elf_elfheader (ibfd)->e_flags;
3198 old_flags = elf_elfheader (obfd)->e_flags;
3199 if (!elf_flags_init (obfd))
3200 {
3201 /* First call, no flags set. */
3202 elf_flags_init (obfd) = TRUE;
3203 elf_elfheader (obfd)->e_flags = new_flags;
3204 }
3205
3206 /* Compatible flags are ok. */
3207 else if (new_flags == old_flags)
3208 ;
3209
3210 /* Incompatible flags. */
3211 else
3212 {
3213 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
3214 to be linked with either. */
3215 error = FALSE;
3216 if ((new_flags & EF_PPC_RELOCATABLE) != 0
3217 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
3218 {
3219 error = TRUE;
3220 (*_bfd_error_handler)
3221 (_("%B: compiled with -mrelocatable and linked with "
3222 "modules compiled normally"), ibfd);
3223 }
3224 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
3225 && (old_flags & EF_PPC_RELOCATABLE) != 0)
3226 {
3227 error = TRUE;
3228 (*_bfd_error_handler)
3229 (_("%B: compiled normally and linked with "
3230 "modules compiled with -mrelocatable"), ibfd);
3231 }
3232
3233 /* The output is -mrelocatable-lib iff both the input files are. */
3234 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
3235 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
3236
3237 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
3238 but each input file is either -mrelocatable or -mrelocatable-lib. */
3239 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
3240 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
3241 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
3242 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
3243
3244 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
3245 any module uses it. */
3246 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
3247
3248 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3249 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3250
3251 /* Warn about any other mismatches. */
3252 if (new_flags != old_flags)
3253 {
3254 error = TRUE;
3255 (*_bfd_error_handler)
3256 (_("%B: uses different e_flags (0x%lx) fields "
3257 "than previous modules (0x%lx)"),
3258 ibfd, (long) new_flags, (long) old_flags);
3259 }
3260
3261 if (error)
3262 {
3263 bfd_set_error (bfd_error_bad_value);
3264 return FALSE;
3265 }
3266 }
3267
3268 return TRUE;
3269 }
3270 \f
3271 /* Choose which PLT scheme to use, and set .plt flags appropriately.
3272 Returns -1 on error, 0 for old PLT, 1 for new PLT. */
3273 int
3274 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
3275 struct bfd_link_info *info,
3276 int force_old_plt)
3277 {
3278 struct ppc_elf_link_hash_table *htab;
3279
3280 htab = ppc_elf_hash_table (info);
3281 if (force_old_plt || !htab->new_plt)
3282 htab->old_plt = 1;
3283
3284 if (!htab->old_plt)
3285 {
3286 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3287 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3288
3289 /* The new PLT is a loaded section. */
3290 if (htab->plt != NULL
3291 && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
3292 return -1;
3293
3294 /* The new GOT is not executable. */
3295 if (htab->got != NULL
3296 && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
3297 return -1;
3298 }
3299 else
3300 {
3301 /* Stop an unused .glink section from affecting .text alignment. */
3302 if (htab->glink != NULL
3303 && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
3304 return -1;
3305 }
3306 return !htab->old_plt;
3307 }
3308 \f
3309 /* Return the section that should be marked against GC for a given
3310 relocation. */
3311
3312 static asection *
3313 ppc_elf_gc_mark_hook (asection *sec,
3314 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3315 Elf_Internal_Rela *rel,
3316 struct elf_link_hash_entry *h,
3317 Elf_Internal_Sym *sym)
3318 {
3319 if (h != NULL)
3320 {
3321 switch (ELF32_R_TYPE (rel->r_info))
3322 {
3323 case R_PPC_GNU_VTINHERIT:
3324 case R_PPC_GNU_VTENTRY:
3325 break;
3326
3327 default:
3328 switch (h->root.type)
3329 {
3330 case bfd_link_hash_defined:
3331 case bfd_link_hash_defweak:
3332 return h->root.u.def.section;
3333
3334 case bfd_link_hash_common:
3335 return h->root.u.c.p->section;
3336
3337 default:
3338 break;
3339 }
3340 }
3341 }
3342 else
3343 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3344
3345 return NULL;
3346 }
3347
3348 /* Update the got, plt and dynamic reloc reference counts for the
3349 section being removed. */
3350
3351 static bfd_boolean
3352 ppc_elf_gc_sweep_hook (bfd *abfd,
3353 struct bfd_link_info *info,
3354 asection *sec,
3355 const Elf_Internal_Rela *relocs)
3356 {
3357 struct ppc_elf_link_hash_table *htab;
3358 Elf_Internal_Shdr *symtab_hdr;
3359 struct elf_link_hash_entry **sym_hashes;
3360 bfd_signed_vma *local_got_refcounts;
3361 const Elf_Internal_Rela *rel, *relend;
3362
3363 if ((sec->flags & SEC_ALLOC) == 0)
3364 return TRUE;
3365
3366 elf_section_data (sec)->local_dynrel = NULL;
3367
3368 htab = ppc_elf_hash_table (info);
3369 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3370 sym_hashes = elf_sym_hashes (abfd);
3371 local_got_refcounts = elf_local_got_refcounts (abfd);
3372
3373 relend = relocs + sec->reloc_count;
3374 for (rel = relocs; rel < relend; rel++)
3375 {
3376 unsigned long r_symndx;
3377 enum elf_ppc_reloc_type r_type;
3378 struct elf_link_hash_entry *h = NULL;
3379
3380 r_symndx = ELF32_R_SYM (rel->r_info);
3381 if (r_symndx >= symtab_hdr->sh_info)
3382 {
3383 struct ppc_elf_dyn_relocs **pp, *p;
3384 struct ppc_elf_link_hash_entry *eh;
3385
3386 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3387 while (h->root.type == bfd_link_hash_indirect
3388 || h->root.type == bfd_link_hash_warning)
3389 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3390 eh = (struct ppc_elf_link_hash_entry *) h;
3391
3392 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3393 if (p->sec == sec)
3394 {
3395 /* Everything must go for SEC. */
3396 *pp = p->next;
3397 break;
3398 }
3399 }
3400
3401 r_type = ELF32_R_TYPE (rel->r_info);
3402 switch (r_type)
3403 {
3404 case R_PPC_GOT_TLSLD16:
3405 case R_PPC_GOT_TLSLD16_LO:
3406 case R_PPC_GOT_TLSLD16_HI:
3407 case R_PPC_GOT_TLSLD16_HA:
3408 htab->tlsld_got.refcount -= 1;
3409 /* Fall thru */
3410
3411 case R_PPC_GOT_TLSGD16:
3412 case R_PPC_GOT_TLSGD16_LO:
3413 case R_PPC_GOT_TLSGD16_HI:
3414 case R_PPC_GOT_TLSGD16_HA:
3415 case R_PPC_GOT_TPREL16:
3416 case R_PPC_GOT_TPREL16_LO:
3417 case R_PPC_GOT_TPREL16_HI:
3418 case R_PPC_GOT_TPREL16_HA:
3419 case R_PPC_GOT_DTPREL16:
3420 case R_PPC_GOT_DTPREL16_LO:
3421 case R_PPC_GOT_DTPREL16_HI:
3422 case R_PPC_GOT_DTPREL16_HA:
3423 case R_PPC_GOT16:
3424 case R_PPC_GOT16_LO:
3425 case R_PPC_GOT16_HI:
3426 case R_PPC_GOT16_HA:
3427 if (h != NULL)
3428 {
3429 if (h->got.refcount > 0)
3430 h->got.refcount--;
3431 }
3432 else if (local_got_refcounts != NULL)
3433 {
3434 if (local_got_refcounts[r_symndx] > 0)
3435 local_got_refcounts[r_symndx]--;
3436 }
3437 break;
3438
3439 case R_PPC_REL24:
3440 case R_PPC_REL14:
3441 case R_PPC_REL14_BRTAKEN:
3442 case R_PPC_REL14_BRNTAKEN:
3443 case R_PPC_REL32:
3444 if (h == NULL || h == htab->elf.hgot)
3445 break;
3446 /* Fall thru */
3447
3448 case R_PPC_ADDR32:
3449 case R_PPC_ADDR24:
3450 case R_PPC_ADDR16:
3451 case R_PPC_ADDR16_LO:
3452 case R_PPC_ADDR16_HI:
3453 case R_PPC_ADDR16_HA:
3454 case R_PPC_ADDR14:
3455 case R_PPC_ADDR14_BRTAKEN:
3456 case R_PPC_ADDR14_BRNTAKEN:
3457 case R_PPC_UADDR32:
3458 case R_PPC_UADDR16:
3459 case R_PPC_PLT32:
3460 case R_PPC_PLTREL24:
3461 case R_PPC_PLT16_LO:
3462 case R_PPC_PLT16_HI:
3463 case R_PPC_PLT16_HA:
3464 if (h != NULL)
3465 {
3466 if (h->plt.refcount > 0)
3467 h->plt.refcount--;
3468 }
3469 break;
3470
3471 default:
3472 break;
3473 }
3474 }
3475 return TRUE;
3476 }
3477 \f
3478 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
3479
3480 asection *
3481 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3482 {
3483 struct ppc_elf_link_hash_table *htab;
3484
3485 htab = ppc_elf_hash_table (info);
3486 if (!htab->old_plt
3487 && htab->plt != NULL
3488 && htab->plt->output_section != NULL)
3489 {
3490 elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
3491 elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
3492 }
3493
3494 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3495 FALSE, FALSE, TRUE);
3496 return _bfd_elf_tls_setup (obfd, info);
3497 }
3498
3499 /* Run through all the TLS relocs looking for optimization
3500 opportunities. */
3501
3502 bfd_boolean
3503 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
3504 struct bfd_link_info *info)
3505 {
3506 bfd *ibfd;
3507 asection *sec;
3508 struct ppc_elf_link_hash_table *htab;
3509
3510 if (info->relocatable || info->shared)
3511 return TRUE;
3512
3513 htab = ppc_elf_hash_table (info);
3514 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3515 {
3516 Elf_Internal_Sym *locsyms = NULL;
3517 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3518
3519 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3520 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3521 {
3522 Elf_Internal_Rela *relstart, *rel, *relend;
3523 int expecting_tls_get_addr;
3524
3525 /* Read the relocations. */
3526 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
3527 info->keep_memory);
3528 if (relstart == NULL)
3529 return FALSE;
3530
3531 expecting_tls_get_addr = 0;
3532 relend = relstart + sec->reloc_count;
3533 for (rel = relstart; rel < relend; rel++)
3534 {
3535 enum elf_ppc_reloc_type r_type;
3536 unsigned long r_symndx;
3537 struct elf_link_hash_entry *h = NULL;
3538 char *tls_mask;
3539 char tls_set, tls_clear;
3540 bfd_boolean is_local;
3541
3542 r_symndx = ELF32_R_SYM (rel->r_info);
3543 if (r_symndx >= symtab_hdr->sh_info)
3544 {
3545 struct elf_link_hash_entry **sym_hashes;
3546
3547 sym_hashes = elf_sym_hashes (ibfd);
3548 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3549 while (h->root.type == bfd_link_hash_indirect
3550 || h->root.type == bfd_link_hash_warning)
3551 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3552 }
3553
3554 is_local = FALSE;
3555 if (h == NULL
3556 || !h->def_dynamic)
3557 is_local = TRUE;
3558
3559 r_type = ELF32_R_TYPE (rel->r_info);
3560 switch (r_type)
3561 {
3562 case R_PPC_GOT_TLSLD16:
3563 case R_PPC_GOT_TLSLD16_LO:
3564 case R_PPC_GOT_TLSLD16_HI:
3565 case R_PPC_GOT_TLSLD16_HA:
3566 /* These relocs should never be against a symbol
3567 defined in a shared lib. Leave them alone if
3568 that turns out to be the case. */
3569 expecting_tls_get_addr = 0;
3570 htab->tlsld_got.refcount -= 1;
3571 if (!is_local)
3572 continue;
3573
3574 /* LD -> LE */
3575 tls_set = 0;
3576 tls_clear = TLS_LD;
3577 expecting_tls_get_addr = 1;
3578 break;
3579
3580 case R_PPC_GOT_TLSGD16:
3581 case R_PPC_GOT_TLSGD16_LO:
3582 case R_PPC_GOT_TLSGD16_HI:
3583 case R_PPC_GOT_TLSGD16_HA:
3584 if (is_local)
3585 /* GD -> LE */
3586 tls_set = 0;
3587 else
3588 /* GD -> IE */
3589 tls_set = TLS_TLS | TLS_TPRELGD;
3590 tls_clear = TLS_GD;
3591 expecting_tls_get_addr = 1;
3592 break;
3593
3594 case R_PPC_GOT_TPREL16:
3595 case R_PPC_GOT_TPREL16_LO:
3596 case R_PPC_GOT_TPREL16_HI:
3597 case R_PPC_GOT_TPREL16_HA:
3598 expecting_tls_get_addr = 0;
3599 if (is_local)
3600 {
3601 /* IE -> LE */
3602 tls_set = 0;
3603 tls_clear = TLS_TPREL;
3604 break;
3605 }
3606 else
3607 continue;
3608
3609 case R_PPC_REL14:
3610 case R_PPC_REL14_BRTAKEN:
3611 case R_PPC_REL14_BRNTAKEN:
3612 case R_PPC_REL24:
3613 if (expecting_tls_get_addr
3614 && h != NULL
3615 && h == htab->tls_get_addr)
3616 {
3617 if (h->plt.refcount > 0)
3618 h->plt.refcount -= 1;
3619 }
3620 expecting_tls_get_addr = 0;
3621 continue;
3622
3623 default:
3624 expecting_tls_get_addr = 0;
3625 continue;
3626 }
3627
3628 if (h != NULL)
3629 {
3630 if (tls_set == 0)
3631 {
3632 /* We managed to get rid of a got entry. */
3633 if (h->got.refcount > 0)
3634 h->got.refcount -= 1;
3635 }
3636 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3637 }
3638 else
3639 {
3640 Elf_Internal_Sym *sym;
3641 bfd_signed_vma *lgot_refs;
3642 char *lgot_masks;
3643
3644 if (locsyms == NULL)
3645 {
3646 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3647 if (locsyms == NULL)
3648 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3649 symtab_hdr->sh_info,
3650 0, NULL, NULL, NULL);
3651 if (locsyms == NULL)
3652 {
3653 if (elf_section_data (sec)->relocs != relstart)
3654 free (relstart);
3655 return FALSE;
3656 }
3657 }
3658 sym = locsyms + r_symndx;
3659 lgot_refs = elf_local_got_refcounts (ibfd);
3660 if (lgot_refs == NULL)
3661 abort ();
3662 if (tls_set == 0)
3663 {
3664 /* We managed to get rid of a got entry. */
3665 if (lgot_refs[r_symndx] > 0)
3666 lgot_refs[r_symndx] -= 1;
3667 }
3668 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
3669 tls_mask = &lgot_masks[r_symndx];
3670 }
3671
3672 *tls_mask |= tls_set;
3673 *tls_mask &= ~tls_clear;
3674 }
3675
3676 if (elf_section_data (sec)->relocs != relstart)
3677 free (relstart);
3678 }
3679
3680 if (locsyms != NULL
3681 && (symtab_hdr->contents != (unsigned char *) locsyms))
3682 {
3683 if (!info->keep_memory)
3684 free (locsyms);
3685 else
3686 symtab_hdr->contents = (unsigned char *) locsyms;
3687 }
3688 }
3689 return TRUE;
3690 }
3691 \f
3692 /* Adjust a symbol defined by a dynamic object and referenced by a
3693 regular object. The current definition is in some section of the
3694 dynamic object, but we're not including those sections. We have to
3695 change the definition to something the rest of the link can
3696 understand. */
3697
3698 static bfd_boolean
3699 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3700 struct elf_link_hash_entry *h)
3701 {
3702 struct ppc_elf_link_hash_table *htab;
3703 asection *s;
3704 unsigned int power_of_two;
3705
3706 #ifdef DEBUG
3707 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
3708 h->root.root.string);
3709 #endif
3710
3711 /* Make sure we know what is going on here. */
3712 htab = ppc_elf_hash_table (info);
3713 BFD_ASSERT (htab->elf.dynobj != NULL
3714 && (h->needs_plt
3715 || h->u.weakdef != NULL
3716 || (h->def_dynamic
3717 && h->ref_regular
3718 && !h->def_regular)));
3719
3720 /* Deal with function syms. */
3721 if (h->type == STT_FUNC
3722 || h->needs_plt)
3723 {
3724 /* Clear procedure linkage table information for any symbol that
3725 won't need a .plt entry. */
3726 if (h->plt.refcount <= 0
3727 || SYMBOL_CALLS_LOCAL (info, h)
3728 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3729 && h->root.type == bfd_link_hash_undefweak))
3730 {
3731 /* A PLT entry is not required/allowed when:
3732
3733 1. We are not using ld.so; because then the PLT entry
3734 can't be set up, so we can't use one. In this case,
3735 ppc_elf_adjust_dynamic_symbol won't even be called.
3736
3737 2. GC has rendered the entry unused.
3738
3739 3. We know for certain that a call to this symbol
3740 will go to this object, or will remain undefined. */
3741 h->plt.offset = (bfd_vma) -1;
3742 h->needs_plt = 0;
3743 }
3744 return TRUE;
3745 }
3746 else
3747 h->plt.offset = (bfd_vma) -1;
3748
3749 /* If this is a weak symbol, and there is a real definition, the
3750 processor independent code will have arranged for us to see the
3751 real definition first, and we can just use the same value. */
3752 if (h->u.weakdef != NULL)
3753 {
3754 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3755 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3756 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3757 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3758 if (ELIMINATE_COPY_RELOCS)
3759 h->non_got_ref = h->u.weakdef->non_got_ref;
3760 return TRUE;
3761 }
3762
3763 /* This is a reference to a symbol defined by a dynamic object which
3764 is not a function. */
3765
3766 /* If we are creating a shared library, we must presume that the
3767 only references to the symbol are via the global offset table.
3768 For such cases we need not do anything here; the relocations will
3769 be handled correctly by relocate_section. */
3770 if (info->shared)
3771 return TRUE;
3772
3773 /* If there are no references to this symbol that do not use the
3774 GOT, we don't need to generate a copy reloc. */
3775 if (!h->non_got_ref)
3776 return TRUE;
3777
3778 if (ELIMINATE_COPY_RELOCS)
3779 {
3780 struct ppc_elf_dyn_relocs *p;
3781 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3782 {
3783 s = p->sec->output_section;
3784 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3785 break;
3786 }
3787
3788 /* If we didn't find any dynamic relocs in read-only sections, then
3789 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3790 if (p == NULL)
3791 {
3792 h->non_got_ref = 0;
3793 return TRUE;
3794 }
3795 }
3796
3797 /* We must allocate the symbol in our .dynbss section, which will
3798 become part of the .bss section of the executable. There will be
3799 an entry for this symbol in the .dynsym section. The dynamic
3800 object will contain position independent code, so all references
3801 from the dynamic object to this symbol will go through the global
3802 offset table. The dynamic linker will use the .dynsym entry to
3803 determine the address it must put in the global offset table, so
3804 both the dynamic object and the regular object will refer to the
3805 same memory location for the variable.
3806
3807 Of course, if the symbol is sufficiently small, we must instead
3808 allocate it in .sbss. FIXME: It would be better to do this if and
3809 only if there were actually SDAREL relocs for that symbol. */
3810
3811 if (h->size <= elf_gp_size (htab->elf.dynobj))
3812 s = htab->dynsbss;
3813 else
3814 s = htab->dynbss;
3815 BFD_ASSERT (s != NULL);
3816
3817 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
3818 copy the initial value out of the dynamic object and into the
3819 runtime process image. We need to remember the offset into the
3820 .rela.bss section we are going to use. */
3821 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3822 {
3823 asection *srel;
3824
3825 if (h->size <= elf_gp_size (htab->elf.dynobj))
3826 srel = htab->relsbss;
3827 else
3828 srel = htab->relbss;
3829 BFD_ASSERT (srel != NULL);
3830 srel->size += sizeof (Elf32_External_Rela);
3831 h->needs_copy = 1;
3832 }
3833
3834 /* We need to figure out the alignment required for this symbol. I
3835 have no idea how ELF linkers handle this. */
3836 power_of_two = bfd_log2 (h->size);
3837 if (power_of_two > 4)
3838 power_of_two = 4;
3839
3840 /* Apply the required alignment. */
3841 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3842 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3843 {
3844 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3845 return FALSE;
3846 }
3847
3848 /* Define the symbol as being at this point in the section. */
3849 h->root.u.def.section = s;
3850 h->root.u.def.value = s->size;
3851
3852 /* Increment the section size to make room for the symbol. */
3853 s->size += h->size;
3854
3855 return TRUE;
3856 }
3857 \f
3858 /* Allocate NEED contiguous space in .got, and return the offset.
3859 Handles allocation of the got header when crossing 32k. */
3860
3861 static bfd_vma
3862 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
3863 {
3864 bfd_vma where;
3865 unsigned int max_before_header = 32768;
3866
3867 if (htab->old_plt)
3868 max_before_header = 32764;
3869
3870 if (need <= htab->got_gap)
3871 {
3872 where = max_before_header - htab->got_gap;
3873 htab->got_gap -= need;
3874 }
3875 else
3876 {
3877 if (htab->got->size + need > max_before_header
3878 && htab->got->size <= max_before_header)
3879 {
3880 htab->got_gap = max_before_header - htab->got->size;
3881 htab->got->size = max_before_header + htab->got_header_size;
3882 }
3883 where = htab->got->size;
3884 htab->got->size += need;
3885 }
3886 return where;
3887 }
3888
3889 /* Allocate space in associated reloc sections for dynamic relocs. */
3890
3891 static bfd_boolean
3892 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3893 {
3894 struct bfd_link_info *info = inf;
3895 struct ppc_elf_link_hash_entry *eh;
3896 struct ppc_elf_link_hash_table *htab;
3897 struct ppc_elf_dyn_relocs *p;
3898
3899 if (h->root.type == bfd_link_hash_indirect)
3900 return TRUE;
3901
3902 if (h->root.type == bfd_link_hash_warning)
3903 /* When warning symbols are created, they **replace** the "real"
3904 entry in the hash table, thus we never get to see the real
3905 symbol in a hash traversal. So look at it now. */
3906 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3907
3908 htab = ppc_elf_hash_table (info);
3909 if (htab->elf.dynamic_sections_created
3910 && h->plt.refcount > 0)
3911 {
3912 /* Make sure this symbol is output as a dynamic symbol. */
3913 if (h->dynindx == -1
3914 && !h->forced_local)
3915 {
3916 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3917 return FALSE;
3918 }
3919
3920 if (info->shared
3921 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3922 {
3923 asection *s = htab->plt;
3924
3925 if (!htab->old_plt)
3926 {
3927 h->plt.offset = s->size;
3928 s->size += 4;
3929
3930 s = htab->glink;
3931 if (!info->shared
3932 && !h->def_regular)
3933 {
3934 h->root.u.def.section = s;
3935 h->root.u.def.value = s->size;
3936 }
3937 s->size += GLINK_ENTRY_SIZE;
3938 }
3939 else
3940 {
3941 /* If this is the first .plt entry, make room for the
3942 special first entry. */
3943 if (s->size == 0)
3944 s->size += PLT_INITIAL_ENTRY_SIZE;
3945
3946 /* The PowerPC PLT is actually composed of two parts, the
3947 first part is 2 words (for a load and a jump), and then
3948 there is a remaining word available at the end. */
3949 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3950 + (PLT_SLOT_SIZE
3951 * ((s->size - PLT_INITIAL_ENTRY_SIZE)
3952 / PLT_ENTRY_SIZE)));
3953
3954 /* If this symbol is not defined in a regular file, and we
3955 are not generating a shared library, then set the symbol
3956 to this location in the .plt. This is required to make
3957 function pointers compare as equal between the normal
3958 executable and the shared library. */
3959 if (! info->shared
3960 && !h->def_regular)
3961 {
3962 h->root.u.def.section = s;
3963 h->root.u.def.value = h->plt.offset;
3964 }
3965
3966 /* Make room for this entry. After the 8192nd entry, room
3967 for two entries is allocated. */
3968 s->size += PLT_ENTRY_SIZE;
3969 if ((s->size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3970 > PLT_NUM_SINGLE_ENTRIES)
3971 s->size += PLT_ENTRY_SIZE;
3972 }
3973
3974 /* We also need to make an entry in the .rela.plt section. */
3975 htab->relplt->size += sizeof (Elf32_External_Rela);
3976 }
3977 else
3978 {
3979 h->plt.offset = (bfd_vma) -1;
3980 h->needs_plt = 0;
3981 }
3982 }
3983 else
3984 {
3985 h->plt.offset = (bfd_vma) -1;
3986 h->needs_plt = 0;
3987 }
3988
3989 eh = (struct ppc_elf_link_hash_entry *) h;
3990 if (eh->elf.got.refcount > 0)
3991 {
3992 /* Make sure this symbol is output as a dynamic symbol. */
3993 if (eh->elf.dynindx == -1
3994 && !eh->elf.forced_local)
3995 {
3996 if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
3997 return FALSE;
3998 }
3999
4000 if (eh->tls_mask == (TLS_TLS | TLS_LD)
4001 && !eh->elf.def_dynamic)
4002 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
4003 eh->elf.got.offset = (bfd_vma) -1;
4004 else
4005 {
4006 bfd_boolean dyn;
4007 unsigned int need = 0;
4008 if ((eh->tls_mask & TLS_TLS) != 0)
4009 {
4010 if ((eh->tls_mask & TLS_LD) != 0)
4011 need += 8;
4012 if ((eh->tls_mask & TLS_GD) != 0)
4013 need += 8;
4014 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
4015 need += 4;
4016 if ((eh->tls_mask & TLS_DTPREL) != 0)
4017 need += 4;
4018 }
4019 else
4020 need += 4;
4021 eh->elf.got.offset = allocate_got (htab, need);
4022 dyn = htab->elf.dynamic_sections_created;
4023 if ((info->shared
4024 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
4025 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
4026 || eh->elf.root.type != bfd_link_hash_undefweak))
4027 {
4028 /* All the entries we allocated need relocs.
4029 Except LD only needs one. */
4030 if ((eh->tls_mask & TLS_LD) != 0)
4031 need -= 4;
4032 htab->relgot->size += need * (sizeof (Elf32_External_Rela) / 4);
4033 }
4034 }
4035 }
4036 else
4037 eh->elf.got.offset = (bfd_vma) -1;
4038
4039 if (eh->dyn_relocs == NULL)
4040 return TRUE;
4041
4042 /* In the shared -Bsymbolic case, discard space allocated for
4043 dynamic pc-relative relocs against symbols which turn out to be
4044 defined in regular objects. For the normal shared case, discard
4045 space for relocs that have become local due to symbol visibility
4046 changes. */
4047
4048 if (info->shared)
4049 {
4050 /* Relocs that use pc_count are those that appear on a call insn,
4051 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
4052 generated via assembly. We want calls to protected symbols to
4053 resolve directly to the function rather than going via the plt.
4054 If people want function pointer comparisons to work as expected
4055 then they should avoid writing weird assembly. */
4056 if (SYMBOL_CALLS_LOCAL (info, h))
4057 {
4058 struct ppc_elf_dyn_relocs **pp;
4059
4060 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4061 {
4062 p->count -= p->pc_count;
4063 p->pc_count = 0;
4064 if (p->count == 0)
4065 *pp = p->next;
4066 else
4067 pp = &p->next;
4068 }
4069 }
4070
4071 /* Also discard relocs on undefined weak syms with non-default
4072 visibility. */
4073 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4074 && h->root.type == bfd_link_hash_undefweak)
4075 eh->dyn_relocs = NULL;
4076
4077 /* Make sure undefined weak symbols are output as a dynamic symbol
4078 in PIEs. */
4079 if (info->pie
4080 && eh->dyn_relocs != NULL
4081 && h->dynindx == -1
4082 && h->root.type == bfd_link_hash_undefweak
4083 && !h->forced_local)
4084 {
4085 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4086 return FALSE;
4087 }
4088 }
4089 else if (ELIMINATE_COPY_RELOCS)
4090 {
4091 /* For the non-shared case, discard space for relocs against
4092 symbols which turn out to need copy relocs or are not
4093 dynamic. */
4094
4095 if (!h->non_got_ref
4096 && h->def_dynamic
4097 && !h->def_regular)
4098 {
4099 /* Make sure this symbol is output as a dynamic symbol.
4100 Undefined weak syms won't yet be marked as dynamic. */
4101 if (h->dynindx == -1
4102 && !h->forced_local)
4103 {
4104 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4105 return FALSE;
4106 }
4107
4108 /* If that succeeded, we know we'll be keeping all the
4109 relocs. */
4110 if (h->dynindx != -1)
4111 goto keep;
4112 }
4113
4114 eh->dyn_relocs = NULL;
4115
4116 keep: ;
4117 }
4118
4119 /* Finally, allocate space. */
4120 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4121 {
4122 asection *sreloc = elf_section_data (p->sec)->sreloc;
4123 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4124 }
4125
4126 return TRUE;
4127 }
4128
4129 /* Find any dynamic relocs that apply to read-only sections. */
4130
4131 static bfd_boolean
4132 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
4133 {
4134 struct ppc_elf_dyn_relocs *p;
4135
4136 if (h->root.type == bfd_link_hash_indirect)
4137 return TRUE;
4138
4139 if (h->root.type == bfd_link_hash_warning)
4140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4141
4142 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4143 {
4144 asection *s = p->sec->output_section;
4145
4146 if (s != NULL
4147 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
4148 == (SEC_READONLY | SEC_ALLOC)))
4149 {
4150 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
4151
4152 /* Not an error, just cut short the traversal. */
4153 return FALSE;
4154 }
4155 }
4156 return TRUE;
4157 }
4158
4159 /* Set the sizes of the dynamic sections. */
4160
4161 static bfd_boolean
4162 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4163 struct bfd_link_info *info)
4164 {
4165 struct ppc_elf_link_hash_table *htab;
4166 asection *s;
4167 bfd_boolean relocs;
4168 bfd *ibfd;
4169
4170 #ifdef DEBUG
4171 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
4172 #endif
4173
4174 htab = ppc_elf_hash_table (info);
4175 BFD_ASSERT (htab->elf.dynobj != NULL);
4176
4177 if (elf_hash_table (info)->dynamic_sections_created)
4178 {
4179 /* Set the contents of the .interp section to the interpreter. */
4180 if (info->executable)
4181 {
4182 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
4183 BFD_ASSERT (s != NULL);
4184 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4185 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4186 }
4187 }
4188
4189 if (htab->old_plt)
4190 htab->got_header_size = 16;
4191 else
4192 htab->got_header_size = 12;
4193
4194 /* Set up .got offsets for local syms, and space for local dynamic
4195 relocs. */
4196 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4197 {
4198 bfd_signed_vma *local_got;
4199 bfd_signed_vma *end_local_got;
4200 char *lgot_masks;
4201 bfd_size_type locsymcount;
4202 Elf_Internal_Shdr *symtab_hdr;
4203
4204 if (!is_ppc_elf_target (ibfd->xvec))
4205 continue;
4206
4207 for (s = ibfd->sections; s != NULL; s = s->next)
4208 {
4209 struct ppc_elf_dyn_relocs *p;
4210
4211 for (p = ((struct ppc_elf_dyn_relocs *)
4212 elf_section_data (s)->local_dynrel);
4213 p != NULL;
4214 p = p->next)
4215 {
4216 if (!bfd_is_abs_section (p->sec)
4217 && bfd_is_abs_section (p->sec->output_section))
4218 {
4219 /* Input section has been discarded, either because
4220 it is a copy of a linkonce section or due to
4221 linker script /DISCARD/, so we'll be discarding
4222 the relocs too. */
4223 }
4224 else if (p->count != 0)
4225 {
4226 elf_section_data (p->sec)->sreloc->size
4227 += p->count * sizeof (Elf32_External_Rela);
4228 if ((p->sec->output_section->flags
4229 & (SEC_READONLY | SEC_ALLOC))
4230 == (SEC_READONLY | SEC_ALLOC))
4231 info->flags |= DF_TEXTREL;
4232 }
4233 }
4234 }
4235
4236 local_got = elf_local_got_refcounts (ibfd);
4237 if (!local_got)
4238 continue;
4239
4240 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4241 locsymcount = symtab_hdr->sh_info;
4242 end_local_got = local_got + locsymcount;
4243 lgot_masks = (char *) end_local_got;
4244 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
4245 if (*local_got > 0)
4246 {
4247 if (*lgot_masks == (TLS_TLS | TLS_LD))
4248 {
4249 /* If just an LD reloc, we'll just use
4250 htab->tlsld_got.offset. */
4251 htab->tlsld_got.refcount += 1;
4252 *local_got = (bfd_vma) -1;
4253 }
4254 else
4255 {
4256 unsigned int need = 0;
4257 if ((*lgot_masks & TLS_TLS) != 0)
4258 {
4259 if ((*lgot_masks & TLS_GD) != 0)
4260 need += 8;
4261 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
4262 need += 4;
4263 if ((*lgot_masks & TLS_DTPREL) != 0)
4264 need += 4;
4265 }
4266 else
4267 need += 4;
4268 *local_got = allocate_got (htab, need);
4269 if (info->shared)
4270 htab->relgot->size += (need
4271 * (sizeof (Elf32_External_Rela) / 4));
4272 }
4273 }
4274 else
4275 *local_got = (bfd_vma) -1;
4276 }
4277
4278 if (htab->tlsld_got.refcount > 0)
4279 {
4280 htab->tlsld_got.offset = allocate_got (htab, 8);
4281 if (info->shared)
4282 htab->relgot->size += sizeof (Elf32_External_Rela);
4283 }
4284 else
4285 htab->tlsld_got.offset = (bfd_vma) -1;
4286
4287 /* Allocate space for global sym dynamic relocs. */
4288 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
4289
4290 if (htab->got != NULL)
4291 {
4292 unsigned int g_o_t = 32768;
4293
4294 /* If we haven't allocated the header, do so now. */
4295 if (htab->got->size <= 32768)
4296 {
4297 g_o_t = htab->got->size;
4298 htab->got->size += htab->got_header_size;
4299 }
4300 if (htab->old_plt)
4301 g_o_t += 4;
4302
4303 htab->elf.hgot->root.u.def.value = g_o_t;
4304 }
4305
4306 if (htab->glink != NULL && htab->glink->size != 0)
4307 {
4308 htab->glink_pltresolve = htab->glink->size;
4309 /* Space for the branch table. */
4310 htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
4311 /* Pad out to align the start of PLTresolve. */
4312 htab->glink->size += -htab->glink->size & 15;
4313 htab->glink->size += GLINK_PLTRESOLVE;
4314 }
4315
4316 /* We've now determined the sizes of the various dynamic sections.
4317 Allocate memory for them. */
4318 relocs = FALSE;
4319 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
4320 {
4321 if ((s->flags & SEC_LINKER_CREATED) == 0)
4322 continue;
4323
4324 if (s == htab->plt
4325 || s == htab->glink
4326 || s == htab->got
4327 || s == htab->sbss)
4328 {
4329 /* Strip this section if we don't need it; see the
4330 comment below. */
4331 }
4332 else if (s == htab->sdata[0].section
4333 || s == htab->sdata[1].section)
4334 {
4335 /* Strip these too. */
4336 }
4337 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4338 {
4339 if (s->size == 0)
4340 {
4341 /* If we don't need this section, strip it from the
4342 output file. This is mostly to handle .rela.bss and
4343 .rela.plt. We must create both sections in
4344 create_dynamic_sections, because they must be created
4345 before the linker maps input sections to output
4346 sections. The linker does that before
4347 adjust_dynamic_symbol is called, and it is that
4348 function which decides whether anything needs to go
4349 into these sections. */
4350 }
4351 else
4352 {
4353 /* Remember whether there are any relocation sections. */
4354 relocs = TRUE;
4355
4356 /* We use the reloc_count field as a counter if we need
4357 to copy relocs into the output file. */
4358 s->reloc_count = 0;
4359 }
4360 }
4361 else
4362 {
4363 /* It's not one of our sections, so don't allocate space. */
4364 continue;
4365 }
4366
4367 if (s->size == 0)
4368 {
4369 s->flags |= SEC_EXCLUDE;
4370 continue;
4371 }
4372
4373 if ((s->flags & SEC_HAS_CONTENTS) == 0)
4374 continue;
4375
4376 /* Allocate memory for the section contents. */
4377 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
4378 if (s->contents == NULL)
4379 return FALSE;
4380 }
4381
4382 if (htab->elf.dynamic_sections_created)
4383 {
4384 /* Add some entries to the .dynamic section. We fill in the
4385 values later, in ppc_elf_finish_dynamic_sections, but we
4386 must add the entries now so that we get the correct size for
4387 the .dynamic section. The DT_DEBUG entry is filled in by the
4388 dynamic linker and used by the debugger. */
4389 #define add_dynamic_entry(TAG, VAL) \
4390 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4391
4392 if (info->executable)
4393 {
4394 if (!add_dynamic_entry (DT_DEBUG, 0))
4395 return FALSE;
4396 }
4397
4398 if (htab->plt != NULL && htab->plt->size != 0)
4399 {
4400 if (!add_dynamic_entry (DT_PLTGOT, 0)
4401 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4402 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4403 || !add_dynamic_entry (DT_JMPREL, 0))
4404 return FALSE;
4405 }
4406
4407 if (htab->glink != NULL && htab->glink->size != 0)
4408 {
4409 if (!add_dynamic_entry (DT_PPC_GOT, 0))
4410 return FALSE;
4411 }
4412
4413 if (relocs)
4414 {
4415 if (!add_dynamic_entry (DT_RELA, 0)
4416 || !add_dynamic_entry (DT_RELASZ, 0)
4417 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4418 return FALSE;
4419 }
4420
4421 /* If any dynamic relocs apply to a read-only section, then we
4422 need a DT_TEXTREL entry. */
4423 if ((info->flags & DF_TEXTREL) == 0)
4424 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
4425 info);
4426
4427 if ((info->flags & DF_TEXTREL) != 0)
4428 {
4429 if (!add_dynamic_entry (DT_TEXTREL, 0))
4430 return FALSE;
4431 }
4432 }
4433 #undef add_dynamic_entry
4434
4435 return TRUE;
4436 }
4437 \f
4438 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
4439
4440 static const int shared_stub_entry[] =
4441 {
4442 0x7c0802a6, /* mflr 0 */
4443 0x429f0005, /* bcl 20, 31, .Lxxx */
4444 0x7d6802a6, /* mflr 11 */
4445 0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
4446 0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
4447 0x7c0803a6, /* mtlr 0 */
4448 0x7d6903a6, /* mtctr 11 */
4449 0x4e800420, /* bctr */
4450 };
4451
4452 static const int stub_entry[] =
4453 {
4454 0x3d600000, /* lis 11,xxx@ha */
4455 0x396b0000, /* addi 11,11,xxx@l */
4456 0x7d6903a6, /* mtctr 11 */
4457 0x4e800420, /* bctr */
4458 };
4459
4460 static bfd_boolean
4461 ppc_elf_relax_section (bfd *abfd,
4462 asection *isec,
4463 struct bfd_link_info *link_info,
4464 bfd_boolean *again)
4465 {
4466 struct one_fixup
4467 {
4468 struct one_fixup *next;
4469 asection *tsec;
4470 bfd_vma toff;
4471 bfd_vma trampoff;
4472 };
4473
4474 Elf_Internal_Shdr *symtab_hdr;
4475 bfd_byte *contents = NULL;
4476 Elf_Internal_Sym *isymbuf = NULL;
4477 Elf_Internal_Rela *internal_relocs = NULL;
4478 Elf_Internal_Rela *irel, *irelend;
4479 struct one_fixup *fixups = NULL;
4480 bfd_boolean changed;
4481 struct ppc_elf_link_hash_table *htab;
4482 bfd_size_type trampoff;
4483
4484 *again = FALSE;
4485
4486 /* Nothing to do if there are no relocations, and no need to do
4487 anything with non-alloc sections. */
4488 if ((isec->flags & SEC_ALLOC) == 0
4489 || (isec->flags & SEC_RELOC) == 0
4490 || isec->reloc_count == 0)
4491 return TRUE;
4492
4493 trampoff = (isec->size + 3) & (bfd_vma) -4;
4494 /* Space for a branch around any trampolines. */
4495 trampoff += 4;
4496
4497 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4498
4499 /* Get a copy of the native relocations. */
4500 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
4501 link_info->keep_memory);
4502 if (internal_relocs == NULL)
4503 goto error_return;
4504
4505 htab = ppc_elf_hash_table (link_info);
4506 irelend = internal_relocs + isec->reloc_count;
4507
4508 for (irel = internal_relocs; irel < irelend; irel++)
4509 {
4510 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
4511 bfd_vma symaddr, reladdr, toff, roff;
4512 asection *tsec;
4513 struct one_fixup *f;
4514 size_t insn_offset = 0;
4515 bfd_vma max_branch_offset, val;
4516 bfd_byte *hit_addr;
4517 unsigned long t0;
4518 unsigned char sym_type;
4519
4520 switch (r_type)
4521 {
4522 case R_PPC_REL24:
4523 case R_PPC_LOCAL24PC:
4524 case R_PPC_PLTREL24:
4525 max_branch_offset = 1 << 25;
4526 break;
4527
4528 case R_PPC_REL14:
4529 case R_PPC_REL14_BRTAKEN:
4530 case R_PPC_REL14_BRNTAKEN:
4531 max_branch_offset = 1 << 15;
4532 break;
4533
4534 default:
4535 continue;
4536 }
4537
4538 /* Get the value of the symbol referred to by the reloc. */
4539 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
4540 {
4541 /* A local symbol. */
4542 Elf_Internal_Sym *isym;
4543
4544 /* Read this BFD's local symbols. */
4545 if (isymbuf == NULL)
4546 {
4547 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4548 if (isymbuf == NULL)
4549 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4550 symtab_hdr->sh_info, 0,
4551 NULL, NULL, NULL);
4552 if (isymbuf == 0)
4553 goto error_return;
4554 }
4555 isym = isymbuf + ELF32_R_SYM (irel->r_info);
4556 if (isym->st_shndx == SHN_UNDEF)
4557 continue; /* We can't do anything with undefined symbols. */
4558 else if (isym->st_shndx == SHN_ABS)
4559 tsec = bfd_abs_section_ptr;
4560 else if (isym->st_shndx == SHN_COMMON)
4561 tsec = bfd_com_section_ptr;
4562 else
4563 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4564
4565 toff = isym->st_value;
4566 sym_type = ELF_ST_TYPE (isym->st_info);
4567 }
4568 else
4569 {
4570 /* Global symbol handling. */
4571 unsigned long indx;
4572 struct elf_link_hash_entry *h;
4573
4574 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
4575 h = elf_sym_hashes (abfd)[indx];
4576
4577 while (h->root.type == bfd_link_hash_indirect
4578 || h->root.type == bfd_link_hash_warning)
4579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4580
4581 if (r_type == R_PPC_PLTREL24
4582 && htab->plt != NULL
4583 && h->plt.offset != (bfd_vma) -1)
4584 {
4585 if (!htab->old_plt)
4586 {
4587 tsec = htab->glink;
4588 toff = h->plt.offset * (GLINK_ENTRY_SIZE / 4);
4589 }
4590 else
4591 {
4592 tsec = htab->plt;
4593 toff = h->plt.offset;
4594 }
4595 }
4596 else if (h->root.type == bfd_link_hash_defined
4597 || h->root.type == bfd_link_hash_defweak)
4598 {
4599 tsec = h->root.u.def.section;
4600 toff = h->root.u.def.value;
4601 }
4602 else
4603 continue;
4604
4605 sym_type = h->type;
4606 }
4607
4608 /* If the branch and target are in the same section, you have
4609 no hope of adding stubs. We'll error out later should the
4610 branch overflow. */
4611 if (tsec == isec)
4612 continue;
4613
4614 /* There probably isn't any reason to handle symbols in
4615 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
4616 attribute for a code section, and we are only looking at
4617 branches. However, implement it correctly here as a
4618 reference for other target relax_section functions. */
4619 if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
4620 {
4621 /* At this stage in linking, no SEC_MERGE symbol has been
4622 adjusted, so all references to such symbols need to be
4623 passed through _bfd_merged_section_offset. (Later, in
4624 relocate_section, all SEC_MERGE symbols *except* for
4625 section symbols have been adjusted.)
4626
4627 gas may reduce relocations against symbols in SEC_MERGE
4628 sections to a relocation against the section symbol when
4629 the original addend was zero. When the reloc is against
4630 a section symbol we should include the addend in the
4631 offset passed to _bfd_merged_section_offset, since the
4632 location of interest is the original symbol. On the
4633 other hand, an access to "sym+addend" where "sym" is not
4634 a section symbol should not include the addend; Such an
4635 access is presumed to be an offset from "sym"; The
4636 location of interest is just "sym". */
4637 if (sym_type == STT_SECTION)
4638 toff += irel->r_addend;
4639
4640 toff = _bfd_merged_section_offset (abfd, &tsec,
4641 elf_section_data (tsec)->sec_info,
4642 toff);
4643
4644 if (sym_type != STT_SECTION)
4645 toff += irel->r_addend;
4646 }
4647 else
4648 toff += irel->r_addend;
4649
4650 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
4651
4652 roff = irel->r_offset;
4653 reladdr = isec->output_section->vma + isec->output_offset + roff;
4654
4655 /* If the branch is in range, no need to do anything. */
4656 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
4657 continue;
4658
4659 /* Look for an existing fixup to this address. */
4660 for (f = fixups; f ; f = f->next)
4661 if (f->tsec == tsec && f->toff == toff)
4662 break;
4663
4664 if (f == NULL)
4665 {
4666 size_t size;
4667 unsigned long stub_rtype;
4668
4669 val = trampoff - roff;
4670 if (val >= max_branch_offset)
4671 /* Oh dear, we can't reach a trampoline. Don't try to add
4672 one. We'll report an error later. */
4673 continue;
4674
4675 if (link_info->shared)
4676 {
4677 size = 4 * ARRAY_SIZE (shared_stub_entry);
4678 insn_offset = 12;
4679 stub_rtype = R_PPC_RELAX32PC;
4680 }
4681 else
4682 {
4683 size = 4 * ARRAY_SIZE (stub_entry);
4684 insn_offset = 0;
4685 stub_rtype = R_PPC_RELAX32;
4686 }
4687
4688 if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
4689 != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
4690 abort ();
4691 if (tsec == htab->plt
4692 || tsec == htab->glink)
4693 stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
4694
4695 /* Hijack the old relocation. Since we need two
4696 relocations for this use a "composite" reloc. */
4697 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4698 stub_rtype);
4699 irel->r_offset = trampoff + insn_offset;
4700
4701 /* Record the fixup so we don't do it again this section. */
4702 f = bfd_malloc (sizeof (*f));
4703 f->next = fixups;
4704 f->tsec = tsec;
4705 f->toff = toff;
4706 f->trampoff = trampoff;
4707 fixups = f;
4708
4709 trampoff += size;
4710 }
4711 else
4712 {
4713 val = f->trampoff - roff;
4714 if (val >= max_branch_offset)
4715 continue;
4716
4717 /* Nop out the reloc, since we're finalizing things here. */
4718 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
4719 }
4720
4721 /* Get the section contents. */
4722 if (contents == NULL)
4723 {
4724 /* Get cached copy if it exists. */
4725 if (elf_section_data (isec)->this_hdr.contents != NULL)
4726 contents = elf_section_data (isec)->this_hdr.contents;
4727 else
4728 {
4729 /* Go get them off disk. */
4730 if (!bfd_malloc_and_get_section (abfd, isec, &contents))
4731 goto error_return;
4732 }
4733 }
4734
4735 /* Fix up the existing branch to hit the trampoline. */
4736 hit_addr = contents + roff;
4737 switch (r_type)
4738 {
4739 case R_PPC_REL24:
4740 case R_PPC_LOCAL24PC:
4741 case R_PPC_PLTREL24:
4742 t0 = bfd_get_32 (abfd, hit_addr);
4743 t0 &= ~0x3fffffc;
4744 t0 |= val & 0x3fffffc;
4745 bfd_put_32 (abfd, t0, hit_addr);
4746 break;
4747
4748 case R_PPC_REL14:
4749 case R_PPC_REL14_BRTAKEN:
4750 case R_PPC_REL14_BRNTAKEN:
4751 t0 = bfd_get_32 (abfd, hit_addr);
4752 t0 &= ~0xfffc;
4753 t0 |= val & 0xfffc;
4754 bfd_put_32 (abfd, t0, hit_addr);
4755 break;
4756 }
4757 }
4758
4759 /* Write out the trampolines. */
4760 changed = fixups != NULL;
4761 if (fixups != NULL)
4762 {
4763 const int *stub;
4764 bfd_byte *dest;
4765 bfd_vma val;
4766 int i, size;
4767
4768 do
4769 {
4770 struct one_fixup *f = fixups;
4771 fixups = fixups->next;
4772 free (f);
4773 }
4774 while (fixups);
4775
4776 contents = bfd_realloc (contents, trampoff);
4777 if (contents == NULL)
4778 goto error_return;
4779
4780 isec->size = (isec->size + 3) & (bfd_vma) -4;
4781 /* Branch around the trampolines. */
4782 val = trampoff - isec->size + 0x48000000;
4783 dest = contents + isec->size;
4784 isec->size = trampoff;
4785 bfd_put_32 (abfd, val, dest);
4786 dest += 4;
4787
4788 if (link_info->shared)
4789 {
4790 stub = shared_stub_entry;
4791 size = ARRAY_SIZE (shared_stub_entry);
4792 }
4793 else
4794 {
4795 stub = stub_entry;
4796 size = ARRAY_SIZE (stub_entry);
4797 }
4798
4799 i = 0;
4800 while (dest < contents + trampoff)
4801 {
4802 bfd_put_32 (abfd, stub[i], dest);
4803 i++;
4804 if (i == size)
4805 i = 0;
4806 dest += 4;
4807 }
4808 BFD_ASSERT (i == 0);
4809 }
4810
4811 if (isymbuf != NULL
4812 && symtab_hdr->contents != (unsigned char *) isymbuf)
4813 {
4814 if (! link_info->keep_memory)
4815 free (isymbuf);
4816 else
4817 {
4818 /* Cache the symbols for elf_link_input_bfd. */
4819 symtab_hdr->contents = (unsigned char *) isymbuf;
4820 }
4821 }
4822
4823 if (contents != NULL
4824 && elf_section_data (isec)->this_hdr.contents != contents)
4825 {
4826 if (!changed && !link_info->keep_memory)
4827 free (contents);
4828 else
4829 {
4830 /* Cache the section contents for elf_link_input_bfd. */
4831 elf_section_data (isec)->this_hdr.contents = contents;
4832 }
4833 }
4834
4835 if (elf_section_data (isec)->relocs != internal_relocs)
4836 {
4837 if (!changed)
4838 free (internal_relocs);
4839 else
4840 elf_section_data (isec)->relocs = internal_relocs;
4841 }
4842
4843 *again = changed;
4844 return TRUE;
4845
4846 error_return:
4847 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
4848 free (isymbuf);
4849 if (contents != NULL
4850 && elf_section_data (isec)->this_hdr.contents != contents)
4851 free (contents);
4852 if (internal_relocs != NULL
4853 && elf_section_data (isec)->relocs != internal_relocs)
4854 free (internal_relocs);
4855 return FALSE;
4856 }
4857 \f
4858 /* Set _SDA_BASE_, _SDA2_BASE, and sbss start and end syms. They are
4859 set here rather than via PROVIDE in the default linker script,
4860 because using PROVIDE inside an output section statement results in
4861 unnecessary output sections. Using PROVIDE outside an output section
4862 statement runs the risk of section alignment affecting where the
4863 section starts. */
4864
4865 bfd_boolean
4866 ppc_elf_set_sdata_syms (bfd *obfd, struct bfd_link_info *info)
4867 {
4868 struct ppc_elf_link_hash_table *htab;
4869 unsigned i;
4870 asection *s;
4871 bfd_vma val;
4872
4873 htab = ppc_elf_hash_table (info);
4874
4875 for (i = 0; i < 2; i++)
4876 {
4877 elf_linker_section_t *lsect = &htab->sdata[i];
4878
4879 s = lsect->section;
4880 if (s != NULL)
4881 s = s->output_section;
4882 if (s == NULL)
4883 s = bfd_get_section_by_name (obfd, lsect->name);
4884 if (s == NULL)
4885 s = bfd_get_section_by_name (obfd, lsect->bss_name);
4886
4887 val = 0;
4888 if (s != NULL)
4889 val = s->vma + 32768;
4890 lsect->sym_val = val;
4891
4892 _bfd_elf_provide_symbol (info, lsect->sym_name, val);
4893 }
4894
4895 s = bfd_get_section_by_name (obfd, ".sbss");
4896 val = 0;
4897 if (s != NULL)
4898 val = s->vma;
4899 _bfd_elf_provide_symbol (info, "__sbss_start", val);
4900 _bfd_elf_provide_symbol (info, "___sbss_start", val);
4901 if (s != NULL)
4902 val += s->size;
4903 _bfd_elf_provide_symbol (info, "__sbss_end", val);
4904 _bfd_elf_provide_symbol (info, "___sbss_end", val);
4905 return TRUE;
4906 }
4907 \f
4908 /* Fill in the address for a pointer generated in a linker section. */
4909
4910 static bfd_vma
4911 elf_finish_pointer_linker_section (bfd *input_bfd,
4912 elf_linker_section_t *lsect,
4913 struct elf_link_hash_entry *h,
4914 bfd_vma relocation,
4915 const Elf_Internal_Rela *rel)
4916 {
4917 elf_linker_section_pointers_t *linker_section_ptr;
4918
4919 BFD_ASSERT (lsect != NULL);
4920
4921 if (h != NULL)
4922 {
4923 /* Handle global symbol. */
4924 struct ppc_elf_link_hash_entry *eh;
4925
4926 eh = (struct ppc_elf_link_hash_entry *) h;
4927 BFD_ASSERT (eh->elf.def_regular);
4928 linker_section_ptr = eh->linker_section_pointer;
4929 }
4930 else
4931 {
4932 /* Handle local symbol. */
4933 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
4934
4935 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
4936 linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
4937 }
4938
4939 linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
4940 rel->r_addend,
4941 lsect);
4942 BFD_ASSERT (linker_section_ptr != NULL);
4943
4944 /* Offset will always be a multiple of four, so use the bottom bit
4945 as a "written" flag. */
4946 if ((linker_section_ptr->offset & 1) == 0)
4947 {
4948 bfd_put_32 (lsect->section->owner,
4949 relocation + linker_section_ptr->addend,
4950 lsect->section->contents + linker_section_ptr->offset);
4951 linker_section_ptr->offset += 1;
4952 }
4953
4954 relocation = (lsect->section->output_offset
4955 + linker_section_ptr->offset - 1
4956 - 0x8000);
4957
4958 #ifdef DEBUG
4959 fprintf (stderr,
4960 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
4961 lsect->name, (long) relocation, (long) relocation);
4962 #endif
4963
4964 /* Subtract out the addend, because it will get added back in by the normal
4965 processing. */
4966 return relocation - linker_section_ptr->addend;
4967 }
4968
4969 /* The RELOCATE_SECTION function is called by the ELF backend linker
4970 to handle the relocations for a section.
4971
4972 The relocs are always passed as Rela structures; if the section
4973 actually uses Rel structures, the r_addend field will always be
4974 zero.
4975
4976 This function is responsible for adjust the section contents as
4977 necessary, and (if using Rela relocs and generating a
4978 relocatable output file) adjusting the reloc addend as
4979 necessary.
4980
4981 This function does not have to worry about setting the reloc
4982 address or the reloc symbol index.
4983
4984 LOCAL_SYMS is a pointer to the swapped in local symbols.
4985
4986 LOCAL_SECTIONS is an array giving the section in the input file
4987 corresponding to the st_shndx field of each local symbol.
4988
4989 The global hash table entry for the global symbols can be found
4990 via elf_sym_hashes (input_bfd).
4991
4992 When generating relocatable output, this function must handle
4993 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4994 going to be the section symbol corresponding to the output
4995 section, which means that the addend must be adjusted
4996 accordingly. */
4997
4998 static bfd_boolean
4999 ppc_elf_relocate_section (bfd *output_bfd,
5000 struct bfd_link_info *info,
5001 bfd *input_bfd,
5002 asection *input_section,
5003 bfd_byte *contents,
5004 Elf_Internal_Rela *relocs,
5005 Elf_Internal_Sym *local_syms,
5006 asection **local_sections)
5007 {
5008 Elf_Internal_Shdr *symtab_hdr;
5009 struct elf_link_hash_entry **sym_hashes;
5010 struct ppc_elf_link_hash_table *htab;
5011 Elf_Internal_Rela *rel;
5012 Elf_Internal_Rela *relend;
5013 Elf_Internal_Rela outrel;
5014 bfd_byte *loc;
5015 asection *sreloc = NULL;
5016 bfd_vma *local_got_offsets;
5017 bfd_boolean ret = TRUE;
5018
5019 #ifdef DEBUG
5020 _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
5021 "%ld relocations%s",
5022 input_bfd, input_section,
5023 (long) input_section->reloc_count,
5024 (info->relocatable) ? " (relocatable)" : "");
5025 #endif
5026
5027 if (info->relocatable)
5028 return TRUE;
5029
5030 /* Initialize howto table if not already done. */
5031 if (!ppc_elf_howto_table[R_PPC_ADDR32])
5032 ppc_elf_howto_init ();
5033
5034 htab = ppc_elf_hash_table (info);
5035 local_got_offsets = elf_local_got_offsets (input_bfd);
5036 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5037 sym_hashes = elf_sym_hashes (input_bfd);
5038 rel = relocs;
5039 relend = relocs + input_section->reloc_count;
5040 for (; rel < relend; rel++)
5041 {
5042 enum elf_ppc_reloc_type r_type;
5043 bfd_vma addend;
5044 bfd_reloc_status_type r;
5045 Elf_Internal_Sym *sym;
5046 asection *sec;
5047 struct elf_link_hash_entry *h;
5048 const char *sym_name;
5049 reloc_howto_type *howto;
5050 unsigned long r_symndx;
5051 bfd_vma relocation;
5052 bfd_vma branch_bit, insn, from;
5053 bfd_boolean unresolved_reloc;
5054 bfd_boolean warned;
5055 unsigned int tls_type, tls_mask, tls_gd;
5056
5057 r_type = ELF32_R_TYPE (rel->r_info);
5058 sym = NULL;
5059 sec = NULL;
5060 h = NULL;
5061 unresolved_reloc = FALSE;
5062 warned = FALSE;
5063 r_symndx = ELF32_R_SYM (rel->r_info);
5064
5065 if (r_symndx < symtab_hdr->sh_info)
5066 {
5067 sym = local_syms + r_symndx;
5068 sec = local_sections[r_symndx];
5069 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
5070
5071 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5072 }
5073 else
5074 {
5075 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5076 r_symndx, symtab_hdr, sym_hashes,
5077 h, sec, relocation,
5078 unresolved_reloc, warned);
5079
5080 sym_name = h->root.root.string;
5081 }
5082
5083 /* TLS optimizations. Replace instruction sequences and relocs
5084 based on information we collected in tls_optimize. We edit
5085 RELOCS so that --emit-relocs will output something sensible
5086 for the final instruction stream. */
5087 tls_mask = 0;
5088 tls_gd = 0;
5089 if (IS_PPC_TLS_RELOC (r_type))
5090 {
5091 if (h != NULL)
5092 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
5093 else if (local_got_offsets != NULL)
5094 {
5095 char *lgot_masks;
5096 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
5097 tls_mask = lgot_masks[r_symndx];
5098 }
5099 }
5100
5101 /* Ensure reloc mapping code below stays sane. */
5102 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
5103 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
5104 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
5105 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
5106 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
5107 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
5108 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
5109 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
5110 abort ();
5111 switch (r_type)
5112 {
5113 default:
5114 break;
5115
5116 case R_PPC_GOT_TPREL16:
5117 case R_PPC_GOT_TPREL16_LO:
5118 if (tls_mask != 0
5119 && (tls_mask & TLS_TPREL) == 0)
5120 {
5121 bfd_vma insn;
5122 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
5123 insn &= 31 << 21;
5124 insn |= 0x3c020000; /* addis 0,2,0 */
5125 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
5126 r_type = R_PPC_TPREL16_HA;
5127 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5128 }
5129 break;
5130
5131 case R_PPC_TLS:
5132 if (tls_mask != 0
5133 && (tls_mask & TLS_TPREL) == 0)
5134 {
5135 bfd_vma insn, rtra;
5136 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5137 if ((insn & ((31 << 26) | (31 << 11)))
5138 == ((31 << 26) | (2 << 11)))
5139 rtra = insn & ((1 << 26) - (1 << 16));
5140 else if ((insn & ((31 << 26) | (31 << 16)))
5141 == ((31 << 26) | (2 << 16)))
5142 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
5143 else
5144 abort ();
5145 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
5146 /* add -> addi. */
5147 insn = 14 << 26;
5148 else if ((insn & (31 << 1)) == 23 << 1
5149 && ((insn & (31 << 6)) < 14 << 6
5150 || ((insn & (31 << 6)) >= 16 << 6
5151 && (insn & (31 << 6)) < 24 << 6)))
5152 /* load and store indexed -> dform. */
5153 insn = (32 | ((insn >> 6) & 31)) << 26;
5154 else if ((insn & (31 << 1)) == 21 << 1
5155 && (insn & (0x1a << 6)) == 0)
5156 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
5157 insn = (((58 | ((insn >> 6) & 4)) << 26)
5158 | ((insn >> 6) & 1));
5159 else if ((insn & (31 << 1)) == 21 << 1
5160 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
5161 /* lwax -> lwa. */
5162 insn = (58 << 26) | 2;
5163 else
5164 abort ();
5165 insn |= rtra;
5166 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5167 r_type = R_PPC_TPREL16_LO;
5168 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5169 /* Was PPC_TLS which sits on insn boundary, now
5170 PPC_TPREL16_LO which is at insn+2. */
5171 rel->r_offset += 2;
5172 }
5173 break;
5174
5175 case R_PPC_GOT_TLSGD16_HI:
5176 case R_PPC_GOT_TLSGD16_HA:
5177 tls_gd = TLS_TPRELGD;
5178 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
5179 goto tls_gdld_hi;
5180 break;
5181
5182 case R_PPC_GOT_TLSLD16_HI:
5183 case R_PPC_GOT_TLSLD16_HA:
5184 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
5185 {
5186 tls_gdld_hi:
5187 if ((tls_mask & tls_gd) != 0)
5188 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
5189 + R_PPC_GOT_TPREL16);
5190 else
5191 {
5192 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
5193 rel->r_offset -= 2;
5194 r_type = R_PPC_NONE;
5195 }
5196 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5197 }
5198 break;
5199
5200 case R_PPC_GOT_TLSGD16:
5201 case R_PPC_GOT_TLSGD16_LO:
5202 tls_gd = TLS_TPRELGD;
5203 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
5204 goto tls_get_addr_check;
5205 break;
5206
5207 case R_PPC_GOT_TLSLD16:
5208 case R_PPC_GOT_TLSLD16_LO:
5209 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
5210 {
5211 tls_get_addr_check:
5212 if (rel + 1 < relend)
5213 {
5214 enum elf_ppc_reloc_type r_type2;
5215 unsigned long r_symndx2;
5216 struct elf_link_hash_entry *h2;
5217 bfd_vma insn1, insn2;
5218 bfd_vma offset;
5219
5220 /* The next instruction should be a call to
5221 __tls_get_addr. Peek at the reloc to be sure. */
5222 r_type2 = ELF32_R_TYPE (rel[1].r_info);
5223 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
5224 if (r_symndx2 < symtab_hdr->sh_info
5225 || (r_type2 != R_PPC_REL14
5226 && r_type2 != R_PPC_REL14_BRTAKEN
5227 && r_type2 != R_PPC_REL14_BRNTAKEN
5228 && r_type2 != R_PPC_REL24
5229 && r_type2 != R_PPC_PLTREL24))
5230 break;
5231
5232 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
5233 while (h2->root.type == bfd_link_hash_indirect
5234 || h2->root.type == bfd_link_hash_warning)
5235 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
5236 if (h2 == NULL || h2 != htab->tls_get_addr)
5237 break;
5238
5239 /* OK, it checks out. Replace the call. */
5240 offset = rel[1].r_offset;
5241 insn1 = bfd_get_32 (output_bfd,
5242 contents + rel->r_offset - 2);
5243 if ((tls_mask & tls_gd) != 0)
5244 {
5245 /* IE */
5246 insn1 &= (1 << 26) - 1;
5247 insn1 |= 32 << 26; /* lwz */
5248 insn2 = 0x7c631214; /* add 3,3,2 */
5249 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
5250 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
5251 + R_PPC_GOT_TPREL16);
5252 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5253 }
5254 else
5255 {
5256 /* LE */
5257 insn1 = 0x3c620000; /* addis 3,2,0 */
5258 insn2 = 0x38630000; /* addi 3,3,0 */
5259 if (tls_gd == 0)
5260 {
5261 /* Was an LD reloc. */
5262 r_symndx = 0;
5263 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
5264 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
5265 }
5266 r_type = R_PPC_TPREL16_HA;
5267 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5268 rel[1].r_info = ELF32_R_INFO (r_symndx,
5269 R_PPC_TPREL16_LO);
5270 rel[1].r_offset += 2;
5271 }
5272 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
5273 bfd_put_32 (output_bfd, insn2, contents + offset);
5274 if (tls_gd == 0)
5275 {
5276 /* We changed the symbol on an LD reloc. Start over
5277 in order to get h, sym, sec etc. right. */
5278 rel--;
5279 continue;
5280 }
5281 }
5282 }
5283 break;
5284 }
5285
5286 /* Handle other relocations that tweak non-addend part of insn. */
5287 branch_bit = 0;
5288 switch (r_type)
5289 {
5290 default:
5291 break;
5292
5293 /* Branch taken prediction relocations. */
5294 case R_PPC_ADDR14_BRTAKEN:
5295 case R_PPC_REL14_BRTAKEN:
5296 branch_bit = BRANCH_PREDICT_BIT;
5297 /* Fall thru */
5298
5299 /* Branch not taken prediction relocations. */
5300 case R_PPC_ADDR14_BRNTAKEN:
5301 case R_PPC_REL14_BRNTAKEN:
5302 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5303 insn &= ~BRANCH_PREDICT_BIT;
5304 insn |= branch_bit;
5305
5306 from = (rel->r_offset
5307 + input_section->output_offset
5308 + input_section->output_section->vma);
5309
5310 /* Invert 'y' bit if not the default. */
5311 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
5312 insn ^= BRANCH_PREDICT_BIT;
5313
5314 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5315 break;
5316 }
5317
5318 addend = rel->r_addend;
5319 tls_type = 0;
5320 howto = NULL;
5321 if (r_type < R_PPC_max)
5322 howto = ppc_elf_howto_table[r_type];
5323 switch (r_type)
5324 {
5325 default:
5326 (*_bfd_error_handler)
5327 (_("%B: unknown relocation type %d for symbol %s"),
5328 input_bfd, (int) r_type, sym_name);
5329
5330 bfd_set_error (bfd_error_bad_value);
5331 ret = FALSE;
5332 continue;
5333
5334 case R_PPC_NONE:
5335 case R_PPC_TLS:
5336 case R_PPC_EMB_MRKREF:
5337 case R_PPC_GNU_VTINHERIT:
5338 case R_PPC_GNU_VTENTRY:
5339 continue;
5340
5341 /* GOT16 relocations. Like an ADDR16 using the symbol's
5342 address in the GOT as relocation value instead of the
5343 symbol's value itself. Also, create a GOT entry for the
5344 symbol and put the symbol value there. */
5345 case R_PPC_GOT_TLSGD16:
5346 case R_PPC_GOT_TLSGD16_LO:
5347 case R_PPC_GOT_TLSGD16_HI:
5348 case R_PPC_GOT_TLSGD16_HA:
5349 tls_type = TLS_TLS | TLS_GD;
5350 goto dogot;
5351
5352 case R_PPC_GOT_TLSLD16:
5353 case R_PPC_GOT_TLSLD16_LO:
5354 case R_PPC_GOT_TLSLD16_HI:
5355 case R_PPC_GOT_TLSLD16_HA:
5356 tls_type = TLS_TLS | TLS_LD;
5357 goto dogot;
5358
5359 case R_PPC_GOT_TPREL16:
5360 case R_PPC_GOT_TPREL16_LO:
5361 case R_PPC_GOT_TPREL16_HI:
5362 case R_PPC_GOT_TPREL16_HA:
5363 tls_type = TLS_TLS | TLS_TPREL;
5364 goto dogot;
5365
5366 case R_PPC_GOT_DTPREL16:
5367 case R_PPC_GOT_DTPREL16_LO:
5368 case R_PPC_GOT_DTPREL16_HI:
5369 case R_PPC_GOT_DTPREL16_HA:
5370 tls_type = TLS_TLS | TLS_DTPREL;
5371 goto dogot;
5372
5373 case R_PPC_GOT16:
5374 case R_PPC_GOT16_LO:
5375 case R_PPC_GOT16_HI:
5376 case R_PPC_GOT16_HA:
5377 dogot:
5378 {
5379 /* Relocation is to the entry for this symbol in the global
5380 offset table. */
5381 bfd_vma off;
5382 bfd_vma *offp;
5383 unsigned long indx;
5384
5385 if (htab->got == NULL)
5386 abort ();
5387
5388 indx = 0;
5389 if (tls_type == (TLS_TLS | TLS_LD)
5390 && (h == NULL
5391 || !h->def_dynamic))
5392 offp = &htab->tlsld_got.offset;
5393 else if (h != NULL)
5394 {
5395 bfd_boolean dyn;
5396 dyn = htab->elf.dynamic_sections_created;
5397 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5398 || (info->shared
5399 && SYMBOL_REFERENCES_LOCAL (info, h)))
5400 /* This is actually a static link, or it is a
5401 -Bsymbolic link and the symbol is defined
5402 locally, or the symbol was forced to be local
5403 because of a version file. */
5404 ;
5405 else
5406 {
5407 indx = h->dynindx;
5408 unresolved_reloc = FALSE;
5409 }
5410 offp = &h->got.offset;
5411 }
5412 else
5413 {
5414 if (local_got_offsets == NULL)
5415 abort ();
5416 offp = &local_got_offsets[r_symndx];
5417 }
5418
5419 /* The offset must always be a multiple of 4. We use the
5420 least significant bit to record whether we have already
5421 processed this entry. */
5422 off = *offp;
5423 if ((off & 1) != 0)
5424 off &= ~1;
5425 else
5426 {
5427 unsigned int tls_m = (tls_mask
5428 & (TLS_LD | TLS_GD | TLS_DTPREL
5429 | TLS_TPREL | TLS_TPRELGD));
5430
5431 if (offp == &htab->tlsld_got.offset)
5432 tls_m = TLS_LD;
5433 else if (h == NULL
5434 || !h->def_dynamic)
5435 tls_m &= ~TLS_LD;
5436
5437 /* We might have multiple got entries for this sym.
5438 Initialize them all. */
5439 do
5440 {
5441 int tls_ty = 0;
5442
5443 if ((tls_m & TLS_LD) != 0)
5444 {
5445 tls_ty = TLS_TLS | TLS_LD;
5446 tls_m &= ~TLS_LD;
5447 }
5448 else if ((tls_m & TLS_GD) != 0)
5449 {
5450 tls_ty = TLS_TLS | TLS_GD;
5451 tls_m &= ~TLS_GD;
5452 }
5453 else if ((tls_m & TLS_DTPREL) != 0)
5454 {
5455 tls_ty = TLS_TLS | TLS_DTPREL;
5456 tls_m &= ~TLS_DTPREL;
5457 }
5458 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5459 {
5460 tls_ty = TLS_TLS | TLS_TPREL;
5461 tls_m = 0;
5462 }
5463
5464 /* Generate relocs for the dynamic linker. */
5465 if ((info->shared || indx != 0)
5466 && (h == NULL
5467 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5468 || h->root.type != bfd_link_hash_undefweak))
5469 {
5470 outrel.r_offset = (htab->got->output_section->vma
5471 + htab->got->output_offset
5472 + off);
5473 outrel.r_addend = 0;
5474 if (tls_ty & (TLS_LD | TLS_GD))
5475 {
5476 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
5477 if (tls_ty == (TLS_TLS | TLS_GD))
5478 {
5479 loc = htab->relgot->contents;
5480 loc += (htab->relgot->reloc_count++
5481 * sizeof (Elf32_External_Rela));
5482 bfd_elf32_swap_reloca_out (output_bfd,
5483 &outrel, loc);
5484 outrel.r_offset += 4;
5485 outrel.r_info
5486 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5487 }
5488 }
5489 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5490 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5491 else if (tls_ty == (TLS_TLS | TLS_TPREL))
5492 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5493 else if (indx == 0)
5494 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5495 else
5496 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
5497 if (indx == 0)
5498 {
5499 outrel.r_addend += relocation;
5500 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
5501 outrel.r_addend -= htab->elf.tls_sec->vma;
5502 }
5503 loc = htab->relgot->contents;
5504 loc += (htab->relgot->reloc_count++
5505 * sizeof (Elf32_External_Rela));
5506 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5507 }
5508
5509 /* Init the .got section contents if we're not
5510 emitting a reloc. */
5511 else
5512 {
5513 bfd_vma value = relocation;
5514
5515 if (tls_ty == (TLS_TLS | TLS_LD))
5516 value = 1;
5517 else if (tls_ty != 0)
5518 {
5519 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
5520 if (tls_ty == (TLS_TLS | TLS_TPREL))
5521 value += DTP_OFFSET - TP_OFFSET;
5522
5523 if (tls_ty == (TLS_TLS | TLS_GD))
5524 {
5525 bfd_put_32 (output_bfd, value,
5526 htab->got->contents + off + 4);
5527 value = 1;
5528 }
5529 }
5530 bfd_put_32 (output_bfd, value,
5531 htab->got->contents + off);
5532 }
5533
5534 off += 4;
5535 if (tls_ty & (TLS_LD | TLS_GD))
5536 off += 4;
5537 }
5538 while (tls_m != 0);
5539
5540 off = *offp;
5541 *offp = off | 1;
5542 }
5543
5544 if (off >= (bfd_vma) -2)
5545 abort ();
5546
5547 if ((tls_type & TLS_TLS) != 0)
5548 {
5549 if (tls_type != (TLS_TLS | TLS_LD))
5550 {
5551 if ((tls_mask & TLS_LD) != 0
5552 && !(h == NULL
5553 || !h->def_dynamic))
5554 off += 8;
5555 if (tls_type != (TLS_TLS | TLS_GD))
5556 {
5557 if ((tls_mask & TLS_GD) != 0)
5558 off += 8;
5559 if (tls_type != (TLS_TLS | TLS_DTPREL))
5560 {
5561 if ((tls_mask & TLS_DTPREL) != 0)
5562 off += 4;
5563 }
5564 }
5565 }
5566 }
5567
5568 relocation = htab->got->output_offset + off;
5569 relocation -= htab->elf.hgot->root.u.def.value;
5570
5571 /* Addends on got relocations don't make much sense.
5572 x+off@got is actually x@got+off, and since the got is
5573 generated by a hash table traversal, the value in the
5574 got at entry m+n bears little relation to the entry m. */
5575 if (addend != 0)
5576 (*_bfd_error_handler)
5577 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
5578 input_bfd,
5579 input_section,
5580 (long) rel->r_offset,
5581 howto->name,
5582 sym_name);
5583 }
5584 break;
5585
5586 /* Relocations that need no special processing. */
5587 case R_PPC_LOCAL24PC:
5588 /* It makes no sense to point a local relocation
5589 at a symbol not in this object. */
5590 if (unresolved_reloc)
5591 {
5592 if (! (*info->callbacks->undefined_symbol) (info,
5593 h->root.root.string,
5594 input_bfd,
5595 input_section,
5596 rel->r_offset,
5597 TRUE))
5598 return FALSE;
5599 continue;
5600 }
5601 break;
5602
5603 case R_PPC_DTPREL16:
5604 case R_PPC_DTPREL16_LO:
5605 case R_PPC_DTPREL16_HI:
5606 case R_PPC_DTPREL16_HA:
5607 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5608 break;
5609
5610 /* Relocations that may need to be propagated if this is a shared
5611 object. */
5612 case R_PPC_TPREL16:
5613 case R_PPC_TPREL16_LO:
5614 case R_PPC_TPREL16_HI:
5615 case R_PPC_TPREL16_HA:
5616 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5617 /* The TPREL16 relocs shouldn't really be used in shared
5618 libs as they will result in DT_TEXTREL being set, but
5619 support them anyway. */
5620 goto dodyn;
5621
5622 case R_PPC_TPREL32:
5623 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5624 goto dodyn;
5625
5626 case R_PPC_DTPREL32:
5627 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5628 goto dodyn;
5629
5630 case R_PPC_DTPMOD32:
5631 relocation = 1;
5632 addend = 0;
5633 goto dodyn;
5634
5635 case R_PPC_REL16:
5636 case R_PPC_REL16_LO:
5637 case R_PPC_REL16_HI:
5638 case R_PPC_REL16_HA:
5639 break;
5640
5641 case R_PPC_REL24:
5642 case R_PPC_REL32:
5643 case R_PPC_REL14:
5644 case R_PPC_REL14_BRTAKEN:
5645 case R_PPC_REL14_BRNTAKEN:
5646 /* If these relocations are not to a named symbol, they can be
5647 handled right here, no need to bother the dynamic linker. */
5648 if (SYMBOL_REFERENCES_LOCAL (info, h)
5649 || h == htab->elf.hgot)
5650 break;
5651 /* fall through */
5652
5653 /* Relocations that always need to be propagated if this is a shared
5654 object. */
5655 case R_PPC_ADDR32:
5656 case R_PPC_ADDR24:
5657 case R_PPC_ADDR16:
5658 case R_PPC_ADDR16_LO:
5659 case R_PPC_ADDR16_HI:
5660 case R_PPC_ADDR16_HA:
5661 case R_PPC_ADDR14:
5662 case R_PPC_ADDR14_BRTAKEN:
5663 case R_PPC_ADDR14_BRNTAKEN:
5664 case R_PPC_UADDR32:
5665 case R_PPC_UADDR16:
5666 /* r_symndx will be zero only for relocs against symbols
5667 from removed linkonce sections, or sections discarded by
5668 a linker script. */
5669 dodyn:
5670 if (r_symndx == 0)
5671 break;
5672 /* Fall thru. */
5673
5674 if ((input_section->flags & SEC_ALLOC) == 0)
5675 break;
5676 /* Fall thru. */
5677
5678 if ((info->shared
5679 && (h == NULL
5680 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5681 || h->root.type != bfd_link_hash_undefweak)
5682 && (MUST_BE_DYN_RELOC (r_type)
5683 || !SYMBOL_CALLS_LOCAL (info, h)))
5684 || (ELIMINATE_COPY_RELOCS
5685 && !info->shared
5686 && h != NULL
5687 && h->dynindx != -1
5688 && !h->non_got_ref
5689 && h->def_dynamic
5690 && !h->def_regular))
5691 {
5692 int skip;
5693
5694 #ifdef DEBUG
5695 fprintf (stderr, "ppc_elf_relocate_section needs to "
5696 "create relocation for %s\n",
5697 (h && h->root.root.string
5698 ? h->root.root.string : "<unknown>"));
5699 #endif
5700
5701 /* When generating a shared object, these relocations
5702 are copied into the output file to be resolved at run
5703 time. */
5704 if (sreloc == NULL)
5705 {
5706 const char *name;
5707
5708 name = (bfd_elf_string_from_elf_section
5709 (input_bfd,
5710 elf_elfheader (input_bfd)->e_shstrndx,
5711 elf_section_data (input_section)->rel_hdr.sh_name));
5712 if (name == NULL)
5713 return FALSE;
5714
5715 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5716 && strcmp (bfd_get_section_name (input_bfd,
5717 input_section),
5718 name + 5) == 0);
5719
5720 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5721 BFD_ASSERT (sreloc != NULL);
5722 }
5723
5724 skip = 0;
5725
5726 outrel.r_offset =
5727 _bfd_elf_section_offset (output_bfd, info, input_section,
5728 rel->r_offset);
5729 if (outrel.r_offset == (bfd_vma) -1
5730 || outrel.r_offset == (bfd_vma) -2)
5731 skip = (int) outrel.r_offset;
5732 outrel.r_offset += (input_section->output_section->vma
5733 + input_section->output_offset);
5734
5735 if (skip)
5736 memset (&outrel, 0, sizeof outrel);
5737 else if (!SYMBOL_REFERENCES_LOCAL (info, h))
5738 {
5739 unresolved_reloc = FALSE;
5740 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5741 outrel.r_addend = rel->r_addend;
5742 }
5743 else
5744 {
5745 outrel.r_addend = relocation + rel->r_addend;
5746
5747 if (r_type == R_PPC_ADDR32)
5748 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5749 else
5750 {
5751 long indx;
5752
5753 if (bfd_is_abs_section (sec))
5754 indx = 0;
5755 else if (sec == NULL || sec->owner == NULL)
5756 {
5757 bfd_set_error (bfd_error_bad_value);
5758 return FALSE;
5759 }
5760 else
5761 {
5762 asection *osec;
5763
5764 /* We are turning this relocation into one
5765 against a section symbol. It would be
5766 proper to subtract the symbol's value,
5767 osec->vma, from the emitted reloc addend,
5768 but ld.so expects buggy relocs. */
5769 osec = sec->output_section;
5770 indx = elf_section_data (osec)->dynindx;
5771 BFD_ASSERT (indx > 0);
5772 #ifdef DEBUG
5773 if (indx <= 0)
5774 printf ("indx=%d section=%s flags=%08x name=%s\n",
5775 indx, osec->name, osec->flags,
5776 h->root.root.string);
5777 #endif
5778 }
5779
5780 outrel.r_info = ELF32_R_INFO (indx, r_type);
5781 }
5782 }
5783
5784 loc = sreloc->contents;
5785 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5786 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5787
5788 if (skip == -1)
5789 continue;
5790
5791 /* This reloc will be computed at runtime. We clear the memory
5792 so that it contains predictable value. */
5793 if (! skip
5794 && ((input_section->flags & SEC_ALLOC) != 0
5795 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5796 {
5797 relocation = howto->pc_relative ? outrel.r_offset : 0;
5798 addend = 0;
5799 break;
5800 }
5801 }
5802 break;
5803
5804 case R_PPC_RELAX32PC_PLT:
5805 case R_PPC_RELAX32_PLT:
5806 BFD_ASSERT (h != NULL
5807 && h->plt.offset != (bfd_vma) -1
5808 && htab->plt != NULL);
5809
5810 if (!htab->old_plt)
5811 relocation = (htab->glink->output_section->vma
5812 + htab->glink->output_offset
5813 + h->plt.offset * (GLINK_ENTRY_SIZE / 4));
5814 else
5815 relocation = (htab->plt->output_section->vma
5816 + htab->plt->output_offset
5817 + h->plt.offset);
5818 if (r_type == R_PPC_RELAX32_PLT)
5819 goto relax32;
5820 /* Fall thru */
5821
5822 case R_PPC_RELAX32PC:
5823 relocation -= (input_section->output_section->vma
5824 + input_section->output_offset
5825 + rel->r_offset - 4);
5826 /* Fall thru */
5827
5828 case R_PPC_RELAX32:
5829 relax32:
5830 {
5831 unsigned long t0;
5832 unsigned long t1;
5833
5834 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
5835 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
5836
5837 /* We're clearing the bits for R_PPC_ADDR16_HA
5838 and R_PPC_ADDR16_LO here. */
5839 t0 &= ~0xffff;
5840 t1 &= ~0xffff;
5841
5842 /* t0 is HA, t1 is LO */
5843 relocation += addend;
5844 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
5845 t1 |= relocation & 0xffff;
5846
5847 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
5848 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
5849 }
5850 continue;
5851
5852 /* Indirect .sdata relocation. */
5853 case R_PPC_EMB_SDAI16:
5854 BFD_ASSERT (htab->sdata[0].section != NULL);
5855 relocation
5856 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
5857 h, relocation, rel);
5858 break;
5859
5860 /* Indirect .sdata2 relocation. */
5861 case R_PPC_EMB_SDA2I16:
5862 BFD_ASSERT (htab->sdata[1].section != NULL);
5863 relocation
5864 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
5865 h, relocation, rel);
5866 break;
5867
5868 /* Handle the TOC16 reloc. We want to use the offset within the .got
5869 section, not the actual VMA. This is appropriate when generating
5870 an embedded ELF object, for which the .got section acts like the
5871 AIX .toc section. */
5872 case R_PPC_TOC16: /* phony GOT16 relocations */
5873 BFD_ASSERT (sec != NULL);
5874 BFD_ASSERT (bfd_is_und_section (sec)
5875 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5876 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
5877
5878 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5879 break;
5880
5881 case R_PPC_PLTREL24:
5882 /* Relocation is to the entry for this symbol in the
5883 procedure linkage table. */
5884 BFD_ASSERT (h != NULL);
5885
5886 if (h->plt.offset == (bfd_vma) -1
5887 || htab->plt == NULL)
5888 {
5889 /* We didn't make a PLT entry for this symbol. This
5890 happens when statically linking PIC code, or when
5891 using -Bsymbolic. */
5892 break;
5893 }
5894
5895 unresolved_reloc = FALSE;
5896 if (!htab->old_plt)
5897 relocation = (htab->glink->output_section->vma
5898 + htab->glink->output_offset
5899 + h->plt.offset * (GLINK_ENTRY_SIZE / 4));
5900 else
5901 relocation = (htab->plt->output_section->vma
5902 + htab->plt->output_offset
5903 + h->plt.offset);
5904 break;
5905
5906 /* Relocate against _SDA_BASE_. */
5907 case R_PPC_SDAREL16:
5908 {
5909 const char *name;
5910
5911 BFD_ASSERT (sec != NULL);
5912 name = bfd_get_section_name (abfd, sec->output_section);
5913 if (! ((strncmp (name, ".sdata", 6) == 0
5914 && (name[6] == 0 || name[6] == '.'))
5915 || (strncmp (name, ".sbss", 5) == 0
5916 && (name[5] == 0 || name[5] == '.'))))
5917 {
5918 (*_bfd_error_handler)
5919 (_("%B: the target (%s) of a %s relocation is "
5920 "in the wrong output section (%s)"),
5921 input_bfd,
5922 sym_name,
5923 howto->name,
5924 name);
5925 }
5926 addend -= htab->sdata[0].sym_val;
5927 }
5928 break;
5929
5930 /* Relocate against _SDA2_BASE_. */
5931 case R_PPC_EMB_SDA2REL:
5932 {
5933 const char *name;
5934
5935 BFD_ASSERT (sec != NULL);
5936 name = bfd_get_section_name (abfd, sec->output_section);
5937 if (! (strncmp (name, ".sdata2", 7) == 0
5938 || strncmp (name, ".sbss2", 6) == 0))
5939 {
5940 (*_bfd_error_handler)
5941 (_("%B: the target (%s) of a %s relocation is "
5942 "in the wrong output section (%s)"),
5943 input_bfd,
5944 sym_name,
5945 howto->name,
5946 name);
5947
5948 bfd_set_error (bfd_error_bad_value);
5949 ret = FALSE;
5950 continue;
5951 }
5952 addend -= htab->sdata[1].sym_val;
5953 }
5954 break;
5955
5956 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
5957 case R_PPC_EMB_SDA21:
5958 case R_PPC_EMB_RELSDA:
5959 {
5960 const char *name;
5961 int reg;
5962
5963 BFD_ASSERT (sec != NULL);
5964 name = bfd_get_section_name (abfd, sec->output_section);
5965 if (((strncmp (name, ".sdata", 6) == 0
5966 && (name[6] == 0 || name[6] == '.'))
5967 || (strncmp (name, ".sbss", 5) == 0
5968 && (name[5] == 0 || name[5] == '.'))))
5969 {
5970 reg = 13;
5971 addend -= htab->sdata[0].sym_val;
5972 }
5973
5974 else if (strncmp (name, ".sdata2", 7) == 0
5975 || strncmp (name, ".sbss2", 6) == 0)
5976 {
5977 reg = 2;
5978 addend -= htab->sdata[1].sym_val;
5979 }
5980
5981 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5982 || strcmp (name, ".PPC.EMB.sbss0") == 0)
5983 {
5984 reg = 0;
5985 }
5986
5987 else
5988 {
5989 (*_bfd_error_handler)
5990 (_("%B: the target (%s) of a %s relocation is "
5991 "in the wrong output section (%s)"),
5992 input_bfd,
5993 sym_name,
5994 howto->name,
5995 name);
5996
5997 bfd_set_error (bfd_error_bad_value);
5998 ret = FALSE;
5999 continue;
6000 }
6001
6002 if (r_type == R_PPC_EMB_SDA21)
6003 { /* fill in register field */
6004 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
6005 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
6006 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
6007 }
6008 }
6009 break;
6010
6011 /* Relocate against the beginning of the section. */
6012 case R_PPC_SECTOFF:
6013 case R_PPC_SECTOFF_LO:
6014 case R_PPC_SECTOFF_HI:
6015 case R_PPC_SECTOFF_HA:
6016 BFD_ASSERT (sec != NULL);
6017 addend -= sec->output_section->vma;
6018 break;
6019
6020 /* Negative relocations. */
6021 case R_PPC_EMB_NADDR32:
6022 case R_PPC_EMB_NADDR16:
6023 case R_PPC_EMB_NADDR16_LO:
6024 case R_PPC_EMB_NADDR16_HI:
6025 case R_PPC_EMB_NADDR16_HA:
6026 addend -= 2 * relocation;
6027 break;
6028
6029 case R_PPC_COPY:
6030 case R_PPC_GLOB_DAT:
6031 case R_PPC_JMP_SLOT:
6032 case R_PPC_RELATIVE:
6033 case R_PPC_PLT32:
6034 case R_PPC_PLTREL32:
6035 case R_PPC_PLT16_LO:
6036 case R_PPC_PLT16_HI:
6037 case R_PPC_PLT16_HA:
6038 case R_PPC_ADDR30:
6039 case R_PPC_EMB_RELSEC16:
6040 case R_PPC_EMB_RELST_LO:
6041 case R_PPC_EMB_RELST_HI:
6042 case R_PPC_EMB_RELST_HA:
6043 case R_PPC_EMB_BIT_FLD:
6044 (*_bfd_error_handler)
6045 (_("%B: relocation %s is not yet supported for symbol %s."),
6046 input_bfd,
6047 howto->name,
6048 sym_name);
6049
6050 bfd_set_error (bfd_error_invalid_operation);
6051 ret = FALSE;
6052 continue;
6053 }
6054
6055 /* Do any further special processing. */
6056 switch (r_type)
6057 {
6058 default:
6059 break;
6060
6061 case R_PPC_ADDR16_HA:
6062 case R_PPC_REL16_HA:
6063 case R_PPC_GOT16_HA:
6064 case R_PPC_PLT16_HA:
6065 case R_PPC_SECTOFF_HA:
6066 case R_PPC_TPREL16_HA:
6067 case R_PPC_DTPREL16_HA:
6068 case R_PPC_GOT_TLSGD16_HA:
6069 case R_PPC_GOT_TLSLD16_HA:
6070 case R_PPC_GOT_TPREL16_HA:
6071 case R_PPC_GOT_DTPREL16_HA:
6072 case R_PPC_EMB_NADDR16_HA:
6073 case R_PPC_EMB_RELST_HA:
6074 /* It's just possible that this symbol is a weak symbol
6075 that's not actually defined anywhere. In that case,
6076 'sec' would be NULL, and we should leave the symbol
6077 alone (it will be set to zero elsewhere in the link). */
6078 if (sec != NULL)
6079 /* Add 0x10000 if sign bit in 0:15 is set.
6080 Bits 0:15 are not used. */
6081 addend += 0x8000;
6082 break;
6083 }
6084
6085 #ifdef DEBUG
6086 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
6087 "offset = %ld, addend = %ld\n",
6088 howto->name,
6089 (int) r_type,
6090 sym_name,
6091 r_symndx,
6092 (long) rel->r_offset,
6093 (long) addend);
6094 #endif
6095
6096 if (unresolved_reloc
6097 && !((input_section->flags & SEC_DEBUGGING) != 0
6098 && h->def_dynamic))
6099 {
6100 (*_bfd_error_handler)
6101 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
6102 input_bfd,
6103 input_section,
6104 (long) rel->r_offset,
6105 howto->name,
6106 sym_name);
6107 ret = FALSE;
6108 }
6109
6110 r = _bfd_final_link_relocate (howto,
6111 input_bfd,
6112 input_section,
6113 contents,
6114 rel->r_offset,
6115 relocation,
6116 addend);
6117
6118 if (r != bfd_reloc_ok)
6119 {
6120 if (r == bfd_reloc_overflow)
6121 {
6122 if (warned)
6123 continue;
6124 if (h != NULL
6125 && h->root.type == bfd_link_hash_undefweak
6126 && howto->pc_relative)
6127 {
6128 /* Assume this is a call protected by other code that
6129 detect the symbol is undefined. If this is the case,
6130 we can safely ignore the overflow. If not, the
6131 program is hosed anyway, and a little warning isn't
6132 going to help. */
6133
6134 continue;
6135 }
6136
6137 if (! (*info->callbacks->reloc_overflow) (info,
6138 (h ? &h->root : NULL),
6139 sym_name,
6140 howto->name,
6141 rel->r_addend,
6142 input_bfd,
6143 input_section,
6144 rel->r_offset))
6145 return FALSE;
6146 }
6147 else
6148 {
6149 (*_bfd_error_handler)
6150 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
6151 input_bfd, input_section,
6152 (long) rel->r_offset, howto->name, sym_name, (int) r);
6153 ret = FALSE;
6154 }
6155 }
6156 }
6157
6158 #ifdef DEBUG
6159 fprintf (stderr, "\n");
6160 #endif
6161
6162 return ret;
6163 }
6164 \f
6165 /* Finish up dynamic symbol handling. We set the contents of various
6166 dynamic sections here. */
6167
6168 static bfd_boolean
6169 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
6170 struct bfd_link_info *info,
6171 struct elf_link_hash_entry *h,
6172 Elf_Internal_Sym *sym)
6173 {
6174 struct ppc_elf_link_hash_table *htab;
6175
6176 #ifdef DEBUG
6177 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
6178 h->root.root.string);
6179 #endif
6180
6181 htab = ppc_elf_hash_table (info);
6182 BFD_ASSERT (htab->elf.dynobj != NULL);
6183
6184 if (h->plt.offset != (bfd_vma) -1)
6185 {
6186 Elf_Internal_Rela rela;
6187 bfd_byte *loc;
6188 bfd_vma reloc_index;
6189
6190 #ifdef DEBUG
6191 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
6192 #endif
6193
6194 /* This symbol has an entry in the procedure linkage table. Set
6195 it up. */
6196
6197 BFD_ASSERT (h->dynindx != -1);
6198 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
6199
6200 if (htab->old_plt)
6201 {
6202 /* We don't need to fill in the .plt. The ppc dynamic linker
6203 will fill it in. */
6204 }
6205 else
6206 {
6207 bfd_vma val = (htab->glink_pltresolve
6208 + h->plt.offset
6209 + htab->glink->output_section->vma
6210 + htab->glink->output_offset);
6211 bfd_put_32 (output_bfd, val, htab->plt->contents + h->plt.offset);
6212 }
6213
6214 /* Fill in the entry in the .rela.plt section. */
6215 rela.r_offset = (htab->plt->output_section->vma
6216 + htab->plt->output_offset
6217 + h->plt.offset);
6218 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
6219 rela.r_addend = 0;
6220
6221 if (!htab->old_plt)
6222 reloc_index = h->plt.offset / 4;
6223 else
6224 {
6225 reloc_index = ((h->plt.offset - PLT_INITIAL_ENTRY_SIZE)
6226 / PLT_SLOT_SIZE);
6227 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
6228 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
6229 }
6230 loc = (htab->relplt->contents
6231 + reloc_index * sizeof (Elf32_External_Rela));
6232 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6233
6234 if (!h->def_regular)
6235 {
6236 /* Mark the symbol as undefined, rather than as defined in
6237 the .plt section. Leave the value alone. */
6238 sym->st_shndx = SHN_UNDEF;
6239 /* If the symbol is weak, we do need to clear the value.
6240 Otherwise, the PLT entry would provide a definition for
6241 the symbol even if the symbol wasn't defined anywhere,
6242 and so the symbol would never be NULL. */
6243 if (!h->ref_regular_nonweak)
6244 sym->st_value = 0;
6245 }
6246 }
6247
6248 if (h->needs_copy)
6249 {
6250 asection *s;
6251 Elf_Internal_Rela rela;
6252 bfd_byte *loc;
6253
6254 /* This symbols needs a copy reloc. Set it up. */
6255
6256 #ifdef DEBUG
6257 fprintf (stderr, ", copy");
6258 #endif
6259
6260 BFD_ASSERT (h->dynindx != -1);
6261
6262 if (h->size <= elf_gp_size (htab->elf.dynobj))
6263 s = htab->relsbss;
6264 else
6265 s = htab->relbss;
6266 BFD_ASSERT (s != NULL);
6267
6268 rela.r_offset = (h->root.u.def.value
6269 + h->root.u.def.section->output_section->vma
6270 + h->root.u.def.section->output_offset);
6271 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
6272 rela.r_addend = 0;
6273 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6274 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6275 }
6276
6277 #ifdef DEBUG
6278 fprintf (stderr, "\n");
6279 #endif
6280
6281 /* Mark some specially defined symbols as absolute. */
6282 if (h == htab->elf.hgot
6283 || strcmp (h->root.root.string, "_DYNAMIC") == 0
6284 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
6285 sym->st_shndx = SHN_ABS;
6286
6287 return TRUE;
6288 }
6289 \f
6290 static enum elf_reloc_type_class
6291 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
6292 {
6293 switch (ELF32_R_TYPE (rela->r_info))
6294 {
6295 case R_PPC_RELATIVE:
6296 return reloc_class_relative;
6297 case R_PPC_REL24:
6298 case R_PPC_ADDR24:
6299 case R_PPC_JMP_SLOT:
6300 return reloc_class_plt;
6301 case R_PPC_COPY:
6302 return reloc_class_copy;
6303 default:
6304 return reloc_class_normal;
6305 }
6306 }
6307 \f
6308 /* Finish up the dynamic sections. */
6309
6310 static bfd_boolean
6311 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
6312 struct bfd_link_info *info)
6313 {
6314 asection *sdyn;
6315 struct ppc_elf_link_hash_table *htab;
6316 bfd_vma got;
6317
6318 #ifdef DEBUG
6319 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
6320 #endif
6321
6322 htab = ppc_elf_hash_table (info);
6323 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
6324
6325 got = 0;
6326 if (htab->elf.hgot != NULL)
6327 got = (htab->elf.hgot->root.u.def.value
6328 + htab->elf.hgot->root.u.def.section->output_section->vma
6329 + htab->elf.hgot->root.u.def.section->output_offset);
6330
6331 if (htab->elf.dynamic_sections_created)
6332 {
6333 Elf32_External_Dyn *dyncon, *dynconend;
6334
6335 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
6336
6337 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6338 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6339 for (; dyncon < dynconend; dyncon++)
6340 {
6341 Elf_Internal_Dyn dyn;
6342 asection *s;
6343
6344 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
6345
6346 switch (dyn.d_tag)
6347 {
6348 case DT_PLTGOT:
6349 s = htab->plt;
6350 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6351 break;
6352
6353 case DT_PLTRELSZ:
6354 dyn.d_un.d_val = htab->relplt->size;
6355 break;
6356
6357 case DT_JMPREL:
6358 s = htab->relplt;
6359 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6360 break;
6361
6362 case DT_PPC_GOT:
6363 dyn.d_un.d_ptr = got;
6364 break;
6365
6366 default:
6367 continue;
6368 }
6369
6370 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6371 }
6372 }
6373
6374 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
6375 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
6376 if (htab->got != NULL)
6377 {
6378 unsigned char *p = htab->got->contents;
6379 bfd_vma val;
6380
6381 p += elf_hash_table (info)->hgot->root.u.def.value;
6382 if (htab->old_plt)
6383 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, p - 4);
6384
6385 val = 0;
6386 if (sdyn != NULL)
6387 val = sdyn->output_section->vma + sdyn->output_offset;
6388 bfd_put_32 (output_bfd, val, p);
6389
6390 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
6391 }
6392
6393 if (htab->glink != NULL && htab->glink->contents != NULL)
6394 {
6395 unsigned char *p;
6396 unsigned char *endp;
6397 bfd_vma pltgot;
6398 unsigned int i;
6399 static const unsigned int plt_resolve[] =
6400 {
6401 SUB_11_11_30,
6402 ADD_0_11_11,
6403 ADD_11_0_11,
6404 LWZ_0_4_30,
6405 MTCTR_0,
6406 LWZ_12_8_30,
6407 BCTR,
6408 NOP,
6409 NOP,
6410 NOP
6411 };
6412
6413 #define PPC_LO(v) ((v) & 0xffff)
6414 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6415 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6416
6417 pltgot = (htab->plt->output_section->vma
6418 + htab->plt->output_offset
6419 - got);
6420
6421 /* Write the plt call stubs. */
6422 p = htab->glink->contents;
6423 endp = p + htab->glink_pltresolve;
6424 while (p < endp)
6425 {
6426 if (pltgot < 0x8000)
6427 {
6428 bfd_put_32 (output_bfd, LWZ_11_X_30 + pltgot, p);
6429 p += 4;
6430 bfd_put_32 (output_bfd, MTCTR_11, p);
6431 p += 4;
6432 bfd_put_32 (output_bfd, BCTR, p);
6433 p += 4;
6434 bfd_put_32 (output_bfd, NOP, p);
6435 p += 4;
6436 }
6437 else
6438 {
6439 bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (pltgot), p);
6440 p += 4;
6441 bfd_put_32 (output_bfd, LWZ_11_X_11 + PPC_LO (pltgot), p);
6442 p += 4;
6443 bfd_put_32 (output_bfd, MTCTR_11, p);
6444 p += 4;
6445 bfd_put_32 (output_bfd, BCTR, p);
6446 p += 4;
6447 }
6448 pltgot += 4;
6449 }
6450
6451 /* Now build the branch table, one for each plt entry (less one),
6452 and perhaps some padding. */
6453 endp = htab->glink->contents;
6454 endp += htab->glink->size - GLINK_PLTRESOLVE;
6455 while (p < endp - 8 * 4)
6456 {
6457 bfd_put_32 (output_bfd, B + endp - p, p);
6458 p += 4;
6459 }
6460 while (p < endp)
6461 {
6462 bfd_put_32 (output_bfd, NOP, p);
6463 p += 4;
6464 }
6465
6466 got -= (htab->glink_pltresolve
6467 + htab->glink->output_section->vma
6468 + htab->glink->output_offset);
6469
6470 /* Last comes the PLTresolve stub. */
6471 bfd_put_32 (output_bfd, ADDIS_11_11 + PPC_HA (got), p);
6472 p += 4;
6473 bfd_put_32 (output_bfd, ADDI_11_11 + PPC_LO (got), p);
6474 p += 4;
6475
6476 for (i = 0; i < ARRAY_SIZE (plt_resolve); i++)
6477 {
6478 bfd_put_32 (output_bfd, plt_resolve[i], p);
6479 p += 4;
6480 }
6481 if (ARRAY_SIZE (plt_resolve) + 2 != GLINK_PLTRESOLVE / 4)
6482 abort ();
6483 }
6484
6485 return TRUE;
6486 }
6487 \f
6488 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
6489 #define TARGET_LITTLE_NAME "elf32-powerpcle"
6490 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
6491 #define TARGET_BIG_NAME "elf32-powerpc"
6492 #define ELF_ARCH bfd_arch_powerpc
6493 #define ELF_MACHINE_CODE EM_PPC
6494 #ifdef __QNXTARGET__
6495 #define ELF_MAXPAGESIZE 0x1000
6496 #else
6497 #define ELF_MAXPAGESIZE 0x10000
6498 #endif
6499 #define ELF_MINPAGESIZE 0x1000
6500 #define elf_info_to_howto ppc_elf_info_to_howto
6501
6502 #ifdef EM_CYGNUS_POWERPC
6503 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6504 #endif
6505
6506 #ifdef EM_PPC_OLD
6507 #define ELF_MACHINE_ALT2 EM_PPC_OLD
6508 #endif
6509
6510 #define elf_backend_plt_not_loaded 1
6511 #define elf_backend_can_gc_sections 1
6512 #define elf_backend_can_refcount 1
6513 #define elf_backend_rela_normal 1
6514
6515 #define bfd_elf32_mkobject ppc_elf_mkobject
6516 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
6517 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
6518 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
6519 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
6520 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
6521
6522 #define elf_backend_object_p ppc_elf_object_p
6523 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
6524 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
6525 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
6526 #define elf_backend_relocate_section ppc_elf_relocate_section
6527 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
6528 #define elf_backend_check_relocs ppc_elf_check_relocs
6529 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
6530 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
6531 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
6532 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
6533 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
6534 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
6535 #define elf_backend_fake_sections ppc_elf_fake_sections
6536 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
6537 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
6538 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
6539 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
6540 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
6541 #define elf_backend_final_write_processing ppc_elf_final_write_processing
6542 #define elf_backend_write_section ppc_elf_write_section
6543 #define elf_backend_special_sections ppc_elf_special_sections
6544 #define elf_backend_plt_sym_val ppc_elf_plt_sym_val
6545
6546 #include "elf32-target.h"