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