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