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