6287921fa0d874aa1c4ef0f92637ccfe746e9a5a
[external/binutils.git] / bfd / elf32-ppc.c
1 /* PowerPC-specific support for 32-bit ELF
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005 Free Software Foundation, Inc.
4    Written by Ian Lance Taylor, Cygnus Support.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the
20    Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 /* This file is based on a preliminary PowerPC ELF ABI.  The
24    information may not match the final PowerPC ELF ABI.  It includes
25    suggestions from the in-progress Embedded PowerPC ABI, and that
26    information may also not match.  */
27
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/ppc.h"
34 #include "elf32-ppc.h"
35
36 /* RELA relocations are used here.  */
37
38 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc_elf_unhandled_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42
43 /* Branch prediction bit for branch taken relocs.  */
44 #define BRANCH_PREDICT_BIT 0x200000
45 /* Mask to set RA in memory instructions.  */
46 #define RA_REGISTER_MASK 0x001f0000
47 /* Value to shift register by to insert RA.  */
48 #define RA_REGISTER_SHIFT 16
49
50 /* The name of the dynamic interpreter.  This is put in the .interp
51    section.  */
52 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
53
54 /* The size in bytes of an entry in the procedure linkage table.  */
55 #define PLT_ENTRY_SIZE 12
56 /* The initial size of the plt reserved for the dynamic linker.  */
57 #define PLT_INITIAL_ENTRY_SIZE 72
58 /* The size of the gap between entries in the PLT.  */
59 #define PLT_SLOT_SIZE 8
60 /* The number of single-slot PLT entries (the rest use two slots).  */
61 #define PLT_NUM_SINGLE_ENTRIES 8192
62
63 /* Some nop instructions.  */
64 #define NOP             0x60000000
65 #define CROR_151515     0x4def7b82
66 #define CROR_313131     0x4ffffb82
67
68 /* Offset of tp and dtp pointers from start of TLS block.  */
69 #define TP_OFFSET       0x7000
70 #define DTP_OFFSET      0x8000
71 \f
72 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
73
74 static reloc_howto_type ppc_elf_howto_raw[] = {
75   /* This reloc does nothing.  */
76   HOWTO (R_PPC_NONE,            /* type */
77          0,                     /* rightshift */
78          2,                     /* size (0 = byte, 1 = short, 2 = long) */
79          32,                    /* bitsize */
80          FALSE,                 /* pc_relative */
81          0,                     /* bitpos */
82          complain_overflow_bitfield, /* complain_on_overflow */
83          bfd_elf_generic_reloc, /* special_function */
84          "R_PPC_NONE",          /* name */
85          FALSE,                 /* partial_inplace */
86          0,                     /* src_mask */
87          0,                     /* dst_mask */
88          FALSE),                /* pcrel_offset */
89
90   /* A standard 32 bit relocation.  */
91   HOWTO (R_PPC_ADDR32,          /* type */
92          0,                     /* rightshift */
93          2,                     /* size (0 = byte, 1 = short, 2 = long) */
94          32,                    /* bitsize */
95          FALSE,                 /* pc_relative */
96          0,                     /* bitpos */
97          complain_overflow_bitfield, /* complain_on_overflow */
98          bfd_elf_generic_reloc, /* special_function */
99          "R_PPC_ADDR32",        /* name */
100          FALSE,                 /* partial_inplace */
101          0,                     /* src_mask */
102          0xffffffff,            /* dst_mask */
103          FALSE),                /* pcrel_offset */
104
105   /* An absolute 26 bit branch; the lower two bits must be zero.
106      FIXME: we don't check that, we just clear them.  */
107   HOWTO (R_PPC_ADDR24,          /* type */
108          0,                     /* rightshift */
109          2,                     /* size (0 = byte, 1 = short, 2 = long) */
110          26,                    /* bitsize */
111          FALSE,                 /* pc_relative */
112          0,                     /* bitpos */
113          complain_overflow_bitfield, /* complain_on_overflow */
114          bfd_elf_generic_reloc, /* special_function */
115          "R_PPC_ADDR24",        /* name */
116          FALSE,                 /* partial_inplace */
117          0,                     /* src_mask */
118          0x3fffffc,             /* dst_mask */
119          FALSE),                /* pcrel_offset */
120
121   /* A standard 16 bit relocation.  */
122   HOWTO (R_PPC_ADDR16,          /* type */
123          0,                     /* rightshift */
124          1,                     /* size (0 = byte, 1 = short, 2 = long) */
125          16,                    /* bitsize */
126          FALSE,                 /* pc_relative */
127          0,                     /* bitpos */
128          complain_overflow_bitfield, /* complain_on_overflow */
129          bfd_elf_generic_reloc, /* special_function */
130          "R_PPC_ADDR16",        /* name */
131          FALSE,                 /* partial_inplace */
132          0,                     /* src_mask */
133          0xffff,                /* dst_mask */
134          FALSE),                /* pcrel_offset */
135
136   /* A 16 bit relocation without overflow.  */
137   HOWTO (R_PPC_ADDR16_LO,       /* type */
138          0,                     /* rightshift */
139          1,                     /* size (0 = byte, 1 = short, 2 = long) */
140          16,                    /* bitsize */
141          FALSE,                 /* pc_relative */
142          0,                     /* bitpos */
143          complain_overflow_dont,/* complain_on_overflow */
144          bfd_elf_generic_reloc, /* special_function */
145          "R_PPC_ADDR16_LO",     /* name */
146          FALSE,                 /* partial_inplace */
147          0,                     /* src_mask */
148          0xffff,                /* dst_mask */
149          FALSE),                /* pcrel_offset */
150
151   /* The high order 16 bits of an address.  */
152   HOWTO (R_PPC_ADDR16_HI,       /* type */
153          16,                    /* rightshift */
154          1,                     /* size (0 = byte, 1 = short, 2 = long) */
155          16,                    /* bitsize */
156          FALSE,                 /* pc_relative */
157          0,                     /* bitpos */
158          complain_overflow_dont, /* complain_on_overflow */
159          bfd_elf_generic_reloc, /* special_function */
160          "R_PPC_ADDR16_HI",     /* name */
161          FALSE,                 /* partial_inplace */
162          0,                     /* src_mask */
163          0xffff,                /* dst_mask */
164          FALSE),                /* pcrel_offset */
165
166   /* The high order 16 bits of an address, plus 1 if the contents of
167      the low 16 bits, treated as a signed number, is negative.  */
168   HOWTO (R_PPC_ADDR16_HA,       /* type */
169          16,                    /* rightshift */
170          1,                     /* size (0 = byte, 1 = short, 2 = long) */
171          16,                    /* bitsize */
172          FALSE,                 /* pc_relative */
173          0,                     /* bitpos */
174          complain_overflow_dont, /* complain_on_overflow */
175          ppc_elf_addr16_ha_reloc, /* special_function */
176          "R_PPC_ADDR16_HA",     /* name */
177          FALSE,                 /* partial_inplace */
178          0,                     /* src_mask */
179          0xffff,                /* dst_mask */
180          FALSE),                /* pcrel_offset */
181
182   /* An absolute 16 bit branch; the lower two bits must be zero.
183      FIXME: we don't check that, we just clear them.  */
184   HOWTO (R_PPC_ADDR14,          /* type */
185          0,                     /* rightshift */
186          2,                     /* size (0 = byte, 1 = short, 2 = long) */
187          16,                    /* 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_ADDR14",        /* name */
193          FALSE,                 /* partial_inplace */
194          0,                     /* src_mask */
195          0xfffc,                /* dst_mask */
196          FALSE),                /* pcrel_offset */
197
198   /* An absolute 16 bit branch, for which bit 10 should be set to
199      indicate that the branch is expected to be taken.  The lower two
200      bits must be zero.  */
201   HOWTO (R_PPC_ADDR14_BRTAKEN,  /* type */
202          0,                     /* rightshift */
203          2,                     /* size (0 = byte, 1 = short, 2 = long) */
204          16,                    /* bitsize */
205          FALSE,                 /* pc_relative */
206          0,                     /* bitpos */
207          complain_overflow_bitfield, /* complain_on_overflow */
208          bfd_elf_generic_reloc, /* special_function */
209          "R_PPC_ADDR14_BRTAKEN",/* name */
210          FALSE,                 /* partial_inplace */
211          0,                     /* src_mask */
212          0xfffc,                /* dst_mask */
213          FALSE),                /* pcrel_offset */
214
215   /* An absolute 16 bit branch, for which bit 10 should be set to
216      indicate that the branch is not expected to be taken.  The lower
217      two bits must be zero.  */
218   HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
219          0,                     /* rightshift */
220          2,                     /* size (0 = byte, 1 = short, 2 = long) */
221          16,                    /* bitsize */
222          FALSE,                 /* pc_relative */
223          0,                     /* bitpos */
224          complain_overflow_bitfield, /* complain_on_overflow */
225          bfd_elf_generic_reloc, /* special_function */
226          "R_PPC_ADDR14_BRNTAKEN",/* name */
227          FALSE,                 /* partial_inplace */
228          0,                     /* src_mask */
229          0xfffc,                /* dst_mask */
230          FALSE),                /* pcrel_offset */
231
232   /* A relative 26 bit branch; the lower two bits must be zero.  */
233   HOWTO (R_PPC_REL24,           /* type */
234          0,                     /* rightshift */
235          2,                     /* size (0 = byte, 1 = short, 2 = long) */
236          26,                    /* bitsize */
237          TRUE,                  /* pc_relative */
238          0,                     /* bitpos */
239          complain_overflow_signed, /* complain_on_overflow */
240          bfd_elf_generic_reloc, /* special_function */
241          "R_PPC_REL24",         /* name */
242          FALSE,                 /* partial_inplace */
243          0,                     /* src_mask */
244          0x3fffffc,             /* dst_mask */
245          TRUE),                 /* pcrel_offset */
246
247   /* A relative 16 bit branch; the lower two bits must be zero.  */
248   HOWTO (R_PPC_REL14,           /* type */
249          0,                     /* rightshift */
250          2,                     /* size (0 = byte, 1 = short, 2 = long) */
251          16,                    /* bitsize */
252          TRUE,                  /* pc_relative */
253          0,                     /* bitpos */
254          complain_overflow_signed, /* complain_on_overflow */
255          bfd_elf_generic_reloc, /* special_function */
256          "R_PPC_REL14",         /* name */
257          FALSE,                 /* partial_inplace */
258          0,                     /* src_mask */
259          0xfffc,                /* dst_mask */
260          TRUE),                 /* pcrel_offset */
261
262   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
263      the branch is expected to be taken.  The lower two bits must be
264      zero.  */
265   HOWTO (R_PPC_REL14_BRTAKEN,   /* type */
266          0,                     /* rightshift */
267          2,                     /* size (0 = byte, 1 = short, 2 = long) */
268          16,                    /* bitsize */
269          TRUE,                  /* pc_relative */
270          0,                     /* bitpos */
271          complain_overflow_signed, /* complain_on_overflow */
272          bfd_elf_generic_reloc, /* special_function */
273          "R_PPC_REL14_BRTAKEN", /* name */
274          FALSE,                 /* partial_inplace */
275          0,                     /* src_mask */
276          0xfffc,                /* dst_mask */
277          TRUE),                 /* pcrel_offset */
278
279   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
280      the branch is not expected to be taken.  The lower two bits must
281      be zero.  */
282   HOWTO (R_PPC_REL14_BRNTAKEN,  /* type */
283          0,                     /* rightshift */
284          2,                     /* size (0 = byte, 1 = short, 2 = long) */
285          16,                    /* bitsize */
286          TRUE,                  /* pc_relative */
287          0,                     /* bitpos */
288          complain_overflow_signed, /* complain_on_overflow */
289          bfd_elf_generic_reloc, /* special_function */
290          "R_PPC_REL14_BRNTAKEN",/* name */
291          FALSE,                 /* partial_inplace */
292          0,                     /* src_mask */
293          0xfffc,                /* dst_mask */
294          TRUE),                 /* pcrel_offset */
295
296   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
297      symbol.  */
298   HOWTO (R_PPC_GOT16,           /* type */
299          0,                     /* rightshift */
300          1,                     /* size (0 = byte, 1 = short, 2 = long) */
301          16,                    /* bitsize */
302          FALSE,                 /* pc_relative */
303          0,                     /* bitpos */
304          complain_overflow_signed, /* complain_on_overflow */
305          bfd_elf_generic_reloc, /* special_function */
306          "R_PPC_GOT16",         /* name */
307          FALSE,                 /* partial_inplace */
308          0,                     /* src_mask */
309          0xffff,                /* dst_mask */
310          FALSE),                /* pcrel_offset */
311
312   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
313      the symbol.  */
314   HOWTO (R_PPC_GOT16_LO,        /* type */
315          0,                     /* rightshift */
316          1,                     /* size (0 = byte, 1 = short, 2 = long) */
317          16,                    /* bitsize */
318          FALSE,                 /* pc_relative */
319          0,                     /* bitpos */
320          complain_overflow_dont, /* complain_on_overflow */
321          bfd_elf_generic_reloc, /* special_function */
322          "R_PPC_GOT16_LO",      /* name */
323          FALSE,                 /* partial_inplace */
324          0,                     /* src_mask */
325          0xffff,                /* dst_mask */
326          FALSE),                /* pcrel_offset */
327
328   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
329      the symbol.  */
330   HOWTO (R_PPC_GOT16_HI,        /* type */
331          16,                    /* rightshift */
332          1,                     /* size (0 = byte, 1 = short, 2 = long) */
333          16,                    /* bitsize */
334          FALSE,                 /* pc_relative */
335          0,                     /* bitpos */
336          complain_overflow_bitfield, /* complain_on_overflow */
337          bfd_elf_generic_reloc, /* special_function */
338          "R_PPC_GOT16_HI",      /* name */
339          FALSE,                 /* partial_inplace */
340          0,                     /* src_mask */
341          0xffff,                /* dst_mask */
342          FALSE),                 /* pcrel_offset */
343
344   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
345      the symbol.  */
346   HOWTO (R_PPC_GOT16_HA,        /* type */
347          16,                    /* rightshift */
348          1,                     /* size (0 = byte, 1 = short, 2 = long) */
349          16,                    /* bitsize */
350          FALSE,                 /* pc_relative */
351          0,                     /* bitpos */
352          complain_overflow_bitfield, /* complain_on_overflow */
353          ppc_elf_addr16_ha_reloc, /* special_function */
354          "R_PPC_GOT16_HA",      /* name */
355          FALSE,                 /* partial_inplace */
356          0,                     /* src_mask */
357          0xffff,                /* dst_mask */
358          FALSE),                /* pcrel_offset */
359
360   /* Like R_PPC_REL24, but referring to the procedure linkage table
361      entry for the symbol.  */
362   HOWTO (R_PPC_PLTREL24,        /* type */
363          0,                     /* rightshift */
364          2,                     /* size (0 = byte, 1 = short, 2 = long) */
365          26,                    /* bitsize */
366          TRUE,                  /* pc_relative */
367          0,                     /* bitpos */
368          complain_overflow_signed,  /* complain_on_overflow */
369          bfd_elf_generic_reloc, /* special_function */
370          "R_PPC_PLTREL24",      /* name */
371          FALSE,                 /* partial_inplace */
372          0,                     /* src_mask */
373          0x3fffffc,             /* dst_mask */
374          TRUE),                 /* pcrel_offset */
375
376   /* This is used only by the dynamic linker.  The symbol should exist
377      both in the object being run and in some shared library.  The
378      dynamic linker copies the data addressed by the symbol from the
379      shared library into the object, because the object being
380      run has to have the data at some particular address.  */
381   HOWTO (R_PPC_COPY,            /* type */
382          0,                     /* rightshift */
383          2,                     /* size (0 = byte, 1 = short, 2 = long) */
384          32,                    /* bitsize */
385          FALSE,                 /* pc_relative */
386          0,                     /* bitpos */
387          complain_overflow_bitfield, /* complain_on_overflow */
388          bfd_elf_generic_reloc,  /* special_function */
389          "R_PPC_COPY",          /* name */
390          FALSE,                 /* partial_inplace */
391          0,                     /* src_mask */
392          0,                     /* dst_mask */
393          FALSE),                /* pcrel_offset */
394
395   /* Like R_PPC_ADDR32, but used when setting global offset table
396      entries.  */
397   HOWTO (R_PPC_GLOB_DAT,        /* type */
398          0,                     /* rightshift */
399          2,                     /* size (0 = byte, 1 = short, 2 = long) */
400          32,                    /* bitsize */
401          FALSE,                 /* pc_relative */
402          0,                     /* bitpos */
403          complain_overflow_bitfield, /* complain_on_overflow */
404          bfd_elf_generic_reloc,  /* special_function */
405          "R_PPC_GLOB_DAT",      /* name */
406          FALSE,                 /* partial_inplace */
407          0,                     /* src_mask */
408          0xffffffff,            /* dst_mask */
409          FALSE),                /* pcrel_offset */
410
411   /* Marks a procedure linkage table entry for a symbol.  */
412   HOWTO (R_PPC_JMP_SLOT,        /* type */
413          0,                     /* rightshift */
414          2,                     /* size (0 = byte, 1 = short, 2 = long) */
415          32,                    /* bitsize */
416          FALSE,                 /* pc_relative */
417          0,                     /* bitpos */
418          complain_overflow_bitfield, /* complain_on_overflow */
419          bfd_elf_generic_reloc,  /* special_function */
420          "R_PPC_JMP_SLOT",      /* name */
421          FALSE,                 /* partial_inplace */
422          0,                     /* src_mask */
423          0,                     /* dst_mask */
424          FALSE),                /* pcrel_offset */
425
426   /* Used only by the dynamic linker.  When the object is run, this
427      longword is set to the load address of the object, plus the
428      addend.  */
429   HOWTO (R_PPC_RELATIVE,        /* type */
430          0,                     /* rightshift */
431          2,                     /* size (0 = byte, 1 = short, 2 = long) */
432          32,                    /* bitsize */
433          FALSE,                 /* pc_relative */
434          0,                     /* bitpos */
435          complain_overflow_bitfield, /* complain_on_overflow */
436          bfd_elf_generic_reloc,  /* special_function */
437          "R_PPC_RELATIVE",      /* name */
438          FALSE,                 /* partial_inplace */
439          0,                     /* src_mask */
440          0xffffffff,            /* dst_mask */
441          FALSE),                /* pcrel_offset */
442
443   /* Like R_PPC_REL24, but uses the value of the symbol within the
444      object rather than the final value.  Normally used for
445      _GLOBAL_OFFSET_TABLE_.  */
446   HOWTO (R_PPC_LOCAL24PC,       /* type */
447          0,                     /* rightshift */
448          2,                     /* size (0 = byte, 1 = short, 2 = long) */
449          26,                    /* bitsize */
450          TRUE,                  /* pc_relative */
451          0,                     /* bitpos */
452          complain_overflow_signed, /* complain_on_overflow */
453          bfd_elf_generic_reloc, /* special_function */
454          "R_PPC_LOCAL24PC",     /* name */
455          FALSE,                 /* partial_inplace */
456          0,                     /* src_mask */
457          0x3fffffc,             /* dst_mask */
458          TRUE),                 /* pcrel_offset */
459
460   /* Like R_PPC_ADDR32, but may be unaligned.  */
461   HOWTO (R_PPC_UADDR32,         /* type */
462          0,                     /* rightshift */
463          2,                     /* size (0 = byte, 1 = short, 2 = long) */
464          32,                    /* bitsize */
465          FALSE,                 /* pc_relative */
466          0,                     /* bitpos */
467          complain_overflow_bitfield, /* complain_on_overflow */
468          bfd_elf_generic_reloc, /* special_function */
469          "R_PPC_UADDR32",       /* name */
470          FALSE,                 /* partial_inplace */
471          0,                     /* src_mask */
472          0xffffffff,            /* dst_mask */
473          FALSE),                /* pcrel_offset */
474
475   /* Like R_PPC_ADDR16, but may be unaligned.  */
476   HOWTO (R_PPC_UADDR16,         /* type */
477          0,                     /* rightshift */
478          1,                     /* size (0 = byte, 1 = short, 2 = long) */
479          16,                    /* bitsize */
480          FALSE,                 /* pc_relative */
481          0,                     /* bitpos */
482          complain_overflow_bitfield, /* complain_on_overflow */
483          bfd_elf_generic_reloc, /* special_function */
484          "R_PPC_UADDR16",       /* name */
485          FALSE,                 /* partial_inplace */
486          0,                     /* src_mask */
487          0xffff,                /* dst_mask */
488          FALSE),                /* pcrel_offset */
489
490   /* 32-bit PC relative */
491   HOWTO (R_PPC_REL32,           /* type */
492          0,                     /* rightshift */
493          2,                     /* size (0 = byte, 1 = short, 2 = long) */
494          32,                    /* bitsize */
495          TRUE,                  /* pc_relative */
496          0,                     /* bitpos */
497          complain_overflow_bitfield, /* complain_on_overflow */
498          bfd_elf_generic_reloc, /* special_function */
499          "R_PPC_REL32",         /* name */
500          FALSE,                 /* partial_inplace */
501          0,                     /* src_mask */
502          0xffffffff,            /* dst_mask */
503          TRUE),                 /* pcrel_offset */
504
505   /* 32-bit relocation to the symbol's procedure linkage table.
506      FIXME: not supported.  */
507   HOWTO (R_PPC_PLT32,           /* type */
508          0,                     /* rightshift */
509          2,                     /* size (0 = byte, 1 = short, 2 = long) */
510          32,                    /* bitsize */
511          FALSE,                 /* pc_relative */
512          0,                     /* bitpos */
513          complain_overflow_bitfield, /* complain_on_overflow */
514          bfd_elf_generic_reloc, /* special_function */
515          "R_PPC_PLT32",         /* name */
516          FALSE,                 /* partial_inplace */
517          0,                     /* src_mask */
518          0,                     /* dst_mask */
519          FALSE),                /* pcrel_offset */
520
521   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
522      FIXME: not supported.  */
523   HOWTO (R_PPC_PLTREL32,        /* type */
524          0,                     /* rightshift */
525          2,                     /* size (0 = byte, 1 = short, 2 = long) */
526          32,                    /* bitsize */
527          TRUE,                  /* pc_relative */
528          0,                     /* bitpos */
529          complain_overflow_bitfield, /* complain_on_overflow */
530          bfd_elf_generic_reloc, /* special_function */
531          "R_PPC_PLTREL32",      /* name */
532          FALSE,                 /* partial_inplace */
533          0,                     /* src_mask */
534          0,                     /* dst_mask */
535          TRUE),                 /* pcrel_offset */
536
537   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
538      the symbol.  */
539   HOWTO (R_PPC_PLT16_LO,        /* type */
540          0,                     /* rightshift */
541          1,                     /* size (0 = byte, 1 = short, 2 = long) */
542          16,                    /* bitsize */
543          FALSE,                 /* pc_relative */
544          0,                     /* bitpos */
545          complain_overflow_dont, /* complain_on_overflow */
546          bfd_elf_generic_reloc, /* special_function */
547          "R_PPC_PLT16_LO",      /* name */
548          FALSE,                 /* partial_inplace */
549          0,                     /* src_mask */
550          0xffff,                /* dst_mask */
551          FALSE),                /* pcrel_offset */
552
553   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
554      the symbol.  */
555   HOWTO (R_PPC_PLT16_HI,        /* type */
556          16,                    /* rightshift */
557          1,                     /* size (0 = byte, 1 = short, 2 = long) */
558          16,                    /* bitsize */
559          FALSE,                 /* pc_relative */
560          0,                     /* bitpos */
561          complain_overflow_bitfield, /* complain_on_overflow */
562          bfd_elf_generic_reloc, /* special_function */
563          "R_PPC_PLT16_HI",      /* name */
564          FALSE,                 /* partial_inplace */
565          0,                     /* src_mask */
566          0xffff,                /* dst_mask */
567          FALSE),                 /* pcrel_offset */
568
569   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
570      the symbol.  */
571   HOWTO (R_PPC_PLT16_HA,        /* type */
572          16,                    /* rightshift */
573          1,                     /* size (0 = byte, 1 = short, 2 = long) */
574          16,                    /* bitsize */
575          FALSE,                 /* pc_relative */
576          0,                     /* bitpos */
577          complain_overflow_bitfield, /* complain_on_overflow */
578          ppc_elf_addr16_ha_reloc, /* special_function */
579          "R_PPC_PLT16_HA",      /* name */
580          FALSE,                 /* partial_inplace */
581          0,                     /* src_mask */
582          0xffff,                /* dst_mask */
583          FALSE),                /* pcrel_offset */
584
585   /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
586      small data items.  */
587   HOWTO (R_PPC_SDAREL16,        /* type */
588          0,                     /* rightshift */
589          1,                     /* size (0 = byte, 1 = short, 2 = long) */
590          16,                    /* bitsize */
591          FALSE,                 /* pc_relative */
592          0,                     /* bitpos */
593          complain_overflow_signed, /* complain_on_overflow */
594          bfd_elf_generic_reloc, /* special_function */
595          "R_PPC_SDAREL16",      /* name */
596          FALSE,                 /* partial_inplace */
597          0,                     /* src_mask */
598          0xffff,                /* dst_mask */
599          FALSE),                /* pcrel_offset */
600
601   /* 16-bit section relative relocation.  */
602   HOWTO (R_PPC_SECTOFF,         /* type */
603          0,                     /* rightshift */
604          1,                     /* size (0 = byte, 1 = short, 2 = long) */
605          16,                    /* bitsize */
606          FALSE,                 /* pc_relative */
607          0,                     /* bitpos */
608          complain_overflow_bitfield, /* complain_on_overflow */
609          bfd_elf_generic_reloc, /* special_function */
610          "R_PPC_SECTOFF",       /* name */
611          FALSE,                 /* partial_inplace */
612          0,                     /* src_mask */
613          0xffff,                /* dst_mask */
614          FALSE),                /* pcrel_offset */
615
616   /* 16-bit lower half section relative relocation.  */
617   HOWTO (R_PPC_SECTOFF_LO,        /* type */
618          0,                     /* rightshift */
619          1,                     /* size (0 = byte, 1 = short, 2 = long) */
620          16,                    /* bitsize */
621          FALSE,                 /* pc_relative */
622          0,                     /* bitpos */
623          complain_overflow_dont, /* complain_on_overflow */
624          bfd_elf_generic_reloc, /* special_function */
625          "R_PPC_SECTOFF_LO",    /* name */
626          FALSE,                 /* partial_inplace */
627          0,                     /* src_mask */
628          0xffff,                /* dst_mask */
629          FALSE),                /* pcrel_offset */
630
631   /* 16-bit upper half section relative relocation.  */
632   HOWTO (R_PPC_SECTOFF_HI,      /* type */
633          16,                    /* rightshift */
634          1,                     /* size (0 = byte, 1 = short, 2 = long) */
635          16,                    /* bitsize */
636          FALSE,                 /* pc_relative */
637          0,                     /* bitpos */
638          complain_overflow_bitfield, /* complain_on_overflow */
639          bfd_elf_generic_reloc, /* special_function */
640          "R_PPC_SECTOFF_HI",    /* name */
641          FALSE,                 /* partial_inplace */
642          0,                     /* src_mask */
643          0xffff,                /* dst_mask */
644          FALSE),                 /* pcrel_offset */
645
646   /* 16-bit upper half adjusted section relative relocation.  */
647   HOWTO (R_PPC_SECTOFF_HA,      /* type */
648          16,                    /* 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          ppc_elf_addr16_ha_reloc, /* special_function */
655          "R_PPC_SECTOFF_HA",    /* name */
656          FALSE,                 /* partial_inplace */
657          0,                     /* src_mask */
658          0xffff,                /* dst_mask */
659          FALSE),                /* pcrel_offset */
660
661   /* Marker reloc for TLS.  */
662   HOWTO (R_PPC_TLS,
663          0,                     /* rightshift */
664          2,                     /* size (0 = byte, 1 = short, 2 = long) */
665          32,                    /* bitsize */
666          FALSE,                 /* pc_relative */
667          0,                     /* bitpos */
668          complain_overflow_dont, /* complain_on_overflow */
669          bfd_elf_generic_reloc, /* special_function */
670          "R_PPC_TLS",           /* name */
671          FALSE,                 /* partial_inplace */
672          0,                     /* src_mask */
673          0,                     /* dst_mask */
674          FALSE),                /* pcrel_offset */
675
676   /* Computes the load module index of the load module that contains the
677      definition of its TLS sym.  */
678   HOWTO (R_PPC_DTPMOD32,
679          0,                     /* rightshift */
680          2,                     /* size (0 = byte, 1 = short, 2 = long) */
681          32,                    /* bitsize */
682          FALSE,                 /* pc_relative */
683          0,                     /* bitpos */
684          complain_overflow_dont, /* complain_on_overflow */
685          ppc_elf_unhandled_reloc, /* special_function */
686          "R_PPC_DTPMOD32",      /* name */
687          FALSE,                 /* partial_inplace */
688          0,                     /* src_mask */
689          0xffffffff,            /* dst_mask */
690          FALSE),                /* pcrel_offset */
691
692   /* Computes a dtv-relative displacement, the difference between the value
693      of sym+add and the base address of the thread-local storage block that
694      contains the definition of sym, minus 0x8000.  */
695   HOWTO (R_PPC_DTPREL32,
696          0,                     /* rightshift */
697          2,                     /* size (0 = byte, 1 = short, 2 = long) */
698          32,                    /* bitsize */
699          FALSE,                 /* pc_relative */
700          0,                     /* bitpos */
701          complain_overflow_dont, /* complain_on_overflow */
702          ppc_elf_unhandled_reloc, /* special_function */
703          "R_PPC_DTPREL32",      /* name */
704          FALSE,                 /* partial_inplace */
705          0,                     /* src_mask */
706          0xffffffff,            /* dst_mask */
707          FALSE),                /* pcrel_offset */
708
709   /* A 16 bit dtprel reloc.  */
710   HOWTO (R_PPC_DTPREL16,
711          0,                     /* rightshift */
712          1,                     /* size (0 = byte, 1 = short, 2 = long) */
713          16,                    /* bitsize */
714          FALSE,                 /* pc_relative */
715          0,                     /* bitpos */
716          complain_overflow_signed, /* complain_on_overflow */
717          ppc_elf_unhandled_reloc, /* special_function */
718          "R_PPC_DTPREL16",      /* name */
719          FALSE,                 /* partial_inplace */
720          0,                     /* src_mask */
721          0xffff,                /* dst_mask */
722          FALSE),                /* pcrel_offset */
723
724   /* Like DTPREL16, but no overflow.  */
725   HOWTO (R_PPC_DTPREL16_LO,
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_dont, /* complain_on_overflow */
732          ppc_elf_unhandled_reloc, /* special_function */
733          "R_PPC_DTPREL16_LO",   /* name */
734          FALSE,                 /* partial_inplace */
735          0,                     /* src_mask */
736          0xffff,                /* dst_mask */
737          FALSE),                /* pcrel_offset */
738
739   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
740   HOWTO (R_PPC_DTPREL16_HI,
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_dont, /* complain_on_overflow */
747          ppc_elf_unhandled_reloc, /* special_function */
748          "R_PPC_DTPREL16_HI",   /* name */
749          FALSE,                 /* partial_inplace */
750          0,                     /* src_mask */
751          0xffff,                /* dst_mask */
752          FALSE),                /* pcrel_offset */
753
754   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
755   HOWTO (R_PPC_DTPREL16_HA,
756          16,                    /* rightshift */
757          1,                     /* size (0 = byte, 1 = short, 2 = long) */
758          16,                    /* bitsize */
759          FALSE,                 /* pc_relative */
760          0,                     /* bitpos */
761          complain_overflow_dont, /* complain_on_overflow */
762          ppc_elf_unhandled_reloc, /* special_function */
763          "R_PPC_DTPREL16_HA",   /* name */
764          FALSE,                 /* partial_inplace */
765          0,                     /* src_mask */
766          0xffff,                /* dst_mask */
767          FALSE),                /* pcrel_offset */
768
769   /* Computes a tp-relative displacement, the difference between the value of
770      sym+add and the value of the thread pointer (r13).  */
771   HOWTO (R_PPC_TPREL32,
772          0,                     /* rightshift */
773          2,                     /* size (0 = byte, 1 = short, 2 = long) */
774          32,                    /* bitsize */
775          FALSE,                 /* pc_relative */
776          0,                     /* bitpos */
777          complain_overflow_dont, /* complain_on_overflow */
778          ppc_elf_unhandled_reloc, /* special_function */
779          "R_PPC_TPREL32",       /* name */
780          FALSE,                 /* partial_inplace */
781          0,                     /* src_mask */
782          0xffffffff,            /* dst_mask */
783          FALSE),                /* pcrel_offset */
784
785   /* A 16 bit tprel reloc.  */
786   HOWTO (R_PPC_TPREL16,
787          0,                     /* rightshift */
788          1,                     /* size (0 = byte, 1 = short, 2 = long) */
789          16,                    /* bitsize */
790          FALSE,                 /* pc_relative */
791          0,                     /* bitpos */
792          complain_overflow_signed, /* complain_on_overflow */
793          ppc_elf_unhandled_reloc, /* special_function */
794          "R_PPC_TPREL16",       /* name */
795          FALSE,                 /* partial_inplace */
796          0,                     /* src_mask */
797          0xffff,                /* dst_mask */
798          FALSE),                /* pcrel_offset */
799
800   /* Like TPREL16, but no overflow.  */
801   HOWTO (R_PPC_TPREL16_LO,
802          0,                     /* rightshift */
803          1,                     /* size (0 = byte, 1 = short, 2 = long) */
804          16,                    /* bitsize */
805          FALSE,                 /* pc_relative */
806          0,                     /* bitpos */
807          complain_overflow_dont, /* complain_on_overflow */
808          ppc_elf_unhandled_reloc, /* special_function */
809          "R_PPC_TPREL16_LO",    /* name */
810          FALSE,                 /* partial_inplace */
811          0,                     /* src_mask */
812          0xffff,                /* dst_mask */
813          FALSE),                /* pcrel_offset */
814
815   /* Like TPREL16_LO, but next higher group of 16 bits.  */
816   HOWTO (R_PPC_TPREL16_HI,
817          16,                    /* rightshift */
818          1,                     /* size (0 = byte, 1 = short, 2 = long) */
819          16,                    /* bitsize */
820          FALSE,                 /* pc_relative */
821          0,                     /* bitpos */
822          complain_overflow_dont, /* complain_on_overflow */
823          ppc_elf_unhandled_reloc, /* special_function */
824          "R_PPC_TPREL16_HI",    /* name */
825          FALSE,                 /* partial_inplace */
826          0,                     /* src_mask */
827          0xffff,                /* dst_mask */
828          FALSE),                /* pcrel_offset */
829
830   /* Like TPREL16_HI, but adjust for low 16 bits.  */
831   HOWTO (R_PPC_TPREL16_HA,
832          16,                    /* rightshift */
833          1,                     /* size (0 = byte, 1 = short, 2 = long) */
834          16,                    /* bitsize */
835          FALSE,                 /* pc_relative */
836          0,                     /* bitpos */
837          complain_overflow_dont, /* complain_on_overflow */
838          ppc_elf_unhandled_reloc, /* special_function */
839          "R_PPC_TPREL16_HA",    /* name */
840          FALSE,                 /* partial_inplace */
841          0,                     /* src_mask */
842          0xffff,                /* dst_mask */
843          FALSE),                /* pcrel_offset */
844
845   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
846      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
847      to the first entry.  */
848   HOWTO (R_PPC_GOT_TLSGD16,
849          0,                     /* rightshift */
850          1,                     /* size (0 = byte, 1 = short, 2 = long) */
851          16,                    /* bitsize */
852          FALSE,                 /* pc_relative */
853          0,                     /* bitpos */
854          complain_overflow_signed, /* complain_on_overflow */
855          ppc_elf_unhandled_reloc, /* special_function */
856          "R_PPC_GOT_TLSGD16",   /* name */
857          FALSE,                 /* partial_inplace */
858          0,                     /* src_mask */
859          0xffff,                /* dst_mask */
860          FALSE),                /* pcrel_offset */
861
862   /* Like GOT_TLSGD16, but no overflow.  */
863   HOWTO (R_PPC_GOT_TLSGD16_LO,
864          0,                     /* rightshift */
865          1,                     /* size (0 = byte, 1 = short, 2 = long) */
866          16,                    /* bitsize */
867          FALSE,                 /* pc_relative */
868          0,                     /* bitpos */
869          complain_overflow_dont, /* complain_on_overflow */
870          ppc_elf_unhandled_reloc, /* special_function */
871          "R_PPC_GOT_TLSGD16_LO", /* name */
872          FALSE,                 /* partial_inplace */
873          0,                     /* src_mask */
874          0xffff,                /* dst_mask */
875          FALSE),                /* pcrel_offset */
876
877   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
878   HOWTO (R_PPC_GOT_TLSGD16_HI,
879          16,                    /* rightshift */
880          1,                     /* size (0 = byte, 1 = short, 2 = long) */
881          16,                    /* bitsize */
882          FALSE,                 /* pc_relative */
883          0,                     /* bitpos */
884          complain_overflow_dont, /* complain_on_overflow */
885          ppc_elf_unhandled_reloc, /* special_function */
886          "R_PPC_GOT_TLSGD16_HI", /* name */
887          FALSE,                 /* partial_inplace */
888          0,                     /* src_mask */
889          0xffff,                /* dst_mask */
890          FALSE),                /* pcrel_offset */
891
892   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
893   HOWTO (R_PPC_GOT_TLSGD16_HA,
894          16,                    /* rightshift */
895          1,                     /* size (0 = byte, 1 = short, 2 = long) */
896          16,                    /* bitsize */
897          FALSE,                 /* pc_relative */
898          0,                     /* bitpos */
899          complain_overflow_dont, /* complain_on_overflow */
900          ppc_elf_unhandled_reloc, /* special_function */
901          "R_PPC_GOT_TLSGD16_HA", /* name */
902          FALSE,                 /* partial_inplace */
903          0,                     /* src_mask */
904          0xffff,                /* dst_mask */
905          FALSE),                /* pcrel_offset */
906
907   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
908      with values (sym+add)@dtpmod and zero, and computes the offset to the
909      first entry.  */
910   HOWTO (R_PPC_GOT_TLSLD16,
911          0,                     /* rightshift */
912          1,                     /* size (0 = byte, 1 = short, 2 = long) */
913          16,                    /* bitsize */
914          FALSE,                 /* pc_relative */
915          0,                     /* bitpos */
916          complain_overflow_signed, /* complain_on_overflow */
917          ppc_elf_unhandled_reloc, /* special_function */
918          "R_PPC_GOT_TLSLD16",   /* name */
919          FALSE,                 /* partial_inplace */
920          0,                     /* src_mask */
921          0xffff,                /* dst_mask */
922          FALSE),                /* pcrel_offset */
923
924   /* Like GOT_TLSLD16, but no overflow.  */
925   HOWTO (R_PPC_GOT_TLSLD16_LO,
926          0,                     /* rightshift */
927          1,                     /* size (0 = byte, 1 = short, 2 = long) */
928          16,                    /* bitsize */
929          FALSE,                 /* pc_relative */
930          0,                     /* bitpos */
931          complain_overflow_dont, /* complain_on_overflow */
932          ppc_elf_unhandled_reloc, /* special_function */
933          "R_PPC_GOT_TLSLD16_LO", /* name */
934          FALSE,                 /* partial_inplace */
935          0,                     /* src_mask */
936          0xffff,                /* dst_mask */
937          FALSE),                /* pcrel_offset */
938
939   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
940   HOWTO (R_PPC_GOT_TLSLD16_HI,
941          16,                    /* rightshift */
942          1,                     /* size (0 = byte, 1 = short, 2 = long) */
943          16,                    /* bitsize */
944          FALSE,                 /* pc_relative */
945          0,                     /* bitpos */
946          complain_overflow_dont, /* complain_on_overflow */
947          ppc_elf_unhandled_reloc, /* special_function */
948          "R_PPC_GOT_TLSLD16_HI", /* name */
949          FALSE,                 /* partial_inplace */
950          0,                     /* src_mask */
951          0xffff,                /* dst_mask */
952          FALSE),                /* pcrel_offset */
953
954   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
955   HOWTO (R_PPC_GOT_TLSLD16_HA,
956          16,                    /* rightshift */
957          1,                     /* size (0 = byte, 1 = short, 2 = long) */
958          16,                    /* bitsize */
959          FALSE,                 /* pc_relative */
960          0,                     /* bitpos */
961          complain_overflow_dont, /* complain_on_overflow */
962          ppc_elf_unhandled_reloc, /* special_function */
963          "R_PPC_GOT_TLSLD16_HA", /* name */
964          FALSE,                 /* partial_inplace */
965          0,                     /* src_mask */
966          0xffff,                /* dst_mask */
967          FALSE),                /* pcrel_offset */
968
969   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
970      the offset to the entry.  */
971   HOWTO (R_PPC_GOT_DTPREL16,
972          0,                     /* rightshift */
973          1,                     /* size (0 = byte, 1 = short, 2 = long) */
974          16,                    /* bitsize */
975          FALSE,                 /* pc_relative */
976          0,                     /* bitpos */
977          complain_overflow_signed, /* complain_on_overflow */
978          ppc_elf_unhandled_reloc, /* special_function */
979          "R_PPC_GOT_DTPREL16",  /* name */
980          FALSE,                 /* partial_inplace */
981          0,                     /* src_mask */
982          0xffff,                /* dst_mask */
983          FALSE),                /* pcrel_offset */
984
985   /* Like GOT_DTPREL16, but no overflow.  */
986   HOWTO (R_PPC_GOT_DTPREL16_LO,
987          0,                     /* rightshift */
988          1,                     /* size (0 = byte, 1 = short, 2 = long) */
989          16,                    /* bitsize */
990          FALSE,                 /* pc_relative */
991          0,                     /* bitpos */
992          complain_overflow_dont, /* complain_on_overflow */
993          ppc_elf_unhandled_reloc, /* special_function */
994          "R_PPC_GOT_DTPREL16_LO", /* name */
995          FALSE,                 /* partial_inplace */
996          0,                     /* src_mask */
997          0xffff,                /* dst_mask */
998          FALSE),                /* pcrel_offset */
999
1000   /* Like GOT_DTPREL16_LO, but next higher group of 16 bits.  */
1001   HOWTO (R_PPC_GOT_DTPREL16_HI,
1002          16,                    /* rightshift */
1003          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1004          16,                    /* bitsize */
1005          FALSE,                 /* pc_relative */
1006          0,                     /* bitpos */
1007          complain_overflow_dont, /* complain_on_overflow */
1008          ppc_elf_unhandled_reloc, /* special_function */
1009          "R_PPC_GOT_DTPREL16_HI", /* name */
1010          FALSE,                 /* partial_inplace */
1011          0,                     /* src_mask */
1012          0xffff,                /* dst_mask */
1013          FALSE),                /* pcrel_offset */
1014
1015   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1016   HOWTO (R_PPC_GOT_DTPREL16_HA,
1017          16,                    /* rightshift */
1018          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1019          16,                    /* bitsize */
1020          FALSE,                 /* pc_relative */
1021          0,                     /* bitpos */
1022          complain_overflow_dont, /* complain_on_overflow */
1023          ppc_elf_unhandled_reloc, /* special_function */
1024          "R_PPC_GOT_DTPREL16_HA", /* name */
1025          FALSE,                 /* partial_inplace */
1026          0,                     /* src_mask */
1027          0xffff,                /* dst_mask */
1028          FALSE),                /* pcrel_offset */
1029
1030   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1031      offset to the entry.  */
1032   HOWTO (R_PPC_GOT_TPREL16,
1033          0,                     /* rightshift */
1034          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1035          16,                    /* bitsize */
1036          FALSE,                 /* pc_relative */
1037          0,                     /* bitpos */
1038          complain_overflow_signed, /* complain_on_overflow */
1039          ppc_elf_unhandled_reloc, /* special_function */
1040          "R_PPC_GOT_TPREL16",   /* name */
1041          FALSE,                 /* partial_inplace */
1042          0,                     /* src_mask */
1043          0xffff,                /* dst_mask */
1044          FALSE),                /* pcrel_offset */
1045
1046   /* Like GOT_TPREL16, but no overflow.  */
1047   HOWTO (R_PPC_GOT_TPREL16_LO,
1048          0,                     /* rightshift */
1049          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1050          16,                    /* bitsize */
1051          FALSE,                 /* pc_relative */
1052          0,                     /* bitpos */
1053          complain_overflow_dont, /* complain_on_overflow */
1054          ppc_elf_unhandled_reloc, /* special_function */
1055          "R_PPC_GOT_TPREL16_LO", /* name */
1056          FALSE,                 /* partial_inplace */
1057          0,                     /* src_mask */
1058          0xffff,                /* dst_mask */
1059          FALSE),                /* pcrel_offset */
1060
1061   /* Like GOT_TPREL16_LO, but next higher group of 16 bits.  */
1062   HOWTO (R_PPC_GOT_TPREL16_HI,
1063          16,                    /* rightshift */
1064          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1065          16,                    /* bitsize */
1066          FALSE,                 /* pc_relative */
1067          0,                     /* bitpos */
1068          complain_overflow_dont, /* complain_on_overflow */
1069          ppc_elf_unhandled_reloc, /* special_function */
1070          "R_PPC_GOT_TPREL16_HI", /* name */
1071          FALSE,                 /* partial_inplace */
1072          0,                     /* src_mask */
1073          0xffff,                /* dst_mask */
1074          FALSE),                /* pcrel_offset */
1075
1076   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1077   HOWTO (R_PPC_GOT_TPREL16_HA,
1078          16,                    /* rightshift */
1079          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1080          16,                    /* bitsize */
1081          FALSE,                 /* pc_relative */
1082          0,                     /* bitpos */
1083          complain_overflow_dont, /* complain_on_overflow */
1084          ppc_elf_unhandled_reloc, /* special_function */
1085          "R_PPC_GOT_TPREL16_HA", /* name */
1086          FALSE,                 /* partial_inplace */
1087          0,                     /* src_mask */
1088          0xffff,                /* dst_mask */
1089          FALSE),                /* pcrel_offset */
1090
1091   /* The remaining relocs are from the Embedded ELF ABI, and are not
1092      in the SVR4 ELF ABI.  */
1093
1094   /* 32 bit value resulting from the addend minus the symbol.  */
1095   HOWTO (R_PPC_EMB_NADDR32,     /* type */
1096          0,                     /* rightshift */
1097          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1098          32,                    /* bitsize */
1099          FALSE,                 /* pc_relative */
1100          0,                     /* bitpos */
1101          complain_overflow_bitfield, /* complain_on_overflow */
1102          bfd_elf_generic_reloc, /* special_function */
1103          "R_PPC_EMB_NADDR32",   /* name */
1104          FALSE,                 /* partial_inplace */
1105          0,                     /* src_mask */
1106          0xffffffff,            /* dst_mask */
1107          FALSE),                /* pcrel_offset */
1108
1109   /* 16 bit value resulting from the addend minus the symbol.  */
1110   HOWTO (R_PPC_EMB_NADDR16,     /* type */
1111          0,                     /* rightshift */
1112          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1113          16,                    /* bitsize */
1114          FALSE,                 /* pc_relative */
1115          0,                     /* bitpos */
1116          complain_overflow_bitfield, /* complain_on_overflow */
1117          bfd_elf_generic_reloc, /* special_function */
1118          "R_PPC_EMB_NADDR16",   /* name */
1119          FALSE,                 /* partial_inplace */
1120          0,                     /* src_mask */
1121          0xffff,                /* dst_mask */
1122          FALSE),                /* pcrel_offset */
1123
1124   /* 16 bit value resulting from the addend minus the symbol.  */
1125   HOWTO (R_PPC_EMB_NADDR16_LO,  /* type */
1126          0,                     /* rightshift */
1127          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1128          16,                    /* bitsize */
1129          FALSE,                 /* pc_relative */
1130          0,                     /* bitpos */
1131          complain_overflow_dont,/* complain_on_overflow */
1132          bfd_elf_generic_reloc, /* special_function */
1133          "R_PPC_EMB_ADDR16_LO", /* name */
1134          FALSE,                 /* partial_inplace */
1135          0,                     /* src_mask */
1136          0xffff,                /* dst_mask */
1137          FALSE),                /* pcrel_offset */
1138
1139   /* The high order 16 bits of the addend minus the symbol.  */
1140   HOWTO (R_PPC_EMB_NADDR16_HI,  /* type */
1141          16,                    /* rightshift */
1142          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1143          16,                    /* bitsize */
1144          FALSE,                 /* pc_relative */
1145          0,                     /* bitpos */
1146          complain_overflow_dont, /* complain_on_overflow */
1147          bfd_elf_generic_reloc, /* special_function */
1148          "R_PPC_EMB_NADDR16_HI", /* name */
1149          FALSE,                 /* partial_inplace */
1150          0,                     /* src_mask */
1151          0xffff,                /* dst_mask */
1152          FALSE),                /* pcrel_offset */
1153
1154   /* The high order 16 bits of the result of the addend minus the address,
1155      plus 1 if the contents of the low 16 bits, treated as a signed number,
1156      is negative.  */
1157   HOWTO (R_PPC_EMB_NADDR16_HA,  /* type */
1158          16,                    /* rightshift */
1159          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1160          16,                    /* bitsize */
1161          FALSE,                 /* pc_relative */
1162          0,                     /* bitpos */
1163          complain_overflow_dont, /* complain_on_overflow */
1164          ppc_elf_addr16_ha_reloc, /* special_function */
1165          "R_PPC_EMB_NADDR16_HA", /* name */
1166          FALSE,                 /* partial_inplace */
1167          0,                     /* src_mask */
1168          0xffff,                /* dst_mask */
1169          FALSE),                /* pcrel_offset */
1170
1171   /* 16 bit value resulting from allocating a 4 byte word to hold an
1172      address in the .sdata section, and returning the offset from
1173      _SDA_BASE_ for that relocation.  */
1174   HOWTO (R_PPC_EMB_SDAI16,      /* type */
1175          0,                     /* rightshift */
1176          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1177          16,                    /* bitsize */
1178          FALSE,                 /* pc_relative */
1179          0,                     /* bitpos */
1180          complain_overflow_bitfield, /* complain_on_overflow */
1181          bfd_elf_generic_reloc, /* special_function */
1182          "R_PPC_EMB_SDAI16",    /* name */
1183          FALSE,                 /* partial_inplace */
1184          0,                     /* src_mask */
1185          0xffff,                /* dst_mask */
1186          FALSE),                /* pcrel_offset */
1187
1188   /* 16 bit value resulting from allocating a 4 byte word to hold an
1189      address in the .sdata2 section, and returning the offset from
1190      _SDA2_BASE_ for that relocation.  */
1191   HOWTO (R_PPC_EMB_SDA2I16,     /* type */
1192          0,                     /* rightshift */
1193          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1194          16,                    /* bitsize */
1195          FALSE,                 /* pc_relative */
1196          0,                     /* bitpos */
1197          complain_overflow_bitfield, /* complain_on_overflow */
1198          bfd_elf_generic_reloc, /* special_function */
1199          "R_PPC_EMB_SDA2I16",   /* name */
1200          FALSE,                 /* partial_inplace */
1201          0,                     /* src_mask */
1202          0xffff,                /* dst_mask */
1203          FALSE),                /* pcrel_offset */
1204
1205   /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1206      small data items.   */
1207   HOWTO (R_PPC_EMB_SDA2REL,     /* type */
1208          0,                     /* rightshift */
1209          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1210          16,                    /* bitsize */
1211          FALSE,                 /* pc_relative */
1212          0,                     /* bitpos */
1213          complain_overflow_signed, /* complain_on_overflow */
1214          bfd_elf_generic_reloc, /* special_function */
1215          "R_PPC_EMB_SDA2REL",   /* name */
1216          FALSE,                 /* partial_inplace */
1217          0,                     /* src_mask */
1218          0xffff,                /* dst_mask */
1219          FALSE),                /* pcrel_offset */
1220
1221   /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1222      signed offset from the appropriate base, and filling in the register
1223      field with the appropriate register (0, 2, or 13).  */
1224   HOWTO (R_PPC_EMB_SDA21,       /* type */
1225          0,                     /* rightshift */
1226          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1227          16,                    /* bitsize */
1228          FALSE,                 /* pc_relative */
1229          0,                     /* bitpos */
1230          complain_overflow_signed, /* complain_on_overflow */
1231          bfd_elf_generic_reloc, /* special_function */
1232          "R_PPC_EMB_SDA21",     /* name */
1233          FALSE,                 /* partial_inplace */
1234          0,                     /* src_mask */
1235          0xffff,                /* dst_mask */
1236          FALSE),                /* pcrel_offset */
1237
1238   /* Relocation not handled: R_PPC_EMB_MRKREF */
1239   /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1240   /* Relocation not handled: R_PPC_EMB_RELST_LO */
1241   /* Relocation not handled: R_PPC_EMB_RELST_HI */
1242   /* Relocation not handled: R_PPC_EMB_RELST_HA */
1243   /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1244
1245   /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1246      in the 16 bit signed offset from the appropriate base, and filling in the
1247      register field with the appropriate register (0, 2, or 13).  */
1248   HOWTO (R_PPC_EMB_RELSDA,      /* type */
1249          0,                     /* rightshift */
1250          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1251          16,                    /* bitsize */
1252          TRUE,                  /* pc_relative */
1253          0,                     /* bitpos */
1254          complain_overflow_signed, /* complain_on_overflow */
1255          bfd_elf_generic_reloc, /* special_function */
1256          "R_PPC_EMB_RELSDA",    /* name */
1257          FALSE,                 /* partial_inplace */
1258          0,                     /* src_mask */
1259          0xffff,                /* dst_mask */
1260          FALSE),                /* pcrel_offset */
1261
1262   /* GNU extension to record C++ vtable hierarchy.  */
1263   HOWTO (R_PPC_GNU_VTINHERIT,   /* type */
1264          0,                     /* rightshift */
1265          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1266          0,                     /* bitsize */
1267          FALSE,                 /* pc_relative */
1268          0,                     /* bitpos */
1269          complain_overflow_dont, /* complain_on_overflow */
1270          NULL,                  /* special_function */
1271          "R_PPC_GNU_VTINHERIT", /* name */
1272          FALSE,                 /* partial_inplace */
1273          0,                     /* src_mask */
1274          0,                     /* dst_mask */
1275          FALSE),                /* pcrel_offset */
1276
1277   /* GNU extension to record C++ vtable member usage.  */
1278   HOWTO (R_PPC_GNU_VTENTRY,     /* type */
1279          0,                     /* rightshift */
1280          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1281          0,                     /* bitsize */
1282          FALSE,                 /* pc_relative */
1283          0,                     /* bitpos */
1284          complain_overflow_dont, /* complain_on_overflow */
1285          NULL,                  /* special_function */
1286          "R_PPC_GNU_VTENTRY",   /* name */
1287          FALSE,                 /* partial_inplace */
1288          0,                     /* src_mask */
1289          0,                     /* dst_mask */
1290          FALSE),                /* pcrel_offset */
1291
1292   /* Phony reloc to handle AIX style TOC entries.  */
1293   HOWTO (R_PPC_TOC16,           /* type */
1294          0,                     /* rightshift */
1295          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1296          16,                    /* bitsize */
1297          FALSE,                 /* pc_relative */
1298          0,                     /* bitpos */
1299          complain_overflow_signed, /* complain_on_overflow */
1300          bfd_elf_generic_reloc, /* special_function */
1301          "R_PPC_TOC16",         /* name */
1302          FALSE,                 /* partial_inplace */
1303          0,                     /* src_mask */
1304          0xffff,                /* dst_mask */
1305          FALSE),                /* pcrel_offset */
1306 };
1307 \f
1308 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
1309
1310 static void
1311 ppc_elf_howto_init (void)
1312 {
1313   unsigned int i, type;
1314
1315   for (i = 0;
1316        i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1317        i++)
1318     {
1319       type = ppc_elf_howto_raw[i].type;
1320       if (type >= (sizeof (ppc_elf_howto_table)
1321                    / sizeof (ppc_elf_howto_table[0])))
1322         abort ();
1323       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1324     }
1325 }
1326
1327 static reloc_howto_type *
1328 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1329                            bfd_reloc_code_real_type code)
1330 {
1331   enum elf_ppc_reloc_type r;
1332
1333   /* Initialize howto table if not already done.  */
1334   if (!ppc_elf_howto_table[R_PPC_ADDR32])
1335     ppc_elf_howto_init ();
1336
1337   switch (code)
1338     {
1339     default:
1340       return NULL;
1341
1342     case BFD_RELOC_NONE:                r = R_PPC_NONE;                 break;
1343     case BFD_RELOC_32:                  r = R_PPC_ADDR32;               break;
1344     case BFD_RELOC_PPC_BA26:            r = R_PPC_ADDR24;               break;
1345     case BFD_RELOC_16:                  r = R_PPC_ADDR16;               break;
1346     case BFD_RELOC_LO16:                r = R_PPC_ADDR16_LO;            break;
1347     case BFD_RELOC_HI16:                r = R_PPC_ADDR16_HI;            break;
1348     case BFD_RELOC_HI16_S:              r = R_PPC_ADDR16_HA;            break;
1349     case BFD_RELOC_PPC_BA16:            r = R_PPC_ADDR14;               break;
1350     case BFD_RELOC_PPC_BA16_BRTAKEN:    r = R_PPC_ADDR14_BRTAKEN;       break;
1351     case BFD_RELOC_PPC_BA16_BRNTAKEN:   r = R_PPC_ADDR14_BRNTAKEN;      break;
1352     case BFD_RELOC_PPC_B26:             r = R_PPC_REL24;                break;
1353     case BFD_RELOC_PPC_B16:             r = R_PPC_REL14;                break;
1354     case BFD_RELOC_PPC_B16_BRTAKEN:     r = R_PPC_REL14_BRTAKEN;        break;
1355     case BFD_RELOC_PPC_B16_BRNTAKEN:    r = R_PPC_REL14_BRNTAKEN;       break;
1356     case BFD_RELOC_16_GOTOFF:           r = R_PPC_GOT16;                break;
1357     case BFD_RELOC_LO16_GOTOFF:         r = R_PPC_GOT16_LO;             break;
1358     case BFD_RELOC_HI16_GOTOFF:         r = R_PPC_GOT16_HI;             break;
1359     case BFD_RELOC_HI16_S_GOTOFF:       r = R_PPC_GOT16_HA;             break;
1360     case BFD_RELOC_24_PLT_PCREL:        r = R_PPC_PLTREL24;             break;
1361     case BFD_RELOC_PPC_COPY:            r = R_PPC_COPY;                 break;
1362     case BFD_RELOC_PPC_GLOB_DAT:        r = R_PPC_GLOB_DAT;             break;
1363     case BFD_RELOC_PPC_LOCAL24PC:       r = R_PPC_LOCAL24PC;            break;
1364     case BFD_RELOC_32_PCREL:            r = R_PPC_REL32;                break;
1365     case BFD_RELOC_32_PLTOFF:           r = R_PPC_PLT32;                break;
1366     case BFD_RELOC_32_PLT_PCREL:        r = R_PPC_PLTREL32;             break;
1367     case BFD_RELOC_LO16_PLTOFF:         r = R_PPC_PLT16_LO;             break;
1368     case BFD_RELOC_HI16_PLTOFF:         r = R_PPC_PLT16_HI;             break;
1369     case BFD_RELOC_HI16_S_PLTOFF:       r = R_PPC_PLT16_HA;             break;
1370     case BFD_RELOC_GPREL16:             r = R_PPC_SDAREL16;             break;
1371     case BFD_RELOC_16_BASEREL:          r = R_PPC_SECTOFF;              break;
1372     case BFD_RELOC_LO16_BASEREL:        r = R_PPC_SECTOFF_LO;           break;
1373     case BFD_RELOC_HI16_BASEREL:        r = R_PPC_SECTOFF_HI;           break;
1374     case BFD_RELOC_HI16_S_BASEREL:      r = R_PPC_SECTOFF_HA;           break;
1375     case BFD_RELOC_CTOR:                r = R_PPC_ADDR32;               break;
1376     case BFD_RELOC_PPC_TOC16:           r = R_PPC_TOC16;                break;
1377     case BFD_RELOC_PPC_TLS:             r = R_PPC_TLS;                  break;
1378     case BFD_RELOC_PPC_DTPMOD:          r = R_PPC_DTPMOD32;             break;
1379     case BFD_RELOC_PPC_TPREL16:         r = R_PPC_TPREL16;              break;
1380     case BFD_RELOC_PPC_TPREL16_LO:      r = R_PPC_TPREL16_LO;           break;
1381     case BFD_RELOC_PPC_TPREL16_HI:      r = R_PPC_TPREL16_HI;           break;
1382     case BFD_RELOC_PPC_TPREL16_HA:      r = R_PPC_TPREL16_HA;           break;
1383     case BFD_RELOC_PPC_TPREL:           r = R_PPC_TPREL32;              break;
1384     case BFD_RELOC_PPC_DTPREL16:        r = R_PPC_DTPREL16;             break;
1385     case BFD_RELOC_PPC_DTPREL16_LO:     r = R_PPC_DTPREL16_LO;          break;
1386     case BFD_RELOC_PPC_DTPREL16_HI:     r = R_PPC_DTPREL16_HI;          break;
1387     case BFD_RELOC_PPC_DTPREL16_HA:     r = R_PPC_DTPREL16_HA;          break;
1388     case BFD_RELOC_PPC_DTPREL:          r = R_PPC_DTPREL32;             break;
1389     case BFD_RELOC_PPC_GOT_TLSGD16:     r = R_PPC_GOT_TLSGD16;          break;
1390     case BFD_RELOC_PPC_GOT_TLSGD16_LO:  r = R_PPC_GOT_TLSGD16_LO;       break;
1391     case BFD_RELOC_PPC_GOT_TLSGD16_HI:  r = R_PPC_GOT_TLSGD16_HI;       break;
1392     case BFD_RELOC_PPC_GOT_TLSGD16_HA:  r = R_PPC_GOT_TLSGD16_HA;       break;
1393     case BFD_RELOC_PPC_GOT_TLSLD16:     r = R_PPC_GOT_TLSLD16;          break;
1394     case BFD_RELOC_PPC_GOT_TLSLD16_LO:  r = R_PPC_GOT_TLSLD16_LO;       break;
1395     case BFD_RELOC_PPC_GOT_TLSLD16_HI:  r = R_PPC_GOT_TLSLD16_HI;       break;
1396     case BFD_RELOC_PPC_GOT_TLSLD16_HA:  r = R_PPC_GOT_TLSLD16_HA;       break;
1397     case BFD_RELOC_PPC_GOT_TPREL16:     r = R_PPC_GOT_TPREL16;          break;
1398     case BFD_RELOC_PPC_GOT_TPREL16_LO:  r = R_PPC_GOT_TPREL16_LO;       break;
1399     case BFD_RELOC_PPC_GOT_TPREL16_HI:  r = R_PPC_GOT_TPREL16_HI;       break;
1400     case BFD_RELOC_PPC_GOT_TPREL16_HA:  r = R_PPC_GOT_TPREL16_HA;       break;
1401     case BFD_RELOC_PPC_GOT_DTPREL16:    r = R_PPC_GOT_DTPREL16;         break;
1402     case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO;      break;
1403     case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI;      break;
1404     case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA;      break;
1405     case BFD_RELOC_PPC_EMB_NADDR32:     r = R_PPC_EMB_NADDR32;          break;
1406     case BFD_RELOC_PPC_EMB_NADDR16:     r = R_PPC_EMB_NADDR16;          break;
1407     case BFD_RELOC_PPC_EMB_NADDR16_LO:  r = R_PPC_EMB_NADDR16_LO;       break;
1408     case BFD_RELOC_PPC_EMB_NADDR16_HI:  r = R_PPC_EMB_NADDR16_HI;       break;
1409     case BFD_RELOC_PPC_EMB_NADDR16_HA:  r = R_PPC_EMB_NADDR16_HA;       break;
1410     case BFD_RELOC_PPC_EMB_SDAI16:      r = R_PPC_EMB_SDAI16;           break;
1411     case BFD_RELOC_PPC_EMB_SDA2I16:     r = R_PPC_EMB_SDA2I16;          break;
1412     case BFD_RELOC_PPC_EMB_SDA2REL:     r = R_PPC_EMB_SDA2REL;          break;
1413     case BFD_RELOC_PPC_EMB_SDA21:       r = R_PPC_EMB_SDA21;            break;
1414     case BFD_RELOC_PPC_EMB_MRKREF:      r = R_PPC_EMB_MRKREF;           break;
1415     case BFD_RELOC_PPC_EMB_RELSEC16:    r = R_PPC_EMB_RELSEC16;         break;
1416     case BFD_RELOC_PPC_EMB_RELST_LO:    r = R_PPC_EMB_RELST_LO;         break;
1417     case BFD_RELOC_PPC_EMB_RELST_HI:    r = R_PPC_EMB_RELST_HI;         break;
1418     case BFD_RELOC_PPC_EMB_RELST_HA:    r = R_PPC_EMB_RELST_HA;         break;
1419     case BFD_RELOC_PPC_EMB_BIT_FLD:     r = R_PPC_EMB_BIT_FLD;          break;
1420     case BFD_RELOC_PPC_EMB_RELSDA:      r = R_PPC_EMB_RELSDA;           break;
1421     case BFD_RELOC_VTABLE_INHERIT:      r = R_PPC_GNU_VTINHERIT;        break;
1422     case BFD_RELOC_VTABLE_ENTRY:        r = R_PPC_GNU_VTENTRY;          break;
1423     }
1424
1425   return ppc_elf_howto_table[r];
1426 };
1427
1428 /* Set the howto pointer for a PowerPC ELF reloc.  */
1429
1430 static void
1431 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1432                        arelent *cache_ptr,
1433                        Elf_Internal_Rela *dst)
1434 {
1435   /* Initialize howto table if not already done.  */
1436   if (!ppc_elf_howto_table[R_PPC_ADDR32])
1437     ppc_elf_howto_init ();
1438
1439   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1440   cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1441 }
1442
1443 /* Handle the R_PPC_ADDR16_HA reloc.  */
1444
1445 static bfd_reloc_status_type
1446 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1447                          arelent *reloc_entry,
1448                          asymbol *symbol,
1449                          void *data ATTRIBUTE_UNUSED,
1450                          asection *input_section,
1451                          bfd *output_bfd,
1452                          char **error_message ATTRIBUTE_UNUSED)
1453 {
1454   bfd_vma relocation;
1455
1456   if (output_bfd != NULL)
1457     {
1458       reloc_entry->address += input_section->output_offset;
1459       return bfd_reloc_ok;
1460     }
1461
1462   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1463     return bfd_reloc_outofrange;
1464
1465   if (bfd_is_com_section (symbol->section))
1466     relocation = 0;
1467   else
1468     relocation = symbol->value;
1469
1470   relocation += symbol->section->output_section->vma;
1471   relocation += symbol->section->output_offset;
1472   relocation += reloc_entry->addend;
1473
1474   reloc_entry->addend += (relocation & 0x8000) << 1;
1475
1476   return bfd_reloc_continue;
1477 }
1478
1479 static bfd_reloc_status_type
1480 ppc_elf_unhandled_reloc (bfd *abfd,
1481                          arelent *reloc_entry,
1482                          asymbol *symbol,
1483                          void *data,
1484                          asection *input_section,
1485                          bfd *output_bfd,
1486                          char **error_message)
1487 {
1488   /* If this is a relocatable link (output_bfd test tells us), just
1489      call the generic function.  Any adjustment will be done at final
1490      link time.  */
1491   if (output_bfd != NULL)
1492     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1493                                   input_section, output_bfd, error_message);
1494
1495   if (error_message != NULL)
1496     {
1497       static char buf[60];
1498       sprintf (buf, _("generic linker can't handle %s"),
1499                reloc_entry->howto->name);
1500       *error_message = buf;
1501     }
1502   return bfd_reloc_dangerous;
1503 }
1504 \f
1505 /* Sections created by the linker.  */
1506
1507 typedef struct elf_linker_section
1508 {
1509   /* Pointer to the bfd section.  */
1510   asection *section;
1511   /* Section name.  */
1512   const char *name;
1513   /* Associated bss section name.  */
1514   const char *bss_name;
1515   /* Associated symbol name.  */
1516   const char *sym_name;
1517   /* Value of symbol.  */
1518   bfd_vma sym_val;
1519 } elf_linker_section_t;
1520
1521 /* Linked list of allocated pointer entries.  This hangs off of the
1522    symbol lists, and provides allows us to return different pointers,
1523    based on different addend's.  */
1524
1525 typedef struct elf_linker_section_pointers
1526 {
1527   /* next allocated pointer for this symbol */
1528   struct elf_linker_section_pointers *next;
1529   /* offset of pointer from beginning of section */
1530   bfd_vma offset;
1531   /* addend used */
1532   bfd_vma addend;
1533   /* which linker section this is */
1534   elf_linker_section_t *lsect;
1535 } elf_linker_section_pointers_t;
1536
1537 struct ppc_elf_obj_tdata
1538 {
1539   struct elf_obj_tdata elf;
1540
1541   /* A mapping from local symbols to offsets into the various linker
1542      sections added.  This is index by the symbol index.  */
1543   elf_linker_section_pointers_t **linker_section_pointers;
1544 };
1545
1546 #define ppc_elf_tdata(bfd) \
1547   ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1548
1549 #define elf_local_ptr_offsets(bfd) \
1550   (ppc_elf_tdata (bfd)->linker_section_pointers)
1551
1552 /* Override the generic function because we store some extras.  */
1553
1554 static bfd_boolean
1555 ppc_elf_mkobject (bfd *abfd)
1556 {
1557   bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
1558   abfd->tdata.any = bfd_zalloc (abfd, amt);
1559   if (abfd->tdata.any == NULL)
1560     return FALSE;
1561   return TRUE;
1562 }
1563
1564 /* Fix bad default arch selected for a 32 bit input bfd when the
1565    default is 64 bit.  */
1566
1567 static bfd_boolean
1568 ppc_elf_object_p (bfd *abfd)
1569 {
1570   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1571     {
1572       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1573
1574       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1575         {
1576           /* Relies on arch after 64 bit default being 32 bit default.  */
1577           abfd->arch_info = abfd->arch_info->next;
1578           BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1579         }
1580     }
1581   return TRUE;
1582 }
1583
1584 /* Function to set whether a module needs the -mrelocatable bit set.  */
1585
1586 static bfd_boolean
1587 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1588 {
1589   BFD_ASSERT (!elf_flags_init (abfd)
1590               || elf_elfheader (abfd)->e_flags == flags);
1591
1592   elf_elfheader (abfd)->e_flags = flags;
1593   elf_flags_init (abfd) = TRUE;
1594   return TRUE;
1595 }
1596
1597 /* Support for core dump NOTE sections.  */
1598
1599 static bfd_boolean
1600 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1601 {
1602   int offset;
1603   unsigned int size;
1604
1605   switch (note->descsz)
1606     {
1607     default:
1608       return FALSE;
1609
1610     case 268:           /* Linux/PPC.  */
1611       /* pr_cursig */
1612       elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1613
1614       /* pr_pid */
1615       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1616
1617       /* pr_reg */
1618       offset = 72;
1619       size = 192;
1620
1621       break;
1622     }
1623
1624   /* Make a ".reg/999" section.  */
1625   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1626                                           size, note->descpos + offset);
1627 }
1628
1629 static bfd_boolean
1630 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1631 {
1632   switch (note->descsz)
1633     {
1634     default:
1635       return FALSE;
1636
1637     case 128:           /* Linux/PPC elf_prpsinfo.  */
1638       elf_tdata (abfd)->core_program
1639         = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1640       elf_tdata (abfd)->core_command
1641         = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1642     }
1643
1644   /* Note that for some reason, a spurious space is tacked
1645      onto the end of the args in some (at least one anyway)
1646      implementations, so strip it off if it exists.  */
1647
1648   {
1649     char *command = elf_tdata (abfd)->core_command;
1650     int n = strlen (command);
1651
1652     if (0 < n && command[n - 1] == ' ')
1653       command[n - 1] = '\0';
1654   }
1655
1656   return TRUE;
1657 }
1658
1659 /* Return address for Ith PLT stub in section PLT, for relocation REL
1660    or (bfd_vma) -1 if it should not be included.  */
1661
1662 static bfd_vma
1663 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1664                      const asection *plt ATTRIBUTE_UNUSED,
1665                      const arelent *rel)
1666 {
1667   return rel->address;
1668 }
1669
1670 /* Handle a PowerPC specific section when reading an object file.  This
1671    is called when bfd_section_from_shdr finds a section with an unknown
1672    type.  */
1673
1674 static bfd_boolean
1675 ppc_elf_section_from_shdr (bfd *abfd,
1676                            Elf_Internal_Shdr *hdr,
1677                            const char *name,
1678                            int shindex)
1679 {
1680   asection *newsect;
1681   flagword flags;
1682
1683   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1684     return FALSE;
1685
1686   newsect = hdr->bfd_section;
1687   flags = bfd_get_section_flags (abfd, newsect);
1688   if (hdr->sh_flags & SHF_EXCLUDE)
1689     flags |= SEC_EXCLUDE;
1690
1691   if (hdr->sh_type == SHT_ORDERED)
1692     flags |= SEC_SORT_ENTRIES;
1693
1694   bfd_set_section_flags (abfd, newsect, flags);
1695   return TRUE;
1696 }
1697
1698 /* Set up any other section flags and such that may be necessary.  */
1699
1700 static bfd_boolean
1701 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1702                        Elf_Internal_Shdr *shdr,
1703                        asection *asect)
1704 {
1705   if ((asect->flags & SEC_EXCLUDE) != 0)
1706     shdr->sh_flags |= SHF_EXCLUDE;
1707
1708   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1709     shdr->sh_type = SHT_ORDERED;
1710
1711   return TRUE;
1712 }
1713
1714 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
1715    need to bump up the number of section headers.  */
1716
1717 static int
1718 ppc_elf_additional_program_headers (bfd *abfd)
1719 {
1720   asection *s;
1721   int ret = 0;
1722
1723   s = bfd_get_section_by_name (abfd, ".sbss2");
1724   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1725     ++ret;
1726
1727   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1728   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1729     ++ret;
1730
1731   return ret;
1732 }
1733
1734 /* Add extra PPC sections -- Note, for now, make .sbss2 and
1735    .PPC.EMB.sbss0 a normal section, and not a bss section so
1736    that the linker doesn't crater when trying to make more than
1737    2 sections.  */
1738
1739 static struct bfd_elf_special_section const ppc_elf_special_sections[]=
1740 {
1741   { ".tags",             5,  0, SHT_ORDERED,  SHF_ALLOC },
1742   { ".sdata",            6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1743   { ".sbss",             5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1744   { ".sdata2",           7, -2, SHT_PROGBITS, SHF_ALLOC },
1745   { ".sbss2",            6, -2, SHT_PROGBITS, SHF_ALLOC },
1746   { ".PPC.EMB.apuinfo", 16,  0, SHT_NOTE,     0 },
1747   { ".PPC.EMB.sdata0",  15,  0, SHT_PROGBITS, SHF_ALLOC },
1748   { ".PPC.EMB.sbss0",   14,  0, SHT_PROGBITS, SHF_ALLOC },
1749   { ".plt",              4,  0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
1750   { NULL,                0,  0, 0,            0 }
1751 };
1752 \f
1753 /* Very simple linked list structure for recording apuinfo values.  */
1754 typedef struct apuinfo_list
1755 {
1756   struct apuinfo_list *next;
1757   unsigned long value;
1758 }
1759 apuinfo_list;
1760
1761 static apuinfo_list *head;
1762
1763
1764 static void
1765 apuinfo_list_init (void)
1766 {
1767   head = NULL;
1768 }
1769
1770 static void
1771 apuinfo_list_add (unsigned long value)
1772 {
1773   apuinfo_list *entry = head;
1774
1775   while (entry != NULL)
1776     {
1777       if (entry->value == value)
1778         return;
1779       entry = entry->next;
1780     }
1781
1782   entry = bfd_malloc (sizeof (* entry));
1783   if (entry == NULL)
1784     return;
1785
1786   entry->value = value;
1787   entry->next  = head;
1788   head = entry;
1789 }
1790
1791 static unsigned
1792 apuinfo_list_length (void)
1793 {
1794   apuinfo_list *entry;
1795   unsigned long count;
1796
1797   for (entry = head, count = 0;
1798        entry;
1799        entry = entry->next)
1800     ++ count;
1801
1802   return count;
1803 }
1804
1805 static inline unsigned long
1806 apuinfo_list_element (unsigned long number)
1807 {
1808   apuinfo_list * entry;
1809
1810   for (entry = head;
1811        entry && number --;
1812        entry = entry->next)
1813     ;
1814
1815   return entry ? entry->value : 0;
1816 }
1817
1818 static void
1819 apuinfo_list_finish (void)
1820 {
1821   apuinfo_list *entry;
1822
1823   for (entry = head; entry;)
1824     {
1825       apuinfo_list *next = entry->next;
1826       free (entry);
1827       entry = next;
1828     }
1829
1830   head = NULL;
1831 }
1832
1833 #define APUINFO_SECTION_NAME    ".PPC.EMB.apuinfo"
1834 #define APUINFO_LABEL           "APUinfo"
1835
1836 /* Scan the input BFDs and create a linked list of
1837    the APUinfo values that will need to be emitted.  */
1838
1839 static void
1840 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
1841 {
1842   bfd *ibfd;
1843   asection *asec;
1844   char *buffer;
1845   unsigned num_input_sections;
1846   bfd_size_type output_section_size;
1847   unsigned i;
1848   unsigned num_entries;
1849   unsigned long offset;
1850   unsigned long length;
1851   const char *error_message = NULL;
1852
1853   if (link_info == NULL)
1854     return;
1855
1856   /* Scan the input bfds, looking for apuinfo sections.  */
1857   num_input_sections = 0;
1858   output_section_size = 0;
1859
1860   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
1861     {
1862       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1863       if (asec)
1864         {
1865           ++ num_input_sections;
1866           output_section_size += asec->size;
1867         }
1868     }
1869
1870   /* We need at least one input sections
1871      in order to make merging worthwhile.  */
1872   if (num_input_sections < 1)
1873     return;
1874
1875   /* Just make sure that the output section exists as well.  */
1876   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1877   if (asec == NULL)
1878     return;
1879
1880   /* Allocate a buffer for the contents of the input sections.  */
1881   buffer = bfd_malloc (output_section_size);
1882   if (buffer == NULL)
1883     return;
1884
1885   offset = 0;
1886   apuinfo_list_init ();
1887
1888   /* Read in the input sections contents.  */
1889   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
1890     {
1891       unsigned long datum;
1892       char *ptr;
1893
1894       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1895       if (asec == NULL)
1896         continue;
1897
1898       length = asec->size;
1899       if (length < 24)
1900         {
1901           error_message = _("corrupt or empty %s section in %B");
1902           goto fail;
1903         }
1904
1905       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
1906           || (bfd_bread (buffer + offset, length, ibfd) != length))
1907         {
1908           error_message = _("unable to read in %s section from %B");
1909           goto fail;
1910         }
1911
1912       /* Process the contents of the section.  */
1913       ptr = buffer + offset;
1914       error_message = _("corrupt %s section in %B");
1915
1916       /* Verify the contents of the header.  Note - we have to
1917          extract the values this way in order to allow for a
1918          host whose endian-ness is different from the target.  */
1919       datum = bfd_get_32 (ibfd, ptr);
1920       if (datum != sizeof APUINFO_LABEL)
1921         goto fail;
1922
1923       datum = bfd_get_32 (ibfd, ptr + 8);
1924       if (datum != 0x2)
1925         goto fail;
1926
1927       if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
1928         goto fail;
1929
1930       /* Get the number of bytes used for apuinfo entries.  */
1931       datum = bfd_get_32 (ibfd, ptr + 4);
1932       if (datum + 20 != length)
1933         goto fail;
1934
1935       /* Make sure that we do not run off the end of the section.  */
1936       if (offset + length > output_section_size)
1937         goto fail;
1938
1939       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
1940       for (i = 0; i < datum; i += 4)
1941         apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
1942
1943       /* Update the offset.  */
1944       offset += length;
1945     }
1946
1947   error_message = NULL;
1948
1949   /* Compute the size of the output section.  */
1950   num_entries = apuinfo_list_length ();
1951   output_section_size = 20 + num_entries * 4;
1952
1953   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1954
1955   if (! bfd_set_section_size (abfd, asec, output_section_size))
1956     ibfd = abfd,
1957       error_message = _("warning: unable to set size of %s section in %B");
1958
1959  fail:
1960   free (buffer);
1961
1962   if (error_message)
1963     (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
1964 }
1965
1966 /* Prevent the output section from accumulating the input sections'
1967    contents.  We have already stored this in our linked list structure.  */
1968
1969 static bfd_boolean
1970 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
1971                        asection *asec,
1972                        bfd_byte *contents ATTRIBUTE_UNUSED)
1973 {
1974   return (apuinfo_list_length ()
1975           && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
1976 }
1977
1978 /* Finally we can generate the output section.  */
1979
1980 static void
1981 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
1982 {
1983   bfd_byte *buffer;
1984   asection *asec;
1985   unsigned i;
1986   unsigned num_entries;
1987   bfd_size_type length;
1988
1989   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1990   if (asec == NULL)
1991     return;
1992
1993   if (apuinfo_list_length () == 0)
1994     return;
1995
1996   length = asec->size;
1997   if (length < 20)
1998     return;
1999
2000   buffer = bfd_malloc (length);
2001   if (buffer == NULL)
2002     {
2003       (*_bfd_error_handler)
2004         (_("failed to allocate space for new APUinfo section."));
2005       return;
2006     }
2007
2008   /* Create the apuinfo header.  */
2009   num_entries = apuinfo_list_length ();
2010   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2011   bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2012   bfd_put_32 (abfd, 0x2, buffer + 8);
2013   strcpy ((char *) buffer + 12, APUINFO_LABEL);
2014
2015   length = 20;
2016   for (i = 0; i < num_entries; i++)
2017     {
2018       bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2019       length += 4;
2020     }
2021
2022   if (length != asec->size)
2023     (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2024
2025   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2026     (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2027
2028   free (buffer);
2029
2030   apuinfo_list_finish ();
2031 }
2032 \f
2033 /* The following functions are specific to the ELF linker, while
2034    functions above are used generally.  They appear in this file more
2035    or less in the order in which they are called.  eg.
2036    ppc_elf_check_relocs is called early in the link process,
2037    ppc_elf_finish_dynamic_sections is one of the last functions
2038    called.  */
2039
2040 /* The PPC linker needs to keep track of the number of relocs that it
2041    decides to copy as dynamic relocs in check_relocs for each symbol.
2042    This is so that it can later discard them if they are found to be
2043    unnecessary.  We store the information in a field extending the
2044    regular ELF linker hash table.  */
2045
2046 struct ppc_elf_dyn_relocs
2047 {
2048   struct ppc_elf_dyn_relocs *next;
2049
2050   /* The input section of the reloc.  */
2051   asection *sec;
2052
2053   /* Total number of relocs copied for the input section.  */
2054   bfd_size_type count;
2055
2056   /* Number of pc-relative relocs copied for the input section.  */
2057   bfd_size_type pc_count;
2058 };
2059
2060 /* Of those relocs that might be copied as dynamic relocs, this macro
2061    selects those that must be copied when linking a shared library,
2062    even when the symbol is local.  */
2063
2064 #define MUST_BE_DYN_RELOC(RTYPE)                \
2065   ((RTYPE) != R_PPC_REL24                       \
2066    && (RTYPE) != R_PPC_REL14                    \
2067    && (RTYPE) != R_PPC_REL14_BRTAKEN            \
2068    && (RTYPE) != R_PPC_REL14_BRNTAKEN           \
2069    && (RTYPE) != R_PPC_REL32)
2070
2071 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2072    copying dynamic variables from a shared lib into an app's dynbss
2073    section, and instead use a dynamic relocation to point into the
2074    shared lib.  */
2075 #define ELIMINATE_COPY_RELOCS 1
2076
2077 /* PPC ELF linker hash entry.  */
2078
2079 struct ppc_elf_link_hash_entry
2080 {
2081   struct elf_link_hash_entry elf;
2082
2083   /* If this symbol is used in the linker created sections, the processor
2084      specific backend uses this field to map the field into the offset
2085      from the beginning of the section.  */
2086   elf_linker_section_pointers_t *linker_section_pointer;
2087
2088   /* Track dynamic relocs copied for this symbol.  */
2089   struct ppc_elf_dyn_relocs *dyn_relocs;
2090
2091   /* Contexts in which symbol is used in the GOT (or TOC).
2092      TLS_GD .. TLS_TLS bits are or'd into the mask as the
2093      corresponding relocs are encountered during check_relocs.
2094      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2095      indicate the corresponding GOT entry type is not needed.  */
2096 #define TLS_GD           1      /* GD reloc. */
2097 #define TLS_LD           2      /* LD reloc. */
2098 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
2099 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
2100 #define TLS_TLS         16      /* Any TLS reloc.  */
2101 #define TLS_TPRELGD     32      /* TPREL reloc resulting from GD->IE. */
2102   char tls_mask;
2103 };
2104
2105 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2106
2107 /* PPC ELF linker hash table.  */
2108
2109 struct ppc_elf_link_hash_table
2110 {
2111   struct elf_link_hash_table elf;
2112
2113   /* Short-cuts to get to dynamic linker sections.  */
2114   asection *got;
2115   asection *relgot;
2116   asection *plt;
2117   asection *relplt;
2118   asection *dynbss;
2119   asection *relbss;
2120   asection *dynsbss;
2121   asection *relsbss;
2122   elf_linker_section_t sdata[2];
2123   asection *sbss;
2124
2125   /* Shortcut to .__tls_get_addr.  */
2126   struct elf_link_hash_entry *tls_get_addr;
2127
2128   /* TLS local dynamic got entry handling.  */
2129   union {
2130     bfd_signed_vma refcount;
2131     bfd_vma offset;
2132   } tlsld_got;
2133
2134   /* Small local sym to section mapping cache.  */
2135   struct sym_sec_cache sym_sec;
2136 };
2137
2138 /* Get the PPC ELF linker hash table from a link_info structure.  */
2139
2140 #define ppc_elf_hash_table(p) \
2141   ((struct ppc_elf_link_hash_table *) (p)->hash)
2142
2143 /* Create an entry in a PPC ELF linker hash table.  */
2144
2145 static struct bfd_hash_entry *
2146 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2147                            struct bfd_hash_table *table,
2148                            const char *string)
2149 {
2150   /* Allocate the structure if it has not already been allocated by a
2151      subclass.  */
2152   if (entry == NULL)
2153     {
2154       entry = bfd_hash_allocate (table,
2155                                  sizeof (struct ppc_elf_link_hash_entry));
2156       if (entry == NULL)
2157         return entry;
2158     }
2159
2160   /* Call the allocation method of the superclass.  */
2161   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2162   if (entry != NULL)
2163     {
2164       ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2165       ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2166       ppc_elf_hash_entry (entry)->tls_mask = 0;
2167     }
2168
2169   return entry;
2170 }
2171
2172 /* Create a PPC ELF linker hash table.  */
2173
2174 static struct bfd_link_hash_table *
2175 ppc_elf_link_hash_table_create (bfd *abfd)
2176 {
2177   struct ppc_elf_link_hash_table *ret;
2178
2179   ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2180   if (ret == NULL)
2181     return NULL;
2182
2183   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
2184                                        ppc_elf_link_hash_newfunc))
2185     {
2186       free (ret);
2187       return NULL;
2188     }
2189
2190   ret->sdata[0].name = ".sdata";
2191   ret->sdata[0].sym_name = "_SDA_BASE_";
2192   ret->sdata[0].bss_name = ".sbss";
2193
2194   ret->sdata[1].name = ".sdata2";
2195   ret->sdata[1].sym_name = "_SDA2_BASE_";
2196   ret->sdata[1].bss_name = ".sbss2";
2197
2198   return &ret->elf.root;
2199 }
2200
2201 /* The powerpc .got has a blrl instruction in it.  Mark it executable.  */
2202
2203 static bfd_boolean
2204 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2205 {
2206   struct ppc_elf_link_hash_table *htab;
2207   asection *s;
2208   flagword flags;
2209
2210   if (!_bfd_elf_create_got_section (abfd, info))
2211     return FALSE;
2212
2213   htab = ppc_elf_hash_table (info);
2214   htab->got = s = bfd_get_section_by_name (abfd, ".got");
2215   if (s == NULL)
2216     abort ();
2217
2218   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2219            | SEC_LINKER_CREATED);
2220   if (!bfd_set_section_flags (abfd, s, flags))
2221     return FALSE;
2222
2223   htab->relgot = bfd_make_section_with_flags (abfd, ".rela.got",
2224                                               SEC_ALLOC | SEC_LOAD
2225                                               | SEC_HAS_CONTENTS
2226                                               | SEC_IN_MEMORY
2227                                               | SEC_LINKER_CREATED
2228                                               | SEC_READONLY);
2229   if (!htab->relgot
2230       || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2231     return FALSE;
2232
2233   return TRUE;
2234 }
2235
2236 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2237    to output sections (just like _bfd_elf_create_dynamic_sections has
2238    to create .dynbss and .rela.bss).  */
2239
2240 static bfd_boolean
2241 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2242 {
2243   struct ppc_elf_link_hash_table *htab;
2244   asection *s;
2245   flagword flags;
2246
2247   htab = ppc_elf_hash_table (info);
2248
2249   if (htab->got == NULL
2250       && !ppc_elf_create_got (abfd, info))
2251     return FALSE;
2252
2253   if (!_bfd_elf_create_dynamic_sections (abfd, info))
2254     return FALSE;
2255
2256   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2257            | SEC_LINKER_CREATED);
2258
2259   htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2260   htab->dynsbss = s = bfd_make_section_with_flags (abfd, ".dynsbss",
2261                                                    SEC_ALLOC
2262                                                    | SEC_LINKER_CREATED);
2263   if (s == NULL)
2264     return FALSE;
2265
2266   if (! info->shared)
2267     {
2268       htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2269       htab->relsbss = s = bfd_make_section_with_flags (abfd,
2270                                                        ".rela.sbss",
2271                                                        flags | SEC_READONLY);
2272       if (s == NULL
2273           || ! bfd_set_section_alignment (abfd, s, 2))
2274         return FALSE;
2275     }
2276
2277   htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2278   htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2279   if (s == NULL)
2280     abort ();
2281
2282   flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2283   return bfd_set_section_flags (abfd, s, flags);
2284 }
2285
2286 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2287
2288 static void
2289 ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
2290                               struct elf_link_hash_entry *dir,
2291                               struct elf_link_hash_entry *ind)
2292 {
2293   struct ppc_elf_link_hash_entry *edir, *eind;
2294
2295   edir = (struct ppc_elf_link_hash_entry *) dir;
2296   eind = (struct ppc_elf_link_hash_entry *) ind;
2297
2298   if (eind->dyn_relocs != NULL)
2299     {
2300       if (edir->dyn_relocs != NULL)
2301         {
2302           struct ppc_elf_dyn_relocs **pp;
2303           struct ppc_elf_dyn_relocs *p;
2304
2305           if (ind->root.type == bfd_link_hash_indirect)
2306             abort ();
2307
2308           /* Add reloc counts against the weak sym to the strong sym
2309              list.  Merge any entries against the same section.  */
2310           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2311             {
2312               struct ppc_elf_dyn_relocs *q;
2313
2314               for (q = edir->dyn_relocs; q != NULL; q = q->next)
2315                 if (q->sec == p->sec)
2316                   {
2317                     q->pc_count += p->pc_count;
2318                     q->count += p->count;
2319                     *pp = p->next;
2320                     break;
2321                   }
2322               if (q == NULL)
2323                 pp = &p->next;
2324             }
2325           *pp = edir->dyn_relocs;
2326         }
2327
2328       edir->dyn_relocs = eind->dyn_relocs;
2329       eind->dyn_relocs = NULL;
2330     }
2331
2332   edir->tls_mask |= eind->tls_mask;
2333
2334   if (ELIMINATE_COPY_RELOCS
2335       && ind->root.type != bfd_link_hash_indirect
2336       && dir->dynamic_adjusted)
2337     {
2338       /* If called to transfer flags for a weakdef during processing
2339          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
2340          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
2341       dir->ref_dynamic |= ind->ref_dynamic;
2342       dir->ref_regular |= ind->ref_regular;
2343       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
2344       dir->needs_plt |= ind->needs_plt;
2345     }
2346   else
2347     _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
2348 }
2349
2350 /* Return 1 if target is one of ours.  */
2351
2352 static bfd_boolean
2353 is_ppc_elf_target (const struct bfd_target *targ)
2354 {
2355   extern const bfd_target bfd_elf32_powerpc_vec;
2356   extern const bfd_target bfd_elf32_powerpcle_vec;
2357
2358   return targ == &bfd_elf32_powerpc_vec || targ == &bfd_elf32_powerpcle_vec;
2359 }
2360
2361 /* Hook called by the linker routine which adds symbols from an object
2362    file.  We use it to put .comm items in .sbss, and not .bss.  */
2363
2364 static bfd_boolean
2365 ppc_elf_add_symbol_hook (bfd *abfd,
2366                          struct bfd_link_info *info,
2367                          Elf_Internal_Sym *sym,
2368                          const char **namep ATTRIBUTE_UNUSED,
2369                          flagword *flagsp ATTRIBUTE_UNUSED,
2370                          asection **secp,
2371                          bfd_vma *valp)
2372 {
2373   if (sym->st_shndx == SHN_COMMON
2374       && !info->relocatable
2375       && sym->st_size <= elf_gp_size (abfd)
2376       && is_ppc_elf_target (info->hash->creator))
2377     {
2378       /* Common symbols less than or equal to -G nn bytes are automatically
2379          put into .sbss.  */
2380       struct ppc_elf_link_hash_table *htab;
2381
2382       htab = ppc_elf_hash_table (info);
2383       if (htab->sbss == NULL)
2384         {
2385           flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
2386
2387           if (!htab->elf.dynobj)
2388             htab->elf.dynobj = abfd;
2389
2390           htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2391                                                            ".sbss",
2392                                                            flags);
2393           if (htab->sbss == NULL)
2394             return FALSE;
2395         }
2396
2397       *secp = htab->sbss;
2398       *valp = sym->st_size;
2399     }
2400
2401   return TRUE;
2402 }
2403 \f
2404 /* Create a special linker section.  */
2405
2406 static bfd_boolean
2407 ppc_elf_create_linker_section (bfd *abfd,
2408                                struct bfd_link_info *info,
2409                                flagword flags,
2410                                elf_linker_section_t *lsect)
2411 {
2412   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2413   asection *s;
2414
2415   flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2416             | SEC_LINKER_CREATED);
2417
2418   /* Record the first bfd that needs the special sections.  */
2419   if (!htab->elf.dynobj)
2420     htab->elf.dynobj = abfd;
2421
2422   s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2423                                           lsect->name,
2424                                           flags);
2425   if (s == NULL
2426       || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2427     return FALSE;
2428   lsect->section = s;
2429
2430   return TRUE;
2431 }
2432
2433 /* Find a linker generated pointer with a given addend and type.  */
2434
2435 static elf_linker_section_pointers_t *
2436 elf_find_pointer_linker_section
2437   (elf_linker_section_pointers_t *linker_pointers,
2438    bfd_vma addend,
2439    elf_linker_section_t *lsect)
2440 {
2441   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2442     if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2443       return linker_pointers;
2444
2445   return NULL;
2446 }
2447
2448 /* Allocate a pointer to live in a linker created section.  */
2449
2450 static bfd_boolean
2451 elf_create_pointer_linker_section (bfd *abfd,
2452                                    elf_linker_section_t *lsect,
2453                                    struct elf_link_hash_entry *h,
2454                                    const Elf_Internal_Rela *rel)
2455 {
2456   elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2457   elf_linker_section_pointers_t *linker_section_ptr;
2458   unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2459   bfd_size_type amt;
2460
2461   BFD_ASSERT (lsect != NULL);
2462
2463   /* Is this a global symbol?  */
2464   if (h != NULL)
2465     {
2466       struct ppc_elf_link_hash_entry *eh;
2467
2468       /* Has this symbol already been allocated?  If so, our work is done.  */
2469       eh = (struct ppc_elf_link_hash_entry *) h;
2470       if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2471                                            rel->r_addend,
2472                                            lsect))
2473         return TRUE;
2474
2475       ptr_linker_section_ptr = &eh->linker_section_pointer;
2476     }
2477   else
2478     {
2479       /* Allocation of a pointer to a local symbol.  */
2480       elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2481
2482       /* Allocate a table to hold the local symbols if first time.  */
2483       if (!ptr)
2484         {
2485           unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2486
2487           amt = num_symbols;
2488           amt *= sizeof (elf_linker_section_pointers_t *);
2489           ptr = bfd_zalloc (abfd, amt);
2490
2491           if (!ptr)
2492             return FALSE;
2493
2494           elf_local_ptr_offsets (abfd) = ptr;
2495         }
2496
2497       /* Has this symbol already been allocated?  If so, our work is done.  */
2498       if (elf_find_pointer_linker_section (ptr[r_symndx],
2499                                            rel->r_addend,
2500                                            lsect))
2501         return TRUE;
2502
2503       ptr_linker_section_ptr = &ptr[r_symndx];
2504     }
2505
2506   /* Allocate space for a pointer in the linker section, and allocate
2507      a new pointer record from internal memory.  */
2508   BFD_ASSERT (ptr_linker_section_ptr != NULL);
2509   amt = sizeof (elf_linker_section_pointers_t);
2510   linker_section_ptr = bfd_alloc (abfd, amt);
2511
2512   if (!linker_section_ptr)
2513     return FALSE;
2514
2515   linker_section_ptr->next = *ptr_linker_section_ptr;
2516   linker_section_ptr->addend = rel->r_addend;
2517   linker_section_ptr->lsect = lsect;
2518   *ptr_linker_section_ptr = linker_section_ptr;
2519
2520   linker_section_ptr->offset = lsect->section->size;
2521   lsect->section->size += 4;
2522
2523 #ifdef DEBUG
2524   fprintf (stderr,
2525            "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2526            lsect->name, (long) linker_section_ptr->offset,
2527            (long) lsect->section->size);
2528 #endif
2529
2530   return TRUE;
2531 }
2532
2533 static bfd_boolean
2534 update_local_sym_info (bfd *abfd,
2535                        Elf_Internal_Shdr *symtab_hdr,
2536                        unsigned long r_symndx,
2537                        int tls_type)
2538 {
2539   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
2540   char *local_got_tls_masks;
2541
2542   if (local_got_refcounts == NULL)
2543     {
2544       bfd_size_type size = symtab_hdr->sh_info;
2545
2546       size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
2547       local_got_refcounts = bfd_zalloc (abfd, size);
2548       if (local_got_refcounts == NULL)
2549         return FALSE;
2550       elf_local_got_refcounts (abfd) = local_got_refcounts;
2551     }
2552
2553   local_got_refcounts[r_symndx] += 1;
2554   local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2555   local_got_tls_masks[r_symndx] |= tls_type;
2556   return TRUE;
2557 }
2558
2559 static void
2560 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
2561 {
2562   (*_bfd_error_handler)
2563     (_("%B: relocation %s cannot be used when making a shared object"),
2564      abfd,
2565      ppc_elf_howto_table[r_type]->name);
2566   bfd_set_error (bfd_error_bad_value);
2567 }
2568
2569 /* Look through the relocs for a section during the first phase, and
2570    allocate space in the global offset table or procedure linkage
2571    table.  */
2572
2573 static bfd_boolean
2574 ppc_elf_check_relocs (bfd *abfd,
2575                       struct bfd_link_info *info,
2576                       asection *sec,
2577                       const Elf_Internal_Rela *relocs)
2578 {
2579   struct ppc_elf_link_hash_table *htab;
2580   Elf_Internal_Shdr *symtab_hdr;
2581   struct elf_link_hash_entry **sym_hashes;
2582   const Elf_Internal_Rela *rel;
2583   const Elf_Internal_Rela *rel_end;
2584   asection *sreloc;
2585
2586   if (info->relocatable)
2587     return TRUE;
2588
2589   /* Don't do anything special with non-loaded, non-alloced sections.
2590      In particular, any relocs in such sections should not affect GOT
2591      and PLT reference counting (ie. we don't allow them to create GOT
2592      or PLT entries), there's no possibility or desire to optimize TLS
2593      relocs, and there's not much point in propagating relocs to shared
2594      libs that the dynamic linker won't relocate.  */
2595   if ((sec->flags & SEC_ALLOC) == 0)
2596     return TRUE;
2597
2598 #ifdef DEBUG
2599   _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
2600                       sec, abfd);
2601 #endif
2602
2603   /* Initialize howto table if not already done.  */
2604   if (!ppc_elf_howto_table[R_PPC_ADDR32])
2605     ppc_elf_howto_init ();
2606
2607   htab = ppc_elf_hash_table (info);
2608   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2609   sym_hashes = elf_sym_hashes (abfd);
2610   sreloc = NULL;
2611
2612   rel_end = relocs + sec->reloc_count;
2613   for (rel = relocs; rel < rel_end; rel++)
2614     {
2615       unsigned long r_symndx;
2616       enum elf_ppc_reloc_type r_type;
2617       struct elf_link_hash_entry *h;
2618       int tls_type = 0;
2619
2620       r_symndx = ELF32_R_SYM (rel->r_info);
2621       if (r_symndx < symtab_hdr->sh_info)
2622         h = NULL;
2623       else
2624         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2625
2626       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2627          This shows up in particular in an R_PPC_ADDR32 in the eabi
2628          startup code.  */
2629       if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2630         {
2631           if (htab->got == NULL)
2632             {
2633               if (htab->elf.dynobj == NULL)
2634                 htab->elf.dynobj = abfd;
2635               if (!ppc_elf_create_got (htab->elf.dynobj, info))
2636                 return FALSE;
2637             }
2638         }
2639
2640       r_type = ELF32_R_TYPE (rel->r_info);
2641       switch (r_type)
2642         {
2643         case R_PPC_GOT_TLSLD16:
2644         case R_PPC_GOT_TLSLD16_LO:
2645         case R_PPC_GOT_TLSLD16_HI:
2646         case R_PPC_GOT_TLSLD16_HA:
2647           htab->tlsld_got.refcount += 1;
2648           tls_type = TLS_TLS | TLS_LD;
2649           goto dogottls;
2650
2651         case R_PPC_GOT_TLSGD16:
2652         case R_PPC_GOT_TLSGD16_LO:
2653         case R_PPC_GOT_TLSGD16_HI:
2654         case R_PPC_GOT_TLSGD16_HA:
2655           tls_type = TLS_TLS | TLS_GD;
2656           goto dogottls;
2657
2658         case R_PPC_GOT_TPREL16:
2659         case R_PPC_GOT_TPREL16_LO:
2660         case R_PPC_GOT_TPREL16_HI:
2661         case R_PPC_GOT_TPREL16_HA:
2662           if (info->shared)
2663             info->flags |= DF_STATIC_TLS;
2664           tls_type = TLS_TLS | TLS_TPREL;
2665           goto dogottls;
2666
2667         case R_PPC_GOT_DTPREL16:
2668         case R_PPC_GOT_DTPREL16_LO:
2669         case R_PPC_GOT_DTPREL16_HI:
2670         case R_PPC_GOT_DTPREL16_HA:
2671           tls_type = TLS_TLS | TLS_DTPREL;
2672         dogottls:
2673           sec->has_tls_reloc = 1;
2674           /* Fall thru */
2675
2676           /* GOT16 relocations */
2677         case R_PPC_GOT16:
2678         case R_PPC_GOT16_LO:
2679         case R_PPC_GOT16_HI:
2680         case R_PPC_GOT16_HA:
2681           /* This symbol requires a global offset table entry.  */
2682           if (htab->got == NULL)
2683             {
2684               if (htab->elf.dynobj == NULL)
2685                 htab->elf.dynobj = abfd;
2686               if (!ppc_elf_create_got (htab->elf.dynobj, info))
2687                 return FALSE;
2688             }
2689           if (h != NULL)
2690             {
2691               h->got.refcount += 1;
2692               ppc_elf_hash_entry (h)->tls_mask |= tls_type;
2693             }
2694           else
2695             /* This is a global offset table entry for a local symbol.  */
2696             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
2697               return FALSE;
2698           break;
2699
2700           /* Indirect .sdata relocation.  */
2701         case R_PPC_EMB_SDAI16:
2702           if (info->shared)
2703             {
2704               bad_shared_reloc (abfd, r_type);
2705               return FALSE;
2706             }
2707           if (htab->sdata[0].section == NULL
2708               && !ppc_elf_create_linker_section (abfd, info, 0,
2709                                                  &htab->sdata[0]))
2710             return FALSE;
2711           if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
2712                                                   h, rel))
2713             return FALSE;
2714           break;
2715
2716           /* Indirect .sdata2 relocation.  */
2717         case R_PPC_EMB_SDA2I16:
2718           if (info->shared)
2719             {
2720               bad_shared_reloc (abfd, r_type);
2721               return FALSE;
2722             }
2723           if (htab->sdata[1].section == NULL
2724               && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
2725                                                  &htab->sdata[1]))
2726             return FALSE;
2727           if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
2728                                                   h, rel))
2729             return FALSE;
2730           break;
2731
2732         case R_PPC_SDAREL16:
2733         case R_PPC_EMB_SDA2REL:
2734         case R_PPC_EMB_SDA21:
2735         case R_PPC_EMB_RELSDA:
2736         case R_PPC_EMB_NADDR32:
2737         case R_PPC_EMB_NADDR16:
2738         case R_PPC_EMB_NADDR16_LO:
2739         case R_PPC_EMB_NADDR16_HI:
2740         case R_PPC_EMB_NADDR16_HA:
2741           if (info->shared)
2742             {
2743               bad_shared_reloc (abfd, r_type);
2744               return FALSE;
2745             }
2746           break;
2747
2748         case R_PPC_PLT32:
2749         case R_PPC_PLTREL24:
2750         case R_PPC_PLTREL32:
2751         case R_PPC_PLT16_LO:
2752         case R_PPC_PLT16_HI:
2753         case R_PPC_PLT16_HA:
2754 #ifdef DEBUG
2755           fprintf (stderr, "Reloc requires a PLT entry\n");
2756 #endif
2757           /* This symbol requires a procedure linkage table entry.  We
2758              actually build the entry in finish_dynamic_symbol,
2759              because this might be a case of linking PIC code without
2760              linking in any dynamic objects, in which case we don't
2761              need to generate a procedure linkage table after all.  */
2762
2763           if (h == NULL)
2764             {
2765               /* It does not make sense to have a procedure linkage
2766                  table entry for a local symbol.  */
2767               (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
2768                                        "local symbol"),
2769                                      abfd,
2770                                      sec,
2771                                      (long) rel->r_offset,
2772                                      ppc_elf_howto_table[r_type]->name);
2773               bfd_set_error (bfd_error_bad_value);
2774               return FALSE;
2775             }
2776
2777           h->needs_plt = 1;
2778           h->plt.refcount++;
2779           break;
2780
2781           /* The following relocations don't need to propagate the
2782              relocation if linking a shared object since they are
2783              section relative.  */
2784         case R_PPC_SECTOFF:
2785         case R_PPC_SECTOFF_LO:
2786         case R_PPC_SECTOFF_HI:
2787         case R_PPC_SECTOFF_HA:
2788         case R_PPC_DTPREL16:
2789         case R_PPC_DTPREL16_LO:
2790         case R_PPC_DTPREL16_HI:
2791         case R_PPC_DTPREL16_HA:
2792         case R_PPC_TOC16:
2793           break;
2794
2795           /* This are just markers.  */
2796         case R_PPC_TLS:
2797         case R_PPC_EMB_MRKREF:
2798         case R_PPC_NONE:
2799         case R_PPC_max:
2800           break;
2801
2802           /* These should only appear in dynamic objects.  */
2803         case R_PPC_COPY:
2804         case R_PPC_GLOB_DAT:
2805         case R_PPC_JMP_SLOT:
2806         case R_PPC_RELATIVE:
2807           break;
2808
2809           /* These aren't handled yet.  We'll report an error later.  */
2810         case R_PPC_ADDR30:
2811         case R_PPC_EMB_RELSEC16:
2812         case R_PPC_EMB_RELST_LO:
2813         case R_PPC_EMB_RELST_HI:
2814         case R_PPC_EMB_RELST_HA:
2815         case R_PPC_EMB_BIT_FLD:
2816           break;
2817
2818           /* This refers only to functions defined in the shared library.  */
2819         case R_PPC_LOCAL24PC:
2820           break;
2821
2822           /* This relocation describes the C++ object vtable hierarchy.
2823              Reconstruct it for later use during GC.  */
2824         case R_PPC_GNU_VTINHERIT:
2825           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2826             return FALSE;
2827           break;
2828
2829           /* This relocation describes which C++ vtable entries are actually
2830              used.  Record for later use during GC.  */
2831         case R_PPC_GNU_VTENTRY:
2832           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2833             return FALSE;
2834           break;
2835
2836           /* We shouldn't really be seeing these.  */
2837         case R_PPC_TPREL32:
2838           if (info->shared)
2839             info->flags |= DF_STATIC_TLS;
2840           goto dodyn;
2841
2842           /* Nor these.  */
2843         case R_PPC_DTPMOD32:
2844         case R_PPC_DTPREL32:
2845           goto dodyn;
2846
2847         case R_PPC_TPREL16:
2848         case R_PPC_TPREL16_LO:
2849         case R_PPC_TPREL16_HI:
2850         case R_PPC_TPREL16_HA:
2851           if (info->shared)
2852             info->flags |= DF_STATIC_TLS;
2853           goto dodyn;
2854
2855           /* When creating a shared object, we must copy these
2856              relocs into the output file.  We create a reloc
2857              section in dynobj and make room for the reloc.  */
2858         case R_PPC_REL24:
2859         case R_PPC_REL14:
2860         case R_PPC_REL14_BRTAKEN:
2861         case R_PPC_REL14_BRNTAKEN:
2862         case R_PPC_REL32:
2863           if (h == NULL
2864               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2865             break;
2866           /* fall through */
2867
2868         case R_PPC_ADDR32:
2869         case R_PPC_ADDR24:
2870         case R_PPC_ADDR16:
2871         case R_PPC_ADDR16_LO:
2872         case R_PPC_ADDR16_HI:
2873         case R_PPC_ADDR16_HA:
2874         case R_PPC_ADDR14:
2875         case R_PPC_ADDR14_BRTAKEN:
2876         case R_PPC_ADDR14_BRNTAKEN:
2877         case R_PPC_UADDR32:
2878         case R_PPC_UADDR16:
2879           if (h != NULL && !info->shared)
2880             {
2881               /* We may need a plt entry if the symbol turns out to be
2882                  a function defined in a dynamic object.  */
2883               h->plt.refcount++;
2884
2885               /* We may need a copy reloc too.  */
2886               h->non_got_ref = 1;
2887             }
2888
2889         dodyn:
2890           /* If we are creating a shared library, and this is a reloc
2891              against a global symbol, or a non PC relative reloc
2892              against a local symbol, then we need to copy the reloc
2893              into the shared library.  However, if we are linking with
2894              -Bsymbolic, we do not need to copy a reloc against a
2895              global symbol which is defined in an object we are
2896              including in the link (i.e., DEF_REGULAR is set).  At
2897              this point we have not seen all the input files, so it is
2898              possible that DEF_REGULAR is not set now but will be set
2899              later (it is never cleared).  In case of a weak definition,
2900              DEF_REGULAR may be cleared later by a strong definition in
2901              a shared library.  We account for that possibility below by
2902              storing information in the dyn_relocs field of the hash
2903              table entry.  A similar situation occurs when creating
2904              shared libraries and symbol visibility changes render the
2905              symbol local.
2906
2907              If on the other hand, we are creating an executable, we
2908              may need to keep relocations for symbols satisfied by a
2909              dynamic library if we manage to avoid copy relocs for the
2910              symbol.  */
2911           if ((info->shared
2912                && (MUST_BE_DYN_RELOC (r_type)
2913                    || (h != NULL
2914                        && (! info->symbolic
2915                            || h->root.type == bfd_link_hash_defweak
2916                            || !h->def_regular))))
2917               || (ELIMINATE_COPY_RELOCS
2918                   && !info->shared
2919                   && h != NULL
2920                   && (h->root.type == bfd_link_hash_defweak
2921                       || !h->def_regular)))
2922             {
2923               struct ppc_elf_dyn_relocs *p;
2924               struct ppc_elf_dyn_relocs **head;
2925
2926 #ifdef DEBUG
2927               fprintf (stderr,
2928                        "ppc_elf_check_relocs needs to "
2929                        "create relocation for %s\n",
2930                        (h && h->root.root.string
2931                         ? h->root.root.string : "<unknown>"));
2932 #endif
2933               if (sreloc == NULL)
2934                 {
2935                   const char *name;
2936
2937                   name = (bfd_elf_string_from_elf_section
2938                           (abfd,
2939                            elf_elfheader (abfd)->e_shstrndx,
2940                            elf_section_data (sec)->rel_hdr.sh_name));
2941                   if (name == NULL)
2942                     return FALSE;
2943
2944                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2945                               && strcmp (bfd_get_section_name (abfd, sec),
2946                                          name + 5) == 0);
2947
2948                   if (htab->elf.dynobj == NULL)
2949                     htab->elf.dynobj = abfd;
2950                   sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
2951                   if (sreloc == NULL)
2952                     {
2953                       flagword flags;
2954
2955                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
2956                                | SEC_IN_MEMORY | SEC_LINKER_CREATED
2957                                | SEC_ALLOC | SEC_LOAD);
2958                       sreloc = bfd_make_section_with_flags (htab->elf.dynobj,
2959                                                             name,
2960                                                             flags);
2961                       if (sreloc == NULL
2962                           || ! bfd_set_section_alignment (htab->elf.dynobj,
2963                                                           sreloc, 2))
2964                         return FALSE;
2965                     }
2966                   elf_section_data (sec)->sreloc = sreloc;
2967                 }
2968
2969               /* If this is a global symbol, we count the number of
2970                  relocations we need for this symbol.  */
2971               if (h != NULL)
2972                 {
2973                   head = &ppc_elf_hash_entry (h)->dyn_relocs;
2974                 }
2975               else
2976                 {
2977                   /* Track dynamic relocs needed for local syms too.
2978                      We really need local syms available to do this
2979                      easily.  Oh well.  */
2980
2981                   asection *s;
2982                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
2983                                                  sec, r_symndx);
2984                   if (s == NULL)
2985                     return FALSE;
2986
2987                   head = ((struct ppc_elf_dyn_relocs **)
2988                           &elf_section_data (s)->local_dynrel);
2989                 }
2990
2991               p = *head;
2992               if (p == NULL || p->sec != sec)
2993                 {
2994                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
2995                   if (p == NULL)
2996                     return FALSE;
2997                   p->next = *head;
2998                   *head = p;
2999                   p->sec = sec;
3000                   p->count = 0;
3001                   p->pc_count = 0;
3002                 }
3003
3004               p->count += 1;
3005               if (!MUST_BE_DYN_RELOC (r_type))
3006                 p->pc_count += 1;
3007             }
3008
3009           break;
3010         }
3011     }
3012
3013   return TRUE;
3014 }
3015 \f
3016 /* Merge backend specific data from an object file to the output
3017    object file when linking.  */
3018
3019 static bfd_boolean
3020 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3021 {
3022   flagword old_flags;
3023   flagword new_flags;
3024   bfd_boolean error;
3025
3026   if (!is_ppc_elf_target (ibfd->xvec)
3027       || !is_ppc_elf_target (obfd->xvec))
3028     return TRUE;
3029
3030   /* Check if we have the same endianess.  */
3031   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3032     return FALSE;
3033
3034   new_flags = elf_elfheader (ibfd)->e_flags;
3035   old_flags = elf_elfheader (obfd)->e_flags;
3036   if (!elf_flags_init (obfd))
3037     {
3038       /* First call, no flags set.  */
3039       elf_flags_init (obfd) = TRUE;
3040       elf_elfheader (obfd)->e_flags = new_flags;
3041     }
3042
3043   /* Compatible flags are ok.  */
3044   else if (new_flags == old_flags)
3045     ;
3046
3047   /* Incompatible flags.  */
3048   else
3049     {
3050       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
3051          to be linked with either.  */
3052       error = FALSE;
3053       if ((new_flags & EF_PPC_RELOCATABLE) != 0
3054           && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
3055         {
3056           error = TRUE;
3057           (*_bfd_error_handler)
3058             (_("%B: compiled with -mrelocatable and linked with "
3059                "modules compiled normally"), ibfd);
3060         }
3061       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
3062                && (old_flags & EF_PPC_RELOCATABLE) != 0)
3063         {
3064           error = TRUE;
3065           (*_bfd_error_handler)
3066             (_("%B: compiled normally and linked with "
3067                "modules compiled with -mrelocatable"), ibfd);
3068         }
3069
3070       /* The output is -mrelocatable-lib iff both the input files are.  */
3071       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
3072         elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
3073
3074       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
3075          but each input file is either -mrelocatable or -mrelocatable-lib.  */
3076       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
3077           && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
3078           && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
3079         elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
3080
3081       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
3082          any module uses it.  */
3083       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
3084
3085       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3086       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3087
3088       /* Warn about any other mismatches.  */
3089       if (new_flags != old_flags)
3090         {
3091           error = TRUE;
3092           (*_bfd_error_handler)
3093             (_("%B: uses different e_flags (0x%lx) fields "
3094                "than previous modules (0x%lx)"),
3095              ibfd, (long) new_flags, (long) old_flags);
3096         }
3097
3098       if (error)
3099         {
3100           bfd_set_error (bfd_error_bad_value);
3101           return FALSE;
3102         }
3103     }
3104
3105   return TRUE;
3106 }
3107 \f
3108 /* Return the section that should be marked against GC for a given
3109    relocation.  */
3110
3111 static asection *
3112 ppc_elf_gc_mark_hook (asection *sec,
3113                       struct bfd_link_info *info ATTRIBUTE_UNUSED,
3114                       Elf_Internal_Rela *rel,
3115                       struct elf_link_hash_entry *h,
3116                       Elf_Internal_Sym *sym)
3117 {
3118   if (h != NULL)
3119     {
3120       switch (ELF32_R_TYPE (rel->r_info))
3121         {
3122         case R_PPC_GNU_VTINHERIT:
3123         case R_PPC_GNU_VTENTRY:
3124           break;
3125
3126         default:
3127           switch (h->root.type)
3128             {
3129             case bfd_link_hash_defined:
3130             case bfd_link_hash_defweak:
3131               return h->root.u.def.section;
3132
3133             case bfd_link_hash_common:
3134               return h->root.u.c.p->section;
3135
3136             default:
3137               break;
3138             }
3139         }
3140     }
3141   else
3142     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3143
3144   return NULL;
3145 }
3146
3147 /* Update the got, plt and dynamic reloc reference counts for the
3148    section being removed.  */
3149
3150 static bfd_boolean
3151 ppc_elf_gc_sweep_hook (bfd *abfd,
3152                        struct bfd_link_info *info,
3153                        asection *sec,
3154                        const Elf_Internal_Rela *relocs)
3155 {
3156   struct ppc_elf_link_hash_table *htab;
3157   Elf_Internal_Shdr *symtab_hdr;
3158   struct elf_link_hash_entry **sym_hashes;
3159   bfd_signed_vma *local_got_refcounts;
3160   const Elf_Internal_Rela *rel, *relend;
3161
3162   if ((sec->flags & SEC_ALLOC) == 0)
3163     return TRUE;
3164
3165   elf_section_data (sec)->local_dynrel = NULL;
3166
3167   htab = ppc_elf_hash_table (info);
3168   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3169   sym_hashes = elf_sym_hashes (abfd);
3170   local_got_refcounts = elf_local_got_refcounts (abfd);
3171
3172   relend = relocs + sec->reloc_count;
3173   for (rel = relocs; rel < relend; rel++)
3174     {
3175       unsigned long r_symndx;
3176       enum elf_ppc_reloc_type r_type;
3177       struct elf_link_hash_entry *h = NULL;
3178
3179       r_symndx = ELF32_R_SYM (rel->r_info);
3180       if (r_symndx >= symtab_hdr->sh_info)
3181         {
3182           struct ppc_elf_dyn_relocs **pp, *p;
3183           struct ppc_elf_link_hash_entry *eh;
3184
3185           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3186           while (h->root.type == bfd_link_hash_indirect
3187                  || h->root.type == bfd_link_hash_warning)
3188             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3189           eh = (struct ppc_elf_link_hash_entry *) h;
3190
3191           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3192             if (p->sec == sec)
3193               {
3194                 /* Everything must go for SEC.  */
3195                 *pp = p->next;
3196                 break;
3197               }
3198         }
3199
3200       r_type = ELF32_R_TYPE (rel->r_info);
3201       switch (r_type)
3202         {
3203         case R_PPC_GOT_TLSLD16:
3204         case R_PPC_GOT_TLSLD16_LO:
3205         case R_PPC_GOT_TLSLD16_HI:
3206         case R_PPC_GOT_TLSLD16_HA:
3207           htab->tlsld_got.refcount -= 1;
3208           /* Fall thru */
3209
3210         case R_PPC_GOT_TLSGD16:
3211         case R_PPC_GOT_TLSGD16_LO:
3212         case R_PPC_GOT_TLSGD16_HI:
3213         case R_PPC_GOT_TLSGD16_HA:
3214         case R_PPC_GOT_TPREL16:
3215         case R_PPC_GOT_TPREL16_LO:
3216         case R_PPC_GOT_TPREL16_HI:
3217         case R_PPC_GOT_TPREL16_HA:
3218         case R_PPC_GOT_DTPREL16:
3219         case R_PPC_GOT_DTPREL16_LO:
3220         case R_PPC_GOT_DTPREL16_HI:
3221         case R_PPC_GOT_DTPREL16_HA:
3222         case R_PPC_GOT16:
3223         case R_PPC_GOT16_LO:
3224         case R_PPC_GOT16_HI:
3225         case R_PPC_GOT16_HA:
3226           if (h != NULL)
3227             {
3228               if (h->got.refcount > 0)
3229                 h->got.refcount--;
3230             }
3231           else if (local_got_refcounts != NULL)
3232             {
3233               if (local_got_refcounts[r_symndx] > 0)
3234                 local_got_refcounts[r_symndx]--;
3235             }
3236           break;
3237
3238         case R_PPC_REL24:
3239         case R_PPC_REL14:
3240         case R_PPC_REL14_BRTAKEN:
3241         case R_PPC_REL14_BRNTAKEN:
3242         case R_PPC_REL32:
3243           if (h == NULL
3244               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3245             break;
3246           /* Fall thru */
3247
3248         case R_PPC_ADDR32:
3249         case R_PPC_ADDR24:
3250         case R_PPC_ADDR16:
3251         case R_PPC_ADDR16_LO:
3252         case R_PPC_ADDR16_HI:
3253         case R_PPC_ADDR16_HA:
3254         case R_PPC_ADDR14:
3255         case R_PPC_ADDR14_BRTAKEN:
3256         case R_PPC_ADDR14_BRNTAKEN:
3257         case R_PPC_UADDR32:
3258         case R_PPC_UADDR16:
3259         case R_PPC_PLT32:
3260         case R_PPC_PLTREL24:
3261         case R_PPC_PLT16_LO:
3262         case R_PPC_PLT16_HI:
3263         case R_PPC_PLT16_HA:
3264           if (h != NULL)
3265             {
3266               if (h->plt.refcount > 0)
3267                 h->plt.refcount--;
3268             }
3269           break;
3270
3271         default:
3272           break;
3273         }
3274     }
3275   return TRUE;
3276 }
3277 \f
3278 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
3279
3280 asection *
3281 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3282 {
3283   struct ppc_elf_link_hash_table *htab;
3284
3285   htab = ppc_elf_hash_table (info);
3286   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3287                                              FALSE, FALSE, TRUE);
3288
3289   return _bfd_elf_tls_setup (obfd, info);
3290 }
3291
3292 /* Run through all the TLS relocs looking for optimization
3293    opportunities.  */
3294
3295 bfd_boolean
3296 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
3297                       struct bfd_link_info *info)
3298 {
3299   bfd *ibfd;
3300   asection *sec;
3301   struct ppc_elf_link_hash_table *htab;
3302
3303   if (info->relocatable || info->shared)
3304     return TRUE;
3305
3306   htab = ppc_elf_hash_table (info);
3307   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3308     {
3309       Elf_Internal_Sym *locsyms = NULL;
3310       Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3311
3312       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3313         if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3314           {
3315             Elf_Internal_Rela *relstart, *rel, *relend;
3316             int expecting_tls_get_addr;
3317
3318             /* Read the relocations.  */
3319             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
3320                                                   info->keep_memory);
3321             if (relstart == NULL)
3322               return FALSE;
3323
3324             expecting_tls_get_addr = 0;
3325             relend = relstart + sec->reloc_count;
3326             for (rel = relstart; rel < relend; rel++)
3327               {
3328                 enum elf_ppc_reloc_type r_type;
3329                 unsigned long r_symndx;
3330                 struct elf_link_hash_entry *h = NULL;
3331                 char *tls_mask;
3332                 char tls_set, tls_clear;
3333                 bfd_boolean is_local;
3334
3335                 r_symndx = ELF32_R_SYM (rel->r_info);
3336                 if (r_symndx >= symtab_hdr->sh_info)
3337                   {
3338                     struct elf_link_hash_entry **sym_hashes;
3339
3340                     sym_hashes = elf_sym_hashes (ibfd);
3341                     h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3342                     while (h->root.type == bfd_link_hash_indirect
3343                            || h->root.type == bfd_link_hash_warning)
3344                       h = (struct elf_link_hash_entry *) h->root.u.i.link;
3345                   }
3346
3347                 is_local = FALSE;
3348                 if (h == NULL
3349                     || !h->def_dynamic)
3350                   is_local = TRUE;
3351
3352                 r_type = ELF32_R_TYPE (rel->r_info);
3353                 switch (r_type)
3354                   {
3355                   case R_PPC_GOT_TLSLD16:
3356                   case R_PPC_GOT_TLSLD16_LO:
3357                   case R_PPC_GOT_TLSLD16_HI:
3358                   case R_PPC_GOT_TLSLD16_HA:
3359                     /* These relocs should never be against a symbol
3360                        defined in a shared lib.  Leave them alone if
3361                        that turns out to be the case.  */
3362                     expecting_tls_get_addr = 0;
3363                     htab->tlsld_got.refcount -= 1;
3364                     if (!is_local)
3365                       continue;
3366
3367                     /* LD -> LE */
3368                     tls_set = 0;
3369                     tls_clear = TLS_LD;
3370                     expecting_tls_get_addr = 1;
3371                     break;
3372
3373                   case R_PPC_GOT_TLSGD16:
3374                   case R_PPC_GOT_TLSGD16_LO:
3375                   case R_PPC_GOT_TLSGD16_HI:
3376                   case R_PPC_GOT_TLSGD16_HA:
3377                     if (is_local)
3378                       /* GD -> LE */
3379                       tls_set = 0;
3380                     else
3381                       /* GD -> IE */
3382                       tls_set = TLS_TLS | TLS_TPRELGD;
3383                     tls_clear = TLS_GD;
3384                     expecting_tls_get_addr = 1;
3385                     break;
3386
3387                   case R_PPC_GOT_TPREL16:
3388                   case R_PPC_GOT_TPREL16_LO:
3389                   case R_PPC_GOT_TPREL16_HI:
3390                   case R_PPC_GOT_TPREL16_HA:
3391                     expecting_tls_get_addr = 0;
3392                     if (is_local)
3393                       {
3394                         /* IE -> LE */
3395                         tls_set = 0;
3396                         tls_clear = TLS_TPREL;
3397                         break;
3398                       }
3399                     else
3400                       continue;
3401
3402                   case R_PPC_REL14:
3403                   case R_PPC_REL14_BRTAKEN:
3404                   case R_PPC_REL14_BRNTAKEN:
3405                   case R_PPC_REL24:
3406                     if (expecting_tls_get_addr
3407                         && h != NULL
3408                         && h == htab->tls_get_addr)
3409                       {
3410                         if (h->plt.refcount > 0)
3411                           h->plt.refcount -= 1;
3412                       }
3413                     expecting_tls_get_addr = 0;
3414                     continue;
3415
3416                   default:
3417                     expecting_tls_get_addr = 0;
3418                     continue;
3419                   }
3420
3421                 if (h != NULL)
3422                   {
3423                     if (tls_set == 0)
3424                       {
3425                         /* We managed to get rid of a got entry.  */
3426                         if (h->got.refcount > 0)
3427                           h->got.refcount -= 1;
3428                       }
3429                     tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3430                   }
3431                 else
3432                   {
3433                     Elf_Internal_Sym *sym;
3434                     bfd_signed_vma *lgot_refs;
3435                     char *lgot_masks;
3436
3437                     if (locsyms == NULL)
3438                       {
3439                         locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3440                         if (locsyms == NULL)
3441                           locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3442                                                           symtab_hdr->sh_info,
3443                                                           0, NULL, NULL, NULL);
3444                         if (locsyms == NULL)
3445                           {
3446                             if (elf_section_data (sec)->relocs != relstart)
3447                               free (relstart);
3448                             return FALSE;
3449                           }
3450                       }
3451                     sym = locsyms + r_symndx;
3452                     lgot_refs = elf_local_got_refcounts (ibfd);
3453                     if (lgot_refs == NULL)
3454                       abort ();
3455                     if (tls_set == 0)
3456                       {
3457                         /* We managed to get rid of a got entry.  */
3458                         if (lgot_refs[r_symndx] > 0)
3459                           lgot_refs[r_symndx] -= 1;
3460                       }
3461                     lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
3462                     tls_mask = &lgot_masks[r_symndx];
3463                   }
3464
3465                 *tls_mask |= tls_set;
3466                 *tls_mask &= ~tls_clear;
3467               }
3468
3469             if (elf_section_data (sec)->relocs != relstart)
3470               free (relstart);
3471           }
3472
3473       if (locsyms != NULL
3474           && (symtab_hdr->contents != (unsigned char *) locsyms))
3475         {
3476           if (!info->keep_memory)
3477             free (locsyms);
3478           else
3479             symtab_hdr->contents = (unsigned char *) locsyms;
3480         }
3481     }
3482   return TRUE;
3483 }
3484 \f
3485 /* Adjust a symbol defined by a dynamic object and referenced by a
3486    regular object.  The current definition is in some section of the
3487    dynamic object, but we're not including those sections.  We have to
3488    change the definition to something the rest of the link can
3489    understand.  */
3490
3491 static bfd_boolean
3492 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3493                                struct elf_link_hash_entry *h)
3494 {
3495   struct ppc_elf_link_hash_table *htab;
3496   asection *s;
3497   unsigned int power_of_two;
3498
3499 #ifdef DEBUG
3500   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
3501            h->root.root.string);
3502 #endif
3503
3504   /* Make sure we know what is going on here.  */
3505   htab = ppc_elf_hash_table (info);
3506   BFD_ASSERT (htab->elf.dynobj != NULL
3507               && (h->needs_plt
3508                   || h->u.weakdef != NULL
3509                   || (h->def_dynamic
3510                       && h->ref_regular
3511                       && !h->def_regular)));
3512
3513   /* Deal with function syms.  */
3514   if (h->type == STT_FUNC
3515       || h->needs_plt)
3516     {
3517       /* Clear procedure linkage table information for any symbol that
3518          won't need a .plt entry.  */
3519       if (h->plt.refcount <= 0
3520           || SYMBOL_CALLS_LOCAL (info, h)
3521           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3522               && h->root.type == bfd_link_hash_undefweak))
3523         {
3524           /* A PLT entry is not required/allowed when:
3525
3526              1. We are not using ld.so; because then the PLT entry
3527              can't be set up, so we can't use one.  In this case,
3528              ppc_elf_adjust_dynamic_symbol won't even be called.
3529
3530              2. GC has rendered the entry unused.
3531
3532              3. We know for certain that a call to this symbol
3533              will go to this object, or will remain undefined.  */
3534           h->plt.offset = (bfd_vma) -1;
3535           h->needs_plt = 0;
3536         }
3537       return TRUE;
3538     }
3539   else
3540     h->plt.offset = (bfd_vma) -1;
3541
3542   /* If this is a weak symbol, and there is a real definition, the
3543      processor independent code will have arranged for us to see the
3544      real definition first, and we can just use the same value.  */
3545   if (h->u.weakdef != NULL)
3546     {
3547       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3548                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
3549       h->root.u.def.section = h->u.weakdef->root.u.def.section;
3550       h->root.u.def.value = h->u.weakdef->root.u.def.value;
3551       if (ELIMINATE_COPY_RELOCS)
3552         h->non_got_ref = h->u.weakdef->non_got_ref;
3553       return TRUE;
3554     }
3555
3556   /* This is a reference to a symbol defined by a dynamic object which
3557      is not a function.  */
3558
3559   /* If we are creating a shared library, we must presume that the
3560      only references to the symbol are via the global offset table.
3561      For such cases we need not do anything here; the relocations will
3562      be handled correctly by relocate_section.  */
3563   if (info->shared)
3564     return TRUE;
3565
3566   /* If there are no references to this symbol that do not use the
3567      GOT, we don't need to generate a copy reloc.  */
3568   if (!h->non_got_ref)
3569     return TRUE;
3570
3571   if (ELIMINATE_COPY_RELOCS)
3572     {
3573       struct ppc_elf_dyn_relocs *p;
3574       for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3575         {
3576           s = p->sec->output_section;
3577           if (s != NULL && (s->flags & SEC_READONLY) != 0)
3578             break;
3579         }
3580
3581       /* If we didn't find any dynamic relocs in read-only sections, then
3582          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
3583       if (p == NULL)
3584         {
3585           h->non_got_ref = 0;
3586           return TRUE;
3587         }
3588     }
3589
3590   /* We must allocate the symbol in our .dynbss section, which will
3591      become part of the .bss section of the executable.  There will be
3592      an entry for this symbol in the .dynsym section.  The dynamic
3593      object will contain position independent code, so all references
3594      from the dynamic object to this symbol will go through the global
3595      offset table.  The dynamic linker will use the .dynsym entry to
3596      determine the address it must put in the global offset table, so
3597      both the dynamic object and the regular object will refer to the
3598      same memory location for the variable.
3599
3600      Of course, if the symbol is sufficiently small, we must instead
3601      allocate it in .sbss.  FIXME: It would be better to do this if and
3602      only if there were actually SDAREL relocs for that symbol.  */
3603
3604   if (h->size <= elf_gp_size (htab->elf.dynobj))
3605     s = htab->dynsbss;
3606   else
3607     s = htab->dynbss;
3608   BFD_ASSERT (s != NULL);
3609
3610   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
3611      copy the initial value out of the dynamic object and into the
3612      runtime process image.  We need to remember the offset into the
3613      .rela.bss section we are going to use.  */
3614   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3615     {
3616       asection *srel;
3617
3618       if (h->size <= elf_gp_size (htab->elf.dynobj))
3619         srel = htab->relsbss;
3620       else
3621         srel = htab->relbss;
3622       BFD_ASSERT (srel != NULL);
3623       srel->size += sizeof (Elf32_External_Rela);
3624       h->needs_copy = 1;
3625     }
3626
3627   /* We need to figure out the alignment required for this symbol.  I
3628      have no idea how ELF linkers handle this.  */
3629   power_of_two = bfd_log2 (h->size);
3630   if (power_of_two > 4)
3631     power_of_two = 4;
3632
3633   /* Apply the required alignment.  */
3634   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3635   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3636     {
3637       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3638         return FALSE;
3639     }
3640
3641   /* Define the symbol as being at this point in the section.  */
3642   h->root.u.def.section = s;
3643   h->root.u.def.value = s->size;
3644
3645   /* Increment the section size to make room for the symbol.  */
3646   s->size += h->size;
3647
3648   return TRUE;
3649 }
3650 \f
3651 /* Allocate space in associated reloc sections for dynamic relocs.  */
3652
3653 static bfd_boolean
3654 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3655 {
3656   struct bfd_link_info *info = inf;
3657   struct ppc_elf_link_hash_entry *eh;
3658   struct ppc_elf_link_hash_table *htab;
3659   struct ppc_elf_dyn_relocs *p;
3660
3661   if (h->root.type == bfd_link_hash_indirect)
3662     return TRUE;
3663
3664   if (h->root.type == bfd_link_hash_warning)
3665     /* When warning symbols are created, they **replace** the "real"
3666        entry in the hash table, thus we never get to see the real
3667        symbol in a hash traversal.  So look at it now.  */
3668     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3669
3670   htab = ppc_elf_hash_table (info);
3671   if (htab->elf.dynamic_sections_created
3672       && h->plt.refcount > 0)
3673     {
3674       /* Make sure this symbol is output as a dynamic symbol.  */
3675       if (h->dynindx == -1
3676           && !h->forced_local)
3677         {
3678           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3679             return FALSE;
3680         }
3681
3682       if (info->shared
3683           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3684         {
3685           asection *s = htab->plt;
3686
3687           /* If this is the first .plt entry, make room for the special
3688              first entry.  */
3689           if (s->size == 0)
3690             s->size += PLT_INITIAL_ENTRY_SIZE;
3691
3692           /* The PowerPC PLT is actually composed of two parts, the
3693              first part is 2 words (for a load and a jump), and then
3694              there is a remaining word available at the end.  */
3695           h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3696                            + (PLT_SLOT_SIZE
3697                               * ((s->size - PLT_INITIAL_ENTRY_SIZE)
3698                                  / PLT_ENTRY_SIZE)));
3699
3700           /* If this symbol is not defined in a regular file, and we
3701              are not generating a shared library, then set the symbol
3702              to this location in the .plt.  This is required to make
3703              function pointers compare as equal between the normal
3704              executable and the shared library.  */
3705           if (! info->shared
3706               && !h->def_regular)
3707             {
3708               h->root.u.def.section = s;
3709               h->root.u.def.value = h->plt.offset;
3710             }
3711
3712           /* Make room for this entry.  After the 8192nd entry, room
3713              for two entries is allocated.  */
3714           s->size += PLT_ENTRY_SIZE;
3715           if ((s->size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3716               > PLT_NUM_SINGLE_ENTRIES)
3717             s->size += PLT_ENTRY_SIZE;
3718
3719           /* We also need to make an entry in the .rela.plt section.  */
3720           htab->relplt->size += sizeof (Elf32_External_Rela);
3721         }
3722       else
3723         {
3724           h->plt.offset = (bfd_vma) -1;
3725           h->needs_plt = 0;
3726         }
3727     }
3728   else
3729     {
3730       h->plt.offset = (bfd_vma) -1;
3731       h->needs_plt = 0;
3732     }
3733
3734   eh = (struct ppc_elf_link_hash_entry *) h;
3735   if (eh->elf.got.refcount > 0)
3736     {
3737       /* Make sure this symbol is output as a dynamic symbol.  */
3738       if (eh->elf.dynindx == -1
3739           && !eh->elf.forced_local)
3740         {
3741           if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
3742             return FALSE;
3743         }
3744
3745       if (eh->tls_mask == (TLS_TLS | TLS_LD)
3746           && !eh->elf.def_dynamic)
3747         /* If just an LD reloc, we'll just use htab->tlsld_got.offset.  */
3748         eh->elf.got.offset = (bfd_vma) -1;
3749       else
3750         {
3751           bfd_boolean dyn;
3752           eh->elf.got.offset = htab->got->size;
3753           if ((eh->tls_mask & TLS_TLS) != 0)
3754             {
3755               if ((eh->tls_mask & TLS_LD) != 0)
3756                 htab->got->size += 8;
3757               if ((eh->tls_mask & TLS_GD) != 0)
3758                 htab->got->size += 8;
3759               if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
3760                 htab->got->size += 4;
3761               if ((eh->tls_mask & TLS_DTPREL) != 0)
3762                 htab->got->size += 4;
3763             }
3764           else
3765             htab->got->size += 4;
3766           dyn = htab->elf.dynamic_sections_created;
3767           if ((info->shared
3768                || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
3769               && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
3770                   || eh->elf.root.type != bfd_link_hash_undefweak))
3771             {
3772               /* All the entries we allocated need relocs.  */
3773               htab->relgot->size
3774                 += ((htab->got->size - eh->elf.got.offset) / 4
3775                     * sizeof (Elf32_External_Rela));
3776               /* Except LD only needs one.  */
3777               if ((eh->tls_mask & TLS_LD) != 0)
3778                 htab->relgot->size -= sizeof (Elf32_External_Rela);
3779             }
3780         }
3781     }
3782   else
3783     eh->elf.got.offset = (bfd_vma) -1;
3784
3785   if (eh->dyn_relocs == NULL)
3786     return TRUE;
3787
3788   /* In the shared -Bsymbolic case, discard space allocated for
3789      dynamic pc-relative relocs against symbols which turn out to be
3790      defined in regular objects.  For the normal shared case, discard
3791      space for relocs that have become local due to symbol visibility
3792      changes.  */
3793
3794   if (info->shared)
3795     {
3796       /* Relocs that use pc_count are those that appear on a call insn,
3797          or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
3798          generated via assembly.  We want calls to protected symbols to
3799          resolve directly to the function rather than going via the plt.
3800          If people want function pointer comparisons to work as expected
3801          then they should avoid writing weird assembly.  */
3802       if (SYMBOL_CALLS_LOCAL (info, h))
3803         {
3804           struct ppc_elf_dyn_relocs **pp;
3805
3806           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3807             {
3808               p->count -= p->pc_count;
3809               p->pc_count = 0;
3810               if (p->count == 0)
3811                 *pp = p->next;
3812               else
3813                 pp = &p->next;
3814             }
3815         }
3816
3817       /* Also discard relocs on undefined weak syms with non-default
3818          visibility.  */
3819       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3820           && h->root.type == bfd_link_hash_undefweak)
3821         eh->dyn_relocs = NULL;
3822
3823       /* Make sure undefined weak symbols are output as a dynamic symbol
3824          in PIEs.  */
3825       if (info->pie
3826           && eh->dyn_relocs != NULL
3827           && h->dynindx == -1
3828           && h->root.type == bfd_link_hash_undefweak
3829           && !h->forced_local)
3830         {
3831           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3832             return FALSE;
3833         }
3834     }
3835   else if (ELIMINATE_COPY_RELOCS)
3836     {
3837       /* For the non-shared case, discard space for relocs against
3838          symbols which turn out to need copy relocs or are not
3839          dynamic.  */
3840
3841       if (!h->non_got_ref
3842           && h->def_dynamic
3843           && !h->def_regular)
3844         {
3845           /* Make sure this symbol is output as a dynamic symbol.
3846              Undefined weak syms won't yet be marked as dynamic.  */
3847           if (h->dynindx == -1
3848               && !h->forced_local)
3849             {
3850               if (! bfd_elf_link_record_dynamic_symbol (info, h))
3851                 return FALSE;
3852             }
3853
3854           /* If that succeeded, we know we'll be keeping all the
3855              relocs.  */
3856           if (h->dynindx != -1)
3857             goto keep;
3858         }
3859
3860       eh->dyn_relocs = NULL;
3861
3862     keep: ;
3863     }
3864
3865   /* Finally, allocate space.  */
3866   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3867     {
3868       asection *sreloc = elf_section_data (p->sec)->sreloc;
3869       sreloc->size += p->count * sizeof (Elf32_External_Rela);
3870     }
3871
3872   return TRUE;
3873 }
3874
3875 /* Find any dynamic relocs that apply to read-only sections.  */
3876
3877 static bfd_boolean
3878 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
3879 {
3880   struct ppc_elf_dyn_relocs *p;
3881
3882   if (h->root.type == bfd_link_hash_indirect)
3883     return TRUE;
3884
3885   if (h->root.type == bfd_link_hash_warning)
3886     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3887
3888   for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3889     {
3890       asection *s = p->sec->output_section;
3891
3892       if (s != NULL
3893           && ((s->flags & (SEC_READONLY | SEC_ALLOC))
3894               == (SEC_READONLY | SEC_ALLOC)))
3895         {
3896           ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
3897
3898           /* Not an error, just cut short the traversal.  */
3899           return FALSE;
3900         }
3901     }
3902   return TRUE;
3903 }
3904
3905 /* Set the sizes of the dynamic sections.  */
3906
3907 static bfd_boolean
3908 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3909                                struct bfd_link_info *info)
3910 {
3911   struct ppc_elf_link_hash_table *htab;
3912   asection *s;
3913   bfd_boolean relocs;
3914   bfd *ibfd;
3915
3916 #ifdef DEBUG
3917   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
3918 #endif
3919
3920   htab = ppc_elf_hash_table (info);
3921   BFD_ASSERT (htab->elf.dynobj != NULL);
3922
3923   if (elf_hash_table (info)->dynamic_sections_created)
3924     {
3925       /* Set the contents of the .interp section to the interpreter.  */
3926       if (info->executable)
3927         {
3928           s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
3929           BFD_ASSERT (s != NULL);
3930           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3931           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3932         }
3933     }
3934
3935   if (htab->tlsld_got.refcount > 0)
3936     {
3937       htab->tlsld_got.offset = htab->got->size;
3938       htab->got->size += 8;
3939       if (info->shared)
3940         htab->relgot->size += sizeof (Elf32_External_Rela);
3941     }
3942   else
3943     htab->tlsld_got.offset = (bfd_vma) -1;
3944
3945   /* Set up .got offsets for local syms, and space for local dynamic
3946      relocs.  */
3947   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3948     {
3949       bfd_signed_vma *local_got;
3950       bfd_signed_vma *end_local_got;
3951       char *lgot_masks;
3952       bfd_size_type locsymcount;
3953       Elf_Internal_Shdr *symtab_hdr;
3954       asection *srel;
3955
3956       if (!is_ppc_elf_target (ibfd->xvec))
3957         continue;
3958
3959       for (s = ibfd->sections; s != NULL; s = s->next)
3960         {
3961           struct ppc_elf_dyn_relocs *p;
3962
3963           for (p = ((struct ppc_elf_dyn_relocs *)
3964                     elf_section_data (s)->local_dynrel);
3965                p != NULL;
3966                p = p->next)
3967             {
3968               if (!bfd_is_abs_section (p->sec)
3969                   && bfd_is_abs_section (p->sec->output_section))
3970                 {
3971                   /* Input section has been discarded, either because
3972                      it is a copy of a linkonce section or due to
3973                      linker script /DISCARD/, so we'll be discarding
3974                      the relocs too.  */
3975                 }
3976               else if (p->count != 0)
3977                 {
3978                   elf_section_data (p->sec)->sreloc->size
3979                     += p->count * sizeof (Elf32_External_Rela);
3980                   if ((p->sec->output_section->flags
3981                        & (SEC_READONLY | SEC_ALLOC))
3982                       == (SEC_READONLY | SEC_ALLOC))
3983                     info->flags |= DF_TEXTREL;
3984                 }
3985             }
3986         }
3987
3988       local_got = elf_local_got_refcounts (ibfd);
3989       if (!local_got)
3990         continue;
3991
3992       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3993       locsymcount = symtab_hdr->sh_info;
3994       end_local_got = local_got + locsymcount;
3995       lgot_masks = (char *) end_local_got;
3996       s = htab->got;
3997       srel = htab->relgot;
3998       for (; local_got < end_local_got; ++local_got, ++lgot_masks)
3999         if (*local_got > 0)
4000           {
4001             if (*lgot_masks == (TLS_TLS | TLS_LD))
4002               {
4003                 /* If just an LD reloc, we'll just use
4004                    htab->tlsld_got.offset.  */
4005                 if (htab->tlsld_got.offset == (bfd_vma) -1)
4006                   {
4007                     htab->tlsld_got.offset = s->size;
4008                     s->size += 8;
4009                     if (info->shared)
4010                       srel->size += sizeof (Elf32_External_Rela);
4011                   }
4012                 *local_got = (bfd_vma) -1;
4013               }
4014             else
4015               {
4016                 *local_got = s->size;
4017                 if ((*lgot_masks & TLS_TLS) != 0)
4018                   {
4019                     if ((*lgot_masks & TLS_GD) != 0)
4020                       s->size += 8;
4021                     if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
4022                       s->size += 4;
4023                     if ((*lgot_masks & TLS_DTPREL) != 0)
4024                       s->size += 4;
4025                   }
4026                 else
4027                   s->size += 4;
4028                 if (info->shared)
4029                   srel->size += ((s->size - *local_got) / 4
4030                                  * sizeof (Elf32_External_Rela));
4031               }
4032           }
4033         else
4034           *local_got = (bfd_vma) -1;
4035     }
4036
4037   /* Allocate space for global sym dynamic relocs.  */
4038   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
4039
4040   /* We've now determined the sizes of the various dynamic sections.
4041      Allocate memory for them.  */
4042   relocs = FALSE;
4043   for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
4044     {
4045       if ((s->flags & SEC_LINKER_CREATED) == 0)
4046         continue;
4047
4048       if (s == htab->plt
4049           || s == htab->got
4050           || s == htab->sbss)
4051         {
4052           /* Strip this section if we don't need it; see the
4053              comment below.  */
4054         }
4055       else if (s == htab->sdata[0].section
4056                || s == htab->sdata[1].section)
4057         {
4058           /* Strip these too.  */
4059         }
4060       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4061         {
4062           if (s->size == 0)
4063             {
4064               /* If we don't need this section, strip it from the
4065                  output file.  This is mostly to handle .rela.bss and
4066                  .rela.plt.  We must create both sections in
4067                  create_dynamic_sections, because they must be created
4068                  before the linker maps input sections to output
4069                  sections.  The linker does that before
4070                  adjust_dynamic_symbol is called, and it is that
4071                  function which decides whether anything needs to go
4072                  into these sections.  */
4073             }
4074           else
4075             {
4076               /* Remember whether there are any relocation sections.  */
4077               relocs = TRUE;
4078
4079               /* We use the reloc_count field as a counter if we need
4080                  to copy relocs into the output file.  */
4081               s->reloc_count = 0;
4082             }
4083         }
4084       else
4085         {
4086           /* It's not one of our sections, so don't allocate space.  */
4087           continue;
4088         }
4089
4090       if (s->size == 0)
4091         {
4092           s->flags |= SEC_EXCLUDE;
4093           continue;
4094         }
4095
4096       if (s == htab->sbss)
4097         continue;
4098
4099       /* Allocate memory for the section contents.  */
4100       s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
4101       if (s->contents == NULL)
4102         return FALSE;
4103     }
4104
4105   if (htab->elf.dynamic_sections_created)
4106     {
4107       /* Add some entries to the .dynamic section.  We fill in the
4108          values later, in ppc_elf_finish_dynamic_sections, but we
4109          must add the entries now so that we get the correct size for
4110          the .dynamic section.  The DT_DEBUG entry is filled in by the
4111          dynamic linker and used by the debugger.  */
4112 #define add_dynamic_entry(TAG, VAL) \
4113   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4114
4115       if (info->executable)
4116         {
4117           if (!add_dynamic_entry (DT_DEBUG, 0))
4118             return FALSE;
4119         }
4120
4121       if (htab->plt != NULL && htab->plt->size != 0)
4122         {
4123           if (!add_dynamic_entry (DT_PLTGOT, 0)
4124               || !add_dynamic_entry (DT_PLTRELSZ, 0)
4125               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4126               || !add_dynamic_entry (DT_JMPREL, 0))
4127             return FALSE;
4128         }
4129
4130       if (relocs)
4131         {
4132           if (!add_dynamic_entry (DT_RELA, 0)
4133               || !add_dynamic_entry (DT_RELASZ, 0)
4134               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4135             return FALSE;
4136         }
4137
4138       /* If any dynamic relocs apply to a read-only section, then we
4139          need a DT_TEXTREL entry.  */
4140       if ((info->flags & DF_TEXTREL) == 0)
4141         elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
4142                                 info);
4143
4144       if ((info->flags & DF_TEXTREL) != 0)
4145         {
4146           if (!add_dynamic_entry (DT_TEXTREL, 0))
4147             return FALSE;
4148         }
4149     }
4150 #undef add_dynamic_entry
4151
4152   return TRUE;
4153 }
4154 \f
4155 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
4156
4157 static const int shared_stub_entry[] =
4158   {
4159     0x7c0802a6, /* mflr 0 */
4160     0x429f0005, /* bcl 20, 31, .Lxxx */
4161     0x7d6802a6, /* mflr 11 */
4162     0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
4163     0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
4164     0x7c0803a6, /* mtlr 0 */
4165     0x7d6903a6, /* mtctr 11 */
4166     0x4e800420, /* bctr */
4167   };
4168
4169 static const int stub_entry[] =
4170   {
4171     0x3d600000, /* lis 11,xxx@ha */
4172     0x396b0000, /* addi 11,11,xxx@l */
4173     0x7d6903a6, /* mtctr 11 */
4174     0x4e800420, /* bctr */
4175   };
4176
4177 static bfd_boolean
4178 ppc_elf_relax_section (bfd *abfd,
4179                        asection *isec,
4180                        struct bfd_link_info *link_info,
4181                        bfd_boolean *again)
4182 {
4183   struct one_fixup
4184   {
4185     struct one_fixup *next;
4186     asection *tsec;
4187     bfd_vma toff;
4188     bfd_vma trampoff;
4189   };
4190
4191   Elf_Internal_Shdr *symtab_hdr;
4192   bfd_byte *contents = NULL;
4193   Elf_Internal_Sym *isymbuf = NULL;
4194   Elf_Internal_Rela *internal_relocs = NULL;
4195   Elf_Internal_Rela *irel, *irelend;
4196   struct one_fixup *fixups = NULL;
4197   bfd_boolean changed;
4198   struct ppc_elf_link_hash_table *ppc_info;
4199   bfd_size_type trampoff;
4200
4201   *again = FALSE;
4202
4203   /* Nothing to do if there are no relocations, and no need to do
4204      anything with non-alloc sections.  */
4205   if ((isec->flags & SEC_ALLOC) == 0
4206       || (isec->flags & SEC_RELOC) == 0
4207       || isec->reloc_count == 0)
4208     return TRUE;
4209
4210   trampoff = (isec->size + 3) & (bfd_vma) -4;
4211   /* Space for a branch around any trampolines.  */
4212   trampoff += 4;
4213
4214   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4215
4216   /* Get a copy of the native relocations.  */
4217   internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
4218                                                link_info->keep_memory);
4219   if (internal_relocs == NULL)
4220     goto error_return;
4221
4222   ppc_info = ppc_elf_hash_table (link_info);
4223   irelend = internal_relocs + isec->reloc_count;
4224
4225   for (irel = internal_relocs; irel < irelend; irel++)
4226     {
4227       unsigned long r_type = ELF32_R_TYPE (irel->r_info);
4228       bfd_vma symaddr, reladdr, toff, roff;
4229       asection *tsec;
4230       struct one_fixup *f;
4231       size_t insn_offset = 0;
4232       bfd_vma max_branch_offset, val;
4233       bfd_byte *hit_addr;
4234       unsigned long t0;
4235       unsigned char sym_type;
4236
4237       switch (r_type)
4238         {
4239         case R_PPC_REL24:
4240         case R_PPC_LOCAL24PC:
4241         case R_PPC_PLTREL24:
4242           max_branch_offset = 1 << 25;
4243           break;
4244
4245         case R_PPC_REL14:
4246         case R_PPC_REL14_BRTAKEN:
4247         case R_PPC_REL14_BRNTAKEN:
4248           max_branch_offset = 1 << 15;
4249           break;
4250
4251         default:
4252           continue;
4253         }
4254
4255       /* Get the value of the symbol referred to by the reloc.  */
4256       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
4257         {
4258           /* A local symbol.  */
4259           Elf_Internal_Sym *isym;
4260
4261           /* Read this BFD's local symbols.  */
4262           if (isymbuf == NULL)
4263             {
4264               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4265               if (isymbuf == NULL)
4266                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4267                                                 symtab_hdr->sh_info, 0,
4268                                                 NULL, NULL, NULL);
4269               if (isymbuf == 0)
4270                 goto error_return;
4271             }
4272           isym = isymbuf + ELF32_R_SYM (irel->r_info);
4273           if (isym->st_shndx == SHN_UNDEF)
4274             continue;   /* We can't do anything with undefined symbols.  */
4275           else if (isym->st_shndx == SHN_ABS)
4276             tsec = bfd_abs_section_ptr;
4277           else if (isym->st_shndx == SHN_COMMON)
4278             tsec = bfd_com_section_ptr;
4279           else
4280             tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4281
4282           toff = isym->st_value;
4283           sym_type = ELF_ST_TYPE (isym->st_info);
4284         }
4285       else
4286         {
4287           /* Global symbol handling.  */
4288           unsigned long indx;
4289           struct elf_link_hash_entry *h;
4290
4291           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
4292           h = elf_sym_hashes (abfd)[indx];
4293
4294           while (h->root.type == bfd_link_hash_indirect
4295                  || h->root.type == bfd_link_hash_warning)
4296             h = (struct elf_link_hash_entry *) h->root.u.i.link;
4297
4298           if (r_type == R_PPC_PLTREL24
4299               && ppc_info->plt != NULL
4300               && h->plt.offset != (bfd_vma) -1)
4301             {
4302               tsec = ppc_info->plt;
4303               toff = h->plt.offset;
4304             }
4305           else if (h->root.type == bfd_link_hash_defined
4306                    || h->root.type == bfd_link_hash_defweak)
4307             {
4308               tsec = h->root.u.def.section;
4309               toff = h->root.u.def.value;
4310             }
4311           else
4312             continue;
4313
4314           sym_type = h->type;
4315         }
4316
4317       /* If the branch and target are in the same section, you have
4318          no hope of adding stubs.  We'll error out later should the
4319          branch overflow.  */
4320       if (tsec == isec)
4321         continue;
4322
4323       /* There probably isn't any reason to handle symbols in
4324          SEC_MERGE sections;  SEC_MERGE doesn't seem a likely
4325          attribute for a code section, and we are only looking at
4326          branches.  However, implement it correctly here as a
4327          reference for other target relax_section functions.  */
4328       if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
4329         {
4330           /* At this stage in linking, no SEC_MERGE symbol has been
4331              adjusted, so all references to such symbols need to be
4332              passed through _bfd_merged_section_offset.  (Later, in
4333              relocate_section, all SEC_MERGE symbols *except* for
4334              section symbols have been adjusted.)
4335
4336              gas may reduce relocations against symbols in SEC_MERGE
4337              sections to a relocation against the section symbol when
4338              the original addend was zero.  When the reloc is against
4339              a section symbol we should include the addend in the
4340              offset passed to _bfd_merged_section_offset, since the
4341              location of interest is the original symbol.  On the
4342              other hand, an access to "sym+addend" where "sym" is not
4343              a section symbol should not include the addend;  Such an
4344              access is presumed to be an offset from "sym";  The
4345              location of interest is just "sym".  */
4346           if (sym_type == STT_SECTION)
4347             toff += irel->r_addend;
4348
4349           toff = _bfd_merged_section_offset (abfd, &tsec,
4350                                              elf_section_data (tsec)->sec_info,
4351                                              toff);
4352
4353           if (sym_type != STT_SECTION)
4354             toff += irel->r_addend;
4355         }
4356       else
4357         toff += irel->r_addend;
4358
4359       symaddr = tsec->output_section->vma + tsec->output_offset + toff;
4360
4361       roff = irel->r_offset;
4362       reladdr = isec->output_section->vma + isec->output_offset + roff;
4363
4364       /* If the branch is in range, no need to do anything.  */
4365       if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
4366         continue;
4367
4368       /* Look for an existing fixup to this address.  */
4369       for (f = fixups; f ; f = f->next)
4370         if (f->tsec == tsec && f->toff == toff)
4371           break;
4372
4373       if (f == NULL)
4374         {
4375           size_t size;
4376           unsigned long stub_rtype;
4377
4378           val = trampoff - roff;
4379           if (val >= max_branch_offset)
4380             /* Oh dear, we can't reach a trampoline.  Don't try to add
4381                one.  We'll report an error later.  */
4382             continue;
4383
4384           if (link_info->shared)
4385             {
4386               size = 4 * ARRAY_SIZE (shared_stub_entry);
4387               insn_offset = 12;
4388               stub_rtype = R_PPC_RELAX32PC;
4389             }
4390           else
4391             {
4392               size = 4 * ARRAY_SIZE (stub_entry);
4393               insn_offset = 0;
4394               stub_rtype = R_PPC_RELAX32;
4395             }
4396
4397           if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
4398               != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
4399             abort ();
4400           if (tsec == ppc_info->plt)
4401             stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
4402
4403           /* Hijack the old relocation.  Since we need two
4404              relocations for this use a "composite" reloc.  */
4405           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4406                                        stub_rtype);
4407           irel->r_offset = trampoff + insn_offset;
4408
4409           /* Record the fixup so we don't do it again this section.  */
4410           f = bfd_malloc (sizeof (*f));
4411           f->next = fixups;
4412           f->tsec = tsec;
4413           f->toff = toff;
4414           f->trampoff = trampoff;
4415           fixups = f;
4416
4417           trampoff += size;
4418         }
4419       else
4420         {
4421           val = f->trampoff - roff;
4422           if (val >= max_branch_offset)
4423             continue;
4424
4425           /* Nop out the reloc, since we're finalizing things here.  */
4426           irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
4427         }
4428
4429       /* Get the section contents.  */
4430       if (contents == NULL)
4431         {
4432           /* Get cached copy if it exists.  */
4433           if (elf_section_data (isec)->this_hdr.contents != NULL)
4434             contents = elf_section_data (isec)->this_hdr.contents;
4435           else
4436             {
4437               /* Go get them off disk.  */
4438               if (!bfd_malloc_and_get_section (abfd, isec, &contents))
4439                 goto error_return;
4440             }
4441         }
4442
4443       /* Fix up the existing branch to hit the trampoline.  */
4444       hit_addr = contents + roff;
4445       switch (r_type)
4446         {
4447         case R_PPC_REL24:
4448         case R_PPC_LOCAL24PC:
4449         case R_PPC_PLTREL24:
4450           t0 = bfd_get_32 (abfd, hit_addr);
4451           t0 &= ~0x3fffffc;
4452           t0 |= val & 0x3fffffc;
4453           bfd_put_32 (abfd, t0, hit_addr);
4454           break;
4455
4456         case R_PPC_REL14:
4457         case R_PPC_REL14_BRTAKEN:
4458         case R_PPC_REL14_BRNTAKEN:
4459           t0 = bfd_get_32 (abfd, hit_addr);
4460           t0 &= ~0xfffc;
4461           t0 |= val & 0xfffc;
4462           bfd_put_32 (abfd, t0, hit_addr);
4463           break;
4464         }
4465     }
4466
4467   /* Write out the trampolines.  */
4468   changed = fixups != NULL;
4469   if (fixups != NULL)
4470     {
4471       const int *stub;
4472       bfd_byte *dest;
4473       bfd_vma val;
4474       int i, size;
4475
4476       do
4477         {
4478           struct one_fixup *f = fixups;
4479           fixups = fixups->next;
4480           free (f);
4481         }
4482       while (fixups);
4483
4484       contents = bfd_realloc (contents, trampoff);
4485       if (contents == NULL)
4486         goto error_return;
4487
4488       isec->size = (isec->size + 3) & (bfd_vma) -4;
4489       /* Branch around the trampolines.  */
4490       val = trampoff - isec->size + 0x48000000;
4491       dest = contents + isec->size;
4492       isec->size = trampoff;
4493       bfd_put_32 (abfd, val, dest);
4494       dest += 4;
4495
4496       if (link_info->shared)
4497         {
4498           stub = shared_stub_entry;
4499           size = ARRAY_SIZE (shared_stub_entry);
4500         }
4501       else
4502         {
4503           stub = stub_entry;
4504           size = ARRAY_SIZE (stub_entry);
4505         }
4506
4507       i = 0;
4508       while (dest < contents + trampoff)
4509         {
4510           bfd_put_32 (abfd, stub[i], dest);
4511           i++;
4512           if (i == size)
4513             i = 0;
4514           dest += 4;
4515         }
4516       BFD_ASSERT (i == 0);
4517     }
4518
4519   if (isymbuf != NULL
4520       && symtab_hdr->contents != (unsigned char *) isymbuf)
4521     {
4522       if (! link_info->keep_memory)
4523         free (isymbuf);
4524       else
4525         {
4526           /* Cache the symbols for elf_link_input_bfd.  */
4527           symtab_hdr->contents = (unsigned char *) isymbuf;
4528         }
4529     }
4530
4531   if (contents != NULL
4532       && elf_section_data (isec)->this_hdr.contents != contents)
4533     {
4534       if (!changed && !link_info->keep_memory)
4535         free (contents);
4536       else
4537         {
4538           /* Cache the section contents for elf_link_input_bfd.  */
4539           elf_section_data (isec)->this_hdr.contents = contents;
4540         }
4541     }
4542
4543   if (elf_section_data (isec)->relocs != internal_relocs)
4544     {
4545       if (!changed)
4546         free (internal_relocs);
4547       else
4548         elf_section_data (isec)->relocs = internal_relocs;
4549     }
4550
4551   *again = changed;
4552   return TRUE;
4553
4554  error_return:
4555   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
4556     free (isymbuf);
4557   if (contents != NULL
4558       && elf_section_data (isec)->this_hdr.contents != contents)
4559     free (contents);
4560   if (internal_relocs != NULL
4561       && elf_section_data (isec)->relocs != internal_relocs)
4562     free (internal_relocs);
4563   return FALSE;
4564 }
4565 \f
4566 /* Set _SDA_BASE_, _SDA2_BASE, and sbss start and end syms.  They are
4567    set here rather than via PROVIDE in the default linker script,
4568    because using PROVIDE inside an output section statement results in
4569    unnecessary output sections.  Using PROVIDE outside an output section
4570    statement runs the risk of section alignment affecting where the
4571    section starts.  */
4572
4573 bfd_boolean
4574 ppc_elf_set_sdata_syms (bfd *obfd, struct bfd_link_info *info)
4575 {
4576   struct ppc_elf_link_hash_table *htab;
4577   unsigned i;
4578   asection *s;
4579   bfd_vma val;
4580
4581   htab = ppc_elf_hash_table (info);
4582
4583   for (i = 0; i < 2; i++)
4584     {
4585       elf_linker_section_t *lsect = &htab->sdata[i];
4586
4587       s = lsect->section;
4588       if (s != NULL)
4589         s = s->output_section;
4590       if (s == NULL)
4591         s = bfd_get_section_by_name (obfd, lsect->name);
4592       if (s == NULL)
4593         s = bfd_get_section_by_name (obfd, lsect->bss_name);
4594
4595       val = 0;
4596       if (s != NULL)
4597         val = s->vma + 32768;
4598       lsect->sym_val = val;
4599
4600       _bfd_elf_provide_symbol (info, lsect->sym_name, val);
4601     }
4602
4603   s = bfd_get_section_by_name (obfd, ".sbss");
4604   val = 0;
4605   if (s != NULL)
4606     val = s->vma;
4607   _bfd_elf_provide_symbol (info, "__sbss_start", val);
4608   _bfd_elf_provide_symbol (info, "___sbss_start", val);
4609   if (s != NULL)
4610     val += s->size;
4611   _bfd_elf_provide_symbol (info, "__sbss_end", val);
4612   _bfd_elf_provide_symbol (info, "___sbss_end", val);
4613   return TRUE;
4614 }
4615 \f
4616 /* Fill in the address for a pointer generated in a linker section.  */
4617
4618 static bfd_vma
4619 elf_finish_pointer_linker_section (bfd *input_bfd,
4620                                    elf_linker_section_t *lsect,
4621                                    struct elf_link_hash_entry *h,
4622                                    bfd_vma relocation,
4623                                    const Elf_Internal_Rela *rel)
4624 {
4625   elf_linker_section_pointers_t *linker_section_ptr;
4626
4627   BFD_ASSERT (lsect != NULL);
4628
4629   if (h != NULL)
4630     {
4631       /* Handle global symbol.  */
4632       struct ppc_elf_link_hash_entry *eh;
4633
4634       eh = (struct ppc_elf_link_hash_entry *) h;
4635       BFD_ASSERT (eh->elf.def_regular);
4636       linker_section_ptr = eh->linker_section_pointer;
4637     }
4638   else
4639     {
4640       /* Handle local symbol.  */
4641       unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
4642
4643       BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
4644       linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
4645     }
4646
4647   linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
4648                                                         rel->r_addend,
4649                                                         lsect);
4650   BFD_ASSERT (linker_section_ptr != NULL);
4651
4652   /* Offset will always be a multiple of four, so use the bottom bit
4653      as a "written" flag.  */
4654   if ((linker_section_ptr->offset & 1) == 0)
4655     {
4656       bfd_put_32 (lsect->section->owner,
4657                   relocation + linker_section_ptr->addend,
4658                   lsect->section->contents + linker_section_ptr->offset);
4659       linker_section_ptr->offset += 1;
4660     }
4661
4662   relocation = (lsect->section->output_offset
4663                 + linker_section_ptr->offset - 1
4664                 - 0x8000);
4665
4666 #ifdef DEBUG
4667   fprintf (stderr,
4668            "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
4669            lsect->name, (long) relocation, (long) relocation);
4670 #endif
4671
4672   /* Subtract out the addend, because it will get added back in by the normal
4673      processing.  */
4674   return relocation - linker_section_ptr->addend;
4675 }
4676
4677 /* The RELOCATE_SECTION function is called by the ELF backend linker
4678    to handle the relocations for a section.
4679
4680    The relocs are always passed as Rela structures; if the section
4681    actually uses Rel structures, the r_addend field will always be
4682    zero.
4683
4684    This function is responsible for adjust the section contents as
4685    necessary, and (if using Rela relocs and generating a
4686    relocatable output file) adjusting the reloc addend as
4687    necessary.
4688
4689    This function does not have to worry about setting the reloc
4690    address or the reloc symbol index.
4691
4692    LOCAL_SYMS is a pointer to the swapped in local symbols.
4693
4694    LOCAL_SECTIONS is an array giving the section in the input file
4695    corresponding to the st_shndx field of each local symbol.
4696
4697    The global hash table entry for the global symbols can be found
4698    via elf_sym_hashes (input_bfd).
4699
4700    When generating relocatable output, this function must handle
4701    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4702    going to be the section symbol corresponding to the output
4703    section, which means that the addend must be adjusted
4704    accordingly.  */
4705
4706 static bfd_boolean
4707 ppc_elf_relocate_section (bfd *output_bfd,
4708                           struct bfd_link_info *info,
4709                           bfd *input_bfd,
4710                           asection *input_section,
4711                           bfd_byte *contents,
4712                           Elf_Internal_Rela *relocs,
4713                           Elf_Internal_Sym *local_syms,
4714                           asection **local_sections)
4715 {
4716   Elf_Internal_Shdr *symtab_hdr;
4717   struct elf_link_hash_entry **sym_hashes;
4718   struct ppc_elf_link_hash_table *htab;
4719   Elf_Internal_Rela *rel;
4720   Elf_Internal_Rela *relend;
4721   Elf_Internal_Rela outrel;
4722   bfd_byte *loc;
4723   asection *sreloc = NULL;
4724   bfd_vma *local_got_offsets;
4725   bfd_boolean ret = TRUE;
4726
4727 #ifdef DEBUG
4728   _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
4729                       "%ld relocations%s",
4730                       input_bfd, input_section,
4731                       (long) input_section->reloc_count,
4732                       (info->relocatable) ? " (relocatable)" : "");
4733 #endif
4734
4735   if (info->relocatable)
4736     return TRUE;
4737
4738   /* Initialize howto table if not already done.  */
4739   if (!ppc_elf_howto_table[R_PPC_ADDR32])
4740     ppc_elf_howto_init ();
4741
4742   htab = ppc_elf_hash_table (info);
4743   local_got_offsets = elf_local_got_offsets (input_bfd);
4744   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4745   sym_hashes = elf_sym_hashes (input_bfd);
4746   rel = relocs;
4747   relend = relocs + input_section->reloc_count;
4748   for (; rel < relend; rel++)
4749     {
4750       enum elf_ppc_reloc_type r_type;
4751       bfd_vma addend;
4752       bfd_reloc_status_type r;
4753       Elf_Internal_Sym *sym;
4754       asection *sec;
4755       struct elf_link_hash_entry *h;
4756       const char *sym_name;
4757       reloc_howto_type *howto;
4758       unsigned long r_symndx;
4759       bfd_vma relocation;
4760       bfd_vma branch_bit, insn, from;
4761       bfd_boolean unresolved_reloc;
4762       bfd_boolean warned;
4763       unsigned int tls_type, tls_mask, tls_gd;
4764
4765       r_type = ELF32_R_TYPE (rel->r_info);
4766       sym = NULL;
4767       sec = NULL;
4768       h = NULL;
4769       unresolved_reloc = FALSE;
4770       warned = FALSE;
4771       r_symndx = ELF32_R_SYM (rel->r_info);
4772
4773       if (r_symndx < symtab_hdr->sh_info)
4774         {
4775           sym = local_syms + r_symndx;
4776           sec = local_sections[r_symndx];
4777           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
4778
4779           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4780         }
4781       else
4782         {
4783           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4784                                    r_symndx, symtab_hdr, sym_hashes,
4785                                    h, sec, relocation,
4786                                    unresolved_reloc, warned);
4787
4788           sym_name = h->root.root.string;
4789         }
4790
4791       /* TLS optimizations.  Replace instruction sequences and relocs
4792          based on information we collected in tls_optimize.  We edit
4793          RELOCS so that --emit-relocs will output something sensible
4794          for the final instruction stream.  */
4795       tls_mask = 0;
4796       tls_gd = 0;
4797       if (IS_PPC_TLS_RELOC (r_type))
4798         {
4799           if (h != NULL)
4800             tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4801           else if (local_got_offsets != NULL)
4802             {
4803               char *lgot_masks;
4804               lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4805               tls_mask = lgot_masks[r_symndx];
4806             }
4807         }
4808
4809       /* Ensure reloc mapping code below stays sane.  */
4810       if ((R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TLSGD16 & 3)
4811           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4812           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4813           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4814           || (R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TPREL16 & 3)
4815           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4816           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4817           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4818         abort ();
4819       switch (r_type)
4820         {
4821         default:
4822           break;
4823
4824         case R_PPC_GOT_TPREL16:
4825         case R_PPC_GOT_TPREL16_LO:
4826           if (tls_mask != 0
4827               && (tls_mask & TLS_TPREL) == 0)
4828             {
4829               bfd_vma insn;
4830               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4831               insn &= 31 << 21;
4832               insn |= 0x3c020000;       /* addis 0,2,0 */
4833               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4834               r_type = R_PPC_TPREL16_HA;
4835               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4836             }
4837           break;
4838
4839         case R_PPC_TLS:
4840           if (tls_mask != 0
4841               && (tls_mask & TLS_TPREL) == 0)
4842             {
4843               bfd_vma insn, rtra;
4844               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4845               if ((insn & ((31 << 26) | (31 << 11)))
4846                   == ((31 << 26) | (2 << 11)))
4847                 rtra = insn & ((1 << 26) - (1 << 16));
4848               else if ((insn & ((31 << 26) | (31 << 16)))
4849                        == ((31 << 26) | (2 << 16)))
4850                 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4851               else
4852                 abort ();
4853               if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4854                 /* add -> addi.  */
4855                 insn = 14 << 26;
4856               else if ((insn & (31 << 1)) == 23 << 1
4857                        && ((insn & (31 << 6)) < 14 << 6
4858                            || ((insn & (31 << 6)) >= 16 << 6
4859                                && (insn & (31 << 6)) < 24 << 6)))
4860                 /* load and store indexed -> dform.  */
4861                 insn = (32 | ((insn >> 6) & 31)) << 26;
4862               else if ((insn & (31 << 1)) == 21 << 1
4863                        && (insn & (0x1a << 6)) == 0)
4864                 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
4865                 insn = (((58 | ((insn >> 6) & 4)) << 26)
4866                         | ((insn >> 6) & 1));
4867               else if ((insn & (31 << 1)) == 21 << 1
4868                        && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4869                 /* lwax -> lwa.  */
4870                 insn = (58 << 26) | 2;
4871               else
4872                 abort ();
4873               insn |= rtra;
4874               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4875               r_type = R_PPC_TPREL16_LO;
4876               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4877               /* Was PPC_TLS which sits on insn boundary, now
4878                  PPC_TPREL16_LO which is at insn+2.  */
4879               rel->r_offset += 2;
4880             }
4881           break;
4882
4883         case R_PPC_GOT_TLSGD16_HI:
4884         case R_PPC_GOT_TLSGD16_HA:
4885           tls_gd = TLS_TPRELGD;
4886           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4887             goto tls_gdld_hi;
4888           break;
4889
4890         case R_PPC_GOT_TLSLD16_HI:
4891         case R_PPC_GOT_TLSLD16_HA:
4892           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4893             {
4894             tls_gdld_hi:
4895               if ((tls_mask & tls_gd) != 0)
4896                 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4897                           + R_PPC_GOT_TPREL16);
4898               else
4899                 {
4900                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4901                   rel->r_offset -= 2;
4902                   r_type = R_PPC_NONE;
4903                 }
4904               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4905             }
4906           break;
4907
4908         case R_PPC_GOT_TLSGD16:
4909         case R_PPC_GOT_TLSGD16_LO:
4910           tls_gd = TLS_TPRELGD;
4911           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4912             goto tls_get_addr_check;
4913           break;
4914
4915         case R_PPC_GOT_TLSLD16:
4916         case R_PPC_GOT_TLSLD16_LO:
4917           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4918             {
4919             tls_get_addr_check:
4920               if (rel + 1 < relend)
4921                 {
4922                   enum elf_ppc_reloc_type r_type2;
4923                   unsigned long r_symndx2;
4924                   struct elf_link_hash_entry *h2;
4925                   bfd_vma insn1, insn2;
4926                   bfd_vma offset;
4927
4928                   /* The next instruction should be a call to
4929                      __tls_get_addr.  Peek at the reloc to be sure.  */
4930                   r_type2 = ELF32_R_TYPE (rel[1].r_info);
4931                   r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4932                   if (r_symndx2 < symtab_hdr->sh_info
4933                       || (r_type2 != R_PPC_REL14
4934                           && r_type2 != R_PPC_REL14_BRTAKEN
4935                           && r_type2 != R_PPC_REL14_BRNTAKEN
4936                           && r_type2 != R_PPC_REL24
4937                           && r_type2 != R_PPC_PLTREL24))
4938                     break;
4939
4940                   h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4941                   while (h2->root.type == bfd_link_hash_indirect
4942                          || h2->root.type == bfd_link_hash_warning)
4943                     h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4944                   if (h2 == NULL || h2 != htab->tls_get_addr)
4945                     break;
4946
4947                   /* OK, it checks out.  Replace the call.  */
4948                   offset = rel[1].r_offset;
4949                   insn1 = bfd_get_32 (output_bfd,
4950                                       contents + rel->r_offset - 2);
4951                   if ((tls_mask & tls_gd) != 0)
4952                     {
4953                       /* IE */
4954                       insn1 &= (1 << 26) - 1;
4955                       insn1 |= 32 << 26;        /* lwz */
4956                       insn2 = 0x7c631214;       /* add 3,3,2 */
4957                       rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4958                       r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4959                                 + R_PPC_GOT_TPREL16);
4960                       rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4961                     }
4962                   else
4963                     {
4964                       /* LE */
4965                       insn1 = 0x3c620000;       /* addis 3,2,0 */
4966                       insn2 = 0x38630000;       /* addi 3,3,0 */
4967                       if (tls_gd == 0)
4968                         {
4969                           /* Was an LD reloc.  */
4970                           r_symndx = 0;
4971                           rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4972                           rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4973                         }
4974                       r_type = R_PPC_TPREL16_HA;
4975                       rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4976                       rel[1].r_info = ELF32_R_INFO (r_symndx,
4977                                                     R_PPC_TPREL16_LO);
4978                       rel[1].r_offset += 2;
4979                     }
4980                   bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4981                   bfd_put_32 (output_bfd, insn2, contents + offset);
4982                   if (tls_gd == 0)
4983                     {
4984                       /* We changed the symbol on an LD reloc.  Start over
4985                          in order to get h, sym, sec etc. right.  */
4986                       rel--;
4987                       continue;
4988                     }
4989                 }
4990             }
4991           break;
4992         }
4993
4994       /* Handle other relocations that tweak non-addend part of insn.  */
4995       branch_bit = 0;
4996       switch (r_type)
4997         {
4998         default:
4999           break;
5000
5001           /* Branch taken prediction relocations.  */
5002         case R_PPC_ADDR14_BRTAKEN:
5003         case R_PPC_REL14_BRTAKEN:
5004           branch_bit = BRANCH_PREDICT_BIT;
5005           /* Fall thru */
5006
5007           /* Branch not taken prediction relocations.  */
5008         case R_PPC_ADDR14_BRNTAKEN:
5009         case R_PPC_REL14_BRNTAKEN:
5010           insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5011           insn &= ~BRANCH_PREDICT_BIT;
5012           insn |= branch_bit;
5013
5014           from = (rel->r_offset
5015                   + input_section->output_offset
5016                   + input_section->output_section->vma);
5017
5018           /* Invert 'y' bit if not the default.  */
5019           if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
5020             insn ^= BRANCH_PREDICT_BIT;
5021
5022           bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5023           break;
5024         }
5025
5026       addend = rel->r_addend;
5027       tls_type = 0;
5028       howto = NULL;
5029       if (r_type < R_PPC_max)
5030         howto = ppc_elf_howto_table[r_type];
5031       switch (r_type)
5032         {
5033         default:
5034           (*_bfd_error_handler)
5035             (_("%B: unknown relocation type %d for symbol %s"),
5036              input_bfd, (int) r_type, sym_name);
5037
5038           bfd_set_error (bfd_error_bad_value);
5039           ret = FALSE;
5040           continue;
5041
5042         case R_PPC_NONE:
5043         case R_PPC_TLS:
5044         case R_PPC_EMB_MRKREF:
5045         case R_PPC_GNU_VTINHERIT:
5046         case R_PPC_GNU_VTENTRY:
5047           continue;
5048
5049           /* GOT16 relocations.  Like an ADDR16 using the symbol's
5050              address in the GOT as relocation value instead of the
5051              symbol's value itself.  Also, create a GOT entry for the
5052              symbol and put the symbol value there.  */
5053         case R_PPC_GOT_TLSGD16:
5054         case R_PPC_GOT_TLSGD16_LO:
5055         case R_PPC_GOT_TLSGD16_HI:
5056         case R_PPC_GOT_TLSGD16_HA:
5057           tls_type = TLS_TLS | TLS_GD;
5058           goto dogot;
5059
5060         case R_PPC_GOT_TLSLD16:
5061         case R_PPC_GOT_TLSLD16_LO:
5062         case R_PPC_GOT_TLSLD16_HI:
5063         case R_PPC_GOT_TLSLD16_HA:
5064           tls_type = TLS_TLS | TLS_LD;
5065           goto dogot;
5066
5067         case R_PPC_GOT_TPREL16:
5068         case R_PPC_GOT_TPREL16_LO:
5069         case R_PPC_GOT_TPREL16_HI:
5070         case R_PPC_GOT_TPREL16_HA:
5071           tls_type = TLS_TLS | TLS_TPREL;
5072           goto dogot;
5073
5074         case R_PPC_GOT_DTPREL16:
5075         case R_PPC_GOT_DTPREL16_LO:
5076         case R_PPC_GOT_DTPREL16_HI:
5077         case R_PPC_GOT_DTPREL16_HA:
5078           tls_type = TLS_TLS | TLS_DTPREL;
5079           goto dogot;
5080
5081         case R_PPC_GOT16:
5082         case R_PPC_GOT16_LO:
5083         case R_PPC_GOT16_HI:
5084         case R_PPC_GOT16_HA:
5085         dogot:
5086           {
5087             /* Relocation is to the entry for this symbol in the global
5088                offset table.  */
5089             bfd_vma off;
5090             bfd_vma *offp;
5091             unsigned long indx;
5092
5093             if (htab->got == NULL)
5094               abort ();
5095
5096             indx = 0;
5097             if (tls_type == (TLS_TLS | TLS_LD)
5098                 && (h == NULL
5099                     || !h->def_dynamic))
5100               offp = &htab->tlsld_got.offset;
5101             else if (h != NULL)
5102               {
5103                 bfd_boolean dyn;
5104                 dyn = htab->elf.dynamic_sections_created;
5105                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5106                     || (info->shared
5107                         && SYMBOL_REFERENCES_LOCAL (info, h)))
5108                   /* This is actually a static link, or it is a
5109                      -Bsymbolic link and the symbol is defined
5110                      locally, or the symbol was forced to be local
5111                      because of a version file.  */
5112                   ;
5113                 else
5114                   {
5115                     indx = h->dynindx;
5116                     unresolved_reloc = FALSE;
5117                   }
5118                 offp = &h->got.offset;
5119               }
5120             else
5121               {
5122                 if (local_got_offsets == NULL)
5123                   abort ();
5124                 offp = &local_got_offsets[r_symndx];
5125               }
5126
5127             /* The offset must always be a multiple of 4.  We use the
5128                least significant bit to record whether we have already
5129                processed this entry.  */
5130             off = *offp;
5131             if ((off & 1) != 0)
5132               off &= ~1;
5133             else
5134               {
5135                 unsigned int tls_m = (tls_mask
5136                                       & (TLS_LD | TLS_GD | TLS_DTPREL
5137                                          | TLS_TPREL | TLS_TPRELGD));
5138
5139                 if (offp == &htab->tlsld_got.offset)
5140                   tls_m = TLS_LD;
5141                 else if (h == NULL
5142                          || !h->def_dynamic)
5143                   tls_m &= ~TLS_LD;
5144
5145                 /* We might have multiple got entries for this sym.
5146                    Initialize them all.  */
5147                 do
5148                   {
5149                     int tls_ty = 0;
5150
5151                     if ((tls_m & TLS_LD) != 0)
5152                       {
5153                         tls_ty = TLS_TLS | TLS_LD;
5154                         tls_m &= ~TLS_LD;
5155                       }
5156                     else if ((tls_m & TLS_GD) != 0)
5157                       {
5158                         tls_ty = TLS_TLS | TLS_GD;
5159                         tls_m &= ~TLS_GD;
5160                       }
5161                     else if ((tls_m & TLS_DTPREL) != 0)
5162                       {
5163                         tls_ty = TLS_TLS | TLS_DTPREL;
5164                         tls_m &= ~TLS_DTPREL;
5165                       }
5166                     else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5167                       {
5168                         tls_ty = TLS_TLS | TLS_TPREL;
5169                         tls_m = 0;
5170                       }
5171
5172                     /* Generate relocs for the dynamic linker.  */
5173                     if ((info->shared || indx != 0)
5174                         && (h == NULL
5175                             || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5176                             || h->root.type != bfd_link_hash_undefweak))
5177                       {
5178                         outrel.r_offset = (htab->got->output_section->vma
5179                                            + htab->got->output_offset
5180                                            + off);
5181                         outrel.r_addend = 0;
5182                         if (tls_ty & (TLS_LD | TLS_GD))
5183                           {
5184                             outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
5185                             if (tls_ty == (TLS_TLS | TLS_GD))
5186                               {
5187                                 loc = htab->relgot->contents;
5188                                 loc += (htab->relgot->reloc_count++
5189                                         * sizeof (Elf32_External_Rela));
5190                                 bfd_elf32_swap_reloca_out (output_bfd,
5191                                                            &outrel, loc);
5192                                 outrel.r_offset += 4;
5193                                 outrel.r_info
5194                                   = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5195                               }
5196                           }
5197                         else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5198                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5199                         else if (tls_ty == (TLS_TLS | TLS_TPREL))
5200                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5201                         else if (indx == 0)
5202                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5203                         else
5204                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
5205                         if (indx == 0)
5206                           {
5207                             outrel.r_addend += relocation;
5208                             if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
5209                               outrel.r_addend -= htab->elf.tls_sec->vma;
5210                           }
5211                         loc = htab->relgot->contents;
5212                         loc += (htab->relgot->reloc_count++
5213                                 * sizeof (Elf32_External_Rela));
5214                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5215                       }
5216
5217                     /* Init the .got section contents if we're not
5218                        emitting a reloc.  */
5219                     else
5220                       {
5221                         bfd_vma value = relocation;
5222
5223                         if (tls_ty == (TLS_TLS | TLS_LD))
5224                           value = 1;
5225                         else if (tls_ty != 0)
5226                           {
5227                             value -= htab->elf.tls_sec->vma + DTP_OFFSET;
5228                             if (tls_ty == (TLS_TLS | TLS_TPREL))
5229                               value += DTP_OFFSET - TP_OFFSET;
5230
5231                             if (tls_ty == (TLS_TLS | TLS_GD))
5232                               {
5233                                 bfd_put_32 (output_bfd, value,
5234                                             htab->got->contents + off + 4);
5235                                 value = 1;
5236                               }
5237                           }
5238                         bfd_put_32 (output_bfd, value,
5239                                     htab->got->contents + off);
5240                       }
5241
5242                     off += 4;
5243                     if (tls_ty & (TLS_LD | TLS_GD))
5244                       off += 4;
5245                   }
5246                 while (tls_m != 0);
5247
5248                 off = *offp;
5249                 *offp = off | 1;
5250               }
5251
5252             if (off >= (bfd_vma) -2)
5253               abort ();
5254
5255             if ((tls_type & TLS_TLS) != 0)
5256               {
5257                 if (tls_type != (TLS_TLS | TLS_LD))
5258                   {
5259                     if ((tls_mask & TLS_LD) != 0
5260                         && !(h == NULL
5261                              || !h->def_dynamic))
5262                       off += 8;
5263                     if (tls_type != (TLS_TLS | TLS_GD))
5264                       {
5265                         if ((tls_mask & TLS_GD) != 0)
5266                           off += 8;
5267                         if (tls_type != (TLS_TLS | TLS_DTPREL))
5268                           {
5269                             if ((tls_mask & TLS_DTPREL) != 0)
5270                               off += 4;
5271                           }
5272                       }
5273                   }
5274               }
5275
5276             relocation = htab->got->output_offset + off - 4;
5277
5278             /* Addends on got relocations don't make much sense.
5279                x+off@got is actually x@got+off, and since the got is
5280                generated by a hash table traversal, the value in the
5281                got at entry m+n bears little relation to the entry m.  */
5282             if (addend != 0)
5283               (*_bfd_error_handler)
5284                 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
5285                  input_bfd,
5286                  input_section,
5287                  (long) rel->r_offset,
5288                  howto->name,
5289                  sym_name);
5290           }
5291         break;
5292
5293         /* Relocations that need no special processing.  */
5294         case R_PPC_LOCAL24PC:
5295           /* It makes no sense to point a local relocation
5296              at a symbol not in this object.  */
5297           if (unresolved_reloc)
5298             {
5299               if (! (*info->callbacks->undefined_symbol) (info,
5300                                                           h->root.root.string,
5301                                                           input_bfd,
5302                                                           input_section,
5303                                                           rel->r_offset,
5304                                                           TRUE))
5305                 return FALSE;
5306               continue;
5307             }
5308           break;
5309
5310         case R_PPC_DTPREL16:
5311         case R_PPC_DTPREL16_LO:
5312         case R_PPC_DTPREL16_HI:
5313         case R_PPC_DTPREL16_HA:
5314           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5315           break;
5316
5317           /* Relocations that may need to be propagated if this is a shared
5318              object.  */
5319         case R_PPC_TPREL16:
5320         case R_PPC_TPREL16_LO:
5321         case R_PPC_TPREL16_HI:
5322         case R_PPC_TPREL16_HA:
5323           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5324           /* The TPREL16 relocs shouldn't really be used in shared
5325              libs as they will result in DT_TEXTREL being set, but
5326              support them anyway.  */
5327           goto dodyn;
5328
5329         case R_PPC_TPREL32:
5330           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5331           goto dodyn;
5332
5333         case R_PPC_DTPREL32:
5334           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5335           goto dodyn;
5336
5337         case R_PPC_DTPMOD32:
5338           relocation = 1;
5339           addend = 0;
5340           goto dodyn;
5341
5342         case R_PPC_REL24:
5343         case R_PPC_REL32:
5344         case R_PPC_REL14:
5345         case R_PPC_REL14_BRTAKEN:
5346         case R_PPC_REL14_BRNTAKEN:
5347           /* If these relocations are not to a named symbol, they can be
5348              handled right here, no need to bother the dynamic linker.  */
5349           if (SYMBOL_REFERENCES_LOCAL (info, h)
5350               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5351             break;
5352           /* fall through */
5353
5354           /* Relocations that always need to be propagated if this is a shared
5355              object.  */
5356         case R_PPC_ADDR32:
5357         case R_PPC_ADDR24:
5358         case R_PPC_ADDR16:
5359         case R_PPC_ADDR16_LO:
5360         case R_PPC_ADDR16_HI:
5361         case R_PPC_ADDR16_HA:
5362         case R_PPC_ADDR14:
5363         case R_PPC_ADDR14_BRTAKEN:
5364         case R_PPC_ADDR14_BRNTAKEN:
5365         case R_PPC_UADDR32:
5366         case R_PPC_UADDR16:
5367           /* r_symndx will be zero only for relocs against symbols
5368              from removed linkonce sections, or sections discarded by
5369              a linker script.  */
5370         dodyn:
5371           if (r_symndx == 0)
5372             break;
5373           /* Fall thru.  */
5374
5375           if ((input_section->flags & SEC_ALLOC) == 0)
5376             break;
5377           /* Fall thru.  */
5378
5379           if ((info->shared
5380                && (h == NULL
5381                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5382                    || h->root.type != bfd_link_hash_undefweak)
5383                && (MUST_BE_DYN_RELOC (r_type)
5384                    || !SYMBOL_CALLS_LOCAL (info, h)))
5385               || (ELIMINATE_COPY_RELOCS
5386                   && !info->shared
5387                   && h != NULL
5388                   && h->dynindx != -1
5389                   && !h->non_got_ref
5390                   && h->def_dynamic
5391                   && !h->def_regular))
5392             {
5393               int skip;
5394
5395 #ifdef DEBUG
5396               fprintf (stderr, "ppc_elf_relocate_section needs to "
5397                        "create relocation for %s\n",
5398                        (h && h->root.root.string
5399                         ? h->root.root.string : "<unknown>"));
5400 #endif
5401
5402               /* When generating a shared object, these relocations
5403                  are copied into the output file to be resolved at run
5404                  time.  */
5405               if (sreloc == NULL)
5406                 {
5407                   const char *name;
5408
5409                   name = (bfd_elf_string_from_elf_section
5410                           (input_bfd,
5411                            elf_elfheader (input_bfd)->e_shstrndx,
5412                            elf_section_data (input_section)->rel_hdr.sh_name));
5413                   if (name == NULL)
5414                     return FALSE;
5415
5416                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5417                               && strcmp (bfd_get_section_name (input_bfd,
5418                                                                input_section),
5419                                          name + 5) == 0);
5420
5421                   sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5422                   BFD_ASSERT (sreloc != NULL);
5423                 }
5424
5425               skip = 0;
5426
5427               outrel.r_offset =
5428                 _bfd_elf_section_offset (output_bfd, info, input_section,
5429                                          rel->r_offset);
5430               if (outrel.r_offset == (bfd_vma) -1
5431                   || outrel.r_offset == (bfd_vma) -2)
5432                 skip = (int) outrel.r_offset;
5433               outrel.r_offset += (input_section->output_section->vma
5434                                   + input_section->output_offset);
5435
5436               if (skip)
5437                 memset (&outrel, 0, sizeof outrel);
5438               else if (!SYMBOL_REFERENCES_LOCAL (info, h))
5439                 {
5440                   unresolved_reloc = FALSE;
5441                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5442                   outrel.r_addend = rel->r_addend;
5443                 }
5444               else
5445                 {
5446                   outrel.r_addend = relocation + rel->r_addend;
5447
5448                   if (r_type == R_PPC_ADDR32)
5449                     outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5450                   else
5451                     {
5452                       long indx;
5453
5454                       if (bfd_is_abs_section (sec))
5455                         indx = 0;
5456                       else if (sec == NULL || sec->owner == NULL)
5457                         {
5458                           bfd_set_error (bfd_error_bad_value);
5459                           return FALSE;
5460                         }
5461                       else
5462                         {
5463                           asection *osec;
5464
5465                           /* We are turning this relocation into one
5466                              against a section symbol.  It would be
5467                              proper to subtract the symbol's value,
5468                              osec->vma, from the emitted reloc addend,
5469                              but ld.so expects buggy relocs.  */
5470                           osec = sec->output_section;
5471                           indx = elf_section_data (osec)->dynindx;
5472                           BFD_ASSERT (indx > 0);
5473 #ifdef DEBUG
5474                           if (indx <= 0)
5475                             printf ("indx=%d section=%s flags=%08x name=%s\n",
5476                                     indx, osec->name, osec->flags,
5477                                     h->root.root.string);
5478 #endif
5479                         }
5480
5481                       outrel.r_info = ELF32_R_INFO (indx, r_type);
5482                     }
5483                 }
5484
5485               loc = sreloc->contents;
5486               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5487               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5488
5489               if (skip == -1)
5490                 continue;
5491
5492               /* This reloc will be computed at runtime.  We clear the memory
5493                  so that it contains predictable value.  */
5494               if (! skip
5495                   && ((input_section->flags & SEC_ALLOC) != 0
5496                       || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5497                 {
5498                   relocation = howto->pc_relative ? outrel.r_offset : 0;
5499                   addend = 0;
5500                   break;
5501                 }
5502             }
5503           break;
5504
5505         case R_PPC_RELAX32PC_PLT:
5506         case R_PPC_RELAX32_PLT:
5507           BFD_ASSERT (h != NULL
5508                       && h->plt.offset != (bfd_vma) -1
5509                       && htab->plt != NULL);
5510
5511           relocation = (htab->plt->output_section->vma
5512                         + htab->plt->output_offset
5513                         + h->plt.offset);
5514           if (r_type == R_PPC_RELAX32_PLT)
5515             goto relax32;
5516           /* Fall thru */
5517
5518         case R_PPC_RELAX32PC:
5519           relocation -= (input_section->output_section->vma
5520                          + input_section->output_offset
5521                          + rel->r_offset - 4);
5522           /* Fall thru */
5523
5524         case R_PPC_RELAX32:
5525         relax32:
5526           {
5527             unsigned long t0;
5528             unsigned long t1;
5529
5530             t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
5531             t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
5532
5533             /* We're clearing the bits for R_PPC_ADDR16_HA
5534                and R_PPC_ADDR16_LO here.  */
5535             t0 &= ~0xffff;
5536             t1 &= ~0xffff;
5537
5538             /* t0 is HA, t1 is LO */
5539             relocation += addend;
5540             t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
5541             t1 |= relocation & 0xffff;
5542
5543             bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
5544             bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
5545           }
5546           continue;
5547
5548           /* Indirect .sdata relocation.  */
5549         case R_PPC_EMB_SDAI16:
5550           BFD_ASSERT (htab->sdata[0].section != NULL);
5551           relocation
5552             = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
5553                                                  h, relocation, rel);
5554           break;
5555
5556           /* Indirect .sdata2 relocation.  */
5557         case R_PPC_EMB_SDA2I16:
5558           BFD_ASSERT (htab->sdata[1].section != NULL);
5559           relocation
5560             = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
5561                                                  h, relocation, rel);
5562           break;
5563
5564           /* Handle the TOC16 reloc.  We want to use the offset within the .got
5565              section, not the actual VMA.  This is appropriate when generating
5566              an embedded ELF object, for which the .got section acts like the
5567              AIX .toc section.  */
5568         case R_PPC_TOC16:                       /* phony GOT16 relocations */
5569           BFD_ASSERT (sec != NULL);
5570           BFD_ASSERT (bfd_is_und_section (sec)
5571                       || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5572                       || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
5573
5574             addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5575           break;
5576
5577         case R_PPC_PLTREL24:
5578           /* Relocation is to the entry for this symbol in the
5579              procedure linkage table.  */
5580           BFD_ASSERT (h != NULL);
5581
5582           if (h->plt.offset == (bfd_vma) -1
5583               || htab->plt == NULL)
5584             {
5585               /* We didn't make a PLT entry for this symbol.  This
5586                  happens when statically linking PIC code, or when
5587                  using -Bsymbolic.  */
5588               break;
5589             }
5590
5591           unresolved_reloc = FALSE;
5592           relocation = (htab->plt->output_section->vma
5593                         + htab->plt->output_offset
5594                         + h->plt.offset);
5595           break;
5596
5597           /* Relocate against _SDA_BASE_.  */
5598         case R_PPC_SDAREL16:
5599           {
5600             const char *name;
5601
5602             BFD_ASSERT (sec != NULL);
5603             name = bfd_get_section_name (abfd, sec->output_section);
5604             if (! ((strncmp (name, ".sdata", 6) == 0
5605                     && (name[6] == 0 || name[6] == '.'))
5606                    || (strncmp (name, ".sbss", 5) == 0
5607                        && (name[5] == 0 || name[5] == '.'))))
5608               {
5609                 (*_bfd_error_handler)
5610                   (_("%B: the target (%s) of a %s relocation is "
5611                      "in the wrong output section (%s)"),
5612                    input_bfd,
5613                    sym_name,
5614                    howto->name,
5615                    name);
5616               }
5617             addend -= htab->sdata[0].sym_val;
5618           }
5619           break;
5620
5621           /* Relocate against _SDA2_BASE_.  */
5622         case R_PPC_EMB_SDA2REL:
5623           {
5624             const char *name;
5625
5626             BFD_ASSERT (sec != NULL);
5627             name = bfd_get_section_name (abfd, sec->output_section);
5628             if (! (strncmp (name, ".sdata2", 7) == 0
5629                    || strncmp (name, ".sbss2", 6) == 0))
5630               {
5631                 (*_bfd_error_handler)
5632                   (_("%B: the target (%s) of a %s relocation is "
5633                      "in the wrong output section (%s)"),
5634                    input_bfd,
5635                    sym_name,
5636                    howto->name,
5637                    name);
5638
5639                 bfd_set_error (bfd_error_bad_value);
5640                 ret = FALSE;
5641                 continue;
5642               }
5643             addend -= htab->sdata[1].sym_val;
5644           }
5645           break;
5646
5647           /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
5648         case R_PPC_EMB_SDA21:
5649         case R_PPC_EMB_RELSDA:
5650           {
5651             const char *name;
5652             int reg;
5653
5654             BFD_ASSERT (sec != NULL);
5655             name = bfd_get_section_name (abfd, sec->output_section);
5656             if (((strncmp (name, ".sdata", 6) == 0
5657                   && (name[6] == 0 || name[6] == '.'))
5658                  || (strncmp (name, ".sbss", 5) == 0
5659                      && (name[5] == 0 || name[5] == '.'))))
5660               {
5661                 reg = 13;
5662                 addend -= htab->sdata[0].sym_val;
5663               }
5664
5665             else if (strncmp (name, ".sdata2", 7) == 0
5666                      || strncmp (name, ".sbss2", 6) == 0)
5667               {
5668                 reg = 2;
5669                 addend -= htab->sdata[1].sym_val;
5670               }
5671
5672             else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5673                      || strcmp (name, ".PPC.EMB.sbss0") == 0)
5674               {
5675                 reg = 0;
5676               }
5677
5678             else
5679               {
5680                 (*_bfd_error_handler)
5681                   (_("%B: the target (%s) of a %s relocation is "
5682                      "in the wrong output section (%s)"),
5683                    input_bfd,
5684                    sym_name,
5685                    howto->name,
5686                    name);
5687
5688                 bfd_set_error (bfd_error_bad_value);
5689                 ret = FALSE;
5690                 continue;
5691               }
5692
5693             if (r_type == R_PPC_EMB_SDA21)
5694               {                 /* fill in register field */
5695                 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5696                 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5697                 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5698               }
5699           }
5700           break;
5701
5702           /* Relocate against the beginning of the section.  */
5703         case R_PPC_SECTOFF:
5704         case R_PPC_SECTOFF_LO:
5705         case R_PPC_SECTOFF_HI:
5706         case R_PPC_SECTOFF_HA:
5707           BFD_ASSERT (sec != NULL);
5708           addend -= sec->output_section->vma;
5709           break;
5710
5711           /* Negative relocations.  */
5712         case R_PPC_EMB_NADDR32:
5713         case R_PPC_EMB_NADDR16:
5714         case R_PPC_EMB_NADDR16_LO:
5715         case R_PPC_EMB_NADDR16_HI:
5716         case R_PPC_EMB_NADDR16_HA:
5717           addend -= 2 * relocation;
5718           break;
5719
5720         case R_PPC_COPY:
5721         case R_PPC_GLOB_DAT:
5722         case R_PPC_JMP_SLOT:
5723         case R_PPC_RELATIVE:
5724         case R_PPC_PLT32:
5725         case R_PPC_PLTREL32:
5726         case R_PPC_PLT16_LO:
5727         case R_PPC_PLT16_HI:
5728         case R_PPC_PLT16_HA:
5729         case R_PPC_ADDR30:
5730         case R_PPC_EMB_RELSEC16:
5731         case R_PPC_EMB_RELST_LO:
5732         case R_PPC_EMB_RELST_HI:
5733         case R_PPC_EMB_RELST_HA:
5734         case R_PPC_EMB_BIT_FLD:
5735           (*_bfd_error_handler)
5736             (_("%B: relocation %s is not yet supported for symbol %s."),
5737              input_bfd,
5738              howto->name,
5739              sym_name);
5740
5741           bfd_set_error (bfd_error_invalid_operation);
5742           ret = FALSE;
5743           continue;
5744         }
5745
5746       /* Do any further special processing.  */
5747       switch (r_type)
5748         {
5749         default:
5750           break;
5751
5752         case R_PPC_ADDR16_HA:
5753         case R_PPC_GOT16_HA:
5754         case R_PPC_PLT16_HA:
5755         case R_PPC_SECTOFF_HA:
5756         case R_PPC_TPREL16_HA:
5757         case R_PPC_DTPREL16_HA:
5758         case R_PPC_GOT_TLSGD16_HA:
5759         case R_PPC_GOT_TLSLD16_HA:
5760         case R_PPC_GOT_TPREL16_HA:
5761         case R_PPC_GOT_DTPREL16_HA:
5762         case R_PPC_EMB_NADDR16_HA:
5763         case R_PPC_EMB_RELST_HA:
5764           /* It's just possible that this symbol is a weak symbol
5765              that's not actually defined anywhere.  In that case,
5766              'sec' would be NULL, and we should leave the symbol
5767              alone (it will be set to zero elsewhere in the link).  */
5768           if (sec != NULL)
5769             /* Add 0x10000 if sign bit in 0:15 is set.
5770                Bits 0:15 are not used.  */
5771             addend += 0x8000;
5772           break;
5773         }
5774
5775 #ifdef DEBUG
5776       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5777                "offset = %ld, addend = %ld\n",
5778                howto->name,
5779                (int) r_type,
5780                sym_name,
5781                r_symndx,
5782                (long) rel->r_offset,
5783                (long) addend);
5784 #endif
5785
5786       if (unresolved_reloc
5787           && !((input_section->flags & SEC_DEBUGGING) != 0
5788                && h->def_dynamic))
5789         {
5790           (*_bfd_error_handler)
5791             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5792              input_bfd,
5793              input_section,
5794              (long) rel->r_offset,
5795              howto->name,
5796              sym_name);
5797           ret = FALSE;
5798         }
5799
5800       r = _bfd_final_link_relocate (howto,
5801                                     input_bfd,
5802                                     input_section,
5803                                     contents,
5804                                     rel->r_offset,
5805                                     relocation,
5806                                     addend);
5807
5808       if (r != bfd_reloc_ok)
5809         {
5810           if (r == bfd_reloc_overflow)
5811             {
5812               if (warned)
5813                 continue;
5814               if (h != NULL
5815                   && h->root.type == bfd_link_hash_undefweak
5816                   && howto->pc_relative)
5817                 {
5818                   /* Assume this is a call protected by other code that
5819                      detect the symbol is undefined.  If this is the case,
5820                      we can safely ignore the overflow.  If not, the
5821                      program is hosed anyway, and a little warning isn't
5822                      going to help.  */
5823
5824                   continue;
5825                 }
5826
5827               if (! (*info->callbacks->reloc_overflow) (info,
5828                                                         (h ? &h->root : NULL),
5829                                                         sym_name,
5830                                                         howto->name,
5831                                                         rel->r_addend,
5832                                                         input_bfd,
5833                                                         input_section,
5834                                                         rel->r_offset))
5835                 return FALSE;
5836             }
5837           else
5838             {
5839               (*_bfd_error_handler)
5840                 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
5841                  input_bfd, input_section,
5842                  (long) rel->r_offset, howto->name, sym_name, (int) r);
5843               ret = FALSE;
5844             }
5845         }
5846     }
5847
5848 #ifdef DEBUG
5849   fprintf (stderr, "\n");
5850 #endif
5851
5852   return ret;
5853 }
5854 \f
5855 /* Finish up dynamic symbol handling.  We set the contents of various
5856    dynamic sections here.  */
5857
5858 static bfd_boolean
5859 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
5860                                struct bfd_link_info *info,
5861                                struct elf_link_hash_entry *h,
5862                                Elf_Internal_Sym *sym)
5863 {
5864   struct ppc_elf_link_hash_table *htab;
5865
5866 #ifdef DEBUG
5867   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
5868            h->root.root.string);
5869 #endif
5870
5871   htab = ppc_elf_hash_table (info);
5872   BFD_ASSERT (htab->elf.dynobj != NULL);
5873
5874   if (h->plt.offset != (bfd_vma) -1)
5875     {
5876       Elf_Internal_Rela rela;
5877       bfd_byte *loc;
5878       bfd_vma reloc_index;
5879
5880 #ifdef DEBUG
5881       fprintf (stderr, ", plt_offset = %d", h->plt.offset);
5882 #endif
5883
5884       /* This symbol has an entry in the procedure linkage table.  Set
5885          it up.  */
5886
5887       BFD_ASSERT (h->dynindx != -1);
5888       BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
5889
5890       /* We don't need to fill in the .plt.  The ppc dynamic linker
5891          will fill it in.  */
5892
5893       /* Fill in the entry in the .rela.plt section.  */
5894       rela.r_offset = (htab->plt->output_section->vma
5895                        + htab->plt->output_offset
5896                        + h->plt.offset);
5897       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
5898       rela.r_addend = 0;
5899
5900       reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
5901       if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
5902         reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
5903       loc = (htab->relplt->contents
5904              + reloc_index * sizeof (Elf32_External_Rela));
5905       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5906
5907       if (!h->def_regular)
5908         {
5909           /* Mark the symbol as undefined, rather than as defined in
5910              the .plt section.  Leave the value alone.  */
5911           sym->st_shndx = SHN_UNDEF;
5912           /* If the symbol is weak, we do need to clear the value.
5913              Otherwise, the PLT entry would provide a definition for
5914              the symbol even if the symbol wasn't defined anywhere,
5915              and so the symbol would never be NULL.  */
5916           if (!h->ref_regular_nonweak)
5917             sym->st_value = 0;
5918         }
5919     }
5920
5921   if (h->needs_copy)
5922     {
5923       asection *s;
5924       Elf_Internal_Rela rela;
5925       bfd_byte *loc;
5926
5927       /* This symbols needs a copy reloc.  Set it up.  */
5928
5929 #ifdef DEBUG
5930       fprintf (stderr, ", copy");
5931 #endif
5932
5933       BFD_ASSERT (h->dynindx != -1);
5934
5935       if (h->size <= elf_gp_size (htab->elf.dynobj))
5936         s = htab->relsbss;
5937       else
5938         s = htab->relbss;
5939       BFD_ASSERT (s != NULL);
5940
5941       rela.r_offset = (h->root.u.def.value
5942                        + h->root.u.def.section->output_section->vma
5943                        + h->root.u.def.section->output_offset);
5944       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
5945       rela.r_addend = 0;
5946       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5947       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5948     }
5949
5950 #ifdef DEBUG
5951   fprintf (stderr, "\n");
5952 #endif
5953
5954   /* Mark some specially defined symbols as absolute.  */
5955   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5956       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
5957       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
5958     sym->st_shndx = SHN_ABS;
5959
5960   return TRUE;
5961 }
5962 \f
5963 static enum elf_reloc_type_class
5964 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
5965 {
5966   switch (ELF32_R_TYPE (rela->r_info))
5967     {
5968     case R_PPC_RELATIVE:
5969       return reloc_class_relative;
5970     case R_PPC_REL24:
5971     case R_PPC_ADDR24:
5972     case R_PPC_JMP_SLOT:
5973       return reloc_class_plt;
5974     case R_PPC_COPY:
5975       return reloc_class_copy;
5976     default:
5977       return reloc_class_normal;
5978     }
5979 }
5980 \f
5981 /* Finish up the dynamic sections.  */
5982
5983 static bfd_boolean
5984 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
5985                                  struct bfd_link_info *info)
5986 {
5987   asection *sdyn;
5988   struct ppc_elf_link_hash_table *htab;
5989
5990 #ifdef DEBUG
5991   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
5992 #endif
5993
5994   htab = ppc_elf_hash_table (info);
5995   sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
5996
5997   if (htab->elf.dynamic_sections_created)
5998     {
5999       Elf32_External_Dyn *dyncon, *dynconend;
6000
6001       BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
6002
6003       dyncon = (Elf32_External_Dyn *) sdyn->contents;
6004       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6005       for (; dyncon < dynconend; dyncon++)
6006         {
6007           Elf_Internal_Dyn dyn;
6008           asection *s;
6009
6010           bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
6011
6012           switch (dyn.d_tag)
6013             {
6014             case DT_PLTGOT:
6015               s = htab->plt;
6016               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6017               break;
6018
6019             case DT_PLTRELSZ:
6020               dyn.d_un.d_val = htab->relplt->size;
6021               break;
6022
6023             case DT_JMPREL:
6024               s = htab->relplt;
6025               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6026               break;
6027
6028             default:
6029               continue;
6030             }
6031
6032           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6033         }
6034     }
6035
6036   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
6037      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
6038   if (htab->got)
6039     {
6040       unsigned char *contents = htab->got->contents;
6041       bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
6042
6043       if (sdyn == NULL)
6044         bfd_put_32 (output_bfd, 0, contents + 4);
6045       else
6046         bfd_put_32 (output_bfd,
6047                     sdyn->output_section->vma + sdyn->output_offset,
6048                     contents + 4);
6049
6050       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
6051     }
6052
6053   return TRUE;
6054 }
6055 \f
6056 #define TARGET_LITTLE_SYM       bfd_elf32_powerpcle_vec
6057 #define TARGET_LITTLE_NAME      "elf32-powerpcle"
6058 #define TARGET_BIG_SYM          bfd_elf32_powerpc_vec
6059 #define TARGET_BIG_NAME         "elf32-powerpc"
6060 #define ELF_ARCH                bfd_arch_powerpc
6061 #define ELF_MACHINE_CODE        EM_PPC
6062 #ifdef __QNXTARGET__
6063 #define ELF_MAXPAGESIZE         0x1000
6064 #else
6065 #define ELF_MAXPAGESIZE         0x10000
6066 #endif
6067 #define ELF_MINPAGESIZE         0x1000
6068 #define elf_info_to_howto       ppc_elf_info_to_howto
6069
6070 #ifdef  EM_CYGNUS_POWERPC
6071 #define ELF_MACHINE_ALT1        EM_CYGNUS_POWERPC
6072 #endif
6073
6074 #ifdef EM_PPC_OLD
6075 #define ELF_MACHINE_ALT2        EM_PPC_OLD
6076 #endif
6077
6078 #define elf_backend_plt_not_loaded      1
6079 #define elf_backend_got_symbol_offset   4
6080 #define elf_backend_can_gc_sections     1
6081 #define elf_backend_can_refcount        1
6082 #define elf_backend_got_header_size     12
6083 #define elf_backend_rela_normal         1
6084
6085 #define bfd_elf32_mkobject                      ppc_elf_mkobject
6086 #define bfd_elf32_bfd_merge_private_bfd_data    ppc_elf_merge_private_bfd_data
6087 #define bfd_elf32_bfd_relax_section             ppc_elf_relax_section
6088 #define bfd_elf32_bfd_reloc_type_lookup         ppc_elf_reloc_type_lookup
6089 #define bfd_elf32_bfd_set_private_flags         ppc_elf_set_private_flags
6090 #define bfd_elf32_bfd_link_hash_table_create    ppc_elf_link_hash_table_create
6091
6092 #define elf_backend_object_p                    ppc_elf_object_p
6093 #define elf_backend_gc_mark_hook                ppc_elf_gc_mark_hook
6094 #define elf_backend_gc_sweep_hook               ppc_elf_gc_sweep_hook
6095 #define elf_backend_section_from_shdr           ppc_elf_section_from_shdr
6096 #define elf_backend_relocate_section            ppc_elf_relocate_section
6097 #define elf_backend_create_dynamic_sections     ppc_elf_create_dynamic_sections
6098 #define elf_backend_check_relocs                ppc_elf_check_relocs
6099 #define elf_backend_copy_indirect_symbol        ppc_elf_copy_indirect_symbol
6100 #define elf_backend_adjust_dynamic_symbol       ppc_elf_adjust_dynamic_symbol
6101 #define elf_backend_add_symbol_hook             ppc_elf_add_symbol_hook
6102 #define elf_backend_size_dynamic_sections       ppc_elf_size_dynamic_sections
6103 #define elf_backend_finish_dynamic_symbol       ppc_elf_finish_dynamic_symbol
6104 #define elf_backend_finish_dynamic_sections     ppc_elf_finish_dynamic_sections
6105 #define elf_backend_fake_sections               ppc_elf_fake_sections
6106 #define elf_backend_additional_program_headers  ppc_elf_additional_program_headers
6107 #define elf_backend_grok_prstatus               ppc_elf_grok_prstatus
6108 #define elf_backend_grok_psinfo                 ppc_elf_grok_psinfo
6109 #define elf_backend_reloc_type_class            ppc_elf_reloc_type_class
6110 #define elf_backend_begin_write_processing      ppc_elf_begin_write_processing
6111 #define elf_backend_final_write_processing      ppc_elf_final_write_processing
6112 #define elf_backend_write_section               ppc_elf_write_section
6113 #define elf_backend_special_sections            ppc_elf_special_sections
6114 #define elf_backend_plt_sym_val                 ppc_elf_plt_sym_val
6115
6116 #include "elf32-target.h"