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