* elf-bfd.h (struct bfd_elf_section_data): Add tdata field.
[external/binutils.git] / bfd / elf32-ppc.c
1 /* PowerPC-specific support for 32-bit ELF
2    Copyright 1994, 1995 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 /* PowerPC relocations defined by the ABIs */
36 enum ppc_reloc_type
37 {
38   R_PPC_NONE                    =   0,
39   R_PPC_ADDR32                  =   1,
40   R_PPC_ADDR24                  =   2,
41   R_PPC_ADDR16                  =   3,
42   R_PPC_ADDR16_LO               =   4,
43   R_PPC_ADDR16_HI               =   5,
44   R_PPC_ADDR16_HA               =   6,
45   R_PPC_ADDR14                  =   7,
46   R_PPC_ADDR14_BRTAKEN          =   8,
47   R_PPC_ADDR14_BRNTAKEN         =   9,
48   R_PPC_REL24                   =  10,
49   R_PPC_REL14                   =  11,
50   R_PPC_REL14_BRTAKEN           =  12,
51   R_PPC_REL14_BRNTAKEN          =  13,
52   R_PPC_GOT16                   =  14,
53   R_PPC_GOT16_LO                =  15,
54   R_PPC_GOT16_HI                =  16,
55   R_PPC_GOT16_HA                =  17,
56   R_PPC_PLTREL24                =  18,
57   R_PPC_COPY                    =  19,
58   R_PPC_GLOB_DAT                =  20,
59   R_PPC_JMP_SLOT                =  21,
60   R_PPC_RELATIVE                =  22,
61   R_PPC_LOCAL24PC               =  23,
62   R_PPC_UADDR32                 =  24,
63   R_PPC_UADDR16                 =  25,
64   R_PPC_REL32                   =  26,
65   R_PPC_PLT32                   =  27,
66   R_PPC_PLTREL32                =  28,
67   R_PPC_PLT16_LO                =  29,
68   R_PPC_PLT16_HI                =  30,
69   R_PPC_PLT16_HA                =  31,
70   R_PPC_SDAREL16                =  32,
71   R_PPC_SECTOFF                 =  33,
72   R_PPC_SECTOFF_LO              =  34,
73   R_PPC_SECTOFF_HI              =  35,
74   R_PPC_SECTOFF_HA              =  36,
75
76   /* The remaining relocs are from the Embedded ELF ABI, and are not
77      in the SVR4 ELF ABI.  */
78   R_PPC_EMB_NADDR32             = 101,
79   R_PPC_EMB_NADDR16             = 102,
80   R_PPC_EMB_NADDR16_LO          = 103,
81   R_PPC_EMB_NADDR16_HI          = 104,
82   R_PPC_EMB_NADDR16_HA          = 105,
83   R_PPC_EMB_SDAI16              = 106,
84   R_PPC_EMB_SDA2I16             = 107,
85   R_PPC_EMB_SDA2REL             = 108,
86   R_PPC_EMB_SDA21               = 109,
87   R_PPC_EMB_MRKREF              = 110,
88   R_PPC_EMB_RELSEC16            = 111,
89   R_PPC_EMB_RELST_LO            = 112,
90   R_PPC_EMB_RELST_HI            = 113,
91   R_PPC_EMB_RELST_HA            = 114,
92   R_PPC_EMB_BIT_FLD             = 115,
93   R_PPC_EMB_RELSDA              = 116,
94
95   /* This is a phony reloc to handle any old fashioned TOC16 references
96      that may still be in object files.  */
97   R_PPC_TOC16                   = 255,
98
99   R_PPC_max
100 };
101
102 static bfd_reloc_status_type ppc_elf_unsupported_reloc
103   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
104 static bfd_reloc_status_type ppc_elf_std_reloc
105   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
106
107 static reloc_howto_type *ppc_elf_reloc_type_lookup
108   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
109 static void ppc_elf_info_to_howto
110   PARAMS ((bfd *abfd, arelent *cache_ptr, Elf32_Internal_Rela *dst));
111 static void ppc_elf_howto_init PARAMS ((void));
112 static boolean ppc_elf_set_private_flags PARAMS ((bfd *, flagword));
113 static boolean ppc_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
114 static boolean ppc_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
115
116 static boolean ppc_elf_check_relocs PARAMS ((bfd *,
117                                              struct bfd_link_info *,
118                                              asection *,
119                                              const Elf_Internal_Rela *));
120
121 static boolean ppc_elf_adjust_dynamic_symbol PARAMS ((struct bfd_link_info *,
122                                                       struct elf_link_hash_entry *));
123
124 static boolean ppc_elf_adjust_dynindx PARAMS ((struct elf_link_hash_entry *, PTR));
125
126 static boolean ppc_elf_size_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
127
128 static boolean ppc_elf_relocate_section PARAMS ((bfd *,
129                                                  struct bfd_link_info *info,
130                                                  bfd *,
131                                                  asection *,
132                                                  bfd_byte *,
133                                                  Elf_Internal_Rela *relocs,
134                                                  Elf_Internal_Sym *local_syms,
135                                                  asection **));
136
137 static boolean ppc_elf_finish_dynamic_symbol PARAMS ((bfd *,
138                                                       struct bfd_link_info *,
139                                                       struct elf_link_hash_entry *,
140                                                       Elf_Internal_Sym *));
141
142 static boolean ppc_elf_finish_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
143
144 #define BRANCH_PREDICT_BIT 0x200000
145
146 /* The name of the dynamic interpreter.  This is put in the .interp
147    section.  */
148
149 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
150
151 \f
152 static reloc_howto_type *ppc_elf_howto_table[ (int)R_PPC_max ];
153
154 static reloc_howto_type ppc_elf_howto_raw[] =
155 {
156   /* This reloc does nothing.  */
157   HOWTO (R_PPC_NONE,            /* type */
158          0,                     /* rightshift */
159          2,                     /* size (0 = byte, 1 = short, 2 = long) */
160          32,                    /* bitsize */
161          false,                 /* pc_relative */
162          0,                     /* bitpos */
163          complain_overflow_bitfield, /* complain_on_overflow */
164          ppc_elf_std_reloc,     /* special_function */
165          "R_PPC_NONE",          /* name */
166          false,                 /* partial_inplace */
167          0,                     /* src_mask */
168          0,                     /* dst_mask */
169          false),                /* pcrel_offset */
170
171   /* A standard 32 bit relocation.  */
172   HOWTO (R_PPC_ADDR32,          /* type */
173          0,                     /* rightshift */
174          2,                     /* size (0 = byte, 1 = short, 2 = long) */
175          32,                    /* bitsize */
176          false,                 /* pc_relative */
177          0,                     /* bitpos */
178          complain_overflow_bitfield, /* complain_on_overflow */
179          ppc_elf_std_reloc,     /* special_function */
180          "R_PPC_ADDR32",        /* name */
181          false,                 /* partial_inplace */
182          0,                     /* src_mask */
183          0xffffffff,            /* dst_mask */
184          false),                /* pcrel_offset */
185
186   /* An absolute 26 bit branch; the lower two bits must be zero.
187      FIXME: we don't check that, we just clear them.  */
188   HOWTO (R_PPC_ADDR24,          /* type */
189          0,                     /* rightshift */
190          2,                     /* size (0 = byte, 1 = short, 2 = long) */
191          26,                    /* bitsize */
192          false,                 /* pc_relative */
193          0,                     /* bitpos */
194          complain_overflow_bitfield, /* complain_on_overflow */
195          ppc_elf_std_reloc,     /* special_function */
196          "R_PPC_ADDR24",        /* name */
197          false,                 /* partial_inplace */
198          0,                     /* src_mask */
199          0x3fffffc,             /* dst_mask */
200          false),                /* pcrel_offset */
201
202   /* A standard 16 bit relocation.  */
203   HOWTO (R_PPC_ADDR16,          /* type */
204          0,                     /* rightshift */
205          1,                     /* size (0 = byte, 1 = short, 2 = long) */
206          16,                    /* bitsize */
207          false,                 /* pc_relative */
208          0,                     /* bitpos */
209          complain_overflow_bitfield, /* complain_on_overflow */
210          ppc_elf_std_reloc,     /* special_function */
211          "R_PPC_ADDR16",        /* name */
212          false,                 /* partial_inplace */
213          0,                     /* src_mask */
214          0xffff,                /* dst_mask */
215          false),                /* pcrel_offset */
216
217   /* A 16 bit relocation without overflow.  */
218   HOWTO (R_PPC_ADDR16_LO,       /* type */
219          0,                     /* rightshift */
220          1,                     /* size (0 = byte, 1 = short, 2 = long) */
221          16,                    /* bitsize */
222          false,                 /* pc_relative */
223          0,                     /* bitpos */
224          complain_overflow_dont,/* complain_on_overflow */
225          ppc_elf_std_reloc,     /* special_function */
226          "R_PPC_ADDR16_LO",     /* name */
227          false,                 /* partial_inplace */
228          0,                     /* src_mask */
229          0xffff,                /* dst_mask */
230          false),                /* pcrel_offset */
231
232   /* The high order 16 bits of an address.  */
233   HOWTO (R_PPC_ADDR16_HI,       /* type */
234          16,                    /* rightshift */
235          1,                     /* size (0 = byte, 1 = short, 2 = long) */
236          16,                    /* bitsize */
237          false,                 /* pc_relative */
238          0,                     /* bitpos */
239          complain_overflow_dont, /* complain_on_overflow */
240          ppc_elf_std_reloc,     /* special_function */
241          "R_PPC_ADDR16_HI",     /* name */
242          false,                 /* partial_inplace */
243          0,                     /* src_mask */
244          0xffff,                /* dst_mask */
245          false),                /* pcrel_offset */
246
247   /* The high order 16 bits of an address, plus 1 if the contents of
248      the low 16 bits, treated as a signed number, is negative.  */
249   HOWTO (R_PPC_ADDR16_HA,       /* type */
250          16,                    /* rightshift */
251          1,                     /* size (0 = byte, 1 = short, 2 = long) */
252          16,                    /* bitsize */
253          false,                 /* pc_relative */
254          0,                     /* bitpos */
255          complain_overflow_dont, /* complain_on_overflow */
256          bfd_elf_generic_reloc, /* special_function */
257          "R_PPC_ADDR16_HA",     /* name */
258          false,                 /* partial_inplace */
259          0,                     /* src_mask */
260          0xffff,                /* dst_mask */
261          false),                /* pcrel_offset */
262
263   /* An absolute 16 bit branch; the lower two bits must be zero.
264      FIXME: we don't check that, we just clear them.  */
265   HOWTO (R_PPC_ADDR14,          /* type */
266          0,                     /* rightshift */
267          2,                     /* size (0 = byte, 1 = short, 2 = long) */
268          16,                    /* bitsize */
269          false,                 /* pc_relative */
270          0,                     /* bitpos */
271          complain_overflow_bitfield, /* complain_on_overflow */
272          ppc_elf_std_reloc,     /* special_function */
273          "R_PPC_ADDR14",        /* name */
274          false,                 /* partial_inplace */
275          0,                     /* src_mask */
276          0xfffc,                /* dst_mask */
277          false),                /* pcrel_offset */
278
279   /* An absolute 16 bit branch, for which bit 10 should be set to
280      indicate that the branch is expected to be taken.  The lower two
281      bits must be zero.  */
282   HOWTO (R_PPC_ADDR14_BRTAKEN,  /* type */
283          0,                     /* rightshift */
284          2,                     /* size (0 = byte, 1 = short, 2 = long) */
285          16,                    /* bitsize */
286          false,                 /* pc_relative */
287          0,                     /* bitpos */
288          complain_overflow_bitfield, /* complain_on_overflow */
289          bfd_elf_generic_reloc, /* special_function */
290          "R_PPC_ADDR14_BRTAKEN",/* name */
291          false,                 /* partial_inplace */
292          0,                     /* src_mask */
293          0xfffc,                /* dst_mask */
294          false),                /* pcrel_offset */
295
296   /* An absolute 16 bit branch, for which bit 10 should be set to
297      indicate that the branch is not expected to be taken.  The lower
298      two bits must be zero.  */
299   HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
300          0,                     /* rightshift */
301          2,                     /* size (0 = byte, 1 = short, 2 = long) */
302          16,                    /* bitsize */
303          false,                 /* pc_relative */
304          0,                     /* bitpos */
305          complain_overflow_bitfield, /* complain_on_overflow */
306          bfd_elf_generic_reloc, /* special_function */
307          "R_PPC_ADDR14_BRNTAKEN",/* name */
308          false,                 /* partial_inplace */
309          0,                     /* src_mask */
310          0xfffc,                /* dst_mask */
311          false),                /* pcrel_offset */
312
313   /* A relative 26 bit branch; the lower two bits must be zero.  */
314   HOWTO (R_PPC_REL24,           /* type */
315          0,                     /* rightshift */
316          2,                     /* size (0 = byte, 1 = short, 2 = long) */
317          26,                    /* bitsize */
318          true,                  /* pc_relative */
319          0,                     /* bitpos */
320          complain_overflow_signed, /* complain_on_overflow */
321          ppc_elf_std_reloc,     /* special_function */
322          "R_PPC_REL24",         /* name */
323          false,                 /* partial_inplace */
324          0,                     /* src_mask */
325          0x3fffffc,             /* dst_mask */
326          true),                 /* pcrel_offset */
327
328   /* A relative 16 bit branch; the lower two bits must be zero.  */
329   HOWTO (R_PPC_REL14,           /* type */
330          0,                     /* rightshift */
331          2,                     /* size (0 = byte, 1 = short, 2 = long) */
332          16,                    /* bitsize */
333          true,                  /* pc_relative */
334          0,                     /* bitpos */
335          complain_overflow_signed, /* complain_on_overflow */
336          ppc_elf_std_reloc,     /* special_function */
337          "R_PPC_REL14",         /* name */
338          false,                 /* partial_inplace */
339          0,                     /* src_mask */
340          0xfffc,                /* dst_mask */
341          true),                 /* pcrel_offset */
342
343   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
344      the branch is expected to be taken.  The lower two bits must be
345      zero.  */
346   HOWTO (R_PPC_REL14_BRTAKEN,   /* type */
347          0,                     /* rightshift */
348          2,                     /* size (0 = byte, 1 = short, 2 = long) */
349          16,                    /* bitsize */
350          true,                  /* pc_relative */
351          0,                     /* bitpos */
352          complain_overflow_signed, /* complain_on_overflow */
353          bfd_elf_generic_reloc, /* special_function */
354          "R_PPC_REL14_BRTAKEN", /* name */
355          false,                 /* partial_inplace */
356          0,                     /* src_mask */
357          0xfffc,                /* dst_mask */
358          true),                 /* pcrel_offset */
359
360   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
361      the branch is not expected to be taken.  The lower two bits must
362      be zero.  */
363   HOWTO (R_PPC_REL14_BRNTAKEN,  /* type */
364          0,                     /* rightshift */
365          2,                     /* size (0 = byte, 1 = short, 2 = long) */
366          16,                    /* bitsize */
367          true,                  /* pc_relative */
368          0,                     /* bitpos */
369          complain_overflow_signed, /* complain_on_overflow */
370          bfd_elf_generic_reloc, /* special_function */
371          "R_PPC_REL14_BRNTAKEN",/* name */
372          false,                 /* partial_inplace */
373          0,                     /* src_mask */
374          0xfffc,                /* dst_mask */
375          true),                 /* pcrel_offset */
376
377   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
378      symbol.  */
379   HOWTO (R_PPC_GOT16,           /* type */
380          0,                     /* rightshift */
381          1,                     /* size (0 = byte, 1 = short, 2 = long) */
382          16,                    /* bitsize */
383          false,                 /* pc_relative */
384          0,                     /* bitpos */
385          complain_overflow_signed, /* complain_on_overflow */
386          bfd_elf_generic_reloc, /* special_function */
387          "R_PPC_GOT16",         /* name */
388          false,                 /* partial_inplace */
389          0,                     /* src_mask */
390          0xffff,                /* dst_mask */
391          false),                /* pcrel_offset */
392
393   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
394      the symbol.  */
395   HOWTO (R_PPC_GOT16_LO,        /* type */
396          0,                     /* rightshift */
397          1,                     /* size (0 = byte, 1 = short, 2 = long) */
398          16,                    /* bitsize */
399          false,                 /* pc_relative */
400          0,                     /* bitpos */
401          complain_overflow_bitfield, /* complain_on_overflow */
402          bfd_elf_generic_reloc, /* special_function */
403          "R_PPC_GOT16_LO",      /* name */
404          false,                 /* partial_inplace */
405          0,                     /* src_mask */
406          0xffff,                /* dst_mask */
407          false),                /* pcrel_offset */
408
409   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
410      the symbol.  */
411   HOWTO (R_PPC_GOT16_HI,        /* type */
412          16,                    /* rightshift */
413          1,                     /* size (0 = byte, 1 = short, 2 = long) */
414          16,                    /* bitsize */
415          false,                 /* pc_relative */
416          0,                     /* bitpos */
417          complain_overflow_bitfield, /* complain_on_overflow */
418          bfd_elf_generic_reloc, /* special_function */
419          "R_PPC_GOT16_HI",      /* name */
420          false,                 /* partial_inplace */
421          0,                     /* src_mask */
422          0xffff,                /* dst_mask */
423          false),                 /* pcrel_offset */
424
425   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
426      the symbol.  FIXME: Not supported.  */
427   HOWTO (R_PPC_GOT16_HA,        /* type */
428          0,                     /* rightshift */
429          1,                     /* size (0 = byte, 1 = short, 2 = long) */
430          16,                    /* bitsize */
431          false,                 /* pc_relative */
432          0,                     /* bitpos */
433          complain_overflow_bitfield, /* complain_on_overflow */
434          ppc_elf_unsupported_reloc, /* special_function */
435          "R_PPC_GOT16_HA",      /* name */
436          false,                 /* partial_inplace */
437          0,                     /* src_mask */
438          0xffff,                /* dst_mask */
439          false),                /* pcrel_offset */
440
441   /* Like R_PPC_REL24, but referring to the procedure linkage table
442      entry for the symbol.  FIXME: Not supported.  */
443   HOWTO (R_PPC_PLTREL24,        /* type */
444          0,                     /* rightshift */
445          2,                     /* size (0 = byte, 1 = short, 2 = long) */
446          26,                    /* bitsize */
447          true,                  /* pc_relative */
448          0,                     /* bitpos */
449          complain_overflow_signed,  /* complain_on_overflow */
450          ppc_elf_unsupported_reloc, /* special_function */
451          "R_PPC_PLTREL24",      /* name */
452          false,                 /* partial_inplace */
453          0,                     /* src_mask */
454          0x3fffffc,             /* dst_mask */
455          true),                 /* pcrel_offset */
456
457   /* This is used only by the dynamic linker.  The symbol should exist
458      both in the object being run and in some shared library.  The
459      dynamic linker copies the data addressed by the symbol from the
460      shared library into the object.  I have no idea what the purpose
461      of this is.  */
462   HOWTO (R_PPC_COPY,            /* type */
463          0,                     /* rightshift */
464          2,                     /* size (0 = byte, 1 = short, 2 = long) */
465          32,                    /* bitsize */
466          false,                 /* pc_relative */
467          0,                     /* bitpos */
468          complain_overflow_bitfield, /* complain_on_overflow */
469          ppc_elf_unsupported_reloc,  /* special_function */
470          "R_PPC_COPY",          /* name */
471          false,                 /* partial_inplace */
472          0,                     /* src_mask */
473          0,                     /* dst_mask */
474          false),                /* pcrel_offset */
475
476   /* Like R_PPC_ADDR32, but used when setting global offset table
477      entries.  */
478   HOWTO (R_PPC_GLOB_DAT,        /* type */
479          0,                     /* rightshift */
480          2,                     /* size (0 = byte, 1 = short, 2 = long) */
481          32,                    /* bitsize */
482          false,                 /* pc_relative */
483          0,                     /* bitpos */
484          complain_overflow_bitfield, /* complain_on_overflow */
485          ppc_elf_unsupported_reloc,  /* special_function */
486          "R_PPC_GLOB_DAT",      /* name */
487          false,                 /* partial_inplace */
488          0,                     /* src_mask */
489          0xffffffff,            /* dst_mask */
490          false),                /* pcrel_offset */
491
492   /* Marks a procedure linkage table entry for a symbol.  */
493   HOWTO (R_PPC_JMP_SLOT,        /* type */
494          0,                     /* rightshift */
495          2,                     /* size (0 = byte, 1 = short, 2 = long) */
496          32,                    /* bitsize */
497          false,                 /* pc_relative */
498          0,                     /* bitpos */
499          complain_overflow_bitfield, /* complain_on_overflow */
500          ppc_elf_unsupported_reloc,  /* special_function */
501          "R_PPC_JMP_SLOT",      /* name */
502          false,                 /* partial_inplace */
503          0,                     /* src_mask */
504          0,                     /* dst_mask */
505          false),                /* pcrel_offset */
506
507   /* Used only by the dynamic linker.  When the object is run, this
508      longword is set to the load address of the object, plus the
509      addend.  */
510   HOWTO (R_PPC_RELATIVE,        /* type */
511          0,                     /* rightshift */
512          2,                     /* size (0 = byte, 1 = short, 2 = long) */
513          32,                    /* bitsize */
514          false,                 /* pc_relative */
515          0,                     /* bitpos */
516          complain_overflow_bitfield, /* complain_on_overflow */
517          ppc_elf_unsupported_reloc,  /* special_function */
518          "R_PPC_RELATIVE",      /* name */
519          false,                 /* partial_inplace */
520          0,                     /* src_mask */
521          0xffffffff,            /* dst_mask */
522          false),                /* pcrel_offset */
523
524   /* Like R_PPC_REL24, but uses the value of the symbol within the
525      object rather than the final value.  Normally used for
526      _GLOBAL_OFFSET_TABLE_.  FIXME: Not supported.  */
527   HOWTO (R_PPC_LOCAL24PC,       /* type */
528          0,                     /* rightshift */
529          2,                     /* size (0 = byte, 1 = short, 2 = long) */
530          26,                    /* bitsize */
531          true,                  /* pc_relative */
532          0,                     /* bitpos */
533          complain_overflow_signed, /* complain_on_overflow */
534          ppc_elf_unsupported_reloc, /* special_function */
535          "R_PPC_LOCAL24PC",     /* name */
536          false,                 /* partial_inplace */
537          0,                     /* src_mask */
538          0x3fffffc,             /* dst_mask */
539          true),                 /* pcrel_offset */
540
541   /* Like R_PPC_ADDR32, but may be unaligned.  */
542   HOWTO (R_PPC_UADDR32,         /* type */
543          0,                     /* rightshift */
544          2,                     /* size (0 = byte, 1 = short, 2 = long) */
545          32,                    /* bitsize */
546          false,                 /* pc_relative */
547          0,                     /* bitpos */
548          complain_overflow_bitfield, /* complain_on_overflow */
549          ppc_elf_std_reloc,     /* special_function */
550          "R_PPC_UADDR32",       /* name */
551          false,                 /* partial_inplace */
552          0,                     /* src_mask */
553          0xffffffff,            /* dst_mask */
554          false),                /* pcrel_offset */
555
556   /* Like R_PPC_ADDR16, but may be unaligned.  */
557   HOWTO (R_PPC_UADDR16,         /* type */
558          0,                     /* rightshift */
559          1,                     /* size (0 = byte, 1 = short, 2 = long) */
560          16,                    /* bitsize */
561          false,                 /* pc_relative */
562          0,                     /* bitpos */
563          complain_overflow_bitfield, /* complain_on_overflow */
564          ppc_elf_std_reloc,     /* special_function */
565          "R_PPC_UADDR16",       /* name */
566          false,                 /* partial_inplace */
567          0,                     /* src_mask */
568          0xffff,                /* dst_mask */
569          false),                /* pcrel_offset */
570
571   /* 32-bit PC relative */
572   HOWTO (R_PPC_REL32,           /* type */
573          0,                     /* rightshift */
574          2,                     /* size (0 = byte, 1 = short, 2 = long) */
575          32,                    /* bitsize */
576          true,                  /* pc_relative */
577          0,                     /* bitpos */
578          complain_overflow_bitfield, /* complain_on_overflow */
579          ppc_elf_std_reloc,     /* special_function */
580          "R_PPC_REL32",         /* name */
581          false,                 /* partial_inplace */
582          0,                     /* src_mask */
583          0xffffffff,            /* dst_mask */
584          true),                 /* pcrel_offset */
585
586   /* 32-bit relocation to the symbol's procedure linkage table.
587      FIXEME: not supported. */
588   HOWTO (R_PPC_PLT32,           /* type */
589          0,                     /* rightshift */
590          2,                     /* size (0 = byte, 1 = short, 2 = long) */
591          32,                    /* bitsize */
592          false,                 /* pc_relative */
593          0,                     /* bitpos */
594          complain_overflow_bitfield, /* complain_on_overflow */
595          ppc_elf_unsupported_reloc, /* special_function */
596          "R_PPC_PLT32",         /* name */
597          false,                 /* partial_inplace */
598          0,                     /* src_mask */
599          0,                     /* dst_mask */
600          false),                /* pcrel_offset */
601
602   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
603      FIXEME: not supported. */
604   HOWTO (R_PPC_PLTREL32,        /* type */
605          0,                     /* rightshift */
606          2,                     /* size (0 = byte, 1 = short, 2 = long) */
607          32,                    /* bitsize */
608          true,                  /* pc_relative */
609          0,                     /* bitpos */
610          complain_overflow_bitfield, /* complain_on_overflow */
611          ppc_elf_unsupported_reloc, /* special_function */
612          "R_PPC_PLTREL32",      /* name */
613          false,                 /* partial_inplace */
614          0,                     /* src_mask */
615          0,                     /* dst_mask */
616          true),                 /* pcrel_offset */
617
618   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
619      the symbol.  */
620   HOWTO (R_PPC_PLT16_LO,        /* type */
621          0,                     /* rightshift */
622          1,                     /* size (0 = byte, 1 = short, 2 = long) */
623          16,                    /* bitsize */
624          false,                 /* pc_relative */
625          0,                     /* bitpos */
626          complain_overflow_bitfield, /* complain_on_overflow */
627          ppc_elf_unsupported_reloc, /* special_function */
628          "R_PPC_PLT16_LO",      /* name */
629          false,                 /* partial_inplace */
630          0,                     /* src_mask */
631          0xffff,                /* dst_mask */
632          false),                /* pcrel_offset */
633
634   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
635      the symbol.  */
636   HOWTO (R_PPC_PLT16_HI,        /* type */
637          16,                    /* rightshift */
638          1,                     /* size (0 = byte, 1 = short, 2 = long) */
639          16,                    /* bitsize */
640          false,                 /* pc_relative */
641          0,                     /* bitpos */
642          complain_overflow_bitfield, /* complain_on_overflow */
643          ppc_elf_unsupported_reloc, /* special_function */
644          "R_PPC_PLT16_HI",      /* name */
645          false,                 /* partial_inplace */
646          0,                     /* src_mask */
647          0xffff,                /* dst_mask */
648          false),                 /* pcrel_offset */
649
650   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
651      the symbol.  FIXME: Not supported.  */
652   HOWTO (R_PPC_PLT16_HA,        /* type */
653          0,                     /* rightshift */
654          1,                     /* size (0 = byte, 1 = short, 2 = long) */
655          16,                    /* bitsize */
656          false,                 /* pc_relative */
657          0,                     /* bitpos */
658          complain_overflow_bitfield, /* complain_on_overflow */
659          ppc_elf_unsupported_reloc, /* special_function */
660          "R_PPC_PLT16_HA",      /* name */
661          false,                 /* partial_inplace */
662          0,                     /* src_mask */
663          0xffff,                /* dst_mask */
664          false),                /* pcrel_offset */
665
666   /* A sign-extended 16 bit value relative to _SDA_BASE, for use with
667      small data items.  */
668   HOWTO (R_PPC_SDAREL16,        /* type */
669          0,                     /* rightshift */
670          1,                     /* size (0 = byte, 1 = short, 2 = long) */
671          16,                    /* bitsize */
672          false,                 /* pc_relative */
673          0,                     /* bitpos */
674          complain_overflow_signed, /* complain_on_overflow */
675          bfd_elf_generic_reloc, /* special_function */
676          "R_PPC_SDAREL16",      /* name */
677          false,                 /* partial_inplace */
678          0,                     /* src_mask */
679          0xffff,                /* dst_mask */
680          false),                /* pcrel_offset */
681
682   /* These next 4 relocations were added by Sun. */
683   /* 32-bit section relative relocation. FIXME: not supported. */
684   HOWTO (R_PPC_SECTOFF,         /* type */
685          0,                     /* rightshift */
686          2,                     /* size (0 = byte, 1 = short, 2 = long) */
687          32,                    /* bitsize */
688          true,                  /* pc_relative */
689          0,                     /* bitpos */
690          complain_overflow_bitfield, /* complain_on_overflow */
691          ppc_elf_unsupported_reloc, /* special_function */
692          "R_PPC_SECTOFF",       /* name */
693          false,                 /* partial_inplace */
694          0,                     /* src_mask */
695          0,                     /* dst_mask */
696          true),                 /* pcrel_offset */
697
698   /* 16-bit lower half section relative relocation. FIXME: not supported. */
699   HOWTO (R_PPC_SECTOFF_LO,        /* type */
700          0,                     /* rightshift */
701          1,                     /* size (0 = byte, 1 = short, 2 = long) */
702          16,                    /* bitsize */
703          false,                 /* pc_relative */
704          0,                     /* bitpos */
705          complain_overflow_bitfield, /* complain_on_overflow */
706          ppc_elf_unsupported_reloc, /* special_function */
707          "R_PPC_SECTOFF_LO",    /* name */
708          false,                 /* partial_inplace */
709          0,                     /* src_mask */
710          0xffff,                /* dst_mask */
711          false),                /* pcrel_offset */
712
713   /* 16-bit upper half section relative relocation. FIXME: not supported. */
714   HOWTO (R_PPC_SECTOFF_HI,      /* type */
715          16,                    /* rightshift */
716          1,                     /* size (0 = byte, 1 = short, 2 = long) */
717          16,                    /* bitsize */
718          false,                 /* pc_relative */
719          0,                     /* bitpos */
720          complain_overflow_bitfield, /* complain_on_overflow */
721          ppc_elf_unsupported_reloc, /* special_function */
722          "R_PPC_SECTOFF_HI",    /* name */
723          false,                 /* partial_inplace */
724          0,                     /* src_mask */
725          0xffff,                /* dst_mask */
726          false),                 /* pcrel_offset */
727
728   /* 16-bit upper half adjusted section relative relocation. FIXME: not supported. */
729   HOWTO (R_PPC_SECTOFF_HA,      /* type */
730          0,                     /* rightshift */
731          1,                     /* size (0 = byte, 1 = short, 2 = long) */
732          16,                    /* bitsize */
733          false,                 /* pc_relative */
734          0,                     /* bitpos */
735          complain_overflow_bitfield, /* complain_on_overflow */
736          ppc_elf_unsupported_reloc, /* special_function */
737          "R_PPC_SECTOFF_HA",    /* name */
738          false,                 /* partial_inplace */
739          0,                     /* src_mask */
740          0xffff,                /* dst_mask */
741          false),                /* pcrel_offset */
742
743   /* The remaining relocs are from the Embedded ELF ABI, and are not
744      in the SVR4 ELF ABI.  */
745
746   /* 32 bit value resulting from the addend minus the symbol */
747   HOWTO (R_PPC_EMB_NADDR32,     /* type */
748          0,                     /* rightshift */
749          2,                     /* size (0 = byte, 1 = short, 2 = long) */
750          32,                    /* bitsize */
751          false,                 /* pc_relative */
752          0,                     /* bitpos */
753          complain_overflow_bitfield, /* complain_on_overflow */
754          ppc_elf_unsupported_reloc, /* special_function */
755          "R_PPC_EMB_NADDR32",   /* name */
756          false,                 /* partial_inplace */
757          0,                     /* src_mask */
758          0xffffffff,            /* dst_mask */
759          false),                /* pcrel_offset */
760
761 /* 16 bit value resulting from the addend minus the symbol */
762   HOWTO (R_PPC_EMB_NADDR16,     /* type */
763          0,                     /* rightshift */
764          1,                     /* size (0 = byte, 1 = short, 2 = long) */
765          16,                    /* bitsize */
766          false,                 /* pc_relative */
767          0,                     /* bitpos */
768          complain_overflow_bitfield, /* complain_on_overflow */
769          ppc_elf_unsupported_reloc, /* special_function */
770          "R_PPC_EMB_NADDR16",   /* name */
771          false,                 /* partial_inplace */
772          0,                     /* src_mask */
773          0xffff,                /* dst_mask */
774          false),                /* pcrel_offset */
775
776   /* 16 bit value resulting from the addend minus the symbol */
777   HOWTO (R_PPC_EMB_NADDR16_LO,  /* type */
778          0,                     /* rightshift */
779          1,                     /* size (0 = byte, 1 = short, 2 = long) */
780          16,                    /* bitsize */
781          false,                 /* pc_relative */
782          0,                     /* bitpos */
783          complain_overflow_dont,/* complain_on_overflow */
784          ppc_elf_unsupported_reloc, /* special_function */
785          "R_PPC_EMB_ADDR16_LO", /* name */
786          false,                 /* partial_inplace */
787          0,                     /* src_mask */
788          0xffff,                /* dst_mask */
789          false),                /* pcrel_offset */
790
791   /* The high order 16 bits of the addend minus the symbol */
792   HOWTO (R_PPC_EMB_NADDR16_HI,  /* type */
793          16,                    /* rightshift */
794          1,                     /* size (0 = byte, 1 = short, 2 = long) */
795          16,                    /* bitsize */
796          false,                 /* pc_relative */
797          0,                     /* bitpos */
798          complain_overflow_dont, /* complain_on_overflow */
799          ppc_elf_unsupported_reloc, /* special_function */
800          "R_PPC_EMB_NADDR16_HI", /* name */
801          false,                 /* partial_inplace */
802          0,                     /* src_mask */
803          0xffff,                /* dst_mask */
804          false),                /* pcrel_offset */
805
806   /* The high order 16 bits of the result of the addend minus the address,
807      plus 1 if the contents of the low 16 bits, treated as a signed number,
808      is negative.  */
809   HOWTO (R_PPC_EMB_NADDR16_HA,  /* type */
810          16,                    /* rightshift */
811          1,                     /* size (0 = byte, 1 = short, 2 = long) */
812          16,                    /* bitsize */
813          false,                 /* pc_relative */
814          0,                     /* bitpos */
815          complain_overflow_dont, /* complain_on_overflow */
816          ppc_elf_unsupported_reloc, /* special_function */
817          "R_PPC_EMB_NADDR16_HA", /* name */
818          false,                 /* partial_inplace */
819          0,                     /* src_mask */
820          0xffff,                /* dst_mask */
821          false),                /* pcrel_offset */
822
823   /* Phony reloc to handle AIX style TOC entries */
824   HOWTO (R_PPC_TOC16,           /* type */
825          0,                     /* rightshift */
826          1,                     /* size (0 = byte, 1 = short, 2 = long) */
827          16,                    /* bitsize */
828          false,                 /* pc_relative */
829          0,                     /* bitpos */
830          complain_overflow_signed, /* complain_on_overflow */
831          bfd_elf_generic_reloc, /* special_function */
832          "R_PPC_TOC16",         /* name */
833          false,                 /* partial_inplace */
834          0,                     /* src_mask */
835          0xffff,                /* dst_mask */
836          false),                /* pcrel_offset */
837 };
838
839 \f
840 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
841
842 static void
843 ppc_elf_howto_init ()
844 {
845   unsigned int i, type;
846
847   for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
848     {
849       type = ppc_elf_howto_raw[i].type;
850       BFD_ASSERT (type < sizeof(ppc_elf_howto_table) / sizeof(ppc_elf_howto_table[0]));
851       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
852     }
853 }
854
855 \f
856 static reloc_howto_type *
857 ppc_elf_reloc_type_lookup (abfd, code)
858      bfd *abfd;
859      bfd_reloc_code_real_type code;
860 {
861   enum ppc_reloc_type ppc_reloc = R_PPC_NONE;
862
863   if (!ppc_elf_howto_table[ R_PPC_ADDR32 ])     /* Initialize howto table if needed */
864     ppc_elf_howto_init ();
865
866   switch ((int)code)
867     {
868     default:
869       return (reloc_howto_type *)NULL;
870
871     case BFD_RELOC_NONE:                ppc_reloc = R_PPC_NONE;                 break;
872     case BFD_RELOC_32:                  ppc_reloc = R_PPC_ADDR32;               break;
873     case BFD_RELOC_PPC_BA26:            ppc_reloc = R_PPC_ADDR24;               break;
874     case BFD_RELOC_16:                  ppc_reloc = R_PPC_ADDR16;               break;
875     case BFD_RELOC_LO16:                ppc_reloc = R_PPC_ADDR16_LO;            break;
876     case BFD_RELOC_HI16:                ppc_reloc = R_PPC_ADDR16_HI;            break;
877     case BFD_RELOC_HI16_S:              ppc_reloc = R_PPC_ADDR16_HA;            break;
878     case BFD_RELOC_PPC_BA16:            ppc_reloc = R_PPC_ADDR14;               break;
879     case BFD_RELOC_PPC_BA16_BRTAKEN:    ppc_reloc = R_PPC_ADDR14_BRTAKEN;       break;
880     case BFD_RELOC_PPC_BA16_BRNTAKEN:   ppc_reloc = R_PPC_ADDR14_BRNTAKEN;      break;
881     case BFD_RELOC_PPC_B26:             ppc_reloc = R_PPC_REL24;                break;
882     case BFD_RELOC_PPC_B16:             ppc_reloc = R_PPC_REL14;                break;
883     case BFD_RELOC_PPC_B16_BRTAKEN:     ppc_reloc = R_PPC_REL14_BRTAKEN;        break;
884     case BFD_RELOC_PPC_B16_BRNTAKEN:    ppc_reloc = R_PPC_REL14_BRNTAKEN;       break;
885     case BFD_RELOC_16_GOTOFF:           ppc_reloc = R_PPC_GOT16;                break;
886     case BFD_RELOC_LO16_GOTOFF:         ppc_reloc = R_PPC_GOT16_LO;             break;
887     case BFD_RELOC_HI16_GOTOFF:         ppc_reloc = R_PPC_GOT16_HI;             break;
888     case BFD_RELOC_HI16_S_GOTOFF:       ppc_reloc = R_PPC_GOT16_HA;             break;
889     case BFD_RELOC_24_PLT_PCREL:        ppc_reloc = R_PPC_PLTREL24;             break;
890     case BFD_RELOC_PPC_COPY:            ppc_reloc = R_PPC_COPY;                 break;
891     case BFD_RELOC_PPC_GLOB_DAT:        ppc_reloc = R_PPC_GLOB_DAT;             break;
892     case BFD_RELOC_PPC_LOCAL24PC:       ppc_reloc = R_PPC_LOCAL24PC;            break;
893     case BFD_RELOC_32_PCREL:            ppc_reloc = R_PPC_REL32;                break;
894     case BFD_RELOC_32_PLTOFF:           ppc_reloc = R_PPC_PLT32;                break;
895     case BFD_RELOC_32_PLT_PCREL:        ppc_reloc = R_PPC_PLTREL32;             break;
896     case BFD_RELOC_LO16_PLTOFF:         ppc_reloc = R_PPC_PLT16_LO;             break;
897     case BFD_RELOC_HI16_PLTOFF:         ppc_reloc = R_PPC_PLT16_HI;             break;
898     case BFD_RELOC_HI16_S_PLTOFF:       ppc_reloc = R_PPC_PLT16_HA;             break;
899     case BFD_RELOC_GPREL16:             ppc_reloc = R_PPC_SDAREL16;             break;
900     case BFD_RELOC_32_BASEREL:          ppc_reloc = R_PPC_SECTOFF;              break;
901     case BFD_RELOC_LO16_BASEREL:        ppc_reloc = R_PPC_SECTOFF_LO;           break;
902     case BFD_RELOC_HI16_BASEREL:        ppc_reloc = R_PPC_SECTOFF_HI;           break;
903     case BFD_RELOC_HI16_S_BASEREL:      ppc_reloc = R_PPC_SECTOFF_HA;           break;
904     case BFD_RELOC_CTOR:                ppc_reloc = R_PPC_ADDR32;               break;
905     case BFD_RELOC_PPC_TOC16:           ppc_reloc = R_PPC_TOC16;                break;
906     }
907
908   return ppc_elf_howto_table[ (int)ppc_reloc ];
909 };
910
911 /* Set the howto pointer for a PowerPC ELF reloc.  */
912
913 static void
914 ppc_elf_info_to_howto (abfd, cache_ptr, dst)
915      bfd *abfd;
916      arelent *cache_ptr;
917      Elf32_Internal_Rela *dst;
918 {
919   if (!ppc_elf_howto_table[ R_PPC_ADDR32 ])     /* Initialize howto table if needed */
920     ppc_elf_howto_init ();
921
922   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
923   cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
924 }
925
926 /* Function to set whether a module needs the -mrelocatable bit set. */
927
928 static boolean
929 ppc_elf_set_private_flags (abfd, flags)
930      bfd *abfd;
931      flagword flags;
932 {
933   BFD_ASSERT (!elf_flags_init (abfd)
934               || elf_elfheader (abfd)->e_flags == flags);
935
936   elf_elfheader (abfd)->e_flags = flags;
937   elf_flags_init (abfd) = true;
938   return true;
939 }
940
941 /* Copy backend specific data from one object module to another */
942 static boolean
943 ppc_elf_copy_private_bfd_data (ibfd, obfd)
944      bfd *ibfd;
945      bfd *obfd;
946 {
947   /* This function is selected based on the input vector.  We only
948      want to copy information over if the output BFD also uses Elf
949      format.  */
950   if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
951     return true;
952
953   BFD_ASSERT (!elf_flags_init (obfd)
954               || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
955
956   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
957   elf_flags_init (obfd) = true;
958   return true;
959 }
960
961 /* Merge backend specific data from an object file to the output
962    object file when linking */
963 static boolean
964 ppc_elf_merge_private_bfd_data (ibfd, obfd)
965      bfd *ibfd;
966      bfd *obfd;
967 {
968   flagword old_flags;
969   flagword new_flags;
970   boolean error;
971
972   /* Check if we have the same endianess */
973   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
974       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
975     {
976       (*_bfd_error_handler)
977         ("%s: compiled for a %s endian system and target is %s endian",
978          bfd_get_filename (ibfd),
979          bfd_big_endian (ibfd) ? "big" : "little",
980          bfd_big_endian (obfd) ? "big" : "little");
981
982       bfd_set_error (bfd_error_wrong_format);
983       return false;
984     }
985
986   /* This function is selected based on the input vector.  We only
987      want to copy information over if the output BFD also uses Elf
988      format.  */
989   if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
990     return true;
991
992   new_flags = elf_elfheader (ibfd)->e_flags;
993   old_flags = elf_elfheader (obfd)->e_flags;
994   if (!elf_flags_init (obfd))   /* First call, no flags set */
995     {
996       elf_flags_init (obfd) = true;
997       elf_elfheader (obfd)->e_flags = new_flags;
998     }
999
1000   else if (new_flags == old_flags)      /* Compatible flags are ok */
1001     ;
1002
1003   else                                  /* Incompatible flags */
1004     {
1005       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib to be linked
1006          with either.  */
1007       error = false;
1008       if ((new_flags & EF_PPC_RELOCATABLE) != 0
1009           && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1010         {
1011           error = true;
1012           (*_bfd_error_handler)
1013             ("%s: compiled with -mrelocatable and linked with modules compiled normally",
1014              bfd_get_filename (ibfd));
1015         }
1016       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1017                && (old_flags & EF_PPC_RELOCATABLE) != 0)
1018         {
1019           error = true;
1020           (*_bfd_error_handler)
1021             ("%s: compiled normally and linked with modules compiled with -mrelocatable",
1022              bfd_get_filename (ibfd));
1023         }
1024       else if ((new_flags & EF_PPC_RELOCATABLE_LIB) != 0)
1025         elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE_LIB;
1026
1027       new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB);
1028       old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB);
1029
1030       /* Warn about eabi vs. V.4 mismatch */
1031       if ((new_flags & EF_PPC_EMB) != 0 && (old_flags & EF_PPC_EMB) == 0)
1032         {
1033           new_flags &= ~EF_PPC_EMB;
1034           error = true;
1035           (*_bfd_error_handler)
1036             ("%s: compiled for the eabi and linked with modules compiled for System V",
1037              bfd_get_filename (ibfd));
1038         }
1039       else if ((new_flags & EF_PPC_EMB) == 0 && (old_flags & EF_PPC_EMB) != 0)
1040         {
1041           old_flags &= ~EF_PPC_EMB;
1042           error = true;
1043           (*_bfd_error_handler)
1044             ("%s: compiled for System V and linked with modules compiled for eabi",
1045              bfd_get_filename (ibfd));
1046         }
1047
1048       /* Warn about any other mismatches */
1049       if (new_flags != old_flags)
1050         {
1051           error = true;
1052           (*_bfd_error_handler)
1053             ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
1054              bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
1055         }
1056
1057       if (error)
1058         {
1059           bfd_set_error (bfd_error_bad_value);
1060           return false;
1061         }
1062     }
1063
1064   return true;
1065 }
1066
1067 \f
1068 /* ELF relocs are against symbols.  If we are producing relocateable
1069    output, and the reloc is against an external symbol, and nothing
1070    has given us any additional addend, the resulting reloc will also
1071    be against the same symbol.  In such a case, we don't want to
1072    change anything about the way the reloc is handled, since it will
1073    all be done at final link time.  Rather than put special case code
1074    into bfd_perform_relocation, all the reloc types use this howto
1075    function.  It just short circuits the reloc if producing
1076    relocateable output against an external symbol.  */
1077
1078 /*ARGSUSED*/
1079 static bfd_reloc_status_type
1080 ppc_elf_std_reloc (abfd,
1081                    reloc_entry,
1082                    symbol,
1083                    data,
1084                    input_section,
1085                    output_bfd,
1086                    error_message)
1087      bfd *abfd;
1088      arelent *reloc_entry;
1089      asymbol *symbol;
1090      PTR data;
1091      asection *input_section;
1092      bfd *output_bfd;
1093      char **error_message;
1094 {
1095   if (output_bfd != (bfd *) NULL
1096       && (symbol->flags & BSF_SECTION_SYM) == 0
1097       && (! reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1098     {
1099       reloc_entry->address += input_section->output_offset;
1100       return bfd_reloc_ok;
1101     }
1102
1103   return bfd_reloc_continue;
1104 }
1105
1106 /* Don't pretend we can deal with unsupported relocs.  */
1107
1108 /*ARGSUSED*/
1109 static bfd_reloc_status_type
1110 ppc_elf_unsupported_reloc (abfd, reloc_entry, symbol, data, input_section,
1111                            output_bfd, error_message)
1112      bfd *abfd;
1113      arelent *reloc_entry;
1114      asymbol *symbol;
1115      PTR data;
1116      asection *input_section;
1117      bfd *output_bfd;
1118      char **error_message;
1119 {
1120   BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
1121   (*_bfd_error_handler)
1122     ("%s: relocation %s (%d) is not currently supported",
1123      bfd_get_filename (abfd),
1124      reloc_entry->howto->name,
1125      reloc_entry->howto->type);
1126
1127   return bfd_reloc_notsupported;
1128 }
1129
1130 \f
1131 /* Adjust a symbol defined by a dynamic object and referenced by a
1132    regular object.  The current definition is in some section of the
1133    dynamic object, but we're not including those sections.  We have to
1134    change the definition to something the rest of the link can
1135    understand.  */
1136
1137 static boolean
1138 ppc_elf_adjust_dynamic_symbol (info, h)
1139      struct bfd_link_info *info;
1140      struct elf_link_hash_entry *h;
1141 {
1142 #ifdef DEBUG
1143   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called\n");
1144 #endif
1145   return true;
1146 }
1147
1148 \f
1149 /* Increment the index of a dynamic symbol by a given amount.  Called
1150    via elf_link_hash_traverse.  */
1151
1152 static boolean
1153 ppc_elf_adjust_dynindx (h, cparg)
1154      struct elf_link_hash_entry *h;
1155      PTR cparg;
1156 {
1157   int *cp = (int *) cparg;
1158
1159 #ifdef DEBUG
1160   fprintf (stderr, "ppc_elf_adjust_dynindx called, h->dynindx = %d, *cp = %d\n", h->dynindx, *cp);
1161 #endif
1162
1163   if (h->dynindx != -1)
1164     h->dynindx += *cp;
1165
1166   return true;
1167 }
1168
1169 \f
1170 /* Set the sizes of the dynamic sections.  */
1171
1172 static boolean
1173 ppc_elf_size_dynamic_sections (output_bfd, info)
1174      bfd *output_bfd;
1175      struct bfd_link_info *info;
1176 {
1177   bfd *dynobj;
1178   asection *s;
1179   boolean reltext;
1180   boolean relplt;
1181
1182 #ifdef DEBUG
1183   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
1184 #endif
1185
1186   dynobj = elf_hash_table (info)->dynobj;
1187   BFD_ASSERT (dynobj != NULL);
1188
1189   if (elf_hash_table (info)->dynamic_sections_created)
1190     {
1191       /* Set the contents of the .interp section to the interpreter.  */
1192       if (! info->shared)
1193         {
1194           s = bfd_get_section_by_name (dynobj, ".interp");
1195           BFD_ASSERT (s != NULL);
1196           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1197           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1198         }
1199
1200       /* Make space for the trailing nop in .plt.  */
1201       s = bfd_get_section_by_name (dynobj, ".plt");
1202       BFD_ASSERT (s != NULL);
1203       if (s->_raw_size > 0)
1204         s->_raw_size += 4;
1205     }
1206   else
1207     {
1208       /* We may have created entries in the .rela.got section.
1209          However, if we are not creating the dynamic sections, we will
1210          not actually use these entries.  Reset the size of .rela.got,
1211          which will cause it to get stripped from the output file
1212          below.  */
1213       s = bfd_get_section_by_name (dynobj, ".rela.got");
1214       if (s != NULL)
1215         s->_raw_size = 0;
1216     }
1217
1218   /* The check_relocs and adjust_dynamic_symbol entry points have
1219      determined the sizes of the various dynamic sections.  Allocate
1220      memory for them.  */
1221   reltext = false;
1222   relplt = false;
1223   for (s = dynobj->sections; s != NULL; s = s->next)
1224     {
1225       const char *name;
1226       boolean strip;
1227
1228       if ((s->flags & SEC_IN_MEMORY) == 0)
1229         continue;
1230
1231       /* It's OK to base decisions on the section name, because none
1232          of the dynobj section names depend upon the input files.  */
1233       name = bfd_get_section_name (dynobj, s);
1234
1235       strip = false;
1236
1237       if (strncmp (name, ".rela", 5) == 0)
1238         {
1239           if (s->_raw_size == 0)
1240             {
1241               /* If we don't need this section, strip it from the
1242                  output file.  This is to handle .rela.bss and
1243                  .rel.plt.  We must create it in
1244                  create_dynamic_sections, because it must be created
1245                  before the linker maps input sections to output
1246                  sections.  The linker does that before
1247                  adjust_dynamic_symbol is called, and it is that
1248                  function which decides whether anything needs to go
1249                  into these sections.  */
1250               strip = true;
1251             }
1252           else
1253             {
1254               asection *target;
1255
1256               /* If this relocation section applies to a read only
1257                  section, then we probably need a DT_TEXTREL entry.  */
1258               target = bfd_get_section_by_name (output_bfd, name + 5);
1259               if (target != NULL
1260                   && (target->flags & SEC_READONLY) != 0)
1261                 reltext = true;
1262
1263               if (strcmp (name, ".rela.plt") == 0)
1264                 relplt = true;
1265
1266               /* We use the reloc_count field as a counter if we need
1267                  to copy relocs into the output file.  */
1268               s->reloc_count = 0;
1269             }
1270         }
1271       else if (strcmp (name, ".plt") != 0
1272                && strcmp (name, ".got") != 0)
1273         {
1274           /* It's not one of our sections, so don't allocate space.  */
1275           continue;
1276         }
1277
1278       if (strip)
1279         {
1280           asection **spp;
1281
1282           for (spp = &s->output_section->owner->sections;
1283                *spp != s->output_section;
1284                spp = &(*spp)->next)
1285             ;
1286           *spp = s->output_section->next;
1287           --s->output_section->owner->section_count;
1288
1289           continue;
1290         }
1291
1292       /* Allocate memory for the section contents.  */
1293       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1294       if (s->contents == NULL && s->_raw_size != 0)
1295         return false;
1296     }
1297
1298   if (elf_hash_table (info)->dynamic_sections_created)
1299     {
1300       /* Add some entries to the .dynamic section.  We fill in the
1301          values later, in ppc_elf_finish_dynamic_sections, but we
1302          must add the entries now so that we get the correct size for
1303          the .dynamic section.  The DT_DEBUG entry is filled in by the
1304          dynamic linker and used by the debugger.  */
1305       if (! info->shared)
1306         {
1307           if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1308             return false;
1309         }
1310
1311       if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
1312         return false;
1313
1314       if (relplt)
1315         {
1316           if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1317               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1318               || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1319             return false;
1320         }
1321
1322       if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1323           || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1324           || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1325                                             sizeof (Elf32_External_Rela)))
1326         return false;
1327
1328       if (reltext)
1329         {
1330           if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1331             return false;
1332         }
1333     }
1334
1335   /* If we are generating a shared library, we generate a section
1336      symbol for each output section.  These are local symbols, which
1337      means that they must come first in the dynamic symbol table.
1338      That means we must increment the dynamic symbol index of every
1339      other dynamic symbol.  */
1340   if (info->shared)
1341     {
1342       int c, i;
1343
1344       c = bfd_count_sections (output_bfd);
1345       elf_link_hash_traverse (elf_hash_table (info),
1346                               ppc_elf_adjust_dynindx,
1347                               (PTR) &c);
1348       elf_hash_table (info)->dynsymcount += c;
1349
1350       for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
1351         {
1352           elf_section_data (s)->dynindx = i;
1353           /* These symbols will have no names, so we don't need to
1354              fiddle with dynstr_index.  */
1355         }
1356     }
1357
1358   return true;
1359 }
1360
1361 \f
1362 /* Look through the relocs for a section during the first phase, and
1363    allocate space in the global offset table or procedure linkage
1364    table.  */
1365
1366 static boolean
1367 ppc_elf_check_relocs (abfd, info, sec, relocs)
1368      bfd *abfd;
1369      struct bfd_link_info *info;
1370      asection *sec;
1371      const Elf_Internal_Rela *relocs;
1372 {
1373   bfd *dynobj;
1374   Elf_Internal_Shdr *symtab_hdr;
1375   struct elf_link_hash_entry **sym_hashes;
1376   bfd_vma *local_got_offsets;
1377   const Elf_Internal_Rela *rel;
1378   const Elf_Internal_Rela *rel_end;
1379   asection *sgot;
1380   asection *srelgot;
1381   asection *sreloc;
1382
1383   if (info->relocateable)
1384     return true;
1385
1386 #ifdef DEBUG
1387   fprintf (stderr, "ppc_elf_check_relocs called for section %s\n", sec->name);
1388 #endif
1389
1390   dynobj = elf_hash_table (info)->dynobj;
1391   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1392   sym_hashes = elf_sym_hashes (abfd);
1393   local_got_offsets = elf_local_got_offsets (abfd);
1394
1395   sgot = NULL;
1396   srelgot = NULL;
1397   sreloc = NULL;
1398
1399   rel_end = relocs + sec->reloc_count;
1400   for (rel = relocs; rel < rel_end; rel++)
1401     {
1402       unsigned long r_symndx;
1403       struct elf_link_hash_entry *h;
1404
1405       r_symndx = ELF32_R_SYM (rel->r_info);
1406       if (r_symndx < symtab_hdr->sh_info)
1407         h = NULL;
1408       else
1409         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1410
1411       switch (ELF32_R_TYPE (rel->r_info))
1412         {
1413         case R_PPC_GOT16:
1414         case R_PPC_GOT16_LO:
1415         case R_PPC_GOT16_HI:
1416         case R_PPC_GOT16_HA:
1417 #ifdef DEBUG
1418           fprintf (stderr, "Reloc requires a GOT entry\n");
1419 #endif
1420           /* This symbol requires a global offset table entry.  */
1421
1422           if (dynobj == NULL)
1423             {
1424               /* Create the .got section.  */
1425               elf_hash_table (info)->dynobj = dynobj = abfd;
1426               if (! _bfd_elf_create_got_section (dynobj, info))
1427                 return false;
1428             }
1429
1430           if (sgot == NULL)
1431             {
1432               sgot = bfd_get_section_by_name (dynobj, ".got");
1433               BFD_ASSERT (sgot != NULL);
1434             }
1435
1436           if (srelgot == NULL
1437               && (h != NULL || info->shared))
1438             {
1439               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1440               if (srelgot == NULL)
1441                 {
1442                   srelgot = bfd_make_section (dynobj, ".rela.got");
1443                   if (srelgot == NULL
1444                       || ! bfd_set_section_flags (dynobj, srelgot,
1445                                                   (SEC_ALLOC
1446                                                    | SEC_LOAD
1447                                                    | SEC_HAS_CONTENTS
1448                                                    | SEC_IN_MEMORY
1449                                                    | SEC_READONLY))
1450                       || ! bfd_set_section_alignment (dynobj, srelgot, 2))
1451                     return false;
1452                 }
1453             }
1454
1455           if (h != NULL)
1456             {
1457               if (h->got_offset != (bfd_vma) -1)
1458                 {
1459                   /* We have already allocated space in the .got.  */
1460                   break;
1461                 }
1462               h->got_offset = sgot->_raw_size;
1463
1464               /* Make sure this symbol is output as a dynamic symbol.  */
1465               if (h->dynindx == -1)
1466                 {
1467                   if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1468                     return false;
1469                 }
1470
1471               srelgot->_raw_size += sizeof (Elf32_External_Rela);
1472             }
1473           else
1474             {
1475               /* This is a global offset table entry for a local
1476                  symbol.  */
1477               if (local_got_offsets == NULL)
1478                 {
1479                   size_t size;
1480                   register unsigned int i;
1481
1482                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
1483                   local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
1484                   if (local_got_offsets == NULL)
1485                     return false;
1486                   elf_local_got_offsets (abfd) = local_got_offsets;
1487                   for (i = 0; i < symtab_hdr->sh_info; i++)
1488                     local_got_offsets[i] = (bfd_vma) -1;
1489                 }
1490               if (local_got_offsets[r_symndx] != (bfd_vma) -1)
1491                 {
1492                   /* We have already allocated space in the .got.  */
1493                   break;
1494                 }
1495               local_got_offsets[r_symndx] = sgot->_raw_size;
1496
1497               if (info->shared)
1498                 {
1499                   /* If we are generating a shared object, we need to
1500                      output a R_PPC_RELATIVE reloc so that the
1501                      dynamic linker can adjust this GOT entry.  */
1502                   srelgot->_raw_size += sizeof (Elf32_External_Rela);
1503                 }
1504             }
1505
1506           sgot->_raw_size += 4;
1507
1508           break;
1509
1510         case R_PPC_PLT32:
1511         case R_PPC_PLTREL24:
1512         case R_PPC_PLT16_LO:
1513         case R_PPC_PLT16_HI:
1514         case R_PPC_PLT16_HA:
1515 #ifdef DEBUG
1516           fprintf (stderr, "Reloc requires a PLT entry\n");
1517 #endif
1518           /* This symbol requires a procedure linkage table entry.  We
1519              actually build the entry in adjust_dynamic_symbol,
1520              because this might be a case of linking PIC code without
1521              linking in any dynamic objects, in which case we don't
1522              need to generate a procedure linkage table after all.  */
1523
1524           if (h == NULL)
1525             {
1526               /* It does not make sense to have a procedure linkage
1527                  table entry for a local symbol.  */
1528               bfd_set_error (bfd_error_bad_value);
1529               return false;
1530             }
1531
1532           /* Make sure this symbol is output as a dynamic symbol.  */
1533           if (h->dynindx == -1)
1534             {
1535               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1536                 return false;
1537             }
1538
1539           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1540           break;
1541
1542 #if 0
1543         case R_SPARC_PC10:
1544         case R_SPARC_PC22:
1545           if (h != NULL
1546               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1547             break;
1548           /* Fall through.  */
1549         case R_SPARC_DISP8:
1550         case R_SPARC_DISP16:
1551         case R_SPARC_DISP32:
1552         case R_SPARC_WDISP30:
1553         case R_SPARC_WDISP22:
1554           if (h == NULL)
1555             break;
1556           /* Fall through.  */
1557         case R_SPARC_8:
1558         case R_SPARC_16:
1559         case R_SPARC_32:
1560         case R_SPARC_HI22:
1561         case R_SPARC_22:
1562         case R_SPARC_13:
1563         case R_SPARC_LO10:
1564         case R_SPARC_UA32:
1565           if (info->shared
1566               && (sec->flags & SEC_ALLOC) != 0)
1567             {
1568               /* When creating a shared object, we must copy these
1569                  relocs into the output file.  We create a reloc
1570                  section in dynobj and make room for the reloc.  */
1571               if (sreloc == NULL)
1572                 {
1573                   const char *name;
1574
1575                   name = (bfd_elf_string_from_elf_section
1576                           (abfd,
1577                            elf_elfheader (abfd)->e_shstrndx,
1578                            elf_section_data (sec)->rel_hdr.sh_name));
1579                   if (name == NULL)
1580                     return false;
1581
1582                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1583                               && strcmp (bfd_get_section_name (abfd, sec),
1584                                          name + 5) == 0);
1585
1586                   sreloc = bfd_get_section_by_name (dynobj, name);
1587                   if (sreloc == NULL)
1588                     {
1589                       sreloc = bfd_make_section (dynobj, name);
1590                       if (sreloc == NULL
1591                           || ! bfd_set_section_flags (dynobj, sreloc,
1592                                                       (SEC_ALLOC
1593                                                        | SEC_LOAD
1594                                                        | SEC_HAS_CONTENTS
1595                                                        | SEC_IN_MEMORY
1596                                                        | SEC_READONLY))
1597                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1598                         return false;
1599                     }
1600                 }
1601
1602               sreloc->_raw_size += sizeof (Elf32_External_Rela);
1603             }
1604
1605           break;
1606 #endif
1607
1608         default:
1609           break;
1610         }
1611     }
1612
1613   return true;
1614 }
1615
1616 \f
1617 /* Finish up dynamic symbol handling.  We set the contents of various
1618    dynamic sections here.  */
1619
1620 static boolean
1621 ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
1622      bfd *output_bfd;
1623      struct bfd_link_info *info;
1624      struct elf_link_hash_entry *h;
1625      Elf_Internal_Sym *sym;
1626 {
1627   bfd *dynobj;
1628
1629 #ifdef DEBUG
1630   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called\n");
1631 #endif
1632
1633   dynobj = elf_hash_table (info)->dynobj;
1634
1635   if (h->plt_offset != (bfd_vma) -1)
1636     {
1637       asection *splt;
1638       asection *srela;
1639       Elf_Internal_Rela rela;
1640
1641       /* This symbol has an entry in the procedure linkage table.  Set
1642          it up.  */
1643
1644       BFD_ASSERT (h->dynindx != -1);
1645
1646       splt = bfd_get_section_by_name (dynobj, ".plt");
1647       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1648       BFD_ASSERT (splt != NULL && srela != NULL);
1649
1650       /* Fill in the entry in the procedure linkage table.  */
1651 #if 0
1652       bfd_put_32 (output_bfd,
1653                   PLT_ENTRY_WORD0 + h->plt_offset,
1654                   splt->contents + h->plt_offset);
1655       bfd_put_32 (output_bfd,
1656                   (PLT_ENTRY_WORD1
1657                    + (((- (h->plt_offset + 4)) >> 2) & 0x3fffff)),
1658                   splt->contents + h->plt_offset + 4);
1659       bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1660                   splt->contents + h->plt_offset + 8);
1661
1662       /* Fill in the entry in the .rela.plt section.  */
1663       rela.r_offset = (splt->output_section->vma
1664                        + splt->output_offset
1665                        + h->plt_offset);
1666       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1667       rela.r_addend = 0;
1668       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1669                                  ((Elf32_External_Rela *) srela->contents
1670                                   + h->plt_offset / PLT_ENTRY_SIZE - 4));
1671 #endif
1672
1673       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1674         {
1675           /* Mark the symbol as undefined, rather than as defined in
1676              the .plt section.  Leave the value alone.  */
1677           sym->st_shndx = SHN_UNDEF;
1678         }
1679     }
1680
1681   if (h->got_offset != (bfd_vma) -1)
1682     {
1683       asection *sgot;
1684       asection *srela;
1685       Elf_Internal_Rela rela;
1686
1687       /* This symbol has an entry in the global offset table.  Set it
1688          up.  */
1689
1690       BFD_ASSERT (h->dynindx != -1);
1691
1692       sgot = bfd_get_section_by_name (dynobj, ".got");
1693       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1694       BFD_ASSERT (sgot != NULL && srela != NULL);
1695
1696       rela.r_offset = (sgot->output_section->vma
1697                        + sgot->output_offset
1698                        + (h->got_offset &~ 1));
1699
1700       /* If this is a -Bsymbolic link, and the symbol is defined
1701          locally, we just want to emit a RELATIVE reloc.  The entry in
1702          the global offset table will already have been initialized in
1703          the relocate_section function.  */
1704       if (info->shared
1705           && info->symbolic
1706           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1707         rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
1708       else
1709         {
1710           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1711           rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_GLOB_DAT);
1712         }
1713
1714       rela.r_addend = 0;
1715       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1716                                  ((Elf32_External_Rela *) srela->contents
1717                                   + srela->reloc_count));
1718       ++srela->reloc_count;
1719     }
1720
1721   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1722     {
1723       asection *s;
1724       Elf_Internal_Rela rela;
1725
1726       /* This symbols needs a copy reloc.  Set it up.  */
1727
1728       BFD_ASSERT (h->dynindx != -1);
1729
1730       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1731                                    ".rela.bss");
1732       BFD_ASSERT (s != NULL);
1733
1734       rela.r_offset = (h->root.u.def.value
1735                        + h->root.u.def.section->output_section->vma
1736                        + h->root.u.def.section->output_offset);
1737       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
1738       rela.r_addend = 0;
1739       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1740                                  ((Elf32_External_Rela *) s->contents
1741                                   + s->reloc_count));
1742       ++s->reloc_count;
1743     }
1744
1745   /* Mark some specially defined symbols as absolute.  */
1746   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1747       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1748       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1749     sym->st_shndx = SHN_ABS;
1750
1751   return true;
1752 }
1753
1754 \f
1755 /* Finish up the dynamic sections.  */
1756
1757 static boolean
1758 ppc_elf_finish_dynamic_sections (output_bfd, info)
1759      bfd *output_bfd;
1760      struct bfd_link_info *info;
1761 {
1762   bfd *dynobj;
1763   asection *sdyn;
1764   asection *sgot;
1765
1766 #ifdef DEBUG
1767   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
1768 #endif
1769
1770   dynobj = elf_hash_table (info)->dynobj;
1771
1772   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1773
1774   if (elf_hash_table (info)->dynamic_sections_created)
1775     {
1776       asection *splt;
1777       Elf32_External_Dyn *dyncon, *dynconend;
1778
1779       splt = bfd_get_section_by_name (dynobj, ".plt");
1780       BFD_ASSERT (splt != NULL && sdyn != NULL);
1781
1782       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1783       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1784       for (; dyncon < dynconend; dyncon++)
1785         {
1786           Elf_Internal_Dyn dyn;
1787           const char *name;
1788           boolean size;
1789
1790           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1791
1792           switch (dyn.d_tag)
1793             {
1794             case DT_PLTGOT:   name = ".plt"; size = false; break;
1795             case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1796             case DT_JMPREL:   name = ".rela.plt"; size = false; break;
1797             default:      name = NULL; size = false; break;
1798             }
1799
1800           if (name != NULL)
1801             {
1802               asection *s;
1803
1804               s = bfd_get_section_by_name (output_bfd, name);
1805               if (s == NULL)
1806                 dyn.d_un.d_val = 0;
1807               else
1808                 {
1809                   if (! size)
1810                     dyn.d_un.d_ptr = s->vma;
1811                   else
1812                     {
1813                       if (s->_cooked_size != 0)
1814                         dyn.d_un.d_val = s->_cooked_size;
1815                       else
1816                         dyn.d_un.d_val = s->_raw_size;
1817                     }
1818                 }
1819               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1820             }
1821         }
1822
1823       /* Clear the first four entries in the procedure linkage table,
1824          and put a nop in the last four bytes.  */
1825 #if 0
1826       if (splt->_raw_size > 0)
1827         {
1828           memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1829           bfd_put_32 (output_bfd, SPARC_NOP,
1830                       splt->contents + splt->_raw_size - 4);
1831         }
1832
1833       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1834         PLT_ENTRY_SIZE;
1835 #endif
1836     }
1837
1838   /* Set the first entry in the global offset table to the address of
1839      the dynamic section.  */
1840   sgot = bfd_get_section_by_name (dynobj, ".got");
1841   BFD_ASSERT (sgot != NULL);
1842   if (sgot->_raw_size > 0)
1843     {
1844       if (sdyn == NULL)
1845         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1846       else
1847         bfd_put_32 (output_bfd,
1848                     sdyn->output_section->vma + sdyn->output_offset,
1849                     sgot->contents);
1850     }
1851
1852   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1853
1854   if (info->shared)
1855     {
1856       asection *sdynsym;
1857       asection *s;
1858       Elf_Internal_Sym sym;
1859
1860       /* Set up the section symbols for the output sections.  */
1861
1862       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1863       BFD_ASSERT (sdynsym != NULL);
1864
1865       sym.st_size = 0;
1866       sym.st_name = 0;
1867       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1868       sym.st_other = 0;
1869
1870       for (s = output_bfd->sections; s != NULL; s = s->next)
1871         {
1872           int indx;
1873
1874           sym.st_value = s->vma;
1875
1876           indx = elf_section_data (s)->this_idx;
1877           BFD_ASSERT (indx > 0);
1878           sym.st_shndx = indx;
1879
1880           bfd_elf32_swap_symbol_out (output_bfd, &sym,
1881                                      (PTR) (((Elf32_External_Sym *)
1882                                              sdynsym->contents)
1883                                             + elf_section_data (s)->dynindx));
1884         }
1885
1886       /* Set the sh_info field of the output .dynsym section to the
1887          index of the first global symbol.  */
1888       elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
1889         bfd_count_sections (output_bfd) + 1;
1890     }
1891
1892   return true;
1893 }
1894
1895 \f
1896 /* The RELOCATE_SECTION function is called by the ELF backend linker
1897    to handle the relocations for a section.
1898
1899    The relocs are always passed as Rela structures; if the section
1900    actually uses Rel structures, the r_addend field will always be
1901    zero.
1902
1903    This function is responsible for adjust the section contents as
1904    necessary, and (if using Rela relocs and generating a
1905    relocateable output file) adjusting the reloc addend as
1906    necessary.
1907
1908    This function does not have to worry about setting the reloc
1909    address or the reloc symbol index.
1910
1911    LOCAL_SYMS is a pointer to the swapped in local symbols.
1912
1913    LOCAL_SECTIONS is an array giving the section in the input file
1914    corresponding to the st_shndx field of each local symbol.
1915
1916    The global hash table entry for the global symbols can be found
1917    via elf_sym_hashes (input_bfd).
1918
1919    When generating relocateable output, this function must handle
1920    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
1921    going to be the section symbol corresponding to the output
1922    section, which means that the addend must be adjusted
1923    accordingly.  */
1924
1925 static boolean
1926 ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1927                           contents, relocs, local_syms, local_sections)
1928      bfd *output_bfd;
1929      struct bfd_link_info *info;
1930      bfd *input_bfd;
1931      asection *input_section;
1932      bfd_byte *contents;
1933      Elf_Internal_Rela *relocs;
1934      Elf_Internal_Sym *local_syms;
1935      asection **local_sections;
1936 {
1937   Elf_Internal_Shdr *symtab_hdr           = &elf_tdata (input_bfd)->symtab_hdr;
1938   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1939   bfd *dynobj                             = elf_hash_table (info)->dynobj;
1940   bfd_vma *local_got_offsets              = elf_local_got_offsets (input_bfd);
1941   asection *sgot                          = (asection *)0;
1942   Elf_Internal_Rela *rel                  = relocs;
1943   Elf_Internal_Rela *relend               = relocs + input_section->reloc_count;
1944   boolean ret                             = true;
1945   long insn;
1946
1947 #ifdef DEBUG
1948   fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
1949            bfd_get_filename (input_bfd),
1950            bfd_section_name(input_bfd, input_section),
1951            (long)input_section->reloc_count,
1952            (info->relocateable) ? " (relocatable)" : "");
1953 #endif
1954
1955   if (!ppc_elf_howto_table[ R_PPC_ADDR32 ])     /* Initialize howto table if needed */
1956     ppc_elf_howto_init ();
1957
1958   for (; rel < relend; rel++)
1959     {
1960       enum ppc_reloc_type r_type        = (enum ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
1961       bfd_vma offset                    = rel->r_offset;
1962       bfd_vma addend                    = rel->r_addend;
1963       bfd_reloc_status_type r           = bfd_reloc_other;
1964       Elf_Internal_Sym *sym             = (Elf_Internal_Sym *)0;
1965       asection *sec                     = (asection *)0;
1966       struct elf_link_hash_entry *h     = (struct elf_link_hash_entry *)0;
1967       reloc_howto_type *howto;
1968       unsigned long r_symndx;
1969       bfd_vma relocation;
1970
1971       /* Unknown relocation handling */
1972       if ((unsigned)r_type >= (unsigned)R_PPC_max || !ppc_elf_howto_table[(int)r_type])
1973         {
1974           (*_bfd_error_handler)
1975             ("%s: unknown relocation type %d",
1976              bfd_get_filename (input_bfd),
1977              (int)r_type);
1978
1979           bfd_set_error (bfd_error_bad_value);
1980           ret = false;
1981           continue;
1982         }
1983
1984       howto = ppc_elf_howto_table[(int)r_type];
1985       r_symndx = ELF32_R_SYM (rel->r_info);
1986
1987       if (info->relocateable)
1988         {
1989           /* This is a relocateable link.  We don't have to change
1990              anything, unless the reloc is against a section symbol,
1991              in which case we have to adjust according to where the
1992              section symbol winds up in the output section.  */
1993           if (r_symndx < symtab_hdr->sh_info)
1994             {
1995               sym = local_syms + r_symndx;
1996               if ((unsigned)ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1997                 {
1998                   sec = local_sections[r_symndx];
1999                   addend = rel->r_addend += sec->output_offset + sym->st_value;
2000                 }
2001             }
2002
2003 #ifdef DEBUG
2004           fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
2005                    howto->name,
2006                    (int)r_type,
2007                    r_symndx,
2008                    (long)offset,
2009                    (long)addend);
2010 #endif
2011           continue;
2012         }
2013
2014       /* This is a final link.  */
2015
2016       /* Complain about known relocation that are not yet supported */
2017       if (howto->special_function == ppc_elf_unsupported_reloc)
2018         {
2019           (*_bfd_error_handler)
2020             ("%s: relocation %s (%d) is not currently supported",
2021              bfd_get_filename (input_bfd),
2022              howto->name,
2023              (int)r_type);
2024
2025           bfd_set_error (bfd_error_bad_value);
2026           ret = false;
2027           continue;
2028         }
2029
2030       if (r_symndx < symtab_hdr->sh_info)
2031         {
2032           sym = local_syms + r_symndx;
2033           sec = local_sections[r_symndx];
2034           relocation = (sec->output_section->vma
2035                         + sec->output_offset
2036                         + sym->st_value);
2037         }
2038       else
2039         {
2040           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2041           if (h->root.type == bfd_link_hash_defined
2042               || h->root.type == bfd_link_hash_defweak)
2043             {
2044               sec = h->root.u.def.section;
2045               relocation = (h->root.u.def.value
2046                             + sec->output_section->vma
2047                             + sec->output_offset);
2048             }
2049           else if (h->root.type == bfd_link_hash_undefweak)
2050             relocation = 0;
2051           else if (info->shared)
2052             relocation = 0;
2053           else
2054             {
2055               (*info->callbacks->undefined_symbol)(info,
2056                                                    h->root.root.string,
2057                                                    input_bfd,
2058                                                    input_section,
2059                                                    rel->r_offset);
2060               ret = false;
2061               continue;
2062             }
2063         }
2064
2065       switch ((int)r_type)
2066         {
2067         default:
2068           break;
2069
2070         case (int)R_PPC_ADDR14_BRTAKEN:         /* branch taken prediction relocations */
2071         case (int)R_PPC_REL14_BRTAKEN:
2072           insn = bfd_get_32 (output_bfd, contents + offset);
2073           if ((relocation - offset) & 0x8000)
2074             insn &= ~BRANCH_PREDICT_BIT;
2075           else
2076             insn |= BRANCH_PREDICT_BIT;
2077           bfd_put_32 (output_bfd, insn, contents + offset);
2078           break;
2079
2080         case (int)R_PPC_ADDR14_BRNTAKEN:        /* branch not taken predicition relocations */
2081         case (int)R_PPC_REL14_BRNTAKEN:
2082           insn = bfd_get_32 (output_bfd, contents + offset);
2083           if ((relocation - offset) & 0x8000)
2084             insn |= BRANCH_PREDICT_BIT;
2085           else
2086             insn &= ~BRANCH_PREDICT_BIT;
2087           bfd_put_32 (output_bfd, insn, contents + offset);
2088           break;
2089
2090         case (int)R_PPC_GOT16:                  /* GOT16 relocations */
2091         case (int)R_PPC_GOT16_LO:
2092         case (int)R_PPC_GOT16_HI:
2093         case (int)R_PPC_SDAREL16:
2094           fprintf (stderr, "GOT relocations in section %s from section %s\n", input_section->name, sec->name);
2095           BFD_ASSERT (sec != (asection *)0);
2096           if (!sgot)
2097             {
2098               sgot = bfd_get_section_by_name (dynobj, ".got");
2099               BFD_ASSERT (sgot != NULL);
2100             }
2101
2102           if (h != NULL)
2103             {
2104               bfd_vma off;
2105
2106               off = h->got_offset;
2107               BFD_ASSERT (off != (bfd_vma) -1);
2108
2109               if (! elf_hash_table (info)->dynamic_sections_created
2110                   || (info->shared
2111                       && info->symbolic
2112                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2113                 {
2114                   /* This is actually a static link, or it is a
2115                      -Bsymbolic link and the symbol is defined
2116                      locally.  We must initialize this entry in the
2117                      global offset table.  Since the offset must
2118                      always be a multiple of 4, we use the least
2119                      significant bit to record whether we have
2120                      initialized it already.
2121
2122                      When doing a dynamic link, we create a .rela.got
2123                      relocation entry to initialize the value.  This
2124                      is done in the finish_dynamic_symbol routine.  */
2125                   if ((off & 1) != 0)
2126                     off &= ~1;
2127                   else
2128                     {
2129                       bfd_put_32 (output_bfd, relocation,
2130                                   sgot->contents + off);
2131                       h->got_offset |= 1;
2132                     }
2133                 }
2134
2135               relocation = sgot->output_offset + off;
2136             }
2137           else
2138             {
2139               bfd_vma off;
2140
2141               BFD_ASSERT (local_got_offsets != NULL
2142                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
2143
2144               off = local_got_offsets[r_symndx];
2145
2146               /* The offset must always be a multiple of 4.  We use
2147                  the least significant bit to record whether we have
2148                  already processed this entry.  */
2149               if ((off & 1) != 0)
2150                 off &= ~1;
2151               else
2152                 {
2153                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
2154
2155                   if (info->shared)
2156                     {
2157                       asection *srelgot;
2158                       Elf_Internal_Rela outrel;
2159
2160                       /* We need to generate a R_SPARC_RELATIVE reloc
2161                          for the dynamic linker.  */
2162                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2163                       BFD_ASSERT (srelgot != NULL);
2164
2165                       outrel.r_offset = (sgot->output_section->vma
2166                                          + sgot->output_offset
2167                                          + off);
2168                       outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
2169                       outrel.r_addend = 0;
2170                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2171                                                  (((Elf32_External_Rela *)
2172                                                    srelgot->contents)
2173                                                   + srelgot->reloc_count));
2174                       ++srelgot->reloc_count;
2175                     }
2176
2177                   local_got_offsets[r_symndx] |= 1;
2178                 }
2179
2180               relocation = sgot->output_offset + off;
2181             }
2182
2183           break;
2184
2185         /* Handle the TOC16 reloc.  We want to use the offset within the .got
2186            section, not the actual VMA.  This is appropriate when generating
2187            an embedded ELF object, for which the .got section acts like the
2188            AIX .toc section.  */
2189         case (int)R_PPC_TOC16:                  /* phony GOT16 relocations */
2190           BFD_ASSERT (sec != (asection *)0);
2191           BFD_ASSERT (bfd_is_und_section (sec)
2192                       || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
2193                       || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0
2194                       || strcmp (bfd_get_section_name (abfd, sec), ".sdata") == 0
2195                       || strcmp (bfd_get_section_name (abfd, sec), ".sbss") == 0)
2196
2197           addend -= sec->output_section->vma + 0x8000;
2198           break;
2199
2200         case (int)R_PPC_ADDR16_HA:              /* arithmetic adjust relocations */
2201           BFD_ASSERT (sec != (asection *)0);
2202           addend += ((relocation + addend) & 0x8000) << 1;
2203           break;
2204         }
2205
2206
2207 #ifdef DEBUG
2208       fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
2209                howto->name,
2210                (int)r_type,
2211                r_symndx,
2212                (long)offset,
2213                (long)addend);
2214 #endif
2215
2216       r = _bfd_final_link_relocate (howto,
2217                                     input_bfd,
2218                                     input_section,
2219                                     contents,
2220                                     offset,
2221                                     relocation,
2222                                     addend);
2223
2224       if (r != bfd_reloc_ok)
2225         {
2226           ret = false;
2227           switch (r)
2228             {
2229             default:
2230               break;
2231
2232             case bfd_reloc_overflow:
2233               {
2234                 const char *name;
2235
2236                 if (h != NULL)
2237                   name = h->root.root.string;
2238                 else
2239                   {
2240                     name = bfd_elf_string_from_elf_section (input_bfd,
2241                                                             symtab_hdr->sh_link,
2242                                                             sym->st_name);
2243                     if (name == NULL)
2244                       break;
2245
2246                     if (*name == '\0')
2247                       name = bfd_section_name (input_bfd, sec);
2248                   }
2249
2250                 (*info->callbacks->reloc_overflow)(info,
2251                                                    name,
2252                                                    howto->name,
2253                                                    (bfd_vma) 0,
2254                                                    input_bfd,
2255                                                    input_section,
2256                                                    offset);
2257               }
2258               break;
2259
2260             }
2261         }
2262     }
2263
2264
2265 #ifdef DEBUG
2266   fprintf (stderr, "\n");
2267 #endif
2268
2269   return ret;
2270 }
2271
2272 #define TARGET_LITTLE_SYM       bfd_elf32_powerpcle_vec
2273 #define TARGET_LITTLE_NAME      "elf32-powerpcle"
2274 #define TARGET_BIG_SYM          bfd_elf32_powerpc_vec
2275 #define TARGET_BIG_NAME         "elf32-powerpc"
2276 #define ELF_ARCH                bfd_arch_powerpc
2277 #define ELF_MACHINE_CODE        EM_PPC
2278 #define ELF_MAXPAGESIZE         0x10000
2279 #define elf_info_to_howto       ppc_elf_info_to_howto
2280
2281 #ifdef  EM_CYGNUS_POWERPC
2282 #define ELF_MACHINE_ALT1        EM_CYGNUS_POWERPC
2283 #endif
2284
2285 #ifdef EM_PPC_OLD
2286 #define ELF_MACHINE_ALT2        EM_PPC_OLD
2287 #endif
2288
2289 #define bfd_elf32_bfd_copy_private_bfd_data     ppc_elf_copy_private_bfd_data
2290 #define bfd_elf32_bfd_merge_private_bfd_data    ppc_elf_merge_private_bfd_data
2291 #define bfd_elf32_bfd_set_private_flags         ppc_elf_set_private_flags
2292 #define bfd_elf32_bfd_reloc_type_lookup         ppc_elf_reloc_type_lookup
2293 #define elf_backend_relocate_section            ppc_elf_relocate_section
2294 #define elf_backend_create_dynamic_sections     _bfd_elf_create_dynamic_sections
2295 #define elf_backend_check_relocs                ppc_elf_check_relocs
2296 #define elf_backend_adjust_dynamic_symbol       ppc_elf_adjust_dynamic_symbol
2297 #define elf_backend_size_dynamic_sections       ppc_elf_size_dynamic_sections
2298 #define elf_backend_finish_dynamic_symbol       ppc_elf_finish_dynamic_symbol
2299 #define elf_backend_finish_dynamic_sections     ppc_elf_finish_dynamic_sections
2300
2301 #include "elf32-target.h"