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