* elf32-ppc.c (ppc_elf_set_sdata_syms): New function, extracted from..
[platform/upstream/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., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, 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   /* whether address was written yet */
1536   bfd_boolean written_address_p;
1537 } elf_linker_section_pointers_t;
1538
1539 struct ppc_elf_obj_tdata
1540 {
1541   struct elf_obj_tdata elf;
1542
1543   /* A mapping from local symbols to offsets into the various linker
1544      sections added.  This is index by the symbol index.  */
1545   elf_linker_section_pointers_t **linker_section_pointers;
1546 };
1547
1548 #define ppc_elf_tdata(bfd) \
1549   ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1550
1551 #define elf_local_ptr_offsets(bfd) \
1552   (ppc_elf_tdata (bfd)->linker_section_pointers)
1553
1554 /* Override the generic function because we store some extras.  */
1555
1556 static bfd_boolean
1557 ppc_elf_mkobject (bfd *abfd)
1558 {
1559   bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
1560   abfd->tdata.any = bfd_zalloc (abfd, amt);
1561   if (abfd->tdata.any == NULL)
1562     return FALSE;
1563   return TRUE;
1564 }
1565
1566 /* Fix bad default arch selected for a 32 bit input bfd when the
1567    default is 64 bit.  */
1568
1569 static bfd_boolean
1570 ppc_elf_object_p (bfd *abfd)
1571 {
1572   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1573     {
1574       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1575
1576       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1577         {
1578           /* Relies on arch after 64 bit default being 32 bit default.  */
1579           abfd->arch_info = abfd->arch_info->next;
1580           BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1581         }
1582     }
1583   return TRUE;
1584 }
1585
1586 /* Function to set whether a module needs the -mrelocatable bit set.  */
1587
1588 static bfd_boolean
1589 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1590 {
1591   BFD_ASSERT (!elf_flags_init (abfd)
1592               || elf_elfheader (abfd)->e_flags == flags);
1593
1594   elf_elfheader (abfd)->e_flags = flags;
1595   elf_flags_init (abfd) = TRUE;
1596   return TRUE;
1597 }
1598
1599 /* Support for core dump NOTE sections.  */
1600
1601 static bfd_boolean
1602 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1603 {
1604   int offset;
1605   unsigned int size;
1606
1607   switch (note->descsz)
1608     {
1609     default:
1610       return FALSE;
1611
1612     case 268:           /* Linux/PPC.  */
1613       /* pr_cursig */
1614       elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1615
1616       /* pr_pid */
1617       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1618
1619       /* pr_reg */
1620       offset = 72;
1621       size = 192;
1622
1623       break;
1624     }
1625
1626   /* Make a ".reg/999" section.  */
1627   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1628                                           size, note->descpos + offset);
1629 }
1630
1631 static bfd_boolean
1632 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1633 {
1634   switch (note->descsz)
1635     {
1636     default:
1637       return FALSE;
1638
1639     case 128:           /* Linux/PPC elf_prpsinfo.  */
1640       elf_tdata (abfd)->core_program
1641         = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1642       elf_tdata (abfd)->core_command
1643         = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1644     }
1645
1646   /* Note that for some reason, a spurious space is tacked
1647      onto the end of the args in some (at least one anyway)
1648      implementations, so strip it off if it exists.  */
1649
1650   {
1651     char *command = elf_tdata (abfd)->core_command;
1652     int n = strlen (command);
1653
1654     if (0 < n && command[n - 1] == ' ')
1655       command[n - 1] = '\0';
1656   }
1657
1658   return TRUE;
1659 }
1660
1661 /* Return address for Ith PLT stub in section PLT, for relocation REL
1662    or (bfd_vma) -1 if it should not be included.  */
1663
1664 static bfd_vma
1665 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1666                      const asection *plt ATTRIBUTE_UNUSED,
1667                      const arelent *rel)
1668 {
1669   return rel->address;
1670 }
1671
1672 /* Handle a PowerPC specific section when reading an object file.  This
1673    is called when bfd_section_from_shdr finds a section with an unknown
1674    type.  */
1675
1676 static bfd_boolean
1677 ppc_elf_section_from_shdr (bfd *abfd,
1678                            Elf_Internal_Shdr *hdr,
1679                            const char *name,
1680                            int shindex)
1681 {
1682   asection *newsect;
1683   flagword flags;
1684
1685   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1686     return FALSE;
1687
1688   newsect = hdr->bfd_section;
1689   flags = bfd_get_section_flags (abfd, newsect);
1690   if (hdr->sh_flags & SHF_EXCLUDE)
1691     flags |= SEC_EXCLUDE;
1692
1693   if (hdr->sh_type == SHT_ORDERED)
1694     flags |= SEC_SORT_ENTRIES;
1695
1696   bfd_set_section_flags (abfd, newsect, flags);
1697   return TRUE;
1698 }
1699
1700 /* Set up any other section flags and such that may be necessary.  */
1701
1702 static bfd_boolean
1703 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1704                        Elf_Internal_Shdr *shdr,
1705                        asection *asect)
1706 {
1707   if ((asect->flags & SEC_EXCLUDE) != 0)
1708     shdr->sh_flags |= SHF_EXCLUDE;
1709
1710   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1711     shdr->sh_type = SHT_ORDERED;
1712
1713   return TRUE;
1714 }
1715
1716 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
1717    need to bump up the number of section headers.  */
1718
1719 static int
1720 ppc_elf_additional_program_headers (bfd *abfd)
1721 {
1722   asection *s;
1723   int ret = 0;
1724
1725   s = bfd_get_section_by_name (abfd, ".sbss2");
1726   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1727     ++ret;
1728
1729   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1730   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1731     ++ret;
1732
1733   return ret;
1734 }
1735
1736 /* Add extra PPC sections -- Note, for now, make .sbss2 and
1737    .PPC.EMB.sbss0 a normal section, and not a bss section so
1738    that the linker doesn't crater when trying to make more than
1739    2 sections.  */
1740
1741 static struct bfd_elf_special_section const ppc_elf_special_sections[]=
1742 {
1743   { ".tags",             5,  0, SHT_ORDERED,  SHF_ALLOC },
1744   { ".sdata",            6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1745   { ".sbss",             5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1746   { ".sdata2",           7, -2, SHT_PROGBITS, SHF_ALLOC },
1747   { ".sbss2",            6, -2, SHT_PROGBITS, SHF_ALLOC },
1748   { ".PPC.EMB.apuinfo", 16,  0, SHT_NOTE,     0 },
1749   { ".PPC.EMB.sdata0",  15,  0, SHT_PROGBITS, SHF_ALLOC },
1750   { ".PPC.EMB.sbss0",   14,  0, SHT_PROGBITS, SHF_ALLOC },
1751   { ".plt",              4,  0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
1752   { NULL,                0,  0, 0,            0 }
1753 };
1754 \f
1755 /* Very simple linked list structure for recording apuinfo values.  */
1756 typedef struct apuinfo_list
1757 {
1758   struct apuinfo_list *next;
1759   unsigned long value;
1760 }
1761 apuinfo_list;
1762
1763 static apuinfo_list *head;
1764
1765
1766 static void
1767 apuinfo_list_init (void)
1768 {
1769   head = NULL;
1770 }
1771
1772 static void
1773 apuinfo_list_add (unsigned long value)
1774 {
1775   apuinfo_list *entry = head;
1776
1777   while (entry != NULL)
1778     {
1779       if (entry->value == value)
1780         return;
1781       entry = entry->next;
1782     }
1783
1784   entry = bfd_malloc (sizeof (* entry));
1785   if (entry == NULL)
1786     return;
1787
1788   entry->value = value;
1789   entry->next  = head;
1790   head = entry;
1791 }
1792
1793 static unsigned
1794 apuinfo_list_length (void)
1795 {
1796   apuinfo_list *entry;
1797   unsigned long count;
1798
1799   for (entry = head, count = 0;
1800        entry;
1801        entry = entry->next)
1802     ++ count;
1803
1804   return count;
1805 }
1806
1807 static inline unsigned long
1808 apuinfo_list_element (unsigned long number)
1809 {
1810   apuinfo_list * entry;
1811
1812   for (entry = head;
1813        entry && number --;
1814        entry = entry->next)
1815     ;
1816
1817   return entry ? entry->value : 0;
1818 }
1819
1820 static void
1821 apuinfo_list_finish (void)
1822 {
1823   apuinfo_list *entry;
1824
1825   for (entry = head; entry;)
1826     {
1827       apuinfo_list *next = entry->next;
1828       free (entry);
1829       entry = next;
1830     }
1831
1832   head = NULL;
1833 }
1834
1835 #define APUINFO_SECTION_NAME    ".PPC.EMB.apuinfo"
1836 #define APUINFO_LABEL           "APUinfo"
1837
1838 /* Scan the input BFDs and create a linked list of
1839    the APUinfo values that will need to be emitted.  */
1840
1841 static void
1842 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
1843 {
1844   bfd *ibfd;
1845   asection *asec;
1846   char *buffer;
1847   unsigned num_input_sections;
1848   bfd_size_type output_section_size;
1849   unsigned i;
1850   unsigned num_entries;
1851   unsigned long offset;
1852   unsigned long length;
1853   const char *error_message = NULL;
1854
1855   if (link_info == NULL)
1856     return;
1857
1858   /* Scan the input bfds, looking for apuinfo sections.  */
1859   num_input_sections = 0;
1860   output_section_size = 0;
1861
1862   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
1863     {
1864       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1865       if (asec)
1866         {
1867           ++ num_input_sections;
1868           output_section_size += asec->size;
1869         }
1870     }
1871
1872   /* We need at least one input sections
1873      in order to make merging worthwhile.  */
1874   if (num_input_sections < 1)
1875     return;
1876
1877   /* Just make sure that the output section exists as well.  */
1878   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1879   if (asec == NULL)
1880     return;
1881
1882   /* Allocate a buffer for the contents of the input sections.  */
1883   buffer = bfd_malloc (output_section_size);
1884   if (buffer == NULL)
1885     return;
1886
1887   offset = 0;
1888   apuinfo_list_init ();
1889
1890   /* Read in the input sections contents.  */
1891   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
1892     {
1893       unsigned long datum;
1894       char *ptr;
1895
1896       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1897       if (asec == NULL)
1898         continue;
1899
1900       length = asec->size;
1901       if (length < 24)
1902         {
1903           error_message = _("corrupt or empty %s section in %B");
1904           goto fail;
1905         }
1906
1907       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
1908           || (bfd_bread (buffer + offset, length, ibfd) != length))
1909         {
1910           error_message = _("unable to read in %s section from %B");
1911           goto fail;
1912         }
1913
1914       /* Process the contents of the section.  */
1915       ptr = buffer + offset;
1916       error_message = _("corrupt %s section in %B");
1917
1918       /* Verify the contents of the header.  Note - we have to
1919          extract the values this way in order to allow for a
1920          host whose endian-ness is different from the target.  */
1921       datum = bfd_get_32 (ibfd, ptr);
1922       if (datum != sizeof APUINFO_LABEL)
1923         goto fail;
1924
1925       datum = bfd_get_32 (ibfd, ptr + 8);
1926       if (datum != 0x2)
1927         goto fail;
1928
1929       if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
1930         goto fail;
1931
1932       /* Get the number of bytes used for apuinfo entries.  */
1933       datum = bfd_get_32 (ibfd, ptr + 4);
1934       if (datum + 20 != length)
1935         goto fail;
1936
1937       /* Make sure that we do not run off the end of the section.  */
1938       if (offset + length > output_section_size)
1939         goto fail;
1940
1941       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
1942       for (i = 0; i < datum; i += 4)
1943         apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
1944
1945       /* Update the offset.  */
1946       offset += length;
1947     }
1948
1949   error_message = NULL;
1950
1951   /* Compute the size of the output section.  */
1952   num_entries = apuinfo_list_length ();
1953   output_section_size = 20 + num_entries * 4;
1954
1955   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1956
1957   if (! bfd_set_section_size (abfd, asec, output_section_size))
1958     ibfd = abfd,
1959       error_message = _("warning: unable to set size of %s section in %B");
1960
1961  fail:
1962   free (buffer);
1963
1964   if (error_message)
1965     (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
1966 }
1967
1968 /* Prevent the output section from accumulating the input sections'
1969    contents.  We have already stored this in our linked list structure.  */
1970
1971 static bfd_boolean
1972 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
1973                        asection *asec,
1974                        bfd_byte *contents ATTRIBUTE_UNUSED)
1975 {
1976   return (apuinfo_list_length ()
1977           && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
1978 }
1979
1980 /* Finally we can generate the output section.  */
1981
1982 static void
1983 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
1984 {
1985   bfd_byte *buffer;
1986   asection *asec;
1987   unsigned i;
1988   unsigned num_entries;
1989   bfd_size_type length;
1990
1991   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1992   if (asec == NULL)
1993     return;
1994
1995   if (apuinfo_list_length () == 0)
1996     return;
1997
1998   length = asec->size;
1999   if (length < 20)
2000     return;
2001
2002   buffer = bfd_malloc (length);
2003   if (buffer == NULL)
2004     {
2005       (*_bfd_error_handler)
2006         (_("failed to allocate space for new APUinfo section."));
2007       return;
2008     }
2009
2010   /* Create the apuinfo header.  */
2011   num_entries = apuinfo_list_length ();
2012   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2013   bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2014   bfd_put_32 (abfd, 0x2, buffer + 8);
2015   strcpy ((char *) buffer + 12, APUINFO_LABEL);
2016
2017   length = 20;
2018   for (i = 0; i < num_entries; i++)
2019     {
2020       bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2021       length += 4;
2022     }
2023
2024   if (length != asec->size)
2025     (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2026
2027   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2028     (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2029
2030   free (buffer);
2031
2032   apuinfo_list_finish ();
2033 }
2034 \f
2035 /* The following functions are specific to the ELF linker, while
2036    functions above are used generally.  They appear in this file more
2037    or less in the order in which they are called.  eg.
2038    ppc_elf_check_relocs is called early in the link process,
2039    ppc_elf_finish_dynamic_sections is one of the last functions
2040    called.  */
2041
2042 /* The PPC linker needs to keep track of the number of relocs that it
2043    decides to copy as dynamic relocs in check_relocs for each symbol.
2044    This is so that it can later discard them if they are found to be
2045    unnecessary.  We store the information in a field extending the
2046    regular ELF linker hash table.  */
2047
2048 struct ppc_elf_dyn_relocs
2049 {
2050   struct ppc_elf_dyn_relocs *next;
2051
2052   /* The input section of the reloc.  */
2053   asection *sec;
2054
2055   /* Total number of relocs copied for the input section.  */
2056   bfd_size_type count;
2057
2058   /* Number of pc-relative relocs copied for the input section.  */
2059   bfd_size_type pc_count;
2060 };
2061
2062 /* Of those relocs that might be copied as dynamic relocs, this macro
2063    selects those that must be copied when linking a shared library,
2064    even when the symbol is local.  */
2065
2066 #define MUST_BE_DYN_RELOC(RTYPE)                \
2067   ((RTYPE) != R_PPC_REL24                       \
2068    && (RTYPE) != R_PPC_REL14                    \
2069    && (RTYPE) != R_PPC_REL14_BRTAKEN            \
2070    && (RTYPE) != R_PPC_REL14_BRNTAKEN           \
2071    && (RTYPE) != R_PPC_REL32)
2072
2073 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2074    copying dynamic variables from a shared lib into an app's dynbss
2075    section, and instead use a dynamic relocation to point into the
2076    shared lib.  */
2077 #define ELIMINATE_COPY_RELOCS 1
2078
2079 /* PPC ELF linker hash entry.  */
2080
2081 struct ppc_elf_link_hash_entry
2082 {
2083   struct elf_link_hash_entry elf;
2084
2085   /* If this symbol is used in the linker created sections, the processor
2086      specific backend uses this field to map the field into the offset
2087      from the beginning of the section.  */
2088   elf_linker_section_pointers_t *linker_section_pointer;
2089
2090   /* Track dynamic relocs copied for this symbol.  */
2091   struct ppc_elf_dyn_relocs *dyn_relocs;
2092
2093   /* Contexts in which symbol is used in the GOT (or TOC).
2094      TLS_GD .. TLS_TLS bits are or'd into the mask as the
2095      corresponding relocs are encountered during check_relocs.
2096      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2097      indicate the corresponding GOT entry type is not needed.  */
2098 #define TLS_GD           1      /* GD reloc. */
2099 #define TLS_LD           2      /* LD reloc. */
2100 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
2101 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
2102 #define TLS_TLS         16      /* Any TLS reloc.  */
2103 #define TLS_TPRELGD     32      /* TPREL reloc resulting from GD->IE. */
2104   char tls_mask;
2105 };
2106
2107 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2108
2109 /* PPC ELF linker hash table.  */
2110
2111 struct ppc_elf_link_hash_table
2112 {
2113   struct elf_link_hash_table elf;
2114
2115   /* Short-cuts to get to dynamic linker sections.  */
2116   asection *got;
2117   asection *relgot;
2118   asection *plt;
2119   asection *relplt;
2120   asection *dynbss;
2121   asection *relbss;
2122   asection *dynsbss;
2123   asection *relsbss;
2124   elf_linker_section_t sdata[2];
2125   asection *sbss;
2126
2127   /* Shortcut to .__tls_get_addr.  */
2128   struct elf_link_hash_entry *tls_get_addr;
2129
2130   /* TLS local dynamic got entry handling.  */
2131   union {
2132     bfd_signed_vma refcount;
2133     bfd_vma offset;
2134   } tlsld_got;
2135
2136   /* Small local sym to section mapping cache.  */
2137   struct sym_sec_cache sym_sec;
2138 };
2139
2140 /* Get the PPC ELF linker hash table from a link_info structure.  */
2141
2142 #define ppc_elf_hash_table(p) \
2143   ((struct ppc_elf_link_hash_table *) (p)->hash)
2144
2145 /* Create an entry in a PPC ELF linker hash table.  */
2146
2147 static struct bfd_hash_entry *
2148 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2149                            struct bfd_hash_table *table,
2150                            const char *string)
2151 {
2152   /* Allocate the structure if it has not already been allocated by a
2153      subclass.  */
2154   if (entry == NULL)
2155     {
2156       entry = bfd_hash_allocate (table,
2157                                  sizeof (struct ppc_elf_link_hash_entry));
2158       if (entry == NULL)
2159         return entry;
2160     }
2161
2162   /* Call the allocation method of the superclass.  */
2163   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2164   if (entry != NULL)
2165     {
2166       ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2167       ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2168       ppc_elf_hash_entry (entry)->tls_mask = 0;
2169     }
2170
2171   return entry;
2172 }
2173
2174 /* Create a PPC ELF linker hash table.  */
2175
2176 static struct bfd_link_hash_table *
2177 ppc_elf_link_hash_table_create (bfd *abfd)
2178 {
2179   struct ppc_elf_link_hash_table *ret;
2180
2181   ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2182   if (ret == NULL)
2183     return NULL;
2184
2185   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
2186                                        ppc_elf_link_hash_newfunc))
2187     {
2188       free (ret);
2189       return NULL;
2190     }
2191
2192   ret->sdata[0].name = ".sdata";
2193   ret->sdata[0].sym_name = "_SDA_BASE_";
2194   ret->sdata[0].bss_name = ".sbss";
2195
2196   ret->sdata[1].name = ".sdata2";
2197   ret->sdata[1].sym_name = "_SDA2_BASE_";
2198   ret->sdata[1].bss_name = ".sbss2";
2199
2200   return &ret->elf.root;
2201 }
2202
2203 /* The powerpc .got has a blrl instruction in it.  Mark it executable.  */
2204
2205 static bfd_boolean
2206 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2207 {
2208   struct ppc_elf_link_hash_table *htab;
2209   asection *s;
2210   flagword flags;
2211
2212   if (!_bfd_elf_create_got_section (abfd, info))
2213     return FALSE;
2214
2215   htab = ppc_elf_hash_table (info);
2216   htab->got = s = bfd_get_section_by_name (abfd, ".got");
2217   if (s == NULL)
2218     abort ();
2219
2220   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2221            | SEC_LINKER_CREATED);
2222   if (!bfd_set_section_flags (abfd, s, flags))
2223     return FALSE;
2224
2225   htab->relgot = bfd_make_section (abfd, ".rela.got");
2226   if (!htab->relgot
2227       || ! bfd_set_section_flags (abfd, htab->relgot,
2228                                   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2229                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED
2230                                    | SEC_READONLY))
2231       || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2232     return FALSE;
2233
2234   return TRUE;
2235 }
2236
2237 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2238    to output sections (just like _bfd_elf_create_dynamic_sections has
2239    to create .dynbss and .rela.bss).  */
2240
2241 static bfd_boolean
2242 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2243 {
2244   struct ppc_elf_link_hash_table *htab;
2245   asection *s;
2246   flagword flags;
2247
2248   htab = ppc_elf_hash_table (info);
2249
2250   if (htab->got == NULL
2251       && !ppc_elf_create_got (abfd, info))
2252     return FALSE;
2253
2254   if (!_bfd_elf_create_dynamic_sections (abfd, info))
2255     return FALSE;
2256
2257   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2258            | SEC_LINKER_CREATED);
2259
2260   htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2261   htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2262   if (s == NULL
2263       || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
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 (abfd, ".rela.sbss");
2270       if (s == NULL
2271           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2272           || ! bfd_set_section_alignment (abfd, s, 2))
2273         return FALSE;
2274     }
2275
2276   htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2277   htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2278   if (s == NULL)
2279     abort ();
2280
2281   flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2282   return bfd_set_section_flags (abfd, s, flags);
2283 }
2284
2285 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2286
2287 static void
2288 ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
2289                               struct elf_link_hash_entry *dir,
2290                               struct elf_link_hash_entry *ind)
2291 {
2292   struct ppc_elf_link_hash_entry *edir, *eind;
2293
2294   edir = (struct ppc_elf_link_hash_entry *) dir;
2295   eind = (struct ppc_elf_link_hash_entry *) ind;
2296
2297   if (eind->dyn_relocs != NULL)
2298     {
2299       if (edir->dyn_relocs != NULL)
2300         {
2301           struct ppc_elf_dyn_relocs **pp;
2302           struct ppc_elf_dyn_relocs *p;
2303
2304           if (ind->root.type == bfd_link_hash_indirect)
2305             abort ();
2306
2307           /* Add reloc counts against the weak sym to the strong sym
2308              list.  Merge any entries against the same section.  */
2309           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2310             {
2311               struct ppc_elf_dyn_relocs *q;
2312
2313               for (q = edir->dyn_relocs; q != NULL; q = q->next)
2314                 if (q->sec == p->sec)
2315                   {
2316                     q->pc_count += p->pc_count;
2317                     q->count += p->count;
2318                     *pp = p->next;
2319                     break;
2320                   }
2321               if (q == NULL)
2322                 pp = &p->next;
2323             }
2324           *pp = edir->dyn_relocs;
2325         }
2326
2327       edir->dyn_relocs = eind->dyn_relocs;
2328       eind->dyn_relocs = NULL;
2329     }
2330
2331   edir->tls_mask |= eind->tls_mask;
2332
2333   if (ELIMINATE_COPY_RELOCS
2334       && ind->root.type != bfd_link_hash_indirect
2335       && dir->dynamic_adjusted)
2336     {
2337       /* If called to transfer flags for a weakdef during processing
2338          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
2339          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
2340       dir->ref_dynamic |= ind->ref_dynamic;
2341       dir->ref_regular |= ind->ref_regular;
2342       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
2343       dir->needs_plt |= ind->needs_plt;
2344     }
2345   else
2346     _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
2347 }
2348
2349 /* Return 1 if target is one of ours.  */
2350
2351 static bfd_boolean
2352 is_ppc_elf_target (const struct bfd_target *targ)
2353 {
2354   extern const bfd_target bfd_elf32_powerpc_vec;
2355   extern const bfd_target bfd_elf32_powerpcle_vec;
2356
2357   return targ == &bfd_elf32_powerpc_vec || targ == &bfd_elf32_powerpcle_vec;
2358 }
2359
2360 /* Hook called by the linker routine which adds symbols from an object
2361    file.  We use it to put .comm items in .sbss, and not .bss.  */
2362
2363 static bfd_boolean
2364 ppc_elf_add_symbol_hook (bfd *abfd,
2365                          struct bfd_link_info *info,
2366                          Elf_Internal_Sym *sym,
2367                          const char **namep ATTRIBUTE_UNUSED,
2368                          flagword *flagsp ATTRIBUTE_UNUSED,
2369                          asection **secp,
2370                          bfd_vma *valp)
2371 {
2372   if (sym->st_shndx == SHN_COMMON
2373       && !info->relocatable
2374       && sym->st_size <= elf_gp_size (abfd)
2375       && is_ppc_elf_target (info->hash->creator))
2376     {
2377       /* Common symbols less than or equal to -G nn bytes are automatically
2378          put into .sbss.  */
2379       struct ppc_elf_link_hash_table *htab;
2380
2381       htab = ppc_elf_hash_table (info);
2382       if (htab->sbss == NULL)
2383         {
2384           flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
2385
2386           if (!htab->elf.dynobj)
2387             htab->elf.dynobj = abfd;
2388
2389           htab->sbss = bfd_make_section_anyway (htab->elf.dynobj, ".sbss");
2390           if (htab->sbss == NULL
2391               || ! bfd_set_section_flags (htab->elf.dynobj, htab->sbss, flags))
2392             return FALSE;
2393         }
2394
2395       *secp = htab->sbss;
2396       *valp = sym->st_size;
2397     }
2398
2399   return TRUE;
2400 }
2401 \f
2402 /* Create a special linker section.  */
2403
2404 static bfd_boolean
2405 ppc_elf_create_linker_section (bfd *abfd,
2406                                struct bfd_link_info *info,
2407                                flagword flags,
2408                                elf_linker_section_t *lsect)
2409 {
2410   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2411   asection *s;
2412
2413   flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2414             | SEC_LINKER_CREATED);
2415
2416   /* Record the first bfd that needs the special sections.  */
2417   if (!htab->elf.dynobj)
2418     htab->elf.dynobj = abfd;
2419
2420   /* See if the section already exists.  */
2421   s = bfd_get_section_by_name (htab->elf.dynobj, lsect->name);
2422   if (s == NULL || (s->flags & flags) != flags)
2423     {
2424       s = bfd_make_section_anyway (htab->elf.dynobj, lsect->name);
2425       if (s == NULL
2426           || !bfd_set_section_flags (htab->elf.dynobj, s, flags))
2427         return FALSE;
2428     }
2429   lsect->section = s;
2430
2431   if (bfd_get_section_alignment (htab->elf.dynobj, s) < 2
2432       && !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2433     return FALSE;
2434
2435   s->size = align_power (s->size, 2);
2436
2437 #ifdef DEBUG
2438   fprintf (stderr, "Creating section %s, current size = %ld\n",
2439            lsect->name, (long) s->size);
2440 #endif
2441
2442   return TRUE;
2443 }
2444
2445 /* Find a linker generated pointer with a given addend and type.  */
2446
2447 static elf_linker_section_pointers_t *
2448 elf_find_pointer_linker_section
2449   (elf_linker_section_pointers_t *linker_pointers,
2450    bfd_vma addend,
2451    elf_linker_section_t *lsect)
2452 {
2453   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2454     if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2455       return linker_pointers;
2456
2457   return NULL;
2458 }
2459
2460 /* Allocate a pointer to live in a linker created section.  */
2461
2462 static bfd_boolean
2463 elf_create_pointer_linker_section (bfd *abfd,
2464                                    struct bfd_link_info *info,
2465                                    elf_linker_section_t *lsect,
2466                                    struct elf_link_hash_entry *h,
2467                                    const Elf_Internal_Rela *rel)
2468 {
2469   elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2470   elf_linker_section_pointers_t *linker_section_ptr;
2471   unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2472   bfd_size_type amt;
2473
2474   BFD_ASSERT (lsect != NULL);
2475
2476   /* Is this a global symbol?  */
2477   if (h != NULL)
2478     {
2479       struct ppc_elf_link_hash_entry *eh;
2480
2481       /* Has this symbol already been allocated?  If so, our work is done.  */
2482       eh = (struct ppc_elf_link_hash_entry *) h;
2483       if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2484                                            rel->r_addend,
2485                                            lsect))
2486         return TRUE;
2487
2488       ptr_linker_section_ptr = &eh->linker_section_pointer;
2489       /* Make sure this symbol is output as a dynamic symbol.  */
2490       if (h->dynindx == -1)
2491         {
2492           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2493             return FALSE;
2494         }
2495     }
2496   else
2497     {
2498       /* Allocation of a pointer to a local symbol.  */
2499       elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2500
2501       /* Allocate a table to hold the local symbols if first time.  */
2502       if (!ptr)
2503         {
2504           unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2505
2506           amt = num_symbols;
2507           amt *= sizeof (elf_linker_section_pointers_t *);
2508           ptr = bfd_zalloc (abfd, amt);
2509
2510           if (!ptr)
2511             return FALSE;
2512
2513           elf_local_ptr_offsets (abfd) = ptr;
2514         }
2515
2516       /* Has this symbol already been allocated?  If so, our work is done.  */
2517       if (elf_find_pointer_linker_section (ptr[r_symndx],
2518                                            rel->r_addend,
2519                                            lsect))
2520         return TRUE;
2521
2522       ptr_linker_section_ptr = &ptr[r_symndx];
2523     }
2524
2525   /* Allocate space for a pointer in the linker section, and allocate
2526      a new pointer record from internal memory.  */
2527   BFD_ASSERT (ptr_linker_section_ptr != NULL);
2528   amt = sizeof (elf_linker_section_pointers_t);
2529   linker_section_ptr = bfd_alloc (abfd, amt);
2530
2531   if (!linker_section_ptr)
2532     return FALSE;
2533
2534   linker_section_ptr->next = *ptr_linker_section_ptr;
2535   linker_section_ptr->addend = rel->r_addend;
2536   linker_section_ptr->lsect = lsect;
2537   linker_section_ptr->written_address_p = FALSE;
2538   *ptr_linker_section_ptr = linker_section_ptr;
2539
2540   linker_section_ptr->offset = lsect->section->size;
2541   lsect->section->size += 4;
2542
2543 #ifdef DEBUG
2544   fprintf (stderr,
2545            "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2546            lsect->name, (long) linker_section_ptr->offset,
2547            (long) lsect->section->size);
2548 #endif
2549
2550   return TRUE;
2551 }
2552
2553 static bfd_boolean
2554 update_local_sym_info (bfd *abfd,
2555                        Elf_Internal_Shdr *symtab_hdr,
2556                        unsigned long r_symndx,
2557                        int tls_type)
2558 {
2559   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
2560   char *local_got_tls_masks;
2561
2562   if (local_got_refcounts == NULL)
2563     {
2564       bfd_size_type size = symtab_hdr->sh_info;
2565
2566       size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
2567       local_got_refcounts = bfd_zalloc (abfd, size);
2568       if (local_got_refcounts == NULL)
2569         return FALSE;
2570       elf_local_got_refcounts (abfd) = local_got_refcounts;
2571     }
2572
2573   local_got_refcounts[r_symndx] += 1;
2574   local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2575   local_got_tls_masks[r_symndx] |= tls_type;
2576   return TRUE;
2577 }
2578
2579 static void
2580 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
2581 {
2582   (*_bfd_error_handler)
2583     (_("%B: relocation %s cannot be used when making a shared object"),
2584      abfd,
2585      ppc_elf_howto_table[r_type]->name);
2586   bfd_set_error (bfd_error_bad_value);
2587 }
2588
2589 /* Look through the relocs for a section during the first phase, and
2590    allocate space in the global offset table or procedure linkage
2591    table.  */
2592
2593 static bfd_boolean
2594 ppc_elf_check_relocs (bfd *abfd,
2595                       struct bfd_link_info *info,
2596                       asection *sec,
2597                       const Elf_Internal_Rela *relocs)
2598 {
2599   struct ppc_elf_link_hash_table *htab;
2600   Elf_Internal_Shdr *symtab_hdr;
2601   struct elf_link_hash_entry **sym_hashes;
2602   const Elf_Internal_Rela *rel;
2603   const Elf_Internal_Rela *rel_end;
2604   asection *sreloc;
2605
2606   if (info->relocatable)
2607     return TRUE;
2608
2609 #ifdef DEBUG
2610   _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
2611                       sec, abfd);
2612 #endif
2613
2614   /* Initialize howto table if not already done.  */
2615   if (!ppc_elf_howto_table[R_PPC_ADDR32])
2616     ppc_elf_howto_init ();
2617
2618   htab = ppc_elf_hash_table (info);
2619   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2620   sym_hashes = elf_sym_hashes (abfd);
2621   sreloc = NULL;
2622
2623   rel_end = relocs + sec->reloc_count;
2624   for (rel = relocs; rel < rel_end; rel++)
2625     {
2626       unsigned long r_symndx;
2627       enum elf_ppc_reloc_type r_type;
2628       struct elf_link_hash_entry *h;
2629       int tls_type = 0;
2630
2631       r_symndx = ELF32_R_SYM (rel->r_info);
2632       if (r_symndx < symtab_hdr->sh_info)
2633         h = NULL;
2634       else
2635         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2636
2637       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2638          This shows up in particular in an R_PPC_ADDR32 in the eabi
2639          startup code.  */
2640       if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2641         {
2642           if (htab->got == NULL)
2643             {
2644               if (htab->elf.dynobj == NULL)
2645                 htab->elf.dynobj = abfd;
2646               if (!ppc_elf_create_got (htab->elf.dynobj, info))
2647                 return FALSE;
2648             }
2649         }
2650
2651       r_type = ELF32_R_TYPE (rel->r_info);
2652       switch (r_type)
2653         {
2654         case R_PPC_GOT_TLSLD16:
2655         case R_PPC_GOT_TLSLD16_LO:
2656         case R_PPC_GOT_TLSLD16_HI:
2657         case R_PPC_GOT_TLSLD16_HA:
2658           htab->tlsld_got.refcount += 1;
2659           tls_type = TLS_TLS | TLS_LD;
2660           goto dogottls;
2661
2662         case R_PPC_GOT_TLSGD16:
2663         case R_PPC_GOT_TLSGD16_LO:
2664         case R_PPC_GOT_TLSGD16_HI:
2665         case R_PPC_GOT_TLSGD16_HA:
2666           tls_type = TLS_TLS | TLS_GD;
2667           goto dogottls;
2668
2669         case R_PPC_GOT_TPREL16:
2670         case R_PPC_GOT_TPREL16_LO:
2671         case R_PPC_GOT_TPREL16_HI:
2672         case R_PPC_GOT_TPREL16_HA:
2673           if (info->shared)
2674             info->flags |= DF_STATIC_TLS;
2675           tls_type = TLS_TLS | TLS_TPREL;
2676           goto dogottls;
2677
2678         case R_PPC_GOT_DTPREL16:
2679         case R_PPC_GOT_DTPREL16_LO:
2680         case R_PPC_GOT_DTPREL16_HI:
2681         case R_PPC_GOT_DTPREL16_HA:
2682           tls_type = TLS_TLS | TLS_DTPREL;
2683         dogottls:
2684           sec->has_tls_reloc = 1;
2685           /* Fall thru */
2686
2687           /* GOT16 relocations */
2688         case R_PPC_GOT16:
2689         case R_PPC_GOT16_LO:
2690         case R_PPC_GOT16_HI:
2691         case R_PPC_GOT16_HA:
2692           /* This symbol requires a global offset table entry.  */
2693           if (htab->got == NULL)
2694             {
2695               if (htab->elf.dynobj == NULL)
2696                 htab->elf.dynobj = abfd;
2697               if (!ppc_elf_create_got (htab->elf.dynobj, info))
2698                 return FALSE;
2699             }
2700           if (h != NULL)
2701             {
2702               h->got.refcount += 1;
2703               ppc_elf_hash_entry (h)->tls_mask |= tls_type;
2704             }
2705           else
2706             /* This is a global offset table entry for a local symbol.  */
2707             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
2708               return FALSE;
2709           break;
2710
2711           /* Indirect .sdata relocation.  */
2712         case R_PPC_EMB_SDAI16:
2713           if (info->shared)
2714             {
2715               bad_shared_reloc (abfd, r_type);
2716               return FALSE;
2717             }
2718           if (htab->sdata[0].section == NULL
2719               && !ppc_elf_create_linker_section (abfd, info, 0,
2720                                                  &htab->sdata[0]))
2721             return FALSE;
2722           if (!elf_create_pointer_linker_section (abfd, info,
2723                                                   &htab->sdata[0], h, rel))
2724             return FALSE;
2725           break;
2726
2727           /* Indirect .sdata2 relocation.  */
2728         case R_PPC_EMB_SDA2I16:
2729           if (info->shared)
2730             {
2731               bad_shared_reloc (abfd, r_type);
2732               return FALSE;
2733             }
2734           if (htab->sdata[1].section == NULL
2735               && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
2736                                                  &htab->sdata[1]))
2737             return FALSE;
2738           if (!elf_create_pointer_linker_section (abfd, info,
2739                                                   &htab->sdata[1], h, rel))
2740             return FALSE;
2741           break;
2742
2743         case R_PPC_SDAREL16:
2744         case R_PPC_EMB_SDA2REL:
2745         case R_PPC_EMB_SDA21:
2746         case R_PPC_EMB_RELSDA:
2747         case R_PPC_EMB_NADDR32:
2748         case R_PPC_EMB_NADDR16:
2749         case R_PPC_EMB_NADDR16_LO:
2750         case R_PPC_EMB_NADDR16_HI:
2751         case R_PPC_EMB_NADDR16_HA:
2752           if (info->shared)
2753             {
2754               bad_shared_reloc (abfd, r_type);
2755               return FALSE;
2756             }
2757           break;
2758
2759         case R_PPC_PLT32:
2760         case R_PPC_PLTREL24:
2761         case R_PPC_PLTREL32:
2762         case R_PPC_PLT16_LO:
2763         case R_PPC_PLT16_HI:
2764         case R_PPC_PLT16_HA:
2765 #ifdef DEBUG
2766           fprintf (stderr, "Reloc requires a PLT entry\n");
2767 #endif
2768           /* This symbol requires a procedure linkage table entry.  We
2769              actually build the entry in finish_dynamic_symbol,
2770              because this might be a case of linking PIC code without
2771              linking in any dynamic objects, in which case we don't
2772              need to generate a procedure linkage table after all.  */
2773
2774           if (h == NULL)
2775             {
2776               /* It does not make sense to have a procedure linkage
2777                  table entry for a local symbol.  */
2778               (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
2779                                        "local symbol"),
2780                                      abfd,
2781                                      sec,
2782                                      (long) rel->r_offset,
2783                                      ppc_elf_howto_table[r_type]->name);
2784               bfd_set_error (bfd_error_bad_value);
2785               return FALSE;
2786             }
2787
2788           h->needs_plt = 1;
2789           h->plt.refcount++;
2790           break;
2791
2792           /* The following relocations don't need to propagate the
2793              relocation if linking a shared object since they are
2794              section relative.  */
2795         case R_PPC_SECTOFF:
2796         case R_PPC_SECTOFF_LO:
2797         case R_PPC_SECTOFF_HI:
2798         case R_PPC_SECTOFF_HA:
2799         case R_PPC_DTPREL16:
2800         case R_PPC_DTPREL16_LO:
2801         case R_PPC_DTPREL16_HI:
2802         case R_PPC_DTPREL16_HA:
2803         case R_PPC_TOC16:
2804           break;
2805
2806           /* This are just markers.  */
2807         case R_PPC_TLS:
2808         case R_PPC_EMB_MRKREF:
2809         case R_PPC_NONE:
2810         case R_PPC_max:
2811           break;
2812
2813           /* These should only appear in dynamic objects.  */
2814         case R_PPC_COPY:
2815         case R_PPC_GLOB_DAT:
2816         case R_PPC_JMP_SLOT:
2817         case R_PPC_RELATIVE:
2818           break;
2819
2820           /* These aren't handled yet.  We'll report an error later.  */
2821         case R_PPC_ADDR30:
2822         case R_PPC_EMB_RELSEC16:
2823         case R_PPC_EMB_RELST_LO:
2824         case R_PPC_EMB_RELST_HI:
2825         case R_PPC_EMB_RELST_HA:
2826         case R_PPC_EMB_BIT_FLD:
2827           break;
2828
2829           /* This refers only to functions defined in the shared library.  */
2830         case R_PPC_LOCAL24PC:
2831           break;
2832
2833           /* This relocation describes the C++ object vtable hierarchy.
2834              Reconstruct it for later use during GC.  */
2835         case R_PPC_GNU_VTINHERIT:
2836           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2837             return FALSE;
2838           break;
2839
2840           /* This relocation describes which C++ vtable entries are actually
2841              used.  Record for later use during GC.  */
2842         case R_PPC_GNU_VTENTRY:
2843           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2844             return FALSE;
2845           break;
2846
2847           /* We shouldn't really be seeing these.  */
2848         case R_PPC_TPREL32:
2849           if (info->shared)
2850             info->flags |= DF_STATIC_TLS;
2851           goto dodyn;
2852
2853           /* Nor these.  */
2854         case R_PPC_DTPMOD32:
2855         case R_PPC_DTPREL32:
2856           goto dodyn;
2857
2858         case R_PPC_TPREL16:
2859         case R_PPC_TPREL16_LO:
2860         case R_PPC_TPREL16_HI:
2861         case R_PPC_TPREL16_HA:
2862           if (info->shared)
2863             info->flags |= DF_STATIC_TLS;
2864           goto dodyn;
2865
2866           /* When creating a shared object, we must copy these
2867              relocs into the output file.  We create a reloc
2868              section in dynobj and make room for the reloc.  */
2869         case R_PPC_REL24:
2870         case R_PPC_REL14:
2871         case R_PPC_REL14_BRTAKEN:
2872         case R_PPC_REL14_BRNTAKEN:
2873         case R_PPC_REL32:
2874           if (h == NULL
2875               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2876             break;
2877           /* fall through */
2878
2879         case R_PPC_ADDR32:
2880         case R_PPC_ADDR24:
2881         case R_PPC_ADDR16:
2882         case R_PPC_ADDR16_LO:
2883         case R_PPC_ADDR16_HI:
2884         case R_PPC_ADDR16_HA:
2885         case R_PPC_ADDR14:
2886         case R_PPC_ADDR14_BRTAKEN:
2887         case R_PPC_ADDR14_BRNTAKEN:
2888         case R_PPC_UADDR32:
2889         case R_PPC_UADDR16:
2890           if (h != NULL && !info->shared)
2891             {
2892               /* We may need a plt entry if the symbol turns out to be
2893                  a function defined in a dynamic object.  */
2894               h->plt.refcount++;
2895
2896               /* We may need a copy reloc too.  */
2897               h->non_got_ref = 1;
2898             }
2899
2900         dodyn:
2901           /* If we are creating a shared library, and this is a reloc
2902              against a global symbol, or a non PC relative reloc
2903              against a local symbol, then we need to copy the reloc
2904              into the shared library.  However, if we are linking with
2905              -Bsymbolic, we do not need to copy a reloc against a
2906              global symbol which is defined in an object we are
2907              including in the link (i.e., DEF_REGULAR is set).  At
2908              this point we have not seen all the input files, so it is
2909              possible that DEF_REGULAR is not set now but will be set
2910              later (it is never cleared).  In case of a weak definition,
2911              DEF_REGULAR may be cleared later by a strong definition in
2912              a shared library.  We account for that possibility below by
2913              storing information in the dyn_relocs field of the hash
2914              table entry.  A similar situation occurs when creating
2915              shared libraries and symbol visibility changes render the
2916              symbol local.
2917
2918              If on the other hand, we are creating an executable, we
2919              may need to keep relocations for symbols satisfied by a
2920              dynamic library if we manage to avoid copy relocs for the
2921              symbol.  */
2922           if ((info->shared
2923                && (MUST_BE_DYN_RELOC (r_type)
2924                    || (h != NULL
2925                        && (! info->symbolic
2926                            || h->root.type == bfd_link_hash_defweak
2927                            || !h->def_regular))))
2928               || (ELIMINATE_COPY_RELOCS
2929                   && !info->shared
2930                   && (sec->flags & SEC_ALLOC) != 0
2931                   && h != NULL
2932                   && (h->root.type == bfd_link_hash_defweak
2933                       || !h->def_regular)))
2934             {
2935               struct ppc_elf_dyn_relocs *p;
2936               struct ppc_elf_dyn_relocs **head;
2937
2938 #ifdef DEBUG
2939               fprintf (stderr,
2940                        "ppc_elf_check_relocs needs to "
2941                        "create relocation for %s\n",
2942                        (h && h->root.root.string
2943                         ? h->root.root.string : "<unknown>"));
2944 #endif
2945               if (sreloc == NULL)
2946                 {
2947                   const char *name;
2948
2949                   name = (bfd_elf_string_from_elf_section
2950                           (abfd,
2951                            elf_elfheader (abfd)->e_shstrndx,
2952                            elf_section_data (sec)->rel_hdr.sh_name));
2953                   if (name == NULL)
2954                     return FALSE;
2955
2956                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2957                               && strcmp (bfd_get_section_name (abfd, sec),
2958                                          name + 5) == 0);
2959
2960                   if (htab->elf.dynobj == NULL)
2961                     htab->elf.dynobj = abfd;
2962                   sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
2963                   if (sreloc == NULL)
2964                     {
2965                       flagword flags;
2966
2967                       sreloc = bfd_make_section (htab->elf.dynobj, name);
2968                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
2969                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2970                       if ((sec->flags & SEC_ALLOC) != 0)
2971                         flags |= SEC_ALLOC | SEC_LOAD;
2972                       if (sreloc == NULL
2973                           || ! bfd_set_section_flags (htab->elf.dynobj,
2974                                                       sreloc, flags)
2975                           || ! bfd_set_section_alignment (htab->elf.dynobj,
2976                                                           sreloc, 2))
2977                         return FALSE;
2978                     }
2979                   elf_section_data (sec)->sreloc = sreloc;
2980                 }
2981
2982               /* If this is a global symbol, we count the number of
2983                  relocations we need for this symbol.  */
2984               if (h != NULL)
2985                 {
2986                   head = &ppc_elf_hash_entry (h)->dyn_relocs;
2987                 }
2988               else
2989                 {
2990                   /* Track dynamic relocs needed for local syms too.
2991                      We really need local syms available to do this
2992                      easily.  Oh well.  */
2993
2994                   asection *s;
2995                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
2996                                                  sec, r_symndx);
2997                   if (s == NULL)
2998                     return FALSE;
2999
3000                   head = ((struct ppc_elf_dyn_relocs **)
3001                           &elf_section_data (s)->local_dynrel);
3002                 }
3003
3004               p = *head;
3005               if (p == NULL || p->sec != sec)
3006                 {
3007                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3008                   if (p == NULL)
3009                     return FALSE;
3010                   p->next = *head;
3011                   *head = p;
3012                   p->sec = sec;
3013                   p->count = 0;
3014                   p->pc_count = 0;
3015                 }
3016
3017               p->count += 1;
3018               if (!MUST_BE_DYN_RELOC (r_type))
3019                 p->pc_count += 1;
3020             }
3021
3022           break;
3023         }
3024     }
3025
3026   return TRUE;
3027 }
3028 \f
3029 /* Merge backend specific data from an object file to the output
3030    object file when linking.  */
3031
3032 static bfd_boolean
3033 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3034 {
3035   flagword old_flags;
3036   flagword new_flags;
3037   bfd_boolean error;
3038
3039   if (!is_ppc_elf_target (ibfd->xvec)
3040       || !is_ppc_elf_target (obfd->xvec))
3041     return TRUE;
3042
3043   /* Check if we have the same endianess.  */
3044   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3045     return FALSE;
3046
3047   new_flags = elf_elfheader (ibfd)->e_flags;
3048   old_flags = elf_elfheader (obfd)->e_flags;
3049   if (!elf_flags_init (obfd))
3050     {
3051       /* First call, no flags set.  */
3052       elf_flags_init (obfd) = TRUE;
3053       elf_elfheader (obfd)->e_flags = new_flags;
3054     }
3055
3056   /* Compatible flags are ok.  */
3057   else if (new_flags == old_flags)
3058     ;
3059
3060   /* Incompatible flags.  */
3061   else
3062     {
3063       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
3064          to be linked with either.  */
3065       error = FALSE;
3066       if ((new_flags & EF_PPC_RELOCATABLE) != 0
3067           && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
3068         {
3069           error = TRUE;
3070           (*_bfd_error_handler)
3071             (_("%B: compiled with -mrelocatable and linked with "
3072                "modules compiled normally"), ibfd);
3073         }
3074       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
3075                && (old_flags & EF_PPC_RELOCATABLE) != 0)
3076         {
3077           error = TRUE;
3078           (*_bfd_error_handler)
3079             (_("%B: compiled normally and linked with "
3080                "modules compiled with -mrelocatable"), ibfd);
3081         }
3082
3083       /* The output is -mrelocatable-lib iff both the input files are.  */
3084       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
3085         elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
3086
3087       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
3088          but each input file is either -mrelocatable or -mrelocatable-lib.  */
3089       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
3090           && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
3091           && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
3092         elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
3093
3094       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
3095          any module uses it.  */
3096       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
3097
3098       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3099       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3100
3101       /* Warn about any other mismatches.  */
3102       if (new_flags != old_flags)
3103         {
3104           error = TRUE;
3105           (*_bfd_error_handler)
3106             (_("%B: uses different e_flags (0x%lx) fields "
3107                "than previous modules (0x%lx)"),
3108              ibfd, (long) new_flags, (long) old_flags);
3109         }
3110
3111       if (error)
3112         {
3113           bfd_set_error (bfd_error_bad_value);
3114           return FALSE;
3115         }
3116     }
3117
3118   return TRUE;
3119 }
3120 \f
3121 /* Return the section that should be marked against GC for a given
3122    relocation.  */
3123
3124 static asection *
3125 ppc_elf_gc_mark_hook (asection *sec,
3126                       struct bfd_link_info *info ATTRIBUTE_UNUSED,
3127                       Elf_Internal_Rela *rel,
3128                       struct elf_link_hash_entry *h,
3129                       Elf_Internal_Sym *sym)
3130 {
3131   if (h != NULL)
3132     {
3133       switch (ELF32_R_TYPE (rel->r_info))
3134         {
3135         case R_PPC_GNU_VTINHERIT:
3136         case R_PPC_GNU_VTENTRY:
3137           break;
3138
3139         default:
3140           switch (h->root.type)
3141             {
3142             case bfd_link_hash_defined:
3143             case bfd_link_hash_defweak:
3144               return h->root.u.def.section;
3145
3146             case bfd_link_hash_common:
3147               return h->root.u.c.p->section;
3148
3149             default:
3150               break;
3151             }
3152         }
3153     }
3154   else
3155     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3156
3157   return NULL;
3158 }
3159
3160 /* Update the got, plt and dynamic reloc reference counts for the
3161    section being removed.  */
3162
3163 static bfd_boolean
3164 ppc_elf_gc_sweep_hook (bfd *abfd,
3165                        struct bfd_link_info *info,
3166                        asection *sec,
3167                        const Elf_Internal_Rela *relocs)
3168 {
3169   struct ppc_elf_link_hash_table *htab;
3170   Elf_Internal_Shdr *symtab_hdr;
3171   struct elf_link_hash_entry **sym_hashes;
3172   bfd_signed_vma *local_got_refcounts;
3173   const Elf_Internal_Rela *rel, *relend;
3174
3175   elf_section_data (sec)->local_dynrel = NULL;
3176
3177   htab = ppc_elf_hash_table (info);
3178   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3179   sym_hashes = elf_sym_hashes (abfd);
3180   local_got_refcounts = elf_local_got_refcounts (abfd);
3181
3182   relend = relocs + sec->reloc_count;
3183   for (rel = relocs; rel < relend; rel++)
3184     {
3185       unsigned long r_symndx;
3186       enum elf_ppc_reloc_type r_type;
3187       struct elf_link_hash_entry *h = NULL;
3188
3189       r_symndx = ELF32_R_SYM (rel->r_info);
3190       if (r_symndx >= symtab_hdr->sh_info)
3191         {
3192           struct ppc_elf_dyn_relocs **pp, *p;
3193           struct ppc_elf_link_hash_entry *eh;
3194
3195           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3196           while (h->root.type == bfd_link_hash_indirect
3197                  || h->root.type == bfd_link_hash_warning)
3198             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3199           eh = (struct ppc_elf_link_hash_entry *) h;
3200
3201           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3202             if (p->sec == sec)
3203               {
3204                 /* Everything must go for SEC.  */
3205                 *pp = p->next;
3206                 break;
3207               }
3208         }
3209
3210       r_type = ELF32_R_TYPE (rel->r_info);
3211       switch (r_type)
3212         {
3213         case R_PPC_GOT_TLSLD16:
3214         case R_PPC_GOT_TLSLD16_LO:
3215         case R_PPC_GOT_TLSLD16_HI:
3216         case R_PPC_GOT_TLSLD16_HA:
3217           htab->tlsld_got.refcount -= 1;
3218           /* Fall thru */
3219
3220         case R_PPC_GOT_TLSGD16:
3221         case R_PPC_GOT_TLSGD16_LO:
3222         case R_PPC_GOT_TLSGD16_HI:
3223         case R_PPC_GOT_TLSGD16_HA:
3224         case R_PPC_GOT_TPREL16:
3225         case R_PPC_GOT_TPREL16_LO:
3226         case R_PPC_GOT_TPREL16_HI:
3227         case R_PPC_GOT_TPREL16_HA:
3228         case R_PPC_GOT_DTPREL16:
3229         case R_PPC_GOT_DTPREL16_LO:
3230         case R_PPC_GOT_DTPREL16_HI:
3231         case R_PPC_GOT_DTPREL16_HA:
3232         case R_PPC_GOT16:
3233         case R_PPC_GOT16_LO:
3234         case R_PPC_GOT16_HI:
3235         case R_PPC_GOT16_HA:
3236           if (h != NULL)
3237             {
3238               if (h->got.refcount > 0)
3239                 h->got.refcount--;
3240             }
3241           else if (local_got_refcounts != NULL)
3242             {
3243               if (local_got_refcounts[r_symndx] > 0)
3244                 local_got_refcounts[r_symndx]--;
3245             }
3246           break;
3247
3248         case R_PPC_REL24:
3249         case R_PPC_REL14:
3250         case R_PPC_REL14_BRTAKEN:
3251         case R_PPC_REL14_BRNTAKEN:
3252         case R_PPC_REL32:
3253           if (h == NULL
3254               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3255             break;
3256           /* Fall thru */
3257
3258         case R_PPC_ADDR32:
3259         case R_PPC_ADDR24:
3260         case R_PPC_ADDR16:
3261         case R_PPC_ADDR16_LO:
3262         case R_PPC_ADDR16_HI:
3263         case R_PPC_ADDR16_HA:
3264         case R_PPC_ADDR14:
3265         case R_PPC_ADDR14_BRTAKEN:
3266         case R_PPC_ADDR14_BRNTAKEN:
3267         case R_PPC_UADDR32:
3268         case R_PPC_UADDR16:
3269         case R_PPC_PLT32:
3270         case R_PPC_PLTREL24:
3271         case R_PPC_PLT16_LO:
3272         case R_PPC_PLT16_HI:
3273         case R_PPC_PLT16_HA:
3274           if (h != NULL)
3275             {
3276               if (h->plt.refcount > 0)
3277                 h->plt.refcount--;
3278             }
3279           break;
3280
3281         default:
3282           break;
3283         }
3284     }
3285   return TRUE;
3286 }
3287 \f
3288 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
3289
3290 asection *
3291 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3292 {
3293   struct ppc_elf_link_hash_table *htab;
3294
3295   htab = ppc_elf_hash_table (info);
3296   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3297                                              FALSE, FALSE, TRUE);
3298
3299   return _bfd_elf_tls_setup (obfd, info);
3300 }
3301
3302 /* Run through all the TLS relocs looking for optimization
3303    opportunities.  */
3304
3305 bfd_boolean
3306 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
3307                       struct bfd_link_info *info)
3308 {
3309   bfd *ibfd;
3310   asection *sec;
3311   struct ppc_elf_link_hash_table *htab;
3312
3313   if (info->relocatable || info->shared)
3314     return TRUE;
3315
3316   htab = ppc_elf_hash_table (info);
3317   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3318     {
3319       Elf_Internal_Sym *locsyms = NULL;
3320       Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3321
3322       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3323         if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3324           {
3325             Elf_Internal_Rela *relstart, *rel, *relend;
3326             int expecting_tls_get_addr;
3327
3328             /* Read the relocations.  */
3329             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
3330                                                   info->keep_memory);
3331             if (relstart == NULL)
3332               return FALSE;
3333
3334             expecting_tls_get_addr = 0;
3335             relend = relstart + sec->reloc_count;
3336             for (rel = relstart; rel < relend; rel++)
3337               {
3338                 enum elf_ppc_reloc_type r_type;
3339                 unsigned long r_symndx;
3340                 struct elf_link_hash_entry *h = NULL;
3341                 char *tls_mask;
3342                 char tls_set, tls_clear;
3343                 bfd_boolean is_local;
3344
3345                 r_symndx = ELF32_R_SYM (rel->r_info);
3346                 if (r_symndx >= symtab_hdr->sh_info)
3347                   {
3348                     struct elf_link_hash_entry **sym_hashes;
3349
3350                     sym_hashes = elf_sym_hashes (ibfd);
3351                     h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3352                     while (h->root.type == bfd_link_hash_indirect
3353                            || h->root.type == bfd_link_hash_warning)
3354                       h = (struct elf_link_hash_entry *) h->root.u.i.link;
3355                   }
3356
3357                 is_local = FALSE;
3358                 if (h == NULL
3359                     || !h->def_dynamic)
3360                   is_local = TRUE;
3361
3362                 r_type = ELF32_R_TYPE (rel->r_info);
3363                 switch (r_type)
3364                   {
3365                   case R_PPC_GOT_TLSLD16:
3366                   case R_PPC_GOT_TLSLD16_LO:
3367                   case R_PPC_GOT_TLSLD16_HI:
3368                   case R_PPC_GOT_TLSLD16_HA:
3369                     /* These relocs should never be against a symbol
3370                        defined in a shared lib.  Leave them alone if
3371                        that turns out to be the case.  */
3372                     expecting_tls_get_addr = 0;
3373                     htab->tlsld_got.refcount -= 1;
3374                     if (!is_local)
3375                       continue;
3376
3377                     /* LD -> LE */
3378                     tls_set = 0;
3379                     tls_clear = TLS_LD;
3380                     expecting_tls_get_addr = 1;
3381                     break;
3382
3383                   case R_PPC_GOT_TLSGD16:
3384                   case R_PPC_GOT_TLSGD16_LO:
3385                   case R_PPC_GOT_TLSGD16_HI:
3386                   case R_PPC_GOT_TLSGD16_HA:
3387                     if (is_local)
3388                       /* GD -> LE */
3389                       tls_set = 0;
3390                     else
3391                       /* GD -> IE */
3392                       tls_set = TLS_TLS | TLS_TPRELGD;
3393                     tls_clear = TLS_GD;
3394                     expecting_tls_get_addr = 1;
3395                     break;
3396
3397                   case R_PPC_GOT_TPREL16:
3398                   case R_PPC_GOT_TPREL16_LO:
3399                   case R_PPC_GOT_TPREL16_HI:
3400                   case R_PPC_GOT_TPREL16_HA:
3401                     expecting_tls_get_addr = 0;
3402                     if (is_local)
3403                       {
3404                         /* IE -> LE */
3405                         tls_set = 0;
3406                         tls_clear = TLS_TPREL;
3407                         break;
3408                       }
3409                     else
3410                       continue;
3411
3412                   case R_PPC_REL14:
3413                   case R_PPC_REL14_BRTAKEN:
3414                   case R_PPC_REL14_BRNTAKEN:
3415                   case R_PPC_REL24:
3416                     if (expecting_tls_get_addr
3417                         && h != NULL
3418                         && h == htab->tls_get_addr)
3419                       {
3420                         if (h->plt.refcount > 0)
3421                           h->plt.refcount -= 1;
3422                       }
3423                     expecting_tls_get_addr = 0;
3424                     continue;
3425
3426                   default:
3427                     expecting_tls_get_addr = 0;
3428                     continue;
3429                   }
3430
3431                 if (h != NULL)
3432                   {
3433                     if (tls_set == 0)
3434                       {
3435                         /* We managed to get rid of a got entry.  */
3436                         if (h->got.refcount > 0)
3437                           h->got.refcount -= 1;
3438                       }
3439                     tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3440                   }
3441                 else
3442                   {
3443                     Elf_Internal_Sym *sym;
3444                     bfd_signed_vma *lgot_refs;
3445                     char *lgot_masks;
3446
3447                     if (locsyms == NULL)
3448                       {
3449                         locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3450                         if (locsyms == NULL)
3451                           locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3452                                                           symtab_hdr->sh_info,
3453                                                           0, NULL, NULL, NULL);
3454                         if (locsyms == NULL)
3455                           {
3456                             if (elf_section_data (sec)->relocs != relstart)
3457                               free (relstart);
3458                             return FALSE;
3459                           }
3460                       }
3461                     sym = locsyms + r_symndx;
3462                     lgot_refs = elf_local_got_refcounts (ibfd);
3463                     if (lgot_refs == NULL)
3464                       abort ();
3465                     if (tls_set == 0)
3466                       {
3467                         /* We managed to get rid of a got entry.  */
3468                         if (lgot_refs[r_symndx] > 0)
3469                           lgot_refs[r_symndx] -= 1;
3470                       }
3471                     lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
3472                     tls_mask = &lgot_masks[r_symndx];
3473                   }
3474
3475                 *tls_mask |= tls_set;
3476                 *tls_mask &= ~tls_clear;
3477               }
3478
3479             if (elf_section_data (sec)->relocs != relstart)
3480               free (relstart);
3481           }
3482
3483       if (locsyms != NULL
3484           && (symtab_hdr->contents != (unsigned char *) locsyms))
3485         {
3486           if (!info->keep_memory)
3487             free (locsyms);
3488           else
3489             symtab_hdr->contents = (unsigned char *) locsyms;
3490         }
3491     }
3492   return TRUE;
3493 }
3494 \f
3495 /* Adjust a symbol defined by a dynamic object and referenced by a
3496    regular object.  The current definition is in some section of the
3497    dynamic object, but we're not including those sections.  We have to
3498    change the definition to something the rest of the link can
3499    understand.  */
3500
3501 static bfd_boolean
3502 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3503                                struct elf_link_hash_entry *h)
3504 {
3505   struct ppc_elf_link_hash_table *htab;
3506   asection *s;
3507   unsigned int power_of_two;
3508
3509 #ifdef DEBUG
3510   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
3511            h->root.root.string);
3512 #endif
3513
3514   /* Make sure we know what is going on here.  */
3515   htab = ppc_elf_hash_table (info);
3516   BFD_ASSERT (htab->elf.dynobj != NULL
3517               && (h->needs_plt
3518                   || h->u.weakdef != NULL
3519                   || (h->def_dynamic
3520                       && h->ref_regular
3521                       && !h->def_regular)));
3522
3523   /* Deal with function syms.  */
3524   if (h->type == STT_FUNC
3525       || h->needs_plt)
3526     {
3527       /* Clear procedure linkage table information for any symbol that
3528          won't need a .plt entry.  */
3529       if (h->plt.refcount <= 0
3530           || SYMBOL_CALLS_LOCAL (info, h)
3531           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3532               && h->root.type == bfd_link_hash_undefweak))
3533         {
3534           /* A PLT entry is not required/allowed when:
3535
3536              1. We are not using ld.so; because then the PLT entry
3537              can't be set up, so we can't use one.  In this case,
3538              ppc_elf_adjust_dynamic_symbol won't even be called.
3539
3540              2. GC has rendered the entry unused.
3541
3542              3. We know for certain that a call to this symbol
3543              will go to this object, or will remain undefined.  */
3544           h->plt.offset = (bfd_vma) -1;
3545           h->needs_plt = 0;
3546         }
3547       return TRUE;
3548     }
3549   else
3550     h->plt.offset = (bfd_vma) -1;
3551
3552   /* If this is a weak symbol, and there is a real definition, the
3553      processor independent code will have arranged for us to see the
3554      real definition first, and we can just use the same value.  */
3555   if (h->u.weakdef != NULL)
3556     {
3557       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3558                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
3559       h->root.u.def.section = h->u.weakdef->root.u.def.section;
3560       h->root.u.def.value = h->u.weakdef->root.u.def.value;
3561       if (ELIMINATE_COPY_RELOCS)
3562         h->non_got_ref = h->u.weakdef->non_got_ref;
3563       return TRUE;
3564     }
3565
3566   /* This is a reference to a symbol defined by a dynamic object which
3567      is not a function.  */
3568
3569   /* If we are creating a shared library, we must presume that the
3570      only references to the symbol are via the global offset table.
3571      For such cases we need not do anything here; the relocations will
3572      be handled correctly by relocate_section.  */
3573   if (info->shared)
3574     return TRUE;
3575
3576   /* If there are no references to this symbol that do not use the
3577      GOT, we don't need to generate a copy reloc.  */
3578   if (!h->non_got_ref)
3579     return TRUE;
3580
3581   if (ELIMINATE_COPY_RELOCS)
3582     {
3583       struct ppc_elf_dyn_relocs *p;
3584       for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3585         {
3586           s = p->sec->output_section;
3587           if (s != NULL && (s->flags & SEC_READONLY) != 0)
3588             break;
3589         }
3590
3591       /* If we didn't find any dynamic relocs in read-only sections, then
3592          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
3593       if (p == NULL)
3594         {
3595           h->non_got_ref = 0;
3596           return TRUE;
3597         }
3598     }
3599
3600   /* We must allocate the symbol in our .dynbss section, which will
3601      become part of the .bss section of the executable.  There will be
3602      an entry for this symbol in the .dynsym section.  The dynamic
3603      object will contain position independent code, so all references
3604      from the dynamic object to this symbol will go through the global
3605      offset table.  The dynamic linker will use the .dynsym entry to
3606      determine the address it must put in the global offset table, so
3607      both the dynamic object and the regular object will refer to the
3608      same memory location for the variable.
3609
3610      Of course, if the symbol is sufficiently small, we must instead
3611      allocate it in .sbss.  FIXME: It would be better to do this if and
3612      only if there were actually SDAREL relocs for that symbol.  */
3613
3614   if (h->size <= elf_gp_size (htab->elf.dynobj))
3615     s = htab->dynsbss;
3616   else
3617     s = htab->dynbss;
3618   BFD_ASSERT (s != NULL);
3619
3620   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
3621      copy the initial value out of the dynamic object and into the
3622      runtime process image.  We need to remember the offset into the
3623      .rela.bss section we are going to use.  */
3624   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3625     {
3626       asection *srel;
3627
3628       if (h->size <= elf_gp_size (htab->elf.dynobj))
3629         srel = htab->relsbss;
3630       else
3631         srel = htab->relbss;
3632       BFD_ASSERT (srel != NULL);
3633       srel->size += sizeof (Elf32_External_Rela);
3634       h->needs_copy = 1;
3635     }
3636
3637   /* We need to figure out the alignment required for this symbol.  I
3638      have no idea how ELF linkers handle this.  */
3639   power_of_two = bfd_log2 (h->size);
3640   if (power_of_two > 4)
3641     power_of_two = 4;
3642
3643   /* Apply the required alignment.  */
3644   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3645   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3646     {
3647       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3648         return FALSE;
3649     }
3650
3651   /* Define the symbol as being at this point in the section.  */
3652   h->root.u.def.section = s;
3653   h->root.u.def.value = s->size;
3654
3655   /* Increment the section size to make room for the symbol.  */
3656   s->size += h->size;
3657
3658   return TRUE;
3659 }
3660 \f
3661 /* Allocate space in associated reloc sections for dynamic relocs.  */
3662
3663 static bfd_boolean
3664 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3665 {
3666   struct bfd_link_info *info = inf;
3667   struct ppc_elf_link_hash_entry *eh;
3668   struct ppc_elf_link_hash_table *htab;
3669   struct ppc_elf_dyn_relocs *p;
3670
3671   if (h->root.type == bfd_link_hash_indirect)
3672     return TRUE;
3673
3674   if (h->root.type == bfd_link_hash_warning)
3675     /* When warning symbols are created, they **replace** the "real"
3676        entry in the hash table, thus we never get to see the real
3677        symbol in a hash traversal.  So look at it now.  */
3678     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3679
3680   htab = ppc_elf_hash_table (info);
3681   if (htab->elf.dynamic_sections_created
3682       && h->plt.refcount > 0)
3683     {
3684       /* Make sure this symbol is output as a dynamic symbol.  */
3685       if (h->dynindx == -1
3686           && !h->forced_local)
3687         {
3688           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3689             return FALSE;
3690         }
3691
3692       if (info->shared
3693           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3694         {
3695           asection *s = htab->plt;
3696
3697           /* If this is the first .plt entry, make room for the special
3698              first entry.  */
3699           if (s->size == 0)
3700             s->size += PLT_INITIAL_ENTRY_SIZE;
3701
3702           /* The PowerPC PLT is actually composed of two parts, the
3703              first part is 2 words (for a load and a jump), and then
3704              there is a remaining word available at the end.  */
3705           h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3706                            + (PLT_SLOT_SIZE
3707                               * ((s->size - PLT_INITIAL_ENTRY_SIZE)
3708                                  / PLT_ENTRY_SIZE)));
3709
3710           /* If this symbol is not defined in a regular file, and we
3711              are not generating a shared library, then set the symbol
3712              to this location in the .plt.  This is required to make
3713              function pointers compare as equal between the normal
3714              executable and the shared library.  */
3715           if (! info->shared
3716               && !h->def_regular)
3717             {
3718               h->root.u.def.section = s;
3719               h->root.u.def.value = h->plt.offset;
3720             }
3721
3722           /* Make room for this entry.  After the 8192nd entry, room
3723              for two entries is allocated.  */
3724           s->size += PLT_ENTRY_SIZE;
3725           if ((s->size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3726               > PLT_NUM_SINGLE_ENTRIES)
3727             s->size += PLT_ENTRY_SIZE;
3728
3729           /* We also need to make an entry in the .rela.plt section.  */
3730           htab->relplt->size += sizeof (Elf32_External_Rela);
3731         }
3732       else
3733         {
3734           h->plt.offset = (bfd_vma) -1;
3735           h->needs_plt = 0;
3736         }
3737     }
3738   else
3739     {
3740       h->plt.offset = (bfd_vma) -1;
3741       h->needs_plt = 0;
3742     }
3743
3744   eh = (struct ppc_elf_link_hash_entry *) h;
3745   if (eh->elf.got.refcount > 0)
3746     {
3747       /* Make sure this symbol is output as a dynamic symbol.  */
3748       if (eh->elf.dynindx == -1
3749           && !eh->elf.forced_local)
3750         {
3751           if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
3752             return FALSE;
3753         }
3754
3755       if (eh->tls_mask == (TLS_TLS | TLS_LD)
3756           && !eh->elf.def_dynamic)
3757         /* If just an LD reloc, we'll just use htab->tlsld_got.offset.  */
3758         eh->elf.got.offset = (bfd_vma) -1;
3759       else
3760         {
3761           bfd_boolean dyn;
3762           eh->elf.got.offset = htab->got->size;
3763           if ((eh->tls_mask & TLS_TLS) != 0)
3764             {
3765               if ((eh->tls_mask & TLS_LD) != 0)
3766                 htab->got->size += 8;
3767               if ((eh->tls_mask & TLS_GD) != 0)
3768                 htab->got->size += 8;
3769               if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
3770                 htab->got->size += 4;
3771               if ((eh->tls_mask & TLS_DTPREL) != 0)
3772                 htab->got->size += 4;
3773             }
3774           else
3775             htab->got->size += 4;
3776           dyn = htab->elf.dynamic_sections_created;
3777           if ((info->shared
3778                || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
3779               && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
3780                   || eh->elf.root.type != bfd_link_hash_undefweak))
3781             {
3782               /* All the entries we allocated need relocs.  */
3783               htab->relgot->size
3784                 += ((htab->got->size - eh->elf.got.offset) / 4
3785                     * sizeof (Elf32_External_Rela));
3786               /* Except LD only needs one.  */
3787               if ((eh->tls_mask & TLS_LD) != 0)
3788                 htab->relgot->size -= sizeof (Elf32_External_Rela);
3789             }
3790         }
3791     }
3792   else
3793     eh->elf.got.offset = (bfd_vma) -1;
3794
3795   if (eh->dyn_relocs == NULL)
3796     return TRUE;
3797
3798   /* In the shared -Bsymbolic case, discard space allocated for
3799      dynamic pc-relative relocs against symbols which turn out to be
3800      defined in regular objects.  For the normal shared case, discard
3801      space for relocs that have become local due to symbol visibility
3802      changes.  */
3803
3804   if (info->shared)
3805     {
3806       /* Relocs that use pc_count are those that appear on a call insn,
3807          or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
3808          generated via assembly.  We want calls to protected symbols to
3809          resolve directly to the function rather than going via the plt.
3810          If people want function pointer comparisons to work as expected
3811          then they should avoid writing weird assembly.  */
3812       if (SYMBOL_CALLS_LOCAL (info, h))
3813         {
3814           struct ppc_elf_dyn_relocs **pp;
3815
3816           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3817             {
3818               p->count -= p->pc_count;
3819               p->pc_count = 0;
3820               if (p->count == 0)
3821                 *pp = p->next;
3822               else
3823                 pp = &p->next;
3824             }
3825         }
3826
3827       /* Also discard relocs on undefined weak syms with non-default
3828          visibility.  */
3829       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3830           && h->root.type == bfd_link_hash_undefweak)
3831         eh->dyn_relocs = NULL;
3832
3833       /* Make sure undefined weak symbols are output as a dynamic symbol
3834          in PIEs.  */
3835       if (info->pie
3836           && eh->dyn_relocs != NULL
3837           && h->dynindx == -1
3838           && h->root.type == bfd_link_hash_undefweak
3839           && !h->forced_local)
3840         {
3841           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3842             return FALSE;
3843         }
3844     }
3845   else if (ELIMINATE_COPY_RELOCS)
3846     {
3847       /* For the non-shared case, discard space for relocs against
3848          symbols which turn out to need copy relocs or are not
3849          dynamic.  */
3850
3851       if (!h->non_got_ref
3852           && h->def_dynamic
3853           && !h->def_regular)
3854         {
3855           /* Make sure this symbol is output as a dynamic symbol.
3856              Undefined weak syms won't yet be marked as dynamic.  */
3857           if (h->dynindx == -1
3858               && !h->forced_local)
3859             {
3860               if (! bfd_elf_link_record_dynamic_symbol (info, h))
3861                 return FALSE;
3862             }
3863
3864           /* If that succeeded, we know we'll be keeping all the
3865              relocs.  */
3866           if (h->dynindx != -1)
3867             goto keep;
3868         }
3869
3870       eh->dyn_relocs = NULL;
3871
3872     keep: ;
3873     }
3874
3875   /* Finally, allocate space.  */
3876   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3877     {
3878       asection *sreloc = elf_section_data (p->sec)->sreloc;
3879       sreloc->size += p->count * sizeof (Elf32_External_Rela);
3880     }
3881
3882   return TRUE;
3883 }
3884
3885 /* Find any dynamic relocs that apply to read-only sections.  */
3886
3887 static bfd_boolean
3888 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
3889 {
3890   struct ppc_elf_dyn_relocs *p;
3891
3892   if (h->root.type == bfd_link_hash_indirect)
3893     return TRUE;
3894
3895   if (h->root.type == bfd_link_hash_warning)
3896     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3897
3898   for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3899     {
3900       asection *s = p->sec->output_section;
3901
3902       if (s != NULL
3903           && ((s->flags & (SEC_READONLY | SEC_ALLOC))
3904               == (SEC_READONLY | SEC_ALLOC)))
3905         {
3906           ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
3907
3908           /* Not an error, just cut short the traversal.  */
3909           return FALSE;
3910         }
3911     }
3912   return TRUE;
3913 }
3914
3915 /* Set the sizes of the dynamic sections.  */
3916
3917 static bfd_boolean
3918 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3919                                struct bfd_link_info *info)
3920 {
3921   struct ppc_elf_link_hash_table *htab;
3922   asection *s;
3923   bfd_boolean relocs;
3924   bfd *ibfd;
3925
3926 #ifdef DEBUG
3927   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
3928 #endif
3929
3930   htab = ppc_elf_hash_table (info);
3931   BFD_ASSERT (htab->elf.dynobj != NULL);
3932
3933   if (elf_hash_table (info)->dynamic_sections_created)
3934     {
3935       /* Set the contents of the .interp section to the interpreter.  */
3936       if (info->executable)
3937         {
3938           s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
3939           BFD_ASSERT (s != NULL);
3940           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3941           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3942         }
3943     }
3944
3945   if (htab->tlsld_got.refcount > 0)
3946     {
3947       htab->tlsld_got.offset = htab->got->size;
3948       htab->got->size += 8;
3949       if (info->shared)
3950         htab->relgot->size += sizeof (Elf32_External_Rela);
3951     }
3952   else
3953     htab->tlsld_got.offset = (bfd_vma) -1;
3954
3955   /* Set up .got offsets for local syms, and space for local dynamic
3956      relocs.  */
3957   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3958     {
3959       bfd_signed_vma *local_got;
3960       bfd_signed_vma *end_local_got;
3961       char *lgot_masks;
3962       bfd_size_type locsymcount;
3963       Elf_Internal_Shdr *symtab_hdr;
3964       asection *srel;
3965
3966       if (!is_ppc_elf_target (ibfd->xvec))
3967         continue;
3968
3969       for (s = ibfd->sections; s != NULL; s = s->next)
3970         {
3971           struct ppc_elf_dyn_relocs *p;
3972
3973           for (p = ((struct ppc_elf_dyn_relocs *)
3974                     elf_section_data (s)->local_dynrel);
3975                p != NULL;
3976                p = p->next)
3977             {
3978               if (!bfd_is_abs_section (p->sec)
3979                   && bfd_is_abs_section (p->sec->output_section))
3980                 {
3981                   /* Input section has been discarded, either because
3982                      it is a copy of a linkonce section or due to
3983                      linker script /DISCARD/, so we'll be discarding
3984                      the relocs too.  */
3985                 }
3986               else if (p->count != 0)
3987                 {
3988                   elf_section_data (p->sec)->sreloc->size
3989                     += p->count * sizeof (Elf32_External_Rela);
3990                   if ((p->sec->output_section->flags
3991                        & (SEC_READONLY | SEC_ALLOC))
3992                       == (SEC_READONLY | SEC_ALLOC))
3993                     info->flags |= DF_TEXTREL;
3994                 }
3995             }
3996         }
3997
3998       local_got = elf_local_got_refcounts (ibfd);
3999       if (!local_got)
4000         continue;
4001
4002       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4003       locsymcount = symtab_hdr->sh_info;
4004       end_local_got = local_got + locsymcount;
4005       lgot_masks = (char *) end_local_got;
4006       s = htab->got;
4007       srel = htab->relgot;
4008       for (; local_got < end_local_got; ++local_got, ++lgot_masks)
4009         if (*local_got > 0)
4010           {
4011             if (*lgot_masks == (TLS_TLS | TLS_LD))
4012               {
4013                 /* If just an LD reloc, we'll just use
4014                    htab->tlsld_got.offset.  */
4015                 if (htab->tlsld_got.offset == (bfd_vma) -1)
4016                   {
4017                     htab->tlsld_got.offset = s->size;
4018                     s->size += 8;
4019                     if (info->shared)
4020                       srel->size += sizeof (Elf32_External_Rela);
4021                   }
4022                 *local_got = (bfd_vma) -1;
4023               }
4024             else
4025               {
4026                 *local_got = s->size;
4027                 if ((*lgot_masks & TLS_TLS) != 0)
4028                   {
4029                     if ((*lgot_masks & TLS_GD) != 0)
4030                       s->size += 8;
4031                     if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
4032                       s->size += 4;
4033                     if ((*lgot_masks & TLS_DTPREL) != 0)
4034                       s->size += 4;
4035                   }
4036                 else
4037                   s->size += 4;
4038                 if (info->shared)
4039                   srel->size += ((s->size - *local_got) / 4
4040                                  * sizeof (Elf32_External_Rela));
4041               }
4042           }
4043         else
4044           *local_got = (bfd_vma) -1;
4045     }
4046
4047   /* Allocate space for global sym dynamic relocs.  */
4048   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
4049
4050   /* We've now determined the sizes of the various dynamic sections.
4051      Allocate memory for them.  */
4052   relocs = FALSE;
4053   for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
4054     {
4055       if ((s->flags & SEC_LINKER_CREATED) == 0)
4056         continue;
4057
4058       if (s == htab->plt
4059           || s == htab->got
4060           || s == htab->sbss)
4061         {
4062           /* Strip this section if we don't need it; see the
4063              comment below.  */
4064         }
4065       else if (s == htab->sdata[0].section
4066                || s == htab->sdata[1].section)
4067         {
4068           /* Strip these too.  */
4069         }
4070       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4071         {
4072           if (s->size == 0)
4073             {
4074               /* If we don't need this section, strip it from the
4075                  output file.  This is mostly to handle .rela.bss and
4076                  .rela.plt.  We must create both sections in
4077                  create_dynamic_sections, because they must be created
4078                  before the linker maps input sections to output
4079                  sections.  The linker does that before
4080                  adjust_dynamic_symbol is called, and it is that
4081                  function which decides whether anything needs to go
4082                  into these sections.  */
4083             }
4084           else
4085             {
4086               /* Remember whether there are any relocation sections.  */
4087               relocs = TRUE;
4088
4089               /* We use the reloc_count field as a counter if we need
4090                  to copy relocs into the output file.  */
4091               s->reloc_count = 0;
4092             }
4093         }
4094       else
4095         {
4096           /* It's not one of our sections, so don't allocate space.  */
4097           continue;
4098         }
4099
4100       if (s->size == 0)
4101         {
4102           _bfd_strip_section_from_output (info, s);
4103           continue;
4104         }
4105
4106       if (s == htab->sbss)
4107         continue;
4108
4109       /* Allocate memory for the section contents.  */
4110       s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
4111       if (s->contents == NULL)
4112         return FALSE;
4113     }
4114
4115   if (htab->elf.dynamic_sections_created)
4116     {
4117       /* Add some entries to the .dynamic section.  We fill in the
4118          values later, in ppc_elf_finish_dynamic_sections, but we
4119          must add the entries now so that we get the correct size for
4120          the .dynamic section.  The DT_DEBUG entry is filled in by the
4121          dynamic linker and used by the debugger.  */
4122 #define add_dynamic_entry(TAG, VAL) \
4123   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4124
4125       if (info->executable)
4126         {
4127           if (!add_dynamic_entry (DT_DEBUG, 0))
4128             return FALSE;
4129         }
4130
4131       if (htab->plt != NULL && htab->plt->size != 0)
4132         {
4133           if (!add_dynamic_entry (DT_PLTGOT, 0)
4134               || !add_dynamic_entry (DT_PLTRELSZ, 0)
4135               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4136               || !add_dynamic_entry (DT_JMPREL, 0))
4137             return FALSE;
4138         }
4139
4140       if (relocs)
4141         {
4142           if (!add_dynamic_entry (DT_RELA, 0)
4143               || !add_dynamic_entry (DT_RELASZ, 0)
4144               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4145             return FALSE;
4146         }
4147
4148       /* If any dynamic relocs apply to a read-only section, then we
4149          need a DT_TEXTREL entry.  */
4150       if ((info->flags & DF_TEXTREL) == 0)
4151         elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
4152                                 info);
4153
4154       if ((info->flags & DF_TEXTREL) != 0)
4155         {
4156           if (!add_dynamic_entry (DT_TEXTREL, 0))
4157             return FALSE;
4158         }
4159     }
4160 #undef add_dynamic_entry
4161
4162   return TRUE;
4163 }
4164 \f
4165 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
4166
4167 static const int shared_stub_entry[] =
4168   {
4169     0x7c0802a6, /* mflr 0 */
4170     0x429f0005, /* bcl 20, 31, .Lxxx */
4171     0x7d6802a6, /* mflr 11 */
4172     0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
4173     0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
4174     0x7c0803a6, /* mtlr 0 */
4175     0x7d6903a6, /* mtctr 11 */
4176     0x4e800420, /* bctr */
4177   };
4178
4179 static const int stub_entry[] =
4180   {
4181     0x3d600000, /* lis 11,xxx@ha */
4182     0x396b0000, /* addi 11,11,xxx@l */
4183     0x7d6903a6, /* mtctr 11 */
4184     0x4e800420, /* bctr */
4185   };
4186
4187 static bfd_boolean
4188 ppc_elf_relax_section (bfd *abfd,
4189                        asection *isec,
4190                        struct bfd_link_info *link_info,
4191                        bfd_boolean *again)
4192 {
4193   struct one_fixup
4194   {
4195     struct one_fixup *next;
4196     asection *tsec;
4197     bfd_vma toff;
4198     bfd_vma trampoff;
4199   };
4200
4201   Elf_Internal_Shdr *symtab_hdr;
4202   bfd_byte *contents = NULL;
4203   Elf_Internal_Sym *isymbuf = NULL;
4204   Elf_Internal_Rela *internal_relocs = NULL;
4205   Elf_Internal_Rela *irel, *irelend;
4206   struct one_fixup *fixups = NULL;
4207   bfd_boolean changed;
4208   struct ppc_elf_link_hash_table *ppc_info;
4209   bfd_size_type trampoff;
4210
4211   *again = FALSE;
4212
4213   /* Nothing to do if there are no relocations.  */
4214   if ((isec->flags & SEC_RELOC) == 0 || isec->reloc_count == 0)
4215     return TRUE;
4216
4217   trampoff = (isec->size + 3) & (bfd_vma) -4;
4218   /* Space for a branch around any trampolines.  */
4219   trampoff += 4;
4220
4221   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4222
4223   /* Get a copy of the native relocations.  */
4224   internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
4225                                                link_info->keep_memory);
4226   if (internal_relocs == NULL)
4227     goto error_return;
4228
4229   ppc_info = ppc_elf_hash_table (link_info);
4230   irelend = internal_relocs + isec->reloc_count;
4231
4232   for (irel = internal_relocs; irel < irelend; irel++)
4233     {
4234       unsigned long r_type = ELF32_R_TYPE (irel->r_info);
4235       bfd_vma symaddr, reladdr, toff, roff;
4236       asection *tsec;
4237       struct one_fixup *f;
4238       size_t insn_offset = 0;
4239       bfd_vma max_branch_offset, val;
4240       bfd_byte *hit_addr;
4241       unsigned long t0;
4242       unsigned char sym_type;
4243
4244       switch (r_type)
4245         {
4246         case R_PPC_REL24:
4247         case R_PPC_LOCAL24PC:
4248         case R_PPC_PLTREL24:
4249           max_branch_offset = 1 << 25;
4250           break;
4251
4252         case R_PPC_REL14:
4253         case R_PPC_REL14_BRTAKEN:
4254         case R_PPC_REL14_BRNTAKEN:
4255           max_branch_offset = 1 << 15;
4256           break;
4257
4258         default:
4259           continue;
4260         }
4261
4262       /* Get the value of the symbol referred to by the reloc.  */
4263       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
4264         {
4265           /* A local symbol.  */
4266           Elf_Internal_Sym *isym;
4267
4268           /* Read this BFD's local symbols.  */
4269           if (isymbuf == NULL)
4270             {
4271               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4272               if (isymbuf == NULL)
4273                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4274                                                 symtab_hdr->sh_info, 0,
4275                                                 NULL, NULL, NULL);
4276               if (isymbuf == 0)
4277                 goto error_return;
4278             }
4279           isym = isymbuf + ELF32_R_SYM (irel->r_info);
4280           if (isym->st_shndx == SHN_UNDEF)
4281             continue;   /* We can't do anything with undefined symbols.  */
4282           else if (isym->st_shndx == SHN_ABS)
4283             tsec = bfd_abs_section_ptr;
4284           else if (isym->st_shndx == SHN_COMMON)
4285             tsec = bfd_com_section_ptr;
4286           else
4287             tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4288
4289           toff = isym->st_value;
4290           sym_type = ELF_ST_TYPE (isym->st_info);
4291         }
4292       else
4293         {
4294           /* Global symbol handling.  */
4295           unsigned long indx;
4296           struct elf_link_hash_entry *h;
4297
4298           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
4299           h = elf_sym_hashes (abfd)[indx];
4300
4301           while (h->root.type == bfd_link_hash_indirect
4302                  || h->root.type == bfd_link_hash_warning)
4303             h = (struct elf_link_hash_entry *) h->root.u.i.link;
4304
4305           if (r_type == R_PPC_PLTREL24
4306               && ppc_info->plt != NULL
4307               && h->plt.offset != (bfd_vma) -1)
4308             {
4309               tsec = ppc_info->plt;
4310               toff = h->plt.offset;
4311             }
4312           else if (h->root.type == bfd_link_hash_defined
4313                    || h->root.type == bfd_link_hash_defweak)
4314             {
4315               tsec = h->root.u.def.section;
4316               toff = h->root.u.def.value;
4317             }
4318           else
4319             continue;
4320
4321           sym_type = h->type;
4322         }
4323
4324       /* If the branch and target are in the same section, you have
4325          no hope of adding stubs.  We'll error out later should the
4326          branch overflow.  */
4327       if (tsec == isec)
4328         continue;
4329
4330       /* There probably isn't any reason to handle symbols in
4331          SEC_MERGE sections;  SEC_MERGE doesn't seem a likely
4332          attribute for a code section, and we are only looking at
4333          branches.  However, implement it correctly here as a
4334          reference for other target relax_section functions.  */
4335       if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
4336         {
4337           /* At this stage in linking, no SEC_MERGE symbol has been
4338              adjusted, so all references to such symbols need to be
4339              passed through _bfd_merged_section_offset.  (Later, in
4340              relocate_section, all SEC_MERGE symbols *except* for
4341              section symbols have been adjusted.)
4342
4343              gas may reduce relocations against symbols in SEC_MERGE
4344              sections to a relocation against the section symbol when
4345              the original addend was zero.  When the reloc is against
4346              a section symbol we should include the addend in the
4347              offset passed to _bfd_merged_section_offset, since the
4348              location of interest is the original symbol.  On the
4349              other hand, an access to "sym+addend" where "sym" is not
4350              a section symbol should not include the addend;  Such an
4351              access is presumed to be an offset from "sym";  The
4352              location of interest is just "sym".  */
4353           if (sym_type == STT_SECTION)
4354             toff += irel->r_addend;
4355
4356           toff = _bfd_merged_section_offset (abfd, &tsec,
4357                                              elf_section_data (tsec)->sec_info,
4358                                              toff);
4359
4360           if (sym_type != STT_SECTION)
4361             toff += irel->r_addend;
4362         }
4363       else
4364         toff += irel->r_addend;
4365
4366       symaddr = tsec->output_section->vma + tsec->output_offset + toff;
4367
4368       roff = irel->r_offset;
4369       reladdr = isec->output_section->vma + isec->output_offset + roff;
4370
4371       /* If the branch is in range, no need to do anything.  */
4372       if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
4373         continue;
4374
4375       /* Look for an existing fixup to this address.  */
4376       for (f = fixups; f ; f = f->next)
4377         if (f->tsec == tsec && f->toff == toff)
4378           break;
4379
4380       if (f == NULL)
4381         {
4382           size_t size;
4383           unsigned long stub_rtype;
4384
4385           val = trampoff - roff;
4386           if (val >= max_branch_offset)
4387             /* Oh dear, we can't reach a trampoline.  Don't try to add
4388                one.  We'll report an error later.  */
4389             continue;
4390
4391           if (link_info->shared)
4392             {
4393               size = 4 * ARRAY_SIZE (shared_stub_entry);
4394               insn_offset = 12;
4395               stub_rtype = R_PPC_RELAX32PC;
4396             }
4397           else
4398             {
4399               size = 4 * ARRAY_SIZE (stub_entry);
4400               insn_offset = 0;
4401               stub_rtype = R_PPC_RELAX32;
4402             }
4403
4404           if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
4405               != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
4406             abort ();
4407           if (tsec == ppc_info->plt)
4408             stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
4409
4410           /* Hijack the old relocation.  Since we need two
4411              relocations for this use a "composite" reloc.  */
4412           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4413                                        stub_rtype);
4414           irel->r_offset = trampoff + insn_offset;
4415
4416           /* Record the fixup so we don't do it again this section.  */
4417           f = bfd_malloc (sizeof (*f));
4418           f->next = fixups;
4419           f->tsec = tsec;
4420           f->toff = toff;
4421           f->trampoff = trampoff;
4422           fixups = f;
4423
4424           trampoff += size;
4425         }
4426       else
4427         {
4428           val = f->trampoff - roff;
4429           if (val >= max_branch_offset)
4430             continue;
4431
4432           /* Nop out the reloc, since we're finalizing things here.  */
4433           irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
4434         }
4435
4436       /* Get the section contents.  */
4437       if (contents == NULL)
4438         {
4439           /* Get cached copy if it exists.  */
4440           if (elf_section_data (isec)->this_hdr.contents != NULL)
4441             contents = elf_section_data (isec)->this_hdr.contents;
4442           else
4443             {
4444               /* Go get them off disk.  */
4445               if (!bfd_malloc_and_get_section (abfd, isec, &contents))
4446                 goto error_return;
4447             }
4448         }
4449
4450       /* Fix up the existing branch to hit the trampoline.  */
4451       hit_addr = contents + roff;
4452       switch (r_type)
4453         {
4454         case R_PPC_REL24:
4455         case R_PPC_LOCAL24PC:
4456         case R_PPC_PLTREL24:
4457           t0 = bfd_get_32 (abfd, hit_addr);
4458           t0 &= ~0x3fffffc;
4459           t0 |= val & 0x3fffffc;
4460           bfd_put_32 (abfd, t0, hit_addr);
4461           break;
4462
4463         case R_PPC_REL14:
4464         case R_PPC_REL14_BRTAKEN:
4465         case R_PPC_REL14_BRNTAKEN:
4466           t0 = bfd_get_32 (abfd, hit_addr);
4467           t0 &= ~0xfffc;
4468           t0 |= val & 0xfffc;
4469           bfd_put_32 (abfd, t0, hit_addr);
4470           break;
4471         }
4472     }
4473
4474   /* Write out the trampolines.  */
4475   changed = fixups != NULL;
4476   if (fixups != NULL)
4477     {
4478       const int *stub;
4479       bfd_byte *dest;
4480       bfd_vma val;
4481       int i, size;
4482
4483       do
4484         {
4485           struct one_fixup *f = fixups;
4486           fixups = fixups->next;
4487           free (f);
4488         }
4489       while (fixups);
4490
4491       contents = bfd_realloc (contents, trampoff);
4492       if (contents == NULL)
4493         goto error_return;
4494
4495       isec->size = (isec->size + 3) & (bfd_vma) -4;
4496       /* Branch around the trampolines.  */
4497       val = trampoff - isec->size + 0x48000000;
4498       dest = contents + isec->size;
4499       isec->size = trampoff;
4500       bfd_put_32 (abfd, val, dest);
4501       dest += 4;
4502
4503       if (link_info->shared)
4504         {
4505           stub = shared_stub_entry;
4506           size = ARRAY_SIZE (shared_stub_entry);
4507         }
4508       else
4509         {
4510           stub = stub_entry;
4511           size = ARRAY_SIZE (stub_entry);
4512         }
4513
4514       i = 0;
4515       while (dest < contents + trampoff)
4516         {
4517           bfd_put_32 (abfd, stub[i], dest);
4518           i++;
4519           if (i == size)
4520             i = 0;
4521           dest += 4;
4522         }
4523       BFD_ASSERT (i == 0);
4524     }
4525
4526   if (isymbuf != NULL
4527       && symtab_hdr->contents != (unsigned char *) isymbuf)
4528     {
4529       if (! link_info->keep_memory)
4530         free (isymbuf);
4531       else
4532         {
4533           /* Cache the symbols for elf_link_input_bfd.  */
4534           symtab_hdr->contents = (unsigned char *) isymbuf;
4535         }
4536     }
4537
4538   if (contents != NULL
4539       && elf_section_data (isec)->this_hdr.contents != contents)
4540     {
4541       if (!changed && !link_info->keep_memory)
4542         free (contents);
4543       else
4544         {
4545           /* Cache the section contents for elf_link_input_bfd.  */
4546           elf_section_data (isec)->this_hdr.contents = contents;
4547         }
4548     }
4549
4550   if (elf_section_data (isec)->relocs != internal_relocs)
4551     {
4552       if (!changed)
4553         free (internal_relocs);
4554       else
4555         elf_section_data (isec)->relocs = internal_relocs;
4556     }
4557
4558   *again = changed;
4559   return TRUE;
4560
4561  error_return:
4562   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
4563     free (isymbuf);
4564   if (contents != NULL
4565       && elf_section_data (isec)->this_hdr.contents != contents)
4566     free (contents);
4567   if (internal_relocs != NULL
4568       && elf_section_data (isec)->relocs != internal_relocs)
4569     free (internal_relocs);
4570   return FALSE;
4571 }
4572 \f
4573 /* Set SYM_NAME to VAL if the symbol exists and is undefined.  */
4574
4575 static void
4576 set_linker_sym (struct ppc_elf_link_hash_table *htab,
4577                 const char *sym_name,
4578                 bfd_vma val)
4579 {
4580   struct elf_link_hash_entry *h;
4581   h = elf_link_hash_lookup (&htab->elf, sym_name, FALSE, FALSE, FALSE);
4582   if (h != NULL && h->root.type == bfd_link_hash_undefined)
4583     {
4584       h->root.type = bfd_link_hash_defined;
4585       h->root.u.def.section = bfd_abs_section_ptr;
4586       h->root.u.def.value = val;
4587       h->def_regular = 1;
4588       h->type = STT_OBJECT;
4589       h->other = STV_HIDDEN;
4590     }
4591 }
4592
4593 /* Set _SDA_BASE_, _SDA2_BASE, and sbss start and end syms.  They are
4594    set here rather than via PROVIDE in the default linker script,
4595    because using PROVIDE inside an output section statement results in
4596    unnecessary output sections.  Using PROVIDE outside an output section
4597    statement runs the risk of section alignment affecting where the
4598    section starts.  */
4599
4600 bfd_boolean
4601 ppc_elf_set_sdata_syms (bfd *obfd, struct bfd_link_info *info)
4602 {
4603   struct ppc_elf_link_hash_table *htab;
4604   unsigned i;
4605   asection *s;
4606   bfd_vma val;
4607
4608   htab = ppc_elf_hash_table (info);
4609
4610   for (i = 0; i < 2; i++)
4611     {
4612       elf_linker_section_t *lsect = &htab->sdata[i];
4613
4614       s = lsect->section;
4615       if (s != NULL)
4616         s = s->output_section;
4617       if (s == NULL)
4618         s = bfd_get_section_by_name (obfd, lsect->name);
4619       if (s == NULL)
4620         s = bfd_get_section_by_name (obfd, lsect->bss_name);
4621
4622       val = 0;
4623       if (s != NULL)
4624         val = s->vma + 32768;
4625       lsect->sym_val = val;
4626
4627       set_linker_sym (htab, lsect->sym_name, val);
4628     }
4629
4630   s = bfd_get_section_by_name (obfd, ".sbss");
4631   val = 0;
4632   if (s != NULL)
4633     val = s->vma;
4634   set_linker_sym (htab, "__sbss_start", val);
4635   set_linker_sym (htab, "___sbss_start", val);
4636   if (s != NULL)
4637     val += s->size;
4638   set_linker_sym (htab, "__sbss_end", val);
4639   set_linker_sym (htab, "___sbss_end", val);
4640   return TRUE;
4641 }
4642 \f
4643 #define bfd_put_ptr(BFD, VAL, ADDR) bfd_put_32 (BFD, VAL, ADDR)
4644
4645 /* Fill in the address for a pointer generated in a linker section.  */
4646
4647 static bfd_vma
4648 elf_finish_pointer_linker_section (bfd *output_bfd,
4649                                    bfd *input_bfd,
4650                                    struct bfd_link_info *info,
4651                                    elf_linker_section_t *lsect,
4652                                    struct elf_link_hash_entry *h,
4653                                    bfd_vma relocation,
4654                                    const Elf_Internal_Rela *rel)
4655 {
4656   elf_linker_section_pointers_t *linker_section_ptr;
4657
4658   BFD_ASSERT (lsect != NULL);
4659
4660   if (h != NULL)
4661     {
4662       /* Handle global symbol.  */
4663       struct ppc_elf_link_hash_entry *eh;
4664
4665       eh = (struct ppc_elf_link_hash_entry *) h;
4666       linker_section_ptr
4667         = elf_find_pointer_linker_section (eh->linker_section_pointer,
4668                                            rel->r_addend,
4669                                            lsect);
4670
4671       BFD_ASSERT (linker_section_ptr != NULL);
4672
4673       if (! elf_hash_table (info)->dynamic_sections_created
4674           || (info->shared
4675               && info->symbolic
4676               && h->def_regular))
4677         {
4678           /* This is actually a static link, or it is a
4679              -Bsymbolic link and the symbol is defined
4680              locally.  We must initialize this entry in the
4681              global section.
4682
4683              When doing a dynamic link, we create a .rela.<xxx>
4684              relocation entry to initialize the value.  This
4685              is done in the finish_dynamic_symbol routine.  */
4686           if (!linker_section_ptr->written_address_p)
4687             {
4688               linker_section_ptr->written_address_p = TRUE;
4689               bfd_put_ptr (output_bfd,
4690                            relocation + linker_section_ptr->addend,
4691                            (lsect->section->contents
4692                             + linker_section_ptr->offset));
4693             }
4694         }
4695     }
4696   else
4697     {
4698       /* Handle local symbol.  */
4699       unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
4700       BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
4701       BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
4702       linker_section_ptr = (elf_find_pointer_linker_section
4703                             (elf_local_ptr_offsets (input_bfd)[r_symndx],
4704                              rel->r_addend,
4705                              lsect));
4706
4707       BFD_ASSERT (linker_section_ptr != NULL);
4708
4709       /* Write out pointer if it hasn't been rewritten out before.  */
4710       if (!linker_section_ptr->written_address_p)
4711         {
4712           linker_section_ptr->written_address_p = TRUE;
4713           bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
4714                        lsect->section->contents + linker_section_ptr->offset);
4715         }
4716     }
4717
4718   relocation = (lsect->section->output_offset
4719                 + linker_section_ptr->offset
4720                 - 0x8000);
4721
4722 #ifdef DEBUG
4723   fprintf (stderr,
4724            "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
4725            lsect->name, (long) relocation, (long) relocation);
4726 #endif
4727
4728   /* Subtract out the addend, because it will get added back in by the normal
4729      processing.  */
4730   return relocation - linker_section_ptr->addend;
4731 }
4732
4733 /* The RELOCATE_SECTION function is called by the ELF backend linker
4734    to handle the relocations for a section.
4735
4736    The relocs are always passed as Rela structures; if the section
4737    actually uses Rel structures, the r_addend field will always be
4738    zero.
4739
4740    This function is responsible for adjust the section contents as
4741    necessary, and (if using Rela relocs and generating a
4742    relocatable output file) adjusting the reloc addend as
4743    necessary.
4744
4745    This function does not have to worry about setting the reloc
4746    address or the reloc symbol index.
4747
4748    LOCAL_SYMS is a pointer to the swapped in local symbols.
4749
4750    LOCAL_SECTIONS is an array giving the section in the input file
4751    corresponding to the st_shndx field of each local symbol.
4752
4753    The global hash table entry for the global symbols can be found
4754    via elf_sym_hashes (input_bfd).
4755
4756    When generating relocatable output, this function must handle
4757    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4758    going to be the section symbol corresponding to the output
4759    section, which means that the addend must be adjusted
4760    accordingly.  */
4761
4762 static bfd_boolean
4763 ppc_elf_relocate_section (bfd *output_bfd,
4764                           struct bfd_link_info *info,
4765                           bfd *input_bfd,
4766                           asection *input_section,
4767                           bfd_byte *contents,
4768                           Elf_Internal_Rela *relocs,
4769                           Elf_Internal_Sym *local_syms,
4770                           asection **local_sections)
4771 {
4772   Elf_Internal_Shdr *symtab_hdr;
4773   struct elf_link_hash_entry **sym_hashes;
4774   struct ppc_elf_link_hash_table *htab;
4775   Elf_Internal_Rela *rel;
4776   Elf_Internal_Rela *relend;
4777   Elf_Internal_Rela outrel;
4778   bfd_byte *loc;
4779   asection *sreloc = NULL;
4780   bfd_vma *local_got_offsets;
4781   bfd_boolean ret = TRUE;
4782
4783 #ifdef DEBUG
4784   _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
4785                       "%ld relocations%s",
4786                       input_bfd, input_section,
4787                       (long) input_section->reloc_count,
4788                       (info->relocatable) ? " (relocatable)" : "");
4789 #endif
4790
4791   if (info->relocatable)
4792     return TRUE;
4793
4794   /* Initialize howto table if not already done.  */
4795   if (!ppc_elf_howto_table[R_PPC_ADDR32])
4796     ppc_elf_howto_init ();
4797
4798   htab = ppc_elf_hash_table (info);
4799   local_got_offsets = elf_local_got_offsets (input_bfd);
4800   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4801   sym_hashes = elf_sym_hashes (input_bfd);
4802   rel = relocs;
4803   relend = relocs + input_section->reloc_count;
4804   for (; rel < relend; rel++)
4805     {
4806       enum elf_ppc_reloc_type r_type;
4807       bfd_vma addend;
4808       bfd_reloc_status_type r;
4809       Elf_Internal_Sym *sym;
4810       asection *sec;
4811       struct elf_link_hash_entry *h;
4812       const char *sym_name;
4813       reloc_howto_type *howto;
4814       unsigned long r_symndx;
4815       bfd_vma relocation;
4816       bfd_vma branch_bit, insn, from;
4817       bfd_boolean unresolved_reloc;
4818       bfd_boolean warned;
4819       unsigned int tls_type, tls_mask, tls_gd;
4820
4821       r_type = ELF32_R_TYPE (rel->r_info);
4822       sym = NULL;
4823       sec = NULL;
4824       h = NULL;
4825       unresolved_reloc = FALSE;
4826       warned = FALSE;
4827       r_symndx = ELF32_R_SYM (rel->r_info);
4828
4829       if (r_symndx < symtab_hdr->sh_info)
4830         {
4831           sym = local_syms + r_symndx;
4832           sec = local_sections[r_symndx];
4833           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym);
4834
4835           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4836         }
4837       else
4838         {
4839           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4840                                    r_symndx, symtab_hdr, sym_hashes,
4841                                    h, sec, relocation,
4842                                    unresolved_reloc, warned);
4843
4844           sym_name = h->root.root.string;
4845         }
4846
4847       /* TLS optimizations.  Replace instruction sequences and relocs
4848          based on information we collected in tls_optimize.  We edit
4849          RELOCS so that --emit-relocs will output something sensible
4850          for the final instruction stream.  */
4851       tls_mask = 0;
4852       tls_gd = 0;
4853       if (IS_PPC_TLS_RELOC (r_type))
4854         {
4855           if (h != NULL)
4856             tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4857           else if (local_got_offsets != NULL)
4858             {
4859               char *lgot_masks;
4860               lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4861               tls_mask = lgot_masks[r_symndx];
4862             }
4863         }
4864
4865       /* Ensure reloc mapping code below stays sane.  */
4866       if ((R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TLSGD16 & 3)
4867           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4868           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4869           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4870           || (R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TPREL16 & 3)
4871           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4872           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4873           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4874         abort ();
4875       switch (r_type)
4876         {
4877         default:
4878           break;
4879
4880         case R_PPC_GOT_TPREL16:
4881         case R_PPC_GOT_TPREL16_LO:
4882           if (tls_mask != 0
4883               && (tls_mask & TLS_TPREL) == 0)
4884             {
4885               bfd_vma insn;
4886               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4887               insn &= 31 << 21;
4888               insn |= 0x3c020000;       /* addis 0,2,0 */
4889               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4890               r_type = R_PPC_TPREL16_HA;
4891               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4892             }
4893           break;
4894
4895         case R_PPC_TLS:
4896           if (tls_mask != 0
4897               && (tls_mask & TLS_TPREL) == 0)
4898             {
4899               bfd_vma insn, rtra;
4900               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4901               if ((insn & ((31 << 26) | (31 << 11)))
4902                   == ((31 << 26) | (2 << 11)))
4903                 rtra = insn & ((1 << 26) - (1 << 16));
4904               else if ((insn & ((31 << 26) | (31 << 16)))
4905                        == ((31 << 26) | (2 << 16)))
4906                 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4907               else
4908                 abort ();
4909               if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4910                 /* add -> addi.  */
4911                 insn = 14 << 26;
4912               else if ((insn & (31 << 1)) == 23 << 1
4913                        && ((insn & (31 << 6)) < 14 << 6
4914                            || ((insn & (31 << 6)) >= 16 << 6
4915                                && (insn & (31 << 6)) < 24 << 6)))
4916                 /* load and store indexed -> dform.  */
4917                 insn = (32 | ((insn >> 6) & 31)) << 26;
4918               else if ((insn & (31 << 1)) == 21 << 1
4919                        && (insn & (0x1a << 6)) == 0)
4920                 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
4921                 insn = (((58 | ((insn >> 6) & 4)) << 26)
4922                         | ((insn >> 6) & 1));
4923               else if ((insn & (31 << 1)) == 21 << 1
4924                        && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4925                 /* lwax -> lwa.  */
4926                 insn = (58 << 26) | 2;
4927               else
4928                 abort ();
4929               insn |= rtra;
4930               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4931               r_type = R_PPC_TPREL16_LO;
4932               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4933               /* Was PPC_TLS which sits on insn boundary, now
4934                  PPC_TPREL16_LO which is at insn+2.  */
4935               rel->r_offset += 2;
4936             }
4937           break;
4938
4939         case R_PPC_GOT_TLSGD16_HI:
4940         case R_PPC_GOT_TLSGD16_HA:
4941           tls_gd = TLS_TPRELGD;
4942           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4943             goto tls_gdld_hi;
4944           break;
4945
4946         case R_PPC_GOT_TLSLD16_HI:
4947         case R_PPC_GOT_TLSLD16_HA:
4948           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4949             {
4950             tls_gdld_hi:
4951               if ((tls_mask & tls_gd) != 0)
4952                 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4953                           + R_PPC_GOT_TPREL16);
4954               else
4955                 {
4956                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4957                   rel->r_offset -= 2;
4958                   r_type = R_PPC_NONE;
4959                 }
4960               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4961             }
4962           break;
4963
4964         case R_PPC_GOT_TLSGD16:
4965         case R_PPC_GOT_TLSGD16_LO:
4966           tls_gd = TLS_TPRELGD;
4967           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4968             goto tls_get_addr_check;
4969           break;
4970
4971         case R_PPC_GOT_TLSLD16:
4972         case R_PPC_GOT_TLSLD16_LO:
4973           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4974             {
4975             tls_get_addr_check:
4976               if (rel + 1 < relend)
4977                 {
4978                   enum elf_ppc_reloc_type r_type2;
4979                   unsigned long r_symndx2;
4980                   struct elf_link_hash_entry *h2;
4981                   bfd_vma insn1, insn2;
4982                   bfd_vma offset;
4983
4984                   /* The next instruction should be a call to
4985                      __tls_get_addr.  Peek at the reloc to be sure.  */
4986                   r_type2 = ELF32_R_TYPE (rel[1].r_info);
4987                   r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4988                   if (r_symndx2 < symtab_hdr->sh_info
4989                       || (r_type2 != R_PPC_REL14
4990                           && r_type2 != R_PPC_REL14_BRTAKEN
4991                           && r_type2 != R_PPC_REL14_BRNTAKEN
4992                           && r_type2 != R_PPC_REL24
4993                           && r_type2 != R_PPC_PLTREL24))
4994                     break;
4995
4996                   h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4997                   while (h2->root.type == bfd_link_hash_indirect
4998                          || h2->root.type == bfd_link_hash_warning)
4999                     h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
5000                   if (h2 == NULL || h2 != htab->tls_get_addr)
5001                     break;
5002
5003                   /* OK, it checks out.  Replace the call.  */
5004                   offset = rel[1].r_offset;
5005                   insn1 = bfd_get_32 (output_bfd,
5006                                       contents + rel->r_offset - 2);
5007                   if ((tls_mask & tls_gd) != 0)
5008                     {
5009                       /* IE */
5010                       insn1 &= (1 << 26) - 1;
5011                       insn1 |= 32 << 26;        /* lwz */
5012                       insn2 = 0x7c631214;       /* add 3,3,2 */
5013                       rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
5014                       r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
5015                                 + R_PPC_GOT_TPREL16);
5016                       rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5017                     }
5018                   else
5019                     {
5020                       /* LE */
5021                       insn1 = 0x3c620000;       /* addis 3,2,0 */
5022                       insn2 = 0x38630000;       /* addi 3,3,0 */
5023                       if (tls_gd == 0)
5024                         {
5025                           /* Was an LD reloc.  */
5026                           r_symndx = 0;
5027                           rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
5028                           rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
5029                         }
5030                       r_type = R_PPC_TPREL16_HA;
5031                       rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5032                       rel[1].r_info = ELF32_R_INFO (r_symndx,
5033                                                     R_PPC_TPREL16_LO);
5034                       rel[1].r_offset += 2;
5035                     }
5036                   bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
5037                   bfd_put_32 (output_bfd, insn2, contents + offset);
5038                   if (tls_gd == 0)
5039                     {
5040                       /* We changed the symbol on an LD reloc.  Start over
5041                          in order to get h, sym, sec etc. right.  */
5042                       rel--;
5043                       continue;
5044                     }
5045                 }
5046             }
5047           break;
5048         }
5049
5050       /* Handle other relocations that tweak non-addend part of insn.  */
5051       branch_bit = 0;
5052       switch (r_type)
5053         {
5054         default:
5055           break;
5056
5057           /* Branch taken prediction relocations.  */
5058         case R_PPC_ADDR14_BRTAKEN:
5059         case R_PPC_REL14_BRTAKEN:
5060           branch_bit = BRANCH_PREDICT_BIT;
5061           /* Fall thru */
5062
5063           /* Branch not taken prediction relocations.  */
5064         case R_PPC_ADDR14_BRNTAKEN:
5065         case R_PPC_REL14_BRNTAKEN:
5066           insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5067           insn &= ~BRANCH_PREDICT_BIT;
5068           insn |= branch_bit;
5069
5070           from = (rel->r_offset
5071                   + input_section->output_offset
5072                   + input_section->output_section->vma);
5073
5074           /* Invert 'y' bit if not the default.  */
5075           if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
5076             insn ^= BRANCH_PREDICT_BIT;
5077
5078           bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5079           break;
5080         }
5081
5082       addend = rel->r_addend;
5083       tls_type = 0;
5084       howto = NULL;
5085       if (r_type < R_PPC_max)
5086         howto = ppc_elf_howto_table[r_type];
5087       switch (r_type)
5088         {
5089         default:
5090           (*_bfd_error_handler)
5091             (_("%B: unknown relocation type %d for symbol %s"),
5092              input_bfd, (int) r_type, sym_name);
5093
5094           bfd_set_error (bfd_error_bad_value);
5095           ret = FALSE;
5096           continue;
5097
5098         case R_PPC_NONE:
5099         case R_PPC_TLS:
5100         case R_PPC_EMB_MRKREF:
5101         case R_PPC_GNU_VTINHERIT:
5102         case R_PPC_GNU_VTENTRY:
5103           continue;
5104
5105           /* GOT16 relocations.  Like an ADDR16 using the symbol's
5106              address in the GOT as relocation value instead of the
5107              symbol's value itself.  Also, create a GOT entry for the
5108              symbol and put the symbol value there.  */
5109         case R_PPC_GOT_TLSGD16:
5110         case R_PPC_GOT_TLSGD16_LO:
5111         case R_PPC_GOT_TLSGD16_HI:
5112         case R_PPC_GOT_TLSGD16_HA:
5113           tls_type = TLS_TLS | TLS_GD;
5114           goto dogot;
5115
5116         case R_PPC_GOT_TLSLD16:
5117         case R_PPC_GOT_TLSLD16_LO:
5118         case R_PPC_GOT_TLSLD16_HI:
5119         case R_PPC_GOT_TLSLD16_HA:
5120           tls_type = TLS_TLS | TLS_LD;
5121           goto dogot;
5122
5123         case R_PPC_GOT_TPREL16:
5124         case R_PPC_GOT_TPREL16_LO:
5125         case R_PPC_GOT_TPREL16_HI:
5126         case R_PPC_GOT_TPREL16_HA:
5127           tls_type = TLS_TLS | TLS_TPREL;
5128           goto dogot;
5129
5130         case R_PPC_GOT_DTPREL16:
5131         case R_PPC_GOT_DTPREL16_LO:
5132         case R_PPC_GOT_DTPREL16_HI:
5133         case R_PPC_GOT_DTPREL16_HA:
5134           tls_type = TLS_TLS | TLS_DTPREL;
5135           goto dogot;
5136
5137         case R_PPC_GOT16:
5138         case R_PPC_GOT16_LO:
5139         case R_PPC_GOT16_HI:
5140         case R_PPC_GOT16_HA:
5141         dogot:
5142           {
5143             /* Relocation is to the entry for this symbol in the global
5144                offset table.  */
5145             bfd_vma off;
5146             bfd_vma *offp;
5147             unsigned long indx;
5148
5149             if (htab->got == NULL)
5150               abort ();
5151
5152             indx = 0;
5153             if (tls_type == (TLS_TLS | TLS_LD)
5154                 && (h == NULL
5155                     || !h->def_dynamic))
5156               offp = &htab->tlsld_got.offset;
5157             else if (h != NULL)
5158               {
5159                 bfd_boolean dyn;
5160                 dyn = htab->elf.dynamic_sections_created;
5161                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5162                     || (info->shared
5163                         && SYMBOL_REFERENCES_LOCAL (info, h)))
5164                   /* This is actually a static link, or it is a
5165                      -Bsymbolic link and the symbol is defined
5166                      locally, or the symbol was forced to be local
5167                      because of a version file.  */
5168                   ;
5169                 else
5170                   {
5171                     indx = h->dynindx;
5172                     unresolved_reloc = FALSE;
5173                   }
5174                 offp = &h->got.offset;
5175               }
5176             else
5177               {
5178                 if (local_got_offsets == NULL)
5179                   abort ();
5180                 offp = &local_got_offsets[r_symndx];
5181               }
5182
5183             /* The offset must always be a multiple of 4.  We use the
5184                least significant bit to record whether we have already
5185                processed this entry.  */
5186             off = *offp;
5187             if ((off & 1) != 0)
5188               off &= ~1;
5189             else
5190               {
5191                 unsigned int tls_m = (tls_mask
5192                                       & (TLS_LD | TLS_GD | TLS_DTPREL
5193                                          | TLS_TPREL | TLS_TPRELGD));
5194
5195                 if (offp == &htab->tlsld_got.offset)
5196                   tls_m = TLS_LD;
5197                 else if (h == NULL
5198                          || !h->def_dynamic)
5199                   tls_m &= ~TLS_LD;
5200
5201                 /* We might have multiple got entries for this sym.
5202                    Initialize them all.  */
5203                 do
5204                   {
5205                     int tls_ty = 0;
5206
5207                     if ((tls_m & TLS_LD) != 0)
5208                       {
5209                         tls_ty = TLS_TLS | TLS_LD;
5210                         tls_m &= ~TLS_LD;
5211                       }
5212                     else if ((tls_m & TLS_GD) != 0)
5213                       {
5214                         tls_ty = TLS_TLS | TLS_GD;
5215                         tls_m &= ~TLS_GD;
5216                       }
5217                     else if ((tls_m & TLS_DTPREL) != 0)
5218                       {
5219                         tls_ty = TLS_TLS | TLS_DTPREL;
5220                         tls_m &= ~TLS_DTPREL;
5221                       }
5222                     else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5223                       {
5224                         tls_ty = TLS_TLS | TLS_TPREL;
5225                         tls_m = 0;
5226                       }
5227
5228                     /* Generate relocs for the dynamic linker.  */
5229                     if ((info->shared || indx != 0)
5230                         && (h == NULL
5231                             || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5232                             || h->root.type != bfd_link_hash_undefweak))
5233                       {
5234                         outrel.r_offset = (htab->got->output_section->vma
5235                                            + htab->got->output_offset
5236                                            + off);
5237                         outrel.r_addend = 0;
5238                         if (tls_ty & (TLS_LD | TLS_GD))
5239                           {
5240                             outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
5241                             if (tls_ty == (TLS_TLS | TLS_GD))
5242                               {
5243                                 loc = htab->relgot->contents;
5244                                 loc += (htab->relgot->reloc_count++
5245                                         * sizeof (Elf32_External_Rela));
5246                                 bfd_elf32_swap_reloca_out (output_bfd,
5247                                                            &outrel, loc);
5248                                 outrel.r_offset += 4;
5249                                 outrel.r_info
5250                                   = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5251                               }
5252                           }
5253                         else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5254                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5255                         else if (tls_ty == (TLS_TLS | TLS_TPREL))
5256                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5257                         else if (indx == 0)
5258                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5259                         else
5260                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
5261                         if (indx == 0)
5262                           {
5263                             outrel.r_addend += relocation;
5264                             if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
5265                               outrel.r_addend -= htab->elf.tls_sec->vma;
5266                           }
5267                         loc = htab->relgot->contents;
5268                         loc += (htab->relgot->reloc_count++
5269                                 * sizeof (Elf32_External_Rela));
5270                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5271                       }
5272
5273                     /* Init the .got section contents if we're not
5274                        emitting a reloc.  */
5275                     else
5276                       {
5277                         bfd_vma value = relocation;
5278
5279                         if (tls_ty == (TLS_TLS | TLS_LD))
5280                           value = 1;
5281                         else if (tls_ty != 0)
5282                           {
5283                             value -= htab->elf.tls_sec->vma + DTP_OFFSET;
5284                             if (tls_ty == (TLS_TLS | TLS_TPREL))
5285                               value += DTP_OFFSET - TP_OFFSET;
5286
5287                             if (tls_ty == (TLS_TLS | TLS_GD))
5288                               {
5289                                 bfd_put_32 (output_bfd, value,
5290                                             htab->got->contents + off + 4);
5291                                 value = 1;
5292                               }
5293                           }
5294                         bfd_put_32 (output_bfd, value,
5295                                     htab->got->contents + off);
5296                       }
5297
5298                     off += 4;
5299                     if (tls_ty & (TLS_LD | TLS_GD))
5300                       off += 4;
5301                   }
5302                 while (tls_m != 0);
5303
5304                 off = *offp;
5305                 *offp = off | 1;
5306               }
5307
5308             if (off >= (bfd_vma) -2)
5309               abort ();
5310
5311             if ((tls_type & TLS_TLS) != 0)
5312               {
5313                 if (tls_type != (TLS_TLS | TLS_LD))
5314                   {
5315                     if ((tls_mask & TLS_LD) != 0
5316                         && !(h == NULL
5317                              || !h->def_dynamic))
5318                       off += 8;
5319                     if (tls_type != (TLS_TLS | TLS_GD))
5320                       {
5321                         if ((tls_mask & TLS_GD) != 0)
5322                           off += 8;
5323                         if (tls_type != (TLS_TLS | TLS_DTPREL))
5324                           {
5325                             if ((tls_mask & TLS_DTPREL) != 0)
5326                               off += 4;
5327                           }
5328                       }
5329                   }
5330               }
5331
5332             relocation = htab->got->output_offset + off - 4;
5333
5334             /* Addends on got relocations don't make much sense.
5335                x+off@got is actually x@got+off, and since the got is
5336                generated by a hash table traversal, the value in the
5337                got at entry m+n bears little relation to the entry m.  */
5338             if (addend != 0)
5339               (*_bfd_error_handler)
5340                 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
5341                  input_bfd,
5342                  input_section,
5343                  (long) rel->r_offset,
5344                  howto->name,
5345                  sym_name);
5346           }
5347         break;
5348
5349         /* Relocations that need no special processing.  */
5350         case R_PPC_LOCAL24PC:
5351           /* It makes no sense to point a local relocation
5352              at a symbol not in this object.  */
5353           if (unresolved_reloc)
5354             {
5355               if (! (*info->callbacks->undefined_symbol) (info,
5356                                                           h->root.root.string,
5357                                                           input_bfd,
5358                                                           input_section,
5359                                                           rel->r_offset,
5360                                                           TRUE))
5361                 return FALSE;
5362               continue;
5363             }
5364           break;
5365
5366         case R_PPC_DTPREL16:
5367         case R_PPC_DTPREL16_LO:
5368         case R_PPC_DTPREL16_HI:
5369         case R_PPC_DTPREL16_HA:
5370           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5371           break;
5372
5373           /* Relocations that may need to be propagated if this is a shared
5374              object.  */
5375         case R_PPC_TPREL16:
5376         case R_PPC_TPREL16_LO:
5377         case R_PPC_TPREL16_HI:
5378         case R_PPC_TPREL16_HA:
5379           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5380           /* The TPREL16 relocs shouldn't really be used in shared
5381              libs as they will result in DT_TEXTREL being set, but
5382              support them anyway.  */
5383           goto dodyn;
5384
5385         case R_PPC_TPREL32:
5386           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5387           goto dodyn;
5388
5389         case R_PPC_DTPREL32:
5390           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5391           goto dodyn;
5392
5393         case R_PPC_DTPMOD32:
5394           relocation = 1;
5395           addend = 0;
5396           goto dodyn;
5397
5398         case R_PPC_REL24:
5399         case R_PPC_REL32:
5400         case R_PPC_REL14:
5401         case R_PPC_REL14_BRTAKEN:
5402         case R_PPC_REL14_BRNTAKEN:
5403           /* If these relocations are not to a named symbol, they can be
5404              handled right here, no need to bother the dynamic linker.  */
5405           if (SYMBOL_REFERENCES_LOCAL (info, h)
5406               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5407             break;
5408           /* fall through */
5409
5410           /* Relocations that always need to be propagated if this is a shared
5411              object.  */
5412         case R_PPC_ADDR32:
5413         case R_PPC_ADDR24:
5414         case R_PPC_ADDR16:
5415         case R_PPC_ADDR16_LO:
5416         case R_PPC_ADDR16_HI:
5417         case R_PPC_ADDR16_HA:
5418         case R_PPC_ADDR14:
5419         case R_PPC_ADDR14_BRTAKEN:
5420         case R_PPC_ADDR14_BRNTAKEN:
5421         case R_PPC_UADDR32:
5422         case R_PPC_UADDR16:
5423           /* r_symndx will be zero only for relocs against symbols
5424              from removed linkonce sections, or sections discarded by
5425              a linker script.  */
5426         dodyn:
5427           if (r_symndx == 0)
5428             break;
5429           /* Fall thru.  */
5430
5431           if ((info->shared
5432                && (h == NULL
5433                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5434                    || h->root.type != bfd_link_hash_undefweak)
5435                && (MUST_BE_DYN_RELOC (r_type)
5436                    || !SYMBOL_CALLS_LOCAL (info, h)))
5437               || (ELIMINATE_COPY_RELOCS
5438                   && !info->shared
5439                   && (input_section->flags & SEC_ALLOC) != 0
5440                   && h != NULL
5441                   && h->dynindx != -1
5442                   && !h->non_got_ref
5443                   && h->def_dynamic
5444                   && !h->def_regular))
5445             {
5446               int skip;
5447
5448 #ifdef DEBUG
5449               fprintf (stderr, "ppc_elf_relocate_section needs to "
5450                        "create relocation for %s\n",
5451                        (h && h->root.root.string
5452                         ? h->root.root.string : "<unknown>"));
5453 #endif
5454
5455               /* When generating a shared object, these relocations
5456                  are copied into the output file to be resolved at run
5457                  time.  */
5458               if (sreloc == NULL)
5459                 {
5460                   const char *name;
5461
5462                   name = (bfd_elf_string_from_elf_section
5463                           (input_bfd,
5464                            elf_elfheader (input_bfd)->e_shstrndx,
5465                            elf_section_data (input_section)->rel_hdr.sh_name));
5466                   if (name == NULL)
5467                     return FALSE;
5468
5469                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5470                               && strcmp (bfd_get_section_name (input_bfd,
5471                                                                input_section),
5472                                          name + 5) == 0);
5473
5474                   sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5475                   BFD_ASSERT (sreloc != NULL);
5476                 }
5477
5478               skip = 0;
5479
5480               outrel.r_offset =
5481                 _bfd_elf_section_offset (output_bfd, info, input_section,
5482                                          rel->r_offset);
5483               if (outrel.r_offset == (bfd_vma) -1
5484                   || outrel.r_offset == (bfd_vma) -2)
5485                 skip = (int) outrel.r_offset;
5486               outrel.r_offset += (input_section->output_section->vma
5487                                   + input_section->output_offset);
5488
5489               if (skip)
5490                 memset (&outrel, 0, sizeof outrel);
5491               else if (!SYMBOL_REFERENCES_LOCAL (info, h))
5492                 {
5493                   unresolved_reloc = FALSE;
5494                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5495                   outrel.r_addend = rel->r_addend;
5496                 }
5497               else
5498                 {
5499                   outrel.r_addend = relocation + rel->r_addend;
5500
5501                   if (r_type == R_PPC_ADDR32)
5502                     outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5503                   else
5504                     {
5505                       long indx;
5506
5507                       if (bfd_is_abs_section (sec))
5508                         indx = 0;
5509                       else if (sec == NULL || sec->owner == NULL)
5510                         {
5511                           bfd_set_error (bfd_error_bad_value);
5512                           return FALSE;
5513                         }
5514                       else
5515                         {
5516                           asection *osec;
5517
5518                           /* We are turning this relocation into one
5519                              against a section symbol.  It would be
5520                              proper to subtract the symbol's value,
5521                              osec->vma, from the emitted reloc addend,
5522                              but ld.so expects buggy relocs.  */
5523                           osec = sec->output_section;
5524                           indx = elf_section_data (osec)->dynindx;
5525                           BFD_ASSERT (indx > 0);
5526 #ifdef DEBUG
5527                           if (indx <= 0)
5528                             printf ("indx=%d section=%s flags=%08x name=%s\n",
5529                                     indx, osec->name, osec->flags,
5530                                     h->root.root.string);
5531 #endif
5532                         }
5533
5534                       outrel.r_info = ELF32_R_INFO (indx, r_type);
5535                     }
5536                 }
5537
5538               loc = sreloc->contents;
5539               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5540               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5541
5542               if (skip == -1)
5543                 continue;
5544
5545               /* This reloc will be computed at runtime.  We clear the memory
5546                  so that it contains predictable value.  */
5547               if (! skip
5548                   && ((input_section->flags & SEC_ALLOC) != 0
5549                       || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5550                 {
5551                   relocation = howto->pc_relative ? outrel.r_offset : 0;
5552                   addend = 0;
5553                   break;
5554                 }
5555             }
5556           break;
5557
5558         case R_PPC_RELAX32PC_PLT:
5559         case R_PPC_RELAX32_PLT:
5560           BFD_ASSERT (h != NULL
5561                       && h->plt.offset != (bfd_vma) -1
5562                       && htab->plt != NULL);
5563
5564           relocation = (htab->plt->output_section->vma
5565                         + htab->plt->output_offset
5566                         + h->plt.offset);
5567           if (r_type == R_PPC_RELAX32_PLT)
5568             goto relax32;
5569           /* Fall thru */
5570
5571         case R_PPC_RELAX32PC:
5572           relocation -= (input_section->output_section->vma
5573                          + input_section->output_offset
5574                          + rel->r_offset - 4);
5575           /* Fall thru */
5576
5577         case R_PPC_RELAX32:
5578         relax32:
5579           {
5580             unsigned long t0;
5581             unsigned long t1;
5582
5583             t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
5584             t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
5585
5586             /* We're clearing the bits for R_PPC_ADDR16_HA
5587                and R_PPC_ADDR16_LO here.  */
5588             t0 &= ~0xffff;
5589             t1 &= ~0xffff;
5590
5591             /* t0 is HA, t1 is LO */
5592             relocation += addend;
5593             t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
5594             t1 |= relocation & 0xffff;
5595
5596             bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
5597             bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
5598           }
5599           continue;
5600
5601           /* Indirect .sdata relocation.  */
5602         case R_PPC_EMB_SDAI16:
5603           BFD_ASSERT (htab->sdata[0].section != NULL);
5604           relocation
5605             = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5606                                                  &htab->sdata[0], h,
5607                                                  relocation, rel);
5608           break;
5609
5610           /* Indirect .sdata2 relocation.  */
5611         case R_PPC_EMB_SDA2I16:
5612           BFD_ASSERT (htab->sdata[1].section != NULL);
5613           relocation
5614             = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5615                                                  &htab->sdata[1], h,
5616                                                  relocation, rel);
5617           break;
5618
5619           /* Handle the TOC16 reloc.  We want to use the offset within the .got
5620              section, not the actual VMA.  This is appropriate when generating
5621              an embedded ELF object, for which the .got section acts like the
5622              AIX .toc section.  */
5623         case R_PPC_TOC16:                       /* phony GOT16 relocations */
5624           BFD_ASSERT (sec != NULL);
5625           BFD_ASSERT (bfd_is_und_section (sec)
5626                       || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5627                       || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
5628
5629             addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5630           break;
5631
5632         case R_PPC_PLTREL24:
5633           /* Relocation is to the entry for this symbol in the
5634              procedure linkage table.  */
5635           BFD_ASSERT (h != NULL);
5636
5637           if (h->plt.offset == (bfd_vma) -1
5638               || htab->plt == NULL)
5639             {
5640               /* We didn't make a PLT entry for this symbol.  This
5641                  happens when statically linking PIC code, or when
5642                  using -Bsymbolic.  */
5643               break;
5644             }
5645
5646           unresolved_reloc = FALSE;
5647           relocation = (htab->plt->output_section->vma
5648                         + htab->plt->output_offset
5649                         + h->plt.offset);
5650           break;
5651
5652           /* Relocate against _SDA_BASE_.  */
5653         case R_PPC_SDAREL16:
5654           {
5655             const char *name;
5656
5657             BFD_ASSERT (sec != NULL);
5658             name = bfd_get_section_name (abfd, sec->output_section);
5659             if (! ((strncmp (name, ".sdata", 6) == 0
5660                     && (name[6] == 0 || name[6] == '.'))
5661                    || (strncmp (name, ".sbss", 5) == 0
5662                        && (name[5] == 0 || name[5] == '.'))))
5663               {
5664                 (*_bfd_error_handler)
5665                   (_("%B: the target (%s) of a %s relocation is "
5666                      "in the wrong output section (%s)"),
5667                    input_bfd,
5668                    sym_name,
5669                    howto->name,
5670                    name);
5671               }
5672             addend -= htab->sdata[0].sym_val;
5673           }
5674           break;
5675
5676           /* Relocate against _SDA2_BASE_.  */
5677         case R_PPC_EMB_SDA2REL:
5678           {
5679             const char *name;
5680
5681             BFD_ASSERT (sec != NULL);
5682             name = bfd_get_section_name (abfd, sec->output_section);
5683             if (! (strncmp (name, ".sdata2", 7) == 0
5684                    || strncmp (name, ".sbss2", 6) == 0))
5685               {
5686                 (*_bfd_error_handler)
5687                   (_("%B: the target (%s) of a %s relocation is "
5688                      "in the wrong output section (%s)"),
5689                    input_bfd,
5690                    sym_name,
5691                    howto->name,
5692                    name);
5693
5694                 bfd_set_error (bfd_error_bad_value);
5695                 ret = FALSE;
5696                 continue;
5697               }
5698             addend -= htab->sdata[1].sym_val;
5699           }
5700           break;
5701
5702           /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
5703         case R_PPC_EMB_SDA21:
5704         case R_PPC_EMB_RELSDA:
5705           {
5706             const char *name;
5707             int reg;
5708
5709             BFD_ASSERT (sec != NULL);
5710             name = bfd_get_section_name (abfd, sec->output_section);
5711             if (((strncmp (name, ".sdata", 6) == 0
5712                   && (name[6] == 0 || name[6] == '.'))
5713                  || (strncmp (name, ".sbss", 5) == 0
5714                      && (name[5] == 0 || name[5] == '.'))))
5715               {
5716                 reg = 13;
5717                 addend -= htab->sdata[0].sym_val;
5718               }
5719
5720             else if (strncmp (name, ".sdata2", 7) == 0
5721                      || strncmp (name, ".sbss2", 6) == 0)
5722               {
5723                 reg = 2;
5724                 addend -= htab->sdata[1].sym_val;
5725               }
5726
5727             else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5728                      || strcmp (name, ".PPC.EMB.sbss0") == 0)
5729               {
5730                 reg = 0;
5731               }
5732
5733             else
5734               {
5735                 (*_bfd_error_handler)
5736                   (_("%B: the target (%s) of a %s relocation is "
5737                      "in the wrong output section (%s)"),
5738                    input_bfd,
5739                    sym_name,
5740                    howto->name,
5741                    name);
5742
5743                 bfd_set_error (bfd_error_bad_value);
5744                 ret = FALSE;
5745                 continue;
5746               }
5747
5748             if (r_type == R_PPC_EMB_SDA21)
5749               {                 /* fill in register field */
5750                 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5751                 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5752                 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5753               }
5754           }
5755           break;
5756
5757           /* Relocate against the beginning of the section.  */
5758         case R_PPC_SECTOFF:
5759         case R_PPC_SECTOFF_LO:
5760         case R_PPC_SECTOFF_HI:
5761         case R_PPC_SECTOFF_HA:
5762           BFD_ASSERT (sec != NULL);
5763           addend -= sec->output_section->vma;
5764           break;
5765
5766           /* Negative relocations.  */
5767         case R_PPC_EMB_NADDR32:
5768         case R_PPC_EMB_NADDR16:
5769         case R_PPC_EMB_NADDR16_LO:
5770         case R_PPC_EMB_NADDR16_HI:
5771         case R_PPC_EMB_NADDR16_HA:
5772           addend -= 2 * relocation;
5773           break;
5774
5775         case R_PPC_COPY:
5776         case R_PPC_GLOB_DAT:
5777         case R_PPC_JMP_SLOT:
5778         case R_PPC_RELATIVE:
5779         case R_PPC_PLT32:
5780         case R_PPC_PLTREL32:
5781         case R_PPC_PLT16_LO:
5782         case R_PPC_PLT16_HI:
5783         case R_PPC_PLT16_HA:
5784         case R_PPC_ADDR30:
5785         case R_PPC_EMB_RELSEC16:
5786         case R_PPC_EMB_RELST_LO:
5787         case R_PPC_EMB_RELST_HI:
5788         case R_PPC_EMB_RELST_HA:
5789         case R_PPC_EMB_BIT_FLD:
5790           (*_bfd_error_handler)
5791             (_("%B: relocation %s is not yet supported for symbol %s."),
5792              input_bfd,
5793              howto->name,
5794              sym_name);
5795
5796           bfd_set_error (bfd_error_invalid_operation);
5797           ret = FALSE;
5798           continue;
5799         }
5800
5801       /* Do any further special processing.  */
5802       switch (r_type)
5803         {
5804         default:
5805           break;
5806
5807         case R_PPC_ADDR16_HA:
5808         case R_PPC_GOT16_HA:
5809         case R_PPC_PLT16_HA:
5810         case R_PPC_SECTOFF_HA:
5811         case R_PPC_TPREL16_HA:
5812         case R_PPC_DTPREL16_HA:
5813         case R_PPC_GOT_TLSGD16_HA:
5814         case R_PPC_GOT_TLSLD16_HA:
5815         case R_PPC_GOT_TPREL16_HA:
5816         case R_PPC_GOT_DTPREL16_HA:
5817         case R_PPC_EMB_NADDR16_HA:
5818         case R_PPC_EMB_RELST_HA:
5819           /* It's just possible that this symbol is a weak symbol
5820              that's not actually defined anywhere.  In that case,
5821              'sec' would be NULL, and we should leave the symbol
5822              alone (it will be set to zero elsewhere in the link).  */
5823           if (sec != NULL)
5824             /* Add 0x10000 if sign bit in 0:15 is set.
5825                Bits 0:15 are not used.  */
5826             addend += 0x8000;
5827           break;
5828         }
5829
5830 #ifdef DEBUG
5831       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5832                "offset = %ld, addend = %ld\n",
5833                howto->name,
5834                (int) r_type,
5835                sym_name,
5836                r_symndx,
5837                (long) rel->r_offset,
5838                (long) addend);
5839 #endif
5840
5841       if (unresolved_reloc
5842           && !((input_section->flags & SEC_DEBUGGING) != 0
5843                && h->def_dynamic))
5844         {
5845           (*_bfd_error_handler)
5846             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5847              input_bfd,
5848              input_section,
5849              (long) rel->r_offset,
5850              howto->name,
5851              sym_name);
5852           ret = FALSE;
5853         }
5854
5855       r = _bfd_final_link_relocate (howto,
5856                                     input_bfd,
5857                                     input_section,
5858                                     contents,
5859                                     rel->r_offset,
5860                                     relocation,
5861                                     addend);
5862
5863       if (r != bfd_reloc_ok)
5864         {
5865           if (sym_name == NULL)
5866             sym_name = "(null)";
5867           if (r == bfd_reloc_overflow)
5868             {
5869               if (warned)
5870                 continue;
5871               if (h != NULL
5872                   && h->root.type == bfd_link_hash_undefweak
5873                   && howto->pc_relative)
5874                 {
5875                   /* Assume this is a call protected by other code that
5876                      detect the symbol is undefined.  If this is the case,
5877                      we can safely ignore the overflow.  If not, the
5878                      program is hosed anyway, and a little warning isn't
5879                      going to help.  */
5880
5881                   continue;
5882                 }
5883
5884               if (! (*info->callbacks->reloc_overflow) (info,
5885                                                         (h ? &h->root : NULL),
5886                                                         sym_name,
5887                                                         howto->name,
5888                                                         rel->r_addend,
5889                                                         input_bfd,
5890                                                         input_section,
5891                                                         rel->r_offset))
5892                 return FALSE;
5893             }
5894           else
5895             {
5896               (*_bfd_error_handler)
5897                 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
5898                  input_bfd, input_section,
5899                  (long) rel->r_offset, howto->name, sym_name, (int) r);
5900               ret = FALSE;
5901             }
5902         }
5903     }
5904
5905 #ifdef DEBUG
5906   fprintf (stderr, "\n");
5907 #endif
5908
5909   return ret;
5910 }
5911 \f
5912 /* Finish up dynamic symbol handling.  We set the contents of various
5913    dynamic sections here.  */
5914
5915 static bfd_boolean
5916 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
5917                                struct bfd_link_info *info,
5918                                struct elf_link_hash_entry *h,
5919                                Elf_Internal_Sym *sym)
5920 {
5921   struct ppc_elf_link_hash_table *htab;
5922
5923 #ifdef DEBUG
5924   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
5925            h->root.root.string);
5926 #endif
5927
5928   htab = ppc_elf_hash_table (info);
5929   BFD_ASSERT (htab->elf.dynobj != NULL);
5930
5931   if (h->plt.offset != (bfd_vma) -1)
5932     {
5933       Elf_Internal_Rela rela;
5934       bfd_byte *loc;
5935       bfd_vma reloc_index;
5936
5937 #ifdef DEBUG
5938       fprintf (stderr, ", plt_offset = %d", h->plt.offset);
5939 #endif
5940
5941       /* This symbol has an entry in the procedure linkage table.  Set
5942          it up.  */
5943
5944       BFD_ASSERT (h->dynindx != -1);
5945       BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
5946
5947       /* We don't need to fill in the .plt.  The ppc dynamic linker
5948          will fill it in.  */
5949
5950       /* Fill in the entry in the .rela.plt section.  */
5951       rela.r_offset = (htab->plt->output_section->vma
5952                        + htab->plt->output_offset
5953                        + h->plt.offset);
5954       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
5955       rela.r_addend = 0;
5956
5957       reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
5958       if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
5959         reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
5960       loc = (htab->relplt->contents
5961              + reloc_index * sizeof (Elf32_External_Rela));
5962       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5963
5964       if (!h->def_regular)
5965         {
5966           /* Mark the symbol as undefined, rather than as defined in
5967              the .plt section.  Leave the value alone.  */
5968           sym->st_shndx = SHN_UNDEF;
5969           /* If the symbol is weak, we do need to clear the value.
5970              Otherwise, the PLT entry would provide a definition for
5971              the symbol even if the symbol wasn't defined anywhere,
5972              and so the symbol would never be NULL.  */
5973           if (!h->ref_regular_nonweak)
5974             sym->st_value = 0;
5975         }
5976     }
5977
5978   if (h->needs_copy)
5979     {
5980       asection *s;
5981       Elf_Internal_Rela rela;
5982       bfd_byte *loc;
5983
5984       /* This symbols needs a copy reloc.  Set it up.  */
5985
5986 #ifdef DEBUG
5987       fprintf (stderr, ", copy");
5988 #endif
5989
5990       BFD_ASSERT (h->dynindx != -1);
5991
5992       if (h->size <= elf_gp_size (htab->elf.dynobj))
5993         s = htab->relsbss;
5994       else
5995         s = htab->relbss;
5996       BFD_ASSERT (s != NULL);
5997
5998       rela.r_offset = (h->root.u.def.value
5999                        + h->root.u.def.section->output_section->vma
6000                        + h->root.u.def.section->output_offset);
6001       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
6002       rela.r_addend = 0;
6003       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6004       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6005     }
6006
6007 #ifdef DEBUG
6008   fprintf (stderr, "\n");
6009 #endif
6010
6011   /* Mark some specially defined symbols as absolute.  */
6012   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6013       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
6014       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
6015     sym->st_shndx = SHN_ABS;
6016
6017   return TRUE;
6018 }
6019 \f
6020 static enum elf_reloc_type_class
6021 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
6022 {
6023   switch (ELF32_R_TYPE (rela->r_info))
6024     {
6025     case R_PPC_RELATIVE:
6026       return reloc_class_relative;
6027     case R_PPC_REL24:
6028     case R_PPC_ADDR24:
6029     case R_PPC_JMP_SLOT:
6030       return reloc_class_plt;
6031     case R_PPC_COPY:
6032       return reloc_class_copy;
6033     default:
6034       return reloc_class_normal;
6035     }
6036 }
6037 \f
6038 /* Finish up the dynamic sections.  */
6039
6040 static bfd_boolean
6041 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
6042                                  struct bfd_link_info *info)
6043 {
6044   asection *sdyn;
6045   struct ppc_elf_link_hash_table *htab;
6046
6047 #ifdef DEBUG
6048   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
6049 #endif
6050
6051   htab = ppc_elf_hash_table (info);
6052   sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
6053
6054   if (htab->elf.dynamic_sections_created)
6055     {
6056       Elf32_External_Dyn *dyncon, *dynconend;
6057
6058       BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
6059
6060       dyncon = (Elf32_External_Dyn *) sdyn->contents;
6061       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6062       for (; dyncon < dynconend; dyncon++)
6063         {
6064           Elf_Internal_Dyn dyn;
6065           asection *s;
6066
6067           bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
6068
6069           switch (dyn.d_tag)
6070             {
6071             case DT_PLTGOT:
6072               s = htab->plt;
6073               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6074               break;
6075
6076             case DT_PLTRELSZ:
6077               dyn.d_un.d_val = htab->relplt->size;
6078               break;
6079
6080             case DT_JMPREL:
6081               s = htab->relplt;
6082               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6083               break;
6084
6085             default:
6086               continue;
6087             }
6088
6089           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6090         }
6091     }
6092
6093   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
6094      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
6095   if (htab->got)
6096     {
6097       unsigned char *contents = htab->got->contents;
6098       bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
6099
6100       if (sdyn == NULL)
6101         bfd_put_32 (output_bfd, 0, contents + 4);
6102       else
6103         bfd_put_32 (output_bfd,
6104                     sdyn->output_section->vma + sdyn->output_offset,
6105                     contents + 4);
6106
6107       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
6108     }
6109
6110   return TRUE;
6111 }
6112 \f
6113 #define TARGET_LITTLE_SYM       bfd_elf32_powerpcle_vec
6114 #define TARGET_LITTLE_NAME      "elf32-powerpcle"
6115 #define TARGET_BIG_SYM          bfd_elf32_powerpc_vec
6116 #define TARGET_BIG_NAME         "elf32-powerpc"
6117 #define ELF_ARCH                bfd_arch_powerpc
6118 #define ELF_MACHINE_CODE        EM_PPC
6119 #ifdef __QNXTARGET__
6120 #define ELF_MAXPAGESIZE         0x1000
6121 #else
6122 #define ELF_MAXPAGESIZE         0x10000
6123 #endif
6124 #define ELF_MINPAGESIZE         0x1000
6125 #define elf_info_to_howto       ppc_elf_info_to_howto
6126
6127 #ifdef  EM_CYGNUS_POWERPC
6128 #define ELF_MACHINE_ALT1        EM_CYGNUS_POWERPC
6129 #endif
6130
6131 #ifdef EM_PPC_OLD
6132 #define ELF_MACHINE_ALT2        EM_PPC_OLD
6133 #endif
6134
6135 #define elf_backend_plt_not_loaded      1
6136 #define elf_backend_got_symbol_offset   4
6137 #define elf_backend_can_gc_sections     1
6138 #define elf_backend_can_refcount        1
6139 #define elf_backend_got_header_size     12
6140 #define elf_backend_rela_normal         1
6141
6142 #define bfd_elf32_mkobject                      ppc_elf_mkobject
6143 #define bfd_elf32_bfd_merge_private_bfd_data    ppc_elf_merge_private_bfd_data
6144 #define bfd_elf32_bfd_relax_section             ppc_elf_relax_section
6145 #define bfd_elf32_bfd_reloc_type_lookup         ppc_elf_reloc_type_lookup
6146 #define bfd_elf32_bfd_set_private_flags         ppc_elf_set_private_flags
6147 #define bfd_elf32_bfd_link_hash_table_create    ppc_elf_link_hash_table_create
6148
6149 #define elf_backend_object_p                    ppc_elf_object_p
6150 #define elf_backend_gc_mark_hook                ppc_elf_gc_mark_hook
6151 #define elf_backend_gc_sweep_hook               ppc_elf_gc_sweep_hook
6152 #define elf_backend_section_from_shdr           ppc_elf_section_from_shdr
6153 #define elf_backend_relocate_section            ppc_elf_relocate_section
6154 #define elf_backend_create_dynamic_sections     ppc_elf_create_dynamic_sections
6155 #define elf_backend_check_relocs                ppc_elf_check_relocs
6156 #define elf_backend_copy_indirect_symbol        ppc_elf_copy_indirect_symbol
6157 #define elf_backend_adjust_dynamic_symbol       ppc_elf_adjust_dynamic_symbol
6158 #define elf_backend_add_symbol_hook             ppc_elf_add_symbol_hook
6159 #define elf_backend_size_dynamic_sections       ppc_elf_size_dynamic_sections
6160 #define elf_backend_finish_dynamic_symbol       ppc_elf_finish_dynamic_symbol
6161 #define elf_backend_finish_dynamic_sections     ppc_elf_finish_dynamic_sections
6162 #define elf_backend_fake_sections               ppc_elf_fake_sections
6163 #define elf_backend_additional_program_headers  ppc_elf_additional_program_headers
6164 #define elf_backend_grok_prstatus               ppc_elf_grok_prstatus
6165 #define elf_backend_grok_psinfo                 ppc_elf_grok_psinfo
6166 #define elf_backend_reloc_type_class            ppc_elf_reloc_type_class
6167 #define elf_backend_begin_write_processing      ppc_elf_begin_write_processing
6168 #define elf_backend_final_write_processing      ppc_elf_final_write_processing
6169 #define elf_backend_write_section               ppc_elf_write_section
6170 #define elf_backend_special_sections            ppc_elf_special_sections
6171 #define elf_backend_plt_sym_val                 ppc_elf_plt_sym_val
6172
6173 #include "elf32-target.h"