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