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