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