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