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