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