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