9a6881bf51e917c2ceb057a4c7a17c3b3ac13702
[platform/upstream/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
34 /* RELA relocations are used here.  */
35
36 static struct bfd_hash_entry *ppc_elf_link_hash_newfunc
37   PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
38            const char *string));
39 static struct bfd_link_hash_table *ppc_elf_link_hash_table_create
40   PARAMS ((bfd *abfd));
41 static void ppc_elf_copy_indirect_symbol
42   PARAMS ((struct elf_backend_data *bed, struct elf_link_hash_entry *dir,
43            struct elf_link_hash_entry *ind));
44 static reloc_howto_type *ppc_elf_reloc_type_lookup
45   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
46 static void ppc_elf_info_to_howto
47   PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
48 static void ppc_elf_howto_init
49   PARAMS ((void));
50 static int ppc_elf_sort_rela
51   PARAMS ((const PTR, const PTR));
52 static bfd_boolean ppc_elf_relax_section
53   PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
54 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
55   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
56 static bfd_boolean ppc_elf_object_p
57   PARAMS ((bfd *));
58 static bfd_boolean ppc_elf_set_private_flags
59   PARAMS ((bfd *, flagword));
60 static bfd_boolean ppc_elf_merge_private_bfd_data
61   PARAMS ((bfd *, bfd *));
62 static int ppc_elf_additional_program_headers
63   PARAMS ((bfd *));
64 static bfd_boolean ppc_elf_modify_segment_map
65   PARAMS ((bfd *));
66 static asection *ppc_elf_create_got
67   PARAMS ((bfd *, struct bfd_link_info *));
68 static bfd_boolean ppc_elf_create_dynamic_sections
69   PARAMS ((bfd *, struct bfd_link_info *));
70 static bfd_boolean ppc_elf_section_from_shdr
71   PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
72 static bfd_boolean ppc_elf_fake_sections
73   PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
74 static elf_linker_section_t *ppc_elf_create_linker_section
75   PARAMS ((bfd *abfd, struct bfd_link_info *info,
76            enum elf_linker_section_enum));
77 static bfd_boolean ppc_elf_check_relocs
78   PARAMS ((bfd *, struct bfd_link_info *, asection *,
79            const Elf_Internal_Rela *));
80 static asection * ppc_elf_gc_mark_hook
81   PARAMS ((asection *sec, struct bfd_link_info *info, Elf_Internal_Rela *rel,
82            struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
83 static bfd_boolean ppc_elf_gc_sweep_hook
84   PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
85            const Elf_Internal_Rela *relocs));
86 static bfd_boolean ppc_elf_adjust_dynamic_symbol
87   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
88 static bfd_boolean allocate_dynrelocs
89   PARAMS ((struct elf_link_hash_entry *, PTR));
90 static bfd_boolean readonly_dynrelocs
91   PARAMS ((struct elf_link_hash_entry *, PTR));
92 static bfd_boolean ppc_elf_size_dynamic_sections
93   PARAMS ((bfd *, struct bfd_link_info *));
94 static bfd_boolean ppc_elf_relocate_section
95   PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
96            Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
97            asection **));
98 static bfd_boolean ppc_elf_add_symbol_hook
99   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
100            const char **, flagword *, asection **, bfd_vma *));
101 static bfd_boolean ppc_elf_finish_dynamic_symbol
102   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
103            Elf_Internal_Sym *));
104 static bfd_boolean ppc_elf_finish_dynamic_sections
105   PARAMS ((bfd *, struct bfd_link_info *));
106 static enum elf_reloc_type_class ppc_elf_reloc_type_class
107   PARAMS ((const Elf_Internal_Rela *));
108 static bfd_boolean ppc_elf_grok_prstatus
109   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
110 static bfd_boolean ppc_elf_grok_psinfo
111   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
112
113 #define BRANCH_PREDICT_BIT 0x200000             /* Branch prediction bit for branch taken relocs.  */
114 #define RA_REGISTER_MASK 0x001f0000             /* Mask to set RA in memory instructions.  */
115 #define RA_REGISTER_SHIFT 16                    /* Value to shift register by to insert RA.  */
116
117 /* The name of the dynamic interpreter.  This is put in the .interp
118    section.  */
119
120 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
121
122 /* The size in bytes of an entry in the procedure linkage table.  */
123 #define PLT_ENTRY_SIZE 12
124 /* The initial size of the plt reserved for the dynamic linker.  */
125 #define PLT_INITIAL_ENTRY_SIZE 72
126 /* The size of the gap between entries in the PLT.  */
127 #define PLT_SLOT_SIZE 8
128 /* The number of single-slot PLT entries (the rest use two slots).  */
129 #define PLT_NUM_SINGLE_ENTRIES 8192
130
131 /* Will references to this symbol always reference the symbol
132    in this object?  */
133 #define SYMBOL_REFERENCES_LOCAL(INFO, H)                                \
134   ((! INFO->shared                                                      \
135     || INFO->symbolic                                                   \
136     || H->dynindx == -1                                                 \
137     || ELF_ST_VISIBILITY (H->other) == STV_INTERNAL                     \
138     || ELF_ST_VISIBILITY (H->other) == STV_HIDDEN)                      \
139    && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
140
141 /* Will _calls_ to this symbol always call the version in this object?  */
142 #define SYMBOL_CALLS_LOCAL(INFO, H)                             \
143   ((! INFO->shared                                                      \
144     || INFO->symbolic                                                   \
145     || H->dynindx == -1                                                 \
146     || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT)                     \
147    && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
148 \f
149 /* The PPC linker needs to keep track of the number of relocs that it
150    decides to copy as dynamic relocs in check_relocs for each symbol.
151    This is so that it can later discard them if they are found to be
152    unnecessary.  We store the information in a field extending the
153    regular ELF linker hash table.  */
154
155 struct ppc_elf_dyn_relocs
156 {
157   struct ppc_elf_dyn_relocs *next;
158
159   /* The input section of the reloc.  */
160   asection *sec;
161
162   /* Total number of relocs copied for the input section.  */
163   bfd_size_type count;
164 };
165
166 /* PPC ELF linker hash entry.  */
167
168 struct ppc_elf_link_hash_entry
169 {
170   struct elf_link_hash_entry root;
171
172   /* Track dynamic relocs copied for this symbol.  */
173   struct ppc_elf_dyn_relocs *dyn_relocs;
174 };
175
176 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
177
178 /* PPC ELF linker hash table.  */
179
180 struct ppc_elf_link_hash_table
181 {
182   struct elf_link_hash_table root;
183
184   /* Small local sym to section mapping cache.  */
185   struct sym_sec_cache sym_sec;
186 };
187
188 /* Get the PPC ELF linker hash table from a link_info structure.  */
189
190 #define ppc_elf_hash_table(p) \
191   ((struct ppc_elf_link_hash_table *) (p)->hash)
192
193 /* Create an entry in a PPC ELF linker hash table.  */
194
195 static struct bfd_hash_entry *
196 ppc_elf_link_hash_newfunc (entry, table, string)
197      struct bfd_hash_entry *entry;
198      struct bfd_hash_table *table;
199      const char *string;
200 {
201   /* Allocate the structure if it has not already been allocated by a
202      subclass.  */
203   if (entry == NULL)
204     {
205       entry = bfd_hash_allocate (table,
206                                  sizeof (struct ppc_elf_link_hash_entry));
207       if (entry == NULL)
208         return entry;
209     }
210
211   /* Call the allocation method of the superclass.  */
212   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
213   if (entry != NULL)
214     ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
215
216   return entry;
217 }
218
219 /* Create a PPC ELF linker hash table.  */
220
221 static struct bfd_link_hash_table *
222 ppc_elf_link_hash_table_create (abfd)
223      bfd *abfd;
224 {
225   struct ppc_elf_link_hash_table *ret;
226
227   ret = ((struct ppc_elf_link_hash_table *)
228          bfd_malloc (sizeof (struct ppc_elf_link_hash_table)));
229   if (ret == NULL)
230     return NULL;
231
232   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
233                                        ppc_elf_link_hash_newfunc))
234     {
235       free (ret);
236       return NULL;
237     }
238
239   ret->sym_sec.abfd = NULL;
240
241   return &ret->root.root;
242 }
243
244 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
245
246 static void
247 ppc_elf_copy_indirect_symbol (bed, dir, ind)
248      struct elf_backend_data *bed;
249      struct elf_link_hash_entry *dir, *ind;
250 {
251   struct ppc_elf_link_hash_entry *edir, *eind;
252
253   edir = (struct ppc_elf_link_hash_entry *) dir;
254   eind = (struct ppc_elf_link_hash_entry *) ind;
255
256   if (eind->dyn_relocs != NULL)
257     {
258       if (edir->dyn_relocs != NULL)
259         {
260           struct ppc_elf_dyn_relocs **pp;
261           struct ppc_elf_dyn_relocs *p;
262
263           if (ind->root.type == bfd_link_hash_indirect)
264             abort ();
265
266           /* Add reloc counts against the weak sym to the strong sym
267              list.  Merge any entries against the same section.  */
268           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
269             {
270               struct ppc_elf_dyn_relocs *q;
271
272               for (q = edir->dyn_relocs; q != NULL; q = q->next)
273                 if (q->sec == p->sec)
274                   {
275                     q->count += p->count;
276                     *pp = p->next;
277                     break;
278                   }
279               if (q == NULL)
280                 pp = &p->next;
281             }
282           *pp = edir->dyn_relocs;
283         }
284
285       edir->dyn_relocs = eind->dyn_relocs;
286       eind->dyn_relocs = NULL;
287     }
288
289   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
290 }
291 \f
292 static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
293
294 static reloc_howto_type ppc_elf_howto_raw[] = {
295   /* This reloc does nothing.  */
296   HOWTO (R_PPC_NONE,            /* type */
297          0,                     /* rightshift */
298          2,                     /* size (0 = byte, 1 = short, 2 = long) */
299          32,                    /* bitsize */
300          FALSE,                 /* pc_relative */
301          0,                     /* bitpos */
302          complain_overflow_bitfield, /* complain_on_overflow */
303          bfd_elf_generic_reloc, /* special_function */
304          "R_PPC_NONE",          /* name */
305          FALSE,                 /* partial_inplace */
306          0,                     /* src_mask */
307          0,                     /* dst_mask */
308          FALSE),                /* pcrel_offset */
309
310   /* A standard 32 bit relocation.  */
311   HOWTO (R_PPC_ADDR32,          /* type */
312          0,                     /* rightshift */
313          2,                     /* size (0 = byte, 1 = short, 2 = long) */
314          32,                    /* bitsize */
315          FALSE,                 /* pc_relative */
316          0,                     /* bitpos */
317          complain_overflow_bitfield, /* complain_on_overflow */
318          bfd_elf_generic_reloc, /* special_function */
319          "R_PPC_ADDR32",        /* name */
320          FALSE,                 /* partial_inplace */
321          0,                     /* src_mask */
322          0xffffffff,            /* dst_mask */
323          FALSE),                /* pcrel_offset */
324
325   /* An absolute 26 bit branch; the lower two bits must be zero.
326      FIXME: we don't check that, we just clear them.  */
327   HOWTO (R_PPC_ADDR24,          /* type */
328          0,                     /* rightshift */
329          2,                     /* size (0 = byte, 1 = short, 2 = long) */
330          26,                    /* bitsize */
331          FALSE,                 /* pc_relative */
332          0,                     /* bitpos */
333          complain_overflow_bitfield, /* complain_on_overflow */
334          bfd_elf_generic_reloc, /* special_function */
335          "R_PPC_ADDR24",        /* name */
336          FALSE,                 /* partial_inplace */
337          0,                     /* src_mask */
338          0x3fffffc,             /* dst_mask */
339          FALSE),                /* pcrel_offset */
340
341   /* A standard 16 bit relocation.  */
342   HOWTO (R_PPC_ADDR16,          /* type */
343          0,                     /* rightshift */
344          1,                     /* size (0 = byte, 1 = short, 2 = long) */
345          16,                    /* bitsize */
346          FALSE,                 /* pc_relative */
347          0,                     /* bitpos */
348          complain_overflow_bitfield, /* complain_on_overflow */
349          bfd_elf_generic_reloc, /* special_function */
350          "R_PPC_ADDR16",        /* name */
351          FALSE,                 /* partial_inplace */
352          0,                     /* src_mask */
353          0xffff,                /* dst_mask */
354          FALSE),                /* pcrel_offset */
355
356   /* A 16 bit relocation without overflow.  */
357   HOWTO (R_PPC_ADDR16_LO,       /* type */
358          0,                     /* rightshift */
359          1,                     /* size (0 = byte, 1 = short, 2 = long) */
360          16,                    /* bitsize */
361          FALSE,                 /* pc_relative */
362          0,                     /* bitpos */
363          complain_overflow_dont,/* complain_on_overflow */
364          bfd_elf_generic_reloc, /* special_function */
365          "R_PPC_ADDR16_LO",     /* name */
366          FALSE,                 /* partial_inplace */
367          0,                     /* src_mask */
368          0xffff,                /* dst_mask */
369          FALSE),                /* pcrel_offset */
370
371   /* The high order 16 bits of an address.  */
372   HOWTO (R_PPC_ADDR16_HI,       /* type */
373          16,                    /* rightshift */
374          1,                     /* size (0 = byte, 1 = short, 2 = long) */
375          16,                    /* bitsize */
376          FALSE,                 /* pc_relative */
377          0,                     /* bitpos */
378          complain_overflow_dont, /* complain_on_overflow */
379          bfd_elf_generic_reloc, /* special_function */
380          "R_PPC_ADDR16_HI",     /* name */
381          FALSE,                 /* partial_inplace */
382          0,                     /* src_mask */
383          0xffff,                /* dst_mask */
384          FALSE),                /* pcrel_offset */
385
386   /* The high order 16 bits of an address, plus 1 if the contents of
387      the low 16 bits, treated as a signed number, is negative.  */
388   HOWTO (R_PPC_ADDR16_HA,       /* type */
389          16,                    /* rightshift */
390          1,                     /* size (0 = byte, 1 = short, 2 = long) */
391          16,                    /* bitsize */
392          FALSE,                 /* pc_relative */
393          0,                     /* bitpos */
394          complain_overflow_dont, /* complain_on_overflow */
395          ppc_elf_addr16_ha_reloc, /* special_function */
396          "R_PPC_ADDR16_HA",     /* name */
397          FALSE,                 /* partial_inplace */
398          0,                     /* src_mask */
399          0xffff,                /* dst_mask */
400          FALSE),                /* pcrel_offset */
401
402   /* An absolute 16 bit branch; the lower two bits must be zero.
403      FIXME: we don't check that, we just clear them.  */
404   HOWTO (R_PPC_ADDR14,          /* type */
405          0,                     /* rightshift */
406          2,                     /* size (0 = byte, 1 = short, 2 = long) */
407          16,                    /* bitsize */
408          FALSE,                 /* pc_relative */
409          0,                     /* bitpos */
410          complain_overflow_bitfield, /* complain_on_overflow */
411          bfd_elf_generic_reloc, /* special_function */
412          "R_PPC_ADDR14",        /* name */
413          FALSE,                 /* partial_inplace */
414          0,                     /* src_mask */
415          0xfffc,                /* dst_mask */
416          FALSE),                /* pcrel_offset */
417
418   /* An absolute 16 bit branch, for which bit 10 should be set to
419      indicate that the branch is expected to be taken.  The lower two
420      bits must be zero.  */
421   HOWTO (R_PPC_ADDR14_BRTAKEN,  /* type */
422          0,                     /* rightshift */
423          2,                     /* size (0 = byte, 1 = short, 2 = long) */
424          16,                    /* bitsize */
425          FALSE,                 /* pc_relative */
426          0,                     /* bitpos */
427          complain_overflow_bitfield, /* complain_on_overflow */
428          bfd_elf_generic_reloc, /* special_function */
429          "R_PPC_ADDR14_BRTAKEN",/* name */
430          FALSE,                 /* partial_inplace */
431          0,                     /* src_mask */
432          0xfffc,                /* dst_mask */
433          FALSE),                /* pcrel_offset */
434
435   /* An absolute 16 bit branch, for which bit 10 should be set to
436      indicate that the branch is not expected to be taken.  The lower
437      two bits must be zero.  */
438   HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
439          0,                     /* rightshift */
440          2,                     /* size (0 = byte, 1 = short, 2 = long) */
441          16,                    /* bitsize */
442          FALSE,                 /* pc_relative */
443          0,                     /* bitpos */
444          complain_overflow_bitfield, /* complain_on_overflow */
445          bfd_elf_generic_reloc, /* special_function */
446          "R_PPC_ADDR14_BRNTAKEN",/* name */
447          FALSE,                 /* partial_inplace */
448          0,                     /* src_mask */
449          0xfffc,                /* dst_mask */
450          FALSE),                /* pcrel_offset */
451
452   /* A relative 26 bit branch; the lower two bits must be zero.  */
453   HOWTO (R_PPC_REL24,           /* type */
454          0,                     /* rightshift */
455          2,                     /* size (0 = byte, 1 = short, 2 = long) */
456          26,                    /* bitsize */
457          TRUE,                  /* pc_relative */
458          0,                     /* bitpos */
459          complain_overflow_signed, /* complain_on_overflow */
460          bfd_elf_generic_reloc, /* special_function */
461          "R_PPC_REL24",         /* name */
462          FALSE,                 /* partial_inplace */
463          0,                     /* src_mask */
464          0x3fffffc,             /* dst_mask */
465          TRUE),                 /* pcrel_offset */
466
467   /* A relative 16 bit branch; the lower two bits must be zero.  */
468   HOWTO (R_PPC_REL14,           /* type */
469          0,                     /* rightshift */
470          2,                     /* size (0 = byte, 1 = short, 2 = long) */
471          16,                    /* bitsize */
472          TRUE,                  /* pc_relative */
473          0,                     /* bitpos */
474          complain_overflow_signed, /* complain_on_overflow */
475          bfd_elf_generic_reloc, /* special_function */
476          "R_PPC_REL14",         /* name */
477          FALSE,                 /* partial_inplace */
478          0,                     /* src_mask */
479          0xfffc,                /* dst_mask */
480          TRUE),                 /* pcrel_offset */
481
482   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
483      the branch is expected to be taken.  The lower two bits must be
484      zero.  */
485   HOWTO (R_PPC_REL14_BRTAKEN,   /* type */
486          0,                     /* rightshift */
487          2,                     /* size (0 = byte, 1 = short, 2 = long) */
488          16,                    /* bitsize */
489          TRUE,                  /* pc_relative */
490          0,                     /* bitpos */
491          complain_overflow_signed, /* complain_on_overflow */
492          bfd_elf_generic_reloc, /* special_function */
493          "R_PPC_REL14_BRTAKEN", /* name */
494          FALSE,                 /* partial_inplace */
495          0,                     /* src_mask */
496          0xfffc,                /* dst_mask */
497          TRUE),                 /* pcrel_offset */
498
499   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
500      the branch is not expected to be taken.  The lower two bits must
501      be zero.  */
502   HOWTO (R_PPC_REL14_BRNTAKEN,  /* type */
503          0,                     /* rightshift */
504          2,                     /* size (0 = byte, 1 = short, 2 = long) */
505          16,                    /* bitsize */
506          TRUE,                  /* pc_relative */
507          0,                     /* bitpos */
508          complain_overflow_signed, /* complain_on_overflow */
509          bfd_elf_generic_reloc, /* special_function */
510          "R_PPC_REL14_BRNTAKEN",/* name */
511          FALSE,                 /* partial_inplace */
512          0,                     /* src_mask */
513          0xfffc,                /* dst_mask */
514          TRUE),                 /* pcrel_offset */
515
516   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
517      symbol.  */
518   HOWTO (R_PPC_GOT16,           /* type */
519          0,                     /* rightshift */
520          1,                     /* size (0 = byte, 1 = short, 2 = long) */
521          16,                    /* bitsize */
522          FALSE,                 /* pc_relative */
523          0,                     /* bitpos */
524          complain_overflow_signed, /* complain_on_overflow */
525          bfd_elf_generic_reloc, /* special_function */
526          "R_PPC_GOT16",         /* name */
527          FALSE,                 /* partial_inplace */
528          0,                     /* src_mask */
529          0xffff,                /* dst_mask */
530          FALSE),                /* pcrel_offset */
531
532   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
533      the symbol.  */
534   HOWTO (R_PPC_GOT16_LO,        /* type */
535          0,                     /* rightshift */
536          1,                     /* size (0 = byte, 1 = short, 2 = long) */
537          16,                    /* bitsize */
538          FALSE,                 /* pc_relative */
539          0,                     /* bitpos */
540          complain_overflow_dont, /* complain_on_overflow */
541          bfd_elf_generic_reloc, /* special_function */
542          "R_PPC_GOT16_LO",      /* name */
543          FALSE,                 /* partial_inplace */
544          0,                     /* src_mask */
545          0xffff,                /* dst_mask */
546          FALSE),                /* pcrel_offset */
547
548   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
549      the symbol.  */
550   HOWTO (R_PPC_GOT16_HI,        /* type */
551          16,                    /* rightshift */
552          1,                     /* size (0 = byte, 1 = short, 2 = long) */
553          16,                    /* bitsize */
554          FALSE,                 /* pc_relative */
555          0,                     /* bitpos */
556          complain_overflow_bitfield, /* complain_on_overflow */
557          bfd_elf_generic_reloc, /* special_function */
558          "R_PPC_GOT16_HI",      /* name */
559          FALSE,                 /* partial_inplace */
560          0,                     /* src_mask */
561          0xffff,                /* dst_mask */
562          FALSE),                 /* pcrel_offset */
563
564   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
565      the symbol.  */
566   HOWTO (R_PPC_GOT16_HA,        /* type */
567          16,                    /* rightshift */
568          1,                     /* size (0 = byte, 1 = short, 2 = long) */
569          16,                    /* bitsize */
570          FALSE,                 /* pc_relative */
571          0,                     /* bitpos */
572          complain_overflow_bitfield, /* complain_on_overflow */
573          ppc_elf_addr16_ha_reloc, /* special_function */
574          "R_PPC_GOT16_HA",      /* name */
575          FALSE,                 /* partial_inplace */
576          0,                     /* src_mask */
577          0xffff,                /* dst_mask */
578          FALSE),                /* pcrel_offset */
579
580   /* Like R_PPC_REL24, but referring to the procedure linkage table
581      entry for the symbol.  */
582   HOWTO (R_PPC_PLTREL24,        /* type */
583          0,                     /* rightshift */
584          2,                     /* size (0 = byte, 1 = short, 2 = long) */
585          26,                    /* bitsize */
586          TRUE,                  /* pc_relative */
587          0,                     /* bitpos */
588          complain_overflow_signed,  /* complain_on_overflow */
589          bfd_elf_generic_reloc, /* special_function */
590          "R_PPC_PLTREL24",      /* name */
591          FALSE,                 /* partial_inplace */
592          0,                     /* src_mask */
593          0x3fffffc,             /* dst_mask */
594          TRUE),                 /* pcrel_offset */
595
596   /* This is used only by the dynamic linker.  The symbol should exist
597      both in the object being run and in some shared library.  The
598      dynamic linker copies the data addressed by the symbol from the
599      shared library into the object, because the object being
600      run has to have the data at some particular address.  */
601   HOWTO (R_PPC_COPY,            /* type */
602          0,                     /* rightshift */
603          2,                     /* size (0 = byte, 1 = short, 2 = long) */
604          32,                    /* bitsize */
605          FALSE,                 /* pc_relative */
606          0,                     /* bitpos */
607          complain_overflow_bitfield, /* complain_on_overflow */
608          bfd_elf_generic_reloc,  /* special_function */
609          "R_PPC_COPY",          /* name */
610          FALSE,                 /* partial_inplace */
611          0,                     /* src_mask */
612          0,                     /* dst_mask */
613          FALSE),                /* pcrel_offset */
614
615   /* Like R_PPC_ADDR32, but used when setting global offset table
616      entries.  */
617   HOWTO (R_PPC_GLOB_DAT,        /* type */
618          0,                     /* rightshift */
619          2,                     /* size (0 = byte, 1 = short, 2 = long) */
620          32,                    /* bitsize */
621          FALSE,                 /* pc_relative */
622          0,                     /* bitpos */
623          complain_overflow_bitfield, /* complain_on_overflow */
624          bfd_elf_generic_reloc,  /* special_function */
625          "R_PPC_GLOB_DAT",      /* name */
626          FALSE,                 /* partial_inplace */
627          0,                     /* src_mask */
628          0xffffffff,            /* dst_mask */
629          FALSE),                /* pcrel_offset */
630
631   /* Marks a procedure linkage table entry for a symbol.  */
632   HOWTO (R_PPC_JMP_SLOT,        /* type */
633          0,                     /* rightshift */
634          2,                     /* size (0 = byte, 1 = short, 2 = long) */
635          32,                    /* bitsize */
636          FALSE,                 /* pc_relative */
637          0,                     /* bitpos */
638          complain_overflow_bitfield, /* complain_on_overflow */
639          bfd_elf_generic_reloc,  /* special_function */
640          "R_PPC_JMP_SLOT",      /* name */
641          FALSE,                 /* partial_inplace */
642          0,                     /* src_mask */
643          0,                     /* dst_mask */
644          FALSE),                /* pcrel_offset */
645
646   /* Used only by the dynamic linker.  When the object is run, this
647      longword is set to the load address of the object, plus the
648      addend.  */
649   HOWTO (R_PPC_RELATIVE,        /* type */
650          0,                     /* rightshift */
651          2,                     /* size (0 = byte, 1 = short, 2 = long) */
652          32,                    /* bitsize */
653          FALSE,                 /* pc_relative */
654          0,                     /* bitpos */
655          complain_overflow_bitfield, /* complain_on_overflow */
656          bfd_elf_generic_reloc,  /* special_function */
657          "R_PPC_RELATIVE",      /* name */
658          FALSE,                 /* partial_inplace */
659          0,                     /* src_mask */
660          0xffffffff,            /* dst_mask */
661          FALSE),                /* pcrel_offset */
662
663   /* Like R_PPC_REL24, but uses the value of the symbol within the
664      object rather than the final value.  Normally used for
665      _GLOBAL_OFFSET_TABLE_.  */
666   HOWTO (R_PPC_LOCAL24PC,       /* type */
667          0,                     /* rightshift */
668          2,                     /* size (0 = byte, 1 = short, 2 = long) */
669          26,                    /* bitsize */
670          TRUE,                  /* pc_relative */
671          0,                     /* bitpos */
672          complain_overflow_signed, /* complain_on_overflow */
673          bfd_elf_generic_reloc, /* special_function */
674          "R_PPC_LOCAL24PC",     /* name */
675          FALSE,                 /* partial_inplace */
676          0,                     /* src_mask */
677          0x3fffffc,             /* dst_mask */
678          TRUE),                 /* pcrel_offset */
679
680   /* Like R_PPC_ADDR32, but may be unaligned.  */
681   HOWTO (R_PPC_UADDR32,         /* type */
682          0,                     /* rightshift */
683          2,                     /* size (0 = byte, 1 = short, 2 = long) */
684          32,                    /* bitsize */
685          FALSE,                 /* pc_relative */
686          0,                     /* bitpos */
687          complain_overflow_bitfield, /* complain_on_overflow */
688          bfd_elf_generic_reloc, /* special_function */
689          "R_PPC_UADDR32",       /* name */
690          FALSE,                 /* partial_inplace */
691          0,                     /* src_mask */
692          0xffffffff,            /* dst_mask */
693          FALSE),                /* pcrel_offset */
694
695   /* Like R_PPC_ADDR16, but may be unaligned.  */
696   HOWTO (R_PPC_UADDR16,         /* type */
697          0,                     /* rightshift */
698          1,                     /* size (0 = byte, 1 = short, 2 = long) */
699          16,                    /* bitsize */
700          FALSE,                 /* pc_relative */
701          0,                     /* bitpos */
702          complain_overflow_bitfield, /* complain_on_overflow */
703          bfd_elf_generic_reloc, /* special_function */
704          "R_PPC_UADDR16",       /* name */
705          FALSE,                 /* partial_inplace */
706          0,                     /* src_mask */
707          0xffff,                /* dst_mask */
708          FALSE),                /* pcrel_offset */
709
710   /* 32-bit PC relative */
711   HOWTO (R_PPC_REL32,           /* type */
712          0,                     /* rightshift */
713          2,                     /* size (0 = byte, 1 = short, 2 = long) */
714          32,                    /* bitsize */
715          TRUE,                  /* pc_relative */
716          0,                     /* bitpos */
717          complain_overflow_bitfield, /* complain_on_overflow */
718          bfd_elf_generic_reloc, /* special_function */
719          "R_PPC_REL32",         /* name */
720          FALSE,                 /* partial_inplace */
721          0,                     /* src_mask */
722          0xffffffff,            /* dst_mask */
723          TRUE),                 /* pcrel_offset */
724
725   /* 32-bit relocation to the symbol's procedure linkage table.
726      FIXME: not supported.  */
727   HOWTO (R_PPC_PLT32,           /* type */
728          0,                     /* rightshift */
729          2,                     /* size (0 = byte, 1 = short, 2 = long) */
730          32,                    /* bitsize */
731          FALSE,                 /* pc_relative */
732          0,                     /* bitpos */
733          complain_overflow_bitfield, /* complain_on_overflow */
734          bfd_elf_generic_reloc, /* special_function */
735          "R_PPC_PLT32",         /* name */
736          FALSE,                 /* partial_inplace */
737          0,                     /* src_mask */
738          0,                     /* dst_mask */
739          FALSE),                /* pcrel_offset */
740
741   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
742      FIXME: not supported.  */
743   HOWTO (R_PPC_PLTREL32,        /* type */
744          0,                     /* rightshift */
745          2,                     /* size (0 = byte, 1 = short, 2 = long) */
746          32,                    /* bitsize */
747          TRUE,                  /* pc_relative */
748          0,                     /* bitpos */
749          complain_overflow_bitfield, /* complain_on_overflow */
750          bfd_elf_generic_reloc, /* special_function */
751          "R_PPC_PLTREL32",      /* name */
752          FALSE,                 /* partial_inplace */
753          0,                     /* src_mask */
754          0,                     /* dst_mask */
755          TRUE),                 /* pcrel_offset */
756
757   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
758      the symbol.  */
759   HOWTO (R_PPC_PLT16_LO,        /* type */
760          0,                     /* rightshift */
761          1,                     /* size (0 = byte, 1 = short, 2 = long) */
762          16,                    /* bitsize */
763          FALSE,                 /* pc_relative */
764          0,                     /* bitpos */
765          complain_overflow_dont, /* complain_on_overflow */
766          bfd_elf_generic_reloc, /* special_function */
767          "R_PPC_PLT16_LO",      /* name */
768          FALSE,                 /* partial_inplace */
769          0,                     /* src_mask */
770          0xffff,                /* dst_mask */
771          FALSE),                /* pcrel_offset */
772
773   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
774      the symbol.  */
775   HOWTO (R_PPC_PLT16_HI,        /* type */
776          16,                    /* rightshift */
777          1,                     /* size (0 = byte, 1 = short, 2 = long) */
778          16,                    /* bitsize */
779          FALSE,                 /* pc_relative */
780          0,                     /* bitpos */
781          complain_overflow_bitfield, /* complain_on_overflow */
782          bfd_elf_generic_reloc, /* special_function */
783          "R_PPC_PLT16_HI",      /* name */
784          FALSE,                 /* partial_inplace */
785          0,                     /* src_mask */
786          0xffff,                /* dst_mask */
787          FALSE),                 /* pcrel_offset */
788
789   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
790      the symbol.  */
791   HOWTO (R_PPC_PLT16_HA,        /* type */
792          16,                    /* rightshift */
793          1,                     /* size (0 = byte, 1 = short, 2 = long) */
794          16,                    /* bitsize */
795          FALSE,                 /* pc_relative */
796          0,                     /* bitpos */
797          complain_overflow_bitfield, /* complain_on_overflow */
798          ppc_elf_addr16_ha_reloc, /* special_function */
799          "R_PPC_PLT16_HA",      /* name */
800          FALSE,                 /* partial_inplace */
801          0,                     /* src_mask */
802          0xffff,                /* dst_mask */
803          FALSE),                /* pcrel_offset */
804
805   /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
806      small data items.  */
807   HOWTO (R_PPC_SDAREL16,        /* type */
808          0,                     /* rightshift */
809          1,                     /* size (0 = byte, 1 = short, 2 = long) */
810          16,                    /* bitsize */
811          FALSE,                 /* pc_relative */
812          0,                     /* bitpos */
813          complain_overflow_signed, /* complain_on_overflow */
814          bfd_elf_generic_reloc, /* special_function */
815          "R_PPC_SDAREL16",      /* name */
816          FALSE,                 /* partial_inplace */
817          0,                     /* src_mask */
818          0xffff,                /* dst_mask */
819          FALSE),                /* pcrel_offset */
820
821   /* 16-bit section relative relocation.  */
822   HOWTO (R_PPC_SECTOFF,         /* type */
823          0,                     /* rightshift */
824          1,                     /* size (0 = byte, 1 = short, 2 = long) */
825          16,                    /* bitsize */
826          FALSE,                 /* pc_relative */
827          0,                     /* bitpos */
828          complain_overflow_bitfield, /* complain_on_overflow */
829          bfd_elf_generic_reloc, /* special_function */
830          "R_PPC_SECTOFF",       /* name */
831          FALSE,                 /* partial_inplace */
832          0,                     /* src_mask */
833          0xffff,                /* dst_mask */
834          FALSE),                /* pcrel_offset */
835
836   /* 16-bit lower half section relative relocation.  */
837   HOWTO (R_PPC_SECTOFF_LO,        /* type */
838          0,                     /* rightshift */
839          1,                     /* size (0 = byte, 1 = short, 2 = long) */
840          16,                    /* bitsize */
841          FALSE,                 /* pc_relative */
842          0,                     /* bitpos */
843          complain_overflow_dont, /* complain_on_overflow */
844          bfd_elf_generic_reloc, /* special_function */
845          "R_PPC_SECTOFF_LO",    /* name */
846          FALSE,                 /* partial_inplace */
847          0,                     /* src_mask */
848          0xffff,                /* dst_mask */
849          FALSE),                /* pcrel_offset */
850
851   /* 16-bit upper half section relative relocation.  */
852   HOWTO (R_PPC_SECTOFF_HI,      /* type */
853          16,                    /* rightshift */
854          1,                     /* size (0 = byte, 1 = short, 2 = long) */
855          16,                    /* bitsize */
856          FALSE,                 /* pc_relative */
857          0,                     /* bitpos */
858          complain_overflow_bitfield, /* complain_on_overflow */
859          bfd_elf_generic_reloc, /* special_function */
860          "R_PPC_SECTOFF_HI",    /* name */
861          FALSE,                 /* partial_inplace */
862          0,                     /* src_mask */
863          0xffff,                /* dst_mask */
864          FALSE),                 /* pcrel_offset */
865
866   /* 16-bit upper half adjusted section relative relocation.  */
867   HOWTO (R_PPC_SECTOFF_HA,      /* type */
868          16,                    /* rightshift */
869          1,                     /* size (0 = byte, 1 = short, 2 = long) */
870          16,                    /* bitsize */
871          FALSE,                 /* pc_relative */
872          0,                     /* bitpos */
873          complain_overflow_bitfield, /* complain_on_overflow */
874          ppc_elf_addr16_ha_reloc, /* special_function */
875          "R_PPC_SECTOFF_HA",    /* name */
876          FALSE,                 /* partial_inplace */
877          0,                     /* src_mask */
878          0xffff,                /* dst_mask */
879          FALSE),                /* pcrel_offset */
880
881   /* The remaining relocs are from the Embedded ELF ABI, and are not
882      in the SVR4 ELF ABI.  */
883
884   /* 32 bit value resulting from the addend minus the symbol.  */
885   HOWTO (R_PPC_EMB_NADDR32,     /* type */
886          0,                     /* rightshift */
887          2,                     /* size (0 = byte, 1 = short, 2 = long) */
888          32,                    /* bitsize */
889          FALSE,                 /* pc_relative */
890          0,                     /* bitpos */
891          complain_overflow_bitfield, /* complain_on_overflow */
892          bfd_elf_generic_reloc, /* special_function */
893          "R_PPC_EMB_NADDR32",   /* name */
894          FALSE,                 /* partial_inplace */
895          0,                     /* src_mask */
896          0xffffffff,            /* dst_mask */
897          FALSE),                /* pcrel_offset */
898
899   /* 16 bit value resulting from the addend minus the symbol.  */
900   HOWTO (R_PPC_EMB_NADDR16,     /* type */
901          0,                     /* rightshift */
902          1,                     /* size (0 = byte, 1 = short, 2 = long) */
903          16,                    /* bitsize */
904          FALSE,                 /* pc_relative */
905          0,                     /* bitpos */
906          complain_overflow_bitfield, /* complain_on_overflow */
907          bfd_elf_generic_reloc, /* special_function */
908          "R_PPC_EMB_NADDR16",   /* name */
909          FALSE,                 /* partial_inplace */
910          0,                     /* src_mask */
911          0xffff,                /* dst_mask */
912          FALSE),                /* pcrel_offset */
913
914   /* 16 bit value resulting from the addend minus the symbol.  */
915   HOWTO (R_PPC_EMB_NADDR16_LO,  /* type */
916          0,                     /* rightshift */
917          1,                     /* size (0 = byte, 1 = short, 2 = long) */
918          16,                    /* bitsize */
919          FALSE,                 /* pc_relative */
920          0,                     /* bitpos */
921          complain_overflow_dont,/* complain_on_overflow */
922          bfd_elf_generic_reloc, /* special_function */
923          "R_PPC_EMB_ADDR16_LO", /* name */
924          FALSE,                 /* partial_inplace */
925          0,                     /* src_mask */
926          0xffff,                /* dst_mask */
927          FALSE),                /* pcrel_offset */
928
929   /* The high order 16 bits of the addend minus the symbol.  */
930   HOWTO (R_PPC_EMB_NADDR16_HI,  /* type */
931          16,                    /* rightshift */
932          1,                     /* size (0 = byte, 1 = short, 2 = long) */
933          16,                    /* bitsize */
934          FALSE,                 /* pc_relative */
935          0,                     /* bitpos */
936          complain_overflow_dont, /* complain_on_overflow */
937          bfd_elf_generic_reloc, /* special_function */
938          "R_PPC_EMB_NADDR16_HI", /* name */
939          FALSE,                 /* partial_inplace */
940          0,                     /* src_mask */
941          0xffff,                /* dst_mask */
942          FALSE),                /* pcrel_offset */
943
944   /* The high order 16 bits of the result of the addend minus the address,
945      plus 1 if the contents of the low 16 bits, treated as a signed number,
946      is negative.  */
947   HOWTO (R_PPC_EMB_NADDR16_HA,  /* type */
948          16,                    /* rightshift */
949          1,                     /* size (0 = byte, 1 = short, 2 = long) */
950          16,                    /* bitsize */
951          FALSE,                 /* pc_relative */
952          0,                     /* bitpos */
953          complain_overflow_dont, /* complain_on_overflow */
954          ppc_elf_addr16_ha_reloc, /* special_function */
955          "R_PPC_EMB_NADDR16_HA", /* name */
956          FALSE,                 /* partial_inplace */
957          0,                     /* src_mask */
958          0xffff,                /* dst_mask */
959          FALSE),                /* pcrel_offset */
960
961   /* 16 bit value resulting from allocating a 4 byte word to hold an
962      address in the .sdata section, and returning the offset from
963      _SDA_BASE_ for that relocation.  */
964   HOWTO (R_PPC_EMB_SDAI16,      /* type */
965          0,                     /* rightshift */
966          1,                     /* size (0 = byte, 1 = short, 2 = long) */
967          16,                    /* bitsize */
968          FALSE,                 /* pc_relative */
969          0,                     /* bitpos */
970          complain_overflow_bitfield, /* complain_on_overflow */
971          bfd_elf_generic_reloc, /* special_function */
972          "R_PPC_EMB_SDAI16",    /* name */
973          FALSE,                 /* partial_inplace */
974          0,                     /* src_mask */
975          0xffff,                /* dst_mask */
976          FALSE),                /* pcrel_offset */
977
978   /* 16 bit value resulting from allocating a 4 byte word to hold an
979      address in the .sdata2 section, and returning the offset from
980      _SDA2_BASE_ for that relocation */
981   HOWTO (R_PPC_EMB_SDA2I16,     /* type */
982          0,                     /* rightshift */
983          1,                     /* size (0 = byte, 1 = short, 2 = long) */
984          16,                    /* bitsize */
985          FALSE,                 /* pc_relative */
986          0,                     /* bitpos */
987          complain_overflow_bitfield, /* complain_on_overflow */
988          bfd_elf_generic_reloc, /* special_function */
989          "R_PPC_EMB_SDA2I16",   /* name */
990          FALSE,                 /* partial_inplace */
991          0,                     /* src_mask */
992          0xffff,                /* dst_mask */
993          FALSE),                /* pcrel_offset */
994
995   /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
996      small data items.   */
997   HOWTO (R_PPC_EMB_SDA2REL,     /* type */
998          0,                     /* rightshift */
999          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1000          16,                    /* bitsize */
1001          FALSE,                 /* pc_relative */
1002          0,                     /* bitpos */
1003          complain_overflow_signed, /* complain_on_overflow */
1004          bfd_elf_generic_reloc, /* special_function */
1005          "R_PPC_EMB_SDA2REL",   /* name */
1006          FALSE,                 /* partial_inplace */
1007          0,                     /* src_mask */
1008          0xffff,                /* dst_mask */
1009          FALSE),                /* pcrel_offset */
1010
1011   /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1012      signed offset from the appropriate base, and filling in the register
1013      field with the appropriate register (0, 2, or 13).  */
1014   HOWTO (R_PPC_EMB_SDA21,       /* type */
1015          0,                     /* rightshift */
1016          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1017          16,                    /* bitsize */
1018          FALSE,                 /* pc_relative */
1019          0,                     /* bitpos */
1020          complain_overflow_signed, /* complain_on_overflow */
1021          bfd_elf_generic_reloc, /* special_function */
1022          "R_PPC_EMB_SDA21",     /* name */
1023          FALSE,                 /* partial_inplace */
1024          0,                     /* src_mask */
1025          0xffff,                /* dst_mask */
1026          FALSE),                /* pcrel_offset */
1027
1028   /* Relocation not handled: R_PPC_EMB_MRKREF */
1029   /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1030   /* Relocation not handled: R_PPC_EMB_RELST_LO */
1031   /* Relocation not handled: R_PPC_EMB_RELST_HI */
1032   /* Relocation not handled: R_PPC_EMB_RELST_HA */
1033   /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1034
1035   /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1036      in the 16 bit signed offset from the appropriate base, and filling in the
1037      register field with the appropriate register (0, 2, or 13).  */
1038   HOWTO (R_PPC_EMB_RELSDA,      /* type */
1039          0,                     /* rightshift */
1040          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1041          16,                    /* bitsize */
1042          TRUE,                  /* pc_relative */
1043          0,                     /* bitpos */
1044          complain_overflow_signed, /* complain_on_overflow */
1045          bfd_elf_generic_reloc, /* special_function */
1046          "R_PPC_EMB_RELSDA",    /* name */
1047          FALSE,                 /* partial_inplace */
1048          0,                     /* src_mask */
1049          0xffff,                /* dst_mask */
1050          FALSE),                /* pcrel_offset */
1051
1052   /* GNU extension to record C++ vtable hierarchy.  */
1053   HOWTO (R_PPC_GNU_VTINHERIT,   /* type */
1054          0,                     /* rightshift */
1055          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1056          0,                     /* bitsize */
1057          FALSE,                 /* pc_relative */
1058          0,                     /* bitpos */
1059          complain_overflow_dont, /* complain_on_overflow */
1060          NULL,                  /* special_function */
1061          "R_PPC_GNU_VTINHERIT", /* name */
1062          FALSE,                 /* partial_inplace */
1063          0,                     /* src_mask */
1064          0,                     /* dst_mask */
1065          FALSE),                /* pcrel_offset */
1066
1067   /* GNU extension to record C++ vtable member usage.  */
1068   HOWTO (R_PPC_GNU_VTENTRY,     /* type */
1069          0,                     /* rightshift */
1070          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1071          0,                     /* bitsize */
1072          FALSE,                 /* pc_relative */
1073          0,                     /* bitpos */
1074          complain_overflow_dont, /* complain_on_overflow */
1075          NULL,                  /* special_function */
1076          "R_PPC_GNU_VTENTRY",   /* name */
1077          FALSE,                 /* partial_inplace */
1078          0,                     /* src_mask */
1079          0,                     /* dst_mask */
1080          FALSE),                /* pcrel_offset */
1081
1082   /* Phony reloc to handle AIX style TOC entries.  */
1083   HOWTO (R_PPC_TOC16,           /* type */
1084          0,                     /* rightshift */
1085          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1086          16,                    /* bitsize */
1087          FALSE,                 /* pc_relative */
1088          0,                     /* bitpos */
1089          complain_overflow_signed, /* complain_on_overflow */
1090          bfd_elf_generic_reloc, /* special_function */
1091          "R_PPC_TOC16",         /* name */
1092          FALSE,                 /* partial_inplace */
1093          0,                     /* src_mask */
1094          0xffff,                /* dst_mask */
1095          FALSE),                /* pcrel_offset */
1096 };
1097 \f
1098 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
1099
1100 static void
1101 ppc_elf_howto_init ()
1102 {
1103   unsigned int i, type;
1104
1105   for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
1106     {
1107       type = ppc_elf_howto_raw[i].type;
1108       BFD_ASSERT (type < sizeof (ppc_elf_howto_table) / sizeof (ppc_elf_howto_table[0]));
1109       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1110     }
1111 }
1112 \f
1113 /* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
1114
1115    The MPC860, revision C0 or earlier contains a bug in the die.
1116    If all of the following conditions are true, the next instruction
1117    to be executed *may* be treated as a no-op.
1118    1/ A forward branch is executed.
1119    2/ The branch is predicted as not taken.
1120    3/ The branch is taken.
1121    4/ The branch is located in the last 5 words of a page.
1122       (The EOP limit is 5 by default but may be specified as any value from 1-10.)
1123
1124    Our software solution is to detect these problematic branches in a
1125    linker pass and modify them as follows:
1126    1/ Unconditional branches - Since these are always predicted taken,
1127       there is no problem and no action is required.
1128    2/ Conditional backward branches - No problem, no action required.
1129    3/ Conditional forward branches - Ensure that the "inverse prediction
1130       bit" is set (ensure it is predicted taken).
1131    4/ Conditional register branches - Ensure that the "y bit" is set
1132       (ensure it is predicted taken).  */
1133
1134 /* Sort sections by address.  */
1135
1136 static int
1137 ppc_elf_sort_rela (arg1, arg2)
1138      const PTR arg1;
1139      const PTR arg2;
1140 {
1141   const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
1142   const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
1143
1144   /* Sort by offset.  */
1145   return ((*rela1)->r_offset - (*rela2)->r_offset);
1146 }
1147
1148 static bfd_boolean
1149 ppc_elf_relax_section (abfd, isec, link_info, again)
1150      bfd *abfd;
1151      asection *isec;
1152      struct bfd_link_info *link_info;
1153      bfd_boolean *again;
1154 {
1155 #define PAGESIZE 0x1000
1156
1157   bfd_byte *contents = NULL;
1158   bfd_byte *free_contents = NULL;
1159   Elf_Internal_Rela *internal_relocs = NULL;
1160   Elf_Internal_Rela *free_relocs = NULL;
1161   Elf_Internal_Rela **rela_comb = NULL;
1162   int comb_curr, comb_count;
1163
1164   /* We never have to do this more than once per input section.  */
1165   *again = FALSE;
1166
1167   /* If needed, initialize this section's cooked size.  */
1168   if (isec->_cooked_size == 0)
1169       isec->_cooked_size = isec->_raw_size;
1170
1171   /* We're only interested in text sections which overlap the
1172      troublesome area at the end of a page.  */
1173   if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1174     {
1175       bfd_vma dot, end_page, end_section;
1176       bfd_boolean section_modified;
1177
1178       /* Get the section contents.  */
1179       /* Get cached copy if it exists.  */
1180       if (elf_section_data (isec)->this_hdr.contents != NULL)
1181           contents = elf_section_data (isec)->this_hdr.contents;
1182       else
1183         {
1184           /* Go get them off disk.  */
1185           contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
1186           if (contents == NULL)
1187             goto error_return;
1188           free_contents = contents;
1189
1190           if (! bfd_get_section_contents (abfd, isec, contents,
1191                                           (file_ptr) 0, isec->_raw_size))
1192             goto error_return;
1193         }
1194
1195       comb_curr = 0;
1196       comb_count = 0;
1197       if (isec->reloc_count)
1198         {
1199           unsigned n;
1200           bfd_size_type amt;
1201
1202           /* Get a copy of the native relocations.  */
1203           internal_relocs = _bfd_elf32_link_read_relocs (
1204             abfd, isec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1205             link_info->keep_memory);
1206           if (internal_relocs == NULL)
1207               goto error_return;
1208           if (! link_info->keep_memory)
1209               free_relocs = internal_relocs;
1210
1211           /* Setup a faster access method for the reloc info we need.  */
1212           amt = isec->reloc_count;
1213           amt *= sizeof (Elf_Internal_Rela*);
1214           rela_comb = (Elf_Internal_Rela**) bfd_malloc (amt);
1215           if (rela_comb == NULL)
1216               goto error_return;
1217           for (n = 0; n < isec->reloc_count; ++n)
1218             {
1219               long r_type;
1220
1221               r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
1222               if (r_type < 0 || r_type >= (int) R_PPC_max)
1223                   goto error_return;
1224
1225               /* Prologue constants are sometimes present in the ".text"
1226               sections and they can be identified by their associated relocation.
1227               We don't want to process those words and some others which
1228               can also be identified by their relocations.  However, not all
1229               conditional branches will have a relocation so we will
1230               only ignore words that 1) have a reloc, and 2) the reloc
1231               is not applicable to a conditional branch.
1232               The array rela_comb is built here for use in the EOP scan loop.  */
1233               switch (r_type)
1234                 {
1235                 case R_PPC_ADDR14_BRNTAKEN:     /* Absolute, predicted not taken */
1236                 case R_PPC_REL14:               /* Relative cond. br.  */
1237                 case R_PPC_REL14_BRNTAKEN:      /* Rel. cond. br., predicted not taken */
1238                   /* We should check the instruction.  */
1239                   break;
1240                 default:
1241                   /* The word is not a conditional branch - ignore it.  */
1242                   rela_comb[comb_count++] = &internal_relocs[n];
1243                   break;
1244                 }
1245             }
1246           if (comb_count > 1)
1247             qsort (rela_comb, (size_t) comb_count, sizeof (int), ppc_elf_sort_rela);
1248         }
1249
1250       /* Enumerate each EOP region that overlaps this section.  */
1251       end_section = isec->vma + isec->_cooked_size;
1252       dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1253       dot -= link_info->mpc860c0;
1254       section_modified = FALSE;
1255       if (dot < isec->vma)      /* Increment the start position if this section */
1256           dot = isec->vma;      /* begins in the middle of its first EOP region.  */
1257       for (;
1258            dot < end_section;
1259            dot += PAGESIZE, end_page += PAGESIZE)
1260         {
1261
1262           /* Check each word in this EOP region.  */
1263           for (; dot < end_page; dot += 4)
1264             {
1265               bfd_vma isec_offset;
1266               unsigned long insn;
1267               bfd_boolean skip, modified;
1268
1269               /* Don't process this word if there is a relocation for it and
1270                  the relocation indicates the word is not a conditional branch.  */
1271               skip = FALSE;
1272               isec_offset = dot - isec->vma;
1273               for (; comb_curr<comb_count; ++comb_curr)
1274                 {
1275                   bfd_vma r_offset;
1276
1277                   r_offset = rela_comb[comb_curr]->r_offset;
1278                   if (r_offset >= isec_offset)
1279                     {
1280                       if (r_offset == isec_offset) skip = TRUE;
1281                       break;
1282                     }
1283                 }
1284               if (skip) continue;
1285
1286               /* Check the current word for a problematic conditional branch.  */
1287 #define BO0(insn) ((insn) & 0x02000000)
1288 #define BO2(insn) ((insn) & 0x00800000)
1289 #define BO4(insn) ((insn) & 0x00200000)
1290               insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1291               modified = FALSE;
1292               if ((insn & 0xFc000000) == 0x40000000)
1293                 {
1294                   /* Instruction is BCx */
1295                   if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1296                     {
1297                       bfd_vma target;
1298                       /* This branch is predicted as "normal".
1299                          If this is a forward branch, it is problematic.  */
1300
1301                       target = insn & 0x0000Fffc;               /*extract*/
1302                       target = (target ^ 0x8000) - 0x8000;      /*sign extend*/
1303                       if ((insn & 0x00000002) == 0)
1304                           target += dot;                        /*convert to abs*/
1305                       if (target > dot)
1306                         {
1307                           insn |= 0x00200000;   /* Set the prediction bit.  */
1308                           modified = TRUE;
1309                         }
1310                     }
1311                 }
1312               else if ((insn & 0xFc00Fffe) == 0x4c000420)
1313                 {
1314                   /* Instruction is BCCTRx.  */
1315                   if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1316                     {
1317                       /* This branch is predicted as not-taken.
1318                          If this is a forward branch, it is problematic.
1319                          Since we can't tell statically if it will branch forward,
1320                          always set the prediction bit.  */
1321                       insn |= 0x00200000;   /* Set the prediction bit.  */
1322                       modified = TRUE;
1323                     }
1324                 }
1325               else if ((insn & 0xFc00Fffe) == 0x4c000020)
1326                 {
1327                   /* Instruction is BCLRx */
1328                   if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1329                     {
1330                       /* This branch is predicted as not-taken.
1331                          If this is a forward branch, it is problematic.
1332                          Since we can't tell statically if it will branch forward,
1333                          always set the prediction bit.  */
1334                       insn |= 0x00200000;   /* Set the prediction bit.  */
1335                       modified = TRUE;
1336                     }
1337                 }
1338 #undef BO0
1339 #undef BO2
1340 #undef BO4
1341               if (modified)
1342                 {
1343                   bfd_put_32 (abfd, (bfd_vma) insn, contents + isec_offset);
1344                   section_modified = TRUE;
1345                 }
1346             }
1347         }
1348       if (section_modified)
1349         {
1350           elf_section_data (isec)->this_hdr.contents = contents;
1351           free_contents = NULL;
1352         }
1353     }
1354
1355   if (rela_comb != NULL)
1356     {
1357       free (rela_comb);
1358       rela_comb = NULL;
1359     }
1360
1361   if (free_relocs != NULL)
1362     {
1363       free (free_relocs);
1364       free_relocs = NULL;
1365     }
1366
1367   if (free_contents != NULL)
1368     {
1369       if (! link_info->keep_memory)
1370         free (free_contents);
1371       else
1372         {
1373           /* Cache the section contents for elf_link_input_bfd.  */
1374           elf_section_data (isec)->this_hdr.contents = contents;
1375         }
1376       free_contents = NULL;
1377     }
1378
1379   return TRUE;
1380
1381 error_return:
1382   if (rela_comb != NULL)
1383     free (rela_comb);
1384   if (free_relocs != NULL)
1385     free (free_relocs);
1386   if (free_contents != NULL)
1387     free (free_contents);
1388   return FALSE;
1389 }
1390 \f
1391 static reloc_howto_type *
1392 ppc_elf_reloc_type_lookup (abfd, code)
1393      bfd *abfd ATTRIBUTE_UNUSED;
1394      bfd_reloc_code_real_type code;
1395 {
1396   enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1397
1398   if (!ppc_elf_howto_table[R_PPC_ADDR32])
1399     /* Initialize howto table if needed.  */
1400     ppc_elf_howto_init ();
1401
1402   switch ((int) code)
1403     {
1404     default:
1405       return (reloc_howto_type *) NULL;
1406
1407     case BFD_RELOC_NONE:                ppc_reloc = R_PPC_NONE;                 break;
1408     case BFD_RELOC_32:                  ppc_reloc = R_PPC_ADDR32;               break;
1409     case BFD_RELOC_PPC_BA26:            ppc_reloc = R_PPC_ADDR24;               break;
1410     case BFD_RELOC_16:                  ppc_reloc = R_PPC_ADDR16;               break;
1411     case BFD_RELOC_LO16:                ppc_reloc = R_PPC_ADDR16_LO;            break;
1412     case BFD_RELOC_HI16:                ppc_reloc = R_PPC_ADDR16_HI;            break;
1413     case BFD_RELOC_HI16_S:              ppc_reloc = R_PPC_ADDR16_HA;            break;
1414     case BFD_RELOC_PPC_BA16:            ppc_reloc = R_PPC_ADDR14;               break;
1415     case BFD_RELOC_PPC_BA16_BRTAKEN:    ppc_reloc = R_PPC_ADDR14_BRTAKEN;       break;
1416     case BFD_RELOC_PPC_BA16_BRNTAKEN:   ppc_reloc = R_PPC_ADDR14_BRNTAKEN;      break;
1417     case BFD_RELOC_PPC_B26:             ppc_reloc = R_PPC_REL24;                break;
1418     case BFD_RELOC_PPC_B16:             ppc_reloc = R_PPC_REL14;                break;
1419     case BFD_RELOC_PPC_B16_BRTAKEN:     ppc_reloc = R_PPC_REL14_BRTAKEN;        break;
1420     case BFD_RELOC_PPC_B16_BRNTAKEN:    ppc_reloc = R_PPC_REL14_BRNTAKEN;       break;
1421     case BFD_RELOC_16_GOTOFF:           ppc_reloc = R_PPC_GOT16;                break;
1422     case BFD_RELOC_LO16_GOTOFF:         ppc_reloc = R_PPC_GOT16_LO;             break;
1423     case BFD_RELOC_HI16_GOTOFF:         ppc_reloc = R_PPC_GOT16_HI;             break;
1424     case BFD_RELOC_HI16_S_GOTOFF:       ppc_reloc = R_PPC_GOT16_HA;             break;
1425     case BFD_RELOC_24_PLT_PCREL:        ppc_reloc = R_PPC_PLTREL24;             break;
1426     case BFD_RELOC_PPC_COPY:            ppc_reloc = R_PPC_COPY;                 break;
1427     case BFD_RELOC_PPC_GLOB_DAT:        ppc_reloc = R_PPC_GLOB_DAT;             break;
1428     case BFD_RELOC_PPC_LOCAL24PC:       ppc_reloc = R_PPC_LOCAL24PC;            break;
1429     case BFD_RELOC_32_PCREL:            ppc_reloc = R_PPC_REL32;                break;
1430     case BFD_RELOC_32_PLTOFF:           ppc_reloc = R_PPC_PLT32;                break;
1431     case BFD_RELOC_32_PLT_PCREL:        ppc_reloc = R_PPC_PLTREL32;             break;
1432     case BFD_RELOC_LO16_PLTOFF:         ppc_reloc = R_PPC_PLT16_LO;             break;
1433     case BFD_RELOC_HI16_PLTOFF:         ppc_reloc = R_PPC_PLT16_HI;             break;
1434     case BFD_RELOC_HI16_S_PLTOFF:       ppc_reloc = R_PPC_PLT16_HA;             break;
1435     case BFD_RELOC_GPREL16:             ppc_reloc = R_PPC_SDAREL16;             break;
1436     case BFD_RELOC_16_BASEREL:          ppc_reloc = R_PPC_SECTOFF;              break;
1437     case BFD_RELOC_LO16_BASEREL:        ppc_reloc = R_PPC_SECTOFF_LO;           break;
1438     case BFD_RELOC_HI16_BASEREL:        ppc_reloc = R_PPC_SECTOFF_HI;           break;
1439     case BFD_RELOC_HI16_S_BASEREL:      ppc_reloc = R_PPC_SECTOFF_HA;           break;
1440     case BFD_RELOC_CTOR:                ppc_reloc = R_PPC_ADDR32;               break;
1441     case BFD_RELOC_PPC_TOC16:           ppc_reloc = R_PPC_TOC16;                break;
1442     case BFD_RELOC_PPC_EMB_NADDR32:     ppc_reloc = R_PPC_EMB_NADDR32;          break;
1443     case BFD_RELOC_PPC_EMB_NADDR16:     ppc_reloc = R_PPC_EMB_NADDR16;          break;
1444     case BFD_RELOC_PPC_EMB_NADDR16_LO:  ppc_reloc = R_PPC_EMB_NADDR16_LO;       break;
1445     case BFD_RELOC_PPC_EMB_NADDR16_HI:  ppc_reloc = R_PPC_EMB_NADDR16_HI;       break;
1446     case BFD_RELOC_PPC_EMB_NADDR16_HA:  ppc_reloc = R_PPC_EMB_NADDR16_HA;       break;
1447     case BFD_RELOC_PPC_EMB_SDAI16:      ppc_reloc = R_PPC_EMB_SDAI16;           break;
1448     case BFD_RELOC_PPC_EMB_SDA2I16:     ppc_reloc = R_PPC_EMB_SDA2I16;          break;
1449     case BFD_RELOC_PPC_EMB_SDA2REL:     ppc_reloc = R_PPC_EMB_SDA2REL;          break;
1450     case BFD_RELOC_PPC_EMB_SDA21:       ppc_reloc = R_PPC_EMB_SDA21;            break;
1451     case BFD_RELOC_PPC_EMB_MRKREF:      ppc_reloc = R_PPC_EMB_MRKREF;           break;
1452     case BFD_RELOC_PPC_EMB_RELSEC16:    ppc_reloc = R_PPC_EMB_RELSEC16;         break;
1453     case BFD_RELOC_PPC_EMB_RELST_LO:    ppc_reloc = R_PPC_EMB_RELST_LO;         break;
1454     case BFD_RELOC_PPC_EMB_RELST_HI:    ppc_reloc = R_PPC_EMB_RELST_HI;         break;
1455     case BFD_RELOC_PPC_EMB_RELST_HA:    ppc_reloc = R_PPC_EMB_RELST_HA;         break;
1456     case BFD_RELOC_PPC_EMB_BIT_FLD:     ppc_reloc = R_PPC_EMB_BIT_FLD;          break;
1457     case BFD_RELOC_PPC_EMB_RELSDA:      ppc_reloc = R_PPC_EMB_RELSDA;           break;
1458     case BFD_RELOC_VTABLE_INHERIT:      ppc_reloc = R_PPC_GNU_VTINHERIT;        break;
1459     case BFD_RELOC_VTABLE_ENTRY:        ppc_reloc = R_PPC_GNU_VTENTRY;          break;
1460     }
1461
1462   return ppc_elf_howto_table[(int) ppc_reloc];
1463 };
1464
1465 /* Set the howto pointer for a PowerPC ELF reloc.  */
1466
1467 static void
1468 ppc_elf_info_to_howto (abfd, cache_ptr, dst)
1469      bfd *abfd ATTRIBUTE_UNUSED;
1470      arelent *cache_ptr;
1471      Elf_Internal_Rela *dst;
1472 {
1473   if (!ppc_elf_howto_table[R_PPC_ADDR32])
1474     /* Initialize howto table if needed.  */
1475     ppc_elf_howto_init ();
1476
1477   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1478   cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1479 }
1480
1481 /* Handle the R_PPC_ADDR16_HA reloc.  */
1482
1483 static bfd_reloc_status_type
1484 ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
1485                          output_bfd, error_message)
1486      bfd *abfd ATTRIBUTE_UNUSED;
1487      arelent *reloc_entry;
1488      asymbol *symbol;
1489      PTR data ATTRIBUTE_UNUSED;
1490      asection *input_section;
1491      bfd *output_bfd;
1492      char **error_message ATTRIBUTE_UNUSED;
1493 {
1494   bfd_vma relocation;
1495
1496   if (output_bfd != NULL)
1497     {
1498       reloc_entry->address += input_section->output_offset;
1499       return bfd_reloc_ok;
1500     }
1501
1502   if (reloc_entry->address > input_section->_cooked_size)
1503     return bfd_reloc_outofrange;
1504
1505   if (bfd_is_com_section (symbol->section))
1506     relocation = 0;
1507   else
1508     relocation = symbol->value;
1509
1510   relocation += symbol->section->output_section->vma;
1511   relocation += symbol->section->output_offset;
1512   relocation += reloc_entry->addend;
1513
1514   reloc_entry->addend += (relocation & 0x8000) << 1;
1515
1516   return bfd_reloc_continue;
1517 }
1518
1519 /* Fix bad default arch selected for a 32 bit input bfd when the
1520    default is 64 bit.  */
1521
1522 static bfd_boolean
1523 ppc_elf_object_p (abfd)
1524      bfd *abfd;
1525 {
1526   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1527     {
1528       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1529
1530       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1531         {
1532           /* Relies on arch after 64 bit default being 32 bit default.  */
1533           abfd->arch_info = abfd->arch_info->next;
1534           BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1535         }
1536     }
1537   return TRUE;
1538 }
1539
1540 /* Function to set whether a module needs the -mrelocatable bit set.  */
1541
1542 static bfd_boolean
1543 ppc_elf_set_private_flags (abfd, flags)
1544      bfd *abfd;
1545      flagword flags;
1546 {
1547   BFD_ASSERT (!elf_flags_init (abfd)
1548               || elf_elfheader (abfd)->e_flags == flags);
1549
1550   elf_elfheader (abfd)->e_flags = flags;
1551   elf_flags_init (abfd) = TRUE;
1552   return TRUE;
1553 }
1554
1555 /* Merge backend specific data from an object file to the output
1556    object file when linking.  */
1557
1558 static bfd_boolean
1559 ppc_elf_merge_private_bfd_data (ibfd, obfd)
1560      bfd *ibfd;
1561      bfd *obfd;
1562 {
1563   flagword old_flags;
1564   flagword new_flags;
1565   bfd_boolean error;
1566
1567   /* Check if we have the same endianess.  */
1568   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
1569     return FALSE;
1570
1571   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1572       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1573     return TRUE;
1574
1575   new_flags = elf_elfheader (ibfd)->e_flags;
1576   old_flags = elf_elfheader (obfd)->e_flags;
1577   if (!elf_flags_init (obfd))   /* First call, no flags set.  */
1578     {
1579       elf_flags_init (obfd) = TRUE;
1580       elf_elfheader (obfd)->e_flags = new_flags;
1581     }
1582
1583   else if (new_flags == old_flags)      /* Compatible flags are ok.  */
1584     ;
1585
1586   else                                  /* Incompatible flags.  */
1587     {
1588       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib to be linked
1589          with either.  */
1590       error = FALSE;
1591       if ((new_flags & EF_PPC_RELOCATABLE) != 0
1592           && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1593         {
1594           error = TRUE;
1595           (*_bfd_error_handler)
1596             (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
1597              bfd_archive_filename (ibfd));
1598         }
1599       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1600                && (old_flags & EF_PPC_RELOCATABLE) != 0)
1601         {
1602           error = TRUE;
1603           (*_bfd_error_handler)
1604             (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
1605              bfd_archive_filename (ibfd));
1606         }
1607
1608       /* The output is -mrelocatable-lib iff both the input files are.  */
1609       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1610         elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1611
1612       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1613          but each input file is either -mrelocatable or -mrelocatable-lib.  */
1614       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1615           && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1616           && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1617         elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1618
1619       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it.  */
1620       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1621
1622       new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1623       old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1624
1625       /* Warn about any other mismatches.  */
1626       if (new_flags != old_flags)
1627         {
1628           error = TRUE;
1629           (*_bfd_error_handler)
1630             (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1631              bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
1632         }
1633
1634       if (error)
1635         {
1636           bfd_set_error (bfd_error_bad_value);
1637           return FALSE;
1638         }
1639     }
1640
1641   return TRUE;
1642 }
1643 \f
1644 /* Handle a PowerPC specific section when reading an object file.  This
1645    is called when elfcode.h finds a section with an unknown type.  */
1646
1647 static bfd_boolean
1648 ppc_elf_section_from_shdr (abfd, hdr, name)
1649      bfd *abfd;
1650      Elf_Internal_Shdr *hdr;
1651      const char *name;
1652 {
1653   asection *newsect;
1654   flagword flags;
1655
1656   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1657     return FALSE;
1658
1659   newsect = hdr->bfd_section;
1660   flags = bfd_get_section_flags (abfd, newsect);
1661   if (hdr->sh_flags & SHF_EXCLUDE)
1662     flags |= SEC_EXCLUDE;
1663
1664   if (hdr->sh_type == SHT_ORDERED)
1665     flags |= SEC_SORT_ENTRIES;
1666
1667   bfd_set_section_flags (abfd, newsect, flags);
1668   return TRUE;
1669 }
1670 \f
1671 /* Set up any other section flags and such that may be necessary.  */
1672
1673 static bfd_boolean
1674 ppc_elf_fake_sections (abfd, shdr, asect)
1675      bfd *abfd ATTRIBUTE_UNUSED;
1676      Elf_Internal_Shdr *shdr;
1677      asection *asect;
1678 {
1679   if ((asect->flags & SEC_EXCLUDE) != 0)
1680     shdr->sh_flags |= SHF_EXCLUDE;
1681
1682   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1683     shdr->sh_type = SHT_ORDERED;
1684
1685   return TRUE;
1686 }
1687 \f
1688 /* Create a special linker section */
1689 static elf_linker_section_t *
1690 ppc_elf_create_linker_section (abfd, info, which)
1691      bfd *abfd;
1692      struct bfd_link_info *info;
1693      enum elf_linker_section_enum which;
1694 {
1695   bfd *dynobj = elf_hash_table (info)->dynobj;
1696   elf_linker_section_t *lsect;
1697
1698   /* Record the first bfd section that needs the special section.  */
1699   if (!dynobj)
1700     dynobj = elf_hash_table (info)->dynobj = abfd;
1701
1702   /* If this is the first time, create the section.  */
1703   lsect = elf_linker_section (dynobj, which);
1704   if (!lsect)
1705     {
1706       elf_linker_section_t defaults;
1707       static elf_linker_section_t zero_section;
1708
1709       defaults = zero_section;
1710       defaults.which = which;
1711       defaults.hole_written_p = FALSE;
1712       defaults.alignment = 2;
1713
1714       /* Both of these sections are (technically) created by the user
1715          putting data in them, so they shouldn't be marked
1716          SEC_LINKER_CREATED.
1717
1718          The linker creates them so it has somewhere to attach their
1719          respective symbols. In fact, if they were empty it would
1720          be OK to leave the symbol set to 0 (or any random number), because
1721          the appropriate register should never be used.  */
1722       defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1723                         | SEC_IN_MEMORY);
1724
1725       switch (which)
1726         {
1727         default:
1728           (*_bfd_error_handler) (_("%s: Unknown special linker type %d"),
1729                                  bfd_get_filename (abfd),
1730                                  (int) which);
1731
1732           bfd_set_error (bfd_error_bad_value);
1733           return (elf_linker_section_t *) 0;
1734
1735         case LINKER_SECTION_SDATA:      /* .sdata/.sbss section */
1736           defaults.name           = ".sdata";
1737           defaults.rel_name       = ".rela.sdata";
1738           defaults.bss_name       = ".sbss";
1739           defaults.sym_name       = "_SDA_BASE_";
1740           defaults.sym_offset     = 32768;
1741           break;
1742
1743         case LINKER_SECTION_SDATA2:     /* .sdata2/.sbss2 section */
1744           defaults.name           = ".sdata2";
1745           defaults.rel_name       = ".rela.sdata2";
1746           defaults.bss_name       = ".sbss2";
1747           defaults.sym_name       = "_SDA2_BASE_";
1748           defaults.sym_offset     = 32768;
1749           defaults.flags         |= SEC_READONLY;
1750           break;
1751         }
1752
1753       lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
1754     }
1755
1756   return lsect;
1757 }
1758 \f
1759 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
1760    need to bump up the number of section headers.  */
1761
1762 static int
1763 ppc_elf_additional_program_headers (abfd)
1764      bfd *abfd;
1765 {
1766   asection *s;
1767   int ret;
1768
1769   ret = 0;
1770
1771   s = bfd_get_section_by_name (abfd, ".interp");
1772   if (s != NULL)
1773     ++ret;
1774
1775   s = bfd_get_section_by_name (abfd, ".sbss2");
1776   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1777     ++ret;
1778
1779   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1780   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1781     ++ret;
1782
1783   return ret;
1784 }
1785
1786 /* Modify the segment map if needed.  */
1787
1788 static bfd_boolean
1789 ppc_elf_modify_segment_map (abfd)
1790      bfd *abfd ATTRIBUTE_UNUSED;
1791 {
1792   return TRUE;
1793 }
1794 \f
1795 /* The powerpc .got has a blrl instruction in it.  Mark it executable.  */
1796
1797 static asection *
1798 ppc_elf_create_got (abfd, info)
1799      bfd *abfd;
1800      struct bfd_link_info *info;
1801 {
1802   register asection *s;
1803   flagword flags;
1804
1805   if (!_bfd_elf_create_got_section (abfd, info))
1806     return NULL;
1807
1808   s = bfd_get_section_by_name (abfd, ".got");
1809   if (s == NULL)
1810     abort ();
1811
1812   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1813            | SEC_LINKER_CREATED);
1814   if (!bfd_set_section_flags (abfd, s, flags))
1815     return NULL;
1816   return s;
1817 }
1818
1819 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
1820    to output sections (just like _bfd_elf_create_dynamic_sections has
1821    to create .dynbss and .rela.bss).  */
1822
1823 static bfd_boolean
1824 ppc_elf_create_dynamic_sections (abfd, info)
1825      bfd *abfd;
1826      struct bfd_link_info *info;
1827 {
1828   register asection *s;
1829   flagword flags;
1830
1831   if (!ppc_elf_create_got (abfd, info))
1832     return FALSE;
1833
1834   if (!_bfd_elf_create_dynamic_sections (abfd, info))
1835     return FALSE;
1836
1837   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1838            | SEC_LINKER_CREATED);
1839
1840   s = bfd_make_section (abfd, ".dynsbss");
1841   if (s == NULL
1842       || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1843     return FALSE;
1844
1845   if (! info->shared)
1846     {
1847       s = bfd_make_section (abfd, ".rela.sbss");
1848       if (s == NULL
1849           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1850           || ! bfd_set_section_alignment (abfd, s, 2))
1851         return FALSE;
1852     }
1853
1854   s = bfd_get_section_by_name (abfd, ".plt");
1855   if (s == NULL)
1856     abort ();
1857
1858   flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
1859   return bfd_set_section_flags (abfd, s, flags);
1860 }
1861
1862 /* Adjust a symbol defined by a dynamic object and referenced by a
1863    regular object.  The current definition is in some section of the
1864    dynamic object, but we're not including those sections.  We have to
1865    change the definition to something the rest of the link can
1866    understand.  */
1867
1868 static bfd_boolean
1869 ppc_elf_adjust_dynamic_symbol (info, h)
1870      struct bfd_link_info *info;
1871      struct elf_link_hash_entry *h;
1872 {
1873   bfd *dynobj = elf_hash_table (info)->dynobj;
1874   asection *s;
1875   unsigned int power_of_two;
1876   bfd_vma plt_offset;
1877
1878 #ifdef DEBUG
1879   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n", h->root.root.string);
1880 #endif
1881
1882   /* Make sure we know what is going on here.  */
1883   BFD_ASSERT (dynobj != NULL
1884               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1885                   || h->weakdef != NULL
1886                   || ((h->elf_link_hash_flags
1887                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1888                       && (h->elf_link_hash_flags
1889                           & ELF_LINK_HASH_REF_REGULAR) != 0
1890                       && (h->elf_link_hash_flags
1891                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1892
1893   /* If this is a function, put it in the procedure linkage table.  We
1894      will fill in the contents of the procedure linkage table later,
1895      when we know the address of the .got section.  */
1896   if (h->type == STT_FUNC
1897       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1898     {
1899       if (! elf_hash_table (info)->dynamic_sections_created
1900           || SYMBOL_CALLS_LOCAL (info, h)
1901           || (info->shared && h->plt.refcount <= 0))
1902         {
1903           /* A PLT entry is not required/allowed when:
1904
1905              1. We are not using ld.so; because then the PLT entry
1906              can't be set up, so we can't use one.
1907
1908              2. We know for certain that a call to this symbol
1909              will go to this object.
1910
1911              3. GC has rendered the entry unused.
1912              Note, however, that in an executable all references to the
1913              symbol go to the PLT, so we can't turn it off in that case.
1914              ??? The correct thing to do here is to reference count
1915              all uses of the symbol, not just those to the GOT or PLT.  */
1916           h->plt.offset = (bfd_vma) -1;
1917           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1918           return TRUE;
1919         }
1920
1921       /* Make sure this symbol is output as a dynamic symbol.  */
1922       if (h->dynindx == -1)
1923         {
1924           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1925             return FALSE;
1926         }
1927       BFD_ASSERT (h->dynindx != -1);
1928
1929       s = bfd_get_section_by_name (dynobj, ".plt");
1930       BFD_ASSERT (s != NULL);
1931
1932       /* If this is the first .plt entry, make room for the special
1933          first entry.  */
1934       if (s->_raw_size == 0)
1935         s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
1936
1937       /* The PowerPC PLT is actually composed of two parts, the first part
1938          is 2 words (for a load and a jump), and then there is a remaining
1939          word available at the end.  */
1940       plt_offset = (PLT_INITIAL_ENTRY_SIZE
1941                     + (PLT_SLOT_SIZE
1942                        * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
1943                           / PLT_ENTRY_SIZE)));
1944
1945       /* If this symbol is not defined in a regular file, and we are
1946          not generating a shared library, then set the symbol to this
1947          location in the .plt.  This is required to make function
1948          pointers compare as equal between the normal executable and
1949          the shared library.  */
1950       if (! info->shared
1951           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1952         {
1953           h->root.u.def.section = s;
1954           h->root.u.def.value = plt_offset;
1955         }
1956
1957       h->plt.offset = plt_offset;
1958
1959       /* Make room for this entry.  After the 8192nd entry, room
1960          for two entries is allocated.  */
1961       if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
1962           >= PLT_NUM_SINGLE_ENTRIES)
1963         s->_raw_size += 2 * PLT_ENTRY_SIZE;
1964       else
1965         s->_raw_size += PLT_ENTRY_SIZE;
1966
1967       /* We also need to make an entry in the .rela.plt section.  */
1968       s = bfd_get_section_by_name (dynobj, ".rela.plt");
1969       BFD_ASSERT (s != NULL);
1970       s->_raw_size += sizeof (Elf32_External_Rela);
1971
1972       return TRUE;
1973     }
1974   else
1975     h->plt.offset = (bfd_vma) -1;
1976
1977   /* If this is a weak symbol, and there is a real definition, the
1978      processor independent code will have arranged for us to see the
1979      real definition first, and we can just use the same value.  */
1980   if (h->weakdef != NULL)
1981     {
1982       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1983                   || h->weakdef->root.type == bfd_link_hash_defweak);
1984       h->root.u.def.section = h->weakdef->root.u.def.section;
1985       h->root.u.def.value = h->weakdef->root.u.def.value;
1986       return TRUE;
1987     }
1988
1989   /* This is a reference to a symbol defined by a dynamic object which
1990      is not a function.  */
1991
1992   /* If we are creating a shared library, we must presume that the
1993      only references to the symbol are via the global offset table.
1994      For such cases we need not do anything here; the relocations will
1995      be handled correctly by relocate_section.  */
1996   if (info->shared)
1997     return TRUE;
1998
1999   /* We must allocate the symbol in our .dynbss section, which will
2000      become part of the .bss section of the executable.  There will be
2001      an entry for this symbol in the .dynsym section.  The dynamic
2002      object will contain position independent code, so all references
2003      from the dynamic object to this symbol will go through the global
2004      offset table.  The dynamic linker will use the .dynsym entry to
2005      determine the address it must put in the global offset table, so
2006      both the dynamic object and the regular object will refer to the
2007      same memory location for the variable.
2008
2009      Of course, if the symbol is sufficiently small, we must instead
2010      allocate it in .sbss.  FIXME: It would be better to do this if and
2011      only if there were actually SDAREL relocs for that symbol.  */
2012
2013   if (h->size <= elf_gp_size (dynobj))
2014     s = bfd_get_section_by_name (dynobj, ".dynsbss");
2015   else
2016     s = bfd_get_section_by_name (dynobj, ".dynbss");
2017   BFD_ASSERT (s != NULL);
2018
2019   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2020      copy the initial value out of the dynamic object and into the
2021      runtime process image.  We need to remember the offset into the
2022      .rela.bss section we are going to use.  */
2023   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2024     {
2025       asection *srel;
2026
2027       if (h->size <= elf_gp_size (dynobj))
2028         srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
2029       else
2030         srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2031       BFD_ASSERT (srel != NULL);
2032       srel->_raw_size += sizeof (Elf32_External_Rela);
2033       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2034     }
2035
2036   /* We need to figure out the alignment required for this symbol.  I
2037      have no idea how ELF linkers handle this.  */
2038   power_of_two = bfd_log2 (h->size);
2039   if (power_of_two > 4)
2040     power_of_two = 4;
2041
2042   /* Apply the required alignment.  */
2043   s->_raw_size = BFD_ALIGN (s->_raw_size,
2044                             (bfd_size_type) (1 << power_of_two));
2045   if (power_of_two > bfd_get_section_alignment (dynobj, s))
2046     {
2047       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2048         return FALSE;
2049     }
2050
2051   /* Define the symbol as being at this point in the section.  */
2052   h->root.u.def.section = s;
2053   h->root.u.def.value = s->_raw_size;
2054
2055   /* Increment the section size to make room for the symbol.  */
2056   s->_raw_size += h->size;
2057
2058   return TRUE;
2059 }
2060 \f
2061 /* Allocate space in associated reloc sections for dynamic relocs.  */
2062
2063 static bfd_boolean
2064 allocate_dynrelocs (h, info)
2065      struct elf_link_hash_entry *h;
2066      PTR info ATTRIBUTE_UNUSED;
2067 {
2068   struct ppc_elf_dyn_relocs *p;
2069
2070   if (h->root.type == bfd_link_hash_indirect)
2071     return TRUE;
2072
2073   if (h->root.type == bfd_link_hash_warning)
2074     /* When warning symbols are created, they **replace** the "real"
2075        entry in the hash table, thus we never get to see the real
2076        symbol in a hash traversal.  So look at it now.  */
2077     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2078
2079   for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2080     {
2081       asection *sreloc = elf_section_data (p->sec)->sreloc;
2082       sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
2083     }
2084
2085   return TRUE;
2086 }
2087
2088 /* Find any dynamic relocs that apply to read-only sections.  */
2089
2090 static bfd_boolean
2091 readonly_dynrelocs (h, info)
2092      struct elf_link_hash_entry *h;
2093      PTR info;
2094 {
2095   struct ppc_elf_dyn_relocs *p;
2096
2097   if (h->root.type == bfd_link_hash_indirect)
2098     return TRUE;
2099
2100   if (h->root.type == bfd_link_hash_warning)
2101     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2102
2103   for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2104     {
2105       asection *s = p->sec->output_section;
2106
2107       if (s != NULL
2108           && ((s->flags & (SEC_READONLY | SEC_ALLOC))
2109               == (SEC_READONLY | SEC_ALLOC)))
2110         {
2111           ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
2112
2113           /* Not an error, just cut short the traversal.  */
2114           return FALSE;
2115         }
2116     }
2117   return TRUE;
2118 }
2119
2120 /* Set the sizes of the dynamic sections.  */
2121
2122 static bfd_boolean
2123 ppc_elf_size_dynamic_sections (output_bfd, info)
2124      bfd *output_bfd ATTRIBUTE_UNUSED;
2125      struct bfd_link_info *info;
2126 {
2127   bfd *dynobj;
2128   asection *s;
2129   bfd_boolean plt;
2130   bfd_boolean relocs;
2131   bfd *ibfd;
2132
2133 #ifdef DEBUG
2134   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
2135 #endif
2136
2137   dynobj = elf_hash_table (info)->dynobj;
2138   BFD_ASSERT (dynobj != NULL);
2139
2140   if (elf_hash_table (info)->dynamic_sections_created)
2141     {
2142       /* Set the contents of the .interp section to the interpreter.  */
2143       if (! info->shared)
2144         {
2145           s = bfd_get_section_by_name (dynobj, ".interp");
2146           BFD_ASSERT (s != NULL);
2147           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2148           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2149         }
2150     }
2151   else
2152     {
2153       /* We may have created entries in the .rela.got, .rela.sdata, and
2154          .rela.sdata2 sections.  However, if we are not creating the
2155          dynamic sections, we will not actually use these entries.  Reset
2156          the size of .rela.got, et al, which will cause it to get
2157          stripped from the output file below.  */
2158       static char *rela_sections[] = { ".rela.got", ".rela.sdata",
2159                                        ".rela.sdata2", ".rela.sbss",
2160                                        (char *) 0 };
2161       char **p;
2162
2163       for (p = rela_sections; *p != (char *) 0; p++)
2164         {
2165           s = bfd_get_section_by_name (dynobj, *p);
2166           if (s != NULL)
2167             s->_raw_size = 0;
2168         }
2169     }
2170
2171   /* Allocate space for local sym dynamic relocs.  */
2172   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2173     {
2174       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2175         continue;
2176
2177       for (s = ibfd->sections; s != NULL; s = s->next)
2178         {
2179           struct ppc_elf_dyn_relocs *p;
2180
2181           for (p = ((struct ppc_elf_dyn_relocs *)
2182                    elf_section_data (s)->local_dynrel);
2183               p != NULL;
2184               p = p->next)
2185             {
2186               if (!bfd_is_abs_section (p->sec)
2187                   && bfd_is_abs_section (p->sec->output_section))
2188                 {
2189                   /* Input section has been discarded, either because
2190                      it is a copy of a linkonce section or due to
2191                      linker script /DISCARD/, so we'll be discarding
2192                      the relocs too.  */
2193                 }
2194               else if (p->count != 0)
2195                 {
2196                   elf_section_data (p->sec)->sreloc->_raw_size
2197                     += p->count * sizeof (Elf32_External_Rela);
2198                   if ((p->sec->output_section->flags
2199                        & (SEC_READONLY | SEC_ALLOC))
2200                       == (SEC_READONLY | SEC_ALLOC))
2201                     info->flags |= DF_TEXTREL;
2202                 }
2203             }
2204         }
2205     }
2206
2207   /* Allocate space for global sym dynamic relocs.  */
2208   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, NULL);
2209
2210   /* The check_relocs and adjust_dynamic_symbol entry points have
2211      determined the sizes of the various dynamic sections.  Allocate
2212      memory for them.  */
2213   plt = FALSE;
2214   relocs = FALSE;
2215   for (s = dynobj->sections; s != NULL; s = s->next)
2216     {
2217       const char *name;
2218       bfd_boolean strip;
2219
2220       if ((s->flags & SEC_LINKER_CREATED) == 0)
2221         continue;
2222
2223       /* It's OK to base decisions on the section name, because none
2224          of the dynobj section names depend upon the input files.  */
2225       name = bfd_get_section_name (dynobj, s);
2226
2227       strip = FALSE;
2228
2229       if (strcmp (name, ".plt") == 0)
2230         {
2231           if (s->_raw_size == 0)
2232             {
2233               /* Strip this section if we don't need it; see the
2234                  comment below.  */
2235               strip = TRUE;
2236             }
2237           else
2238             {
2239               /* Remember whether there is a PLT.  */
2240               plt = TRUE;
2241             }
2242         }
2243       else if (strncmp (name, ".rela", 5) == 0)
2244         {
2245           if (s->_raw_size == 0)
2246             {
2247               /* If we don't need this section, strip it from the
2248                  output file.  This is mostly to handle .rela.bss and
2249                  .rela.plt.  We must create both sections in
2250                  create_dynamic_sections, because they must be created
2251                  before the linker maps input sections to output
2252                  sections.  The linker does that before
2253                  adjust_dynamic_symbol is called, and it is that
2254                  function which decides whether anything needs to go
2255                  into these sections.  */
2256               strip = TRUE;
2257             }
2258           else
2259             {
2260               /* Remember whether there are any relocation sections.  */
2261               relocs = TRUE;
2262
2263               /* We use the reloc_count field as a counter if we need
2264                  to copy relocs into the output file.  */
2265               s->reloc_count = 0;
2266             }
2267         }
2268       else if (strcmp (name, ".got") != 0
2269                && strcmp (name, ".sdata") != 0
2270                && strcmp (name, ".sdata2") != 0)
2271         {
2272           /* It's not one of our sections, so don't allocate space.  */
2273           continue;
2274         }
2275
2276       if (strip)
2277         {
2278           _bfd_strip_section_from_output (info, s);
2279           continue;
2280         }
2281
2282       /* Allocate memory for the section contents.  */
2283       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2284       if (s->contents == NULL && s->_raw_size != 0)
2285         return FALSE;
2286     }
2287
2288   if (elf_hash_table (info)->dynamic_sections_created)
2289     {
2290       /* Add some entries to the .dynamic section.  We fill in the
2291          values later, in ppc_elf_finish_dynamic_sections, but we
2292          must add the entries now so that we get the correct size for
2293          the .dynamic section.  The DT_DEBUG entry is filled in by the
2294          dynamic linker and used by the debugger.  */
2295 #define add_dynamic_entry(TAG, VAL) \
2296   bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2297
2298       if (!info->shared)
2299         {
2300           if (!add_dynamic_entry (DT_DEBUG, 0))
2301             return FALSE;
2302         }
2303
2304       if (plt)
2305         {
2306           if (!add_dynamic_entry (DT_PLTGOT, 0)
2307               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2308               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2309               || !add_dynamic_entry (DT_JMPREL, 0))
2310             return FALSE;
2311         }
2312
2313       if (relocs)
2314         {
2315           if (!add_dynamic_entry (DT_RELA, 0)
2316               || !add_dynamic_entry (DT_RELASZ, 0)
2317               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2318             return FALSE;
2319         }
2320
2321       /* If any dynamic relocs apply to a read-only section, then we
2322          need a DT_TEXTREL entry.  */
2323       if ((info->flags & DF_TEXTREL) == 0)
2324         elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
2325                                 (PTR) info);
2326
2327       if ((info->flags & DF_TEXTREL) != 0)
2328         {
2329           if (!add_dynamic_entry (DT_TEXTREL, 0))
2330             return FALSE;
2331         }
2332     }
2333 #undef add_dynamic_entry
2334
2335   return TRUE;
2336 }
2337 \f
2338 /* Look through the relocs for a section during the first phase, and
2339    allocate space in the global offset table or procedure linkage
2340    table.  */
2341
2342 static bfd_boolean
2343 ppc_elf_check_relocs (abfd, info, sec, relocs)
2344      bfd *abfd;
2345      struct bfd_link_info *info;
2346      asection *sec;
2347      const Elf_Internal_Rela *relocs;
2348 {
2349   bfd *dynobj;
2350   Elf_Internal_Shdr *symtab_hdr;
2351   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2352   const Elf_Internal_Rela *rel;
2353   const Elf_Internal_Rela *rel_end;
2354   bfd_signed_vma *local_got_refcounts;
2355   elf_linker_section_t *sdata;
2356   elf_linker_section_t *sdata2;
2357   asection *sreloc;
2358   asection *sgot = NULL;
2359   asection *srelgot = NULL;
2360
2361   if (info->relocateable)
2362     return TRUE;
2363
2364 #ifdef DEBUG
2365   fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
2366            bfd_get_section_name (abfd, sec),
2367            bfd_archive_filename (abfd));
2368 #endif
2369
2370   /* Create the linker generated sections all the time so that the
2371      special symbols are created.  */
2372
2373   if ((sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA)) == NULL)
2374     {
2375       sdata = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2376       if (!sdata)
2377         return FALSE;
2378     }
2379
2380   if ((sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2)) == NULL)
2381     {
2382       sdata2 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA2);
2383       if (!sdata2)
2384         return FALSE;
2385     }
2386
2387   dynobj = elf_hash_table (info)->dynobj;
2388   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2389   local_got_refcounts = elf_local_got_refcounts (abfd);
2390
2391   sym_hashes = elf_sym_hashes (abfd);
2392   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2393   if (!elf_bad_symtab (abfd))
2394     sym_hashes_end -= symtab_hdr->sh_info;
2395
2396   sreloc = NULL;
2397
2398   rel_end = relocs + sec->reloc_count;
2399   for (rel = relocs; rel < rel_end; rel++)
2400     {
2401       unsigned long r_symndx;
2402       struct elf_link_hash_entry *h;
2403
2404       r_symndx = ELF32_R_SYM (rel->r_info);
2405       if (r_symndx < symtab_hdr->sh_info)
2406         h = NULL;
2407       else
2408         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2409
2410       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2411          This shows up in particular in an R_PPC_ADDR32 in the eabi
2412          startup code.  */
2413       if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2414         {
2415           if (sgot == NULL)
2416             {
2417               if (dynobj == NULL)
2418                 elf_hash_table (info)->dynobj = dynobj = abfd;
2419               sgot = ppc_elf_create_got (dynobj, info);
2420               if (sgot == NULL)
2421                 return FALSE;
2422             }
2423         }
2424
2425       switch (ELF32_R_TYPE (rel->r_info))
2426         {
2427         /* GOT16 relocations */
2428         case R_PPC_GOT16:
2429         case R_PPC_GOT16_LO:
2430         case R_PPC_GOT16_HI:
2431         case R_PPC_GOT16_HA:
2432           /* This symbol requires a global offset table entry.  */
2433
2434           if (sgot == NULL)
2435             {
2436               if (dynobj == NULL)
2437                 elf_hash_table (info)->dynobj = dynobj = abfd;
2438               sgot = ppc_elf_create_got (dynobj, info);
2439               if (sgot == NULL)
2440                 return FALSE;
2441             }
2442
2443           if (srelgot == NULL
2444               && (h != NULL || info->shared))
2445             {
2446               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2447               if (srelgot == NULL)
2448                 {
2449                   srelgot = bfd_make_section (dynobj, ".rela.got");
2450                   if (srelgot == NULL
2451                       || ! bfd_set_section_flags (dynobj, srelgot,
2452                                                   (SEC_ALLOC
2453                                                    | SEC_LOAD
2454                                                    | SEC_HAS_CONTENTS
2455                                                    | SEC_IN_MEMORY
2456                                                    | SEC_LINKER_CREATED
2457                                                    | SEC_READONLY))
2458                       || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2459                     return FALSE;
2460                 }
2461             }
2462
2463           if (h != NULL)
2464             {
2465               if (h->got.refcount == 0)
2466                 {
2467                   /* Make sure this symbol is output as a dynamic symbol.  */
2468                   if (h->dynindx == -1)
2469                     if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2470                       return FALSE;
2471
2472                   /* Allocate space in the .got.  */
2473                   sgot->_raw_size += 4;
2474                   /* Allocate relocation space.  */
2475                   srelgot->_raw_size += sizeof (Elf32_External_Rela);
2476                 }
2477               h->got.refcount++;
2478             }
2479           else
2480             {
2481               /* This is a global offset table entry for a local symbol.  */
2482               if (local_got_refcounts == NULL)
2483                 {
2484                   bfd_size_type size;
2485
2486                   size = symtab_hdr->sh_info;
2487                   size *= sizeof (bfd_signed_vma);
2488                   local_got_refcounts
2489                     = (bfd_signed_vma *) bfd_zalloc (abfd, size);
2490                   if (local_got_refcounts == NULL)
2491                     return FALSE;
2492                   elf_local_got_refcounts (abfd) = local_got_refcounts;
2493                 }
2494               if (local_got_refcounts[r_symndx] == 0)
2495                 {
2496                   sgot->_raw_size += 4;
2497
2498                   /* If we are generating a shared object, we need to
2499                      output a R_PPC_RELATIVE reloc so that the
2500                      dynamic linker can adjust this GOT entry.  */
2501                   if (info->shared)
2502                     srelgot->_raw_size += sizeof (Elf32_External_Rela);
2503                 }
2504               local_got_refcounts[r_symndx]++;
2505             }
2506           break;
2507
2508         /* Indirect .sdata relocation.  */
2509         case R_PPC_EMB_SDAI16:
2510           if (info->shared)
2511             {
2512               ((*_bfd_error_handler)
2513                (_("%s: relocation %s cannot be used when making a shared object"),
2514                 bfd_archive_filename (abfd), "R_PPC_EMB_SDAI16"));
2515               return FALSE;
2516             }
2517
2518           if (srelgot == NULL && (h != NULL || info->shared))
2519             {
2520               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2521               if (srelgot == NULL)
2522                 {
2523                   srelgot = bfd_make_section (dynobj, ".rela.got");
2524                   if (srelgot == NULL
2525                       || ! bfd_set_section_flags (dynobj, srelgot,
2526                                                   (SEC_ALLOC
2527                                                    | SEC_LOAD
2528                                                    | SEC_HAS_CONTENTS
2529                                                    | SEC_IN_MEMORY
2530                                                    | SEC_LINKER_CREATED
2531                                                    | SEC_READONLY))
2532                       || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2533                     return FALSE;
2534                 }
2535             }
2536
2537           if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata, h, rel))
2538             return FALSE;
2539
2540           break;
2541
2542         /* Indirect .sdata2 relocation.  */
2543         case R_PPC_EMB_SDA2I16:
2544           if (info->shared)
2545             {
2546               ((*_bfd_error_handler)
2547                (_("%s: relocation %s cannot be used when making a shared object"),
2548                 bfd_archive_filename (abfd), "R_PPC_EMB_SDA2I16"));
2549               return FALSE;
2550             }
2551
2552           if (srelgot == NULL && (h != NULL || info->shared))
2553             {
2554               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2555               if (srelgot == NULL)
2556                 {
2557                   srelgot = bfd_make_section (dynobj, ".rela.got");
2558                   if (srelgot == NULL
2559                       || ! bfd_set_section_flags (dynobj, srelgot,
2560                                                   (SEC_ALLOC
2561                                                    | SEC_LOAD
2562                                                    | SEC_HAS_CONTENTS
2563                                                    | SEC_IN_MEMORY
2564                                                    | SEC_LINKER_CREATED
2565                                                    | SEC_READONLY))
2566                       || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2567                     return FALSE;
2568                 }
2569             }
2570
2571           if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata2, h, rel))
2572             return FALSE;
2573
2574           break;
2575
2576         case R_PPC_SDAREL16:
2577         case R_PPC_EMB_SDA2REL:
2578         case R_PPC_EMB_SDA21:
2579           if (info->shared)
2580             {
2581               ((*_bfd_error_handler)
2582                (_("%s: relocation %s cannot be used when making a shared object"),
2583                 bfd_archive_filename (abfd),
2584                 ppc_elf_howto_table[(int) ELF32_R_TYPE (rel->r_info)]->name));
2585               return FALSE;
2586             }
2587           break;
2588
2589         case R_PPC_PLT32:
2590         case R_PPC_PLTREL24:
2591         case R_PPC_PLT16_LO:
2592         case R_PPC_PLT16_HI:
2593         case R_PPC_PLT16_HA:
2594 #ifdef DEBUG
2595           fprintf (stderr, "Reloc requires a PLT entry\n");
2596 #endif
2597           /* This symbol requires a procedure linkage table entry.  We
2598              actually build the entry in adjust_dynamic_symbol,
2599              because this might be a case of linking PIC code without
2600              linking in any dynamic objects, in which case we don't
2601              need to generate a procedure linkage table after all.  */
2602
2603           if (h == NULL)
2604             {
2605               /* It does not make sense to have a procedure linkage
2606                  table entry for a local symbol.  */
2607               bfd_set_error (bfd_error_bad_value);
2608               return FALSE;
2609             }
2610
2611           /* Make sure this symbol is output as a dynamic symbol.  */
2612           if (h->dynindx == -1)
2613             {
2614               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2615                 return FALSE;
2616             }
2617           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2618           h->plt.refcount++;
2619           break;
2620
2621           /* The following relocations don't need to propagate the
2622              relocation if linking a shared object since they are
2623              section relative.  */
2624         case R_PPC_SECTOFF:
2625         case R_PPC_SECTOFF_LO:
2626         case R_PPC_SECTOFF_HI:
2627         case R_PPC_SECTOFF_HA:
2628           break;
2629
2630           /* This refers only to functions defined in the shared library.  */
2631         case R_PPC_LOCAL24PC:
2632           break;
2633
2634           /* This relocation describes the C++ object vtable hierarchy.
2635              Reconstruct it for later use during GC.  */
2636         case R_PPC_GNU_VTINHERIT:
2637           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2638             return FALSE;
2639           break;
2640
2641           /* This relocation describes which C++ vtable entries are actually
2642              used.  Record for later use during GC.  */
2643         case R_PPC_GNU_VTENTRY:
2644           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2645             return FALSE;
2646           break;
2647
2648           /* When creating a shared object, we must copy these
2649              relocs into the output file.  We create a reloc
2650              section in dynobj and make room for the reloc.  */
2651         case R_PPC_REL24:
2652         case R_PPC_REL14:
2653         case R_PPC_REL14_BRTAKEN:
2654         case R_PPC_REL14_BRNTAKEN:
2655         case R_PPC_REL32:
2656           if (h == NULL
2657               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2658               || SYMBOL_REFERENCES_LOCAL (info, h))
2659             break;
2660           /* fall through */
2661
2662         default:
2663           if (info->shared)
2664             {
2665               struct ppc_elf_dyn_relocs *p;
2666               struct ppc_elf_dyn_relocs **head;
2667
2668 #ifdef DEBUG
2669               fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
2670                        (h && h->root.root.string) ? h->root.root.string : "<unknown>");
2671 #endif
2672               if (sreloc == NULL)
2673                 {
2674                   const char *name;
2675
2676                   name = (bfd_elf_string_from_elf_section
2677                           (abfd,
2678                            elf_elfheader (abfd)->e_shstrndx,
2679                            elf_section_data (sec)->rel_hdr.sh_name));
2680                   if (name == NULL)
2681                     return FALSE;
2682
2683                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2684                               && strcmp (bfd_get_section_name (abfd, sec),
2685                                          name + 5) == 0);
2686
2687                   sreloc = bfd_get_section_by_name (dynobj, name);
2688                   if (sreloc == NULL)
2689                     {
2690                       flagword flags;
2691
2692                       sreloc = bfd_make_section (dynobj, name);
2693                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
2694                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2695                       if ((sec->flags & SEC_ALLOC) != 0)
2696                         flags |= SEC_ALLOC | SEC_LOAD;
2697                       if (sreloc == NULL
2698                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
2699                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
2700                         return FALSE;
2701                     }
2702                   elf_section_data (sec)->sreloc = sreloc;
2703                 }
2704
2705               /* If this is a global symbol, we count the number of
2706                  relocations we need for this symbol.  */
2707               if (h != NULL)
2708                 {
2709                   head = &ppc_elf_hash_entry (h)->dyn_relocs;
2710                 }
2711               else
2712                 {
2713                   /* Track dynamic relocs needed for local syms too.
2714                      We really need local syms available to do this
2715                      easily.  Oh well.  */
2716
2717                   asection *s;
2718                   s = (bfd_section_from_r_symndx
2719                        (abfd, &ppc_elf_hash_table (info)->sym_sec,
2720                         sec, r_symndx));
2721                   if (s == NULL)
2722                     return FALSE;
2723
2724                   head = ((struct ppc_elf_dyn_relocs **)
2725                           &elf_section_data (s)->local_dynrel);
2726                 }
2727
2728               p = *head;
2729               if (p == NULL || p->sec != sec)
2730                 {
2731                   p = ((struct ppc_elf_dyn_relocs *)
2732                        bfd_alloc (elf_hash_table (info)->dynobj, sizeof *p));
2733                   if (p == NULL)
2734                     return FALSE;
2735                   p->next = *head;
2736                   *head = p;
2737                   p->sec = sec;
2738                   p->count = 0;
2739                 }
2740
2741               p->count++;
2742             }
2743
2744           break;
2745         }
2746     }
2747
2748   return TRUE;
2749 }
2750
2751 /* Return the section that should be marked against GC for a given
2752    relocation.  */
2753
2754 static asection *
2755 ppc_elf_gc_mark_hook (sec, info, rel, h, sym)
2756      asection *sec;
2757      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2758      Elf_Internal_Rela *rel;
2759      struct elf_link_hash_entry *h;
2760      Elf_Internal_Sym *sym;
2761 {
2762   if (h != NULL)
2763     {
2764       switch (ELF32_R_TYPE (rel->r_info))
2765         {
2766         case R_PPC_GNU_VTINHERIT:
2767         case R_PPC_GNU_VTENTRY:
2768           break;
2769
2770         default:
2771           switch (h->root.type)
2772             {
2773             case bfd_link_hash_defined:
2774             case bfd_link_hash_defweak:
2775               return h->root.u.def.section;
2776
2777             case bfd_link_hash_common:
2778               return h->root.u.c.p->section;
2779
2780             default:
2781               break;
2782             }
2783         }
2784     }
2785   else
2786     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2787
2788   return NULL;
2789 }
2790
2791 /* Update the got entry reference counts for the section being removed.  */
2792
2793 static bfd_boolean
2794 ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
2795      bfd *abfd;
2796      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2797      asection *sec;
2798      const Elf_Internal_Rela *relocs;
2799 {
2800   Elf_Internal_Shdr *symtab_hdr;
2801   struct elf_link_hash_entry **sym_hashes;
2802   bfd_signed_vma *local_got_refcounts;
2803   const Elf_Internal_Rela *rel, *relend;
2804   unsigned long r_symndx;
2805   struct elf_link_hash_entry *h;
2806
2807   elf_section_data (sec)->local_dynrel = NULL;
2808
2809   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2810   sym_hashes = elf_sym_hashes (abfd);
2811   local_got_refcounts = elf_local_got_refcounts (abfd);
2812
2813   relend = relocs + sec->reloc_count;
2814   for (rel = relocs; rel < relend; rel++)
2815     switch (ELF32_R_TYPE (rel->r_info))
2816       {
2817       case R_PPC_GOT16:
2818       case R_PPC_GOT16_LO:
2819       case R_PPC_GOT16_HI:
2820       case R_PPC_GOT16_HA:
2821         r_symndx = ELF32_R_SYM (rel->r_info);
2822         if (r_symndx >= symtab_hdr->sh_info)
2823           {
2824             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2825             if (h->got.refcount > 0)
2826               h->got.refcount--;
2827           }
2828         else if (local_got_refcounts != NULL)
2829           {
2830             if (local_got_refcounts[r_symndx] > 0)
2831               local_got_refcounts[r_symndx]--;
2832           }
2833         break;
2834
2835       case R_PPC_PLT32:
2836       case R_PPC_PLTREL24:
2837       case R_PPC_PLT16_LO:
2838       case R_PPC_PLT16_HI:
2839       case R_PPC_PLT16_HA:
2840         r_symndx = ELF32_R_SYM (rel->r_info);
2841         if (r_symndx >= symtab_hdr->sh_info)
2842           {
2843             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2844             if (h->plt.refcount > 0)
2845               h->plt.refcount--;
2846           }
2847         /* Fall through */
2848
2849       default:
2850         r_symndx = ELF32_R_SYM (rel->r_info);
2851         if (r_symndx >= symtab_hdr->sh_info)
2852           {
2853             struct ppc_elf_dyn_relocs **pp, *p;
2854
2855             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2856
2857             for (pp = &ppc_elf_hash_entry (h)->dyn_relocs;
2858                  (p = *pp) != NULL;
2859                  pp = &p->next)
2860               if (p->sec == sec)
2861                 {
2862                   if (--p->count == 0)
2863                     *pp = p->next;
2864                   break;
2865                 }
2866           }
2867         break;
2868       }
2869
2870   return TRUE;
2871 }
2872 \f
2873 /* Hook called by the linker routine which adds symbols from an object
2874    file.  We use it to put .comm items in .sbss, and not .bss.  */
2875
2876 static bfd_boolean
2877 ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2878      bfd *abfd;
2879      struct bfd_link_info *info;
2880      const Elf_Internal_Sym *sym;
2881      const char **namep ATTRIBUTE_UNUSED;
2882      flagword *flagsp ATTRIBUTE_UNUSED;
2883      asection **secp;
2884      bfd_vma *valp;
2885 {
2886   if (sym->st_shndx == SHN_COMMON
2887       && !info->relocateable
2888       && sym->st_size <= elf_gp_size (abfd)
2889       && info->hash->creator->flavour == bfd_target_elf_flavour)
2890     {
2891       /* Common symbols less than or equal to -G nn bytes are automatically
2892          put into .sdata.  */
2893       elf_linker_section_t *sdata
2894         = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2895
2896       if (!sdata->bss_section)
2897         {
2898           bfd_size_type amt;
2899
2900           /* We don't go through bfd_make_section, because we don't
2901              want to attach this common section to DYNOBJ.  The linker
2902              will move the symbols to the appropriate output section
2903              when it defines common symbols.  */
2904           amt = sizeof (asection);
2905           sdata->bss_section = (asection *) bfd_zalloc (abfd, amt);
2906           if (sdata->bss_section == NULL)
2907             return FALSE;
2908           sdata->bss_section->name = sdata->bss_name;
2909           sdata->bss_section->flags = SEC_IS_COMMON;
2910           sdata->bss_section->output_section = sdata->bss_section;
2911           amt = sizeof (asymbol);
2912           sdata->bss_section->symbol = (asymbol *) bfd_zalloc (abfd, amt);
2913           amt = sizeof (asymbol *);
2914           sdata->bss_section->symbol_ptr_ptr =
2915             (asymbol **) bfd_zalloc (abfd, amt);
2916           if (sdata->bss_section->symbol == NULL
2917               || sdata->bss_section->symbol_ptr_ptr == NULL)
2918             return FALSE;
2919           sdata->bss_section->symbol->name = sdata->bss_name;
2920           sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
2921           sdata->bss_section->symbol->section = sdata->bss_section;
2922           *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
2923         }
2924
2925       *secp = sdata->bss_section;
2926       *valp = sym->st_size;
2927     }
2928
2929   return TRUE;
2930 }
2931 \f
2932 /* Finish up dynamic symbol handling.  We set the contents of various
2933    dynamic sections here.  */
2934
2935 static bfd_boolean
2936 ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
2937      bfd *output_bfd;
2938      struct bfd_link_info *info;
2939      struct elf_link_hash_entry *h;
2940      Elf_Internal_Sym *sym;
2941 {
2942   bfd *dynobj;
2943
2944 #ifdef DEBUG
2945   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
2946            h->root.root.string);
2947 #endif
2948
2949   dynobj = elf_hash_table (info)->dynobj;
2950   BFD_ASSERT (dynobj != NULL);
2951
2952   if (h->plt.offset != (bfd_vma) -1)
2953     {
2954       asection *splt;
2955       asection *srela;
2956       Elf_Internal_Rela rela;
2957       bfd_byte *loc;
2958       bfd_vma reloc_index;
2959
2960 #ifdef DEBUG
2961       fprintf (stderr, ", plt_offset = %d", h->plt.offset);
2962 #endif
2963
2964       /* This symbol has an entry in the procedure linkage table.  Set
2965          it up.  */
2966
2967       BFD_ASSERT (h->dynindx != -1);
2968
2969       splt = bfd_get_section_by_name (dynobj, ".plt");
2970       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2971       BFD_ASSERT (splt != NULL && srela != NULL);
2972
2973       /* We don't need to fill in the .plt.  The ppc dynamic linker
2974          will fill it in.  */
2975
2976       /* Fill in the entry in the .rela.plt section.  */
2977       rela.r_offset = (splt->output_section->vma
2978                        + splt->output_offset
2979                        + h->plt.offset);
2980       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
2981       rela.r_addend = 0;
2982
2983       reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
2984       if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
2985         reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
2986       loc = srela->contents + reloc_index * sizeof (Elf32_External_Rela);
2987       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2988
2989       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2990         {
2991           /* Mark the symbol as undefined, rather than as defined in
2992              the .plt section.  Leave the value alone.  */
2993           sym->st_shndx = SHN_UNDEF;
2994           /* If the symbol is weak, we do need to clear the value.
2995              Otherwise, the PLT entry would provide a definition for
2996              the symbol even if the symbol wasn't defined anywhere,
2997              and so the symbol would never be NULL.  */
2998           if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
2999               == 0)
3000             sym->st_value = 0;
3001         }
3002     }
3003
3004   if (h->got.offset != (bfd_vma) -1)
3005     {
3006       asection *sgot;
3007       asection *srela;
3008       Elf_Internal_Rela rela;
3009       bfd_byte *loc;
3010
3011       /* This symbol has an entry in the global offset table.  Set it
3012          up.  */
3013
3014       sgot = bfd_get_section_by_name (dynobj, ".got");
3015       srela = bfd_get_section_by_name (dynobj, ".rela.got");
3016       BFD_ASSERT (sgot != NULL && srela != NULL);
3017
3018       rela.r_offset = (sgot->output_section->vma
3019                        + sgot->output_offset
3020                        + (h->got.offset &~ (bfd_vma) 1));
3021
3022       /* If this is a -Bsymbolic link, and the symbol is defined
3023          locally, we just want to emit a RELATIVE reloc.  The entry in
3024          the global offset table will already have been initialized in
3025          the relocate_section function.  */
3026       if (info->shared
3027           && SYMBOL_REFERENCES_LOCAL (info, h))
3028         {
3029           rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3030           rela.r_addend = (h->root.u.def.value
3031                            + h->root.u.def.section->output_section->vma
3032                            + h->root.u.def.section->output_offset);
3033         }
3034       else
3035         {
3036           BFD_ASSERT ((h->got.offset & 1) == 0);
3037           rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_GLOB_DAT);
3038           rela.r_addend = 0;
3039         }
3040
3041       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3042       loc = srela->contents;
3043       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3044       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3045     }
3046
3047   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3048     {
3049       asection *s;
3050       Elf_Internal_Rela rela;
3051       bfd_byte *loc;
3052
3053       /* This symbols needs a copy reloc.  Set it up.  */
3054
3055 #ifdef DEBUG
3056       fprintf (stderr, ", copy");
3057 #endif
3058
3059       BFD_ASSERT (h->dynindx != -1);
3060
3061       if (h->size <= elf_gp_size (dynobj))
3062         s = bfd_get_section_by_name (h->root.u.def.section->owner,
3063                                      ".rela.sbss");
3064       else
3065         s = bfd_get_section_by_name (h->root.u.def.section->owner,
3066                                      ".rela.bss");
3067       BFD_ASSERT (s != NULL);
3068
3069       rela.r_offset = (h->root.u.def.value
3070                        + h->root.u.def.section->output_section->vma
3071                        + h->root.u.def.section->output_offset);
3072       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
3073       rela.r_addend = 0;
3074       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3075       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3076     }
3077
3078 #ifdef DEBUG
3079   fprintf (stderr, "\n");
3080 #endif
3081
3082   /* Mark some specially defined symbols as absolute.  */
3083   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3084       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3085       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3086     sym->st_shndx = SHN_ABS;
3087
3088   return TRUE;
3089 }
3090 \f
3091 /* Finish up the dynamic sections.  */
3092
3093 static bfd_boolean
3094 ppc_elf_finish_dynamic_sections (output_bfd, info)
3095      bfd *output_bfd;
3096      struct bfd_link_info *info;
3097 {
3098   asection *sdyn;
3099   bfd *dynobj = elf_hash_table (info)->dynobj;
3100   asection *sgot = bfd_get_section_by_name (dynobj, ".got");
3101
3102 #ifdef DEBUG
3103   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
3104 #endif
3105
3106   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3107
3108   if (elf_hash_table (info)->dynamic_sections_created)
3109     {
3110       asection *splt;
3111       Elf32_External_Dyn *dyncon, *dynconend;
3112
3113       splt = bfd_get_section_by_name (dynobj, ".plt");
3114       BFD_ASSERT (splt != NULL && sdyn != NULL);
3115
3116       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3117       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3118       for (; dyncon < dynconend; dyncon++)
3119         {
3120           Elf_Internal_Dyn dyn;
3121           const char *name;
3122           bfd_boolean size;
3123
3124           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3125
3126           switch (dyn.d_tag)
3127             {
3128             case DT_PLTGOT:   name = ".plt";      size = FALSE; break;
3129             case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE;  break;
3130             case DT_JMPREL:   name = ".rela.plt"; size = FALSE; break;
3131             default:          name = NULL;        size = FALSE; break;
3132             }
3133
3134           if (name != NULL)
3135             {
3136               asection *s;
3137
3138               s = bfd_get_section_by_name (output_bfd, name);
3139               if (s == NULL)
3140                 dyn.d_un.d_val = 0;
3141               else
3142                 {
3143                   if (! size)
3144                     dyn.d_un.d_ptr = s->vma;
3145                   else
3146                     {
3147                       if (s->_cooked_size != 0)
3148                         dyn.d_un.d_val = s->_cooked_size;
3149                       else
3150                         dyn.d_un.d_val = s->_raw_size;
3151                     }
3152                 }
3153               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3154             }
3155         }
3156     }
3157
3158   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
3159      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
3160   if (sgot)
3161     {
3162       unsigned char *contents = sgot->contents;
3163       bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
3164
3165       if (sdyn == NULL)
3166         bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
3167       else
3168         bfd_put_32 (output_bfd,
3169                     sdyn->output_section->vma + sdyn->output_offset,
3170                     contents+4);
3171
3172       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3173     }
3174
3175   return TRUE;
3176 }
3177 \f
3178 /* The RELOCATE_SECTION function is called by the ELF backend linker
3179    to handle the relocations for a section.
3180
3181    The relocs are always passed as Rela structures; if the section
3182    actually uses Rel structures, the r_addend field will always be
3183    zero.
3184
3185    This function is responsible for adjust the section contents as
3186    necessary, and (if using Rela relocs and generating a
3187    relocateable output file) adjusting the reloc addend as
3188    necessary.
3189
3190    This function does not have to worry about setting the reloc
3191    address or the reloc symbol index.
3192
3193    LOCAL_SYMS is a pointer to the swapped in local symbols.
3194
3195    LOCAL_SECTIONS is an array giving the section in the input file
3196    corresponding to the st_shndx field of each local symbol.
3197
3198    The global hash table entry for the global symbols can be found
3199    via elf_sym_hashes (input_bfd).
3200
3201    When generating relocateable output, this function must handle
3202    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
3203    going to be the section symbol corresponding to the output
3204    section, which means that the addend must be adjusted
3205    accordingly.  */
3206
3207 static bfd_boolean
3208 ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
3209                           contents, relocs, local_syms, local_sections)
3210      bfd *output_bfd;
3211      struct bfd_link_info *info;
3212      bfd *input_bfd;
3213      asection *input_section;
3214      bfd_byte *contents;
3215      Elf_Internal_Rela *relocs;
3216      Elf_Internal_Sym *local_syms;
3217      asection **local_sections;
3218 {
3219   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3220   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3221   bfd *dynobj = elf_hash_table (info)->dynobj;
3222   elf_linker_section_t *sdata = NULL;
3223   elf_linker_section_t *sdata2 = NULL;
3224   Elf_Internal_Rela *rel = relocs;
3225   Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
3226   asection *sreloc = NULL;
3227   asection *splt;
3228   asection *sgot;
3229   bfd_vma *local_got_offsets;
3230   bfd_boolean ret = TRUE;
3231   long insn;
3232
3233   if (dynobj)
3234     {
3235       sdata = elf_linker_section (dynobj, LINKER_SECTION_SDATA);
3236       sdata2 = elf_linker_section (dynobj, LINKER_SECTION_SDATA2);
3237     }
3238
3239 #ifdef DEBUG
3240   fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
3241            bfd_archive_filename (input_bfd),
3242            bfd_section_name(input_bfd, input_section),
3243            (long) input_section->reloc_count,
3244            (info->relocateable) ? " (relocatable)" : "");
3245 #endif
3246
3247   if (info->relocateable)
3248     return TRUE;
3249
3250   if (!ppc_elf_howto_table[R_PPC_ADDR32])
3251     /* Initialize howto table if needed.  */
3252     ppc_elf_howto_init ();
3253
3254   local_got_offsets = elf_local_got_offsets (input_bfd);
3255
3256   splt = sgot = NULL;
3257   if (dynobj != NULL)
3258     {
3259       splt = bfd_get_section_by_name (dynobj, ".plt");
3260       sgot = bfd_get_section_by_name (dynobj, ".got");
3261     }
3262
3263   for (; rel < relend; rel++)
3264     {
3265       enum elf_ppc_reloc_type r_type    = (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
3266       bfd_vma offset                    = rel->r_offset;
3267       bfd_vma addend                    = rel->r_addend;
3268       bfd_reloc_status_type r           = bfd_reloc_other;
3269       Elf_Internal_Sym *sym             = (Elf_Internal_Sym *) 0;
3270       asection *sec                     = (asection *) 0;
3271       struct elf_link_hash_entry *h     = (struct elf_link_hash_entry *) 0;
3272       const char *sym_name              = (const char *) 0;
3273       reloc_howto_type *howto;
3274       unsigned long r_symndx;
3275       bfd_vma relocation;
3276       int will_become_local;
3277
3278       /* Unknown relocation handling */
3279       if ((unsigned) r_type >= (unsigned) R_PPC_max
3280           || !ppc_elf_howto_table[(int) r_type])
3281         {
3282           (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
3283                                  bfd_archive_filename (input_bfd),
3284                                  (int) r_type);
3285
3286           bfd_set_error (bfd_error_bad_value);
3287           ret = FALSE;
3288           continue;
3289         }
3290
3291       howto = ppc_elf_howto_table[(int) r_type];
3292       r_symndx = ELF32_R_SYM (rel->r_info);
3293
3294       if (r_symndx < symtab_hdr->sh_info)
3295         {
3296           sym = local_syms + r_symndx;
3297           sec = local_sections[r_symndx];
3298           sym_name = "<local symbol>";
3299
3300           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
3301           addend = rel->r_addend;
3302           /* Relocs to local symbols are always resolved.  */
3303           will_become_local = 1;
3304         }
3305       else
3306         {
3307           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3308           while (h->root.type == bfd_link_hash_indirect
3309                  || h->root.type == bfd_link_hash_warning)
3310             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3311           sym_name = h->root.root.string;
3312
3313           /* Can this relocation be resolved immediately?  */
3314           will_become_local = SYMBOL_REFERENCES_LOCAL (info, h);
3315
3316           if (h->root.type == bfd_link_hash_defined
3317               || h->root.type == bfd_link_hash_defweak)
3318             {
3319               sec = h->root.u.def.section;
3320               if (((r_type == R_PPC_PLT32
3321                     || r_type == R_PPC_PLTREL24)
3322                    && splt != NULL
3323                    && h->plt.offset != (bfd_vma) -1)
3324                   || (r_type == R_PPC_LOCAL24PC
3325                       && sec->output_section == NULL)
3326                   || ((r_type == R_PPC_GOT16
3327                        || r_type == R_PPC_GOT16_LO
3328                        || r_type == R_PPC_GOT16_HI
3329                        || r_type == R_PPC_GOT16_HA)
3330                       && elf_hash_table (info)->dynamic_sections_created
3331                       && (! info->shared || ! will_become_local))
3332                   || (info->shared
3333                       && ! will_become_local
3334                       && ((input_section->flags & SEC_ALLOC) != 0
3335                           /* Testing SEC_DEBUGGING here may be wrong.
3336                              It's here to avoid a crash when
3337                              generating a shared library with DWARF
3338                              debugging information.  */
3339                           || ((input_section->flags & SEC_DEBUGGING) != 0
3340                               && (h->elf_link_hash_flags
3341                                   & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
3342                       && (r_type == R_PPC_ADDR32
3343                           || r_type == R_PPC_ADDR24
3344                           || r_type == R_PPC_ADDR16
3345                           || r_type == R_PPC_ADDR16_LO
3346                           || r_type == R_PPC_ADDR16_HI
3347                           || r_type == R_PPC_ADDR16_HA
3348                           || r_type == R_PPC_ADDR14
3349                           || r_type == R_PPC_ADDR14_BRTAKEN
3350                           || r_type == R_PPC_ADDR14_BRNTAKEN
3351                           || r_type == R_PPC_COPY
3352                           || r_type == R_PPC_GLOB_DAT
3353                           || r_type == R_PPC_JMP_SLOT
3354                           || r_type == R_PPC_UADDR32
3355                           || r_type == R_PPC_UADDR16
3356                           || r_type == R_PPC_SDAREL16
3357                           || r_type == R_PPC_EMB_NADDR32
3358                           || r_type == R_PPC_EMB_NADDR16
3359                           || r_type == R_PPC_EMB_NADDR16_LO
3360                           || r_type == R_PPC_EMB_NADDR16_HI
3361                           || r_type == R_PPC_EMB_NADDR16_HA
3362                           || r_type == R_PPC_EMB_SDAI16
3363                           || r_type == R_PPC_EMB_SDA2I16
3364                           || r_type == R_PPC_EMB_SDA2REL
3365                           || r_type == R_PPC_EMB_SDA21
3366                           || r_type == R_PPC_EMB_MRKREF
3367                           || r_type == R_PPC_EMB_BIT_FLD
3368                           || r_type == R_PPC_EMB_RELSDA
3369                           || ((r_type == R_PPC_REL24
3370                                || r_type == R_PPC_REL32
3371                                || r_type == R_PPC_REL14
3372                                || r_type == R_PPC_REL14_BRTAKEN
3373                                || r_type == R_PPC_REL14_BRNTAKEN
3374                                || r_type == R_PPC_RELATIVE)
3375                               && strcmp (h->root.root.string,
3376                                          "_GLOBAL_OFFSET_TABLE_") != 0))))
3377                 {
3378                   /* In these cases, we don't need the relocation
3379                      value.  We check specially because in some
3380                      obscure cases sec->output_section will be NULL.  */
3381                   relocation = 0;
3382                 }
3383               else if (sec->output_section == NULL)
3384                 {
3385                   (*_bfd_error_handler)
3386                     (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
3387                      bfd_archive_filename (input_bfd), h->root.root.string,
3388                      bfd_get_section_name (input_bfd, input_section));
3389                   relocation = 0;
3390                 }
3391               else
3392                 relocation = (h->root.u.def.value
3393                               + sec->output_section->vma
3394                               + sec->output_offset);
3395             }
3396           else if (h->root.type == bfd_link_hash_undefweak)
3397             relocation = 0;
3398           else if (info->shared
3399                    && !info->no_undefined
3400                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3401             relocation = 0;
3402           else
3403             {
3404               if (! (*info->callbacks->undefined_symbol) (info,
3405                                                          h->root.root.string,
3406                                                          input_bfd,
3407                                                          input_section,
3408                                                          rel->r_offset,
3409                                                          (!info->shared
3410                                                           || info->no_undefined
3411                                                           || ELF_ST_VISIBILITY (h->other))))
3412                 return FALSE;
3413               relocation = 0;
3414             }
3415         }
3416
3417       switch ((int) r_type)
3418         {
3419         default:
3420           (*_bfd_error_handler) (_("%s: unknown relocation type %d for symbol %s"),
3421                                  bfd_archive_filename (input_bfd),
3422                                  (int) r_type, sym_name);
3423
3424           bfd_set_error (bfd_error_bad_value);
3425           ret = FALSE;
3426           continue;
3427
3428         case (int) R_PPC_NONE:
3429           continue;
3430
3431         /* Relocations that need no special processing.  */
3432         case (int) R_PPC_LOCAL24PC:
3433           /* It makes no sense to point a local relocation
3434              at a symbol not in this object.  */
3435           if (h != NULL
3436               && (h->root.type == bfd_link_hash_defined
3437                   || h->root.type == bfd_link_hash_defweak)
3438               && sec->output_section == NULL)
3439             {
3440               if (! (*info->callbacks->undefined_symbol) (info,
3441                                                           h->root.root.string,
3442                                                           input_bfd,
3443                                                           input_section,
3444                                                           rel->r_offset,
3445                                                           TRUE))
3446                 return FALSE;
3447               continue;
3448             }
3449           break;
3450
3451         /* Relocations that may need to be propagated if this is a shared
3452            object.  */
3453         case (int) R_PPC_REL24:
3454         case (int) R_PPC_REL32:
3455         case (int) R_PPC_REL14:
3456           /* If these relocations are not to a named symbol, they can be
3457              handled right here, no need to bother the dynamic linker.  */
3458           if (h == NULL
3459               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3460               || SYMBOL_REFERENCES_LOCAL (info, h))
3461             break;
3462         /* fall through */
3463
3464         /* Relocations that always need to be propagated if this is a shared
3465            object.  */
3466         case (int) R_PPC_ADDR32:
3467         case (int) R_PPC_ADDR24:
3468         case (int) R_PPC_ADDR16:
3469         case (int) R_PPC_ADDR16_LO:
3470         case (int) R_PPC_ADDR16_HI:
3471         case (int) R_PPC_ADDR16_HA:
3472         case (int) R_PPC_ADDR14:
3473         case (int) R_PPC_UADDR32:
3474         case (int) R_PPC_UADDR16:
3475           if (info->shared && r_symndx != 0)
3476             {
3477               Elf_Internal_Rela outrel;
3478               bfd_byte *loc;
3479               int skip;
3480
3481 #ifdef DEBUG
3482               fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
3483                        (h && h->root.root.string) ? h->root.root.string : "<unknown>");
3484 #endif
3485
3486               /* When generating a shared object, these relocations
3487                  are copied into the output file to be resolved at run
3488                  time.  */
3489               if (sreloc == NULL)
3490                 {
3491                   const char *name;
3492
3493                   name = (bfd_elf_string_from_elf_section
3494                           (input_bfd,
3495                            elf_elfheader (input_bfd)->e_shstrndx,
3496                            elf_section_data (input_section)->rel_hdr.sh_name));
3497                   if (name == NULL)
3498                     return FALSE;
3499
3500                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3501                               && strcmp (bfd_get_section_name (input_bfd,
3502                                                                input_section),
3503                                          name + 5) == 0);
3504
3505                   sreloc = bfd_get_section_by_name (dynobj, name);
3506                   BFD_ASSERT (sreloc != NULL);
3507                 }
3508
3509               skip = 0;
3510
3511               outrel.r_offset =
3512                 _bfd_elf_section_offset (output_bfd, info, input_section,
3513                                          rel->r_offset);
3514               if (outrel.r_offset == (bfd_vma) -1
3515                   || outrel.r_offset == (bfd_vma) -2)
3516                 skip = (int) outrel.r_offset;
3517               outrel.r_offset += (input_section->output_section->vma
3518                                   + input_section->output_offset);
3519
3520               if (skip)
3521                 memset (&outrel, 0, sizeof outrel);
3522               /* h->dynindx may be -1 if this symbol was marked to
3523                  become local.  */
3524               else if (! will_become_local)
3525                 {
3526                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3527                   outrel.r_addend = rel->r_addend;
3528                 }
3529               else
3530                 {
3531                   outrel.r_addend = relocation + rel->r_addend;
3532
3533                   if (r_type == R_PPC_ADDR32)
3534                     outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3535                   else
3536                     {
3537                       long indx;
3538
3539                       if (h == NULL)
3540                         sec = local_sections[r_symndx];
3541                       else
3542                         {
3543                           BFD_ASSERT (h->root.type == bfd_link_hash_defined
3544                                       || (h->root.type
3545                                           == bfd_link_hash_defweak));
3546                           sec = h->root.u.def.section;
3547                         }
3548
3549                       if (bfd_is_abs_section (sec))
3550                         indx = 0;
3551                       else if (sec == NULL || sec->owner == NULL)
3552                         {
3553                           bfd_set_error (bfd_error_bad_value);
3554                           return FALSE;
3555                         }
3556                       else
3557                         {
3558                           asection *osec;
3559
3560                           /* We are turning this relocation into one
3561                              against a section symbol.  It would be
3562                              proper to subtract the symbol's value,
3563                              osec->vma, from the emitted reloc addend,
3564                              but ld.so expects buggy relocs.  */
3565                           osec = sec->output_section;
3566                           indx = elf_section_data (osec)->dynindx;
3567                           BFD_ASSERT (indx > 0);
3568 #ifdef DEBUG
3569                           if (indx <= 0)
3570                             {
3571                               printf ("indx=%d section=%s flags=%08x name=%s\n",
3572                                      indx, osec->name, osec->flags,
3573                                      h->root.root.string);
3574                             }
3575 #endif
3576                         }
3577
3578                       outrel.r_info = ELF32_R_INFO (indx, r_type);
3579                     }
3580                 }
3581
3582               loc = sreloc->contents;
3583               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3584               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3585
3586               if (skip == -1)
3587                 continue;
3588
3589               /* This reloc will be computed at runtime.  We clear the memory
3590                  so that it contains predictable value.  */
3591               if (! skip
3592                   && ((input_section->flags & SEC_ALLOC) != 0
3593                       || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
3594                 {
3595                   relocation = howto->pc_relative ? outrel.r_offset : 0;
3596                   addend = 0;
3597                   break;
3598                 }
3599             }
3600
3601           /* Arithmetic adjust relocations that aren't going into a
3602              shared object.  */
3603           if (r_type == R_PPC_ADDR16_HA
3604               /* It's just possible that this symbol is a weak symbol
3605                  that's not actually defined anywhere. In that case,
3606                  'sec' would be NULL, and we should leave the symbol
3607                  alone (it will be set to zero elsewhere in the link).  */
3608               && sec != NULL)
3609             {
3610               addend += ((relocation + addend) & 0x8000) << 1;
3611             }
3612           break;
3613
3614         /* Branch taken prediction relocations.  */
3615         case (int) R_PPC_ADDR14_BRTAKEN:
3616         case (int) R_PPC_REL14_BRTAKEN:
3617           insn = bfd_get_32 (output_bfd, contents + offset);
3618           if ((relocation - offset) & 0x8000)
3619             insn &= ~BRANCH_PREDICT_BIT;
3620           else
3621             insn |= BRANCH_PREDICT_BIT;
3622           bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3623           break;
3624
3625         /* Branch not taken predicition relocations.  */
3626         case (int) R_PPC_ADDR14_BRNTAKEN:
3627         case (int) R_PPC_REL14_BRNTAKEN:
3628           insn = bfd_get_32 (output_bfd, contents + offset);
3629           if ((relocation - offset) & 0x8000)
3630             insn |= BRANCH_PREDICT_BIT;
3631           else
3632             insn &= ~BRANCH_PREDICT_BIT;
3633           bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3634           break;
3635
3636         /* GOT16 relocations.  */
3637         case (int) R_PPC_GOT16:
3638         case (int) R_PPC_GOT16_LO:
3639         case (int) R_PPC_GOT16_HI:
3640         case (int) R_PPC_GOT16_HA:
3641           /* Relocation is to the entry for this symbol in the global
3642              offset table.  */
3643           BFD_ASSERT (sgot != NULL);
3644
3645           if (h != NULL)
3646             {
3647               bfd_vma off;
3648
3649               off = h->got.offset;
3650               BFD_ASSERT (off != (bfd_vma) -1);
3651
3652               if (! elf_hash_table (info)->dynamic_sections_created
3653                   || (info->shared
3654                       && SYMBOL_REFERENCES_LOCAL (info, h)))
3655                 {
3656                   /* This is actually a static link, or it is a
3657                      -Bsymbolic link and the symbol is defined
3658                      locally.  We must initialize this entry in the
3659                      global offset table.  Since the offset must
3660                      always be a multiple of 4, we use the least
3661                      significant bit to record whether we have
3662                      initialized it already.
3663
3664                      When doing a dynamic link, we create a .rela.got
3665                      relocation entry to initialize the value.  This
3666                      is done in the finish_dynamic_symbol routine.  */
3667                   if ((off & 1) != 0)
3668                     off &= ~1;
3669                   else
3670                     {
3671                       bfd_put_32 (output_bfd, relocation,
3672                                   sgot->contents + off);
3673                       h->got.offset |= 1;
3674                     }
3675                 }
3676
3677               relocation = sgot->output_offset + off - 4;
3678             }
3679           else
3680             {
3681               bfd_vma off;
3682
3683               BFD_ASSERT (local_got_offsets != NULL
3684                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
3685
3686               off = local_got_offsets[r_symndx];
3687
3688               /* The offset must always be a multiple of 4.  We use
3689                  the least significant bit to record whether we have
3690                  already processed this entry.  */
3691               if ((off & 1) != 0)
3692                 off &= ~1;
3693               else
3694                 {
3695
3696                   if (info->shared)
3697                     {
3698                       asection *srelgot;
3699                       Elf_Internal_Rela outrel;
3700                       bfd_byte *loc;
3701
3702                       /* We need to generate a R_PPC_RELATIVE reloc
3703                          for the dynamic linker.  */
3704                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3705                       BFD_ASSERT (srelgot != NULL);
3706
3707                       outrel.r_offset = (sgot->output_section->vma
3708                                          + sgot->output_offset
3709                                          + off);
3710                       outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3711                       outrel.r_addend = relocation;
3712                       loc = srelgot->contents;
3713                       loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3714                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3715                       relocation = 0;
3716                     }
3717
3718                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3719                   local_got_offsets[r_symndx] |= 1;
3720                 }
3721
3722               relocation = sgot->output_offset + off - 4;
3723             }
3724           if (r_type == R_PPC_GOT16_HA)
3725             addend += ((relocation + addend) & 0x8000) << 1;
3726           break;
3727
3728         /* Indirect .sdata relocation.  */
3729         case (int) R_PPC_EMB_SDAI16:
3730           BFD_ASSERT (sdata != NULL);
3731           relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3732                                                                 sdata, h, relocation, rel,
3733                                                                 R_PPC_RELATIVE);
3734           break;
3735
3736         /* Indirect .sdata2 relocation.  */
3737         case (int) R_PPC_EMB_SDA2I16:
3738           BFD_ASSERT (sdata2 != NULL);
3739           relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3740                                                                 sdata2, h, relocation, rel,
3741                                                                 R_PPC_RELATIVE);
3742           break;
3743
3744         /* Handle the TOC16 reloc.  We want to use the offset within the .got
3745            section, not the actual VMA.  This is appropriate when generating
3746            an embedded ELF object, for which the .got section acts like the
3747            AIX .toc section.  */
3748         case (int) R_PPC_TOC16:                 /* phony GOT16 relocations */
3749           BFD_ASSERT (sec != (asection *) 0);
3750           BFD_ASSERT (bfd_is_und_section (sec)
3751                       || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
3752                       || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
3753
3754           addend -= sec->output_section->vma + sec->output_offset + 0x8000;
3755           break;
3756
3757         case (int) R_PPC_PLTREL24:
3758           /* Relocation is to the entry for this symbol in the
3759              procedure linkage table.  */
3760           BFD_ASSERT (h != NULL);
3761
3762           if (h->plt.offset == (bfd_vma) -1
3763               || splt == NULL)
3764             {
3765               /* We didn't make a PLT entry for this symbol.  This
3766                  happens when statically linking PIC code, or when
3767                  using -Bsymbolic.  */
3768               break;
3769             }
3770
3771           relocation = (splt->output_section->vma
3772                         + splt->output_offset
3773                         + h->plt.offset);
3774           break;
3775
3776         /* Relocate against _SDA_BASE_.  */
3777         case (int) R_PPC_SDAREL16:
3778           {
3779             const char *name;
3780
3781             BFD_ASSERT (sec != (asection *) 0);
3782             name = bfd_get_section_name (abfd, sec->output_section);
3783             if (! ((strncmp (name, ".sdata", 6) == 0
3784                     && (name[6] == 0 || name[6] == '.'))
3785                    || (strncmp (name, ".sbss", 5) == 0
3786                        && (name[5] == 0 || name[5] == '.'))))
3787               {
3788                 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3789                                        bfd_archive_filename (input_bfd),
3790                                        sym_name,
3791                                        ppc_elf_howto_table[(int) r_type]->name,
3792                                        name);
3793               }
3794             addend -= (sdata->sym_hash->root.u.def.value
3795                        + sdata->sym_hash->root.u.def.section->output_section->vma
3796                        + sdata->sym_hash->root.u.def.section->output_offset);
3797           }
3798           break;
3799
3800         /* relocate against _SDA2_BASE_ */
3801         case (int) R_PPC_EMB_SDA2REL:
3802           {
3803             const char *name;
3804
3805             BFD_ASSERT (sec != (asection *) 0);
3806             name = bfd_get_section_name (abfd, sec->output_section);
3807             if (! (strncmp (name, ".sdata2", 7) == 0
3808                    || strncmp (name, ".sbss2", 6) == 0))
3809               {
3810                 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3811                                        bfd_archive_filename (input_bfd),
3812                                        sym_name,
3813                                        ppc_elf_howto_table[(int) r_type]->name,
3814                                        name);
3815
3816                 bfd_set_error (bfd_error_bad_value);
3817                 ret = FALSE;
3818                 continue;
3819               }
3820             addend -= (sdata2->sym_hash->root.u.def.value
3821                        + sdata2->sym_hash->root.u.def.section->output_section->vma
3822                        + sdata2->sym_hash->root.u.def.section->output_offset);
3823           }
3824           break;
3825
3826         /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
3827         case (int) R_PPC_EMB_SDA21:
3828         case (int) R_PPC_EMB_RELSDA:
3829           {
3830             const char *name;
3831             int reg;
3832
3833             BFD_ASSERT (sec != (asection *) 0);
3834             name = bfd_get_section_name (abfd, sec->output_section);
3835             if (((strncmp (name, ".sdata", 6) == 0
3836                   && (name[6] == 0 || name[6] == '.'))
3837                  || (strncmp (name, ".sbss", 5) == 0
3838                      && (name[5] == 0 || name[5] == '.'))))
3839               {
3840                 reg = 13;
3841                 addend -= (sdata->sym_hash->root.u.def.value
3842                            + sdata->sym_hash->root.u.def.section->output_section->vma
3843                            + sdata->sym_hash->root.u.def.section->output_offset);
3844               }
3845
3846             else if (strncmp (name, ".sdata2", 7) == 0
3847                      || strncmp (name, ".sbss2", 6) == 0)
3848               {
3849                 reg = 2;
3850                 addend -= (sdata2->sym_hash->root.u.def.value
3851                            + sdata2->sym_hash->root.u.def.section->output_section->vma
3852                            + sdata2->sym_hash->root.u.def.section->output_offset);
3853               }
3854
3855             else if (strcmp (name, ".PPC.EMB.sdata0") == 0
3856                      || strcmp (name, ".PPC.EMB.sbss0") == 0)
3857               {
3858                 reg = 0;
3859               }
3860
3861             else
3862               {
3863                 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3864                                        bfd_archive_filename (input_bfd),
3865                                        sym_name,
3866                                        ppc_elf_howto_table[(int) r_type]->name,
3867                                        name);
3868
3869                 bfd_set_error (bfd_error_bad_value);
3870                 ret = FALSE;
3871                 continue;
3872               }
3873
3874             if (r_type == R_PPC_EMB_SDA21)
3875               {                 /* fill in register field */
3876                 insn = bfd_get_32 (output_bfd, contents + offset);
3877                 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
3878                 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3879               }
3880           }
3881           break;
3882
3883         /* Relocate against the beginning of the section.  */
3884         case (int) R_PPC_SECTOFF:
3885         case (int) R_PPC_SECTOFF_LO:
3886         case (int) R_PPC_SECTOFF_HI:
3887           BFD_ASSERT (sec != (asection *) 0);
3888           addend -= sec->output_section->vma;
3889           break;
3890
3891         case (int) R_PPC_SECTOFF_HA:
3892           BFD_ASSERT (sec != (asection *) 0);
3893           addend -= sec->output_section->vma;
3894           addend += ((relocation + addend) & 0x8000) << 1;
3895           break;
3896
3897         /* Negative relocations */
3898         case (int) R_PPC_EMB_NADDR32:
3899         case (int) R_PPC_EMB_NADDR16:
3900         case (int) R_PPC_EMB_NADDR16_LO:
3901         case (int) R_PPC_EMB_NADDR16_HI:
3902           addend -= 2 * relocation;
3903           break;
3904
3905         case (int) R_PPC_EMB_NADDR16_HA:
3906           addend -= 2 * relocation;
3907           addend += ((relocation + addend) & 0x8000) << 1;
3908           break;
3909
3910         /* NOP relocation that prevents garbage collecting linkers from omitting a
3911            reference.  */
3912         case (int) R_PPC_EMB_MRKREF:
3913           continue;
3914
3915         case (int) R_PPC_COPY:
3916         case (int) R_PPC_GLOB_DAT:
3917         case (int) R_PPC_JMP_SLOT:
3918         case (int) R_PPC_RELATIVE:
3919         case (int) R_PPC_PLT32:
3920         case (int) R_PPC_PLTREL32:
3921         case (int) R_PPC_PLT16_LO:
3922         case (int) R_PPC_PLT16_HI:
3923         case (int) R_PPC_PLT16_HA:
3924         case (int) R_PPC_EMB_RELSEC16:
3925         case (int) R_PPC_EMB_RELST_LO:
3926         case (int) R_PPC_EMB_RELST_HI:
3927         case (int) R_PPC_EMB_RELST_HA:
3928         case (int) R_PPC_EMB_BIT_FLD:
3929           (*_bfd_error_handler) (_("%s: Relocation %s is not yet supported for symbol %s."),
3930                                  bfd_archive_filename (input_bfd),
3931                                  ppc_elf_howto_table[(int) r_type]->name,
3932                                  sym_name);
3933
3934           bfd_set_error (bfd_error_invalid_operation);
3935           ret = FALSE;
3936           continue;
3937
3938         case (int) R_PPC_GNU_VTINHERIT:
3939         case (int) R_PPC_GNU_VTENTRY:
3940           /* These are no-ops in the end.  */
3941           continue;
3942         }
3943
3944 #ifdef DEBUG
3945       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
3946                howto->name,
3947                (int) r_type,
3948                sym_name,
3949                r_symndx,
3950                (long) offset,
3951                (long) addend);
3952 #endif
3953
3954       r = _bfd_final_link_relocate (howto,
3955                                     input_bfd,
3956                                     input_section,
3957                                     contents,
3958                                     offset,
3959                                     relocation,
3960                                     addend);
3961
3962       if (r == bfd_reloc_ok)
3963         ;
3964       else if (r == bfd_reloc_overflow)
3965         {
3966           const char *name;
3967
3968           if (h != NULL)
3969             {
3970               if (h->root.type == bfd_link_hash_undefweak
3971                   && howto->pc_relative)
3972                 {
3973                   /* Assume this is a call protected by other code that
3974                      detect the symbol is undefined.  If this is the case,
3975                      we can safely ignore the overflow.  If not, the
3976                      program is hosed anyway, and a little warning isn't
3977                      going to help.  */
3978
3979                   continue;
3980                 }
3981
3982               name = h->root.root.string;
3983             }
3984           else
3985             {
3986               name = bfd_elf_string_from_elf_section (input_bfd,
3987                                                       symtab_hdr->sh_link,
3988                                                       sym->st_name);
3989               if (name == NULL)
3990                 continue;
3991               if (*name == '\0')
3992                 name = bfd_section_name (input_bfd, sec);
3993             }
3994
3995           if (! (*info->callbacks->reloc_overflow) (info,
3996                                                    name,
3997                                                    howto->name,
3998                                                    (bfd_vma) 0,
3999                                                    input_bfd,
4000                                                    input_section,
4001                                                    offset))
4002             return FALSE;
4003         }
4004       else
4005         ret = FALSE;
4006     }
4007
4008 #ifdef DEBUG
4009   fprintf (stderr, "\n");
4010 #endif
4011
4012   return ret;
4013 }
4014
4015 static enum elf_reloc_type_class
4016 ppc_elf_reloc_type_class (rela)
4017      const Elf_Internal_Rela *rela;
4018 {
4019   switch ((int) ELF32_R_TYPE (rela->r_info))
4020     {
4021     case R_PPC_RELATIVE:
4022       return reloc_class_relative;
4023     case R_PPC_REL24:
4024     case R_PPC_ADDR24:
4025     case R_PPC_JMP_SLOT:
4026       return reloc_class_plt;
4027     case R_PPC_COPY:
4028       return reloc_class_copy;
4029     default:
4030       return reloc_class_normal;
4031     }
4032 }
4033 \f
4034 /* Support for core dump NOTE sections.  */
4035
4036 static bfd_boolean
4037 ppc_elf_grok_prstatus (abfd, note)
4038      bfd *abfd;
4039      Elf_Internal_Note *note;
4040 {
4041   int offset;
4042   unsigned int raw_size;
4043
4044   switch (note->descsz)
4045     {
4046       default:
4047         return FALSE;
4048
4049       case 268:         /* Linux/PPC.  */
4050         /* pr_cursig */
4051         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
4052
4053         /* pr_pid */
4054         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
4055
4056         /* pr_reg */
4057         offset = 72;
4058         raw_size = 192;
4059
4060         break;
4061     }
4062
4063   /* Make a ".reg/999" section.  */
4064   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
4065                                           raw_size, note->descpos + offset);
4066 }
4067
4068 static bfd_boolean
4069 ppc_elf_grok_psinfo (abfd, note)
4070      bfd *abfd;
4071      Elf_Internal_Note *note;
4072 {
4073   switch (note->descsz)
4074     {
4075       default:
4076         return FALSE;
4077
4078       case 128:         /* Linux/PPC elf_prpsinfo.  */
4079         elf_tdata (abfd)->core_program
4080          = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
4081         elf_tdata (abfd)->core_command
4082          = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
4083     }
4084
4085   /* Note that for some reason, a spurious space is tacked
4086      onto the end of the args in some (at least one anyway)
4087      implementations, so strip it off if it exists.  */
4088
4089   {
4090     char *command = elf_tdata (abfd)->core_command;
4091     int n = strlen (command);
4092
4093     if (0 < n && command[n - 1] == ' ')
4094       command[n - 1] = '\0';
4095   }
4096
4097   return TRUE;
4098 }
4099 \f
4100 /* Very simple linked list structure for recording apuinfo values.  */
4101 typedef struct apuinfo_list
4102 {
4103   struct apuinfo_list * next;
4104   unsigned long         value;
4105 }
4106 apuinfo_list;
4107
4108 static apuinfo_list * head;
4109
4110 static void          apuinfo_list_init    PARAMS ((void));
4111 static void          apuinfo_list_add     PARAMS ((unsigned long));
4112 static unsigned      apuinfo_list_length  PARAMS ((void));
4113 static unsigned long apuinfo_list_element PARAMS ((unsigned long));
4114 static void          apuinfo_list_finish  PARAMS ((void));
4115
4116 extern void          ppc_elf_begin_write_processing
4117   PARAMS ((bfd *, struct bfd_link_info *));
4118 extern void          ppc_elf_final_write_processing
4119   PARAMS ((bfd *, bfd_boolean));
4120 extern bfd_boolean   ppc_elf_write_section
4121   PARAMS ((bfd *, asection *, bfd_byte *));
4122
4123
4124
4125 static void
4126 apuinfo_list_init PARAMS ((void))
4127 {
4128   head = NULL;
4129 }
4130
4131 static void
4132 apuinfo_list_add (value)
4133      unsigned long value;
4134 {
4135   apuinfo_list * entry = head;
4136
4137   while (entry != NULL)
4138     {
4139       if (entry->value == value)
4140         return;
4141       entry = entry->next;
4142     }
4143
4144   entry = bfd_malloc (sizeof (* entry));
4145   if (entry == NULL)
4146     return;
4147
4148   entry->value = value;
4149   entry->next  = head;
4150   head = entry;
4151 }
4152
4153 static unsigned
4154 apuinfo_list_length PARAMS ((void))
4155 {
4156   apuinfo_list * entry;
4157   unsigned long count;
4158
4159   for (entry = head, count = 0;
4160        entry;
4161        entry = entry->next)
4162     ++ count;
4163
4164   return count;
4165 }
4166
4167 static inline unsigned long
4168 apuinfo_list_element (number)
4169      unsigned long number;
4170 {
4171   apuinfo_list * entry;
4172
4173   for (entry = head;
4174        entry && number --;
4175        entry = entry->next)
4176     ;
4177
4178   return entry ? entry->value : 0;
4179 }
4180
4181 static void
4182 apuinfo_list_finish PARAMS ((void))
4183 {
4184   apuinfo_list * entry;
4185
4186   for (entry = head; entry;)
4187     {
4188       apuinfo_list * next = entry->next;
4189       free (entry);
4190       entry = next;
4191     }
4192
4193   head = NULL;
4194 }
4195
4196 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
4197 #define APUINFO_LABEL        "APUinfo"
4198
4199 /* Scan the input BFDs and create a linked list of
4200    the APUinfo values that will need to be emitted.  */
4201
4202 void
4203 ppc_elf_begin_write_processing (abfd, link_info)
4204      bfd *abfd;
4205      struct bfd_link_info *link_info;
4206 {
4207   bfd *         ibfd;
4208   asection *    asec;
4209   char *        buffer;
4210   unsigned      num_input_sections;
4211   bfd_size_type output_section_size;
4212   unsigned      i;
4213   unsigned      num_entries;
4214   unsigned long offset;
4215   unsigned long length;
4216   const char *  error_message = NULL;
4217
4218   if (link_info == NULL)
4219     return;
4220
4221   /* Scan the input bfds, looking for apuinfo sections.  */
4222   num_input_sections = 0;
4223   output_section_size = 0;
4224
4225   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
4226     {
4227       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
4228       if (asec)
4229         {
4230           ++ num_input_sections;
4231           output_section_size += asec->_raw_size;
4232         }
4233     }
4234
4235   /* We need at least one input sections
4236      in order to make merging worthwhile.  */
4237   if (num_input_sections < 1)
4238     return;
4239
4240   /* Just make sure that the output section exists as well.  */
4241   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
4242   if (asec == NULL)
4243     return;
4244
4245   /* Allocate a buffer for the contents of the input sections.  */
4246   buffer = bfd_malloc (output_section_size);
4247   if (buffer == NULL)
4248     return;
4249
4250   offset = 0;
4251   apuinfo_list_init ();
4252
4253   /* Read in the input sections contents.  */
4254   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
4255     {
4256       unsigned long     datum;
4257       char *            ptr;
4258
4259
4260       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
4261       if (asec == NULL)
4262         continue;
4263
4264       length = asec->_raw_size;
4265       if (length < 24)
4266         {
4267           error_message = _("corrupt or empty %s section in %s");
4268           goto fail;
4269         }
4270
4271       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
4272           || (bfd_bread (buffer + offset, length, ibfd) != length))
4273         {
4274           error_message = _("unable to read in %s section from %s");
4275           goto fail;
4276         }
4277
4278       /* Process the contents of the section.  */
4279       ptr = buffer + offset;
4280       error_message = _("corrupt %s section in %s");
4281
4282       /* Verify the contents of the header.  Note - we have to
4283          extract the values this way in order to allow for a
4284          host whose endian-ness is different from the target.  */
4285       datum = bfd_get_32 (ibfd, ptr);
4286       if (datum != sizeof APUINFO_LABEL)
4287         goto fail;
4288
4289       datum = bfd_get_32 (ibfd, ptr + 8);
4290       if (datum != 0x2)
4291         goto fail;
4292
4293       if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
4294         goto fail;
4295
4296       /* Get the number of apuinfo entries.  */
4297       datum = bfd_get_32 (ibfd, ptr + 4);
4298       if ((datum * 4 + 20) != length)
4299         goto fail;
4300
4301       /* Make sure that we do not run off the end of the section.  */
4302       if (offset + length > output_section_size)
4303         goto fail;
4304
4305       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
4306       for (i = 0; i < datum; i++)
4307         apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + (i * 4)));
4308
4309       /* Update the offset.  */
4310       offset += length;
4311     }
4312
4313   error_message = NULL;
4314
4315   /* Compute the size of the output section.  */
4316   num_entries = apuinfo_list_length ();
4317   output_section_size = 20 + num_entries * 4;
4318
4319   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
4320
4321   if (! bfd_set_section_size  (abfd, asec, output_section_size))
4322     ibfd = abfd,
4323       error_message = _("warning: unable to set size of %s section in %s");
4324
4325  fail:
4326   free (buffer);
4327
4328   if (error_message)
4329     _bfd_error_handler (error_message, APUINFO_SECTION_NAME,
4330                         bfd_archive_filename (ibfd));
4331 }
4332
4333
4334 /* Prevent the output section from accumulating the input sections'
4335    contents.  We have already stored this in our linked list structure.  */
4336
4337 bfd_boolean
4338 ppc_elf_write_section (abfd, asec, contents)
4339      bfd * abfd ATTRIBUTE_UNUSED;
4340      asection * asec;
4341      bfd_byte * contents ATTRIBUTE_UNUSED;
4342 {
4343   return apuinfo_list_length () && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
4344 }
4345
4346
4347 /* Finally we can generate the output section.  */
4348
4349 void
4350 ppc_elf_final_write_processing (abfd, linker)
4351      bfd * abfd;
4352      bfd_boolean linker ATTRIBUTE_UNUSED;
4353 {
4354   bfd_byte *    buffer;
4355   asection *    asec;
4356   unsigned      i;
4357   unsigned      num_entries;
4358   bfd_size_type length;
4359
4360   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
4361   if (asec == NULL)
4362     return;
4363
4364   if (apuinfo_list_length () == 0)
4365     return;
4366
4367   length = asec->_raw_size;
4368   if (length < 20)
4369     return;
4370
4371   buffer = bfd_malloc (length);
4372   if (buffer == NULL)
4373     {
4374       _bfd_error_handler (_("failed to allocate space for new APUinfo section."));
4375       return;
4376     }
4377
4378   /* Create the apuinfo header.  */
4379   num_entries = apuinfo_list_length ();
4380   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
4381   bfd_put_32 (abfd, num_entries, buffer + 4);
4382   bfd_put_32 (abfd, 0x2, buffer + 8);
4383   strcpy (buffer + 12, APUINFO_LABEL);
4384
4385   length = 20;
4386   for (i = 0; i < num_entries; i++)
4387     {
4388       bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
4389       length += 4;
4390     }
4391
4392   if (length != asec->_raw_size)
4393     _bfd_error_handler (_("failed to compute new APUinfo section."));
4394
4395   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
4396     _bfd_error_handler (_("failed to install new APUinfo section."));
4397
4398   free (buffer);
4399
4400   apuinfo_list_finish ();
4401 }
4402 \f
4403 #define TARGET_LITTLE_SYM       bfd_elf32_powerpcle_vec
4404 #define TARGET_LITTLE_NAME      "elf32-powerpcle"
4405 #define TARGET_BIG_SYM          bfd_elf32_powerpc_vec
4406 #define TARGET_BIG_NAME         "elf32-powerpc"
4407 #define ELF_ARCH                bfd_arch_powerpc
4408 #define ELF_MACHINE_CODE        EM_PPC
4409 #define ELF_MAXPAGESIZE         0x10000
4410 #define elf_info_to_howto       ppc_elf_info_to_howto
4411
4412 #ifdef  EM_CYGNUS_POWERPC
4413 #define ELF_MACHINE_ALT1        EM_CYGNUS_POWERPC
4414 #endif
4415
4416 #ifdef EM_PPC_OLD
4417 #define ELF_MACHINE_ALT2        EM_PPC_OLD
4418 #endif
4419
4420 #define elf_backend_plt_not_loaded      1
4421 #define elf_backend_got_symbol_offset   4
4422 #define elf_backend_can_gc_sections     1
4423 #define elf_backend_can_refcount        1
4424 #define elf_backend_got_header_size     12
4425 #define elf_backend_plt_header_size     PLT_INITIAL_ENTRY_SIZE
4426 #define elf_backend_rela_normal         1
4427
4428 #define bfd_elf32_bfd_merge_private_bfd_data    ppc_elf_merge_private_bfd_data
4429 #define bfd_elf32_bfd_relax_section             ppc_elf_relax_section
4430 #define bfd_elf32_bfd_reloc_type_lookup         ppc_elf_reloc_type_lookup
4431 #define bfd_elf32_bfd_set_private_flags         ppc_elf_set_private_flags
4432 #define bfd_elf32_bfd_final_link                _bfd_elf32_gc_common_final_link
4433 #define bfd_elf32_bfd_link_hash_table_create    ppc_elf_link_hash_table_create
4434
4435 #define elf_backend_object_p                    ppc_elf_object_p
4436 #define elf_backend_gc_mark_hook                ppc_elf_gc_mark_hook
4437 #define elf_backend_gc_sweep_hook               ppc_elf_gc_sweep_hook
4438 #define elf_backend_section_from_shdr           ppc_elf_section_from_shdr
4439 #define elf_backend_relocate_section            ppc_elf_relocate_section
4440 #define elf_backend_create_dynamic_sections     ppc_elf_create_dynamic_sections
4441 #define elf_backend_check_relocs                ppc_elf_check_relocs
4442 #define elf_backend_copy_indirect_symbol        ppc_elf_copy_indirect_symbol
4443 #define elf_backend_adjust_dynamic_symbol       ppc_elf_adjust_dynamic_symbol
4444 #define elf_backend_add_symbol_hook             ppc_elf_add_symbol_hook
4445 #define elf_backend_size_dynamic_sections       ppc_elf_size_dynamic_sections
4446 #define elf_backend_finish_dynamic_symbol       ppc_elf_finish_dynamic_symbol
4447 #define elf_backend_finish_dynamic_sections     ppc_elf_finish_dynamic_sections
4448 #define elf_backend_fake_sections               ppc_elf_fake_sections
4449 #define elf_backend_additional_program_headers  ppc_elf_additional_program_headers
4450 #define elf_backend_modify_segment_map          ppc_elf_modify_segment_map
4451 #define elf_backend_grok_prstatus               ppc_elf_grok_prstatus
4452 #define elf_backend_grok_psinfo                 ppc_elf_grok_psinfo
4453 #define elf_backend_reloc_type_class            ppc_elf_reloc_type_class
4454 #define elf_backend_begin_write_processing      ppc_elf_begin_write_processing
4455 #define elf_backend_final_write_processing      ppc_elf_final_write_processing
4456 #define elf_backend_write_section               ppc_elf_write_section
4457
4458 #include "elf32-target.h"